Crashes in zend_accel_inheritance_cache_find since upgrading to 8.1.3 due to corrupt on-disk file cache
Description
I dont't have a minimal reproducer for this but an application which is based on sabre/dav running on Windows Server 2016 using IIS
Since upgrading from 8.1.2 to 8.1.3 I get frequent 500 errors. In the event log I find
Log Name: Application
Source: Application Error
Date: 23.02.2022 11:26:34
Event ID: 1000
Task Category: (100)
Level: Error
Keywords: Classic
User: N/A
Computer: PSDEVMB.test.example.com
Description:
Faulting application name: php-cgi.exe, version: 8.1.3.0, time stamp: 0x62160b66
Faulting module name: php_opcache.dll, version: 8.1.3.0, time stamp: 0x62160b62
Exception code: 0xc0000005
Fault offset: 0x00000000000043a3
Faulting process id: 0xfd0
Faulting application start time: 0x01d8289fd314a2a7
Faulting application path: C:\Program Files\PHP\v8.1\php-cgi.exe
Faulting module path: C:\Program Files\PHP\v8.1\ext\php_opcache.dll
Report Id: f5dc9377-9cfc-46be-ad05-5ad40b0fafe7
Faulting package full name:
Faulting package-relative application ID:
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Application Error" />
<EventID Qualifiers="0">1000</EventID>
<Level>2</Level>
<Task>100</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2022-02-23T10:26:34.226734200Z" />
<EventRecordID>44517</EventRecordID>
<Channel>Application</Channel>
<Computer>PSDEVMB.test.example.com</Computer>
<Security />
</System>
<EventData>
<Data>php-cgi.exe</Data>
<Data>8.1.3.0</Data>
<Data>62160b66</Data>
<Data>php_opcache.dll</Data>
<Data>8.1.3.0</Data>
<Data>62160b62</Data>
<Data>c0000005</Data>
<Data>00000000000043a3</Data>
<Data>fd0</Data>
<Data>01d8289fd314a2a7</Data>
<Data>C:\Program Files\PHP\v8.1\php-cgi.exe</Data>
<Data>C:\Program Files\PHP\v8.1\ext\php_opcache.dll</Data>
<Data>f5dc9377-9cfc-46be-ad05-5ad40b0fafe7</Data>
<Data>
</Data>
<Data>
</Data>
</EventData>
</Event>
The relevant opcache configuration from php.ini:
[opcache]
zend_extension=php_opcache.dll
opcache.enable_cli=1
opcache.enable=1
opcache.enable_file_override=1
opcache.file_cache="C:\Windows\temp\"
opcache.file_cache_fallback=1
I ran a git bisect since 8.1.2 was working fine. The first faulty commit is https://github.com/php/php-src/commit/78fd573d0c4aef74ec264fb6b8726a5a72e591f9
Attaching a debugger to a running php-cgi.exe process, I see
Exception thrown at 0x00007FF992294F38 in php-cgi.exe: Microsoft C++ exception: std::out_of_range at memory location 0x00000013415FB250.
Exception thrown at 0x00007FF992294F38 in php-cgi.exe: Microsoft C++ exception: std::out_of_range at memory location 0x00000013415FB250.
Exception thrown at 0x00007FF992294F38 in php-cgi.exe: Microsoft C++ exception: std::out_of_range at memory location 0x00000013415FB250.
Exception thrown at 0x00007FF992294F38 in php-cgi.exe: Microsoft C++ exception: std::out_of_range at memory location 0x00000013415FB250.
Exception thrown at 0x00007FF956D743A3 (php_opcache.dll) in php-cgi.exe: 0xC0000005: Access violation reading location 0x0000015600000012.
PHP Version
PHP 8.1.3
Operating System
Windows Server 2016 / IIS
For version 8.1.6 it happens a lot more often and it doesn't go away if I clear opcache
Program terminated with signal 11, Segmentation fault.
b#0 0x00007fbf83127387 in __GI_raise (sig=11) at ../nptl/sysdeps/unix/sysv/linux/raise.c:55
55 return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
(gdb) bt
#0 0x00007fbf83127387 in __GI_raise (sig=11) at ../nptl/sysdeps/unix/sysv/linux/raise.c:55
#1 0x00007fbf7071859f in skgesigOSCrash () from /opt/oracle/instantclient_21_4/libclntsh.so.21.1
#2 0x00007fbf70e0ce8d in kpeDbgSignalHandler () from /opt/oracle/instantclient_21_4/libclntsh.so.21.1
#3 0x00007fbf70718882 in skgesig_sigactionHandler () from /opt/oracle/instantclient_21_4/libclntsh.so.21.1
#4 <signal handler called>
#5 zend_accel_inheritance_cache_find (needs_autoload_ptr=<optimized out>, traits_and_interfaces=<optimized out>, parent=<optimized out>, ce=<optimized out>, entry=0x7f1fab550948)
at /usr/src/debug/php-8.1.6/ext/opcache/ZendAccelerator.c:2254
#6 zend_accel_inheritance_cache_get () at /usr/src/debug/php-8.1.6/ext/opcache/ZendAccelerator.c:2295
#7 0x000055a60a2b54bb in zend_do_link_class () at /usr/src/debug/php-8.1.6/Zend/zend_inheritance.c:2781
#8 0x000055a60a1fc96e in zend_bind_class_in_slot () at /usr/src/debug/php-8.1.6/Zend/zend_compile.c:1131
#9 0x000055a60a1fca38 in do_bind_class () at /usr/src/debug/php-8.1.6/Zend/zend_compile.c:1164
#10 0x000055a60a255ba5 in ZEND_DECLARE_CLASS_SPEC_CONST_HANDLER () at /usr/src/debug/php-8.1.6/Zend/zend_vm_execute.h:5319
#11 0x000055a60a2876fd in execute_ex () at /usr/src/debug/php-8.1.6/Zend/zend_vm_execute.h:56195
#12 0x000055a60a214624 in zend_call_function () at /usr/src/debug/php-8.1.6/Zend/zend_execute_API.c:908
#13 0x000055a60a214a15 in zend_call_known_function () at /usr/src/debug/php-8.1.6/Zend/zend_execute_API.c:997
#14 0x000055a60a11a500 in spl_perform_autoload (class_name=0x7fbf2cb5e220, lc_name=0x7fbf2cb5e260) at /usr/src/debug/php-8.1.6/ext/spl/php_spl.c:433
#15 0x000055a60a2138fc in zend_lookup_class_ex (name=name@entry=0x7fbf2cb5e220, key=0x7fbf2cb5e260, flags=flags@entry=512) at /usr/src/debug/php-8.1.6/Zend/zend_execute_API.c:1141
#16 0x000055a60a214d62 in zend_fetch_class_by_name () at /usr/src/debug/php-8.1.6/Zend/zend_execute_API.c:1601
#17 0x000055a60a25edbf in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER () at /usr/src/debug/php-8.1.6/Zend/zend_vm_execute.h:10147
#18 0x000055a60a2878a4 in execute_ex () at /usr/src/debug/php-8.1.6/Zend/zend_vm_execute.h:56659
#19 0x000055a60a2905dd in zend_execute (op_array=0x7fbf80c02000, return_value=0x0) at /usr/src/debug/php-8.1.6/Zend/zend_vm_execute.h:60123
#20 0x000055a60a222e20 in zend_execute_scripts () at /usr/src/debug/php-8.1.6/Zend/zend.c:1792
#21 0x000055a60a1be8b1 in php_execute_script () at /usr/src/debug/php-8.1.6/main/main.c:2538
#22 0x000055a60a00b80d in main (argc=<optimized out>, argv=<optimized out>) at /usr/src/debug/php-8.1.6/sapi/fpm/fpm/fpm_main.c:1914
We also experience crashes due to segmentation fault in zend_accel_inheritance_cache_find in php 8.1.12 (also in 8.1.10, 8.1.8, 8.1.6).
Previous attempts to debug this issue showed that entry paramenter to zend_accel_inheritance_cache_find is valid upon the entry to the function, but during iteration over chain links, a corrupted entry encountered at some point.
Execution environment
We use php-fpm with opcache and opcache filecache. This bug happens erratically (and thus is very difficult to reproduce) when we deploy a new code release using the following scheme: we pre-warm opcache by referencing files in a new code release (which creates opcache filecache entries), and then we switch current revision symlink to a new release and do php-fpm reload (that is opcache is populated from filecache). This bug is not specific to a single host, but to a lot of machines (20+ hosts)
2256
2257 while (entry) {
2258 bool found = 1;
2259 bool needs_autoload = 0;
2260
---> 2261 if (entry->parent != parent) { <----
2262 found = 0;
2263 } else {
2264 for (i = 0; i < ce->num_traits + ce->num_interfaces; i++) {
2265 if (entry->traits_and_interfaces[i] != traits_and_interfaces[i]) {
(gdb) bt
#0 zend_accel_inheritance_cache_find (needs_autoload_ptr=<optimized out>, traits_and_interfaces=<optimized out>, parent=<optimized out>,
ce=<optimized out>, entry=0x695f746e65726170) at /usr/src/debug/php-8.1.12/ext/opcache/ZendAccelerator.c:2261
#1 zend_accel_inheritance_cache_get () at /usr/src/debug/php-8.1.12/ext/opcache/ZendAccelerator.c:2302
#2 0x000000000085cb5f in zend_try_early_bind () at /usr/src/debug/php-8.1.12/Zend/zend_inheritance.c:3021
#3 0x0000000000799f9d in zend_do_delayed_early_binding (op_array=op_array@entry=0x7f83a8823c00,
first_early_binding_opline=<optimized out>) at /usr/src/debug/php-8.1.12/Zend/zend_compile.c:1381
#4 0x00007f83ba88521b in zend_accel_load_script () at /usr/src/debug/php-8.1.12/ext/opcache/zend_accelerator_util_funcs.c:255
#5 0x000000000077bb78 in compile_filename (type=type@entry=2, filename=filename@entry=0x4279ef78)
at /usr/src/debug/php-8.1.12/Zend/zend_language_scanner.c:707
#6 0x00000000007f4edb in zend_include_or_eval (inc_filename_zv=<optimized out>, type=2)
at /usr/src/debug/php-8.1.12/Zend/zend_execute.c:4623
#7 0x000000000080364b in ZEND_INCLUDE_OR_EVAL_SPEC_CV_HANDLER () at /usr/src/debug/php-8.1.12/Zend/zend_vm_execute.h:38731
We also switch to new releases by updating a symlink, maybe this confuses opcache.
After updating symlink we call opcache_clear() by making an curl api call (to make sure that opcache_clear() is called in the correct context: nginx + php-fpm).
From what I remember, the crash is not happening immediately after deploying new version but much later (even days later).
Currently at version 8.1.12, it happens less often than 8.1.6 (don't know exactly when it started to happen less often).
I ran into what seems to be this while upgrading between Debian-compatible builds of 8.1.15 provided by the Sury repository. Clearing out the file cache fixed the segmentation fault, which seems to have arisen on the 'entry->parent` line mentioned.
Here are the steps to reproduce that work for my php-fpm:
- put some traffic to the website, for example, with siege:
siege -b -c 50 http://endpoint/ - reload php-fpm:
kill -USR2 <PID of php-fpm master process> - here are your segfaults:
[20-Apr-2023 18:56:43] NOTICE: Reloading in progress ...
[20-Apr-2023 18:56:43] NOTICE: reloading: execvp("php-fpm", {"php-fpm", "--nodaemonize"})
[20-Apr-2023 18:56:43] NOTICE: using inherited socket fd=8, ":::9000"
[20-Apr-2023 18:56:43] NOTICE: using inherited socket fd=8, ":::9000"
[20-Apr-2023 18:56:43] NOTICE: fpm is running, pid 1
[20-Apr-2023 18:56:43] NOTICE: ready to handle connections
[20-Apr-2023 18:56:44] WARNING: [pool worker] child 141 exited on signal 11 (SIGSEGV - core dumped) after 1.184849 seconds from start
[20-Apr-2023 18:56:44] NOTICE: [pool worker] child 144 started
[20-Apr-2023 18:56:44] WARNING: [pool worker] child 142 exited on signal 11 (SIGSEGV - core dumped) after 1.232448 seconds from start
[20-Apr-2023 18:56:44] NOTICE: [pool worker] child 145 started
My PHP version:
# php -v
PHP 8.1.16 (cli) (built: Apr 20 2023 14:55:08) (NTS DEBUG)
Copyright (c) The PHP Group
Zend Engine v4.1.16, Copyright (c) Zend Technologies
with Zend OPcache v8.1.16, Copyright (c), by Zend Technologies
Backtrace:
(gdb) bt
#0 0x00007fa727e8124b in zend_accel_inheritance_cache_find () from /usr/local/lib/php/extensions/debug-non-zts-20210902/opcache.so
#1 0x00007fa727e813c9 in zend_accel_inheritance_cache_get () from /usr/local/lib/php/extensions/debug-non-zts-20210902/opcache.so
#2 0x0000562f6fe561cd in zend_do_link_class (ce=0x46e1f760, lc_parent_name=0x43959b40, key=0x43a77e70) at /usr/src/php/Zend/zend_inheritance.c:2784
#3 0x0000562f6fd1c6c6 in zend_bind_class_in_slot (class_table_slot=0x7fa724bd04f0, lcname=0x46e202a0, lc_parent_name=0x43959b40) at /usr/src/php/Zend/zend_compile.c:1131
#4 0x0000562f6fd1c88c in do_bind_class (lcname=0x46e202a0, lc_parent_name=0x43959b40) at /usr/src/php/Zend/zend_compile.c:1164
#5 0x0000562f6fda3ea1 in ZEND_DECLARE_CLASS_SPEC_CONST_HANDLER () at /usr/src/php/Zend/zend_vm_execute.h:5339
#6 0x0000562f6fe10506 in execute_ex (ex=0x7fa728016a20) at /usr/src/php/Zend/zend_vm_execute.h:56220
#7 0x0000562f6fd3e9dc in zend_call_function (fci=0x7fff07a52850, fci_cache=0x7fff07a52830) at /usr/src/php/Zend/zend_execute_API.c:912
#8 0x0000562f6fd3eecb in zend_call_known_function (fn=0x462d2ef0, object=0x7fa728002000, called_scope=0x462cd8a0, retval_ptr=0x0, param_count=1, params=0x7fff07a52900, named_params=0x0) at /usr/src/php/Zend/zend_execute_API.c:1001
#9 0x0000562f6faf342b in spl_perform_autoload (class_name=0x43280548, lc_name=0x43a77e70) at /usr/src/php/ext/spl/php_spl.c:446
#10 0x0000562f6fd3f5a1 in zend_lookup_class_ex (name=0x43280548, key=0x43a77e70, flags=512) at /usr/src/php/Zend/zend_execute_API.c:1145
#11 0x0000562f6fd403ba in zend_fetch_class_by_name (class_name=0x43280548, key=0x43a77e70, fetch_type=512) at /usr/src/php/Zend/zend_execute_API.c:1605
#12 0x0000562f6fdad07f in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER () at /usr/src/php/Zend/zend_vm_execute.h:10167
#13 0x0000562f6fe10c46 in execute_ex (ex=0x7fa728016940) at /usr/src/php/Zend/zend_vm_execute.h:56684
#14 0x0000562f6fd3e9dc in zend_call_function (fci=0x7fff07a52e30, fci_cache=0x7fff07a52e10) at /usr/src/php/Zend/zend_execute_API.c:912
#15 0x0000562f6fb65e31 in zif_call_user_func (execute_data=0x7fa7280168d0, return_value=0x7fa7280168b0) at /usr/src/php/ext/standard/basic_functions.c:1566
#16 0x0000562f6fd97b7c in ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_USED_HANDLER () at /usr/src/php/Zend/zend_vm_execute.h:1558
#17 0x0000562f6fe0fb3a in execute_ex (ex=0x7fa728016020) at /usr/src/php/Zend/zend_vm_execute.h:55799
#18 0x0000562f6fe14418 in zend_execute (op_array=0x7fa728069000, return_value=0x0) at /usr/src/php/Zend/zend_vm_execute.h:60151
#19 0x0000562f6fd5814d in zend_execute_scripts (type=8, retval=0x0, file_count=3) at /usr/src/php/Zend/zend.c:1799
#20 0x0000562f6fcabb4d in php_execute_script (primary_file=0x7fff07a55620) at /usr/src/php/main/main.c:2542
#21 0x0000562f6fee55db in main (argc=2, argv=0x7fff07a559f8) at /usr/src/php/sapi/fpm/fpm/fpm_main.c:1917
Strace of failing worker:
mmap(NULL, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fca8de5c000
munmap(0x7fca8de5c000, 16384) = 0
mmap(NULL, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fca8de5c000
munmap(0x7fca8de5c000, 16384) = 0
mmap(NULL, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fca8de5c000
munmap(0x7fca8de5c000, 16384) = 0
mmap(NULL, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fca8de5c000
munmap(0x7fca8de5c000, 16384) = 0
mmap(NULL, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fca8de5c000
munmap(0x7fca8de5c000, 16384) = 0
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0xffffffff00000016} ---
+++ killed by SIGSEGV (core dumped) +++
This should be fixed via https://github.com/php/php-src/commit/90f2e7607a7a9da1c50f3588adc79e189fd66349