2 * object.c: Object creation for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2004 Ximian, Inc.
15 #include <mono/metadata/mono-endian.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/tokentype.h>
18 #include <mono/metadata/loader.h>
19 #include <mono/metadata/object.h>
20 #include <mono/metadata/gc-internal.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/appdomain.h>
23 #include <mono/metadata/assembly.h>
24 #include <mono/metadata/threadpool.h>
25 #include <mono/metadata/marshal.h>
26 #include "mono/metadata/debug-helpers.h"
27 #include "mono/metadata/marshal.h"
28 #include <mono/metadata/threads.h>
29 #include <mono/metadata/environment.h>
30 #include "mono/metadata/profiler-private.h"
31 #include <mono/os/gc_wrapper.h>
32 #include <mono/utils/strenc.h>
35 * Enable experimental typed allocation using the GC_gcj_malloc function.
37 #ifdef HAVE_GC_GCJ_MALLOC
38 #define CREATION_SPEEDUP 1
42 mono_runtime_object_init (MonoObject *this)
45 MonoMethod *method = NULL;
46 MonoClass *klass = this->vtable->klass;
48 for (i = 0; i < klass->method.count; ++i) {
49 if (!strcmp (".ctor", klass->methods [i]->name) &&
50 klass->methods [i]->signature->param_count == 0) {
51 method = klass->methods [i];
58 mono_runtime_invoke (method, this, NULL, NULL);
62 * mono_runtime_class_init:
63 * @vtable: vtable that needs to be initialized
65 * This routine calls the class constructor for @vtable.
68 mono_runtime_class_init (MonoVTable *vtable)
72 MonoException *exc_to_throw;
73 MonoMethod *method = NULL;
77 MonoDomain *last_domain = NULL;
81 if (vtable->initialized || vtable->initializing)
86 klass = vtable->klass;
88 for (i = 0; i < klass->method.count; ++i) {
89 method = klass->methods [i];
90 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
91 (strcmp (".cctor", method->name) == 0)) {
98 mono_domain_lock (vtable->domain);
100 if (vtable->initialized || vtable->initializing) {
101 mono_domain_unlock (vtable->domain);
104 vtable->initializing = 1;
105 if (mono_domain_get () != vtable->domain) {
106 /* Transfer into the target domain */
107 last_domain = mono_domain_get ();
108 if (!mono_domain_set (vtable->domain, FALSE)) {
109 vtable->initialized = 1;
110 vtable->initializing = 0;
111 mono_domain_unlock (vtable->domain);
112 mono_raise_exception (mono_get_exception_appdomain_unloaded ());
115 mono_runtime_invoke (method, NULL, NULL, (MonoObject **) &exc);
117 mono_domain_set (last_domain, TRUE);
118 vtable->initialized = 1;
119 vtable->initializing = 0;
120 /* FIXME: if the cctor fails, the type must be marked as unusable */
121 mono_domain_unlock (vtable->domain);
123 vtable->initialized = 1;
128 (klass->image == mono_defaults.corlib &&
129 !strcmp (klass->name_space, "System") &&
130 !strcmp (klass->name, "TypeInitializationException")))
131 return; /* No static constructor found or avoid infinite loop */
133 if (klass->name_space && *klass->name_space)
134 full_name = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
136 full_name = g_strdup (klass->name);
138 exc_to_throw = mono_get_exception_type_initialization (full_name, exc);
141 mono_raise_exception (exc_to_throw);
145 default_trampoline (MonoMethod *method)
151 default_remoting_trampoline (MonoMethod *method)
153 g_error ("remoting not installed");
157 static MonoTrampoline arch_create_jit_trampoline = default_trampoline;
158 static MonoTrampoline arch_create_remoting_trampoline = default_remoting_trampoline;
161 mono_install_trampoline (MonoTrampoline func)
163 arch_create_jit_trampoline = func? func: default_trampoline;
167 mono_install_remoting_trampoline (MonoTrampoline func)
169 arch_create_remoting_trampoline = func? func: default_remoting_trampoline;
172 static MonoCompileFunc default_mono_compile_method = NULL;
175 mono_install_compile_method (MonoCompileFunc func)
177 default_mono_compile_method = func;
181 mono_compile_method (MonoMethod *method)
183 if (!default_mono_compile_method) {
184 g_error ("compile method called on uninitialized runtime");
187 return default_mono_compile_method (method);
191 #if 0 && HAVE_BOEHM_GC
193 vtable_finalizer (void *obj, void *data) {
194 g_print ("%s finalized (%p)\n", (char*)data, obj);
200 #define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
203 * The vtables in the root appdomain are assumed to be reachable by other
204 * roots, and we don't use typed allocation in the other domains.
207 #define GC_HEADER_BITMAP (1 << (G_STRUCT_OFFSET (MonoObject,synchronisation) / sizeof(gpointer)))
210 mono_class_compute_gc_descriptor (MonoClass *class)
212 MonoClassField *field;
215 static gboolean gcj_inited = FALSE;
220 GC_init_gcj_malloc (5, NULL);
224 mono_class_init (class);
226 if (class->gc_descr_inited)
229 class->gc_descr_inited = TRUE;
230 class->gc_descr = GC_NO_DESCRIPTOR;
232 if (class == mono_defaults.string_class) {
233 bitmap = GC_HEADER_BITMAP;
234 class->gc_descr = (gpointer)GC_make_descriptor ((GC_bitmap)&bitmap, 2);
236 else if (class->rank) {
237 mono_class_compute_gc_descriptor (class->element_class);
239 if (class->element_class->valuetype && (class->element_class->gc_descr != GC_NO_DESCRIPTOR) && (class->element_class->gc_bitmap == GC_HEADER_BITMAP)) {
240 bitmap = GC_HEADER_BITMAP;
242 bitmap += 1 << (G_STRUCT_OFFSET (MonoArray,bounds) / sizeof(gpointer));
243 class->gc_descr = (gpointer)GC_make_descriptor ((GC_bitmap)&bitmap, 3);
247 static int count = 0;
251 /* GC 6.1 has trouble handling 64 bit descriptors... */
252 if ((class->instance_size / sizeof (gpointer)) > 30) {
253 // printf ("TOO LARGE: %s %d.\n", class->name, class->instance_size / sizeof (gpointer));
257 bitmap = GC_HEADER_BITMAP;
264 // printf("KLASS: %s.\n", class->name);
266 for (p = class; p != NULL; p = p->parent) {
267 for (i = 0; i < p->field.count; ++i) {
268 field = &p->fields [i];
269 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
271 if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
274 pos = field->offset / sizeof (gpointer);
276 if (field->type->byref)
279 switch (field->type->type) {
280 case MONO_TYPE_BOOLEAN:
292 // printf ("F: %s %s %d %lld %llx.\n", class->name, field->name, field->offset, ((guint64)1) << pos, bitmap);
295 case MONO_TYPE_STRING:
296 case MONO_TYPE_SZARRAY:
297 case MONO_TYPE_CLASS:
298 case MONO_TYPE_OBJECT:
299 case MONO_TYPE_ARRAY:
301 g_assert ((field->offset % sizeof(gpointer)) == 0);
303 bitmap |= ((guint64)1) << pos;
304 // printf ("F: %s %s %d %d %lld %llx.\n", class->name, field->name, field->offset, pos, ((guint64)(1)) << pos, bitmap);
306 case MONO_TYPE_VALUETYPE: {
307 MonoClass *fclass = field->type->data.klass;
308 if (!fclass->enumtype) {
309 mono_class_compute_gc_descriptor (fclass);
310 bitmap |= (fclass->gc_bitmap & ~2) << pos;
320 // printf("CLASS: %s.%s -> %d %llx.\n", class->name_space, class->name, class->instance_size / sizeof (gpointer), bitmap);
321 class->gc_bitmap = bitmap;
322 class->gc_descr = (gpointer)GC_make_descriptor ((GC_bitmap)&bitmap, class->instance_size / sizeof (gpointer));
325 #endif /* CREATION_SPEEDUP */
328 * field_is_special_static:
330 * Returns SPECIAL_STATIC_THREAD if the field is thread static, SPECIAL_STATIC_CONTEXT if it is context static,
331 * SPECIAL_STATIC_NONE otherwise.
334 field_is_special_static (MonoClass *fklass, MonoClassField *field)
336 MonoCustomAttrInfo *ainfo;
338 ainfo = mono_custom_attrs_from_field (fklass, field);
341 for (i = 0; i < ainfo->num_attrs; ++i) {
342 MonoClass *klass = ainfo->attrs [i].ctor->klass;
343 if (klass->image == mono_defaults.corlib) {
344 if (strcmp (klass->name, "ThreadStaticAttribute") == 0) {
345 mono_custom_attrs_free (ainfo);
346 return SPECIAL_STATIC_THREAD;
348 else if (strcmp (klass->name, "ContextStaticAttribute") == 0) {
349 mono_custom_attrs_free (ainfo);
350 return SPECIAL_STATIC_CONTEXT;
354 mono_custom_attrs_free (ainfo);
355 return SPECIAL_STATIC_NONE;
360 * @domain: the application domain
361 * @class: the class to initialize
363 * VTables are domain specific because we create domain specific code, and
364 * they contain the domain specific static class data.
367 mono_class_vtable (MonoDomain *domain, MonoClass *class)
369 MonoVTable *vt = NULL;
370 MonoClassField *field;
376 guint32 constant_cols [MONO_CONSTANT_SIZE];
380 vt = class->cached_vtable;
381 if (vt && vt->domain == domain)
384 mono_domain_lock (domain);
385 if ((vt = mono_g_hash_table_lookup (domain->class_vtable_hash, class))) {
386 mono_domain_unlock (domain);
391 mono_class_init (class);
393 mono_stats.used_class_count++;
394 mono_stats.class_vtable_size += sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer);
396 vtable_size = sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer);
398 vt = mono_mempool_alloc0 (domain->mp, vtable_size);
404 mono_class_compute_gc_descriptor (class);
405 if (domain != mono_root_domain)
407 * We can't use typed allocation in the non-root domains, since the
408 * collector needs the GC descriptor stored in the vtable even after
409 * the mempool containing the vtable is destroyed when the domain is
410 * unloaded. An alternative might be to allocate vtables in the GC
411 * heap, but this does not seem to work (it leads to crashes inside
412 * libgc). If that approach is tried, two gc descriptors need to be
413 * allocated for each class: one for the root domain, and one for all
414 * other domains. The second descriptor should contain a bit for the
415 * vtable field in MonoObject, since we can no longer assume the
416 * vtable is reachable by other roots after the appdomain is unloaded.
418 vt->gc_descr = GC_NO_DESCRIPTOR;
420 vt->gc_descr = class->gc_descr;
423 if (class->class_size) {
425 vt->data = GC_MALLOC (class->class_size + 8);
426 /*vt->data = GC_debug_malloc (class->class_size + 8, class->name, 2);*/
427 /*GC_register_finalizer (vt->data, vtable_finalizer, class->name, NULL, NULL);*/
428 mono_g_hash_table_insert (domain->static_data_hash, class, vt->data);
430 vt->data = mono_mempool_alloc0 (domain->mp, class->class_size + 8);
433 mono_stats.class_static_data_size += class->class_size + 8;
437 for (i = class->field.first; i < class->field.last; ++i) {
438 field = &class->fields [i - class->field.first];
439 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
441 if (mono_field_is_deleted (field))
443 if (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL)) {
444 gint32 special_static = field_is_special_static (class, field);
445 if (special_static != SPECIAL_STATIC_NONE) {
446 guint32 size, align, offset;
447 size = mono_type_size (field->type, &align);
448 offset = mono_alloc_special_static_data (special_static, size, align);
449 if (!domain->special_static_fields)
450 domain->special_static_fields = g_hash_table_new (NULL, NULL);
451 g_hash_table_insert (domain->special_static_fields, field, GUINT_TO_POINTER (offset));
455 if ((field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA)) {
456 MonoClass *fklass = mono_class_from_mono_type (field->type);
457 t = (char*)vt->data + field->offset;
458 if (fklass->valuetype) {
459 memcpy (t, field->data, mono_class_value_size (fklass, NULL));
461 /* it's a pointer type: add check */
462 g_assert (fklass->byval_arg.type == MONO_TYPE_PTR);
463 *t = *(char *)field->data;
467 if (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT))
470 if (!field->def_value) {
471 cindex = mono_metadata_get_constant_index (class->image, MONO_TOKEN_FIELD_DEF | (i + 1), cindex + 1);
474 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
475 field->def_value = g_new0 (MonoConstant, 1);
476 field->def_value->type = constant_cols [MONO_CONSTANT_TYPE];
477 field->def_value->value = (gpointer)mono_metadata_blob_heap (class->image, constant_cols [MONO_CONSTANT_VALUE]);
480 p = field->def_value->value;
481 len = mono_metadata_decode_blob_size (p, &p);
482 t = (char*)vt->data + field->offset;
483 /* should we check that the type matches? */
484 switch (field->def_value->type) {
485 case MONO_TYPE_BOOLEAN:
493 guint16 *val = (guint16*)t;
499 guint32 *val = (guint32*)t;
505 guint64 *val = (guint64*)t;
510 float *val = (float*)t;
515 double *val = (double*)t;
519 case MONO_TYPE_STRING: {
520 gpointer *val = (gpointer*)t;
521 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
522 gunichar2 *copy = g_malloc (len);
524 for (j = 0; j < len/2; j++) {
525 copy [j] = read16 (p);
528 *val = mono_string_new_utf16 (domain, copy, len/2);
531 *val = mono_string_new_utf16 (domain, (const guint16*)p, len/2);
535 case MONO_TYPE_CLASS:
536 /* nothing to do, we malloc0 the data and the value can be 0 only */
539 g_warning ("type 0x%02x should not be in constant table", field->def_value->type);
543 vt->max_interface_id = class->max_interface_id;
545 vt->interface_offsets = mono_mempool_alloc0 (domain->mp,
546 sizeof (gpointer) * (class->max_interface_id + 1));
548 /* initialize interface offsets */
549 for (i = 0; i <= class->max_interface_id; ++i) {
550 int slot = class->interface_offsets [i];
552 vt->interface_offsets [i] = &(vt->vtable [slot]);
556 * arch_create_jit_trampoline () can recursively call this function again
557 * because it compiles icall methods right away.
559 mono_g_hash_table_insert (domain->class_vtable_hash, class, vt);
560 if (!class->cached_vtable)
561 class->cached_vtable = vt;
563 /* initialize vtable */
564 for (i = 0; i < class->vtable_size; ++i) {
567 if ((cm = class->vtable [i]))
568 vt->vtable [i] = arch_create_jit_trampoline (cm);
571 mono_domain_unlock (domain);
573 /* make sure the the parent is initialized */
575 mono_class_vtable (domain, class->parent);
577 vt->type = mono_type_get_object (domain, &class->byval_arg);
578 if (class->contextbound)
587 * mono_class_proxy_vtable:
588 * @domain: the application domain
589 * @class: the class to proxy
591 * Creates a vtable for transparent proxies. It is basically
592 * a copy of the real vtable of @class, but all function pointers invoke
593 * the remoting functions, and vtable->klass points to the
594 * transparent proxy class, and not to @class.
597 mono_class_proxy_vtable (MonoDomain *domain, MonoClass *class)
599 MonoVTable *vt, *pvt;
600 int i, j, vtsize, interface_vtsize = 0;
601 MonoClass* iclass = NULL;
604 mono_domain_lock (domain);
605 pvt = mono_g_hash_table_lookup (domain->proxy_vtable_hash, class);
608 mono_domain_unlock (domain);
612 if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
615 class = mono_defaults.marshalbyrefobject_class;
617 method_count = iclass->method.count;
618 for (i = 0; i < iclass->interface_count; i++)
619 method_count += iclass->interfaces[i]->method.count;
621 interface_vtsize = method_count * sizeof (gpointer);
624 vt = mono_class_vtable (domain, class);
625 vtsize = sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer);
627 mono_stats.class_vtable_size += vtsize + interface_vtsize;
629 pvt = mono_mempool_alloc (domain->mp, vtsize + interface_vtsize);
630 memcpy (pvt, vt, vtsize);
632 pvt->klass = mono_defaults.transparent_proxy_class;
634 /* initialize vtable */
635 for (i = 0; i < class->vtable_size; ++i) {
638 if ((cm = class->vtable [i]))
639 pvt->vtable [i] = arch_create_remoting_trampoline (cm);
642 if (class->flags & TYPE_ATTRIBUTE_ABSTRACT)
644 /* create trampolines for abstract methods */
645 for (k = class; k; k = k->parent) {
646 for (i = 0; i < k->method.count; i++) {
647 int slot = k->methods [i]->slot;
648 if (!pvt->vtable [slot])
649 pvt->vtable [slot] = arch_create_remoting_trampoline (k->methods[i]);
659 pvt->max_interface_id = iclass->max_interface_id;
661 pvt->interface_offsets = mono_mempool_alloc0 (domain->mp,
662 sizeof (gpointer) * (pvt->max_interface_id + 1));
664 /* Create trampolines for the methods of the interfaces */
665 slot = class->vtable_size;
669 pvt->interface_offsets [interf->interface_id] = &pvt->vtable [slot];
671 for (j = 0; j < interf->method.count; ++j) {
672 MonoMethod *cm = interf->methods [j];
673 pvt->vtable [slot + j] = arch_create_remoting_trampoline (cm);
675 slot += interf->method.count;
676 if (++i < iclass->interface_count) interf = iclass->interfaces[i];
685 pvt->interface_offsets = mono_mempool_alloc0 (domain->mp,
686 sizeof (gpointer) * (pvt->max_interface_id + 1));
688 /* initialize interface offsets */
689 for (i = 0; i <= class->max_interface_id; ++i) {
690 int slot = class->interface_offsets [i];
692 pvt->interface_offsets [i] = &(pvt->vtable [slot]);
696 mono_g_hash_table_insert (domain->proxy_vtable_hash, class, pvt);
698 mono_domain_unlock (domain);
704 * Retrieve the MonoMethod that would to be called on obj if obj is passed as
705 * the instance of a callvirt of method.
708 mono_object_get_virtual_method (MonoObject *obj, MonoMethod *method) {
712 MonoMethod *res = NULL;
714 klass = mono_object_class (obj);
715 if (klass == mono_defaults.transparent_proxy_class) {
716 klass = ((MonoTransparentProxy *)obj)->klass;
722 if (!is_proxy && ((method->flags & METHOD_ATTRIBUTE_FINAL) || !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)))
725 vtable = klass->vtable;
727 /* check method->slot is a valid index: perform isinstance? */
728 if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
730 res = vtable [klass->interface_offsets [method->klass->interface_id] + method->slot];
732 res = vtable [method->slot];
736 if (!res) res = method; /* It may be an interface or abstract class method */
737 res = mono_marshal_get_remoting_invoke (res);
746 dummy_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
748 g_error ("runtime invoke called on uninitialized runtime");
752 static MonoInvokeFunc default_mono_runtime_invoke = dummy_mono_runtime_invoke;
755 mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
757 return default_mono_runtime_invoke (method, obj, params, exc);
761 set_value (MonoType *type, void *dest, void *value, int deref_pointer) {
764 gpointer *p = (gpointer*)dest;
771 case MONO_TYPE_BOOLEAN:
774 guint8 *p = (guint8*)dest;
775 *p = *(guint8*)value;
780 case MONO_TYPE_CHAR: {
781 guint16 *p = (guint16*)dest;
782 *p = *(guint16*)value;
785 #if SIZEOF_VOID_P == 4
791 gint32 *p = (gint32*)dest;
792 *p = *(gint32*)value;
795 #if SIZEOF_VOID_P == 8
801 gint64 *p = (gint64*)dest;
802 *p = *(gint64*)value;
806 float *p = (float*)dest;
811 double *p = (double*)dest;
812 *p = *(double*)value;
815 case MONO_TYPE_STRING:
816 case MONO_TYPE_SZARRAY:
817 case MONO_TYPE_CLASS:
818 case MONO_TYPE_OBJECT:
819 case MONO_TYPE_ARRAY:
820 case MONO_TYPE_PTR: {
821 gpointer *p = (gpointer*)dest;
822 *p = deref_pointer? *(gpointer*)value: value;
825 case MONO_TYPE_VALUETYPE:
826 if (type->data.klass->enumtype) {
827 t = type->data.klass->enum_basetype->type;
831 size = mono_class_value_size (type->data.klass, NULL);
832 memcpy (dest, value, size);
836 g_warning ("got type %x", type->type);
837 g_assert_not_reached ();
842 mono_field_set_value (MonoObject *obj, MonoClassField *field, void *value)
846 g_return_if_fail (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC));
848 dest = (char*)obj + field->offset;
849 set_value (field->type, dest, value, FALSE);
853 mono_field_static_set_value (MonoVTable *vt, MonoClassField *field, void *value)
857 g_return_if_fail (field->type->attrs & FIELD_ATTRIBUTE_STATIC);
859 dest = (char*)vt->data + field->offset;
860 set_value (field->type, dest, value, FALSE);
864 mono_field_get_value (MonoObject *obj, MonoClassField *field, void *value)
868 g_return_if_fail (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC));
870 src = (char*)obj + field->offset;
871 set_value (field->type, value, src, TRUE);
875 mono_field_get_value_object (MonoDomain *domain, MonoClassField *field, MonoObject *obj)
879 MonoVTable *vtable = NULL;
881 gboolean is_static = FALSE;
882 gboolean is_ref = FALSE;
884 switch (field->type->type) {
885 case MONO_TYPE_STRING:
886 case MONO_TYPE_OBJECT:
887 case MONO_TYPE_CLASS:
888 case MONO_TYPE_ARRAY:
889 case MONO_TYPE_SZARRAY:
894 case MONO_TYPE_BOOLEAN:
906 case MONO_TYPE_VALUETYPE:
907 is_ref = field->type->byref;
910 g_error ("type 0x%x not handled in "
911 "mono_field_get_value_object", field->type->type);
915 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
917 vtable = mono_class_vtable (domain, field->parent);
918 if (!vtable->initialized)
919 mono_runtime_class_init (vtable);
924 mono_field_static_get_value (vtable, field, &o);
926 mono_field_get_value (obj, field, &o);
931 /* boxed value type */
932 klass = mono_class_from_mono_type (field->type);
933 o = mono_object_new (domain, klass);
934 v = ((gchar *) o) + sizeof (MonoObject);
936 mono_field_static_get_value (vtable, field, v);
938 mono_field_get_value (obj, field, v);
946 mono_field_static_get_value (MonoVTable *vt, MonoClassField *field, void *value)
950 g_return_if_fail (field->type->attrs & FIELD_ATTRIBUTE_STATIC);
952 src = (char*)vt->data + field->offset;
953 set_value (field->type, value, src, TRUE);
957 mono_property_set_value (MonoProperty *prop, void *obj, void **params, MonoObject **exc)
959 default_mono_runtime_invoke (prop->set, obj, params, exc);
963 mono_property_get_value (MonoProperty *prop, void *obj, void **params, MonoObject **exc)
965 return default_mono_runtime_invoke (prop->get, obj, params, exc);
970 mono_get_delegate_invoke (MonoClass *klass)
977 for (i = 0; i < klass->method.count; ++i) {
978 if (klass->methods [i]->name[0] == 'I' &&
979 !strcmp ("Invoke", klass->methods [i]->name)) {
980 im = klass->methods [i];
990 mono_runtime_delegate_invoke (MonoObject *delegate, void **params, MonoObject **exc)
994 im = mono_get_delegate_invoke (delegate->vtable->klass);
997 return mono_runtime_invoke (im, delegate, params, exc);
1000 static MonoArray* main_args;
1003 mono_runtime_get_main_args (void)
1009 fire_process_exit_event (void)
1011 MonoClassField *field;
1012 MonoDomain *domain = mono_domain_get ();
1014 MonoObject *delegate, *exc;
1016 field = mono_class_get_field_from_name (mono_defaults.appdomain_class, "ProcessExit");
1019 if (domain != mono_root_domain)
1022 delegate = *(MonoObject **)(((char *)domain->domain) + field->offset);
1023 if (delegate == NULL)
1028 mono_runtime_delegate_invoke (delegate, pa, &exc);
1032 * Execute a standard Main() method (argc/argv contains the
1033 * executable name). This method also sets the command line argument value
1034 * needed by System.Environment.
1037 mono_runtime_run_main (MonoMethod *method, int argc, char* argv[],
1041 MonoArray *args = NULL;
1042 MonoDomain *domain = mono_domain_get ();
1043 gchar *utf8_fullpath;
1046 main_args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, argc);
1048 if (!g_path_is_absolute (argv [0])) {
1049 gchar *basename = g_path_get_basename (argv [0]);
1050 gchar *fullpath = g_build_filename (method->klass->image->assembly->basedir,
1054 utf8_fullpath = mono_utf8_from_external (fullpath);
1055 if(utf8_fullpath == NULL) {
1056 /* Printing the arg text will cause glib to
1057 * whinge about "Invalid UTF-8", but at least
1058 * its relevant, and shows the problem text
1061 g_print ("\nCannot determine the text encoding for the assembly location: %s\n", fullpath);
1062 g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
1069 utf8_fullpath = mono_utf8_from_external (argv[0]);
1070 if(utf8_fullpath == NULL) {
1071 g_print ("\nCannot determine the text encoding for the assembly location: %s\n", argv[0]);
1072 g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
1077 mono_array_set (main_args, gpointer, 0, mono_string_new (domain, utf8_fullpath));
1078 g_free (utf8_fullpath);
1080 for (i = 1; i < argc; ++i) {
1084 utf8_arg=mono_utf8_from_external (argv[i]);
1085 if(utf8_arg==NULL) {
1086 /* Ditto the comment about Invalid UTF-8 here */
1087 g_print ("\nCannot determine the text encoding for argument %d (%s).\n", i, argv[i]);
1088 g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
1092 arg = mono_string_new (domain, utf8_arg);
1093 mono_array_set (main_args, gpointer, i, arg);
1097 if (method->signature->param_count) {
1098 args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, argc);
1099 for (i = 0; i < argc; ++i) {
1100 /* The encodings should all work, given that
1101 * we've checked all these args for the
1104 MonoString *arg = mono_string_new (domain, mono_utf8_from_external (argv [i]));
1105 mono_array_set (args, gpointer, i, arg);
1108 args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, 0);
1111 mono_assembly_set_main (method->klass->image->assembly);
1113 result = mono_runtime_exec_main (method, args, exc);
1114 fire_process_exit_event ();
1118 /* Used in mono_unhandled_exception */
1120 create_unhandled_exception_eventargs (MonoObject *exc)
1124 MonoMethod *method = NULL;
1125 MonoBoolean is_terminating = TRUE;
1129 klass = mono_class_from_name (mono_defaults.corlib, "System", "UnhandledExceptionEventArgs");
1132 mono_class_init (klass);
1134 /* UnhandledExceptionEventArgs only has 1 public ctor with 2 args */
1135 for (i = 0; i < klass->method.count; ++i) {
1136 method = klass->methods [i];
1137 if (!strcmp (".ctor", method->name) &&
1138 method->signature->param_count == 2 &&
1139 method->flags & METHOD_ATTRIBUTE_PUBLIC)
1147 args [1] = &is_terminating;
1149 obj = mono_object_new (mono_domain_get (), klass);
1150 mono_runtime_invoke (method, obj, args, NULL);
1156 * We call this function when we detect an unhandled exception
1157 * in the default domain.
1158 * It invokes the * UnhandledException event in AppDomain or prints
1159 * a warning to the console
1162 mono_unhandled_exception (MonoObject *exc)
1164 MonoDomain *domain = mono_domain_get ();
1165 MonoClassField *field;
1166 MonoObject *delegate;
1168 field=mono_class_get_field_from_name(mono_defaults.appdomain_class,
1169 "UnhandledException");
1172 if (exc->vtable->klass != mono_defaults.threadabortexception_class) {
1173 delegate = *(MonoObject **)(((char *)domain->domain) + field->offset);
1175 /* set exitcode only in the main thread? */
1176 mono_environment_exitcode_set (1);
1177 if (domain != mono_root_domain || !delegate) {
1178 mono_print_unhandled_exception (exc);
1180 MonoObject *e = NULL;
1183 pa [0] = domain->domain;
1184 pa [1] = create_unhandled_exception_eventargs (exc);
1185 mono_runtime_delegate_invoke (delegate, pa, &e);
1188 gchar *msg = mono_string_to_utf8 (((MonoException *) e)->message);
1189 g_warning ("exception inside UnhandledException handler: %s\n", msg);
1197 * Launch a new thread to start all setup that requires managed code
1200 * main_func is called back from the thread with main_args as the
1201 * parameter. The callback function is expected to start Main()
1202 * eventually. This function then waits for all managed threads to
1206 mono_runtime_exec_managed_code (MonoDomain *domain,
1207 MonoMainThreadFunc main_func,
1210 mono_thread_create (domain, main_func, main_args);
1212 mono_thread_manage ();
1216 * Execute a standard Main() method (args doesn't contain the
1220 mono_runtime_exec_main (MonoMethod *method, MonoArray *args, MonoObject **exc)
1230 domain = mono_object_domain (args);
1231 if (!domain->entry_assembly) {
1232 domain->entry_assembly = method->klass->image->assembly;
1233 ves_icall_System_AppDomainSetup_InitAppDomainSetup (domain->setup);
1236 /* FIXME: check signature of method */
1237 if (method->signature->ret->type == MONO_TYPE_I4) {
1239 res = mono_runtime_invoke (method, NULL, pa, exc);
1241 rval = *(guint32 *)((char *)res + sizeof (MonoObject));
1245 mono_environment_exitcode_set (rval);
1247 mono_runtime_invoke (method, NULL, pa, exc);
1251 /* If the return type of Main is void, only
1252 * set the exitcode if an exception was thrown
1253 * (we don't want to blow away an
1254 * explicitly-set exit code)
1257 mono_environment_exitcode_set (rval);
1265 mono_install_runtime_invoke (MonoInvokeFunc func)
1267 default_mono_runtime_invoke = func ? func: dummy_mono_runtime_invoke;
1271 mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params,
1274 MonoMethodSignature *sig = method->signature;
1275 gpointer *pa = NULL;
1278 if (NULL != params) {
1279 pa = alloca (sizeof (gpointer) * mono_array_length (params));
1280 for (i = 0; i < mono_array_length (params); i++) {
1281 if (sig->params [i]->byref) {
1285 switch (sig->params [i]->type) {
1288 case MONO_TYPE_BOOLEAN:
1291 case MONO_TYPE_CHAR:
1300 case MONO_TYPE_VALUETYPE:
1301 pa [i] = (char *)(((gpointer *)params->vector)[i]) + sizeof (MonoObject);
1303 case MONO_TYPE_STRING:
1304 case MONO_TYPE_OBJECT:
1305 case MONO_TYPE_CLASS:
1306 case MONO_TYPE_ARRAY:
1307 case MONO_TYPE_SZARRAY:
1308 if (sig->params [i]->byref)
1309 pa [i] = &(((gpointer *)params->vector)[i]);
1311 pa [i] = (char *)(((gpointer *)params->vector)[i]);
1314 g_error ("type 0x%x not handled in ves_icall_InternalInvoke", sig->params [i]->type);
1319 if (!strcmp (method->name, ".ctor") && method->klass != mono_defaults.string_class) {
1321 obj = mono_object_new (mono_domain_get (), method->klass);
1322 if (mono_object_class(obj) == mono_defaults.transparent_proxy_class) {
1323 method = mono_marshal_get_remoting_invoke (method->klass->vtable [method->slot]);
1326 mono_runtime_invoke (method, obj, pa, exc);
1329 return mono_runtime_invoke (method, obj, pa, exc);
1333 out_of_memory (size_t size)
1336 * we could allocate at program startup some memory that we could release
1337 * back to the system at this point if we're really low on memory (ie, size is
1338 * lower than the memory we set apart)
1340 mono_raise_exception (mono_domain_get ()->out_of_memory_ex);
1344 * mono_object_allocate:
1345 * @size: number of bytes to allocate
1347 * This is a very simplistic routine until we have our GC-aware
1350 * Returns: an allocated object of size @size, or NULL on failure.
1352 static inline void *
1353 mono_object_allocate (size_t size)
1356 /* if this is changed to GC_debug_malloc(), we need to change also metadata/gc.c */
1357 void *o = GC_MALLOC (size);
1359 void *o = calloc (1, size);
1361 mono_stats.new_object_count++;
1364 out_of_memory (size);
1368 #if CREATION_SPEEDUP
1369 static inline void *
1370 mono_object_allocate_spec (size_t size, void *gcdescr)
1372 /* if this is changed to GC_debug_malloc(), we need to change also metadata/gc.c */
1373 void *o = GC_GCJ_MALLOC (size, gcdescr);
1374 mono_stats.new_object_count++;
1377 out_of_memory (size);
1385 * Frees the memory used by the object. Debugging purposes
1386 * only, as we will have our GC system.
1389 mono_object_free (MonoObject *o)
1392 g_error ("mono_object_free called with boehm gc.");
1394 MonoClass *c = o->vtable->klass;
1396 memset (o, 0, c->instance_size);
1403 * @klass: the class of the object that we want to create
1405 * Returns: A newly created object whose definition is
1406 * looked up using @klass
1409 mono_object_new (MonoDomain *domain, MonoClass *klass)
1411 MONO_ARCH_SAVE_REGS;
1412 return mono_object_new_specific (mono_class_vtable (domain, klass));
1416 * mono_object_new_specific:
1417 * @vtable: the vtable of the object that we want to create
1419 * Returns: A newly created object with class and domain specified
1423 mono_object_new_specific (MonoVTable *vtable)
1427 MONO_ARCH_SAVE_REGS;
1432 MonoMethod *im = vtable->domain->create_proxy_for_type_method;
1435 MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting.Activation", "ActivationServices");
1439 mono_class_init (klass);
1441 for (i = 0; i < klass->method.count; ++i) {
1442 if (!strcmp ("CreateProxyForType", klass->methods [i]->name) &&
1443 klass->methods [i]->signature->param_count == 1) {
1444 im = klass->methods [i];
1449 vtable->domain->create_proxy_for_type_method = im;
1452 pa [0] = mono_type_get_object (mono_domain_get (), &vtable->klass->byval_arg);
1454 o = mono_runtime_invoke (im, NULL, pa, NULL);
1455 if (o != NULL) return o;
1458 return mono_object_new_alloc_specific (vtable);
1462 mono_object_new_fast (MonoVTable *vtable)
1465 MONO_ARCH_SAVE_REGS;
1467 #if CREATION_SPEEDUP
1468 if (vtable->gc_descr != GC_NO_DESCRIPTOR) {
1469 o = mono_object_allocate_spec (vtable->klass->instance_size, vtable);
1471 // printf("OBJECT: %s.%s.\n", vtable->klass->name_space, vtable->klass->name);
1472 o = mono_object_allocate (vtable->klass->instance_size);
1476 o = mono_object_allocate (vtable->klass->instance_size);
1483 mono_object_new_alloc_specific (MonoVTable *vtable)
1487 #if CREATION_SPEEDUP
1488 if (vtable->gc_descr != GC_NO_DESCRIPTOR) {
1489 o = mono_object_allocate_spec (vtable->klass->instance_size, vtable);
1491 // printf("OBJECT: %s.%s.\n", vtable->klass->name_space, vtable->klass->name);
1492 o = mono_object_allocate (vtable->klass->instance_size);
1496 o = mono_object_allocate (vtable->klass->instance_size);
1499 if (vtable->klass->has_finalize)
1500 mono_object_register_finalizer (o);
1502 mono_profiler_allocation (o, vtable->klass);
1507 * mono_object_new_from_token:
1508 * @image: Context where the type_token is hosted
1509 * @token: a token of the type that we want to create
1511 * Returns: A newly created object whose definition is
1512 * looked up using @token in the @image image
1515 mono_object_new_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1519 class = mono_class_get (image, token);
1521 return mono_object_new (domain, class);
1526 * mono_object_clone:
1527 * @obj: the object to clone
1529 * Returns: A newly created object who is a shallow copy of @obj
1532 mono_object_clone (MonoObject *obj)
1537 size = obj->vtable->klass->instance_size;
1538 o = mono_object_allocate (size);
1539 mono_profiler_allocation (o, obj->vtable->klass);
1541 memcpy (o, obj, size);
1543 if (obj->vtable->klass->has_finalize)
1544 mono_object_register_finalizer (o);
1550 * @array: the array to clone
1552 * Returns: A newly created array who is a shallow copy of @array
1555 mono_array_clone (MonoArray *array)
1560 MonoClass *klass = array->obj.vtable->klass;
1562 MONO_ARCH_SAVE_REGS;
1564 if (array->bounds == NULL) {
1565 size = mono_array_length (array);
1566 o = mono_array_new_full (((MonoObject *)array)->vtable->domain,
1567 klass, &size, NULL);
1569 size *= mono_array_element_size (klass);
1570 memcpy (o, array, sizeof (MonoArray) + size);
1575 sizes = alloca (klass->rank * sizeof(guint32) * 2);
1576 size = mono_array_element_size (klass);
1577 for (i = 0; i < klass->rank; ++i) {
1578 sizes [i] = array->bounds [i].length;
1579 size *= array->bounds [i].length;
1580 sizes [i + klass->rank] = array->bounds [i].lower_bound;
1582 o = mono_array_new_full (((MonoObject *)array)->vtable->domain,
1583 klass, sizes, sizes + klass->rank);
1584 memcpy (o, array, sizeof(MonoArray) + size);
1589 /* helper macros to check for overflow when calculating the size of arrays */
1590 #define MYGUINT32_MAX 4294967295U
1591 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1592 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1593 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1594 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1595 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1598 * mono_array_new_full:
1599 * @domain: domain where the object is created
1600 * @array_class: array class
1601 * @lengths: lengths for each dimension in the array
1602 * @lower_bounds: lower bounds for each dimension in the array (may be NULL)
1604 * This routine creates a new array objects with the given dimensions,
1605 * lower bounds and type.
1608 mono_array_new_full (MonoDomain *domain, MonoClass *array_class,
1609 guint32 *lengths, guint32 *lower_bounds)
1611 guint32 byte_len, len;
1614 MonoArrayBounds *bounds;
1618 if (!array_class->inited)
1619 mono_class_init (array_class);
1621 byte_len = mono_array_element_size (array_class);
1624 if (array_class->rank == 1 &&
1625 (lower_bounds == NULL || lower_bounds [0] == 0)) {
1630 bounds = GC_MALLOC (sizeof (MonoArrayBounds) * array_class->rank);
1632 bounds = g_malloc0 (sizeof (MonoArrayBounds) * array_class->rank);
1634 for (i = 0; i < array_class->rank; ++i) {
1635 bounds [i].length = lengths [i];
1636 if (CHECK_MUL_OVERFLOW_UN (len, lengths [i]))
1637 out_of_memory (MYGUINT32_MAX);
1642 for (i = 0; i < array_class->rank; ++i)
1643 bounds [i].lower_bound = lower_bounds [i];
1646 if (CHECK_MUL_OVERFLOW_UN (byte_len, len))
1647 out_of_memory (MYGUINT32_MAX);
1649 if (CHECK_ADD_OVERFLOW_UN (byte_len, sizeof (MonoArray)))
1650 out_of_memory (MYGUINT32_MAX);
1651 byte_len += sizeof (MonoArray);
1653 * Following three lines almost taken from mono_object_new ():
1654 * they need to be kept in sync.
1656 vtable = mono_class_vtable (domain, array_class);
1657 #if CREATION_SPEEDUP
1658 if (vtable->gc_descr != GC_NO_DESCRIPTOR)
1659 o = mono_object_allocate_spec (byte_len, vtable);
1661 o = mono_object_allocate (byte_len);
1665 o = mono_object_allocate (byte_len);
1669 array = (MonoArray*)o;
1671 array->bounds = bounds;
1672 array->max_length = len;
1674 mono_profiler_allocation (o, array_class);
1681 * @domain: domain where the object is created
1682 * @eclass: element class
1683 * @n: number of array elements
1685 * This routine creates a new szarray with @n elements of type @eclass.
1688 mono_array_new (MonoDomain *domain, MonoClass *eclass, guint32 n)
1692 MONO_ARCH_SAVE_REGS;
1694 ac = mono_array_class_get (eclass, 1);
1695 g_assert (ac != NULL);
1697 return mono_array_new_specific (mono_class_vtable (domain, ac), n);
1701 * mono_array_new_specific:
1702 * @vtable: a vtable in the appropriate domain for an initialized class
1703 * @n: number of array elements
1705 * This routine is a fast alternative to mono_array_new() for code which
1706 * can be sure about the domain it operates in.
1709 mono_array_new_specific (MonoVTable *vtable, guint32 n)
1713 guint32 byte_len, elem_size;
1715 MONO_ARCH_SAVE_REGS;
1717 elem_size = mono_array_element_size (vtable->klass);
1718 if (CHECK_MUL_OVERFLOW_UN (n, elem_size))
1719 out_of_memory (MYGUINT32_MAX);
1720 byte_len = n * elem_size;
1721 if (CHECK_ADD_OVERFLOW_UN (byte_len, sizeof (MonoArray)))
1722 out_of_memory (MYGUINT32_MAX);
1723 byte_len += sizeof (MonoArray);
1724 #if CREATION_SPEEDUP
1725 if (vtable->gc_descr != GC_NO_DESCRIPTOR) {
1726 o = mono_object_allocate_spec (byte_len, vtable);
1728 // printf("ARRAY: %s.%s.\n", vtable->klass->name_space, vtable->klass->name);
1729 o = mono_object_allocate (byte_len);
1733 o = mono_object_allocate (byte_len);
1737 ao = (MonoArray *)o;
1740 mono_profiler_allocation (o, vtable->klass);
1746 * mono_string_new_utf16:
1747 * @text: a pointer to an utf16 string
1748 * @len: the length of the string
1750 * Returns: A newly created string object which contains @text.
1753 mono_string_new_utf16 (MonoDomain *domain, const guint16 *text, gint32 len)
1757 s = mono_string_new_size (domain, len);
1758 g_assert (s != NULL);
1760 memcpy (mono_string_chars (s), text, len * 2);
1766 * mono_string_new_size:
1767 * @text: a pointer to an utf16 string
1768 * @len: the length of the string
1770 * Returns: A newly created string object of @len
1773 mono_string_new_size (MonoDomain *domain, gint32 len)
1778 vtable = mono_class_vtable (domain, mono_defaults.string_class);
1780 #if CREATION_SPEEDUP
1781 if (vtable->gc_descr != GC_NO_DESCRIPTOR)
1782 s = mono_object_allocate_spec (sizeof (MonoString) + ((len + 1) * 2), vtable);
1784 s = (MonoString*)mono_object_allocate (sizeof (MonoString) + ((len + 1) * 2));
1785 s->object.vtable = vtable;
1788 s = (MonoString*)mono_object_allocate (sizeof (MonoString) + ((len + 1) * 2));
1789 s->object.vtable = vtable;
1793 mono_profiler_allocation ((MonoObject*)s, mono_defaults.string_class);
1799 * mono_string_new_len:
1800 * @text: a pointer to an utf8 string
1801 * @length: number of bytes in @text to consider
1803 * Returns: A newly created string object which contains @text.
1806 mono_string_new_len (MonoDomain *domain, const char *text, guint length)
1808 GError *error = NULL;
1809 MonoString *o = NULL;
1811 glong items_written;
1813 ut = g_utf8_to_utf16 (text, length, NULL, &items_written, &error);
1816 o = mono_string_new_utf16 (domain, ut, items_written);
1818 g_error_free (error);
1827 * @text: a pointer to an utf8 string
1829 * Returns: A newly created string object which contains @text.
1832 mono_string_new (MonoDomain *domain, const char *text)
1834 GError *error = NULL;
1835 MonoString *o = NULL;
1837 glong items_written;
1842 ut = g_utf8_to_utf16 (text, l, NULL, &items_written, &error);
1845 o = mono_string_new_utf16 (domain, ut, items_written);
1847 g_error_free (error);
1855 * mono_string_new_wrapper:
1856 * @text: pointer to utf8 characters.
1858 * Helper function to create a string object from @text in the current domain.
1861 mono_string_new_wrapper (const char *text)
1863 MonoDomain *domain = mono_domain_get ();
1865 MONO_ARCH_SAVE_REGS;
1868 return mono_string_new (domain, text);
1875 * @class: the class of the value
1876 * @value: a pointer to the unboxed data
1878 * Returns: A newly created object which contains @value.
1881 mono_value_box (MonoDomain *domain, MonoClass *class, gpointer value)
1887 g_assert (class->valuetype);
1889 vtable = mono_class_vtable (domain, class);
1890 size = mono_class_instance_size (class);
1891 res = mono_object_allocate (size);
1892 res->vtable = vtable;
1893 mono_profiler_allocation (res, class);
1895 size = size - sizeof (MonoObject);
1897 #if NO_UNALIGNED_ACCESS
1898 memcpy ((char *)res + sizeof (MonoObject), value, size);
1902 *((guint8 *) res + sizeof (MonoObject)) = *(guint8 *) value;
1905 *(guint16 *)((guint8 *) res + sizeof (MonoObject)) = *(guint16 *) value;
1908 *(guint32 *)((guint8 *) res + sizeof (MonoObject)) = *(guint32 *) value;
1911 *(guint64 *)((guint8 *) res + sizeof (MonoObject)) = *(guint64 *) value;
1914 memcpy ((char *)res + sizeof (MonoObject), value, size);
1917 if (class->has_finalize)
1918 mono_object_register_finalizer (res);
1923 mono_object_unbox (MonoObject *obj)
1925 /* add assert for valuetypes? */
1926 return ((char*)obj) + sizeof (MonoObject);
1930 * mono_object_isinst:
1932 * @klass: a pointer to a class
1934 * Returns: @obj if @obj is derived from @klass
1937 mono_object_isinst (MonoObject *obj, MonoClass *klass)
1942 if (klass->marshalbyref)
1943 return mono_object_isinst_mbyref (obj, klass);
1952 mono_class_init (klass);
1954 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
1955 if ((klass->interface_id <= vt->max_interface_id) &&
1956 (vt->interface_offsets [klass->interface_id] != 0))
1962 return mono_class_is_assignable_from (klass, oklass) ? obj : NULL;
1966 mono_object_isinst_mbyref (MonoObject *obj, MonoClass *klass)
1977 if (oklass != klass && oklass == mono_defaults.transparent_proxy_class) {
1978 oklass = ((MonoTransparentProxy *)obj)->klass;
1979 if ((oklass->idepth >= klass->idepth) && (oklass->supertypes [klass->idepth - 1] == klass))
1982 /* Check for IRemotingTypeInfo */
1983 if (((MonoTransparentProxy *)obj)->custom_type_info) {
1986 MonoObject *rp = (MonoObject *)((MonoTransparentProxy *)obj)->rp;
1987 MonoClass *rpklass = rp->vtable->klass;
1988 MonoMethod *im = NULL;
1992 for (i = 0; i < rpklass->method.count; ++i) {
1993 if (!strcmp ("CanCastTo", rpklass->methods [i]->name) &&
1994 rpklass->methods [i]->signature->param_count == 2) {
1995 im = rpklass->methods [i];
2002 pa [0] = mono_type_get_object (mono_domain_get (), &klass->byval_arg);
2005 res = mono_runtime_invoke (im, rp, pa, NULL);
2006 if (*(MonoBoolean *) mono_object_unbox(res))
2013 if ((oklass->idepth >= klass->idepth) && (oklass->supertypes [klass->idepth - 1] == klass))
2020 * mono_object_castclass_mbyref:
2022 * @klass: a pointer to a class
2024 * Returns: @obj if @obj is derived from @klass, throws an exception otherwise
2027 mono_object_castclass_mbyref (MonoObject *obj, MonoClass *klass)
2029 if (!obj) return NULL;
2030 if (mono_object_isinst_mbyref (obj, klass)) return obj;
2032 mono_raise_exception (mono_exception_from_name (mono_defaults.corlib,
2034 "InvalidCastException"));
2039 MonoDomain *orig_domain;
2045 str_lookup (MonoDomain *domain, gpointer user_data)
2047 LDStrInfo *info = user_data;
2048 if (info->res || domain == info->orig_domain)
2050 mono_domain_lock (domain);
2051 info->res = mono_g_hash_table_lookup (domain->ldstr_table, info->ins);
2052 mono_domain_unlock (domain);
2056 mono_string_is_interned_lookup (MonoString *str, int insert)
2058 MonoGHashTable *ldstr_table;
2061 char *ins = g_malloc (4 + str->length * 2);
2065 /* Encode the length */
2067 mono_metadata_encode_value (2 * str->length, p, &p);
2070 mono_metadata_encode_value (bloblen + 2 * str->length, p, &p);
2071 bloblen = (p - ins) + 2 * str->length;
2073 * ins is stored in the hash table as a key and needs to have the same
2074 * representation as in the metadata: we swap the character bytes on big
2077 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
2080 char *p2 = (char *)mono_string_chars (str);
2081 for (i = 0; i < str->length; ++i) {
2088 memcpy (p, mono_string_chars (str), str->length * 2);
2090 domain = ((MonoObject *)str)->vtable->domain;
2091 ldstr_table = domain->ldstr_table;
2092 mono_domain_lock (domain);
2093 if ((res = mono_g_hash_table_lookup (ldstr_table, ins))) {
2094 mono_domain_unlock (domain);
2099 mono_g_hash_table_insert (ldstr_table, ins, str);
2100 mono_domain_unlock (domain);
2103 LDStrInfo ldstr_info;
2104 ldstr_info.orig_domain = domain;
2105 ldstr_info.ins = ins;
2106 ldstr_info.res = NULL;
2108 mono_domain_foreach (str_lookup, &ldstr_info);
2109 if (ldstr_info.res) {
2111 * the string was already interned in some other domain:
2112 * intern it in the current one as well.
2114 mono_g_hash_table_insert (ldstr_table, ins, str);
2115 mono_domain_unlock (domain);
2119 mono_domain_unlock (domain);
2125 mono_string_is_interned (MonoString *o)
2127 return mono_string_is_interned_lookup (o, FALSE);
2131 mono_string_intern (MonoString *str)
2133 return mono_string_is_interned_lookup (str, TRUE);
2138 * @domain: the domain where the string will be used.
2139 * @image: a metadata context
2140 * @idx: index into the user string table.
2142 * Implementation for the ldstr opcode.
2145 mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 idx)
2147 const char *str, *sig;
2151 MONO_ARCH_SAVE_REGS;
2154 return mono_lookup_dynamic_token (image, MONO_TOKEN_STRING | idx);
2156 sig = str = mono_metadata_user_string (image, idx);
2158 mono_domain_lock (domain);
2159 if ((o = mono_g_hash_table_lookup (domain->ldstr_table, sig))) {
2160 mono_domain_unlock (domain);
2164 len2 = mono_metadata_decode_blob_size (str, &str);
2167 o = mono_string_new_utf16 (domain, (guint16*)str, len2);
2168 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
2171 guint16 *p2 = (guint16*)mono_string_chars (o);
2172 for (i = 0; i < len2; ++i) {
2173 *p2 = GUINT16_FROM_LE (*p2);
2178 mono_g_hash_table_insert (domain->ldstr_table, (gpointer)sig, o);
2179 mono_domain_unlock (domain);
2185 * mono_string_to_utf8:
2186 * @s: a System.String
2188 * Return the UTF8 representation for @s.
2189 * the resulting buffer nedds to be freed with g_free().
2192 mono_string_to_utf8 (MonoString *s)
2195 GError *error = NULL;
2201 return g_strdup ("");
2203 as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, NULL, &error);
2205 g_warning (error->message);
2206 g_error_free (error);
2213 * mono_string_to_utf16:
2216 * Return an null-terminated array of the utf-16 chars
2217 * contained in @s. The result must be freed with g_free().
2218 * This is a temporary helper until our string implementation
2219 * is reworked to always include the null terminating char.
2222 mono_string_to_utf16 (MonoString *s)
2229 as = g_malloc ((s->length * 2) + 2);
2230 as [(s->length * 2)] = '\0';
2231 as [(s->length * 2) + 1] = '\0';
2234 return (gunichar2 *)(as);
2237 memcpy (as, mono_string_chars(s), s->length * 2);
2238 return (gunichar2 *)(as);
2242 * Converts a NULL terminated UTF16 string (LPWSTR) to a MonoString
2245 mono_string_from_utf16 (gunichar2 *data)
2247 MonoDomain *domain = mono_domain_get ();
2253 while (data [len]) len++;
2255 return mono_string_new_utf16 (domain, data, len);
2259 default_ex_handler (MonoException *ex)
2261 MonoObject *o = (MonoObject*)ex;
2262 g_error ("Exception %s.%s raised in C code", o->vtable->klass->name_space, o->vtable->klass->name);
2266 static MonoExceptionFunc ex_handler = default_ex_handler;
2269 mono_install_handler (MonoExceptionFunc func)
2271 ex_handler = func? func: default_ex_handler;
2275 * mono_raise_exception:
2276 * @ex: exception object
2278 * Signal the runtime that the exception @ex has been raised in unmanaged code.
2281 mono_raise_exception (MonoException *ex)
2284 * NOTE: Do NOT annotate this function with G_GNUC_NORETURN, since
2285 * that will cause gcc to omit the function epilog, causing problems when
2286 * the JIT tries to walk the stack, since the return address on the stack
2287 * will point into the next function in the executable, not this one.
2294 mono_wait_handle_new (MonoDomain *domain, HANDLE handle)
2296 MonoWaitHandle *res;
2298 res = (MonoWaitHandle *)mono_object_new (domain, mono_defaults.waithandle_class);
2300 res->handle = handle;
2306 mono_async_result_new (MonoDomain *domain, HANDLE handle, MonoObject *state, gpointer data)
2308 MonoAsyncResult *res;
2310 res = (MonoAsyncResult *)mono_object_new (domain, mono_defaults.asyncresult_class);
2313 res->async_state = state;
2314 res->handle = (MonoObject *)mono_wait_handle_new (domain, handle);
2315 res->sync_completed = FALSE;
2316 res->completed = FALSE;
2322 mono_message_init (MonoDomain *domain,
2323 MonoMethodMessage *this,
2324 MonoReflectionMethod *method,
2325 MonoArray *out_args)
2327 MonoMethodSignature *sig = method->method->signature;
2333 this->method = method;
2335 this->args = mono_array_new (domain, mono_defaults.object_class, sig->param_count);
2336 this->arg_types = mono_array_new (domain, mono_defaults.byte_class, sig->param_count);
2337 this->async_result = NULL;
2338 this->call_type = CallType_Sync;
2340 names = g_new (char *, sig->param_count);
2341 mono_method_get_param_names (method->method, (const char **) names);
2342 this->names = mono_array_new (domain, mono_defaults.string_class, sig->param_count);
2344 for (i = 0; i < sig->param_count; i++) {
2345 name = mono_string_new (domain, names [i]);
2346 mono_array_set (this->names, gpointer, i, name);
2350 for (i = 0, j = 0; i < sig->param_count; i++) {
2352 if (sig->params [i]->byref) {
2354 gpointer arg = mono_array_get (out_args, gpointer, j);
2355 mono_array_set (this->args, gpointer, i, arg);
2359 if (!(sig->params [i]->attrs & PARAM_ATTRIBUTE_OUT))
2364 mono_array_set (this->arg_types, guint8, i, arg_type);
2369 * mono_remoting_invoke:
2370 * @real_proxy: pointer to a RealProxy object
2371 * @msg: The MonoMethodMessage to execute
2372 * @exc: used to store exceptions
2373 * @out_args: used to store output arguments
2375 * This is used to call RealProxy::Invoke(). RealProxy::Invoke() returns an
2376 * IMessage interface and it is not trivial to extract results from there. So
2377 * we call an helper method PrivateInvoke instead of calling
2378 * RealProxy::Invoke() directly.
2380 * Returns: the result object.
2383 mono_remoting_invoke (MonoObject *real_proxy, MonoMethodMessage *msg,
2384 MonoObject **exc, MonoArray **out_args)
2386 MonoMethod *im = real_proxy->vtable->domain->private_invoke_method;
2389 /*static MonoObject *(*invoke) (gpointer, gpointer, MonoObject **, MonoArray **) = NULL;*/
2395 klass = mono_defaults.real_proxy_class;
2397 for (i = 0; i < klass->method.count; ++i) {
2398 if (!strcmp ("PrivateInvoke", klass->methods [i]->name) &&
2399 klass->methods [i]->signature->param_count == 4) {
2400 im = klass->methods [i];
2406 real_proxy->vtable->domain->private_invoke_method = im;
2409 pa [0] = real_proxy;
2414 return mono_runtime_invoke (im, NULL, pa, exc);
2418 mono_message_invoke (MonoObject *target, MonoMethodMessage *msg,
2419 MonoObject **exc, MonoArray **out_args)
2423 MonoMethodSignature *sig;
2424 int i, j, outarg_count = 0;
2426 if (target && target->vtable->klass == mono_defaults.transparent_proxy_class) {
2428 MonoTransparentProxy* tp = (MonoTransparentProxy *)target;
2429 if (tp->klass->contextbound && tp->rp->context == (MonoObject *) mono_context_get ()) {
2430 target = tp->rp->unwrapped_server;
2432 return mono_remoting_invoke ((MonoObject *)tp->rp, msg, exc, out_args);
2436 domain = mono_domain_get ();
2437 method = msg->method->method;
2438 sig = method->signature;
2440 for (i = 0; i < sig->param_count; i++) {
2441 if (sig->params [i]->byref)
2445 *out_args = mono_array_new (domain, mono_defaults.object_class, outarg_count);
2448 for (i = 0, j = 0; i < sig->param_count; i++) {
2449 if (sig->params [i]->byref) {
2451 arg = mono_array_get (msg->args, gpointer, i);
2452 mono_array_set (*out_args, gpointer, j, arg);
2457 return mono_runtime_invoke_array (method, target, msg->args, exc);
2461 mono_print_unhandled_exception (MonoObject *exc)
2463 char *message = (char *) "";
2467 gboolean free_message = FALSE;
2470 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
2471 klass = exc->vtable->klass;
2473 while (klass && method == NULL) {
2474 for (i = 0; i < klass->method.count; ++i) {
2475 method = klass->methods [i];
2476 if (!strcmp ("ToString", method->name) &&
2477 method->signature->param_count == 0 &&
2478 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
2479 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
2486 klass = klass->parent;
2491 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
2493 message = mono_string_to_utf8 (str);
2494 free_message = TRUE;
2499 * g_printerr ("\nUnhandled Exception: %s.%s: %s\n", exc->vtable->klass->name_space,
2500 * exc->vtable->klass->name, message);
2502 g_printerr ("\nUnhandled Exception: %s\n", message);
2509 * mono_delegate_ctor:
2510 * @this: pointer to an uninitialized delegate object
2511 * @target: target object
2512 * @addr: pointer to native code
2514 * This is used to initialize a delegate. We also insert the method_info if
2515 * we find the info with mono_jit_info_table_find().
2518 mono_delegate_ctor (MonoObject *this, MonoObject *target, gpointer addr)
2520 MonoDomain *domain = mono_domain_get ();
2521 MonoDelegate *delegate = (MonoDelegate *)this;
2522 MonoMethod *method = NULL;
2529 class = this->vtable->klass;
2531 if ((ji = mono_jit_info_table_find (domain, addr))) {
2532 method = ji->method;
2533 delegate->method_info = mono_method_get_object (domain, method, NULL);
2536 if (target && target->vtable->klass == mono_defaults.transparent_proxy_class) {
2538 method = mono_marshal_get_remoting_invoke (method);
2539 delegate->method_ptr = mono_compile_method (method);
2540 delegate->target = target;
2542 delegate->method_ptr = addr;
2543 delegate->target = target;
2548 * mono_method_call_message_new:
2550 * Translates arguments pointers into a Message.
2553 mono_method_call_message_new (MonoMethod *method, gpointer *params, MonoMethod *invoke,
2554 MonoDelegate **cb, MonoObject **state)
2556 MonoDomain *domain = mono_domain_get ();
2557 MonoMethodSignature *sig = method->signature;
2558 MonoMethodMessage *msg;
2561 msg = (MonoMethodMessage *)mono_object_new (domain, mono_defaults.mono_method_message_class);
2564 mono_message_init (domain, msg, mono_method_get_object (domain, invoke, NULL), NULL);
2565 count = sig->param_count - 2;
2567 mono_message_init (domain, msg, mono_method_get_object (domain, method, NULL), NULL);
2568 count = sig->param_count;
2571 for (i = 0; i < count; i++) {
2576 if (sig->params [i]->byref)
2577 vpos = *((gpointer *)params [i]);
2581 type = sig->params [i]->type;
2582 class = mono_class_from_mono_type (sig->params [i]);
2584 if (class->valuetype)
2585 arg = mono_value_box (domain, class, vpos);
2587 arg = *((MonoObject **)vpos);
2589 mono_array_set (msg->args, gpointer, i, arg);
2592 if (cb != NULL && state != NULL) {
2593 *cb = *((MonoDelegate **)params [i]);
2595 *state = *((MonoObject **)params [i]);
2602 * mono_method_return_message_restore:
2604 * Restore results from message based processing back to arguments pointers
2607 mono_method_return_message_restore (MonoMethod *method, gpointer *params, MonoArray *out_args)
2609 MonoMethodSignature *sig = method->signature;
2610 int i, j, type, size;
2611 for (i = 0, j = 0; i < sig->param_count; i++) {
2612 MonoType *pt = sig->params [i];
2615 char *arg = mono_array_get (out_args, gpointer, j);
2619 case MONO_TYPE_VOID:
2620 g_assert_not_reached ();
2624 case MONO_TYPE_BOOLEAN:
2627 case MONO_TYPE_CHAR:
2634 case MONO_TYPE_VALUETYPE: {
2635 size = mono_class_value_size (((MonoObject*)arg)->vtable->klass, NULL);
2636 memcpy (*((gpointer *)params [i]), arg + sizeof (MonoObject), size);
2639 case MONO_TYPE_STRING:
2640 case MONO_TYPE_CLASS:
2641 case MONO_TYPE_ARRAY:
2642 case MONO_TYPE_SZARRAY:
2643 **((MonoObject ***)params [i]) = (MonoObject *)arg;
2646 g_assert_not_reached ();
2655 * mono_load_remote_field:
2656 * @this: pointer to an object
2657 * @klass: klass of the object containing @field
2658 * @field: the field to load
2659 * @res: a storage to store the result
2661 * This method is called by the runtime on attempts to load fields of
2662 * transparent proxy objects. @this points to such TP, @klass is the class of
2663 * the object containing @field. @res is a storage location which can be
2664 * used to store the result.
2666 * Returns: an address pointing to the value of field.
2669 mono_load_remote_field (MonoObject *this, MonoClass *klass, MonoClassField *field, gpointer *res)
2671 static MonoMethod *getter = NULL;
2672 MonoDomain *domain = mono_domain_get ();
2673 MonoTransparentProxy *tp = (MonoTransparentProxy *) this;
2674 MonoClass *field_class;
2675 MonoMethodMessage *msg;
2676 MonoArray *out_args;
2680 g_assert (this->vtable->klass == mono_defaults.transparent_proxy_class);
2685 if (tp->klass->contextbound && tp->rp->context == (MonoObject *) mono_context_get ()) {
2686 mono_field_get_value (tp->rp->unwrapped_server, field, res);
2693 for (i = 0; i < mono_defaults.object_class->method.count; ++i) {
2694 MonoMethod *cm = mono_defaults.object_class->methods [i];
2696 if (!strcmp (cm->name, "FieldGetter")) {
2704 field_class = mono_class_from_mono_type (field->type);
2706 msg = (MonoMethodMessage *)mono_object_new (domain, mono_defaults.mono_method_message_class);
2707 out_args = mono_array_new (domain, mono_defaults.object_class, 1);
2708 mono_message_init (domain, msg, mono_method_get_object (domain, getter, NULL), out_args);
2710 mono_array_set (msg->args, gpointer, 0, mono_string_new (domain, klass->name));
2711 mono_array_set (msg->args, gpointer, 1, mono_string_new (domain, field->name));
2713 mono_remoting_invoke ((MonoObject *)(tp->rp), msg, &exc, &out_args);
2715 if (exc) mono_raise_exception ((MonoException *)exc);
2717 *res = mono_array_get (out_args, MonoObject *, 0);
2719 if (field_class->valuetype) {
2720 return ((char *)*res) + sizeof (MonoObject);
2726 mono_load_remote_field_new (MonoObject *this, MonoClass *klass, MonoClassField *field)
2728 static MonoMethod *getter = NULL;
2729 MonoDomain *domain = mono_domain_get ();
2730 MonoTransparentProxy *tp = (MonoTransparentProxy *) this;
2731 MonoClass *field_class;
2732 MonoMethodMessage *msg;
2733 MonoArray *out_args;
2734 MonoObject *exc, *res;
2736 g_assert (this->vtable->klass == mono_defaults.transparent_proxy_class);
2738 field_class = mono_class_from_mono_type (field->type);
2740 if (tp->klass->contextbound && tp->rp->context == (MonoObject *) mono_context_get ()) {
2742 if (field_class->valuetype) {
2743 res = mono_object_new (domain, field_class);
2744 val = ((gchar *) res) + sizeof (MonoObject);
2748 mono_field_get_value (tp->rp->unwrapped_server, field, val);
2755 for (i = 0; i < mono_defaults.object_class->method.count; ++i) {
2756 MonoMethod *cm = mono_defaults.object_class->methods [i];
2758 if (!strcmp (cm->name, "FieldGetter")) {
2766 msg = (MonoMethodMessage *)mono_object_new (domain, mono_defaults.mono_method_message_class);
2767 out_args = mono_array_new (domain, mono_defaults.object_class, 1);
2768 mono_message_init (domain, msg, mono_method_get_object (domain, getter, NULL), out_args);
2770 mono_array_set (msg->args, gpointer, 0, mono_string_new (domain, klass->name));
2771 mono_array_set (msg->args, gpointer, 1, mono_string_new (domain, field->name));
2773 mono_remoting_invoke ((MonoObject *)(tp->rp), msg, &exc, &out_args);
2775 if (exc) mono_raise_exception ((MonoException *)exc);
2777 res = mono_array_get (out_args, MonoObject *, 0);
2783 * mono_store_remote_field:
2784 * @this: pointer to an object
2785 * @klass: klass of the object containing @field
2786 * @field: the field to load
2787 * @val: the value/object to store
2789 * This method is called by the runtime on attempts to store fields of
2790 * transparent proxy objects. @this points to such TP, @klass is the class of
2791 * the object containing @field. @val is the new value to store in @field.
2794 mono_store_remote_field (MonoObject *this, MonoClass *klass, MonoClassField *field, gpointer val)
2796 static MonoMethod *setter = NULL;
2797 MonoDomain *domain = mono_domain_get ();
2798 MonoTransparentProxy *tp = (MonoTransparentProxy *) this;
2799 MonoClass *field_class;
2800 MonoMethodMessage *msg;
2801 MonoArray *out_args;
2805 g_assert (this->vtable->klass == mono_defaults.transparent_proxy_class);
2807 field_class = mono_class_from_mono_type (field->type);
2809 if (tp->klass->contextbound && tp->rp->context == (MonoObject *) mono_context_get ()) {
2810 if (field_class->valuetype)
2811 mono_field_set_value (tp->rp->unwrapped_server, field, val);
2813 mono_field_set_value (tp->rp->unwrapped_server, field, *((MonoObject **)val));
2820 for (i = 0; i < mono_defaults.object_class->method.count; ++i) {
2821 MonoMethod *cm = mono_defaults.object_class->methods [i];
2823 if (!strcmp (cm->name, "FieldSetter")) {
2831 if (field_class->valuetype)
2832 arg = mono_value_box (domain, field_class, val);
2834 arg = *((MonoObject **)val);
2837 msg = (MonoMethodMessage *)mono_object_new (domain, mono_defaults.mono_method_message_class);
2838 mono_message_init (domain, msg, mono_method_get_object (domain, setter, NULL), NULL);
2840 mono_array_set (msg->args, gpointer, 0, mono_string_new (domain, klass->name));
2841 mono_array_set (msg->args, gpointer, 1, mono_string_new (domain, field->name));
2842 mono_array_set (msg->args, gpointer, 2, arg);
2844 mono_remoting_invoke ((MonoObject *)(tp->rp), msg, &exc, &out_args);
2846 if (exc) mono_raise_exception ((MonoException *)exc);
2850 mono_store_remote_field_new (MonoObject *this, MonoClass *klass, MonoClassField *field, MonoObject *arg)
2852 static MonoMethod *setter = NULL;
2853 MonoDomain *domain = mono_domain_get ();
2854 MonoTransparentProxy *tp = (MonoTransparentProxy *) this;
2855 MonoClass *field_class;
2856 MonoMethodMessage *msg;
2857 MonoArray *out_args;
2860 g_assert (this->vtable->klass == mono_defaults.transparent_proxy_class);
2862 field_class = mono_class_from_mono_type (field->type);
2864 if (tp->klass->contextbound && tp->rp->context == (MonoObject *) mono_context_get ()) {
2865 if (field_class->valuetype)
2866 mono_field_set_value (tp->rp->unwrapped_server, field, ((gchar *) arg) + sizeof (MonoObject));
2868 mono_field_set_value (tp->rp->unwrapped_server, field, arg);
2875 for (i = 0; i < mono_defaults.object_class->method.count; ++i) {
2876 MonoMethod *cm = mono_defaults.object_class->methods [i];
2878 if (!strcmp (cm->name, "FieldSetter")) {
2886 msg = (MonoMethodMessage *)mono_object_new (domain, mono_defaults.mono_method_message_class);
2887 mono_message_init (domain, msg, mono_method_get_object (domain, setter, NULL), NULL);
2889 mono_array_set (msg->args, gpointer, 0, mono_string_new (domain, klass->name));
2890 mono_array_set (msg->args, gpointer, 1, mono_string_new (domain, field->name));
2891 mono_array_set (msg->args, gpointer, 2, arg);
2893 mono_remoting_invoke ((MonoObject *)(tp->rp), msg, &exc, &out_args);
2895 if (exc) mono_raise_exception ((MonoException *)exc);