Mon Jan 19 17:44:50 CET 2004 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / exceptions-x86.c
index 98aa0cbc2b141ebedefaeee7651c6b49b1ac069e..4a35c4b75ca3cfc8af533e4f37c1d087171431a2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * exception.c: exception support
+ * exceptions-x86.c: exception support for x86
  *
  * Authors:
  *   Dietmar Maurer (dietmar@ximian.com)
 #include "mini.h"
 #include "mini-x86.h"
 
+#ifdef PLATFORM_WIN32
+
+#include <windows.h>
+
+/* use SIG* defines if possible */
+#ifdef HAVE_SIGNAL_H
+#include <signal.h>
+#endif
+
+/* sigcontext surrogate */
+struct sigcontext {
+       unsigned int eax;
+       unsigned int ebx;
+       unsigned int ecx;
+       unsigned int edx;
+       unsigned int ebp;
+       unsigned int esp;
+       unsigned int esi;
+       unsigned int edi;
+       unsigned int eip;
+};
+
+
+typedef void (* MonoW32ExceptionHandler) (int);
+void win32_seh_init(void);
+void win32_seh_cleanup(void);
+void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
+
+#ifndef SIGFPE
+#define SIGFPE 4
+#endif
+
+#ifndef SIGILL
+#define SIGILL 8
+#endif
+
+#ifndef        SIGSEGV
+#define        SIGSEGV 11
+#endif
+
+LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
+
+static MonoW32ExceptionHandler fpe_handler;
+static MonoW32ExceptionHandler ill_handler;
+static MonoW32ExceptionHandler segv_handler;
+
+static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
+
+#define W32_SEH_HANDLE_EX(_ex) \
+       if (_ex##_handler) _ex##_handler((int)sctx)
+
+/*
+ * Unhandled Exception Filter
+ * Top-level per-process exception handler.
+ */
+LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
+{
+       EXCEPTION_RECORD* er;
+       CONTEXT* ctx;
+       struct sigcontext* sctx;
+       LONG res;
+
+       res = EXCEPTION_CONTINUE_EXECUTION;
+
+       er = ep->ExceptionRecord;
+       ctx = ep->ContextRecord;
+       sctx = g_malloc(sizeof(struct sigcontext));
+
+       /* Copy Win32 context to UNIX style context */
+       sctx->eax = ctx->Eax;
+       sctx->ebx = ctx->Ebx;
+       sctx->ecx = ctx->Ecx;
+       sctx->edx = ctx->Edx;
+       sctx->ebp = ctx->Ebp;
+       sctx->esp = ctx->Esp;
+       sctx->esi = ctx->Esi;
+       sctx->edi = ctx->Edi;
+       sctx->eip = ctx->Eip;
+
+       switch (er->ExceptionCode) {
+       case EXCEPTION_ACCESS_VIOLATION:
+               W32_SEH_HANDLE_EX(segv);
+               break;
+       case EXCEPTION_ILLEGAL_INSTRUCTION:
+               W32_SEH_HANDLE_EX(ill);
+               break;
+       case EXCEPTION_INT_DIVIDE_BY_ZERO:
+       case EXCEPTION_INT_OVERFLOW:
+       case EXCEPTION_FLT_DIVIDE_BY_ZERO:
+       case EXCEPTION_FLT_OVERFLOW:
+       case EXCEPTION_FLT_UNDERFLOW:
+       case EXCEPTION_FLT_INEXACT_RESULT:
+               W32_SEH_HANDLE_EX(fpe);
+               break;
+       default:
+               break;
+       }
+
+       /* Copy context back */
+       ctx->Eax = sctx->eax;
+       ctx->Ebx = sctx->ebx;
+       ctx->Ecx = sctx->ecx;
+       ctx->Edx = sctx->edx;
+       ctx->Ebp = sctx->ebp;
+       ctx->Esp = sctx->esp;
+       ctx->Esi = sctx->esi;
+       ctx->Edi = sctx->edi;
+       ctx->Eip = sctx->eip;
+
+       return res;
+}
+
+void win32_seh_init()
+{
+       old_handler = SetUnhandledExceptionFilter(seh_handler);
+}
+
+void win32_seh_cleanup()
+{
+       if (old_handler) SetUnhandledExceptionFilter(old_handler);
+}
+
+void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
+{
+       switch (type) {
+       case SIGFPE:
+               fpe_handler = handler;
+               break;
+       case SIGILL:
+               ill_handler = handler;
+               break;
+       case SIGSEGV:
+               segv_handler = handler;
+               break;
+       default:
+               break;
+       }
+}
+
+#endif /* PLATFORM_WIN32 */
+
 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
 # define SC_EAX sc_eax
 # define SC_EBX sc_ebx
 # define SC_ESI esi
 #endif
 
+gboolean  mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
+
 typedef struct sigcontext MonoContext;
 
 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->SC_EIP = (long)ip; } while (0); 
