3 * generic exception support
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Mono Team (mono-list@lists.ximian.com)
9 * Copyright 2001-2003 Ximian, Inc.
10 * Copyright 2003-2008 Novell, Inc.
11 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
23 #ifdef HAVE_EXECINFO_H
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
31 #ifdef HAVE_SYS_WAIT_H
39 #ifdef HAVE_SYS_SYSCALL_H
40 #include <sys/syscall.h>
43 #ifdef HAVE_SYS_PRCTL_H
44 #include <sys/prctl.h>
51 #include <mono/metadata/appdomain.h>
52 #include <mono/metadata/tabledefs.h>
53 #include <mono/metadata/threads.h>
54 #include <mono/metadata/threads-types.h>
55 #include <mono/metadata/debug-helpers.h>
56 #include <mono/metadata/exception.h>
57 #include <mono/metadata/exception-internals.h>
58 #include <mono/metadata/object-internals.h>
59 #include <mono/metadata/reflection-internals.h>
60 #include <mono/metadata/gc-internals.h>
61 #include <mono/metadata/mono-debug.h>
62 #include <mono/metadata/profiler.h>
63 #include <mono/metadata/mono-endian.h>
64 #include <mono/metadata/environment.h>
65 #include <mono/metadata/mono-mlist.h>
66 #include <mono/utils/mono-mmap.h>
67 #include <mono/utils/mono-logger-internals.h>
68 #include <mono/utils/mono-error.h>
69 #include <mono/utils/mono-error-internals.h>
73 #include "debugger-agent.h"
74 #include "seq-points.h"
75 #include "llvm-runtime.h"
76 #include "mini-llvm.h"
77 #include "interp/interp.h"
80 #include "mini-llvm-cpp.h"
83 #ifndef MONO_ARCH_CONTEXT_DEF
84 #define MONO_ARCH_CONTEXT_DEF
87 #ifndef MONO_ARCH_STACK_GROWS_UP
88 #define MONO_ARCH_STACK_GROWS_UP 0
91 static gpointer restore_context_func, call_filter_func;
92 static gpointer throw_exception_func, rethrow_exception_func;
93 static gpointer throw_corlib_exception_func;
95 static gpointer try_more_restore_tramp = NULL;
96 static gpointer restore_stack_protection_tramp = NULL;
98 static void try_more_restore (void);
99 static void restore_stack_protection (void);
100 static void mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data);
101 static void mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx);
102 static void mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data);
103 static gboolean mono_current_thread_has_handle_block_guard (void);
106 first_managed (MonoStackFrameInfo *frame, MonoContext *ctx, gpointer addr)
108 gpointer **data = (gpointer **)addr;
114 // FIXME: Happens with llvm_only
119 *data = MONO_CONTEXT_GET_SP (ctx);
125 mono_thread_get_managed_sp (void)
127 gpointer addr = NULL;
128 mono_walk_stack (first_managed, MONO_UNWIND_SIGNAL_SAFE, &addr);
133 mini_abort_threshold_offset (gpointer threshold, gpointer sp)
135 intptr_t stack_threshold = (intptr_t) threshold;
136 intptr_t stack_pointer = (intptr_t) sp;
138 const int direction = MONO_ARCH_STACK_GROWS_UP ? -1 : 1;
139 intptr_t magnitude = stack_pointer - stack_threshold;
141 return direction * magnitude;
145 mini_clear_abort_threshold (void)
147 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
148 jit_tls->abort_exc_stack_threshold = NULL;
152 mini_set_abort_threshold (MonoContext *ctx)
154 gpointer sp = MONO_CONTEXT_GET_SP (ctx);
155 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
156 // Only move it up, to avoid thrown/caught
157 // exceptions lower in the stack from triggering
159 gboolean above_threshold = mini_abort_threshold_offset (jit_tls->abort_exc_stack_threshold, sp) >= 0;
160 if (!jit_tls->abort_exc_stack_threshold || above_threshold) {
161 jit_tls->abort_exc_stack_threshold = sp;
165 // Note: In the case that the frame is above where the thread abort
166 // was set we bump the threshold so that functions called from the new,
167 // higher threshold don't trigger the thread abort exception
168 static inline gboolean
169 mini_above_abort_threshold (void)
171 gpointer sp = mono_thread_get_managed_sp ();
172 MonoJitTlsData *jit_tls = (MonoJitTlsData*) mono_tls_get_jit_tls ();
177 gboolean above_threshold = mini_abort_threshold_offset (jit_tls->abort_exc_stack_threshold, sp) >= 0;
180 jit_tls->abort_exc_stack_threshold = sp;
182 return above_threshold;
186 mono_get_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset)
189 if (mono_find_prev_seq_point_for_native_offset (domain, method, native_offset, NULL, &sp))
195 mono_exceptions_init (void)
197 MonoRuntimeExceptionHandlingCallbacks cbs;
199 restore_context_func = mono_aot_get_trampoline ("restore_context");
200 call_filter_func = mono_aot_get_trampoline ("call_filter");
201 throw_exception_func = mono_aot_get_trampoline ("throw_exception");
202 rethrow_exception_func = mono_aot_get_trampoline ("rethrow_exception");
206 restore_context_func = mono_arch_get_restore_context (&info, FALSE);
207 mono_tramp_info_register (info, NULL);
208 call_filter_func = mono_arch_get_call_filter (&info, FALSE);
209 mono_tramp_info_register (info, NULL);
210 throw_exception_func = mono_arch_get_throw_exception (&info, FALSE);
211 mono_tramp_info_register (info, NULL);
212 rethrow_exception_func = mono_arch_get_rethrow_exception (&info, FALSE);
213 mono_tramp_info_register (info, NULL);
215 #ifdef MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT
216 if (!mono_llvm_only) {
217 try_more_restore_tramp = mono_create_specific_trampoline (try_more_restore, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
218 restore_stack_protection_tramp = mono_create_specific_trampoline (restore_stack_protection, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
222 #ifdef MONO_ARCH_HAVE_EXCEPTIONS_INIT
223 mono_arch_exceptions_init ();
225 #ifdef ENABLE_INTERPRETER
226 if (mono_use_interpreter)
227 cbs.mono_walk_stack_with_ctx = interp_walk_stack_with_ctx;
230 cbs.mono_walk_stack_with_ctx = mono_runtime_walk_stack_with_ctx;
232 cbs.mono_walk_stack_with_state = mono_walk_stack_with_state;
235 cbs.mono_raise_exception = mono_llvm_raise_exception;
237 cbs.mono_raise_exception = (void (*)(MonoException *))mono_get_throw_exception ();
238 cbs.mono_raise_exception_with_ctx = mono_raise_exception_with_ctx;
239 cbs.mono_exception_walk_trace = mono_exception_walk_trace;
240 cbs.mono_install_handler_block_guard = mono_install_handler_block_guard;
241 cbs.mono_current_thread_has_handle_block_guard = mono_current_thread_has_handle_block_guard;
242 cbs.mono_clear_abort_threshold = mini_clear_abort_threshold;
243 cbs.mono_above_abort_threshold = mini_above_abort_threshold;
244 mono_install_eh_callbacks (&cbs);
245 mono_install_get_seq_point (mono_get_seq_point_for_native_offset);
249 mono_get_throw_exception (void)
251 g_assert (throw_exception_func);
252 return throw_exception_func;
256 mono_get_rethrow_exception (void)
258 g_assert (rethrow_exception_func);
259 return rethrow_exception_func;
263 mono_get_call_filter (void)
265 g_assert (call_filter_func);
266 return call_filter_func;
270 mono_get_restore_context (void)
272 g_assert (restore_context_func);
273 return restore_context_func;
277 mono_get_throw_corlib_exception (void)
279 gpointer code = NULL;
282 /* This depends on corlib classes so cannot be inited in mono_exceptions_init () */
283 if (throw_corlib_exception_func)
284 return throw_corlib_exception_func;
287 code = mono_aot_get_trampoline ("throw_corlib_exception");
289 code = mono_arch_get_throw_corlib_exception (&info, FALSE);
290 mono_tramp_info_register (info, NULL);
293 mono_memory_barrier ();
295 throw_corlib_exception_func = code;
297 return throw_corlib_exception_func;
301 * mono_get_throw_exception_addr:
303 * Return an address which stores the result of
304 * mono_get_throw_exception.
307 mono_get_throw_exception_addr (void)
309 return &throw_exception_func;
313 is_address_protected (MonoJitInfo *ji, MonoJitExceptionInfo *ei, gpointer ip)
315 MonoTryBlockHoleTableJitInfo *table;
320 if (ei->try_start > ip || ip >= ei->try_end)
323 if (!ji->has_try_block_holes)
326 table = mono_jit_info_get_try_block_hole_table_info (ji);
327 offset = (guint32)((char*)ip - (char*)ji->code_start);
328 clause = (guint16)(ei - ji->clauses);
329 g_assert (clause < ji->num_clauses);
331 for (i = 0; i < table->num_holes; ++i) {
332 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
333 if (hole->clause == clause && hole->offset <= offset && hole->offset + hole->length > offset)
339 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
342 static gboolean show_native_addresses = TRUE;
344 static gboolean show_native_addresses = FALSE;
347 static _Unwind_Reason_Code
348 build_stack_trace (struct _Unwind_Context *frame_ctx, void *state)
350 MonoDomain *domain = mono_domain_get ();
351 uintptr_t ip = _Unwind_GetIP (frame_ctx);
353 if (show_native_addresses || mono_jit_info_table_find (domain, (char*)ip)) {
354 GList **trace_ips = (GList **)state;
355 *trace_ips = g_list_prepend (*trace_ips, (gpointer)ip);
358 return _URC_NO_REASON;
362 get_unwind_backtrace (void)
366 _Unwind_Backtrace (build_stack_trace, &ips);
368 return g_slist_reverse (ips);
374 get_unwind_backtrace (void)
384 * Translate between the mono_arch_unwind_frame function and the old API.
387 find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
388 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
390 StackFrameInfo frame;
393 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
395 /* Avoid costly table lookup during stack overflow */
396 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
399 ji = mini_jit_info_table_find (domain, (char *)ip, NULL);
404 err = mono_arch_unwind_frame (domain, jit_tls, ji, ctx, new_ctx, lmf, NULL, &frame);
406 return (MonoJitInfo *)-1;
408 if (*lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
410 * Remove any unused lmf.
411 * Mask out the lower bits which might be used to hold additional information.
413 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
416 /* Convert between the new and the old APIs */
417 switch (frame.type) {
418 case FRAME_TYPE_MANAGED:
422 case FRAME_TYPE_TRAMPOLINE:
424 case FRAME_TYPE_MANAGED_TO_NATIVE:
428 memset (res, 0, sizeof (MonoJitInfo));
429 res->d.method = frame.method;
432 case FRAME_TYPE_DEBUGGER_INVOKE: {
436 * The normal exception handling code can't handle this frame, so just
439 ji = find_jit_info (domain, jit_tls, res, NULL, new_ctx, &tmp_ctx, lmf, managed);
440 memcpy (new_ctx, &tmp_ctx, sizeof (MonoContext));
444 g_assert_not_reached ();
449 /* mono_find_jit_info:
451 * This function is used to gather information from @ctx. It return the
452 * MonoJitInfo of the corresponding function, unwinds one stack frame and
453 * stores the resulting context into @new_ctx. It also stores a string
454 * describing the stack location into @trace (if not NULL), and modifies
455 * the @lmf if necessary. @native_offset return the IP offset from the
456 * start of the function or -1 if that info is not available.
459 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
460 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
464 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
466 MonoMethod *method = NULL;
477 ji = find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
479 if (ji == (gpointer)-1)
482 if (ji && !ji->is_trampoline)
483 method = jinfo_get_method (ji);
485 if (managed2 || (method && method->wrapper_type)) {
486 const char *real_ip, *start;
489 start = (const char *)ji->code_start;
491 /* ctx->ip points into native code */
492 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
494 real_ip = (const char*)ip;
496 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
497 offset = real_ip - start;
502 *native_offset = offset;
505 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
509 *trace = mono_debug_print_stack_frame (method, offset, domain);
512 char *fname = mono_method_full_name (jinfo_get_method (res), TRUE);
513 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
522 * mono_find_jit_info_ext:
524 * A version of mono_find_jit_info which returns all data in the StackFrameInfo
526 * A note about frames of type FRAME_TYPE_MANAGED_TO_NATIVE:
527 * - These frames are used to mark managed-to-native transitions, so CTX will refer to native
528 * code, and new_ctx will refer to the last managed frame. The caller should unwind once more
529 * to obtain the last managed frame.
530 * If SAVE_LOCATIONS is not NULL, it should point to an array of size MONO_MAX_IREGS.
531 * On return, it will be filled with the locations where callee saved registers are saved
532 * by the current frame. This is returned outside of StackFrameInfo because it can be
533 * quite large on some platforms.
534 * If ASYNC true, this function will be async safe, but some fields of frame and frame->ji will
538 mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
539 MonoJitInfo *prev_ji, MonoContext *ctx,
540 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
541 mgreg_t **save_locations,
542 StackFrameInfo *frame)
545 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
547 MonoDomain *target_domain = domain;
548 MonoMethod *method = NULL;
549 gboolean async = mono_thread_info_is_async_context ();
554 /* Avoid costly table lookup during stack overflow */
555 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
558 ji = mini_jit_info_table_find (domain, (char *)ip, &target_domain);
561 target_domain = domain;
564 memset (save_locations, 0, MONO_MAX_IREGS * sizeof (mgreg_t*));
566 err = mono_arch_unwind_frame (target_domain, jit_tls, ji, ctx, new_ctx, lmf, save_locations, frame);
570 if (*lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
572 * Remove any unused lmf.
573 * Mask out the lower bits which might be used to hold additional information.
575 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
578 if (frame->ji && !frame->ji->is_trampoline && !frame->ji->async)
579 method = jinfo_get_method (frame->ji);
581 if (frame->type == FRAME_TYPE_MANAGED && method) {
582 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
583 frame->managed = TRUE;
586 if (frame->type == FRAME_TYPE_MANAGED_TO_NATIVE) {
588 * This type of frame is just a marker, the caller should unwind once more to get the
589 * last managed frame.
592 frame->method = NULL;
595 frame->native_offset = -1;
596 frame->domain = target_domain;
597 frame->async_context = async;
601 if (frame->type == FRAME_TYPE_MANAGED)
602 frame->method = method;
604 if (ji && (frame->managed || (method && method->wrapper_type))) {
605 const char *real_ip, *start;
607 start = (const char *)ji->code_start;
608 if (frame->type == FRAME_TYPE_MANAGED)
609 real_ip = (const char*)ip;
611 /* ctx->ip points into native code */
612 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
614 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
615 frame->native_offset = real_ip - start;
617 frame->native_offset = -1;
621 *trace = mono_debug_print_stack_frame (method, frame->native_offset, domain);
623 if (trace && frame->method) {
624 char *fname = mono_method_full_name (frame->method, TRUE);
625 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
634 * This function is async-safe.
637 get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
639 MonoGenericJitInfo *gi;
643 if (!ji->has_generic_jit_info)
645 gi = mono_jit_info_get_generic_jit_info (ji);
651 * Search location list if available, it contains the precise location of the
652 * argument for every pc offset, even if the method was interrupted while it was in
656 int offset = (mgreg_t)MONO_CONTEXT_GET_IP (ctx) - (mgreg_t)ji->code_start;
659 for (i = 0; i < gi->nlocs; ++i) {
660 MonoDwarfLocListEntry *entry = &gi->locations [i];
662 if (offset >= entry->from && (offset < entry->to || entry->to == 0)) {
664 info = (gpointer)mono_arch_context_get_int_reg (ctx, entry->reg);
666 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, entry->reg) + entry->offset);
670 g_assert (i < gi->nlocs);
673 info = (gpointer)mono_arch_context_get_int_reg (ctx, gi->this_reg);
675 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
679 method = jinfo_get_method (ji);
680 if (mono_method_get_context (method)->method_inst) {
681 /* A MonoMethodRuntimeGenericContext* */
683 } else if ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype) {
687 /* Avoid returning a managed object */
688 MonoObject *this_obj = (MonoObject *)info;
690 return this_obj->vtable;
695 * generic_info is either a MonoMethodRuntimeGenericContext or a MonoVTable.
697 static MonoGenericContext
698 get_generic_context_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
700 MonoGenericContext context = { NULL, NULL };
701 MonoClass *klass, *method_container_class;
704 g_assert (generic_info);
706 method = jinfo_get_method (ji);
707 g_assert (method->is_inflated);
708 if (mono_method_get_context (method)->method_inst) {
709 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext *)generic_info;
711 klass = mrgctx->class_vtable->klass;
712 context.method_inst = mrgctx->method_inst;
713 g_assert (context.method_inst);
715 MonoVTable *vtable = (MonoVTable *)generic_info;
717 klass = vtable->klass;
720 //g_assert (!mono_class_is_gtd (method->klass));
721 if (mono_class_is_ginst (method->klass))
722 method_container_class = mono_class_get_generic_class (method->klass)->container_class;
724 method_container_class = method->klass;
726 /* class might refer to a subclass of method's class */
727 while (!(klass == method->klass || (mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->container_class == method_container_class))) {
728 klass = klass->parent;
732 if (mono_class_is_ginst (klass) || mono_class_is_gtd (klass))
733 context.class_inst = mini_class_get_context (klass)->class_inst;
735 if (mono_class_is_ginst (klass))
736 g_assert (mono_class_has_parent_and_ignore_generics (mono_class_get_generic_class (klass)->container_class, method_container_class));
738 g_assert (mono_class_has_parent_and_ignore_generics (klass, method_container_class));
744 get_method_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
747 MonoGenericContext context;
750 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
751 return jinfo_get_method (ji);
752 context = get_generic_context_from_stack_frame (ji, generic_info);
754 method = jinfo_get_method (ji);
755 method = mono_method_get_declaring_generic_method (method);
756 method = mono_class_inflate_generic_method_checked (method, &context, &error);
757 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
763 * mono_exception_walk_native_trace:
764 * \param ex The exception object whose frames should be walked
765 * \param func callback to call for each stack frame
766 * \param user_data data passed to the callback
767 * This function walks the stacktrace of an exception. For
768 * each frame the callback function is called with the relevant info.
769 * The walk ends when no more stack frames are found or when the callback
770 * returns a TRUE value.
774 mono_exception_walk_trace (MonoException *ex, MonoExceptionFrameWalk func, gpointer user_data)
776 MONO_REQ_GC_UNSAFE_MODE;
778 MonoDomain *domain = mono_domain_get ();
779 MonoArray *ta = ex->trace_ips;
785 len = mono_array_length (ta) >> 1;
786 for (i = 0; i < len; i++) {
787 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
788 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
789 MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *)ip);
792 if (func (NULL, ip, 0, FALSE, user_data))
795 MonoMethod *method = get_method_from_stack_frame (ji, generic_info);
796 if (func (method, ji->code_start, (char *) ip - (char *) ji->code_start, TRUE, user_data))
805 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
808 MonoDomain *domain = mono_domain_get ();
810 MonoArray *ta = exc->trace_ips;
811 MonoDebugSourceLocation *location;
815 /* Exception is not thrown yet */
816 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
817 mono_error_set_pending_exception (&error);
821 len = mono_array_length (ta) >> 1;
823 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
824 if (mono_error_set_pending_exception (&error))
827 for (i = skip; i < len; i++) {
829 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
830 if (!mono_error_ok (&error)) {
831 mono_error_set_pending_exception (&error);
834 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
835 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
838 ji = mono_jit_info_table_find (domain, (char *)ip);
840 /* Unmanaged frame */
841 mono_array_setref (res, i, sf);
845 g_assert (ji != NULL);
848 /* Can't resolve actual method */
849 method = jinfo_get_method (ji);
851 method = get_method_from_stack_frame (ji, generic_info);
852 if (jinfo_get_method (ji)->wrapper_type) {
856 s = mono_method_get_name_full (method, TRUE, FALSE, MONO_TYPE_NAME_FORMAT_REFLECTION);
857 MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
861 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, method, NULL, &error);
862 if (!mono_error_ok (&error)) {
863 mono_error_set_pending_exception (&error);
866 MONO_OBJECT_SETREF (sf, method, rm);
869 sf->method_index = ji->from_aot ? mono_aot_find_method_index (method) : 0xffffff;
870 sf->method_address = (gsize) ji->code_start;
871 sf->native_offset = (char *)ip - (char *)ji->code_start;
874 * mono_debug_lookup_source_location() returns both the file / line number information
875 * and the IL offset. Note that computing the IL offset is already an expensive
876 * operation, so we shouldn't call this method twice.
878 location = mono_debug_lookup_source_location (jinfo_get_method (ji), sf->native_offset, domain);
880 sf->il_offset = location->il_offset;
883 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (ji), sf->native_offset, NULL, &sp))
884 sf->il_offset = sp.il_offset;
889 if (need_file_info) {
890 if (location && location->source_file) {
891 MONO_OBJECT_SETREF (sf, filename, mono_string_new (domain, location->source_file));
892 sf->line = location->row;
893 sf->column = location->column;
895 sf->line = sf->column = 0;
900 mono_debug_free_source_location (location);
901 mono_array_setref (res, i, sf);
908 mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
911 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
912 if (jit_tls && jit_tls->orig_ex_ctx_set)
913 start_ctx = &jit_tls->orig_ex_ctx;
915 mono_walk_stack_with_ctx (func, start_ctx, unwind_options, user_data);
918 * mono_walk_stack_with_ctx:
919 * Unwind the current thread starting at \p start_ctx.
920 * If \p start_ctx is null, we capture the current context.
923 mono_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
925 MonoContext extra_ctx;
926 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
927 MONO_ARCH_CONTEXT_DEF
929 if (!thread || !thread->jit_data)
933 mono_arch_flush_register_windows ();
935 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
936 MONO_INIT_CONTEXT_FROM_CURRENT (&extra_ctx);
938 MONO_INIT_CONTEXT_FROM_FUNC (&extra_ctx, mono_walk_stack_with_ctx);
940 start_ctx = &extra_ctx;
943 mono_walk_stack_full (func, start_ctx, mono_domain_get (), (MonoJitTlsData *)thread->jit_data, mono_get_lmf (), unwind_options, user_data);
947 * mono_walk_stack_with_state:
948 * Unwind a thread described by \p state.
950 * State must be valid (state->valid == TRUE).
952 * If you are using this function to unwind another thread, make sure it is suspended.
954 * If \p state is null, we capture the current context.
957 mono_walk_stack_with_state (MonoJitStackWalk func, MonoThreadUnwindState *state, MonoUnwindOptions unwind_options, void *user_data)
959 MonoThreadUnwindState extra_state;
961 g_assert (!mono_thread_info_is_async_context ());
962 if (!mono_thread_state_init_from_current (&extra_state))
964 state = &extra_state;
967 g_assert (state->valid);
969 if (!state->unwind_data [MONO_UNWIND_DATA_DOMAIN])
973 mono_walk_stack_full (func,
975 (MonoDomain *)state->unwind_data [MONO_UNWIND_DATA_DOMAIN],
976 (MonoJitTlsData *)state->unwind_data [MONO_UNWIND_DATA_JIT_TLS],
977 (MonoLMF *)state->unwind_data [MONO_UNWIND_DATA_LMF],
978 unwind_options, user_data);
982 mono_walk_stack (MonoJitStackWalk func, MonoUnwindOptions options, void *user_data)
984 MonoThreadUnwindState state;
985 if (!mono_thread_state_init_from_current (&state))
987 mono_walk_stack_with_state (func, &state, options, user_data);
991 * mono_walk_stack_full:
992 * \param func callback to call for each stack frame
993 * \param domain starting appdomain, can be NULL to use the current domain
994 * \param unwind_options what extra information the unwinder should gather
995 * \param start_ctx starting state of the stack walk, can be NULL.
996 * \param thread the thread whose stack to walk, can be NULL to use the current thread
997 * \param lmf the LMF of \p thread, can be NULL to use the LMF of the current thread
998 * \param user_data data passed to the callback
999 * This function walks the stack of a thread, starting from the state
1000 * represented by \p start_ctx. For each frame the callback
1001 * function is called with the relevant info. The walk ends when no more
1002 * managed stack frames are found or when the callback returns a TRUE value.
1005 mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data)
1008 MonoContext ctx, new_ctx;
1009 StackFrameInfo frame;
1011 mgreg_t *reg_locations [MONO_MAX_IREGS];
1012 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
1013 gboolean get_reg_locations = unwind_options & MONO_UNWIND_REG_LOCATIONS;
1014 gboolean async = mono_thread_info_is_async_context ();
1016 if (mono_llvm_only) {
1022 ips = get_unwind_backtrace ();
1023 for (l = ips; l; l = l->next) {
1024 guint8 *ip = (guint8*)l->data;
1025 memset (&frame, 0, sizeof (StackFrameInfo));
1026 frame.ji = mini_jit_info_table_find (domain, (char*)ip, &frame.domain);
1027 if (!frame.ji || frame.ji->is_trampoline)
1029 frame.type = FRAME_TYPE_MANAGED;
1030 frame.method = jinfo_get_method (frame.ji);
1031 // FIXME: Cannot lookup the actual method
1032 frame.actual_method = frame.method;
1033 if (frame.type == FRAME_TYPE_MANAGED) {
1034 if (!frame.method->wrapper_type || frame.method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
1035 frame.managed = TRUE;
1037 frame.native_offset = ip - (guint8*)frame.ji->code_start;
1038 frame.il_offset = -1;
1040 if (func (&frame, NULL, user_data))
1047 g_assert (start_ctx);
1050 /*The LMF will be null if the target have no managed frames.*/
1051 /* g_assert (lmf); */
1054 g_assert (unwind_options == MONO_UNWIND_NONE);
1056 memcpy (&ctx, start_ctx, sizeof (MonoContext));
1057 memset (reg_locations, 0, sizeof (reg_locations));
1059 while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
1061 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, get_reg_locations ? new_reg_locations : NULL, &frame);
1065 if ((unwind_options & MONO_UNWIND_LOOKUP_IL_OFFSET) && frame.ji && !frame.ji->is_trampoline) {
1066 MonoDebugSourceLocation *source;
1068 source = mono_debug_lookup_source_location (jinfo_get_method (frame.ji), frame.native_offset, domain);
1070 il_offset = source->il_offset;
1073 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (frame.ji), frame.native_offset, NULL, &sp))
1074 il_offset = sp.il_offset;
1078 mono_debug_free_source_location (source);
1082 frame.il_offset = il_offset;
1084 if ((unwind_options & MONO_UNWIND_LOOKUP_ACTUAL_METHOD) && frame.ji && !frame.ji->is_trampoline) {
1085 frame.actual_method = get_method_from_stack_frame (frame.ji, get_generic_info_from_stack_frame (frame.ji, &ctx));
1087 frame.actual_method = frame.method;
1090 if (get_reg_locations)
1091 frame.reg_locations = reg_locations;
1093 if (func (&frame, &ctx, user_data))
1096 if (get_reg_locations) {
1097 for (i = 0; i < MONO_MAX_IREGS; ++i)
1098 if (new_reg_locations [i])
1099 reg_locations [i] = new_reg_locations [i];
1107 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
1108 MonoReflectionMethod **method,
1109 gint32 *iloffset, gint32 *native_offset,
1110 MonoString **file, gint32 *line, gint32 *column)
1113 MonoDomain *domain = mono_domain_get ();
1114 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1115 MonoLMF *lmf = mono_get_lmf ();
1116 MonoJitInfo *ji = NULL;
1117 MonoContext ctx, new_ctx;
1118 MonoDebugSourceLocation *location;
1119 MonoMethod *jmethod = NULL, *actual_method;
1120 StackFrameInfo frame;
1123 MONO_ARCH_CONTEXT_DEF;
1125 if (mono_llvm_only) {
1127 MonoDomain *frame_domain;
1128 guint8 *frame_ip = NULL;
1130 /* FIXME: Generalize this code with an interface which returns an array of StackFrame structures */
1132 ips = get_unwind_backtrace ();
1133 for (l = ips; l && skip >= 0; l = l->next) {
1134 guint8 *ip = (guint8*)l->data;
1138 ji = mini_jit_info_table_find (mono_domain_get (), (char*)ip, &frame_domain);
1139 if (!ji || ji->is_trampoline)
1142 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1143 jmethod = jinfo_get_method (ji);
1144 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1151 /* No way to resolve generic instances */
1152 actual_method = jmethod;
1153 *native_offset = frame_ip - (guint8*)ji->code_start;
1155 mono_arch_flush_register_windows ();
1157 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
1158 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
1160 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
1166 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1170 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1171 frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1172 frame.type == FRAME_TYPE_TRAMPOLINE)
1176 *native_offset = frame.native_offset;
1178 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1179 jmethod = jinfo_get_method (ji);
1180 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1183 } while (skip >= 0);
1185 actual_method = get_method_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, &ctx));
1188 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, actual_method, NULL, &error);
1189 if (!mono_error_ok (&error)) {
1190 mono_error_set_pending_exception (&error);
1193 mono_gc_wbarrier_generic_store (method, (MonoObject*) rm);
1195 location = mono_debug_lookup_source_location (jmethod, *native_offset, domain);
1197 *iloffset = location->il_offset;
1201 if (need_file_info) {
1203 mono_gc_wbarrier_generic_store (file, (MonoObject*) mono_string_new (domain, location->source_file));
1204 *line = location->row;
1205 *column = location->column;
1208 *line = *column = 0;
1212 mono_debug_free_source_location (location);
1218 get_exception_catch_class (MonoJitExceptionInfo *ei, MonoJitInfo *ji, MonoContext *ctx)
1221 MonoClass *catch_class = ei->data.catch_class;
1222 MonoType *inflated_type;
1223 MonoGenericContext context;
1225 /*MonoJitExceptionInfo::data is an union used by filter and finally clauses too.*/
1226 if (!catch_class || ei->flags != MONO_EXCEPTION_CLAUSE_NONE)
1229 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
1231 context = get_generic_context_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, ctx));
1233 /* FIXME: we shouldn't inflate but instead put the
1234 type in the rgctx and fetch it from there. It
1235 might be a good idea to do this lazily, i.e. only
1236 when the exception is actually thrown, so as not to
1237 waste space for exception clauses which might never
1239 inflated_type = mono_class_inflate_generic_type_checked (&catch_class->byval_arg, &context, &error);
1240 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
1242 catch_class = mono_class_from_mono_type (inflated_type);
1243 mono_metadata_free_type (inflated_type);
1249 * mini_jit_info_table_find_ext:
1251 * Same as mono_jit_info_table_find, but search all the domains of the current thread
1252 * if ADDR is not found in DOMAIN. The domain where the method was found is stored into
1253 * OUT_DOMAIN if it is not NULL.
1256 mini_jit_info_table_find_ext (MonoDomain *domain, char *addr, gboolean allow_trampolines, MonoDomain **out_domain)
1259 MonoInternalThread *t = mono_thread_internal_current ();
1265 ji = mono_jit_info_table_find_internal (domain, addr, TRUE, allow_trampolines);
1268 *out_domain = domain;
1272 /* maybe it is shared code, so we also search in the root domain */
1273 if (domain != mono_get_root_domain ()) {
1274 ji = mono_jit_info_table_find_internal (mono_get_root_domain (), addr, TRUE, allow_trampolines);
1277 *out_domain = mono_get_root_domain ();
1285 refs = (gpointer *)((t->appdomain_refs) ? *(gpointer *) t->appdomain_refs : NULL);
1286 for (; refs && *refs; refs++) {
1287 if (*refs != domain && *refs != mono_get_root_domain ()) {
1288 ji = mono_jit_info_table_find_internal ((MonoDomain*) *refs, addr, TRUE, allow_trampolines);
1291 *out_domain = (MonoDomain*) *refs;
1301 mini_jit_info_table_find (MonoDomain *domain, char *addr, MonoDomain **out_domain)
1303 return mini_jit_info_table_find_ext (domain, addr, FALSE, out_domain);
1306 /* Class lazy loading functions */
1307 static GENERATE_GET_CLASS_WITH_CACHE (runtime_compat_attr, "System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute")
1310 * wrap_non_exception_throws:
1312 * Determine whenever M's assembly has a RuntimeCompatibilityAttribute with the
1313 * WrapNonExceptionThrows flag set.
1316 wrap_non_exception_throws (MonoMethod *m)
1319 MonoAssembly *ass = m->klass->image->assembly;
1320 MonoCustomAttrInfo* attrs;
1323 gboolean val = FALSE;
1326 if (ass->wrap_non_exception_throws_inited)
1327 return ass->wrap_non_exception_throws;
1329 klass = mono_class_get_runtime_compat_attr_class ();
1331 attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, &error);
1332 mono_error_cleanup (&error); /* FIXME don't swallow the error */
1334 for (i = 0; i < attrs->num_attrs; ++i) {
1335 MonoCustomAttrEntry *attr = &attrs->attrs [i];
1337 int num_named, named_type, name_len;
1340 if (!attr->ctor || attr->ctor->klass != klass)
1342 /* Decode the RuntimeCompatibilityAttribute. See reflection.c */
1343 p = (const char*)attr->data;
1344 g_assert (read16 (p) == 0x0001);
1346 num_named = read16 (p);
1352 /* data_type = *p; */
1355 if (named_type != 0x54)
1357 name_len = mono_metadata_decode_blob_size (p, &p);
1358 name = (char *)g_malloc (name_len + 1);
1359 memcpy (name, p, name_len);
1360 name [name_len] = 0;
1362 g_assert (!strcmp (name, "WrapNonExceptionThrows"));
1364 /* The value is a BOOLEAN */
1367 mono_custom_attrs_free (attrs);
1370 ass->wrap_non_exception_throws = val;
1371 mono_memory_barrier ();
1372 ass->wrap_non_exception_throws_inited = TRUE;
1377 #define MAX_UNMANAGED_BACKTRACE 128
1379 build_native_trace (MonoError *error)
1382 /* This puppy only makes sense on mobile, IOW, ARM. */
1383 #if defined (HAVE_BACKTRACE_SYMBOLS) && defined (TARGET_ARM)
1385 void *native_trace [MAX_UNMANAGED_BACKTRACE];
1388 size = backtrace (native_trace, MAX_UNMANAGED_BACKTRACE);
1394 res = mono_array_new_checked (mono_domain_get (), mono_defaults.int_class, size, error);
1395 return_val_if_nok (error, NULL);
1397 for (i = 0; i < size; i++)
1398 mono_array_set (res, gpointer, i, native_trace [i]);
1406 setup_stack_trace (MonoException *mono_ex, GSList *dynamic_methods, GList **trace_ips)
1409 *trace_ips = g_list_reverse (*trace_ips);
1411 MonoArray *ips_arr = mono_glist_to_array (*trace_ips, mono_defaults.int_class, &error);
1412 mono_error_assert_ok (&error);
1413 MONO_OBJECT_SETREF (mono_ex, trace_ips, ips_arr);
1414 MONO_OBJECT_SETREF (mono_ex, native_trace_ips, build_native_trace (&error));
1415 mono_error_assert_ok (&error);
1416 if (dynamic_methods) {
1417 /* These methods could go away anytime, so save a reference to them in the exception object */
1419 MonoMList *list = NULL;
1421 for (l = dynamic_methods; l; l = l->next) {
1423 MonoDomain *domain = mono_domain_get ();
1425 if (domain->method_to_dyn_method) {
1426 mono_domain_lock (domain);
1427 dis_link = (guint32)(size_t)g_hash_table_lookup (domain->method_to_dyn_method, l->data);
1428 mono_domain_unlock (domain);
1430 MonoObject *o = mono_gchandle_get_target (dis_link);
1432 list = mono_mlist_prepend_checked (list, o, &error);
1433 mono_error_assert_ok (&error);
1439 MONO_OBJECT_SETREF (mono_ex, dynamic_methods, list);
1442 g_list_free (*trace_ips);
1447 * mono_handle_exception_internal_first_pass:
1449 * The first pass of exception handling. Unwind the stack until a catch clause which can catch
1450 * OBJ is found. Run the index of the filter clause which caught the exception into
1451 * OUT_FILTER_IDX. Return TRUE if the exception is caught, FALSE otherwise.
1454 mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoJitInfo **out_prev_ji, MonoObject *non_exception)
1457 MonoDomain *domain = mono_domain_get ();
1458 MonoJitInfo *ji = NULL;
1459 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1460 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1461 MonoLMF *lmf = mono_get_lmf ();
1462 GList *trace_ips = NULL;
1463 GSList *dynamic_methods = NULL;
1464 MonoException *mono_ex;
1465 gboolean stack_overflow = FALSE;
1466 MonoContext initial_ctx;
1468 int frame_count = 0;
1473 g_assert (ctx != NULL);
1475 if (obj == (MonoObject *)domain->stack_overflow_ex)
1476 stack_overflow = TRUE;
1478 mono_ex = (MonoException*)obj;
1479 MonoArray *initial_trace_ips = mono_ex->trace_ips;
1480 if (initial_trace_ips) {
1481 int len = mono_array_length (initial_trace_ips) >> 1;
1483 for (i = 0; i < (len - 1); i++) {
1484 gpointer ip = mono_array_get (initial_trace_ips, gpointer, i * 2 + 0);
1485 gpointer generic_info = mono_array_get (initial_trace_ips, gpointer, i * 2 + 1);
1486 trace_ips = g_list_prepend (trace_ips, ip);
1487 trace_ips = g_list_prepend (trace_ips, generic_info);
1491 if (!mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1492 mono_error_assert_ok (&error);
1497 call_filter = (int (*) (MonoContext *, void *))mono_get_call_filter ();
1499 g_assert (jit_tls->end_of_stack);
1500 g_assert (jit_tls->abort_func);
1503 *out_filter_idx = -1;
1507 *out_prev_ji = NULL;
1512 MonoContext new_ctx;
1514 int clause_index_start = 0;
1515 gboolean unwind_res = TRUE;
1517 StackFrameInfo frame;
1522 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1524 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1525 frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1526 frame.type == FRAME_TYPE_TRAMPOLINE) {
1530 g_assert (frame.type == FRAME_TYPE_MANAGED);
1535 setup_stack_trace (mono_ex, dynamic_methods, &trace_ips);
1536 g_slist_free (dynamic_methods);
1541 method = jinfo_get_method (ji);
1542 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1544 if (mini_get_debug_options ()->reverse_pinvoke_exceptions && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
1545 g_error ("A native frame was found while unwinding the stack after an exception.\n"
1546 "The native frame called the managed method:\n%s\n",
1547 mono_method_full_name (method, TRUE));
1550 if (method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1551 // avoid giant stack traces during a stack overflow
1552 if (frame_count < 1000) {
1553 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1554 trace_ips = g_list_prepend (trace_ips, get_generic_info_from_stack_frame (ji, ctx));
1558 if (method->dynamic)
1559 dynamic_methods = g_slist_prepend (dynamic_methods, method);
1561 if (stack_overflow) {
1562 if (MONO_ARCH_STACK_GROWS_UP)
1563 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1565 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1567 free_stack = 0xffffff;
1570 for (i = clause_index_start; i < ji->num_clauses; i++) {
1571 MonoJitExceptionInfo *ei = &ji->clauses [i];
1572 gboolean filtered = FALSE;
1575 * During stack overflow, wait till the unwinding frees some stack
1576 * space before running handlers/finalizers.
1578 if (free_stack <= (64 * 1024))
1581 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1583 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1586 * Have to unwrap RuntimeWrappedExceptions if the
1587 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1589 if (non_exception && !wrap_non_exception_throws (method))
1590 ex_obj = non_exception;
1594 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1595 #ifndef DISABLE_PERFCOUNTERS
1596 mono_perfcounters->exceptions_filters++;
1599 #ifndef MONO_CROSS_COMPILE
1600 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1602 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1604 /* Can't pass the ex object in a register yet to filter clauses, because call_filter () might not support it */
1605 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1607 g_assert (!ji->from_llvm);
1608 /* store the exception object in bp + ei->exvar_offset */
1609 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1613 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
1615 * Pass the original il clause index to the landing pad so it can
1616 * branch to the landing pad associated with the il clause.
1617 * This is needed because llvm compiled code assumes that the EH
1618 * code always branches to the innermost landing pad.
1621 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
1624 mono_debugger_agent_begin_exception_filter (mono_ex, ctx, &initial_ctx);
1625 filtered = call_filter (ctx, ei->data.filter);
1626 mono_debugger_agent_end_exception_filter (mono_ex, ctx, &initial_ctx);
1627 if (filtered && out_filter_idx)
1628 *out_filter_idx = filter_idx;
1634 setup_stack_trace (mono_ex, dynamic_methods, &trace_ips);
1635 g_slist_free (dynamic_methods);
1636 /* mono_debugger_agent_handle_exception () needs this */
1637 mini_set_abort_threshold (ctx);
1638 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1643 MonoError isinst_error;
1644 error_init (&isinst_error);
1645 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (ex_obj, catch_class, &error)) {
1646 setup_stack_trace (mono_ex, dynamic_methods, &trace_ips);
1647 g_slist_free (dynamic_methods);
1652 /* mono_debugger_agent_handle_exception () needs this */
1653 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1656 mono_error_cleanup (&isinst_error);
1663 g_assert_not_reached ();
1667 * mono_handle_exception_internal:
1668 * \param ctx saved processor state
1669 * \param obj the exception object
1670 * \param resume whenever to resume unwinding based on the state in \c MonoJitTlsData.
1673 mono_handle_exception_internal (MonoContext *ctx, MonoObject *obj, gboolean resume, MonoJitInfo **out_ji)
1676 MonoDomain *domain = mono_domain_get ();
1677 MonoJitInfo *ji, *prev_ji;
1678 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1679 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1680 MonoLMF *lmf = mono_get_lmf ();
1681 MonoException *mono_ex;
1682 gboolean stack_overflow = FALSE;
1683 MonoContext initial_ctx;
1685 int frame_count = 0;
1686 gint32 filter_idx, first_filter_idx = 0;
1689 MonoObject *non_exception = NULL;
1691 g_assert (ctx != NULL);
1693 MonoException *ex = mono_get_exception_null_reference ();
1694 MONO_OBJECT_SETREF (ex, message, mono_string_new (domain, "Object reference not set to an instance of an object"));
1695 obj = (MonoObject *)ex;
1699 * Allocate a new exception object instead of the preconstructed ones.
1701 if (obj == (MonoObject *)domain->stack_overflow_ex) {
1703 * It is not a good idea to try and put even more pressure on the little stack available.
1704 * obj = mono_get_exception_stack_overflow ();
1706 stack_overflow = TRUE;
1708 else if (obj == (MonoObject *)domain->null_reference_ex) {
1709 obj = (MonoObject *)mono_get_exception_null_reference ();
1712 if (!mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1713 mono_error_assert_ok (&error);
1714 non_exception = obj;
1715 obj = (MonoObject *)mono_get_exception_runtime_wrapped_checked (obj, &error);
1716 mono_error_assert_ok (&error);
1719 mono_ex = (MonoException*)obj;
1721 if (mini_get_debug_options ()->suspend_on_exception) {
1722 mono_runtime_printf_err ("Exception thrown, suspending...");
1727 if (mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1728 mono_ex = (MonoException*)obj;
1730 mono_error_assert_ok (&error);
1734 if (mono_ex && jit_tls->class_cast_from) {
1735 if (!strcmp (mono_ex->object.vtable->klass->name, "InvalidCastException")) {
1736 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1737 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1738 char *msg = g_strdup_printf ("Unable to cast object of type '%s' to type '%s'.", from_name, to_name);
1739 mono_ex->message = mono_string_new (domain, msg);
1744 if (!strcmp (mono_ex->object.vtable->klass->name, "ArrayTypeMismatchException")) {
1745 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1746 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1747 char *msg = g_strdup_printf ("Source array of type '%s' cannot be cast to destination array type '%s'.", from_name, to_name);
1748 mono_ex->message = mono_string_new (domain, msg);
1756 call_filter = (int (*)(MonoContext *, void*))mono_get_call_filter ();
1758 g_assert (jit_tls->end_of_stack);
1759 g_assert (jit_tls->abort_func);
1762 * We set orig_ex_ctx_set to TRUE/FALSE around profiler calls to make sure it doesn't
1763 * end up being TRUE on any code path.
1765 memcpy (&jit_tls->orig_ex_ctx, ctx, sizeof (MonoContext));
1770 MonoContext ctx_cp = *ctx;
1771 if (mono_trace_is_enabled ()) {
1772 MonoMethod *system_exception_get_message = mono_class_get_method_from_name (mono_defaults.exception_class, "get_Message", 0);
1773 MonoMethod *get_message = system_exception_get_message == NULL ? NULL : mono_object_get_virtual_method (obj, system_exception_get_message);
1774 MonoObject *message;
1775 const char *type_name = mono_class_get_name (mono_object_class (mono_ex));
1777 if (get_message == NULL) {
1779 } else if (!strcmp (type_name, "OutOfMemoryException") || !strcmp (type_name, "StackOverflowException")) {
1781 msg = g_strdup_printf ("(No exception message for: %s)\n", type_name);
1783 MonoObject *exc = NULL;
1784 message = mono_runtime_try_invoke (get_message, obj, NULL, &exc, &error);
1785 g_assert (exc == NULL);
1786 mono_error_assert_ok (&error);
1790 msg = mono_string_to_utf8_checked ((MonoString *) message, &error);
1791 if (!is_ok (&error)) {
1792 mono_error_cleanup (&error);
1793 msg = g_strdup ("(error while display System.Exception.Message property)");
1796 msg = g_strdup ("(System.Exception.Message property not available)");
1799 g_print ("[%p:] EXCEPTION handling: %s.%s: %s\n", (void*)mono_native_thread_id_get (), mono_object_class (obj)->name_space, mono_object_class (obj)->name, msg);
1801 if (mono_ex && mono_trace_eval_exception (mono_object_class (mono_ex)))
1802 mono_print_thread_dump_from_ctx (ctx);
1804 jit_tls->orig_ex_ctx_set = TRUE;
1805 mono_profiler_exception_thrown (obj);
1806 jit_tls->orig_ex_ctx_set = FALSE;
1808 res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, &first_filter_idx, &ji, &prev_ji, non_exception);
1811 if (mini_get_debug_options ()->break_on_exc)
1813 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1815 if (mini_get_debug_options ()->suspend_on_unhandled) {
1816 mono_runtime_printf_err ("Unhandled exception, suspending...");
1821 // FIXME: This runs managed code so it might cause another stack overflow when
1822 // we are handling a stack overflow
1823 mini_set_abort_threshold (ctx);
1824 mono_unhandled_exception (obj);
1826 gboolean unhandled = FALSE;
1829 * The exceptions caught by the mono_runtime_invoke_checked () calls
1830 * in the threadpool needs to be treated as unhandled (#669836).
1832 * FIXME: The check below is hackish, but its hard to distinguish
1833 * these runtime invoke calls from others in the runtime.
1835 if (ji && jinfo_get_method (ji)->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1836 if (prev_ji && jinfo_get_method (prev_ji) == mono_defaults.threadpool_perform_wait_callback_method)
1841 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1843 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, &ctx_cp);
1853 MonoContext new_ctx;
1855 int clause_index_start = 0;
1856 gboolean unwind_res = TRUE;
1860 ji = jit_tls->resume_state.ji;
1861 new_ctx = jit_tls->resume_state.new_ctx;
1862 clause_index_start = jit_tls->resume_state.clause_index;
1863 lmf = jit_tls->resume_state.lmf;
1864 first_filter_idx = jit_tls->resume_state.first_filter_idx;
1865 filter_idx = jit_tls->resume_state.filter_idx;
1867 StackFrameInfo frame;
1869 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1871 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1872 frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1873 frame.type == FRAME_TYPE_TRAMPOLINE) {
1877 g_assert (frame.type == FRAME_TYPE_MANAGED);
1883 *(mono_get_lmf_addr ()) = lmf;
1885 jit_tls->abort_func (obj);
1886 g_assert_not_reached ();
1889 method = jinfo_get_method (ji);
1891 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1893 if (stack_overflow) {
1894 if (MONO_ARCH_STACK_GROWS_UP)
1895 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1897 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1899 free_stack = 0xffffff;
1902 for (i = clause_index_start; i < ji->num_clauses; i++) {
1903 MonoJitExceptionInfo *ei = &ji->clauses [i];
1904 gboolean filtered = FALSE;
1907 * During stack overflow, wait till the unwinding frees some stack
1908 * space before running handlers/finalizers.
1910 if (free_stack <= (64 * 1024))
1913 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1915 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1918 * Have to unwrap RuntimeWrappedExceptions if the
1919 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1921 if (non_exception && !wrap_non_exception_throws (method))
1922 ex_obj = non_exception;
1926 if (((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER))) {
1927 #ifndef MONO_CROSS_COMPILE
1928 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1929 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1931 g_assert (!ji->from_llvm);
1932 /* store the exception object in bp + ei->exvar_offset */
1933 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1938 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
1940 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
1943 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1945 * Filter clauses should only be run in the
1946 * first pass of exception handling.
1948 filtered = (filter_idx == first_filter_idx);
1952 error_init (&error);
1953 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1954 mono_object_isinst_checked (ex_obj, catch_class, &error)) || filtered) {
1956 * This guards against the situation that we abort a thread that is executing a finally clause
1957 * that was called by the EH machinery. It won't have a guard trampoline installed, so we must
1958 * check for this situation here and resume interruption if we are below the guarded block.
1960 if (G_UNLIKELY (jit_tls->handler_block_return_address)) {
1961 gboolean is_outside = FALSE;
1962 gpointer prot_bp = MONO_CONTEXT_GET_BP (&jit_tls->handler_block_context);
1963 gpointer catch_bp = MONO_CONTEXT_GET_BP (ctx);
1964 //FIXME make this stack direction aware
1965 if (catch_bp > prot_bp) {
1967 } else if (catch_bp == prot_bp) {
1968 /* Can be either try { try { } catch {} } finally {} or try { try { } finally {} } catch {}
1969 * So we check if the catch handler_start is protected by the guarded handler protected region
1972 * If there is an outstanding guarded_block return address, it means the current thread must be aborted.
1973 * This is the only way to reach out the guarded block as other cases are handled by the trampoline.
1974 * There aren't any further finally/fault handler blocks down the stack over this exception.
1975 * This must be ensured by the code that installs the guard trampoline.
1977 g_assert (ji == mini_jit_info_table_find (domain, (char *)MONO_CONTEXT_GET_IP (&jit_tls->handler_block_context), NULL));
1979 if (!is_address_protected (ji, jit_tls->handler_block, ei->handler_start)) {
1984 jit_tls->handler_block_return_address = NULL;
1985 jit_tls->handler_block = NULL;
1986 mono_thread_resume_interruption (TRUE); /*We ignore the exception here, it will be raised later*/
1990 if (mono_trace_is_enabled () && mono_trace_eval (method))
1991 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1992 jit_tls->orig_ex_ctx_set = TRUE;
1993 mono_profiler_exception_clause_handler (method, ei->flags, i);
1994 jit_tls->orig_ex_ctx_set = FALSE;
1995 mini_set_abort_threshold (ctx);
1996 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1998 #ifndef DISABLE_PERFCOUNTERS
1999 mono_perfcounters->exceptions_depth += frame_count;
2001 if (obj == (MonoObject *)domain->stack_overflow_ex)
2002 jit_tls->handling_stack_ovf = FALSE;
2006 mono_error_cleanup (&error);
2007 if (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
2008 if (mono_trace_is_enabled () && mono_trace_eval (method))
2009 g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (method, TRUE));
2010 jit_tls->orig_ex_ctx_set = TRUE;
2011 mono_profiler_exception_clause_handler (method, ei->flags, i);
2012 jit_tls->orig_ex_ctx_set = FALSE;
2013 mini_set_abort_threshold (ctx);
2014 call_filter (ctx, ei->handler_start);
2016 if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
2017 if (mono_trace_is_enabled () && mono_trace_eval (method))
2018 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (method, TRUE));
2019 jit_tls->orig_ex_ctx_set = TRUE;
2020 mono_profiler_exception_clause_handler (method, ei->flags, i);
2021 jit_tls->orig_ex_ctx_set = FALSE;
2022 #ifndef DISABLE_PERFCOUNTERS
2023 mono_perfcounters->exceptions_finallys++;
2026 if (ji->from_llvm) {
2028 * LLVM compiled finally handlers follow the design
2029 * of the c++ ehabi, i.e. they call a resume function
2030 * at the end instead of returning to the caller.
2031 * So save the exception handling state,
2032 * mono_resume_unwind () will call us again to continue
2035 jit_tls->resume_state.ex_obj = obj;
2036 jit_tls->resume_state.ji = ji;
2037 jit_tls->resume_state.clause_index = i + 1;
2038 jit_tls->resume_state.ctx = *ctx;
2039 jit_tls->resume_state.new_ctx = new_ctx;
2040 jit_tls->resume_state.lmf = lmf;
2041 jit_tls->resume_state.first_filter_idx = first_filter_idx;
2042 jit_tls->resume_state.filter_idx = filter_idx;
2043 mini_set_abort_threshold (ctx);
2044 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
2047 mini_set_abort_threshold (ctx);
2048 call_filter (ctx, ei->handler_start);
2054 jit_tls->orig_ex_ctx_set = TRUE;
2055 mono_profiler_exception_method_leave (method);
2056 jit_tls->orig_ex_ctx_set = FALSE;
2061 g_assert_not_reached ();
2065 * mono_debugger_run_finally:
2066 * \param start_ctx saved processor state
2067 * This method is called by the Mono Debugger to call all \c finally clauses of the
2068 * current stack frame. It's used when the user issues a \c return command to make
2069 * the current stack frame return. After returning from this method, the debugger
2070 * unwinds the stack one frame and gives control back to the user.
2071 * NOTE: This method is only used when running inside the Mono Debugger.
2074 mono_debugger_run_finally (MonoContext *start_ctx)
2076 static int (*call_filter) (MonoContext *, gpointer) = NULL;
2077 MonoDomain *domain = mono_domain_get ();
2078 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2079 MonoLMF *lmf = mono_get_lmf ();
2080 MonoContext ctx, new_ctx;
2081 MonoJitInfo *ji, rji;
2086 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
2087 if (!ji || ji == (gpointer)-1)
2091 call_filter = (int (*)(MonoContext *, void *))mono_get_call_filter ();
2093 for (i = 0; i < ji->num_clauses; i++) {
2094 MonoJitExceptionInfo *ei = &ji->clauses [i];
2096 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (&ctx)) &&
2097 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
2098 call_filter (&ctx, ei->handler_start);
2104 * mono_handle_exception:
2105 * \param ctx saved processor state
2106 * \param obj the exception object
2109 mono_handle_exception (MonoContext *ctx, MonoObject *obj)
2111 MONO_REQ_GC_UNSAFE_MODE;
2113 #ifndef DISABLE_PERFCOUNTERS
2114 mono_perfcounters->exceptions_thrown++;
2117 return mono_handle_exception_internal (ctx, obj, FALSE, NULL);
2120 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2122 #ifndef MONO_ARCH_USE_SIGACTION
2123 #error "Can't use sigaltstack without sigaction"
2126 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
2129 mono_setup_altstack (MonoJitTlsData *tls)
2133 guint8 *staddr = NULL;
2135 if (mono_running_on_valgrind ())
2138 mono_thread_info_get_stack_bounds (&staddr, &stsize);
2142 tls->end_of_stack = staddr + stsize;
2143 tls->stack_size = stsize;
2145 /*g_print ("thread %p, stack_base: %p, stack_size: %d\n", (gpointer)pthread_self (), staddr, stsize);*/
2147 tls->stack_ovf_guard_base = staddr + mono_pagesize ();
2148 tls->stack_ovf_guard_size = ALIGN_TO (8 * 4096, mono_pagesize ());
2150 g_assert ((guint8*)&sa >= (guint8*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size);
2152 if (mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE)) {
2153 /* mprotect can fail for the main thread stack */
2154 gpointer gaddr = mono_valloc (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE|MONO_MMAP_PRIVATE|MONO_MMAP_ANON|MONO_MMAP_FIXED, MONO_MEM_ACCOUNT_EXCEPTIONS);
2155 g_assert (gaddr == tls->stack_ovf_guard_base);
2156 tls->stack_ovf_valloced = TRUE;
2159 /* Setup an alternate signal stack */
2160 tls->signal_stack = mono_valloc (0, MONO_ARCH_SIGNAL_STACK_SIZE, MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_PRIVATE|MONO_MMAP_ANON, MONO_MEM_ACCOUNT_EXCEPTIONS);
2161 tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2163 g_assert (tls->signal_stack);
2165 sa.ss_sp = tls->signal_stack;
2166 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2168 g_assert (sigaltstack (&sa, NULL) == 0);
2170 mono_gc_register_altstack ((char*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size, (char*)staddr + stsize - ((char*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size), tls->signal_stack, tls->signal_stack_size);
2174 mono_free_altstack (MonoJitTlsData *tls)
2179 sa.ss_sp = tls->signal_stack;
2180 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2181 sa.ss_flags = SS_DISABLE;
2182 err = sigaltstack (&sa, NULL);
2183 g_assert (err == 0);
2185 if (tls->signal_stack)
2186 mono_vfree (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE, MONO_MEM_ACCOUNT_EXCEPTIONS);
2187 if (tls->stack_ovf_valloced)
2188 mono_vfree (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MEM_ACCOUNT_EXCEPTIONS);
2190 mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_READ|MONO_MMAP_WRITE);
2193 #else /* !MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2196 mono_setup_altstack (MonoJitTlsData *tls)
2201 mono_free_altstack (MonoJitTlsData *tls)
2205 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2208 try_restore_stack_protection (MonoJitTlsData *jit_tls, int extra_bytes)
2210 gint32 unprotect_size = jit_tls->stack_ovf_guard_size;
2211 /* we need to leave some room for throwing the exception */
2212 while (unprotect_size >= 0 && (char*)jit_tls->stack_ovf_guard_base + unprotect_size > ((char*)&unprotect_size - extra_bytes))
2213 unprotect_size -= mono_pagesize ();
2214 /* at this point we could try and build a new domain->stack_overflow_ex, but only if there
2215 * is sufficient stack
2217 //fprintf (stderr, "restoring stack protection: %p-%p (%d)\n", jit_tls->stack_ovf_guard_base, (char*)jit_tls->stack_ovf_guard_base + unprotect_size, unprotect_size);
2219 mono_mprotect (jit_tls->stack_ovf_guard_base, unprotect_size, MONO_MMAP_NONE);
2220 return unprotect_size == jit_tls->stack_ovf_guard_size;
2223 static G_GNUC_UNUSED void
2224 try_more_restore (void)
2226 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2227 if (try_restore_stack_protection (jit_tls, 500))
2228 jit_tls->restore_stack_prot = NULL;
2231 static G_GNUC_UNUSED void
2232 restore_stack_protection (void)
2234 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2235 MonoException *ex = mono_domain_get ()->stack_overflow_ex;
2236 /* if we can't restore the stack protection, keep a callback installed so
2237 * we'll try to restore as much stack as we can at each return from unmanaged
2240 if (try_restore_stack_protection (jit_tls, 4096))
2241 jit_tls->restore_stack_prot = NULL;
2243 jit_tls->restore_stack_prot = try_more_restore_tramp;
2244 /* here we also throw a stack overflow exception */
2245 ex->trace_ips = NULL;
2246 ex->stack_trace = NULL;
2247 mono_raise_exception (ex);
2251 mono_altstack_restore_prot (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
2253 MONO_REQ_GC_UNSAFE_MODE;
2255 void (*func)(void) = (void (*)(void))tramp_data;
2261 mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, guint8* fault_addr)
2266 /* we got a stack overflow in the soft-guard pages
2267 * There are two cases:
2268 * 1) managed code caused the overflow: we unprotect the soft-guard page
2269 * and let the arch-specific code trigger the exception handling mechanism
2270 * in the thread stack. The soft-guard pages will be protected again as the stack is unwound.
2271 * 2) unmanaged code caused the overflow: we unprotect the soft-guard page
2272 * and hope we can continue with those enabled, at least until the hard-guard page
2273 * is hit. The alternative to continuing here is to just print a message and abort.
2274 * We may add in the future the code to protect the pages again in the codepath
2275 * when we return from unmanaged to managed code.
2277 if (jit_tls->stack_ovf_guard_size && fault_addr >= (guint8*)jit_tls->stack_ovf_guard_base &&
2278 fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size) {
2279 /* we unprotect the minimum amount we can */
2281 gboolean handled = FALSE;
2283 guard_size = jit_tls->stack_ovf_guard_size - (mono_pagesize () * SIZEOF_VOID_P / 4);
2284 while (guard_size && fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + guard_size) {
2285 guard_size -= mono_pagesize ();
2287 guard_size = jit_tls->stack_ovf_guard_size - guard_size;
2288 /*fprintf (stderr, "unprotecting: %d\n", guard_size);*/
2289 mono_mprotect ((char*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size - guard_size, guard_size, MONO_MMAP_READ|MONO_MMAP_WRITE);
2290 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2292 mono_arch_handle_altstack_exception (ctx, siginfo, fault_addr, TRUE);
2297 /* We print a message: after this even managed stack overflows
2298 * may crash the runtime
2300 mono_runtime_printf_err ("Stack overflow in unmanaged: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2301 if (!jit_tls->handling_stack_ovf) {
2302 jit_tls->restore_stack_prot = restore_stack_protection_tramp;
2303 jit_tls->handling_stack_ovf = 1;
2305 /*fprintf (stderr, "Already handling stack overflow\n");*/
2314 MonoMethod *omethod;
2316 } PrintOverflowUserData;
2318 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2320 print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2322 MonoMethod *method = NULL;
2323 PrintOverflowUserData *user_data = (PrintOverflowUserData *)data;
2326 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2327 method = jinfo_get_method (frame->ji);
2330 if (user_data->count == 0) {
2331 /* The first frame is in its prolog, so a line number cannot be computed */
2332 user_data->count ++;
2336 /* If this is a one method overflow, skip the other instances */
2337 if (method == user_data->omethod)
2340 location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2341 mono_runtime_printf_err (" %s", location);
2344 if (user_data->count == 1) {
2345 mono_runtime_printf_err (" <...>");
2346 user_data->omethod = method;
2348 user_data->omethod = NULL;
2351 user_data->count ++;
2353 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2360 mono_handle_hard_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr)
2362 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2363 PrintOverflowUserData ud;
2367 /* we don't do much now, but we can warn the user with a useful message */
2368 mono_runtime_printf_err ("Stack overflow: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2370 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2371 mono_sigctx_to_monoctx (ctx, &mctx);
2373 mono_runtime_printf_err ("Stacktrace:");
2375 memset (&ud, 0, sizeof (ud));
2377 mono_walk_stack_with_ctx (print_overflow_stack_frame, &mctx, MONO_UNWIND_LOOKUP_ACTUAL_METHOD, &ud);
2379 if (ji && !ji->is_trampoline && jinfo_get_method (ji))
2380 mono_runtime_printf_err ("At %s", mono_method_full_name (jinfo_get_method (ji), TRUE));
2382 mono_runtime_printf_err ("At <unmanaged>.");
2389 print_stack_frame_to_stderr (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2391 MonoMethod *method = NULL;
2393 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2394 method = jinfo_get_method (frame->ji);
2397 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2398 mono_runtime_printf_err (" %s", location);
2401 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2406 static G_GNUC_UNUSED gboolean
2407 print_stack_frame_to_string (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2409 GString *p = (GString*)data;
2410 MonoMethod *method = NULL;
2412 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2413 method = jinfo_get_method (frame->ji);
2415 if (method && frame->domain) {
2416 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, frame->domain);
2417 g_string_append_printf (p, " %s\n", location);
2420 g_string_append_printf (p, " at <unknown> <0x%05x>\n", frame->native_offset);
2425 #ifndef MONO_CROSS_COMPILE
2427 static void print_process_map (void)
2430 FILE *fp = fopen ("/proc/self/maps", "r");
2434 mono_runtime_printf_err ("no /proc/self/maps, not on linux?\n");
2438 mono_runtime_printf_err ("/proc/self/maps:");
2440 while (fgets (line, sizeof (line), fp)) {
2442 size_t len = strlen (line) - 1;
2443 if (len >= 0 && line [len] == '\n')
2446 mono_runtime_printf_err ("%s", line);
2455 static gboolean handling_sigsegv = FALSE;
2458 * mono_handle_native_crash:
2460 * Handle a native crash (e.g. SIGSEGV) while in native code by
2461 * printing diagnostic information and aborting.
2464 mono_handle_native_crash (const char *signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2466 #ifdef MONO_ARCH_USE_SIGACTION
2467 struct sigaction sa;
2469 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2471 gboolean is_sigsegv = !strcmp ("SIGSEGV", signal);
2473 if (handling_sigsegv && is_sigsegv)
2476 if (mini_get_debug_options ()->suspend_on_native_crash) {
2477 mono_runtime_printf_err ("Received %s, suspending...", signal);
2488 /* To prevent infinite loops when the stack walk causes a crash */
2490 handling_sigsegv = TRUE;
2492 /* !jit_tls means the thread was not registered with the runtime */
2493 if (jit_tls && mono_thread_internal_current ()) {
2494 mono_runtime_printf_err ("Stacktrace:\n");
2496 /* FIXME: Is MONO_UNWIND_LOOKUP_IL_OFFSET correct here? */
2497 mono_walk_stack (print_stack_frame_to_stderr, MONO_UNWIND_LOOKUP_IL_OFFSET, NULL);
2500 print_process_map ();
2502 #ifdef HAVE_BACKTRACE_SYMBOLS
2508 mono_runtime_printf_err ("\nNative stacktrace:\n");
2510 size = backtrace (array, 256);
2511 names = backtrace_symbols (array, size);
2512 for (i =0; i < size; ++i) {
2513 mono_runtime_printf_err ("\t%s", names [i]);
2517 /* Try to get more meaningful information using gdb */
2519 #if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_fork)
2520 if (!mini_get_debug_options ()->no_gdb_backtrace) {
2521 /* From g_spawn_command_line_sync () in eglib */
2524 pid_t crashed_pid = getpid ();
2527 * glibc fork acquires some locks, so if the crash happened inside malloc/free,
2528 * it will deadlock. Call the syscall directly instead.
2530 #if defined(PLATFORM_ANDROID)
2531 /* SYS_fork is defined to be __NR_fork which is not defined in some ndk versions */
2532 g_assert_not_reached ();
2533 #elif !defined(PLATFORM_MACOSX) && defined(SYS_fork)
2534 pid = (pid_t) syscall (SYS_fork);
2535 #elif defined(PLATFORM_MACOSX) && HAVE_FORK
2536 pid = (pid_t) fork ();
2538 g_assert_not_reached ();
2541 #if defined (HAVE_PRCTL) && defined(PR_SET_PTRACER)
2543 // Allow gdb to attach to the process even if ptrace_scope sysctl variable is set to
2544 // a value other than 0 (the most permissive ptrace scope). Most modern Linux
2545 // distributions set the scope to 1 which allows attaching only to direct children of
2546 // the current process
2547 prctl (PR_SET_PTRACER, pid, 0, 0, 0);
2551 dup2 (STDERR_FILENO, STDOUT_FILENO);
2553 mono_gdb_render_native_backtraces (crashed_pid);
2557 mono_runtime_printf_err ("\nDebug info from gdb:\n");
2558 waitpid (pid, &status, 0);
2563 #ifdef PLATFORM_ANDROID
2564 /* set DUMPABLE for this process so debuggerd can attach with ptrace(2), see:
2565 * https://android.googlesource.com/platform/bionic/+/151da681000c07da3c24cd30a3279b1ca017f452/linker/debugger.cpp#206
2566 * this has changed on later versions of Android. Also, we don't want to
2567 * set this on start-up as DUMPABLE has security implications. */
2568 prctl (PR_SET_DUMPABLE, 1);
2570 mono_runtime_printf_err ("\nNo native Android stacktrace (see debuggerd output).\n");
2575 * A SIGSEGV indicates something went very wrong so we can no longer depend
2576 * on anything working. So try to print out lots of diagnostics, starting
2577 * with ones which have a greater chance of working.
2579 mono_runtime_printf_err (
2581 "=================================================================\n"
2582 "Got a %s while executing native code. This usually indicates\n"
2583 "a fatal error in the mono runtime or one of the native libraries \n"
2584 "used by your application.\n"
2585 "=================================================================\n",
2588 #ifdef MONO_ARCH_USE_SIGACTION
2589 sa.sa_handler = SIG_DFL;
2590 sigemptyset (&sa.sa_mask);
2593 /* Remove our SIGABRT handler */
2594 g_assert (sigaction (SIGABRT, &sa, NULL) != -1);
2596 /* On some systems we get a SIGILL when calling abort (), because it might
2597 * fail to raise SIGABRT */
2598 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
2601 if (!mono_do_crash_chaining) {
2602 /*Android abort is a fluke, it doesn't abort, it triggers another segv. */
2603 #if defined (PLATFORM_ANDROID)
2614 mono_handle_native_crash (const char *signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2616 g_assert_not_reached ();
2619 #endif /* !MONO_CROSS_COMPILE */
2622 mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
2624 MonoInternalThread *thread = mono_thread_internal_current ();
2625 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2630 GError *error = NULL;
2635 text = g_string_new (0);
2637 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
2639 g_string_append_printf (text, "\n\"%s\"", name);
2642 else if (thread->threadpool_thread)
2643 g_string_append (text, "\n\"<threadpool thread>\"");
2645 g_string_append (text, "\n\"<unnamed thread>\"");
2647 g_string_append_printf (text, " tid=0x%p this=0x%p ", (gpointer)(gsize)thread->tid, thread);
2648 mono_thread_internal_describe (thread, text);
2649 g_string_append (text, "\n");
2651 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2653 memcpy (&ctx, start_ctx, sizeof (MonoContext));
2655 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_print_thread_dump);
2657 mono_sigctx_to_monoctx (sigctx, &ctx);
2659 mono_walk_stack_with_ctx (print_stack_frame_to_string, &ctx, MONO_UNWIND_LOOKUP_ALL, text);
2661 mono_runtime_printf ("\t<Stack traces in thread dumps not supported on this platform>");
2664 mono_runtime_printf ("%s", text->str);
2666 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
2667 OutputDebugStringA(text->str);
2670 g_string_free (text, TRUE);
2671 mono_runtime_stdout_fflush ();
2675 * mono_print_thread_dump:
2677 * Print information about the current thread to stdout.
2678 * SIGCTX can be NULL, allowing this to be called from gdb.
2681 mono_print_thread_dump (void *sigctx)
2683 mono_print_thread_dump_internal (sigctx, NULL);
2687 mono_print_thread_dump_from_ctx (MonoContext *ctx)
2689 mono_print_thread_dump_internal (NULL, ctx);
2693 * mono_resume_unwind:
2695 * This is called by a trampoline from LLVM compiled finally clauses to continue
2699 mono_resume_unwind (MonoContext *ctx)
2701 MONO_REQ_GC_UNSAFE_MODE;
2703 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2704 MonoContext new_ctx;
2706 MONO_CONTEXT_SET_IP (ctx, MONO_CONTEXT_GET_IP (&jit_tls->resume_state.ctx));
2707 MONO_CONTEXT_SET_SP (ctx, MONO_CONTEXT_GET_SP (&jit_tls->resume_state.ctx));
2710 mono_handle_exception_internal (&new_ctx, (MonoObject *)jit_tls->resume_state.ex_obj, TRUE, NULL);
2712 mono_restore_context (&new_ctx);
2715 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
2720 MonoJitExceptionInfo *ei;
2721 } FindHandlerBlockData;
2724 find_last_handler_block (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2728 FindHandlerBlockData *pdata = (FindHandlerBlockData *)data;
2729 MonoJitInfo *ji = frame->ji;
2734 ip = MONO_CONTEXT_GET_IP (ctx);
2736 for (i = 0; i < ji->num_clauses; ++i) {
2737 MonoJitExceptionInfo *ei = ji->clauses + i;
2738 if (ei->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2740 /*If ip points to the first instruction it means the handler block didn't start
2741 so we can leave its execution to the EH machinery*/
2742 if (ei->handler_start < ip && ip < ei->data.handler_end) {
2754 install_handler_block_guard (MonoJitInfo *ji, MonoContext *ctx)
2757 MonoJitExceptionInfo *clause = NULL;
2760 ip = MONO_CONTEXT_GET_IP (ctx);
2762 for (i = 0; i < ji->num_clauses; ++i) {
2763 clause = &ji->clauses [i];
2764 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2766 if (clause->handler_start < ip && clause->data.handler_end > ip)
2770 /*no matching finally */
2771 if (i == ji->num_clauses)
2774 /*If we stopped on the instruction right before the try, we haven't actually started executing it*/
2775 if (ip == clause->handler_start)
2778 return mono_arch_install_handler_block_guard (ji, clause, ctx, mono_create_handler_block_trampoline ());
2782 * Finds the bottom handler block running and install a block guard if needed.
2785 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2787 FindHandlerBlockData data = { 0 };
2788 MonoJitTlsData *jit_tls = (MonoJitTlsData *)ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS];
2791 #ifndef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD_AOT
2796 /* Guard against a null MonoJitTlsData. This can happens if the thread receives the
2797 * interrupt signal before the JIT has time to initialize its TLS data for the given thread.
2799 if (!jit_tls || jit_tls->handler_block_return_address)
2802 /* Do an async safe stack walk */
2803 mono_thread_info_set_is_async_context (TRUE);
2804 mono_walk_stack_with_state (find_last_handler_block, ctx, MONO_UNWIND_NONE, &data);
2805 mono_thread_info_set_is_async_context (FALSE);
2810 memcpy (&jit_tls->handler_block_context, &data.ctx, sizeof (MonoContext));
2812 resume_ip = install_handler_block_guard (data.ji, &data.ctx);
2813 if (resume_ip == NULL)
2816 jit_tls->handler_block_return_address = resume_ip;
2817 jit_tls->handler_block = data.ei;
2823 mono_current_thread_has_handle_block_guard (void)
2825 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2826 return jit_tls && jit_tls->handler_block_return_address != NULL;
2831 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2837 mono_current_thread_has_handle_block_guard (void)
2845 mono_set_cast_details (MonoClass *from, MonoClass *to)
2847 MonoJitTlsData *jit_tls = NULL;
2849 if (mini_get_debug_options ()->better_cast_details) {
2850 jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2851 jit_tls->class_cast_from = from;
2852 jit_tls->class_cast_to = to;
2857 /*returns false if the thread is not attached*/
2859 mono_thread_state_init_from_sigctx (MonoThreadUnwindState *ctx, void *sigctx)
2861 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2862 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2869 mono_sigctx_to_monoctx (sigctx, &ctx->ctx);
2871 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2872 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2873 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2876 mono_thread_state_init (ctx);
2879 if (!ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] || !ctx->unwind_data [MONO_UNWIND_DATA_LMF])
2885 g_error ("Implement mono_arch_sigctx_to_monoctx for the current target");
2891 mono_thread_state_init (MonoThreadUnwindState *ctx)
2893 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2895 #if defined(MONO_CROSS_COMPILE)
2896 ctx->valid = FALSE; //A cross compiler doesn't need to suspend.
2897 #elif MONO_ARCH_HAS_MONO_CONTEXT
2898 MONO_CONTEXT_GET_CURRENT (ctx->ctx);
2900 g_error ("Use a null sigctx requires a working mono-context");
2903 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2904 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2905 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread ? thread->jit_data : NULL;
2911 mono_thread_state_init_from_monoctx (MonoThreadUnwindState *ctx, MonoContext *mctx)
2913 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2920 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2921 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2922 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2927 /*returns false if the thread is not attached*/
2929 mono_thread_state_init_from_current (MonoThreadUnwindState *ctx)
2931 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2932 MONO_ARCH_CONTEXT_DEF
2934 mono_arch_flush_register_windows ();
2936 if (!thread || !thread->jit_data) {
2940 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
2941 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx->ctx);
2943 MONO_INIT_CONTEXT_FROM_FUNC (&ctx->ctx, mono_thread_state_init_from_current);
2946 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2947 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2948 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2954 mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx)
2956 mono_handle_exception (ctx, (MonoObject *)exc);
2957 mono_restore_context (ctx);
2960 /*FIXME Move all monoctx -> sigctx conversion to signal handlers once all archs support utils/mono-context */
2962 mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
2964 #ifdef MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK
2965 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2966 jit_tls->ex_ctx = *ctx;
2968 mono_arch_setup_async_callback (ctx, async_cb, user_data);
2970 g_error ("This target doesn't support mono_arch_setup_async_callback");
2975 * mono_restore_context:
2977 * Call the architecture specific restore context function.
2980 mono_restore_context (MonoContext *ctx)
2982 static void (*restore_context) (MonoContext *);
2984 if (!restore_context)
2985 restore_context = (void (*)(MonoContext *))mono_get_restore_context ();
2986 restore_context (ctx);
2987 g_assert_not_reached ();
2991 * mono_jinfo_get_unwind_info:
2993 * Return the unwind info for JI.
2996 mono_jinfo_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2999 return mono_aot_get_unwind_info (ji, unwind_info_len);
3001 return mono_get_cached_unwind_info (ji->unwind_info, unwind_info_len);
3005 mono_jinfo_get_epilog_size (MonoJitInfo *ji)
3007 MonoArchEHJitInfo *info;
3009 info = mono_jit_info_get_arch_eh_info (ji);
3012 return info->epilog_size;
3016 * LLVM/Bitcode exception handling.
3020 throw_exception (MonoObject *ex, gboolean rethrow)
3022 MONO_REQ_GC_UNSAFE_MODE;
3025 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3026 MonoException *mono_ex;
3028 if (!mono_object_isinst_checked (ex, mono_defaults.exception_class, &error)) {
3029 mono_error_assert_ok (&error);
3030 mono_ex = mono_get_exception_runtime_wrapped_checked (ex, &error);
3031 mono_error_assert_ok (&error);
3032 jit_tls->thrown_non_exc = mono_gchandle_new (ex, FALSE);
3035 mono_ex = (MonoException*)ex;
3038 jit_tls->thrown_exc = mono_gchandle_new ((MonoObject*)mono_ex, FALSE);
3041 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
3042 GList *l, *ips = NULL;
3045 _Unwind_Backtrace (build_stack_trace, &ips);
3046 /* The list contains ip-gshared info pairs */
3048 ips = g_list_reverse (ips);
3049 for (l = ips; l; l = l->next) {
3050 trace = g_list_append (trace, l->data);
3051 trace = g_list_append (trace, NULL);
3053 MonoArray *ips_arr = mono_glist_to_array (trace, mono_defaults.int_class, &error);
3054 mono_error_assert_ok (&error);
3055 MONO_OBJECT_SETREF (mono_ex, trace_ips, ips_arr);
3057 g_list_free (trace);
3061 mono_llvm_cpp_throw_exception ();
3065 mono_llvm_throw_exception (MonoObject *ex)
3067 throw_exception (ex, FALSE);
3071 mono_llvm_rethrow_exception (MonoObject *ex)
3073 throw_exception (ex, TRUE);
3077 mono_llvm_raise_exception (MonoException *e)
3079 mono_llvm_throw_exception ((MonoObject*)e);
3083 mono_llvm_throw_corlib_exception (guint32 ex_token_index)
3085 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
3088 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
3090 mono_llvm_throw_exception ((MonoObject*)ex);
3094 * mono_llvm_resume_exception:
3096 * Resume exception propagation.
3099 mono_llvm_resume_exception (void)
3101 mono_llvm_cpp_throw_exception ();
3105 * mono_llvm_load_exception:
3107 * Return the currently thrown exception.
3110 mono_llvm_load_exception (void)
3113 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3115 MonoException *mono_ex = (MonoException*)mono_gchandle_get_target (jit_tls->thrown_exc);
3117 if (mono_ex->trace_ips) {
3118 GList *trace_ips = NULL;
3119 gpointer ip = __builtin_return_address (0);
3121 size_t upper = mono_array_length (mono_ex->trace_ips);
3123 for (int i = 0; i < upper; i+= 2) {
3124 gpointer curr_ip = mono_array_get (mono_ex->trace_ips, gpointer, i);
3125 gpointer curr_info = mono_array_get (mono_ex->trace_ips, gpointer, i + 1);
3126 trace_ips = g_list_append (trace_ips, curr_ip);
3127 trace_ips = g_list_append (trace_ips, curr_info);
3133 // FIXME: Does this work correctly for rethrows?
3134 // We may be discarding useful information
3135 // when this gets GC'ed
3136 MonoArray *ips_arr = mono_glist_to_array (trace_ips, mono_defaults.int_class, &error);
3137 mono_error_assert_ok (&error);
3138 MONO_OBJECT_SETREF (mono_ex, trace_ips, ips_arr);
3139 g_list_free (trace_ips);
3142 //MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
3144 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_array_new_checked (mono_domain_get (), mono_defaults.int_class, 0, &error));
3145 mono_error_assert_ok (&error);
3146 MONO_OBJECT_SETREF (mono_ex, stack_trace, mono_array_new_checked (mono_domain_get (), mono_defaults.stack_frame_class, 0, &error));
3147 mono_error_assert_ok (&error);
3150 return &mono_ex->object;
3154 * mono_llvm_clear_exception:
3156 * Mark the currently thrown exception as handled.
3159 mono_llvm_clear_exception (void)
3161 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3162 mono_gchandle_free (jit_tls->thrown_exc);
3163 jit_tls->thrown_exc = 0;
3164 if (jit_tls->thrown_non_exc)
3165 mono_gchandle_free (jit_tls->thrown_non_exc);
3166 jit_tls->thrown_non_exc = 0;
3168 mono_memory_barrier ();
3172 * mono_llvm_match_exception:
3174 * Return the innermost clause containing REGION_START-REGION_END which can handle
3175 * the current exception.
3178 mono_llvm_match_exception (MonoJitInfo *jinfo, guint32 region_start, guint32 region_end, gpointer rgctx, MonoObject *this_obj)
3181 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3185 g_assert (jit_tls->thrown_exc);
3186 exc = mono_gchandle_get_target (jit_tls->thrown_exc);
3187 if (jit_tls->thrown_non_exc) {
3189 * Have to unwrap RuntimeWrappedExceptions if the
3190 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
3192 if (!wrap_non_exception_throws (jinfo_get_method (jinfo)))
3193 exc = mono_gchandle_get_target (jit_tls->thrown_non_exc);
3196 for (int i = 0; i < jinfo->num_clauses; i++) {
3197 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
3198 MonoClass *catch_class;
3200 if (! (ei->try_offset == region_start && ei->try_offset + ei->try_len == region_end) )
3203 catch_class = ei->data.catch_class;
3204 if (mono_class_is_open_constructed_type (&catch_class->byval_arg)) {
3205 MonoGenericContext context;
3206 MonoType *inflated_type;
3208 g_assert (rgctx || this_obj);
3209 context = get_generic_context_from_stack_frame (jinfo, rgctx ? rgctx : this_obj->vtable);
3210 inflated_type = mono_class_inflate_generic_type_checked (&catch_class->byval_arg, &context, &error);
3211 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
3213 catch_class = mono_class_from_mono_type (inflated_type);
3214 mono_metadata_free_type (inflated_type);
3217 // FIXME: Handle edge cases handled in get_exception_catch_class
3218 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (exc, catch_class, &error)) {
3219 index = ei->clause_index;
3222 mono_error_assert_ok (&error);
3224 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3225 g_assert_not_reached ();
3234 mono_debug_personality (int a, _Unwind_Action b,
3235 uint64_t c, struct _Unwind_Exception *d, struct _Unwind_Context *e)
3237 g_assert_not_reached ();
3241 mono_debug_personality (void);
3244 mono_debug_personality (void)
3246 g_assert_not_reached ();