Skip to content

Commit 6c73573

Browse files
committed
Merge branch 'PHP-8.3'
* PHP-8.3: Workaround ZTS persistent resource crashes (PHP 8.3 and lower)
2 parents 847c745 + 3ab7aa0 commit 6c73573

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

Zend/zend.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,8 @@ void zend_shutdown(void) /* {{{ */
11831183
#endif
11841184
zend_destroy_rsrc_list_dtors();
11851185

1186+
zend_unload_modules();
1187+
11861188
zend_optimizer_shutdown();
11871189
startup_done = false;
11881190
}

Zend/zend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ void zend_shutdown(void);
279279
void zend_register_standard_ini_entries(void);
280280
zend_result zend_post_startup(void);
281281
void zend_set_utility_values(zend_utility_values *utility_values);
282+
void zend_unload_modules(void);
282283

283284
ZEND_API ZEND_COLD ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno);
284285
ZEND_API size_t zend_get_page_size(void);

Zend/zend_API.c

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ZEND_API HashTable module_registry;
4141
static zend_module_entry **module_request_startup_handlers;
4242
static zend_module_entry **module_request_shutdown_handlers;
4343
static zend_module_entry **module_post_deactivate_handlers;
44+
static zend_module_entry **modules_dl_loaded;
4445

4546
static zend_class_entry **class_cleanup_handlers;
4647

@@ -2398,6 +2399,7 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
23982399
int startup_count = 0;
23992400
int shutdown_count = 0;
24002401
int post_deactivate_count = 0;
2402+
int dl_loaded_count = 0;
24012403
zend_class_entry *ce;
24022404
int class_count = 0;
24032405

@@ -2412,6 +2414,9 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
24122414
if (module->post_deactivate_func) {
24132415
post_deactivate_count++;
24142416
}
2417+
if (module->handle) {
2418+
dl_loaded_count++;
2419+
}
24152420
} ZEND_HASH_FOREACH_END();
24162421
module_request_startup_handlers = (zend_module_entry**)realloc(
24172422
module_request_startup_handlers,
@@ -2424,6 +2429,9 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
24242429
module_request_shutdown_handlers[shutdown_count] = NULL;
24252430
module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
24262431
module_post_deactivate_handlers[post_deactivate_count] = NULL;
2432+
/* Cannot reuse module_request_startup_handlers because it is freed in zend_destroy_modules, which happens before zend_unload_modules. */
2433+
modules_dl_loaded = realloc(modules_dl_loaded, sizeof(zend_module_entry*) * (dl_loaded_count + 1));
2434+
modules_dl_loaded[dl_loaded_count] = NULL;
24272435
startup_count = 0;
24282436

24292437
ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
@@ -2436,6 +2444,9 @@ ZEND_API void zend_collect_module_handlers(void) /* {{{ */
24362444
if (module->post_deactivate_func) {
24372445
module_post_deactivate_handlers[--post_deactivate_count] = module;
24382446
}
2447+
if (module->handle) {
2448+
modules_dl_loaded[--dl_loaded_count] = module;
2449+
}
24392450
} ZEND_HASH_FOREACH_END();
24402451

24412452
/* Collect internal classes with static members */
@@ -3238,18 +3249,23 @@ void module_destructor(zend_module_entry *module) /* {{{ */
32383249
clean_module_functions(module);
32393250
}
32403251

3241-
#if HAVE_LIBDL
3242-
if (module->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
3243-
DL_UNLOAD(module->handle);
3244-
}
3245-
#endif
3246-
32473252
#if ZEND_RC_DEBUG
32483253
zend_rc_debug = orig_rc_debug;
32493254
#endif
32503255
}
32513256
/* }}} */
32523257

3258+
void module_registry_unload(const zend_module_entry *module)
3259+
{
3260+
#if HAVE_LIBDL
3261+
if (!getenv("ZEND_DONT_UNLOAD_MODULES")) {
3262+
DL_UNLOAD(module->handle);
3263+
}
3264+
#else
3265+
ZEND_IGNORE_VALUE(module);
3266+
#endif
3267+
}
3268+
32533269
ZEND_API void zend_activate_modules(void) /* {{{ */
32543270
{
32553271
zend_module_entry **p = module_request_startup_handlers;
@@ -3294,6 +3310,18 @@ ZEND_API void zend_deactivate_modules(void) /* {{{ */
32943310
}
32953311
/* }}} */
32963312

3313+
void zend_unload_modules(void) /* {{{ */
3314+
{
3315+
zend_module_entry **modules = modules_dl_loaded;
3316+
while (*modules) {
3317+
module_registry_unload(*modules);
3318+
modules++;
3319+
}
3320+
free(modules_dl_loaded);
3321+
modules_dl_loaded = NULL;
3322+
}
3323+
/* }}} */
3324+
32973325
ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
32983326
{
32993327
if (EG(full_tables_cleanup)) {
@@ -3312,6 +3340,9 @@ ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
33123340
break;
33133341
}
33143342
module_destructor(module);
3343+
if (module->handle) {
3344+
module_registry_unload(module);
3345+
}
33153346
zend_string_release_ex(key, 0);
33163347
} ZEND_HASH_MAP_FOREACH_END_DEL();
33173348
} else {

Zend/zend_modules.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ extern ZEND_API HashTable module_registry;
125125

126126
void module_destructor(zend_module_entry *module);
127127
int module_registry_request_startup(zend_module_entry *module);
128-
int module_registry_unload_temp(const zend_module_entry *module);
128+
void module_registry_unload(const zend_module_entry *module);
129129
END_EXTERN_C()
130130

131131
#endif

0 commit comments

Comments
 (0)