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
26 changes: 26 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ 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. Declare them with PHP_RBEGIN_FUNCTION() and PHP_REND_FUNCTION(), and
set them with STANDARD_MODULE_PROPERTIES_REQUEST_HOOKS(). request_begin_func
runs when userland code first runs for a request, so not when linting, when
preloading or when FPM serves a status page. request_end_func runs right
before the module's request_shutdown_func, or between two requests served
by the same script, and only if request_begin_func ran. It releases only the
state built from the finished request or tied to its response. Other state
is shared by the requests, as in a long-running CLI script.
- Added a new zend_object_copy() function which increases the refcount of
a zend_object* and returns it. Allowing more descriptive code rather than
doing an assignment followed by GC_ADDREF().
Expand Down Expand Up @@ -110,6 +119,23 @@ 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.
- php_execute_script() calls the request_begin_func hooks. SAPIs running
userland code with zend_eval_string() instead call
zend_request_begin_modules() first, which does nothing if the hooks already
ran for the current request. The CLI, phpdbg and php_embed_init() do. When
a hook fails, php_execute_script() doesn't run the script, sets the exit
status to 255 and the HTTP response code to 500.
- Added php_release_request_globals(), called by php_request_begin() before
the output of the new request is active. The script reads the superglobals
of the finished request until then. SAPIs running userland code between two
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
151 changes: 135 additions & 16 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ZEND_API bool zend_dl_use_deepbind = false;

static zend_module_entry **module_request_startup_handlers;
static zend_module_entry **module_request_shutdown_handlers;
static zend_module_entry **module_request_begin_handlers;
static zend_module_entry **module_post_deactivate_handlers;
static zend_module_entry **modules_dl_loaded;

Expand Down Expand Up @@ -2572,16 +2573,20 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
int startup_count = 0;
int shutdown_count = 0;
int post_deactivate_count = 0;
int begin_count = 0;
int dl_loaded_count = 0;

/* Collect extensions with request startup/shutdown handlers */
ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
if (module->request_startup_func) {
startup_count++;
}
if (module->request_shutdown_func) {
if (module->request_shutdown_func || module->request_end_func) {
shutdown_count++;
}
if (module->request_begin_func) {
begin_count++;
}
if (module->post_deactivate_func) {
post_deactivate_count++;
}
Expand All @@ -2594,22 +2599,29 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
sizeof(zend_module_entry*) *
(startup_count + 1 +
shutdown_count + 1 +
post_deactivate_count + 1), true);
post_deactivate_count + 1 +
begin_count + 1), true);
module_request_startup_handlers[startup_count] = NULL;
module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
module_request_shutdown_handlers[shutdown_count] = NULL;
module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
module_post_deactivate_handlers[post_deactivate_count] = NULL;
module_request_begin_handlers = module_post_deactivate_handlers + post_deactivate_count + 1;
module_request_begin_handlers[begin_count] = NULL;
/* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */
modules_dl_loaded = perealloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1), true);
modules_dl_loaded[dl_loaded_count] = NULL;
startup_count = 0;
begin_count = 0;

ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
if (module->request_startup_func) {
module_request_startup_handlers[startup_count++] = module;
}
if (module->request_shutdown_func) {
if (module->request_begin_func) {
module_request_begin_handlers[begin_count++] = module;
}
if (module->request_shutdown_func || module->request_end_func) {
module_request_shutdown_handlers[--shutdown_count] = module;
}
if (module->post_deactivate_func) {
Expand Down Expand Up @@ -3468,29 +3480,136 @@ ZEND_API void zend_activate_modules(void) /* {{{ */
}
/* }}} */

ZEND_API void zend_deactivate_modules(void) /* {{{ */
/* A bailout doesn't skip the next modules: each module entering its request_begin_func gets its request_end_func */
static zend_result zend_request_begin_module(const zend_module_entry *module)
{
EG(current_execute_data) = NULL; /* we're no longer executing anything */
zend_result result = SUCCESS;

zend_try {
if (module->request_begin_func(module->type, module->module_number) == FAILURE) {
zend_error(E_WARNING, "request_begin() for %s module failed", module->name);
result = FAILURE;
}
} zend_catch {
result = FAILURE;
} zend_end_try();

return result;
}

ZEND_API zend_result zend_request_begin_dl_module(const zend_module_entry *module)
{
if (!(EG(flags) & EG_FLAGS_REQUEST_BEGUN) || !module->request_begin_func) {
return SUCCESS;
}

return zend_request_begin_module(module);
}

/* Does nothing when the hooks already began the current request */
ZEND_API zend_result zend_request_begin_modules(void)
{
zend_result result = SUCCESS;

if (EG(flags) & EG_FLAGS_REQUEST_BEGUN) {
return SUCCESS;
}
EG(flags) |= EG_FLAGS_REQUEST_BEGUN;

/* dl() loads modules missing from the handler lists */
if (EG(full_tables_cleanup)) {
zend_module_entry *module;

ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
if (module->request_begin_func && zend_request_begin_module(module) == FAILURE) {
result = FAILURE;
}
} ZEND_HASH_FOREACH_END();
} else {
for (zend_module_entry **p = module_request_begin_handlers; *p; p++) {
if (zend_request_begin_module(*p) == FAILURE) {
result = FAILURE;
}
}
}

return result;
}

