Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT segmentation fault in PHP 8.1 #7817

Open
cappadaan opened this issue Dec 23, 2021 · 201 comments
Open

JIT segmentation fault in PHP 8.1 #7817

cappadaan opened this issue Dec 23, 2021 · 201 comments

Comments

@cappadaan
Copy link

cappadaan commented Dec 23, 2021

Description

PHP 8.1.0 + 8.1.1 produces segfault, randomly. Downgrading to 8.0 solves the issue.

--core dump---

BFD: Warning: coredump-php-fpm.30267 is truncated: expected core file size >= 5413076992, found: 35983360.
[New LWP 30267]
[New LWP 1887]
[New LWP 1886]
[New LWP 1888]
Cannot access memory at address 0x7f277dbb3128
Cannot access memory at address 0x7f277dbb3120
Failed to read a valid object file image from memory.
Core was generated by `php-fpm: pool xxxxxx '.

Program terminated with signal 11, Segmentation fault.
#0 0x000055bbf04c0f25 in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER () at /usr/src/debug/php-8.1.1/Zend/zend_vm_execute.h:10137
10137 ce = CACHED_PTR(opline->op2.num);
(gdb) bt
Python Exception <class 'gdb.MemoryError'> Cannot access memory at address 0x7ffdc940e3a8:
(gdb) bt
#0 0x000055bbf04c0f25 in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER () at /usr/src/debug/php-8.1.1/Zend/zend_vm_execute.h:10137
Cannot access memory at address 0x7ffdc940e3a8
(gdb) frame 0
#0 0x000055bbf04c0f25 in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER () at /usr/src/debug/php-8.1.1/Zend/zend_vm_execute.h:10137
10137 ce = CACHED_PTR(opline->op2.num);
(gdb) info frame
Stack level 0, frame at 0x7ffdc940e3b0:
rip = 0x55bbf04c0f25 in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER (/usr/src/debug/php-8.1.1/Zend/zend_vm_execute.h:10137); saved rip Cannot access memory at address 0x7ffdc940e3a8

this is the only available info in the core dump.

PHP Version

PHP 8.1.0 + 8.1.1

Operating System

CentOS 7

@cmb69
Copy link
Member

cmb69 commented Dec 23, 2021

I'm afraid this is not actionable without further information, since you didn't provide a reproduce script, and the stack backtrace only shows 1 frame. Since you claim that happens randomly, providing a reproduce script may not be possible, but at the very least we need more info, such as whether OPcache is enabled, whether OPcache JIT is enabled (tracing or function), and in which environment (SAPI) this happens, and also whether that happens only occasionally (and recovers afterwards or not), or happens frequently. Also, try to run with the latest PHP-8.1 development version, where some PHP 8.1.1 bugs have been fixed.

@cappadaan
Copy link
Author

  • it happens in php-fpm
  • randomly and frequent, cannot reproduce
  • there is nothing more in the backtrace, so its hard to find what function causes this
  • opcache settings:
    opcache.enable = on;
    opcache.enable_cli=1;
    opcache.jit_buffer_size=1G;
    opcache.validate_root = on;
    opcache.enable_cli = on;
    opcache.file_update_protection = 0;
    opcache.revalidate_freq = 0;
    opcache.validate_timestamps = 0;
    opcache.interned_strings_buffer = 64;
    opcache.max_accelerated_files = 16229;
    opcache.memory_consumption = 4096;
    opcache.fast_shutdown = 1;
    opcache.log_verbosity_level = 2;
    opcache.blacklist_filename = /etc/opcache-blacklist.txt;
    opcache.force_restart_timeout = 1800;

@cmb69
Copy link
Member

cmb69 commented Dec 23, 2021

Thanks for the further info! Does it also happen when JIT is disabled (i.e. opcache.jit_buffer_size=0)? If not, what happens if you use a smaller jit_buffer_size (1GB seems very much, maybe try 16M or 64M).

@cappadaan
Copy link
Author

I disabled JIT for now, will update in a few days.

@cmb69
Copy link
Member

cmb69 commented Dec 23, 2021

Okay, I'll change back to feeback status (the ticket will be kept open for 2 weeks).

@cappadaan
Copy link
Author

Disabling JIT seemed to solve the problem. So JIT is the cause of the segfault.

I have now enabled it and set the cache to 64M, will update later.

@cappadaan
Copy link
Author

Again another crash with SIGSEGV after setting JIT to 64M.
So definitely JIT causes this crash.

For now we leave it off there is some sort of fix (or newer php version).

If you need more info just let me know.

@nikic nikic changed the title Segmentation fault PHP 8.1 JIT segmentation fault in PHP 8.1 Dec 25, 2021
@w3yyb
Copy link

w3yyb commented Dec 25, 2021

It's similar to the problem :https://bugs.php.net/bug.php?id=81664

@cmb69
Copy link
Member

cmb69 commented Dec 27, 2021

Okay, so we now there is a randomly but frequently segfault if (tracing) JIT is enabled under FPM, which happens at:

#0 0x000055bbf04c0f25 in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER () at /usr/src/debug/php-8.1.1/Zend/zend_vm_execute.h:10137
10137 ce = CACHED_PTR(opline->op2.num);

I'm afraid that is insufficient information to be actionable, and generally it's hard to fix a bug which is not reproducible. :(

@meinemitternacht
Copy link

meinemitternacht commented Dec 27, 2021

We are also encountering this bug (or a very similar one) at my company. We use Yocto to build target images for X86_64, and PHP 8.1 is now producing segfaults with JIT enabled. What debugging options would be helpful here?

@meinemitternacht
Copy link

@cmb69 After some troubleshooting, it seems that our issue was due to the first flag, CPU optimizations. When enabled, CPUs with the avx instruction set, but not avx2 or avx512..., exhibited segfaults when running JIT. It may be worthwhile to see why this is occurring, but it would be more appropriate for us to open a separate issue at some point in the future.

@cmb69
Copy link
Member

cmb69 commented Dec 27, 2021

@meinemitternacht, so you have segfaults with opcache.jit=1254, but not with opcache.jit=0254 on these machines?

@meinemitternacht
Copy link

meinemitternacht commented Dec 27, 2021

@cmb69 That is correct.

After testing, this was determined to be incorrect.

@jorismak
Copy link

jorismak commented Nov 16, 2023 via email

@reza305z
Copy link

this is still happening to me on php8.2.8-fpm. the only temporary solution I found is to use function instead of tracing.

Same here with php8.2.12-fpm. I think maybe there is some php extension that causes this problem.

@fidoboy
Copy link

fidoboy commented Nov 18, 2023

Same here. Can't enable JIT with PHP-FPM 8.1.25 because it crashes. Centos server with Apache 2.4.58

@fidoboy
Copy link

fidoboy commented Nov 23, 2023

In my particular case I've discovered after some time of trial and error that the issue was caused by a shit called "Ioncube Loader" that was loading from PHP ini. I've removed it and now JIT is working as expected.

@alex-enginity
Copy link

Nice catch!. The server where I have this thing crash and take down most of my server, it crashed on PHP 8.1 and that particular version had Ioncube loader active, as I am using WHMCS on it.
The other versions don't have ioncube on them. So this could explain why all hell broke loose.

@fidoboy
Copy link

fidoboy commented Nov 23, 2023

Nice catch!. The server where I have this thing crash and take down most of my server, it crashed on PHP 8.1 and that particular version had Ioncube loader active, as I am using WHMCS on it. The other versions don't have ioncube on them. So this could explain why all hell broke loose.

You'll see the change as soon as you remove that Ioncube Loader from your PHP setup. I hope that this useless thing disappears from the open source realm forever. In my case it was almost inmediate. Since I removed that useless module, all is working as expected and there is no seg faults at all.

@DavidAnderson684
Copy link

I've got many servers which segfault where php-fpm segfaults when I turn JIT on (PHP 8.0, 8.1, 8.2), but don't otherwise, and none of them are running ionCube. So unfortunately not all the problems are inside ionCube,

@fidoboy
Copy link

fidoboy commented Nov 23, 2023

ou'll see the change as soon as you remove that Ioncube Loader from your PHP setup. I hope that this useless thing disappears from the open source realm forever. In my case it was almost inmediate. Since I removed that useless module, all is working as expected and there is no seg faults at all.

There are other issues involved obviously. Because in depth, the issue is that the design of JIT is already very young. But you can have for sure that Ioncube Loader is causing crashes with JIT.

@fidoboy
Copy link

fidoboy commented Nov 23, 2023

In my checks, as soon as I enable the Ioncube thing there is a failure. And it works fine when it's disabled.

@alex-enginity
Copy link

alex-enginity commented Nov 23, 2023

It must be quick on your case because maybe the extension is interacting in a way that creates more issues faster than without it. But in my particular case, even PHP8.2 was affected after some more time. On PHP8.1 with ioncube loader it went nuts in just 2 hours but I also lost access to Plesk, which was using it's on webserver. This means that whatever JIT is doing, it is jeopardizing the entire system.

Even if they fix the issues, I don't see me enabling this feature in any forseable future. It's way too risky. Specially if other people's business are on the line. Stability means everything to me.

This featured should be labelled EXPERIMENTAL with a warning message. Not used widely like some of them are doing such as Plesk in their new Performance Booster.

@DavidAnderson684
Copy link

What would be useful would an option for the JIT cache for a pool to completely empty itself if any child process in the pool segfaults. I am using pools with pm = ondemand . What I notice in the logs is that once one process in that pool segfaults, all the others launched in that pool segfault, and that continues until the requests stop coming in and allow the number of workers to drop again to zero, and then things are OK. So, by inference, something in the JIT cache is corrupt, and that means that any future worker using it will segfault; if the cache could then be automatically emptied by the master process clearing it (whether directly or by killing all workers in the pool), then things would go a lot better.

@miermontoto
Copy link

PHP 8.3 could help fix this issue:
image

i haven't had the chance to test this thoroughly yet.

@DavidAnderson684
Copy link

Interestingly, I've had only a total of 5 segfaults in total of php-fpm since December 21st, which coincides with upgrading to 8.1.26 and 8.2.13 The week before that there were hundreds. The changelogs of both those versions include a fix of an issue causing a segfault in php-fpm.

@DavidAnderson684
Copy link

Well, scratch that theory: 83 today thus far already.

@marcing
Copy link

marcing commented Jan 31, 2024

I have a doctrine repository fork which is segfaulting on Windows when running unit tests on PHP CLI (8.0, 8.1, 8.2, 8.3). I cannot isolate a smaller sample, but I noticed also linux + FPM crashes on other projects when JIT is enabled. It does not happen every time like on Windows though.
For Windows reproducible segfault check: https://github.com/ovos/doctrine1 + php tests/run.php = segfault after Doctrine_Ticket_1113_TestCase, every single time on a Ryzen 5900X CPU, but not on GitHub Actions (probably a different CPU?)

@adic3x
Copy link

adic3x commented Feb 17, 2024

I have SIGSEGV error on 8.3.1

So environment is dev on win11 x86-64, Docker with nginx and php-fpm 8.3.1 / wsl. php souce files mapped as volumes: - ./:/var/www/myproject/

php.ini:

zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.jit_buffer_size=134217728
opcache.jit=1235 ; I don't remember why not 1255 or 1205

I can't say how to reproduce the problem, but I have two ideas.

First. Is editing source files works correctly with JIT at all? In dev mode I edit php files and changes applied immediately. Must I disable JIT on dev environment when I use Docker?

And second, maybe this code reproduces SIGSEGV error:

trait foo
{
    protected static function load(MyClass $thisStatic, bool $condition): mixed
    {
        $query = Comment::where('use', '=', $thisStatic->id);

        if ($condition) {
            $query->where(function (mixed $query) use ($thisStatic): void {
                $query->where('timestamp', '>=', $thisStatic->start)
                      ->orWhere('created_at', '>=', date('Y-m-d H:i:s', $thisStatic->start));
            });
        } else {
            $query->where(function (mixed $query) use ($thisStatic, $divider): void {
                $query->whereBetween('timestamp', [$thisStatic->start, $thisStatic->end])
                      ->orWhere('timestamp', '>', $divider)
                      ->orWhereBetween('created_at', [date('Y-m-d H:i:s', $thisStatic->start), date('Y-m-d H:i:s', $thisStatic->end)])
                      ->orWhere('created_at', '>', date('Y-m-d H:i:s', $divider));
            });
        }

        return $query;
    }
}

It's Laravel Eloquent query builder. I could guess JIT engine may not correctly process Closure with arguments (argument is object passed to function).

However I'm not sure. It's hard to fix a non-reproducible problem.

I will try to rewrite query without Closure and with whereRaw instead. And use 1205 flags. I will write here if I find any information.

@dstogov
Copy link
Member

dstogov commented Feb 19, 2024

First. Is editing source files works correctly with JIT at all? In dev mode I edit php files and changes applied immediately. Must I disable JIT on dev environment when I use Docker?

JIT should work transparently, but of course "editing" may trigger some bugs that are not visible in usual production environments and that are even harder to reproduce.

In case you could provide a docker image with reproducible scenario, we would be able to understand and fix the problem.

@xpader
Copy link

xpader commented Mar 29, 2024

Still happend with php 8.1.27 in offical php docker container.

There's nothing different, except add some extensions like this:

FROM php:8.1.27-fpm-alpine3.19

RUN echo "Start building.." \
    set -ex \
    && apk update \
    # install system base packages
    && apk add tzdata tini git openssh-server openssh-client rsync \
    # config timezone
    && ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
    && echo "${TIMEZONE}" > /etc/timezone \
    # install php extensions
    && apk add pcre-dev $PHPIZE_DEPS gmp-dev zlib-dev libpng-dev \
    && docker-php-ext-install bcmath gmp calendar exif gd pdo_mysql sockets opcache \
    && pecl install -n redis && docker-php-ext-enable redis.so \
    # show php version and extensions
    && php -v \
    && php -m

CMD ["tini", "--", "entrypoint.sh"]

Running by tini, use exec to instead sh process.

entrypoint.sh:

#!/bin/sh -e
/usr/sbin/sshd -e -E /var/log/sshd.log &

cp /opt/crontab /etc/crontabs/www && chown root:root /etc/crontabs/www && chmod 600 /etc/crontabs/www &
crond -L /var/log/crond.log &

exec /usr/local/sbin/php-fpm

@palgalik
Copy link

palgalik commented Apr 13, 2024

I ran into segmentation fault on 8.3.6 with my Symfony 7.x application with this config:

opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 16000
opcache.revalidate_freq = 60
opcache.fast_shutdown = 1
opcache.enable=1
opcache.enable_cli=1
opcache.jit=1235
opcache.jit_buffer_size=256M

@marcing
Copy link

marcing commented Apr 17, 2024

https://github.com/ovos/doctrine1 + php tests/run.php still segfaulting as of PHP 8.3.6.

@jorismak
Copy link

jorismak commented Apr 18, 2024 via email

@marcing
Copy link

marcing commented Apr 18, 2024

Dear @jorismak I tried few times in the past, but never managed to compile PHP on Windows. I'm using the official package from
https://www.php.net/downloads.php
However we have also segfaults on servers (Linux), so we do not use JIT in production right now, because we have to restart FPM every time it happens.

@klaussilveira
Copy link
Contributor

On PHP 8.3.9 and Symfony 7.1.2, and with opcache.jit=1235, also segfault. Zend Engine v4.3.9, Zend OPcache v8.3.9.

@cmb69
Copy link
Member

cmb69 commented Jul 30, 2024

@jorismak, @marcing, it is unlikely that the official Windows builds would use SIMD instructions which are not available, since they still use only SSE2 (on x64) and SSE (on x86). If you still want to do your own builds, see https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2. About a minimal build environment, see https://gist.github.com/cmb69/47b8c7fb392f5d79b245c74ac496632c.

As for the actual issue: these may actually be quite different issues, and it's almost impossible to fix; see #7817 (comment) on what might help.

To mitigate the crashes: try with tracing JIT disabled. If that doesn't work disable JIT altogether. If that still doesn't work, disable OPcache. Oh, and of course, if there are other "invasive" extensions (often zend_extensions) active, disable these, and see if it fixes (or at least improves) the situation. Info about what worked for you, and what not, wouldn't help us to fix the issue, but at least you would have stable system up and running.

@klaussilveira
Copy link
Contributor

opcache.jit=1205 has been stable for me so far.

@mreho
Copy link

mreho commented Aug 19, 2024

Hello, seems to be the same case here with a Wordpress installation that runs on PHP 8.3 (was the same on 8.1 and 8.2 using jit set at 1255)

[10-Aug-2024 13:39:51] WARNING: [pool www.site.tld] child 4001726 exited on signal 11 (SIGSEGV - core dumped) after 0.209034 seconds from start

Switching to jit 1205 seems to fix this issue

JIT seems experimental at this point..

@cmb69
Copy link
Member

cmb69 commented Aug 19, 2024

Info about what worked for you, and what not, wouldn't help us to fix the issue, […]

@daniellockyer
Copy link

daniellockyer commented Aug 28, 2024

This issue is really plaguing us. We were on 8.1.17, experiencing a really strange segfault within some Stripe-PHP code on one of our apps. I updated to 8.1.20, and that problem is fixed, but then I start getting segfaults on a totally different PHP on the same server. Had to revert that one to 7.3 😖 Changing JIT options doesn't seem to help with much

Unfortunately, I can't provide a repro but I have the following logs:

[33624850.446814] php-fpm8.1[5390]: segfault at 7fd70a5f1a38 ip 00007fed0cc18a6c sp 00007ffd2f24a3e0 error 4 in opcache.so[7fed0cc00000+df000] likely on CPU 7 (core 7, socket 0)

@cmb69
Copy link
Member

cmb69 commented Aug 28, 2024

Yesterday there was an accident in my household, so I called the ambulance. They asked: "Where do you live?" I replied: "In my house, of course!" For some reason, they still didn't show up. ;)

What I want to say: please don't reply to this thread if your issue isn't even related to JIT (I already expect that those who reported JIT related segfaults may actually face quite different issues). Instead open a new ticket, and try to provide as much useful information as you can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests