Skip to content

Commit a9695cc

Browse files
Girgiastrowski
andcommitted
Refactor register shutdown function mechanism
Use FCI/FCC structure instead of custom implementation which does the same. This also fixes the "bug" which prevented static methods from being shutdown functions. Closes phpGH-5829 Co-authored-by: Aaron Piotrowski <[email protected]>
1 parent fcd1875 commit a9695cc

File tree

5 files changed

+39
-50
lines changed

5 files changed

+39
-50
lines changed

Zend/tests/bug41026.phpt

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class try_class
1212

1313
static public function on_shutdown ()
1414
{
15-
printf ("CHECKPOINT\n"); /* never reached */
15+
printf ("CHECKPOINT\n");
1616
}
1717
}
1818

@@ -22,5 +22,4 @@ echo "Done\n";
2222
?>
2323
--EXPECT--
2424
Done
25-
26-
Fatal error: Registered shutdown function self::on_shutdown() cannot be called, function does not exist in Unknown on line 0
25+
CHECKPOINT

ext/session/session.c

+16-8
Original file line numberDiff line numberDiff line change
@@ -2047,13 +2047,18 @@ PHP_FUNCTION(session_set_save_handler)
20472047
if (register_shutdown) {
20482048
/* create shutdown function */
20492049
php_shutdown_function_entry shutdown_function_entry;
2050-
ZVAL_STRING(&shutdown_function_entry.function_name, "session_register_shutdown");
2051-
shutdown_function_entry.arg_count = 0;
2052-
shutdown_function_entry.arguments = NULL;
2050+
zval callable;
2051+
zend_result result;
2052+
2053+
ZVAL_STRING(&callable, "session_register_shutdown");
2054+
result = zend_fcall_info_init(&callable, 0, &shutdown_function_entry.fci,
2055+
&shutdown_function_entry.fci_cache, NULL, NULL);
2056+
2057+
ZEND_ASSERT(result == SUCCESS);
20532058

20542059
/* add shutdown function, removing the old one if it exists */
20552060
if (!register_user_shutdown_function("session_shutdown", sizeof("session_shutdown") - 1, &shutdown_function_entry)) {
2056-
zval_ptr_dtor(&shutdown_function_entry.function_name);
2061+
zval_ptr_dtor(&callable);
20572062
php_error_docref(NULL, E_WARNING, "Unable to register session shutdown function");
20582063
RETURN_FALSE;
20592064
}
@@ -2654,6 +2659,8 @@ PHP_FUNCTION(session_status)
26542659
PHP_FUNCTION(session_register_shutdown)
26552660
{
26562661
php_shutdown_function_entry shutdown_function_entry;
2662+
zval callable;
2663+
zend_result result;
26572664

26582665
ZEND_PARSE_PARAMETERS_NONE();
26592666

@@ -2663,13 +2670,14 @@ PHP_FUNCTION(session_register_shutdown)
26632670
* function after calling session_set_save_handler(), which expects
26642671
* the session still to be available.
26652672
*/
2673+
ZVAL_STRING(&callable, "session_write_close");
2674+
result = zend_fcall_info_init(&callable, 0, &shutdown_function_entry.fci,
2675+
&shutdown_function_entry.fci_cache, NULL, NULL);
26662676

2667-
ZVAL_STRING(&shutdown_function_entry.function_name, "session_write_close");
2668-
shutdown_function_entry.arg_count = 0;
2669-
shutdown_function_entry.arguments = NULL;
2677+
ZEND_ASSERT(result == SUCCESS);
26702678

26712679
if (!append_user_shutdown_function(&shutdown_function_entry)) {
2672-
zval_ptr_dtor(&shutdown_function_entry.function_name);
2680+
zval_ptr_dtor(&callable);
26732681

26742682
/* Unable to register shutdown function, presumably because of lack
26752683
* of memory, so flush the session now. It would be done in rshutdown

ext/standard/basic_functions.c

+18-35
Original file line numberDiff line numberDiff line change
@@ -1664,14 +1664,10 @@ PHP_FUNCTION(forward_static_call_array)
16641664

16651665
void user_shutdown_function_dtor(zval *zv) /* {{{ */
16661666
{
1667-
int i;
16681667
php_shutdown_function_entry *shutdown_function_entry = Z_PTR_P(zv);
16691668

1670-
zval_ptr_dtor(&shutdown_function_entry->function_name);
1671-
for (i = 0; i < shutdown_function_entry->arg_count; i++) {
1672-
zval_ptr_dtor(&shutdown_function_entry->arguments[i]);
1673-
}
1674-
efree(shutdown_function_entry->arguments);
1669+
zval_ptr_dtor(&shutdown_function_entry->fci.function_name);
1670+
zend_fcall_info_args_clear(&shutdown_function_entry->fci, true);
16751671
efree(shutdown_function_entry);
16761672
}
16771673
/* }}} */
@@ -1685,24 +1681,16 @@ void user_tick_function_dtor(user_tick_function_entry *tick_function_entry) /* {
16851681

16861682
static int user_shutdown_function_call(zval *zv) /* {{{ */
16871683
{
1688-
php_shutdown_function_entry *shutdown_function_entry = Z_PTR_P(zv);
1684+
php_shutdown_function_entry *shutdown_function_entry = Z_PTR_P(zv);
16891685
zval retval;
1686+
zend_result call_status;
16901687

1691-
if (!zend_is_callable(&shutdown_function_entry->function_name, 0, NULL)) {
1692-
zend_string *function_name = zend_get_callable_name(&shutdown_function_entry->function_name);
1693-
zend_throw_error(NULL, "Registered shutdown function %s() cannot be called, function does not exist", ZSTR_VAL(function_name));
1694-
zend_string_release(function_name);
1695-
return 0;
1696-
}
1688+
/* set retval zval for FCI struct */
1689+
shutdown_function_entry->fci.retval = &retval;
1690+
call_status = zend_call_function(&shutdown_function_entry->fci, &shutdown_function_entry->fci_cache);
1691+
ZEND_ASSERT(call_status == SUCCESS);
1692+
zval_ptr_dtor(&retval);
16971693

1698-
if (call_user_function(NULL, NULL,
1699-
&shutdown_function_entry->function_name,
1700-
&retval,
1701-
shutdown_function_entry->arg_count,
1702-
shutdown_function_entry->arguments) == SUCCESS)
1703-
{
1704-
zval_ptr_dtor(&retval);
1705-
}
17061694
return 0;
17071695
}
17081696
/* }}} */
@@ -1761,8 +1749,7 @@ PHPAPI void php_call_shutdown_functions(void) /* {{{ */
17611749
if (BG(user_shutdown_function_names)) {
17621750
zend_try {
17631751
zend_hash_apply(BG(user_shutdown_function_names), user_shutdown_function_call);
1764-
}
1765-
zend_end_try();
1752+
} zend_end_try();
17661753
}
17671754
}
17681755
/* }}} */
@@ -1786,23 +1773,19 @@ PHPAPI void php_free_shutdown_functions(void) /* {{{ */
17861773
PHP_FUNCTION(register_shutdown_function)
17871774
{
17881775
php_shutdown_function_entry entry;
1789-
zend_fcall_info fci;
1790-
zend_fcall_info_cache fcc;
1791-
zval *args;
1792-
int arg_count = 0;
1776+
zval *params = NULL;
1777+
uint32_t param_count = 0;
1778+
bool status;
17931779

1794-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "f*", &fci, &fcc, &args, &arg_count) == FAILURE) {
1780+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "f*", &entry.fci, &entry.fci_cache, &params, &param_count) == FAILURE) {
17951781
RETURN_THROWS();
17961782
}
17971783

