Skip to content
Open
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 @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
--TEST--
opcache file cache: closure in attribute argument on promoted property (attribute compiled twice)
--EXTENSIONS--
opcache
--SKIPIF--
<?php
if (PHP_OS_FAMILY === 'Windows') die('skip FIXME');
if (!function_exists('opcache_is_script_cached_in_file_cache')) die('skip opcache.file_cache not available');
$dir = __DIR__ . '/file_cache_closure_attribute_promoted_property';
if (!@mkdir("$dir/cache", 0777, true)) die('skip cannot create cache directory');
?>
--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--
<?php
$dir = __DIR__ . '/file_cache_closure_attribute_promoted_property';
$cache = "$dir/cache";
$script = "$dir/attr.php";

// For a promoted property the attribute is compiled twice (parameter and property);
// both copies reference the same constant-expression AST that holds the closure's op_array.
file_put_contents($script, <<<'PHP'
<?php
class Callback
{
public function __construct(
#[\MyAttribute(static function (): string { return 'closure-from-cache'; })]
public array $data = [],
) {}
}
PHP);
$mtime = time() - 10;
touch($script, $mtime);

var_dump(opcache_compile_file($script));
var_dump(opcache_is_script_cached_in_file_cache($script));

// Replace the source: from now on the script can only be served from the file cache.
file_put_contents($script, '<?php throw new Error("script was recompiled instead of being loaded from the file cache");');
touch($script, $mtime);

$check = <<<'PHP'
$param = (new ReflectionParameter([Callback::class, '__construct'], 0))->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--
<?php
function removeDirRecursive(string $dir): void {
if (!is_dir($dir)) return;
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
);
foreach ($iterator as $fileinfo) {
$fileinfo->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
20 changes: 12 additions & 8 deletions ext/opcache/zend_file_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Loading