php-src icon indicating copy to clipboard operation
php-src copied to clipboard

opcache.consistency_checks > 0 causes segfaults in PHP >= 8.1.5 in fpm context

Open CyberLine opened this issue 4 years ago • 29 comments

Description

After upgrading some projects to PHP 8.1.0 and 8.1.2 we could see requests getting slower and slower over time. Restart of the correspondig fpm-pool fixed that for short. After digging in the problem we could isolate and reproduce it:

  • use a Debian 10 or 11 maschine and install php8.1-fpm and php8.1-opcache.
  • modify opcache.ini:
zend_extension=opcache.so

[opcache]
opcache.consistency_checks=1
  • require laminas: composer require laminas/laminas-servicemanager
  • create a test script (we use laminas-servicemanager here):
<?php

declare(strict_types=1);

chdir(dirname(__DIR__));
require 'vendor/autoload.php';

use Laminas\ServiceManager\ServiceManager;
$container = new ServiceManager();
  • call that script in a loop 1000 or more times while running htop in another terminal: for i in {1..10000}; do curl localhost/test.php; done;

You would see an increment usage of memory that will not getting freed after all. Set opcache.consistency_checks to 0 to check that it will not keep any used memory.

PHP Version

8.1.0

Operating System

Debian 10/11

CyberLine avatar Feb 09 '22 09:02 CyberLine

consistency_checks definitely used to be a production-quality feature, of course that came with a penalty of a certain performance hit - but running a checksum every now and again is often not a huge price to pay for the safety net it used to provide. Can it go back into being that?

Provided we can fix the checksum calculation, I think we should change how errors are handled. Currently, only an info is logged (which are not enabled by default), so these errors are easily missed. From an info it's also unclear to the user whether that is even an issue. Scripts are also recompiled with every checksum mismatch which will fill shared memory very quickly if it happens after every request, and thus lead to a complete restart of opcache very often. I think that does more harm than good. It might make more sense to just log the error, keep using the old script and ask the user file a bug. If the safety net leads to filling of memory and crashing servers people will likely not use it :slightly_smiling_face:

but it doesn't explain why until recently, consistency_checks worked fine while doing a whole ->mem checksum.

I checked and it looks like the original implementation is very similar to what we have now:

https://github.com/php/php-src/blob/528006a3b49fd45d6b2803c7b7843b2e7d6929d6/ZendAccelerator.c#L818-L839

The zend_persistent_script.dynamic_members are explicitly filtered out, the rest is used for the checksum calculation. If I remember correctly classes were always copied from shared memory into process memory at the time specifically to avoid modifications to shared memory, so this is not something that should've occurred back then.

So, at least from how I understand it, the mem block (apart from the zend_persistent_script.dynamic_members) was indeed completely immutable. This only changed with the JIT (modifying opcode handlers) and inheritance cache (modifying zend_class_entry.inheritance_cache). The reason this was not noticed is likely because we don't have a single test that enables consistency_checks manually, nor do we test it in the opcache variation tests on nightly. This is definitely something we should do if we can sort things out.

I'll wait for Dmitry to weigh in

:+1: Since this issue can be avoided by disabling consistency_checks for now there's no rush.

iluuu1994 avatar Aug 21 '22 13:08 iluuu1994

consistency_checks definitely used to be a production-quality feature, of course that came with a penalty of a certain performance hit - but running a checksum every now and again is often not a huge price to pay for the safety net it used to provide. Can it go back into being that?

Provided we can fix the checksum calculation, I think we should change how errors are handled. Currently, only an info is logged (which are not enabled by default), so these errors are easily missed. From an info it's also unclear to the user whether that is even an issue. Scripts are also recompiled with every checksum mismatch which will fill shared memory very quickly if it happens after every request, and thus lead to a complete restart of opcache very often. I think that does more harm than good. It might make more sense to just log the error, keep using the old script and ask the user file a bug. If the safety net leads to filling of memory and crashing servers people will likely not use it 🙂

