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 /* call the handler */
122 x86_call_reg (code, X86_ECX);
124 x86_pop_reg (code, X86_EBP);
125 /* restore saved regs */
126 x86_pop_reg (code, X86_ESI);
127 x86_pop_reg (code, X86_EDI);
128 x86_pop_reg (code, X86_EBX);
132 g_assert ((code - start) < 28);
137 * return TRUE if the exception is catched. It also sets the
138 * stack_trace String.
141 arch_exc_is_catched (MonoDomain *domain, MonoJitTlsData *jit_tls, gpointer ip,
142 gpointer *bp, gpointer obj)
145 gpointer *end_of_stack;
146 MonoLMF *lmf = jit_tls->lmf;
150 end_of_stack = jit_tls->end_of_stack;
151 g_assert (end_of_stack);
155 ji = mono_jit_info_table_find (domain, ip);
157 if (ji) { /* we are inside managed code */
160 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
162 char *tmp, *tmpsig, *source_location, *tmpaddr;
163 gint32 address, iloffset;
165 if (!((MonoException*)obj)->stack_trace)
166 strace = g_strdup ("");
168 strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
170 address = (char *)ip - (char *)ji->code_start;
172 source_location = mono_debug_source_location_from_address (m, address);
173 iloffset = mono_debug_il_offset_from_address (m, address);
176 tmpaddr = g_strdup_printf ("<0x%05x>", address);
178 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
180 tmpsig = mono_signature_get_desc(m->signature, TRUE);
182 tmp = g_strdup_printf ("%sin %s (at %s) %s.%s:%s (%s)\n", strace, tmpaddr,
183 source_location, m->klass->name_space, m->klass->name,
186 tmp = g_strdup_printf ("%sin %s %s.%s:%s (%s)\n", strace, tmpaddr,
187 m->klass->name_space, m->klass->name, m->name, tmpsig);
188 g_free (source_location);
192 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
196 if (ji->num_clauses) {
198 g_assert (ji->clauses);
200 for (i = 0; i < ji->num_clauses; i++) {
201 MonoJitExceptionInfo *ei = &ji->clauses [i];
203 if (ei->try_start <= ip && ip <= (ei->try_end)) {
205 if (ei->flags == 0 && mono_object_isinst (obj,
206 mono_class_get (m->klass->image, ei->token_or_filter))) {
213 /* continue unwinding */
215 ip = (gpointer)(*((int *)bp + 1) - 5);
216 bp = (gpointer)(*((int *)bp));
218 if (bp >= end_of_stack)
225 bp = (gpointer)lmf->ebp;
226 ip = (gpointer)lmf->eip;
230 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
234 if (!((MonoException*)obj)->stack_trace)
235 strace = g_strdup ("");
237 strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
239 tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,
240 m->klass->name, m->name);
244 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
248 lmf = lmf->previous_lmf;
250 if (bp >= end_of_stack)
259 * arch_handle_exception:
260 * @ctx: saved processor state
264 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
266 MonoDomain *domain = mono_domain_get ();
268 gpointer ip = (gpointer)ctx->SC_EIP;
269 static void (*restore_context) (struct sigcontext *);
270 static void (*call_finally) (struct sigcontext *, unsigned long);
271 void (*cleanup) (MonoObject *exc);
272 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
273 gpointer end_of_stack;
275 g_assert (ctx != NULL);
278 MonoException *ex = mono_get_exception_null_reference ();
279 ex->message = mono_string_new (domain,
280 "Object reference not set to an instance of an object");
281 obj = (MonoObject *)ex;
284 ((MonoException *)obj)->stack_trace = NULL;
286 if (!restore_context)
287 restore_context = arch_get_restore_context ();
290 call_finally = arch_get_call_finally ();
292 end_of_stack = jit_tls->end_of_stack;
293 g_assert (end_of_stack);
295 cleanup = jit_tls->abort_func;
297 if (!arch_exc_is_catched (domain, jit_tls, ip, (gpointer *)ctx->SC_EBP, obj)) {
298 if (mono_debug_format != MONO_DEBUG_FORMAT_NONE) {
299 mono_debug_make_symbols ();
302 mono_unhandled_exception (obj);
307 ji = mono_jit_info_table_find (domain, ip);
309 if (ji) { /* we are inside managed code */
310 MonoMethod *m = ji->method;
313 if (ji->num_clauses) {
316 g_assert (ji->clauses);
318 for (i = 0; i < ji->num_clauses; i++) {
319 MonoJitExceptionInfo *ei = &ji->clauses [i];
321 if (ei->try_start <= ip && ip <= (ei->try_end)) {
323 if (ei->flags == 0 && mono_object_isinst (obj,
324 mono_class_get (m->klass->image, ei->token_or_filter))) {
326 ctx->SC_EIP = (unsigned long)ei->handler_start;
327 ctx->SC_ECX = (unsigned long)obj;
328 restore_context (ctx);
329 g_assert_not_reached ();
334 /* no handler found - we need to call all finally handlers */
335 for (i = 0; i < ji->num_clauses; i++) {
336 MonoJitExceptionInfo *ei = &ji->clauses [i];
338 if (ei->try_start <= ip && ip < (ei->try_end) &&
339 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
340 call_finally (ctx, (unsigned long)ei->handler_start);
345 /* continue unwinding */
348 /* restore caller saved registers */
349 if (ji->used_regs & X86_ESI_MASK) {
350 ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
353 if (ji->used_regs & X86_EDI_MASK) {
354 ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
357 if (ji->used_regs & X86_EBX_MASK) {
358 ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
361 ctx->SC_ESP = ctx->SC_EBP;
362 ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 5;
363 ctx->SC_EBP = *((int *)ctx->SC_EBP);
365 ip = (gpointer)ctx->SC_EIP;
367 if (ctx->SC_EBP > (unsigned)end_of_stack) {
370 g_assert_not_reached ();
374 MonoLMF *lmf = jit_tls->lmf;
379 g_assert_not_reached ();
382 jit_tls->lmf = lmf->previous_lmf;
384 ctx->SC_ESI = lmf->esi;
385 ctx->SC_EDI = lmf->edi;
386 ctx->SC_EBX = lmf->ebx;
387 ctx->SC_EBP = lmf->ebp;
388 ctx->SC_EIP = lmf->eip;
389 ctx->SC_ESP = (unsigned long)&lmf->eip;
391 ip = (gpointer)ctx->SC_EIP;
393 if (ctx->SC_EBP >= (unsigned)end_of_stack) {
396 g_assert_not_reached ();
401 g_assert_not_reached ();
405 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
406 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
407 unsigned long eip, unsigned long esp)
409 struct sigcontext ctx;
411 /* adjust eip so that it point to the call instruction */
424 arch_handle_exception (&ctx, exc);
426 g_assert_not_reached ();
430 * 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 arch_get_throw_exception (void)
444 static guint8 start [24];
445 static int inited = 0;
454 x86_push_reg (code, X86_ESP);
455 x86_push_membase (code, X86_ESP, 4); /* IP */
456 x86_push_membase (code, X86_ESP, 12); /* exception */
457 x86_push_reg (code, X86_EBP);
458 x86_push_reg (code, X86_EDI);
459 x86_push_reg (code, X86_ESI);
460 x86_push_reg (code, X86_EBX);
461 x86_push_reg (code, X86_EDX);
462 x86_push_reg (code, X86_ECX);
463 x86_push_reg (code, X86_EAX);
464 x86_call_code (code, throw_exception);
465 /* we should never reach this breakpoint */
466 x86_breakpoint (code);
468 g_assert ((code - start) < 24);
473 * arch_get_throw_exception_by_name:
475 * Returns a function pointer which can be used to raise
476 * corlib exceptions. The returned function has the following
477 * signature: void (*func) (char *exc_name);
478 * For example to raise an arithmetic exception you can use:
480 * x86_push_imm (code, "ArithmeticException");
481 * x86_call_code (code, arch_get_throw_exception ());
485 arch_get_throw_exception_by_name ()
487 static guint8 start [32];
488 static int inited = 0;
497 /* fixme: we do not save EAX, EDX, ECD - unsure if we need that */
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, arch_get_throw_exception ());
508 g_assert ((code - start) < 32);