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/7] 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/7] 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/7] 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/7] 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/7] 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/7] 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/7] 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);