Skip to content

Commit b911575

Browse files
committed
Fix phpGH-18417: Windows SHM reattachment fails when increasing memory_consumption or jit_buffer_size
When a first PHP process launches, Opcache creates a shared file mapping to use as a shm region. The size of this mapping is set by opcache.memory_consumption. When a new PHP process launches while the old one is still running, Opcache tries to reattach to the shm. When reattaching it tries to map the requested size (i.e. set by opcache.memory_consumption). However, if the new requested size is larger than the size used in the original file mapping, then the call to VirtualProtect() will fail and the new PHP process will fail to launch. It's not possible to resize the virtual region on Windows, unless relying on undocumented APIs like `NtExtendSection` but then we would sitll need to communicate that to the first process. This issue is the root cause of Psalm end-to-end tests failing in phpGH-18417: Psalm estimates the required memory sizes and relaunches itself with more memory requested, if its estimate is below the currently allocated shared memory. This causes a crash on startup and the tests fail. To solve this, we need to make the mappings unique per requested size. There are two ideas: 1. Include in zend_system_id. However, this also affects other things and may be too overkill. 2. Include it in the filename, this is an easy local change. I went with this option.
1 parent 2beec54 commit b911575

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

ext/opcache/shared_alloc_win32.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ static void zend_win_error_message(int type, char *msg, int err)
6969
php_win32_error_msg_free(buf);
7070
}
7171

72-
static char *create_name_with_username(char *name)
72+
static char *create_name_with_username(const char *name, size_t unique_id)
7373
{
74-
static char newname[MAXPATHLEN + 1 + 32 + 1 + 20 + 1 + 32 + 1];
74+
static char newname[MAXPATHLEN + 1 + 32 + 1 + 20 + 1 + 32 + sizeof("ffffffffffffffff")-1 + 1];
7575
char *p = newname;
7676
p += strlcpy(newname, name, MAXPATHLEN + 1);
7777
*(p++) = '@';
@@ -82,15 +82,19 @@ static char *create_name_with_username(char *name)
8282
*(p++) = '@';
8383
memcpy(p, zend_system_id, 32);
8484
p += 32;
85-
*(p++) = '\0';
85+
if (unique_id) {
86+
p += snprintf(p, sizeof("ffffffffffffffff"), "%zx", unique_id) + 1;
87+
} else {
88+
*(p++) = '\0';
89+
}
8690
ZEND_ASSERT(p - newname <= sizeof(newname));
8791

8892
return newname;
8993
}
9094

9195
void zend_shared_alloc_create_lock(void)
9296
{
93-
memory_mutex = CreateMutex(NULL, FALSE, create_name_with_username(ACCEL_MUTEX_NAME));
97+
memory_mutex = CreateMutex(NULL, FALSE, create_name_with_username(ACCEL_MUTEX_NAME, 0));
9498
if (!memory_mutex) {
9599
zend_accel_error(ACCEL_LOG_FATAL, "Cannot create mutex (error %u)", GetLastError());
96100
return;
@@ -224,7 +228,7 @@ static int create_segments(size_t requested_size, zend_shared_segment ***shared_
224228
can be called before the child process is killed. In this case, the mapping will fail
225229
and we have to sleep some time (until the child releases the mapping object) and retry.*/
226230
do {
227-
memfile = OpenFileMapping(FILE_MAP_READ|FILE_MAP_WRITE|FILE_MAP_EXECUTE, 0, create_name_with_username(ACCEL_FILEMAP_NAME));
231+
memfile = OpenFileMapping(FILE_MAP_READ|FILE_MAP_WRITE|FILE_MAP_EXECUTE, 0, create_name_with_username(ACCEL_FILEMAP_NAME, requested_size));
228232
if (memfile == NULL) {
229233
err = GetLastError();
230234
break;
@@ -269,7 +273,7 @@ static int create_segments(size_t requested_size, zend_shared_segment ***shared_
269273
(*shared_segments_p)[0] = shared_segment;
270274

271275
memfile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE | SEC_COMMIT, size_high, size_low,
272-
create_name_with_username(ACCEL_FILEMAP_NAME));
276+
create_name_with_username(ACCEL_FILEMAP_NAME, requested_size));
273277
if (memfile == NULL) {
274278
err = GetLastError();
275279
zend_shared_alloc_unlock_win32();

ext/opcache/tests/gh18417.phpt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
GH-18417 (Windows SHM reattachment fails when increasing memory_consumption or jit_buffer_size)
3+
--SKIPIF--
4+
<?php
5+
$extDir = ini_get('extension_dir');
6+
if (!file_exists($extDir . '/php_opcache.dll')) {
7+
die('skip Opcache DLL not found in extension_dir (Windows-only)');
8+
}
9+
?>
10+
--FILE--
11+
<?php
12+
$memory_consumption = (int) ini_get("opcache.memory_consumption");
13+
$new_memory_consumption = $memory_consumption * 2;
14+
$extension_dir = ini_get("extension_dir");
15+
16+
$descriptorspec = [
17+
0 => ["pipe", "r"],
18+
1 => ["pipe", "w"],
19+
2 => ["pipe", "w"],
20+
];
21+
22+
$proc = proc_open([
23+
PHP_BINARY,
24+
"-n",
25+
"-d", "extension_dir=$extension_dir",
26+
"-d", "zend_extension=opcache",
27+
"-d", "opcache.memory_consumption=$new_memory_consumption",
28+
"-d", "opcache.enable=1",
29+
"-d", "opcache.enable_cli=1",
30+
"-r",
31+
"echo 1;"
32+
], $descriptorspec, $pipes);
33+
34+
echo stream_get_contents($pipes[1]);
35+
echo stream_get_contents($pipes[2]);
36+
37+
proc_close($proc);
38+
?>
39+
--EXPECT--
40+
1

0 commit comments

Comments
 (0)