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;
219 void win32_seh_init()
221 /* install restore stack helper */
223 restore_stack = mono_win32_get_handle_stackoverflow ();
225 old_handler = SetUnhandledExceptionFilter(seh_handler);
228 void win32_seh_cleanup()
230 if (old_handler) SetUnhandledExceptionFilter(old_handler);
233 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
237 fpe_handler = handler;
240 ill_handler = handler;
243 segv_handler = handler;
250 #endif /* PLATFORM_WIN32 */
253 * mono_arch_get_restore_context:
255 * Returns a pointer to a method which restores a previously saved sigcontext.
258 mono_arch_get_restore_context (void)
260 static guint8 *start = NULL;
266 /* restore_contect (MonoContext *ctx) */
267 /* we do not restore X86_EAX, X86_EDX */
269 start = code = mono_global_codeman_reserve (128);
272 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
274 /* get return address, stored in EDX */
275 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (MonoContext, eip), 4);
277 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
279 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
281 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
283 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (MonoContext, esp), 4);
285 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
287 /* jump to the saved IP */
288 x86_jump_reg (code, X86_EDX);
294 * mono_arch_get_call_filter:
296 * Returns a pointer to a method which calls an exception filter. We
297 * also use this function to call finally handlers (we pass NULL as
298 * @exc object in this case).
301 mono_arch_get_call_filter (void)
303 static guint8* start;
304 static int inited = 0;
311 /* call_filter (MonoContext *ctx, unsigned long eip) */
312 start = code = mono_global_codeman_reserve (64);
314 x86_push_reg (code, X86_EBP);
315 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
316 x86_push_reg (code, X86_EBX);
317 x86_push_reg (code, X86_EDI);
318 x86_push_reg (code, X86_ESI);
321 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
323 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
325 x86_push_reg (code, X86_EBP);
328 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebp), 4);
329 /* restore registers used by global register allocation (EBX & ESI) */
330 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (MonoContext, ebx), 4);
331 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (MonoContext, esi), 4);
332 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (MonoContext, edi), 4);
334 /* call the handler */
335 x86_call_reg (code, X86_ECX);
338 x86_pop_reg (code, X86_EBP);
340 /* restore saved regs */
341 x86_pop_reg (code, X86_ESI);
342 x86_pop_reg (code, X86_EDI);
343 x86_pop_reg (code, X86_EBX);
347 g_assert ((code - start) < 64);
352 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
353 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
354 unsigned long eip, unsigned long esp, gboolean rethrow)
356 static void (*restore_context) (MonoContext *);
359 if (!restore_context)
360 restore_context = mono_arch_get_restore_context ();
362 /* Pop argument and return address */
363 ctx.esp = esp + (2 * sizeof (gpointer));
373 if (mono_debugger_throw_exception ((gpointer)(eip - 5), (gpointer)esp, exc)) {
375 * The debugger wants us to stop on the `throw' instruction.
376 * By the time we get here, it already inserted a breakpoint on
377 * eip - 5 (which is the address of the call).
380 ctx.esp = esp + sizeof (gpointer);
381 restore_context (&ctx);
382 g_assert_not_reached ();
385 /* adjust eip so that it point into the call instruction */
388 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
389 MonoException *mono_ex = (MonoException*)exc;
391 mono_ex->stack_trace = NULL;
393 mono_handle_exception (&ctx, exc, (gpointer)eip, FALSE);
394 restore_context (&ctx);
396 g_assert_not_reached ();
400 get_throw_exception (gboolean rethrow)
402 guint8 *start, *code;
404 start = code = mono_global_codeman_reserve (64);
406 x86_push_reg (code, X86_ESP);
407 x86_push_membase (code, X86_ESP, 4); /* IP */
408 x86_push_membase (code, X86_ESP, 12); /* exception */
409 x86_push_reg (code, X86_EBP);
410 x86_push_reg (code, X86_EDI);
411 x86_push_reg (code, X86_ESI);
412 x86_push_reg (code, X86_EBX);
413 x86_push_reg (code, X86_EDX);
414 x86_push_reg (code, X86_ECX);
415 x86_push_reg (code, X86_EAX);
416 x86_call_code (code, throw_exception);
417 /* we should never reach this breakpoint */
418 x86_breakpoint (code);
420 g_assert ((code - start) < 64);
426 * mono_arch_get_throw_exception:
428 * Returns a function pointer which can be used to raise
429 * exceptions. The returned function has the following
430 * signature: void (*func) (MonoException *exc);
431 * For example to raise an arithmetic exception you can use:
433 * x86_push_imm (code, mono_get_exception_arithmetic ());
434 * x86_call_code (code, arch_get_throw_exception ());
438 mono_arch_get_throw_exception (void)
440 static guint8 *start;
441 static int inited = 0;
446 start = get_throw_exception (FALSE);
454 mono_arch_get_rethrow_exception (void)
456 static guint8 *start;
457 static int inited = 0;
462 start = get_throw_exception (TRUE);
470 * mono_arch_get_throw_exception_by_name:
472 * Returns a function pointer which can be used to raise
473 * corlib exceptions. The returned function has the following
474 * signature: void (*func) (gpointer ip, char *exc_name);
475 * For example to raise an arithmetic exception you can use:
477 * x86_push_imm (code, "ArithmeticException");
478 * x86_push_imm (code, <IP>)
479 * x86_jump_code (code, arch_get_throw_exception_by_name ());
483 mono_arch_get_throw_exception_by_name (void)
485 static guint8* start;
486 static int inited = 0;
493 code = start = mono_global_codeman_reserve (32);
495 x86_push_membase (code, X86_ESP, 4); /* exception name */
496 x86_push_imm (code, "System");
497 x86_push_imm (code, mono_defaults.exception_class->image);
498 x86_call_code (code, mono_exception_from_name);
499 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
500 /* save the newly create object (overwrite exception name)*/
501 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
502 x86_jump_code (code, mono_arch_get_throw_exception ());
504 g_assert ((code - start) < 32);
510 * mono_arch_get_throw_corlib_exception:
512 * Returns a function pointer which can be used to raise
513 * corlib exceptions. The returned function has the following
514 * signature: void (*func) (guint32 ex_token, guint32 offset);
515 * Here, offset is the offset which needs to be substracted from the caller IP
516 * to get the IP of the throw. Passing the offset has the advantage that it
517 * needs no relocations in the caller.
520 mono_arch_get_throw_corlib_exception (void)
522 static guint8* start;
523 static int inited = 0;
530 code = start = mono_global_codeman_reserve (64);
532 x86_push_membase (code, X86_ESP, 4); /* token */
533 x86_push_imm (code, mono_defaults.exception_class->image);
534 x86_call_code (code, mono_exception_from_token);
535 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
536 /* Compute caller ip */
537 x86_pop_reg (code, X86_ECX);
539 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
540 x86_pop_reg (code, X86_EDX);
541 x86_alu_reg_reg (code, X86_SUB, X86_ECX, X86_EDX);
542 /* Push exception object */
543 x86_push_reg (code, X86_EAX);
545 x86_push_reg (code, X86_ECX);
546 x86_jump_code (code, mono_arch_get_throw_exception ());
548 g_assert ((code - start) < 64);
553 /* mono_arch_find_jit_info:
555 * This function is used to gather information from @ctx. It return the
556 * MonoJitInfo of the corresponding function, unwinds one stack frame and
557 * stores the resulting context into @new_ctx. It also stores a string
558 * describing the stack location into @trace (if not NULL), and modifies
559 * the @lmf if necessary. @native_offset return the IP offset from the
560 * start of the function or -1 if that info is not available.
563 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
564 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
568 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
570 /* Avoid costly table lookup during stack overflow */
571 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
574 ji = mono_jit_info_table_find (domain, ip);
585 if (!ji->method->wrapper_type)
589 * Some managed methods like pinvoke wrappers might have save_lmf set.
590 * In this case, register save/restore code is not generated by the
591 * JIT, so we have to restore callee saved registers from the lmf.
593 if (ji->method->save_lmf) {
595 * We only need to do this if the exception was raised in managed
596 * code, since otherwise the lmf was already popped of the stack.
598 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
599 new_ctx->esi = (*lmf)->esi;
600 new_ctx->edi = (*lmf)->edi;
601 new_ctx->ebx = (*lmf)->ebx;
606 /* restore caller saved registers */
607 if (ji->used_regs & X86_EBX_MASK) {
608 new_ctx->ebx = *((int *)ctx->ebp + offset);
611 if (ji->used_regs & X86_EDI_MASK) {
612 new_ctx->edi = *((int *)ctx->ebp + offset);
615 if (ji->used_regs & X86_ESI_MASK) {
616 new_ctx->esi = *((int *)ctx->ebp + offset);
620 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
621 /* remove any unused lmf */
622 *lmf = (*lmf)->previous_lmf;
625 /* Pop EBP and the return address */
626 new_ctx->esp = ctx->ebp + (2 * sizeof (gpointer));
627 /* we substract 1, so that the IP points into the call instruction */
628 new_ctx->eip = *((int *)ctx->ebp + 1) - 1;
629 new_ctx->ebp = *((int *)ctx->ebp);
631 /* Pop arguments off the stack */
633 MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
635 guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
636 new_ctx->esp += stack_to_pop;
644 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
649 /* Trampoline lmf frame */
650 memset (res, 0, sizeof (MonoJitInfo));
651 res->method = (*lmf)->method;
654 new_ctx->esi = (*lmf)->esi;
655 new_ctx->edi = (*lmf)->edi;
656 new_ctx->ebx = (*lmf)->ebx;
657 new_ctx->ebp = (*lmf)->ebp;
658 new_ctx->eip = (*lmf)->eip;
659 /* the lmf is always stored on the stack, so the following
660 * expression points to a stack location which can be used as ESP */
661 new_ctx->esp = (unsigned long)&((*lmf)->eip);
663 *lmf = (*lmf)->previous_lmf;
665 return ji ? ji : res;
684 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
686 #ifdef MONO_ARCH_USE_SIGACTION
687 ucontext_t *ctx = (ucontext_t*)sigctx;
689 mctx->eax = UCONTEXT_REG_EAX (ctx);
690 mctx->ebx = UCONTEXT_REG_EBX (ctx);
691 mctx->ecx = UCONTEXT_REG_ECX (ctx);
692 mctx->edx = UCONTEXT_REG_EDX (ctx);
693 mctx->ebp = UCONTEXT_REG_EBP (ctx);
694 mctx->esp = UCONTEXT_REG_ESP (ctx);
695 mctx->esi = UCONTEXT_REG_ESI (ctx);
696 mctx->edi = UCONTEXT_REG_EDI (ctx);
697 mctx->eip = UCONTEXT_REG_EIP (ctx);
699 struct sigcontext *ctx = (struct sigcontext *)sigctx;
701 mctx->eax = ctx->SC_EAX;
702 mctx->ebx = ctx->SC_EBX;
703 mctx->ecx = ctx->SC_ECX;
704 mctx->edx = ctx->SC_EDX;
705 mctx->ebp = ctx->SC_EBP;
706 mctx->esp = ctx->SC_ESP;
707 mctx->esi = ctx->SC_ESI;
708 mctx->edi = ctx->SC_EDI;
709 mctx->eip = ctx->SC_EIP;
714 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
716 #ifdef MONO_ARCH_USE_SIGACTION
717 ucontext_t *ctx = (ucontext_t*)sigctx;
719 UCONTEXT_REG_EAX (ctx) = mctx->eax;
720 UCONTEXT_REG_EBX (ctx) = mctx->ebx;
721 UCONTEXT_REG_ECX (ctx) = mctx->ecx;
722 UCONTEXT_REG_EDX (ctx) = mctx->edx;
723 UCONTEXT_REG_EBP (ctx) = mctx->ebp;
724 UCONTEXT_REG_ESP (ctx) = mctx->esp;
725 UCONTEXT_REG_ESI (ctx) = mctx->esi;
726 UCONTEXT_REG_EDI (ctx) = mctx->edi;
727 UCONTEXT_REG_EIP (ctx) = mctx->eip;
729 struct sigcontext *ctx = (struct sigcontext *)sigctx;
731 ctx->SC_EAX = mctx->eax;
732 ctx->SC_EBX = mctx->ebx;
733 ctx->SC_ECX = mctx->ecx;
734 ctx->SC_EDX = mctx->edx;
735 ctx->SC_EBP = mctx->ebp;
736 ctx->SC_ESP = mctx->esp;
737 ctx->SC_ESI = mctx->esi;
738 ctx->SC_EDI = mctx->edi;
739 ctx->SC_EIP = mctx->eip;
744 mono_arch_ip_from_context (void *sigctx)
746 #ifdef MONO_ARCH_USE_SIGACTION
747 ucontext_t *ctx = (ucontext_t*)sigctx;
748 return (gpointer)UCONTEXT_REG_EIP (ctx);
750 struct sigcontext *ctx = sigctx;
751 return (gpointer)ctx->SC_EIP;
756 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
760 mono_arch_sigctx_to_monoctx (sigctx, &mctx);
762 mono_handle_exception (&mctx, obj, (gpointer)mctx.eip, test_only);
764 mono_arch_monoctx_to_sigctx (&mctx, sigctx);