Skip to content

Workaround ZTS persistent resource crashes (PHP 8.3 and lower) #13388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,8 @@ void zend_shutdown(void) /* {{{ */
#endif
zend_destroy_rsrc_list_dtors();

zend_unload_modules();

zend_optimizer_shutdown();
startup_done = false;
}
Expand Down
1 change: 1 addition & 0 deletions Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ void zend_shutdown(void);
void zend_register_standard_ini_entries(void);
zend_result zend_post_startup(void);
void zend_set_utility_values(zend_utility_values *utility_values);
void zend_unload_modules(void);

ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno);
ZEND_API size_t zend_get_page_size(void);
Expand Down
43 changes: 37 additions & 6 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ZEND_API HashTable module_registry;
static zend_module_entry **module_request_startup_handlers;
static zend_module_entry **module_request_shutdown_handlers;
static zend_module_entry **module_post_deactivate_handlers;
static zend_module_entry **modules_dl_loaded;

static zend_class_entry **class_cleanup_handlers;

Expand Down Expand Up @@ -2292,6 +2293,7 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
int startup_count = 0;
int shutdown_count = 0;
int post_deactivate_count = 0;
int dl_loaded_count = 0;
zend_class_entry *ce;
int class_count = 0;

Expand All @@ -2306,6 +2308,9 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
if (module->post_deactivate_func) {
post_deactivate_count++;
}
if (module->handle) {
dl_loaded_count++;
}
} ZEND_HASH_FOREACH_END();
module_request_startup_handlers = (zend_module_entry**)realloc(
module_request_startup_handlers,
Expand All @@ -2318,6 +2323,9 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
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;
/* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */
Copy link
Member Author

@nielsdos nielsdos Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to move freeing module_request_startup_handlers to zend_unload_modules, but that changes public API.
And we want to avoid that in stable branches. The reason for this different workaround for lower branches was to avoid changing the public API in the first place.

modules_dl_loaded = realloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1));
modules_dl_loaded[dl_loaded_count] = NULL;
startup_count = 0;

ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
Expand All @@ -2330,6 +2338,9 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
if (module->post_deactivate_func) {
module_post_deactivate_handlers[--post_deactivate_count] = module;
}
if (module->handle) {
modules_dl_loaded[--dl_loaded_count] = module;
}
} ZEND_HASH_FOREACH_END();

/* Collect internal classes with static members */
Expand Down Expand Up @@ -3073,18 +3084,23 @@ void module_destructor(zend_module_entry *module) /* {{{ */
clean_module_functions(module);
}

#if HAVE_LIBDL
if (module->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
DL_UNLOAD(module->handle);
}
#endif

#if ZEND_RC_DEBUG
zend_rc_debug = orig_rc_debug;
#endif
}
/* }}} */

void module_registry_unload(const zend_module_entry *module)
{
#if HAVE_LIBDL
if (!getenv("ZEND_DONT_UNLOAD_MODULES")) {
DL_UNLOAD(module->handle);
}
#else
ZEND_IGNORE_VALUE(module);
#endif
}

ZEND_API void zend_activate_modules(void) /* {{{ */
{
zend_module_entry **p = module_request_startup_handlers;
Expand Down Expand Up @@ -3129,6 +3145,18 @@ ZEND_API void zend_deactivate_modules(void) /* {{{ */
}
/* }}} */

void zend_unload_modules(void) /* {{{ */
{
zend_module_entry **modules = modules_dl_loaded;
while (*modules) {
module_registry_unload(*modules);
modules++;
}
free(modules_dl_loaded);
modules_dl_loaded = NULL;
}
/* }}} */

ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
{
if (EG(full_tables_cleanup)) {
Expand All @@ -3147,6 +3175,9 @@ ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
break;
}
module_destructor(module);
if (module->handle) {
module_registry_unload(module);
}
zend_string_release_ex(key, 0);
} ZEND_HASH_MAP_FOREACH_END_DEL();
} else {
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ extern ZEND_API HashTable module_registry;

void module_destructor(zend_module_entry *module);
int module_registry_request_startup(zend_module_entry *module);
int module_registry_unload_temp(const zend_module_entry *module);
void module_registry_unload(const zend_module_entry *module);
END_EXTERN_C()

#endif