diff --git a/NEWS b/NEWS index d3e2d045a55e..9d58ee4ed877 100644 --- a/NEWS +++ b/NEWS @@ -158,6 +158,8 @@ PHP NEWS (Brian Marks) . Fixed function JIT ASSIGN_DIM reporting an undefined value or key out of order, or when the VM does not, on non-array containers. (Jorg Sowa) + . Fixed segfault when storing a script with a closure in an attribute on a + promoted property in the file cache. (Evgenij Efimov) . Fix JIT register allocation for ASSIGN to traced reference. (ndossche) . Fixed a double free when a destructor throws while freeing a temporary whose result the optimizer discarded. (Ilia Alshanetsky) diff --git a/ext/opcache/tests/file_cache_closure_attribute_promoted_property.phpt b/ext/opcache/tests/file_cache_closure_attribute_promoted_property.phpt new file mode 100644 index 000000000000..375397627084 --- /dev/null +++ b/ext/opcache/tests/file_cache_closure_attribute_promoted_property.phpt @@ -0,0 +1,104 @@ +--TEST-- +opcache file cache: closure in attribute argument on promoted property (attribute compiled twice) +--EXTENSIONS-- +opcache +--SKIPIF-- + +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.jit=disable +opcache.file_cache="{PWD}/file_cache_closure_attribute_promoted_property/cache" +opcache.file_update_protection=0 +--FILE-- +getAttributes()[0]->getArguments()[0]; +$prop = (new ReflectionProperty(Callback::class, 'data'))->getAttributes()[0]->getArguments()[0]; +echo "param: ", $param(), "\n"; +echo "prop: ", $prop(), "\n"; +PHP; + +// Same process: opcache_compile_file() has already declared the class, evaluate the attribute +// arguments of the in-memory copy that was just serialized. +echo "in-process:\n"; +eval($check); + +// Fresh process. +$cmd = [PHP_BINARY, '-n']; +$ext = ini_get('extension_dir'); +if (file_exists($ext . DIRECTORY_SEPARATOR . 'opcache.so')) { + array_push($cmd, '-d', 'zend_extension=opcache'); +} +array_push($cmd, + '-d', 'opcache.enable_cli=1', + '-d', 'opcache.file_cache="' . $cache . '"', + '-d', 'opcache.file_cache_only=1', + '-d', 'opcache.file_update_protection=0', + '-r', 'require $argv[1]; ' . $check, + $script, +); +$proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); +echo "child process:\n", stream_get_contents($pipes[1]); +fclose($pipes[1]); +$stderr = stream_get_contents($pipes[2]); +fclose($pipes[2]); +echo "child exit code: ", proc_close($proc), "\n"; +if ($stderr !== '') { echo "stderr: $stderr"; } +?> +--CLEAN-- +isDir() ? rmdir($fileinfo->getPathname()) : unlink($fileinfo->getPathname()); + } + rmdir($dir); +} +removeDirRecursive(__DIR__ . '/file_cache_closure_attribute_promoted_property'); +?> +--EXPECT-- +bool(true) +bool(true) +in-process: +param: closure-from-cache +prop: closure-from-cache +child process: +param: closure-from-cache +prop: closure-from-cache +child exit code: 0 diff --git a/ext/opcache/zend_file_cache.c b/ext/opcache/zend_file_cache.c index 2f969c55f9d5..6181774eb5d4 100644 --- a/ext/opcache/zend_file_cache.c +++ b/ext/opcache/zend_file_cache.c @@ -377,10 +377,12 @@ static void zend_file_cache_serialize_ast(zend_ast *ast, } } } else if (ast->kind == ZEND_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); + if (!IS_SERIALIZED(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); @@ -1282,10 +1284,12 @@ static void zend_file_cache_unserialize_ast(zend_ast *ast, } } } else if (ast->kind == ZEND_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); + if (!IS_UNSERIALIZED(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; if (!script->corrupted) {