2 * mini-exceptions.c: generic exception support
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Mono Team (mono-list@lists.ximian.com)
8 * Copyright 2001-2003 Ximian, Inc.
9 * Copyright 2003-2008 Novell, Inc.
10 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
22 #ifdef HAVE_EXECINFO_H
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
30 #ifdef HAVE_SYS_WAIT_H
38 #ifdef HAVE_SYS_SYSCALL_H
39 #include <sys/syscall.h>
42 #ifdef HAVE_SYS_PRCTL_H
43 #include <sys/prctl.h>
50 #include <mono/metadata/appdomain.h>
51 #include <mono/metadata/tabledefs.h>
52 #include <mono/metadata/threads.h>
53 #include <mono/metadata/threads-types.h>
54 #include <mono/metadata/debug-helpers.h>
55 #include <mono/metadata/exception.h>
56 #include <mono/metadata/exception-internals.h>
57 #include <mono/metadata/object-internals.h>
58 #include <mono/metadata/reflection-internals.h>
59 #include <mono/metadata/gc-internals.h>
60 #include <mono/metadata/mono-debug.h>
61 #include <mono/metadata/profiler.h>
62 #include <mono/metadata/mono-endian.h>
63 #include <mono/metadata/environment.h>
64 #include <mono/metadata/mono-mlist.h>
65 #include <mono/utils/mono-mmap.h>
66 #include <mono/utils/mono-logger-internals.h>
67 #include <mono/utils/mono-error.h>
68 #include <mono/utils/mono-error-internals.h>
72 #include "debugger-agent.h"
73 #include "seq-points.h"
74 #include "llvm-runtime.h"
75 #include "mini-llvm.h"
78 #include "mini-llvm-cpp.h"
81 #ifndef MONO_ARCH_CONTEXT_DEF
82 #define MONO_ARCH_CONTEXT_DEF
85 #ifndef MONO_ARCH_STACK_GROWS_UP
86 #define MONO_ARCH_STACK_GROWS_UP 0
89 static gpointer restore_context_func, call_filter_func;
90 static gpointer throw_exception_func, rethrow_exception_func;
91 static gpointer throw_corlib_exception_func;
93 static gpointer try_more_restore_tramp = NULL;
94 static gpointer restore_stack_protection_tramp = NULL;
96 static void try_more_restore (void);
97 static void restore_stack_protection (void);
98 static void mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data);
99 static void mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx);
100 static void mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data);
101 static gboolean mono_current_thread_has_handle_block_guard (void);
104 first_managed (MonoStackFrameInfo *frame, MonoContext *ctx, gpointer addr)
106 gpointer **data = (gpointer **)addr;
112 // FIXME: Happens with llvm_only
117 *data = MONO_CONTEXT_GET_SP (ctx);
123 mono_thread_get_managed_sp (void)
125 gpointer addr = NULL;
126 mono_walk_stack (first_managed, MONO_UNWIND_SIGNAL_SAFE, &addr);
131 mini_abort_threshold_offset (gpointer threshold, gpointer sp)
133 intptr_t stack_threshold = (intptr_t) threshold;
134 intptr_t stack_pointer = (intptr_t) sp;
136 const int direction = MONO_ARCH_STACK_GROWS_UP ? -1 : 1;
137 intptr_t magnitude = stack_pointer - stack_threshold;
139 return direction * magnitude;
143 mini_clear_abort_threshold (void)
145 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
146 jit_tls->abort_exc_stack_threshold = NULL;
150 mini_set_abort_threshold (MonoContext *ctx)
152 gpointer sp = MONO_CONTEXT_GET_SP (ctx);
153 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
154 // Only move it up, to avoid thrown/caught
155 // exceptions lower in the stack from triggering
157 gboolean above_threshold = mini_abort_threshold_offset (jit_tls->abort_exc_stack_threshold, sp) >= 0;
158 if (!jit_tls->abort_exc_stack_threshold || above_threshold) {
159 jit_tls->abort_exc_stack_threshold = sp;
163 // Note: In the case that the frame is above where the thread abort
164 // was set we bump the threshold so that functions called from the new,
165 // higher threshold don't trigger the thread abort exception
166 static inline gboolean
167 mini_above_abort_threshold (void)
169 gpointer sp = mono_thread_get_managed_sp ();
170 MonoJitTlsData *jit_tls = (MonoJitTlsData*) mono_tls_get_jit_tls ();
175 gboolean above_threshold = mini_abort_threshold_offset (jit_tls->abort_exc_stack_threshold, sp) >= 0;
178 jit_tls->abort_exc_stack_threshold = sp;
180 return above_threshold;
184 mono_exceptions_init (void)
186 MonoRuntimeExceptionHandlingCallbacks cbs;
188 restore_context_func = mono_aot_get_trampoline ("restore_context");
189 call_filter_func = mono_aot_get_trampoline ("call_filter");
190 throw_exception_func = mono_aot_get_trampoline ("throw_exception");
191 rethrow_exception_func = mono_aot_get_trampoline ("rethrow_exception");
195 restore_context_func = mono_arch_get_restore_context (&info, FALSE);
196 mono_tramp_info_register (info, NULL);
197 call_filter_func = mono_arch_get_call_filter (&info, FALSE);
198 mono_tramp_info_register (info, NULL);
199 throw_exception_func = mono_arch_get_throw_exception (&info, FALSE);
200 mono_tramp_info_register (info, NULL);
201 rethrow_exception_func = mono_arch_get_rethrow_exception (&info, FALSE);
202 mono_tramp_info_register (info, NULL);
204 #ifdef MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT
205 if (!mono_llvm_only) {
206 try_more_restore_tramp = mono_create_specific_trampoline (try_more_restore, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
207 restore_stack_protection_tramp = mono_create_specific_trampoline (restore_stack_protection, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
211 #ifdef MONO_ARCH_HAVE_EXCEPTIONS_INIT
212 mono_arch_exceptions_init ();
214 cbs.mono_walk_stack_with_ctx = mono_runtime_walk_stack_with_ctx;
215 cbs.mono_walk_stack_with_state = mono_walk_stack_with_state;
218 cbs.mono_raise_exception = mono_llvm_raise_exception;
220 cbs.mono_raise_exception = (void (*)(MonoException *))mono_get_throw_exception ();
221 cbs.mono_raise_exception_with_ctx = mono_raise_exception_with_ctx;
222 cbs.mono_exception_walk_trace = mono_exception_walk_trace;
223 cbs.mono_install_handler_block_guard = mono_install_handler_block_guard;
224 cbs.mono_current_thread_has_handle_block_guard = mono_current_thread_has_handle_block_guard;
225 cbs.mono_clear_abort_threshold = mini_clear_abort_threshold;
226 cbs.mono_above_abort_threshold = mini_above_abort_threshold;
227 mono_install_eh_callbacks (&cbs);
231 mono_get_throw_exception (void)
233 g_assert (throw_exception_func);
234 return throw_exception_func;
238 mono_get_rethrow_exception (void)
240 g_assert (rethrow_exception_func);
241 return rethrow_exception_func;
245 mono_get_call_filter (void)
247 g_assert (call_filter_func);
248 return call_filter_func;
252 mono_get_restore_context (void)
254 g_assert (restore_context_func);
255 return restore_context_func;
259 mono_get_throw_corlib_exception (void)
261 gpointer code = NULL;
264 /* This depends on corlib classes so cannot be inited in mono_exceptions_init () */
265 if (throw_corlib_exception_func)
266 return throw_corlib_exception_func;
269 code = mono_aot_get_trampoline ("throw_corlib_exception");
271 code = mono_arch_get_throw_corlib_exception (&info, FALSE);
272 mono_tramp_info_register (info, NULL);
275 mono_memory_barrier ();
277 throw_corlib_exception_func = code;
279 return throw_corlib_exception_func;
283 * mono_get_throw_exception_addr:
285 * Return an address which stores the result of
286 * mono_get_throw_exception.
289 mono_get_throw_exception_addr (void)
291 return &throw_exception_func;
295 is_address_protected (MonoJitInfo *ji, MonoJitExceptionInfo *ei, gpointer ip)
297 MonoTryBlockHoleTableJitInfo *table;
302 if (ei->try_start > ip || ip >= ei->try_end)
305 if (!ji->has_try_block_holes)
308 table = mono_jit_info_get_try_block_hole_table_info (ji);
309 offset = (guint32)((char*)ip - (char*)ji->code_start);
310 clause = (guint16)(ei - ji->clauses);
311 g_assert (clause < ji->num_clauses);
313 for (i = 0; i < table->num_holes; ++i) {
314 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
315 if (hole->clause == clause && hole->offset <= offset && hole->offset + hole->length > offset)
321 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
324 static gboolean show_native_addresses = TRUE;
326 static gboolean show_native_addresses = FALSE;
329 static _Unwind_Reason_Code
330 build_stack_trace (struct _Unwind_Context *frame_ctx, void *state)
332 MonoDomain *domain = mono_domain_get ();
333 uintptr_t ip = _Unwind_GetIP (frame_ctx);
335 if (show_native_addresses || mono_jit_info_table_find (domain, (char*)ip)) {
336 GList **trace_ips = (GList **)state;
337 *trace_ips = g_list_prepend (*trace_ips, (gpointer)ip);
340 return _URC_NO_REASON;
344 get_unwind_backtrace (void)
348 _Unwind_Backtrace (build_stack_trace, &ips);
350 return g_slist_reverse (ips);
356 get_unwind_backtrace (void)
366 * Translate between the mono_arch_unwind_frame function and the old API.
369 find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
370 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
372 StackFrameInfo frame;
375 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
377 /* Avoid costly table lookup during stack overflow */
378 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
381 ji = mini_jit_info_table_find (domain, (char *)ip, NULL);
386 err = mono_arch_unwind_frame (domain, jit_tls, ji, ctx, new_ctx, lmf, NULL, &frame);
388 return (MonoJitInfo *)-1;
390 if (*lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
392 * Remove any unused lmf.
393 * Mask out the lower bits which might be used to hold additional information.
395 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
398 /* Convert between the new and the old APIs */
399 switch (frame.type) {
400 case FRAME_TYPE_MANAGED:
404 case FRAME_TYPE_TRAMPOLINE:
406 case FRAME_TYPE_MANAGED_TO_NATIVE:
410 memset (res, 0, sizeof (MonoJitInfo));
411 res->d.method = frame.method;
414 case FRAME_TYPE_DEBUGGER_INVOKE: {
418 * The normal exception handling code can't handle this frame, so just
421 ji = find_jit_info (domain, jit_tls, res, NULL, new_ctx, &tmp_ctx, lmf, managed);
422 memcpy (new_ctx, &tmp_ctx, sizeof (MonoContext));
426 g_assert_not_reached ();
431 /* mono_find_jit_info:
433 * This function is used to gather information from @ctx. It return the
434 * MonoJitInfo of the corresponding function, unwinds one stack frame and
435 * stores the resulting context into @new_ctx. It also stores a string
436 * describing the stack location into @trace (if not NULL), and modifies
437 * the @lmf if necessary. @native_offset return the IP offset from the
438 * start of the function or -1 if that info is not available.
441 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
442 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
446 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
448 MonoMethod *method = NULL;
459 ji = find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
461 if (ji == (gpointer)-1)
464 if (ji && !ji->is_trampoline)
465 method = jinfo_get_method (ji);
467 if (managed2 || (method && method->wrapper_type)) {
468 const char *real_ip, *start;
471 start = (const char *)ji->code_start;
473 /* ctx->ip points into native code */
474 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
476 real_ip = (const char*)ip;
478 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
479 offset = real_ip - start;
484 *native_offset = offset;
487 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
491 *trace = mono_debug_print_stack_frame (method, offset, domain);
494 char *fname = mono_method_full_name (jinfo_get_method (res), TRUE);
495 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
504 * mono_find_jit_info_ext:
506 * A version of mono_find_jit_info which returns all data in the StackFrameInfo
508 * A note about frames of type FRAME_TYPE_MANAGED_TO_NATIVE:
509 * - These frames are used to mark managed-to-native transitions, so CTX will refer to native
510 * code, and new_ctx will refer to the last managed frame. The caller should unwind once more
511 * to obtain the last managed frame.
512 * If SAVE_LOCATIONS is not NULL, it should point to an array of size MONO_MAX_IREGS.
513 * On return, it will be filled with the locations where callee saved registers are saved
514 * by the current frame. This is returned outside of StackFrameInfo because it can be
515 * quite large on some platforms.
516 * If ASYNC true, this function will be async safe, but some fields of frame and frame->ji will
520 mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
521 MonoJitInfo *prev_ji, MonoContext *ctx,
522 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
523 mgreg_t **save_locations,
524 StackFrameInfo *frame)
527 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
529 MonoDomain *target_domain = domain;
530 MonoMethod *method = NULL;
531 gboolean async = mono_thread_info_is_async_context ();
536 /* Avoid costly table lookup during stack overflow */
537 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
540 ji = mini_jit_info_table_find (domain, (char *)ip, &target_domain);
543 target_domain = domain;
546 memset (save_locations, 0, MONO_MAX_IREGS * sizeof (mgreg_t*));
548 err = mono_arch_unwind_frame (target_domain, jit_tls, ji, ctx, new_ctx, lmf, save_locations, frame);
552 if (*lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
554 * Remove any unused lmf.
555 * Mask out the lower bits which might be used to hold additional information.
557 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
560 if (frame->ji && !frame->ji->is_trampoline && !frame->ji->async)
561 method = jinfo_get_method (frame->ji);
563 if (frame->type == FRAME_TYPE_MANAGED && method) {
564 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
565 frame->managed = TRUE;
568 if (frame->type == FRAME_TYPE_MANAGED_TO_NATIVE) {
570 * This type of frame is just a marker, the caller should unwind once more to get the
571 * last managed frame.
574 frame->method = NULL;
577 frame->native_offset = -1;
578 frame->domain = target_domain;
579 frame->async_context = async;
583 if (frame->type == FRAME_TYPE_MANAGED)
584 frame->method = method;
586 if (ji && (frame->managed || (method && method->wrapper_type))) {
587 const char *real_ip, *start;
589 start = (const char *)ji->code_start;
590 if (frame->type == FRAME_TYPE_MANAGED)
591 real_ip = (const char*)ip;
593 /* ctx->ip points into native code */
594 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
596 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
597 frame->native_offset = real_ip - start;
599 frame->native_offset = -1;
603 *trace = mono_debug_print_stack_frame (method, frame->native_offset, domain);
605 if (trace && frame->method) {
606 char *fname = mono_method_full_name (frame->method, TRUE);
607 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
616 * This function is async-safe.
619 get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
621 MonoGenericJitInfo *gi;
625 if (!ji->has_generic_jit_info)
627 gi = mono_jit_info_get_generic_jit_info (ji);
633 * Search location list if available, it contains the precise location of the
634 * argument for every pc offset, even if the method was interrupted while it was in
638 int offset = (mgreg_t)MONO_CONTEXT_GET_IP (ctx) - (mgreg_t)ji->code_start;
641 for (i = 0; i < gi->nlocs; ++i) {
642 MonoDwarfLocListEntry *entry = &gi->locations [i];
644 if (offset >= entry->from && (offset < entry->to || entry->to == 0)) {
646 info = (gpointer)mono_arch_context_get_int_reg (ctx, entry->reg);
648 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, entry->reg) + entry->offset);
652 g_assert (i < gi->nlocs);
655 info = (gpointer)mono_arch_context_get_int_reg (ctx, gi->this_reg);
657 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
661 method = jinfo_get_method (ji);
662 if (mono_method_get_context (method)->method_inst) {
663 /* A MonoMethodRuntimeGenericContext* */
665 } else if ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype) {
669 /* Avoid returning a managed object */
670 MonoObject *this_obj = (MonoObject *)info;
672 return this_obj->vtable;
677 * generic_info is either a MonoMethodRuntimeGenericContext or a MonoVTable.
679 static MonoGenericContext
680 get_generic_context_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
682 MonoGenericContext context = { NULL, NULL };
683 MonoClass *klass, *method_container_class;
686 g_assert (generic_info);
688 method = jinfo_get_method (ji);
689 g_assert (method->is_inflated);
690 if (mono_method_get_context (method)->method_inst) {
691 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext *)generic_info;
693 klass = mrgctx->class_vtable->klass;
694 context.method_inst = mrgctx->method_inst;
695 g_assert (context.method_inst);
697 MonoVTable *vtable = (MonoVTable *)generic_info;
699 klass = vtable->klass;
702 //g_assert (!mono_class_is_gtd (method->klass));
703 if (mono_class_is_ginst (method->klass))
704 method_container_class = mono_class_get_generic_class (method->klass)->container_class;
706 method_container_class = method->klass;
708 /* class might refer to a subclass of method's class */
709 while (!(klass == method->klass || (mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->container_class == method_container_class))) {
710 klass = klass->parent;
714 if (mono_class_is_ginst (klass) || mono_class_is_gtd (klass))
715 context.class_inst = mini_class_get_context (klass)->class_inst;
717 if (mono_class_is_ginst (klass))
718 g_assert (mono_class_has_parent_and_ignore_generics (mono_class_get_generic_class (klass)->container_class, method_container_class));
720 g_assert (mono_class_has_parent_and_ignore_generics (klass, method_container_class));
726 get_method_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
729 MonoGenericContext context;
732 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
733 return jinfo_get_method (ji);
734 context = get_generic_context_from_stack_frame (ji, generic_info);
736 method = jinfo_get_method (ji);
737 method = mono_method_get_declaring_generic_method (method);
738 method = mono_class_inflate_generic_method_checked (method, &context, &error);
739 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
745 * mono_exception_walk_native_trace:
746 * @ex: The exception object whose frames should be walked
747 * @func: callback to call for each stack frame
748 * @user_data: data passed to the callback
750 * This function walks the stacktrace of an exception. For
751 * each frame the callback function is called with the relevant info.
752 * The walk ends when no more stack frames are found or when the callback
753 * returns a TRUE value.
757 mono_exception_walk_trace (MonoException *ex, MonoExceptionFrameWalk func, gpointer user_data)
759 MONO_REQ_GC_UNSAFE_MODE;
761 MonoDomain *domain = mono_domain_get ();
762 MonoArray *ta = ex->trace_ips;
768 len = mono_array_length (ta) >> 1;
769 for (i = 0; i < len; i++) {
770 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
771 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
772 MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *)ip);
775 if (func (NULL, ip, 0, FALSE, user_data))
778 MonoMethod *method = get_method_from_stack_frame (ji, generic_info);
779 if (func (method, ji->code_start, (char *) ip - (char *) ji->code_start, TRUE, user_data))
788 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
791 MonoDomain *domain = mono_domain_get ();
793 MonoArray *ta = exc->trace_ips;
794 MonoDebugSourceLocation *location;
798 /* Exception is not thrown yet */
799 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
800 mono_error_set_pending_exception (&error);
804 len = mono_array_length (ta) >> 1;
806 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
807 if (mono_error_set_pending_exception (&error))
810 for (i = skip; i < len; i++) {
812 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
813 if (!mono_error_ok (&error)) {
814 mono_error_set_pending_exception (&error);
817 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
818 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
821 ji = mono_jit_info_table_find (domain, (char *)ip);
823 /* Unmanaged frame */
824 mono_array_setref (res, i, sf);
828 g_assert (ji != NULL);
831 /* Can't resolve actual method */
832 method = jinfo_get_method (ji);
834 method = get_method_from_stack_frame (ji, generic_info);
835 if (jinfo_get_method (ji)->wrapper_type) {
839 s = mono_method_get_name_full (method, TRUE, FALSE, MONO_TYPE_NAME_FORMAT_REFLECTION);
840 MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
844 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, method, NULL, &error);
845 if (!mono_error_ok (&error)) {
846 mono_error_set_pending_exception (&error);
849 MONO_OBJECT_SETREF (sf, method, rm);
852 sf->method_index = ji->from_aot ? mono_aot_find_method_index (method) : 0xffffff;
853 sf->method_address = (gsize) ji->code_start;
854 sf->native_offset = (char *)ip - (char *)ji->code_start;
857 * mono_debug_lookup_source_location() returns both the file / line number information
858 * and the IL offset. Note that computing the IL offset is already an expensive
859 * operation, so we shouldn't call this method twice.
861 location = mono_debug_lookup_source_location (jinfo_get_method (ji), sf->native_offset, domain);
863 sf->il_offset = location->il_offset;
866 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (ji), sf->native_offset, NULL, &sp))
867 sf->il_offset = sp.il_offset;
872 if (need_file_info) {
873 if (location && location->source_file) {
874 MONO_OBJECT_SETREF (sf, filename, mono_string_new (domain, location->source_file));
875 sf->line = location->row;
876 sf->column = location->column;
878 sf->line = sf->column = 0;
883 mono_debug_free_source_location (location);
884 mono_array_setref (res, i, sf);
891 mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
894 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
895 if (jit_tls && jit_tls->orig_ex_ctx_set)
896 start_ctx = &jit_tls->orig_ex_ctx;
898 mono_walk_stack_with_ctx (func, start_ctx, unwind_options, user_data);
901 * mono_walk_stack_with_ctx:
903 * Unwind the current thread starting at @start_ctx.
905 * If @start_ctx is null, we capture the current context.
908 mono_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
910 MonoContext extra_ctx;
911 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
912 MONO_ARCH_CONTEXT_DEF
914 if (!thread || !thread->jit_data)
918 mono_arch_flush_register_windows ();
920 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
921 MONO_INIT_CONTEXT_FROM_CURRENT (&extra_ctx);
923 MONO_INIT_CONTEXT_FROM_FUNC (&extra_ctx, mono_walk_stack_with_ctx);
925 start_ctx = &extra_ctx;
928 mono_walk_stack_full (func, start_ctx, mono_domain_get (), (MonoJitTlsData *)thread->jit_data, mono_get_lmf (), unwind_options, user_data);
932 * mono_walk_stack_with_state:
934 * Unwind a thread described by @state.
936 * State must be valid (state->valid == TRUE).
938 * If you are using this function to unwind another thread, make sure it is suspended.
940 * If @state is null, we capture the current context.
943 mono_walk_stack_with_state (MonoJitStackWalk func, MonoThreadUnwindState *state, MonoUnwindOptions unwind_options, void *user_data)
945 MonoThreadUnwindState extra_state;
947 g_assert (!mono_thread_info_is_async_context ());
948 if (!mono_thread_state_init_from_current (&extra_state))
950 state = &extra_state;
953 g_assert (state->valid);
955 if (!state->unwind_data [MONO_UNWIND_DATA_DOMAIN])
959 mono_walk_stack_full (func,
961 (MonoDomain *)state->unwind_data [MONO_UNWIND_DATA_DOMAIN],
962 (MonoJitTlsData *)state->unwind_data [MONO_UNWIND_DATA_JIT_TLS],
963 (MonoLMF *)state->unwind_data [MONO_UNWIND_DATA_LMF],
964 unwind_options, user_data);
968 mono_walk_stack (MonoJitStackWalk func, MonoUnwindOptions options, void *user_data)
970 MonoThreadUnwindState state;
971 if (!mono_thread_state_init_from_current (&state))
973 mono_walk_stack_with_state (func, &state, options, user_data);
977 * mono_walk_stack_full:
978 * @func: callback to call for each stack frame
979 * @domain: starting appdomain, can be NULL to use the current domain
980 * @unwind_options: what extra information the unwinder should gather
981 * @start_ctx: starting state of the stack walk, can be NULL.
982 * @thread: the thread whose stack to walk, can be NULL to use the current thread
983 * @lmf: the LMF of @thread, can be NULL to use the LMF of the current thread
984 * @user_data: data passed to the callback
986 * This function walks the stack of a thread, starting from the state
987 * represented by start_ctx. For each frame the callback
988 * function is called with the relevant info. The walk ends when no more
989 * managed stack frames are found or when the callback returns a TRUE value.
992 mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data)
995 MonoContext ctx, new_ctx;
996 StackFrameInfo frame;
998 mgreg_t *reg_locations [MONO_MAX_IREGS];
999 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
1000 gboolean get_reg_locations = unwind_options & MONO_UNWIND_REG_LOCATIONS;
1001 gboolean async = mono_thread_info_is_async_context ();
1003 if (mono_llvm_only) {
1009 ips = get_unwind_backtrace ();
1010 for (l = ips; l; l = l->next) {
1011 guint8 *ip = (guint8*)l->data;
1012 memset (&frame, 0, sizeof (StackFrameInfo));
1013 frame.ji = mini_jit_info_table_find (domain, (char*)ip, &frame.domain);
1014 if (!frame.ji || frame.ji->is_trampoline)
1016 frame.type = FRAME_TYPE_MANAGED;
1017 frame.method = jinfo_get_method (frame.ji);
1018 // FIXME: Cannot lookup the actual method
1019 frame.actual_method = frame.method;
1020 if (frame.type == FRAME_TYPE_MANAGED) {
1021 if (!frame.method->wrapper_type || frame.method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
1022 frame.managed = TRUE;
1024 frame.native_offset = ip - (guint8*)frame.ji->code_start;
1025 frame.il_offset = -1;
1027 if (func (&frame, NULL, user_data))
1034 g_assert (start_ctx);
1037 /*The LMF will be null if the target have no managed frames.*/
1038 /* g_assert (lmf); */
1041 g_assert (unwind_options == MONO_UNWIND_NONE);
1043 memcpy (&ctx, start_ctx, sizeof (MonoContext));
1044 memset (reg_locations, 0, sizeof (reg_locations));
1046 while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
1048 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, get_reg_locations ? new_reg_locations : NULL, &frame);
1052 if ((unwind_options & MONO_UNWIND_LOOKUP_IL_OFFSET) && frame.ji && !frame.ji->is_trampoline) {
1053 MonoDebugSourceLocation *source;
1055 source = mono_debug_lookup_source_location (jinfo_get_method (frame.ji), frame.native_offset, domain);
1057 il_offset = source->il_offset;
1060 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (frame.ji), frame.native_offset, NULL, &sp))
1061 il_offset = sp.il_offset;
1065 mono_debug_free_source_location (source);
1069 frame.il_offset = il_offset;
1071 if ((unwind_options & MONO_UNWIND_LOOKUP_ACTUAL_METHOD) && frame.ji && !frame.ji->is_trampoline) {
1072 frame.actual_method = get_method_from_stack_frame (frame.ji, get_generic_info_from_stack_frame (frame.ji, &ctx));
1074 frame.actual_method = frame.method;
1077 if (get_reg_locations)
1078 frame.reg_locations = reg_locations;
1080 if (func (&frame, &ctx, user_data))
1083 if (get_reg_locations) {
1084 for (i = 0; i < MONO_MAX_IREGS; ++i)
1085 if (new_reg_locations [i])
1086 reg_locations [i] = new_reg_locations [i];
1094 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
1095 MonoReflectionMethod **method,
1096 gint32 *iloffset, gint32 *native_offset,
1097 MonoString **file, gint32 *line, gint32 *column)
1100 MonoDomain *domain = mono_domain_get ();
1101 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1102 MonoLMF *lmf = mono_get_lmf ();
1103 MonoJitInfo *ji = NULL;
1104 MonoContext ctx, new_ctx;
1105 MonoDebugSourceLocation *location;
1106 MonoMethod *jmethod = NULL, *actual_method;
1107 StackFrameInfo frame;
1110 MONO_ARCH_CONTEXT_DEF;
1112 if (mono_llvm_only) {
1114 MonoDomain *frame_domain;
1115 guint8 *frame_ip = NULL;
1117 /* FIXME: Generalize this code with an interface which returns an array of StackFrame structures */
1119 ips = get_unwind_backtrace ();
1120 for (l = ips; l && skip >= 0; l = l->next) {
1121 guint8 *ip = (guint8*)l->data;
1125 ji = mini_jit_info_table_find (mono_domain_get (), (char*)ip, &frame_domain);
1126 if (!ji || ji->is_trampoline)
1129 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1130 jmethod = jinfo_get_method (ji);
1131 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1138 /* No way to resolve generic instances */
1139 actual_method = jmethod;
1140 *native_offset = frame_ip - (guint8*)ji->code_start;
1142 mono_arch_flush_register_windows ();
1144 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
1145 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
1147 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
1153 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1157 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1158 frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1159 frame.type == FRAME_TYPE_TRAMPOLINE)
1163 *native_offset = frame.native_offset;
1165 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1166 jmethod = jinfo_get_method (ji);
1167 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1170 } while (skip >= 0);
1172 actual_method = get_method_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, &ctx));
1175 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, actual_method, NULL, &error);
1176 if (!mono_error_ok (&error)) {
1177 mono_error_set_pending_exception (&error);
1180 mono_gc_wbarrier_generic_store (method, (MonoObject*) rm);
1182 location = mono_debug_lookup_source_location (jmethod, *native_offset, domain);
1184 *iloffset = location->il_offset;
1188 if (need_file_info) {
1190 mono_gc_wbarrier_generic_store (file, (MonoObject*) mono_string_new (domain, location->source_file));
1191 *line = location->row;
1192 *column = location->column;
1195 *line = *column = 0;
1199 mono_debug_free_source_location (location);
1205 get_exception_catch_class (MonoJitExceptionInfo *ei, MonoJitInfo *ji, MonoContext *ctx)
1208 MonoClass *catch_class = ei->data.catch_class;
1209 MonoType *inflated_type;
1210 MonoGenericContext context;
1212 /*MonoJitExceptionInfo::data is an union used by filter and finally clauses too.*/
1213 if (!catch_class || ei->flags != MONO_EXCEPTION_CLAUSE_NONE)
1216 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
1218 context = get_generic_context_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, ctx));
1220 /* FIXME: we shouldn't inflate but instead put the
1221 type in the rgctx and fetch it from there. It
1222 might be a good idea to do this lazily, i.e. only
1223 when the exception is actually thrown, so as not to
1224 waste space for exception clauses which might never
1226 inflated_type = mono_class_inflate_generic_type_checked (&catch_class->byval_arg, &context, &error);
1227 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
1229 catch_class = mono_class_from_mono_type (inflated_type);
1230 mono_metadata_free_type (inflated_type);
1236 * mini_jit_info_table_find_ext:
1238 * Same as mono_jit_info_table_find, but search all the domains of the current thread
1239 * if ADDR is not found in DOMAIN. The domain where the method was found is stored into
1240 * OUT_DOMAIN if it is not NULL.
1243 mini_jit_info_table_find_ext (MonoDomain *domain, char *addr, gboolean allow_trampolines, MonoDomain **out_domain)
1246 MonoInternalThread *t = mono_thread_internal_current ();
1252 ji = mono_jit_info_table_find_internal (domain, addr, TRUE, allow_trampolines);
1255 *out_domain = domain;
1259 /* maybe it is shared code, so we also search in the root domain */
1260 if (domain != mono_get_root_domain ()) {
1261 ji = mono_jit_info_table_find_internal (mono_get_root_domain (), addr, TRUE, allow_trampolines);
1264 *out_domain = mono_get_root_domain ();
1272 refs = (gpointer *)((t->appdomain_refs) ? *(gpointer *) t->appdomain_refs : NULL);
1273 for (; refs && *refs; refs++) {
1274 if (*refs != domain && *refs != mono_get_root_domain ()) {
1275 ji = mono_jit_info_table_find_internal ((MonoDomain*) *refs, addr, TRUE, allow_trampolines);
1278 *out_domain = (MonoDomain*) *refs;
1288 mini_jit_info_table_find (MonoDomain *domain, char *addr, MonoDomain **out_domain)
1290 return mini_jit_info_table_find_ext (domain, addr, FALSE, out_domain);
1293 /* Class lazy loading functions */
1294 static GENERATE_GET_CLASS_WITH_CACHE (runtime_compat_attr, System.Runtime.CompilerServices, RuntimeCompatibilityAttribute)
1297 * wrap_non_exception_throws:
1299 * Determine whenever M's assembly has a RuntimeCompatibilityAttribute with the
1300 * WrapNonExceptionThrows flag set.
1303 wrap_non_exception_throws (MonoMethod *m)
1306 MonoAssembly *ass = m->klass->image->assembly;
1307 MonoCustomAttrInfo* attrs;
1310 gboolean val = FALSE;
1313 if (ass->wrap_non_exception_throws_inited)
1314 return ass->wrap_non_exception_throws;
1316 klass = mono_class_get_runtime_compat_attr_class ();
1318 attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, &error);
1319 mono_error_cleanup (&error); /* FIXME don't swallow the error */
1321 for (i = 0; i < attrs->num_attrs; ++i) {
1322 MonoCustomAttrEntry *attr = &attrs->attrs [i];
1324 int num_named, named_type, name_len;
1327 if (!attr->ctor || attr->ctor->klass != klass)
1329 /* Decode the RuntimeCompatibilityAttribute. See reflection.c */
1330 p = (const char*)attr->data;
1331 g_assert (read16 (p) == 0x0001);
1333 num_named = read16 (p);
1339 /* data_type = *p; */
1342 if (named_type != 0x54)
1344 name_len = mono_metadata_decode_blob_size (p, &p);
1345 name = (char *)g_malloc (name_len + 1);
1346 memcpy (name, p, name_len);
1347 name [name_len] = 0;
1349 g_assert (!strcmp (name, "WrapNonExceptionThrows"));
1351 /* The value is a BOOLEAN */
1354 mono_custom_attrs_free (attrs);
1357 ass->wrap_non_exception_throws = val;
1358 mono_memory_barrier ();
1359 ass->wrap_non_exception_throws_inited = TRUE;
1364 #define MAX_UNMANAGED_BACKTRACE 128
1366 build_native_trace (MonoError *error)
1368 mono_error_init (error);
1369 /* This puppy only makes sense on mobile, IOW, ARM. */
1370 #if defined (HAVE_BACKTRACE_SYMBOLS) && defined (TARGET_ARM)
1372 void *native_trace [MAX_UNMANAGED_BACKTRACE];
1375 size = backtrace (native_trace, MAX_UNMANAGED_BACKTRACE);
1381 res = mono_array_new_checked (mono_domain_get (), mono_defaults.int_class, size, error);
1382 return_val_if_nok (error, NULL);
1384 for (i = 0; i < size; i++)
1385 mono_array_set (res, gpointer, i, native_trace [i]);
1393 setup_stack_trace (MonoException *mono_ex, GSList *dynamic_methods, MonoArray *initial_trace_ips, GList **trace_ips)
1395 if (mono_ex && !initial_trace_ips) {
1396 *trace_ips = g_list_reverse (*trace_ips);
1398 MonoArray *ips_arr = mono_glist_to_array (*trace_ips, mono_defaults.int_class, &error);
1399 mono_error_assert_ok (&error);
1400 MONO_OBJECT_SETREF (mono_ex, trace_ips, ips_arr);
1401 MONO_OBJECT_SETREF (mono_ex, native_trace_ips, build_native_trace (&error));
1402 mono_error_assert_ok (&error);
1403 if (dynamic_methods) {
1404 /* These methods could go away anytime, so save a reference to them in the exception object */
1406 MonoMList *list = NULL;
1408 for (l = dynamic_methods; l; l = l->next) {
1410 MonoDomain *domain = mono_domain_get ();
1412 if (domain->method_to_dyn_method) {
1413 mono_domain_lock (domain);
1414 dis_link = (guint32)(size_t)g_hash_table_lookup (domain->method_to_dyn_method, l->data);
1415 mono_domain_unlock (domain);
1417 MonoObject *o = mono_gchandle_get_target (dis_link);
1419 list = mono_mlist_prepend_checked (list, o, &error);
1420 mono_error_assert_ok (&error);
1426 MONO_OBJECT_SETREF (mono_ex, dynamic_methods, list);
1429 g_list_free (*trace_ips);
1434 * mono_handle_exception_internal_first_pass:
1436 * The first pass of exception handling. Unwind the stack until a catch clause which can catch
1437 * OBJ is found. Run the index of the filter clause which caught the exception into
1438 * OUT_FILTER_IDX. Return TRUE if the exception is caught, FALSE otherwise.
1441 mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoJitInfo **out_prev_ji, MonoObject *non_exception)
1444 MonoDomain *domain = mono_domain_get ();
1445 MonoJitInfo *ji = NULL;
1446 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1447 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1448 MonoLMF *lmf = mono_get_lmf ();
1449 MonoArray *initial_trace_ips = NULL;
1450 GList *trace_ips = NULL;
1451 GSList *dynamic_methods = NULL;
1452 MonoException *mono_ex;
1453 gboolean stack_overflow = FALSE;
1454 MonoContext initial_ctx;
1456 int frame_count = 0;
1461 g_assert (ctx != NULL);
1463 if (obj == (MonoObject *)domain->stack_overflow_ex)
1464 stack_overflow = TRUE;
1466 mono_ex = (MonoException*)obj;
1467 initial_trace_ips = mono_ex->trace_ips;
1469 if (mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1470 mono_ex = (MonoException*)obj;
1471 initial_trace_ips = mono_ex->trace_ips;
1473 mono_error_assert_ok (&error);
1478 call_filter = (int (*) (MonoContext *, void *))mono_get_call_filter ();
1480 g_assert (jit_tls->end_of_stack);
1481 g_assert (jit_tls->abort_func);
1484 *out_filter_idx = -1;
1488 *out_prev_ji = NULL;
1493 MonoContext new_ctx;
1495 int clause_index_start = 0;
1496 gboolean unwind_res = TRUE;
1498 StackFrameInfo frame;
1503 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1505 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1506 frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1507 frame.type == FRAME_TYPE_TRAMPOLINE) {
1511 g_assert (frame.type == FRAME_TYPE_MANAGED);
1516 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1517 g_slist_free (dynamic_methods);
1522 method = jinfo_get_method (ji);
1523 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1525 if (mini_get_debug_options ()->reverse_pinvoke_exceptions && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
1526 g_error ("A native frame was found while unwinding the stack after an exception.\n"
1527 "The native frame called the managed method:\n%s\n",
1528 mono_method_full_name (method, TRUE));
1531 if (method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1533 * Avoid overwriting the stack trace if the exception is
1534 * rethrown. Also avoid giant stack traces during a stack
1537 if (!initial_trace_ips && (frame_count < 1000)) {
1538 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1539 trace_ips = g_list_prepend (trace_ips,
1540 get_generic_info_from_stack_frame (ji, ctx));
1544 if (method->dynamic)
1545 dynamic_methods = g_slist_prepend (dynamic_methods, method);
1547 if (stack_overflow) {
1548 if (MONO_ARCH_STACK_GROWS_UP)
1549 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1551 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1553 free_stack = 0xffffff;
1556 for (i = clause_index_start; i < ji->num_clauses; i++) {
1557 MonoJitExceptionInfo *ei = &ji->clauses [i];
1558 gboolean filtered = FALSE;
1561 * During stack overflow, wait till the unwinding frees some stack
1562 * space before running handlers/finalizers.
1564 if (free_stack <= (64 * 1024))
1567 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1569 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1572 * Have to unwrap RuntimeWrappedExceptions if the
1573 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1575 if (non_exception && !wrap_non_exception_throws (method))
1576 ex_obj = non_exception;
1580 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1581 gboolean is_user_frame = method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD;
1582 #ifndef DISABLE_PERFCOUNTERS
1583 mono_perfcounters->exceptions_filters++;
1586 Here's the thing, if this is a filter clause done by a wrapper like runtime invoke, we don't want to
1587 trim the stackframe since if it returns FALSE we lose information.
1589 FIXME Not 100% sure if it's a good idea even with user clauses.
1592 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1594 #ifndef MONO_CROSS_COMPILE
1595 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1597 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1599 /* Can't pass the ex object in a register yet to filter clauses, because call_filter () might not support it */
1600 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1602 g_assert (!ji->from_llvm);
1603 /* store the exception object in bp + ei->exvar_offset */
1604 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1608 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
1610 * Pass the original il clause index to the landing pad so it can
1611 * branch to the landing pad associated with the il clause.
1612 * This is needed because llvm compiled code assumes that the EH
1613 * code always branches to the innermost landing pad.
1616 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
1619 mono_debugger_agent_begin_exception_filter (mono_ex, ctx, &initial_ctx);
1620 filtered = call_filter (ctx, ei->data.filter);
1621 mono_debugger_agent_end_exception_filter (mono_ex, ctx, &initial_ctx);
1622 if (filtered && out_filter_idx)
1623 *out_filter_idx = filter_idx;
1630 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1631 g_slist_free (dynamic_methods);
1632 /* mono_debugger_agent_handle_exception () needs this */
1633 mini_set_abort_threshold (ctx);
1634 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1639 MonoError isinst_error;
1640 mono_error_init (&isinst_error);
1641 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (ex_obj, catch_class, &error)) {
1642 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1643 g_slist_free (dynamic_methods);
1648 /* mono_debugger_agent_handle_exception () needs this */
1649 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1652 mono_error_cleanup (&isinst_error);
1659 g_assert_not_reached ();
1663 * mono_handle_exception_internal:
1664 * @ctx: saved processor state
1665 * @obj: the exception object
1666 * @resume: whenever to resume unwinding based on the state in MonoJitTlsData.
1669 mono_handle_exception_internal (MonoContext *ctx, MonoObject *obj, gboolean resume, MonoJitInfo **out_ji)
1672 MonoDomain *domain = mono_domain_get ();
1673 MonoJitInfo *ji, *prev_ji;
1674 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1675 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1676 MonoLMF *lmf = mono_get_lmf ();
1677 MonoException *mono_ex;
1678 gboolean stack_overflow = FALSE;
1679 MonoContext initial_ctx;
1681 int frame_count = 0;
1682 gint32 filter_idx, first_filter_idx = 0;
1685 MonoObject *non_exception = NULL;
1687 g_assert (ctx != NULL);
1689 MonoException *ex = mono_get_exception_null_reference ();
1690 MONO_OBJECT_SETREF (ex, message, mono_string_new (domain, "Object reference not set to an instance of an object"));
1691 obj = (MonoObject *)ex;
1695 * Allocate a new exception object instead of the preconstructed ones.
1697 if (obj == (MonoObject *)domain->stack_overflow_ex) {
1699 * It is not a good idea to try and put even more pressure on the little stack available.
1700 * obj = mono_get_exception_stack_overflow ();
1702 stack_overflow = TRUE;
1704 else if (obj == (MonoObject *)domain->null_reference_ex) {
1705 obj = (MonoObject *)mono_get_exception_null_reference ();
1708 if (!mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1709 mono_error_assert_ok (&error);
1710 non_exception = obj;
1711 obj = (MonoObject *)mono_get_exception_runtime_wrapped_checked (obj, &error);
1712 mono_error_assert_ok (&error);
1715 mono_ex = (MonoException*)obj;
1717 if (mini_get_debug_options ()->suspend_on_exception) {
1718 mono_runtime_printf_err ("Exception thrown, suspending...");
1723 if (mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1724 mono_ex = (MonoException*)obj;
1726 mono_error_assert_ok (&error);
1730 if (mono_ex && jit_tls->class_cast_from) {
1731 if (!strcmp (mono_ex->object.vtable->klass->name, "InvalidCastException")) {
1732 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1733 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1734 char *msg = g_strdup_printf ("Unable to cast object of type '%s' to type '%s'.", from_name, to_name);
1735 mono_ex->message = mono_string_new (domain, msg);
1740 if (!strcmp (mono_ex->object.vtable->klass->name, "ArrayTypeMismatchException")) {
1741 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1742 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1743 char *msg = g_strdup_printf ("Source array of type '%s' cannot be cast to destination array type '%s'.", from_name, to_name);
1744 mono_ex->message = mono_string_new (domain, msg);
1752 call_filter = (int (*)(MonoContext *, void*))mono_get_call_filter ();
1754 g_assert (jit_tls->end_of_stack);
1755 g_assert (jit_tls->abort_func);
1758 * We set orig_ex_ctx_set to TRUE/FALSE around profiler calls to make sure it doesn't
1759 * end up being TRUE on any code path.
1761 memcpy (&jit_tls->orig_ex_ctx, ctx, sizeof (MonoContext));
1766 MonoContext ctx_cp = *ctx;
1767 if (mono_trace_is_enabled ()) {
1768 MonoMethod *system_exception_get_message = mono_class_get_method_from_name (mono_defaults.exception_class, "get_Message", 0);
1769 MonoMethod *get_message = system_exception_get_message == NULL ? NULL : mono_object_get_virtual_method (obj, system_exception_get_message);
1770 MonoObject *message;
1771 const char *type_name = mono_class_get_name (mono_object_class (mono_ex));
1773 if (get_message == NULL) {
1775 } else if (!strcmp (type_name, "OutOfMemoryException") || !strcmp (type_name, "StackOverflowException")) {
1777 msg = g_strdup_printf ("(No exception message for: %s)\n", type_name);
1779 MonoObject *exc = NULL;
1780 message = mono_runtime_try_invoke (get_message, obj, NULL, &exc, &error);
1781 g_assert (exc == NULL);
1782 mono_error_assert_ok (&error);
1786 msg = mono_string_to_utf8_checked ((MonoString *) message, &error);
1787 if (!is_ok (&error)) {
1788 mono_error_cleanup (&error);
1789 msg = g_strdup ("(error while display System.Exception.Message property)");
1792 msg = g_strdup ("(System.Exception.Message property not available)");
1795 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);
1797 if (mono_ex && mono_trace_eval_exception (mono_object_class (mono_ex)))
1798 mono_print_thread_dump_from_ctx (ctx);
1800 jit_tls->orig_ex_ctx_set = TRUE;
1801 mono_profiler_exception_thrown (obj);
1802 jit_tls->orig_ex_ctx_set = FALSE;
1804 res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, &first_filter_idx, &ji, &prev_ji, non_exception);
1807 if (mini_get_debug_options ()->break_on_exc)
1809 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1811 if (mini_get_debug_options ()->suspend_on_unhandled) {
1812 mono_runtime_printf_err ("Unhandled exception, suspending...");
1817 // FIXME: This runs managed code so it might cause another stack overflow when
1818 // we are handling a stack overflow
1819 mini_set_abort_threshold (ctx);
1820 mono_unhandled_exception (obj);
1822 gboolean unhandled = FALSE;
1825 * The exceptions caught by the mono_runtime_invoke_checked () calls
1826 * in the threadpool needs to be treated as unhandled (#669836).
1828 * FIXME: The check below is hackish, but its hard to distinguish
1829 * these runtime invoke calls from others in the runtime.
1831 if (ji && jinfo_get_method (ji)->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1832 if (prev_ji && jinfo_get_method (prev_ji) == mono_defaults.threadpool_perform_wait_callback_method)
1837 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1839 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, &ctx_cp);
1849 MonoContext new_ctx;
1851 int clause_index_start = 0;
1852 gboolean unwind_res = TRUE;
1856 ji = jit_tls->resume_state.ji;
1857 new_ctx = jit_tls->resume_state.new_ctx;
1858 clause_index_start = jit_tls->resume_state.clause_index;
1859 lmf = jit_tls->resume_state.lmf;
1860 first_filter_idx = jit_tls->resume_state.first_filter_idx;
1861 filter_idx = jit_tls->resume_state.filter_idx;
1863 StackFrameInfo frame;
1865 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1867 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1868 frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1869 frame.type == FRAME_TYPE_TRAMPOLINE) {
1873 g_assert (frame.type == FRAME_TYPE_MANAGED);
1879 *(mono_get_lmf_addr ()) = lmf;
1881 jit_tls->abort_func (obj);
1882 g_assert_not_reached ();
1885 method = jinfo_get_method (ji);
1887 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1889 if (stack_overflow) {
1890 if (MONO_ARCH_STACK_GROWS_UP)
1891 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1893 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1895 free_stack = 0xffffff;
1898 for (i = clause_index_start; i < ji->num_clauses; i++) {
1899 MonoJitExceptionInfo *ei = &ji->clauses [i];
1900 gboolean filtered = FALSE;
1903 * During stack overflow, wait till the unwinding frees some stack
1904 * space before running handlers/finalizers.
1906 if (free_stack <= (64 * 1024))
1909 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1911 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1914 * Have to unwrap RuntimeWrappedExceptions if the
1915 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1917 if (non_exception && !wrap_non_exception_throws (method))
1918 ex_obj = non_exception;
1922 if (((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER))) {
1923 #ifndef MONO_CROSS_COMPILE
1924 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1925 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1927 g_assert (!ji->from_llvm);
1928 /* store the exception object in bp + ei->exvar_offset */
1929 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1934 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
1936 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
1939 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1941 * Filter clauses should only be run in the
1942 * first pass of exception handling.
1944 filtered = (filter_idx == first_filter_idx);
1948 mono_error_init (&error);
1949 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1950 mono_object_isinst_checked (ex_obj, catch_class, &error)) || filtered) {
1952 * This guards against the situation that we abort a thread that is executing a finally clause
1953 * that was called by the EH machinery. It won't have a guard trampoline installed, so we must
1954 * check for this situation here and resume interruption if we are below the guarded block.
1956 if (G_UNLIKELY (jit_tls->handler_block_return_address)) {
1957 gboolean is_outside = FALSE;
1958 gpointer prot_bp = MONO_CONTEXT_GET_BP (&jit_tls->handler_block_context);
1959 gpointer catch_bp = MONO_CONTEXT_GET_BP (ctx);
1960 //FIXME make this stack direction aware
1961 if (catch_bp > prot_bp) {
1963 } else if (catch_bp == prot_bp) {
1964 /* Can be either try { try { } catch {} } finally {} or try { try { } finally {} } catch {}
1965 * So we check if the catch handler_start is protected by the guarded handler protected region
1968 * If there is an outstanding guarded_block return address, it means the current thread must be aborted.
1969 * This is the only way to reach out the guarded block as other cases are handled by the trampoline.
1970 * There aren't any further finally/fault handler blocks down the stack over this exception.
1971 * This must be ensured by the code that installs the guard trampoline.
1973 g_assert (ji == mini_jit_info_table_find (domain, (char *)MONO_CONTEXT_GET_IP (&jit_tls->handler_block_context), NULL));
1975 if (!is_address_protected (ji, jit_tls->handler_block, ei->handler_start)) {
1980 jit_tls->handler_block_return_address = NULL;
1981 jit_tls->handler_block = NULL;
1982 mono_thread_resume_interruption (); /*We ignore the exception here, it will be raised later*/
1986 if (mono_trace_is_enabled () && mono_trace_eval (method))
1987 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1988 jit_tls->orig_ex_ctx_set = TRUE;
1989 mono_profiler_exception_clause_handler (method, ei->flags, i);
1990 jit_tls->orig_ex_ctx_set = FALSE;
1991 mini_set_abort_threshold (ctx);
1992 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1994 #ifndef DISABLE_PERFCOUNTERS
1995 mono_perfcounters->exceptions_depth += frame_count;
1997 if (obj == (MonoObject *)domain->stack_overflow_ex)
1998 jit_tls->handling_stack_ovf = FALSE;
2002 mono_error_cleanup (&error);
2003 if (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
2004 if (mono_trace_is_enabled () && mono_trace_eval (method))
2005 g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (method, TRUE));
2006 jit_tls->orig_ex_ctx_set = TRUE;
2007 mono_profiler_exception_clause_handler (method, ei->flags, i);
2008 jit_tls->orig_ex_ctx_set = FALSE;
2009 mini_set_abort_threshold (ctx);
2010 call_filter (ctx, ei->handler_start);
2012 if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
2013 if (mono_trace_is_enabled () && mono_trace_eval (method))
2014 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (method, TRUE));
2015 jit_tls->orig_ex_ctx_set = TRUE;
2016 mono_profiler_exception_clause_handler (method, ei->flags, i);
2017 jit_tls->orig_ex_ctx_set = FALSE;
2018 #ifndef DISABLE_PERFCOUNTERS
2019 mono_perfcounters->exceptions_finallys++;
2022 if (ji->from_llvm) {
2024 * LLVM compiled finally handlers follow the design
2025 * of the c++ ehabi, i.e. they call a resume function
2026 * at the end instead of returning to the caller.
2027 * So save the exception handling state,
2028 * mono_resume_unwind () will call us again to continue
2031 jit_tls->resume_state.ex_obj = obj;
2032 jit_tls->resume_state.ji = ji;
2033 jit_tls->resume_state.clause_index = i + 1;
2034 jit_tls->resume_state.ctx = *ctx;
2035 jit_tls->resume_state.new_ctx = new_ctx;
2036 jit_tls->resume_state.lmf = lmf;
2037 jit_tls->resume_state.first_filter_idx = first_filter_idx;
2038 jit_tls->resume_state.filter_idx = filter_idx;
2039 mini_set_abort_threshold (ctx);
2040 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
2043 mini_set_abort_threshold (ctx);
2044 call_filter (ctx, ei->handler_start);
2050 jit_tls->orig_ex_ctx_set = TRUE;
2051 mono_profiler_exception_method_leave (method);
2052 jit_tls->orig_ex_ctx_set = FALSE;
2057 g_assert_not_reached ();
2061 * mono_debugger_run_finally:
2062 * @start_ctx: saved processor state
2064 * This method is called by the Mono Debugger to call all `finally' clauses of the
2065 * current stack frame. It's used when the user issues a `return' command to make
2066 * the current stack frame return. After returning from this method, the debugger
2067 * unwinds the stack one frame and gives control back to the user.
2069 * NOTE: This method is only used when running inside the Mono Debugger.
2072 mono_debugger_run_finally (MonoContext *start_ctx)
2074 static int (*call_filter) (MonoContext *, gpointer) = NULL;
2075 MonoDomain *domain = mono_domain_get ();
2076 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2077 MonoLMF *lmf = mono_get_lmf ();
2078 MonoContext ctx, new_ctx;
2079 MonoJitInfo *ji, rji;
2084 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
2085 if (!ji || ji == (gpointer)-1)
2089 call_filter = (int (*)(MonoContext *, void *))mono_get_call_filter ();
2091 for (i = 0; i < ji->num_clauses; i++) {
2092 MonoJitExceptionInfo *ei = &ji->clauses [i];
2094 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (&ctx)) &&
2095 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
2096 call_filter (&ctx, ei->handler_start);
2102 * mono_handle_exception:
2103 * @ctx: saved processor state
2104 * @obj: the exception object
2107 mono_handle_exception (MonoContext *ctx, MonoObject *obj)
2109 MONO_REQ_GC_UNSAFE_MODE;
2111 #ifndef DISABLE_PERFCOUNTERS
2112 mono_perfcounters->exceptions_thrown++;
2115 return mono_handle_exception_internal (ctx, obj, FALSE, NULL);
2118 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2120 #ifndef MONO_ARCH_USE_SIGACTION
2121 #error "Can't use sigaltstack without sigaction"
2124 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
2127 mono_setup_altstack (MonoJitTlsData *tls)
2131 guint8 *staddr = NULL;
2133 if (mono_running_on_valgrind ())
2136 mono_thread_info_get_stack_bounds (&staddr, &stsize);
2140 tls->end_of_stack = staddr + stsize;
2141 tls->stack_size = stsize;
2143 /*g_print ("thread %p, stack_base: %p, stack_size: %d\n", (gpointer)pthread_self (), staddr, stsize);*/
2145 tls->stack_ovf_guard_base = staddr + mono_pagesize ();
2146 tls->stack_ovf_guard_size = ALIGN_TO (8 * 4096, mono_pagesize ());
2148 g_assert ((guint8*)&sa >= (guint8*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size);
2150 if (mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE)) {
2151 /* mprotect can fail for the main thread stack */
2152 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);
2153 g_assert (gaddr == tls->stack_ovf_guard_base);
2154 tls->stack_ovf_valloced = TRUE;
2157 /* Setup an alternate signal stack */
2158 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);
2159 tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2161 g_assert (tls->signal_stack);
2163 sa.ss_sp = tls->signal_stack;
2164 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2166 g_assert (sigaltstack (&sa, NULL) == 0);
2168 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);
2172 mono_free_altstack (MonoJitTlsData *tls)
2177 sa.ss_sp = tls->signal_stack;
2178 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2179 sa.ss_flags = SS_DISABLE;
2180 err = sigaltstack (&sa, NULL);
2181 g_assert (err == 0);
2183 if (tls->signal_stack)
2184 mono_vfree (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE, MONO_MEM_ACCOUNT_EXCEPTIONS);
2185 if (tls->stack_ovf_valloced)
2186 mono_vfree (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MEM_ACCOUNT_EXCEPTIONS);
2188 mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_READ|MONO_MMAP_WRITE);
2191 #else /* !MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2194 mono_setup_altstack (MonoJitTlsData *tls)
2199 mono_free_altstack (MonoJitTlsData *tls)
2203 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2206 try_restore_stack_protection (MonoJitTlsData *jit_tls, int extra_bytes)
2208 gint32 unprotect_size = jit_tls->stack_ovf_guard_size;
2209 /* we need to leave some room for throwing the exception */
2210 while (unprotect_size >= 0 && (char*)jit_tls->stack_ovf_guard_base + unprotect_size > ((char*)&unprotect_size - extra_bytes))
2211 unprotect_size -= mono_pagesize ();
2212 /* at this point we could try and build a new domain->stack_overflow_ex, but only if there
2213 * is sufficient stack
2215 //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);
2217 mono_mprotect (jit_tls->stack_ovf_guard_base, unprotect_size, MONO_MMAP_NONE);
2218 return unprotect_size == jit_tls->stack_ovf_guard_size;
2221 static G_GNUC_UNUSED void
2222 try_more_restore (void)
2224 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2225 if (try_restore_stack_protection (jit_tls, 500))
2226 jit_tls->restore_stack_prot = NULL;
2229 static G_GNUC_UNUSED void
2230 restore_stack_protection (void)
2232 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2233 MonoException *ex = mono_domain_get ()->stack_overflow_ex;
2234 /* if we can't restore the stack protection, keep a callback installed so
2235 * we'll try to restore as much stack as we can at each return from unmanaged
2238 if (try_restore_stack_protection (jit_tls, 4096))
2239 jit_tls->restore_stack_prot = NULL;
2241 jit_tls->restore_stack_prot = try_more_restore_tramp;
2242 /* here we also throw a stack overflow exception */
2243 ex->trace_ips = NULL;
2244 ex->stack_trace = NULL;
2245 mono_raise_exception (ex);
2249 mono_altstack_restore_prot (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
2251 MONO_REQ_GC_UNSAFE_MODE;
2253 void (*func)(void) = (void (*)(void))tramp_data;
2259 mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, guint8* fault_addr)
2264 /* we got a stack overflow in the soft-guard pages
2265 * There are two cases:
2266 * 1) managed code caused the overflow: we unprotect the soft-guard page
2267 * and let the arch-specific code trigger the exception handling mechanism
2268 * in the thread stack. The soft-guard pages will be protected again as the stack is unwound.
2269 * 2) unmanaged code caused the overflow: we unprotect the soft-guard page
2270 * and hope we can continue with those enabled, at least until the hard-guard page
2271 * is hit. The alternative to continuing here is to just print a message and abort.
2272 * We may add in the future the code to protect the pages again in the codepath
2273 * when we return from unmanaged to managed code.
2275 if (jit_tls->stack_ovf_guard_size && fault_addr >= (guint8*)jit_tls->stack_ovf_guard_base &&
2276 fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size) {
2277 /* we unprotect the minimum amount we can */
2279 gboolean handled = FALSE;
2281 guard_size = jit_tls->stack_ovf_guard_size - (mono_pagesize () * SIZEOF_VOID_P / 4);
2282 while (guard_size && fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + guard_size) {
2283 guard_size -= mono_pagesize ();
2285 guard_size = jit_tls->stack_ovf_guard_size - guard_size;
2286 /*fprintf (stderr, "unprotecting: %d\n", guard_size);*/
2287 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);
2288 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2290 mono_arch_handle_altstack_exception (ctx, siginfo, fault_addr, TRUE);
2295 /* We print a message: after this even managed stack overflows
2296 * may crash the runtime
2298 mono_runtime_printf_err ("Stack overflow in unmanaged: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2299 if (!jit_tls->handling_stack_ovf) {
2300 jit_tls->restore_stack_prot = restore_stack_protection_tramp;
2301 jit_tls->handling_stack_ovf = 1;
2303 /*fprintf (stderr, "Already handling stack overflow\n");*/
2312 MonoMethod *omethod;
2314 } PrintOverflowUserData;
2316 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2318 print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2320 MonoMethod *method = NULL;
2321 PrintOverflowUserData *user_data = (PrintOverflowUserData *)data;
2324 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2325 method = jinfo_get_method (frame->ji);
2328 if (user_data->count == 0) {
2329 /* The first frame is in its prolog, so a line number cannot be computed */
2330 user_data->count ++;
2334 /* If this is a one method overflow, skip the other instances */
2335 if (method == user_data->omethod)
2338 location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2339 mono_runtime_printf_err (" %s", location);
2342 if (user_data->count == 1) {
2343 mono_runtime_printf_err (" <...>");
2344 user_data->omethod = method;
2346 user_data->omethod = NULL;
2349 user_data->count ++;
2351 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2358 mono_handle_hard_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr)
2360 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2361 PrintOverflowUserData ud;
2365 /* we don't do much now, but we can warn the user with a useful message */
2366 mono_runtime_printf_err ("Stack overflow: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2368 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2369 mono_sigctx_to_monoctx (ctx, &mctx);
2371 mono_runtime_printf_err ("Stacktrace:");
2373 memset (&ud, 0, sizeof (ud));
2375 mono_walk_stack_with_ctx (print_overflow_stack_frame, &mctx, MONO_UNWIND_LOOKUP_ACTUAL_METHOD, &ud);
2377 if (ji && !ji->is_trampoline && jinfo_get_method (ji))
2378 mono_runtime_printf_err ("At %s", mono_method_full_name (jinfo_get_method (ji), TRUE));
2380 mono_runtime_printf_err ("At <unmanaged>.");
2387 print_stack_frame_to_stderr (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2389 MonoMethod *method = NULL;
2391 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2392 method = jinfo_get_method (frame->ji);
2395 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2396 mono_runtime_printf_err (" %s", location);
2399 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2404 static G_GNUC_UNUSED gboolean
2405 print_stack_frame_to_string (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2407 GString *p = (GString*)data;
2408 MonoMethod *method = NULL;
2410 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2411 method = jinfo_get_method (frame->ji);
2413 if (method && frame->domain) {
2414 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, frame->domain);
2415 g_string_append_printf (p, " %s\n", location);
2418 g_string_append_printf (p, " at <unknown> <0x%05x>\n", frame->native_offset);
2423 #ifndef MONO_CROSS_COMPILE
2425 static gboolean handling_sigsegv = FALSE;
2428 * mono_handle_native_crash:
2430 * Handle a native crash (e.g. SIGSEGV) while in native code by
2431 * printing diagnostic information and aborting.
2434 mono_handle_native_crash (const char *signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2436 #ifdef MONO_ARCH_USE_SIGACTION
2437 struct sigaction sa;
2439 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2441 if (handling_sigsegv)
2444 if (mini_get_debug_options ()->suspend_on_sigsegv) {
2445 mono_runtime_printf_err ("Received %s, suspending...", signal);
2456 /* To prevent infinite loops when the stack walk causes a crash */
2457 handling_sigsegv = TRUE;
2459 /* !jit_tls means the thread was not registered with the runtime */
2460 if (jit_tls && mono_thread_internal_current ()) {
2461 mono_runtime_printf_err ("Stacktrace:\n");
2463 /* FIXME: Is MONO_UNWIND_LOOKUP_IL_OFFSET correct here? */
2464 mono_walk_stack (print_stack_frame_to_stderr, MONO_UNWIND_LOOKUP_IL_OFFSET, NULL);
2467 #ifdef HAVE_BACKTRACE_SYMBOLS
2473 mono_runtime_printf_err ("\nNative stacktrace:\n");
2475 size = backtrace (array, 256);
2476 names = backtrace_symbols (array, size);
2477 for (i =0; i < size; ++i) {
2478 mono_runtime_printf_err ("\t%s", names [i]);
2482 /* Try to get more meaningful information using gdb */
2484 #if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_fork)
2485 if (!mini_get_debug_options ()->no_gdb_backtrace) {
2486 /* From g_spawn_command_line_sync () in eglib */
2489 pid_t crashed_pid = getpid ();
2493 * glibc fork acquires some locks, so if the crash happened inside malloc/free,
2494 * it will deadlock. Call the syscall directly instead.
2496 pid = mono_runtime_syscall_fork ();
2497 #if defined (HAVE_PRCTL) && defined(PR_SET_PTRACER)
2499 // Allow gdb to attach to the process even if ptrace_scope sysctl variable is set to
2500 // a value other than 0 (the most permissive ptrace scope). Most modern Linux
2501 // distributions set the scope to 1 which allows attaching only to direct children of
2502 // the current process
2503 prctl (PR_SET_PTRACER, pid, 0, 0, 0);
2507 dup2 (STDERR_FILENO, STDOUT_FILENO);
2509 mono_gdb_render_native_backtraces (crashed_pid);
2513 mono_runtime_printf_err ("\nDebug info from gdb:\n");
2514 waitpid (pid, &status, 0);
2519 mono_exception_native_unwind (ctx, info);
2523 * A SIGSEGV indicates something went very wrong so we can no longer depend
2524 * on anything working. So try to print out lots of diagnostics, starting
2525 * with ones which have a greater chance of working.
2527 mono_runtime_printf_err (
2529 "=================================================================\n"
2530 "Got a %s while executing native code. This usually indicates\n"
2531 "a fatal error in the mono runtime or one of the native libraries \n"
2532 "used by your application.\n"
2533 "=================================================================\n",
2537 #ifdef MONO_ARCH_USE_SIGACTION
2539 /* Remove our SIGABRT handler */
2540 sa.sa_handler = SIG_DFL;
2541 sigemptyset (&sa.sa_mask);
2544 g_assert (sigaction (SIGABRT, &sa, NULL) != -1);
2548 if (!mono_do_crash_chaining) {
2549 /*Android abort is a fluke, it doesn't abort, it triggers another segv. */
2550 #if defined (PLATFORM_ANDROID)
2561 mono_handle_native_crash (const char *signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2563 g_assert_not_reached ();
2566 #endif /* !MONO_CROSS_COMPILE */
2569 mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
2571 MonoInternalThread *thread = mono_thread_internal_current ();
2572 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2577 GError *error = NULL;
2582 text = g_string_new (0);
2584 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
2586 g_string_append_printf (text, "\n\"%s\"", name);
2589 else if (thread->threadpool_thread)
2590 g_string_append (text, "\n\"<threadpool thread>\"");
2592 g_string_append (text, "\n\"<unnamed thread>\"");
2594 g_string_append_printf (text, " tid=0x%p this=0x%p ", (gpointer)(gsize)thread->tid, thread);
2595 mono_thread_internal_describe (thread, text);
2596 g_string_append (text, "\n");
2598 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2600 memcpy (&ctx, start_ctx, sizeof (MonoContext));
2602 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_print_thread_dump);
2604 mono_sigctx_to_monoctx (sigctx, &ctx);
2606 mono_walk_stack_with_ctx (print_stack_frame_to_string, &ctx, MONO_UNWIND_LOOKUP_ALL, text);
2608 mono_runtime_printf ("\t<Stack traces in thread dumps not supported on this platform>");
2611 mono_runtime_printf ("%s", text->str);
2613 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
2614 OutputDebugStringA(text->str);
2617 g_string_free (text, TRUE);
2618 mono_runtime_stdout_fflush ();
2622 * mono_print_thread_dump:
2624 * Print information about the current thread to stdout.
2625 * SIGCTX can be NULL, allowing this to be called from gdb.
2628 mono_print_thread_dump (void *sigctx)
2630 mono_print_thread_dump_internal (sigctx, NULL);
2634 mono_print_thread_dump_from_ctx (MonoContext *ctx)
2636 mono_print_thread_dump_internal (NULL, ctx);
2640 * mono_resume_unwind:
2642 * This is called by a trampoline from LLVM compiled finally clauses to continue
2646 mono_resume_unwind (MonoContext *ctx)
2648 MONO_REQ_GC_UNSAFE_MODE;
2650 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2651 MonoContext new_ctx;
2653 MONO_CONTEXT_SET_IP (ctx, MONO_CONTEXT_GET_IP (&jit_tls->resume_state.ctx));
2654 MONO_CONTEXT_SET_SP (ctx, MONO_CONTEXT_GET_SP (&jit_tls->resume_state.ctx));
2657 mono_handle_exception_internal (&new_ctx, (MonoObject *)jit_tls->resume_state.ex_obj, TRUE, NULL);
2659 mono_restore_context (&new_ctx);
2662 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
2667 MonoJitExceptionInfo *ei;
2668 } FindHandlerBlockData;
2671 find_last_handler_block (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2675 FindHandlerBlockData *pdata = (FindHandlerBlockData *)data;
2676 MonoJitInfo *ji = frame->ji;
2681 ip = MONO_CONTEXT_GET_IP (ctx);
2683 for (i = 0; i < ji->num_clauses; ++i) {
2684 MonoJitExceptionInfo *ei = ji->clauses + i;
2685 if (ei->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2687 /*If ip points to the first instruction it means the handler block didn't start
2688 so we can leave its execution to the EH machinery*/
2689 if (ei->handler_start < ip && ip < ei->data.handler_end) {
2701 install_handler_block_guard (MonoJitInfo *ji, MonoContext *ctx)
2704 MonoJitExceptionInfo *clause = NULL;
2707 ip = MONO_CONTEXT_GET_IP (ctx);
2709 for (i = 0; i < ji->num_clauses; ++i) {
2710 clause = &ji->clauses [i];
2711 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2713 if (clause->handler_start < ip && clause->data.handler_end > ip)
2717 /*no matching finally */
2718 if (i == ji->num_clauses)
2721 /*If we stopped on the instruction right before the try, we haven't actually started executing it*/
2722 if (ip == clause->handler_start)
2725 return mono_arch_install_handler_block_guard (ji, clause, ctx, mono_create_handler_block_trampoline ());
2729 * Finds the bottom handler block running and install a block guard if needed.
2732 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2734 FindHandlerBlockData data = { 0 };
2735 MonoJitTlsData *jit_tls = (MonoJitTlsData *)ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS];
2738 #ifndef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD_AOT
2743 /* Guard against a null MonoJitTlsData. This can happens if the thread receives the
2744 * interrupt signal before the JIT has time to initialize its TLS data for the given thread.
2746 if (!jit_tls || jit_tls->handler_block_return_address)
2749 /* Do an async safe stack walk */
2750 mono_thread_info_set_is_async_context (TRUE);
2751 mono_walk_stack_with_state (find_last_handler_block, ctx, MONO_UNWIND_NONE, &data);
2752 mono_thread_info_set_is_async_context (FALSE);
2757 memcpy (&jit_tls->handler_block_context, &data.ctx, sizeof (MonoContext));
2759 resume_ip = install_handler_block_guard (data.ji, &data.ctx);
2760 if (resume_ip == NULL)
2763 jit_tls->handler_block_return_address = resume_ip;
2764 jit_tls->handler_block = data.ei;
2770 mono_current_thread_has_handle_block_guard (void)
2772 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2773 return jit_tls && jit_tls->handler_block_return_address != NULL;
2778 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2784 mono_current_thread_has_handle_block_guard (void)
2792 mono_set_cast_details (MonoClass *from, MonoClass *to)
2794 MonoJitTlsData *jit_tls = NULL;
2796 if (mini_get_debug_options ()->better_cast_details) {
2797 jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2798 jit_tls->class_cast_from = from;
2799 jit_tls->class_cast_to = to;
2804 /*returns false if the thread is not attached*/
2806 mono_thread_state_init_from_sigctx (MonoThreadUnwindState *ctx, void *sigctx)
2808 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2809 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2812 g_error ("Invoked mono_thread_state_init_from_sigctx from non-Mono thread");
2817 mono_sigctx_to_monoctx (sigctx, &ctx->ctx);
2819 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2820 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2821 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2824 mono_thread_state_init (ctx);
2827 if (!ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] || !ctx->unwind_data [MONO_UNWIND_DATA_LMF])
2833 g_error ("Implement mono_arch_sigctx_to_monoctx for the current target");
2839 mono_thread_state_init (MonoThreadUnwindState *ctx)
2841 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2843 #if defined(MONO_CROSS_COMPILE)
2844 ctx->valid = FALSE; //A cross compiler doesn't need to suspend.
2845 #elif MONO_ARCH_HAS_MONO_CONTEXT
2846 MONO_CONTEXT_GET_CURRENT (ctx->ctx);
2848 g_error ("Use a null sigctx requires a working mono-context");
2851 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2852 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2853 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread ? thread->jit_data : NULL;
2859 mono_thread_state_init_from_monoctx (MonoThreadUnwindState *ctx, MonoContext *mctx)
2861 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2868 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2869 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2870 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2875 /*returns false if the thread is not attached*/
2877 mono_thread_state_init_from_current (MonoThreadUnwindState *ctx)
2879 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2880 MONO_ARCH_CONTEXT_DEF
2882 mono_arch_flush_register_windows ();
2884 if (!thread || !thread->jit_data) {
2888 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
2889 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx->ctx);
2891 MONO_INIT_CONTEXT_FROM_FUNC (&ctx->ctx, mono_thread_state_init_from_current);
2894 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2895 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2896 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2902 mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx)
2904 mono_handle_exception (ctx, (MonoObject *)exc);
2905 mono_restore_context (ctx);
2908 /*FIXME Move all monoctx -> sigctx conversion to signal handlers once all archs support utils/mono-context */
2910 mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
2912 #ifdef MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK
2913 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2914 jit_tls->ex_ctx = *ctx;
2916 mono_arch_setup_async_callback (ctx, async_cb, user_data);
2918 g_error ("This target doesn't support mono_arch_setup_async_callback");
2923 * mono_restore_context:
2925 * Call the architecture specific restore context function.
2928 mono_restore_context (MonoContext *ctx)
2930 static void (*restore_context) (MonoContext *);
2932 if (!restore_context)
2933 restore_context = (void (*)(MonoContext *))mono_get_restore_context ();
2934 restore_context (ctx);
2935 g_assert_not_reached ();
2939 * mono_jinfo_get_unwind_info:
2941 * Return the unwind info for JI.
2944 mono_jinfo_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2947 return mono_aot_get_unwind_info (ji, unwind_info_len);
2949 return mono_get_cached_unwind_info (ji->unwind_info, unwind_info_len);
2953 mono_jinfo_get_epilog_size (MonoJitInfo *ji)
2955 MonoArchEHJitInfo *info;
2957 info = mono_jit_info_get_arch_eh_info (ji);
2960 return info->epilog_size;
2964 * LLVM/Bitcode exception handling.
2968 throw_exception (MonoObject *ex, gboolean rethrow)
2970 MONO_REQ_GC_UNSAFE_MODE;
2973 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
2974 MonoException *mono_ex;
2976 if (!mono_object_isinst_checked (ex, mono_defaults.exception_class, &error)) {
2977 mono_error_assert_ok (&error);
2978 mono_ex = mono_get_exception_runtime_wrapped_checked (ex, &error);
2979 mono_error_assert_ok (&error);
2980 jit_tls->thrown_non_exc = mono_gchandle_new (ex, FALSE);
2983 mono_ex = (MonoException*)ex;
2986 jit_tls->thrown_exc = mono_gchandle_new ((MonoObject*)mono_ex, FALSE);
2989 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
2990 GList *l, *ips = NULL;
2993 _Unwind_Backtrace (build_stack_trace, &ips);
2994 /* The list contains ip-gshared info pairs */
2996 ips = g_list_reverse (ips);
2997 for (l = ips; l; l = l->next) {
2998 trace = g_list_append (trace, l->data);
2999 trace = g_list_append (trace, NULL);
3001 MonoArray *ips_arr = mono_glist_to_array (trace, mono_defaults.int_class, &error);
3002 mono_error_assert_ok (&error);
3003 MONO_OBJECT_SETREF (mono_ex, trace_ips, ips_arr);
3005 g_list_free (trace);
3009 mono_llvm_cpp_throw_exception ();
3013 mono_llvm_throw_exception (MonoObject *ex)
3015 throw_exception (ex, FALSE);
3019 mono_llvm_rethrow_exception (MonoObject *ex)
3021 throw_exception (ex, TRUE);
3025 mono_llvm_raise_exception (MonoException *e)
3027 mono_llvm_throw_exception ((MonoObject*)e);
3031 mono_llvm_throw_corlib_exception (guint32 ex_token_index)
3033 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
3036 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
3038 mono_llvm_throw_exception ((MonoObject*)ex);
3042 * mono_llvm_resume_exception:
3044 * Resume exception propagation.
3047 mono_llvm_resume_exception (void)
3049 mono_llvm_cpp_throw_exception ();
3053 * mono_llvm_load_exception:
3055 * Return the currently thrown exception.
3058 mono_llvm_load_exception (void)
3061 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3063 MonoException *mono_ex = (MonoException*)mono_gchandle_get_target (jit_tls->thrown_exc);
3065 if (mono_ex->trace_ips) {
3066 GList *trace_ips = NULL;
3067 gpointer ip = __builtin_return_address (0);
3069 size_t upper = mono_array_length (mono_ex->trace_ips);
3071 for (int i = 0; i < upper; i+= 2) {
3072 gpointer curr_ip = mono_array_get (mono_ex->trace_ips, gpointer, i);
3073 gpointer curr_info = mono_array_get (mono_ex->trace_ips, gpointer, i + 1);
3074 trace_ips = g_list_append (trace_ips, curr_ip);
3075 trace_ips = g_list_append (trace_ips, curr_info);
3081 // FIXME: Does this work correctly for rethrows?
3082 // We may be discarding useful information
3083 // when this gets GC'ed
3084 MonoArray *ips_arr = mono_glist_to_array (trace_ips, mono_defaults.int_class, &error);
3085 mono_error_assert_ok (&error);
3086 MONO_OBJECT_SETREF (mono_ex, trace_ips, ips_arr);
3087 g_list_free (trace_ips);
3090 //MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
3092 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_array_new_checked (mono_domain_get (), mono_defaults.int_class, 0, &error));
3093 mono_error_assert_ok (&error);
3094 MONO_OBJECT_SETREF (mono_ex, stack_trace, mono_array_new_checked (mono_domain_get (), mono_defaults.stack_frame_class, 0, &error));
3095 mono_error_assert_ok (&error);
3098 return &mono_ex->object;
3102 * mono_llvm_clear_exception:
3104 * Mark the currently thrown exception as handled.
3107 mono_llvm_clear_exception (void)
3109 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3110 mono_gchandle_free (jit_tls->thrown_exc);
3111 jit_tls->thrown_exc = 0;
3112 if (jit_tls->thrown_non_exc)
3113 mono_gchandle_free (jit_tls->thrown_non_exc);
3114 jit_tls->thrown_non_exc = 0;
3116 mono_memory_barrier ();
3120 * mono_llvm_match_exception:
3122 * Return the innermost clause containing REGION_START-REGION_END which can handle
3123 * the current exception.
3126 mono_llvm_match_exception (MonoJitInfo *jinfo, guint32 region_start, guint32 region_end, gpointer rgctx, MonoObject *this_obj)
3129 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3133 g_assert (jit_tls->thrown_exc);
3134 exc = mono_gchandle_get_target (jit_tls->thrown_exc);
3135 if (jit_tls->thrown_non_exc) {
3137 * Have to unwrap RuntimeWrappedExceptions if the
3138 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
3140 if (!wrap_non_exception_throws (jinfo_get_method (jinfo)))
3141 exc = mono_gchandle_get_target (jit_tls->thrown_non_exc);
3144 for (int i = 0; i < jinfo->num_clauses; i++) {
3145 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
3146 MonoClass *catch_class;
3148 if (! (ei->try_offset == region_start && ei->try_offset + ei->try_len == region_end) )
3151 catch_class = ei->data.catch_class;
3152 if (mono_class_is_open_constructed_type (&catch_class->byval_arg)) {
3153 MonoGenericContext context;
3154 MonoType *inflated_type;
3156 g_assert (rgctx || this_obj);
3157 context = get_generic_context_from_stack_frame (jinfo, rgctx ? rgctx : this_obj->vtable);
3158 inflated_type = mono_class_inflate_generic_type_checked (&catch_class->byval_arg, &context, &error);
3159 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
3161 catch_class = mono_class_from_mono_type (inflated_type);
3162 mono_metadata_free_type (inflated_type);
3165 // FIXME: Handle edge cases handled in get_exception_catch_class
3166 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (exc, catch_class, &error)) {
3167 index = ei->clause_index;
3170 mono_error_assert_ok (&error);
3172 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3173 g_assert_not_reached ();
3182 mono_debug_personality (int a, _Unwind_Action b,
3183 uint64_t c, struct _Unwind_Exception *d, struct _Unwind_Context *e)
3185 g_assert_not_reached ();
3189 mono_debug_personality (void);
3192 mono_debug_personality (void)
3194 g_assert_not_reached ();