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/debug-internals.h>
62 #include <mono/metadata/mono-debug.h>
63 #include <mono/metadata/profiler.h>
64 #include <mono/metadata/mono-endian.h>
65 #include <mono/metadata/environment.h>
66 #include <mono/metadata/mono-mlist.h>
67 #include <mono/utils/mono-mmap.h>
68 #include <mono/utils/mono-logger-internals.h>
69 #include <mono/utils/mono-error.h>
70 #include <mono/utils/mono-error-internals.h>
74 #include "debugger-agent.h"
75 #include "seq-points.h"
76 #include "llvm-runtime.h"
77 #include "mini-llvm.h"
78 #include "interp/interp.h"
81 #include "mini-llvm-cpp.h"
84 #ifndef MONO_ARCH_CONTEXT_DEF
85 #define MONO_ARCH_CONTEXT_DEF
88 #ifndef MONO_ARCH_STACK_GROWS_UP
89 #define MONO_ARCH_STACK_GROWS_UP 0
92 static gpointer restore_context_func, call_filter_func;
93 static gpointer throw_exception_func, rethrow_exception_func;
94 static gpointer throw_corlib_exception_func;
96 static gpointer try_more_restore_tramp = NULL;
97 static gpointer restore_stack_protection_tramp = NULL;
99 static void try_more_restore (void);
100 static void restore_stack_protection (void);
101 static void mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data);
102 static void mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx);
103 static void mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data);
104 static gboolean mono_current_thread_has_handle_block_guard (void);
107 first_managed (MonoStackFrameInfo *frame, MonoContext *ctx, gpointer addr)
109 gpointer **data = (gpointer **)addr;
115 // FIXME: Happens with llvm_only
120 *data = MONO_CONTEXT_GET_SP (ctx);
126 mono_thread_get_managed_sp (void)
128 gpointer addr = NULL;
129 mono_walk_stack (first_managed, MONO_UNWIND_SIGNAL_SAFE, &addr);
134 mini_abort_threshold_offset (gpointer threshold, gpointer sp)
136 intptr_t stack_threshold = (intptr_t) threshold;
137 intptr_t stack_pointer = (intptr_t) sp;
139 const int direction = MONO_ARCH_STACK_GROWS_UP ? -1 : 1;
140 intptr_t magnitude = stack_pointer - stack_threshold;
142 return direction * magnitude;
146 mini_clear_abort_threshold (void)
148 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
149 jit_tls->abort_exc_stack_threshold = NULL;
153 mini_set_abort_threshold (MonoContext *ctx)
155 gpointer sp = MONO_CONTEXT_GET_SP (ctx);
156 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
157 // Only move it up, to avoid thrown/caught
158 // exceptions lower in the stack from triggering
160 gboolean above_threshold = mini_abort_threshold_offset (jit_tls->abort_exc_stack_threshold, sp) >= 0;
161 if (!jit_tls->abort_exc_stack_threshold || above_threshold) {
162 jit_tls->abort_exc_stack_threshold = sp;
166 // Note: In the case that the frame is above where the thread abort
167 // was set we bump the threshold so that functions called from the new,
168 // higher threshold don't trigger the thread abort exception
169 static inline gboolean
170 mini_above_abort_threshold (void)
172 gpointer sp = mono_thread_get_managed_sp ();
173 MonoJitTlsData *jit_tls = (MonoJitTlsData*) mono_tls_get_jit_tls ();
178 gboolean above_threshold = mini_abort_threshold_offset (jit_tls->abort_exc_stack_threshold, sp) >= 0;
181 jit_tls->abort_exc_stack_threshold = sp;
183 return above_threshold;
187 mono_get_seq_point_for_native_offset (MonoDomain *domain, MonoMethod *method, gint32 native_offset)
190 if (mono_find_prev_seq_point_for_native_offset (domain, method, native_offset, NULL, &sp))
196 mono_exceptions_init (void)
198 MonoRuntimeExceptionHandlingCallbacks cbs;
200 restore_context_func = mono_aot_get_trampoline ("restore_context");
201 call_filter_func = mono_aot_get_trampoline ("call_filter");
202 throw_exception_func = mono_aot_get_trampoline ("throw_exception");
203 rethrow_exception_func = mono_aot_get_trampoline ("rethrow_exception");
207 restore_context_func = mono_arch_get_restore_context (&info, FALSE);
208 mono_tramp_info_register (info, NULL);
209 call_filter_func = mono_arch_get_call_filter (&info, FALSE);
210 mono_tramp_info_register (info, NULL);
211 throw_exception_func = mono_arch_get_throw_exception (&info, FALSE);
212 mono_tramp_info_register (info, NULL);
213 rethrow_exception_func = mono_arch_get_rethrow_exception (&info, FALSE);
214 mono_tramp_info_register (info, NULL);
216 #ifdef MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT
217 if (!mono_llvm_only) {
218 try_more_restore_tramp = mono_create_specific_trampoline (try_more_restore, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
219 restore_stack_protection_tramp = mono_create_specific_trampoline (restore_stack_protection, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
223 #ifdef MONO_ARCH_HAVE_EXCEPTIONS_INIT
224 mono_arch_exceptions_init ();
226 #ifdef ENABLE_INTERPRETER
227 if (mono_use_interpreter)
228 cbs.mono_walk_stack_with_ctx = interp_walk_stack_with_ctx;
231 cbs.mono_walk_stack_with_ctx = mono_runtime_walk_stack_with_ctx;
233 cbs.mono_walk_stack_with_state = mono_walk_stack_with_state;
236 cbs.mono_raise_exception = mono_llvm_raise_exception;
238 cbs.mono_raise_exception = (void (*)(MonoException *))mono_get_throw_exception ();
239 cbs.mono_raise_exception_with_ctx = mono_raise_exception_with_ctx;
240 cbs.mono_exception_walk_trace = mono_exception_walk_trace;
241 cbs.mono_install_handler_block_guard = mono_install_handler_block_guard;
242 cbs.mono_current_thread_has_handle_block_guard = mono_current_thread_has_handle_block_guard;
243 cbs.mono_clear_abort_threshold = mini_clear_abort_threshold;
244 cbs.mono_above_abort_threshold = mini_above_abort_threshold;
245 mono_install_eh_callbacks (&cbs);
246 mono_install_get_seq_point (mono_get_seq_point_for_native_offset);
250 mono_get_throw_exception (void)
252 g_assert (throw_exception_func);
253 return throw_exception_func;
257 mono_get_rethrow_exception (void)
259 g_assert (rethrow_exception_func);
260 return rethrow_exception_func;
264 mono_get_call_filter (void)
266 g_assert (call_filter_func);
267 return call_filter_func;
271 mono_get_restore_context (void)
273 g_assert (restore_context_func);
274 return restore_context_func;
278 mono_get_throw_corlib_exception (void)
280 gpointer code = NULL;
283 /* This depends on corlib classes so cannot be inited in mono_exceptions_init () */
284 if (throw_corlib_exception_func)
285 return throw_corlib_exception_func;
288 code = mono_aot_get_trampoline ("throw_corlib_exception");
290 code = mono_arch_get_throw_corlib_exception (&info, FALSE);
291 mono_tramp_info_register (info, NULL);
294 mono_memory_barrier ();
296 throw_corlib_exception_func = code;
298 return throw_corlib_exception_func;
302 * mono_get_throw_exception_addr:
304 * Return an address which stores the result of
305 * mono_get_throw_exception.
308 mono_get_throw_exception_addr (void)
310 return &throw_exception_func;
314 is_address_protected (MonoJitInfo *ji, MonoJitExceptionInfo *ei, gpointer ip)
316 MonoTryBlockHoleTableJitInfo *table;
321 if (ei->try_start > ip || ip >= ei->try_end)
324 if (!ji->has_try_block_holes)
327 table = mono_jit_info_get_try_block_hole_table_info (ji);
328 offset = (guint32)((char*)ip - (char*)ji->code_start);
329 clause = (guint16)(ei - ji->clauses);
330 g_assert (clause < ji->num_clauses);
332 for (i = 0; i < table->num_holes; ++i) {
333 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
334 if (hole->clause == clause && hole->offset <= offset && hole->offset + hole->length > offset)
340 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
343 static gboolean show_native_addresses = TRUE;
345 static gboolean show_native_addresses = FALSE;
348 static _Unwind_Reason_Code
349 build_stack_trace (struct _Unwind_Context *frame_ctx, void *state)
351 MonoDomain *domain = mono_domain_get ();
352 uintptr_t ip = _Unwind_GetIP (frame_ctx);
354 if (show_native_addresses || mono_jit_info_table_find (domain, (char*)ip)) {
355 GList **trace_ips = (GList **)state;
356 *trace_ips = g_list_prepend (*trace_ips, (gpointer)ip);
359 return _URC_NO_REASON;
363 get_unwind_backtrace (void)
367 _Unwind_Backtrace (build_stack_trace, &ips);
369 return g_slist_reverse (ips);
375 get_unwind_backtrace (void)
385 * Translate between the mono_arch_unwind_frame function and the old API.
388 find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
389 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
391 StackFrameInfo frame;
394 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
396 /* Avoid costly table lookup during stack overflow */
397 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
400 ji = mini_jit_info_table_find (domain, (char *)ip, NULL);
405 err = mono_arch_unwind_frame (domain, jit_tls, ji, ctx, new_ctx, lmf, NULL, &frame);
407 return (MonoJitInfo *)-1;
409 if (*lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
411 * Remove any unused lmf.
412 * Mask out the lower bits which might be used to hold additional information.
414 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
417 /* Convert between the new and the old APIs */
418 switch (frame.type) {
419 case FRAME_TYPE_MANAGED:
423 case FRAME_TYPE_TRAMPOLINE:
425 case FRAME_TYPE_MANAGED_TO_NATIVE:
429 memset (res, 0, sizeof (MonoJitInfo));
430 res->d.method = frame.method;
433 case FRAME_TYPE_DEBUGGER_INVOKE: {
437 * The normal exception handling code can't handle this frame, so just
440 ji = find_jit_info (domain, jit_tls, res, NULL, new_ctx, &tmp_ctx, lmf, managed);
441 memcpy (new_ctx, &tmp_ctx, sizeof (MonoContext));
445 g_assert_not_reached ();
450 /* mono_find_jit_info:
452 * This function is used to gather information from @ctx. It return the
453 * MonoJitInfo of the corresponding function, unwinds one stack frame and
454 * stores the resulting context into @new_ctx. It also stores a string
455 * describing the stack location into @trace (if not NULL), and modifies
456 * the @lmf if necessary. @native_offset return the IP offset from the
457 * start of the function or -1 if that info is not available.
460 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
461 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
465 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
467 MonoMethod *method = NULL;
478 ji = find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
480 if (ji == (gpointer)-1)
483 if (ji && !ji->is_trampoline)
484 method = jinfo_get_method (ji);
486 if (managed2 || (method && method->wrapper_type)) {
487 const char *real_ip, *start;
490 start = (const char *)ji->code_start;
492 /* ctx->ip points into native code */
493 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
495 real_ip = (const char*)ip;
497 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
498 offset = real_ip - start;
503 *native_offset = offset;
506 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
510 *trace = mono_debug_print_stack_frame (method, offset, domain);
513 char *fname = mono_method_full_name (jinfo_get_method (res), TRUE);
514 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
523 * mono_find_jit_info_ext:
525 * A version of mono_find_jit_info which returns all data in the StackFrameInfo
527 * A note about frames of type FRAME_TYPE_MANAGED_TO_NATIVE:
528 * - These frames are used to mark managed-to-native transitions, so CTX will refer to native
529 * code, and new_ctx will refer to the last managed frame. The caller should unwind once more
530 * to obtain the last managed frame.
531 * If SAVE_LOCATIONS is not NULL, it should point to an array of size MONO_MAX_IREGS.
532 * On return, it will be filled with the locations where callee saved registers are saved
533 * by the current frame. This is returned outside of StackFrameInfo because it can be
534 * quite large on some platforms.
535 * If ASYNC true, this function will be async safe, but some fields of frame and frame->ji will
539 mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
540 MonoJitInfo *prev_ji, MonoContext *ctx,
541 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
542 mgreg_t **save_locations,
543 StackFrameInfo *frame)
546 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
548 MonoDomain *target_domain = domain;
549 MonoMethod *method = NULL;
550 gboolean async = mono_thread_info_is_async_context ();
555 /* Avoid costly table lookup during stack overflow */
556 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
559 ji = mini_jit_info_table_find (domain, (char *)ip, &target_domain);
562 target_domain = domain;
565 memset (save_locations, 0, MONO_MAX_IREGS * sizeof (mgreg_t*));
567 err = mono_arch_unwind_frame (target_domain, jit_tls, ji, ctx, new_ctx, lmf, save_locations, frame);
571 if (frame->type != FRAME_TYPE_INTERP_TO_MANAGED && *lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
573 * Remove any unused lmf.
574 * Mask out the lower bits which might be used to hold additional information.
576 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
579 if (frame->ji && !frame->ji->is_trampoline && !frame->ji->async)
580 method = jinfo_get_method (frame->ji);
582 if (frame->type == FRAME_TYPE_MANAGED && method) {
583 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
584 frame->managed = TRUE;
587 if (frame->type == FRAME_TYPE_MANAGED_TO_NATIVE) {
589 * This type of frame is just a marker, the caller should unwind once more to get the
590 * last managed frame.
593 frame->method = NULL;
596 frame->native_offset = -1;
597 frame->domain = target_domain;
598 frame->async_context = async;
602 if (frame->type == FRAME_TYPE_MANAGED)
603 frame->method = method;
605 if (ji && (frame->managed || (method && method->wrapper_type))) {
606 const char *real_ip, *start;
608 start = (const char *)ji->code_start;
609 if (frame->type == FRAME_TYPE_MANAGED)
610 real_ip = (const char*)ip;
612 /* ctx->ip points into native code */
613 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
615 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
616 frame->native_offset = real_ip - start;
618 frame->native_offset = -1;
622 *trace = mono_debug_print_stack_frame (method, frame->native_offset, domain);
624 if (trace && frame->method) {
625 char *fname = mono_method_full_name (frame->method, TRUE);
626 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
636 MonoInterpStackIter interp_iter;
640 unwinder_init (Unwinder *unwinder)
642 memset (unwinder, 0, sizeof (Unwinder));
645 #if defined(__GNUC__) && defined(TARGET_ARM64)
646 /* gcc 4.9.2 seems to miscompile this on arm64 */
647 static __attribute__((optimize("O0"))) gboolean
651 unwinder_unwind_frame (Unwinder *unwinder,
652 MonoDomain *domain, MonoJitTlsData *jit_tls,
653 MonoJitInfo *prev_ji, MonoContext *ctx,
654 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
655 mgreg_t **save_locations,
656 StackFrameInfo *frame)
658 if (unwinder->in_interp) {
659 unwinder->in_interp = mono_interp_frame_iter_next (&unwinder->interp_iter, frame);
660 if (!unwinder->in_interp) {
661 return unwinder_unwind_frame (unwinder, domain, jit_tls, prev_ji, ctx, new_ctx, trace, lmf, save_locations, frame);
665 gboolean res = mono_find_jit_info_ext (domain, jit_tls, prev_ji, ctx, new_ctx, trace, lmf,
666 save_locations, frame);
669 if (frame->type == FRAME_TYPE_INTERP_TO_MANAGED) {
670 unwinder->in_interp = TRUE;
671 mono_interp_frame_iter_init (&unwinder->interp_iter, frame->interp_exit_data);
678 * This function is async-safe.
681 get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
683 MonoGenericJitInfo *gi;
687 if (!ji->has_generic_jit_info)
689 gi = mono_jit_info_get_generic_jit_info (ji);
695 * Search location list if available, it contains the precise location of the
696 * argument for every pc offset, even if the method was interrupted while it was in
700 int offset = (mgreg_t)MONO_CONTEXT_GET_IP (ctx) - (mgreg_t)ji->code_start;
703 for (i = 0; i < gi->nlocs; ++i) {
704 MonoDwarfLocListEntry *entry = &gi->locations [i];
706 if (offset >= entry->from && (offset < entry->to || entry->to == 0)) {
708 info = (gpointer)mono_arch_context_get_int_reg (ctx, entry->reg);
710 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, entry->reg) + entry->offset);
714 g_assert (i < gi->nlocs);
717 info = (gpointer)mono_arch_context_get_int_reg (ctx, gi->this_reg);
719 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
723 method = jinfo_get_method (ji);
724 if (mono_method_get_context (method)->method_inst) {
725 /* A MonoMethodRuntimeGenericContext* */
727 } else if ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype) {
731 /* Avoid returning a managed object */
732 MonoObject *this_obj = (MonoObject *)info;
734 return this_obj->vtable;
739 * generic_info is either a MonoMethodRuntimeGenericContext or a MonoVTable.
741 static MonoGenericContext
742 get_generic_context_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
744 MonoGenericContext context = { NULL, NULL };
745 MonoClass *klass, *method_container_class;
748 g_assert (generic_info);
750 method = jinfo_get_method (ji);
751 g_assert (method->is_inflated);
752 if (mono_method_get_context (method)->method_inst) {
753 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext *)generic_info;
755 klass = mrgctx->class_vtable->klass;
756 context.method_inst = mrgctx->method_inst;
757 g_assert (context.method_inst);
759 MonoVTable *vtable = (MonoVTable *)generic_info;
761 klass = vtable->klass;
764 //g_assert (!mono_class_is_gtd (method->klass));
765 if (mono_class_is_ginst (method->klass))
766 method_container_class = mono_class_get_generic_class (method->klass)->container_class;
768 method_container_class = method->klass;
770 /* class might refer to a subclass of method's class */
771 while (!(klass == method->klass || (mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->container_class == method_container_class))) {
772 klass = klass->parent;
776 if (mono_class_is_ginst (klass) || mono_class_is_gtd (klass))
777 context.class_inst = mini_class_get_context (klass)->class_inst;
779 if (mono_class_is_ginst (klass))
780 g_assert (mono_class_has_parent_and_ignore_generics (mono_class_get_generic_class (klass)->container_class, method_container_class));
782 g_assert (mono_class_has_parent_and_ignore_generics (klass, method_container_class));
788 get_method_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
791 MonoGenericContext context;
794 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
795 return jinfo_get_method (ji);
796 context = get_generic_context_from_stack_frame (ji, generic_info);
798 method = jinfo_get_method (ji);
799 method = mono_method_get_declaring_generic_method (method);
800 method = mono_class_inflate_generic_method_checked (method, &context, &error);
801 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
807 * mono_exception_walk_native_trace:
808 * \param ex The exception object whose frames should be walked
809 * \param func callback to call for each stack frame
810 * \param user_data data passed to the callback
811 * This function walks the stacktrace of an exception. For
812 * each frame the callback function is called with the relevant info.
813 * The walk ends when no more stack frames are found or when the callback
814 * returns a TRUE value.
818 mono_exception_walk_trace (MonoException *ex, MonoExceptionFrameWalk func, gpointer user_data)
820 MONO_REQ_GC_UNSAFE_MODE;
822 MonoDomain *domain = mono_domain_get ();
823 MonoArray *ta = ex->trace_ips;
829 len = mono_array_length (ta) >> 1;
830 for (i = 0; i < len; i++) {
831 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
832 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
833 MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *)ip);
836 if (func (NULL, ip, 0, FALSE, user_data))
839 MonoMethod *method = get_method_from_stack_frame (ji, generic_info);
840 if (func (method, ji->code_start, (char *) ip - (char *) ji->code_start, TRUE, user_data))
849 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
852 MonoDomain *domain = mono_domain_get ();
854 MonoArray *ta = exc->trace_ips;
855 MonoDebugSourceLocation *location;
859 /* Exception is not thrown yet */
860 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
861 mono_error_set_pending_exception (&error);
865 len = mono_array_length (ta) >> 1;
867 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
868 if (mono_error_set_pending_exception (&error))
871 for (i = skip; i < len; i++) {
873 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
874 if (!mono_error_ok (&error)) {
875 mono_error_set_pending_exception (&error);
878 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
879 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
882 ji = mono_jit_info_table_find (domain, (char *)ip);
884 /* Unmanaged frame */
885 mono_array_setref (res, i, sf);
889 g_assert (ji != NULL);
892 /* Can't resolve actual method */
893 method = jinfo_get_method (ji);
895 method = get_method_from_stack_frame (ji, generic_info);
896 if (jinfo_get_method (ji)->wrapper_type) {
900 s = mono_method_get_name_full (method, TRUE, FALSE, MONO_TYPE_NAME_FORMAT_REFLECTION);
901 MonoString *name = mono_string_new_checked (domain, s, &error);
903 if (!is_ok (&error)) {
904 mono_error_set_pending_exception (&error);
907 MONO_OBJECT_SETREF (sf, internal_method_name, name);
910 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, method, NULL, &error);
911 if (!mono_error_ok (&error)) {
912 mono_error_set_pending_exception (&error);
915 MONO_OBJECT_SETREF (sf, method, rm);
918 sf->method_index = ji->from_aot ? mono_aot_find_method_index (method) : 0xffffff;
919 sf->method_address = (gsize) ji->code_start;
920 sf->native_offset = (char *)ip - (char *)ji->code_start;
923 * mono_debug_lookup_source_location() returns both the file / line number information
924 * and the IL offset. Note that computing the IL offset is already an expensive
925 * operation, so we shouldn't call this method twice.
927 location = mono_debug_lookup_source_location (jinfo_get_method (ji), sf->native_offset, domain);
929 sf->il_offset = location->il_offset;
932 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (ji), sf->native_offset, NULL, &sp))
933 sf->il_offset = sp.il_offset;
938 if (need_file_info) {
939 if (location && location->source_file) {
940 MonoString *filename = mono_string_new_checked (domain, location->source_file, &error);
941 if (!is_ok (&error)) {
942 mono_error_set_pending_exception (&error);
945 MONO_OBJECT_SETREF (sf, filename, filename);
946 sf->line = location->row;
947 sf->column = location->column;
949 sf->line = sf->column = 0;
954 mono_debug_free_source_location (location);
955 mono_array_setref (res, i, sf);
962 mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
965 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
966 if (jit_tls && jit_tls->orig_ex_ctx_set)
967 start_ctx = &jit_tls->orig_ex_ctx;
969 mono_walk_stack_with_ctx (func, start_ctx, unwind_options, user_data);
972 * mono_walk_stack_with_ctx:
973 * Unwind the current thread starting at \p start_ctx.
974 * If \p start_ctx is null, we capture the current context.
977 mono_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
979 MonoContext extra_ctx;
980 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
981 MONO_ARCH_CONTEXT_DEF
983 if (!thread || !thread->jit_data)
987 mono_arch_flush_register_windows ();
989 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
990 MONO_INIT_CONTEXT_FROM_CURRENT (&extra_ctx);
992 MONO_INIT_CONTEXT_FROM_FUNC (&extra_ctx, mono_walk_stack_with_ctx);
994 start_ctx = &extra_ctx;
997 mono_walk_stack_full (func, start_ctx, mono_domain_get (), (MonoJitTlsData *)thread->jit_data, mono_get_lmf (), unwind_options, user_data);
1001 * mono_walk_stack_with_state:
1002 * Unwind a thread described by \p state.
1004 * State must be valid (state->valid == TRUE).
1006 * If you are using this function to unwind another thread, make sure it is suspended.
1008 * If \p state is null, we capture the current context.
1011 mono_walk_stack_with_state (MonoJitStackWalk func, MonoThreadUnwindState *state, MonoUnwindOptions unwind_options, void *user_data)
1013 MonoThreadUnwindState extra_state;
1015 g_assert (!mono_thread_info_is_async_context ());
1016 if (!mono_thread_state_init_from_current (&extra_state))
1018 state = &extra_state;
1021 g_assert (state->valid);
1023 if (!state->unwind_data [MONO_UNWIND_DATA_DOMAIN])
1027 mono_walk_stack_full (func,
1029 (MonoDomain *)state->unwind_data [MONO_UNWIND_DATA_DOMAIN],
1030 (MonoJitTlsData *)state->unwind_data [MONO_UNWIND_DATA_JIT_TLS],
1031 (MonoLMF *)state->unwind_data [MONO_UNWIND_DATA_LMF],
1032 unwind_options, user_data);
1036 mono_walk_stack (MonoJitStackWalk func, MonoUnwindOptions options, void *user_data)
1038 MonoThreadUnwindState state;
1039 if (!mono_thread_state_init_from_current (&state))
1041 mono_walk_stack_with_state (func, &state, options, user_data);
1045 * mono_walk_stack_full:
1046 * \param func callback to call for each stack frame
1047 * \param domain starting appdomain, can be NULL to use the current domain
1048 * \param unwind_options what extra information the unwinder should gather
1049 * \param start_ctx starting state of the stack walk, can be NULL.
1050 * \param thread the thread whose stack to walk, can be NULL to use the current thread
1051 * \param lmf the LMF of \p thread, can be NULL to use the LMF of the current thread
1052 * \param user_data data passed to the callback
1053 * This function walks the stack of a thread, starting from the state
1054 * represented by \p start_ctx. For each frame the callback
1055 * function is called with the relevant info. The walk ends when no more
1056 * managed stack frames are found or when the callback returns a TRUE value.
1059 mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data)
1062 MonoContext ctx, new_ctx;
1063 StackFrameInfo frame;
1065 mgreg_t *reg_locations [MONO_MAX_IREGS];
1066 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
1067 gboolean get_reg_locations = unwind_options & MONO_UNWIND_REG_LOCATIONS;
1068 gboolean async = mono_thread_info_is_async_context ();
1070 if (mono_llvm_only) {
1076 ips = get_unwind_backtrace ();
1077 for (l = ips; l; l = l->next) {
1078 guint8 *ip = (guint8*)l->data;
1079 memset (&frame, 0, sizeof (StackFrameInfo));
1080 frame.ji = mini_jit_info_table_find (domain, (char*)ip, &frame.domain);
1081 if (!frame.ji || frame.ji->is_trampoline)
1083 frame.type = FRAME_TYPE_MANAGED;
1084 frame.method = jinfo_get_method (frame.ji);
1085 // FIXME: Cannot lookup the actual method
1086 frame.actual_method = frame.method;
1087 if (frame.type == FRAME_TYPE_MANAGED) {
1088 if (!frame.method->wrapper_type || frame.method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
1089 frame.managed = TRUE;
1091 frame.native_offset = ip - (guint8*)frame.ji->code_start;
1092 frame.il_offset = -1;
1094 if (func (&frame, NULL, user_data))
1101 g_assert (start_ctx);
1104 /*The LMF will be null if the target have no managed frames.*/
1105 /* g_assert (lmf); */
1108 g_assert (unwind_options == MONO_UNWIND_NONE);
1110 memcpy (&ctx, start_ctx, sizeof (MonoContext));
1111 memset (reg_locations, 0, sizeof (reg_locations));
1113 while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
1115 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, get_reg_locations ? new_reg_locations : NULL, &frame);
1119 if ((unwind_options & MONO_UNWIND_LOOKUP_IL_OFFSET) && frame.ji && !frame.ji->is_trampoline) {
1120 MonoDebugSourceLocation *source;
1122 source = mono_debug_lookup_source_location (jinfo_get_method (frame.ji), frame.native_offset, domain);
1124 il_offset = source->il_offset;
1127 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (frame.ji), frame.native_offset, NULL, &sp))
1128 il_offset = sp.il_offset;
1132 mono_debug_free_source_location (source);
1136 frame.il_offset = il_offset;
1138 if ((unwind_options & MONO_UNWIND_LOOKUP_ACTUAL_METHOD) && frame.ji && !frame.ji->is_trampoline) {
1139 frame.actual_method = get_method_from_stack_frame (frame.ji, get_generic_info_from_stack_frame (frame.ji, &ctx));
1141 frame.actual_method = frame.method;
1144 if (get_reg_locations)
1145 frame.reg_locations = reg_locations;
1147 if (func (&frame, &ctx, user_data))
1150 if (get_reg_locations) {
1151 for (i = 0; i < MONO_MAX_IREGS; ++i)
1152 if (new_reg_locations [i])
1153 reg_locations [i] = new_reg_locations [i];
1161 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
1162 MonoReflectionMethod **method,
1163 gint32 *iloffset, gint32 *native_offset,
1164 MonoString **file, gint32 *line, gint32 *column)
1167 MonoDomain *domain = mono_domain_get ();
1168 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1169 MonoLMF *lmf = mono_get_lmf ();
1170 MonoJitInfo *ji = NULL;
1171 MonoContext ctx, new_ctx;
1172 MonoDebugSourceLocation *location;
1173 MonoMethod *jmethod = NULL, *actual_method;
1174 StackFrameInfo frame;
1179 MONO_ARCH_CONTEXT_DEF;
1181 if (mono_llvm_only) {
1183 MonoDomain *frame_domain;
1184 guint8 *frame_ip = NULL;
1186 /* FIXME: Generalize this code with an interface which returns an array of StackFrame structures */
1188 ips = get_unwind_backtrace ();
1189 for (l = ips; l && skip >= 0; l = l->next) {
1190 guint8 *ip = (guint8*)l->data;
1194 ji = mini_jit_info_table_find (mono_domain_get (), (char*)ip, &frame_domain);
1195 if (!ji || ji->is_trampoline)
1198 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1199 jmethod = jinfo_get_method (ji);
1200 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1207 /* No way to resolve generic instances */
1208 actual_method = jmethod;
1209 *native_offset = frame_ip - (guint8*)ji->code_start;
1211 mono_arch_flush_register_windows ();
1213 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
1214 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
1216 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
1219 unwinder_init (&unwinder);
1224 res = unwinder_unwind_frame (&unwinder, domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1227 switch (frame.type) {
1228 case FRAME_TYPE_MANAGED_TO_NATIVE:
1229 case FRAME_TYPE_DEBUGGER_INVOKE:
1230 case FRAME_TYPE_TRAMPOLINE:
1231 case FRAME_TYPE_INTERP_TO_MANAGED:
1233 case FRAME_TYPE_INTERP:
1238 *native_offset = frame.native_offset;
1240 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1241 jmethod = jinfo_get_method (ji);
1242 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1247 } while (skip >= 0);
1249 if (frame.type == FRAME_TYPE_INTERP) {
1250 jmethod = frame.method;
1251 actual_method = frame.actual_method;
1252 *native_offset = frame.native_offset;
1254 actual_method = get_method_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, &ctx));
1258 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, actual_method, NULL, &error);
1259 if (!mono_error_ok (&error)) {
1260 mono_error_set_pending_exception (&error);
1263 mono_gc_wbarrier_generic_store (method, (MonoObject*) rm);
1265 if (il_offset != -1) {
1266 location = mono_debug_lookup_source_location_by_il (jmethod, il_offset, domain);
1268 location = mono_debug_lookup_source_location (jmethod, *native_offset, domain);
1271 *iloffset = location->il_offset;
1275 if (need_file_info) {
1277 MonoString *filename = mono_string_new_checked (domain, location->source_file, &error);
1278 if (!is_ok (&error)) {
1279 mono_error_set_pending_exception (&error);
1282 mono_gc_wbarrier_generic_store (file, (MonoObject*)filename);
1283 *line = location->row;
1284 *column = location->column;
1287 *line = *column = 0;
1291 mono_debug_free_source_location (location);
1297 get_exception_catch_class (MonoJitExceptionInfo *ei, MonoJitInfo *ji, MonoContext *ctx)
1300 MonoClass *catch_class = ei->data.catch_class;
1301 MonoType *inflated_type;
1302 MonoGenericContext context;
1304 /*MonoJitExceptionInfo::data is an union used by filter and finally clauses too.*/
1305 if (!catch_class || ei->flags != MONO_EXCEPTION_CLAUSE_NONE)
1308 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
1310 context = get_generic_context_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, ctx));
1312 /* FIXME: we shouldn't inflate but instead put the
1313 type in the rgctx and fetch it from there. It
1314 might be a good idea to do this lazily, i.e. only
1315 when the exception is actually thrown, so as not to
1316 waste space for exception clauses which might never
1318 inflated_type = mono_class_inflate_generic_type_checked (&catch_class->byval_arg, &context, &error);
1319 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
1321 catch_class = mono_class_from_mono_type (inflated_type);
1322 mono_metadata_free_type (inflated_type);
1328 * mini_jit_info_table_find_ext:
1330 * Same as mono_jit_info_table_find, but search all the domains of the current thread
1331 * if ADDR is not found in DOMAIN. The domain where the method was found is stored into
1332 * OUT_DOMAIN if it is not NULL.
1335 mini_jit_info_table_find_ext (MonoDomain *domain, char *addr, gboolean allow_trampolines, MonoDomain **out_domain)
1338 MonoInternalThread *t = mono_thread_internal_current ();
1344 ji = mono_jit_info_table_find_internal (domain, addr, TRUE, allow_trampolines);
1347 *out_domain = domain;
1351 /* maybe it is shared code, so we also search in the root domain */
1352 if (domain != mono_get_root_domain ()) {
1353 ji = mono_jit_info_table_find_internal (mono_get_root_domain (), addr, TRUE, allow_trampolines);
1356 *out_domain = mono_get_root_domain ();
1364 refs = (gpointer *)((t->appdomain_refs) ? *(gpointer *) t->appdomain_refs : NULL);
1365 for (; refs && *refs; refs++) {
1366 if (*refs != domain && *refs != mono_get_root_domain ()) {
1367 ji = mono_jit_info_table_find_internal ((MonoDomain*) *refs, addr, TRUE, allow_trampolines);
1370 *out_domain = (MonoDomain*) *refs;
1380 mini_jit_info_table_find (MonoDomain *domain, char *addr, MonoDomain **out_domain)
1382 return mini_jit_info_table_find_ext (domain, addr, FALSE, out_domain);
1385 /* Class lazy loading functions */
1386 static GENERATE_GET_CLASS_WITH_CACHE (runtime_compat_attr, "System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute")
1389 * wrap_non_exception_throws:
1391 * Determine whenever M's assembly has a RuntimeCompatibilityAttribute with the
1392 * WrapNonExceptionThrows flag set.
1395 wrap_non_exception_throws (MonoMethod *m)
1398 MonoAssembly *ass = m->klass->image->assembly;
1399 MonoCustomAttrInfo* attrs;
1402 gboolean val = FALSE;
1405 if (ass->wrap_non_exception_throws_inited)
1406 return ass->wrap_non_exception_throws;
1408 klass = mono_class_get_runtime_compat_attr_class ();
1410 attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, &error);
1411 mono_error_cleanup (&error); /* FIXME don't swallow the error */
1413 for (i = 0; i < attrs->num_attrs; ++i) {
1414 MonoCustomAttrEntry *attr = &attrs->attrs [i];
1416 int num_named, named_type, name_len;
1419 if (!attr->ctor || attr->ctor->klass != klass)
1421 /* Decode the RuntimeCompatibilityAttribute. See reflection.c */
1422 p = (const char*)attr->data;
1423 g_assert (read16 (p) == 0x0001);
1425 num_named = read16 (p);
1431 /* data_type = *p; */
1434 if (named_type != 0x54)
1436 name_len = mono_metadata_decode_blob_size (p, &p);
1437 name = (char *)g_malloc (name_len + 1);
1438 memcpy (name, p, name_len);
1439 name [name_len] = 0;
1441 g_assert (!strcmp (name, "WrapNonExceptionThrows"));
1443 /* The value is a BOOLEAN */
1446 mono_custom_attrs_free (attrs);
1449 ass->wrap_non_exception_throws = val;
1450 mono_memory_barrier ();
1451 ass->wrap_non_exception_throws_inited = TRUE;
1456 #define MAX_UNMANAGED_BACKTRACE 128
1458 build_native_trace (MonoError *error)
1461 /* This puppy only makes sense on mobile, IOW, ARM. */
1462 #if defined (HAVE_BACKTRACE_SYMBOLS) && defined (TARGET_ARM)
1464 void *native_trace [MAX_UNMANAGED_BACKTRACE];
1467 size = backtrace (native_trace, MAX_UNMANAGED_BACKTRACE);
1473 res = mono_array_new_checked (mono_domain_get (), mono_defaults.int_class, size, error);
1474 return_val_if_nok (error, NULL);
1476 for (i = 0; i < size; i++)
1477 mono_array_set (res, gpointer, i, native_trace [i]);
1485 setup_stack_trace (MonoException *mono_ex, GSList *dynamic_methods, GList **trace_ips)
1488 *trace_ips = g_list_reverse (*trace_ips);
1490 MonoArray *ips_arr = mono_glist_to_array (*trace_ips, mono_defaults.int_class, &error);
1491 mono_error_assert_ok (&error);
1492 MONO_OBJECT_SETREF (mono_ex, trace_ips, ips_arr);
1493 MONO_OBJECT_SETREF (mono_ex, native_trace_ips, build_native_trace (&error));
1494 mono_error_assert_ok (&error);
1495 if (dynamic_methods) {
1496 /* These methods could go away anytime, so save a reference to them in the exception object */
1498 MonoMList *list = NULL;
1500 for (l = dynamic_methods; l; l = l->next) {
1502 MonoDomain *domain = mono_domain_get ();
1504 if (domain->method_to_dyn_method) {
1505 mono_domain_lock (domain);
1506 dis_link = (guint32)(size_t)g_hash_table_lookup (domain->method_to_dyn_method, l->data);
1507 mono_domain_unlock (domain);
1509 MonoObject *o = mono_gchandle_get_target (dis_link);
1511 list = mono_mlist_prepend_checked (list, o, &error);
1512 mono_error_assert_ok (&error);
1518 MONO_OBJECT_SETREF (mono_ex, dynamic_methods, list);
1521 g_list_free (*trace_ips);
1526 * mono_handle_exception_internal_first_pass:
1528 * The first pass of exception handling. Unwind the stack until a catch clause which can catch
1529 * OBJ is found. Run the index of the filter clause which caught the exception into
1530 * OUT_FILTER_IDX. Return TRUE if the exception is caught, FALSE otherwise.
1533 mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoJitInfo **out_prev_ji, MonoObject *non_exception)
1536 MonoDomain *domain = mono_domain_get ();
1537 MonoJitInfo *ji = NULL;
1538 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1539 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1540 MonoLMF *lmf = mono_get_lmf ();
1541 GList *trace_ips = NULL;
1542 GSList *dynamic_methods = NULL;
1543 MonoException *mono_ex;
1544 gboolean stack_overflow = FALSE;
1545 MonoContext initial_ctx;
1547 int frame_count = 0;
1554 g_assert (ctx != NULL);
1556 if (obj == (MonoObject *)domain->stack_overflow_ex)
1557 stack_overflow = TRUE;
1559 mono_ex = (MonoException*)obj;
1560 MonoArray *initial_trace_ips = mono_ex->trace_ips;
1561 if (initial_trace_ips) {
1562 int len = mono_array_length (initial_trace_ips) >> 1;
1564 for (i = 0; i < (len - 1); i++) {
1565 gpointer ip = mono_array_get (initial_trace_ips, gpointer, i * 2 + 0);
1566 gpointer generic_info = mono_array_get (initial_trace_ips, gpointer, i * 2 + 1);
1567 trace_ips = g_list_prepend (trace_ips, ip);
1568 trace_ips = g_list_prepend (trace_ips, generic_info);
1572 if (!mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1573 mono_error_assert_ok (&error);
1578 call_filter = (int (*) (MonoContext *, void *))mono_get_call_filter ();
1580 g_assert (jit_tls->end_of_stack);
1581 g_assert (jit_tls->abort_func);
1584 *out_filter_idx = -1;
1588 *out_prev_ji = NULL;
1592 unwinder_init (&unwinder);
1595 MonoContext new_ctx;
1597 int clause_index_start = 0;
1598 gboolean unwind_res = TRUE;
1600 StackFrameInfo frame;
1605 unwind_res = unwinder_unwind_frame (&unwinder, domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1607 setup_stack_trace (mono_ex, dynamic_methods, &trace_ips);
1608 g_slist_free (dynamic_methods);
1612 switch (frame.type) {
1613 case FRAME_TYPE_DEBUGGER_INVOKE:
1614 case FRAME_TYPE_MANAGED_TO_NATIVE:
1615 case FRAME_TYPE_TRAMPOLINE:
1616 case FRAME_TYPE_INTERP_TO_MANAGED:
1619 case FRAME_TYPE_INTERP:
1620 case FRAME_TYPE_MANAGED:
1623 g_assert_not_reached ();
1627 in_interp = frame.type == FRAME_TYPE_INTERP;
1632 ip = (guint16*)ji->code_start + frame.native_offset;
1634 ip = MONO_CONTEXT_GET_IP (ctx);
1637 method = jinfo_get_method (ji);
1638 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1640 if (mini_get_debug_options ()->reverse_pinvoke_exceptions && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
1641 g_error ("A native frame was found while unwinding the stack after an exception.\n"
1642 "The native frame called the managed method:\n%s\n",
1643 mono_method_full_name (method, TRUE));
1646 if (method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1647 // avoid giant stack traces during a stack overflow
1648 if (frame_count < 1000) {
1649 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1650 trace_ips = g_list_prepend (trace_ips, get_generic_info_from_stack_frame (ji, ctx));
1654 if (method->dynamic)
1655 dynamic_methods = g_slist_prepend (dynamic_methods, method);
1657 if (stack_overflow) {
1658 if (MONO_ARCH_STACK_GROWS_UP)
1659 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1661 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1663 free_stack = 0xffffff;
1666 for (i = clause_index_start; i < ji->num_clauses; i++) {
1667 MonoJitExceptionInfo *ei = &ji->clauses [i];
1668 gboolean filtered = FALSE;
1671 * During stack overflow, wait till the unwinding frees some stack
1672 * space before running handlers/finalizers.
1674 if (free_stack <= (64 * 1024))
1677 if (is_address_protected (ji, ei, ip)) {
1679 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1682 * Have to unwrap RuntimeWrappedExceptions if the
1683 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1685 if (non_exception && !wrap_non_exception_throws (method))
1686 ex_obj = non_exception;
1690 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1691 #ifndef DISABLE_PERFCOUNTERS
1692 mono_perfcounters->exceptions_filters++;
1695 #ifndef MONO_CROSS_COMPILE
1696 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1698 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1700 /* Can't pass the ex object in a register yet to filter clauses, because call_filter () might not support it */
1701 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1703 g_assert (!ji->from_llvm);
1704 /* store the exception object in bp + ei->exvar_offset */
1705 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1709 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
1711 * Pass the original il clause index to the landing pad so it can
1712 * branch to the landing pad associated with the il clause.
1713 * This is needed because llvm compiled code assumes that the EH
1714 * code always branches to the innermost landing pad.
1717 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
1720 mono_debugger_agent_begin_exception_filter (mono_ex, ctx, &initial_ctx);
1721 filtered = call_filter (ctx, ei->data.filter);
1722 mono_debugger_agent_end_exception_filter (mono_ex, ctx, &initial_ctx);
1723 if (filtered && out_filter_idx)
1724 *out_filter_idx = filter_idx;
1730 setup_stack_trace (mono_ex, dynamic_methods, &trace_ips);
1731 g_slist_free (dynamic_methods);
1732 /* mono_debugger_agent_handle_exception () needs this */
1733 mini_set_abort_threshold (ctx);
1734 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1739 MonoError isinst_error;
1740 error_init (&isinst_error);
1741 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (ex_obj, catch_class, &error)) {
1742 setup_stack_trace (mono_ex, dynamic_methods, &trace_ips);
1743 g_slist_free (dynamic_methods);
1748 /* mono_debugger_agent_handle_exception () needs this */
1750 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1753 mono_error_cleanup (&isinst_error);
1760 g_assert_not_reached ();
1764 * mono_handle_exception_internal:
1765 * \param ctx saved processor state
1766 * \param obj the exception object
1767 * \param resume whenever to resume unwinding based on the state in \c MonoJitTlsData.
1770 mono_handle_exception_internal (MonoContext *ctx, MonoObject *obj, gboolean resume, MonoJitInfo **out_ji)
1773 MonoDomain *domain = mono_domain_get ();
1774 MonoJitInfo *ji, *prev_ji;
1775 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1776 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1777 MonoLMF *lmf = mono_get_lmf ();
1778 MonoException *mono_ex;
1779 gboolean stack_overflow = FALSE;
1780 MonoContext initial_ctx;
1782 int frame_count = 0;
1783 gint32 filter_idx, first_filter_idx = 0;
1786 MonoObject *non_exception = NULL;
1790 g_assert (ctx != NULL);
1792 MonoException *ex = mono_get_exception_null_reference ();
1793 MonoString *msg = mono_string_new_checked (domain, "Object reference not set to an instance of an object", &error);
1794 mono_error_assert_ok (&error);
1795 MONO_OBJECT_SETREF (ex, message, msg);
1796 obj = (MonoObject *)ex;
1800 * Allocate a new exception object instead of the preconstructed ones.
1802 if (obj == (MonoObject *)domain->stack_overflow_ex) {
1804 * It is not a good idea to try and put even more pressure on the little stack available.
1805 * obj = mono_get_exception_stack_overflow ();
1807 stack_overflow = TRUE;
1809 else if (obj == (MonoObject *)domain->null_reference_ex) {
1810 obj = (MonoObject *)mono_get_exception_null_reference ();
1813 if (!mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1814 mono_error_assert_ok (&error);
1815 non_exception = obj;
1816 obj = (MonoObject *)mono_get_exception_runtime_wrapped_checked (obj, &error);
1817 mono_error_assert_ok (&error);
1820 mono_ex = (MonoException*)obj;
1822 if (mini_get_debug_options ()->suspend_on_exception) {
1823 mono_runtime_printf_err ("Exception thrown, suspending...");
1828 if (mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1829 mono_ex = (MonoException*)obj;
1831 mono_error_assert_ok (&error);
1835 if (mono_ex && jit_tls->class_cast_from) {
1836 if (!strcmp (mono_ex->object.vtable->klass->name, "InvalidCastException")) {
1837 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1838 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1839 char *msg = g_strdup_printf ("Unable to cast object of type '%s' to type '%s'.", from_name, to_name);
1840 mono_ex->message = mono_string_new_checked (domain, msg, &error);
1843 if (!is_ok (&error)) {
1844 mono_runtime_printf_err ("Error creating class cast exception message '%s'\n", msg);
1845 mono_error_assert_ok (&error);
1849 if (!strcmp (mono_ex->object.vtable->klass->name, "ArrayTypeMismatchException")) {
1850 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1851 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1852 char *msg = g_strdup_printf ("Source array of type '%s' cannot be cast to destination array type '%s'.", from_name, to_name);
1853 mono_ex->message = mono_string_new_checked (domain, msg, &error);
1856 if (!is_ok (&error)) {
1857 mono_runtime_printf_err ("Error creating array type mismatch exception message '%s'\n", msg);
1858 mono_error_assert_ok (&error);
1865 call_filter = (int (*)(MonoContext *, void*))mono_get_call_filter ();
1867 g_assert (jit_tls->end_of_stack);
1868 g_assert (jit_tls->abort_func);
1871 * We set orig_ex_ctx_set to TRUE/FALSE around profiler calls to make sure it doesn't
1872 * end up being TRUE on any code path.
1874 memcpy (&jit_tls->orig_ex_ctx, ctx, sizeof (MonoContext));
1879 MonoContext ctx_cp = *ctx;
1880 if (mono_trace_is_enabled ()) {
1881 MonoMethod *system_exception_get_message = mono_class_get_method_from_name (mono_defaults.exception_class, "get_Message", 0);
1882 MonoMethod *get_message = system_exception_get_message == NULL ? NULL : mono_object_get_virtual_method (obj, system_exception_get_message);
1883 MonoObject *message;
1884 const char *type_name = mono_class_get_name (mono_object_class (mono_ex));
1886 if (get_message == NULL) {
1888 } else if (!strcmp (type_name, "OutOfMemoryException") || !strcmp (type_name, "StackOverflowException")) {
1890 msg = g_strdup_printf ("(No exception message for: %s)\n", type_name);
1892 MonoObject *exc = NULL;
1893 message = mono_runtime_try_invoke (get_message, obj, NULL, &exc, &error);
1894 g_assert (exc == NULL);
1895 mono_error_assert_ok (&error);
1899 msg = mono_string_to_utf8_checked ((MonoString *) message, &error);
1900 if (!is_ok (&error)) {
1901 mono_error_cleanup (&error);
1902 msg = g_strdup ("(error while display System.Exception.Message property)");
1905 msg = g_strdup ("(System.Exception.Message property not available)");
1908 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);
1910 if (mono_ex && mono_trace_eval_exception (mono_object_class (mono_ex)))
1911 mono_print_thread_dump_from_ctx (ctx);
1913 jit_tls->orig_ex_ctx_set = TRUE;
1914 mono_profiler_exception_thrown (obj);
1915 jit_tls->orig_ex_ctx_set = FALSE;
1917 res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, &first_filter_idx, &ji, &prev_ji, non_exception);
1920 if (mini_get_debug_options ()->break_on_exc)
1922 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1924 if (mini_get_debug_options ()->suspend_on_unhandled) {
1925 mono_runtime_printf_err ("Unhandled exception, suspending...");
1930 // FIXME: This runs managed code so it might cause another stack overflow when
1931 // we are handling a stack overflow
1932 mini_set_abort_threshold (ctx);
1933 mono_unhandled_exception (obj);
1935 gboolean unhandled = FALSE;
1938 * The exceptions caught by the mono_runtime_invoke_checked () calls
1939 * in the threadpool needs to be treated as unhandled (#669836).
1941 * FIXME: The check below is hackish, but its hard to distinguish
1942 * these runtime invoke calls from others in the runtime.
1944 if (ji && jinfo_get_method (ji)->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1945 if (prev_ji && jinfo_get_method (prev_ji) == mono_defaults.threadpool_perform_wait_callback_method)
1950 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1952 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, &ctx_cp);
1961 unwinder_init (&unwinder);
1964 MonoContext new_ctx;
1966 int clause_index_start = 0;
1967 gboolean unwind_res = TRUE;
1968 StackFrameInfo frame;
1973 ji = jit_tls->resume_state.ji;
1974 new_ctx = jit_tls->resume_state.new_ctx;
1975 clause_index_start = jit_tls->resume_state.clause_index;
1976 lmf = jit_tls->resume_state.lmf;
1977 first_filter_idx = jit_tls->resume_state.first_filter_idx;
1978 filter_idx = jit_tls->resume_state.filter_idx;
1981 unwind_res = unwinder_unwind_frame (&unwinder, domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1983 *(mono_get_lmf_addr ()) = lmf;
1985 jit_tls->abort_func (obj);
1986 g_assert_not_reached ();
1988 switch (frame.type) {
1989 case FRAME_TYPE_DEBUGGER_INVOKE:
1990 case FRAME_TYPE_MANAGED_TO_NATIVE:
1991 case FRAME_TYPE_TRAMPOLINE:
1994 case FRAME_TYPE_INTERP_TO_MANAGED:
1996 case FRAME_TYPE_INTERP:
1997 case FRAME_TYPE_MANAGED:
2000 g_assert_not_reached ();
2003 in_interp = frame.type == FRAME_TYPE_INTERP;
2008 ip = (guint16*)ji->code_start + frame.native_offset;
2010 ip = MONO_CONTEXT_GET_IP (ctx);
2012 method = jinfo_get_method (ji);
2014 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
2016 if (stack_overflow) {
2017 if (MONO_ARCH_STACK_GROWS_UP)
2018 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
2020 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
2022 free_stack = 0xffffff;
2025 for (i = clause_index_start; i < ji->num_clauses; i++) {
2026 MonoJitExceptionInfo *ei = &ji->clauses [i];
2027 gboolean filtered = FALSE;
2030 * During stack overflow, wait till the unwinding frees some stack
2031 * space before running handlers/finalizers.
2033 if (free_stack <= (64 * 1024))
2036 if (is_address_protected (ji, ei, ip)) {
2038 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
2041 * Have to unwrap RuntimeWrappedExceptions if the
2042 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
2044 if (non_exception && !wrap_non_exception_throws (method))
2045 ex_obj = non_exception;
2049 if (((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER))) {
2050 #ifndef MONO_CROSS_COMPILE
2051 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
2052 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
2054 g_assert (!ji->from_llvm);
2055 /* store the exception object in bp + ei->exvar_offset */
2056 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
2061 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
2063 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
2066 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2068 * Filter clauses should only be run in the
2069 * first pass of exception handling.
2071 filtered = (filter_idx == first_filter_idx);
2075 error_init (&error);
2076 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
2077 mono_object_isinst_checked (ex_obj, catch_class, &error)) || filtered) {
2079 * This guards against the situation that we abort a thread that is executing a finally clause
2080 * that was called by the EH machinery. It won't have a guard trampoline installed, so we must
2081 * check for this situation here and resume interruption if we are below the guarded block.
2083 if (G_UNLIKELY (jit_tls->handler_block_return_address)) {
2084 gboolean is_outside = FALSE;
2085 gpointer prot_bp = MONO_CONTEXT_GET_BP (&jit_tls->handler_block_context);
2086 gpointer catch_bp = MONO_CONTEXT_GET_BP (ctx);
2087 //FIXME make this stack direction aware
2088 if (catch_bp > prot_bp) {
2090 } else if (catch_bp == prot_bp) {
2091 /* Can be either try { try { } catch {} } finally {} or try { try { } finally {} } catch {}
2092 * So we check if the catch handler_start is protected by the guarded handler protected region
2095 * If there is an outstanding guarded_block return address, it means the current thread must be aborted.
2096 * This is the only way to reach out the guarded block as other cases are handled by the trampoline.
2097 * There aren't any further finally/fault handler blocks down the stack over this exception.
2098 * This must be ensured by the code that installs the guard trampoline.
2100 g_assert (ji == mini_jit_info_table_find (domain, (char *)MONO_CONTEXT_GET_IP (&jit_tls->handler_block_context), NULL));
2102 if (!is_address_protected (ji, jit_tls->handler_block, ei->handler_start)) {
2107 jit_tls->handler_block_return_address = NULL;
2108 jit_tls->handler_block = NULL;
2109 mono_thread_resume_interruption (TRUE); /*We ignore the exception here, it will be raised later*/
2113 if (mono_trace_is_enabled () && mono_trace_eval (method))
2114 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (method, TRUE));
2115 jit_tls->orig_ex_ctx_set = TRUE;
2116 mono_profiler_exception_clause_handler (method, ei->flags, i);
2117 jit_tls->orig_ex_ctx_set = FALSE;
2118 mini_set_abort_threshold (ctx);
2122 * ctx->pc points into the interpreter, after the call which transitioned to
2123 * JITted code. Store the unwind state into the
2124 * interpeter state, then resume, the interpreter will unwind itself until
2125 * it reaches the target frame and will continue execution from there.
2126 * The resuming is kinda hackish, from the native code standpoint, it looks
2127 * like the call which transitioned to JITted code has succeeded, but the
2128 * return value register etc. is not set, so we have to be careful.
2130 mono_interp_set_resume_state (mono_ex, &frame, ei->handler_start);
2131 /* Undo the IP adjustment done by mono_arch_unwind_frame () */
2132 #if defined(TARGET_AMD64)
2133 ctx->gregs [AMD64_RIP] ++;
2134 #elif defined(TARGET_ARM64)
2140 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
2143 #ifndef DISABLE_PERFCOUNTERS
2144 mono_perfcounters->exceptions_depth += frame_count;
2146 if (obj == (MonoObject *)domain->stack_overflow_ex)
2147 jit_tls->handling_stack_ovf = FALSE;
2151 mono_error_cleanup (&error);
2152 if (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
2153 if (mono_trace_is_enabled () && mono_trace_eval (method))
2154 g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (method, TRUE));
2155 jit_tls->orig_ex_ctx_set = TRUE;
2156 mono_profiler_exception_clause_handler (method, ei->flags, i);
2157 jit_tls->orig_ex_ctx_set = FALSE;
2158 mini_set_abort_threshold (ctx);
2159 call_filter (ctx, ei->handler_start);
2161 if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
2162 if (mono_trace_is_enabled () && mono_trace_eval (method))
2163 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (method, TRUE));
2164 jit_tls->orig_ex_ctx_set = TRUE;
2165 mono_profiler_exception_clause_handler (method, ei->flags, i);
2166 jit_tls->orig_ex_ctx_set = FALSE;
2167 #ifndef DISABLE_PERFCOUNTERS
2168 mono_perfcounters->exceptions_finallys++;
2171 if (ji->from_llvm) {
2173 * LLVM compiled finally handlers follow the design
2174 * of the c++ ehabi, i.e. they call a resume function
2175 * at the end instead of returning to the caller.
2176 * So save the exception handling state,
2177 * mono_resume_unwind () will call us again to continue
2180 jit_tls->resume_state.ex_obj = obj;
2181 jit_tls->resume_state.ji = ji;
2182 jit_tls->resume_state.clause_index = i + 1;
2183 jit_tls->resume_state.ctx = *ctx;
2184 jit_tls->resume_state.new_ctx = new_ctx;
2185 jit_tls->resume_state.lmf = lmf;
2186 jit_tls->resume_state.first_filter_idx = first_filter_idx;
2187 jit_tls->resume_state.filter_idx = filter_idx;
2188 mini_set_abort_threshold (ctx);
2189 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
2192 mini_set_abort_threshold (ctx);
2194 mono_interp_run_finally (&frame, i, ei->handler_start);
2196 call_filter (ctx, ei->handler_start);
2202 jit_tls->orig_ex_ctx_set = TRUE;
2203 mono_profiler_exception_method_leave (method);
2204 jit_tls->orig_ex_ctx_set = FALSE;
2209 g_assert_not_reached ();
2213 * mono_debugger_run_finally:
2214 * \param start_ctx saved processor state
2215 * This method is called by the Mono Debugger to call all \c finally clauses of the
2216 * current stack frame. It's used when the user issues a \c return command to make
2217 * the current stack frame return. After returning from this method, the debugger
2218 * unwinds the stack one frame and gives control back to the user.
2219 * NOTE: This method is only used when running inside the Mono Debugger.
2222 mono_debugger_run_finally (MonoContext *start_ctx)
2224 static int (*call_filter) (MonoContext *, gpointer) = NULL;
2225 MonoDomain *domain = mono_domain_get ();
2226 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2227 MonoLMF *lmf = mono_get_lmf ();
2228 MonoContext ctx, new_ctx;
2229 MonoJitInfo *ji, rji;
2234 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
2235 if (!ji || ji == (gpointer)-1)
2239 call_filter = (int (*)(MonoContext *, void *))mono_get_call_filter ();
2241 for (i = 0; i < ji->num_clauses; i++) {
2242 MonoJitExceptionInfo *ei = &ji->clauses [i];
2244 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (&ctx)) &&
2245 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
2246 call_filter (&ctx, ei->handler_start);
2252 * mono_handle_exception:
2253 * \param ctx saved processor state
2254 * \param obj the exception object
2257 mono_handle_exception (MonoContext *ctx, MonoObject *obj)
2259 MONO_REQ_GC_UNSAFE_MODE;
2261 #ifndef DISABLE_PERFCOUNTERS
2262 mono_perfcounters->exceptions_thrown++;
2265 return mono_handle_exception_internal (ctx, obj, FALSE, NULL);
2268 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2270 #ifndef MONO_ARCH_USE_SIGACTION
2271 #error "Can't use sigaltstack without sigaction"
2274 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
2277 mono_setup_altstack (MonoJitTlsData *tls)
2281 guint8 *staddr = NULL;
2283 if (mono_running_on_valgrind ())
2286 mono_thread_info_get_stack_bounds (&staddr, &stsize);
2290 tls->end_of_stack = staddr + stsize;
2291 tls->stack_size = stsize;
2293 /*g_print ("thread %p, stack_base: %p, stack_size: %d\n", (gpointer)pthread_self (), staddr, stsize);*/
2295 tls->stack_ovf_guard_base = staddr + mono_pagesize ();
2296 tls->stack_ovf_guard_size = ALIGN_TO (8 * 4096, mono_pagesize ());
2298 g_assert ((guint8*)&sa >= (guint8*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size);
2300 if (mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE)) {
2301 /* mprotect can fail for the main thread stack */
2302 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);
2303 g_assert (gaddr == tls->stack_ovf_guard_base);
2304 tls->stack_ovf_valloced = TRUE;
2307 /* Setup an alternate signal stack */
2308 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);
2309 tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2311 g_assert (tls->signal_stack);
2313 sa.ss_sp = tls->signal_stack;
2314 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2316 g_assert (sigaltstack (&sa, NULL) == 0);
2318 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);
2322 mono_free_altstack (MonoJitTlsData *tls)
2327 sa.ss_sp = tls->signal_stack;
2328 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2329 sa.ss_flags = SS_DISABLE;
2330 err = sigaltstack (&sa, NULL);
2331 g_assert (err == 0);
2333 if (tls->signal_stack)
2334 mono_vfree (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE, MONO_MEM_ACCOUNT_EXCEPTIONS);
2335 if (tls->stack_ovf_valloced)
2336 mono_vfree (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MEM_ACCOUNT_EXCEPTIONS);
2338 mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_READ|MONO_MMAP_WRITE);
2341 #else /* !MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2344 mono_setup_altstack (MonoJitTlsData *tls)
2349 mono_free_altstack (MonoJitTlsData *tls)
2353 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2356 try_restore_stack_protection (MonoJitTlsData *jit_tls, int extra_bytes)
2358 gint32 unprotect_size = jit_tls->stack_ovf_guard_size;
2359 /* we need to leave some room for throwing the exception */
2360 while (unprotect_size >= 0 && (char*)jit_tls->stack_ovf_guard_base + unprotect_size > ((char*)&unprotect_size - extra_bytes))
2361 unprotect_size -= mono_pagesize ();
2362 /* at this point we could try and build a new domain->stack_overflow_ex, but only if there
2363 * is sufficient stack
2365 //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);
2367 mono_mprotect (jit_tls->stack_ovf_guard_base, unprotect_size, MONO_MMAP_NONE);
2368 return unprotect_size == jit_tls->stack_ovf_guard_size;
2371 static G_GNUC_UNUSED void
2372 try_more_restore (void)
2374 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2375 if (try_restore_stack_protection (jit_tls, 500))
2376 jit_tls->restore_stack_prot = NULL;
2379 static G_GNUC_UNUSED void
2380 restore_stack_protection (void)
2382 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2383 MonoException *ex = mono_domain_get ()->stack_overflow_ex;
2384 /* if we can't restore the stack protection, keep a callback installed so
2385 * we'll try to restore as much stack as we can at each return from unmanaged
2388 if (try_restore_stack_protection (jit_tls, 4096))
2389 jit_tls->restore_stack_prot = NULL;
2391 jit_tls->restore_stack_prot = try_more_restore_tramp;
2392 /* here we also throw a stack overflow exception */
2393 ex->trace_ips = NULL;
2394 ex->stack_trace = NULL;
2395 mono_raise_exception (ex);
2399 mono_altstack_restore_prot (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
2401 MONO_REQ_GC_UNSAFE_MODE;
2403 void (*func)(void) = (void (*)(void))tramp_data;
2409 mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, guint8* fault_addr)
2414 /* we got a stack overflow in the soft-guard pages
2415 * There are two cases:
2416 * 1) managed code caused the overflow: we unprotect the soft-guard page
2417 * and let the arch-specific code trigger the exception handling mechanism
2418 * in the thread stack. The soft-guard pages will be protected again as the stack is unwound.
2419 * 2) unmanaged code caused the overflow: we unprotect the soft-guard page
2420 * and hope we can continue with those enabled, at least until the hard-guard page
2421 * is hit. The alternative to continuing here is to just print a message and abort.
2422 * We may add in the future the code to protect the pages again in the codepath
2423 * when we return from unmanaged to managed code.
2425 if (jit_tls->stack_ovf_guard_size && fault_addr >= (guint8*)jit_tls->stack_ovf_guard_base &&
2426 fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size) {
2427 /* we unprotect the minimum amount we can */
2429 gboolean handled = FALSE;
2431 guard_size = jit_tls->stack_ovf_guard_size - (mono_pagesize () * SIZEOF_VOID_P / 4);
2432 while (guard_size && fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + guard_size) {
2433 guard_size -= mono_pagesize ();
2435 guard_size = jit_tls->stack_ovf_guard_size - guard_size;
2436 /*fprintf (stderr, "unprotecting: %d\n", guard_size);*/
2437 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);
2438 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2440 mono_arch_handle_altstack_exception (ctx, siginfo, fault_addr, TRUE);
2445 /* We print a message: after this even managed stack overflows
2446 * may crash the runtime
2448 mono_runtime_printf_err ("Stack overflow in unmanaged: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2449 if (!jit_tls->handling_stack_ovf) {
2450 jit_tls->restore_stack_prot = restore_stack_protection_tramp;
2451 jit_tls->handling_stack_ovf = 1;
2453 /*fprintf (stderr, "Already handling stack overflow\n");*/
2462 MonoMethod *omethod;
2464 } PrintOverflowUserData;
2466 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2468 print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2470 MonoMethod *method = NULL;
2471 PrintOverflowUserData *user_data = (PrintOverflowUserData *)data;
2474 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2475 method = jinfo_get_method (frame->ji);
2478 if (user_data->count == 0) {
2479 /* The first frame is in its prolog, so a line number cannot be computed */
2480 user_data->count ++;
2484 /* If this is a one method overflow, skip the other instances */
2485 if (method == user_data->omethod)
2488 location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2489 mono_runtime_printf_err (" %s", location);
2492 if (user_data->count == 1) {
2493 mono_runtime_printf_err (" <...>");
2494 user_data->omethod = method;
2496 user_data->omethod = NULL;
2499 user_data->count ++;
2501 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2508 mono_handle_hard_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr)
2510 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2511 PrintOverflowUserData ud;
2515 /* we don't do much now, but we can warn the user with a useful message */
2516 mono_runtime_printf_err ("Stack overflow: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2518 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2519 mono_sigctx_to_monoctx (ctx, &mctx);
2521 mono_runtime_printf_err ("Stacktrace:");
2523 memset (&ud, 0, sizeof (ud));
2525 mono_walk_stack_with_ctx (print_overflow_stack_frame, &mctx, MONO_UNWIND_LOOKUP_ACTUAL_METHOD, &ud);
2527 if (ji && !ji->is_trampoline && jinfo_get_method (ji))
2528 mono_runtime_printf_err ("At %s", mono_method_full_name (jinfo_get_method (ji), TRUE));
2530 mono_runtime_printf_err ("At <unmanaged>.");
2537 print_stack_frame_to_stderr (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2539 MonoMethod *method = NULL;
2541 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2542 method = jinfo_get_method (frame->ji);
2545 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2546 mono_runtime_printf_err (" %s", location);
2549 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2554 static G_GNUC_UNUSED gboolean
2555 print_stack_frame_to_string (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2557 GString *p = (GString*)data;
2558 MonoMethod *method = NULL;
2560 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2561 method = jinfo_get_method (frame->ji);
2563 if (method && frame->domain) {
2564 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, frame->domain);
2565 g_string_append_printf (p, " %s\n", location);
2568 g_string_append_printf (p, " at <unknown> <0x%05x>\n", frame->native_offset);
2573 #ifndef MONO_CROSS_COMPILE
2575 static void print_process_map (void)
2578 FILE *fp = fopen ("/proc/self/maps", "r");
2582 mono_runtime_printf_err ("no /proc/self/maps, not on linux?\n");
2586 mono_runtime_printf_err ("/proc/self/maps:");
2588 while (fgets (line, sizeof (line), fp)) {
2590 size_t len = strlen (line) - 1;
2591 if (len >= 0 && line [len] == '\n')
2594 mono_runtime_printf_err ("%s", line);
2603 static gboolean handling_sigsegv = FALSE;
2606 * mono_handle_native_crash:
2608 * Handle a native crash (e.g. SIGSEGV) while in native code by
2609 * printing diagnostic information and aborting.
2612 mono_handle_native_crash (const char *signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2614 #ifdef MONO_ARCH_USE_SIGACTION
2615 struct sigaction sa;
2617 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2619 gboolean is_sigsegv = !strcmp ("SIGSEGV", signal);
2621 if (handling_sigsegv && is_sigsegv)
2624 if (mini_get_debug_options ()->suspend_on_native_crash) {
2625 mono_runtime_printf_err ("Received %s, suspending...", signal);
2636 /* To prevent infinite loops when the stack walk causes a crash */
2638 handling_sigsegv = TRUE;
2640 /* !jit_tls means the thread was not registered with the runtime */
2641 if (jit_tls && mono_thread_internal_current ()) {
2642 mono_runtime_printf_err ("Stacktrace:\n");
2644 /* FIXME: Is MONO_UNWIND_LOOKUP_IL_OFFSET correct here? */
2645 mono_walk_stack (print_stack_frame_to_stderr, MONO_UNWIND_LOOKUP_IL_OFFSET, NULL);
2648 print_process_map ();
2650 #ifdef HAVE_BACKTRACE_SYMBOLS
2656 mono_runtime_printf_err ("\nNative stacktrace:\n");
2658 size = backtrace (array, 256);
2659 names = backtrace_symbols (array, size);
2660 for (i =0; i < size; ++i) {
2661 mono_runtime_printf_err ("\t%s", names [i]);
2665 /* Try to get more meaningful information using gdb */
2667 #if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_fork)
2668 if (!mini_get_debug_options ()->no_gdb_backtrace) {
2669 /* From g_spawn_command_line_sync () in eglib */
2672 pid_t crashed_pid = getpid ();
2675 * glibc fork acquires some locks, so if the crash happened inside malloc/free,
2676 * it will deadlock. Call the syscall directly instead.
2678 #if defined(PLATFORM_ANDROID)
2679 /* SYS_fork is defined to be __NR_fork which is not defined in some ndk versions */
2680 g_assert_not_reached ();
2681 #elif !defined(PLATFORM_MACOSX) && defined(SYS_fork)
2682 pid = (pid_t) syscall (SYS_fork);
2683 #elif defined(PLATFORM_MACOSX) && HAVE_FORK
2684 pid = (pid_t) fork ();
2686 g_assert_not_reached ();
2689 #if defined (HAVE_PRCTL) && defined(PR_SET_PTRACER)
2691 // Allow gdb to attach to the process even if ptrace_scope sysctl variable is set to
2692 // a value other than 0 (the most permissive ptrace scope). Most modern Linux
2693 // distributions set the scope to 1 which allows attaching only to direct children of
2694 // the current process
2695 prctl (PR_SET_PTRACER, pid, 0, 0, 0);
2699 dup2 (STDERR_FILENO, STDOUT_FILENO);
2701 mono_gdb_render_native_backtraces (crashed_pid);
2705 mono_runtime_printf_err ("\nDebug info from gdb:\n");
2706 waitpid (pid, &status, 0);
2711 #ifdef PLATFORM_ANDROID
2712 /* set DUMPABLE for this process so debuggerd can attach with ptrace(2), see:
2713 * https://android.googlesource.com/platform/bionic/+/151da681000c07da3c24cd30a3279b1ca017f452/linker/debugger.cpp#206
2714 * this has changed on later versions of Android. Also, we don't want to
2715 * set this on start-up as DUMPABLE has security implications. */
2716 prctl (PR_SET_DUMPABLE, 1);
2718 mono_runtime_printf_err ("\nNo native Android stacktrace (see debuggerd output).\n");
2723 * A SIGSEGV indicates something went very wrong so we can no longer depend
2724 * on anything working. So try to print out lots of diagnostics, starting
2725 * with ones which have a greater chance of working.
2727 mono_runtime_printf_err (
2729 "=================================================================\n"
2730 "Got a %s while executing native code. This usually indicates\n"
2731 "a fatal error in the mono runtime or one of the native libraries \n"
2732 "used by your application.\n"
2733 "=================================================================\n",
2736 #ifdef MONO_ARCH_USE_SIGACTION
2737 sa.sa_handler = SIG_DFL;
2738 sigemptyset (&sa.sa_mask);
2741 /* Remove our SIGABRT handler */
2742 g_assert (sigaction (SIGABRT, &sa, NULL) != -1);
2744 /* On some systems we get a SIGILL when calling abort (), because it might
2745 * fail to raise SIGABRT */
2746 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
2749 if (!mono_do_crash_chaining) {
2750 /*Android abort is a fluke, it doesn't abort, it triggers another segv. */
2751 #if defined (PLATFORM_ANDROID)
2762 mono_handle_native_crash (const char *signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2764 g_assert_not_reached ();
2767 #endif /* !MONO_CROSS_COMPILE */
2770 mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
2772 MonoInternalThread *thread = mono_thread_internal_current ();
2773 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2778 GError *error = NULL;
2783 text = g_string_new (0);
2785 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
2787 g_string_append_printf (text, "\n\"%s\"", name);
2790 else if (thread->threadpool_thread)
2791 g_string_append (text, "\n\"<threadpool thread>\"");
2793 g_string_append (text, "\n\"<unnamed thread>\"");
2795 g_string_append_printf (text, " tid=0x%p this=0x%p ", (gpointer)(gsize)thread->tid, thread);
2796 mono_thread_internal_describe (thread, text);
2797 g_string_append (text, "\n");
2799 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2801 memcpy (&ctx, start_ctx, sizeof (MonoContext));
2803 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_print_thread_dump);
2805 mono_sigctx_to_monoctx (sigctx, &ctx);
2807 mono_walk_stack_with_ctx (print_stack_frame_to_string, &ctx, MONO_UNWIND_LOOKUP_ALL, text);
2809 mono_runtime_printf ("\t<Stack traces in thread dumps not supported on this platform>");
2812 mono_runtime_printf ("%s", text->str);
2814 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
2815 OutputDebugStringA(text->str);
2818 g_string_free (text, TRUE);
2819 mono_runtime_stdout_fflush ();
2823 * mono_print_thread_dump:
2825 * Print information about the current thread to stdout.
2826 * \p sigctx can be NULL, allowing this to be called from gdb.
2829 mono_print_thread_dump (void *sigctx)
2831 mono_print_thread_dump_internal (sigctx, NULL);
2835 mono_print_thread_dump_from_ctx (MonoContext *ctx)
2837 mono_print_thread_dump_internal (NULL, ctx);
2841 * mono_resume_unwind:
2843 * This is called by a trampoline from LLVM compiled finally clauses to continue
2847 mono_resume_unwind (MonoContext *ctx)
2849 MONO_REQ_GC_UNSAFE_MODE;
2851 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2852 MonoContext new_ctx;
2854 MONO_CONTEXT_SET_IP (ctx, MONO_CONTEXT_GET_IP (&jit_tls->resume_state.ctx));
2855 MONO_CONTEXT_SET_SP (ctx, MONO_CONTEXT_GET_SP (&jit_tls->resume_state.ctx));
2858 mono_handle_exception_internal (&new_ctx, (MonoObject *)jit_tls->resume_state.ex_obj, TRUE, NULL);
2860 mono_restore_context (&new_ctx);
2863 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
2868 MonoJitExceptionInfo *ei;
2869 } FindHandlerBlockData;
2872 find_last_handler_block (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2876 FindHandlerBlockData *pdata = (FindHandlerBlockData *)data;
2877 MonoJitInfo *ji = frame->ji;
2882 ip = MONO_CONTEXT_GET_IP (ctx);
2884 for (i = 0; i < ji->num_clauses; ++i) {
2885 MonoJitExceptionInfo *ei = ji->clauses + i;
2886 if (ei->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2888 /*If ip points to the first instruction it means the handler block didn't start
2889 so we can leave its execution to the EH machinery*/
2890 if (ei->handler_start < ip && ip < ei->data.handler_end) {
2902 install_handler_block_guard (MonoJitInfo *ji, MonoContext *ctx)
2905 MonoJitExceptionInfo *clause = NULL;
2908 ip = MONO_CONTEXT_GET_IP (ctx);
2910 for (i = 0; i < ji->num_clauses; ++i) {
2911 clause = &ji->clauses [i];
2912 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2914 if (clause->handler_start < ip && clause->data.handler_end > ip)
2918 /*no matching finally */
2919 if (i == ji->num_clauses)
2922 /*If we stopped on the instruction right before the try, we haven't actually started executing it*/
2923 if (ip == clause->handler_start)
2926 return mono_arch_install_handler_block_guard (ji, clause, ctx, mono_create_handler_block_trampoline ());
2930 * Finds the bottom handler block running and install a block guard if needed.
2933 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2935 FindHandlerBlockData data = { 0 };
2936 MonoJitTlsData *jit_tls = (MonoJitTlsData *)ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS];
2939 #ifndef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD_AOT
2944 /* Guard against a null MonoJitTlsData. This can happens if the thread receives the
2945 * interrupt signal before the JIT has time to initialize its TLS data for the given thread.
2947 if (!jit_tls || jit_tls->handler_block_return_address)
2950 /* Do an async safe stack walk */
2951 mono_thread_info_set_is_async_context (TRUE);
2952 mono_walk_stack_with_state (find_last_handler_block, ctx, MONO_UNWIND_NONE, &data);
2953 mono_thread_info_set_is_async_context (FALSE);
2958 memcpy (&jit_tls->handler_block_context, &data.ctx, sizeof (MonoContext));
2960 resume_ip = install_handler_block_guard (data.ji, &data.ctx);
2961 if (resume_ip == NULL)
2964 jit_tls->handler_block_return_address = resume_ip;
2965 jit_tls->handler_block = data.ei;
2971 mono_current_thread_has_handle_block_guard (void)
2973 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2974 return jit_tls && jit_tls->handler_block_return_address != NULL;
2979 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2985 mono_current_thread_has_handle_block_guard (void)
2993 mono_set_cast_details (MonoClass *from, MonoClass *to)
2995 MonoJitTlsData *jit_tls = NULL;
2997 if (mini_get_debug_options ()->better_cast_details) {
2998 jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
2999 jit_tls->class_cast_from = from;
3000 jit_tls->class_cast_to = to;
3005 /*returns false if the thread is not attached*/
3007 mono_thread_state_init_from_sigctx (MonoThreadUnwindState *ctx, void *sigctx)
3009 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
3010 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
3017 mono_sigctx_to_monoctx (sigctx, &ctx->ctx);
3019 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
3020 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
3021 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
3024 mono_thread_state_init (ctx);
3027 if (!ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] || !ctx->unwind_data [MONO_UNWIND_DATA_LMF])
3033 g_error ("Implement mono_arch_sigctx_to_monoctx for the current target");
3039 mono_thread_state_init (MonoThreadUnwindState *ctx)
3041 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
3043 #if defined(MONO_CROSS_COMPILE)
3044 ctx->valid = FALSE; //A cross compiler doesn't need to suspend.
3045 #elif MONO_ARCH_HAS_MONO_CONTEXT
3046 MONO_CONTEXT_GET_CURRENT (ctx->ctx);
3048 g_error ("Use a null sigctx requires a working mono-context");
3051 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
3052 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
3053 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread ? thread->jit_data : NULL;
3059 mono_thread_state_init_from_monoctx (MonoThreadUnwindState *ctx, MonoContext *mctx)
3061 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
3068 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
3069 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
3070 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
3075 /*returns false if the thread is not attached*/
3077 mono_thread_state_init_from_current (MonoThreadUnwindState *ctx)
3079 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
3080 MONO_ARCH_CONTEXT_DEF
3082 mono_arch_flush_register_windows ();
3084 if (!thread || !thread->jit_data) {
3088 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
3089 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx->ctx);
3091 MONO_INIT_CONTEXT_FROM_FUNC (&ctx->ctx, mono_thread_state_init_from_current);
3094 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
3095 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
3096 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
3102 mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx)
3104 mono_handle_exception (ctx, (MonoObject *)exc);
3105 mono_restore_context (ctx);
3108 /*FIXME Move all monoctx -> sigctx conversion to signal handlers once all archs support utils/mono-context */
3110 mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
3112 #ifdef MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK
3113 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
3114 jit_tls->ex_ctx = *ctx;
3116 mono_arch_setup_async_callback (ctx, async_cb, user_data);
3118 g_error ("This target doesn't support mono_arch_setup_async_callback");
3123 * mono_restore_context:
3125 * Call the architecture specific restore context function.
3128 mono_restore_context (MonoContext *ctx)
3130 static void (*restore_context) (MonoContext *);
3132 if (!restore_context)
3133 restore_context = (void (*)(MonoContext *))mono_get_restore_context ();
3134 restore_context (ctx);
3135 g_assert_not_reached ();
3139 * mono_jinfo_get_unwind_info:
3141 * Return the unwind info for JI.
3144 mono_jinfo_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3146 if (ji->has_unwind_info) {
3147 /* The address/length in the MonoJitInfo structure itself */
3148 MonoUnwindJitInfo *info = mono_jit_info_get_unwind_info (ji);
3149 *unwind_info_len = info->unw_info_len;
3150 return info->unw_info;
3151 } else if (ji->from_aot)
3152 return mono_aot_get_unwind_info (ji, unwind_info_len);
3154 return mono_get_cached_unwind_info (ji->unwind_info, unwind_info_len);
3158 mono_jinfo_get_epilog_size (MonoJitInfo *ji)
3160 MonoArchEHJitInfo *info;
3162 info = mono_jit_info_get_arch_eh_info (ji);
3165 return info->epilog_size;
3169 * LLVM/Bitcode exception handling.
3173 throw_exception (MonoObject *ex, gboolean rethrow)
3175 MONO_REQ_GC_UNSAFE_MODE;
3178 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3179 MonoException *mono_ex;
3181 if (!mono_object_isinst_checked (ex, mono_defaults.exception_class, &error)) {
3182 mono_error_assert_ok (&error);
3183 mono_ex = mono_get_exception_runtime_wrapped_checked (ex, &error);
3184 mono_error_assert_ok (&error);
3185 jit_tls->thrown_non_exc = mono_gchandle_new (ex, FALSE);
3188 mono_ex = (MonoException*)ex;
3191 jit_tls->thrown_exc = mono_gchandle_new ((MonoObject*)mono_ex, FALSE);
3194 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
3195 GList *l, *ips = NULL;
3198 _Unwind_Backtrace (build_stack_trace, &ips);
3199 /* The list contains ip-gshared info pairs */
3201 ips = g_list_reverse (ips);
3202 for (l = ips; l; l = l->next) {
3203 trace = g_list_append (trace, l->data);
3204 trace = g_list_append (trace, NULL);
3206 MonoArray *ips_arr = mono_glist_to_array (trace, mono_defaults.int_class, &error);
3207 mono_error_assert_ok (&error);
3208 MONO_OBJECT_SETREF (mono_ex, trace_ips, ips_arr);
3210 g_list_free (trace);
3214 mono_llvm_cpp_throw_exception ();
3218 mono_llvm_throw_exception (MonoObject *ex)
3220 throw_exception (ex, FALSE);
3224 mono_llvm_rethrow_exception (MonoObject *ex)
3226 throw_exception (ex, TRUE);
3230 mono_llvm_raise_exception (MonoException *e)
3232 mono_llvm_throw_exception ((MonoObject*)e);
3236 mono_llvm_throw_corlib_exception (guint32 ex_token_index)
3238 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
3241 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
3243 mono_llvm_throw_exception ((MonoObject*)ex);
3247 * mono_llvm_resume_exception:
3249 * Resume exception propagation.
3252 mono_llvm_resume_exception (void)
3254 mono_llvm_cpp_throw_exception ();
3258 * mono_llvm_load_exception:
3260 * Return the currently thrown exception.
3263 mono_llvm_load_exception (void)
3266 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3268 MonoException *mono_ex = (MonoException*)mono_gchandle_get_target (jit_tls->thrown_exc);
3270 if (mono_ex->trace_ips) {
3271 GList *trace_ips = NULL;
3272 gpointer ip = __builtin_return_address (0);
3274 size_t upper = mono_array_length (mono_ex->trace_ips);
3276 for (int i = 0; i < upper; i+= 2) {
3277 gpointer curr_ip = mono_array_get (mono_ex->trace_ips, gpointer, i);
3278 gpointer curr_info = mono_array_get (mono_ex->trace_ips, gpointer, i + 1);
3279 trace_ips = g_list_append (trace_ips, curr_ip);
3280 trace_ips = g_list_append (trace_ips, curr_info);
3286 // FIXME: Does this work correctly for rethrows?
3287 // We may be discarding useful information
3288 // when this gets GC'ed
3289 MonoArray *ips_arr = mono_glist_to_array (trace_ips, mono_defaults.int_class, &error);
3290 mono_error_assert_ok (&error);
3291 MONO_OBJECT_SETREF (mono_ex, trace_ips, ips_arr);
3292 g_list_free (trace_ips);
3295 //MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
3297 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_array_new_checked (mono_domain_get (), mono_defaults.int_class, 0, &error));
3298 mono_error_assert_ok (&error);
3299 MONO_OBJECT_SETREF (mono_ex, stack_trace, mono_array_new_checked (mono_domain_get (), mono_defaults.stack_frame_class, 0, &error));
3300 mono_error_assert_ok (&error);
3303 return &mono_ex->object;
3307 * mono_llvm_clear_exception:
3309 * Mark the currently thrown exception as handled.
3312 mono_llvm_clear_exception (void)
3314 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3315 mono_gchandle_free (jit_tls->thrown_exc);
3316 jit_tls->thrown_exc = 0;
3317 if (jit_tls->thrown_non_exc)
3318 mono_gchandle_free (jit_tls->thrown_non_exc);
3319 jit_tls->thrown_non_exc = 0;
3321 mono_memory_barrier ();
3325 * mono_llvm_match_exception:
3327 * Return the innermost clause containing REGION_START-REGION_END which can handle
3328 * the current exception.
3331 mono_llvm_match_exception (MonoJitInfo *jinfo, guint32 region_start, guint32 region_end, gpointer rgctx, MonoObject *this_obj)
3334 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3338 g_assert (jit_tls->thrown_exc);
3339 exc = mono_gchandle_get_target (jit_tls->thrown_exc);
3340 if (jit_tls->thrown_non_exc) {
3342 * Have to unwrap RuntimeWrappedExceptions if the
3343 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
3345 if (!wrap_non_exception_throws (jinfo_get_method (jinfo)))
3346 exc = mono_gchandle_get_target (jit_tls->thrown_non_exc);
3349 for (int i = 0; i < jinfo->num_clauses; i++) {
3350 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
3351 MonoClass *catch_class;
3353 if (! (ei->try_offset == region_start && ei->try_offset + ei->try_len == region_end) )
3356 catch_class = ei->data.catch_class;
3357 if (mono_class_is_open_constructed_type (&catch_class->byval_arg)) {
3358 MonoGenericContext context;
3359 MonoType *inflated_type;
3361 g_assert (rgctx || this_obj);
3362 context = get_generic_context_from_stack_frame (jinfo, rgctx ? rgctx : this_obj->vtable);
3363 inflated_type = mono_class_inflate_generic_type_checked (&catch_class->byval_arg, &context, &error);
3364 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
3366 catch_class = mono_class_from_mono_type (inflated_type);
3367 mono_metadata_free_type (inflated_type);
3370 // FIXME: Handle edge cases handled in get_exception_catch_class
3371 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (exc, catch_class, &error)) {
3372 index = ei->clause_index;
3375 mono_error_assert_ok (&error);
3377 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3378 g_assert_not_reached ();
3387 mono_debug_personality (int a, _Unwind_Action b,
3388 uint64_t c, struct _Unwind_Exception *d, struct _Unwind_Context *e)
3390 g_assert_not_reached ();
3394 mono_debug_personality (void);
3397 mono_debug_personality (void)
3399 g_assert_not_reached ();
3403 #ifndef ENABLE_INTERPRETER
3404 /* Stubs of interpreter functions */
3406 mono_interp_set_resume_state (MonoException *ex, StackFrameInfo *frame, gpointer handler_ip)
3408 g_assert_not_reached ();
3412 mono_interp_run_finally (StackFrameInfo *frame, int clause_index, gpointer handler_ip)
3414 g_assert_not_reached ();
3418 mono_interp_frame_iter_init (MonoInterpStackIter *iter, gpointer interp_exit_data)
3420 g_assert_not_reached ();
3424 mono_interp_frame_iter_next (MonoInterpStackIter *iter, StackFrameInfo *frame)
3426 g_assert_not_reached ();