-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix life time of FFI CData and FFI CType with FFI\CDef #12915
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
base: PHP-8.2
Are you sure you want to change the base?
Conversation
0a24b9c
to
3ac36a2
Compare
For phpGH-8556 the return type is stored in the CDef and doesn't outlive the CData. In that case it happened because temporaries were used, but it's also possible to get this with compiled variables when the variable holding the CDef leaves the scope. For phpGH-12910 the exception trace is built after the compiled variables are destroyed, so that means the FFI CDef instance got destroyed but the CData argument still remained. When the trace building code tries to get the class string for the exception, the CData instance tries to access the already-freed CDef. Fix all of this by storing a reference to the CDef object inside of CData and CType. Closes phpGH-8556. Closes phpGH-12910.
3ac36a2
to
d8bed7d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this new FFI\CDef
dependency especially for CData
.
This looks like an attempt to manage, unmanaged C
data.
And FFI anyway provides another hundred ways to short into the lag.
I'll need to think about this. I'll return to the PR a bit later.
Any arguments are welcome.
Thanks for taking a look. I agree that FFI provides many ways to crash PHP, but those usually involve pointers or library calls. And I agree we can't do anything about that as it's the users' responsibility. That is indeed unmanaged data. The cases that this PR tries to solve are different: they are for problems with types being freed. I think we should avoid use-after-frees for types: users will have a hard time figuring out what goes wrong because use-after-frees often lead to random crashes later. I think this approach is simpler and more efficient than remembering the types like I did in my previous attempt. |
I agree, that we should try to solve this, especially for types and may be your approach is good enough, but I remember, FFI already had some mechanism for dallying type destruction. I should find time, to look into the problem more careful. The main thing that I didn't like from the first look, was the |
I didn't completely get the reason for I think this should be fixed in a different way. In #10574 you tried to do this delaying store, by resetting |
For GH-8556 the return type is stored in the CDef and doesn't outlive the CData. In that case it happened because temporaries were used, but it's also possible to get this with compiled variables when the variable holding the CDef leaves the scope.
For GH-12910 the exception trace is built after the compiled variables are destroyed, so that means the FFI CDef instance got destroyed but the CData argument still remained. When the trace building code tries to get the class string for the exception, the CData instance tries to access the already-freed CDef.
Fix all of this by storing a reference to the CDef object inside of CData and CType.
Closes GH-8556.
Closes GH-12910.
This is an alternative for #10574 that fixes the issue more efficiently and more generally.