Segfault now in shutdown (dtor misdefined? in TSRM.c:180) with and without mutex [Was: [PECL-DEV] pemalloc in PHP_MINIT_FUNCTION segfaults on win (but
not on linux)]
| From: | Holger Blasum | Date: | Wed, 14 Mar 2007 17:43:18 +0000 |
| Subject: | Segfault now in shutdown (dtor misdefined? in TSRM.c:180) with and without mutex [Was: [PECL-DEV] pemalloc in PHP_MINIT_FUNCTION segfaults on win (but not on linux)] |
||
| References: | 1 2 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-4632@lists.php.net to get a copy of this message | ||
Hi An(dre|ton)y, hello list,
thanks for general encouragement and the mutex suggestion. First, the
the reason why it compiled under linux but not under win was that (ouch)
I hadn't noted with php-5.2.0 ZTS mode is enabled by default under windows
but not under linux. Now added --enable-zts-maintainer to the
linux compile and reproduced the problem there. In a first
attempt of fixing I've tried to add globals_ctor/globals_dtor functions
in the way of Golemon's book (chapter 12, although looking at the
many ways other extensions do it I am not yet sure about which set
of macros is best practice for such a simple toy example). Doing this
the code runs but still hickups while shutdown (stack-simple-zts).
I also followed the suggestion to add a MUTEX_T (stack-simple-zts-mutex)
following the way it is done in ext/standard/string.c, but while
caring for concurrency is generally a good idea, it does not
seem to fix the particular bug at hand.
My guess is that I misnamed the destructor (valgrind:
Jump to the invalid address stated on the next line, at 0x4073AF3:
???, by 0x8282BF0: main (php_cli.c:1277), Address 0x4073AF3 is not
stack'd, malloc'd or (recently) free'd) but I just don't see it.
In addition, here is the gdb output:
(gdb) set args -r "PushStack(1);PushStack(2);echo PopStack();echo
PopStack();"
(gdb) run
Starting program: /usr/local/bin/php -r
"PushStack(1);PushStack(2);echo PopStack();echo PopStack();"
Failed to read a valid object file image from memory.
[Thread debugging using libthread_db enabled]
[New Thread -1211577984 (LWP 31118)]
21 /*** remark: this is the output from the stack***/
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1211577984 (LWP 31118)]
0xb7f6eaf3 in ?? ()
(gdb) bt
#0 0xb7f6eaf3 in ?? ()
#1 0x08195792 in tsrm_shutdown () at
/usr/local/php-5.2.0/TSRM/TSRM.c:180
#2 0x08282bf1 in main (argc=3, argv=0xbfc23f54)
at /usr/local/php-5.2.0/sapi/cli/php_cli.c:1277
For ready reference TSRM.c:180 is
for (j=0; j<p->count; j++) {
if (p->storage[j]) {
if (resource_types_table && !resource_types_table[j].done &&
resource_types_table[j].dtor) {
resource_types_table[j].dtor(p->storage[j],&p->storage);<-line 180
}
free(p->storage[j]);
}}
which works fine for a lot of 'j's (although for most
resource_types_table[j].dtor is just 0) but not for mine ...
Code from php_stack.c shared I consider relevant (assuming ZTS
defined), my guess is that sth is wrong with the destructor, but
having barked at the wrong tree yesterday take it with some
caution (again, full code both with and without mutex is at
http://www.blasum.net/holger/journal/20070312/).
int stack_globals_id;
ZEND_BEGIN_MODULE_GLOBALS(stack)
STACK *s;
ZEND_END_MODULE_GLOBALS(stack)
#define stack_globals(v) TSRMG(stack_globals_id, zend_stack_globals *, v)
static void php_stack_globals_ctor (zend_stack_globals
*stack_globals TSRMLS_DC) {
stack_globals(s) = (STACK *)pemalloc(sizeof(STACK),1);
}
static void php_stack_globals_dtor (zend_stack_globals
*stack_globals TSRMLS_DC) {
pefree(stack_globals(s),1);
}
PHP_MINIT_FUNCTION(stack) {
ts_allocate_id(&stack_globals_id, sizeof(zend_stack_globals),
(ts_allocate_ctor)php_stack_globals_ctor,
(ts_allocate_dtor)php_stack_globals_dtor);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(stack) {
return SUCCESS;
}
Thanks again for any help or suggestions!
--
Holger Blasum +49-174-7313590 (cell) GnuPG 1024D/ACDFC3B769DC1ED66B47