What we're currently seeing is a result of the checksum calculation being inherently broken. Behind that feature, there's a hidden assumption that generally speaking - neither that feature, nor some other glaring part in PHP - are fundamentally broken, and repeatedly result in a varying checksum on every request - which would in turn fill up the shared memory and do a lot more harm than good. If we put these scenarios aside (which apparently aren't that common) - this feature is aimed at providing a safety net against unexpected, not necessarily recurring bugs in PHP or underlying library that happen in certain edge cases.

E.g., IIRC in the past (20 years ago or so), there was a bug in the OCI8 library that overwrote parts of opcodes in some rare situations. The problem is that without that feature - it would effectively mean the server would start crashing on every access to the corrupted file (not just in case of the rare extreme bug, but any subsequent request following it) - without any realistic prospect for recovery other than a server restart. Over the years there have been issues that were detected by this feature, and I can't recall (not that it means that much 🙂) situations similar to the one we're currently experiencing, when corruption was so prevalent that this feature was effectively useless.

but it doesn't explain why until recently, consistency_checks worked fine while doing a whole ->mem checksum.

I checked and it looks like the original implementation is very similar to what we have now:

https://github.com/php/php-src/blob/528006a3b49fd45d6b2803c7b7843b2e7d6929d6/ZendAccelerator.c#L818-L839

It's old, but it's certainly not the original implementation... The original was written back in 1999-2000. So a lot has passed between then and when we open sourced it in 2013. That said, it's quite possible that I'm just not remembering correctly (I remember a much more complex checksum calculation implementation). Unfortunately I no longer have access to the original CVS (!) code that hosted these archeological versions...

So, at least from how I understand it, the mem block (apart from the zend_persistent_script.dynamic_members) was indeed completely immutable. This only changed with the JIT (modifying opcode handlers) and inheritance cache (modifying zend_class_entry.inheritance_cache). The reason this was not noticed is likely because we don't have a single test that enables consistency_checks manually, nor do we test it in the opcache variation tests on nightly. This is definitely something we should do if we can sort things out.

I'll wait for Dmitry to weigh in

👍 Since this issue can be avoided by disabling consistency_checks for now there's no rush.

If there's no way around this then I guess disabling consistency_checks when jit is enabled could be a way to go... But, still need to understand whether we need both consistency_checks and protect_memory.

zsuraski avatar Aug 21 '22 14:08 zsuraski

This is definitely a bug that was introduced with tracing (and adoptive) JIT and then with inheritance cache.

I think the easiest way to fix the problem (without binary compatibility breaks) is maintaining of a skip list (just a sorted plain array of offsets from zend_persistent_script.mem). This list should be used by zend_adler32() to skip some mutable pointers.

@iluuu1994 would you like to try implement this solution?

dstogov avatar Aug 22 '22 07:08 dstogov

@dstogov Sure, I can give it a try. This should work well for inheritance_cache as it is always NULL initially. For tracing JIT, on the initial checksum calculation it will be unknown (I think?) which op handlers will be replaced at a later point in time, so would we need to skip all handlers for the hashsum calculation?

iluuu1994 avatar Aug 22 '22 07:08 iluuu1994

I think, we will need to skip only the handlers overridden by zend_jit_setup_hot_trace_counters() and potentially by zend_jit_trace_setup_ret_counter(). Function JIT may override only the handlers of the first instructions of basic blocks with (ZEND_BB_START|ZEND_BB_ENTRY|ZEND_BB_RECV_ENTRY) flags.

dstogov avatar Aug 22 '22 08:08 dstogov

I would construct that skip list, by adding pointers directly from zend_jit_setup_hot_trace_counters() and similar functions for adoptive function JIT.

dstogov avatar Aug 22 '22 08:08 dstogov

@dstogov Thanks for the suggestion! I'll give that a try soon.

iluuu1994 avatar Aug 22 '22 08:08 iluuu1994

@dstogov This seems to also be necessary for zend_jit_trace_setup_ret_counter. However, I have no idea under what circumstances this handler gets replaced. Can you guide me on how to identify these oplines? Here's the initial PoC:

https://github.com/php/php-src/compare/master...iluuu1994:php-src:checksum-skip-list

It works well for the consistency_checks, (JIT_G(hot_loop) and JIT_G(hot_func), although I'm not sure if there's a way to avoid recomputation of the CFG for JIT_G(hot_loop) and just skipping some oplines more generously. Also, the patch is not binary compatible as we're modifying zend_persistent_script , accel_globals and zend_adler32, but I don't see a way to avoid that.

iluuu1994 avatar Aug 25 '22 13:08 iluuu1994

@iluuu1994 I made only a quick review...

zend_jit_trace_setup_ret_counter may set a handler on zend_op that follows DO_UCALL/DO_FCALL instruction. (run sapi/cli/php -d opcache.jit_debug=0x1ff002 ../Zend/bench.php 2>&1 | less than search for "start (return" for example)

It would be great to avoid CFG recalculation. For tracing JIT you may call checksum_skip_list_add directly from zend_jit_restart_hot_trace_counters() + add instructions after DO_UCALL/DO_FCALL. Function JIT should work out of the box. JIT on first exec, profile request and hot counter may need CFG.

Binary compatibility shouldn't be a problem, because zend_persistent_script , accel_globals and zend_adler32 are used only by opcache.

dstogov avatar Aug 29 '22 06:08 dstogov