[php-src] Issue #25213: OPcache on Windows: the JIT buffer is not page-aligned, so writing to the last page of the cache crashes
| From: | dg | Date: | Thu, 05 Nov 2026 15:07:08 +0000 |
| Subject: | [php-src] Issue #25213: OPcache on Windows: the JIT buffer is not page-aligned, so writing to the last page of the cache crashes | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-254021@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/25213
Author: dg
### Description
On Windows with JIT enabled, PHP dies with an access violation (
0xC0000005) when the
shared memory of OPcache is nearly full, instead of OPcache reporting cache_full.
The following code:
```php
<?php
// Fills the shared memory of OPcache with small classes until it reports that it is full.
$dir = sys_get_temp_dir() . '/opcache-fill-' . getmypid();
mkdir($dir);
for ($i = 0; ; $i++) {
$file = "$dir/c$i.php";
file_put_contents($file, "<?php\nclass C$i extends ArrayObject { public function m(int
\$a): int { return \$a + $i; } }\n");
require $file;
unlink($file);
$status = opcache_get_status(false);
if ($status['cache_full']) {
rmdir($dir);
echo "cache_full after $i files, free:
{$status['memory_usage']['free_memory']}\n";
exit;
}
}
```
run with a small cache so that it fills up quickly (on PHP 8.4 and older also with -d
zend_extension=<path>/php_opcache.dll):
```
php -n -d opcache.enable_cli=1 -d opcache.memory_consumption=16 -d opcache.interned_strings_buffer=1
-d opcache.file_update_protection=0 -d opcache.jit=tracing -d opcache.jit_buffer_size=64M repro.php
```
Resulted in this output: none, the process ends with exit code 0xC0000005.
But I expected this output instead (this is what the same command prints with -d
opcache.jit_buffer_size=0):
```
cache_full after 6833 files, free: 736
```
### The cause
create_segments() in ext/opcache/shared_alloc_win32.c makes the segment
smaller by the header, although p stays at the start of the mapping:
```c
shared_segment->pos = ACCEL_BASE_POINTER_SIZE;
shared_segment->size = requested_size - ACCEL_BASE_POINTER_SIZE;
```
zend_shared_alloc_startup() then places the JIT buffer at p + size -
reserved_size, which is 16 bytes below a page boundary. zend_jit_startup() calls
VirtualProtect(dasm_buf, dasm_size, PAGE_EXECUTE_READ, &old) with that address, and
VirtualProtect rounds it down to the page. The last page of the cache, 4080 bytes it
still counts as free, is therefore not writable.
The debugger shows that on 8.4.27 with the settings above, where the cache is the first 16 MB of the
mapping at 0x100000000000:
```
Access violation - code c0000005 (first chance)
Attempt to write to address 0000100000fff1f0
ExceptionAddress: php_opcache+0x87f8f
!address 0x100000ffeff0 0000100000000000 - 0000100000fff000 PAGE_READWRITE
!address 0x100000fff000 0000100000fff000 - 0000100005000000 PAGE_EXECUTE_READ
```
shared_alloc_mmap.c has pos = 0 and size = requested_size, so
the buffer is aligned there, and the script ends normally on Linux.
Setting shared_segment->size = requested_size looks like the fix, since
pos already accounts for the header, but I have not built or tested it.
### How it shows up in practice
I ran into this with PHPStan in several worker processes at the default sizes. On Windows they share
one OPcache instance, and they fill it quickly because PHPStan calls
opcache_invalidate() with force on the files it has included. The crashes
look random, on whichever file happens to be compiled at the time.
The sporadic crashes on Windows in #12010, #11549 and #7915 may have the same cause, but I have not
verified that.
### PHP Version
Reproduced on x64 ZTS builds of 8.3.36, 8.4.27, 8.5.12 and 8.6.0RC4. The same code is in
master. NTS builds not tested.
### Operating System
Windows 11