@@ -81,21 +224,21 @@ get_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum)
 {
        switch (dwarf_regnum) {
        case X86_EAX:
-               return ctx->eax;
+               return ctx->SC_EAX;
        case X86_EBX:
-               return ctx->ebx;
+               return ctx->SC_EBX;
        case X86_ECX:
-               return ctx->ecx;
+               return ctx->SC_ECX;
        case X86_EDX:
-               return ctx->edx;
+               return ctx->SC_EDX;
        case X86_ESI:
-               return ctx->esi;
+               return ctx->SC_ESI;
        case X86_EDI:
-               return ctx->edi;
+               return ctx->SC_EDI;
        case X86_EBP:
-               return ctx->ebp;
+               return ctx->SC_EBP;
        case X86_ESP:
-               return ctx->esp;
+               return ctx->SC_ESP;
        default:
                g_assert_not_reached ();
        }
@@ -108,31 +251,31 @@ set_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum, long value)
 {
        switch (dwarf_regnum) {
        case X86_EAX:
-               ctx->eax = value;
+               ctx->SC_EAX = value;
                break;
        case X86_EBX:
-               ctx->ebx = value;
+               ctx->SC_EBX = value;
                break;
        case X86_ECX:
-               ctx->ecx = value;
+               ctx->SC_ECX = value;
                break;
        case X86_EDX:
-               ctx->edx = value;
+               ctx->SC_EDX = value;
                break;
        case X86_ESI:
-               ctx->esi = value;
+               ctx->SC_ESI = value;
                break;
        case X86_EDI:
-               ctx->edi = value;
+               ctx->SC_EDI = value;
                break;
        case X86_EBP:
-               ctx->ebp = value;
+               ctx->SC_EBP = value;
                break;
        case X86_ESP:
-               ctx->esp = value;
+               ctx->SC_ESP = value;
                break;
        case 8:
-               ctx->eip = value;
+               ctx->SC_EIP = value;
                break;
        default:
                g_assert_not_reached ();
@@ -200,16 +343,14 @@ mono_arch_has_unwind_info (gconstpointer addr)
        /* offset 10 is just a guess, but it works for all methods tested */
        if ((res = frame_state_for ((char *)addr + 10, &state_in))) {
 
-               if (res->saved [X86_EBX] != 1 ||
-                   res->saved [X86_EDI] != 1 ||
-                   res->saved [X86_EBP] != 1 ||
-                   res->saved [X86_ESI] != 1) {
-                       return FALSE;
-               }
-               return TRUE;
-       } else {
-               return FALSE;
+               if (res->saved [X86_EBX] == 1 &&
+                   res->saved [X86_EDI] == 1 &&
+                   res->saved [X86_EBP] == 1 &&
+                   res->saved [X86_ESI] == 1)
+                       return TRUE;
        }
+
+       return FALSE;
 }
 
 struct stack_frame
@@ -278,7 +419,7 @@ x86_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, struct sig
                                        set_sigcontext_reg (new_ctx, i, val);
                                }
                        }
