static const char* get_assembly_name (MonoErrorInternal *error) { if (error->assembly_name) return error->assembly_name; MonoClass *klass = get_class (error); if (klass && m_class_get_image (klass)) return m_class_get_image (klass)->name; return "<unknown assembly>"; }
/* * set_type_load_exception_type * * Set MONO_EXCEPTION_TYPE_LOAD on the specified 'class' and provide * a descriptive message for the exception. This message is also, * optionally, being logged (export MONO_LOG_MASK="security") for * debugging purposes. */ static void set_type_load_exception_type (const char *format, MonoClass *klass) { char *type_name = mono_type_get_full_name (klass); char *parent_name = mono_type_get_full_name (m_class_get_parent (klass)); char *message = mono_image_strdup_printf (m_class_get_image (klass), format, type_name, parent_name); g_free (parent_name); g_free (type_name); mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_SECURITY, "%s", message); mono_class_set_type_load_failure (klass, "%s", message); // note: do not free string given to mono_class_set_failure }
/** * mono_debug_print_stack_frame: * \param native_offset Native offset within the \p method's machine code. * Conventient wrapper around \c mono_debug_lookup_source_location which can be * used if you only want to use the location to print a stack frame. */ gchar * mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDomain *domain) { MonoDebugSourceLocation *location; gchar *fname, *ptr, *res; int offset; fname = mono_method_full_name (method, TRUE); for (ptr = fname; *ptr; ptr++) { if (*ptr == ':') *ptr = '.'; } location = mono_debug_lookup_source_location (method, native_offset, domain); if (!location) { if (mono_debug_initialized) { mono_debugger_lock (); offset = il_offset_from_address (method, domain, native_offset); mono_debugger_unlock (); } else { offset = -1; } if (offset < 0 && get_seq_point) offset = get_seq_point (domain, method, native_offset); if (offset < 0) res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset); else { char *mvid = mono_guid_to_string_minimal ((uint8_t*)m_class_get_image (method->klass)->heap_guid.data); char *aotid = mono_runtime_get_aotid (); if (aotid) res = g_strdup_printf ("at %s [0x%05x] in <%s#%s>:0" , fname, offset, mvid, aotid); else res = g_strdup_printf ("at %s [0x%05x] in <%s>:0" , fname, offset, mvid); g_free (aotid); g_free (mvid); } g_free (fname); return res; } res = g_strdup_printf ("at %s [0x%05x] in %s:%d", fname, location->il_offset, location->source_file, location->row); g_free (fname); mono_debug_free_source_location (location); return res; }
void mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index, mgreg_t eip, gint32 pc_offset) { guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index; MonoException *ex; ex = mono_exception_from_token (m_class_get_image (mono_defaults.exception_class), ex_token); eip -= pc_offset; /* Negate the ip adjustment done in mono_x86_throw_exception () */ eip += 1; mono_x86_throw_exception (regs, (MonoObject*)ex, eip, FALSE); }
static inline gboolean mono_class_is_magic_assembly (MonoClass *klass) { const char *aname = m_class_get_image (klass)->assembly_name; if (!aname) return FALSE; if (!strcmp ("Xamarin.iOS", aname)) return TRUE; if (!strcmp ("Xamarin.Mac", aname)) return TRUE; if (!strcmp ("Xamarin.WatchOS", aname)) return TRUE; /* regression test suite */ if (!strcmp ("builtin-types", aname)) return TRUE; if (!strcmp ("mini_tests", aname)) return TRUE; return FALSE; }
gboolean mono_class_is_magic_float (MonoClass *klass) { static MonoClass *magic_nfloat_class; if (klass == magic_nfloat_class) return TRUE; if (magic_nfloat_class) return FALSE; if (!mono_class_is_magic_assembly (klass)) return FALSE; if (strcmp ("System", m_class_get_name_space (klass)) != 0) return FALSE; if (strcmp ("nfloat", m_class_get_name (klass)) == 0) { magic_nfloat_class = klass; /* Assert that we are using the matching assembly */ MonoClassField *value_field = mono_class_get_field_from_name_full (klass, "v", NULL); g_assert (value_field); MonoType *t = mono_field_get_type (value_field); MonoType *native = mini_native_type_replace_type (m_class_get_byval_arg (klass)); if (t->type != native->type) g_error ("Assembly used for native types '%s' doesn't match this runtime, %s is mapped to %s, expecting %s.\n", m_class_get_image (klass)->name, m_class_get_name (klass), mono_type_full_name (t), mono_type_full_name (native)); return TRUE; } return FALSE; }
static MonoMethod * create_method_ilgen (MonoMethodBuilder *mb, MonoMethodSignature *signature, int max_stack) { MonoMethodHeader *header; MonoMethodWrapper *mw; MonoImage *image; MonoMethod *method; GList *l; int i; g_assert (mb != NULL); image = m_class_get_image (mb->method->klass); if (mb->dynamic) { method = mb->method; mw = (MonoMethodWrapper*)method; method->name = mb->name; method->dynamic = TRUE; mw->header = header = (MonoMethodHeader *) g_malloc0 (MONO_SIZEOF_METHOD_HEADER + mb->locals * sizeof (MonoType *)); header->code = mb->code; for (i = 0, l = mb->locals_list; l; l = l->next, i++) { header->locals [i] = (MonoType*)l->data; } } else { /* Realloc the method info into a mempool */ method = (MonoMethod *)mono_image_alloc0 (image, sizeof (MonoMethodWrapper)); memcpy (method, mb->method, sizeof (MonoMethodWrapper)); mw = (MonoMethodWrapper*) method; if (mb->no_dup_name) method->name = mb->name; else method->name = mono_image_strdup (image, mb->name); mw->header = header = (MonoMethodHeader *) mono_image_alloc0 (image, MONO_SIZEOF_METHOD_HEADER + mb->locals * sizeof (MonoType *)); header->code = (const unsigned char *)mono_image_alloc (image, mb->pos); memcpy ((char*)header->code, mb->code, mb->pos); for (i = 0, l = mb->locals_list; l; l = l->next, i++) { header->locals [i] = (MonoType*)l->data; } } /* Free the locals list so mono_mb_free () doesn't free the types twice */ g_list_free (mb->locals_list); mb->locals_list = NULL; method->signature = signature; if (!signature->hasthis) method->flags |= METHOD_ATTRIBUTE_STATIC; if (max_stack < 8) max_stack = 8; header->max_stack = max_stack; header->code_size = mb->pos; header->num_locals = mb->locals; header->init_locals = mb->init_locals; header->volatile_args = mb->volatile_args; header->volatile_locals = mb->volatile_locals; mb->volatile_args = NULL; mb->volatile_locals = NULL; header->num_clauses = mb->num_clauses; header->clauses = mb->clauses; method->skip_visibility = mb->skip_visibility; i = g_list_length ((GList *)mw->method_data); if (i) { GList *tmp; void **data; l = g_list_reverse ((GList *)mw->method_data); if (method_is_dynamic (method)) data = (void **)g_malloc (sizeof (gpointer) * (i + 1)); else data = (void **)mono_image_alloc (image, sizeof (gpointer) * (i + 1)); /* store the size in the first element */ data [0] = GUINT_TO_POINTER (i); i = 1; for (tmp = l; tmp; tmp = tmp->next) { data [i++] = tmp->data; } g_list_free (l); mw->method_data = data; } /*{ static int total_code = 0; static int total_alloc = 0; total_code += mb->pos; total_alloc += mb->code_size; g_print ("code size: %d of %d (allocated: %d)\n", mb->pos, total_code, total_alloc); }*/ #ifdef DEBUG_RUNTIME_CODE printf ("RUNTIME CODE FOR %s\n", mono_method_full_name (method, TRUE)); printf ("%s\n", mono_disasm_code (&marshal_dh, method, mb->code, mb->code + mb->pos)); #endif if (mb->param_names) { char **param_names = (char **)mono_image_alloc0 (image, signature->param_count * sizeof (gpointer)); for (i = 0; i < signature->param_count; ++i) param_names [i] = mono_image_strdup (image, mb->param_names [i]); mono_image_lock (image); if (!image->wrapper_param_names) image->wrapper_param_names = g_hash_table_new (NULL, NULL); g_hash_table_insert (image->wrapper_param_names, method, param_names); mono_image_unlock (image); } return method; }
/* * actually, we might want to queue the finalize requests in a separate thread, * but we need to be careful about the execution domain of the thread... */ void mono_gc_run_finalize (void *obj, void *data) { ERROR_DECL (error); MonoObject *exc = NULL; MonoObject *o; #ifndef HAVE_SGEN_GC MonoObject *o2; #endif MonoMethod* finalizer = NULL; MonoDomain *caller_domain = mono_domain_get (); MonoDomain *domain; // This function is called from the innards of the GC, so our best alternative for now is to do polling here mono_threads_safepoint (); o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data)); const char *o_ns = m_class_get_name_space (mono_object_class (o)); const char *o_name = m_class_get_name (mono_object_class (o)); if (mono_do_not_finalize) { if (!mono_do_not_finalize_class_names) return; size_t namespace_len = strlen (o_ns); for (int i = 0; mono_do_not_finalize_class_names [i]; ++i) { const char *name = mono_do_not_finalize_class_names [i]; if (strncmp (name, o_ns, namespace_len)) break; if (name [namespace_len] != '.') break; if (strcmp (name + namespace_len + 1, o_name)) break; return; } } if (mono_log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_DEBUG, "<%s at %p> Starting finalizer checks.", o_name, o); if (suspend_finalizers) return; domain = o->vtable->domain; #ifndef HAVE_SGEN_GC mono_domain_finalizers_lock (domain); o2 = (MonoObject *)g_hash_table_lookup (domain->finalizable_objects_hash, o); mono_domain_finalizers_unlock (domain); if (!o2) /* Already finalized somehow */ return; #endif /* make sure the finalizer is not called again if the object is resurrected */ object_register_finalizer ((MonoObject *)obj, NULL); if (mono_log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Registered finalizer as processed.", o_name, o); if (o->vtable->klass == mono_defaults.internal_thread_class) { MonoInternalThread *t = (MonoInternalThread*)o; if (mono_gc_is_finalizer_internal_thread (t)) /* Avoid finalizing ourselves */ return; } if (m_class_get_image (mono_object_class (o)) == mono_defaults.corlib && !strcmp (o_name, "DynamicMethod") && finalizing_root_domain) { /* * These can't be finalized during unloading/shutdown, since that would * free the native code which can still be referenced by other * finalizers. * FIXME: This is not perfect, objects dying at the same time as * dynamic methods can still reference them even when !shutdown. */ return; } if (mono_runtime_get_no_exec ()) return; /* speedup later... and use a timeout */ /* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */ /* Use _internal here, since this thread can enter a doomed appdomain */ mono_domain_set_internal (mono_object_domain (o)); /* delegates that have a native function pointer allocated are * registered for finalization, but they don't have a Finalize * method, because in most cases it's not needed and it's just a waste. */ if (m_class_is_delegate (mono_object_class (o))) { MonoDelegate* del = (MonoDelegate*)o; if (del->delegate_trampoline) mono_delegate_free_ftnptr ((MonoDelegate*)o); mono_domain_set_internal (caller_domain); return; } finalizer = mono_class_get_finalizer (o->vtable->klass); /* If object has a CCW but has no finalizer, it was only * registered for finalization in order to free the CCW. * Else it needs the regular finalizer run. * FIXME: what to do about ressurection and suppression * of finalizer on object with CCW. */ if (mono_marshal_free_ccw (o) && !finalizer) { mono_domain_set_internal (caller_domain); return; } /* * To avoid the locking plus the other overhead of mono_runtime_invoke_checked (), * create and precompile a wrapper which calls the finalize method using * a CALLVIRT. */ if (mono_log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Compiling finalizer.", o_name, o); #ifndef HOST_WASM if (!domain->finalize_runtime_invoke) { MonoMethod *finalize_method = mono_class_get_method_from_name_checked (mono_defaults.object_class, "Finalize", 0, 0, error); mono_error_assert_ok (error); MonoMethod *invoke = mono_marshal_get_runtime_invoke (finalize_method, TRUE); domain->finalize_runtime_invoke = mono_compile_method_checked (invoke, error); mono_error_assert_ok (error); /* expect this not to fail */ } RuntimeInvokeFunction runtime_invoke = (RuntimeInvokeFunction)domain->finalize_runtime_invoke; #endif mono_runtime_class_init_full (o->vtable, error); goto_if_nok (error, unhandled_error); if (G_UNLIKELY (MONO_GC_FINALIZE_INVOKE_ENABLED ())) { MONO_GC_FINALIZE_INVOKE ((unsigned long)o, mono_object_get_size_internal (o), o_ns, o_name); } if (mono_log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Calling finalizer.", o_name, o); MONO_PROFILER_RAISE (gc_finalizing_object, (o)); #ifdef HOST_WASM if (finalizer) { // null finalizers work fine when using the vcall invoke as Object has an empty one gpointer params [1]; params [0] = NULL; mono_runtime_try_invoke (finalizer, o, params, &exc, error); } #else runtime_invoke (o, NULL, &exc, NULL); #endif MONO_PROFILER_RAISE (gc_finalized_object, (o)); if (mono_log_finalizers) g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Returned from finalizer.", o_name, o); unhandled_error: if (!is_ok (error)) exc = (MonoObject*)mono_error_convert_to_exception (error); if (exc) mono_thread_internal_unhandled_exception (exc); mono_domain_set_internal (caller_domain); }