38360df679909390bd252b4ddba169a76c9b5e3b
[mono.git] / mono / mini / mini-exceptions.c
1 /*
2  * mini-exceptions.c: generic 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 #include <signal.h>
13 #include <string.h>
14
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/threads.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/gc-internal.h>
21 #include <mono/metadata/mono-debug.h>
22
23 #include "mini.h"
24
25 #define IS_ON_SIGALTSTACK(jit_tls) ((jit_tls) && ((guint8*)&(jit_tls) > (guint8*)(jit_tls)->signal_stack) && ((guint8*)&(jit_tls) < ((guint8*)(jit_tls)->signal_stack + (jit_tls)->signal_stack_size)))
26
27 MonoArray *
28 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
29 {
30         MonoDomain *domain = mono_domain_get ();
31         MonoArray *res;
32         MonoArray *ta = exc->trace_ips;
33         int i, len;
34
35         if (ta == NULL) {
36                 /* Exception is not thrown yet */
37                 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
38         }
39         
40         len = mono_array_length (ta);
41
42         res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
43
44         for (i = skip; i < len; i++) {
45                 MonoJitInfo *ji;
46                 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
47                 gpointer ip = mono_array_get (ta, gpointer, i);
48
49                 ji = mono_jit_info_table_find (domain, ip);
50                 if (ji == NULL) {
51                         /* Unmanaged frame */
52                         mono_array_set (res, gpointer, i, sf);
53                         continue;
54                 }
55
56                 g_assert (ji != NULL);
57
58                 sf->method = mono_method_get_object (domain, ji->method, NULL);
59                 sf->native_offset = (char *)ip - (char *)ji->code_start;
60
61                 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
62
63                 if (need_file_info) {
64                         gchar *filename;
65                         
66                         filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
67
68                         sf->filename = filename? mono_string_new (domain, filename): NULL;
69                         sf->column = 0;
70
71                         g_free (filename);
72                 }
73
74                 mono_array_set (res, gpointer, i, sf);
75         }
76
77         return res;
78 }
79
80 void
81 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
82         MonoDomain *domain = mono_domain_get ();
83         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
84         MonoLMF *lmf = jit_tls->lmf;
85         MonoJitInfo *ji, rji;
86         gint native_offset, il_offset;
87         gboolean managed;
88
89         MonoContext ctx, new_ctx;
90
91         MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
92         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
93
94         while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
95                 
96                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
97                 g_assert (ji);
98
99                 if (ji == (gpointer)-1)
100                         return;
101
102                 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
103
104                 if (func (ji->method, native_offset, il_offset, managed, user_data))
105                         return;
106                 
107                 ctx = new_ctx;
108         }
109 }
110
111 MonoBoolean
112 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
113                           MonoReflectionMethod **method, 
114                           gint32 *iloffset, gint32 *native_offset,
115                           MonoString **file, gint32 *line, gint32 *column)
116 {
117         MonoDomain *domain = mono_domain_get ();
118         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
119         MonoLMF *lmf = jit_tls->lmf;
120         MonoJitInfo *ji, rji;
121         MonoContext ctx, new_ctx;
122
123         MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
124         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
125
126         skip++;
127
128         do {
129                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
130
131                 ctx = new_ctx;
132                 
133                 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
134                         return FALSE;
135
136                 /* skip all wrappers ??*/
137                 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
138                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
139                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
140                         continue;
141
142                 skip--;
143
144         } while (skip >= 0);
145
146         *method = mono_method_get_object (domain, ji->method, NULL);
147         *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
148
149         if (need_file_info) {
150                 gchar *filename;
151
152                 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
153
154                 *file = filename? mono_string_new (domain, filename): NULL;
155                 *column = 0;
156
157                 g_free (filename);
158         }
159
160         return TRUE;
161 }
162
163 static MonoArray *
164 glist_to_array (GList *list) 
165 {
166         MonoDomain *domain = mono_domain_get ();
167         MonoArray *res;
168         int len, i;
169
170         if (!list)
171                 return NULL;
172
173         len = g_list_length (list);
174         res = mono_array_new (domain, mono_defaults.int_class, len);
175
176         for (i = 0; list; list = list->next, i++)
177                 mono_array_set (res, gpointer, i, list->data);
178
179         return res;
180 }
181
182 /**
183  * mono_handle_exception:
184  * @ctx: saved processor state
185  * @obj: the exception object
186  * @test_only: only test if the exception is caught, but dont call handlers
187  *
188  */
189 gboolean
190 mono_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
191 {
192         MonoDomain *domain = mono_domain_get ();
193         MonoJitInfo *ji, rji;
194         static int (*call_filter) (MonoContext *, gpointer) = NULL;
195         static void (*restore_context) (struct sigcontext *);
196         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
197         MonoLMF *lmf = jit_tls->lmf;            
198         GList *trace_ips = NULL;
199         MonoException *mono_ex;
200         gboolean stack_overflow = FALSE;
201         MonoContext initial_ctx;
202         int frame_count = 0;
203         gboolean gc_disabled = FALSE;
204         MonoString *initial_stack_trace;
205         
206         /*
207          * This function might execute on an alternate signal stack, and Boehm GC
208          * can't handle that.
209          * Also, since the altstack is small, stack space intensive operations like
210          * JIT compilation should be avoided.
211          */
212         if (IS_ON_SIGALTSTACK (jit_tls)) {
213                 /* 
214                  * FIXME: disabling/enabling GC while already on a signal stack might
215                  * not be safe either.
216                  */
217                 /* Have to reenable it later */
218                 gc_disabled = TRUE;
219                 mono_gc_disable ();
220         }
221
222         g_assert (ctx != NULL);
223         if (!obj) {
224                 MonoException *ex = mono_get_exception_null_reference ();
225                 ex->message = mono_string_new (domain, "Object reference not set to an instance of an object");
226                 obj = (MonoObject *)ex;
227         } 
228
229         /*
230          * Allocate a new exception object instead of the preconstructed ones.
231          * We can't do this in sigsegv_signal_handler, since GC is not yet
232          * disabled.
233          */
234         if (obj == domain->stack_overflow_ex) {
235                 obj = mono_get_exception_stack_overflow ();
236         }
237         else if (obj == domain->null_reference_ex) {
238                 obj = mono_get_exception_null_reference ();
239         }
240
241         if (mono_object_isinst (obj, mono_defaults.exception_class)) {
242                 mono_ex = (MonoException*)obj;
243                 initial_stack_trace = mono_ex->stack_trace;
244         } else {
245                 mono_ex = NULL;
246         }
247
248         if (obj == domain->stack_overflow_ex)
249                 stack_overflow = TRUE;
250
251         if (!call_filter)
252                 call_filter = mono_arch_get_call_filter ();
253
254         if (!restore_context)
255                 restore_context = mono_arch_get_restore_context ();
256
257         g_assert (jit_tls->end_of_stack);
258         g_assert (jit_tls->abort_func);
259
260         if (!test_only) {
261                 MonoContext ctx_cp = *ctx;
262                 if (mono_jit_trace_calls != NULL)
263                         g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
264                 if (!mono_handle_exception (&ctx_cp, obj, TRUE)) {
265                         if (mono_break_on_exc)
266                                 G_BREAKPOINT ();
267                         mono_unhandled_exception (obj);
268                 }
269         }
270
271         initial_ctx = *ctx;
272         memset (&rji, 0, sizeof (rji));
273
274         while (1) {
275                 MonoContext new_ctx;
276                 char *trace = NULL;
277                 gboolean need_trace = FALSE;
278                 guint32 free_stack;
279
280                 if (test_only && (frame_count < 1000))
281                         need_trace = TRUE;
282
283                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx, 
284                                               need_trace ? &trace : NULL, &lmf, NULL, NULL);
285                 if (!ji) {
286                         g_warning ("Exception inside function without unwind info");
287                         g_assert_not_reached ();
288                 }
289
290                 if (ji != (gpointer)-1) {
291                         frame_count ++;
292                         //printf ("M: %s %p %p %d.\n", mono_method_full_name (ji->method, TRUE), jit_tls->end_of_stack, ctx->ebp, count);
293
294                         if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
295                                 char *tmp, *strace;
296
297                                 /* 
298                                  * Avoid overwriting the stack trace if the exception is
299                                  * rethrown. Also avoid giant stack traces during a stack
300                                  * overflow.
301                                  */
302                                 if (!initial_stack_trace && (frame_count < 1000)) {
303                                         trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
304
305                                         if (!mono_ex->stack_trace)
306                                                 strace = g_strdup ("");
307                                         else
308                                                 strace = mono_string_to_utf8 (mono_ex->stack_trace);
309
310                                         tmp = g_strdup_printf ("%s%s\n", strace, trace);
311                                         g_free (strace);
312
313                                         mono_ex->stack_trace = mono_string_new (domain, tmp);
314
315                                         g_free (tmp);
316                                 }
317                         }
318
319                         if (stack_overflow)
320                                 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
321                         else
322                                 free_stack = 0xffffff;
323
324                         /* 
325                          * During stack overflow, wait till the unwinding frees some stack
326                          * space before running handlers/finalizers.
327                          */
328                         if ((free_stack > (64 * 1024)) && ji->num_clauses) {
329                                 int i;
330                                 
331                                 g_assert (ji->clauses);
332                         
333                                 for (i = 0; i < ji->num_clauses; i++) {
334                                         MonoJitExceptionInfo *ei = &ji->clauses [i];
335
336                                         if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
337                                             MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) { 
338                                                 /* catch block */
339
340                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
341                                                         /* store the exception object int cfg->excvar */
342                                                         g_assert (ji->exvar_offset);
343                                                         *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
344                                                 }
345
346                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
347                                                      mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
348                                                     ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
349                                                       call_filter (ctx, ei->data.filter)))) {
350                                                         if (test_only) {
351                                                                 if (mono_ex)
352                                                                         mono_ex->trace_ips = glist_to_array (trace_ips);
353                                                                 g_list_free (trace_ips);
354                                                                 g_free (trace);
355
356                                                                 if (gc_disabled)
357                                                                         mono_gc_enable ();
358                                                                 return TRUE;
359                                                         }
360                                                         if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
361                                                                 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
362                                                         MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
363                                                         jit_tls->lmf = lmf;
364                                                         g_free (trace);
365
366                                                         if (gc_disabled)
367                                                                 mono_gc_enable ();
368                                                         return 0;
369                                                 }
370                                                 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
371                                                     MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
372                                                     (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
373                                                         if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
374                                                                 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
375                                                         call_filter (ctx, ei->handler_start);
376                                                 }
377                                                 
378                                         }
379                                 }
380                         }
381                 }
382
383                 g_free (trace);
384                         
385                 *ctx = new_ctx;
386
387                 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
388                         if (gc_disabled)
389                                 mono_gc_enable ();
390
391                         if (!test_only) {
392                                 jit_tls->lmf = lmf;
393
394                                 if (IS_ON_SIGALTSTACK (jit_tls)) {
395                                         /* Switch back to normal stack */
396                                         if (stack_overflow)
397                                                 /* Free up some stack space */
398                                                 initial_ctx.SC_ESP += (64 * 1024);
399                                         initial_ctx.SC_EIP = (unsigned int)jit_tls->abort_func;
400                                         restore_context (&initial_ctx);
401                                 }
402                                 else
403                                         jit_tls->abort_func (obj);
404                                 g_assert_not_reached ();
405                         } else {
406                                 if (mono_ex)
407                                         mono_ex->trace_ips = glist_to_array (trace_ips);
408                                 g_list_free (trace_ips);
409                                 return FALSE;
410                         }
411                 }
412         }
413
414         g_assert_not_reached ();
415 }