Skip to content

opcache_is_script_cached_in_file_cache(string $filename) #16979

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

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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: 1 addition & 1 deletion ext/opcache/opcache.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ 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(string $filename, bool $file_cache = false): bool {}
7 changes: 5 additions & 2 deletions ext/opcache/opcache_arginfo.h

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

27 changes: 27 additions & 0 deletions ext/opcache/tests/gt16979.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__, false)
);

var_dump(
opcache_is_script_cached(__FILE__, true)
);

?>
--EXPECT--
bool(true)
bool(true)
40 changes: 38 additions & 2 deletions ext/opcache/zend_accelerator_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
#include <time.h>

#include "php.h"
#include "zend.h"
#include "ZendAccelerator.h"
#include "zend_API.h"
#include "zend_closures.h"
#include "zend_shared_alloc.h"
#include "zend_accelerator_blacklist.h"
#include "zend_file_cache.h"
#include "php_ini.h"
#include "SAPI.h"
#include "zend_virtual_cwd.h"
Expand Down Expand Up @@ -363,6 +365,33 @@ static int filename_is_in_cache(zend_string *filename)
return 0;
}

static int filename_is_in_file_cache(zend_string *filename)
{
if (!ZCG(accel_directives).file_cache) {
return 0;
}

zend_string *realpath;

realpath = zend_resolve_path(filename);

if (!realpath) {
return 0;
}

zend_file_handle handle;

zend_stream_init_filename_ex(&handle, filename);
handle.opened_path = realpath;

bool result = zend_file_cache_script_validate(&handle);

zend_destroy_file_handle(&handle);

return result;
}


static int accel_file_in_cache(INTERNAL_FUNCTION_PARAMETERS)
{
if (ZEND_NUM_ARGS() == 1) {
Expand Down Expand Up @@ -983,9 +1012,12 @@ ZEND_FUNCTION(opcache_compile_file)
ZEND_FUNCTION(opcache_is_script_cached)
{
zend_string *script_name;
bool file_cache = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(script_name)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(file_cache)
ZEND_PARSE_PARAMETERS_END();

if (!validate_api_restriction()) {
Expand All @@ -996,5 +1028,9 @@ ZEND_FUNCTION(opcache_is_script_cached)
RETURN_FALSE;
}

RETURN_BOOL(filename_is_in_cache(script_name));
if (file_cache) {
RETURN_BOOL(filename_is_in_file_cache(script_name));
} else {
RETURN_BOOL(filename_is_in_cache(script_name));
}
}
88 changes: 88 additions & 0 deletions ext/opcache/zend_file_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,94 @@ static void zend_file_cache_unserialize(zend_persistent_script *script,
zend_file_cache_unserialize_early_bindings(script, buf);
}

// Function to validate the file cache and return a boolean result
bool zend_file_cache_script_validate(zend_file_handle *file_handle)
{
int fd;
char *filename;
zend_file_cache_metainfo info;
unsigned int actual_checksum;
void *mem, *checkpoint;

if (!file_handle || !file_handle->opened_path) {
return false;
}

filename = zend_file_cache_get_bin_file_path(file_handle->opened_path);
fd = zend_file_cache_open(filename, O_RDONLY | O_BINARY);
if (fd < 0) {
goto failure; // Open failed
}

if (zend_file_cache_flock(fd, LOCK_SH) != 0) {
goto failure_close; // Flock failed
}


if (read(fd, &info, sizeof(info)) != sizeof(info)) {
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (info)\n", filename);
goto failure_unlock_close;
}

// Verify header
if (memcmp(info.magic, "OPCACHE", 8) != 0 || memcmp(info.system_id, zend_system_id, 32) != 0) {
zend_accel_error(ACCEL_LOG_WARNING, "opcache invalid header in file '%s'\n", filename);
goto failure_unlock_close;
}

// Verify timestamp if required
if (ZCG(accel_directives).validate_timestamps &&
zend_get_file_handle_timestamp(file_handle, NULL) != info.timestamp) {
goto failure_unlock_close;
}

checkpoint = zend_arena_checkpoint(CG(arena));
#if defined(__AVX__) || defined(__SSE2__)
/* Align to 64-byte boundary */
mem = zend_arena_alloc(&CG(arena), info.mem_size + info.str_size + 64);
mem = (void*)(((uintptr_t)mem + 63L) & ~63L);
#else
mem = zend_arena_alloc(&CG(arena), info.mem_size + info.str_size);
#endif

if (read(fd, mem, info.mem_size + info.str_size) != (ssize_t)(info.mem_size + info.str_size)) {
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (mem)\n", filename);
zend_arena_release(&CG(arena), checkpoint);
goto failure_unlock_close;
}
if (zend_file_cache_flock(fd, LOCK_UN) != 0) {
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s'\n", filename);
}
close(fd);

/* verify checksum */
if (ZCG(accel_directives).file_cache_consistency_checks &&
(actual_checksum = zend_adler32(ADLER32_INIT, mem, info.mem_size + info.str_size)) != info.checksum) {
zend_accel_error(ACCEL_LOG_WARNING, "corrupted file '%s' excepted checksum: 0x%08x actual checksum: 0x%08x\n", filename, info.checksum, actual_checksum);
zend_arena_release(&CG(arena), checkpoint);
goto failure;
}

zend_arena_release(&CG(arena), checkpoint);
zend_file_cache_flock(fd, LOCK_UN);
close(fd);
efree(filename);
return true; // Validation successful

failure_unlock_close:
zend_file_cache_flock(fd, LOCK_UN);
failure_close:
close(fd);
failure:
if (!ZCG(accel_directives).file_cache_read_only) {
zend_file_cache_unlink(filename);
}
efree(filename);

return false; // Validation failed
}


zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle)
{
zend_string *full_path = file_handle->opened_path;
Expand Down
1 change: 1 addition & 0 deletions ext/opcache/zend_file_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define ZEND_FILE_CACHE_H

int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm);
bool zend_file_cache_script_validate(zend_file_handle *file_handle);
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle);
void zend_file_cache_invalidate(zend_string *full_path);

Expand Down
Loading