* Paolo Molaro (lupus@ximian.com)
*
* Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
- * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
+ * Copyright 2004-2011 Novell, Inc (http://www.novell.com)
+ * Copyright 2001 Xamarin Inc (http://www.xamarin.com)
*/
#include <config.h>
#ifdef HAVE_ALLOCA_H
#include <mono/utils/strenc.h>
#include <mono/utils/mono-counters.h>
#include <mono/utils/mono-error-internals.h>
+#include <mono/utils/mono-memory-model.h>
#include "cominterop.h"
#ifdef HAVE_BOEHM_GC
static void
free_main_args (void);
+static char *
+mono_string_to_utf8_internal (MonoMemPool *mp, MonoImage *image, MonoString *s, gboolean ignore_error, MonoError *error);
+
+
#define ldstr_lock() EnterCriticalSection (&ldstr_section)
#define ldstr_unlock() LeaveCriticalSection (&ldstr_section)
static CRITICAL_SECTION ldstr_section;
MonoClass *klass = this->vtable->klass;
method = mono_class_get_method_from_name (klass, ".ctor", 0);
- g_assert (method);
+ if (!method)
+ g_error ("Could not lookup zero argument constructor for class %s", mono_type_get_full_name (klass));
if (method->klass->valuetype)
this = mono_object_unbox (this);
void
mono_thread_set_main (MonoThread *thread)
{
+ static gboolean registered = FALSE;
+
+ if (!registered) {
+ MONO_GC_REGISTER_ROOT_SINGLE (main_thread);
+ registered = TRUE;
+ }
+
main_thread = thread;
}
MonoException *ex;
gchar *full_name;
- g_assert (vtable->init_failed);
-
+ if (!vtable->init_failed)
+ g_error ("Trying to get the init exception for a non-failed vtable of class %s", mono_type_get_full_name (klass));
+
/*
* If the initializing thread was rudely aborted, the exception is not stored
* in the hash.
g_hash_table_remove (type_initialization_hash, vtable);
g_free (lock);
}
+ mono_memory_barrier ();
if (!vtable->init_failed)
vtable->initialized = 1;
mono_type_initialization_unlock ();
}
static gpointer
-default_delegate_trampoline (MonoClass *klass)
+default_delegate_trampoline (MonoDomain *domain, MonoClass *klass)
{
g_assert_not_reached ();
return NULL;
gpointer
mono_runtime_create_delegate_trampoline (MonoClass *klass)
{
- return arch_create_delegate_trampoline (klass);
+ return arch_create_delegate_trampoline (mono_domain_get (), klass);
}
static MonoFreeMethodFunc default_mono_free_method = NULL;
return bitmap;
}
+/**
+ * mono_class_compute_bitmap:
+ *
+ * Mono internal function to compute a bitmap of reference fields in a class.
+ */
+gsize*
+mono_class_compute_bitmap (MonoClass *class, gsize *bitmap, int size, int offset, int *max_set, gboolean static_fields)
+{
+ return compute_class_bitmap (class, bitmap, size, offset, max_set, static_fields);
+}
+
#if 0
/*
* similar to the above, but sets the bits in the bitmap for any non-ref field
mono_class_compute_gc_descriptor (class->element_class);
if (!class->element_class->valuetype) {
gsize abm = 1;
- class->gc_descr = mono_gc_make_descr_for_array (TRUE, &abm, 1, sizeof (gpointer));
+ class->gc_descr = mono_gc_make_descr_for_array (class->byval_arg.type == MONO_TYPE_SZARRAY, &abm, 1, sizeof (gpointer));
/*printf ("new array descriptor: 0x%x for %s.%s\n", class->gc_descr,
class->name_space, class->name);*/
} else {
/* remove the object header */
bitmap = compute_class_bitmap (class->element_class, default_bitmap, sizeof (default_bitmap) * 8, - (int)(sizeof (MonoObject) / sizeof (gpointer)), &max_set, FALSE);
- class->gc_descr = mono_gc_make_descr_for_array (TRUE, bitmap, mono_array_element_size (class) / sizeof (gpointer), mono_array_element_size (class));
+ class->gc_descr = mono_gc_make_descr_for_array (class->byval_arg.type == MONO_TYPE_SZARRAY, bitmap, mono_array_element_size (class) / sizeof (gpointer), mono_array_element_size (class));
/*printf ("new vt array descriptor: 0x%x for %s.%s\n", class->gc_descr,
class->name_space, class->name);*/
if (bitmap != default_bitmap)
hashes = hashes_start;
if (! MONO_CLASS_IS_INTERFACE (method->klass)) {
- printf ("mono_method_get_imt_slot: %s.%s.%s is not an interface MonoMethod\n",
+ g_error ("mono_method_get_imt_slot: %s.%s.%s is not an interface MonoMethod",
method->klass->name_space, method->klass->name, method->name);
- g_assert_not_reached ();
}
/* Initialize hashes */
p = mono_domain_code_reserve (domain, size);
*p = size;
+ mono_domain_lock (domain);
+ if (!domain->generic_virtual_thunks)
+ domain->generic_virtual_thunks = g_hash_table_new (NULL, NULL);
+ g_hash_table_insert (domain->generic_virtual_thunks, p, p);
+ mono_domain_unlock (domain);
+
return p + 1;
}
{
guint32 *p = code;
MonoThunkFreeList *l = (MonoThunkFreeList*)(p - 1);
+ gboolean found = FALSE;
+
+ mono_domain_lock (domain);
+ if (!domain->generic_virtual_thunks)
+ domain->generic_virtual_thunks = g_hash_table_new (NULL, NULL);
+ if (g_hash_table_lookup (domain->generic_virtual_thunks, l))
+ found = TRUE;
+ mono_domain_unlock (domain);
+ if (!found)
+ /* Not allocated by mono_method_alloc_generic_virtual_thunk (), i.e. AOT */
+ return;
init_thunk_free_lists (domain);
while (domain->thunk_free_lists [0] && domain->thunk_free_lists [0]->length >= MAX_WAIT_LENGTH) {
g_ptr_array_free (sorted, TRUE);
}
+#ifndef __native_client__
+ /* We don't re-use any thunks as there is a lot of overhead */
+ /* to deleting and re-using code in Native Client. */
if (old_thunk != vtable_trampoline && old_thunk != imt_trampoline)
invalidate_generic_virtual_thunk (domain, old_thunk);
+#endif
}
mono_domain_unlock (domain);
MonoClassRuntimeInfo *runtime_info, *old_info;
MonoClassField *field;
char *t;
- int i;
+ int i, vtable_slots;
int imt_table_bytes = 0;
+ int gc_bits;
guint32 vtable_size, class_size;
guint32 cindex;
gpointer iter;
if (class->generic_class && !class->vtable)
mono_class_check_vtable_constraints (class, NULL);
+ /* Initialize klass->has_finalize */
+ mono_class_has_finalizer (class);
+
if (class->exception_type) {
mono_domain_unlock (domain);
mono_loader_unlock ();
return NULL;
}
+ vtable_slots = class->vtable_size;
+ /* we add an additional vtable slot to store the pointer to static field data only when needed */
+ class_size = mono_class_data_size (class);
+ if (class_size)
+ vtable_slots++;
+
if (ARCH_USE_IMT) {
- vtable_size = MONO_SIZEOF_VTABLE + class->vtable_size * sizeof (gpointer);
+ vtable_size = MONO_SIZEOF_VTABLE + vtable_slots * sizeof (gpointer);
if (class->interface_offsets_count) {
imt_table_bytes = sizeof (gpointer) * (MONO_IMT_SIZE);
vtable_size += sizeof (gpointer) * (MONO_IMT_SIZE);
}
} else {
vtable_size = sizeof (gpointer) * (class->max_interface_id + 1) +
- MONO_SIZEOF_VTABLE + class->vtable_size * sizeof (gpointer);
+ MONO_SIZEOF_VTABLE + vtable_slots * sizeof (gpointer);
}
mono_stats.used_class_count++;
#endif
vt->gc_descr = class->gc_descr;
- if ((class_size = mono_class_data_size (class))) {
+ gc_bits = mono_gc_get_vtable_bits (class);
+ g_assert (!(gc_bits & ~((1 << MONO_VTABLE_AVAILABLE_GC_BITS) - 1)));
+
+ vt->gc_bits = gc_bits;
+
+ if (class_size) {
+ /* we store the static field pointer at the end of the vtable: vt->vtable [class->vtable_size] */
if (class->has_static_refs) {
gpointer statics_gc_descr;
int max_set = 0;
bitmap = compute_class_bitmap (class, default_bitmap, sizeof (default_bitmap) * 8, 0, &max_set, TRUE);
/*g_print ("bitmap 0x%x for %s.%s (size: %d)\n", bitmap [0], class->name_space, class->name, class_size);*/
statics_gc_descr = mono_gc_make_descr_from_bitmap (bitmap, max_set + 1);
- vt->data = mono_gc_alloc_fixed (class_size, statics_gc_descr);
- mono_domain_add_class_static_data (domain, class, vt->data, NULL);
+ vt->vtable [class->vtable_size] = mono_gc_alloc_fixed (class_size, statics_gc_descr);
+ mono_domain_add_class_static_data (domain, class, vt->vtable [class->vtable_size], NULL);
if (bitmap != default_bitmap)
g_free (bitmap);
} else {
- vt->data = mono_domain_alloc0 (domain, class_size);
+ vt->vtable [class->vtable_size] = mono_domain_alloc0 (domain, class_size);
}
+ vt->has_static_fields = TRUE;
mono_stats.class_static_data_size += class_size;
}
gsize default_bitmap [4] = {0};
gsize *bitmap;
int max_set = 0;
+ int numbits;
MonoClass *fclass;
if (mono_type_is_reference (field->type)) {
default_bitmap [0] = 1;
- max_set = 1;
+ numbits = 1;
bitmap = default_bitmap;
} else if (mono_type_is_struct (field->type)) {
fclass = mono_class_from_mono_type (field->type);
bitmap = compute_class_bitmap (fclass, default_bitmap, sizeof (default_bitmap) * 8, 0, &max_set, FALSE);
+ numbits = max_set + 1;
} else {
default_bitmap [0] = 0;
- max_set = 0;
+ numbits = 0;
bitmap = default_bitmap;
}
size = mono_type_size (field->type, &align);
- offset = mono_alloc_special_static_data (special_static, size, align, bitmap, max_set);
+ offset = mono_alloc_special_static_data (special_static, size, align, (uintptr_t*)bitmap, numbits);
if (!domain->special_static_fields)
domain->special_static_fields = g_hash_table_new (NULL, NULL);
g_hash_table_insert (domain->special_static_fields, field, GUINT_TO_POINTER (offset));
const char *data = mono_field_get_data (field);
g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT));
- t = (char*)vt->data + field->offset;
+ t = (char*)mono_vtable_get_static_field_data (vt) + field->offset;
/* some fields don't really have rva, they are just zeroed (bss? bug #343083) */
if (!data)
continue;
}
}
+ /* Initialize vtable */
+ if (callbacks.get_vtable_trampoline) {
+ // This also covers the AOT case
+ for (i = 0; i < class->vtable_size; ++i) {
+ vt->vtable [i] = callbacks.get_vtable_trampoline (i);
+ }
+ } else {
+ mono_class_setup_vtable (class);
+
+ for (i = 0; i < class->vtable_size; ++i) {
+ MonoMethod *cm;
+
+ if ((cm = class->vtable [i]))
+ vt->vtable [i] = arch_create_jit_trampoline (cm);
+ }
+ }
+
+ if (ARCH_USE_IMT && imt_table_bytes) {
+ /* Now that the vtable is full, we can actually fill up the IMT */
+ if (callbacks.get_imt_trampoline) {
+ /* lazy construction of the IMT entries enabled */
+ for (i = 0; i < MONO_IMT_SIZE; ++i)
+ interface_offsets [i] = callbacks.get_imt_trampoline (i);
+ } else {
+ build_imt (class, vt, domain, interface_offsets, NULL);
+ }
+ }
+
+ /*
+ * FIXME: Is it ok to allocate while holding the domain/loader locks ? If not, we can release them, allocate, then
+ * re-acquire them and check if another thread has created the vtable in the meantime.
+ */
+ /* Special case System.MonoType to avoid infinite recursion */
+ if (class != mono_defaults.monotype_class) {
+ /*FIXME check for OOM*/
+ vt->type = mono_type_get_object (domain, &class->byval_arg);
+ if (mono_object_get_class (vt->type) != mono_defaults.monotype_class)
+ /* This is unregistered in
+ unregister_vtable_reflection_type() in
+ domain.c. */
+ MONO_GC_REGISTER_ROOT_IF_MOVING(vt->type);
+ }
+
+ if (class->contextbound)
+ vt->remote = 1;
+ else
+ vt->remote = 0;
+
/* class_vtable_array keeps an array of created vtables
*/
g_ptr_array_add (domain->class_vtable_array, vt);
* it it enlarged and when it is stored info.
*/
+ /*
+ * Store the vtable in class->runtime_info.
+ * class->runtime_info is accessed without locking, so this do this last after the vtable has been constructed.
+ */
+ mono_memory_barrier ();
+
old_info = class->runtime_info;
if (old_info && old_info->max_domain >= domain->domain_id) {
/* someone already created a large enough runtime info */
- mono_memory_barrier ();
old_info->domain_vtables [domain->domain_id] = vt;
} else {
int new_size = domain->domain_id;
class->runtime_info = runtime_info;
}
- /* Initialize vtable */
- if (callbacks.get_vtable_trampoline) {
- // This also covers the AOT case
- for (i = 0; i < class->vtable_size; ++i) {
- vt->vtable [i] = callbacks.get_vtable_trampoline (i);
- }
- } else {
- mono_class_setup_vtable (class);
-
- for (i = 0; i < class->vtable_size; ++i) {
- MonoMethod *cm;
-
- if ((cm = class->vtable [i]))
- vt->vtable [i] = arch_create_jit_trampoline (cm);
- }
- }
-
- if (ARCH_USE_IMT && imt_table_bytes) {
- /* Now that the vtable is full, we can actually fill up the IMT */
- if (callbacks.get_imt_trampoline) {
- /* lazy construction of the IMT entries enabled */
- for (i = 0; i < MONO_IMT_SIZE; ++i)
- interface_offsets [i] = callbacks.get_imt_trampoline (i);
- } else {
- build_imt (class, vt, domain, interface_offsets, NULL);
- }
+ if (class == mono_defaults.monotype_class) {
+ /*FIXME check for OOM*/
+ vt->type = mono_type_get_object (domain, &class->byval_arg);
+ if (mono_object_get_class (vt->type) != mono_defaults.monotype_class)
+ /* This is unregistered in
+ unregister_vtable_reflection_type() in
+ domain.c. */
+ MONO_GC_REGISTER_ROOT_IF_MOVING(vt->type);
}
mono_domain_unlock (domain);
if (class->parent)
mono_class_vtable_full (domain, class->parent, raise_on_error);
- /*FIXME check for OOM*/
- vt->type = mono_type_get_object (domain, &class->byval_arg);
- if (mono_object_get_class (vt->type) != mono_defaults.monotype_class)
- /* This is unregistered in
- unregister_vtable_reflection_type() in
- domain.c. */
- MONO_GC_REGISTER_ROOT_IF_MOVING(vt->type);
- if (class->contextbound)
- vt->remote = 1;
- else
- vt->remote = 0;
-
return vt;
}
return FALSE;
}
+/**
+ * mono_class_field_get_special_static_type:
+ * @field: The MonoClassField describing the field.
+ *
+ * Returns: SPECIAL_STATIC_THREAD if the field is thread static, SPECIAL_STATIC_CONTEXT if it is context static,
+ * SPECIAL_STATIC_NONE otherwise.
+ */
+guint32
+mono_class_field_get_special_static_type (MonoClassField *field)
+{
+ if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
+ return SPECIAL_STATIC_NONE;
+ if (mono_field_is_deleted (field))
+ return SPECIAL_STATIC_NONE;
+ if (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL))
+ return field_is_special_static (field->parent, field);
+ return SPECIAL_STATIC_NONE;
+}
+
/**
* mono_class_has_special_static_fields:
*
MonoClass *class = mono_class_from_mono_type (type);
int size = mono_class_value_size (class, NULL);
if (value == NULL)
- memset (dest, 0, size);
+ mono_gc_bzero (dest, size);
else
mono_gc_wbarrier_value_copy (dest, value, 1, class);
}
t = type->data.generic_class->container_class->byval_arg.type;
goto handle_enum;
default:
- g_warning ("got type %x", type->type);
- g_assert_not_reached ();
+ g_error ("got type %x", type->type);
}
}
if (field->offset == -1) {
/* Special static */
- gpointer addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
+ gpointer addr;
+
+ mono_domain_lock (vt->domain);
+ addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
+ mono_domain_unlock (vt->domain);
dest = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
} else {
- dest = (char*)vt->data + field->offset;
+ dest = (char*)mono_vtable_get_static_field_data (vt) + field->offset;
}
set_value (field->type, dest, value, FALSE);
}
-/* Used by the debugger */
+/**
+ * mono_vtable_get_static_field_data:
+ *
+ * Internal use function: return a pointer to the memory holding the static fields
+ * for a class or NULL if there are no static fields.
+ * This is exported only for use by the debugger.
+ */
void *
mono_vtable_get_static_field_data (MonoVTable *vt)
{
- return vt->data;
+ if (!vt->has_static_fields)
+ return NULL;
+ return vt->vtable [vt->klass->vtable_size];
}
static guint8*
if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
if (field->offset == -1) {
/* Special static */
- gpointer addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
+ gpointer addr;
+
+ mono_domain_lock (vt->domain);
+ addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
+ mono_domain_unlock (vt->domain);
src = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
} else {
- src = (guint8*)vt->data + field->offset;
+ src = (guint8*)mono_vtable_get_static_field_data (vt) + field->offset;
}
} else {
src = (guint8*)obj + field->offset;
mono_get_constant_value_from_blob (domain, def_type, data, value);
}
+void
+mono_field_static_get_value_for_thread (MonoInternalThread *thread, MonoVTable *vt, MonoClassField *field, void *value)
+{
+ void *src;
+
+ g_return_if_fail (field->type->attrs & FIELD_ATTRIBUTE_STATIC);
+
+ if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL) {
+ get_default_field_value (vt->domain, field, value);
+ return;
+ }
+
+ if (field->offset == -1) {
+ /* Special static */
+ gpointer addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
+ src = mono_get_special_static_data_for_thread (thread, GPOINTER_TO_UINT (addr));
+ } else {
+ src = (char*)mono_vtable_get_static_field_data (vt) + field->offset;
+ }
+ set_value (field->type, value, src, TRUE);
+}
+
/**
* mono_field_static_get_value:
* @vt: vtable to the object
void
mono_field_static_get_value (MonoVTable *vt, MonoClassField *field, void *value)
{
- void *src;
-
- g_return_if_fail (field->type->attrs & FIELD_ATTRIBUTE_STATIC);
-
- if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL) {
- get_default_field_value (vt->domain, field, value);
- return;
- }
-
- if (field->offset == -1) {
- /* Special static */
- gpointer addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
- src = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
- } else {
- src = (char*)vt->data + field->offset;
- }
- set_value (field->type, value, src, TRUE);
+ return mono_field_static_get_value_for_thread (mono_thread_internal_current (), vt, field, value);
}
/**
mono_nullable_init (guint8 *buf, MonoObject *value, MonoClass *klass)
{
MonoClass *param_class = klass->cast_class;
+
+ mono_class_setup_fields_locking (klass);
+ g_assert (klass->fields_inited);
g_assert (mono_class_from_mono_type (klass->fields [0].type) == param_class);
g_assert (mono_class_from_mono_type (klass->fields [1].type) == mono_defaults.boolean_class);
if (param_class->has_references)
mono_gc_wbarrier_value_copy (buf + klass->fields [0].offset - sizeof (MonoObject), mono_object_unbox (value), 1, param_class);
else
- memcpy (buf + klass->fields [0].offset - sizeof (MonoObject), mono_object_unbox (value), mono_class_value_size (param_class, NULL));
+ mono_gc_memmove (buf + klass->fields [0].offset - sizeof (MonoObject), mono_object_unbox (value), mono_class_value_size (param_class, NULL));
} else {
- memset (buf + klass->fields [0].offset - sizeof (MonoObject), 0, mono_class_value_size (param_class, NULL));
+ mono_gc_bzero (buf + klass->fields [0].offset - sizeof (MonoObject), mono_class_value_size (param_class, NULL));
}
}
{
MonoClass *param_class = klass->cast_class;
+ mono_class_setup_fields_locking (klass);
+ g_assert (klass->fields_inited);
+
g_assert (mono_class_from_mono_type (klass->fields [0].type) == param_class);
g_assert (mono_class_from_mono_type (klass->fields [1].type) == mono_defaults.boolean_class);
if (param_class->has_references)
mono_gc_wbarrier_value_copy (mono_object_unbox (o), buf + klass->fields [0].offset - sizeof (MonoObject), 1, param_class);
else
- memcpy (mono_object_unbox (o), buf + klass->fields [0].offset - sizeof (MonoObject), mono_class_value_size (param_class, NULL));
+ mono_gc_memmove (mono_object_unbox (o), buf + klass->fields [0].offset - sizeof (MonoObject), mono_class_value_size (param_class, NULL));
return o;
}
else
mono_runtime_delegate_invoke (MonoObject *delegate, void **params, MonoObject **exc)
{
MonoMethod *im;
+ MonoClass *klass = delegate->vtable->klass;
- im = mono_get_delegate_invoke (delegate->vtable->klass);
- g_assert (im);
+ im = mono_get_delegate_invoke (klass);
+ if (!im)
+ g_error ("Could not lookup delegate invoke method for delegate %s", mono_type_get_full_name (klass));
return mono_runtime_invoke (im, delegate, params, exc);
}
}
/* set exitcode only if we will abort the process */
- if (abort_process)
- mono_environment_exitcode_set (1);
if ((current_appdomain_delegate == NULL) && (root_appdomain_delegate == NULL)) {
+ if (abort_process)
+ mono_environment_exitcode_set (1);
mono_print_unhandled_exception (exc);
} else {
if (root_appdomain_delegate) {
* mono_object_new_pinned:
*
* Same as mono_object_new, but the returned object will be pinned.
- * FIXME: How are these freed under sgen ?
+ * For SGEN, these objects will only be freed at appdomain unload.
*/
MonoObject *
mono_object_new_pinned (MonoDomain *domain, MonoClass *klass)
if (!(mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
profile_allocs = FALSE;
- if (vtable->klass->has_finalize || vtable->klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
+ if (mono_class_has_finalizer (vtable->klass) || vtable->klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
return mono_object_new_specific;
if (!vtable->klass->has_references) {
MonoObject *o;
int size = obj->vtable->klass->instance_size;
+ if (obj->vtable->klass->rank)
+ return (MonoObject*)mono_array_clone ((MonoArray*)obj);
+
o = mono_object_allocate (size, obj->vtable);
if (obj->vtable->klass->has_references) {
} else {
int size = obj->vtable->klass->instance_size;
/* do not copy the sync state */
- memcpy ((char*)o + sizeof (MonoObject), (char*)obj + sizeof (MonoObject), size - sizeof (MonoObject));
+ mono_gc_memmove ((char*)o + sizeof (MonoObject), (char*)obj + sizeof (MonoObject), size - sizeof (MonoObject));
}
if (G_UNLIKELY (profile_allocs))
mono_profiler_allocation (o, obj->vtable->klass);
if (klass->element_class->has_references)
mono_value_copy_array (dest, 0, mono_array_addr_with_size (src, 0, 0), mono_array_length (src));
else
- memcpy (&dest->vector, &src->vector, size);
+ mono_gc_memmove (&dest->vector, &src->vector, size);
} else {
mono_array_memcpy_refs (dest, 0, src, 0, mono_array_length (src));
}
#else
- memcpy (&dest->vector, &src->vector, size);
+ mono_gc_memmove (&dest->vector, &src->vector, size);
#endif
}
if (klass->element_class->has_references)
mono_value_copy_array (o, 0, mono_array_addr_with_size (array, 0, 0), mono_array_length (array));
else
- memcpy (&o->vector, &array->vector, size);
+ mono_gc_memmove (&o->vector, &array->vector, size);
} else {
mono_array_memcpy_refs (o, 0, array, 0, mono_array_length (array));
}
#else
- memcpy (&o->vector, &array->vector, size);
+ mono_gc_memmove (&o->vector, &array->vector, size);
#endif
return o;
}
if (klass->element_class->has_references)
mono_value_copy_array (o, 0, mono_array_addr_with_size (array, 0, 0), mono_array_length (array));
else
- memcpy (&o->vector, &array->vector, size);
+ mono_gc_memmove (&o->vector, &array->vector, size);
} else {
mono_array_memcpy_refs (o, 0, array, 0, mono_array_length (array));
}
#else
- memcpy (&o->vector, &array->vector, size);
+ mono_gc_memmove (&o->vector, &array->vector, size);
#endif
return o;
guint16 *ut;
glong items_written;
- ut = g_utf8_to_utf16 (text, length, NULL, &items_written, &error);
+ ut = eg_utf8_to_utf16_with_nuls (text, length, NULL, &items_written, &error);
if (!error)
o = mono_string_new_utf16 (domain, ut, items_written);
mono_gc_wbarrier_value_copy ((char *)res + sizeof (MonoObject), value, 1, class);
#else
#if NO_UNALIGNED_ACCESS
- memcpy ((char *)res + sizeof (MonoObject), value, size);
+ mono_gc_memmove ((char *)res + sizeof (MonoObject), value, size);
#else
switch (size) {
case 1:
*(guint64 *)((guint8 *) res + sizeof (MonoObject)) = *(guint64 *) value;
break;
default:
- memcpy ((char *)res + sizeof (MonoObject), value, size);
+ mono_gc_memmove ((char *)res + sizeof (MonoObject), value, size);
}
#endif
#endif
return obj;
} else {
MonoClass *oklass = vt->klass;
- if ((oklass == mono_defaults.transparent_proxy_class))
+ if (oklass == mono_defaults.transparent_proxy_class)
oklass = ((MonoTransparentProxy *)obj)->remote_class->proxy_class;
-
+
+ mono_class_setup_supertypes (klass);
if ((oklass->idepth >= klass->idepth) && (oklass->supertypes [klass->idepth - 1] == klass))
return obj;
}
return as;
}
+/**
+ * mono_string_to_utf8_ignore:
+ * @s: a MonoString
+ *
+ * Converts a MonoString to its UTF8 representation. Will ignore
+ * invalid surrogate pairs.
+ * The resulting buffer should be freed with mono_free().
+ *
+ */
+char *
+mono_string_to_utf8_ignore (MonoString *s)
+{
+ long written = 0;
+ char *as;
+
+ if (s == NULL)
+ return NULL;
+
+ if (!s->length)
+ return g_strdup ("");
+
+ as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, &written, NULL);
+
+ /* g_utf16_to_utf8 may not be able to complete the convertion (e.g. NULL values were found, #335488) */
+ if (s->length > written) {
+ /* allocate the total length and copy the part of the string that has been converted */
+ char *as2 = g_malloc0 (s->length);
+ memcpy (as2, as, written);
+ g_free (as);
+ as = as2;
+ }
+
+ return as;
+}
+
+/**
+ * mono_string_to_utf8_image_ignore:
+ * @s: a System.String
+ *
+ * Same as mono_string_to_utf8_ignore, but allocate the string from the image mempool.
+ */
+char *
+mono_string_to_utf8_image_ignore (MonoImage *image, MonoString *s)
+{
+ return mono_string_to_utf8_internal (NULL, image, s, TRUE, NULL);
+}
+
+/**
+ * mono_string_to_utf8_mp_ignore:
+ * @s: a System.String
+ *
+ * Same as mono_string_to_utf8_ignore, but allocate the string from a mempool.
+ */
+char *
+mono_string_to_utf8_mp_ignore (MonoMemPool *mp, MonoString *s)
+{
+ return mono_string_to_utf8_internal (mp, NULL, s, TRUE, NULL);
+}
+
+
/**
* mono_string_to_utf16:
* @s: a MonoString
static char *
-mono_string_to_utf8_internal (MonoMemPool *mp, MonoImage *image, MonoString *s, MonoError *error)
+mono_string_to_utf8_internal (MonoMemPool *mp, MonoImage *image, MonoString *s, gboolean ignore_error, MonoError *error)
{
char *r;
char *mp_s;
int len;
- r = mono_string_to_utf8_checked (s, error);
- if (!mono_error_ok (error))
- return NULL;
+ if (ignore_error) {
+ r = mono_string_to_utf8_ignore (s);
+ } else {
+ r = mono_string_to_utf8_checked (s, error);
+ if (!mono_error_ok (error))
+ return NULL;
+ }
if (!mp && !image)
return r;
char *
mono_string_to_utf8_image (MonoImage *image, MonoString *s, MonoError *error)
{
- return mono_string_to_utf8_internal (NULL, image, s, error);
+ return mono_string_to_utf8_internal (NULL, image, s, FALSE, error);
}
/**
char *
mono_string_to_utf8_mp (MonoMemPool *mp, MonoString *s, MonoError *error)
{
- return mono_string_to_utf8_internal (mp, NULL, s, error);
+ return mono_string_to_utf8_internal (mp, NULL, s, FALSE, error);
}
-static void
-default_ex_handler (MonoException *ex)
-{
- MonoObject *o = (MonoObject*)ex;
- g_error ("Exception %s.%s raised in C code", o->vtable->klass->name_space, o->vtable->klass->name);
- exit (1);
-}
-static MonoExceptionFunc ex_handler = default_ex_handler;
+static MonoRuntimeExceptionHandlingCallbacks eh_callbacks;
-/**
- * mono_install_handler:
- * @func: exception handler
- *
- * This is an internal JIT routine used to install the handler for exceptions
- * being throwh.
- */
void
-mono_install_handler (MonoExceptionFunc func)
+mono_install_eh_callbacks (MonoRuntimeExceptionHandlingCallbacks *cbs)
+{
+ eh_callbacks = *cbs;
+}
+
+MonoRuntimeExceptionHandlingCallbacks *
+mono_get_eh_callbacks (void)
{
- ex_handler = func? func: default_ex_handler;
+ return &eh_callbacks;
}
/**
* that will cause gcc to omit the function epilog, causing problems when
* the JIT tries to walk the stack, since the return address on the stack
* will point into the next function in the executable, not this one.
- */
+ */
+ eh_callbacks.mono_raise_exception (ex);
+}
- if (((MonoObject*)ex)->vtable->klass == mono_defaults.threadabortexception_class) {
- MonoInternalThread *thread = mono_thread_internal_current ();
- g_assert (ex->object.vtable->domain == mono_domain_get ());
- MONO_OBJECT_SETREF (thread, abort_exc, ex);
- }
-
- ex_handler (ex);
+void
+mono_raise_exception_with_context (MonoException *ex, MonoContext *ctx)
+{
+ eh_callbacks.mono_raise_exception_with_ctx (ex, ctx);
}
/**
if (!object_array_klass) {
MonoClass *klass;
- klass = mono_array_class_get (mono_defaults.object_class, 1);
- g_assert (klass);
-
- mono_memory_barrier ();
- object_array_klass = klass;
-
klass = mono_array_class_get (mono_defaults.byte_class, 1);
g_assert (klass);
-
- mono_memory_barrier ();
byte_array_klass = klass;
klass = mono_array_class_get (mono_defaults.string_class, 1);
g_assert (klass);
-
- mono_memory_barrier ();
string_array_klass = klass;
+
+ klass = mono_array_class_get (mono_defaults.object_class, 1);
+ g_assert (klass);
+
+ mono_atomic_store_release (&object_array_klass, klass);
}
MONO_OBJECT_SETREF (this, method, method);
if (exc == (MonoObject*)mono_object_domain (exc)->out_of_memory_ex) {
message = g_strdup ("OutOfMemoryException");
+ free_message = TRUE;
} else {
- str = mono_object_to_string (exc, NULL);
- if (str) {
- message = mono_string_to_utf8_checked (str, &error);
- if (!mono_error_ok (&error)) {
- mono_error_cleanup (&error);
- message = (char *) "";
- } else {
- free_message = TRUE;
+
+ if (((MonoException*)exc)->native_trace_ips) {
+ message = mono_exception_get_native_backtrace ((MonoException*)exc);
+ free_message = TRUE;
+ } else {
+ str = mono_object_to_string (exc, NULL);
+ if (str) {
+ message = mono_string_to_utf8_checked (str, &error);
+ if (!mono_error_ok (&error)) {
+ mono_error_cleanup (&error);
+ message = (char *) "";
+ } else {
+ free_message = TRUE;
+ }
}
}
}
* g_printerr ("\nUnhandled Exception: %s.%s: %s\n", exc->vtable->klass->name_space,
* exc->vtable->klass->name, message);
*/
- g_printerr ("\nUnhandled Exception: %s\n", message);
+ g_printerr ("\nUnhandled Exception:\n%s\n", message);
if (free_message)
g_free (message);
MONO_OBJECT_SETREF (delegate, target, target);
}
- delegate->invoke_impl = arch_create_delegate_trampoline (delegate->object.vtable->klass);
+ delegate->invoke_impl = arch_create_delegate_trampoline (delegate->object.vtable->domain, delegate->object.vtable->klass);
}
/**
g_assert (addr);
- if ((ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (addr)))) {
+ ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (addr));
+ /* Shared code */
+ if (!ji && domain != mono_get_root_domain ())
+ ji = mono_jit_info_table_find (mono_get_root_domain (), mono_get_addr_from_ftnptr (addr));
+ if (ji) {
method = ji->method;
g_assert (!method->klass->generic_container);
}
if (class->has_references)
mono_gc_wbarrier_value_copy (*((gpointer *)params [i]), arg + sizeof (MonoObject), 1, class);
else
- memcpy (*((gpointer *)params [i]), arg + sizeof (MonoObject), size);
+ mono_gc_memmove (*((gpointer *)params [i]), arg + sizeof (MonoObject), size);
} else {
size = mono_class_value_size (mono_class_from_mono_type (pt), NULL);
- memset (*((gpointer *)params [i]), 0, size);
+ mono_gc_bzero (*((gpointer *)params [i]), size);
}
}