[runtime] Fix detecting abort at end of abort protected block
[mono.git] / mono / mini / mini-exceptions.c
index 39de4a89bbbc1bc615d82c7222d6d2d0eff38969..18fdc3cb847146614b09b2b087e02772d7f7e01d 100644 (file)
@@ -73,6 +73,7 @@
 #include "seq-points.h"
 #include "llvm-runtime.h"
 #include "mini-llvm.h"
+#include "interp/interp.h"
 
 #ifdef ENABLE_LLVM
 #include "mini-llvm-cpp.h"
@@ -220,7 +221,13 @@ mono_exceptions_init (void)
 #ifdef MONO_ARCH_HAVE_EXCEPTIONS_INIT
        mono_arch_exceptions_init ();
 #endif
-       cbs.mono_walk_stack_with_ctx = mono_runtime_walk_stack_with_ctx;
+#ifdef ENABLE_INTERPRETER
+       if (mono_use_interpreter)
+               cbs.mono_walk_stack_with_ctx = interp_walk_stack_with_ctx;
+       else
+#endif
+               cbs.mono_walk_stack_with_ctx = mono_runtime_walk_stack_with_ctx;
+
        cbs.mono_walk_stack_with_state = mono_walk_stack_with_state;
 
        if (mono_llvm_only)
@@ -1301,7 +1308,7 @@ mini_jit_info_table_find (MonoDomain *domain, char *addr, MonoDomain **out_domai
 }
 
 /* Class lazy loading functions */
-static GENERATE_GET_CLASS_WITH_CACHE (runtime_compat_attr, System.Runtime.CompilerServices, RuntimeCompatibilityAttribute)
+static GENERATE_GET_CLASS_WITH_CACHE (runtime_compat_attr, "System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute")
 
 /*
  * wrap_non_exception_throws:
@@ -1375,7 +1382,7 @@ wrap_non_exception_throws (MonoMethod *m)
 static MonoArray*
 build_native_trace (MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
 /* This puppy only makes sense on mobile, IOW, ARM. */
 #if defined (HAVE_BACKTRACE_SYMBOLS) && defined (TARGET_ARM)
        MonoArray *res;
@@ -1400,9 +1407,9 @@ build_native_trace (MonoError *error)
 }
 
 static void
