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

Constants in Enum classes causes php-fpm worker to segfault

Open alshenetsky opened this issue 4 years ago • 16 comments

Description

I have such class:

<?php

namespace App\Domain\TournamentTable;

enum ResultsModifier
{
    case HOME;
    case AWAY;
    case OVERTIME;
    case REGULATION_TIME;
    case OVERALL;
    public const SPECIAL_MODIFIERS = [
        ResultsModifier::AWAY,
        ResultsModifier::HOME,
        ResultsModifier::OVERTIME,
        ResultsModifier::REGULATION_TIME,
    ];
}

When I use this constant in a loop in another class, the php-worker crashes

            foreach (ResultsModifier::SPECIAL_MODIFIERS as $modifier) {
// segfault

Output:

[21-Feb-2022 23:28:26] WARNING: [pool www] child 46 exited on signal 11 (SIGSEGV) after 3.227291 seconds from start

The same code works if I:

  • either turn off opcache (opcache.enable = 0)
  • either remove reference to the constant:
foreach ([ResultsModifier::AWAY, ResultsModifier::HOME, ResultsModifier::OVERTIME, ResultsModifier::REGULATION_TIME] as $modifier) {
// works as expected

So it is probably a bug with opcache.

PHP Version

PHP 8.1.3

Operating System

Alpine 3.13

alshenetsky avatar Feb 21 '22 23:02 alshenetsky

I cannot reproduce the segfault (PHP-8.1 on Windows CLI; tried with JIT and without). If I put var_dump($modifiers); in the loop body, I get the expected:

enum(App\Domain\TournamentTable\ResultsModifier::AWAY)
enum(App\Domain\TournamentTable\ResultsModifier::HOME)
enum(App\Domain\TournamentTable\ResultsModifier::OVERTIME)
enum(App\Domain\TournamentTable\ResultsModifier::REGULATION_TIME)

Can you please provide a stack backtrace?

cmb69 avatar Feb 22 '22 08:02 cmb69

I would be happy to, and I have the coredump file, but when I compiled php with the --enable-debug flag, my application no longer runs:

Assertion failed: zval_get_type(&(*(z))) < 6 (/usr/src/php/ext/opcache/zend_persist_calc.c: zend_persist_zval_calc: 138)

So I can't, sorry.

alshenetsky avatar Feb 22 '22 23:02 alshenetsky

I think you still could get a backtrace if you're running under gdb; after the assertion has been triggered, typing bt should provide it. However, I guess the assertion failed due to a prior memory corruption. Are you running with JIT enabled (if in doubt, check PHP info)? If so, what's the value of opcache.jit?

cmb69 avatar Feb 23 '22 10:02 cmb69

I failed at it. As far as I understand, gdb attaches to one of the fpm processes. But my php-fpm (when compiled with --enable-debug) doesn't start at all. I should also clarify that I observe this in a docker container inside a kubernetes pod.

Also, I cannot reproduce it running same script in CLI (even with opcache.enable_cli => On).

In answer to the second question:

php -i | grep "jit"
auto_globals_jit => On => On
pcre.jit => 1 => 1
opcache.jit => tracing => tracing
opcache.jit_bisect_limit => 0 => 0
opcache.jit_blacklist_root_trace => 16 => 16
opcache.jit_blacklist_side_trace => 8 => 8
opcache.jit_buffer_size => 0 => 0
opcache.jit_debug => 0 => 0
opcache.jit_hot_func => 127 => 127
opcache.jit_hot_loop => 64 => 64
opcache.jit_hot_return => 8 => 8
opcache.jit_hot_side_exit => 8 => 8
opcache.jit_max_exit_counters => 8192 => 8192
opcache.jit_max_loop_unrolls => 8 => 8
opcache.jit_max_polymorphic_calls => 2 => 2
opcache.jit_max_recursive_calls => 2 => 2
opcache.jit_max_recursive_returns => 2 => 2
opcache.jit_max_root_traces => 1024 => 1024
opcache.jit_max_side_traces => 128 => 128
opcache.jit_prof_threshold => 0.005 => 0.005

alshenetsky avatar Feb 24 '22 00:02 alshenetsky

opcache.jit_buffer_size => 0 => 0

So OPcache JIT is disabled. I have no idea how to proceed here; maybe someone else has.

cmb69 avatar Feb 24 '22 09:02 cmb69

I also tried reproducing this, to no avail.

I would be happy to, and I have the coredump file, but when I compiled php with the --enable-debug flag, my application no longer runs

The assertion hints at z holding an unexpected value. You could place a breakpoint at that line and check what z->u1.v.type returns. According to that assert it should be < IS_STRING (6), so a primitive zval with no additional data.

iluuu1994 avatar Mar 11 '22 15:03 iluuu1994

We are meeting very similar issue. Disabling opcache helps.

janedbal avatar Mar 25 '22 14:03 janedbal

Seems like even disabling preloading helps in our case.

janedbal avatar Mar 28 '22 11:03 janedbal

No feedback was provided. The issue is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so. Thank you.

github-actions[bot] avatar Apr 12 '22 00:04 github-actions[bot]

Thanks for the hint, I can reproduce it with preloading.

iluuu1994 avatar Apr 14 '22 09:04 iluuu1994

Thanks!

janedbal avatar Apr 21 '22 10:04 janedbal

Just for the record, we're experiencing really strange errors after re-enabling preloading on PHP 8.1.6, as if the memory was somehow messed up:

return BillingType::tryFrom($value);

where BillingType is regular backed enum:

enum BillingType: string
{
    case Terms = 'terms';
    case Prepaid = 'prepaid';
}

fails on following error: Fulfillment\CommonBundle\Utils\EnumHelper::create(): Return value must be of type BackedEnum, Doctrine\ORM\Id\IdentityGenerator returned, which does not make any sense.

We're still using the same preloading configuration which worked before transitioning from constants to enums and the code works just fine with preloading disabled.

I'll try to create a reproducible case and submit a new issue, I just wanted to warn others about possible problems for now.

olsavmic avatar May 20 '22 14:05 olsavmic

@olsavmic, yes, please file a new ticket. :)

cmb69 avatar May 20 '22 14:05 cmb69

@olsavmic You never created an issue for this, are you still experiencing this problem?

iluuu1994 avatar May 27 '22 11:05 iluuu1994

@iluuu1994 Yes, the issue still occurs, we're running with preloading disabled. I was just not able to create a reproducible test case nor extract some insightful logs as I was quite busy for the past week (and will be for the week to come).

If you're fine with creating an open issue with no more details than those I posted to this channel, I'll create the issue offhand :)

olsavmic avatar May 27 '22 12:05 olsavmic

@olsavmic It can also wait until you have more information, I just wanted to confirm this is still an issue. :slightly_smiling_face: Thanks for your response.

iluuu1994 avatar May 27 '22 13:05 iluuu1994