45699a9199a20751db031fc89532a2e578cdcc02
[mono.git] / mono / jit / exception.c
1 /*
2  * exception.c: exception support
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12
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>
18
19 #include "jit.h"
20 #include "codegen.h"
21
22 #ifdef __FreeBSD__
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
32 #else
33 # define SC_EAX eax
34 # define SC_EBX ebx
35 # define SC_ECX ecx
36 # define SC_EDX edx
37 # define SC_EBP ebp
38 # define SC_EIP eip
39 # define SC_ESP esp
40 # define SC_EDI edi
41 # define SC_ESI esi
42 #endif
43
44 /*
45  * arch_get_restore_context:
46  *
47  * Returns a pointer to a method which restores a previously saved sigcontext.
48  */
49 static gpointer
50 arch_get_restore_context (void)
51 {
52         static guint8 *start = NULL;
53         guint8 *code;
54
55         if (start)
56                 return start;
57
58         /* restore_contect (struct sigcontext *ctx) */
59         /* we do not restore X86_EAX, X86_EDX */
60
61         start = code = g_malloc (1024);
62         
63         /* load ctx */
64         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
65
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);
68         /* restore EBX */
69         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
70         /* restore EDI */
71         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
72         /* restore ESI */
73         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
74         /* restore ESP */
75         x86_mov_reg_membase (code, X86_ESP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
76         /* restore EBP */
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);
80
81         /* jump to the saved IP */
82         x86_jump_reg (code, X86_EDX);
83
84         return start;
85 }
86
87 /*
88  * arch_get_call_finally:
89  *
90  * Returns a pointer to a method which calls a finally handler.
91  */
92 static gpointer
93 arch_get_call_finally (void)
94 {
95         static guint8 start [28];
96         static int inited = 0;
97         guint8 *code;
98
99         if (inited)
100                 return start;
101
102         inited = 1;
103         /* call_finally (struct sigcontext *ctx, unsigned long eip) */
104         code = start;
105
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);
111
112         /* load ctx */
113         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
114         /* load eip */
115         x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
116         /* save EBP */
117         x86_push_reg (code, X86_EBP);
118         /* set new 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);
122         /* restore EBP */
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);
128         x86_leave (code);
129         x86_ret (code);
130
131         g_assert ((code - start) < 28);
132         return start;
133 }
134
135
136 /**
137  * arch_handle_exception:
138  * @ctx: saved processor state
139  * @obj:
140  */
141 void
142 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
143 {
144         MonoDomain *domain = mono_domain_get ();
145         MonoJitInfo *ji;
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;
152
153         g_assert (ctx != NULL);
154         g_assert (obj != NULL);
155
156         ji = mono_jit_info_table_find (domain, ip);
157
158         end_of_stack = jit_tls->end_of_stack;
159                 
160         if (!restore_context)
161                 restore_context = arch_get_restore_context ();
162         
163         if (!call_finally)
164                 call_finally = arch_get_call_finally ();
165
166         cleanup = jit_tls->abort_func;
167
168         if (ji) { /* we are inside managed code */
169                 MonoMethod *m = ji->method;
170                 int offset = 2;
171
172                 if (ji->num_clauses) {
173                         int i;
174
175                         g_assert (ji->clauses);
176                         
177                         for (i = 0; i < ji->num_clauses; i++) {
178                                 MonoJitExceptionInfo *ei = &ji->clauses [i];
179
180                                 if (ei->try_start <= ip && ip <= (ei->try_end)) { 
181                                         /* catch block */
182                                         if (ei->flags == 0 && mono_object_isinst (obj, 
183                                                 mono_class_get (m->klass->image, ei->token_or_filter))) {
184                                         
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 ();
189                                         }
190                                 }
191                         }
192
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];
196
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);
200                                 }
201                         }
202                 }
203
204                 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
205                         char  *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
206                         char  *tmp, *tmpsig;
207
208                         if (!strcmp (strace, "TODO: implement stack traces")){
209                                 g_free (strace);
210                                 strace = g_strdup ("");
211                         }
212
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);
217                         g_free (tmpsig);
218                         g_free (strace);
219
220                         ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
221                         g_free (tmp);
222                 }
223
224                 /* continue unwinding */
225
226                 /* restore caller saved registers */
227                 if (ji->used_regs & X86_ESI_MASK) {
228                         ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
229                         offset++;
230                 }
231                 if (ji->used_regs & X86_EDI_MASK) {
232                         ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
233                         offset++;
234                 }
235                 if (ji->used_regs & X86_EBX_MASK) {
236                         ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
237                 }
238
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);
242                 
243                 if (ctx->SC_EBP < (unsigned)end_of_stack)
244                         arch_handle_exception (ctx, obj);
245                 else {
246                         g_assert (cleanup);
247                         cleanup (obj);
248                 }
249         
250         } else {
251                 MonoLMF *lmf = jit_tls->lmf;
252                 MonoMethod *m;
253
254                 if (!lmf) {
255                         g_assert (cleanup);
256                         cleanup (obj);
257                 }
258                 m = lmf->method;
259
260                 jit_tls->lmf = lmf->previous_lmf;
261
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;
268
269                 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
270                         char  *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
271                         char  *tmp;
272
273                         if (!strcmp (strace, "TODO: implement stack traces"))
274                                 strace = g_strdup ("");
275
276                         tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,  
277                                                m->klass->name, m->name);
278
279                         g_free (strace);
280
281                         ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
282                         g_free (tmp);
283                 }
284
285                 if (ctx->SC_EBP < (unsigned)end_of_stack)
286                         arch_handle_exception (ctx, obj);
287                 else {
288                         g_assert (cleanup);
289                         cleanup (obj);
290                 }
291         }
292
293         g_assert_not_reached ();
294 }
295
296 static void
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)
300 {
301         struct sigcontext ctx;
302
303         /* adjust eip so that it point to the call instruction */
304         eip -= 5;
305
306         ctx.SC_ESP = esp;
307         ctx.SC_EIP = eip;
308         ctx.SC_EBP = ebp;
309         ctx.SC_EDI = edi;
310         ctx.SC_ESI = esi;
311         ctx.SC_EBX = ebx;
312         ctx.SC_EDX = edx;
313         ctx.SC_ECX = ecx;
314         ctx.SC_EAX = eax;
315         
316         arch_handle_exception (&ctx, exc);
317
318         g_assert_not_reached ();
319 }
320
321 /**
322  * arch_get_throw_exception:
323  *
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:
328  *
329  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
330  * x86_call_code (code, arch_get_throw_exception ()); 
331  *
332  */
333 gpointer 
334 arch_get_throw_exception (void)
335 {
336         static guint8 start [24];
337         static int inited = 0;
338         guint8 *code;
339
340         if (inited)
341                 return start;
342
343         inited = 1;
344         code = start;
345
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);
359
360         g_assert ((code - start) < 24);
361         return start;
362 }
363
364 /**
365  * arch_get_throw_exception_by_name:
366  *
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:
371  *
372  * x86_push_imm (code, "ArithmeticException"); 
373  * x86_call_code (code, arch_get_throw_exception ()); 
374  *
375  */
376 gpointer 
377 arch_get_throw_exception_by_name ()
378 {
379         static guint8 start [32];
380         static int inited = 0;
381         guint8 *code;
382
383         if (inited)
384                 return start;
385
386         inited = 1;
387         code = start;
388
389         /* fixme: we do not save EAX, EDX, ECD - unsure if we need that */
390
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 ());
399
400         g_assert ((code - start) < 32);
401
402         return start;
403 }