-                       new_ctx->esp = (long)cfa;
+                       new_ctx->SC_ESP = (long)cfa;
 
                        if (res->saved [X86_EBX] == 1 &&
                            res->saved [X86_EDI] == 1 &&
@@ -378,7 +519,7 @@ arch_get_call_filter (void)
                return start;
 
        inited = 1;
-       /* call_filter (struct sigcontext *ctx, unsigned long eip, gpointer exc) */
+       /* call_filter (struct sigcontext *ctx, unsigned long eip) */
        code = start;
 
        x86_push_reg (code, X86_EBP);
@@ -393,21 +534,20 @@ arch_get_call_filter (void)
        x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
        /* save EBP */
        x86_push_reg (code, X86_EBP);
-       /* push exc */
-       x86_push_membase (code, X86_EBP, 16);
+
        /* set new EBP */
        x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
        /* restore registers used by global register allocation (EBX & ESI) */
        x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
        x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
        x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
-       /* save the ESP - this is used by endfinally */
-       x86_mov_membase_reg (code, X86_EBP, mono_exc_esp_offset, X86_ESP, 4);
+
        /* call the handler */
        x86_call_reg (code, X86_ECX);
-       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
+
        /* restore EBP */
        x86_pop_reg (code, X86_EBP);
+
        /* restore saved regs */
        x86_pop_reg (code, X86_ESI);
        x86_pop_reg (code, X86_EDI);
@@ -433,7 +573,8 @@ throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsign
        /* adjust eip so that it point into the call instruction */
        eip -= 1;
 
-       ctx.SC_ESP = esp;
+       /* Pop argument and return address */
+       ctx.SC_ESP = esp + (2 * sizeof (gpointer));
        ctx.SC_EIP = eip;
        ctx.SC_EBP = ebp;
        ctx.SC_EDI = edi;
@@ -585,11 +726,6 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
 
                *new_ctx = *ctx;
 
-               if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
-                       /* remove any unused lmf */
-                       *lmf = (*lmf)->previous_lmf;
-               }
-
                address = (char *)ip - (char *)ji->code_start;
 
                if (native_offset)
@@ -600,10 +736,8 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                                *managed = TRUE;
 
                if (trace) {
-                       mono_debug_update ();
-
-                       source_location = mono_debug_source_location_from_address (ji->method, address, NULL);
-                       iloffset = mono_debug_il_offset_from_address (ji->method, address);
+                       source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
+                       iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
 
                        if (iloffset < 0)
                                tmpaddr = g_strdup_printf ("<0x%05x>", address);
@@ -621,22 +755,46 @@ mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInf
                        g_free (source_location);
                        g_free (tmpaddr);
                }
-                               
-               offset = -1;
-               /* restore caller saved registers */
-               if (ji->used_regs & X86_EBX_MASK) {
-                       new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
-                       offset--;
+
+               /*
+                * Some managed methods like pinvoke wrappers might have save_lmf set.
+                * In this case, register save/restore code is not generated by the 
+                * JIT, so we have to restore callee saved registers from the lmf.
+                */
+               if (ji->method->save_lmf) {
+                       /* 
+                        * We only need to do this if the exception was raised in managed
+                        * code, since otherwise the lmf was already popped of the stack.
+                        */
+                       if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
+                               new_ctx->SC_ESI = (*lmf)->esi;
+                               new_ctx->SC_EDI = (*lmf)->edi;
+                               new_ctx->SC_EBX = (*lmf)->ebx;
+                       }
                }
-               if (ji->used_regs & X86_EDI_MASK) {
-                       new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
-                       offset--;
+               else {
+                       offset = -1;
+                       /* restore caller saved registers */
+                       if (ji->used_regs & X86_EBX_MASK) {
+                               new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
+                               offset--;
+                       }
+                       if (ji->used_regs & X86_EDI_MASK) {
+                               new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
+                               offset--;
+                       }
+                       if (ji->used_regs & X86_ESI_MASK) {
+                               new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
+                       }
                }
-               if (ji->used_regs & X86_ESI_MASK) {
-                       new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
+
+               if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
+                       /* remove any unused lmf */
+                       *lmf = (*lmf)->previous_lmf;
                }
 
-               new_ctx->SC_ESP = ctx->SC_EBP;
+               /* Pop EBP and the return address */
+               new_ctx->SC_ESP = ctx->SC_EBP + (2 * sizeof (gpointer));
                /* we substract 1, so that the IP points into the call instruction */
                new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
                new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
@@ -717,12 +875,12 @@ ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info
                sf->method = mono_method_get_object (domain, ji->method, NULL);
                sf->native_offset = (char *)ip - (char *)ji->code_start;
 
-               sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset);
+               sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
 
                if (need_file_info) {
                        gchar *filename;
                        
-                       filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line);
+                       filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
 
                        sf->filename = filename? mono_string_new (domain, filename): NULL;
                        sf->column = 0;
@@ -758,7 +916,7 @@ mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
                if (ji == (gpointer)-1)
                        return;
 
-               il_offset = mono_debug_il_offset_from_address (ji->method, native_offset);
+               il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
 
                if (func (ji->method, native_offset, il_offset, managed, user_data))
                        return;
