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>
23 # define SC_EAX sc_eax
24 # define SC_EBX sc_ebx
25 # define SC_ECX sc_ecx
26 # define SC_EDX sc_edx
27 # define SC_EBP sc_ebp
28 # define SC_EIP sc_eip
29 # define SC_ESP sc_esp
30 # define SC_EDI sc_edi
31 # define SC_ESI sc_esi
45 * arch_get_restore_context:
47 * Returns a pointer to a method which restores a previously saved sigcontext.
50 arch_get_restore_context (void)
52 static guint8 *start = NULL;
58 /* restore_contect (struct sigcontext *ctx) */
59 /* we do not restore X86_EAX, X86_EDX */
61 start = code = g_malloc (1024);
64 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
66 /* get return address, stored in EDX */
67 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EIP), 4);
69 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
71 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
73 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
75 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
77 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
78 /* restore ECX. the exception object is passed here to the catch handler */
79 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ECX), 4);
81 /* jump to the saved IP */
82 x86_jump_reg (code, X86_EDX);
88 * arch_get_call_finally:
90 * Returns a pointer to a method which calls a finally handler.
93 arch_get_call_finally (void)
95 static guint8 start [28];
96 static int inited = 0;
103 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
106 x86_push_reg (code, X86_EBP);
107 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
108 x86_push_reg (code, X86_EBX);
109 x86_push_reg (code, X86_EDI);
110 x86_push_reg (code, X86_ESI);
113 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
115 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
117 x86_push_reg (code, X86_EBP);
119 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
120 /* call the handler */
121 x86_call_reg (code, X86_ECX);
123 x86_pop_reg (code, X86_EBP);
124 /* restore saved regs */
125 x86_pop_reg (code, X86_ESI);
126 x86_pop_reg (code, X86_EDI);
127 x86_pop_reg (code, X86_EBX);
131 g_assert ((code - start) < 28);
137 * arch_handle_exception:
138 * @ctx: saved processor state
142 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
144 MonoDomain *domain = mono_domain_get ();
146 gpointer ip = (gpointer)ctx->SC_EIP;
147 static void (*restore_context) (struct sigcontext *);
148 static void (*call_finally) (struct sigcontext *, unsigned long);
149 void (*cleanup) (MonoObject *exc);
150 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
151 gpointer end_of_stack;
153 g_assert (ctx != NULL);
154 g_assert (obj != NULL);
156 ji = mono_jit_info_table_find (domain, ip);
158 end_of_stack = jit_tls->end_of_stack;
160 if (!restore_context)
161 restore_context = arch_get_restore_context ();
164 call_finally = arch_get_call_finally ();
166 cleanup = jit_tls->abort_func;
168 if (ji) { /* we are inside managed code */
169 MonoMethod *m = ji->method;
172 if (ji->num_clauses) {
175 g_assert (ji->clauses);
177 for (i = 0; i < ji->num_clauses; i++) {
178 MonoJitExceptionInfo *ei = &ji->clauses [i];
180 if (ei->try_start <= ip && ip <= (ei->try_end)) {
182 if (ei->flags == 0 && mono_object_isinst (obj,
183 mono_class_get (m->klass->image, ei->token_or_filter))) {
185 ctx->SC_EIP = (unsigned long)ei->handler_start;
186 ctx->SC_ECX = (unsigned long)obj;
187 restore_context (ctx);
188 g_assert_not_reached ();
193 /* no handler found - we need to call all finally handlers */
194 for (i = 0; i < ji->num_clauses; i++) {
195 MonoJitExceptionInfo *ei = &ji->clauses [i];
197 if (ei->try_start <= ip && ip < (ei->try_end) &&
198 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
199 call_finally (ctx, (unsigned long)ei->handler_start);
204 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
205 char *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
208 if (!strcmp (strace, "TODO: implement stack traces")){
210 strace = g_strdup ("");
213 tmpsig = mono_signature_get_desc(m->signature, TRUE);
214 tmp = g_strdup_printf ("%sin %04x %s.%s:%s (%s)\n", strace,
215 (char *)ip - (char *)ji->code_start,
216 m->klass->name_space, m->klass->name, m->name, tmpsig);
220 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
224 /* continue unwinding */
226 /* restore caller saved registers */
227 if (ji->used_regs & X86_ESI_MASK) {
228 ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
231 if (ji->used_regs & X86_EDI_MASK) {
232 ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
235 if (ji->used_regs & X86_EBX_MASK) {
236 ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
239 ctx->SC_ESP = ctx->SC_EBP;
240 ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 5;
241 ctx->SC_EBP = *((int *)ctx->SC_EBP);
243 if (ctx->SC_EBP < (unsigned)end_of_stack)
244 arch_handle_exception (ctx, obj);
251 MonoLMF *lmf = jit_tls->lmf;
260 jit_tls->lmf = lmf->previous_lmf;
262 ctx->SC_ESI = lmf->esi;
263 ctx->SC_EDI = lmf->edi;
264 ctx->SC_EBX = lmf->ebx;
265 ctx->SC_EBP = lmf->ebp;
266 ctx->SC_EIP = lmf->eip;
267 ctx->SC_ESP = (unsigned long)&lmf->eip;
269 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
270 char *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
273 if (!strcmp (strace, "TODO: implement stack traces"))
274 strace = g_strdup ("");
276 tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,
277 m->klass->name, m->name);
281 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
285 if (ctx->SC_EBP < (unsigned)end_of_stack)
286 arch_handle_exception (ctx, obj);
293 g_assert_not_reached ();
297 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
298 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
299 unsigned long eip, unsigned long esp)
301 struct sigcontext ctx;
303 /* adjust eip so that it point to the call instruction */
316 arch_handle_exception (&ctx, exc);
318 g_assert_not_reached ();
322 * arch_get_throw_exception:
324 * Returns a function pointer which can be used to raise
325 * exceptions. The returned function has the following
326 * signature: void (*func) (MonoException *exc);
327 * For example to raise an arithmetic exception you can use:
329 * x86_push_imm (code, mono_get_exception_arithmetic ());
330 * x86_call_code (code, arch_get_throw_exception ());
334 arch_get_throw_exception (void)
336 static guint8 start [24];
337 static int inited = 0;
346 x86_push_reg (code, X86_ESP);
347 x86_push_membase (code, X86_ESP, 4); /* IP */
348 x86_push_membase (code, X86_ESP, 12); /* exception */
349 x86_push_reg (code, X86_EBP);
350 x86_push_reg (code, X86_EDI);
351 x86_push_reg (code, X86_ESI);
352 x86_push_reg (code, X86_EBX);
353 x86_push_reg (code, X86_EDX);
354 x86_push_reg (code, X86_ECX);
355 x86_push_reg (code, X86_EAX);
356 x86_call_code (code, throw_exception);
357 /* we should never reach this breakpoint */
358 x86_breakpoint (code);
360 g_assert ((code - start) < 24);
365 * arch_get_throw_exception_by_name:
367 * Returns a function pointer which can be used to raise
368 * corlib exceptions. The returned function has the following
369 * signature: void (*func) (char *exc_name);
370 * For example to raise an arithmetic exception you can use:
372 * x86_push_imm (code, "ArithmeticException");
373 * x86_call_code (code, arch_get_throw_exception ());
377 arch_get_throw_exception_by_name ()
379 static guint8 start [32];
380 static int inited = 0;
389 /* fixme: we do not save EAX, EDX, ECD - unsure if we need that */
391 x86_push_membase (code, X86_ESP, 4); /* exception name */
392 x86_push_imm (code, "System");
393 x86_push_imm (code, mono_defaults.exception_class->image);
394 x86_call_code (code, mono_exception_from_name);
395 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
396 /* save the newly create object (overwrite exception name)*/
397 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
398 x86_jump_code (code, arch_get_throw_exception ());
400 g_assert ((code - start) < 32);