2 * exceptions-x86.c: exception support for x86
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #include <mono/arch/x86/x86-codegen.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/exception.h>
21 #include <mono/metadata/gc-internal.h>
22 #include <mono/metadata/mono-debug.h>
28 static void (*restore_stack) (void *);
30 static MonoW32ExceptionHandler fpe_handler;
31 static MonoW32ExceptionHandler ill_handler;
32 static MonoW32ExceptionHandler segv_handler;
34 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
36 #define W32_SEH_HANDLE_EX(_ex) \
37 if (_ex##_handler) _ex##_handler((int)sctx)
40 * mono_win32_get_handle_stackoverflow (void):
42 * Returns a pointer to a method which restores the current context stack
43 * and calls handle_exceptions, when done restores the original stack.
46 mono_win32_get_handle_stackoverflow (void)
48 static guint8 *start = NULL;
54 /* restore_contect (void *sigctx) */
55 start = code = mono_global_codeman_reserve (128);
57 /* load context into ebx */
58 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
60 /* move current stack into edi for later restore */
61 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
63 /* use the new freed stack from sigcontext */
64 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
66 /* get the current domain */
67 x86_call_code (code, mono_domain_get);
69 /* get stack overflow exception from domain object */
70 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
72 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj, FALSE) */
73 x86_push_imm (code, 0);
74 x86_push_reg (code, X86_EAX);
75 x86_push_reg (code, X86_EBX);
76 x86_call_code (code, mono_arch_handle_exception);
78 /* restore the SEH handler stack */
79 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
87 /* Special hack to workaround the fact that the
88 * when the SEH handler is called the stack is
89 * to small to recover.
91 * Stack walking part of this method is from mono_handle_exception
94 * - walk the stack to free some space (64k)
95 * - set esp to new stack location
96 * - call mono_arch_handle_exception with stack overflow exception
97 * - set esp to SEH handlers stack
101 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
105 MonoDomain *domain = mono_domain_get ();
106 MonoJitInfo *ji, rji;
107 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
108 MonoLMF *lmf = jit_tls->lmf;
109 MonoContext initial_ctx;
111 guint32 free_stack = 0;
113 /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
114 mono_arch_sigctx_to_monoctx (sctx, &ctx);
116 /* get our os page size */
118 page_size = si.dwPageSize;
120 /* Let's walk the stack to recover
121 * the needed stack space (if possible)
123 memset (&rji, 0, sizeof (rji));
126 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
128 /* try to free 64kb from our stack */
132 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
134 g_warning ("Exception inside function without unwind info");
135 g_assert_not_reached ();
138 if (ji != (gpointer)-1) {
139 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
142 /* todo: we should call abort if ji is -1 */
144 } while (free_stack < 64 * 1024 && ji != (gpointer) -1);
146 /* convert into sigcontext to be used in mono_arch_handle_exception */
147 mono_arch_monoctx_to_sigctx (&ctx, sctx);
149 /* todo: install new stack-guard page */
151 /* use the new stack and call mono_arch_handle_exception () */
152 restore_stack (sctx);
156 * Unhandled Exception Filter
157 * Top-level per-process exception handler.
159 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
161 EXCEPTION_RECORD* er;
163 struct sigcontext* sctx;
166 res = EXCEPTION_CONTINUE_EXECUTION;
168 er = ep->ExceptionRecord;
169 ctx = ep->ContextRecord;
170 sctx = g_malloc(sizeof(struct sigcontext));
172 /* Copy Win32 context to UNIX style context */
173 sctx->eax = ctx->Eax;
174 sctx->ebx = ctx->Ebx;
175 sctx->ecx = ctx->Ecx;
176 sctx->edx = ctx->Edx;
177 sctx->ebp = ctx->Ebp;
178 sctx->esp = ctx->Esp;
179 sctx->esi = ctx->Esi;
180 sctx->edi = ctx->Edi;
181 sctx->eip = ctx->Eip;
183 switch (er->ExceptionCode) {
184 case EXCEPTION_STACK_OVERFLOW:
185 win32_handle_stack_overflow (ep, sctx);
187 case EXCEPTION_ACCESS_VIOLATION:
188 W32_SEH_HANDLE_EX(segv);
190 case EXCEPTION_ILLEGAL_INSTRUCTION:
191 W32_SEH_HANDLE_EX(ill);
193 case EXCEPTION_INT_DIVIDE_BY_ZERO:
194 case EXCEPTION_INT_OVERFLOW:
195 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
196 case EXCEPTION_FLT_OVERFLOW:
197 case EXCEPTION_FLT_UNDERFLOW:
198 case EXCEPTION_FLT_INEXACT_RESULT:
199 W32_SEH_HANDLE_EX(fpe);
205 /* Copy context back */
206 ctx->Eax = sctx->eax;
207 ctx->Ebx = sctx->ebx;
208 ctx->Ecx = sctx->ecx;
209 ctx->Edx = sctx->edx;
210 ctx->Ebp = sctx->ebp;
211 ctx->Esp = sctx->esp;
212 ctx->Esi = sctx->esi;
213 ctx->Edi = sctx->edi;
214 ctx->Eip = sctx->eip;
221 void win32_seh_init()
223 /* install restore stack helper */
225 restore_stack = mono_win32_get_handle_stackoverflow ();
227 old_handler = SetUnhandledExceptionFilter(seh_handler);
230 void win32_seh_cleanup()
232 if (old_handler) SetUnhandledExceptionFilter(old_handler);
235 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
239 fpe_handler = handler;
242 ill_handler = handler;
245 segv_handler = handler;
252 #endif /* PLATFORM_WIN32 */
255 * mono_arch_get_restore_context:
257 * Returns a pointer to a method which restores a previously saved sigcontext.
260 mono_arch_get_restore_context (void)
262 static guint8 *start = NULL;
268 /* restore_contect (MonoContext *ctx) */
269 /* we do not restore X86_EAX, X86_EDX */
271 start = code = mono_global_codeman_reserve (128);
274 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
276 /* get return address, stored in EDX */
277 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
279 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
281 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
283 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
285 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
287 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
289 /* jump to the saved IP */
290 x86_jump_reg (code, X86_EDX);
296 * mono_arch_get_call_filter:
298 * Returns a pointer to a method which calls an exception filter. We
299 * also use this function to call finally handlers (we pass NULL as
300 * @exc object in this case).
303 mono_arch_get_call_filter (void)
305 static guint8* start;
306 static int inited = 0;
313 /* call_filter (MonoContext *ctx, unsigned long eip) */
314 start = code = mono_global_codeman_reserve (64);
316 x86_push_reg (code, X86_EBP);
317 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
318 x86_push_reg (code, X86_EBX);
319 x86_push_reg (code, X86_EDI);
320 x86_push_reg (code, X86_ESI);
323 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
325 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
327 x86_push_reg (code, X86_EBP);
330 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
331 /* restore registers used by global register allocation (EBX & ESI) */
332 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
333 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
334 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
336 /* call the handler */
337 x86_call_reg (code, X86_ECX);
340 x86_pop_reg (code, X86_EBP);
342 /* restore saved regs */
343 x86_pop_reg (code, X86_ESI);
344 x86_pop_reg (code, X86_EDI);
345 x86_pop_reg (code, X86_EBX);
349 g_assert ((code - start) < 64);
354 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
355 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
356 unsigned long eip, unsigned long esp, gboolean rethrow)
358 static void (*restore_context) (MonoContext *);
361 if (!restore_context)
362 restore_context = mono_arch_get_restore_context ();
364 /* Pop argument and return address */
365 ctx.esp = esp + (2 * sizeof (gpointer));
375 if (mono_debugger_throw_exception ((gpointer)(eip - 5), (gpointer)esp, exc)) {
377 * The debugger wants us to stop on the `throw' instruction.
378 * By the time we get here, it already inserted a breakpoint on
379 * eip - 5 (which is the address of the call).
382 ctx.esp = esp + sizeof (gpointer);
383 restore_context (&ctx);
384 g_assert_not_reached ();
387 /* adjust eip so that it point into the call instruction */
390 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
391 MonoException *mono_ex = (MonoException*)exc;
393 mono_ex->stack_trace = NULL;
395 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
396 restore_context (&ctx);
398 g_assert_not_reached ();
402 get_throw_exception (gboolean rethrow)
404 guint8 *start, *code;
406 start = code = mono_global_codeman_reserve (64);
408 x86_push_reg (code, X86_ESP);
409 x86_push_membase (code, X86_ESP, 4); /* IP */
410 x86_push_membase (code, X86_ESP, 12); /* exception */
411 x86_push_reg (code, X86_EBP);
412 x86_push_reg (code, X86_EDI);
413 x86_push_reg (code, X86_ESI);
414 x86_push_reg (code, X86_EBX);
415 x86_push_reg (code, X86_EDX);
416 x86_push_reg (code, X86_ECX);
417 x86_push_reg (code, X86_EAX);
418 x86_call_code (code, throw_exception);
419 /* we should never reach this breakpoint */
420 x86_breakpoint (code);
422 g_assert ((code - start) < 64);
428 * mono_arch_get_throw_exception:
430 * Returns a function pointer which can be used to raise
431 * exceptions. The returned function has the following
432 * signature: void (*func) (MonoException *exc);
433 * For example to raise an arithmetic exception you can use:
435 * x86_push_imm (code, mono_get_exception_arithmetic ());
436 * x86_call_code (code, arch_get_throw_exception ());
440 mono_arch_get_throw_exception (void)
442 static guint8 *start;
443 static int inited = 0;
448 start = get_throw_exception (FALSE);
456 mono_arch_get_rethrow_exception (void)
458 static guint8 *start;
459 static int inited = 0;
464 start = get_throw_exception (TRUE);
472 * mono_arch_get_throw_exception_by_name:
474 * Returns a function pointer which can be used to raise
475 * corlib exceptions. The returned function has the following
476 * signature: void (*func) (gpointer ip, char *exc_name);
477 * For example to raise an arithmetic exception you can use:
479 * x86_push_imm (code, "ArithmeticException");
480 * x86_push_imm (code, <IP>)
481 * x86_jump_code (code, arch_get_throw_exception_by_name ());
485 mono_arch_get_throw_exception_by_name (void)
487 static guint8* start;
488 static int inited = 0;
495 code = start = mono_global_codeman_reserve (32);
497 x86_push_membase (code, X86_ESP, 4); /* exception name */
498 x86_push_imm (code, "System");
499 x86_push_imm (code, mono_defaults.exception_class->image);
500 x86_call_code (code, mono_exception_from_name);
501 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
502 /* save the newly create object (overwrite exception name)*/
503 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
504 x86_jump_code (code, mono_arch_get_throw_exception ());
506 g_assert ((code - start) < 32);
512 * mono_arch_get_throw_corlib_exception:
514 * Returns a function pointer which can be used to raise
515 * corlib exceptions. The returned function has the following
516 * signature: void (*func) (guint32 ex_token, guint32 offset);
517 * Here, offset is the offset which needs to be substracted from the caller IP
518 * to get the IP of the throw. Passing the offset has the advantage that it
519 * needs no relocations in the caller.
522 mono_arch_get_throw_corlib_exception (void)
524 static guint8* start;
525 static int inited = 0;
532 code = start = mono_global_codeman_reserve (64);
534 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4); /* token */
535 x86_alu_reg_imm (code, X86_ADD, X86_EAX, MONO_TOKEN_TYPE_DEF);
536 x86_push_reg (code, X86_EAX);
537 x86_push_imm (code, mono_defaults.exception_class->image);
538 x86_call_code (code, mono_exception_from_token);
539 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
540 /* Compute caller ip */
541 x86_pop_reg (code, X86_ECX);
543 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
544 x86_pop_reg (code, X86_EDX);
545 x86_alu_reg_reg (code, X86_SUB, X86_ECX, X86_EDX);
546 /* Push exception object */
547 x86_push_reg (code, X86_EAX);
549 x86_push_reg (code, X86_ECX);
550 x86_jump_code (code, mono_arch_get_throw_exception ());
552 g_assert ((code - start) < 64);
557 /* mono_arch_find_jit_info:
559 * This function is used to gather information from @ctx. It return the
560 * MonoJitInfo of the corresponding function, unwinds one stack frame and
561 * stores the resulting context into @new_ctx. It also stores a string
562 * describing the stack location into @trace (if not NULL), and modifies
563 * the @lmf if necessary. @native_offset return the IP offset from the
564 * start of the function or -1 if that info is not available.
567 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
568 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
572 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
574 /* Avoid costly table lookup during stack overflow */
575 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
578 ji = mono_jit_info_table_find (domain, ip);
589 if (!ji->method->wrapper_type)
593 * Some managed methods like pinvoke wrappers might have save_lmf set.
594 * In this case, register save/restore code is not generated by the
595 * JIT, so we have to restore callee saved registers from the lmf.
597 if (ji->method->save_lmf) {
599 * We only need to do this if the exception was raised in managed
600 * code, since otherwise the lmf was already popped of the stack.
602 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
603 new_ctx->esi = (*lmf)->esi;
604 new_ctx->edi = (*lmf)->edi;
605 new_ctx->ebx = (*lmf)->ebx;
610 /* restore caller saved registers */
611 if (ji->used_regs & X86_EBX_MASK) {
612 new_ctx->ebx = *((int *)ctx->ebp + offset);
615 if (ji->used_regs & X86_EDI_MASK) {
616 new_ctx->edi = *((int *)ctx->ebp + offset);
619 if (ji->used_regs & X86_ESI_MASK) {
620 new_ctx->esi = *((int *)ctx->ebp + offset);
624 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
625 /* remove any unused lmf */
626 *lmf = (gpointer)(((guint32)(*lmf)->previous_lmf) & ~1);
629 /* Pop EBP and the return address */
630 new_ctx->esp = ctx->ebp + (2 * sizeof (gpointer));
631 /* we substract 1, so that the IP points into the call instruction */
632 new_ctx->eip = *((int *)ctx->ebp + 1) - 1;
633 new_ctx->ebp = *((int *)ctx->ebp);
635 /* Pop arguments off the stack */
637 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
639 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
640 new_ctx->esp += stack_to_pop;
648 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
653 /* Trampoline lmf frame */
654 memset (res, 0, sizeof (MonoJitInfo));
655 res->method = (*lmf)->method;
658 new_ctx->esi = (*lmf)->esi;
659 new_ctx->edi = (*lmf)->edi;
660 new_ctx->ebx = (*lmf)->ebx;
661 new_ctx->ebp = (*lmf)->ebp;
662 new_ctx->eip = (*lmf)->eip;
664 /* Check if we are in a trampoline LMF frame */
665 if ((guint32)((*lmf)->previous_lmf) & 1) {
666 /* lmf->esp is set by the trampoline code */
667 new_ctx->esp = (*lmf)->esp;
669 /* Pop arguments off the stack */
671 MonoMethod *method = (*lmf)->method;
672 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (method)->param_count + 1);
674 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (method), mono_method_signature (method)->param_count, arg_info);
675 new_ctx->esp += stack_to_pop;
679 /* the lmf is always stored on the stack, so the following
680 * expression points to a stack location which can be used as ESP */
681 new_ctx->esp = (unsigned long)&((*lmf)->eip);
683 *lmf = (gpointer)(((guint32)(*lmf)->previous_lmf) & ~1);
685 return ji ? ji : res;
704 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
706 #ifdef MONO_ARCH_USE_SIGACTION
707 ucontext_t *ctx = (ucontext_t*)sigctx;
709 mctx->eax = UCONTEXT_REG_EAX (ctx);
710 mctx->ebx = UCONTEXT_REG_EBX (ctx);
711 mctx->ecx = UCONTEXT_REG_ECX (ctx);
712 mctx->edx = UCONTEXT_REG_EDX (ctx);
713 mctx->ebp = UCONTEXT_REG_EBP (ctx);
714 mctx->esp = UCONTEXT_REG_ESP (ctx);
715 mctx->esi = UCONTEXT_REG_ESI (ctx);
716 mctx->edi = UCONTEXT_REG_EDI (ctx);
717 mctx->eip = UCONTEXT_REG_EIP (ctx);
719 struct sigcontext *ctx = (struct sigcontext *)sigctx;
721 mctx->eax = ctx->SC_EAX;
722 mctx->ebx = ctx->SC_EBX;
723 mctx->ecx = ctx->SC_ECX;
724 mctx->edx = ctx->SC_EDX;
725 mctx->ebp = ctx->SC_EBP;
726 mctx->esp = ctx->SC_ESP;
727 mctx->esi = ctx->SC_ESI;
728 mctx->edi = ctx->SC_EDI;
729 mctx->eip = ctx->SC_EIP;
734 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
736 #ifdef MONO_ARCH_USE_SIGACTION
737 ucontext_t *ctx = (ucontext_t*)sigctx;
739 UCONTEXT_REG_EAX (ctx) = mctx->eax;
740 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
741 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
742 UCONTEXT_REG_EDX (ctx) = mctx->edx;
743 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
744 UCONTEXT_REG_ESP (ctx) = mctx->esp;
745 UCONTEXT_REG_ESI (ctx) = mctx->esi;
746 UCONTEXT_REG_EDI (ctx) = mctx->edi;
747 UCONTEXT_REG_EIP (ctx) = mctx->eip;
749 struct sigcontext *ctx = (struct sigcontext *)sigctx;
751 ctx->SC_EAX = mctx->eax;
752 ctx->SC_EBX = mctx->ebx;
753 ctx->SC_ECX = mctx->ecx;
754 ctx->SC_EDX = mctx->edx;
755 ctx->SC_EBP = mctx->ebp;
756 ctx->SC_ESP = mctx->esp;
757 ctx->SC_ESI = mctx->esi;
758 ctx->SC_EDI = mctx->edi;
759 ctx->SC_EIP = mctx->eip;
764 mono_arch_ip_from_context (void *sigctx)
766 #ifdef MONO_ARCH_USE_SIGACTION
767 ucontext_t *ctx = (ucontext_t*)sigctx;
768 return (gpointer)UCONTEXT_REG_EIP (ctx);
770 struct sigcontext *ctx = sigctx;
771 return (gpointer)ctx->SC_EIP;
776 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
780 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
782 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
784 mono_arch_monoctx_to_sigctx (&mctx, sigctx);