Skip to content

Fix use after free during shutdown destruction #18834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ PHP NEWS
?? ??? ????, PHP 8.3.23

- Core:
. Fixed GH-18833 (use after free during destruction).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for self: move this entry

(Daniil Gentili)
. Fixed GH-18695 (zend_ast_export() - float number is not preserved).
(Oleg Efimov)

Expand Down
24 changes: 24 additions & 0 deletions Zend/tests/gh18833.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Bug #18833 (Use after free with weakmaps dependent on destruction order)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: For bugs on github, it's supposed to be GH-18833 instead of a pound sign. I'll fix this in the merge

--FILE--
<?php

class a {
public static WeakMap $map;
public static Generator $storage;
}

a::$map = new WeakMap;

$closure = function () {
$obj = new a;
a::$map[$obj] = true;
yield $obj;
};
a::$storage = $closure();
a::$storage->current();

echo "ok\n";
?>
--EXPECT--
ok
4 changes: 3 additions & 1 deletion Zend/zend_objects_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ ZEND_API void ZEND_FASTCALL zend_objects_store_free_object_storage(zend_objects_
if (IS_OBJ_VALID(obj)) {
if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
if (obj->handlers->free_obj != zend_object_std_dtor) {
if (obj->handlers->free_obj != zend_object_std_dtor
|| (OBJ_FLAGS(obj) & IS_OBJ_WEAKLY_REFERENCED)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codestyle nit:

Suggested change
|| (OBJ_FLAGS(obj) & IS_OBJ_WEAKLY_REFERENCED)
|| (OBJ_FLAGS(obj) & IS_OBJ_WEAKLY_REFERENCED)

I'll fix it in the merge

) {
GC_ADDREF(obj);
obj->handlers->free_obj(obj);
}
Expand Down
Loading