diff --git a/Zend/zend_API.c b/Zend/zend_API.c index da7e06040d2f..84255dee5339 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -47,6 +47,10 @@ static zend_module_entry **modules_dl_loaded; static zend_class_entry **class_cleanup_handlers; +/* Where zend_activate_modules() stopped: NULL if it did not run, the + * terminating NULL if every RINIT returned, else the RINIT that bailed out */ +ZEND_TLS zend_module_entry **rinit_stop = NULL; + ZEND_API zend_result zend_get_parameters_array_ex(uint32_t param_count, zval *argument_array) /* {{{ */ { zval *param_ptr; @@ -3415,20 +3419,56 @@ ZEND_API void zend_activate_modules(void) /* {{{ */ while (*p) { zend_module_entry *module = *p; + /* still points to this module if its RINIT bails out */ + rinit_stop = p; if (module->request_startup_func(module->type, module->module_number)==FAILURE) { zend_error(E_WARNING, "request_startup() for %s module failed", module->name); exit(1); } p++; } + + rinit_stop = p; } /* }}} */ +/* Skips the modules whose RINIT never ran or bailed out */ +static void zend_deactivate_modules_after_rinit_bailout(const zend_module_entry *bailed_out) +{ + zend_module_entry *module; + bool rinit_ran = false; + + ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { + if (module == bailed_out) { + rinit_ran = true; + continue; + } + if (!module->request_shutdown_func || (!rinit_ran && module->request_startup_func)) { + continue; + } + zend_try { + module->request_shutdown_func(module->type, module->module_number); + } zend_catch { + /* module shutdown is incomplete */ + _exit(1); + } zend_end_try(); + } ZEND_HASH_FOREACH_END(); +} + ZEND_API void zend_deactivate_modules(void) /* {{{ */ { + zend_module_entry **stop = rinit_stop; + + if (!stop) { + return; + } + rinit_stop = NULL; + EG(current_execute_data) = NULL; /* we're no longer executing anything */ - if (EG(full_tables_cleanup)) { + if (*stop) { + zend_deactivate_modules_after_rinit_bailout(*stop); + } else if (EG(full_tables_cleanup)) { zend_module_entry *module; ZEND_HASH_MAP_REVERSE_FOREACH_PTR(&module_registry, module) { diff --git a/ext/session/tests/rinit_bailout_releases_lock.phpt b/ext/session/tests/rinit_bailout_releases_lock.phpt new file mode 100644 index 000000000000..7f6ba5cd029e --- /dev/null +++ b/ext/session/tests/rinit_bailout_releases_lock.phpt @@ -0,0 +1,63 @@ +--TEST-- +A request whose startup bails out after session.auto_start releases the session lock +--EXTENSIONS-- +session +zend_test +--SKIPIF-- + +--FILE-- + +--CLEAN-- + +--EXPECT-- +[] +[rinitbailout] diff --git a/ext/session/tests/rinit_bailout_releases_lock_trans_sid.phpt b/ext/session/tests/rinit_bailout_releases_lock_trans_sid.phpt new file mode 100644 index 000000000000..206d22796f46 --- /dev/null +++ b/ext/session/tests/rinit_bailout_releases_lock_trans_sid.phpt @@ -0,0 +1,47 @@ +--TEST-- +A request whose startup bails out after session.auto_start with session.use_trans_sid +--EXTENSIONS-- +session +zend_test +--SKIPIF-- + +--INI-- +default_socket_timeout=5 +--FILE-- + +--CLEAN-- + +--EXPECT-- +string(0) "" +string(20) "rinitbailouttranssid" diff --git a/ext/zend_test/php_test.h b/ext/zend_test/php_test.h index 9f06d960c4ff..78f511d38584 100644 --- a/ext/zend_test/php_test.h +++ b/ext/zend_test/php_test.h @@ -61,6 +61,8 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test) bool rearm_vm_interrupt; bool print_stderr_mshutdown; bool warn_on_startup; + zend_long bailout_in_rinit; + zend_string *rinit_bailout_putenv; zend_long limit_copy_file_range; int observe_opline_in_zendmm; zend_mm_heap* zend_orig_heap; diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 2d106337c5cb..5c0dbac00336 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -1407,6 +1407,8 @@ PHP_INI_BEGIN() STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.rearm_vm_interrupt", "0", PHP_INI_SYSTEM, OnUpdateBool, rearm_vm_interrupt, zend_zend_test_globals, zend_test_globals) STD_PHP_INI_BOOLEAN("zend_test.print_stderr_mshutdown", "0", PHP_INI_SYSTEM, OnUpdateBool, print_stderr_mshutdown, zend_zend_test_globals, zend_test_globals) + STD_PHP_INI_ENTRY("zend_test.bailout_in_rinit", "0", PHP_INI_SYSTEM, OnUpdateLong, bailout_in_rinit, zend_zend_test_globals, zend_test_globals) + STD_PHP_INI_ENTRY("zend_test.rinit_bailout_putenv", "", PHP_INI_SYSTEM, OnUpdateStr, rinit_bailout_putenv, zend_zend_test_globals, zend_test_globals) #ifdef HAVE_COPY_FILE_RANGE STD_PHP_INI_ENTRY("zend_test.limit_copy_file_range", "-1", PHP_INI_ALL, OnUpdateLong, limit_copy_file_range, zend_zend_test_globals, zend_test_globals) #endif @@ -1657,8 +1659,34 @@ PHP_MSHUTDOWN_FUNCTION(zend_test) return SUCCESS; } +/* Called by the output flush of the request shutdown, before any RSHUTDOWN */ +static zend_result zend_test_rinit_bailout_putenv_handler(void **handler_context, php_output_context *output_context) +{ + zval func, arg, retval; + + ZVAL_STRINGL(&func, "putenv", sizeof("putenv") - 1); + ZVAL_STR_COPY(&arg, ZT_G(rinit_bailout_putenv)); + call_user_function(NULL, NULL, &func, &retval, 1, &arg); + zval_ptr_dtor(&retval); + zval_ptr_dtor(&arg); + zval_ptr_dtor(&func); + + /* pass the output through */ + return FAILURE; +} + PHP_RINIT_FUNCTION(zend_test) { + /* number of requests whose startup fails */ + if (ZT_G(bailout_in_rinit) > 0) { + ZT_G(bailout_in_rinit)--; + if (ZT_G(rinit_bailout_putenv) && ZSTR_LEN(ZT_G(rinit_bailout_putenv))) { + php_output_handler_start(php_output_handler_create_internal( + ZEND_STRL("zend_test_rinit_bailout_putenv"), zend_test_rinit_bailout_putenv_handler, 0, PHP_OUTPUT_HANDLER_STDFLAGS)); + } + zend_bailout(); + } + ALLOC_HASHTABLE(ZT_G(global_weakmap)); zend_hash_init(ZT_G(global_weakmap), 8, NULL, ZVAL_PTR_DTOR, 0); ZT_G(observer_nesting_depth) = 0; diff --git a/ext/zend_test/tests/rinit_bailout_rshutdown_after_output.phpt b/ext/zend_test/tests/rinit_bailout_rshutdown_after_output.phpt new file mode 100644 index 000000000000..2c4f0e7a5ccd --- /dev/null +++ b/ext/zend_test/tests/rinit_bailout_rshutdown_after_output.phpt @@ -0,0 +1,47 @@ +--TEST-- +After a RINIT bailout, RSHUTDOWN runs after the output handlers are flushed +--EXTENSIONS-- +zend_test +--SKIPIF-- + +--FILE-- + +--EXPECT-- +[] +[bool(false) +] diff --git a/main/main.c b/main/main.c index 4d1612a09c78..c7c7c336c361 100644 --- a/main/main.c +++ b/main/main.c @@ -1956,9 +1956,7 @@ void php_request_shutdown(void *dummy) } zend_end_try(); /* 5. Call all extensions RSHUTDOWN functions */ - if (PG(modules_activated)) { - zend_deactivate_modules(); - } + zend_deactivate_modules(); /* 6. Shutdown output layer (send the set HTTP headers, cleanup output handlers, etc.) */ zend_try {