@@ -792,7 +950,10 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
                if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
                        return FALSE;
 
-               if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE)
+               /* skip all wrappers ??*/
+               if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
+                   ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
+                   ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
                        continue;
 
                skip--;
@@ -800,12 +961,12 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
        } while (skip >= 0);
 
        *method = mono_method_get_object (domain, ji->method, NULL);
-       *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset);
+       *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
 
        if (need_file_info) {
                gchar *filename;
 
-               filename = mono_debug_source_location_from_address (ji->method, *native_offset, line);
+               filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
 
                *file = filename? mono_string_new (domain, filename): NULL;
                *column = 0;
@@ -829,10 +990,11 @@ mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
 {
        MonoDomain *domain = mono_domain_get ();
        MonoJitInfo *ji, rji;
-       static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
+       static int (*call_filter) (MonoContext *, gpointer) = NULL;
        MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
        MonoLMF *lmf = jit_tls->lmf;            
        GList *trace_ips = NULL;
+       MonoException *mono_ex;
 
        g_assert (ctx != NULL);
        if (!obj) {
@@ -842,7 +1004,12 @@ mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
                obj = (MonoObject *)ex;
        } 
 
-       g_assert (mono_object_isinst (obj, mono_defaults.exception_class));
+       if (mono_object_isinst (obj, mono_defaults.exception_class)) {
+               mono_ex = (MonoException*)obj;
+               mono_ex->stack_trace = NULL;
+       } else {
+               mono_ex = NULL;
+       }
 
        if (!call_filter)
                call_filter = arch_get_call_filter ();
@@ -852,13 +1019,11 @@ mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
 
        if (!test_only) {
                MonoContext ctx_cp = *ctx;
-               if (mono_jit_trace_calls)
+               if (mono_jit_trace_calls != NULL)
                        g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
                if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
-                       if (mono_break_on_exc) {
-                               mono_debug_update ();
+                       if (mono_break_on_exc)
                                G_BREAKPOINT ();
-                       }
                        mono_unhandled_exception (obj);
                }
        }
@@ -876,20 +1041,20 @@ mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
 
                if (ji != (gpointer)-1) {
                        
-                       if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE) {
+                       if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
                                char *tmp, *strace;
 
                                trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
 
-                               if (!((MonoException*)obj)->stack_trace)
+                               if (!mono_ex->stack_trace)
                                        strace = g_strdup ("");
                                else
-                                       strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
+                                       strace = mono_string_to_utf8 (mono_ex->stack_trace);
                        
                                tmp = g_strdup_printf ("%s%s\n", strace, trace);
                                g_free (strace);
 
-                               ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
+                               mono_ex->stack_trace = mono_string_new (domain, tmp);
 
                                g_free (tmp);
                        }
@@ -905,28 +1070,37 @@ mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
                                        if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
                                            MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) { 
                                                /* catch block */
+
+                                               if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
+                                                       /* store the exception object int cfg->excvar */
+                                                       g_assert (ji->exvar_offset);
+                                                       *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
+                                               }
+
                                                if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
                                                     mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
                                                    ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
-                                                     call_filter (ctx, ei->data.filter, obj)))) {
+                                                     call_filter (ctx, ei->data.filter)))) {
                                                        if (test_only) {
-                                                               ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
+                                                               if (mono_ex)
+                                                                       mono_ex->trace_ips = glist_to_array (trace_ips);
                                                                g_list_free (trace_ips);
+                                                               g_free (trace);
                                                                return TRUE;
                                                        }
-                                                       if (mono_jit_trace_calls)
+                                                       if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
                                                                g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
                                                        MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
-                                                       *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
                                                        jit_tls->lmf = lmf;
+                                                       g_free (trace);
                                                        return 0;
                                                }
                                                if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
                                                    MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
                                                    (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
-                                                       if (mono_jit_trace_calls)
+                                                       if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
                                                                g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
-                                                       call_filter (ctx, ei->handler_start, NULL);
+                                                       call_filter (ctx, ei->handler_start);
                                                }
                                                
                                        }
@@ -944,7 +1118,8 @@ mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
                                jit_tls->abort_func (obj);
                                g_assert_not_reached ();
                        } else {
-                               ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
+                               if (mono_ex)
+                                       mono_ex->trace_ips = glist_to_array (trace_ips);
                                g_list_free (trace_ips);
                                return FALSE;
                        }