From 15b403e2854876a56a22418918499d20f1b51383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 29 Oct 2026 18:06:38 +0100 Subject: [PATCH 1/9] Add request begin/end hooks for SAPIs serving several requests per script Worker SAPIs run one script that serves many requests, so RINIT and RSHUTDOWN don't run between them, and the SAPIs reset other extensions' globals by hand. php_request_end() and php_request_begin() run the output, SAPI and superglobal steps of php_request_shutdown() and php_request_startup() without ending the script, and call the new request_end_func and request_begin_func module hooks. php://input streams hold a reference to the request body, and freeing the body clears SG(request_info).request_body, so php_request_end() can release it. --- UPGRADING.INTERNALS | 10 +++ Zend/tests/arginfo_zpp_mismatch.inc | 1 + Zend/zend.h | 2 + Zend/zend_API.c | 25 ++++++ Zend/zend_API.h | 8 ++ Zend/zend_modules.h | 11 ++- ext/standard/php_fopen_wrapper.c | 9 ++- ext/zend_test/test.c | 9 +++ ext/zend_test/test.stub.php | 3 + ext/zend_test/test_arginfo.h | 20 +++-- ext/zend_test/test_decl.h | 8 +- ext/zend_test/test_legacy_arginfo.h | 6 +- main/main.c | 120 ++++++++++++++++++++++++---- main/php.h | 4 + main/php_main.h | 2 + main/streams/streams.c | 6 ++ 16 files changed, 213 insertions(+), 31 deletions(-) diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index b5134705b375..20f5236b0eb5 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -45,6 +45,12 @@ PHP 8.7 INTERNALS UPGRADE NOTES on POSIX platforms. - Function pointer signature for interface_gets_implemented has been made void. Implementations are expected to use zend_error_noreturn upon failure. +- zend_module_entry gained the request_begin_func and request_end_func + fields, called between two requests served by the same script. Declare them + with PHP_RBEGIN_FUNCTION() and PHP_REND_FUNCTION(), and set them with + STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(). request_end_func releases only + the state built from the finished request or tied to its response. Other + state is shared by the requests, as in a long-running CLI script. - Added a new zend_object_copy() function which increases the refcount of a zend_object* and returns it. Allowing more descriptive code rather than doing an assignment followed by GC_ADDREF(). @@ -110,6 +116,10 @@ PHP 8.7 INTERNALS UPGRADE NOTES 5. SAPI changes ======================== +- Added php_request_begin() and php_request_end() for SAPIs running a script + that serves several requests, such as a worker script. They replace copies of + php_request_startup() and php_request_shutdown() and call the module + request_begin_func and request_end_func hooks. - The size of the SAPI structure has changed. . The terminate_process method has been removed. . The force_http_10 method has been removed. diff --git a/Zend/tests/arginfo_zpp_mismatch.inc b/Zend/tests/arginfo_zpp_mismatch.inc index 695486d23132..39a436d5e859 100644 --- a/Zend/tests/arginfo_zpp_mismatch.inc +++ b/Zend/tests/arginfo_zpp_mismatch.inc @@ -15,6 +15,7 @@ function skipFunction($function): bool { || $function === 'zend_create_unterminated_string' || $function === 'zend_test_array_return' || $function === 'zend_test_crash' + || $function === 'zend_test_request_boundary' || $function === 'zend_leak_bytes' /* mess with output */ || (is_string($function) && str_starts_with($function, 'ob_')) diff --git a/Zend/zend.h b/Zend/zend.h index 2bbf0283991d..fa7fff185624 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -331,6 +331,8 @@ ZEND_API void zend_call_destructors(void); ZEND_API void zend_activate_modules(void); ZEND_API void zend_deactivate_modules(void); ZEND_API void zend_post_deactivate_modules(void); +ZEND_API void zend_request_begin_modules(void); +ZEND_API void zend_request_end_modules(void); ZEND_API void free_estring(char **str_p); diff --git a/Zend/zend_API.c b/Zend/zend_API.c index c9f5ed38aee5..afd063c21f39 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -3468,6 +3468,31 @@ ZEND_API void zend_activate_modules(void) /* {{{ */ } /* }}} */ +ZEND_API void zend_request_begin_modules(void) +{ + zend_module_entry *module; + + ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) { + if (module->request_begin_func + && module->request_begin_func(module->type, module->module_number) == FAILURE) { + zend_error(E_WARNING, "request_begin() for %s module failed", module->name); + } + } ZEND_HASH_FOREACH_END(); +} + +ZEND_API void zend_request_end_modules(void) +{ + zend_module_entry *module; + + ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { + if (module->request_end_func) { + zend_try { + module->request_end_func(module->type, module->module_number); + } zend_end_try(); + } + } ZEND_HASH_FOREACH_END(); +} + ZEND_API void zend_deactivate_modules(void) /* {{{ */ { EG(current_execute_data) = NULL; /* we're no longer executing anything */ diff --git a/Zend/zend_API.h b/Zend/zend_API.h index d1064970dcd5..ecf0f8808f48 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -226,6 +226,8 @@ typedef struct _zend_fcall_info_cache { #define ZEND_MODULE_ACTIVATE_N(module) zm_activate_##module #define ZEND_MODULE_DEACTIVATE_N(module) zm_deactivate_##module #define ZEND_MODULE_POST_ZEND_DEACTIVATE_N(module) zm_post_zend_deactivate_##module +#define ZEND_MODULE_REQUEST_BEGIN_N(module) zm_request_begin_##module +#define ZEND_MODULE_REQUEST_END_N(module) zm_request_end_##module #define ZEND_MODULE_INFO_N(module) zm_info_##module #define ZEND_MODULE_GLOBALS_CTOR_N(module) zm_globals_ctor_##module #define ZEND_MODULE_GLOBALS_DTOR_N(module) zm_globals_dtor_##module @@ -236,6 +238,8 @@ typedef struct _zend_fcall_info_cache { #define ZEND_MODULE_ACTIVATE_D(module) zend_result ZEND_MODULE_ACTIVATE_N(module)(INIT_FUNC_ARGS) #define ZEND_MODULE_DEACTIVATE_D(module) zend_result ZEND_MODULE_DEACTIVATE_N(module)(SHUTDOWN_FUNC_ARGS) #define ZEND_MODULE_POST_ZEND_DEACTIVATE_D(module) zend_result ZEND_MODULE_POST_ZEND_DEACTIVATE_N(module)(void) +#define ZEND_MODULE_REQUEST_BEGIN_D(module) zend_result ZEND_MODULE_REQUEST_BEGIN_N(module)(INIT_FUNC_ARGS) +#define ZEND_MODULE_REQUEST_END_D(module) zend_result ZEND_MODULE_REQUEST_END_N(module)(SHUTDOWN_FUNC_ARGS) #define ZEND_MODULE_INFO_D(module) ZEND_COLD void ZEND_MODULE_INFO_N(module)(ZEND_MODULE_INFO_FUNC_ARGS) #define ZEND_MODULE_GLOBALS_CTOR_D(module) void ZEND_MODULE_GLOBALS_CTOR_N(module)(zend_##module##_globals *module##_globals) #define ZEND_MODULE_GLOBALS_DTOR_D(module) void ZEND_MODULE_GLOBALS_DTOR_N(module)(zend_##module##_globals *module##_globals) @@ -1156,6 +1160,8 @@ static zend_always_inline bool ZVAL_FSTRING(zval *z, const char *s) { #define ZEND_MSHUTDOWN ZEND_MODULE_SHUTDOWN_N #define ZEND_RINIT ZEND_MODULE_ACTIVATE_N #define ZEND_RSHUTDOWN ZEND_MODULE_DEACTIVATE_N +#define ZEND_RBEGIN ZEND_MODULE_REQUEST_BEGIN_N +#define ZEND_REND ZEND_MODULE_REQUEST_END_N #define ZEND_MINFO ZEND_MODULE_INFO_N #define ZEND_GINIT(module) ((void (*)(void*))(ZEND_MODULE_GLOBALS_CTOR_N(module))) #define ZEND_GSHUTDOWN(module) ((void (*)(void*))(ZEND_MODULE_GLOBALS_DTOR_N(module))) @@ -1164,6 +1170,8 @@ static zend_always_inline bool ZVAL_FSTRING(zval *z, const char *s) { #define ZEND_MSHUTDOWN_FUNCTION ZEND_MODULE_SHUTDOWN_D #define ZEND_RINIT_FUNCTION ZEND_MODULE_ACTIVATE_D #define ZEND_RSHUTDOWN_FUNCTION ZEND_MODULE_DEACTIVATE_D +#define ZEND_RBEGIN_FUNCTION ZEND_MODULE_REQUEST_BEGIN_D +#define ZEND_REND_FUNCTION ZEND_MODULE_REQUEST_END_D #define ZEND_MINFO_FUNCTION ZEND_MODULE_INFO_D #define ZEND_GINIT_FUNCTION ZEND_MODULE_GLOBALS_CTOR_D #define ZEND_GSHUTDOWN_FUNCTION ZEND_MODULE_GLOBALS_DTOR_D diff --git a/Zend/zend_modules.h b/Zend/zend_modules.h index a941fd063a5b..422f698e3a0e 100644 --- a/Zend/zend_modules.h +++ b/Zend/zend_modules.h @@ -45,12 +45,16 @@ #define ZEND_MODULE_BUILD_ID "API" ZEND_TOSTR(ZEND_MODULE_API_NO) ZEND_BUILD_TS ZEND_BUILD_DEBUG ZEND_BUILD_SYSTEM ZEND_BUILD_EXTRA -#define STANDARD_MODULE_PROPERTIES_EX \ +#define STANDARD_MODULE_PROPERTIES_EX STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(NULL, NULL) + +#define STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(request_begin, request_end) \ .module_started = 0, \ .type = 0, \ .handle = NULL, \ .module_number = 0, \ - .build_id = ZEND_MODULE_BUILD_ID + .build_id = ZEND_MODULE_BUILD_ID, \ + .request_begin_func = request_begin, \ + .request_end_func = request_end #define NO_MODULE_GLOBALS 0, NULL, NULL, NULL @@ -102,6 +106,9 @@ struct _zend_module_entry { void *handle; int module_number; const char *build_id; + /* Called between two requests served by the same script, see php_request_begin() and php_request_end() */ + zend_result (*request_begin_func)(INIT_FUNC_ARGS); + zend_result (*request_end_func)(SHUTDOWN_FUNC_ARGS); }; #define MODULE_DEP_REQUIRED 1 diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index 5d24c30727f6..6f73ef2122e5 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -61,6 +61,7 @@ static const php_stream_ops php_stream_output_ops = { typedef struct php_stream_input { /* {{{ */ php_stream *body; + zend_resource *body_res; zend_off_t position; } php_stream_input_t; /* }}} */ @@ -105,7 +106,10 @@ static ssize_t php_stream_input_read(php_stream *stream, char *buf, size_t count static int php_stream_input_close(php_stream *stream, int close_handle) /* {{{ */ { - efree(stream->abstract); + php_stream_input_t *input = stream->abstract; + + zend_list_delete(input->body_res); + efree(input); stream->abstract = NULL; return 0; @@ -251,6 +255,9 @@ static php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, const c input->body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir)); SG(request_info).request_body = input->body; } + /* The body outlives the request when a long-running script keeps this stream */ + input->body_res = input->body->res; + GC_ADDREF(input->body_res); return php_stream_alloc(&php_stream_input_ops, input, 0, "rb"); } diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index fc346dd5aa0a..c77615beaa1c 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -34,6 +34,7 @@ #include "Zend/Optimizer/zend_optimizer.h" #include "Zend/zend_alloc.h" #include "SAPI.h" +#include "php_main.h" #include "rfc1867.h" #include "test_arginfo.h" #include "tmp_methods_arginfo.h" @@ -106,6 +107,14 @@ static ZEND_FUNCTION(zend_test_void_return) ZEND_PARSE_PARAMETERS_NONE(); } +static ZEND_FUNCTION(zend_test_request_boundary) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + php_request_end(); + RETURN_BOOL(php_request_begin() == SUCCESS); +} + static void pass1(zend_script *script, void *context) { php_printf("pass1\n"); diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 87ad8c9b93c8..fffae61fe9d6 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -267,6 +267,9 @@ function zend_test_void_return(): void {} function &zend_test_forward_reference(mixed &$value): mixed {} + /** Ends the current request and begins a new one, like a worker script between two requests */ + function zend_test_request_boundary(): bool {} + function zend_test_compile_string(string $source_string, string $filename, int $position): void {} /** @deprecated */ diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index 314d66a2799a..a1ed34a6879e 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 test.stub.php instead. - * Stub hash: 64eb27c89a9a5a378385e5f778cd17cfd67caed8 + * Stub hash: afaf90f82bb94a431f6a9cd133b4dacba99595f2 * Has decl header: yes */ #include "zend_attributes.h" @@ -24,6 +24,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_forward_reference, 1, ZEND_ARG_TYPE_INFO(1, value, IS_MIXED, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_request_boundary, 0, 0, _IS_BOOL, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_compile_string, 0, 3, IS_VOID, 0) ZEND_ARG_TYPE_INFO(0, source_string, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) @@ -335,8 +338,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_override_libxml_global ZEND_END_ARG_INFO() #endif -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_is_pcre_bundled, 0, 0, _IS_BOOL, 0) -ZEND_END_ARG_INFO() +#define arginfo_zend_test_is_pcre_bundled arginfo_zend_test_request_boundary #if defined(PHP_WIN32) ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_set_fmode, 0, 1, IS_VOID, 0) @@ -360,7 +362,7 @@ 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_zend_test_opcache_preloading arginfo_zend_test_request_boundary ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_uri_parser, 0, 2, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 0) @@ -382,7 +384,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_set_local_var, 0, 2, _ ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0) ZEND_END_ARG_INFO() -#define arginfo_ZendTestNS2_namespaced_func arginfo_zend_test_is_pcre_bundled +#define arginfo_ZendTestNS2_namespaced_func arginfo_zend_test_request_boundary #define arginfo_ZendTestNS2_namespaced_deprecated_func arginfo_zend_test_void_return @@ -390,7 +392,7 @@ ZEND_END_ARG_INFO() #define arginfo_ZendTestNS2_namespaced_deprecated_aliased_func arginfo_zend_test_void_return -#define arginfo_ZendTestNS2_ZendSubNS_namespaced_func arginfo_zend_test_is_pcre_bundled +#define arginfo_ZendTestNS2_ZendSubNS_namespaced_func arginfo_zend_test_request_boundary #define arginfo_ZendTestNS2_ZendSubNS_namespaced_deprecated_func arginfo_zend_test_void_return @@ -430,9 +432,9 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class__ZendTestChildClass_returnsThrowable, 0, 0, Exception, 0) ZEND_END_ARG_INFO() -#define arginfo_class_ZendAttributeTest_testMethod arginfo_zend_test_is_pcre_bundled +#define arginfo_class_ZendAttributeTest_testMethod arginfo_zend_test_request_boundary -#define arginfo_class__ZendTestTrait_testMethod arginfo_zend_test_is_pcre_bundled +#define arginfo_class__ZendTestTrait_testMethod arginfo_zend_test_request_boundary ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ZendTestAttributeWithArguments___construct, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, arg, IS_MIXED, 0) @@ -476,6 +478,7 @@ static ZEND_FUNCTION(zend_test_array_return); static ZEND_FUNCTION(zend_test_nullable_array_return); static ZEND_FUNCTION(zend_test_void_return); static ZEND_FUNCTION(zend_test_forward_reference); +static ZEND_FUNCTION(zend_test_request_boundary); static ZEND_FUNCTION(zend_test_compile_string); static ZEND_FUNCTION(zend_test_deprecated); static ZEND_FUNCTION(zend_test_deprecated_attr); @@ -630,6 +633,7 @@ static const zend_function_entry ext_functions[] = { #endif ZEND_FE(zend_test_void_return, arginfo_zend_test_void_return) ZEND_FE(zend_test_forward_reference, arginfo_zend_test_forward_reference) + ZEND_FE(zend_test_request_boundary, arginfo_zend_test_request_boundary) ZEND_FE(zend_test_compile_string, arginfo_zend_test_compile_string) #if (PHP_VERSION_ID >= 80400) ZEND_RAW_FENTRY("zend_test_deprecated", zif_zend_test_deprecated, arginfo_zend_test_deprecated, ZEND_ACC_DEPRECATED, NULL, NULL) diff --git a/ext/zend_test/test_decl.h b/ext/zend_test/test_decl.h index 494e097fa576..3de8a959eb26 100644 --- a/ext/zend_test/test_decl.h +++ b/ext/zend_test/test_decl.h @@ -1,8 +1,8 @@ /* This is a generated file, edit test.stub.php instead. - * Stub hash: 64eb27c89a9a5a378385e5f778cd17cfd67caed8 */ + * Stub hash: afaf90f82bb94a431f6a9cd133b4dacba99595f2 */ -#ifndef ZEND_TEST_DECL_64eb27c89a9a5a378385e5f778cd17cfd67caed8_H -#define ZEND_TEST_DECL_64eb27c89a9a5a378385e5f778cd17cfd67caed8_H +#ifndef ZEND_TEST_DECL_afaf90f82bb94a431f6a9cd133b4dacba99595f2_H +#define ZEND_TEST_DECL_afaf90f82bb94a431f6a9cd133b4dacba99595f2_H typedef enum zend_enum_ZendTestUnitEnum { ZEND_ENUM_ZendTestUnitEnum_Foo = 1, @@ -27,4 +27,4 @@ typedef enum zend_enum_ZendTestEnumWithInterface { ZEND_ENUM_ZendTestEnumWithInterface_Bar = 2, } zend_enum_ZendTestEnumWithInterface; -#endif /* ZEND_TEST_DECL_64eb27c89a9a5a378385e5f778cd17cfd67caed8_H */ +#endif /* ZEND_TEST_DECL_afaf90f82bb94a431f6a9cd133b4dacba99595f2_H */ diff --git a/ext/zend_test/test_legacy_arginfo.h b/ext/zend_test/test_legacy_arginfo.h index f941cf44c573..2917aed9a313 100644 --- a/ext/zend_test/test_legacy_arginfo.h +++ b/ext/zend_test/test_legacy_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit test.stub.php instead. - * Stub hash: 64eb27c89a9a5a378385e5f778cd17cfd67caed8 + * Stub hash: afaf90f82bb94a431f6a9cd133b4dacba99595f2 * Has decl header: yes */ #include "zend_constants.h" @@ -20,6 +20,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_zend_test_forward_reference, 0, 1, 1) ZEND_ARG_INFO(1, value) ZEND_END_ARG_INFO() +#define arginfo_zend_test_request_boundary arginfo_zend_trigger_bailout + ZEND_BEGIN_ARG_INFO_EX(arginfo_zend_test_compile_string, 0, 0, 3) ZEND_ARG_INFO(0, source_string) ZEND_ARG_INFO(0, filename) @@ -400,6 +402,7 @@ static ZEND_FUNCTION(zend_test_array_return); static ZEND_FUNCTION(zend_test_nullable_array_return); static ZEND_FUNCTION(zend_test_void_return); static ZEND_FUNCTION(zend_test_forward_reference); +static ZEND_FUNCTION(zend_test_request_boundary); static ZEND_FUNCTION(zend_test_compile_string); static ZEND_FUNCTION(zend_test_deprecated); static ZEND_FUNCTION(zend_test_deprecated_attr); @@ -546,6 +549,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(zend_test_nullable_array_return, arginfo_zend_test_nullable_array_return) ZEND_FE(zend_test_void_return, arginfo_zend_test_void_return) ZEND_FE(zend_test_forward_reference, arginfo_zend_test_forward_reference) + ZEND_FE(zend_test_request_boundary, arginfo_zend_test_request_boundary) ZEND_FE(zend_test_compile_string, arginfo_zend_test_compile_string) #if (PHP_VERSION_ID >= 80400) ZEND_RAW_FENTRY("zend_test_deprecated", zif_zend_test_deprecated, arginfo_zend_test_deprecated, ZEND_ACC_DEPRECATED, NULL, NULL) diff --git a/main/main.c b/main/main.c index 962739df4219..6e98f5febb1a 100644 --- a/main/main.c +++ b/main/main.c @@ -1876,6 +1876,25 @@ PHPAPI void php_child_init(void) } ZEND_HASH_FOREACH_END(); } +static void php_request_start_output(void) +{ + if (PG(expose_php) && !SG(headers_sent)) { + sapi_add_header(SAPI_PHP_VERSION_HEADER, sizeof(SAPI_PHP_VERSION_HEADER)-1, 1); + } + + if (PG(output_handler)) { + zval oh; + + ZVAL_STR(&oh, zend_string_dup(PG(output_handler), false)); + php_output_start_user(&oh, 0, PHP_OUTPUT_HANDLER_STDFLAGS); + zval_ptr_dtor(&oh); + } else if (PG(output_buffering)) { + php_output_start_user(NULL, PG(output_buffering) > 1 ? PG(output_buffering) : 0, PHP_OUTPUT_HANDLER_STDFLAGS); + } else if (PG(implicit_flush)) { + php_output_set_implicit_flush(1); + } +} + /* {{{ php_request_startup */ zend_result php_request_startup(void) { @@ -1929,21 +1948,7 @@ zend_result php_request_startup(void) CWDG(realpath_cache_size_limit) = 0; } - if (PG(expose_php) && !SG(headers_sent)) { - sapi_add_header(SAPI_PHP_VERSION_HEADER, sizeof(SAPI_PHP_VERSION_HEADER)-1, 1); - } - - if (PG(output_handler)) { - zval oh; - - ZVAL_STR(&oh, zend_string_dup(PG(output_handler), false)); - php_output_start_user(&oh, 0, PHP_OUTPUT_HANDLER_STDFLAGS); - zval_ptr_dtor(&oh); - } else if (PG(output_buffering)) { - php_output_start_user(NULL, PG(output_buffering) > 1 ? PG(output_buffering) : 0, PHP_OUTPUT_HANDLER_STDFLAGS); - } else if (PG(implicit_flush)) { - php_output_set_implicit_flush(1); - } + php_request_start_output(); /* We turn this off in php_execute_script() */ /* PG(during_request_startup) = 0; */ @@ -1966,6 +1971,91 @@ zend_result php_request_startup(void) } /* }}} */ +/* Superglobals already compiled into the running script are imported again, + * the others are armed for just in time creation. $_ENV comes from the process, + * not the request, so the script keeps it. */ +static void php_rehash_environment(void) +{ + zend_auto_global *auto_global; + + for (int i = 0; i < NUM_TRACK_VARS; i++) { + if (i == TRACK_VARS_ENV) { + continue; + } + zval_ptr_dtor(&PG(http_globals)[i]); + ZVAL_UNDEF(&PG(http_globals)[i]); + } + + ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) { + if (auto_global->name == ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV)) { + continue; + } + if (!auto_global->auto_global_callback) { + auto_global->armed = false; + } else if (!auto_global->jit || zend_hash_exists(&EG(symbol_table), auto_global->name)) { + auto_global->armed = auto_global->auto_global_callback(auto_global->name); + } else { + auto_global->armed = true; + } + } ZEND_HASH_FOREACH_END(); +} + +/* Starts a new request in a script that served the previous one, such as a + * worker script. The SAPI must fill SG(request_info) and SG(server_context) first. */ +PHPAPI zend_result php_request_begin(void) +{ + zend_result retval = SUCCESS; + + zend_try { + php_output_activate(); + + PG(header_is_being_sent) = 0; + PG(connection_status) = PHP_CONNECTION_NORMAL; + + sapi_activate(); + + /* The script is already executing, there is no separate input phase */ + zend_set_timeout(EG(timeout_seconds), 0); + + php_request_start_output(); + php_rehash_environment(); + zend_request_begin_modules(); + } zend_catch { + retval = FAILURE; + } zend_end_try(); + + return retval; +} + +/* Ends a request without ending the script serving it. */ +PHPAPI void php_request_end(void) +{ + php_stream *request_body = SG(request_info).request_body; + + zend_try { + php_output_end_all(); + } zend_end_try(); + + zend_try { + zend_unset_timeout(); + } zend_end_try(); + + zend_request_end_modules(); + + zend_try { + php_output_deactivate(); + } zend_end_try(); + + zend_try { + sapi_deactivate(); + } zend_end_try(); + + /* sapi_deactivate() forgets the body without closing it */ + if (request_body) { + zend_list_delete(request_body->res); + } +} + /* {{{ php_request_shutdown */ void php_request_shutdown(void *dummy) { diff --git a/main/php.h b/main/php.h index e5a6d6f9633f..b257cb6cc1a0 100644 --- a/main/php.h +++ b/main/php.h @@ -366,6 +366,8 @@ END_EXTERN_C() #define PHP_MSHUTDOWN ZEND_MODULE_SHUTDOWN_N #define PHP_RINIT ZEND_MODULE_ACTIVATE_N #define PHP_RSHUTDOWN ZEND_MODULE_DEACTIVATE_N +#define PHP_RBEGIN ZEND_MODULE_REQUEST_BEGIN_N +#define PHP_REND ZEND_MODULE_REQUEST_END_N #define PHP_MINFO ZEND_MODULE_INFO_N #define PHP_GINIT ZEND_GINIT #define PHP_GSHUTDOWN ZEND_GSHUTDOWN @@ -375,6 +377,8 @@ END_EXTERN_C() #define PHP_MSHUTDOWN_FUNCTION ZEND_MODULE_SHUTDOWN_D #define PHP_RINIT_FUNCTION ZEND_MODULE_ACTIVATE_D #define PHP_RSHUTDOWN_FUNCTION ZEND_MODULE_DEACTIVATE_D +#define PHP_RBEGIN_FUNCTION ZEND_MODULE_REQUEST_BEGIN_D +#define PHP_REND_FUNCTION ZEND_MODULE_REQUEST_END_D #define PHP_MINFO_FUNCTION ZEND_MODULE_INFO_D #define PHP_GINIT_FUNCTION ZEND_GINIT_FUNCTION #define PHP_GSHUTDOWN_FUNCTION ZEND_GSHUTDOWN_FUNCTION diff --git a/main/php_main.h b/main/php_main.h index de2ace1c25ed..0b439c463f3f 100644 --- a/main/php_main.h +++ b/main/php_main.h @@ -50,6 +50,8 @@ PHPAPI void php_print_version(sapi_module_struct *sapi_module); PHPAPI void php_child_init(void); PHPAPI zend_result php_request_startup(void); PHPAPI void php_request_shutdown(void *dummy); +PHPAPI zend_result php_request_begin(void); +PHPAPI void php_request_end(void); PHPAPI zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module); PHPAPI void php_module_shutdown(void); PHPAPI int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals); diff --git a/main/streams/streams.c b/main/streams/streams.c index a83e5d6042b2..f2c324521930 100644 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -20,6 +20,7 @@ #endif #include "php.h" #include "php_globals.h" +#include "SAPI.h" #include "php_memory_streams.h" #include "php_network.h" #include "php_io.h" @@ -389,6 +390,11 @@ fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remov } if (close_options & PHP_STREAM_FREE_RELEASE_STREAM) { + /* The script closed the request body, php_request_end() must not release it again */ + if (stream == SG(request_info).request_body) { + SG(request_info).request_body = NULL; + } + if (stream->poll_watchers) { php_io_poll_stream_notify_close(stream); } From 321067449bb0eadd9217975509c803a753494cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 29 Oct 2026 18:06:38 +0100 Subject: [PATCH 2/9] ext/standard: reset the URL rewriter between requests served by the same script The URL rewriter state drives the response output handler: without a reset, output_add_rewrite_var() never restarts the handler after the first request and its variables accumulate. Other state, such as strtok(), the stat cache or putenv() changes, is shared by the requests as in a long-running script. --- ext/standard/basic_functions.c | 10 ++++- ext/standard/basic_functions.h | 1 + .../tests/request_boundary_standard.phpt | 41 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 ext/zend_test/tests/request_boundary_standard.phpt diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index db302ab724ab..8593e2292a8a 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -158,7 +158,7 @@ zend_module_entry basic_functions_module = { /* {{{ */ PHP_STANDARD_VERSION, /* extension version */ NO_MODULE_GLOBALS, ZEND_MODULE_POST_ZEND_DEACTIVATE_N(streams), - STANDARD_MODULE_PROPERTIES_EX + STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(NULL, PHP_REND(basic)) }; /* }}} */ @@ -429,6 +429,14 @@ PHP_RINIT_FUNCTION(basic) /* {{{ */ } /* }}} */ +/* Other state, like strtok() or the stat cache, is shared by the requests as in a long-running script */ +PHP_REND_FUNCTION(basic) +{ + BASIC_RSHUTDOWN_SUBMODULE(url_scanner_ex) + + return SUCCESS; +} + PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */ { if (BG(strtok_string)) { diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h index 10dfd584d704..142ea4b0f2a1 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -38,6 +38,7 @@ PHP_MINIT_FUNCTION(basic); PHP_MSHUTDOWN_FUNCTION(basic); PHP_RINIT_FUNCTION(basic); PHP_RSHUTDOWN_FUNCTION(basic); +PHP_REND_FUNCTION(basic); PHP_MINFO_FUNCTION(basic); ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini *syntax_highlighter_ini); diff --git a/ext/zend_test/tests/request_boundary_standard.phpt b/ext/zend_test/tests/request_boundary_standard.phpt new file mode 100644 index 000000000000..1b2e19a78ca0 --- /dev/null +++ b/ext/zend_test/tests/request_boundary_standard.phpt @@ -0,0 +1,41 @@ +--TEST-- +Request boundary resets the URL rewriter and keeps state unrelated to the request +--EXTENSIONS-- +zend_test +--INI-- +variables_order=EGPCS +url_rewriter.tags="a=href" +--FILE-- +link', "\n"; +$_GET['previous'] = 'request'; +$_ENV['kept'] = 'by the script'; + +var_dump(zend_test_request_boundary()); + +var_dump($_GET); +var_dump($_ENV['kept']); +var_dump(error_get_last()['message']); +var_dump(getenv('ZEND_TEST_REQUEST_BOUNDARY')); +var_dump(strtok(' ')); +var_dump(ob_list_handlers()); +output_add_rewrite_var('current', 'request'); +?> +link +--EXPECT-- +string(8) "previous" +link +bool(true) +array(0) { +} +string(13) "by the script" +string(16) "previous request" +string(1) "1" +string(7) "request" +array(0) { +} +link From 5677ec78eb6a1ab7866d2f0e6973e522c79eb885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 29 Oct 2026 18:06:38 +0100 Subject: [PATCH 3/9] ext/session: end and begin sessions between requests served by the same script The session is written and closed and $_SESSION is removed, keeping the save handlers registered by the script. session.auto_start starts the session of the next request. --- ext/session/session.c | 34 ++++++++++- .../tests/request_boundary_session.phpt | 58 +++++++++++++++++++ .../request_boundary_session_auto_start.phpt | 24 ++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 ext/zend_test/tests/request_boundary_session.phpt create mode 100644 ext/zend_test/tests/request_boundary_session_auto_start.phpt diff --git a/ext/session/session.c b/ext/session/session.c index c15941b74523..c31f0744f271 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -2945,6 +2945,38 @@ static PHP_RINIT_FUNCTION(session) ZVAL_UNDEF(&PS(mod_user_names).struct_name); \ } +static PHP_RBEGIN_FUNCTION(session) +{ + if (PS(auto_start) && PS(session_status) == php_session_none) { + php_session_start(); + } + + return SUCCESS; +} + +/* Unlike RSHUTDOWN, keeps the save handlers registered by the running script */ +static PHP_REND_FUNCTION(session) +{ + bool uses_object_methods_as_handlers = PS(mod_user_uses_object_methods_as_handlers); + + if (PS(session_status) == php_session_active) { + zend_try { + php_session_flush(true); + } zend_end_try(); + } + php_rshutdown_session_globals(); + php_rinit_session_globals(); + PS(mod_user_uses_object_methods_as_handlers) = uses_object_methods_as_handlers; + + if (PS(mod) == NULL || PS(serializer) == NULL) { + PS(session_status) = php_session_disabled; + } + + zend_hash_str_del(&EG(symbol_table), ZEND_STRL("_SESSION")); + + return SUCCESS; +} + static PHP_RSHUTDOWN_FUNCTION(session) { if (PS(session_status) == php_session_active) { @@ -3469,7 +3501,7 @@ zend_module_entry session_module_entry = { PHP_GINIT(ps), NULL, NULL, - STANDARD_MODULE_PROPERTIES_EX, + STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(PHP_RBEGIN(session), PHP_REND(session)), .child_init_func = PHP_CINIT(session) }; diff --git a/ext/zend_test/tests/request_boundary_session.phpt b/ext/zend_test/tests/request_boundary_session.phpt new file mode 100644 index 000000000000..534555e41346 --- /dev/null +++ b/ext/zend_test/tests/request_boundary_session.phpt @@ -0,0 +1,58 @@ +--TEST-- +Request boundary writes and closes the session and keeps the save handler +--EXTENSIONS-- +session +zend_test +--INI-- +session.use_cookies=0 +session.use_strict_mode=0 +session.cache_limiter= +--FILE-- + +--EXPECT-- +write previous +close +bool(true) +bool(true) +string(0) "" +bool(false) +array(0) { +} +close +array(1) { + ["user"]=> + string(5) "alice" +} +close diff --git a/ext/zend_test/tests/request_boundary_session_auto_start.phpt b/ext/zend_test/tests/request_boundary_session_auto_start.phpt new file mode 100644 index 000000000000..a148f4723277 --- /dev/null +++ b/ext/zend_test/tests/request_boundary_session_auto_start.phpt @@ -0,0 +1,24 @@ +--TEST-- +Request boundary starts the session of the next request when session.auto_start is enabled +--EXTENSIONS-- +session +zend_test +--INI-- +session.auto_start=1 +session.use_cookies=0 +session.cache_limiter= +session.save_handler=files +--FILE-- + +--EXPECT-- +bool(true) +bool(true) +bool(true) From ef93a3474f4373e743d310fe353782b54e88ba4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 5 Nov 2026 14:03:21 +0100 Subject: [PATCH 4/9] ext/filter: release the input copies between requests served by the same script ext/filter keeps a copy of each input variable it parses, freed only by RSHUTDOWN. sapi_activate() drops these copies without freeing them, so a script serving several requests leaked them on each request. --- ext/filter/filter.c | 9 ++++++- ext/filter/php_filter.h | 1 + .../tests/request_boundary_filter.phpt | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 ext/zend_test/tests/request_boundary_filter.phpt diff --git a/ext/filter/filter.c b/ext/filter/filter.c index adabe6e6ee9a..e2251334bf56 100644 --- a/ext/filter/filter.c +++ b/ext/filter/filter.c @@ -89,7 +89,9 @@ zend_module_entry filter_module_entry = { PHP_RSHUTDOWN(filter), PHP_MINFO(filter), PHP_FILTER_VERSION, - STANDARD_MODULE_PROPERTIES + NO_MODULE_GLOBALS, + NULL, + STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(NULL, PHP_REND(filter)) }; /* }}} */ @@ -195,6 +197,11 @@ PHP_RSHUTDOWN_FUNCTION(filter) } /* }}} */ +PHP_REND_FUNCTION(filter) +{ + return PHP_RSHUTDOWN(filter)(SHUTDOWN_FUNC_ARGS_PASSTHRU); +} + /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(filter) { diff --git a/ext/filter/php_filter.h b/ext/filter/php_filter.h index ccf8b6b4883f..1fb4a7250d52 100644 --- a/ext/filter/php_filter.h +++ b/ext/filter/php_filter.h @@ -32,6 +32,7 @@ PHP_MINIT_FUNCTION(filter); PHP_MSHUTDOWN_FUNCTION(filter); PHP_RINIT_FUNCTION(filter); PHP_RSHUTDOWN_FUNCTION(filter); +PHP_REND_FUNCTION(filter); PHP_MINFO_FUNCTION(filter); ZEND_BEGIN_MODULE_GLOBALS(filter) diff --git a/ext/zend_test/tests/request_boundary_filter.phpt b/ext/zend_test/tests/request_boundary_filter.phpt new file mode 100644 index 000000000000..0ee947c7e53c --- /dev/null +++ b/ext/zend_test/tests/request_boundary_filter.phpt @@ -0,0 +1,26 @@ +--TEST-- +Request boundary releases the copies of the request input kept by ext/filter +--EXTENSIONS-- +zend_test +filter +--ENV-- +ZEND_TEST_FILTER=kept +--INI-- +variables_order=EGPCS +--FILE-- + +--EXPECT-- +bool(true) +string(4) "kept" From f6e4ca5b61614440a9b38e9aa768869a77da4db8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 9 Nov 2026 09:25:11 +0100 Subject: [PATCH 5/9] Release the superglobals between requests served by the same script php_rehash_environment() replaced the superglobals of the finished request once the output of the next one was active, so the destructors of the objects stored in them wrote to the next client's response, and data put back by those destructors leaked into the next request. php_request_begin() now releases them, and $_SESSION, before the output of the next request is active. The script still reads the superglobals of the finished request until then, as worker runtimes document. --- UPGRADING.INTERNALS | 5 ++ ext/session/session.c | 2 - ext/zend_test/test.c | 11 +++- ext/zend_test/test.stub.php | 4 +- ext/zend_test/test_arginfo.h | 16 +++--- ext/zend_test/test_decl.h | 8 +-- ext/zend_test/test_legacy_arginfo.h | 6 +- .../tests/request_boundary_superglobals.phpt | 57 +++++++++++++++++++ main/main.c | 3 + main/php_variables.c | 46 +++++++++++++++ main/php_variables.h | 1 + 11 files changed, 141 insertions(+), 18 deletions(-) create mode 100644 ext/zend_test/tests/request_boundary_superglobals.phpt diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index 20f5236b0eb5..52b384709b0e 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -120,6 +120,11 @@ PHP 8.7 INTERNALS UPGRADE NOTES that serves several requests, such as a worker script. They replace copies of php_request_startup() and php_request_shutdown() and call the module request_begin_func and request_end_func hooks. +- Added php_release_request_globals(), called by php_request_begin() before + the output of the new request is active. The script reads the superglobals + of the finished request until then. SAPIs running userland code between two + requests can call it before waiting for the next one, so a destructor + throwing there doesn't affect the next request. - The size of the SAPI structure has changed. . The terminate_process method has been removed. . The force_http_10 method has been removed. diff --git a/ext/session/session.c b/ext/session/session.c index c31f0744f271..d6349d981952 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -2972,8 +2972,6 @@ static PHP_REND_FUNCTION(session) PS(session_status) = php_session_disabled; } - zend_hash_str_del(&EG(symbol_table), ZEND_STRL("_SESSION")); - return SUCCESS; } diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index c77615beaa1c..7460c5e72555 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -109,9 +109,18 @@ static ZEND_FUNCTION(zend_test_void_return) static ZEND_FUNCTION(zend_test_request_boundary) { - ZEND_PARSE_PARAMETERS_NONE(); + zend_fcall_info fci = empty_fcall_info; + zend_fcall_info_cache fcc = empty_fcall_info_cache; + + ZEND_PARSE_PARAMETERS_START(0, 1) + Z_PARAM_OPTIONAL + Z_PARAM_FUNC_OR_NULL(fci, fcc) + ZEND_PARSE_PARAMETERS_END(); php_request_end(); + if (ZEND_FCI_INITIALIZED(fci)) { + zend_call_known_fcc(&fcc, NULL, 0, NULL, NULL); + } RETURN_BOOL(php_request_begin() == SUCCESS); } diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index fffae61fe9d6..0f40f34775ad 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -267,8 +267,8 @@ function zend_test_void_return(): void {} function &zend_test_forward_reference(mixed &$value): mixed {} - /** Ends the current request and begins a new one, like a worker script between two requests */ - function zend_test_request_boundary(): bool {} + /** Ends the current request, calls $between, and begins a new one, like a worker script between two requests */ + function zend_test_request_boundary(?callable $between = null): bool {} function zend_test_compile_string(string $source_string, string $filename, int $position): void {} diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index a1ed34a6879e..73a9a9e4fa9a 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 test.stub.php instead. - * Stub hash: afaf90f82bb94a431f6a9cd133b4dacba99595f2 + * Stub hash: 0d4aef6dc380491c756b7db128db865464bd22c9 * Has decl header: yes */ #include "zend_attributes.h" @@ -25,6 +25,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_forward_reference, 1, ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_request_boundary, 0, 0, _IS_BOOL, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, between, IS_CALLABLE, 1, "null") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_compile_string, 0, 3, IS_VOID, 0) @@ -338,7 +339,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_override_libxml_global ZEND_END_ARG_INFO() #endif -#define arginfo_zend_test_is_pcre_bundled arginfo_zend_test_request_boundary +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_is_pcre_bundled, 0, 0, _IS_BOOL, 0) +ZEND_END_ARG_INFO() #if defined(PHP_WIN32) ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_set_fmode, 0, 1, IS_VOID, 0) @@ -362,7 +364,7 @@ ZEND_END_ARG_INFO() #define arginfo_zend_test_gh18756 arginfo_zend_test_void_return -#define arginfo_zend_test_opcache_preloading arginfo_zend_test_request_boundary +#define arginfo_zend_test_opcache_preloading arginfo_zend_test_is_pcre_bundled ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_uri_parser, 0, 2, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 0) @@ -384,7 +386,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_set_local_var, 0, 2, _ ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0) ZEND_END_ARG_INFO() -#define arginfo_ZendTestNS2_namespaced_func arginfo_zend_test_request_boundary +#define arginfo_ZendTestNS2_namespaced_func arginfo_zend_test_is_pcre_bundled #define arginfo_ZendTestNS2_namespaced_deprecated_func arginfo_zend_test_void_return @@ -392,7 +394,7 @@ ZEND_END_ARG_INFO() #define arginfo_ZendTestNS2_namespaced_deprecated_aliased_func arginfo_zend_test_void_return -#define arginfo_ZendTestNS2_ZendSubNS_namespaced_func arginfo_zend_test_request_boundary +#define arginfo_ZendTestNS2_ZendSubNS_namespaced_func arginfo_zend_test_is_pcre_bundled #define arginfo_ZendTestNS2_ZendSubNS_namespaced_deprecated_func arginfo_zend_test_void_return @@ -432,9 +434,9 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class__ZendTestChildClass_returnsThrowable, 0, 0, Exception, 0) ZEND_END_ARG_INFO() -#define arginfo_class_ZendAttributeTest_testMethod arginfo_zend_test_request_boundary +#define arginfo_class_ZendAttributeTest_testMethod arginfo_zend_test_is_pcre_bundled -#define arginfo_class__ZendTestTrait_testMethod arginfo_zend_test_request_boundary +#define arginfo_class__ZendTestTrait_testMethod arginfo_zend_test_is_pcre_bundled ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ZendTestAttributeWithArguments___construct, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, arg, IS_MIXED, 0) diff --git a/ext/zend_test/test_decl.h b/ext/zend_test/test_decl.h index 3de8a959eb26..3d5cf5d1d499 100644 --- a/ext/zend_test/test_decl.h +++ b/ext/zend_test/test_decl.h @@ -1,8 +1,8 @@ /* This is a generated file, edit test.stub.php instead. - * Stub hash: afaf90f82bb94a431f6a9cd133b4dacba99595f2 */ + * Stub hash: 0d4aef6dc380491c756b7db128db865464bd22c9 */ -#ifndef ZEND_TEST_DECL_afaf90f82bb94a431f6a9cd133b4dacba99595f2_H -#define ZEND_TEST_DECL_afaf90f82bb94a431f6a9cd133b4dacba99595f2_H +#ifndef ZEND_TEST_DECL_0d4aef6dc380491c756b7db128db865464bd22c9_H +#define ZEND_TEST_DECL_0d4aef6dc380491c756b7db128db865464bd22c9_H typedef enum zend_enum_ZendTestUnitEnum { ZEND_ENUM_ZendTestUnitEnum_Foo = 1, @@ -27,4 +27,4 @@ typedef enum zend_enum_ZendTestEnumWithInterface { ZEND_ENUM_ZendTestEnumWithInterface_Bar = 2, } zend_enum_ZendTestEnumWithInterface; -#endif /* ZEND_TEST_DECL_afaf90f82bb94a431f6a9cd133b4dacba99595f2_H */ +#endif /* ZEND_TEST_DECL_0d4aef6dc380491c756b7db128db865464bd22c9_H */ diff --git a/ext/zend_test/test_legacy_arginfo.h b/ext/zend_test/test_legacy_arginfo.h index 2917aed9a313..3fb3af9d76a0 100644 --- a/ext/zend_test/test_legacy_arginfo.h +++ b/ext/zend_test/test_legacy_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit test.stub.php instead. - * Stub hash: afaf90f82bb94a431f6a9cd133b4dacba99595f2 + * Stub hash: 0d4aef6dc380491c756b7db128db865464bd22c9 * Has decl header: yes */ #include "zend_constants.h" @@ -20,7 +20,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_zend_test_forward_reference, 0, 1, 1) ZEND_ARG_INFO(1, value) ZEND_END_ARG_INFO() -#define arginfo_zend_test_request_boundary arginfo_zend_trigger_bailout +ZEND_BEGIN_ARG_INFO_EX(arginfo_zend_test_request_boundary, 0, 0, 0) + ZEND_ARG_INFO(0, between) +ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_zend_test_compile_string, 0, 0, 3) ZEND_ARG_INFO(0, source_string) diff --git a/ext/zend_test/tests/request_boundary_superglobals.phpt b/ext/zend_test/tests/request_boundary_superglobals.phpt new file mode 100644 index 000000000000..44601d7567ec --- /dev/null +++ b/ext/zend_test/tests/request_boundary_superglobals.phpt @@ -0,0 +1,57 @@ +--TEST-- +Request boundary releases the objects stored in superglobals when the next request begins, except $_ENV +--EXTENSIONS-- +zend_test +--FILE-- + */ + public static array $log = []; + + public function __construct(public string $value) + { + } + + public function __destruct() + { + self::$log[] = "destroyed {$this->value}"; + } +} + +final class Mover +{ + public function __destruct() + { + $_GET['moved'] = new Secret('moved'); + } +} + +$_GET['secret'] = new Secret('get'); +$_POST['secret'] = new Secret('post'); +$_COOKIE['secret'] = new Secret('cookie'); +$_SERVER['secret'] = new Secret('server'); +$_REQUEST['secret'] = new Secret('request'); +$_FILES['mover'] = new Mover(); +$_ENV['secret'] = new Secret('env'); + +zend_test_request_boundary(static function (): void { + Secret::$log[] = "between, still readable: {$_GET['secret']->value}"; + $_POST['between'] = new Secret('between'); +}); + +echo implode("\n", Secret::$log), "\n"; +var_dump(isset($_GET['moved']), isset($_POST['between']), $_ENV['secret']->value); +?> +--EXPECT-- +between, still readable: get +destroyed get +destroyed post +destroyed between +destroyed cookie +destroyed server +destroyed request +destroyed moved +bool(false) +bool(false) +string(3) "env" diff --git a/main/main.c b/main/main.c index 6e98f5febb1a..9753073eb375 100644 --- a/main/main.c +++ b/main/main.c @@ -2007,6 +2007,9 @@ PHPAPI zend_result php_request_begin(void) zend_result retval = SUCCESS; zend_try { + /* the script reads the previous request's superglobals until now */ + php_release_request_globals(); + php_output_activate(); PG(header_is_being_sent) = 0; diff --git a/main/php_variables.c b/main/php_variables.c index b510ea0b203c..9569d1b5f490 100644 --- a/main/php_variables.c +++ b/main/php_variables.c @@ -820,6 +820,52 @@ PHPAPI zend_result php_hash_environment(void) } /* }}} */ +static bool php_release_request_globals_once(void) +{ + zend_auto_global *auto_global; + bool released = false; + + ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) { + zval *value, *array, garbage; + + if (auto_global->name == ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV) + || !(value = zend_hash_find(&EG(symbol_table), auto_global->name))) { + continue; + } + + /* created by their extension when needed, such as $_SESSION */ + if (!auto_global->auto_global_callback) { + zend_hash_del(&EG(symbol_table), auto_global->name); + released = true; + continue; + } + + array = value; + ZVAL_DEREF(array); + if (Z_TYPE_P(array) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(array)) == 0) { + continue; + } + + /* kept as an empty array so php_request_begin() imports it again */ + ZVAL_COPY_VALUE(&garbage, value); + ZVAL_EMPTY_ARRAY(value); + zval_ptr_dtor(&garbage); + released = true; + } ZEND_HASH_FOREACH_END(); + + return released; +} + +/* Runs the destructors of the objects stored in superglobals before the output + * of the next request is active. $_ENV comes from the process, not the request, + * so the script keeps it. */ +PHPAPI void php_release_request_globals(void) +{ + /* a destructor can put data back into a superglobal already released */ + while (php_release_request_globals_once()) { + } +} + static bool php_auto_globals_create_get(zend_string *name) { if (PG(variables_order) && (strchr(PG(variables_order),'G') || strchr(PG(variables_order),'g'))) { diff --git a/main/php_variables.h b/main/php_variables.h index 27a45e8daa3c..13701fffdda5 100644 --- a/main/php_variables.h +++ b/main/php_variables.h @@ -38,6 +38,7 @@ PHPAPI void php_register_known_variable(const char *var, size_t var_len, zval *v PHPAPI void php_build_argv(const char *s, zval *track_vars_array); PHPAPI zend_result php_hash_environment(void); +PHPAPI void php_release_request_globals(void); END_EXTERN_C() #define NUM_TRACK_VARS 6 From 68693a260847fd3f8249b56066ae3dcf45938b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 10 Nov 2026 15:20:11 +0100 Subject: [PATCH 6/9] Release the state set between two requests Code running between two requests served by the same script, like the worker loop or a destructor, can set request state after php_request_end(). sapi_activate() dropped the header callback, headers, status line, mimetype and php://input body without releasing them, and the next request inherited the URL rewriter state and the session. php_request_begin() now releases the SAPI state before the output of the new request is active, and the standard request_begin hook resets the URL rewriter. session_start() and session_id() refuse to start or name a session when no request is active, as no client would get it. php_request_begin() sets SG(sapi_started) like php_request_startup(). --- ext/session/session.c | 12 ++++ ext/standard/basic_functions.c | 10 +++- ext/standard/basic_functions.h | 1 + .../tests/request_boundary_between.phpt | 55 +++++++++++++++++++ .../request_boundary_header_callback.phpt | 30 ++++++++++ main/main.c | 31 +++++++++++ 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 ext/zend_test/tests/request_boundary_between.phpt create mode 100644 ext/zend_test/tests/request_boundary_header_callback.phpt diff --git a/ext/session/session.c b/ext/session/session.c index d6349d981952..2ca33a1b05ac 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -2394,6 +2394,12 @@ PHP_FUNCTION(session_id) RETURN_FALSE; } + /* code running between two requests served by the same script has no client */ + if (name && UNEXPECTED(!SG(sapi_started))) { + php_error_docref(NULL, E_WARNING, "Session ID cannot be changed when no request is active"); + RETURN_FALSE; + } + if (PS(id)) { /* keep compatibility for "\0" characters ??? * see: ext/session/tests/session_id_error3.phpt */ @@ -2713,6 +2719,12 @@ PHP_FUNCTION(session_start) RETURN_THROWS(); } + /* code running between two requests served by the same script has no client */ + if (UNEXPECTED(!SG(sapi_started))) { + php_error_docref(NULL, E_WARNING, "Session cannot be started when no request is active"); + RETURN_FALSE; + } + if (PS(session_status) == php_session_active) { php_session_session_already_started_error(E_NOTICE, "Ignoring session_start() because a session is already active"); RETURN_TRUE; diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 8593e2292a8a..bf853bf648d6 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -158,7 +158,7 @@ zend_module_entry basic_functions_module = { /* {{{ */ PHP_STANDARD_VERSION, /* extension version */ NO_MODULE_GLOBALS, ZEND_MODULE_POST_ZEND_DEACTIVATE_N(streams), - STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(NULL, PHP_REND(basic)) + STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(PHP_RBEGIN(basic), PHP_REND(basic)) }; /* }}} */ @@ -437,6 +437,14 @@ PHP_REND_FUNCTION(basic) return SUCCESS; } +/* Discards the URL rewriter state code running between two requests set */ +PHP_RBEGIN_FUNCTION(basic) +{ + BASIC_RSHUTDOWN_SUBMODULE(url_scanner_ex) + + return SUCCESS; +} + PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */ { if (BG(strtok_string)) { diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h index 142ea4b0f2a1..fb2ea8ca4ae5 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -39,6 +39,7 @@ PHP_MSHUTDOWN_FUNCTION(basic); PHP_RINIT_FUNCTION(basic); PHP_RSHUTDOWN_FUNCTION(basic); PHP_REND_FUNCTION(basic); +PHP_RBEGIN_FUNCTION(basic); PHP_MINFO_FUNCTION(basic); ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini *syntax_highlighter_ini); diff --git a/ext/zend_test/tests/request_boundary_between.phpt b/ext/zend_test/tests/request_boundary_between.phpt new file mode 100644 index 000000000000..c6a1c7dfa977 --- /dev/null +++ b/ext/zend_test/tests/request_boundary_between.phpt @@ -0,0 +1,55 @@ +--TEST-- +Request boundary releases the request state set between two requests +--EXTENSIONS-- +zend_test +session +--INI-- +display_errors=0 +session.use_cookies=0 +session.cache_limiter= +--FILE-- + stream_get_meta_data($s)['stream_type'] === 'TEMP')); +} + +$streams = temp_streams(); +$memory = memory_get_usage(); +$session = []; + +zend_test_request_boundary(static function () use (&$session): void { + header('X-Between: ' . str_repeat('a', 1 << 20)); + header('HTTP/1.1 200 ' . str_repeat('a', 1 << 20)); + header('Content-Type: text/' . str_repeat('a', 1 << 20)); + fclose(fopen('php://input', 'r')); + output_add_rewrite_var('between', 'requests'); + + $session[] = session_id('between'); + $session[] = error_get_last()['message']; + $session[] = session_start(); + $session[] = error_get_last()['message']; +}); + +var_dump(memory_get_usage() - $memory < 1 << 20); +var_dump(temp_streams() === $streams); +var_dump($session, session_id()); + +output_add_rewrite_var('current', 'request'); +echo '
', "\n"; +?> +--EXPECT-- +bool(true) +bool(true) +array(4) { + [0]=> + bool(false) + [1]=> + string(68) "session_id(): Session ID cannot be changed when no request is active" + [2]=> + bool(false) + [3]=> + string(68) "session_start(): Session cannot be started when no request is active" +} +string(0) "" +
diff --git a/ext/zend_test/tests/request_boundary_header_callback.phpt b/ext/zend_test/tests/request_boundary_header_callback.phpt new file mode 100644 index 000000000000..4f7198365118 --- /dev/null +++ b/ext/zend_test/tests/request_boundary_header_callback.phpt @@ -0,0 +1,30 @@ +--TEST-- +Request boundary releases a header callback registered between two requests +--EXTENSIONS-- +zend_test +--FILE-- + */ + public static array $log = []; + + public function __destruct() + { + self::$log[] = 'destroyed'; + } +} + +zend_test_request_boundary(static function (): void { + $tracker = new Tracker(); + header_register_callback(static function () use ($tracker): void { + Tracker::$log[] = 'called'; + }); +}); + +Tracker::$log[] = 'next request'; +echo implode("\n", Tracker::$log), "\n"; +?> +--EXPECT-- +destroyed +next request diff --git a/main/main.c b/main/main.c index 9753073eb375..91fc30f8005a 100644 --- a/main/main.c +++ b/main/main.c @@ -2000,6 +2000,34 @@ static void php_rehash_environment(void) } ZEND_HASH_FOREACH_END(); } +/* Code running between two requests can set these after php_request_end(), + * sapi_activate() would drop them without releasing them */ +static void php_release_sapi_globals(void) +{ + if (ZEND_FCC_INITIALIZED(SG(send_header_fcc))) { + zend_fcall_info_cache callback = SG(send_header_fcc); + SG(send_header_fcc) = empty_fcall_info_cache; + zend_fcc_dtor(&callback); + } + + zend_llist_clean(&SG(sapi_headers).headers); + if (SG(sapi_headers).http_status_line) { + efree(SG(sapi_headers).http_status_line); + SG(sapi_headers).http_status_line = NULL; + } + if (SG(sapi_headers).mimetype) { + efree(SG(sapi_headers).mimetype); + SG(sapi_headers).mimetype = NULL; + } + + /* opening php://input creates an empty body */ + php_stream *request_body = SG(request_info).request_body; + if (request_body) { + SG(request_info).request_body = NULL; + zend_list_delete(request_body->res); + } +} + /* Starts a new request in a script that served the previous one, such as a * worker script. The SAPI must fill SG(request_info) and SG(server_context) first. */ PHPAPI zend_result php_request_begin(void) @@ -2009,6 +2037,7 @@ PHPAPI zend_result php_request_begin(void) zend_try { /* the script reads the previous request's superglobals until now */ php_release_request_globals(); + php_release_sapi_globals(); php_output_activate(); @@ -2027,6 +2056,8 @@ PHPAPI zend_result php_request_begin(void) retval = FAILURE; } zend_end_try(); + SG(sapi_started) = 1; + return retval; } From 82ab81649baf0be1e621fd502c68985a1a6fcdbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 11 Nov 2026 13:45:55 +0100 Subject: [PATCH 7/9] Return FAILURE when a request hook fails A failing request_startup_func ends the process, while php_request_begin() and php_request_end() ignored failing hooks and the script kept serving requests with state its extensions couldn't set up or release. Both now return FAILURE when a hook fails or a step bails out, so the SAPI can end the script. zend_test gets request hooks that fail on demand. --- UPGRADING.INTERNALS | 4 ++- Zend/zend.h | 4 +-- Zend/zend_API.c | 18 ++++++++++--- ext/zend_test/php_test.h | 2 ++ ext/zend_test/test.c | 19 ++++++++++--- .../tests/request_boundary_hook_failure.phpt | 27 +++++++++++++++++++ main/main.c | 17 +++++++++--- main/php_main.h | 2 +- 8 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 ext/zend_test/tests/request_boundary_hook_failure.phpt diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index 52b384709b0e..3037185d0817 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -119,7 +119,9 @@ PHP 8.7 INTERNALS UPGRADE NOTES - Added php_request_begin() and php_request_end() for SAPIs running a script that serves several requests, such as a worker script. They replace copies of php_request_startup() and php_request_shutdown() and call the module - request_begin_func and request_end_func hooks. + request_begin_func and request_end_func hooks. They return FAILURE when a + hook fails or a step bails out: the SAPI should then end the script, as a + failing request_startup_func ends the process. - Added php_release_request_globals(), called by php_request_begin() before the output of the new request is active. The script reads the superglobals of the finished request until then. SAPIs running userland code between two diff --git a/Zend/zend.h b/Zend/zend.h index fa7fff185624..dca14421867b 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -331,8 +331,8 @@ ZEND_API void zend_call_destructors(void); ZEND_API void zend_activate_modules(void); ZEND_API void zend_deactivate_modules(void); ZEND_API void zend_post_deactivate_modules(void); -ZEND_API void zend_request_begin_modules(void); -ZEND_API void zend_request_end_modules(void); +ZEND_API zend_result zend_request_begin_modules(void); +ZEND_API zend_result zend_request_end_modules(void); ZEND_API void free_estring(char **str_p); diff --git a/Zend/zend_API.c b/Zend/zend_API.c index afd063c21f39..951d5f7b87c8 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -3468,29 +3468,41 @@ ZEND_API void zend_activate_modules(void) /* {{{ */ } /* }}} */ -ZEND_API void zend_request_begin_modules(void) +ZEND_API zend_result zend_request_begin_modules(void) { zend_module_entry *module; + zend_result result = SUCCESS; ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) { if (module->request_begin_func && module->request_begin_func(module->type, module->module_number) == FAILURE) { zend_error(E_WARNING, "request_begin() for %s module failed", module->name); + result = FAILURE; } } ZEND_HASH_FOREACH_END(); + + return result; } -ZEND_API void zend_request_end_modules(void) +ZEND_API zend_result zend_request_end_modules(void) { zend_module_entry *module; + zend_result result = SUCCESS; ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { if (module->request_end_func) { zend_try { - module->request_end_func(module->type, module->module_number); + if (module->request_end_func(module->type, module->module_number) == FAILURE) { + zend_error(E_WARNING, "request_end() for %s module failed", module->name); + result = FAILURE; + } + } zend_catch { + result = FAILURE; } zend_end_try(); } } ZEND_HASH_FOREACH_END(); + + return result; } ZEND_API void zend_deactivate_modules(void) /* {{{ */ diff --git a/ext/zend_test/php_test.h b/ext/zend_test/php_test.h index 34f6d7c30bfe..84362bec53bd 100644 --- a/ext/zend_test/php_test.h +++ b/ext/zend_test/php_test.h @@ -57,6 +57,8 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test) int register_passes; bool rearm_vm_interrupt; bool print_stderr_mshutdown; + bool request_begin_fails; + bool request_end_fails; zend_long limit_copy_file_range; int observe_opline_in_zendmm; zend_mm_heap* zend_orig_heap; diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 7460c5e72555..44d4f5e0cc8b 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -117,11 +117,12 @@ static ZEND_FUNCTION(zend_test_request_boundary) Z_PARAM_FUNC_OR_NULL(fci, fcc) ZEND_PARSE_PARAMETERS_END(); - php_request_end(); + bool ended = php_request_end() == SUCCESS; if (ZEND_FCI_INITIALIZED(fci)) { zend_call_known_fcc(&fcc, NULL, 0, NULL, NULL); } - RETURN_BOOL(php_request_begin() == SUCCESS); + bool begun = php_request_begin() == SUCCESS; + RETURN_BOOL(ended && begun); } static void pass1(zend_script *script, void *context) @@ -2305,6 +2306,8 @@ PHP_INI_BEGIN() STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.rearm_vm_interrupt", "0", PHP_INI_SYSTEM, OnUpdateBool, rearm_vm_interrupt, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.print_stderr_mshutdown", "0", PHP_INI_SYSTEM, OnUpdateBool, print_stderr_mshutdown, zend_zend_test_globals, zend_test_globals) + STD_PHP_INI_BOOLEAN("zend_test.request_begin_fails", "0", PHP_INI_ALL, OnUpdateBool, request_begin_fails, zend_zend_test_globals, zend_test_globals) + STD_PHP_INI_BOOLEAN("zend_test.request_end_fails", "0", PHP_INI_ALL, OnUpdateBool, request_end_fails, zend_zend_test_globals, zend_test_globals) #ifdef HAVE_COPY_FILE_RANGE STD_PHP_INI_ENTRY("zend_test.limit_copy_file_range", "-1", PHP_INI_ALL, OnUpdateLong, limit_copy_file_range, zend_zend_test_globals, zend_test_globals) #endif @@ -2592,6 +2595,16 @@ PHP_RSHUTDOWN_FUNCTION(zend_test) return SUCCESS; } +static PHP_RBEGIN_FUNCTION(zend_test) +{ + return ZT_G(request_begin_fails) ? FAILURE : SUCCESS; +} + +static PHP_REND_FUNCTION(zend_test) +{ + return ZT_G(request_end_fails) ? FAILURE : SUCCESS; +} + static PHP_GINIT_FUNCTION(zend_test) { #if defined(COMPILE_DL_ZEND_TEST) && defined(ZTS) @@ -2636,7 +2649,7 @@ zend_module_entry zend_test_module_entry = { PHP_GINIT(zend_test), PHP_GSHUTDOWN(zend_test), NULL, - STANDARD_MODULE_PROPERTIES_EX, + STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(PHP_RBEGIN(zend_test), PHP_REND(zend_test)), .child_init_func = zend_test_child_init }; diff --git a/ext/zend_test/tests/request_boundary_hook_failure.phpt b/ext/zend_test/tests/request_boundary_hook_failure.phpt new file mode 100644 index 000000000000..8ca7d03b9881 --- /dev/null +++ b/ext/zend_test/tests/request_boundary_hook_failure.phpt @@ -0,0 +1,27 @@ +--TEST-- +Request boundary reports a failing request_begin or request_end hook +--EXTENSIONS-- +zend_test +--FILE-- + +--EXPECTF-- +bool(true) + +Warning: request_end() for zend_test module failed in %s on line %d +bool(false) + +Warning: request_begin() for zend_test module failed in %s on line %d +bool(false) +bool(true) diff --git a/main/main.c b/main/main.c index 91fc30f8005a..b2afc096e918 100644 --- a/main/main.c +++ b/main/main.c @@ -2051,7 +2051,7 @@ PHPAPI zend_result php_request_begin(void) php_request_start_output(); php_rehash_environment(); - zend_request_begin_modules(); + retval = zend_request_begin_modules(); } zend_catch { retval = FAILURE; } zend_end_try(); @@ -2062,32 +2062,43 @@ PHPAPI zend_result php_request_begin(void) } /* Ends a request without ending the script serving it. */ -PHPAPI void php_request_end(void) +PHPAPI zend_result php_request_end(void) { php_stream *request_body = SG(request_info).request_body; + zend_result retval = SUCCESS; zend_try { php_output_end_all(); + } zend_catch { + retval = FAILURE; } zend_end_try(); zend_try { zend_unset_timeout(); } zend_end_try(); - zend_request_end_modules(); + if (zend_request_end_modules() == FAILURE) { + retval = FAILURE; + } zend_try { php_output_deactivate(); + } zend_catch { + retval = FAILURE; } zend_end_try(); zend_try { sapi_deactivate(); + } zend_catch { + retval = FAILURE; } zend_end_try(); /* sapi_deactivate() forgets the body without closing it */ if (request_body) { zend_list_delete(request_body->res); } + + return retval; } /* {{{ php_request_shutdown */ diff --git a/main/php_main.h b/main/php_main.h index 0b439c463f3f..359847d2894f 100644 --- a/main/php_main.h +++ b/main/php_main.h @@ -51,7 +51,7 @@ PHPAPI void php_child_init(void); PHPAPI zend_result php_request_startup(void); PHPAPI void php_request_shutdown(void *dummy); PHPAPI zend_result php_request_begin(void); -PHPAPI void php_request_end(void); +PHPAPI zend_result php_request_end(void); PHPAPI zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module); PHPAPI void php_module_shutdown(void); PHPAPI int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals); From 1f08b442dca99c97ab2d8e9424b5300fc58d667b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 27 Nov 2026 16:40:05 +0100 Subject: [PATCH 8/9] Stop old php://input streams reading new bodies A php://input stream kept across a request boundary read from the SAPI when the script used it during the next request, as sapi_activate() resets the read counters. It returned the next request's body and took those bytes from the stream the next request opens. The stream now reads from the SAPI only while its body is the body of the current request. A stream kept from a previous request returns what it already buffered, then EOF. --- ext/standard/php_fopen_wrapper.c | 4 +++- .../tests/request_boundary_input.phpt | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 ext/zend_test/tests/request_boundary_input.phpt diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index 6f73ef2122e5..bd29fc4d0b0f 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -77,7 +77,9 @@ static ssize_t php_stream_input_read(php_stream *stream, char *buf, size_t count php_stream_input_t *input = stream->abstract; ssize_t read; - if (!SG(post_read) && SG(read_post_bytes) < (int64_t)(input->position + count)) { + /* a stream kept from a previous request must not read the body of the current one */ + if (input->body == SG(request_info).request_body + && !SG(post_read) && SG(read_post_bytes) < (int64_t)(input->position + count)) { /* read requested data from SAPI */ size_t read_bytes = sapi_read_post_block(buf, count); diff --git a/ext/zend_test/tests/request_boundary_input.phpt b/ext/zend_test/tests/request_boundary_input.phpt new file mode 100644 index 000000000000..fa1bcad41085 --- /dev/null +++ b/ext/zend_test/tests/request_boundary_input.phpt @@ -0,0 +1,23 @@ +--TEST-- +Request boundary keeps a php://input stream from reading the body of the next request +--EXTENSIONS-- +zend_test +--INI-- +enable_post_data_reading=0 +--POST_RAW-- +Content-Type: application/unknown +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +--FILE-- + +--EXPECTF-- +string(4) "aaaa" +%Aint(8188) +string(0) "" From b88d95f2e021f084343474ab1d0ef94f07dc3a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 27 Nov 2026 17:04:40 +0100 Subject: [PATCH 9/9] Call the request hooks in every SAPI request_begin_func ran only between two requests of a worker script, so extensions needed the same setup in RINIT and RBEGIN, and could not rely on RBEGIN in SAPIs without a worker mode. php_execute_script() now calls the request_begin_func hooks before the userland code of the request runs, so linting, preloading and FPM status pages don't call them. The CLI, phpdbg and php_embed_init() call zend_request_begin_modules() before code they evaluate with zend_eval_string(), and dl() calls the hook of a module loaded after the request began. A failing hook keeps the script from running, with exit status 255 and HTTP response code 500. request_end_func runs once for each module that entered its request_begin_func, even if another module bailed out: from php_request_end() between two requests, or in php_request_shutdown() right before the same module's request_shutdown_func, so the session is still written at the place of the session module's shutdown. The session module doesn't retry a session.auto_start that RINIT already tried. php_request_begin() resets the URL rewriter state set between two requests itself, as the standard module's request_begin_func would now also run before the first userland code of every request. --- UPGRADING.INTERNALS | 19 ++- Zend/zend_API.c | 148 ++++++++++++++---- Zend/zend_API.h | 2 + Zend/zend_globals.h | 1 + ext/session/php_session.h | 1 + ext/session/session.c | 5 +- ext/standard/basic_functions.c | 10 +- ext/standard/basic_functions.h | 1 - ext/standard/dl.c | 3 + ext/zend_test/php_test.h | 1 + ext/zend_test/test.c | 14 ++ .../tests/request_hooks_begin_failure.phpt | 37 +++++ ext/zend_test/tests/request_hooks_cli.phpt | 34 ++++ ext/zend_test/tests/request_hooks_order.phpt | 20 +++ .../request_hooks_session_auto_start.phpt | 20 +++ .../tests/request_hooks_session_order.phpt | 40 +++++ main/main.c | 13 +- sapi/cli/php_cli.c | 29 +++- sapi/embed/php_embed.c | 7 + sapi/phpdbg/phpdbg_prompt.c | 11 +- 20 files changed, 358 insertions(+), 58 deletions(-) create mode 100644 ext/zend_test/tests/request_hooks_begin_failure.phpt create mode 100644 ext/zend_test/tests/request_hooks_cli.phpt create mode 100644 ext/zend_test/tests/request_hooks_order.phpt create mode 100644 ext/zend_test/tests/request_hooks_session_auto_start.phpt create mode 100644 ext/zend_test/tests/request_hooks_session_order.phpt diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index 3037185d0817..1f651767d0ef 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -46,11 +46,14 @@ PHP 8.7 INTERNALS UPGRADE NOTES - Function pointer signature for interface_gets_implemented has been made void. Implementations are expected to use zend_error_noreturn upon failure. - zend_module_entry gained the request_begin_func and request_end_func - fields, called between two requests served by the same script. Declare them - with PHP_RBEGIN_FUNCTION() and PHP_REND_FUNCTION(), and set them with - STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(). request_end_func releases only - the state built from the finished request or tied to its response. Other - state is shared by the requests, as in a long-running CLI script. + fields. Declare them with PHP_RBEGIN_FUNCTION() and PHP_REND_FUNCTION(), and + set them with STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(). request_begin_func + runs when userland code first runs for a request, so not when linting, when + preloading or when FPM serves a status page. request_end_func runs right + before the module's request_shutdown_func, or between two requests served + by the same script, and only if request_begin_func ran. It releases only the + state built from the finished request or tied to its response. Other state + is shared by the requests, as in a long-running CLI script. - Added a new zend_object_copy() function which increases the refcount of a zend_object* and returns it. Allowing more descriptive code rather than doing an assignment followed by GC_ADDREF(). @@ -122,6 +125,12 @@ PHP 8.7 INTERNALS UPGRADE NOTES request_begin_func and request_end_func hooks. They return FAILURE when a hook fails or a step bails out: the SAPI should then end the script, as a failing request_startup_func ends the process. +- php_execute_script() calls the request_begin_func hooks. SAPIs running + userland code with zend_eval_string() instead call + zend_request_begin_modules() first, which does nothing if the hooks already + ran for the current request. The CLI, phpdbg and php_embed_init() do. When + a hook fails, php_execute_script() doesn't run the script, sets the exit + status to 255 and the HTTP response code to 500. - Added php_release_request_globals(), called by php_request_begin() before the output of the new request is active. The script reads the superglobals of the finished request until then. SAPIs running userland code between two diff --git a/Zend/zend_API.c b/Zend/zend_API.c index 951d5f7b87c8..9bd81bd2f091 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -44,6 +44,7 @@ ZEND_API bool zend_dl_use_deepbind = false; static zend_module_entry **module_request_startup_handlers; static zend_module_entry **module_request_shutdown_handlers; +static zend_module_entry **module_request_begin_handlers; static zend_module_entry **module_post_deactivate_handlers; static zend_module_entry **modules_dl_loaded; @@ -2572,6 +2573,7 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */ int startup_count = 0; int shutdown_count = 0; int post_deactivate_count = 0; + int begin_count = 0; int dl_loaded_count = 0; /* Collect extensions with request startup/shutdown handlers */ @@ -2579,9 +2581,12 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */ if (module->request_startup_func) { startup_count++; } - if (module->request_shutdown_func) { + if (module->request_shutdown_func || module->request_end_func) { shutdown_count++; } + if (module->request_begin_func) { + begin_count++; + } if (module->post_deactivate_func) { post_deactivate_count++; } @@ -2594,22 +2599,29 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */ sizeof(zend_module_entry*) * (startup_count + 1 + shutdown_count + 1 + - post_deactivate_count + 1), true); + post_deactivate_count + 1 + + begin_count + 1), true); module_request_startup_handlers[startup_count] = NULL; module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1; module_request_shutdown_handlers[shutdown_count] = NULL; module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1; module_post_deactivate_handlers[post_deactivate_count] = NULL; + module_request_begin_handlers = module_post_deactivate_handlers + post_deactivate_count + 1; + module_request_begin_handlers[begin_count] = NULL; /* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */ modules_dl_loaded = perealloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1), true); modules_dl_loaded[dl_loaded_count] = NULL; startup_count = 0; + begin_count = 0; ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) { if (module->request_startup_func) { module_request_startup_handlers[startup_count++] = module; } - if (module->request_shutdown_func) { + if (module->request_begin_func) { + module_request_begin_handlers[begin_count++] = module; + } + if (module->request_shutdown_func || module->request_end_func) { module_request_shutdown_handlers[--shutdown_count] = module; } if (module->post_deactivate_func) { @@ -3468,66 +3480,136 @@ ZEND_API void zend_activate_modules(void) /* {{{ */ } /* }}} */ -ZEND_API zend_result zend_request_begin_modules(void) +/* A bailout doesn't skip the next modules: each module entering its request_begin_func gets its request_end_func */ +static zend_result zend_request_begin_module(const zend_module_entry *module) { - zend_module_entry *module; zend_result result = SUCCESS; - ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) { - if (module->request_begin_func - && module->request_begin_func(module->type, module->module_number) == FAILURE) { + zend_try { + if (module->request_begin_func(module->type, module->module_number) == FAILURE) { zend_error(E_WARNING, "request_begin() for %s module failed", module->name); result = FAILURE; } - } ZEND_HASH_FOREACH_END(); + } zend_catch { + result = FAILURE; + } zend_end_try(); + + return result; +} + +ZEND_API zend_result zend_request_begin_dl_module(const zend_module_entry *module) +{ + if (!(EG(flags) & EG_FLAGS_REQUEST_BEGUN) || !module->request_begin_func) { + return SUCCESS; + } + + return zend_request_begin_module(module); +} + +/* Does nothing when the hooks already began the current request */ +ZEND_API zend_result zend_request_begin_modules(void) +{ + zend_result result = SUCCESS; + + if (EG(flags) & EG_FLAGS_REQUEST_BEGUN) { + return SUCCESS; + } + EG(flags) |= EG_FLAGS_REQUEST_BEGUN; + + /* dl() loads modules missing from the handler lists */ + if (EG(full_tables_cleanup)) { + zend_module_entry *module; + + ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) { + if (module->request_begin_func && zend_request_begin_module(module) == FAILURE) { + result = FAILURE; + } + } ZEND_HASH_FOREACH_END(); + } else { + for (zend_module_entry **p = module_request_begin_handlers; *p; p++) { + if (zend_request_begin_module(*p) == FAILURE) { + result = FAILURE; + } + } + } return result; } +static zend_result zend_request_end_module(const zend_module_entry *module) +{ + zend_result result = SUCCESS; + + zend_try { + if (module->request_end_func(module->type, module->module_number) == FAILURE) { + zend_error(E_WARNING, "request_end() for %s module failed", module->name); + result = FAILURE; + } + } zend_catch { + result = FAILURE; + } zend_end_try(); + + return result; +} + +/* Does nothing when the hooks didn't begin the current request */ ZEND_API zend_result zend_request_end_modules(void) { - zend_module_entry *module; zend_result result = SUCCESS; - ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { - if (module->request_end_func) { - zend_try { - if (module->request_end_func(module->type, module->module_number) == FAILURE) { - zend_error(E_WARNING, "request_end() for %s module failed", module->name); - result = FAILURE; - } - } zend_catch { + if (!(EG(flags) & EG_FLAGS_REQUEST_BEGUN)) { + return SUCCESS; + } + EG(flags) &= ~EG_FLAGS_REQUEST_BEGUN; + + if (EG(full_tables_cleanup)) { + zend_module_entry *module; + + ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { + if (module->request_end_func && zend_request_end_module(module) == FAILURE) { result = FAILURE; - } zend_end_try(); + } + } ZEND_HASH_FOREACH_END(); + } else { + for (zend_module_entry **p = module_request_shutdown_handlers; *p; p++) { + if ((*p)->request_end_func && zend_request_end_module(*p) == FAILURE) { + result = FAILURE; + } } - } ZEND_HASH_FOREACH_END(); + } return result; } +/* A module ends the request right before its own shutdown, keeping the order other modules rely on */ +static void zend_deactivate_module(const zend_module_entry *module, bool request_begun) +{ + if (request_begun && module->request_end_func) { + zend_request_end_module(module); + } + if (module->request_shutdown_func) { + zend_try { + module->request_shutdown_func(module->type, module->module_number); + } zend_end_try(); + } +} + ZEND_API void zend_deactivate_modules(void) /* {{{ */ { + bool request_begun = EG(flags) & EG_FLAGS_REQUEST_BEGUN; + EG(current_execute_data) = NULL; /* we're no longer executing anything */ + EG(flags) &= ~EG_FLAGS_REQUEST_BEGUN; if (EG(full_tables_cleanup)) { zend_module_entry *module; ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { - if (module->request_shutdown_func) { - zend_try { - module->request_shutdown_func(module->type, module->module_number); - } zend_end_try(); - } + zend_deactivate_module(module, request_begun); } ZEND_HASH_FOREACH_END(); } else { - zend_module_entry **p = module_request_shutdown_handlers; - - while (*p) { - const zend_module_entry *module = *p; - zend_try { - module->request_shutdown_func(module->type, module->module_number); - } zend_end_try(); - p++; + for (zend_module_entry **p = module_request_shutdown_handlers; *p; p++) { + zend_deactivate_module(*p, request_begun); } } } diff --git a/Zend/zend_API.h b/Zend/zend_API.h index ecf0f8808f48..e88999b44df7 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -352,6 +352,8 @@ ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module, i ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module); ZEND_API void zend_startup_modules(void); ZEND_API void zend_collect_module_handlers(void); +/* Begins the request for a module loaded with dl() after the other modules began it */ +ZEND_API zend_result zend_request_begin_dl_module(const zend_module_entry *module); ZEND_API void zend_destroy_modules(void); ZEND_API void zend_check_magic_method_implementation( const zend_class_entry *ce, const zend_function *fptr, const zend_string *lcname, int error_type); diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index bf93bf0013a9..f4aa2b0d7cbe 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -365,6 +365,7 @@ struct _zend_tsrm_ls_cache { #define EG_FLAGS_IN_SHUTDOWN (1<<0) #define EG_FLAGS_OBJECT_STORE_NO_REUSE (1<<1) #define EG_FLAGS_IN_RESOURCE_SHUTDOWN (1<<2) +#define EG_FLAGS_REQUEST_BEGUN (1<<3) struct _zend_ini_scanner_globals { zend_file_handle *yy_in; diff --git a/ext/session/php_session.h b/ext/session/php_session.h index 7711d9432dc3..b70dc0e63078 100644 --- a/ext/session/php_session.h +++ b/ext/session/php_session.h @@ -134,6 +134,7 @@ typedef struct _php_ps_globals { bool use_only_cookies; bool send_cookie; bool auto_start; + bool auto_start_tried; /* the current request already tried session.auto_start */ bool define_sid; const ps_module *mod; const ps_module *default_mod; diff --git a/ext/session/session.c b/ext/session/session.c index 2ca33a1b05ac..ceb35975a093 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -127,6 +127,7 @@ static inline void php_rinit_session_globals(void) PS(session_vars) = NULL; PS(module_number) = my_module_number; PS(in_rfc1867_update) = false; + PS(auto_start_tried) = false; ZVAL_UNDEF(&PS(http_session_vars)); } @@ -2940,6 +2941,7 @@ static zend_result php_rinit_session(bool auto_start) } if (auto_start) { + PS(auto_start_tried) = true; php_session_start(); } @@ -2959,7 +2961,8 @@ static PHP_RINIT_FUNCTION(session) static PHP_RBEGIN_FUNCTION(session) { - if (PS(auto_start) && PS(session_status) == php_session_none) { + if (PS(auto_start) && !PS(auto_start_tried) && PS(session_status) == php_session_none) { + PS(auto_start_tried) = true; php_session_start(); } diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index bf853bf648d6..8593e2292a8a 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -158,7 +158,7 @@ zend_module_entry basic_functions_module = { /* {{{ */ PHP_STANDARD_VERSION, /* extension version */ NO_MODULE_GLOBALS, ZEND_MODULE_POST_ZEND_DEACTIVATE_N(streams), - STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(PHP_RBEGIN(basic), PHP_REND(basic)) + STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(NULL, PHP_REND(basic)) }; /* }}} */ @@ -437,14 +437,6 @@ PHP_REND_FUNCTION(basic) return SUCCESS; } -/* Discards the URL rewriter state code running between two requests set */ -PHP_RBEGIN_FUNCTION(basic) -{ - BASIC_RSHUTDOWN_SUBMODULE(url_scanner_ex) - - return SUCCESS; -} - PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */ { if (BG(strtok_string)) { diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h index fb2ea8ca4ae5..142ea4b0f2a1 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -39,7 +39,6 @@ PHP_MSHUTDOWN_FUNCTION(basic); PHP_RINIT_FUNCTION(basic); PHP_RSHUTDOWN_FUNCTION(basic); PHP_REND_FUNCTION(basic); -PHP_RBEGIN_FUNCTION(basic); PHP_MINFO_FUNCTION(basic); ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini *syntax_highlighter_ini); diff --git a/ext/standard/dl.c b/ext/standard/dl.c index ad2035fb5c74..f7f73f2895a0 100644 --- a/ext/standard/dl.c +++ b/ext/standard/dl.c @@ -249,6 +249,9 @@ PHPAPI zend_result php_load_extension(const char *filename, int type, bool start return FAILURE; } } + if ((type == MODULE_TEMPORARY || start_now) && zend_request_begin_dl_module(module_entry) == FAILURE) { + return FAILURE; + } return SUCCESS; } /* }}} */ diff --git a/ext/zend_test/php_test.h b/ext/zend_test/php_test.h index 84362bec53bd..7418253c4fa2 100644 --- a/ext/zend_test/php_test.h +++ b/ext/zend_test/php_test.h @@ -59,6 +59,7 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test) bool print_stderr_mshutdown; bool request_begin_fails; bool request_end_fails; + bool print_request_hooks; zend_long limit_copy_file_range; int observe_opline_in_zendmm; zend_mm_heap* zend_orig_heap; diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 44d4f5e0cc8b..62995cabf834 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -2308,6 +2308,7 @@ PHP_INI_BEGIN() STD_PHP_INI_BOOLEAN("zend_test.print_stderr_mshutdown", "0", PHP_INI_SYSTEM, OnUpdateBool, print_stderr_mshutdown, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.request_begin_fails", "0", PHP_INI_ALL, OnUpdateBool, request_begin_fails, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.request_end_fails", "0", PHP_INI_ALL, OnUpdateBool, request_end_fails, zend_zend_test_globals, zend_test_globals) + STD_PHP_INI_BOOLEAN("zend_test.print_request_hooks", "0", PHP_INI_ALL, OnUpdateBool, print_request_hooks, zend_zend_test_globals, zend_test_globals) #ifdef HAVE_COPY_FILE_RANGE STD_PHP_INI_ENTRY("zend_test.limit_copy_file_range", "-1", PHP_INI_ALL, OnUpdateLong, limit_copy_file_range, zend_zend_test_globals, zend_test_globals) #endif @@ -2592,16 +2593,29 @@ PHP_RSHUTDOWN_FUNCTION(zend_test) } zend_test_mm_custom_handlers_rshutdown(); + + if (ZT_G(print_request_hooks)) { + php_printf("request_shutdown\n"); + } + return SUCCESS; } static PHP_RBEGIN_FUNCTION(zend_test) { + if (ZT_G(print_request_hooks)) { + php_printf("request_begin\n"); + } + return ZT_G(request_begin_fails) ? FAILURE : SUCCESS; } static PHP_REND_FUNCTION(zend_test) { + if (ZT_G(print_request_hooks)) { + php_printf("request_end\n"); + } + return ZT_G(request_end_fails) ? FAILURE : SUCCESS; } diff --git a/ext/zend_test/tests/request_hooks_begin_failure.phpt b/ext/zend_test/tests/request_hooks_begin_failure.phpt new file mode 100644 index 000000000000..2a998c5b3404 --- /dev/null +++ b/ext/zend_test/tests/request_hooks_begin_failure.phpt @@ -0,0 +1,37 @@ +--TEST-- +A failing request_begin hook keeps the script from running and fails the process +--EXTENSIONS-- +zend_test +--FILE-- + +--CLEAN-- + +--EXPECTF-- +array(2) { + [0]=> + string(0) "" + [1]=> + string(%d) "Warning: request_begin() for zend_test module failed in %s" +} +int(255) +array(2) { + [0]=> + string(0) "" + [1]=> + string(%d) "Warning: request_begin() for zend_test module failed in %s" +} +int(255) diff --git a/ext/zend_test/tests/request_hooks_cli.phpt b/ext/zend_test/tests/request_hooks_cli.phpt new file mode 100644 index 000000000000..7e6c75e050ea --- /dev/null +++ b/ext/zend_test/tests/request_hooks_cli.phpt @@ -0,0 +1,34 @@ +--TEST-- +Request hooks begin for command line code, not when linting or when no code runs +--EXTENSIONS-- +zend_test +--FILE-- + +--CLEAN-- + +--EXPECTF-- +No syntax errors detected in %s +request_shutdown +request_begin +1 +request_end +request_shutdown +request_begin +input +request_end +request_shutdown +empty input +request_shutdown diff --git a/ext/zend_test/tests/request_hooks_order.phpt b/ext/zend_test/tests/request_hooks_order.phpt new file mode 100644 index 000000000000..f11f8c3dde6f --- /dev/null +++ b/ext/zend_test/tests/request_hooks_order.phpt @@ -0,0 +1,20 @@ +--TEST-- +Request hooks begin when userland code runs and end right before the module shutdown +--EXTENSIONS-- +zend_test +--INI-- +zend_test.print_request_hooks=1 +--FILE-- + +--EXPECT-- +request_begin +first request +request_end +request_begin +next request +request_end +request_shutdown diff --git a/ext/zend_test/tests/request_hooks_session_auto_start.phpt b/ext/zend_test/tests/request_hooks_session_auto_start.phpt new file mode 100644 index 000000000000..fe3a7ac3da71 --- /dev/null +++ b/ext/zend_test/tests/request_hooks_session_auto_start.phpt @@ -0,0 +1,20 @@ +--TEST-- +Request hooks don't retry a failed session.auto_start in the request RINIT already tried it for +--EXTENSIONS-- +session +zend_test +--INI-- +session.auto_start=1 +session.save_handler=files +session.save_path=/nonexistent/request_hooks +session.use_cookies=0 +session.cache_limiter= +--FILE-- + +--EXPECTF-- +Warning: PHP Request Startup: open(%s) failed: %s in Unknown on line 0 + +Warning: PHP Request Startup: Failed to read session data: files (path: %s) in Unknown on line 0 +bool(true) diff --git a/ext/zend_test/tests/request_hooks_session_order.phpt b/ext/zend_test/tests/request_hooks_session_order.phpt new file mode 100644 index 000000000000..9bb15e21fe75 --- /dev/null +++ b/ext/zend_test/tests/request_hooks_session_order.phpt @@ -0,0 +1,40 @@ +--TEST-- +Request hooks keep the session write at the place of the session module shutdown +--EXTENSIONS-- +session +zend_test +--SKIPIF-- + array_search('zend_test', $extensions)) { + die('skip session must be loaded before zend_test'); +} +?> +--INI-- +session.use_cookies=0 +session.cache_limiter= +--FILE-- + +--EXPECT-- +request_end +request_shutdown +session write diff --git a/main/main.c b/main/main.c index b2afc096e918..85f4987ca7f6 100644 --- a/main/main.c +++ b/main/main.c @@ -2038,6 +2038,8 @@ PHPAPI zend_result php_request_begin(void) /* the script reads the previous request's superglobals until now */ php_release_request_globals(); php_release_sapi_globals(); + /* code running between two requests can enable the URL rewriter too */ + PHP_RSHUTDOWN(url_scanner_ex)(MODULE_PERSISTENT, 0); php_output_activate(); @@ -2143,7 +2145,7 @@ void php_request_shutdown(void *dummy) zend_unset_timeout(); } zend_end_try(); - /* 5. Call all extensions RSHUTDOWN functions */ + /* 5. Call all extensions REND and RSHUTDOWN functions */ if (PG(modules_activated)) { zend_deactivate_modules(); } @@ -2791,6 +2793,15 @@ PHPAPI bool php_execute_script_ex(zend_file_handle *primary_file, zval *retval) zend_set_timeout(zend_ini_long_literal("max_execution_time"), false); } + /* userland code runs from here, unlike when linting or serving a status page */ + if (zend_request_begin_modules() == FAILURE) { + result = false; + EG(exit_status) = 255; + if (!SG(headers_sent)) { + SG(sapi_headers).http_response_code = 500; + } + } + if (prepend_file_p && result) { result = zend_execute_script(ZEND_REQUIRE, NULL, prepend_file_p) == SUCCESS; } diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index fc62c0dd4b46..d7543b2cbe26 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -508,6 +508,19 @@ static void php_cli_usage(char *argv0) static php_stream *s_in_process = NULL; +/* code run with zend_eval_string() doesn't go through php_execute_script(), which begins the request hooks */ +static bool cli_request_begin(void) +{ + static bool failed = false; + + if (!failed && zend_request_begin_modules() == FAILURE) { + failed = true; + EG(exit_status) = 255; + } + + return !failed; +} + static void cli_register_file_handles(void) { php_stream *s_in, *s_out, *s_err; @@ -938,7 +951,9 @@ static int do_cli(int argc, char **argv) /* {{{ */ cli_register_file_handles(); if (interactive) { - EG(exit_status) = cli_shell_callbacks.cli_shell_run(); + if (cli_request_begin()) { + EG(exit_status) = cli_shell_callbacks.cli_shell_run(); + } } else { php_execute_script(&file_handle); } @@ -970,7 +985,9 @@ static int do_cli(int argc, char **argv) /* {{{ */ break; case PHP_CLI_MODE_CLI_DIRECT: cli_register_file_handles(); - zend_eval_string_ex(exec_direct, NULL, "Command line code", 1); + if (cli_request_begin()) { + zend_eval_string_ex(exec_direct, NULL, "Command line code", 1); + } break; case PHP_CLI_MODE_PROCESS_STDIN: @@ -986,7 +1003,7 @@ static int do_cli(int argc, char **argv) /* {{{ */ cli_register_file_handles(); - if (exec_begin) { + if (exec_begin && cli_request_begin()) { zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1); } while (EG(exit_status) == SUCCESS && (input = php_stream_gets(s_in_process, NULL, 0)) != NULL) { @@ -999,7 +1016,9 @@ static int do_cli(int argc, char **argv) /* {{{ */ ZVAL_LONG(&argi, ++index); zend_hash_str_update(&EG(symbol_table), "argi", sizeof("argi")-1, &argi); if (exec_run) { - zend_eval_string_ex(exec_run, NULL, "Command line run code", 1); + if (cli_request_begin()) { + zend_eval_string_ex(exec_run, NULL, "Command line run code", 1); + } } else { if (script_file) { if (cli_seek_file_begin(&file_handle, script_file) == FAILURE) { @@ -1012,7 +1031,7 @@ static int do_cli(int argc, char **argv) /* {{{ */ } efree(input); } - if (exec_end) { + if (exec_end && cli_request_begin()) { zend_eval_string_ex(exec_end, NULL, "Command line end code", 1); } diff --git a/sapi/embed/php_embed.c b/sapi/embed/php_embed.c index 3be8db4cc014..fa041e11ce92 100644 --- a/sapi/embed/php_embed.c +++ b/sapi/embed/php_embed.c @@ -238,6 +238,13 @@ EMBED_SAPI_API int php_embed_init(int argc, char **argv) SG(request_info).no_headers = 1; php_register_variable("PHP_SELF", "-", NULL); + /* embedders may run code with zend_eval_string() instead of php_execute_script() */ + if (zend_request_begin_modules() == FAILURE) { + php_request_shutdown(NULL); + php_module_shutdown(); + return FAILURE; + } + return SUCCESS; } diff --git a/sapi/phpdbg/phpdbg_prompt.c b/sapi/phpdbg/phpdbg_prompt.c index fe8502da47e9..2fabb21b7a3a 100644 --- a/sapi/phpdbg/phpdbg_prompt.c +++ b/sapi/phpdbg/phpdbg_prompt.c @@ -203,7 +203,9 @@ static void phpdbg_line_init(char *cmd, struct phpdbg_init_state *state) { if (memcmp(cmd, ":>", sizeof(":>")-1) == SUCCESS) { state->in_code = 0; state->code[state->code_len] = '\0'; - zend_eval_stringl(state->code, state->code_len, NULL, "phpdbginit code"); + if (zend_request_begin_modules() == SUCCESS) { + zend_eval_stringl(state->code, state->code_len, NULL, "phpdbginit code"); + } free(state->code); state->code = NULL; return; @@ -855,7 +857,9 @@ PHPDBG_COMMAND(run) /* {{{ */ zend_try { PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE; PHPDBG_G(flags) |= PHPDBG_IS_RUNNING; - zend_execute(PHPDBG_G(ops), &PHPDBG_G(retval)); + if (zend_request_begin_modules() == SUCCESS) { + zend_execute(PHPDBG_G(ops), &PHPDBG_G(retval)); + } PHPDBG_G(flags) ^= PHPDBG_IS_INTERACTIVE; } zend_catch { PHPDBG_G(in_execution) = 0; @@ -941,7 +945,8 @@ PHPDBG_COMMAND(ev) /* {{{ */ /* disable stepping while eval() in progress */ PHPDBG_G(flags) |= PHPDBG_IN_EVAL; zend_try { - if (zend_eval_stringl(param->str, param->len, &retval, "eval()'d code") == SUCCESS) { + if (zend_request_begin_modules() == SUCCESS + && zend_eval_stringl(param->str, param->len, &retval, "eval()'d code") == SUCCESS) { if (EG(exception)) { ex = EG(exception); zend_exception_error(EG(exception), E_ERROR);