Apache / PHP segfaults (PHP 8.0 and 8.1, but also 7.4)
Description
PHP 7.4.27 / 8.0.15 / 8.1.3 Apache 2.4.52 OpenBSD 7.0 / amd64
If running any of the above PHP versions under Apache (as module), I noticed that every time when apache rotate it's logs it crashes. To be sure of this issue I set the log rotation at a fixed hour (not by size). In the last 3 days, each day at the exactly same hour apache crashed and did not crashed in the rest. Finally I did not loaded the opcache.so and it did not crashed on log rotation. After further investigation I noticed this:
The problem is somwehere in opcache.so. All the above versions are affected. The PHP 7.3.33 works OK, it does not crash.
How to reproduce the issue:
- load the opcache.so PHP extensions
- apachectl2 graceful OR kill -HUP 12345 (where 12345 is the process ID number)
- apache crashes
If I do not load opcache.so it does not crashes.
PHP Version
8.0.15
Operating System
OpenBSD 7.0
I was also tried a different compile scenario.
I compiled PHP 8.0.16 as above, win an extra additional flag: --enable-zts
In this scenario apache2 refuses to load the PHP opcache. Without "--enable-zts" it loads the opcache but crashes on kill -HUP / kill -USR1.
If ZTS enabled, everything works OK except opcache ... If I try to load opcache.so apache does not start at all and display the following error in the errors_log:
ld.so: httpd2: relocation error 16 idx 3086
If ZTS flag disabled, without opcache it works just fine. With PHP opcache crashes. Isn't opcache supposed to work and be threadsafe (if compiled with ZTS ?).
PS: with ZTS enabled, the php-cli also fails with relocation error if opcache is loaded
Update: To make the opcache to function when PHP 8.0 is build with --enable-zts I had to add this flag too: --disable-opcache-jit.
Otherwise apache or php-cli exit with errors if PHP ZTS try to load opcache extension:
ld.so: xxxx: relocation error 16 idx 3086
Is this what you expect ? Or is this another bug ?
@dstogov Could you reproduce the apache crash on SIGHUP or SIGUSR1 bug ?
@devnexen is the ld.so: xxxx: relocation error 16 idx 3086 a bug or not ?
If a bug, is related or not with the original bug described here ? If separate bug I can report it as a separate bug
@unix-world
If a bug, is related or not with the original bug described here
- it is not related, openbsd is not a proper target yet for ZTS build (thus why the packager maintainer did not enable it originally).
you highlighted a lot of concerns not related with the issue at hand but would you be able to create a patch which would replace (same file name as above)
https://github.com/php/php-src/blob/PHP-8.0.16/ext/opcache/ZendAccelerator.c#L771
int signal = SIGTERM;
by
int signal = SIGKILL;
?
normally it should not crash however SIGTERM had been chosen because it can be blocked but just to verify something.
I have tried this and still crashing. Not the SIGTERM is the problem ...
But I observed that in PHP 8.0 2 extra lines have been added and in PHP 7.3.33 they are not present:
// line # 783
} else if (errno != 0) {
zend_accel_error(ACCEL_LOG_WARNING, "Failed to send SIGKILL to locker %d: %s", mem_usage_check->l_pid, strerror(errno));
and
// line #795
} else if (errno != 0) {
zend_accel_error(ACCEL_LOG_WARNING, "Failed to check locker %d: %s", mem_usage_check->l_pid, strerror(errno));
I am trying with this 4 lines commented out, ... I have an idea that here might be the clue, after issuing SIGTERM or SIGKILL the apache may halt the access to logging and here might be a crash perhaps ...
because the other log situations in this code block I could not reproduce but they might be also crashing, but never got yet entered in the if (errno == ESRCH) { where are other 2 calls to zend_accel_error().
Let me try this and I get back.
@devnexen
I was trying any of the above scenarios.
int signal = SIGTERM; -> int signal = SIGKILL;
Then commenting out the } else if (errno != 0) { zend_accel_error(...) lines.
Still the same result, crashing on SIGHUP or SIGUSR1, both scenarios.
I was talking with an internal guy here that have some limited C skills and he told me that from what he can see in the debug log the crashing issue comes from accel_startup or start_accel_module methods, thus he presumes everything is fine when doing SIGTERM or SIGHUP, but on SIGHUP actually apache is shuting down then restart all childs, thus he thinks is an issue with restart after SIGHUP not with the closing process on SIGHUP.
#1 0x00000cec80fed18e in start_accel_module () at ext/opcache/zend_accelerator_module.c:518
No locals.
#2 0x00000cec80fdf63f in accel_startup (extension=0xcec16ffe210) at ext/opcache/ZendAccelerator.c:3114
No locals.
Thus I have noticed only one major difference in the logic between PHP 7.3.33 and 8.0.16: In 7.3.33 the accel_activate method is called in ext/opcache/ZendAccelerator.c, line # 3156 https://github.com/php/php-src/blob/php-7.3.33/ext/opcache/ZendAccelerator.c#L3156
In 8.0.16 the accel_activate method is called in a completely different place: ext/opcache/zend_accelerator_module.c, line # 507 https://github.com/php/php-src/blob/php-8.0.16/ext/opcache/zend_accelerator_module.c#L507
What is your opinion on this ? I am not aware too much of Zend internal code ... Should I try to create a patch to call accel_activate in the same place as it was in PHP 7.3.33 ? Does it make sense ?
I do not have opinion yet, I ll try to set up this weekend hopefully a separate openbsd VM to be able to "mess up" with packages and debugging unless someone find some clue in the meantime.
@devnexen
OK. If you cannot reproduce the bug just let me know and I will send you the exact configs I use. I use OpenBSD since more than 10 years or so, if you need any support for installing, config or others just let me know too. I am very focused to see this bug solved ... :-)
By the way, you can install the binary ports, but in order to compile it from source there are some things you have to know if you don't already know:
- the original src ports of OpenBSD 7.0 are too old and have not the latest updates to build from source PHP 8.0.16
- the current ports are already targeting OpenBSD 7.1 beta, thus perhaps will fail to build at some packages
- the original src ports of OpenBSD 7.0, with updates packages just targeting OpenBSD 7.0 you can find here https://codeberg.org/unixman/ports-openbsd-uxm (this repo is containing only the changes marked as stable from the ongoing ports and also extra several ports that are not found in the original openbsd ports: ex: unixman/php-mongodb or unixman/php-xdebug)
- or you can download and use original OpenBSD 7.0 src ports but you have to update from the current src ports some packages like: zstd, apr, apr-utils, apache-httpd, php and some others they depend on
- an alternative also would be you install all binary ports except PHP and use and updated src ports for PHP to start build 8.0.16 (but not sure exactly if all dependencies are met).
I can confirm that also the original ports built by the package maintainers have this bug. I get the email messages with one of the maintainers. He told me that this is for sure something inside PHP opcache that changed.
PS: also be sure to use the llvm 11, which is the default compiler (not gcc)
R.
@unix-world I suspect, the crash is related to Apache graceful restart and dynamic loading of php and opcache. According to backtrace opcache can't call back to PHP from accel_startup(). I don't have OpenBSD installation and can't reproduce the problem,
@unix-world
The best backtrace shows crash under start_accel_module(), but this function just forwards to zend_startup_module(), that is not in backtrace.
Try to attach GDB/LLDB to the main Apache process, set breakpoint on start_accel_module() and then try to understand what is going wrong after restart. It's also possible to add some instrumentation code into start_accel_module() to write address of zend_startup_module and accel_module_entry into Apache error log.
@dstogov Based on your explanation, I am not sure this is all ... Why the PHP 7.3.33 does not crash with the exactly same apache build and exactly same configuration ? I currently have 4 versions of PHP all installed: 7.3.33 7.4.28 8.0.16 8.1.3 Except PHP 7.3.33 all the rest are crashing (7.4.28 / 8.0.16 / 8.1.3). Apache is the same, 2.4.52. When switching from a PHP version to another I just change one thing - the PHP configuration for apache, in the apache configuration is 100% identical. More, to test I loaded just opcache.so extension for any of these versions. See the only configs I change when switching between the PHP versions:
LoadModule php7_module /usr/local/lib/php-7.3/libphp7.so
<IfModule mod_php7.c>
AddType application/x-httpd-php .php .phtml .php3
AddType application/x-httpd-php-source .phps
# Most php configs require this
DirectoryIndex index.php
</IfModule>
LoadModule php7_module /usr/local/lib/php-7.4/libphp7.so
<IfModule mod_php7.c>
AddType application/x-httpd-php .php .phtml .php3
AddType application/x-httpd-php-source .phps
# Most php configs require this
DirectoryIndex index.php
</IfModule>
LoadModule php_module /usr/local/lib/php-8.0/libphp.so
<IfModule mod_php.c>
AddType application/x-httpd-php .php .phtml .php3
AddType application/x-httpd-php-source .phps
# Most php configs require this
DirectoryIndex index.php
</IfModule>
LoadModule php_module /usr/local/lib/php-8.1/libphp.so
<IfModule mod_php.c>
AddType application/x-httpd-php .php .phtml .php3
AddType application/x-httpd-php-source .phps
# Most php configs require this
DirectoryIndex index.php
</IfModule>
All 4 versions are built with exactly the same build options and same platform patches (of course adapted by version).
How can you explain this ?
I noticed all the crashing versions (7.4.x / 8.0.x / 8.1.x) have inside opcache code a major change:
the accel_activate method is called in a completely different place:
ext/opcache/zend_accelerator_module.c
but in 7.3.33 which is not crashing
the accel_activate method is called in ext/opcache/ZendAccelerator.c
I suspect is something about this change ... because the opcache JIT is just in 8.0.x and 8.1.x but 7.4.x still crashes and does not have yet the opcache JIT...
PS: I will try to fill up with logging before each execution line and see where it stops ... Running apache prefork with GDB I am not sure if possible, there are many childs not only the main process running at the same time concurrently and the crashing is when doing SIGHUP which suppose will stop all childs except the main process. Do you have any idea how to run this under gdb ?
I am really confused. You closed this bug without any explanation.
Look at the below bug, it was an older bug reporting approximately the same issue, perhaps still not solved: https://bugs.php.net/bug.php?id=73901
** And I think the following FreeBSD bug is exactly the same after they switch from PHP 7.3 to 7.4 ... and still unsolved / unfixed in PHP 8.0 / 8.1 !: ** https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=247487
@devnexen I don't know why this ticket was closed without an answer. More, this looks like a serious security issue to me.
In my opinion the bug I reported here is because of the new changes in the opcache, in PHP 7.4 / 8.0 and 8.1. PHP 7.3 is working correctly, without this kind bug. Somewhere in time it this kind of opcache bugs were fixed, but now appears (in another form) that have come back. Looks to me very similar with this one: https://bugzilla.redhat.com/show_bug.cgi?id=1314888
I am not a C expert, but this clear looks to me like a "use-after-free error" ... And probabily is comming from this changed area:
In 7.3.33 the accel_activate method is called in ext/opcache/ZendAccelerator.c, line # 3156 https://github.com/php/php-src/blob/php-7.3.33/ext/opcache/ZendAccelerator.c#L3156
In 8.0.16 the accel_activate method is called in a completely different place: ext/opcache/zend_accelerator_module.c, line # 507 https://github.com/php/php-src/blob/php-8.0.16/ext/opcache/zend_accelerator_module.c#L507
Please not that I included the PHP 7.4 in this discussion just to exclude the possibility this bug have nothing to do with the opcache JIT (introduced only since 8.0) ! Thus, PHP 7.4 have no opcache JIT and still crashing because it uses a similar way of calling accel_activate in ext/opcache/zend_accelerator_module.c just like PHP 8.0 and 8.1.
@dstogov , as you said:
The best backtrace shows crash under start_accel_module(), but this function just forwards to zend_startup_module(), that is not in backtrace.
But please look at this part of the code and you see that accel_activate is called here !!:
PHP 8.0 / 8.1 / 7.4
static zend_module_entry accel_module_entry = {
STANDARD_MODULE_HEADER,
ACCELERATOR_PRODUCT_NAME,
ext_functions,
ZEND_MINIT(zend_accelerator),
ZEND_MSHUTDOWN(zend_accelerator),
accel_activate, // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! THIS I AM TALKING ABOUT
NULL,
zend_accel_info,
PHP_VERSION,
NO_MODULE_GLOBALS,
accel_post_deactivate,
STANDARD_MODULE_PROPERTIES_EX
};
int start_accel_module(void)
{
return zend_startup_module(&accel_module_entry);
}
But in PHP 7.3 (not crashing) is called in completely different place
static zend_module_entry accel_module_entry = {
STANDARD_MODULE_HEADER,
ACCELERATOR_PRODUCT_NAME,
accel_functions,
ZEND_MINIT(zend_accelerator),
ZEND_MSHUTDOWN(zend_accelerator),
NULL, // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! THIS I AM TALKING ABOUT
NULL,
zend_accel_info,
PHP_VERSION,
NO_MODULE_GLOBALS,
accel_post_deactivate,
STANDARD_MODULE_PROPERTIES_EX
};
int start_accel_module(void)
{
return zend_startup_module(&accel_module_entry);
}
In PHP 7.3 the accel_activate is called here
ZEND_EXT_API zend_extension zend_extension_entry = {
ACCELERATOR_PRODUCT_NAME, /* name */
PHP_VERSION, /* version */
"Zend Technologies", /* author */
"http://www.zend.com/", /* URL */
"Copyright (c) 1999-2018", /* copyright */
accel_startup, /* startup */
NULL, /* shutdown */
accel_activate, /* per-script activation */
accel_deactivate, /* per-script deactivation */
NULL, /* message handler */
NULL, /* op_array handler */
NULL, /* extended statement handler */
NULL, /* extended fcall begin handler */
NULL, /* extended fcall end handler */
NULL, /* op_array ctor */
NULL, /* op_array dtor */
STANDARD_ZEND_EXTENSION_PROPERTIES
};
I see accel_activate() is going to be called from different places, but I don't see it in the backtrace. So this is not a problem. The crash occurs in start_accel_module(), that is the same for all PHP versions. I can't guess what is going wrong. I suspect issues with dynamic loader.
I suspect calling the accel_activate() from the new place in PHP 7.4 / 8.0 / 8.1 which also occurs when calling start_accel_module() could be a cause ... not sure.
You may revert the the commit https://github.com/php/php-src/commit/34a491d1ed5 to check this. Anyway, there is nothing wrong with this commit.