2 * exception.c: exception support
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/arch/x86/x86-codegen.h>
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/threads.h>
17 #include <mono/metadata/debug-helpers.h>
24 # define SC_EAX sc_eax
25 # define SC_EBX sc_ebx
26 # define SC_ECX sc_ecx
27 # define SC_EDX sc_edx
28 # define SC_EBP sc_ebp
29 # define SC_EIP sc_eip
30 # define SC_ESP sc_esp
31 # define SC_EDI sc_edi
32 # define SC_ESI sc_esi
46 * arch_get_restore_context:
48 * Returns a pointer to a method which restores a previously saved sigcontext.
51 arch_get_restore_context (void)
53 static guint8 *start = NULL;
59 /* restore_contect (struct sigcontext *ctx) */
60 /* we do not restore X86_EAX, X86_EDX */
62 start = code = g_malloc (1024);
65 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
67 /* get return address, stored in EDX */
68 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EIP), 4);
70 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
72 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
74 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
76 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
78 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
79 /* restore ECX. the exception object is passed here to the catch handler */
80 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ECX), 4);
82 /* jump to the saved IP */
83 x86_jump_reg (code, X86_EDX);
89 * arch_get_call_finally:
91 * Returns a pointer to a method which calls a finally handler.
94 arch_get_call_finally (void)
96 static guint8 start [28];
97 static int inited = 0;
104 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
107 x86_push_reg (code, X86_EBP);
108 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
109 x86_push_reg (code, X86_EBX);
110 x86_push_reg (code, X86_EDI);
111 x86_push_reg (code, X86_ESI);
114 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
116 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
118 x86_push_reg (code, X86_EBP);
120 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
121 /* save the ESP - this is used by endfinally */
122 x86_mov_membase_reg (code, X86_EBP, -16, X86_ESP, 4);
123 /* call the handler */
124 x86_call_reg (code, X86_ECX);
126 x86_pop_reg (code, X86_EBP);
127 /* restore saved regs */
128 x86_pop_reg (code, X86_ESI);
129 x86_pop_reg (code, X86_EDI);
130 x86_pop_reg (code, X86_EBX);
134 g_assert ((code - start) < 28);
139 * return TRUE if the exception is catched. It also sets the
140 * stack_trace String.
143 arch_exc_is_caught (MonoDomain *domain, MonoJitTlsData *jit_tls, gpointer ip,
144 gpointer *bp, gpointer obj)
147 gpointer *end_of_stack;
148 MonoLMF *lmf = jit_tls->lmf;
152 end_of_stack = jit_tls->end_of_stack;
153 g_assert (end_of_stack);
157 ji = mono_jit_info_table_find (domain, ip);
159 if (ji) { /* we are inside managed code */
162 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
164 char *tmp, *tmpsig, *source_location, *tmpaddr;
165 gint32 address, iloffset;
167 if (!((MonoException*)obj)->stack_trace)
168 strace = g_strdup ("");
170 strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
172 address = (char *)ip - (char *)ji->code_start;
174 source_location = mono_debug_source_location_from_address (m, address);
175 iloffset = mono_debug_il_offset_from_address (m, address);
178 tmpaddr = g_strdup_printf ("<0x%05x>", address);
180 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
182 tmpsig = mono_signature_get_desc(m->signature, TRUE);
184 tmp = g_strdup_printf ("%sin %s (at %s) %s.%s:%s (%s)\n", strace, tmpaddr,
185 source_location, m->klass->name_space, m->klass->name,
188 tmp = g_strdup_printf ("%sin %s %s.%s:%s (%s)\n", strace, tmpaddr,
189 m->klass->name_space, m->klass->name, m->name, tmpsig);
190 g_free (source_location);
194 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
198 if (ji->num_clauses) {
200 g_assert (ji->clauses);
202 for (i = 0; i < ji->num_clauses; i++) {
203 MonoJitExceptionInfo *ei = &ji->clauses [i];
205 if (ei->try_start <= ip && ip <= (ei->try_end)) {
207 if (ei->flags == 0 && mono_object_isinst (obj,
208 mono_class_get (m->klass->image, ei->token_or_filter))) {
215 /* continue unwinding */
217 ip = (gpointer)(*((int *)bp + 1) - 5);
218 bp = (gpointer)(*((int *)bp));
220 if (bp >= end_of_stack) {
230 bp = (gpointer)lmf->ebp;
231 ip = (gpointer)lmf->eip;
235 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
239 if (!((MonoException*)obj)->stack_trace)
240 strace = g_strdup ("");
242 strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
244 tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,
245 m->klass->name, m->name);
249 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
253 lmf = lmf->previous_lmf;
255 if (bp >= end_of_stack) {
268 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
269 MonoReflectionMethod **method,
270 gint32 *iloffset, gint32 *native_offset,
271 MonoString **file, gint32 *line, gint32 *column)
273 MonoDomain *domain = mono_domain_get ();
274 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
275 MonoLMF *lmf = jit_tls->lmf;
276 gpointer *sf = (gpointer *)&skip;
277 gpointer ip = sf [-1];
279 gpointer *bp = sf [-2];
280 MonoMethod *m = NULL;
286 if ((ji = mono_jit_info_table_find (domain, ip))) {
288 addr = (char *)ip - (char *)ji->code_start;
289 ip = (gpointer)((char *)bp [1] - 5);
293 g_assert_not_reached ();
297 bp = (gpointer)lmf->ebp;
298 ip = (gpointer)lmf->eip;
300 lmf = lmf->previous_lmf;
303 if ((unsigned)bp >= (unsigned)jit_tls->end_of_stack)
306 } while (skip-- > 0);
310 *method = mono_method_get_object (domain, m);
311 *iloffset = mono_debug_il_offset_from_address (m, addr);
312 *native_offset = addr;
314 if (need_file_info) {
315 *file = mono_string_new (domain, m->klass->image->name);
316 *column = 0; // fixme
324 * arch_handle_exception:
325 * @ctx: saved processor state
329 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
331 MonoDomain *domain = mono_domain_get ();
333 gpointer ip = (gpointer)ctx->SC_EIP;
334 static void (*restore_context) (struct sigcontext *);
335 static void (*call_finally) (struct sigcontext *, unsigned long);
336 void (*cleanup) (MonoObject *exc);
337 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
338 gpointer end_of_stack;
340 g_assert (ctx != NULL);
343 MonoException *ex = mono_get_exception_null_reference ();
344 ex->message = mono_string_new (domain,
345 "Object reference not set to an instance of an object");
346 obj = (MonoObject *)ex;
349 ((MonoException *)obj)->stack_trace = NULL;
351 if (!restore_context)
352 restore_context = arch_get_restore_context ();
355 call_finally = arch_get_call_finally ();
357 end_of_stack = jit_tls->end_of_stack;
358 g_assert (end_of_stack);
360 cleanup = jit_tls->abort_func;
362 if (!arch_exc_is_caught (domain, jit_tls, ip, (gpointer *)ctx->SC_EBP, obj)) {
363 if (mono_break_on_exc) {
364 if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
365 mono_debug_make_symbols ();
368 mono_unhandled_exception (obj);
373 ji = mono_jit_info_table_find (domain, ip);
375 if (ji) { /* we are inside managed code */
376 MonoMethod *m = ji->method;
379 if (ji->num_clauses) {
382 g_assert (ji->clauses);
384 for (i = 0; i < ji->num_clauses; i++) {
385 MonoJitExceptionInfo *ei = &ji->clauses [i];
387 if (ei->try_start <= ip && ip <= (ei->try_end)) {
389 if (ei->flags == 0 && mono_object_isinst (obj,
390 mono_class_get (m->klass->image, ei->token_or_filter))) {
392 ctx->SC_EIP = (unsigned long)ei->handler_start;
393 ctx->SC_ECX = (unsigned long)obj;
394 restore_context (ctx);
395 g_assert_not_reached ();
400 /* no handler found - we need to call all finally handlers */
401 for (i = 0; i < ji->num_clauses; i++) {
402 MonoJitExceptionInfo *ei = &ji->clauses [i];
404 if (ei->try_start <= ip && ip < (ei->try_end) &&
405 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
406 call_finally (ctx, (unsigned long)ei->handler_start);
411 /* continue unwinding */
414 /* restore caller saved registers */
415 if (ji->used_regs & X86_EBX_MASK) {
416 ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
419 if (ji->used_regs & X86_EDI_MASK) {
420 ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
423 if (ji->used_regs & X86_ESI_MASK) {
424 ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
427 ctx->SC_ESP = ctx->SC_EBP;
428 ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 5;
429 ctx->SC_EBP = *((int *)ctx->SC_EBP);
431 ip = (gpointer)ctx->SC_EIP;
433 if (ctx->SC_EBP > (unsigned)end_of_stack) {
436 g_assert_not_reached ();
440 MonoLMF *lmf = jit_tls->lmf;
445 g_assert_not_reached ();
448 jit_tls->lmf = lmf->previous_lmf;
450 ctx->SC_ESI = lmf->esi;
451 ctx->SC_EDI = lmf->edi;
452 ctx->SC_EBX = lmf->ebx;
453 ctx->SC_EBP = lmf->ebp;
454 ctx->SC_EIP = lmf->eip;
455 ctx->SC_ESP = (unsigned long)&lmf->eip;
457 ip = (gpointer)ctx->SC_EIP;
459 if (ctx->SC_EBP >= (unsigned)end_of_stack) {
462 g_assert_not_reached ();
467 g_assert_not_reached ();
471 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
472 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
473 unsigned long eip, unsigned long esp)
475 struct sigcontext ctx;
477 /* adjust eip so that it point to the call instruction */
490 arch_handle_exception (&ctx, exc);
492 g_assert_not_reached ();
496 * arch_get_throw_exception:
498 * Returns a function pointer which can be used to raise
499 * exceptions. The returned function has the following
500 * signature: void (*func) (MonoException *exc);
501 * For example to raise an arithmetic exception you can use:
503 * x86_push_imm (code, mono_get_exception_arithmetic ());
504 * x86_call_code (code, arch_get_throw_exception ());
508 arch_get_throw_exception (void)
510 static guint8 start [24];
511 static int inited = 0;
520 x86_push_reg (code, X86_ESP);
521 x86_push_membase (code, X86_ESP, 4); /* IP */
522 x86_push_membase (code, X86_ESP, 12); /* exception */
523 x86_push_reg (code, X86_EBP);
524 x86_push_reg (code, X86_EDI);
525 x86_push_reg (code, X86_ESI);
526 x86_push_reg (code, X86_EBX);
527 x86_push_reg (code, X86_EDX);
528 x86_push_reg (code, X86_ECX);
529 x86_push_reg (code, X86_EAX);
530 x86_call_code (code, throw_exception);
531 /* we should never reach this breakpoint */
532 x86_breakpoint (code);
534 g_assert ((code - start) < 24);
539 * arch_get_throw_exception_by_name:
541 * Returns a function pointer which can be used to raise
542 * corlib exceptions. The returned function has the following
543 * signature: void (*func) (char *exc_name);
544 * For example to raise an arithmetic exception you can use:
546 * x86_push_imm (code, "ArithmeticException");
547 * x86_call_code (code, arch_get_throw_exception ());
551 arch_get_throw_exception_by_name ()
553 static guint8 start [32];
554 static int inited = 0;
563 /* fixme: we do not save EAX, EDX, ECD - unsure if we need that */
565 x86_push_membase (code, X86_ESP, 4); /* exception name */
566 x86_push_imm (code, "System");
567 x86_push_imm (code, mono_defaults.exception_class->image);
568 x86_call_code (code, mono_exception_from_name);
569 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
570 /* save the newly create object (overwrite exception name)*/
571 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
572 x86_jump_code (code, arch_get_throw_exception ());
574 g_assert ((code - start) < 32);