[php-src] Issue #14208: Segmentation fault in php_request_startup with ZTS
| From: | TiagoOliveiraMarques | Date: | Sun, 12 May 2024 14:49:07 +0000 |
| Subject: | [php-src] Issue #14208: Segmentation fault in php_request_startup with ZTS | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246900@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/14208
Author: TiagoOliveiraMarques
### Description
Hey 👋
I'm trying to embed PHP ZTS into a multi threaded C/C++ application, but I'm hitting a
segmentation fault on
php_request_startup when running scripts from multiple threads.
For my scenario I could perfectly fork the process but I always thought ZTS would somehow solve this
limitation (I might be missing some flag?)
I'm running from commit 8776561581a153a90619e2160e8e1c35871e3edb), but I was able to reproduce
when building from the branch PHP-8.3.7.
```
PHP 8.4.0-dev (cli) (built: May 11 2024 21:09:48) (ZTS DEBUG)
Copyright (c) The PHP Group
Zend Engine v4.4.0-dev, Copyright (c) Zend Technologies
```
I've user the following flags to build PHP:
```
./configure --enable-embed --enable-zts --disable-zend-signals --enable-zend-max-timers
--enable-debug
```
The C code I'm using to debug the issue (I removed some error handling logic for the sake of
making it short):
```
#include <pthread.h>
#include <stdio.h>
#include <sapi/embed/php_embed.h>
#include <unistd.h>
int main() {
const char HARDCODED_INI[] =
"html_errors=0\n"
"register_argc_argv=1\n"
"implicit_flush=1\n"
"output_buffering=0\n"
"max_execution_time=0\n"
"max_input_time=-1\n\0";
php_tsrm_startup_ex(10);
sapi_startup(&php_embed_module);
php_embed_module.ini_entries = HARDCODED_INI;
if (php_embed_module.startup(&php_embed_module) == FAILURE) {
sapi_shutdown();
tsrm_shutdown();
return -1;
}
// Execute
pthread_t threads[10];
for (int i = 0; i < 10; i++) {
pthread_create(&threads[i], NULL, [](void* arg) -> void* {
const char* filepath = "samples/hello_world.php";
for (int i = 0; i < 10; i++) {
if (php_request_startup() == FAILURE) {
fprintf(stderr, "php request startup failed\n");
return NULL;
}
FILE* fp = fopen(filepath, "r");
if (fp == NULL) {
php_request_shutdown((void*) 0);
return NULL;
}
zend_file_handle file_handle;
zend_stream_init_fp(&file_handle, fp, basename(filepath));
php_execute_script(&file_handle);
int exitStatus = EG(exit_status);
php_request_shutdown((void*) 0);
}
return NULL;
}, NULL);
}
for (int i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
return EXIT_SUCCESS;
}
```
The sample PHP file I'm using to debug the issue:
```php
<?php
echo "hello world\n";
```
Resulted in a segmentation fault. Running valgrind shows the following:
```
==216272== Memcheck, a memory error detector
==216272== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==216272== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==216272== Command: ./php-arrow
==216272==
==216272== Conditional jump or move depends on uninitialised value(s)
==216272== at 0x503D662: zend_string_equal_val (zend_string.c:447)
==216272== by 0x4F59985: zend_string_equal_content (zend_string.h:386)
==216272== by 0x4F5C773: zend_hash_find_bucket (zend_hash.c:756)
==216272== by 0x4F6348E: zend_hash_find (zend_hash.c:2667)
==216272== by 0x4F1CF5D: zend_hash_find_ptr (zend_hash.h:869)
==216272== by 0x4F1E04E: zend_get_constant_ptr (zend_constants.c:273)
==216272== by 0x4F1E0C4: zend_get_constant (zend_constants.c:288)
==216272== by 0x4EE05B0: zend_ini_get_constant (zend_ini_parser.y:152)
==216272== by 0x4EE22E9: ini_parse (zend_ini_parser.y:467)
==216272== by 0x4EE0B3A: zend_parse_ini_file (zend_ini_parser.y:238)
==216272== by 0x4E77E5E: php_init_config (php_ini.c:604)
==216272== by 0x4E6C68F: php_module_startup (main.c:2155)
==216272==
==216272== Conditional jump or move depends on uninitialised value(s)
==216272== at 0x503D662: zend_string_equal_val (zend_string.c:447)
==216272== by 0x4F59985: zend_string_equal_content (zend_string.h:386)
==216272== by 0x4F5C773: zend_hash_find_bucket (zend_hash.c:756)
==216272== by 0x4F5CA67: _zend_hash_add_or_update_i (zend_hash.c:831)
==216272== by 0x4F5D29A: zend_hash_update (zend_hash.c:994)
==216272== by 0x4E7709E: php_ini_parser_cb (php_ini.c:213)
==216272== by 0x4EE1B19: ini_parse (zend_ini_parser.y:375)
==216272== by 0x4EE0C0D: zend_parse_ini_string (zend_ini_parser.y:265)
==216272== by 0x4E78614: php_init_config (php_ini.c:724)
==216272== by 0x4E6C68F: php_module_startup (main.c:2155)
==216272== by 0x50E3ED9: php_embed_startup (php_embed.c:122)
==216272== by 0x10F8D7: main (in /home/<REDACTED>)
==216272==
==216272== Conditional jump or move depends on uninitialised value(s)
==216272== at 0x503D662: zend_string_equal_val (zend_string.c:447)
==216272== by 0x503C632: zend_string_equal_content (zend_string.h:386)
==216272== by 0x503CDA4: zend_interned_string_ht_lookup (zend_string.c:167)
==216272== by 0x503CF78: zend_new_interned_string_permanent (zend_string.c:218)
==216272== by 0x501C388: zend_register_ini_entries_ex (zend_ini.c:252)
==216272== by 0x4E6C6BF: php_module_startup (main.c:2161)
==216272== by 0x50E3ED9: php_embed_startup (php_embed.c:122)
==216272== by 0x10F8D7: main (in /home/<REDACTED>)
==216272==
==216272== Conditional jump or move depends on uninitialised value(s)
==216272== at 0x503D662: zend_string_equal_val (zend_string.c:447)
==216272== by 0x4F59985: zend_string_equal_content (zend_string.h:386)
==216272== by 0x4F5F5A8: zend_hash_del (zend_hash.c:1549)
==216272== by 0x4F5540C: zend_declare_typed_property (zend_API.c:4473)
==216272== by 0x4AF4739: register_class_DOMException (php_dom_arginfo.h:2559)
==216272== by 0x4AFBC22: zm_startup_dom (php_dom.c:734)
==216272== by 0x4F4D42B: zend_startup_module_ex (zend_API.c:2362)
==216272== by 0x4F4D4A5: zend_startup_module_zval (zend_API.c:2377)
==216272== by 0x4F612DB: zend_hash_apply (zend_hash.c:2080)
==216272== by 0x4F4DCA8: zend_startup_modules (zend_API.c:2500)
==216272== by 0x4E6C821: php_module_startup (main.c:2222)
==216272== by 0x50E3ED9: php_embed_startup (php_embed.c:122)
==216272==
==216272== Thread 4:
==216272== Invalid write of size 4
==216272== at 0x4F59408: zend_gc_set_refcount (zend_types.h:1318)
==216272== by 0x4F5A6FF: _zend_hash_init_int (zend_hash.c:264)
==216272== by 0x4F5A7D9: _zend_hash_init (zend_hash.c:279)
==216272== by 0x503C9B0: zend_init_interned_strings_ht (zend_string.c:80)
==216272== by 0x503D4F2: zend_interned_strings_activate (zend_string.c:353)
==216272== by 0x4E6B68F: php_request_startup (main.c:1749)
==216272== by 0x10F6A7: main::{lambda(void*)#1}::operator()(void*) const (in
/home/<REDACTED>)
==216272== by 0x10F7AA: main::{lambda(void*)#1}::_FUN(void*) (in /home/<REDACTED>)
==216272== by 0x635DAC2: start_thread (pthread_create.c:442)
==216272== by 0x63EEA03: clone (clone.S:100)
==216272== Address 0x2708 is not stack'd, malloc'd or (recently) free'd
==216272==
==216272==
==216272== Process terminating with default action of signal 11 (SIGSEGV)
==216272== Access not within mapped region at address 0x2708
==216272== at 0x4F59408: zend_gc_set_refcount (zend_types.h:1318)
==216272== by 0x4F5A6FF: _zend_hash_init_int (zend_hash.c:264)
==216272== by 0x4F5A7D9: _zend_hash_init (zend_hash.c:279)
==216272== by 0x503C9B0: zend_init_interned_strings_ht (zend_string.c:80)
==216272== by 0x503D4F2: zend_interned_strings_activate (zend_string.c:353)
==216272== by 0x4E6B68F: php_request_startup (main.c:1749)
==216272== by 0x10F6A7: main::{lambda(void*)#1}::operator()(void*) const (in
/home/<REDACTED>)
==216272== by 0x10F7AA: main::{lambda(void*)#1}::_FUN(void*) (in /home/<REDACTED>)
==216272== by 0x635DAC2: start_thread (pthread_create.c:442)
==216272== by 0x63EEA03: clone (clone.S:100)
==216272== If you believe this happened as a result of a stack
==216272== overflow in your program's main thread (unlikely but
==216272== possible), you can try to increase the size of the
==216272== main thread stack using the --main-stacksize= flag.
==216272== The main thread stack size used in this run was 8388608.
==216272==
==216272== HEAP SUMMARY:
==216272== in use at exit: 2,529,241 bytes in 16,370 blocks
==216272== total heap usage: 18,091 allocs, 1,721 frees, 3,204,951 bytes allocated
==216272==
==216272== LEAK SUMMARY:
==216272== definitely lost: 26,016 bytes in 813 blocks
==216272== indirectly lost: 40 bytes in 1 blocks
==216272== possibly lost: 1,973,248 bytes in 13,354 blocks
==216272== still reachable: 529,937 bytes in 2,202 blocks
==216272== suppressed: 0 bytes in 0 blocks
==216272== Rerun with --leak-check=full to see details of leaked memory
==216272==
==216272== Use --track-origins=yes to see where uninitialised values come from
==216272== For lists of detected and suppressed errors, rerun with: -s
==216272== ERROR SUMMARY: 601 errors from 5 contexts (suppressed: 0 from 0)
Segmentation fault
```
But I expected this output instead:
```
Hello world
```
(printed multiple times)
Thank you in advance.
### PHP Version
8.3
### Operating System
Ubuntu 22.04.3 (with WSL)