-setup_stack_trace (MonoException *mono_ex, GSList *dynamic_methods, MonoArray *initial_trace_ips, GList **trace_ips)
+setup_stack_trace (MonoException *mono_ex, GSList *dynamic_methods, GList **trace_ips)
 {
-       if (mono_ex && !initial_trace_ips) {
+       if (mono_ex) {
                *trace_ips = g_list_reverse (*trace_ips);
                MonoError error;
                MonoArray *ips_arr = mono_glist_to_array (*trace_ips, mono_defaults.int_class, &error);
@@ -1456,7 +1463,6 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gi
        static int (*call_filter) (MonoContext *, gpointer) = NULL;
        MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
        MonoLMF *lmf = mono_get_lmf ();
-       MonoArray *initial_trace_ips = NULL;
        GList *trace_ips = NULL;
        GSList *dynamic_methods = NULL;
        MonoException *mono_ex;
@@ -1474,12 +1480,19 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gi
                stack_overflow = TRUE;
 
        mono_ex = (MonoException*)obj;
-       initial_trace_ips = mono_ex->trace_ips;
+       MonoArray *initial_trace_ips = mono_ex->trace_ips;
+       if (initial_trace_ips) {
+               int len = mono_array_length (initial_trace_ips) >> 1;
+
+               for (i = 0; i < (len - 1); i++) {
+                       gpointer ip = mono_array_get (initial_trace_ips, gpointer, i * 2 + 0);
+                       gpointer generic_info = mono_array_get (initial_trace_ips, gpointer, i * 2 + 1);
+                       trace_ips = g_list_prepend (trace_ips, ip);
+                       trace_ips = g_list_prepend (trace_ips, generic_info);
+               }
+       }
 
-       if (mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
-               mono_ex = (MonoException*)obj;
-               initial_trace_ips = mono_ex->trace_ips;
-       } else {
+       if (!mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
                mono_error_assert_ok (&error);
                mono_ex = NULL;
        }
@@ -1523,7 +1536,7 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gi
                }
 
                if (!unwind_res) {
-                       setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
+                       setup_stack_trace (mono_ex, dynamic_methods, &trace_ips);
                        g_slist_free (dynamic_methods);
                        return FALSE;
                }
@@ -1539,15 +1552,10 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gi
                }
 
                if (method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
-                       /* 
-                        * Avoid overwriting the stack trace if the exception is
-                        * rethrown. Also avoid giant stack traces during a stack
-                        * overflow.
-                        */
-                       if (!initial_trace_ips && (frame_count < 1000)) {
+                       // avoid giant stack traces during a stack overflow
+                       if (frame_count < 1000) {
                                trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
-                               trace_ips = g_list_prepend (trace_ips,
-                                                                                       get_generic_info_from_stack_frame (ji, ctx));
+                               trace_ips = g_list_prepend (trace_ips, get_generic_info_from_stack_frame (ji, ctx));
                        }
                }
 
@@ -1588,18 +1596,9 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gi
                                        ex_obj = obj;
 
                                if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
-                                       gboolean is_user_frame = method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD;
 #ifndef DISABLE_PERFCOUNTERS
                                        mono_perfcounters->exceptions_filters++;
 #endif
-                                       /*
-                                       Here's the thing, if this is a filter clause done by a wrapper like runtime invoke, we don't want to
-                                       trim the stackframe since if it returns FALSE we lose information.
-
-                                       FIXME Not 100% sure if it's a good idea even with user clauses.
-                                       */
-                                       if (is_user_frame)
-                                               setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
 
 #ifndef MONO_CROSS_COMPILE
 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
@@ -1636,8 +1635,7 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gi
                                        filter_idx ++;
 
                                        if (filtered) {
-                                               if (!is_user_frame)
-                                                       setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
+                                               setup_stack_trace (mono_ex, dynamic_methods, &trace_ips);
                                                g_slist_free (dynamic_methods);
                                                /* mono_debugger_agent_handle_exception () needs this */
                                                mini_set_abort_threshold (ctx);
@@ -1647,9 +1645,9 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gi
                                }
 
                                MonoError isinst_error;
-                               mono_error_init (&isinst_error);
+                               error_init (&isinst_error);
                                if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (ex_obj, catch_class, &error)) {
-                                       setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
+                                       setup_stack_trace (mono_ex, dynamic_methods, &trace_ips);
                                        g_slist_free (dynamic_methods);
 
                                        if (out_ji)
@@ -1955,7 +1953,7 @@ mono_handle_exception_internal (MonoContext *ctx, MonoObject *obj, gboolean resu
                                        filter_idx ++;
                                }
 
-                               mono_error_init (&error);
+                               error_init (&error);
                                if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
                                     mono_object_isinst_checked (ex_obj, catch_class, &error)) || filtered) {
                                        /*
@@ -1989,7 +1987,7 @@ mono_handle_exception_internal (MonoContext *ctx, MonoObject *obj, gboolean resu
                                                if (is_outside) {
                                                        jit_tls->handler_block_return_address = NULL;
                                                        jit_tls->handler_block = NULL;
-                                                       mono_thread_resume_interruption (); /*We ignore the exception here, it will be raised later*/
+                                                       mono_thread_resume_interruption (TRUE); /*We ignore the exception here, it will be raised later*/
                                                }
                                        }
 
@@ -2432,7 +2430,7 @@ print_stack_frame_to_string (StackFrameInfo *frame, MonoContext *ctx, gpointer d
 
 #ifndef MONO_CROSS_COMPILE
 
-static void print_process_map ()
+static void print_process_map (void)
 {
 #ifdef __linux__
        FILE *fp = fopen ("/proc/self/maps", "r");
@@ -2481,7 +2479,7 @@ mono_handle_native_crash (const char *signal, void *ctx, MONO_SIG_HANDLER_INFO_T
        if (handling_sigsegv && is_sigsegv)
                return;
 
-       if (mini_get_debug_options ()->suspend_on_sigsegv && is_sigsegv) {
+       if (mini_get_debug_options ()->suspend_on_native_crash) {
                mono_runtime_printf_err ("Received %s, suspending...", signal);
 #ifdef HOST_WIN32
                while (1)
@@ -2870,7 +2868,6 @@ mono_thread_state_init_from_sigctx (MonoThreadUnwindState *ctx, void *sigctx)
        MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
        if (!thread) {
                ctx->valid = FALSE;
-               g_error ("Invoked mono_thread_state_init_from_sigctx from non-Mono thread");
                return FALSE;
        }