Skip to content

[Mono]: Make sure interpreter gc_transitions gets reset on managed exceptions. #115052

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/mono/mono/mini/interp/interp.c
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
#include <mono/utils/mono-logger-internals.h>
#include <mono/utils/mono-tls-inline.h>
#include <mono/utils/mono-threads.h>
#include <mono/utils/mono-threads-coop.h>
#include <mono/utils/mono-memory-model.h>

#ifdef HAVE_ALLOCA_H
@@ -1739,6 +1740,7 @@ ves_pinvoke_method (
frame.retval = ret_sp;

MonoLMFExt ext;
gpointer gc_safe_cookie = NULL;
gpointer args;

MONO_REQ_GC_UNSAFE_MODE;
@@ -1808,9 +1810,10 @@ ves_pinvoke_method (
INTERP_PUSH_LMF_WITH_CTX (&frame, ext, exit_pinvoke);

if (*gc_transitions) {
MONO_ENTER_GC_SAFE;
MONO_STACKDATA (stack_data);
gc_safe_cookie = mono_threads_enter_gc_safe_region_internal (&stack_data);
entry_func ((gpointer) addr, args);
MONO_EXIT_GC_SAFE;
mono_threads_exit_gc_safe_region_internal (gc_safe_cookie, &stack_data);
*gc_transitions = FALSE;
} else {
entry_func ((gpointer) addr, args);
@@ -1852,7 +1855,13 @@ ves_pinvoke_method (
g_free (margs.fargs);
#endif
goto exit_pinvoke; // prevent unused label warning in some configurations

/* If an exception is thrown from native code, execution will continue here */
exit_pinvoke:
if (*gc_transitions) {
mono_threads_abort_gc_safe_region_internal (gc_safe_cookie);
*gc_transitions = FALSE;
}
return NULL;
}
#ifdef _MSC_VER
@@ -2460,12 +2469,15 @@ static MONO_NO_OPTIMIZATION MONO_NEVER_INLINE gpointer
do_icall_wrapper (InterpFrame *frame, MonoMethodSignature *sig, MintICallSig op, stackval *ret_sp, stackval *sp, gpointer ptr, gboolean save_last_error, gboolean *gc_transitions)
{
MonoLMFExt ext;
gpointer gc_safe_cookie = NULL;

INTERP_PUSH_LMF_WITH_CTX (frame, ext, exit_icall);

if (*gc_transitions) {
MONO_ENTER_GC_SAFE;
MONO_STACKDATA (stack_data);
gc_safe_cookie = mono_threads_enter_gc_safe_region_internal (&stack_data);
do_icall (sig, op, ret_sp, sp, ptr, save_last_error);
MONO_EXIT_GC_SAFE;
mono_threads_exit_gc_safe_region_internal (gc_safe_cookie, &stack_data);
*gc_transitions = FALSE;
} else {
do_icall (sig, op, ret_sp, sp, ptr, save_last_error);
@@ -2474,8 +2486,14 @@ do_icall_wrapper (InterpFrame *frame, MonoMethodSignature *sig, MintICallSig op,
interp_pop_lmf (&ext);

goto exit_icall; // prevent unused label warning in some configurations
/* If an exception is thrown from native code, execution will continue here */

/* If an exception is thrown from native code, execution will continue here */
exit_icall:
if (*gc_transitions) {
mono_threads_abort_gc_safe_region_internal (gc_safe_cookie);
*gc_transitions = FALSE;
}

return NULL;
}
#ifdef _MSC_VER
21 changes: 21 additions & 0 deletions src/mono/mono/utils/mono-threads-coop.c
Original file line number Diff line number Diff line change
@@ -807,3 +807,24 @@ mono_threads_set_runtime_startup_finished (void)
{
mono_threads_is_runtime_startup_finished_hidden_do_not_modify = 1;
}

// If exception gets raised by mono_raise_exception, it will switch to GC unsafe but
// there is no way to rebalance the GC state in case we unwind out of a MONO_ENTER_GC_SAFE/MONO_EXIT_GC_SAFE
// block. This function makes sure thread gets back into GC unsafe mode and that cookie stack gets rebalanced
// under checked builds. Mainly used by interpreters icall and p/invoke wrappers.
void
mono_threads_abort_gc_safe_region_internal (gpointer cookie)
{
#ifdef ENABLE_CHECKED_BUILD_GC
MONO_DEFINE_LAST_ERROR_RESTORE_POINT;
if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
coop_tls_pop (cookie);
MONO_RESTORE_LAST_ERROR_FROM_RESTORE_POINT;
#endif

if (!mono_thread_is_gc_unsafe_mode())
{
MONO_STACKDATA (stackdata);
mono_threads_enter_gc_unsafe_region_unbalanced_with_info (mono_thread_info_current (), &stackdata);
}
}
4 changes: 4 additions & 0 deletions src/mono/mono/utils/mono-threads-coop.h
Original file line number Diff line number Diff line change
@@ -167,4 +167,8 @@ mono_threads_is_runtime_startup_finished (void)
void
mono_threads_set_runtime_startup_finished (void);

MONO_PROFILER_API
void
mono_threads_abort_gc_safe_region_internal (gpointer cookie);

#endif
Loading