[Mono]: Make sure interpreter gc_transitions gets reset on managed exceptions. #115052
+48
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Interpreters
do_icall_wrapper
andves_pinvoke_method
uses a frame variable calledgc_transitions
that can put thread in GC SAFE mode before calling native code and switch back on return. When interpreter calls native code it also push a LMF frame with context and unwind label so in case of managed exception it will continue execution at label, but in that case it would also miss to reset thegc_transitions
flag. That could then lead to future errors since other icalls would run under incorrect GC state due to incorrect GC switch.This issue was observed in #114840. In that case a custom icall could call
mono_raise_exception
that would hit this scenario, leaving thegc_transitions
set to TRUE that would then cause issues since next icall would run in wrong GC mode causing undefined behavior.Fix adds a mechanism to abort a GC safe region making sure thread gets back to GC unsafe mode and also make sure the cookie stack gets rebalanced on checked builds. It also makes sure
gc_transitions
gets reset toFALSE
, if its stillTRUE
reaching the exit label.Fix makes sure we correctly restore the interpreter GC state in case of a managed exception in native code.