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 1 commit
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
28 changes: 28 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,33 @@ zend_result zend_post_startup(void) /* {{{ */
}
/* }}} */

/* Returns a NULL-terminated array of module entries that were dynamically loaded. */
static zend_module_entry **zend_collect_dl_loaded_module_entries(void)
{
zend_module_entry **modules = malloc(sizeof(zend_module_entry *) * (zend_hash_num_elements(&module_registry) + 1));
zend_module_entry *module;
unsigned int index = 0;
ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) {
if (module->handle) {
modules[index++] = module;
}
} ZEND_HASH_FOREACH_END();
modules[index] = NULL;
return modules;
}

static void zend_unload_modules(zend_module_entry **modules)
{
while (*modules) {
module_registry_unload(*modules);
modules++;
}
}

void zend_shutdown(void) /* {{{ */
{
zend_module_entry **modules = zend_collect_dl_loaded_module_entries();

zend_vm_dtor();

zend_destroy_rsrc_list(&EG(persistent_list));
Expand Down Expand Up @@ -1141,6 +1166,9 @@ void zend_shutdown(void) /* {{{ */
#endif
zend_destroy_rsrc_list_dtors();

zend_unload_modules(modules);
free(modules);

zend_optimizer_shutdown();
startup_done = false;
}
Expand Down
16 changes: 11 additions & 5 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -3073,17 +3073,22 @@ void module_destructor(zend_module_entry *module) /* {{{ */
clean_module_functions(module);
}

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

void module_registry_unload(const zend_module_entry *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;
#else
ZEND_IGNORE_VALUE(module);
#endif
}
/* }}} */

ZEND_API void zend_activate_modules(void) /* {{{ */
{
Expand Down Expand Up @@ -3147,6 +3152,7 @@ ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
break;
}
module_destructor(module);
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