Skip to content
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

opcache_is_script_cached_in_file_cache(string $filename) #16979

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ static zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int

HANDLE_BLOCK_INTERRUPTIONS();
SHM_UNPROTECT();
persistent_script = zend_file_cache_script_load(file_handle);
persistent_script = zend_file_cache_script_load(file_handle, false);
SHM_PROTECT();
HANDLE_UNBLOCK_INTERRUPTIONS();
if (persistent_script) {
Expand Down Expand Up @@ -2134,7 +2134,7 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)

/* Check the second level cache */
if (!persistent_script && ZCG(accel_directives).file_cache) {
persistent_script = zend_file_cache_script_load(file_handle);
persistent_script = zend_file_cache_script_load(file_handle, false);
}

/* If script was not found or invalidated by validate_timestamps */
Expand Down
2 changes: 2 additions & 0 deletions ext/opcache/opcache.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ function opcache_jit_blacklist(Closure $closure): void {}
function opcache_get_configuration(): array|false {}

function opcache_is_script_cached(string $filename): bool {}

function opcache_is_script_cached_in_file_cache(string $filename): bool {}
6 changes: 5 additions & 1 deletion ext/opcache/opcache_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions ext/opcache/tests/gh16551_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
GH-16551: opcache file cache read only: generate file cache
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=disable
opcache.jit_buffer_size=0
opcache.file_cache="{TMP}"
--EXTENSIONS--
opcache
--CONFLICTS--
opcache_file_cache
--FILE--
<?php

$file = __DIR__ . '/gh16551_998.inc';
$uncached_file = __DIR__ . '/gh16551_999.inc';

opcache_compile_file($file);

var_dump(
opcache_is_script_cached($file)
);

var_dump(
opcache_is_script_cached_in_file_cache($file)
);

var_dump(
opcache_is_script_cached($uncached_file)
);

var_dump(
opcache_is_script_cached_in_file_cache($uncached_file)
);

?>
--EXPECT--
bool(true)
bool(true)
bool(false)
bool(false)
37 changes: 37 additions & 0 deletions ext/opcache/tests/gh16551_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
GH-16551: opcache file cache read only: read file cache
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=disable
opcache.jit_buffer_size=0
opcache.file_cache="{TMP}"
opcache.file_cache_read_only=1
--EXTENSIONS--
opcache
--CONFLICTS--
opcache_file_cache
--FILE--
<?php

$file = __DIR__ . '/gh16551_998.inc';
$uncached_file = __DIR__ . '/gh16551_999.inc';

var_dump(
opcache_is_script_cached_in_file_cache($file)
Copy link
Member

Choose a reason for hiding this comment

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

These tests look problematic to me. They will fail on first execution (confirmed locally).

========DIFF========
001- bool(true)
001+ bool(false)
     9
     bool(false)
     8
========DONE========
FAIL GH-16551: opcache file cache read only: read file cache with limited checks [ext/opcache/tests/gh16551_003.phpt]

========DIFF========
001- bool(true)
002- bool(true)
001+ bool(false)
002+ bool(false)
     9
========DONE========
FAIL GH-16551: opcache file cache read only: ensure we can't invalidate [ext/opcache/tests/gh16551_004.phpt]

I don't understand why they don't fail on CI. It's also possible they are order dependent, i.e. one job will prime the cache, another will check that the cache is not primed.

One solution might be to set opcache.file_cache to some sub-directory and clean it in the --CLEAN-- section to ensure a consistent state.

);

require $file;

var_dump(
opcache_is_script_cached_in_file_cache($uncached_file)
);

require $uncached_file;

?>
--EXPECT--
bool(true)
9
bool(false)
8
41 changes: 41 additions & 0 deletions ext/opcache/tests/gh16551_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
GH-16551: opcache file cache read only: read file cache with limited checks
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=disable
opcache.jit_buffer_size=0
opcache.file_cache="{TMP}"
opcache.file_cache_read_only=1
opcache.revalidate_freq=0
opcache.enable_file_override=true
opcache.validate_timestamps=false
opcache.file_cache_consistency_checks=false
--EXTENSIONS--
opcache
--CONFLICTS--
opcache_file_cache
--FILE--
<?php

$file = __DIR__ . '/gh16551_998.inc';
$uncached_file = __DIR__ . '/gh16551_999.inc';

