Segmentation Fault when trying to call zend_new_array.

  111682
August 25, 2020 11:31 ivanzanev477@gmail.com (Ivan Zanev)
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
  111683
August 25, 2020 11:39 internals@lists.php.net ("Levi Morrison via internals")
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.
  111687
August 26, 2020 07:28 ivanzanev477@gmail.com (Ivan Zanev)
Yes, it does.

I recompiled php now with

--configure --enable-embed=static

and wrote a simple script:

#include 
#include 

#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 morrison@datadoghq.com>
wrote:

> 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. >
  111691
August 26, 2020 08:00 alexinbeijing@gmail.com (Alex)
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 ". 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:

> Yes, it does. > > I recompiled php now with > > --configure --enable-embed=static > > and wrote a simple script: > > #include > #include > > #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 morrison@datadoghq.com > > > wrote: > > > 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. > > >
  111693
August 26, 2020 09:18 ivanzanev477@gmail.com (Ivan Zanev)
Thank you!

On Wed, Aug 26, 2020 at 11:00 AM Alex <alexinbeijing@gmail.com> wrote:

> 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 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: > >> Yes, it does. >> >> I recompiled php now with >> >> --configure --enable-embed=static >> >> and wrote a simple script: >> >> #include >> #include >> >> #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: >> >> > 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. >> > >> >