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>
27 #if defined(__FreeBSD__)
32 static void (*restore_stack) (void *);
34 static MonoW32ExceptionHandler fpe_handler;
35 static MonoW32ExceptionHandler ill_handler;
36 static MonoW32ExceptionHandler segv_handler;
38 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
40 #define W32_SEH_HANDLE_EX(_ex) \
41 if (_ex##_handler) _ex##_handler((int)sctx)
44 * mono_win32_get_handle_stackoverflow (void):
46 * Returns a pointer to a method which restores the current context stack
47 * and calls handle_exceptions, when done restores the original stack.
50 mono_win32_get_handle_stackoverflow (void)
52 static guint8 *start = NULL;
58 /* restore_contect (void *sigctx) */
59 start = code = mono_global_codeman_reserve (128);
61 /* load context into ebx */
62 x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
64 /* move current stack into edi for later restore */
65 x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
67 /* use the new freed stack from sigcontext */
68 x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
70 /* get the current domain */
71 x86_call_code (code, mono_domain_get);
73 /* get stack overflow exception from domain object */
74 x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
76 /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj, FALSE) */
77 x86_push_imm (code, 0);
78 x86_push_reg (code, X86_EAX);
79 x86_push_reg (code, X86_EBX);
80 x86_call_code (code, mono_arch_handle_exception);
82 /* restore the SEH handler stack */
83 x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
91 /* Special hack to workaround the fact that the
92 * when the SEH handler is called the stack is
93 * to small to recover.
95 * Stack walking part of this method is from mono_handle_exception
98 * - walk the stack to free some space (64k)
99 * - set esp to new stack location
100 * - call mono_arch_handle_exception with stack overflow exception
101 * - set esp to SEH handlers stack
105 win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
109 MonoDomain *domain = mono_domain_get ();
110 MonoJitInfo *ji, rji;
111 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
112 MonoLMF *lmf = jit_tls->lmf;
113 MonoContext initial_ctx;
115 guint32 free_stack = 0;
117 /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
118 mono_arch_sigctx_to_monoctx (sctx, &ctx);
120 /* get our os page size */
122 page_size = si.dwPageSize;
124 /* Let's walk the stack to recover
125 * the needed stack space (if possible)
127 memset (&rji, 0, sizeof (rji));
130 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
132 /* try to free 64kb from our stack */
136 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
138 g_warning ("Exception inside function without unwind info");
139 g_assert_not_reached ();
142 if (ji != (gpointer)-1) {
143 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
146 /* todo: we should call abort if ji is -1 */
148 } while (free_stack < 64 * 1024 && ji != (gpointer) -1);
150 /* convert into sigcontext to be used in mono_arch_handle_exception */
151 mono_arch_monoctx_to_sigctx (&ctx, sctx);
153 /* todo: install new stack-guard page */
155 /* use the new stack and call mono_arch_handle_exception () */
156 restore_stack (sctx);
160 * Unhandled Exception Filter
161 * Top-level per-process exception handler.
163 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
165 EXCEPTION_RECORD* er;
167 struct sigcontext* sctx;
170 res = EXCEPTION_CONTINUE_EXECUTION;
172 er = ep->ExceptionRecord;
173 ctx = ep->ContextRecord;
174 sctx = g_malloc(sizeof(struct sigcontext));
176 /* Copy Win32 context to UNIX style context */
177 sctx->eax = ctx->Eax;
178 sctx->ebx = ctx->Ebx;
179 sctx->ecx = ctx->Ecx;
180 sctx->edx = ctx->Edx;
181 sctx->ebp = ctx->Ebp;
182 sctx->esp = ctx->Esp;
183 sctx->esi = ctx->Esi;
184 sctx->edi = ctx->Edi;
185 sctx->eip = ctx->Eip;
187 switch (er->ExceptionCode) {
188 case EXCEPTION_STACK_OVERFLOW:
189 win32_handle_stack_overflow (ep, sctx);
191 case EXCEPTION_ACCESS_VIOLATION:
192 W32_SEH_HANDLE_EX(segv);
194 case EXCEPTION_ILLEGAL_INSTRUCTION:
195 W32_SEH_HANDLE_EX(ill);
197 case EXCEPTION_INT_DIVIDE_BY_ZERO:
198 case EXCEPTION_INT_OVERFLOW:
199 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
200 case EXCEPTION_FLT_OVERFLOW:
201 case EXCEPTION_FLT_UNDERFLOW:
202 case EXCEPTION_FLT_INEXACT_RESULT:
203 W32_SEH_HANDLE_EX(fpe);
209 /* Copy context back */
210 ctx->Eax = sctx->eax;
211 ctx->Ebx = sctx->ebx;
212 ctx->Ecx = sctx->ecx;
213 ctx->Edx = sctx->edx;
214 ctx->Ebp = sctx->ebp;
215 ctx->Esp = sctx->esp;
216 ctx->Esi = sctx->esi;
217 ctx->Edi = sctx->edi;
218 ctx->Eip = sctx->eip;
223 void win32_seh_init()
225 /* install restore stack helper */
227 restore_stack = mono_win32_get_handle_stackoverflow ();
229 old_handler = SetUnhandledExceptionFilter(seh_handler);
232 void win32_seh_cleanup()
234 if (old_handler) SetUnhandledExceptionFilter(old_handler);
237 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
241 fpe_handler = handler;
244 ill_handler = handler;
247 segv_handler = handler;
254 #endif /* PLATFORM_WIN32 */
257 * mono_arch_get_restore_context:
259 * Returns a pointer to a method which restores a previously saved sigcontext.
262 mono_arch_get_restore_context (void)
264 static guint8 *start = NULL;
270 /* restore_contect (MonoContext *ctx) */
271 /* we do not restore X86_EAX, X86_EDX */
273 start = code = mono_global_codeman_reserve (128);
276 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
278 /* get return address, stored in EDX */
279 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
281 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
283 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
285 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
287 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
289 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
291 /* jump to the saved IP */
292 x86_jump_reg (code, X86_EDX);
298 * mono_arch_get_call_filter:
300 * Returns a pointer to a method which calls an exception filter. We
301 * also use this function to call finally handlers (we pass NULL as
302 * @exc object in this case).
305 mono_arch_get_call_filter (void)
307 static guint8* start;
308 static int inited = 0;
315 /* call_filter (MonoContext *ctx, unsigned long eip) */
316 start = code = mono_global_codeman_reserve (64);
318 x86_push_reg (code, X86_EBP);
319 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
320 x86_push_reg (code, X86_EBX);
321 x86_push_reg (code, X86_EDI);
322 x86_push_reg (code, X86_ESI);
325 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
327 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
329 x86_push_reg (code, X86_EBP);
332 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
333 /* restore registers used by global register allocation (EBX & ESI) */
334 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
335 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
336 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
338 /* call the handler */
339 x86_call_reg (code, X86_ECX);
342 x86_pop_reg (code, X86_EBP);
344 /* restore saved regs */
345 x86_pop_reg (code, X86_ESI);
346 x86_pop_reg (code, X86_EDI);
347 x86_pop_reg (code, X86_EBX);
351 g_assert ((code - start) < 64);
356 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
357 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
358 unsigned long eip, unsigned long esp, gboolean rethrow)
360 static void (*restore_context) (MonoContext *);
363 if (!restore_context)
364 restore_context = mono_arch_get_restore_context ();
366 /* Pop argument and return address */
367 ctx.esp = esp + (2 * sizeof (gpointer));
377 if (mono_debugger_throw_exception ((gpointer)(eip - 5), (gpointer)esp, exc)) {
379 * The debugger wants us to stop on the `throw' instruction.
380 * By the time we get here, it already inserted a breakpoint on
381 * eip - 5 (which is the address of the call).
384 ctx.esp = esp + sizeof (gpointer);
385 restore_context (&ctx);
386 g_assert_not_reached ();
389 /* adjust eip so that it point into the call instruction */
392 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
393 MonoException *mono_ex = (MonoException*)exc;
395 mono_ex->stack_trace = NULL;
397 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
398 restore_context (&ctx);
400 g_assert_not_reached ();
404 get_throw_exception (gboolean rethrow)
406 guint8 *start, *code;
408 start = code = mono_global_codeman_reserve (64);
410 x86_push_reg (code, X86_ESP);
411 x86_push_membase (code, X86_ESP, 4); /* IP */
412 x86_push_membase (code, X86_ESP, 12); /* exception */
413 x86_push_reg (code, X86_EBP);
414 x86_push_reg (code, X86_EDI);
415 x86_push_reg (code, X86_ESI);
416 x86_push_reg (code, X86_EBX);
417 x86_push_reg (code, X86_EDX);
418 x86_push_reg (code, X86_ECX);
419 x86_push_reg (code, X86_EAX);
420 x86_call_code (code, throw_exception);
421 /* we should never reach this breakpoint */
422 x86_breakpoint (code);
424 g_assert ((code - start) < 64);
430 * mono_arch_get_throw_exception:
432 * Returns a function pointer which can be used to raise
433 * exceptions. The returned function has the following
434 * signature: void (*func) (MonoException *exc);
435 * For example to raise an arithmetic exception you can use:
437 * x86_push_imm (code, mono_get_exception_arithmetic ());
438 * x86_call_code (code, arch_get_throw_exception ());
442 mono_arch_get_throw_exception (void)
444 static guint8 *start;
445 static int inited = 0;
450 start = get_throw_exception (FALSE);
458 mono_arch_get_rethrow_exception (void)
460 static guint8 *start;
461 static int inited = 0;
466 start = get_throw_exception (TRUE);
474 * mono_arch_get_throw_exception_by_name:
476 * Returns a function pointer which can be used to raise
477 * corlib exceptions. The returned function has the following
478 * signature: void (*func) (gpointer ip, char *exc_name);
479 * For example to raise an arithmetic exception you can use:
481 * x86_push_imm (code, "ArithmeticException");
482 * x86_push_imm (code, <IP>)
483 * x86_jump_code (code, arch_get_throw_exception_by_name ());
487 mono_arch_get_throw_exception_by_name (void)
489 static guint8* start;
490 static int inited = 0;
497 code = start = mono_global_codeman_reserve (32);
499 x86_push_membase (code, X86_ESP, 4); /* exception name */
500 x86_push_imm (code, "System");
501 x86_push_imm (code, mono_defaults.exception_class->image);
502 x86_call_code (code, mono_exception_from_name);
503 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
504 /* save the newly create object (overwrite exception name)*/
505 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
506 x86_jump_code (code, mono_arch_get_throw_exception ());
508 g_assert ((code - start) < 32);
514 * mono_arch_get_throw_corlib_exception:
516 * Returns a function pointer which can be used to raise
517 * corlib exceptions. The returned function has the following
518 * signature: void (*func) (guint32 ex_token, guint32 offset);
519 * Here, offset is the offset which needs to be substracted from the caller IP
520 * to get the IP of the throw. Passing the offset has the advantage that it
521 * needs no relocations in the caller.
524 mono_arch_get_throw_corlib_exception (void)
526 static guint8* start;
527 static int inited = 0;
534 code = start = mono_global_codeman_reserve (64);
536 x86_push_membase (code, X86_ESP, 4); /* token */
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 = (*lmf)->previous_lmf;
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;
651 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
653 memset (res, 0, sizeof (MonoJitInfo));
654 res->method = (*lmf)->method;
657 new_ctx->esi = (*lmf)->esi;
658 new_ctx->edi = (*lmf)->edi;
659 new_ctx->ebx = (*lmf)->ebx;
660 new_ctx->ebp = (*lmf)->ebp;
661 new_ctx->eip = (*lmf)->eip;
662 /* the lmf is always stored on the stack, so the following
663 * expression points to a stack location which can be used as ESP */
664 new_ctx->esp = (unsigned long)&((*lmf)->eip);
666 *lmf = (*lmf)->previous_lmf;
668 return ji ? ji : res;
687 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
689 #ifdef MONO_ARCH_USE_SIGACTION
690 ucontext_t *ctx = (ucontext_t*)sigctx;
692 #if defined(__FreeBSD__)
693 mctx->eax = ctx->uc_mcontext.mc_eax;
694 mctx->ebx = ctx->uc_mcontext.mc_ebx;
695 mctx->ecx = ctx->uc_mcontext.mc_ecx;
696 mctx->edx = ctx->uc_mcontext.mc_edx;
697 mctx->ebp = ctx->uc_mcontext.mc_ebp;
698 mctx->esp = ctx->uc_mcontext.mc_esp;
699 mctx->esi = ctx->uc_mcontext.mc_esi;
700 mctx->edi = ctx->uc_mcontext.mc_edi;
701 mctx->eip = ctx->uc_mcontext.mc_eip;
703 mctx->eax = ctx->uc_mcontext.gregs [REG_EAX];
704 mctx->ebx = ctx->uc_mcontext.gregs [REG_EBX];
705 mctx->ecx = ctx->uc_mcontext.gregs [REG_ECX];
706 mctx->edx = ctx->uc_mcontext.gregs [REG_EDX];
707 mctx->ebp = ctx->uc_mcontext.gregs [REG_EBP];
708 mctx->esp = ctx->uc_mcontext.gregs [REG_ESP];
709 mctx->esi = ctx->uc_mcontext.gregs [REG_ESI];
710 mctx->edi = ctx->uc_mcontext.gregs [REG_EDI];
711 mctx->eip = ctx->uc_mcontext.gregs [REG_EIP];
714 struct sigcontext *ctx = (struct sigcontext *)sigctx;
716 mctx->eax = ctx->SC_EAX;
717 mctx->ebx = ctx->SC_EBX;
718 mctx->ecx = ctx->SC_ECX;
719 mctx->edx = ctx->SC_EDX;
720 mctx->ebp = ctx->SC_EBP;
721 mctx->esp = ctx->SC_ESP;
722 mctx->esi = ctx->SC_ESI;
723 mctx->edi = ctx->SC_EDI;
724 mctx->eip = ctx->SC_EIP;
729 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
731 #ifdef MONO_ARCH_USE_SIGACTION
732 ucontext_t *ctx = (ucontext_t*)sigctx;
734 #if defined(__FreeBSD__)
735 ctx->uc_mcontext.mc_eax = mctx->eax;
736 ctx->uc_mcontext.mc_ebx = mctx->ebx;
737 ctx->uc_mcontext.mc_ecx = mctx->ecx;
738 ctx->uc_mcontext.mc_edx = mctx->edx;
739 ctx->uc_mcontext.mc_ebp = mctx->ebp;
740 ctx->uc_mcontext.mc_esp = mctx->esp;
741 ctx->uc_mcontext.mc_esi = mctx->esi;
742 ctx->uc_mcontext.mc_edi = mctx->edi;
743 ctx->uc_mcontext.mc_eip = mctx->eip;
746 ctx->uc_mcontext.gregs [REG_EAX] = mctx->eax;
747 ctx->uc_mcontext.gregs [REG_EBX] = mctx->ebx;
748 ctx->uc_mcontext.gregs [REG_ECX] = mctx->ecx;
749 ctx->uc_mcontext.gregs [REG_EDX] = mctx->edx;
750 ctx->uc_mcontext.gregs [REG_EBP] = mctx->ebp;
751 ctx->uc_mcontext.gregs [REG_ESP] = mctx->esp;
752 ctx->uc_mcontext.gregs [REG_ESI] = mctx->esi;
753 ctx->uc_mcontext.gregs [REG_EDI] = mctx->edi;
754 ctx->uc_mcontext.gregs [REG_EIP] = mctx->eip;
757 struct sigcontext *ctx = (struct sigcontext *)sigctx;
759 ctx->SC_EAX = mctx->eax;
760 ctx->SC_EBX = mctx->ebx;
761 ctx->SC_ECX = mctx->ecx;
762 ctx->SC_EDX = mctx->edx;
763 ctx->SC_EBP = mctx->ebp;
764 ctx->SC_ESP = mctx->esp;
765 ctx->SC_ESI = mctx->esi;
766 ctx->SC_EDI = mctx->edi;
767 ctx->SC_EIP = mctx->eip;
772 mono_arch_ip_from_context (void *sigctx)
774 #ifdef MONO_ARCH_USE_SIGACTION
775 ucontext_t *ctx = (ucontext_t*)sigctx;
776 #if defined(__FreeBSD__)
777 return (gpointer)ctx->uc_mcontext.mc_eip;
779 return (gpointer)ctx->uc_mcontext.gregs [REG_EIP];
782 struct sigcontext *ctx = sigctx;
783 return (gpointer)ctx->SC_EIP;
788 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
792 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
794 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
796 mono_arch_monoctx_to_sigctx (&mctx, sigctx);