Skip to content
Open
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
42 changes: 41 additions & 1 deletion Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
63 changes: 63 additions & 0 deletions ext/session/tests/rinit_bailout_releases_lock.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
--TEST--
A request whose startup bails out after session.auto_start releases the session lock
--EXTENSIONS--
session
zend_test
--SKIPIF--
<?php
include 'skipif.inc';
if (!file_exists(__DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc")) {
echo "skip sapi/cli/tests/php_cli_server.inc required but not found";
}
?>
--FILE--
<?php
include __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc";

$savePath = __DIR__ . '/rinit_bailout_releases_lock_sessions';
@mkdir($savePath);

$args = [
'-d', 'extension_dir=' . ini_get('extension_dir'),
'-d', 'session.auto_start=1',
'-d', 'session.save_handler=files',
'-d', 'session.save_path=' . $savePath,
'-d', 'zend_test.bailout_in_rinit=1',
];
$builtin = shell_exec(escapeshellarg(getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY) . ' -n -m');
foreach (['session', 'zend_test'] as $ext) {
if (!preg_match("/^$ext$/m", $builtin)) {
array_push($args, '-d', "extension=$ext");
}
}

php_cli_server_start('echo session_id();', null, $args);

function request(): string {
$fp = stream_socket_client('tcp://' . PHP_CLI_SERVER_ADDRESS, $errno, $errstr, 5);
stream_set_timeout($fp, 5);
fwrite($fp, "GET / HTTP/1.0\r\nHost: " . PHP_CLI_SERVER_HOSTNAME . "\r\nCookie: PHPSESSID=rinitbailout\r\n\r\n");
$response = stream_get_contents($fp);
fclose($fp);

[, $body] = explode("\r\n\r\n", $response, 2) + ['', ''];

return "[$body]";
}

// the first request fails in zend_test's RINIT, after the session module locked the session
echo request(), "\n";
// blocks if the session is still locked
echo request(), "\n";
?>
--CLEAN--
<?php
$savePath = __DIR__ . '/rinit_bailout_releases_lock_sessions';
foreach (glob("$savePath/*") as $file) {
unlink($file);
}
@rmdir($savePath);
?>
--EXPECT--
[]
[rinitbailout]
47 changes: 47 additions & 0 deletions ext/session/tests/rinit_bailout_releases_lock_trans_sid.phpt
Original file line number Diff line number Diff line change
@@ -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--
<?php
include 'skipif.inc';
if (!file_exists(__DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc")) {
echo "skip sapi/cli/tests/php_cli_server.inc required but not found";
}
?>
--INI--
default_socket_timeout=5
--FILE--
<?php
include __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc";

$args = [
'-d', 'extension_dir=' . ini_get('extension_dir'),
'-d', 'session.auto_start=1',
'-d', 'session.use_trans_sid=1',
'-d', 'session.use_cookies=0',
'-d', 'session.use_only_cookies=0',
'-d', 'session.save_path=' . __DIR__,
'-d', 'zend_test.bailout_in_rinit=1',
];
$builtin = shell_exec(escapeshellarg(getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY) . ' -n -m');
foreach (['session', 'zend_test'] as $ext) {
if (!preg_match("/^$ext$/m", $builtin)) {
array_push($args, '-d', "extension=$ext");
}
}

php_cli_server_start('echo session_id();', null, $args);

$url = 'http://' . PHP_CLI_SERVER_ADDRESS . '/?PHPSESSID=rinitbailouttranssid';
var_dump(@file_get_contents($url));
var_dump(@file_get_contents($url));
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/sess_rinitbailouttranssid');
?>
--EXPECT--
string(0) ""
string(20) "rinitbailouttranssid"
2 changes: 2 additions & 0 deletions ext/zend_test/php_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions ext/zend_test/tests/rinit_bailout_rshutdown_after_output.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
After a RINIT bailout, RSHUTDOWN runs after the output handlers are flushed
--EXTENSIONS--
zend_test
--SKIPIF--
<?php
if (!file_exists(__DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc")) {
echo "skip sapi/cli/tests/php_cli_server.inc required but not found";
}
?>
--FILE--
<?php
include __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc";

$args = [
'-d', 'extension_dir=' . ini_get('extension_dir'),
'-d', 'zend_test.bailout_in_rinit=1',
'-d', 'zend_test.rinit_bailout_putenv="ZEND_TEST_RINIT_BAILOUT=1"',
];
$builtin = shell_exec(escapeshellarg(getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY) . ' -n -m');
if (!preg_match("/^zend_test$/m", $builtin)) {
array_push($args, '-d', 'extension=zend_test');
}

php_cli_server_start('var_dump(getenv("ZEND_TEST_RINIT_BAILOUT"));', null, $args);

function request(): string {
$fp = stream_socket_client('tcp://' . PHP_CLI_SERVER_ADDRESS, $errno, $errstr, 5);
stream_set_timeout($fp, 5);
fwrite($fp, "GET / HTTP/1.0\r\nHost: " . PHP_CLI_SERVER_HOSTNAME . "\r\n\r\n");
$response = stream_get_contents($fp);
fclose($fp);

[, $body] = explode("\r\n\r\n", $response, 2) + ['', ''];

return "[$body]";
}

// zend_test's RINIT bails out, its output handler calls putenv() during the request shutdown
echo request(), "\n";
// ext/standard's RSHUTDOWN restored the environment
echo request(), "\n";
?>
--EXPECT--
[]
[bool(false)
]
4 changes: 1 addition & 3 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading