From b30ce960dbaaee20aa275aeeebcab73342602740 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Tue, 8 Nov 2016 11:37:54 -0500 Subject: [PATCH] [coop] Use coop handles for Mono.Runtime.GetNativeStackTrace Implement mono_exception_get_native_backtrace in terms of mono_exception_handle_get_native_backtrace. --- mono/metadata/exception.c | 33 ++++++++++++++++++-------------- mono/metadata/icall-def.h | 2 +- mono/metadata/object-internals.h | 9 ++++++--- mono/metadata/object.c | 11 ++++++++++- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/mono/metadata/exception.c b/mono/metadata/exception.c index b4be09844ac..79454c3f38b 100644 --- a/mono/metadata/exception.c +++ b/mono/metadata/exception.c @@ -962,29 +962,32 @@ mono_exception_get_managed_backtrace (MonoException *exc) } char * -mono_exception_get_native_backtrace (MonoException *exc) +mono_exception_handle_get_native_backtrace (MonoExceptionHandle exc) { #ifdef HAVE_BACKTRACE_SYMBOLS MonoDomain *domain; - MonoArray *arr = exc->native_trace_ips; + MonoArrayHandle arr = MONO_HANDLE_NEW(MonoArray, NULL); int i, len; GString *text; char **messages; - if (!arr) + MONO_HANDLE_GET (arr, exc, native_trace_ips); + + if (MONO_HANDLE_IS_NULL(arr)) return g_strdup (""); domain = mono_domain_get (); - len = mono_array_length (arr); + len = mono_array_handle_length (arr); text = g_string_new_len (NULL, len * 20); - uint32_t gchandle = mono_gchandle_new (&arr->obj, TRUE); /* pinned */ - void* addr = mono_array_addr (arr, gpointer, 0); + uint32_t gchandle; + void *addr = MONO_ARRAY_HANDLE_PIN (arr, gpointer, 0, &gchandle); MONO_ENTER_GC_SAFE; messages = backtrace_symbols (addr, len); MONO_EXIT_GC_SAFE; mono_gchandle_free (gchandle); for (i = 0; i < len; ++i) { - gpointer ip = mono_array_get (arr, gpointer, i); + gpointer ip; + MONO_HANDLE_ARRAY_GETVAL (ip, arr, gpointer, i); MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip); if (ji) { char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain); @@ -1002,18 +1005,20 @@ mono_exception_get_native_backtrace (MonoException *exc) #endif } -MonoString * -ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc) +MonoStringHandle +ves_icall_Mono_Runtime_GetNativeStackTrace (MonoExceptionHandle exc, MonoError *error) { char *trace; - MonoString *res; + MonoStringHandle res; + mono_error_init (error); + if (!exc) { - mono_set_pending_exception (mono_get_exception_argument_null ("exception")); - return NULL; + mono_error_set_argument_null (error, "exception", ""); + return NULL_HANDLE_STRING; } - trace = mono_exception_get_native_backtrace (exc); - res = mono_string_new (mono_domain_get (), trace); + trace = mono_exception_handle_get_native_backtrace (exc); + res = mono_string_new_handle (mono_domain_get (), trace, error); g_free (trace); return res; } diff --git a/mono/metadata/icall-def.h b/mono/metadata/icall-def.h index d7d3e62aa11..b622700231b 100644 --- a/mono/metadata/icall-def.h +++ b/mono/metadata/icall-def.h @@ -88,8 +88,8 @@ ICALL_TYPE(TLS_PROVIDER_FACTORY, "Mono.Net.Security.MonoTlsProviderFactory", TLS ICALL(TLS_PROVIDER_FACTORY_1, "IsBtlsSupported", ves_icall_Mono_TlsProviderFactory_IsBtlsSupported) ICALL_TYPE(RUNTIME, "Mono.Runtime", RUNTIME_1) -ICALL(RUNTIME_12, "GetNativeStackTrace", ves_icall_Mono_Runtime_GetNativeStackTrace) HANDLES(ICALL(RUNTIME_1, "GetDisplayName", ves_icall_Mono_Runtime_GetDisplayName)) +HANDLES(ICALL(RUNTIME_12, "GetNativeStackTrace", ves_icall_Mono_Runtime_GetNativeStackTrace)) ICALL_TYPE(RTCLASS, "Mono.RuntimeClassHandle", RTCLASS_1) ICALL(RTCLASS_1, "GetTypeFromClass", ves_icall_Mono_RuntimeClassHandle_GetTypeFromClass) diff --git a/mono/metadata/object-internals.h b/mono/metadata/object-internals.h index 46269e2a377..b3ad83d12e7 100644 --- a/mono/metadata/object-internals.h +++ b/mono/metadata/object-internals.h @@ -219,6 +219,9 @@ struct _MonoException { MonoArray *native_trace_ips; }; +/* Safely access System.Exception from native code */ +TYPED_HANDLE_DECL (MonoException); + typedef struct { MonoException base; } MonoSystemException; @@ -1627,10 +1630,10 @@ MonoString* mono_string_intern_checked (MonoString *str, MonoError *error); char * -mono_exception_get_native_backtrace (MonoException *exc); +mono_exception_handle_get_native_backtrace (MonoExceptionHandle exc); -MonoString * -ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc); +MonoStringHandle +ves_icall_Mono_Runtime_GetNativeStackTrace (MonoExceptionHandle exc, MonoError *erro); char * mono_exception_get_managed_backtrace (MonoException *exc); diff --git a/mono/metadata/object.c b/mono/metadata/object.c index 07982207428..0c8d83cffbf 100644 --- a/mono/metadata/object.c +++ b/mono/metadata/object.c @@ -7700,6 +7700,15 @@ mono_object_try_to_string (MonoObject *obj, MonoObject **exc, MonoError *error) +static char * +get_native_backtrace (MonoException *exc_raw) +{ + HANDLE_FUNCTION_ENTER (); + MONO_HANDLE_DCL(MonoException, exc); + char * trace = mono_exception_handle_get_native_backtrace (exc); + HANDLE_FUNCTION_RETURN_VAL (trace); +} + /** * mono_print_unhandled_exception: * @exc: The exception @@ -7725,7 +7734,7 @@ mono_print_unhandled_exception (MonoObject *exc) } else { if (((MonoException*)exc)->native_trace_ips) { - message = mono_exception_get_native_backtrace ((MonoException*)exc); + message = get_native_backtrace ((MonoException*)exc); free_message = TRUE; } else { MonoObject *other_exc = NULL; -- 2.25.1