diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index b5134705b375..3037185d0817 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,17 @@ 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. 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 + 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/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..dca14421867b 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 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 c9f5ed38aee5..951d5f7b87c8 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -3468,6 +3468,43 @@ ZEND_API void zend_activate_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 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 { + result = FAILURE; + } zend_end_try(); + } + } ZEND_HASH_FOREACH_END(); + + return result; +} + 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/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/session/session.c b/ext/session/session.c index c15941b74523..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; @@ -2945,6 +2957,36 @@ 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; + } + + return SUCCESS; +} + static PHP_RSHUTDOWN_FUNCTION(session) { if (PS(session_status) == php_session_active) { @@ -3469,7 +3511,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/standard/basic_functions.c b/ext/standard/basic_functions.c index db302ab724ab..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_EX + STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(PHP_RBEGIN(basic), PHP_REND(basic)) }; /* }}} */ @@ -429,6 +429,22 @@ 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; +} + +/* 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 10dfd584d704..fb2ea8ca4ae5 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -38,6 +38,8 @@ PHP_MINIT_FUNCTION(basic); 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/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/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 fc346dd5aa0a..44d4f5e0cc8b 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,24 @@ static ZEND_FUNCTION(zend_test_void_return) ZEND_PARSE_PARAMETERS_NONE(); } +static ZEND_FUNCTION(zend_test_request_boundary) +{ + 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(); + + bool ended = php_request_end() == SUCCESS; + if (ZEND_FCI_INITIALIZED(fci)) { + zend_call_known_fcc(&fcc, NULL, 0, NULL, NULL); + } + bool begun = php_request_begin() == SUCCESS; + RETURN_BOOL(ended && begun); +} + static void pass1(zend_script *script, void *context) { php_printf("pass1\n"); @@ -2287,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 @@ -2574,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) @@ -2618,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/test.stub.php b/ext/zend_test/test.stub.php index 87ad8c9b93c8..0f40f34775ad 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, 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 {} /** @deprecated */ diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index 314d66a2799a..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: 64eb27c89a9a5a378385e5f778cd17cfd67caed8 + * Stub hash: 0d4aef6dc380491c756b7db128db865464bd22c9 * Has decl header: yes */ #include "zend_attributes.h" @@ -24,6 +24,10 @@ 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_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) ZEND_ARG_TYPE_INFO(0, source_string, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) @@ -476,6 +480,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 +635,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..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: 64eb27c89a9a5a378385e5f778cd17cfd67caed8 */ + * Stub hash: 0d4aef6dc380491c756b7db128db865464bd22c9 */ -#ifndef ZEND_TEST_DECL_64eb27c89a9a5a378385e5f778cd17cfd67caed8_H -#define ZEND_TEST_DECL_64eb27c89a9a5a378385e5f778cd17cfd67caed8_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_64eb27c89a9a5a378385e5f778cd17cfd67caed8_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 f941cf44c573..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: 64eb27c89a9a5a378385e5f778cd17cfd67caed8 + * Stub hash: 0d4aef6dc380491c756b7db128db865464bd22c9 * Has decl header: yes */ #include "zend_constants.h" @@ -20,6 +20,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_zend_test_forward_reference, 0, 1, 1) ZEND_ARG_INFO(1, value) ZEND_END_ARG_INFO() +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) ZEND_ARG_INFO(0, filename) @@ -400,6 +404,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 +551,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/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_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" 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/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/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) 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 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 962739df4219..b2afc096e918 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,136 @@ 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(); +} + +/* 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) +{ + zend_result retval = SUCCESS; + + zend_try { + /* the script reads the previous request's superglobals until now */ + php_release_request_globals(); + php_release_sapi_globals(); + + 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(); + retval = zend_request_begin_modules(); + } zend_catch { + retval = FAILURE; + } zend_end_try(); + + SG(sapi_started) = 1; + + return retval; +} + +/* Ends a request without ending the script serving it. */ +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(); + + 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 */ 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..359847d2894f 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 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); 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 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); }