Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions Zend/tests/arginfo_zpp_mismatch.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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_'))
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
37 changes: 37 additions & 0 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
8 changes: 8 additions & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)))
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions Zend/zend_modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion ext/filter/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
};
/* }}} */

Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions ext/filter/php_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 43 additions & 1 deletion ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
};

Expand Down
18 changes: 17 additions & 1 deletion ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
};
/* }}} */

Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion ext/standard/php_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/* }}} */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/php_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading