Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phpunit9+mockery Segfault #149

Open
rlerdorf opened this issue Jul 28, 2021 · 9 comments
Open

phpunit9+mockery Segfault #149

rlerdorf opened this issue Jul 28, 2021 · 9 comments
Labels

Comments

@rlerdorf
Copy link
Contributor

There is a segfault in here somewhere, but I haven't been able to narrow down a simple reproduce case. It only seems to happen when I run an entire test suite. It always crashes in the same spot, but doesn't crash if I just run the particular test file where the crash happens.

The backtrace looks like this:

#0  zend_mm_alloc_small (bin_num=8, heap=0x7f4c0d600040) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_alloc.c:1267
1267			heap->free_slot[bin_num] = p->next_free_slot;

(gdb) bt
#0  zend_mm_alloc_small (bin_num=8, heap=0x7f4c0d600040) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_alloc.c:1267
#1  zend_mm_alloc_heap (size=<optimized out>, heap=0x7f4c0d600040) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_alloc.c:1338
#2  _emalloc (size=<optimized out>) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_alloc.c:2552
#3  0x00000000008ff75b in zend_objects_new (ce=0x7f4aa699fb38) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_objects.c:178
#4  0x000000000087d1d9 in _object_and_properties_init (properties=0x0, class_type=<optimized out>, arg=0x7f4c0d61d250)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_API.c:1429
#5  object_init_ex (arg=arg@entry=0x7f4c0d61d250, class_type=<optimized out>)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_API.c:1452
#6  0x00007f4c0ce6b154 in uopz_vm_new (execute_data=0x7f4c0d61d1f0) at /builddir/build/BUILD/uopz-7.0.0/src/handlers.c:339
#7  0x00000000008db899 in ZEND_USER_OPCODE_SPEC_HANDLER () at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:2984
#8  0x00000000008dc4c5 in execute_ex (ex=0x4) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:54640
#9  0x000000000086d3b0 in zend_call_function (fci=fci@entry=0x7ffd9750ee00, fci_cache=fci_cache@entry=0x7ffd9750ede0)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_execute_API.c:895
#10 0x00000000007667a4 in zif_array_map (execute_data=<optimized out>, return_value=0x7f4c0d61d170)
    at /usr/src/debug/php-src-php-8.0.8/ext/standard/array.c:6023
#11 0x00000000004610d7 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:1865
#12 0x00000000008dc4c5 in execute_ex (ex=0x4) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:54640
#13 0x000000000086d3b0 in zend_call_function (fci=fci@entry=0x7ffd9750f0c0, fci_cache=fci_cache@entry=0x7ffd9750f0a0)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_execute_API.c:895
#14 0x00007f4c0ce67482 in zif_uopz_call_user_func_array (execute_data=0x7f4c0d61bdf0, return_value=0x7f4c0d61bdd0)
    at /builddir/build/BUILD/uopz-7.0.0/src/util.c:286
#15 0x00000000004610d7 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:1865
#16 0x00000000008dc4c5 in execute_ex (ex=0x4) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:54640
#17 0x000000000086d3b0 in zend_call_function (fci=fci@entry=0x7ffd9750f370, fci_cache=fci_cache@entry=0x7ffd9750f350)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_execute_API.c:895
#18 0x00007f4c0ce67482 in zif_uopz_call_user_func_array (execute_data=0x7f4c0d61ac10, return_value=0x7f4c0d61abf0)
    at /builddir/build/BUILD/uopz-7.0.0/src/util.c:286
#19 0x00000000004610d7 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:1865
#20 0x00000000008dc4c5 in execute_ex (ex=0x4) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:54640
#21 0x00000000008e5510 in zend_execute (op_array=0x7f4c0d66a100, return_value=<optimized out>)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:58875
#22 0x000000000087b2ab in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0, file_count=224504512, 
    file_count@entry=3) at /usr/src/debug/php-src-php-8.0.8/Zend/zend.c:1680
#23 0x0000000000814ef4 in php_execute_script (primary_file=primary_file@entry=0x7ffd97511860)
    at /usr/src/debug/php-src-php-8.0.8/main/main.c:2518
#24 0x000000000090c277 in do_cli (argc=11, argv=0x2b8f430) at /usr/src/debug/php-src-php-8.0.8/sapi/cli/php_cli.c:949
#25 0x00000000004754da in main (argc=11, argv=0x2b8f430) at /usr/src/debug/php-src-php-8.0.8/sapi/cli/php_cli.c:1336

