Segmentation Fault when trying to call zend_new_array.

php.internals

Ivan Zanev

6 years ago
Hello, I'm trying to learn a bit more about HashTable in PHP internally and how memory is allocated when generating arrays with various sizes. I created a simple C script that would call HashTable *ht = zend_new_array(15); However, when I compile the script with gcc, I get segmentation fault; gdb tells that the problem is located within _emalloc_56, specifically: 2535 if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { That's how I compiled the script: gcc -I./Zend -I. -I./TSRM -I./main Zend/*.c TSRM/TSRM.c test.c -ldl -lm -g For the configuration I used: ./configure Could you please guide me in the right direction? Is there something I should do before calling zend_new_array? Sincerely, Ivan

Unnamed Person

6 years ago
On Tue, Aug 25, 2020 at 5:31 AM Ivan Zanev <ivanzanev477@gmail.com> wrote:
> > Hello, > > I'm trying to learn a bit more about HashTable in PHP internally and how > memory is allocated when generating arrays with various sizes. I created a > simple C script that would call > > HashTable *ht = zend_new_array(15); > > However, when I compile the script with gcc, I get segmentation fault; gdb > tells that the problem is located within _emalloc_56, specifically: > > 2535 if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { > > That's how I compiled the script: > gcc -I./Zend -I. -I./TSRM -I./main Zend/*.c TSRM/TSRM.c test.c -ldl -lm -g > > For the configuration I used: > ./configure > > Could you please guide me in the right direction? Is there something I > should do before calling zend_new_array? > > Sincerely, > Ivan
Does `test.c` include a main? If so you probably want to use the embed SAPI. If not then you probably want to build it as an extension.

Ivan Zanev

6 years ago
Yes, it does. I recompiled php now with --configure --enable-embed=static and wrote a simple script: #include <stdlib.h> #include <stdio.h> #include "sapi/embed/php_embed.h" int main(int argc, char *argv[]) { php_embed_init(argc, argv); HashTable *ht = zend_new_array(15); return 0; } Compiled with the following command: gcc -I./Zend -I. -I./TSRM -I./main Zend/*.c TSRM/TSRM.c test-embed.c -ldl -lm -lphp7 -g The segmentation fault problem still occurs, when calling php_embed_init. Not sure if that's a problem of libphp7.so which I installed via apt or it is something related to a missing piece in the test-embed.c script I wrote? On Tue, Aug 25, 2020 at 2:39 PM Levi Morrison <levi.morrison@datadoghq.com> wrote:

Alex Dowad

6 years ago
Dear Ivan, Some suggestions: - Build your own copy of the PHP embed library using ./configure --enable-embed=shared or ./configure --enable-embed=static. Link against that. - If it doesn't work, get some already-working sample code for use of the PHP embed SAPI and modify it rather than starting from scratch. - If you are still struggling, run your test under gdb so you can get a stack trace when it segfaults. It's as simple as "gdb --args <CLI invocation here>". Make sure that both the PHP embed SAPI and your own test code were compiled with debug info enabled. Alex On Wed, Aug 26, 2020 at 9:29 AM Ivan Zanev <ivanzanev477@gmail.com> wrote:

Ivan Zanev

6 years ago
Thank you! On Wed, Aug 26, 2020 at 11:00 AM Alex <alexinbeijing@gmail.com> wrote: