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

Segmentation fault when running a PHPUnit test containing mocked object #12373

Closed
DragosMocrii opened this issue Oct 6, 2023 · 12 comments
Closed

Comments

@DragosMocrii
Copy link

Description

Please see this repository on how to reproduce this issue: https://github.com/DragosMocrii/php-bug-destruct-segfault

The segmentation fault occurs when running a PHPUnit test containing a mocked object of a class containing a __destruct method, in which a public method with a self return type is called.

Issue is present with: PHP 8.2.10, PHP 8.1.23.

PHP Version

8.2.10

Operating System

Ubuntu 22.04

@nielsdos
Copy link
Member

nielsdos commented Oct 6, 2023

Infinite loop causing a stack overflow, this part repeats:

    #215 0x55942681d00b in execute_ex /run/media/niels/MoreData/php-8.1/Zend/zend_vm_execute.h:55666
    #216 0x559426601fcd in zend_call_function /run/media/niels/MoreData/php-8.1/Zend/zend_execute_API.c:931
    #217 0x559426602e68 in zend_call_known_function /run/media/niels/MoreData/php-8.1/Zend/zend_execute_API.c:1020
    #218 0x55942689a106 in zend_call_known_instance_method /run/media/niels/MoreData/php-8.1/Zend/zend_API.h:670
    #219 0x55942689a140 in zend_call_known_instance_method_with_0_params /run/media/niels/MoreData/php-8.1/Zend/zend_API.h:676
    #220 0x55942689afbc in zend_objects_destroy_object /run/media/niels/MoreData/php-8.1/Zend/zend_objects.c:170
    #221 0x5594268adbe2 in zend_objects_store_del /run/media/niels/MoreData/php-8.1/Zend/zend_objects_API.c:184
    #222 0x5594266340b6 in rc_dtor_func /run/media/niels/MoreData/php-8.1/Zend/zend_variables.c:57
    #223 0x5594266a7d30 in i_zval_ptr_dtor /run/media/niels/MoreData/php-8.1/Zend/zend_variables.h:44
    #224 0x5594266c3127 in i_free_compiled_variables /run/media/niels/MoreData/php-8.1/Zend/zend_execute.c:379

@nielsdos
Copy link
Member

nielsdos commented Oct 6, 2023

The reason it segfaults is because of infinite recursion, PHP 8.3 and up no longer segfault on this but throw an actual error.
I'm not so certain this is a bug in PHP. I can see __destruct is being called over and over again, and I suspect this is an issue with PHPUnit's mock object's code generator calling the method, although I'm not entirely certain yet because I haven't spend much time on this yet...

@DragosMocrii
Copy link
Author

@nielsdos thank you for looking into this. Do you think this would need to be reported with PHPUnit as well?

@nielsdos
Copy link
Member

nielsdos commented Oct 6, 2023

I'll have a deeper look into this this weekend, so maybe wait until we get results from that. :)

@EliasKotlyar
Copy link

I can confirm that this bug causing a "Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)" can be reproduced on:

PHP 8.2.9 (cli) (built: Aug 16 2023 19:49:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.9, Copyright (c) Zend Technologies
with Zend OPcache v8.2.9, Copyright (c), by Zend Technologies
with Xdebug v3.2.1, Copyright (c) 2002-2023, by Derick Rethans

I have sucesfully updated my system to:
PHP 8.2.15 (cli) (built: Jan 20 2024 14:17:05) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.15, Copyright (c) Zend Technologies
with Zend OPcache v8.2.15, Copyright (c), by Zend Technologies
with Xdebug v3.3.1, Copyright (c) 2002-2023, by Derick Rethans

And this bug was gone.

@matthewjumpsoffbuildings
Copy link

matthewjumpsoffbuildings commented Apr 3, 2024

I am seeing this issue, PHPUnit exiting with code 139, running PHPUnit 10.5.16 using PHP 8.3.4 on Linux and PHP 8.3.0 on macOS

The project is a Laravel 10.48.4 project, which has a ton of __destruct methods all over the place. I am guessing its related?

@davideprevosto
Copy link

I am seeing this issue, PHPUnit exiting with code 139, running PHPUnit 10.5.16 using PHP 8.3.4 on Linux and PHP 8.3.0 on macOS

The project is a Laravel 10.48.4 project, which has a ton of __destruct methods all over the place. I am guessing its related?

@matthewjumpsoffbuildings I am running on the same issue.

8779 segmentation fault ./vendor/bin/pest --parallel --bail --exclude-group=architectural --coverage

I am using the following version of PHP and Xdebug and I am getting that error since few days, on different projects. Did you maybe find any workaround?

PHP 8.2.16 (cli) (built: Feb 16 2024 01:06:54) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.16, Copyright (c) Zend Technologies
    with Xdebug v3.3.1, Copyright (c) 2002-2023, by Derick Rethans
    with Zend OPcache v8.2.16, Copyright (c), by Zend Technologies

@matthewjumpsoffbuildings

Sadly no, I have no idea.

The worst thing is it comes and goes, its very intermittent for me, failing at different steps, or sometimes ill get a few good runs and it will be fine

@nielsdos
Copy link
Member

Does it reproduce on PHP 8.3 for any of you?

@matthewjumpsoffbuildings
PHPUnit 10.5.16 by Sebastian Bergmann and contributors.
Runtime:       PHP 8.3.4
Configuration: /opt/atlassian/pipelines/agent/build/phpunit.xml
Segmentation fault
Script phpunit handling the test event returned with error code 139

I have seen it in 8.1, 8.2, and 8.3

@nielsdos
Copy link
Member

I've debugged this far enough now to be sure this is a phpunit bug. I'll be making a report at that repo soon with my findings. In short: this is an infinite recursion caused by phpunit mocking when it tries to match the return type.
It doesn't repro reliably on PHP 8.3 because of the recursion detection that is introduced in that release.

@nielsdos
Copy link
Member

Opened sebastianbergmann/phpunit#5809, closing this now as invalid as it's not a PHP bug, and I'll monitor the PHPUnit issue.

@nielsdos nielsdos closed this as not planned Won't fix, can't repro, duplicate, stale Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants