Skip to content

Commit 91f888f

Browse files
committed
fix: system ID for Apache graceful restart
Apache forks workers from a parent process. During a graceful restart, PHP shuts down and starts up again in that same parent process. However, `finalized` was not reset to 0 during subsequent startups, so all calls to `zend_add_system_entropy()` would fail. This could cause the process state to diverge from the system ID used to validate OPcache's file cache. For example, `zend_get_resource_handle()` would still allocate a handle, but its entropy would not contribute to the system ID. Enabling, disabling, or reordering extensions or features across graceful restarts could therefore change resource-slot assignments without a corresponding change to the system ID. Reset `finalized` when initializing the system ID so each startup can register fresh entropy.
1 parent 7050d57 commit 91f888f

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎Zend/zend_system_id.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ ZEND_API zend_result zend_add_system_entropy(const char *module_name, const char
4343

4444
void zend_startup_system_id(void)
4545
{
46+
finalized = 0;
4647
PHP_MD5Init(&context);
4748
PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
4849
PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1);

‎ext/zend_test/test.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "test_arginfo.h"
3737
#include "zend_call_stack.h"
3838
#include "zend_exceptions.h"
39+
#include "zend_system_id.h"
3940
#include "zend_mm_custom_handlers.h"
4041
#include <signal.h>
4142

@@ -1149,6 +1150,7 @@ static ZEND_METHOD(_ZendTestMagicCallForward, __call)
11491150
}
11501151

11511152
PHP_INI_BEGIN()
1153+
PHP_INI_ENTRY("zend_test.register_system_entropy", "0", PHP_INI_SYSTEM, NULL)
11521154
STD_PHP_INI_BOOLEAN("zend_test.replace_zend_execute_ex", "0", PHP_INI_SYSTEM, OnUpdateBool, replace_zend_execute_ex, zend_zend_test_globals, zend_test_globals)
11531155
STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals)
11541156
STD_PHP_INI_BOOLEAN("zend_test.print_stderr_mshutdown", "0", PHP_INI_SYSTEM, OnUpdateBool, print_stderr_mshutdown, zend_zend_test_globals, zend_test_globals)
@@ -1347,6 +1349,10 @@ PHP_MINIT_FUNCTION(zend_test)
13471349
// Loading via dl() not supported with the observer API
13481350
if (type != MODULE_TEMPORARY) {
13491351
REGISTER_INI_ENTRIES();
1352+
if (INI_BOOL("zend_test.register_system_entropy") &&
1353+
zend_add_system_entropy("zend_test", "test", ZEND_STRL("test entropy")) == FAILURE) {
1354+
php_error_docref(NULL, E_WARNING, "Failed to register system entropy");
1355+
}
13501356
} else {
13511357
(void)ini_entries;
13521358
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
System ID reinitialization accepts extension entropy after engine restart
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('zend_test')) die('skip zend_test extension required');
6+
?>
7+
--INI--
8+
zend_test.register_system_entropy=1
9+
--PHPDBG--
10+
r
11+
clean
12+
r
13+
clean
14+
r
15+
q
16+
--FILE--
17+
<?php
18+
echo "Done\n";
19+
?>
20+
--EXPECTF--
21+
[Successful compilation of %s]
22+
prompt> Done
23+
[Script ended normally]
24+
prompt> Cleaning Execution Environment
25+
Classes %d
26+
Functions %d
27+
Constants %d
28+
Includes 0
29+
[Script ended normally]
30+
prompt> Done
31+
[Script ended normally]
32+
prompt> Cleaning Execution Environment
33+
Classes %d
34+
Functions %d
35+
Constants %d
36+
Includes 0
37+
[Script ended normally]
38+
prompt> Done
39+
[Script ended normally]
40+
prompt>

0 commit comments

Comments
 (0)