static zend_result zend_request_end_module(const zend_module_entry *module)
{
zend_result result = SUCCESS;

zend_try {
if (module->request_end_func(module->type, module->module_number) == FAILURE) {
zend_error(E_WARNING, "request_end() for %s module failed", module->name);
result = FAILURE;
}
} zend_catch {
result = FAILURE;
} zend_end_try();

return result;
}

/* Does nothing when the hooks didn't begin the current request */
ZEND_API zend_result zend_request_end_modules(void)
{
zend_result result = SUCCESS;

if (!(EG(flags) & EG_FLAGS_REQUEST_BEGUN)) {
return SUCCESS;
}
EG(flags) &= ~EG_FLAGS_REQUEST_BEGUN;

if (EG(full_tables_cleanup)) {
zend_module_entry *module;

ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) {
if (module->request_shutdown_func) {
zend_try {
module->request_shutdown_func(module->type, module->module_number);
} zend_end_try();
if (module->request_end_func && zend_request_end_module(module) == FAILURE) {
result = FAILURE;
}
} ZEND_HASH_FOREACH_END();
} else {
zend_module_entry **p = module_request_shutdown_handlers;
for (zend_module_entry **p = module_request_shutdown_handlers; *p; p++) {
if ((*p)->request_end_func && zend_request_end_module(*p) == FAILURE) {
result = FAILURE;
}
}
}

while (*p) {
const zend_module_entry *module = *p;
zend_try {
module->request_shutdown_func(module->type, module->module_number);
} zend_end_try();
p++;
return result;
}

/* A module ends the request right before its own shutdown, keeping the order other modules rely on */
static void zend_deactivate_module(const zend_module_entry *module, bool request_begun)
{
if (request_begun && module->request_end_func) {
zend_request_end_module(module);
}
if (module->request_shutdown_func) {
zend_try {
module->request_shutdown_func(module->type, module->module_number);
} zend_end_try();
}
}

ZEND_API void zend_deactivate_modules(void) /* {{{ */
{
bool request_begun = EG(flags) & EG_FLAGS_REQUEST_BEGUN;

EG(current_execute_data) = NULL; /* we're no longer executing anything */
EG(flags) &= ~EG_FLAGS_REQUEST_BEGUN;

if (EG(full_tables_cleanup)) {
zend_module_entry *module;

ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) {
zend_deactivate_module(module, request_begun);
} ZEND_HASH_FOREACH_END();
} else {
for (zend_module_entry **p = module_request_shutdown_handlers; *p; p++) {
zend_deactivate_module(*p, request_begun);
}
}
}
Expand Down
10 changes: 10 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 @@ -348,6 +352,8 @@ ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module, i
ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module);
ZEND_API void zend_startup_modules(void);
ZEND_API void zend_collect_module_handlers(void);
/* Begins the request for a module loaded with dl() after the other modules began it */
ZEND_API zend_result zend_request_begin_dl_module(const zend_module_entry *module);
ZEND_API void zend_destroy_modules(void);
ZEND_API void zend_check_magic_method_implementation(
const zend_class_entry *ce, const zend_function *fptr, const zend_string *lcname, int error_type);
Expand Down Expand Up @@ -1156,6 +1162,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 +1172,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
1 change: 1 addition & 0 deletions Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ struct _zend_tsrm_ls_cache {
#define EG_FLAGS_IN_SHUTDOWN (1<<0)
#define EG_FLAGS_OBJECT_STORE_NO_REUSE (1<<1)
#define EG_FLAGS_IN_RESOURCE_SHUTDOWN (1<<2)
#define EG_FLAGS_REQUEST_BEGUN (1<<3)

struct _zend_ini_scanner_globals {
zend_file_handle *yy_in;
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
1 change: 1 addition & 0 deletions ext/session/php_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ typedef struct _php_ps_globals {
bool use_only_cookies;
bool send_cookie;
bool auto_start;
bool auto_start_tried; /* the current request already tried session.auto_start */
bool define_sid;
const ps_module *mod;
const ps_module *default_mod;
Expand Down
Loading