Merge pull request #1668 from alexanderkyte/bug1856
[mono.git] / mono / mini / exceptions-x86.c
old mode 100755 (executable)
new mode 100644 (file)
index 615b3f2..d7d4f72
@@ -31,7 +31,7 @@
 static gpointer signal_exception_trampoline;
 
 gpointer
-mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot) MONO_INTERNAL;
+mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot);
 
 #ifdef TARGET_WIN32
 static void (*restore_stack) (void *);
@@ -59,7 +59,7 @@ LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
        }
 #endif
 
-       mono_handle_native_sigsegv (SIGSEGV, NULL);
+       mono_handle_native_sigsegv (SIGSEGV, NULL, NULL);
 
        return EXCEPTION_CONTINUE_SEARCH;
 }
@@ -96,7 +96,7 @@ mono_win32_get_handle_stackoverflow (void)
        x86_call_code (code, mono_domain_get);
 
        /* get stack overflow exception from domain object */
-       x86_mov_reg_membase (code, X86_EAX, X86_EAX, MONO_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
+       x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
 
        /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj) */
        x86_push_reg (code, X86_EAX);
@@ -109,6 +109,9 @@ mono_win32_get_handle_stackoverflow (void)
        /* return */
        x86_ret (code);
 
+       mono_arch_flush_icache (start, code - start);
+       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
+
        return start;
 }
 
@@ -139,8 +142,7 @@ win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
        guint32 free_stack = 0;
        StackFrameInfo frame;
 
-       /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
-       mono_arch_sigctx_to_monoctx (sctx, &ctx);
+       mono_sigctx_to_monoctx (sctx, &ctx);
        
        /* get our os page size */
        GetSystemInfo(&si);
@@ -172,8 +174,7 @@ win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
                ctx = new_ctx;
        } while (free_stack < 64 * 1024 && frame.ji != (gpointer) -1);
 
-       /* convert into sigcontext to be used in mono_arch_handle_exception */
-       mono_arch_monoctx_to_sigctx (&ctx, sctx);
+       mono_monoctx_to_sigctx (&ctx, sctx);
 
        /* todo: install new stack-guard page */
 
@@ -356,6 +357,9 @@ mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
                g_slist_free (unwind_ops);
        }
 
+       mono_arch_flush_icache (start, code - start);
+       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
+
        return start;
 }
 
@@ -433,6 +437,9 @@ mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
                g_slist_free (unwind_ops);
        }
 
+       mono_arch_flush_icache (start, code - start);
+       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
+
        g_assert ((code - start) < kMaxCodeSize);
        return start;
 }
@@ -465,8 +472,10 @@ mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc,
 
        if (mono_object_isinst (exc, mono_defaults.exception_class)) {
                MonoException *mono_ex = (MonoException*)exc;
-               if (!rethrow)
+               if (!rethrow) {
                        mono_ex->stack_trace = NULL;
+                       mono_ex->trace_ips = NULL;
+               }
        }
 
        /* adjust eip so that it point into the call instruction */
@@ -655,6 +664,9 @@ get_throw_trampoline (const char *name, gboolean rethrow, gboolean llvm, gboolea
                g_slist_free (unwind_ops);
        }
 
+       mono_arch_flush_icache (start, code - start);
+       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
+
        return start;
 }
 
@@ -708,7 +720,7 @@ mono_arch_exceptions_init (void)
  * for SEHs to behave. This requires hotfix http://support.microsoft.com/kb/976038
  * or (eventually) Windows 7 SP1.
  */
-#ifdef HOST_WIN32
+#ifdef TARGET_WIN32
        DWORD flags;
        FARPROC getter;
        FARPROC setter;
@@ -828,14 +840,12 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
 
                        return TRUE;
                }
-               
+
                if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->eip, NULL))) {
+                       frame->ji = ji;
                } else {
-                       if (!((guint32)((*lmf)->previous_lmf) & 1))
-                               /* Top LMF entry */
+                       if (!(*lmf)->method)
                                return FALSE;
-                       g_assert_not_reached ();
-                       /* Trampoline lmf frame */
                        frame->method = (*lmf)->method;
                }
 