the call_user_func_array() call overridden by uopz in frame 14 is this phpunit9+Mockery call https://github.com/mockery/mockery/blob/master/library/Mockery.php#L117
and the array_map() call in frame 10 is https://github.com/mockery/mockery/blob/master/library/Mockery/Generator/DefinedTargetClass.php#L56-L58 where we can see the source of the uopz_vm_new()

How exactly that ends up corrupting the heap allocator there is the question.

I know it is not nearly enough to go on, and I will update when I get closer to it. Filing in case others are seeing it too and might have a simpler reproduce case.

krakjoe added a commit that referenced this issue Jul 28, 2021
  - named param support was apparently missing or broken
  - cufa may corrupt heap if it tries to free fci.params
@krakjoe
Copy link
Owner

krakjoe commented Jul 28, 2021

I done some stabbing, any good ?

@rlerdorf
Copy link
Contributor Author

rlerdorf commented Jul 28, 2021

With HEAD (93b5675) the segfault has moved, but there is still a heap issue. But I think this one is simply that it is getting a null clazz there.

Program terminated with signal 11, Segmentation fault.
#0  zend_mm_alloc_small (bin_num=8, heap=0x7f74e7800040) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_alloc.c:1267
1267			heap->free_slot[bin_num] = p->next_free_slot;

(gdb) bt
#0  zend_mm_alloc_small (bin_num=8, heap=0x7f74e7800040) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_alloc.c:1267
#1  zend_mm_alloc_heap (size=<optimized out>, heap=0x7f74e7800040) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_alloc.c:1338
#2  _emalloc (size=<optimized out>) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_alloc.c:2552
#3  0x0000000000872bae in zend_string_alloc (persistent=false, len=44) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_string.h:141
#4  zend_string_tolower_ex (str=0x7f74cc3bbf50, persistent=persistent@entry=false)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_operators.c:2645
#5  0x00007f74e7069fe7 in uopz_find_mock (clazz=<optimized out>, object=object@entry=0x7ffd068febc8, mock=mock@entry=0x7ffd068febc0)
    at /home/rlerdorf/src/uopz/src/class.c:74
#6  0x00007f74e706b037 in uopz_vm_new (execute_data=0x7f74e781bb90) at /home/rlerdorf/src/uopz/src/handlers.c:295
#7  0x00000000008db899 in ZEND_USER_OPCODE_SPEC_HANDLER () at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:2984
#8  0x00000000008dc4c5 in execute_ex (ex=0x4) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:54640
#9  0x000000000086d3b0 in zend_call_function (fci=fci@entry=0x7ffd068fee20, fci_cache=fci_cache@entry=0x7ffd068fee00)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_execute_API.c:895
#10 0x00007f74e706744c in zif_uopz_call_user_func_array (execute_data=0x7f74e781bb20, return_value=0x7f74e781bb00)
    at /home/rlerdorf/src/uopz/src/util.c:287
#11 0x00000000004610d7 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:1865
#12 0x00000000008dc4c5 in execute_ex (ex=0x4) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:54640
#13 0x000000000086d3b0 in zend_call_function (fci=fci@entry=0x7ffd068ff0d0, fci_cache=fci_cache@entry=0x7ffd068ff0b0)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_execute_API.c:895
#14 0x00007f74e706744c in zif_uopz_call_user_func_array (execute_data=0x7f74e781a4f0, return_value=0x7f74e781a4d0)
    at /home/rlerdorf/src/uopz/src/util.c:287
#15 0x00000000004610d7 in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER () at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:1865
#16 0x00000000008dc4c5 in execute_ex (ex=0x4) at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:54640
#17 0x00000000008e5510 in zend_execute (op_array=0x7f74e786a100, return_value=<optimized out>)
    at /usr/src/debug/php-src-php-8.0.8/Zend/zend_vm_execute.h:58875
#18 0x000000000087b2ab in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0, file_count=-410934368, 
    file_count@entry=3) at /usr/src/debug/php-src-php-8.0.8/Zend/zend.c:1680
#19 0x0000000000814ef4 in php_execute_script (primary_file=primary_file@entry=0x7ffd069015c0)
    at /usr/src/debug/php-src-php-8.0.8/main/main.c:2518