1798-
ZVAL_COPY(&entry.function_name, &fci.function_name);
1799-
entry.arguments = (zval *) safe_emalloc(sizeof(zval), arg_count, 0);
1800-
entry.arg_count = arg_count;
1801-
for (int i = 0; i < arg_count; i++) {
1802-
ZVAL_COPY(&entry.arguments[i], &args[i]);
1803-
}
1784+
Z_TRY_ADDREF(entry.fci.function_name);
1785+
zend_fcall_info_argp(&entry.fci, param_count, params);
18041786

1805-
append_user_shutdown_function(&entry);
1787+
status = append_user_shutdown_function(&entry);
1788+
ZEND_ASSERT(status);
18061789
}
18071790
/* }}} */
18081791

ext/standard/basic_functions.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ PHPAPI double php_get_nan(void);
143143
PHPAPI double php_get_inf(void);
144144

145145
typedef struct _php_shutdown_function_entry {
146-
zval function_name;
147-
zval *arguments;
148-
int arg_count;
146+
zend_fcall_info fci;
147+
zend_fcall_info_cache fci_cache;
149148
} php_shutdown_function_entry;
150149

151150
PHPAPI extern bool register_user_shutdown_function(const char *function_name, size_t function_len, php_shutdown_function_entry *shutdown_function_entry);

ext/standard/tests/general_functions/010.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class test {
1212
}
1313

1414
try {
15-
register_shutdown_function(array("test","__call"));
15+
var_dump(register_shutdown_function(array("test","__call")));
1616
} catch (TypeError $exception) {
1717
echo $exception->getMessage() . "\n";
1818
}

0 commit comments

Comments
 (0)