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
72 changes: 72 additions & 0 deletions ext/opcache/tests/gh25555.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
--TEST--
GH-25555: Scripts loaded from file_cache into SHM report memory_consumption 0 when stored without SHM
--EXTENSIONS--
opcache
--FILE--
<?php
$cache = __DIR__ . DIRECTORY_SEPARATOR . 'gh25555_cache';
$script = $cache . DIRECTORY_SEPARATOR . 'target.php';
mkdir($cache);
file_put_contents($script, <<<'PHP'
<?php
function gh25555_test(): int {
return 42;
}
PHP);

function run_child(string $cache, string $script, bool $file_cache_only): void {
$ext = ini_get('extension_dir');
$opcache = PHP_OS_FAMILY === 'Windows' ? 'php_opcache.dll' : 'opcache.so';
$cmd = [PHP_BINARY, '-n', '-d', 'extension_dir=' . $ext];
if (file_exists($ext . DIRECTORY_SEPARATOR . $opcache)) {
array_push($cmd, '-d', 'zend_extension=opcache');
}
$code = sprintf(
'opcache_compile_file(%s);'
. ' if (!%s) {'
. ' $s = opcache_get_status(true);'
. ' $mem = $s["scripts"][%s]["memory_consumption"];'
. ' $w0 = $s["memory_usage"]["wasted_memory"];'
. ' opcache_invalidate(%s, true);'
. ' $w1 = opcache_get_status(false)["memory_usage"]["wasted_memory"];'
. ' var_dump($mem > 0, $w1 - $w0 === $mem);'
. ' }',
var_export($script, true),
$file_cache_only ? 'true' : 'false',
var_export($script, true),
var_export($script, true),
);
array_push($cmd,
'-d', 'opcache.enable=1',
'-d', 'opcache.enable_cli=1',
'-d', 'opcache.jit=0',
'-d', 'opcache.file_cache=' . $cache,
'-d', 'opcache.file_cache_only=' . ($file_cache_only ? '1' : '0'),
'-d', 'opcache.file_update_protection=0',
'-r', $code,
);
$proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
echo stream_get_contents($pipes[1]);
proc_close($proc);
}

run_child($cache, $script, true);
run_child($cache, $script, false);
?>
--CLEAN--
<?php
$cache = __DIR__ . DIRECTORY_SEPARATOR . 'gh25555_cache';
if (is_dir($cache)) {
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($cache, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($it as $file) {
$file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname());
}
rmdir($cache);
}
?>
--EXPECT--
bool(true)
bool(true)
1 change: 1 addition & 0 deletions ext/opcache/zend_file_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,7 @@ zend_persistent_script *zend_file_cache_script_load_ex(zend_file_handle *file_ha
ZCSG(map_ptr_last) = CG(map_ptr_last);
zend_accel_pending_ce_cache_commit();
script->dynamic_members.last_used = ZCG(request_time);
script->dynamic_members.memory_consumption = ZEND_ALIGNED_SIZE(script->size);

zend_accel_hash_update(&ZCSG(hash), script->script.filename, 0, script);

Expand Down
Loading