var_dump(
opcache_is_script_cached_in_file_cache($file)
);

require $file;

var_dump(
opcache_is_script_cached_in_file_cache($uncached_file)
);

require $uncached_file;

?>
--EXPECT--
bool(true)
9
bool(false)
8
36 changes: 36 additions & 0 deletions ext/opcache/tests/gh16551_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
GH-16551: opcache file cache read only: ensure we can't invalidate
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=disable
opcache.jit_buffer_size=0
opcache.file_cache="{TMP}"
opcache.file_cache_read_only=1
--EXTENSIONS--
opcache
--CONFLICTS--
opcache_file_cache
--FILE--
<?php

$file = __DIR__ . '/gh16551_998.inc';

var_dump(
opcache_is_script_cached_in_file_cache($file)
);

// invalidate SHM, but silently ignore file cache.
opcache_invalidate($file, true);

var_dump(
opcache_is_script_cached_in_file_cache($file)
);

require $file;

?>
--EXPECT--
bool(true)
bool(true)
9
32 changes: 32 additions & 0 deletions ext/opcache/tests/gh16551_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-16551: opcache file cache read only: ensure cache files aren't created
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=disable
opcache.jit_buffer_size=0
opcache.file_cache="{TMP}"
opcache.file_cache_read_only=1
--EXTENSIONS--
opcache
--CONFLICTS--
opcache_file_cache
--FILE--
<?php

$uncached_file = __DIR__ . '/gh16551_999.inc';

opcache_compile_file($uncached_file);

var_dump(
opcache_is_script_cached($uncached_file)
);

var_dump(
opcache_is_script_cached_in_file_cache($uncached_file)
);

?>
--EXPECT--
bool(true)
bool(false)
70 changes: 70 additions & 0 deletions ext/opcache/tests/gh16551_099.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
--TEST--
GH-16551: opcache file cache read only: double check file_cache_only
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=disable
opcache.jit_buffer_size=0
opcache.file_cache="{PWD}"
opcache.file_cache_only=1
--EXTENSIONS--
opcache
--CONFLICTS--
opcache_file_cache
--FILE--
<?php

$uncached_file = __DIR__ . '/gh16551_999.inc';

var_dump(
opcache_is_script_cached($uncached_file)
);

var_dump(
opcache_is_script_cached_in_file_cache($uncached_file)
);

opcache_compile_file($uncached_file);

var_dump(
opcache_is_script_cached($uncached_file)
);

// check the cache file itself exists...
if (substr(PHP_OS, 0, 3) !== 'WIN') {
$pattern = __DIR__ . '/*/' . __DIR__ . '/*16551_999.inc.bin';
} else {
$pattern = __DIR__ . '/*/*/' . str_replace(':', '', __DIR__) . '/*16551_999.inc.bin';
}
foreach (glob($pattern) as $p) {
var_dump($p !== false);
}

// check it is reported as existing...
var_dump(
opcache_is_script_cached_in_file_cache($uncached_file)
);

?>
--CLEAN--
<?php
if (substr(PHP_OS, 0, 3) !== 'WIN') {
$pattern = __DIR__ . '/*/' . __DIR__ . '/*16551*.bin';
} else {
$pattern = __DIR__ . '/*/*/' . str_replace(':', '', __DIR__) . '/*16551*.bin';
}
foreach (glob($pattern) as $p) {
unlink($p);
$p = dirname($p);
while(strlen($p) > strlen(__DIR__)) {
@rmdir($p);
$p = dirname($p);
}
}
?>
--EXPECT--
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
5 changes: 5 additions & 0 deletions ext/opcache/tests/gh16551_998.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$a = 4+5;

echo $a . "\n";
5 changes: 5 additions & 0 deletions ext/opcache/tests/gh16551_999.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$a = 3+5;

echo $a . "\n";
27 changes: 27 additions & 0 deletions ext/opcache/tests/gh16979_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-16979: opcache_is_script_cached support file_cache argument
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=disable
opcache.jit_buffer_size=0
opcache.file_cache="{TMP}"
--EXTENSIONS--
opcache
--FILE--
<?php

opcache_compile_file(__FILE__);

var_dump(
opcache_is_script_cached(__FILE__)
);

var_dump(
opcache_is_script_cached_in_file_cache(__FILE__)
);

?>
--EXPECT--
bool(true)
bool(true)
Loading