# Help: call_user_function() core dumps

- Thread: https://externals.io/message/37364
- Original archive: https://news-web.php.net/php.internals/37364
- Started by Dhiru Pandey on 2008-04-30T16:52:13+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #37364 — Dhiru Pandey — 2008-04-30T16:52:13+00:00

URL: https://externals.io/message/37364#37364

I am new to PHP extension writing and embedding. Following the book from
Sara Goleman - Extending and Embedding PHP (Developer's Library)
I wrote the following program based on her example in Chap. 20

===================================================================
#include <stdio.h>
#include <stdlib.h>

#include <sapi/embed/php_embed.h>

int main(int argc, char** argv) {
      PHP_EMBED_START_BLOCK(argc, argv);

    zval* args[2];
    zval funcname;
    zval input_str;
    zval count;
    zval retval;
    char* ans;
      ZVAL_STRING(&funcname, "str_repeat", 0);
    args[0] = &input_str;
    args[1] = &count;
    ZVAL_STRINGL(args[0], "DHIRU ", 6, 0);
    ZVAL_LONG(args[1], 5L);
      call_user_function(EG(function_table), NULL, &funcname, &retval, 2, args TSRMLS_CC);
      ans = Z_STRVAL(retval);
    printf("%s\n", ans);
      PHP_EMBED_END_BLOCK();
      return 0;
}

========================================================================

When I run this program it core dumps with the following message:

/export/home/dhiru/work/php/php-5.2.5/Zend/zend_variables.h(35) : Block 0x08049188 status:
/export/home/dhiru/work/php/php-5.2.5/Zend/zend_variables.c(36) : Actual location (location was relayed)
Invalid pointer: ((thread_id=0x4C45535F) != (expected=0xB78D26B0))
Segmentation fault (core dumped)

When I step thru the debugger it appears that the function str_repeat call is successful
via zend_call_function(), but after the call to the str_repeat function it tries to execute
zend_ptr_stack_clear_multiple(TSRMLS_C), which in turn calls zend_mm_check_ptr() and something
seems to go wrong here :

#ifdef ZTS
    if (ZEND_MM_BAD_THREAD_ID(p)) {
        if (!silent) {
            zend_debug_alloc_output("Invalid pointer: ((thread_id=0x%0.8X) != (expected=0x%0.8X))\n", (long)p->thread_id, 
(long)tsrm_thread_id());
            had_problems = 1;
        } else {
            return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
        }
    }
#endif


Any help will be greatly appreciated

Thanks,
-Dhiru
P.S.: I am using Ubuntu and PHP 5.2.5

---

## #37380 — johannes@php.net — 2008-04-30T18:16:25+00:00

URL: https://externals.io/message/37364#37380 · In reply to: #37364

Hi,


> I am new to PHP extension writing and embedding. Following the book from
> Sara Goleman - Extending and Embedding PHP (Developer's Library)
> I wrote the following program based on her example in Chap. 20
> 
> ===================================================================
> #include <stdio.h>
> #include <stdlib.h>
> 
> #include <sapi/embed/php_embed.h>
> 
> int main(int argc, char** argv) {
>       PHP_EMBED_START_BLOCK(argc, argv);
> 
>     zval* args[2];
>     zval funcname;
>     zval input_str;
>     zval count;
>     zval retval;
>     char* ans;
>       ZVAL_STRING(&funcname, "str_repeat", 0);
>     args[0] = &input_str;
>     args[1] = &count;

You have to use emalloc'ed memory for (basically) everything you give to
the engine and no pointers to local variables else the engine tries to
free these local variables.

johannes
