-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
[FFI] Segmentation fault when calling callback function #9214
Comments
if testCloseSessionCallback() function blocks (doesn't create new thread) program instead of while(true); this will work |
Yeah, I don't think that FFI callbacks are thread-safe (and they may have issue with fibers as well). I'm not sure if that's worth fixing, though. |
@cmb69 What we could do is offering a possibilty to synchronize on EG(vm_interrupt). I.e. offer a way to wrap a function pointer in a function which stalls the thread until the next time the interrupt handler is invoked, then jumps to the original function. I would not go down the rabbit hole of attempting to break the shared-nothing model (that's the responsibility of extensions like pthreads or parallel). |
I am experiencing the same issue. In my (rather esotheric) case, i'm providing PHP scripting as an optional add-on for my ezinject project. This add-on makes it possible to inject the PHP interpreter into other processes by using the embed SAPI, and replace native functions with PHP code: https://github.com/smx-smx/ezinject/tree/master/samples/php A real world use of this system can be seen here: https://github.com/Simon34545/lginputhook/blob/main/service/inputhook/lginput-hook.php The problem is that, without a locking mechanism, the process can crash in the same way described by the OP, especially if calls can come from arbitrary threads in the target process. (it's kind of a hit or miss) |
I made a preliminary implementation of this approach. It can be found here: https://gist.github.com/smx-smx/608d2f339d1a822df4b1e125c3a33ca7 it seems to work for the test case posted though (vs a crash on vanilla PHP) Thanks |
I think for ZTS support this will need passing _trsmls_cache around to the trampoline.
This part of the patch probably should be just called once, globally. Needs to happen in MINIT. But yes, as a first approach, this seems sensible. |
I didn't want to keep the interrupt handler function around after servicing the call, since the handler is specific for the active callback. Either way, i'll convert it to a PR this evening so we can discuss about it |
You cannot do that, after all, theoretically, another code might install another interrupt handler at the same time and it'd conflict. Hence interrupt handlers must be installed once for all. Yes, you'll have to add a separate flag to check on. |
I posted a PR with the changes so far. |
Description
The following code:
Resulted in this output:
But I expected this output instead:
gdb backtrace:

PHP Version
PHP 8.0.21 (zts) with Zend OPcache v8.0.21
Operating System
Manjaro Linux
The text was updated successfully, but these errors were encountered: