diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 4892b1062c7..43f8bd3e172 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -4845,6 +4845,8 @@ static zend_result accel_finish_startup_preload(bool in_child) bool old_reset_signals = SIGG(reset); #endif + ZCG(preloading) = true; + sapi_module.activate = NULL; sapi_module.deactivate = NULL; sapi_module.register_server_variables = NULL; @@ -4926,6 +4928,8 @@ static zend_result accel_finish_startup_preload(bool in_child) sapi_module.ub_write = orig_ub_write; sapi_module.flush = orig_flush; + ZCG(preloading) = false; + sapi_activate(); return ret; diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h index 0766996cbb1..524a6f5e121 100644 --- a/ext/opcache/ZendAccelerator.h +++ b/ext/opcache/ZendAccelerator.h @@ -220,6 +220,7 @@ typedef struct _zend_accel_globals { #endif void *preloaded_internal_run_time_cache; size_t preloaded_internal_run_time_cache_size; + bool preloading; /* preallocated shared-memory block to save current script */ void *mem; zend_persistent_script *current_persistent_script; diff --git a/ext/opcache/config.m4 b/ext/opcache/config.m4 index e9ce51bbd22..67df53d90d5 100644 --- a/ext/opcache/config.m4 +++ b/ext/opcache/config.m4 @@ -325,6 +325,7 @@ PHP_NEW_EXTENSION([opcache], m4_normalize([ shared_alloc_mmap.c shared_alloc_posix.c shared_alloc_shm.c + zend_accelerator_api.c zend_accelerator_blacklist.c zend_accelerator_debug.c zend_accelerator_hash.c @@ -361,3 +362,5 @@ AS_VAR_IF([PHP_OPCACHE_JIT], [yes], [ ]) PHP_ADD_MAKEFILE_FRAGMENT([$ext_srcdir/jit/Makefile.frag]) ]) + +PHP_INSTALL_HEADERS([ext/opcache], [zend_accelerator_api.h]) diff --git a/ext/opcache/config.w32 b/ext/opcache/config.w32 index 93c5319894b..9b8f2a7e510 100644 --- a/ext/opcache/config.w32 +++ b/ext/opcache/config.w32 @@ -4,6 +4,7 @@ PHP_OPCACHE="yes"; ZEND_EXTENSION('opcache', "\ ZendAccelerator.c \ + zend_accelerator_api.c \ zend_accelerator_blacklist.c \ zend_accelerator_debug.c \ zend_accelerator_hash.c \ @@ -68,3 +69,5 @@ if (PHP_OPCACHE_JIT == "yes") { } ADD_FLAG('CFLAGS_OPCACHE', "/I " + configure_module_dirname); + +PHP_INSTALL_HEADERS("ext/opcache", "zend_accelerator_api.h"); diff --git a/ext/opcache/tests/api/opcache_preloading.inc b/ext/opcache/tests/api/opcache_preloading.inc new file mode 100644 index 00000000000..0466028a370 --- /dev/null +++ b/ext/opcache/tests/api/opcache_preloading.inc @@ -0,0 +1,3 @@ + diff --git a/ext/opcache/tests/api/opcache_preloading_001.phpt b/ext/opcache/tests/api/opcache_preloading_001.phpt new file mode 100644 index 00000000000..4ecbc3b929d --- /dev/null +++ b/ext/opcache/tests/api/opcache_preloading_001.phpt @@ -0,0 +1,19 @@ +--TEST-- +opcache_preloading() api 001 +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.preload={PWD}/opcache_preloading.inc +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +%sopcache_preloading.inc: 1 +%sopcache_preloading_001.php: 0 diff --git a/ext/opcache/tests/api/opcache_preloading_002.phpt b/ext/opcache/tests/api/opcache_preloading_002.phpt new file mode 100644 index 00000000000..a664b0d9233 --- /dev/null +++ b/ext/opcache/tests/api/opcache_preloading_002.phpt @@ -0,0 +1,23 @@ +--TEST-- +opcache_preloading() api 002 +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.preload={PWD}/opcache_preloading.inc +opcache.preload_user={ENV:TEST_NON_ROOT_USER} +--EXTENSIONS-- +posix +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +%sopcache_preloading.inc: 1 +%sopcache_preloading_002.php: 0 diff --git a/ext/opcache/zend_accelerator_api.c b/ext/opcache/zend_accelerator_api.c new file mode 100644 index 00000000000..27267e8791a --- /dev/null +++ b/ext/opcache/zend_accelerator_api.c @@ -0,0 +1,23 @@ +/* + +----------------------------------------------------------------------+ + | Zend OPcache | + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ +*/ + +#include "zend_accelerator_api.h" +#include "ZendAccelerator.h" + +ZEND_API bool opcache_preloading(void) +{ + return ZCG(preloading); +} diff --git a/ext/opcache/zend_accelerator_api.h b/ext/opcache/zend_accelerator_api.h new file mode 100644 index 00000000000..4ea42f29d17 --- /dev/null +++ b/ext/opcache/zend_accelerator_api.h @@ -0,0 +1,29 @@ +/* + +----------------------------------------------------------------------+ + | Zend OPcache | + +----------------------------------------------------------------------+ + | Copyright (c) The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | https://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ +*/ + +#ifndef ZEND_ACCELERATOR_API_H +#define ZEND_ACCELERATOR_API_H + +#include "Zend/zend_portability.h" + +BEGIN_EXTERN_C() + +/* Returns true during preloading */ +ZEND_API bool opcache_preloading(void); + +END_EXTERN_C() + +#endif /* ZEND_ACCELERATOR_API_H */ diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index bbf9d68fb21..255b0a7f994 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -14,6 +14,8 @@ +----------------------------------------------------------------------+ */ +#include "ext/opcache/zend_accelerator_api.h" +#include "zend_API.h" #include "zend_modules.h" #include "zend_types.h" #ifdef HAVE_CONFIG_H @@ -1645,3 +1647,10 @@ static PHP_FUNCTION(zend_test_gh18756) zend_mm_gc(heap); zend_mm_shutdown(heap, true, false); } + +static PHP_FUNCTION(zend_test_opcache_preloading) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + RETURN_BOOL(opcache_preloading()); +} diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 88ef73786cd..200ab22e794 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -338,6 +338,8 @@ function zend_test_override_libxml_global_state(): void {} function zend_test_compile_to_ast(string $str): string {} function zend_test_gh18756(): void {} + + function zend_test_opcache_preloading(): bool {} } namespace ZendTestNS { diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index ac04ba20950..bf3d626088f 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 073039fa0d9c41eb842f6944eb4acfc9217103cf */ + * Stub hash: 781677c7ada9095af9c964cf86ce6ba63a52a930 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0) ZEND_END_ARG_INFO() @@ -185,6 +185,8 @@ ZEND_END_ARG_INFO() #define arginfo_zend_test_gh18756 arginfo_zend_test_void_return +#define arginfo_zend_test_opcache_preloading arginfo_zend_test_is_pcre_bundled + #define arginfo_ZendTestNS2_namespaced_func arginfo_zend_test_is_pcre_bundled #define arginfo_ZendTestNS2_namespaced_deprecated_func arginfo_zend_test_void_return @@ -325,6 +327,7 @@ static ZEND_FUNCTION(zend_test_is_zend_ptr); static ZEND_FUNCTION(zend_test_log_err_debug); static ZEND_FUNCTION(zend_test_compile_to_ast); static ZEND_FUNCTION(zend_test_gh18756); +static ZEND_FUNCTION(zend_test_opcache_preloading); static ZEND_FUNCTION(ZendTestNS2_namespaced_func); static ZEND_FUNCTION(ZendTestNS2_namespaced_deprecated_func); static ZEND_FUNCTION(ZendTestNS2_ZendSubNS_namespaced_func); @@ -452,6 +455,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(zend_test_log_err_debug, arginfo_zend_test_log_err_debug) ZEND_FE(zend_test_compile_to_ast, arginfo_zend_test_compile_to_ast) ZEND_FE(zend_test_gh18756, arginfo_zend_test_gh18756) + ZEND_FE(zend_test_opcache_preloading, arginfo_zend_test_opcache_preloading) #if (PHP_VERSION_ID >= 80400) ZEND_RAW_FENTRY(ZEND_NS_NAME("ZendTestNS2", "namespaced_func"), zif_ZendTestNS2_namespaced_func, arginfo_ZendTestNS2_namespaced_func, 0, NULL, NULL) #else