Skip to content

Commit 5703a5c

Browse files
committed
Refactor register shutdown function mechanism
Note: Some tests seem to hang
1 parent 207666e commit 5703a5c

File tree

5 files changed

+25
-57
lines changed

5 files changed

+25
-57
lines changed

Zend/tests/bug41026.phpt

Lines changed: 2 additions & 3 deletions
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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,16 +2047,13 @@ 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+
2052+
ZVAL_STRING(&callable, "session_write_close");
2053+
zend_fcall_info_init(&callable, 0, &shutdown_function_entry.fci, &shutdown_function_entry.fci_cache, NULL, NULL);
20532054

20542055
/* add shutdown function, removing the old one if it exists */
2055-
if (!register_user_shutdown_function("session_shutdown", sizeof("session_shutdown") - 1, &shutdown_function_entry)) {
2056-
zval_ptr_dtor(&shutdown_function_entry.function_name);
2057-
php_error_docref(NULL, E_WARNING, "Unable to register session shutdown function");
2058-
RETURN_FALSE;
2059-
}
2056+
register_user_shutdown_function("session_shutdown", sizeof("session_shutdown") - 1, &shutdown_function_entry);
20602057
} else {
20612058
/* remove shutdown function */
20622059
remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") - 1);
@@ -2654,6 +2651,7 @@ PHP_FUNCTION(session_status)
26542651
PHP_FUNCTION(session_register_shutdown)
26552652
{
26562653
php_shutdown_function_entry shutdown_function_entry;
2654+
zval callable;
26572655

26582656
ZEND_PARSE_PARAMETERS_NONE();
26592657

@@ -2664,12 +2662,13 @@ PHP_FUNCTION(session_register_shutdown)
26642662
* the session still to be available.
26652663
*/
26662664

2667-
ZVAL_STRING(&shutdown_function_entry.function_name, "session_write_close");
2668-
shutdown_function_entry.arg_count = 0;
2669-
shutdown_function_entry.arguments = NULL;
2665+
// TODO ALLOC MEMORY FOR STRUCT FUNCTION ENTRY
2666+
ZVAL_STRING(&callable, "session_write_close");
2667+
zend_fcall_info_init(&callable, 0, &shutdown_function_entry.fci, &shutdown_function_entry.fci_cache, NULL, NULL);
26702668

26712669
if (!append_user_shutdown_function(&shutdown_function_entry)) {
2672-
zval_ptr_dtor(&shutdown_function_entry.function_name);
2670+
zval_ptr_dtor(&shutdown_function_entry.fci.function_name);
2671+
//efree(shutdown_function_entry);
26732672

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

ext/standard/basic_functions.c

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,14 +1664,9 @@ 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);
16751670
efree(shutdown_function_entry);
16761671
}
16771672
/* }}} */
@@ -1690,23 +1685,7 @@ void user_tick_function_dtor(user_tick_function_entry *tick_function_entry) /* {
16901685
static int user_shutdown_function_call(zval *zv) /* {{{ */
16911686
{
16921687
php_shutdown_function_entry *shutdown_function_entry = Z_PTR_P(zv);
1693-
zval retval;
1694-
1695-
if (!zend_is_callable(&shutdown_function_entry->function_name, 0, NULL)) {
1696-
zend_string *function_name = zend_get_callable_name(&shutdown_function_entry->function_name);
1697-
zend_throw_error(NULL, "Registered shutdown function %s() cannot be called, function does not exist", ZSTR_VAL(function_name));
1698-
zend_string_release(function_name);
1699-
return 0;
1700-
}
1701-
1702-
if (call_user_function(NULL, NULL,
1703-
&shutdown_function_entry->function_name,
1704-
&retval,
1705-
shutdown_function_entry->arg_count,
1706-
shutdown_function_entry->arguments) == SUCCESS)
1707-
{
1708-
zval_ptr_dtor(&retval);
1709-
}
1688+
zend_fcall_info_call(&shutdown_function_entry->fci, &shutdown_function_entry->fci_cache, NULL, NULL);
17101689
return 0;
17111690
}
17121691
/* }}} */
@@ -1798,21 +1777,13 @@ PHPAPI void php_free_shutdown_functions(void) /* {{{ */
17981777
PHP_FUNCTION(register_shutdown_function)
17991778
{
18001779
php_shutdown_function_entry entry;
1801-
zend_fcall_info fci;
1802-
zend_fcall_info_cache fcc;
1803-
zval *args;
1804-
int arg_count = 0;
18051780

1806-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "f*", &fci, &fcc, &args, &arg_count) == FAILURE) {
1807-
RETURN_THROWS();
1808-
}
1781+
ZEND_PARSE_PARAMETERS_START(1, -1)
1782+
Z_PARAM_FUNC(entry.fci, entry.fci_cache)
1783+
Z_PARAM_VARIADIC('*', entry.fci.params, entry.fci.param_count)
1784+
ZEND_PARSE_PARAMETERS_END();
18091785

1810-
ZVAL_COPY(&entry.function_name, &fci.function_name);
1811-
entry.arguments = (zval *) safe_emalloc(sizeof(zval), arg_count, 0);
1812-
entry.arg_count = arg_count;
1813-
for (int i = 0; i < arg_count; i++) {
1814-
ZVAL_COPY(&entry.arguments[i], &args[i]);
1815-
}
1786+
Z_TRY_ADDREF(entry.fci.function_name);
18161787

18171788
append_user_shutdown_function(&entry);
18181789
}

ext/standard/basic_functions.h

Lines changed: 2 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class test {
1212
}
1313

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

2020
echo "Done\n";

0 commit comments

Comments
 (0)