@@ -848,28 +858,12 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
                /* Adjust IP */
                new_ctx->eip --;
 
-               frame->ji = ji;
                frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
 
                /* Check if we are in a trampoline LMF frame */
                if ((guint32)((*lmf)->previous_lmf) & 1) {
                        /* lmf->esp is set by the trampoline code */
                        new_ctx->esp = (*lmf)->esp;
-
-                       /* Pop arguments off the stack */
-                       /* FIXME: Handle the delegate case too ((*lmf)->method == NULL) */
-                       /* FIXME: Handle the IMT/vtable case too */
-#if 0
-#ifndef ENABLE_LLVM
-                       if ((*lmf)->method) {
-                               MonoMethod *method = (*lmf)->method;
-                               MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
-
-                               guint32 stack_to_pop = mono_arch_get_argument_info (NULL, mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
-                               new_ctx->esp += stack_to_pop;
-                       }
-#endif
-#endif
                }
                else
                        /* the lmf is always stored on the stack, so the following
@@ -884,18 +878,6 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
        return FALSE;
 }
 
-void
-mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
-{
-       mono_sigctx_to_monoctx (sigctx, mctx);
-}
-
-void
-mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
-{
-       mono_monoctx_to_sigctx (mctx, sigctx);
-}
-
 gpointer
 mono_arch_ip_from_context (void *sigctx)
 {
@@ -975,6 +957,9 @@ mono_x86_get_signal_exception_trampoline (MonoTrampInfo **info, gboolean aot)
                g_slist_free (unwind_ops);
        }
 
+       mono_arch_flush_icache (start, code - start);
+       mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL);
+
        return start;
 }
 
@@ -1012,7 +997,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj)
        MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
 
        /* Pass the ctx parameter in TLS */
-       mono_arch_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
+       mono_sigctx_to_monoctx (ctx, &jit_tls->ex_ctx);
 
        mctx = jit_tls->ex_ctx;
        mono_setup_async_callback (&mctx, handle_signal_exception, obj);
@@ -1024,7 +1009,7 @@ mono_arch_handle_exception (void *sigctx, gpointer obj)
        MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        struct sigcontext *ctx = (struct sigcontext *)sigctx;
 
-       mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
+       mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
 
        mctx = jit_tls->ex_ctx;
        mono_setup_async_callback (&mctx, handle_signal_exception, obj);
@@ -1034,11 +1019,11 @@ mono_arch_handle_exception (void *sigctx, gpointer obj)
 #else
        MonoContext mctx;
 
-       mono_arch_sigctx_to_monoctx (sigctx, &mctx);
+       mono_sigctx_to_monoctx (sigctx, &mctx);
 
        mono_handle_exception (&mctx, obj);
 
-       mono_arch_monoctx_to_sigctx (&mctx, sigctx);
+       mono_monoctx_to_sigctx (&mctx, sigctx);
 
        return TRUE;
 #endif
@@ -1084,7 +1069,7 @@ altstack_handle_and_restore (MonoContext *ctx, gpointer obj, gboolean stack_ovf)
 }
 
 void
-mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
+mono_arch_handle_altstack_exception (void *sigctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, gpointer fault_addr, gboolean stack_ovf)
 {
 #ifdef MONO_ARCH_USE_SIGACTION
        MonoException *exc = NULL;
@@ -1106,7 +1091,7 @@ mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean
        if (stack_ovf)
                exc = mono_domain_get ()->stack_overflow_ex;
        if (!ji)
-               mono_handle_native_sigsegv (SIGSEGV, sigctx);
+               mono_handle_native_sigsegv (SIGSEGV, sigctx, siginfo);
        /* setup a call frame on the real stack so that control is returned there
         * and exception handling can continue.
         * If this was a stack overflow the caller already ensured the stack pages