#20 0x000000000090c277 in do_cli (argc=11, argv=0x18fa430) at /usr/src/debug/php-src-php-8.0.8/sapi/cli/php_cli.c:949
#21 0x00000000004754da in main (argc=11, argv=0x18fa430) at /usr/src/debug/php-src-php-8.0.8/sapi/cli/php_cli.c:1336

(gdb) frame 5
#5  0x00007f74e7069fe7 in uopz_find_mock (clazz=<optimized out>, object=object@entry=0x7ffd068febc8, mock=mock@entry=0x7ffd068febc0)
    at /home/rlerdorf/src/uopz/src/class.c:74
74		zend_string *key = zend_string_tolower(clazz);
(gdb) p clazz
$1 = <optimized out>

@krakjoe
Copy link
Owner

krakjoe commented Jul 28, 2021

Well clazz is optimized away in the find_mock call, but the tolower call seems to have a valid address ... and this is the relevant lines:

	if (opline->op1_type == IS_CONST) {
		if (uopz_find_mock(Z_STR_P(EX_CONSTANT(opline->op1)), &obj, &ce) != SUCCESS) {

So, clazz, is a constant string, it must not be null.

I think we're looking at the result of corruption, but some other code is doing the actual corruption.

@krakjoe
Copy link
Owner

krakjoe commented Jul 28, 2021

Does valgrind have anything useful to say maybe ?

@rlerdorf
Copy link
Contributor Author

Yeah, I am going to give it a shot. But running this whole thing without Valgrind takes a good 20 minutes. I'll probably have to leave it overnight running under Valgrind's memcheck.

@krakjoe
Copy link
Owner

krakjoe commented Jul 29, 2021

We've made many changes, any change here ?

@rlerdorf
Copy link
Contributor Author

No, still corruption somewhere. I am still chasing it.

Program terminated with signal 11, Segmentation fault.
#0  0x0000000000851bf4 in zend_mm_free_large (pages_count=<optimized out>, page_num=<optimized out>, chunk=<optimized out>, heap=<optimized out>) at /usr/src/debug/php-src-php-8.0.9/Zend/zend_alloc.c:1129
1129		zend_mm_free_pages(heap, chunk, page_num, pages_count);

(gdb) bt
#0  0x0000000000851bf4 in zend_mm_free_large (pages_count=<optimized out>, page_num=<optimized out>, chunk=<optimized out>, heap=<optimized out>) at /usr/src/debug/php-src-php-8.0.9/Zend/zend_alloc.c:1129
#1  zend_mm_free_heap (ptr=<optimized out>, heap=<optimized out>) at /usr/src/debug/php-src-php-8.0.9/Zend/zend_alloc.c:1387
#2  _efree (ptr=<optimized out>) at /usr/src/debug/php-src-php-8.0.9/Zend/zend_alloc.c:2563
#3  0x00007ffd068feb60 in ?? ()
#4  0x0000000000872bae in zend_str_tolower_impl (length=140139619401620, str=<optimized out>, dest=<optimized out>) at /usr/src/debug/php-src-php-8.0.9/Zend/zend_operators.c:2588
#5  zend_string_tolower_ex (str=<optimized out>, persistent=<optimized out>) at /usr/src/debug/php-src-php-8.0.9/Zend/zend_operators.c:2665
#6  0x00003e3f00000308 in ?? ()
#7  0x0000000000000119 in ?? ()
#8  0x0000000000000000 in ?? ()
(gdb) zbacktrace
[0x7f74e781bb90] PHPUnit\Framework\MockObject\Builder\InvocationMocker->method("isMobile") ...
[0x7f74e781bb20] call_user_func_array(array(2)[0x7f74e781bb70], array(1)[0x7f74e781bb80]) [internal function]

@rlerdorf
Copy link
Contributor Author

This is a group of about 74k unit tests in a massive code base. There are other groups, but this is the largest and was the one that segfaulted if I ran the whole group via pake. I've been going through and running them individually and fixing mostly signature compatibility errors in them migrating from PHP 7.3 to PHP 8.0. Now when I run the whole group again the segfault is gone. So either the corruption was caused by these signature errors, or more likely, the corruption is hitting somewhere less critical now.

@cmb69
Copy link
Collaborator

cmb69 commented Aug 3, 2024

Is that still an issue?

@cmb69 cmb69 added the question label Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants