8000 Zend: Fix reference counting for Closures in const-expr by TimWolla · Pull Request #17853 · php/php-src · GitHub
[go: up one dir, main page]

Skip to content

Zend: Fix reference counting for Closures in const-expr #17853

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

Merged
merged 2 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Zend: Fix reference counting for Closures in const-expr
Fixes #17851
  • Loading branch information
TimWolla committed Mar 27, 2025
commit 2365ff74ff816326de2be67ddd785d452655aaf4
4 changes: 2 additions & 2 deletions Zend/tests/closures/closure_const_expr/attributes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ reflection
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Attr {
public function __construct(public Closure $value) {
$value('foo');
$value('foo');
}
}

#[Attr(static function () { })]
#[Attr(static function (...$args) {
var_dump($args);
var_dump($args);
})]
class C {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

#[Attr(static function () { })]
#[Attr(static function (...$args) {
var_dump($args);
})]
class C {}

?>
57 changes: 57 additions & 0 deletions Zend/tests/closures/closure_const_expr/attributes_autoload.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--TEST--
GH-17851: Use-after-free when instantiating autoloaded class with attribute having a Closure parameter.
--EXTENSIONS--
reflection
--FILE--
<?php

spl_autoload_register(static function ($className) {
if ($className === 'C') {
require(__DIR__ . '/attributes_autoload.inc');
}
});

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Attr {
public function __construct(public Closure $value) {
$value('foo');
}
}

foreach ((new ReflectionClass(C::class))->getAttributes() as $reflectionAttribute) {
var_dump($reflectionAttribute->newInstance());
}

?>
--EXPECTF--
object(Attr)#%d (1) {
["value"]=>
object(Closure)#%d (3) {
["name"]=>
string(%d) "{closure:%s:%d}"
["file"]=>
string(%d) "%s"
["line"]=>
int(%d)
}
}
array(1) {
[0]=>
string(3) "foo"
}
object(Attr)#%d (1) {
["value"]=>
object(Closure)#%d (4) {
["name"]=>
string(%d) "{closure:%s:%d}"
["file"]=>
string(%d) "%s"
["line"]=>
int(%d)
["parameter"]=>
array(1) {
["$args"]=>
string(10) "<optional>"
}
}
}
77 changes: 77 additions & 0 deletions Zend/tests/closures/closure_const_expr/static_variable.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
--TEST--
Closures in const expressions support static variables.
--FILE--
<?php

const Closure = static function () {
static $x = [];
static $i = 1;
$i *= 2;
$x[] = $i;
var_dump($x);
};

var_dump(Closure);
(Closure)();
(Closure)();
(Closure)();
var_dump(Closure);

?>
--EXPECTF--
object(Closure)#%d (4) {
["name"]=>
string(%d) "{closure:%s:%d}"
["file"]=>
string(%d) "%s"
["line"]=>
int(3)
["static"]=>
array(2) {
["x"]=>
array(0) {
}
["i"]=>
int(1)
}
}
array(1) {
[0]=>
int(2)
}
array(2) {
[0]=>
int(2)
[1]=>
int(4)
}
array(3) {
[0]=>
int(2)
[1]=>
int(4)
[2]=>
int(8)
}
object(Closure)#%d (4) {
["name"]=>
string(%d) "{closure:%s:%d}"
["file"]=>
string(%d) "%s"
["line"]=>
int(3)
["static"]=>
array(2) {
["x"]=>
array(3) {
[0]=>
int(2)
[1]=>
int(4)
[2]=>
int(8)
}
["i"]=>
int(8)
}
}
3 changes: 2 additions & 1 deletion Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@ static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf)
new->attr = old->attr;
new->lineno = old->lineno;
new->op_array = old->op_array;
function_add_ref((zend_function *)new->op_array);
buf = (void*)((char*)buf + sizeof(zend_ast_op_array));
} else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
zend_ast_fcc *old = (zend_ast_fcc*)ast;
Expand Down Expand Up @@ -1353,7 +1354,7 @@ ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
} else if (EXPECTED(ast->kind == ZEND_AST_CONSTANT)) {
zend_string_release_ex(zend_ast_get_constant_name(ast), 0);
} else if (EXPECTED(ast->kind == ZEND_AST_OP_ARRAY)) {
/* Nothing to do. */
destroy_op_array(zend_ast_get_op_array(ast)->op_array);
} else if (EXPECTED(zend_ast_is_decl(ast))) {
zend_ast_decl *decl = (zend_ast_decl *) ast;

Expand Down
4 changes: 1 addition & 3 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8244,9 +8244,6 @@ static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array,

zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
switch (level) {
case FUNC_DECL_LEVEL_CONSTEXPR:
zend_add_dynamic_func_def(op_array);
break;
case FUNC_DECL_LEVEL_NESTED: {
uint32_t func_ref = zend_add_dynamic_func_def(op_array);
if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
Expand All @@ -8261,6 +8258,7 @@ static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array,
}
break;
}
case FUNC_DECL_LEVEL_CONSTEXPR:
case FUNC_DECL_LEVEL_TOPLEVEL:
/* Nothing to do. */
break;
Expand Down
21 changes: 17 additions & 4 deletions ext/opcache/zend_file_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ static void zend_file_cache_unserialize_zval(zval *zv,
zend_persistent_script *script,
void *buf);

static void zend_file_cache_serialize_func(zval *zv,
zend_persistent_script *script,
zend_file_cache_metainfo *info,
void *buf);

static void zend_file_cache_unserialize_func(zval *zv,
zend_persistent_script *script,
void *buf);

static void *zend_file_cache_serialize_interned(zend_string *str,
zend_file_cache_metainfo *info)
{
Expand Down Expand Up @@ -364,8 +373,10 @@ static void zend_file_cache_serialize_ast(zend_ast *ast,
}
}
} else if (ast->kind == ZEND_AST_OP_ARRAY) {
/* The op_array itself will be serialized as part of the dynamic_func_defs. */
SERIALIZE_PTR(zend_ast_get_op_array(ast)->op_array);
zval z;
ZVAL_PTR(&z, zend_ast_get_op_array(ast)->op_array);
zend_file_cache_serialize_func(&z, script, info, buf);
zend_ast_get_op_array(ast)->op_array = Z_PTR(z);
} else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
zend_ast_fcc *fcc = (zend_ast_fcc*)ast;
ZEND_MAP_PTR_INIT(fcc->fptr, NULL);
Expand Down Expand Up @@ -1252,8 +1263,10 @@ static void zend_file_cache_unserialize_ast(zend_ast *ast,
}
}
} else if (ast->kind == ZEND_AST_OP_ARRAY) {
/* The op_array itself will be unserialized as part of the dynamic_func_defs. */
UNSERIALIZE_PTR(zend_ast_get_op_array(ast)->op_array);
zval z;
ZVAL_PTR(&z, zend_ast_get_op_array(ast)->op_array);
zend_file_cache_unserialize_func(&z, script, buf);
zend_ast_get_op_array(ast)->op_array = Z_PTR(z);
} else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
zend_ast_fcc *fcc = (zend_ast_fcc*)ast;
ZEND_MAP_PTR_NEW(fcc->fptr);
Expand Down
Loading
0