2 * object.c: Object creation for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2011 Novell, Inc (http://www.novell.com)
10 * Copyright 2001 Xamarin Inc (http://www.xamarin.com)
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/tokentype.h>
22 #include <mono/metadata/loader.h>
23 #include <mono/metadata/object.h>
24 #include <mono/metadata/gc-internals.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/exception-internals.h>
27 #include <mono/metadata/domain-internals.h>
28 #include "mono/metadata/metadata-internals.h"
29 #include "mono/metadata/class-internals.h"
30 #include <mono/metadata/assembly.h>
31 #include <mono/metadata/marshal.h>
32 #include "mono/metadata/debug-helpers.h"
33 #include "mono/metadata/marshal.h"
34 #include <mono/metadata/threads.h>
35 #include <mono/metadata/threads-types.h>
36 #include <mono/metadata/environment.h>
37 #include "mono/metadata/profiler-private.h"
38 #include "mono/metadata/security-manager.h"
39 #include "mono/metadata/mono-debug-debugger.h"
40 #include <mono/metadata/gc-internals.h>
41 #include <mono/metadata/verify-internals.h>
42 #include <mono/metadata/reflection-internals.h>
43 #include <mono/utils/strenc.h>
44 #include <mono/utils/mono-counters.h>
45 #include <mono/utils/mono-error-internals.h>
46 #include <mono/utils/mono-memory-model.h>
47 #include <mono/utils/checked-build.h>
48 #include <mono/utils/mono-threads.h>
49 #include "cominterop.h"
52 get_default_field_value (MonoDomain* domain, MonoClassField *field, void *value);
55 mono_ldstr_metadata_sig (MonoDomain *domain, const char* sig);
58 free_main_args (void);
61 mono_string_to_utf8_internal (MonoMemPool *mp, MonoImage *image, MonoString *s, gboolean ignore_error, MonoError *error);
63 /* Class lazy loading functions */
64 static GENERATE_GET_CLASS_WITH_CACHE (pointer, System.Reflection, Pointer)
65 static GENERATE_GET_CLASS_WITH_CACHE (remoting_services, System.Runtime.Remoting, RemotingServices)
66 static GENERATE_GET_CLASS_WITH_CACHE (unhandled_exception_event_args, System, UnhandledExceptionEventArgs)
67 static GENERATE_GET_CLASS_WITH_CACHE (sta_thread_attribute, System, STAThreadAttribute)
68 static GENERATE_GET_CLASS_WITH_CACHE (activation_services, System.Runtime.Remoting.Activation, ActivationServices)
71 #define ldstr_lock() mono_os_mutex_lock (&ldstr_section)
72 #define ldstr_unlock() mono_os_mutex_unlock (&ldstr_section)
73 static mono_mutex_t ldstr_section;
76 * mono_runtime_object_init:
77 * @this_obj: the object to initialize
79 * This function calls the zero-argument constructor (which must
80 * exist) for the given object.
83 mono_runtime_object_init (MonoObject *this_obj)
86 mono_runtime_object_init_checked (this_obj, &error);
87 mono_error_assert_ok (&error);
91 * mono_runtime_object_init_checked:
92 * @this_obj: the object to initialize
93 * @error: set on error.
95 * This function calls the zero-argument constructor (which must
96 * exist) for the given object and returns TRUE on success, or FALSE
97 * on error and sets @error.
100 mono_runtime_object_init_checked (MonoObject *this_obj, MonoError *error)
102 MONO_REQ_GC_UNSAFE_MODE;
104 MonoMethod *method = NULL;
105 MonoClass *klass = this_obj->vtable->klass;
107 mono_error_init (error);
108 method = mono_class_get_method_from_name (klass, ".ctor", 0);
110 g_error ("Could not lookup zero argument constructor for class %s", mono_type_get_full_name (klass));
112 if (method->klass->valuetype)
113 this_obj = (MonoObject *)mono_object_unbox (this_obj);
115 mono_runtime_invoke_checked (method, this_obj, NULL, error);
116 return is_ok (error);
119 /* The pseudo algorithm for type initialization from the spec
120 Note it doesn't say anything about domains - only threads.
122 2. If the type is initialized you are done.
123 2.1. If the type is not yet initialized, try to take an
125 2.2. If successful, record this thread as responsible for
126 initializing the type and proceed to step 2.3.
127 2.2.1. If not, see whether this thread or any thread
128 waiting for this thread to complete already holds the lock.
129 2.2.2. If so, return since blocking would create a deadlock. This thread
130 will now see an incompletely initialized state for the type,
131 but no deadlock will arise.
132 2.2.3 If not, block until the type is initialized then return.
133 2.3 Initialize the parent type and then all interfaces implemented
135 2.4 Execute the type initialization code for this type.
136 2.5 Mark the type as initialized, release the initialization lock,
137 awaken any threads waiting for this type to be initialized,
144 MonoNativeThreadId initializing_tid;
145 guint32 waiting_count;
147 MonoCoopMutex initialization_section;
148 } TypeInitializationLock;
150 /* for locking access to type_initialization_hash and blocked_thread_hash */
151 static MonoCoopMutex type_initialization_section;
154 mono_type_initialization_lock (void)
156 /* The critical sections protected by this lock in mono_runtime_class_init_full () can block */
157 mono_coop_mutex_lock (&type_initialization_section);
161 mono_type_initialization_unlock (void)
163 mono_coop_mutex_unlock (&type_initialization_section);
167 mono_type_init_lock (TypeInitializationLock *lock)
169 MONO_REQ_GC_NEUTRAL_MODE;
171 mono_coop_mutex_lock (&lock->initialization_section);
175 mono_type_init_unlock (TypeInitializationLock *lock)
177 mono_coop_mutex_unlock (&lock->initialization_section);
180 /* from vtable to lock */
181 static GHashTable *type_initialization_hash;
183 /* from thread id to thread id being waited on */
184 static GHashTable *blocked_thread_hash;
187 static MonoThread *main_thread;
189 /* Functions supplied by the runtime */
190 static MonoRuntimeCallbacks callbacks;
193 * mono_thread_set_main:
194 * @thread: thread to set as the main thread
196 * This function can be used to instruct the runtime to treat @thread
197 * as the main thread, ie, the thread that would normally execute the Main()
198 * method. This basically means that at the end of @thread, the runtime will
199 * wait for the existing foreground threads to quit and other such details.
202 mono_thread_set_main (MonoThread *thread)
204 MONO_REQ_GC_UNSAFE_MODE;
206 static gboolean registered = FALSE;
209 MONO_GC_REGISTER_ROOT_SINGLE (main_thread, MONO_ROOT_SOURCE_THREADING, "main thread object");
213 main_thread = thread;
217 mono_thread_get_main (void)
219 MONO_REQ_GC_UNSAFE_MODE;
225 mono_type_initialization_init (void)
227 mono_coop_mutex_init_recursive (&type_initialization_section);
228 type_initialization_hash = g_hash_table_new (NULL, NULL);
229 blocked_thread_hash = g_hash_table_new (NULL, NULL);
230 mono_os_mutex_init_recursive (&ldstr_section);
234 mono_type_initialization_cleanup (void)
237 /* This is causing race conditions with
238 * mono_release_type_locks
240 mono_coop_mutex_destroy (&type_initialization_section);
241 g_hash_table_destroy (type_initialization_hash);
242 type_initialization_hash = NULL;
244 mono_os_mutex_destroy (&ldstr_section);
245 g_hash_table_destroy (blocked_thread_hash);
246 blocked_thread_hash = NULL;
252 * get_type_init_exception_for_vtable:
254 * Return the stored type initialization exception for VTABLE.
256 static MonoException*
257 get_type_init_exception_for_vtable (MonoVTable *vtable)
259 MONO_REQ_GC_UNSAFE_MODE;
262 MonoDomain *domain = vtable->domain;
263 MonoClass *klass = vtable->klass;
267 if (!vtable->init_failed)
268 g_error ("Trying to get the init exception for a non-failed vtable of class %s", mono_type_get_full_name (klass));
271 * If the initializing thread was rudely aborted, the exception is not stored
275 mono_domain_lock (domain);
276 if (domain->type_init_exception_hash)
277 ex = (MonoException *)mono_g_hash_table_lookup (domain->type_init_exception_hash, klass);
278 mono_domain_unlock (domain);
281 if (klass->name_space && *klass->name_space)
282 full_name = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
284 full_name = g_strdup (klass->name);
285 ex = mono_get_exception_type_initialization_checked (full_name, NULL, &error);
287 return_val_if_nok (&error, NULL);
294 * mono_runtime_class_init:
295 * @vtable: vtable that needs to be initialized
297 * This routine calls the class constructor for @vtable.
300 mono_runtime_class_init (MonoVTable *vtable)
302 MONO_REQ_GC_UNSAFE_MODE;
305 mono_runtime_class_init_full (vtable, &error);
306 mono_error_assert_ok (&error);
310 * mono_runtime_class_init_full:
311 * @vtable that neeeds to be initialized
312 * @error set on error
314 * returns TRUE if class constructor .cctor has been initialized successfully, or FALSE otherwise and sets @error.
318 mono_runtime_class_init_full (MonoVTable *vtable, MonoError *error)
320 MONO_REQ_GC_UNSAFE_MODE;
322 MonoMethod *method = NULL;
325 MonoDomain *domain = vtable->domain;
326 TypeInitializationLock *lock;
327 MonoNativeThreadId tid;
328 int do_initialization = 0;
329 MonoDomain *last_domain = NULL;
331 mono_error_init (error);
333 if (vtable->initialized)
336 klass = vtable->klass;
338 if (!klass->image->checked_module_cctor) {
339 mono_image_check_for_module_cctor (klass->image);
340 if (klass->image->has_module_cctor) {
341 MonoClass *module_klass;
342 MonoVTable *module_vtable;
344 module_klass = mono_class_get_checked (klass->image, MONO_TOKEN_TYPE_DEF | 1, error);
349 module_vtable = mono_class_vtable_full (vtable->domain, module_klass, error);
352 if (!mono_runtime_class_init_full (module_vtable, error))
356 method = mono_class_get_cctor (klass);
358 vtable->initialized = 1;
362 tid = mono_native_thread_id_get ();
364 mono_type_initialization_lock ();
365 /* double check... */
366 if (vtable->initialized) {
367 mono_type_initialization_unlock ();
370 if (vtable->init_failed) {
371 mono_type_initialization_unlock ();
373 /* The type initialization already failed once, rethrow the same exception */
374 mono_error_set_exception_instance (error, get_type_init_exception_for_vtable (vtable));
377 lock = (TypeInitializationLock *)g_hash_table_lookup (type_initialization_hash, vtable);
379 /* This thread will get to do the initialization */
380 if (mono_domain_get () != domain) {
381 /* Transfer into the target domain */
382 last_domain = mono_domain_get ();
383 if (!mono_domain_set (domain, FALSE)) {
384 vtable->initialized = 1;
385 mono_type_initialization_unlock ();
386 mono_error_set_exception_instance (error, mono_get_exception_appdomain_unloaded ());
390 lock = (TypeInitializationLock *)g_malloc (sizeof (TypeInitializationLock));
391 mono_coop_mutex_init_recursive (&lock->initialization_section);
392 lock->initializing_tid = tid;
393 lock->waiting_count = 1;
395 /* grab the vtable lock while this thread still owns type_initialization_section */
396 /* This is why type_initialization_lock needs to enter blocking mode */
397 mono_type_init_lock (lock);
398 g_hash_table_insert (type_initialization_hash, vtable, lock);
399 do_initialization = 1;
402 TypeInitializationLock *pending_lock;
404 if (mono_native_thread_id_equals (lock->initializing_tid, tid) || lock->done) {
405 mono_type_initialization_unlock ();
408 /* see if the thread doing the initialization is already blocked on this thread */
409 blocked = GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (lock->initializing_tid));
410 while ((pending_lock = (TypeInitializationLock*) g_hash_table_lookup (blocked_thread_hash, blocked))) {
411 if (mono_native_thread_id_equals (pending_lock->initializing_tid, tid)) {
412 if (!pending_lock->done) {
413 mono_type_initialization_unlock ();
416 /* the thread doing the initialization is blocked on this thread,
417 but on a lock that has already been freed. It just hasn't got
422 blocked = GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (pending_lock->initializing_tid));
424 ++lock->waiting_count;
425 /* record the fact that we are waiting on the initializing thread */
426 g_hash_table_insert (blocked_thread_hash, GUINT_TO_POINTER (tid), lock);
428 mono_type_initialization_unlock ();
430 if (do_initialization) {
431 MonoException *exc = NULL;
432 mono_runtime_try_invoke (method, NULL, NULL, (MonoObject**) &exc, error);
433 if (exc != NULL && mono_error_ok (error)) {
434 mono_error_set_exception_instance (error, exc);
437 /* If the initialization failed, mark the class as unusable. */
438 /* Avoid infinite loops */
439 if (!(mono_error_ok(error) ||
440 (klass->image == mono_defaults.corlib &&
441 !strcmp (klass->name_space, "System") &&
442 !strcmp (klass->name, "TypeInitializationException")))) {
443 vtable->init_failed = 1;
445 if (klass->name_space && *klass->name_space)
446 full_name = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
448 full_name = g_strdup (klass->name);
450 MonoException *exc_to_throw = mono_get_exception_type_initialization_checked (full_name, exc, error);
452 return_val_if_nok (error, FALSE);
454 mono_error_set_exception_instance (error, exc_to_throw);
456 MonoException *exc_to_store = mono_error_convert_to_exception (error);
457 /* What we really want to do here is clone the error object and store one copy in the
458 * domain's exception hash and use the other one to error out here. */
459 mono_error_set_exception_instance (error, exc_to_store);
461 * Store the exception object so it could be thrown on subsequent
464 mono_domain_lock (domain);
465 if (!domain->type_init_exception_hash)
466 domain->type_init_exception_hash = mono_g_hash_table_new_type (mono_aligned_addr_hash, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, "type initialization exceptions table");
467 mono_g_hash_table_insert (domain->type_init_exception_hash, klass, exc_to_store);
468 mono_domain_unlock (domain);
472 mono_domain_set (last_domain, TRUE);
474 mono_type_init_unlock (lock);
476 /* this just blocks until the initializing thread is done */
477 mono_type_init_lock (lock);
478 mono_type_init_unlock (lock);
481 mono_type_initialization_lock ();
482 if (!mono_native_thread_id_equals (lock->initializing_tid, tid))
483 g_hash_table_remove (blocked_thread_hash, GUINT_TO_POINTER (tid));
484 --lock->waiting_count;
485 if (lock->waiting_count == 0) {
486 mono_coop_mutex_destroy (&lock->initialization_section);
487 g_hash_table_remove (type_initialization_hash, vtable);
490 mono_memory_barrier ();
491 if (!vtable->init_failed)
492 vtable->initialized = 1;
493 mono_type_initialization_unlock ();
495 if (vtable->init_failed) {
496 /* Either we were the initializing thread or we waited for the initialization */
497 mono_error_set_exception_instance (error, get_type_init_exception_for_vtable (vtable));
504 gboolean release_type_locks (gpointer key, gpointer value, gpointer user)
506 MONO_REQ_GC_NEUTRAL_MODE;
508 MonoVTable *vtable = (MonoVTable*)key;
510 TypeInitializationLock *lock = (TypeInitializationLock*) value;
511 if (mono_native_thread_id_equals (lock->initializing_tid, MONO_UINT_TO_NATIVE_THREAD_ID (GPOINTER_TO_UINT (user))) && !lock->done) {
514 * Have to set this since it cannot be set by the normal code in
515 * mono_runtime_class_init (). In this case, the exception object is not stored,
516 * and get_type_init_exception_for_class () needs to be aware of this.
518 vtable->init_failed = 1;
519 mono_type_init_unlock (lock);
520 --lock->waiting_count;
521 if (lock->waiting_count == 0) {
522 mono_coop_mutex_destroy (&lock->initialization_section);
531 mono_release_type_locks (MonoInternalThread *thread)
533 MONO_REQ_GC_UNSAFE_MODE;
535 mono_type_initialization_lock ();
536 g_hash_table_foreach_remove (type_initialization_hash, release_type_locks, GUINT_TO_POINTER (thread->tid));
537 mono_type_initialization_unlock ();
541 default_trampoline (MonoMethod *method)
547 default_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
549 g_assert_not_reached ();
554 #ifndef DISABLE_REMOTING
557 default_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target)
559 g_error ("remoting not installed");
563 static MonoRemotingTrampoline arch_create_remoting_trampoline = default_remoting_trampoline;
567 default_delegate_trampoline (MonoDomain *domain, MonoClass *klass)
569 g_assert_not_reached ();
573 static MonoDelegateTrampoline arch_create_delegate_trampoline = default_delegate_trampoline;
574 static MonoImtThunkBuilder imt_thunk_builder;
575 static gboolean always_build_imt_thunks;
577 #if (MONO_IMT_SIZE > 32)
578 #error "MONO_IMT_SIZE cannot be larger than 32"
582 mono_install_callbacks (MonoRuntimeCallbacks *cbs)
584 memcpy (&callbacks, cbs, sizeof (*cbs));
587 MonoRuntimeCallbacks*
588 mono_get_runtime_callbacks (void)
593 #ifndef DISABLE_REMOTING
595 mono_install_remoting_trampoline (MonoRemotingTrampoline func)
597 arch_create_remoting_trampoline = func? func: default_remoting_trampoline;
602 mono_install_delegate_trampoline (MonoDelegateTrampoline func)
604 arch_create_delegate_trampoline = func? func: default_delegate_trampoline;
608 mono_install_imt_thunk_builder (MonoImtThunkBuilder func) {
609 imt_thunk_builder = func;
613 mono_set_always_build_imt_thunks (gboolean value)
615 always_build_imt_thunks = value;
619 * mono_compile_method:
620 * @method: The method to compile.
622 * This JIT-compiles the method, and returns the pointer to the native code
626 mono_compile_method (MonoMethod *method)
631 MONO_REQ_GC_NEUTRAL_MODE
633 if (!callbacks.compile_method) {
634 g_error ("compile method called on uninitialized runtime");
637 res = callbacks.compile_method (method, &error);
638 if (!mono_error_ok (&error))
639 mono_error_raise_exception (&error);
644 mono_runtime_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
649 MONO_REQ_GC_NEUTRAL_MODE;
651 res = callbacks.create_jump_trampoline (domain, method, add_sync_wrapper, &error);
652 if (!mono_error_ok (&error))
653 mono_error_raise_exception (&error); /* FIXME: Don't raise here */
658 mono_runtime_create_delegate_trampoline (MonoClass *klass)
660 MONO_REQ_GC_NEUTRAL_MODE
662 return arch_create_delegate_trampoline (mono_domain_get (), klass);
665 static MonoFreeMethodFunc default_mono_free_method = NULL;
668 * mono_install_free_method:
669 * @func: pointer to the MonoFreeMethodFunc used to release a method
671 * This is an internal VM routine, it is used for the engines to
672 * register a handler to release the resources associated with a method.
674 * Methods are freed when no more references to the delegate that holds
678 mono_install_free_method (MonoFreeMethodFunc func)
680 default_mono_free_method = func;
684 * mono_runtime_free_method:
685 * @domain; domain where the method is hosted
686 * @method: method to release
688 * This routine is invoked to free the resources associated with
689 * a method that has been JIT compiled. This is used to discard
690 * methods that were used only temporarily (for example, used in marshalling)
694 mono_runtime_free_method (MonoDomain *domain, MonoMethod *method)
696 MONO_REQ_GC_NEUTRAL_MODE
698 if (default_mono_free_method != NULL)
699 default_mono_free_method (domain, method);
701 mono_method_clear_object (domain, method);
703 mono_free_method (method);
707 * The vtables in the root appdomain are assumed to be reachable by other
708 * roots, and we don't use typed allocation in the other domains.
711 /* The sync block is no longer a GC pointer */
712 #define GC_HEADER_BITMAP (0)
714 #define BITMAP_EL_SIZE (sizeof (gsize) * 8)
717 compute_class_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int *max_set, gboolean static_fields)
719 MONO_REQ_GC_NEUTRAL_MODE;
721 MonoClassField *field;
727 max_size = mono_class_data_size (klass) / sizeof (gpointer);
729 max_size = klass->instance_size / sizeof (gpointer);
730 if (max_size > size) {
731 g_assert (offset <= 0);
732 bitmap = (gsize *)g_malloc0 ((max_size + BITMAP_EL_SIZE - 1) / BITMAP_EL_SIZE * sizeof (gsize));
737 /*An Ephemeron cannot be marked by sgen*/
738 if (!static_fields && klass->image == mono_defaults.corlib && !strcmp ("Ephemeron", klass->name)) {
740 memset (bitmap, 0, size / 8);
745 for (p = klass; p != NULL; p = p->parent) {
746 gpointer iter = NULL;
747 while ((field = mono_class_get_fields (p, &iter))) {
751 if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)))
753 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
756 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
759 /* FIXME: should not happen, flag as type load error */
760 if (field->type->byref)
763 if (static_fields && field->offset == -1)
767 pos = field->offset / sizeof (gpointer);
770 type = mono_type_get_underlying_type (field->type);
771 switch (type->type) {
774 case MONO_TYPE_FNPTR:
776 /* only UIntPtr is allowed to be GC-tracked and only in mscorlib */
781 if (klass->image != mono_defaults.corlib)
784 case MONO_TYPE_STRING:
785 case MONO_TYPE_SZARRAY:
786 case MONO_TYPE_CLASS:
787 case MONO_TYPE_OBJECT:
788 case MONO_TYPE_ARRAY:
789 g_assert ((field->offset % sizeof(gpointer)) == 0);
791 g_assert (pos < size || pos <= max_size);
792 bitmap [pos / BITMAP_EL_SIZE] |= ((gsize)1) << (pos % BITMAP_EL_SIZE);
793 *max_set = MAX (*max_set, pos);
795 case MONO_TYPE_GENERICINST:
796 if (!mono_type_generic_inst_is_valuetype (type)) {
797 g_assert ((field->offset % sizeof(gpointer)) == 0);
799 bitmap [pos / BITMAP_EL_SIZE] |= ((gsize)1) << (pos % BITMAP_EL_SIZE);
800 *max_set = MAX (*max_set, pos);
805 case MONO_TYPE_VALUETYPE: {
806 MonoClass *fclass = mono_class_from_mono_type (field->type);
807 if (fclass->has_references) {
808 /* remove the object header */
809 compute_class_bitmap (fclass, bitmap, size, pos - (sizeof (MonoObject) / sizeof (gpointer)), max_set, FALSE);
823 case MONO_TYPE_BOOLEAN:
827 g_error ("compute_class_bitmap: Invalid type %x for field %s:%s\n", type->type, mono_type_get_full_name (field->parent), field->name);
838 * mono_class_compute_bitmap:
840 * Mono internal function to compute a bitmap of reference fields in a class.
843 mono_class_compute_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int *max_set, gboolean static_fields)
845 MONO_REQ_GC_NEUTRAL_MODE;
847 return compute_class_bitmap (klass, bitmap, size, offset, max_set, static_fields);
852 * similar to the above, but sets the bits in the bitmap for any non-ref field
853 * and ignores static fields
856 compute_class_non_ref_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset)
858 MonoClassField *field;
863 max_size = class->instance_size / sizeof (gpointer);
864 if (max_size >= size) {
865 bitmap = g_malloc0 (sizeof (gsize) * ((max_size) + 1));
868 for (p = class; p != NULL; p = p->parent) {
869 gpointer iter = NULL;
870 while ((field = mono_class_get_fields (p, &iter))) {
873 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
875 /* FIXME: should not happen, flag as type load error */
876 if (field->type->byref)
879 pos = field->offset / sizeof (gpointer);
882 type = mono_type_get_underlying_type (field->type);
883 switch (type->type) {
884 #if SIZEOF_VOID_P == 8
888 case MONO_TYPE_FNPTR:
893 if ((((field->offset + 7) / sizeof (gpointer)) + offset) != pos) {
894 pos2 = ((field->offset + 7) / sizeof (gpointer)) + offset;
895 bitmap [pos2 / BITMAP_EL_SIZE] |= ((gsize)1) << (pos2 % BITMAP_EL_SIZE);
898 #if SIZEOF_VOID_P == 4
902 case MONO_TYPE_FNPTR:
907 if ((((field->offset + 3) / sizeof (gpointer)) + offset) != pos) {
908 pos2 = ((field->offset + 3) / sizeof (gpointer)) + offset;
909 bitmap [pos2 / BITMAP_EL_SIZE] |= ((gsize)1) << (pos2 % BITMAP_EL_SIZE);
915 if ((((field->offset + 1) / sizeof (gpointer)) + offset) != pos) {
916 pos2 = ((field->offset + 1) / sizeof (gpointer)) + offset;
917 bitmap [pos2 / BITMAP_EL_SIZE] |= ((gsize)1) << (pos2 % BITMAP_EL_SIZE);
920 case MONO_TYPE_BOOLEAN:
923 bitmap [pos / BITMAP_EL_SIZE] |= ((gsize)1) << (pos % BITMAP_EL_SIZE);
925 case MONO_TYPE_STRING:
926 case MONO_TYPE_SZARRAY:
927 case MONO_TYPE_CLASS:
928 case MONO_TYPE_OBJECT:
929 case MONO_TYPE_ARRAY:
931 case MONO_TYPE_GENERICINST:
932 if (!mono_type_generic_inst_is_valuetype (type)) {
937 case MONO_TYPE_VALUETYPE: {
938 MonoClass *fclass = mono_class_from_mono_type (field->type);
939 /* remove the object header */
940 compute_class_non_ref_bitmap (fclass, bitmap, size, pos - (sizeof (MonoObject) / sizeof (gpointer)));
944 g_assert_not_reached ();
953 * mono_class_insecure_overlapping:
954 * check if a class with explicit layout has references and non-references
955 * fields overlapping.
957 * Returns: TRUE if it is insecure to load the type.
960 mono_class_insecure_overlapping (MonoClass *klass)
964 gsize default_bitmap [4] = {0};
966 gsize default_nrbitmap [4] = {0};
967 int i, insecure = FALSE;
970 bitmap = compute_class_bitmap (klass, default_bitmap, sizeof (default_bitmap) * 8, 0, &max_set, FALSE);
971 nrbitmap = compute_class_non_ref_bitmap (klass, default_nrbitmap, sizeof (default_nrbitmap) * 8, 0);
973 for (i = 0; i <= max_set; i += sizeof (bitmap [0]) * 8) {
974 int idx = i % (sizeof (bitmap [0]) * 8);
975 if (bitmap [idx] & nrbitmap [idx]) {
980 if (bitmap != default_bitmap)
982 if (nrbitmap != default_nrbitmap)
985 g_print ("class %s.%s in assembly %s has overlapping references\n", klass->name_space, klass->name, klass->image->name);
993 ves_icall_string_alloc (int length)
996 MonoString *str = mono_string_new_size_checked (mono_domain_get (), length, &error);
997 mono_error_raise_exception (&error);
1003 mono_class_compute_gc_descriptor (MonoClass *klass)
1005 MONO_REQ_GC_NEUTRAL_MODE;
1009 gsize default_bitmap [4] = {0};
1010 static gboolean gcj_inited = FALSE;
1013 mono_loader_lock ();
1015 mono_register_jit_icall (ves_icall_object_new_fast, "ves_icall_object_new_fast", mono_create_icall_signature ("object ptr"), FALSE);
1016 mono_register_jit_icall (ves_icall_string_alloc, "ves_icall_string_alloc", mono_create_icall_signature ("object int"), FALSE);
1019 mono_loader_unlock ();
1023 mono_class_init (klass);
1025 if (klass->gc_descr_inited)
1028 klass->gc_descr_inited = TRUE;
1029 klass->gc_descr = MONO_GC_DESCRIPTOR_NULL;
1031 bitmap = default_bitmap;
1032 if (klass == mono_defaults.string_class) {
1033 klass->gc_descr = mono_gc_make_descr_for_string (bitmap, 2);
1034 } else if (klass->rank) {
1035 mono_class_compute_gc_descriptor (klass->element_class);
1036 if (MONO_TYPE_IS_REFERENCE (&klass->element_class->byval_arg)) {
1038 klass->gc_descr = mono_gc_make_descr_for_array (klass->byval_arg.type == MONO_TYPE_SZARRAY, &abm, 1, sizeof (gpointer));
1039 /*printf ("new array descriptor: 0x%x for %s.%s\n", class->gc_descr,
1040 class->name_space, class->name);*/
1042 /* remove the object header */
1043 bitmap = compute_class_bitmap (klass->element_class, default_bitmap, sizeof (default_bitmap) * 8, - (int)(sizeof (MonoObject) / sizeof (gpointer)), &max_set, FALSE);
1044 klass->gc_descr = mono_gc_make_descr_for_array (klass->byval_arg.type == MONO_TYPE_SZARRAY, bitmap, mono_array_element_size (klass) / sizeof (gpointer), mono_array_element_size (klass));
1045 /*printf ("new vt array descriptor: 0x%x for %s.%s\n", class->gc_descr,
1046 class->name_space, class->name);*/
1047 if (bitmap != default_bitmap)
1051 /*static int count = 0;
1054 bitmap = compute_class_bitmap (klass, default_bitmap, sizeof (default_bitmap) * 8, 0, &max_set, FALSE);
1055 klass->gc_descr = mono_gc_make_descr_for_object (bitmap, max_set + 1, klass->instance_size);
1057 if (class->gc_descr == MONO_GC_DESCRIPTOR_NULL)
1058 g_print ("disabling typed alloc (%d) for %s.%s\n", max_set, class->name_space, class->name);
1060 /*printf ("new descriptor: %p 0x%x for %s.%s\n", class->gc_descr, bitmap [0], class->name_space, class->name);*/
1061 if (bitmap != default_bitmap)
1067 * field_is_special_static:
1068 * @fklass: The MonoClass to look up.
1069 * @field: The MonoClassField describing the field.
1071 * Returns: SPECIAL_STATIC_THREAD if the field is thread static, SPECIAL_STATIC_CONTEXT if it is context static,
1072 * SPECIAL_STATIC_NONE otherwise.
1075 field_is_special_static (MonoClass *fklass, MonoClassField *field)
1077 MONO_REQ_GC_NEUTRAL_MODE;
1080 MonoCustomAttrInfo *ainfo;
1082 ainfo = mono_custom_attrs_from_field_checked (fklass, field, &error);
1083 mono_error_cleanup (&error); /* FIXME don't swallow the error? */
1086 for (i = 0; i < ainfo->num_attrs; ++i) {
1087 MonoClass *klass = ainfo->attrs [i].ctor->klass;
1088 if (klass->image == mono_defaults.corlib) {
1089 if (strcmp (klass->name, "ThreadStaticAttribute") == 0) {
1090 mono_custom_attrs_free (ainfo);
1091 return SPECIAL_STATIC_THREAD;
1093 else if (strcmp (klass->name, "ContextStaticAttribute") == 0) {
1094 mono_custom_attrs_free (ainfo);
1095 return SPECIAL_STATIC_CONTEXT;
1099 mono_custom_attrs_free (ainfo);
1100 return SPECIAL_STATIC_NONE;
1103 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
1104 #define mix(a,b,c) { \
1105 a -= c; a ^= rot(c, 4); c += b; \
1106 b -= a; b ^= rot(a, 6); a += c; \
1107 c -= b; c ^= rot(b, 8); b += a; \
1108 a -= c; a ^= rot(c,16); c += b; \
1109 b -= a; b ^= rot(a,19); a += c; \
1110 c -= b; c ^= rot(b, 4); b += a; \
1112 #define final(a,b,c) { \
1113 c ^= b; c -= rot(b,14); \
1114 a ^= c; a -= rot(c,11); \
1115 b ^= a; b -= rot(a,25); \
1116 c ^= b; c -= rot(b,16); \
1117 a ^= c; a -= rot(c,4); \
1118 b ^= a; b -= rot(a,14); \
1119 c ^= b; c -= rot(b,24); \
1123 * mono_method_get_imt_slot:
1125 * The IMT slot is embedded into AOTed code, so this must return the same value
1126 * for the same method across all executions. This means:
1127 * - pointers shouldn't be used as hash values.
1128 * - mono_metadata_str_hash () should be used for hashing strings.
1131 mono_method_get_imt_slot (MonoMethod *method)
1133 MONO_REQ_GC_NEUTRAL_MODE;
1135 MonoMethodSignature *sig;
1137 guint32 *hashes_start, *hashes;
1141 /* This can be used to stress tests the collision code */
1145 * We do this to simplify generic sharing. It will hurt
1146 * performance in cases where a class implements two different
1147 * instantiations of the same generic interface.
1148 * The code in build_imt_slots () depends on this.
1150 if (method->is_inflated)
1151 method = ((MonoMethodInflated*)method)->declaring;
1153 sig = mono_method_signature (method);
1154 hashes_count = sig->param_count + 4;
1155 hashes_start = (guint32 *)malloc (hashes_count * sizeof (guint32));
1156 hashes = hashes_start;
1158 if (! MONO_CLASS_IS_INTERFACE (method->klass)) {
1159 g_error ("mono_method_get_imt_slot: %s.%s.%s is not an interface MonoMethod",
1160 method->klass->name_space, method->klass->name, method->name);
1163 /* Initialize hashes */
1164 hashes [0] = mono_metadata_str_hash (method->klass->name);
1165 hashes [1] = mono_metadata_str_hash (method->klass->name_space);
1166 hashes [2] = mono_metadata_str_hash (method->name);
1167 hashes [3] = mono_metadata_type_hash (sig->ret);
1168 for (i = 0; i < sig->param_count; i++) {
1169 hashes [4 + i] = mono_metadata_type_hash (sig->params [i]);
1172 /* Setup internal state */
1173 a = b = c = 0xdeadbeef + (((guint32)hashes_count)<<2);
1175 /* Handle most of the hashes */
1176 while (hashes_count > 3) {
1185 /* Handle the last 3 hashes (all the case statements fall through) */
1186 switch (hashes_count) {
1187 case 3 : c += hashes [2];
1188 case 2 : b += hashes [1];
1189 case 1 : a += hashes [0];
1191 case 0: /* nothing left to add */
1195 free (hashes_start);
1196 /* Report the result */
1197 return c % MONO_IMT_SIZE;
1206 add_imt_builder_entry (MonoImtBuilderEntry **imt_builder, MonoMethod *method, guint32 *imt_collisions_bitmap, int vtable_slot, int slot_num) {
1207 MONO_REQ_GC_NEUTRAL_MODE;
1209 guint32 imt_slot = mono_method_get_imt_slot (method);
1210 MonoImtBuilderEntry *entry;
1212 if (slot_num >= 0 && imt_slot != slot_num) {
1213 /* we build just a single imt slot and this is not it */
1217 entry = (MonoImtBuilderEntry *)g_malloc0 (sizeof (MonoImtBuilderEntry));
1218 entry->key = method;
1219 entry->value.vtable_slot = vtable_slot;
1220 entry->next = imt_builder [imt_slot];
1221 if (imt_builder [imt_slot] != NULL) {
1222 entry->children = imt_builder [imt_slot]->children + 1;
1223 if (entry->children == 1) {
1224 mono_stats.imt_slots_with_collisions++;
1225 *imt_collisions_bitmap |= (1 << imt_slot);
1228 entry->children = 0;
1229 mono_stats.imt_used_slots++;
1231 imt_builder [imt_slot] = entry;
1234 char *method_name = mono_method_full_name (method, TRUE);
1235 printf ("Added IMT slot for method (%p) %s: imt_slot = %d, vtable_slot = %d, colliding with other %d entries\n",
1236 method, method_name, imt_slot, vtable_slot, entry->children);
1237 g_free (method_name);
1244 print_imt_entry (const char* message, MonoImtBuilderEntry *e, int num) {
1246 MonoMethod *method = e->key;
1247 printf (" * %s [%d]: (%p) '%s.%s.%s'\n",
1251 method->klass->name_space,
1252 method->klass->name,
1255 printf (" * %s: NULL\n", message);
1261 compare_imt_builder_entries (const void *p1, const void *p2) {
1262 MonoImtBuilderEntry *e1 = *(MonoImtBuilderEntry**) p1;
1263 MonoImtBuilderEntry *e2 = *(MonoImtBuilderEntry**) p2;
1265 return (e1->key < e2->key) ? -1 : ((e1->key > e2->key) ? 1 : 0);
1269 imt_emit_ir (MonoImtBuilderEntry **sorted_array, int start, int end, GPtrArray *out_array)
1271 MONO_REQ_GC_NEUTRAL_MODE;
1273 int count = end - start;
1274 int chunk_start = out_array->len;
1277 for (i = start; i < end; ++i) {
1278 MonoIMTCheckItem *item = g_new0 (MonoIMTCheckItem, 1);
1279 item->key = sorted_array [i]->key;
1280 item->value = sorted_array [i]->value;
1281 item->has_target_code = sorted_array [i]->has_target_code;
1282 item->is_equals = TRUE;
1284 item->check_target_idx = out_array->len + 1;
1286 item->check_target_idx = 0;
1287 g_ptr_array_add (out_array, item);
1290 int middle = start + count / 2;
1291 MonoIMTCheckItem *item = g_new0 (MonoIMTCheckItem, 1);
1293 item->key = sorted_array [middle]->key;
1294 item->is_equals = FALSE;
1295 g_ptr_array_add (out_array, item);
1296 imt_emit_ir (sorted_array, start, middle, out_array);
1297 item->check_target_idx = imt_emit_ir (sorted_array, middle, end, out_array);
1303 imt_sort_slot_entries (MonoImtBuilderEntry *entries) {
1304 MONO_REQ_GC_NEUTRAL_MODE;
1306 int number_of_entries = entries->children + 1;
1307 MonoImtBuilderEntry **sorted_array = (MonoImtBuilderEntry **)malloc (sizeof (MonoImtBuilderEntry*) * number_of_entries);
1308 GPtrArray *result = g_ptr_array_new ();
1309 MonoImtBuilderEntry *current_entry;
1312 for (current_entry = entries, i = 0; current_entry != NULL; current_entry = current_entry->next, i++) {
1313 sorted_array [i] = current_entry;
1315 qsort (sorted_array, number_of_entries, sizeof (MonoImtBuilderEntry*), compare_imt_builder_entries);
1317 /*for (i = 0; i < number_of_entries; i++) {
1318 print_imt_entry (" sorted array:", sorted_array [i], i);
1321 imt_emit_ir (sorted_array, 0, number_of_entries, result);
1323 free (sorted_array);
1328 initialize_imt_slot (MonoVTable *vtable, MonoDomain *domain, MonoImtBuilderEntry *imt_builder_entry, gpointer fail_tramp)
1330 MONO_REQ_GC_NEUTRAL_MODE;
1332 if (imt_builder_entry != NULL) {
1333 if (imt_builder_entry->children == 0 && !fail_tramp && !always_build_imt_thunks) {
1334 /* No collision, return the vtable slot contents */
1335 return vtable->vtable [imt_builder_entry->value.vtable_slot];
1337 /* Collision, build the thunk */
1338 GPtrArray *imt_ir = imt_sort_slot_entries (imt_builder_entry);
1341 result = imt_thunk_builder (vtable, domain,
1342 (MonoIMTCheckItem**)imt_ir->pdata, imt_ir->len, fail_tramp);
1343 for (i = 0; i < imt_ir->len; ++i)
1344 g_free (g_ptr_array_index (imt_ir, i));
1345 g_ptr_array_free (imt_ir, TRUE);
1357 static MonoImtBuilderEntry*
1358 get_generic_virtual_entries (MonoDomain *domain, gpointer *vtable_slot);
1361 * LOCKING: requires the loader and domain locks.
1365 build_imt_slots (MonoClass *klass, MonoVTable *vt, MonoDomain *domain, gpointer* imt, GSList *extra_interfaces, int slot_num)
1367 MONO_REQ_GC_NEUTRAL_MODE;
1371 guint32 imt_collisions_bitmap = 0;
1372 MonoImtBuilderEntry **imt_builder = (MonoImtBuilderEntry **)calloc (MONO_IMT_SIZE, sizeof (MonoImtBuilderEntry*));
1373 int method_count = 0;
1374 gboolean record_method_count_for_max_collisions = FALSE;
1375 gboolean has_generic_virtual = FALSE, has_variant_iface = FALSE;
1378 printf ("Building IMT for class %s.%s slot %d\n", klass->name_space, klass->name, slot_num);
1380 for (i = 0; i < klass->interface_offsets_count; ++i) {
1381 MonoClass *iface = klass->interfaces_packed [i];
1382 int interface_offset = klass->interface_offsets_packed [i];
1383 int method_slot_in_interface, vt_slot;
1385 if (mono_class_has_variant_generic_params (iface))
1386 has_variant_iface = TRUE;
1388 mono_class_setup_methods (iface);
1389 vt_slot = interface_offset;
1390 for (method_slot_in_interface = 0; method_slot_in_interface < iface->method.count; method_slot_in_interface++) {
1393 if (slot_num >= 0 && iface->is_inflated) {
1395 * The imt slot of the method is the same as for its declaring method,
1396 * see the comment in mono_method_get_imt_slot (), so we can
1397 * avoid inflating methods which will be discarded by
1398 * add_imt_builder_entry anyway.
1400 method = mono_class_get_method_by_index (iface->generic_class->container_class, method_slot_in_interface);
1401 if (mono_method_get_imt_slot (method) != slot_num) {
1406 method = mono_class_get_method_by_index (iface, method_slot_in_interface);
1407 if (method->is_generic) {
1408 has_generic_virtual = TRUE;
1413 if (!(method->flags & METHOD_ATTRIBUTE_STATIC)) {
1414 add_imt_builder_entry (imt_builder, method, &imt_collisions_bitmap, vt_slot, slot_num);
1419 if (extra_interfaces) {
1420 int interface_offset = klass->vtable_size;
1422 for (list_item = extra_interfaces; list_item != NULL; list_item=list_item->next) {
1423 MonoClass* iface = (MonoClass *)list_item->data;
1424 int method_slot_in_interface;
1425 for (method_slot_in_interface = 0; method_slot_in_interface < iface->method.count; method_slot_in_interface++) {
1426 MonoMethod *method = mono_class_get_method_by_index (iface, method_slot_in_interface);
1428 if (method->is_generic)
1429 has_generic_virtual = TRUE;
1430 add_imt_builder_entry (imt_builder, method, &imt_collisions_bitmap, interface_offset + method_slot_in_interface, slot_num);
1432 interface_offset += iface->method.count;
1435 for (i = 0; i < MONO_IMT_SIZE; ++i) {
1436 /* overwrite the imt slot only if we're building all the entries or if
1437 * we're building this specific one
1439 if (slot_num < 0 || i == slot_num) {
1440 MonoImtBuilderEntry *entries = get_generic_virtual_entries (domain, &imt [i]);
1443 if (imt_builder [i]) {
1444 MonoImtBuilderEntry *entry;
1446 /* Link entries with imt_builder [i] */
1447 for (entry = entries; entry->next; entry = entry->next) {
1449 MonoMethod *method = (MonoMethod*)entry->key;
1450 char *method_name = mono_method_full_name (method, TRUE);
1451 printf ("Added extra entry for method (%p) %s: imt_slot = %d\n", method, method_name, i);
1452 g_free (method_name);
1455 entry->next = imt_builder [i];
1456 entries->children += imt_builder [i]->children + 1;
1458 imt_builder [i] = entries;
1461 if (has_generic_virtual || has_variant_iface) {
1463 * There might be collisions later when the the thunk is expanded.
1465 imt_collisions_bitmap |= (1 << i);
1468 * The IMT thunk might be called with an instance of one of the
1469 * generic virtual methods, so has to fallback to the IMT trampoline.
1471 imt [i] = initialize_imt_slot (vt, domain, imt_builder [i], callbacks.get_imt_trampoline (vt, i));
1473 imt [i] = initialize_imt_slot (vt, domain, imt_builder [i], NULL);
1476 printf ("initialize_imt_slot[%d]: %p methods %d\n", i, imt [i], imt_builder [i]->children + 1);
1480 if (imt_builder [i] != NULL) {
1481 int methods_in_slot = imt_builder [i]->children + 1;
1482 if (methods_in_slot > mono_stats.imt_max_collisions_in_slot) {
1483 mono_stats.imt_max_collisions_in_slot = methods_in_slot;
1484 record_method_count_for_max_collisions = TRUE;
1486 method_count += methods_in_slot;
1490 mono_stats.imt_number_of_methods += method_count;
1491 if (record_method_count_for_max_collisions) {
1492 mono_stats.imt_method_count_when_max_collisions = method_count;
1495 for (i = 0; i < MONO_IMT_SIZE; i++) {
1496 MonoImtBuilderEntry* entry = imt_builder [i];
1497 while (entry != NULL) {
1498 MonoImtBuilderEntry* next = entry->next;
1504 /* we OR the bitmap since we may build just a single imt slot at a time */
1505 vt->imt_collisions_bitmap |= imt_collisions_bitmap;
1509 build_imt (MonoClass *klass, MonoVTable *vt, MonoDomain *domain, gpointer* imt, GSList *extra_interfaces) {
1510 MONO_REQ_GC_NEUTRAL_MODE;
1512 build_imt_slots (klass, vt, domain, imt, extra_interfaces, -1);
1516 * mono_vtable_build_imt_slot:
1517 * @vtable: virtual object table struct
1518 * @imt_slot: slot in the IMT table
1520 * Fill the given @imt_slot in the IMT table of @vtable with
1521 * a trampoline or a thunk for the case of collisions.
1522 * This is part of the internal mono API.
1524 * LOCKING: Take the domain lock.
1527 mono_vtable_build_imt_slot (MonoVTable* vtable, int imt_slot)
1529 MONO_REQ_GC_NEUTRAL_MODE;
1531 gpointer *imt = (gpointer*)vtable;
1532 imt -= MONO_IMT_SIZE;
1533 g_assert (imt_slot >= 0 && imt_slot < MONO_IMT_SIZE);
1535 /* no support for extra interfaces: the proxy objects will need
1536 * to build the complete IMT
1537 * Update and heck needs to ahppen inside the proper domain lock, as all
1538 * the changes made to a MonoVTable.
1540 mono_loader_lock (); /*FIXME build_imt_slots requires the loader lock.*/
1541 mono_domain_lock (vtable->domain);
1542 /* we change the slot only if it wasn't changed from the generic imt trampoline already */
1543 if (!callbacks.imt_entry_inited (vtable, imt_slot))
1544 build_imt_slots (vtable->klass, vtable, vtable->domain, imt, NULL, imt_slot);
1545 mono_domain_unlock (vtable->domain);
1546 mono_loader_unlock ();
1551 * The first two free list entries both belong to the wait list: The
1552 * first entry is the pointer to the head of the list and the second
1553 * entry points to the last element. That way appending and removing
1554 * the first element are both O(1) operations.
1556 #ifdef MONO_SMALL_CONFIG
1557 #define NUM_FREE_LISTS 6
1559 #define NUM_FREE_LISTS 12
1561 #define FIRST_FREE_LIST_SIZE 64
1562 #define MAX_WAIT_LENGTH 50
1563 #define THUNK_THRESHOLD 10
1566 * LOCKING: The domain lock must be held.
1569 init_thunk_free_lists (MonoDomain *domain)
1571 MONO_REQ_GC_NEUTRAL_MODE;
1573 if (domain->thunk_free_lists)
1575 domain->thunk_free_lists = (MonoThunkFreeList **)mono_domain_alloc0 (domain, sizeof (gpointer) * NUM_FREE_LISTS);
1579 list_index_for_size (int item_size)
1582 int size = FIRST_FREE_LIST_SIZE;
1584 while (item_size > size && i < NUM_FREE_LISTS - 1) {
1593 * mono_method_alloc_generic_virtual_thunk:
1595 * @size: size in bytes
1597 * Allocs size bytes to be used for the code of a generic virtual
1598 * thunk. It's either allocated from the domain's code manager or
1599 * reused from a previously invalidated piece.
1601 * LOCKING: The domain lock must be held.
1604 mono_method_alloc_generic_virtual_thunk (MonoDomain *domain, int size)
1606 MONO_REQ_GC_NEUTRAL_MODE;
1608 static gboolean inited = FALSE;
1609 static int generic_virtual_thunks_size = 0;
1613 MonoThunkFreeList **l;
1615 init_thunk_free_lists (domain);
1617 size += sizeof (guint32);
1618 if (size < sizeof (MonoThunkFreeList))
1619 size = sizeof (MonoThunkFreeList);
1621 i = list_index_for_size (size);
1622 for (l = &domain->thunk_free_lists [i]; *l; l = &(*l)->next) {
1623 if ((*l)->size >= size) {
1624 MonoThunkFreeList *item = *l;
1626 return ((guint32*)item) + 1;
1630 /* no suitable item found - search lists of larger sizes */
1631 while (++i < NUM_FREE_LISTS) {
1632 MonoThunkFreeList *item = domain->thunk_free_lists [i];
1635 g_assert (item->size > size);
1636 domain->thunk_free_lists [i] = item->next;
1637 return ((guint32*)item) + 1;
1640 /* still nothing found - allocate it */
1642 mono_counters_register ("Generic virtual thunk bytes",
1643 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &generic_virtual_thunks_size);
1646 generic_virtual_thunks_size += size;
1648 p = (guint32 *)mono_domain_code_reserve (domain, size);
1651 mono_domain_lock (domain);
1652 if (!domain->generic_virtual_thunks)
1653 domain->generic_virtual_thunks = g_hash_table_new (NULL, NULL);
1654 g_hash_table_insert (domain->generic_virtual_thunks, p, p);
1655 mono_domain_unlock (domain);
1661 * LOCKING: The domain lock must be held.
1664 invalidate_generic_virtual_thunk (MonoDomain *domain, gpointer code)
1666 MONO_REQ_GC_NEUTRAL_MODE;
1668 guint32 *p = (guint32 *)code;
1669 MonoThunkFreeList *l = (MonoThunkFreeList*)(p - 1);
1670 gboolean found = FALSE;
1672 mono_domain_lock (domain);
1673 if (!domain->generic_virtual_thunks)
1674 domain->generic_virtual_thunks = g_hash_table_new (NULL, NULL);
1675 if (g_hash_table_lookup (domain->generic_virtual_thunks, l))
1677 mono_domain_unlock (domain);
1680 /* Not allocated by mono_method_alloc_generic_virtual_thunk (), i.e. AOT */
1682 init_thunk_free_lists (domain);
1684 while (domain->thunk_free_lists [0] && domain->thunk_free_lists [0]->length >= MAX_WAIT_LENGTH) {
1685 MonoThunkFreeList *item = domain->thunk_free_lists [0];
1686 int length = item->length;
1689 /* unlink the first item from the wait list */
1690 domain->thunk_free_lists [0] = item->next;
1691 domain->thunk_free_lists [0]->length = length - 1;
1693 i = list_index_for_size (item->size);
1695 /* put it in the free list */
1696 item->next = domain->thunk_free_lists [i];
1697 domain->thunk_free_lists [i] = item;
1701 if (domain->thunk_free_lists [1]) {
1702 domain->thunk_free_lists [1] = domain->thunk_free_lists [1]->next = l;
1703 domain->thunk_free_lists [0]->length++;
1705 g_assert (!domain->thunk_free_lists [0]);
1707 domain->thunk_free_lists [0] = domain->thunk_free_lists [1] = l;
1708 domain->thunk_free_lists [0]->length = 1;
1712 typedef struct _GenericVirtualCase {
1716 struct _GenericVirtualCase *next;
1717 } GenericVirtualCase;
1720 * get_generic_virtual_entries:
1722 * Return IMT entries for the generic virtual method instances and
1723 * variant interface methods for vtable slot
1726 static MonoImtBuilderEntry*
1727 get_generic_virtual_entries (MonoDomain *domain, gpointer *vtable_slot)
1729 MONO_REQ_GC_NEUTRAL_MODE;
1731 GenericVirtualCase *list;
1732 MonoImtBuilderEntry *entries;
1734 mono_domain_lock (domain);
1735 if (!domain->generic_virtual_cases)
1736 domain->generic_virtual_cases = g_hash_table_new (mono_aligned_addr_hash, NULL);
1738 list = (GenericVirtualCase *)g_hash_table_lookup (domain->generic_virtual_cases, vtable_slot);
1741 for (; list; list = list->next) {
1742 MonoImtBuilderEntry *entry;
1744 if (list->count < THUNK_THRESHOLD)
1747 entry = g_new0 (MonoImtBuilderEntry, 1);
1748 entry->key = list->method;
1749 entry->value.target_code = mono_get_addr_from_ftnptr (list->code);
1750 entry->has_target_code = 1;
1752 entry->children = entries->children + 1;
1753 entry->next = entries;
1757 mono_domain_unlock (domain);
1759 /* FIXME: Leaking memory ? */
1764 * mono_method_add_generic_virtual_invocation:
1766 * @vtable_slot: pointer to the vtable slot
1767 * @method: the inflated generic virtual method
1768 * @code: the method's code
1770 * Registers a call via unmanaged code to a generic virtual method
1771 * instantiation or variant interface method. If the number of calls reaches a threshold
1772 * (THUNK_THRESHOLD), the method is added to the vtable slot's generic
1773 * virtual method thunk.
1776 mono_method_add_generic_virtual_invocation (MonoDomain *domain, MonoVTable *vtable,
1777 gpointer *vtable_slot,
1778 MonoMethod *method, gpointer code)
1780 MONO_REQ_GC_NEUTRAL_MODE;
1782 static gboolean inited = FALSE;
1783 static int num_added = 0;
1785 GenericVirtualCase *gvc, *list;
1786 MonoImtBuilderEntry *entries;
1790 mono_domain_lock (domain);
1791 if (!domain->generic_virtual_cases)
1792 domain->generic_virtual_cases = g_hash_table_new (mono_aligned_addr_hash, NULL);
1794 /* Check whether the case was already added */
1795 list = (GenericVirtualCase *)g_hash_table_lookup (domain->generic_virtual_cases, vtable_slot);
1798 if (gvc->method == method)
1803 /* If not found, make a new one */
1805 gvc = (GenericVirtualCase *)mono_domain_alloc (domain, sizeof (GenericVirtualCase));
1806 gvc->method = method;
1809 gvc->next = (GenericVirtualCase *)g_hash_table_lookup (domain->generic_virtual_cases, vtable_slot);
1811 g_hash_table_insert (domain->generic_virtual_cases, vtable_slot, gvc);
1814 mono_counters_register ("Generic virtual cases", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_added);
1820 if (++gvc->count == THUNK_THRESHOLD) {
1821 gpointer *old_thunk = (void **)*vtable_slot;
1822 gpointer vtable_trampoline = NULL;
1823 gpointer imt_trampoline = NULL;
1825 if ((gpointer)vtable_slot < (gpointer)vtable) {
1826 int displacement = (gpointer*)vtable_slot - (gpointer*)vtable;
1827 int imt_slot = MONO_IMT_SIZE + displacement;
1829 /* Force the rebuild of the thunk at the next call */
1830 imt_trampoline = callbacks.get_imt_trampoline (vtable, imt_slot);
1831 *vtable_slot = imt_trampoline;
1833 vtable_trampoline = callbacks.get_vtable_trampoline ? callbacks.get_vtable_trampoline (vtable, (gpointer*)vtable_slot - (gpointer*)vtable->vtable) : NULL;
1835 entries = get_generic_virtual_entries (domain, vtable_slot);
1837 sorted = imt_sort_slot_entries (entries);
1839 *vtable_slot = imt_thunk_builder (NULL, domain, (MonoIMTCheckItem**)sorted->pdata, sorted->len,
1843 MonoImtBuilderEntry *next = entries->next;
1848 for (i = 0; i < sorted->len; ++i)
1849 g_free (g_ptr_array_index (sorted, i));
1850 g_ptr_array_free (sorted, TRUE);
1853 #ifndef __native_client__
1854 /* We don't re-use any thunks as there is a lot of overhead */
1855 /* to deleting and re-using code in Native Client. */
1856 if (old_thunk != vtable_trampoline && old_thunk != imt_trampoline)
1857 invalidate_generic_virtual_thunk (domain, old_thunk);
1861 mono_domain_unlock (domain);
1864 static MonoVTable *mono_class_create_runtime_vtable (MonoDomain *domain, MonoClass *klass, MonoError *error);
1867 * mono_class_vtable:
1868 * @domain: the application domain
1869 * @class: the class to initialize
1871 * VTables are domain specific because we create domain specific code, and
1872 * they contain the domain specific static class data.
1873 * On failure, NULL is returned, and class->exception_type is set.
1876 mono_class_vtable (MonoDomain *domain, MonoClass *klass)
1879 MonoVTable* vtable = mono_class_vtable_full (domain, klass, &error);
1880 mono_error_cleanup (&error);
1885 * mono_class_vtable_full:
1886 * @domain: the application domain
1887 * @class: the class to initialize
1888 * @error set on failure.
1890 * VTables are domain specific because we create domain specific code, and
1891 * they contain the domain specific static class data.
1894 mono_class_vtable_full (MonoDomain *domain, MonoClass *klass, MonoError *error)
1896 MONO_REQ_GC_UNSAFE_MODE;
1898 MonoClassRuntimeInfo *runtime_info;
1900 mono_error_init (error);
1904 if (mono_class_has_failure (klass)) {
1905 mono_error_set_exception_instance (error, mono_class_get_exception_for_failure (klass));
1909 /* this check can be inlined in jitted code, too */
1910 runtime_info = klass->runtime_info;
1911 if (runtime_info && runtime_info->max_domain >= domain->domain_id && runtime_info->domain_vtables [domain->domain_id])
1912 return runtime_info->domain_vtables [domain->domain_id];
1913 return mono_class_create_runtime_vtable (domain, klass, error);
1917 * mono_class_try_get_vtable:
1918 * @domain: the application domain
1919 * @class: the class to initialize
1921 * This function tries to get the associated vtable from @class if
1922 * it was already created.
1925 mono_class_try_get_vtable (MonoDomain *domain, MonoClass *klass)
1927 MONO_REQ_GC_NEUTRAL_MODE;
1929 MonoClassRuntimeInfo *runtime_info;
1933 runtime_info = klass->runtime_info;
1934 if (runtime_info && runtime_info->max_domain >= domain->domain_id && runtime_info->domain_vtables [domain->domain_id])
1935 return runtime_info->domain_vtables [domain->domain_id];
1940 alloc_vtable (MonoDomain *domain, size_t vtable_size, size_t imt_table_bytes)
1942 MONO_REQ_GC_NEUTRAL_MODE;
1944 size_t alloc_offset;
1947 * We want the pointer to the MonoVTable aligned to 8 bytes because SGen uses three
1948 * address bits. The IMT has an odd number of entries, however, so on 32 bits the
1949 * alignment will be off. In that case we allocate 4 more bytes and skip over them.
1951 if (sizeof (gpointer) == 4 && (imt_table_bytes & 7)) {
1952 g_assert ((imt_table_bytes & 7) == 4);
1959 return (gpointer*) ((char*)mono_domain_alloc0 (domain, vtable_size) + alloc_offset);
1963 mono_class_create_runtime_vtable (MonoDomain *domain, MonoClass *klass, MonoError *error)
1965 MONO_REQ_GC_UNSAFE_MODE;
1968 MonoClassRuntimeInfo *runtime_info, *old_info;
1969 MonoClassField *field;
1971 int i, vtable_slots;
1972 size_t imt_table_bytes;
1974 guint32 vtable_size, class_size;
1976 gpointer *interface_offsets;
1978 mono_error_init (error);
1980 mono_loader_lock (); /*FIXME mono_class_init acquires it*/
1981 mono_domain_lock (domain);
1982 runtime_info = klass->runtime_info;
1983 if (runtime_info && runtime_info->max_domain >= domain->domain_id && runtime_info->domain_vtables [domain->domain_id]) {
1984 mono_domain_unlock (domain);
1985 mono_loader_unlock ();
1986 return runtime_info->domain_vtables [domain->domain_id];
1988 if (!klass->inited || mono_class_has_failure (klass)) {
1989 if (!mono_class_init (klass) || mono_class_has_failure (klass)) {
1990 mono_domain_unlock (domain);
1991 mono_loader_unlock ();
1992 mono_error_set_exception_instance (error, mono_class_get_exception_for_failure (klass));
1997 /* Array types require that their element type be valid*/
1998 if (klass->byval_arg.type == MONO_TYPE_ARRAY || klass->byval_arg.type == MONO_TYPE_SZARRAY) {
1999 MonoClass *element_class = klass->element_class;
2000 if (!element_class->inited)
2001 mono_class_init (element_class);
2003 /*mono_class_init can leave the vtable layout to be lazily done and we can't afford this here*/
2004 if (!mono_class_has_failure (element_class) && !element_class->vtable_size)
2005 mono_class_setup_vtable (element_class);
2007 if (mono_class_has_failure (element_class)) {
2008 /*Can happen if element_class only got bad after mono_class_setup_vtable*/
2009 if (!mono_class_has_failure (klass))
2010 mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, NULL);
2011 mono_domain_unlock (domain);
2012 mono_loader_unlock ();
2013 mono_error_set_exception_instance (error, mono_class_get_exception_for_failure (klass));
2019 * For some classes, mono_class_init () already computed klass->vtable_size, and
2020 * that is all that is needed because of the vtable trampolines.
2022 if (!klass->vtable_size)
2023 mono_class_setup_vtable (klass);
2025 if (klass->generic_class && !klass->vtable)
2026 mono_class_check_vtable_constraints (klass, NULL);
2028 /* Initialize klass->has_finalize */
2029 mono_class_has_finalizer (klass);
2031 if (mono_class_has_failure (klass)) {
2032 mono_domain_unlock (domain);
2033 mono_loader_unlock ();
2034 mono_error_set_exception_instance (error, mono_class_get_exception_for_failure (klass));
2038 vtable_slots = klass->vtable_size;
2039 /* we add an additional vtable slot to store the pointer to static field data only when needed */
2040 class_size = mono_class_data_size (klass);
2044 if (klass->interface_offsets_count) {
2045 imt_table_bytes = sizeof (gpointer) * (MONO_IMT_SIZE);
2046 mono_stats.imt_number_of_tables++;
2047 mono_stats.imt_tables_size += imt_table_bytes;
2049 imt_table_bytes = 0;
2052 vtable_size = imt_table_bytes + MONO_SIZEOF_VTABLE + vtable_slots * sizeof (gpointer);
2054 mono_stats.used_class_count++;
2055 mono_stats.class_vtable_size += vtable_size;
2057 interface_offsets = alloc_vtable (domain, vtable_size, imt_table_bytes);
2058 vt = (MonoVTable*) ((char*)interface_offsets + imt_table_bytes);
2059 g_assert (!((gsize)vt & 7));
2062 vt->rank = klass->rank;
2063 vt->domain = domain;
2065 mono_class_compute_gc_descriptor (klass);
2067 * We can't use typed allocation in the non-root domains, since the
2068 * collector needs the GC descriptor stored in the vtable even after
2069 * the mempool containing the vtable is destroyed when the domain is
2070 * unloaded. An alternative might be to allocate vtables in the GC
2071 * heap, but this does not seem to work (it leads to crashes inside
2072 * libgc). If that approach is tried, two gc descriptors need to be
2073 * allocated for each class: one for the root domain, and one for all
2074 * other domains. The second descriptor should contain a bit for the
2075 * vtable field in MonoObject, since we can no longer assume the
2076 * vtable is reachable by other roots after the appdomain is unloaded.
2078 #ifdef HAVE_BOEHM_GC
2079 if (domain != mono_get_root_domain () && !mono_dont_free_domains)
2080 vt->gc_descr = MONO_GC_DESCRIPTOR_NULL;
2083 vt->gc_descr = klass->gc_descr;
2085 gc_bits = mono_gc_get_vtable_bits (klass);
2086 g_assert (!(gc_bits & ~((1 << MONO_VTABLE_AVAILABLE_GC_BITS) - 1)));
2088 vt->gc_bits = gc_bits;
2091 /* we store the static field pointer at the end of the vtable: vt->vtable [class->vtable_size] */
2092 if (klass->has_static_refs) {
2093 MonoGCDescriptor statics_gc_descr;
2095 gsize default_bitmap [4] = {0};
2098 bitmap = compute_class_bitmap (klass, default_bitmap, sizeof (default_bitmap) * 8, 0, &max_set, TRUE);
2099 /*g_print ("bitmap 0x%x for %s.%s (size: %d)\n", bitmap [0], klass->name_space, klass->name, class_size);*/
2100 statics_gc_descr = mono_gc_make_descr_from_bitmap (bitmap, max_set + 1);
2101 vt->vtable [klass->vtable_size] = mono_gc_alloc_fixed (class_size, statics_gc_descr, MONO_ROOT_SOURCE_STATIC, "managed static variables");
2102 mono_domain_add_class_static_data (domain, klass, vt->vtable [klass->vtable_size], NULL);
2103 if (bitmap != default_bitmap)
2106 vt->vtable [klass->vtable_size] = mono_domain_alloc0 (domain, class_size);
2108 vt->has_static_fields = TRUE;
2109 mono_stats.class_static_data_size += class_size;
2113 while ((field = mono_class_get_fields (klass, &iter))) {
2114 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
2116 if (mono_field_is_deleted (field))
2118 if (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL)) {
2119 gint32 special_static = klass->no_special_static_fields ? SPECIAL_STATIC_NONE : field_is_special_static (klass, field);
2120 if (special_static != SPECIAL_STATIC_NONE) {
2121 guint32 size, offset;
2123 gsize default_bitmap [4] = {0};
2128 if (mono_type_is_reference (field->type)) {
2129 default_bitmap [0] = 1;
2131 bitmap = default_bitmap;
2132 } else if (mono_type_is_struct (field->type)) {
2133 fclass = mono_class_from_mono_type (field->type);
2134 bitmap = compute_class_bitmap (fclass, default_bitmap, sizeof (default_bitmap) * 8, - (int)(sizeof (MonoObject) / sizeof (gpointer)), &max_set, FALSE);
2135 numbits = max_set + 1;
2137 default_bitmap [0] = 0;
2139 bitmap = default_bitmap;
2141 size = mono_type_size (field->type, &align);
2142 offset = mono_alloc_special_static_data (special_static, size, align, (uintptr_t*)bitmap, numbits);
2143 if (!domain->special_static_fields)
2144 domain->special_static_fields = g_hash_table_new (NULL, NULL);
2145 g_hash_table_insert (domain->special_static_fields, field, GUINT_TO_POINTER (offset));
2146 if (bitmap != default_bitmap)
2149 * This marks the field as special static to speed up the
2150 * checks in mono_field_static_get/set_value ().
2156 if ((field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA)) {
2157 MonoClass *fklass = mono_class_from_mono_type (field->type);
2158 const char *data = mono_field_get_data (field);
2160 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT));
2161 t = (char*)mono_vtable_get_static_field_data (vt) + field->offset;
2162 /* some fields don't really have rva, they are just zeroed (bss? bug #343083) */
2165 if (fklass->valuetype) {
2166 memcpy (t, data, mono_class_value_size (fklass, NULL));
2168 /* it's a pointer type: add check */
2169 g_assert ((fklass->byval_arg.type == MONO_TYPE_PTR) || (fklass->byval_arg.type == MONO_TYPE_FNPTR));
2176 vt->max_interface_id = klass->max_interface_id;
2177 vt->interface_bitmap = klass->interface_bitmap;
2179 //printf ("Initializing VT for class %s (interface_offsets_count = %d)\n",
2180 // class->name, klass->interface_offsets_count);
2182 /* Initialize vtable */
2183 if (callbacks.get_vtable_trampoline) {
2184 // This also covers the AOT case
2185 for (i = 0; i < klass->vtable_size; ++i) {
2186 vt->vtable [i] = callbacks.get_vtable_trampoline (vt, i);
2189 mono_class_setup_vtable (klass);
2191 for (i = 0; i < klass->vtable_size; ++i) {
2194 cm = klass->vtable [i];
2196 vt->vtable [i] = callbacks.create_jit_trampoline (domain, cm, error);
2197 if (!is_ok (error)) {
2198 mono_domain_unlock (domain);
2199 mono_loader_unlock ();
2206 if (imt_table_bytes) {
2207 /* Now that the vtable is full, we can actually fill up the IMT */
2208 for (i = 0; i < MONO_IMT_SIZE; ++i)
2209 interface_offsets [i] = callbacks.get_imt_trampoline (vt, i);
2213 * FIXME: Is it ok to allocate while holding the domain/loader locks ? If not, we can release them, allocate, then
2214 * re-acquire them and check if another thread has created the vtable in the meantime.
2216 /* Special case System.MonoType to avoid infinite recursion */
2217 if (klass != mono_defaults.monotype_class) {
2218 vt->type = mono_type_get_object_checked (domain, &klass->byval_arg, error);
2219 if (!is_ok (error)) {
2220 mono_domain_unlock (domain);
2221 mono_loader_unlock ();
2225 if (mono_object_get_class ((MonoObject *)vt->type) != mono_defaults.monotype_class)
2226 /* This is unregistered in
2227 unregister_vtable_reflection_type() in
2229 MONO_GC_REGISTER_ROOT_IF_MOVING(vt->type, MONO_ROOT_SOURCE_REFLECTION, "vtable reflection type");
2232 mono_vtable_set_is_remote (vt, mono_class_is_contextbound (klass));
2234 /* class_vtable_array keeps an array of created vtables
2236 g_ptr_array_add (domain->class_vtable_array, vt);
2237 /* klass->runtime_info is protected by the loader lock, both when
2238 * it it enlarged and when it is stored info.
2242 * Store the vtable in klass->runtime_info.
2243 * klass->runtime_info is accessed without locking, so this do this last after the vtable has been constructed.
2245 mono_memory_barrier ();
2247 old_info = klass->runtime_info;
2248 if (old_info && old_info->max_domain >= domain->domain_id) {
2249 /* someone already created a large enough runtime info */
2250 old_info->domain_vtables [domain->domain_id] = vt;
2252 int new_size = domain->domain_id;
2254 new_size = MAX (new_size, old_info->max_domain);
2256 /* make the new size a power of two */
2258 while (new_size > i)
2261 /* this is a bounded memory retention issue: may want to
2262 * handle it differently when we'll have a rcu-like system.
2264 runtime_info = (MonoClassRuntimeInfo *)mono_image_alloc0 (klass->image, MONO_SIZEOF_CLASS_RUNTIME_INFO + new_size * sizeof (gpointer));
2265 runtime_info->max_domain = new_size - 1;
2266 /* copy the stuff from the older info */
2268 memcpy (runtime_info->domain_vtables, old_info->domain_vtables, (old_info->max_domain + 1) * sizeof (gpointer));
2270 runtime_info->domain_vtables [domain->domain_id] = vt;
2272 mono_memory_barrier ();
2273 klass->runtime_info = runtime_info;
2276 if (klass == mono_defaults.monotype_class) {
2277 vt->type = mono_type_get_object_checked (domain, &klass->byval_arg, error);
2278 if (!is_ok (error)) {
2279 mono_domain_unlock (domain);
2280 mono_loader_unlock ();
2284 if (mono_object_get_class ((MonoObject *)vt->type) != mono_defaults.monotype_class)
2285 /* This is unregistered in
2286 unregister_vtable_reflection_type() in
2288 MONO_GC_REGISTER_ROOT_IF_MOVING(vt->type, MONO_ROOT_SOURCE_REFLECTION, "vtable reflection type");
2291 mono_domain_unlock (domain);
2292 mono_loader_unlock ();
2294 /* make sure the parent is initialized */
2295 /*FIXME shouldn't this fail the current type?*/
2297 mono_class_vtable_full (domain, klass->parent, error);
2302 #ifndef DISABLE_REMOTING
2304 * mono_class_proxy_vtable:
2305 * @domain: the application domain
2306 * @remove_class: the remote class
2308 * Creates a vtable for transparent proxies. It is basically
2309 * a copy of the real vtable of the class wrapped in @remote_class,
2310 * but all function pointers invoke the remoting functions, and
2311 * vtable->klass points to the transparent proxy class, and not to @class.
2314 mono_class_proxy_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, MonoRemotingTarget target_type)
2316 MONO_REQ_GC_UNSAFE_MODE;
2319 MonoVTable *vt, *pvt;
2320 int i, j, vtsize, max_interface_id, extra_interface_vtsize = 0;
2322 GSList *extra_interfaces = NULL;
2323 MonoClass *klass = remote_class->proxy_class;
2324 gpointer *interface_offsets;
2327 size_t imt_table_bytes;
2329 #ifdef COMPRESSED_INTERFACE_BITMAP
2333 vt = mono_class_vtable (domain, klass);
2334 g_assert (vt); /*FIXME property handle failure*/
2335 max_interface_id = vt->max_interface_id;
2337 /* Calculate vtable space for extra interfaces */
2338 for (j = 0; j < remote_class->interface_count; j++) {
2339 MonoClass* iclass = remote_class->interfaces[j];
2343 /*FIXME test for interfaces with variant generic arguments*/
2344 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, iclass->interface_id))
2345 continue; /* interface implemented by the class */
2346 if (g_slist_find (extra_interfaces, iclass))
2349 extra_interfaces = g_slist_prepend (extra_interfaces, iclass);
2351 method_count = mono_class_num_methods (iclass);
2353 ifaces = mono_class_get_implemented_interfaces (iclass, &error);
2354 g_assert (mono_error_ok (&error)); /*FIXME do proper error handling*/
2356 for (i = 0; i < ifaces->len; ++i) {
2357 MonoClass *ic = (MonoClass *)g_ptr_array_index (ifaces, i);
2358 /*FIXME test for interfaces with variant generic arguments*/
2359 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, ic->interface_id))
2360 continue; /* interface implemented by the class */
2361 if (g_slist_find (extra_interfaces, ic))
2363 extra_interfaces = g_slist_prepend (extra_interfaces, ic);
2364 method_count += mono_class_num_methods (ic);
2366 g_ptr_array_free (ifaces, TRUE);
2369 extra_interface_vtsize += method_count * sizeof (gpointer);
2370 if (iclass->max_interface_id > max_interface_id) max_interface_id = iclass->max_interface_id;
2373 imt_table_bytes = sizeof (gpointer) * MONO_IMT_SIZE;
2374 mono_stats.imt_number_of_tables++;
2375 mono_stats.imt_tables_size += imt_table_bytes;
2377 vtsize = imt_table_bytes + MONO_SIZEOF_VTABLE + klass->vtable_size * sizeof (gpointer);
2379 mono_stats.class_vtable_size += vtsize + extra_interface_vtsize;
2381 interface_offsets = alloc_vtable (domain, vtsize + extra_interface_vtsize, imt_table_bytes);
2382 pvt = (MonoVTable*) ((char*)interface_offsets + imt_table_bytes);
2383 g_assert (!((gsize)pvt & 7));
2385 memcpy (pvt, vt, MONO_SIZEOF_VTABLE + klass->vtable_size * sizeof (gpointer));
2387 pvt->klass = mono_defaults.transparent_proxy_class;
2388 /* we need to keep the GC descriptor for a transparent proxy or we confuse the precise GC */
2389 pvt->gc_descr = mono_defaults.transparent_proxy_class->gc_descr;
2391 /* initialize vtable */
2392 mono_class_setup_vtable (klass);
2393 for (i = 0; i < klass->vtable_size; ++i) {
2396 if ((cm = klass->vtable [i]))
2397 pvt->vtable [i] = arch_create_remoting_trampoline (domain, cm, target_type);
2399 pvt->vtable [i] = NULL;
2402 if (klass->flags & TYPE_ATTRIBUTE_ABSTRACT) {
2403 /* create trampolines for abstract methods */
2404 for (k = klass; k; k = k->parent) {
2406 gpointer iter = NULL;
2407 while ((m = mono_class_get_methods (k, &iter)))
2408 if (!pvt->vtable [m->slot])
2409 pvt->vtable [m->slot] = arch_create_remoting_trampoline (domain, m, target_type);
2413 pvt->max_interface_id = max_interface_id;
2414 bsize = sizeof (guint8) * (max_interface_id/8 + 1 );
2415 #ifdef COMPRESSED_INTERFACE_BITMAP
2416 bitmap = (uint8_t *)g_malloc0 (bsize);
2418 bitmap = (uint8_t *)mono_domain_alloc0 (domain, bsize);
2421 for (i = 0; i < klass->interface_offsets_count; ++i) {
2422 int interface_id = klass->interfaces_packed [i]->interface_id;
2423 bitmap [interface_id >> 3] |= (1 << (interface_id & 7));
2426 if (extra_interfaces) {
2427 int slot = klass->vtable_size;
2433 /* Create trampolines for the methods of the interfaces */
2434 for (list_item = extra_interfaces; list_item != NULL; list_item=list_item->next) {
2435 interf = (MonoClass *)list_item->data;
2437 bitmap [interf->interface_id >> 3] |= (1 << (interf->interface_id & 7));
2441 while ((cm = mono_class_get_methods (interf, &iter)))
2442 pvt->vtable [slot + j++] = arch_create_remoting_trampoline (domain, cm, target_type);
2444 slot += mono_class_num_methods (interf);
2448 /* Now that the vtable is full, we can actually fill up the IMT */
2449 build_imt (klass, pvt, domain, interface_offsets, extra_interfaces);
2450 if (extra_interfaces) {
2451 g_slist_free (extra_interfaces);
2454 #ifdef COMPRESSED_INTERFACE_BITMAP
2455 bcsize = mono_compress_bitmap (NULL, bitmap, bsize);
2456 pvt->interface_bitmap = mono_domain_alloc0 (domain, bcsize);
2457 mono_compress_bitmap (pvt->interface_bitmap, bitmap, bsize);
2460 pvt->interface_bitmap = bitmap;
2465 #endif /* DISABLE_REMOTING */
2468 * mono_class_field_is_special_static:
2470 * Returns whether @field is a thread/context static field.
2473 mono_class_field_is_special_static (MonoClassField *field)
2475 MONO_REQ_GC_NEUTRAL_MODE
2477 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
2479 if (mono_field_is_deleted (field))
2481 if (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL)) {
2482 if (field_is_special_static (field->parent, field) != SPECIAL_STATIC_NONE)
2489 * mono_class_field_get_special_static_type:
2490 * @field: The MonoClassField describing the field.
2492 * Returns: SPECIAL_STATIC_THREAD if the field is thread static, SPECIAL_STATIC_CONTEXT if it is context static,
2493 * SPECIAL_STATIC_NONE otherwise.
2496 mono_class_field_get_special_static_type (MonoClassField *field)
2498 MONO_REQ_GC_NEUTRAL_MODE
2500 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
2501 return SPECIAL_STATIC_NONE;
2502 if (mono_field_is_deleted (field))
2503 return SPECIAL_STATIC_NONE;
2504 if (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL))
2505 return field_is_special_static (field->parent, field);
2506 return SPECIAL_STATIC_NONE;
2510 * mono_class_has_special_static_fields:
2512 * Returns whenever @klass has any thread/context static fields.
2515 mono_class_has_special_static_fields (MonoClass *klass)
2517 MONO_REQ_GC_NEUTRAL_MODE
2519 MonoClassField *field;
2523 while ((field = mono_class_get_fields (klass, &iter))) {
2524 g_assert (field->parent == klass);
2525 if (mono_class_field_is_special_static (field))
2532 #ifndef DISABLE_REMOTING
2534 * create_remote_class_key:
2535 * Creates an array of pointers that can be used as a hash key for a remote class.
2536 * The first element of the array is the number of pointers.
2539 create_remote_class_key (MonoRemoteClass *remote_class, MonoClass *extra_class)
2541 MONO_REQ_GC_NEUTRAL_MODE;
2546 if (remote_class == NULL) {
2547 if (extra_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
2548 key = (void **)g_malloc (sizeof(gpointer) * 3);
2549 key [0] = GINT_TO_POINTER (2);
2550 key [1] = mono_defaults.marshalbyrefobject_class;
2551 key [2] = extra_class;
2553 key = (void **)g_malloc (sizeof(gpointer) * 2);
2554 key [0] = GINT_TO_POINTER (1);
2555 key [1] = extra_class;
2558 if (extra_class != NULL && (extra_class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
2559 key = (void **)g_malloc (sizeof(gpointer) * (remote_class->interface_count + 3));
2560 key [0] = GINT_TO_POINTER (remote_class->interface_count + 2);
2561 key [1] = remote_class->proxy_class;
2563 // Keep the list of interfaces sorted
2564 for (i = 0, j = 2; i < remote_class->interface_count; i++, j++) {
2565 if (extra_class && remote_class->interfaces [i] > extra_class) {
2566 key [j++] = extra_class;
2569 key [j] = remote_class->interfaces [i];
2572 key [j] = extra_class;
2574 // Replace the old class. The interface list is the same
2575 key = (void **)g_malloc (sizeof(gpointer) * (remote_class->interface_count + 2));
2576 key [0] = GINT_TO_POINTER (remote_class->interface_count + 1);
2577 key [1] = extra_class != NULL ? extra_class : remote_class->proxy_class;
2578 for (i = 0; i < remote_class->interface_count; i++)
2579 key [2 + i] = remote_class->interfaces [i];
2587 * copy_remote_class_key:
2589 * Make a copy of KEY in the domain and return the copy.
2592 copy_remote_class_key (MonoDomain *domain, gpointer *key)
2594 MONO_REQ_GC_NEUTRAL_MODE
2596 int key_size = (GPOINTER_TO_UINT (key [0]) + 1) * sizeof (gpointer);
2597 gpointer *mp_key = (gpointer *)mono_domain_alloc (domain, key_size);
2599 memcpy (mp_key, key, key_size);
2605 * mono_remote_class:
2606 * @domain: the application domain
2607 * @class_name: name of the remote class
2609 * Creates and initializes a MonoRemoteClass object for a remote type.
2611 * Can raise an exception on failure.
2614 mono_remote_class (MonoDomain *domain, MonoString *class_name, MonoClass *proxy_class)
2616 MONO_REQ_GC_UNSAFE_MODE;
2619 MonoRemoteClass *rc;
2620 gpointer* key, *mp_key;
2623 key = create_remote_class_key (NULL, proxy_class);
2625 mono_domain_lock (domain);
2626 rc = (MonoRemoteClass *)g_hash_table_lookup (domain->proxy_vtable_hash, key);
2630 mono_domain_unlock (domain);
2634 name = mono_string_to_utf8_mp (domain->mp, class_name, &error);
2635 if (!mono_error_ok (&error)) {
2637 mono_domain_unlock (domain);
2638 mono_error_raise_exception (&error);
2641 mp_key = copy_remote_class_key (domain, key);
2645 if (proxy_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
2646 rc = (MonoRemoteClass *)mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS + sizeof(MonoClass*));
2647 rc->interface_count = 1;
2648 rc->interfaces [0] = proxy_class;
2649 rc->proxy_class = mono_defaults.marshalbyrefobject_class;
2651 rc = (MonoRemoteClass *)mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS);
2652 rc->interface_count = 0;
2653 rc->proxy_class = proxy_class;
2656 rc->default_vtable = NULL;
2657 rc->xdomain_vtable = NULL;
2658 rc->proxy_class_name = name;
2659 #ifndef DISABLE_PERFCOUNTERS
2660 mono_perfcounters->loader_bytes += mono_string_length (class_name) + 1;
2663 g_hash_table_insert (domain->proxy_vtable_hash, key, rc);
2665 mono_domain_unlock (domain);
2670 * clone_remote_class:
2671 * Creates a copy of the remote_class, adding the provided class or interface
2673 static MonoRemoteClass*
2674 clone_remote_class (MonoDomain *domain, MonoRemoteClass* remote_class, MonoClass *extra_class)
2676 MONO_REQ_GC_NEUTRAL_MODE;
2678 MonoRemoteClass *rc;
2679 gpointer* key, *mp_key;
2681 key = create_remote_class_key (remote_class, extra_class);
2682 rc = (MonoRemoteClass *)g_hash_table_lookup (domain->proxy_vtable_hash, key);
2688 mp_key = copy_remote_class_key (domain, key);
2692 if (extra_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
2694 rc = (MonoRemoteClass *)mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS + sizeof(MonoClass*) * (remote_class->interface_count + 1));
2695 rc->proxy_class = remote_class->proxy_class;
2696 rc->interface_count = remote_class->interface_count + 1;
2698 // Keep the list of interfaces sorted, since the hash key of
2699 // the remote class depends on this
2700 for (i = 0, j = 0; i < remote_class->interface_count; i++, j++) {
2701 if (remote_class->interfaces [i] > extra_class && i == j)
2702 rc->interfaces [j++] = extra_class;
2703 rc->interfaces [j] = remote_class->interfaces [i];
2706 rc->interfaces [j] = extra_class;
2708 // Replace the old class. The interface array is the same
2709 rc = (MonoRemoteClass *)mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS + sizeof(MonoClass*) * remote_class->interface_count);
2710 rc->proxy_class = extra_class;
2711 rc->interface_count = remote_class->interface_count;
2712 if (rc->interface_count > 0)
2713 memcpy (rc->interfaces, remote_class->interfaces, rc->interface_count * sizeof (MonoClass*));
2716 rc->default_vtable = NULL;
2717 rc->xdomain_vtable = NULL;
2718 rc->proxy_class_name = remote_class->proxy_class_name;
2720 g_hash_table_insert (domain->proxy_vtable_hash, key, rc);
2726 mono_remote_class_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, MonoRealProxy *rp)
2728 MONO_REQ_GC_UNSAFE_MODE;
2730 mono_loader_lock (); /*FIXME mono_class_from_mono_type and mono_class_proxy_vtable take it*/
2731 mono_domain_lock (domain);
2732 if (rp->target_domain_id != -1) {
2733 if (remote_class->xdomain_vtable == NULL)
2734 remote_class->xdomain_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_APPDOMAIN);
2735 mono_domain_unlock (domain);
2736 mono_loader_unlock ();
2737 return remote_class->xdomain_vtable;
2739 if (remote_class->default_vtable == NULL) {
2742 type = ((MonoReflectionType *)rp->class_to_proxy)->type;
2743 klass = mono_class_from_mono_type (type);
2745 if ((mono_class_is_com_object (klass) || (mono_class_get_com_object_class () && klass == mono_class_get_com_object_class ())) && !mono_vtable_is_remote (mono_class_vtable (mono_domain_get (), klass)))
2746 remote_class->default_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_COMINTEROP);
2749 remote_class->default_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_UNKNOWN);
2752 mono_domain_unlock (domain);
2753 mono_loader_unlock ();
2754 return remote_class->default_vtable;
2758 * mono_upgrade_remote_class:
2759 * @domain: the application domain
2760 * @tproxy: the proxy whose remote class has to be upgraded.
2761 * @klass: class to which the remote class can be casted.
2763 * Updates the vtable of the remote class by adding the necessary method slots
2764 * and interface offsets so it can be safely casted to klass. klass can be a
2765 * class or an interface.
2768 mono_upgrade_remote_class (MonoDomain *domain, MonoObject *proxy_object, MonoClass *klass)
2770 MONO_REQ_GC_UNSAFE_MODE;
2772 MonoTransparentProxy *tproxy;
2773 MonoRemoteClass *remote_class;
2774 gboolean redo_vtable;
2776 mono_loader_lock (); /*FIXME mono_remote_class_vtable requires it.*/
2777 mono_domain_lock (domain);
2779 tproxy = (MonoTransparentProxy*) proxy_object;
2780 remote_class = tproxy->remote_class;
2782 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2785 for (i = 0; i < remote_class->interface_count && redo_vtable; i++)
2786 if (remote_class->interfaces [i] == klass)
2787 redo_vtable = FALSE;
2790 redo_vtable = (remote_class->proxy_class != klass);
2794 tproxy->remote_class = clone_remote_class (domain, remote_class, klass);
2795 proxy_object->vtable = (MonoVTable *)mono_remote_class_vtable (domain, tproxy->remote_class, tproxy->rp);
2798 mono_domain_unlock (domain);
2799 mono_loader_unlock ();
2801 #endif /* DISABLE_REMOTING */
2805 * mono_object_get_virtual_method:
2806 * @obj: object to operate on.
2809 * Retrieves the MonoMethod that would be called on obj if obj is passed as
2810 * the instance of a callvirt of method.
2813 mono_object_get_virtual_method (MonoObject *obj, MonoMethod *method)
2815 MONO_REQ_GC_UNSAFE_MODE;
2818 MonoMethod **vtable;
2819 gboolean is_proxy = FALSE;
2820 MonoMethod *res = NULL;
2822 klass = mono_object_class (obj);
2823 #ifndef DISABLE_REMOTING
2824 if (klass == mono_defaults.transparent_proxy_class) {
2825 klass = ((MonoTransparentProxy *)obj)->remote_class->proxy_class;
2830 if (!is_proxy && ((method->flags & METHOD_ATTRIBUTE_FINAL) || !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)))
2833 mono_class_setup_vtable (klass);
2834 vtable = klass->vtable;
2836 if (method->slot == -1) {
2837 /* method->slot might not be set for instances of generic methods */
2838 if (method->is_inflated) {
2839 g_assert (((MonoMethodInflated*)method)->declaring->slot != -1);
2840 method->slot = ((MonoMethodInflated*)method)->declaring->slot;
2843 g_assert_not_reached ();
2847 /* check method->slot is a valid index: perform isinstance? */
2848 if (method->slot != -1) {
2849 if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2851 gboolean variance_used = FALSE;
2852 int iface_offset = mono_class_interface_offset_with_variance (klass, method->klass, &variance_used);
2853 g_assert (iface_offset > 0);
2854 res = vtable [iface_offset + method->slot];
2857 res = vtable [method->slot];
2861 #ifndef DISABLE_REMOTING
2863 /* It may be an interface, abstract class method or generic method */
2864 if (!res || mono_method_signature (res)->generic_param_count)
2867 /* generic methods demand invoke_with_check */
2868 if (mono_method_signature (res)->generic_param_count)
2869 res = mono_marshal_get_remoting_invoke_with_check (res);
2872 if (klass == mono_class_get_com_object_class () || mono_class_is_com_object (klass))
2873 res = mono_cominterop_get_invoke (res);
2876 res = mono_marshal_get_remoting_invoke (res);
2881 if (method->is_inflated) {
2883 /* Have to inflate the result */
2884 res = mono_class_inflate_generic_method_checked (res, &((MonoMethodInflated*)method)->context, &error);
2885 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
2895 do_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
2897 MONO_REQ_GC_UNSAFE_MODE;
2899 MonoObject *result = NULL;
2901 g_assert (callbacks.runtime_invoke);
2903 mono_error_init (error);
2905 if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
2906 mono_profiler_method_start_invoke (method);
2908 MONO_PREPARE_RESET_BLOCKING;
2910 result = callbacks.runtime_invoke (method, obj, params, exc, error);
2912 MONO_FINISH_RESET_BLOCKING;
2914 if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
2915 mono_profiler_method_end_invoke (method);
2917 if (!mono_error_ok (error))
2924 * mono_runtime_invoke:
2925 * @method: method to invoke
2926 * @obJ: object instance
2927 * @params: arguments to the method
2928 * @exc: exception information.
2930 * Invokes the method represented by @method on the object @obj.
2932 * obj is the 'this' pointer, it should be NULL for static
2933 * methods, a MonoObject* for object instances and a pointer to
2934 * the value type for value types.
2936 * The params array contains the arguments to the method with the
2937 * same convention: MonoObject* pointers for object instances and
2938 * pointers to the value type otherwise.
2940 * From unmanaged code you'll usually use the
2941 * mono_runtime_invoke() variant.
2943 * Note that this function doesn't handle virtual methods for
2944 * you, it will exec the exact method you pass: we still need to
2945 * expose a function to lookup the derived class implementation
2946 * of a virtual method (there are examples of this in the code,
2949 * You can pass NULL as the exc argument if you don't want to
2950 * catch exceptions, otherwise, *exc will be set to the exception
2951 * thrown, if any. if an exception is thrown, you can't use the
2952 * MonoObject* result from the function.
2954 * If the method returns a value type, it is boxed in an object
2958 mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
2963 res = mono_runtime_try_invoke (method, obj, params, exc, &error);
2964 if (*exc == NULL && !mono_error_ok(&error)) {
2965 *exc = (MonoObject*) mono_error_convert_to_exception (&error);
2967 mono_error_cleanup (&error);
2969 res = mono_runtime_invoke_checked (method, obj, params, &error);
2970 mono_error_raise_exception (&error);
2976 * mono_runtime_try_invoke:
2977 * @method: method to invoke
2978 * @obJ: object instance
2979 * @params: arguments to the method
2980 * @exc: exception information.
2981 * @error: set on error
2983 * Invokes the method represented by @method on the object @obj.
2985 * obj is the 'this' pointer, it should be NULL for static
2986 * methods, a MonoObject* for object instances and a pointer to
2987 * the value type for value types.
2989 * The params array contains the arguments to the method with the
2990 * same convention: MonoObject* pointers for object instances and
2991 * pointers to the value type otherwise.
2993 * From unmanaged code you'll usually use the
2994 * mono_runtime_invoke() variant.
2996 * Note that this function doesn't handle virtual methods for
2997 * you, it will exec the exact method you pass: we still need to
2998 * expose a function to lookup the derived class implementation
2999 * of a virtual method (there are examples of this in the code,
3002 * For this function, you must not pass NULL as the exc argument if
3003 * you don't want to catch exceptions, use
3004 * mono_runtime_invoke_checked(). If an exception is thrown, you
3005 * can't use the MonoObject* result from the function.
3007 * If this method cannot be invoked, @error will be set and @exc and
3008 * the return value must not be used.
3010 * If the method returns a value type, it is boxed in an object
3014 mono_runtime_try_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError* error)
3016 MONO_REQ_GC_UNSAFE_MODE;
3018 g_assert (exc != NULL);
3020 if (mono_runtime_get_no_exec ())
3021 g_warning ("Invoking method '%s' when running in no-exec mode.\n", mono_method_full_name (method, TRUE));
3023 return do_runtime_invoke (method, obj, params, exc, error);
3027 * mono_runtime_invoke_checked:
3028 * @method: method to invoke
3029 * @obJ: object instance
3030 * @params: arguments to the method
3031 * @error: set on error
3033 * Invokes the method represented by @method on the object @obj.
3035 * obj is the 'this' pointer, it should be NULL for static
3036 * methods, a MonoObject* for object instances and a pointer to
3037 * the value type for value types.
3039 * The params array contains the arguments to the method with the
3040 * same convention: MonoObject* pointers for object instances and
3041 * pointers to the value type otherwise.
3043 * From unmanaged code you'll usually use the
3044 * mono_runtime_invoke() variant.
3046 * Note that this function doesn't handle virtual methods for
3047 * you, it will exec the exact method you pass: we still need to
3048 * expose a function to lookup the derived class implementation
3049 * of a virtual method (there are examples of this in the code,
3052 * If an exception is thrown, you can't use the MonoObject* result
3053 * from the function.
3055 * If this method cannot be invoked, @error will be set. If the
3056 * method throws an exception (and we're in coop mode) the exception
3057 * will be set in @error.
3059 * If the method returns a value type, it is boxed in an object
3063 mono_runtime_invoke_checked (MonoMethod *method, void *obj, void **params, MonoError* error)
3065 MONO_REQ_GC_UNSAFE_MODE;
3067 if (mono_runtime_get_no_exec ())
3068 g_warning ("Invoking method '%s' when running in no-exec mode.\n", mono_method_full_name (method, TRUE));
3070 return do_runtime_invoke (method, obj, params, NULL, error);
3074 * mono_method_get_unmanaged_thunk:
3075 * @method: method to generate a thunk for.
3077 * Returns an unmanaged->managed thunk that can be used to call
3078 * a managed method directly from C.
3080 * The thunk's C signature closely matches the managed signature:
3082 * C#: public bool Equals (object obj);
3083 * C: typedef MonoBoolean (*Equals)(MonoObject*,
3084 * MonoObject*, MonoException**);
3086 * The 1st ("this") parameter must not be used with static methods:
3088 * C#: public static bool ReferenceEquals (object a, object b);
3089 * C: typedef MonoBoolean (*ReferenceEquals)(MonoObject*, MonoObject*,
3092 * The last argument must be a non-null pointer of a MonoException* pointer.
3093 * It has "out" semantics. After invoking the thunk, *ex will be NULL if no
3094 * exception has been thrown in managed code. Otherwise it will point
3095 * to the MonoException* caught by the thunk. In this case, the result of
3096 * the thunk is undefined:
3098 * MonoMethod *method = ... // MonoMethod* of System.Object.Equals
3099 * MonoException *ex = NULL;
3100 * Equals func = mono_method_get_unmanaged_thunk (method);
3101 * MonoBoolean res = func (thisObj, objToCompare, &ex);
3103 * // handle exception
3106 * The calling convention of the thunk matches the platform's default
3107 * convention. This means that under Windows, C declarations must
3108 * contain the __stdcall attribute:
3110 * C: typedef MonoBoolean (__stdcall *Equals)(MonoObject*,
3111 * MonoObject*, MonoException**);
3115 * Value type arguments and return values are treated as they were objects:
3117 * C#: public static Rectangle Intersect (Rectangle a, Rectangle b);
3118 * C: typedef MonoObject* (*Intersect)(MonoObject*, MonoObject*, MonoException**);
3120 * Arguments must be properly boxed upon trunk's invocation, while return
3121 * values must be unboxed.
3124 mono_method_get_unmanaged_thunk (MonoMethod *method)
3126 MONO_REQ_GC_NEUTRAL_MODE;
3127 MONO_REQ_API_ENTRYPOINT;
3131 MONO_PREPARE_RESET_BLOCKING;
3132 method = mono_marshal_get_thunk_invoke_wrapper (method);
3133 res = mono_compile_method (method);
3134 MONO_FINISH_RESET_BLOCKING;
3140 mono_copy_value (MonoType *type, void *dest, void *value, int deref_pointer)
3142 MONO_REQ_GC_UNSAFE_MODE;
3146 /* object fields cannot be byref, so we don't need a
3148 gpointer *p = (gpointer*)dest;
3155 case MONO_TYPE_BOOLEAN:
3157 case MONO_TYPE_U1: {
3158 guint8 *p = (guint8*)dest;
3159 *p = value ? *(guint8*)value : 0;
3164 case MONO_TYPE_CHAR: {
3165 guint16 *p = (guint16*)dest;
3166 *p = value ? *(guint16*)value : 0;
3169 #if SIZEOF_VOID_P == 4
3174 case MONO_TYPE_U4: {
3175 gint32 *p = (gint32*)dest;
3176 *p = value ? *(gint32*)value : 0;
3179 #if SIZEOF_VOID_P == 8
3184 case MONO_TYPE_U8: {
3185 gint64 *p = (gint64*)dest;
3186 *p = value ? *(gint64*)value : 0;
3189 case MONO_TYPE_R4: {
3190 float *p = (float*)dest;
3191 *p = value ? *(float*)value : 0;
3194 case MONO_TYPE_R8: {
3195 double *p = (double*)dest;
3196 *p = value ? *(double*)value : 0;
3199 case MONO_TYPE_STRING:
3200 case MONO_TYPE_SZARRAY:
3201 case MONO_TYPE_CLASS:
3202 case MONO_TYPE_OBJECT:
3203 case MONO_TYPE_ARRAY:
3204 mono_gc_wbarrier_generic_store (dest, deref_pointer ? *(MonoObject **)value : (MonoObject *)value);
3206 case MONO_TYPE_FNPTR:
3207 case MONO_TYPE_PTR: {
3208 gpointer *p = (gpointer*)dest;
3209 *p = deref_pointer? *(gpointer*)value: value;
3212 case MONO_TYPE_VALUETYPE:
3213 /* note that 't' and 'type->type' can be different */
3214 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype) {
3215 t = mono_class_enum_basetype (type->data.klass)->type;
3218 MonoClass *klass = mono_class_from_mono_type (type);
3219 int size = mono_class_value_size (klass, NULL);
3221 mono_gc_bzero_atomic (dest, size);
3223 mono_gc_wbarrier_value_copy (dest, value, 1, klass);
3226 case MONO_TYPE_GENERICINST:
3227 t = type->data.generic_class->container_class->byval_arg.type;
3230 g_error ("got type %x", type->type);
3235 * mono_field_set_value:
3236 * @obj: Instance object
3237 * @field: MonoClassField describing the field to set
3238 * @value: The value to be set
3240 * Sets the value of the field described by @field in the object instance @obj
3241 * to the value passed in @value. This method should only be used for instance
3242 * fields. For static fields, use mono_field_static_set_value.
3244 * The value must be on the native format of the field type.
3247 mono_field_set_value (MonoObject *obj, MonoClassField *field, void *value)
3249 MONO_REQ_GC_UNSAFE_MODE;
3253 g_return_if_fail (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC));
3255 dest = (char*)obj + field->offset;
3256 mono_copy_value (field->type, dest, value, FALSE);
3260 * mono_field_static_set_value:
3261 * @field: MonoClassField describing the field to set
3262 * @value: The value to be set
3264 * Sets the value of the static field described by @field
3265 * to the value passed in @value.
3267 * The value must be on the native format of the field type.
3270 mono_field_static_set_value (MonoVTable *vt, MonoClassField *field, void *value)
3272 MONO_REQ_GC_UNSAFE_MODE;
3276 g_return_if_fail (field->type->attrs & FIELD_ATTRIBUTE_STATIC);
3277 /* you cant set a constant! */
3278 g_return_if_fail (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
3280 if (field->offset == -1) {
3281 /* Special static */
3284 mono_domain_lock (vt->domain);
3285 addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
3286 mono_domain_unlock (vt->domain);
3287 dest = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3289 dest = (char*)mono_vtable_get_static_field_data (vt) + field->offset;
3291 mono_copy_value (field->type, dest, value, FALSE);
3295 * mono_vtable_get_static_field_data:
3297 * Internal use function: return a pointer to the memory holding the static fields
3298 * for a class or NULL if there are no static fields.
3299 * This is exported only for use by the debugger.
3302 mono_vtable_get_static_field_data (MonoVTable *vt)
3304 MONO_REQ_GC_NEUTRAL_MODE
3306 if (!vt->has_static_fields)
3308 return vt->vtable [vt->klass->vtable_size];
3312 mono_field_get_addr (MonoObject *obj, MonoVTable *vt, MonoClassField *field)
3314 MONO_REQ_GC_UNSAFE_MODE;
3318 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
3319 if (field->offset == -1) {
3320 /* Special static */
3323 mono_domain_lock (vt->domain);
3324 addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
3325 mono_domain_unlock (vt->domain);
3326 src = (guint8 *)mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3328 src = (guint8*)mono_vtable_get_static_field_data (vt) + field->offset;
3331 src = (guint8*)obj + field->offset;
3338 * mono_field_get_value:
3339 * @obj: Object instance
3340 * @field: MonoClassField describing the field to fetch information from
3341 * @value: pointer to the location where the value will be stored
3343 * Use this routine to get the value of the field @field in the object
3346 * The pointer provided by value must be of the field type, for reference
3347 * types this is a MonoObject*, for value types its the actual pointer to
3352 * mono_field_get_value (obj, int_field, &i);
3355 mono_field_get_value (MonoObject *obj, MonoClassField *field, void *value)
3357 MONO_REQ_GC_UNSAFE_MODE;
3363 g_return_if_fail (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC));
3365 src = (char*)obj + field->offset;
3366 mono_copy_value (field->type, value, src, TRUE);
3370 * mono_field_get_value_object:
3371 * @domain: domain where the object will be created (if boxing)
3372 * @field: MonoClassField describing the field to fetch information from
3373 * @obj: The object instance for the field.
3375 * Returns: a new MonoObject with the value from the given field. If the
3376 * field represents a value type, the value is boxed.
3380 mono_field_get_value_object (MonoDomain *domain, MonoClassField *field, MonoObject *obj)
3382 MONO_REQ_GC_UNSAFE_MODE;
3387 MonoVTable *vtable = NULL;
3389 gboolean is_static = FALSE;
3390 gboolean is_ref = FALSE;
3391 gboolean is_literal = FALSE;
3392 gboolean is_ptr = FALSE;
3393 MonoType *type = mono_field_get_type_checked (field, &error);
3395 if (!mono_error_ok (&error))
3396 mono_error_raise_exception (&error); /* FIXME don't raise here */
3398 switch (type->type) {
3399 case MONO_TYPE_STRING:
3400 case MONO_TYPE_OBJECT:
3401 case MONO_TYPE_CLASS:
3402 case MONO_TYPE_ARRAY:
3403 case MONO_TYPE_SZARRAY:
3408 case MONO_TYPE_BOOLEAN:
3411 case MONO_TYPE_CHAR:
3420 case MONO_TYPE_VALUETYPE:
3421 is_ref = type->byref;
3423 case MONO_TYPE_GENERICINST:
3424 is_ref = !mono_type_generic_inst_is_valuetype (type);
3430 g_error ("type 0x%x not handled in "
3431 "mono_field_get_value_object", type->type);
3435 if (type->attrs & FIELD_ATTRIBUTE_LITERAL)
3438 if (type->attrs & FIELD_ATTRIBUTE_STATIC) {
3442 vtable = mono_class_vtable_full (domain, field->parent, &error);
3443 mono_error_raise_exception (&error); /* FIXME don't raise here */
3445 if (!vtable->initialized) {
3446 mono_runtime_class_init_full (vtable, &error);
3447 mono_error_raise_exception (&error); /* FIXME don't raise here */
3456 get_default_field_value (domain, field, &o);
3457 } else if (is_static) {
3458 mono_field_static_get_value (vtable, field, &o);
3460 mono_field_get_value (obj, field, &o);
3466 static MonoMethod *m;
3472 MonoClass *ptr_klass = mono_class_get_pointer_class ();
3473 m = mono_class_get_method_from_name_flags (ptr_klass, "Box", 2, METHOD_ATTRIBUTE_STATIC);
3479 get_default_field_value (domain, field, v);
3480 } else if (is_static) {
3481 mono_field_static_get_value (vtable, field, v);
3483 mono_field_get_value (obj, field, v);
3486 /* MONO_TYPE_PTR is passed by value to runtime_invoke () */
3487 args [0] = ptr ? *ptr : NULL;
3488 args [1] = mono_type_get_object_checked (mono_domain_get (), type, &error);
3489 mono_error_raise_exception (&error); /* FIXME don't raise here */
3491 o = mono_runtime_invoke_checked (m, NULL, args, &error);
3492 mono_error_raise_exception (&error); /* FIXME don't raise here */
3497 /* boxed value type */
3498 klass = mono_class_from_mono_type (type);
3500 if (mono_class_is_nullable (klass))
3501 return mono_nullable_box (mono_field_get_addr (obj, vtable, field), klass);
3503 o = mono_object_new_checked (domain, klass, &error);
3504 mono_error_raise_exception (&error); /* FIXME don't raise here */
3505 v = ((gchar *) o) + sizeof (MonoObject);
3508 get_default_field_value (domain, field, v);
3509 } else if (is_static) {
3510 mono_field_static_get_value (vtable, field, v);
3512 mono_field_get_value (obj, field, v);
3519 mono_get_constant_value_from_blob (MonoDomain* domain, MonoTypeEnum type, const char *blob, void *value)
3521 MONO_REQ_GC_UNSAFE_MODE;
3524 const char *p = blob;
3525 mono_metadata_decode_blob_size (p, &p);
3528 case MONO_TYPE_BOOLEAN:
3531 *(guint8 *) value = *p;
3533 case MONO_TYPE_CHAR:
3536 *(guint16*) value = read16 (p);
3540 *(guint32*) value = read32 (p);
3544 *(guint64*) value = read64 (p);
3547 readr4 (p, (float*) value);
3550 readr8 (p, (double*) value);
3552 case MONO_TYPE_STRING:
3553 *(gpointer*) value = mono_ldstr_metadata_sig (domain, blob);
3555 case MONO_TYPE_CLASS:
3556 *(gpointer*) value = NULL;
3560 g_warning ("type 0x%02x should not be in constant table", type);
3566 get_default_field_value (MonoDomain* domain, MonoClassField *field, void *value)
3568 MONO_REQ_GC_NEUTRAL_MODE;
3570 MonoTypeEnum def_type;
3573 data = mono_class_get_field_default_value (field, &def_type);
3574 mono_get_constant_value_from_blob (domain, def_type, data, value);
3578 mono_field_static_get_value_for_thread (MonoInternalThread *thread, MonoVTable *vt, MonoClassField *field, void *value)
3580 MONO_REQ_GC_UNSAFE_MODE;
3584 g_return_if_fail (field->type->attrs & FIELD_ATTRIBUTE_STATIC);
3586 if (field->type->attrs & FIELD_ATTRIBUTE_LITERAL) {
3587 get_default_field_value (vt->domain, field, value);
3591 if (field->offset == -1) {
3592 /* Special static */
3593 gpointer addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
3594 src = mono_get_special_static_data_for_thread (thread, GPOINTER_TO_UINT (addr));
3596 src = (char*)mono_vtable_get_static_field_data (vt) + field->offset;
3598 mono_copy_value (field->type, value, src, TRUE);
3602 * mono_field_static_get_value:
3603 * @vt: vtable to the object
3604 * @field: MonoClassField describing the field to fetch information from
3605 * @value: where the value is returned
3607 * Use this routine to get the value of the static field @field value.
3609 * The pointer provided by value must be of the field type, for reference
3610 * types this is a MonoObject*, for value types its the actual pointer to
3615 * mono_field_static_get_value (vt, int_field, &i);
3618 mono_field_static_get_value (MonoVTable *vt, MonoClassField *field, void *value)
3620 MONO_REQ_GC_NEUTRAL_MODE;
3622 mono_field_static_get_value_for_thread (mono_thread_internal_current (), vt, field, value);
3626 * mono_property_set_value:
3627 * @prop: MonoProperty to set
3628 * @obj: instance object on which to act
3629 * @params: parameters to pass to the propery
3630 * @exc: optional exception
3632 * Invokes the property's set method with the given arguments on the
3633 * object instance obj (or NULL for static properties).
3635 * You can pass NULL as the exc argument if you don't want to
3636 * catch exceptions, otherwise, *exc will be set to the exception
3637 * thrown, if any. if an exception is thrown, you can't use the
3638 * MonoObject* result from the function.
3641 mono_property_set_value (MonoProperty *prop, void *obj, void **params, MonoObject **exc)
3643 MONO_REQ_GC_UNSAFE_MODE;
3646 do_runtime_invoke (prop->set, obj, params, exc, &error);
3647 if (exc && *exc == NULL && !mono_error_ok (&error)) {
3648 *exc = (MonoObject*) mono_error_convert_to_exception (&error);
3650 mono_error_raise_exception (&error); /* FIXME don't raise here */
3655 * mono_property_get_value:
3656 * @prop: MonoProperty to fetch
3657 * @obj: instance object on which to act
3658 * @params: parameters to pass to the propery
3659 * @exc: optional exception
3661 * Invokes the property's get method with the given arguments on the
3662 * object instance obj (or NULL for static properties).
3664 * You can pass NULL as the exc argument if you don't want to
3665 * catch exceptions, otherwise, *exc will be set to the exception
3666 * thrown, if any. if an exception is thrown, you can't use the
3667 * MonoObject* result from the function.
3669 * Returns: the value from invoking the get method on the property.
3672 mono_property_get_value (MonoProperty *prop, void *obj, void **params, MonoObject **exc)
3674 MONO_REQ_GC_UNSAFE_MODE;
3677 MonoObject *val = do_runtime_invoke (prop->get, obj, params, exc, &error);
3678 if (exc && *exc == NULL && !mono_error_ok (&error)) {
3679 *exc = (MonoObject*) mono_error_convert_to_exception (&error);
3681 mono_error_raise_exception (&error); /* FIXME don't raise here */
3688 * mono_nullable_init:
3689 * @buf: The nullable structure to initialize.
3690 * @value: the value to initialize from
3691 * @klass: the type for the object
3693 * Initialize the nullable structure pointed to by @buf from @value which
3694 * should be a boxed value type. The size of @buf should be able to hold
3695 * as much data as the @klass->instance_size (which is the number of bytes
3696 * that will be copies).
3698 * Since Nullables have variable structure, we can not define a C
3699 * structure for them.
3702 mono_nullable_init (guint8 *buf, MonoObject *value, MonoClass *klass)
3704 MONO_REQ_GC_UNSAFE_MODE;
3706 MonoClass *param_class = klass->cast_class;
3708 mono_class_setup_fields_locking (klass);
3709 g_assert (klass->fields_inited);
3711 g_assert (mono_class_from_mono_type (klass->fields [0].type) == param_class);
3712 g_assert (mono_class_from_mono_type (klass->fields [1].type) == mono_defaults.boolean_class);
3714 *(guint8*)(buf + klass->fields [1].offset - sizeof (MonoObject)) = value ? 1 : 0;
3716 if (param_class->has_references)
3717 mono_gc_wbarrier_value_copy (buf + klass->fields [0].offset - sizeof (MonoObject), mono_object_unbox (value), 1, param_class);
3719 mono_gc_memmove_atomic (buf + klass->fields [0].offset - sizeof (MonoObject), mono_object_unbox (value), mono_class_value_size (param_class, NULL));
3721 mono_gc_bzero_atomic (buf + klass->fields [0].offset - sizeof (MonoObject), mono_class_value_size (param_class, NULL));
3726 * mono_nullable_box:
3727 * @buf: The buffer representing the data to be boxed
3728 * @klass: the type to box it as.
3730 * Creates a boxed vtype or NULL from the Nullable structure pointed to by
3734 mono_nullable_box (guint8 *buf, MonoClass *klass)
3736 MONO_REQ_GC_UNSAFE_MODE;
3740 MonoClass *param_class = klass->cast_class;
3742 mono_class_setup_fields_locking (klass);
3743 g_assert (klass->fields_inited);
3745 g_assert (mono_class_from_mono_type (klass->fields [0].type) == param_class);
3746 g_assert (mono_class_from_mono_type (klass->fields [1].type) == mono_defaults.boolean_class);
3748 if (*(guint8*)(buf + klass->fields [1].offset - sizeof (MonoObject))) {
3749 MonoObject *o = mono_object_new_checked (mono_domain_get (), param_class, &error);
3750 mono_error_raise_exception (&error); /* FIXME don't raise here */
3751 if (param_class->has_references)
3752 mono_gc_wbarrier_value_copy (mono_object_unbox (o), buf + klass->fields [0].offset - sizeof (MonoObject), 1, param_class);
3754 mono_gc_memmove_atomic (mono_object_unbox (o), buf + klass->fields [0].offset - sizeof (MonoObject), mono_class_value_size (param_class, NULL));
3762 * mono_get_delegate_invoke:
3763 * @klass: The delegate class
3765 * Returns: the MonoMethod for the "Invoke" method in the delegate klass or NULL if @klass is a broken delegate type
3768 mono_get_delegate_invoke (MonoClass *klass)
3770 MONO_REQ_GC_NEUTRAL_MODE;
3774 /* This is called at runtime, so avoid the slower search in metadata */
3775 mono_class_setup_methods (klass);
3776 if (mono_class_has_failure (klass))
3778 im = mono_class_get_method_from_name (klass, "Invoke", -1);
3783 * mono_get_delegate_begin_invoke:
3784 * @klass: The delegate class
3786 * Returns: the MonoMethod for the "BeginInvoke" method in the delegate klass or NULL if @klass is a broken delegate type
3789 mono_get_delegate_begin_invoke (MonoClass *klass)
3791 MONO_REQ_GC_NEUTRAL_MODE;
3795 /* This is called at runtime, so avoid the slower search in metadata */
3796 mono_class_setup_methods (klass);
3797 if (mono_class_has_failure (klass))
3799 im = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
3804 * mono_get_delegate_end_invoke:
3805 * @klass: The delegate class
3807 * Returns: the MonoMethod for the "EndInvoke" method in the delegate klass or NULL if @klass is a broken delegate type
3810 mono_get_delegate_end_invoke (MonoClass *klass)
3812 MONO_REQ_GC_NEUTRAL_MODE;
3816 /* This is called at runtime, so avoid the slower search in metadata */
3817 mono_class_setup_methods (klass);
3818 if (mono_class_has_failure (klass))
3820 im = mono_class_get_method_from_name (klass, "EndInvoke", -1);
3825 * mono_runtime_delegate_invoke:
3826 * @delegate: pointer to a delegate object.
3827 * @params: parameters for the delegate.
3828 * @exc: Pointer to the exception result.
3830 * Invokes the delegate method @delegate with the parameters provided.
3832 * You can pass NULL as the exc argument if you don't want to
3833 * catch exceptions, otherwise, *exc will be set to the exception
3834 * thrown, if any. if an exception is thrown, you can't use the
3835 * MonoObject* result from the function.
3838 mono_runtime_delegate_invoke (MonoObject *delegate, void **params, MonoObject **exc)
3840 MONO_REQ_GC_UNSAFE_MODE;
3844 MonoClass *klass = delegate->vtable->klass;
3847 im = mono_get_delegate_invoke (klass);
3849 g_error ("Could not lookup delegate invoke method for delegate %s", mono_type_get_full_name (klass));
3852 o = mono_runtime_try_invoke (im, delegate, params, exc, &error);
3853 if (*exc == NULL && !mono_error_ok (&error))
3854 *exc = (MonoObject*) mono_error_convert_to_exception (&error);
3856 mono_error_cleanup (&error);
3858 o = mono_runtime_invoke_checked (im, delegate, params, &error);
3859 mono_error_raise_exception (&error); /* FIXME don't raise here */
3865 static char **main_args = NULL;
3866 static int num_main_args = 0;
3869 * mono_runtime_get_main_args:
3871 * Returns: a MonoArray with the arguments passed to the main program
3874 mono_runtime_get_main_args (void)
3876 MONO_REQ_GC_UNSAFE_MODE;
3880 MonoDomain *domain = mono_domain_get ();
3882 res = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, num_main_args);
3884 for (i = 0; i < num_main_args; ++i)
3885 mono_array_setref (res, i, mono_string_new (domain, main_args [i]));
3891 free_main_args (void)
3893 MONO_REQ_GC_NEUTRAL_MODE;
3897 for (i = 0; i < num_main_args; ++i)
3898 g_free (main_args [i]);
3905 * mono_runtime_set_main_args:
3906 * @argc: number of arguments from the command line
3907 * @argv: array of strings from the command line
3909 * Set the command line arguments from an embedding application that doesn't otherwise call
3910 * mono_runtime_run_main ().
3913 mono_runtime_set_main_args (int argc, char* argv[])
3915 MONO_REQ_GC_NEUTRAL_MODE;
3920 main_args = g_new0 (char*, argc);
3921 num_main_args = argc;
3923 for (i = 0; i < argc; ++i) {
3926 utf8_arg = mono_utf8_from_external (argv[i]);
3927 if (utf8_arg == NULL) {
3928 g_print ("\nCannot determine the text encoding for argument %d (%s).\n", i, argv [i]);
3929 g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
3933 main_args [i] = utf8_arg;
3940 * mono_runtime_run_main:
3941 * @method: the method to start the application with (usually Main)
3942 * @argc: number of arguments from the command line
3943 * @argv: array of strings from the command line
3944 * @exc: excetption results
3946 * Execute a standard Main() method (argc/argv contains the
3947 * executable name). This method also sets the command line argument value
3948 * needed by System.Environment.
3953 mono_runtime_run_main (MonoMethod *method, int argc, char* argv[],
3956 MONO_REQ_GC_UNSAFE_MODE;
3959 MonoArray *args = NULL;
3960 MonoDomain *domain = mono_domain_get ();
3961 gchar *utf8_fullpath;
3962 MonoMethodSignature *sig;
3964 g_assert (method != NULL);
3966 mono_thread_set_main (mono_thread_current ());
3968 main_args = g_new0 (char*, argc);
3969 num_main_args = argc;
3971 if (!g_path_is_absolute (argv [0])) {
3972 gchar *basename = g_path_get_basename (argv [0]);
3973 gchar *fullpath = g_build_filename (method->klass->image->assembly->basedir,
3977 utf8_fullpath = mono_utf8_from_external (fullpath);
3978 if(utf8_fullpath == NULL) {
3979 /* Printing the arg text will cause glib to
3980 * whinge about "Invalid UTF-8", but at least
3981 * its relevant, and shows the problem text
3984 g_print ("\nCannot determine the text encoding for the assembly location: %s\n", fullpath);
3985 g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
3992 utf8_fullpath = mono_utf8_from_external (argv[0]);
3993 if(utf8_fullpath == NULL) {
3994 g_print ("\nCannot determine the text encoding for the assembly location: %s\n", argv[0]);
3995 g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
4000 main_args [0] = utf8_fullpath;
4002 for (i = 1; i < argc; ++i) {
4005 utf8_arg=mono_utf8_from_external (argv[i]);
4006 if(utf8_arg==NULL) {
4007 /* Ditto the comment about Invalid UTF-8 here */
4008 g_print ("\nCannot determine the text encoding for argument %d (%s).\n", i, argv[i]);
4009 g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
4013 main_args [i] = utf8_arg;
4018 sig = mono_method_signature (method);
4020 g_print ("Unable to load Main method.\n");
4024 if (sig->param_count) {
4025 args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, argc);
4026 for (i = 0; i < argc; ++i) {
4027 /* The encodings should all work, given that
4028 * we've checked all these args for the
4031 gchar *str = mono_utf8_from_external (argv [i]);
4032 MonoString *arg = mono_string_new (domain, str);
4033 mono_array_setref (args, i, arg);
4037 args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, 0);
4040 mono_assembly_set_main (method->klass->image->assembly);
4042 return mono_runtime_exec_main (method, args, exc);
4046 serialize_object (MonoObject *obj, gboolean *failure, MonoObject **exc)
4048 static MonoMethod *serialize_method;
4054 if (!serialize_method) {
4055 MonoClass *klass = mono_class_get_remoting_services_class ();
4056 serialize_method = mono_class_get_method_from_name (klass, "SerializeCallData", -1);
4059 if (!serialize_method) {
4064 g_assert (!mono_class_is_marshalbyref (mono_object_class (obj)));
4069 array = mono_runtime_try_invoke (serialize_method, NULL, params, exc, &error);
4070 if (*exc == NULL && !mono_error_ok (&error))
4071 *exc = (MonoObject*) mono_error_convert_to_exception (&error); /* FIXME convert serialize_object to MonoError */
4073 mono_error_cleanup (&error);
4082 deserialize_object (MonoObject *obj, gboolean *failure, MonoObject **exc)
4084 MONO_REQ_GC_UNSAFE_MODE;
4086 static MonoMethod *deserialize_method;
4092 if (!deserialize_method) {
4093 MonoClass *klass = mono_class_get_remoting_services_class ();
4094 deserialize_method = mono_class_get_method_from_name (klass, "DeserializeCallData", -1);
4096 if (!deserialize_method) {
4104 result = mono_runtime_try_invoke (deserialize_method, NULL, params, exc, &error);
4105 if (*exc == NULL && !mono_error_ok (&error))
4106 *exc = (MonoObject*) mono_error_convert_to_exception (&error); /* FIXME convert deserialize_object to MonoError */
4108 mono_error_cleanup (&error);
4116 #ifndef DISABLE_REMOTING
4118 make_transparent_proxy (MonoObject *obj, gboolean *failure, MonoObject **exc)
4120 MONO_REQ_GC_UNSAFE_MODE;
4122 static MonoMethod *get_proxy_method;
4125 MonoDomain *domain = mono_domain_get ();
4126 MonoRealProxy *real_proxy;
4127 MonoReflectionType *reflection_type;
4128 MonoTransparentProxy *transparent_proxy;
4130 if (!get_proxy_method)
4131 get_proxy_method = mono_class_get_method_from_name (mono_defaults.real_proxy_class, "GetTransparentProxy", 0);
4133 g_assert (mono_class_is_marshalbyref (obj->vtable->klass));
4135 real_proxy = (MonoRealProxy*) mono_object_new_checked (domain, mono_defaults.real_proxy_class, &error);
4136 mono_error_raise_exception (&error); /* FIXME don't raise here */
4137 reflection_type = mono_type_get_object_checked (domain, &obj->vtable->klass->byval_arg, &error);
4138 mono_error_raise_exception (&error); /* FIXME don't raise here */
4140 MONO_OBJECT_SETREF (real_proxy, class_to_proxy, reflection_type);
4141 MONO_OBJECT_SETREF (real_proxy, unwrapped_server, obj);
4145 transparent_proxy = (MonoTransparentProxy*) mono_runtime_try_invoke (get_proxy_method, real_proxy, NULL, exc, &error);
4146 if (*exc == NULL && !mono_error_ok (&error))
4147 *exc = (MonoObject*) mono_error_convert_to_exception (&error); /* FIXME change make_transparent_proxy outarg to MonoError */
4149 mono_error_cleanup (&error);
4153 return (MonoObject*) transparent_proxy;
4155 #endif /* DISABLE_REMOTING */
4158 * mono_object_xdomain_representation
4160 * @target_domain: a domain
4161 * @exc: pointer to a MonoObject*
4163 * Creates a representation of obj in the domain target_domain. This
4164 * is either a copy of obj arrived through via serialization and
4165 * deserialization or a proxy, depending on whether the object is
4166 * serializable or marshal by ref. obj must not be in target_domain.
4168 * If the object cannot be represented in target_domain, NULL is
4169 * returned and *exc is set to an appropriate exception.
4172 mono_object_xdomain_representation (MonoObject *obj, MonoDomain *target_domain, MonoObject **exc)
4174 MONO_REQ_GC_UNSAFE_MODE;
4176 MonoObject *deserialized = NULL;
4177 gboolean failure = FALSE;
4179 g_assert (exc != NULL);
4182 #ifndef DISABLE_REMOTING
4183 if (mono_class_is_marshalbyref (mono_object_class (obj))) {
4184 deserialized = make_transparent_proxy (obj, &failure, exc);
4189 MonoDomain *domain = mono_domain_get ();
4190 MonoObject *serialized;
4192 mono_domain_set_internal_with_options (mono_object_domain (obj), FALSE);
4193 serialized = serialize_object (obj, &failure, exc);
4194 mono_domain_set_internal_with_options (target_domain, FALSE);
4196 deserialized = deserialize_object (serialized, &failure, exc);
4197 if (domain != target_domain)
4198 mono_domain_set_internal_with_options (domain, FALSE);
4201 return deserialized;
4204 /* Used in call_unhandled_exception_delegate */
4206 create_unhandled_exception_eventargs (MonoObject *exc)
4208 MONO_REQ_GC_UNSAFE_MODE;
4213 MonoMethod *method = NULL;
4214 MonoBoolean is_terminating = TRUE;
4217 klass = mono_class_get_unhandled_exception_event_args_class ();
4218 mono_class_init (klass);
4220 /* UnhandledExceptionEventArgs only has 1 public ctor with 2 args */
4221 method = mono_class_get_method_from_name_flags (klass, ".ctor", 2, METHOD_ATTRIBUTE_PUBLIC);
4225 args [1] = &is_terminating;
4227 obj = mono_object_new_checked (mono_domain_get (), klass, &error);
4228 mono_error_raise_exception (&error); /* FIXME don't raise here */
4230 mono_runtime_invoke_checked (method, obj, args, &error);
4231 mono_error_raise_exception (&error); /* FIXME don't raise here */
4236 /* Used in mono_unhandled_exception */
4238 call_unhandled_exception_delegate (MonoDomain *domain, MonoObject *delegate, MonoObject *exc) {
4239 MONO_REQ_GC_UNSAFE_MODE;
4241 MonoObject *e = NULL;
4243 MonoDomain *current_domain = mono_domain_get ();
4245 if (domain != current_domain)
4246 mono_domain_set_internal_with_options (domain, FALSE);
4248 g_assert (domain == mono_object_domain (domain->domain));
4250 if (mono_object_domain (exc) != domain) {
4251 MonoObject *serialization_exc;
4253 exc = mono_object_xdomain_representation (exc, domain, &serialization_exc);
4255 if (serialization_exc) {
4257 exc = mono_object_xdomain_representation (serialization_exc, domain, &dummy);
4260 exc = (MonoObject*) mono_exception_from_name_msg (mono_get_corlib (),
4261 "System.Runtime.Serialization", "SerializationException",
4262 "Could not serialize unhandled exception.");
4266 g_assert (mono_object_domain (exc) == domain);
4268 pa [0] = domain->domain;
4269 pa [1] = create_unhandled_exception_eventargs (exc);
4270 mono_runtime_delegate_invoke (delegate, pa, &e);
4272 if (domain != current_domain)
4273 mono_domain_set_internal_with_options (current_domain, FALSE);
4277 gchar *msg = mono_string_to_utf8_checked (((MonoException *) e)->message, &error);
4278 if (!mono_error_ok (&error)) {
4279 g_warning ("Exception inside UnhandledException handler with invalid message (Invalid characters)\n");
4280 mono_error_cleanup (&error);
4282 g_warning ("exception inside UnhandledException handler: %s\n", msg);
4288 static MonoRuntimeUnhandledExceptionPolicy runtime_unhandled_exception_policy = MONO_UNHANDLED_POLICY_CURRENT;
4291 * mono_runtime_unhandled_exception_policy_set:
4292 * @policy: the new policy
4294 * This is a VM internal routine.
4296 * Sets the runtime policy for handling unhandled exceptions.
4299 mono_runtime_unhandled_exception_policy_set (MonoRuntimeUnhandledExceptionPolicy policy) {
4300 runtime_unhandled_exception_policy = policy;
4304 * mono_runtime_unhandled_exception_policy_get:
4306 * This is a VM internal routine.
4308 * Gets the runtime policy for handling unhandled exceptions.
4310 MonoRuntimeUnhandledExceptionPolicy
4311 mono_runtime_unhandled_exception_policy_get (void) {
4312 return runtime_unhandled_exception_policy;
4316 * mono_unhandled_exception:
4317 * @exc: exception thrown
4319 * This is a VM internal routine.
4321 * We call this function when we detect an unhandled exception
4322 * in the default domain.
4324 * It invokes the * UnhandledException event in AppDomain or prints
4325 * a warning to the console
4328 mono_unhandled_exception (MonoObject *exc)
4330 MONO_REQ_GC_UNSAFE_MODE;
4332 MonoClassField *field;
4333 MonoDomain *current_domain, *root_domain;
4334 MonoObject *current_appdomain_delegate = NULL, *root_appdomain_delegate = NULL;
4336 if (mono_class_has_parent (exc->vtable->klass, mono_defaults.threadabortexception_class))
4339 field = mono_class_get_field_from_name (mono_defaults.appdomain_class, "UnhandledException");
4342 current_domain = mono_domain_get ();
4343 root_domain = mono_get_root_domain ();
4345 root_appdomain_delegate = mono_field_get_value_object (root_domain, field, (MonoObject*) root_domain->domain);
4346 if (current_domain != root_domain)
4347 current_appdomain_delegate = mono_field_get_value_object (current_domain, field, (MonoObject*) current_domain->domain);
4349 if (!current_appdomain_delegate && !root_appdomain_delegate) {
4350 mono_print_unhandled_exception (exc);
4352 if (root_appdomain_delegate)
4353 call_unhandled_exception_delegate (root_domain, root_appdomain_delegate, exc);
4354 if (current_appdomain_delegate)
4355 call_unhandled_exception_delegate (current_domain, current_appdomain_delegate, exc);
4358 /* set exitcode only if we will abort the process */
4359 if ((main_thread && mono_thread_internal_current () == main_thread->internal_thread)
4360 || mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_CURRENT)
4362 mono_environment_exitcode_set (1);
4367 * mono_runtime_exec_managed_code:
4368 * @domain: Application domain
4369 * @main_func: function to invoke from the execution thread
4370 * @main_args: parameter to the main_func
4372 * Launch a new thread to execute a function
4374 * main_func is called back from the thread with main_args as the
4375 * parameter. The callback function is expected to start Main()
4376 * eventually. This function then waits for all managed threads to
4378 * It is not necesseray anymore to execute managed code in a subthread,
4379 * so this function should not be used anymore by default: just
4380 * execute the code and then call mono_thread_manage ().
4383 mono_runtime_exec_managed_code (MonoDomain *domain,
4384 MonoMainThreadFunc main_func,
4387 mono_thread_create (domain, main_func, main_args);
4389 mono_thread_manage ();
4393 * Execute a standard Main() method (args doesn't contain the
4397 mono_runtime_exec_main (MonoMethod *method, MonoArray *args, MonoObject **exc)
4399 MONO_REQ_GC_UNSAFE_MODE;
4405 MonoCustomAttrInfo* cinfo;
4406 gboolean has_stathread_attribute;
4407 MonoInternalThread* thread = mono_thread_internal_current ();
4413 domain = mono_object_domain (args);
4414 if (!domain->entry_assembly) {
4416 MonoAssembly *assembly;
4418 assembly = method->klass->image->assembly;
4419 domain->entry_assembly = assembly;
4420 /* Domains created from another domain already have application_base and configuration_file set */
4421 if (domain->setup->application_base == NULL) {
4422 MONO_OBJECT_SETREF (domain->setup, application_base, mono_string_new (domain, assembly->basedir));
4425 if (domain->setup->configuration_file == NULL) {
4426 str = g_strconcat (assembly->image->name, ".config", NULL);
4427 MONO_OBJECT_SETREF (domain->setup, configuration_file, mono_string_new (domain, str));
4429 mono_domain_set_options_from_config (domain);
4433 cinfo = mono_custom_attrs_from_method_checked (method, &error);
4434 mono_error_cleanup (&error); /* FIXME warn here? */
4436 has_stathread_attribute = mono_custom_attrs_has_attr (cinfo, mono_class_get_sta_thread_attribute_class ());
4438 mono_custom_attrs_free (cinfo);
4440 has_stathread_attribute = FALSE;
4442 if (has_stathread_attribute) {
4443 thread->apartment_state = ThreadApartmentState_STA;
4445 thread->apartment_state = ThreadApartmentState_MTA;
4447 mono_thread_init_apartment_state ();
4449 /* FIXME: check signature of method */
4450 if (mono_method_signature (method)->ret->type == MONO_TYPE_I4) {
4453 res = mono_runtime_try_invoke (method, NULL, pa, exc, &error);
4454 if (*exc == NULL && !mono_error_ok (&error))
4455 *exc = (MonoObject*) mono_error_convert_to_exception (&error);
4457 mono_error_cleanup (&error);
4459 res = mono_runtime_invoke_checked (method, NULL, pa, &error);
4460 mono_error_raise_exception (&error); /* FIXME don't raise here */
4464 rval = *(guint32 *)((char *)res + sizeof (MonoObject));
4468 mono_environment_exitcode_set (rval);
4471 mono_runtime_try_invoke (method, NULL, pa, exc, &error);
4472 if (*exc == NULL && !mono_error_ok (&error))
4473 *exc = (MonoObject*) mono_error_convert_to_exception (&error);
4475 mono_error_cleanup (&error);
4477 mono_runtime_invoke_checked (method, NULL, pa, &error);
4478 mono_error_raise_exception (&error); /* FIXME don't raise here */
4484 /* If the return type of Main is void, only
4485 * set the exitcode if an exception was thrown
4486 * (we don't want to blow away an
4487 * explicitly-set exit code)
4490 mono_environment_exitcode_set (rval);
4498 * mono_runtime_invoke_array:
4499 * @method: method to invoke
4500 * @obJ: object instance
4501 * @params: arguments to the method
4502 * @exc: exception information.
4504 * Invokes the method represented by @method on the object @obj.
4506 * obj is the 'this' pointer, it should be NULL for static
4507 * methods, a MonoObject* for object instances and a pointer to
4508 * the value type for value types.
4510 * The params array contains the arguments to the method with the
4511 * same convention: MonoObject* pointers for object instances and
4512 * pointers to the value type otherwise. The _invoke_array
4513 * variant takes a C# object[] as the params argument (MonoArray
4514 * *params): in this case the value types are boxed inside the
4515 * respective reference representation.
4517 * From unmanaged code you'll usually use the
4518 * mono_runtime_invoke_checked() variant.
4520 * Note that this function doesn't handle virtual methods for
4521 * you, it will exec the exact method you pass: we still need to
4522 * expose a function to lookup the derived class implementation
4523 * of a virtual method (there are examples of this in the code,
4526 * You can pass NULL as the exc argument if you don't want to
4527 * catch exceptions, otherwise, *exc will be set to the exception
4528 * thrown, if any. if an exception is thrown, you can't use the
4529 * MonoObject* result from the function.
4531 * If the method returns a value type, it is boxed in an object
4535 mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params,
4538 MONO_REQ_GC_UNSAFE_MODE;
4541 MonoMethodSignature *sig = mono_method_signature (method);
4542 gpointer *pa = NULL;
4545 gboolean has_byref_nullables = FALSE;
4547 if (NULL != params) {
4548 pa = (void **)alloca (sizeof (gpointer) * mono_array_length (params));
4549 for (i = 0; i < mono_array_length (params); i++) {
4550 MonoType *t = sig->params [i];
4556 case MONO_TYPE_BOOLEAN:
4559 case MONO_TYPE_CHAR:
4568 case MONO_TYPE_VALUETYPE:
4569 if (t->type == MONO_TYPE_VALUETYPE && mono_class_is_nullable (mono_class_from_mono_type (sig->params [i]))) {
4570 /* The runtime invoke wrapper needs the original boxed vtype, it does handle byref values as well. */
4571 pa [i] = mono_array_get (params, MonoObject*, i);
4573 has_byref_nullables = TRUE;
4575 /* MS seems to create the objects if a null is passed in */
4576 if (!mono_array_get (params, MonoObject*, i)) {
4577 MonoObject *o = mono_object_new_checked (mono_domain_get (), mono_class_from_mono_type (sig->params [i]), &error);
4578 mono_error_raise_exception (&error); /* FIXME don't raise here */
4579 mono_array_setref (params, i, o);
4584 * We can't pass the unboxed vtype byref to the callee, since
4585 * that would mean the callee would be able to modify boxed
4586 * primitive types. So we (and MS) make a copy of the boxed
4587 * object, pass that to the callee, and replace the original
4588 * boxed object in the arg array with the copy.
4590 MonoObject *orig = mono_array_get (params, MonoObject*, i);
4591 MonoObject *copy = mono_value_box (mono_domain_get (), orig->vtable->klass, mono_object_unbox (orig));
4592 mono_array_setref (params, i, copy);
4595 pa [i] = mono_object_unbox (mono_array_get (params, MonoObject*, i));
4598 case MONO_TYPE_STRING:
4599 case MONO_TYPE_OBJECT:
4600 case MONO_TYPE_CLASS:
4601 case MONO_TYPE_ARRAY:
4602 case MONO_TYPE_SZARRAY:
4604 pa [i] = mono_array_addr (params, MonoObject*, i);
4605 // FIXME: I need to check this code path
4607 pa [i] = mono_array_get (params, MonoObject*, i);
4609 case MONO_TYPE_GENERICINST:
4611 t = &t->data.generic_class->container_class->this_arg;
4613 t = &t->data.generic_class->container_class->byval_arg;
4615 case MONO_TYPE_PTR: {
4618 /* The argument should be an IntPtr */
4619 arg = mono_array_get (params, MonoObject*, i);
4623 g_assert (arg->vtable->klass == mono_defaults.int_class);
4624 pa [i] = ((MonoIntPtr*)arg)->m_value;
4629 g_error ("type 0x%x not handled in mono_runtime_invoke_array", sig->params [i]->type);
4634 if (!strcmp (method->name, ".ctor") && method->klass != mono_defaults.string_class) {
4637 if (mono_class_is_nullable (method->klass)) {
4638 /* Need to create a boxed vtype instead */
4644 return mono_value_box (mono_domain_get (), method->klass->cast_class, pa [0]);
4648 obj = mono_object_new_checked (mono_domain_get (), method->klass, &error);
4649 g_assert (obj && mono_error_ok (&error)); /*maybe we should raise a TLE instead?*/ /* FIXME don't swallow error */
4650 #ifndef DISABLE_REMOTING
4651 if (mono_object_class(obj) == mono_defaults.transparent_proxy_class) {
4652 method = mono_marshal_get_remoting_invoke (method->slot == -1 ? method : method->klass->vtable [method->slot]);
4655 if (method->klass->valuetype)
4656 o = (MonoObject *)mono_object_unbox ((MonoObject *)obj);
4659 } else if (method->klass->valuetype) {
4660 obj = mono_value_box (mono_domain_get (), method->klass, obj);
4664 mono_runtime_try_invoke (method, o, pa, exc, &error);
4665 if (*exc == NULL && !mono_error_ok (&error))
4666 *exc = (MonoObject*) mono_error_convert_to_exception (&error);
4668 mono_error_cleanup (&error);
4670 mono_runtime_invoke_checked (method, o, pa, &error);
4671 mono_error_raise_exception (&error); /* FIXME don't raise here */
4674 return (MonoObject *)obj;
4676 if (mono_class_is_nullable (method->klass)) {
4677 MonoObject *nullable;
4679 /* Convert the unboxed vtype into a Nullable structure */
4680 nullable = mono_object_new_checked (mono_domain_get (), method->klass, &error);
4681 mono_error_raise_exception (&error); /* FIXME don't raise here */
4683 mono_nullable_init ((guint8 *)mono_object_unbox (nullable), mono_value_box (mono_domain_get (), method->klass->cast_class, obj), method->klass);
4684 obj = mono_object_unbox (nullable);
4687 /* obj must be already unboxed if needed */
4689 res = mono_runtime_try_invoke (method, obj, pa, exc, &error);
4690 if (*exc == NULL && !mono_error_ok (&error))
4691 *exc = (MonoObject*) mono_error_convert_to_exception (&error);
4693 mono_error_cleanup (&error);
4695 res = mono_runtime_invoke_checked (method, obj, pa, &error);
4696 mono_error_raise_exception (&error); /* FIXME don't raise here */
4699 if (sig->ret->type == MONO_TYPE_PTR) {
4700 MonoClass *pointer_class;
4701 static MonoMethod *box_method;
4703 MonoObject *box_exc;
4706 * The runtime-invoke wrapper returns a boxed IntPtr, need to
4707 * convert it to a Pointer object.
4709 pointer_class = mono_class_get_pointer_class ();
4711 box_method = mono_class_get_method_from_name (pointer_class, "Box", -1);
4713 g_assert (res->vtable->klass == mono_defaults.int_class);
4714 box_args [0] = ((MonoIntPtr*)res)->m_value;
4715 box_args [1] = mono_type_get_object_checked (mono_domain_get (), sig->ret, &error);
4716 mono_error_raise_exception (&error); /* FIXME don't raise here */
4718 res = mono_runtime_try_invoke (box_method, NULL, box_args, &box_exc, &error);
4719 g_assert (box_exc == NULL);
4720 mono_error_assert_ok (&error);
4723 if (has_byref_nullables) {
4725 * The runtime invoke wrapper already converted byref nullables back,
4726 * and stored them in pa, we just need to copy them back to the
4729 for (i = 0; i < mono_array_length (params); i++) {
4730 MonoType *t = sig->params [i];
4732 if (t->byref && t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t)))
4733 mono_array_setref (params, i, pa [i]);
4743 * @klass: the class of the object that we want to create
4745 * Returns: a newly created object whose definition is
4746 * looked up using @klass. This will not invoke any constructors,
4747 * so the consumer of this routine has to invoke any constructors on
4748 * its own to initialize the object.
4750 * It returns NULL on failure.
4753 mono_object_new (MonoDomain *domain, MonoClass *klass)
4755 MONO_REQ_GC_UNSAFE_MODE;
4759 MonoObject * result = mono_object_new_checked (domain, klass, &error);
4761 mono_error_raise_exception (&error);
4766 ves_icall_object_new (MonoDomain *domain, MonoClass *klass)
4768 MONO_REQ_GC_UNSAFE_MODE;
4772 MonoObject * result = mono_object_new_checked (domain, klass, &error);
4774 mono_error_raise_exception (&error);
4779 * mono_object_new_checked:
4780 * @klass: the class of the object that we want to create
4781 * @error: set on error
4783 * Returns: a newly created object whose definition is
4784 * looked up using @klass. This will not invoke any constructors,
4785 * so the consumer of this routine has to invoke any constructors on
4786 * its own to initialize the object.
4788 * It returns NULL on failure and sets @error.
4791 mono_object_new_checked (MonoDomain *domain, MonoClass *klass, MonoError *error)
4793 MONO_REQ_GC_UNSAFE_MODE;
4797 vtable = mono_class_vtable (domain, klass);
4798 g_assert (vtable); /* FIXME don't swallow the error */
4800 MonoObject *o = mono_object_new_specific_checked (vtable, error);
4805 * mono_object_new_pinned:
4807 * Same as mono_object_new, but the returned object will be pinned.
4808 * For SGEN, these objects will only be freed at appdomain unload.
4811 mono_object_new_pinned (MonoDomain *domain, MonoClass *klass, MonoError *error)
4813 MONO_REQ_GC_UNSAFE_MODE;
4817 mono_error_init (error);
4819 vtable = mono_class_vtable (domain, klass);
4820 g_assert (vtable); /* FIXME don't swallow the error */
4822 MonoObject *o = (MonoObject *)mono_gc_alloc_pinned_obj (vtable, mono_class_instance_size (klass));
4824 if (G_UNLIKELY (!o))
4825 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", mono_class_instance_size (klass));
4826 else if (G_UNLIKELY (vtable->klass->has_finalize))
4827 mono_object_register_finalizer (o, error);
4833 * mono_object_new_specific:
4834 * @vtable: the vtable of the object that we want to create
4836 * Returns: A newly created object with class and domain specified
4840 mono_object_new_specific (MonoVTable *vtable)
4843 MonoObject *o = mono_object_new_specific_checked (vtable, &error);
4844 mono_error_raise_exception (&error);
4850 mono_object_new_specific_checked (MonoVTable *vtable, MonoError *error)
4852 MONO_REQ_GC_UNSAFE_MODE;
4856 mono_error_init (error);
4858 /* check for is_com_object for COM Interop */
4859 if (mono_vtable_is_remote (vtable) || mono_class_is_com_object (vtable->klass))
4862 MonoMethod *im = vtable->domain->create_proxy_for_type_method;
4865 MonoClass *klass = mono_class_get_activation_services_class ();
4868 mono_class_init (klass);
4870 im = mono_class_get_method_from_name (klass, "CreateProxyForType", 1);
4872 mono_error_set_generic_error (error, "System", "NotSupportedException", "Linked away.");
4875 vtable->domain->create_proxy_for_type_method = im;
4878 pa [0] = mono_type_get_object_checked (mono_domain_get (), &vtable->klass->byval_arg, error);
4879 if (!mono_error_ok (error))
4882 o = mono_runtime_invoke_checked (im, NULL, pa, error);
4883 if (!mono_error_ok (error))
4890 return mono_object_new_alloc_specific_checked (vtable, error);
4894 ves_icall_object_new_specific (MonoVTable *vtable)
4897 MonoObject *o = mono_object_new_specific_checked (vtable, &error);
4898 mono_error_raise_exception (&error);
4904 * mono_object_new_alloc_specific:
4905 * @vtable: virtual table for the object.
4907 * This function allocates a new `MonoObject` with the type derived
4908 * from the @vtable information. If the class of this object has a
4909 * finalizer, then the object will be tracked for finalization.
4911 * This method might raise an exception on errors. Use the
4912 * `mono_object_new_fast_checked` method if you want to manually raise
4915 * Returns: the allocated object.
4918 mono_object_new_alloc_specific (MonoVTable *vtable)
4921 MonoObject *o = mono_object_new_alloc_specific_checked (vtable, &error);
4922 mono_error_raise_exception (&error);
4928 * mono_object_new_alloc_specific_checked:
4929 * @vtable: virtual table for the object.
4930 * @error: holds the error return value.
4932 * This function allocates a new `MonoObject` with the type derived
4933 * from the @vtable information. If the class of this object has a
4934 * finalizer, then the object will be tracked for finalization.
4936 * If there is not enough memory, the @error parameter will be set
4937 * and will contain a user-visible message with the amount of bytes
4938 * that were requested.
4940 * Returns: the allocated object, or NULL if there is not enough memory
4944 mono_object_new_alloc_specific_checked (MonoVTable *vtable, MonoError *error)
4946 MONO_REQ_GC_UNSAFE_MODE;
4950 mono_error_init (error);
4952 o = (MonoObject *)mono_gc_alloc_obj (vtable, vtable->klass->instance_size);
4954 if (G_UNLIKELY (!o))
4955 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", vtable->klass->instance_size);
4956 else if (G_UNLIKELY (vtable->klass->has_finalize))
4957 mono_object_register_finalizer (o, error);
4963 * mono_object_new_fast:
4964 * @vtable: virtual table for the object.
4966 * This function allocates a new `MonoObject` with the type derived
4967 * from the @vtable information. The returned object is not tracked
4968 * for finalization. If your object implements a finalizer, you should
4969 * use `mono_object_new_alloc_specific` instead.
4971 * This method might raise an exception on errors. Use the
4972 * `mono_object_new_fast_checked` method if you want to manually raise
4975 * Returns: the allocated object.
4978 mono_object_new_fast (MonoVTable *vtable)
4981 MonoObject *o = mono_object_new_fast_checked (vtable, &error);
4982 mono_error_raise_exception (&error);
4988 * mono_object_new_fast_checked:
4989 * @vtable: virtual table for the object.
4990 * @error: holds the error return value.
4992 * This function allocates a new `MonoObject` with the type derived
4993 * from the @vtable information. The returned object is not tracked
4994 * for finalization. If your object implements a finalizer, you should
4995 * use `mono_object_new_alloc_specific_checked` instead.
4997 * If there is not enough memory, the @error parameter will be set
4998 * and will contain a user-visible message with the amount of bytes
4999 * that were requested.
5001 * Returns: the allocated object, or NULL if there is not enough memory
5005 mono_object_new_fast_checked (MonoVTable *vtable, MonoError *error)
5007 MONO_REQ_GC_UNSAFE_MODE;
5011 mono_error_init (error);
5013 o = mono_gc_alloc_obj (vtable, vtable->klass->instance_size);
5015 if (G_UNLIKELY (!o))
5016 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", vtable->klass->instance_size);
5022 ves_icall_object_new_fast (MonoVTable *vtable)
5025 MonoObject *o = mono_object_new_fast_checked (vtable, &error);
5026 mono_error_raise_exception (&error);
5032 mono_object_new_mature (MonoVTable *vtable, MonoError *error)
5034 MONO_REQ_GC_UNSAFE_MODE;
5038 mono_error_init (error);
5040 o = mono_gc_alloc_mature (vtable, vtable->klass->instance_size);
5042 if (G_UNLIKELY (!o))
5043 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", vtable->klass->instance_size);
5044 else if (G_UNLIKELY (vtable->klass->has_finalize))
5045 mono_object_register_finalizer (o, error);
5051 * mono_class_get_allocation_ftn:
5053 * @for_box: the object will be used for boxing
5054 * @pass_size_in_words:
5056 * Return the allocation function appropriate for the given class.
5060 mono_class_get_allocation_ftn (MonoVTable *vtable, gboolean for_box, gboolean *pass_size_in_words)
5062 MONO_REQ_GC_NEUTRAL_MODE;
5064 *pass_size_in_words = FALSE;
5066 if (mono_class_has_finalizer (vtable->klass) || mono_class_is_marshalbyref (vtable->klass) || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
5067 return ves_icall_object_new_specific;
5069 if (vtable->gc_descr != MONO_GC_DESCRIPTOR_NULL) {
5071 return ves_icall_object_new_fast;
5074 * FIXME: This is actually slower than ves_icall_object_new_fast, because
5075 * of the overhead of parameter passing.
5078 *pass_size_in_words = TRUE;
5079 #ifdef GC_REDIRECT_TO_LOCAL
5080 return GC_local_gcj_fast_malloc;
5082 return GC_gcj_fast_malloc;
5087 return ves_icall_object_new_specific;
5091 * mono_object_new_from_token:
5092 * @image: Context where the type_token is hosted
5093 * @token: a token of the type that we want to create
5095 * Returns: A newly created object whose definition is
5096 * looked up using @token in the @image image
5099 mono_object_new_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
5101 MONO_REQ_GC_UNSAFE_MODE;
5107 klass = mono_class_get_checked (image, token, &error);
5108 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
5110 result = mono_object_new_checked (domain, klass, &error);
5112 mono_error_raise_exception (&error); /* FIXME don't raise here */
5119 * mono_object_clone:
5120 * @obj: the object to clone
5122 * Returns: A newly created object who is a shallow copy of @obj
5125 mono_object_clone (MonoObject *obj)
5128 MonoObject *o = mono_object_clone_checked (obj, &error);
5129 mono_error_raise_exception (&error);
5135 mono_object_clone_checked (MonoObject *obj, MonoError *error)
5137 MONO_REQ_GC_UNSAFE_MODE;
5142 mono_error_init (error);
5144 size = obj->vtable->klass->instance_size;
5146 if (obj->vtable->klass->rank)
5147 return (MonoObject*)mono_array_clone ((MonoArray*)obj);
5149 o = (MonoObject *)mono_gc_alloc_obj (obj->vtable, size);
5151 if (G_UNLIKELY (!o)) {
5152 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", size);
5156 /* If the object doesn't contain references this will do a simple memmove. */
5157 mono_gc_wbarrier_object_copy (o, obj);
5159 if (obj->vtable->klass->has_finalize)
5160 mono_object_register_finalizer (o, error);
5165 * mono_array_full_copy:
5166 * @src: source array to copy
5167 * @dest: destination array
5169 * Copies the content of one array to another with exactly the same type and size.
5172 mono_array_full_copy (MonoArray *src, MonoArray *dest)
5174 MONO_REQ_GC_UNSAFE_MODE;
5177 MonoClass *klass = src->obj.vtable->klass;
5179 g_assert (klass == dest->obj.vtable->klass);
5181 size = mono_array_length (src);
5182 g_assert (size == mono_array_length (dest));
5183 size *= mono_array_element_size (klass);
5185 if (klass->element_class->valuetype) {
5186 if (klass->element_class->has_references)
5187 mono_value_copy_array (dest, 0, mono_array_addr_with_size_fast (src, 0, 0), mono_array_length (src));
5189 mono_gc_memmove_atomic (&dest->vector, &src->vector, size);
5191 mono_array_memcpy_refs (dest, 0, src, 0, mono_array_length (src));
5194 mono_gc_memmove_atomic (&dest->vector, &src->vector, size);
5199 * mono_array_clone_in_domain:
5200 * @domain: the domain in which the array will be cloned into
5201 * @array: the array to clone
5203 * This routine returns a copy of the array that is hosted on the
5204 * specified MonoDomain.
5207 mono_array_clone_in_domain (MonoDomain *domain, MonoArray *array)
5209 MONO_REQ_GC_UNSAFE_MODE;
5215 MonoClass *klass = array->obj.vtable->klass;
5217 if (array->bounds == NULL) {
5218 size = mono_array_length (array);
5219 o = mono_array_new_full_checked (domain, klass, &size, NULL, &error);
5220 mono_error_raise_exception (&error); /* FIXME don't raise here */
5222 size *= mono_array_element_size (klass);
5224 if (klass->element_class->valuetype) {
5225 if (klass->element_class->has_references)
5226 mono_value_copy_array (o, 0, mono_array_addr_with_size_fast (array, 0, 0), mono_array_length (array));
5228 mono_gc_memmove_atomic (&o->vector, &array->vector, size);
5230 mono_array_memcpy_refs (o, 0, array, 0, mono_array_length (array));
5233 mono_gc_memmove_atomic (&o->vector, &array->vector, size);
5238 sizes = (uintptr_t *)alloca (klass->rank * sizeof(intptr_t) * 2);
5239 size = mono_array_element_size (klass);
5240 for (i = 0; i < klass->rank; ++i) {
5241 sizes [i] = array->bounds [i].length;
5242 size *= array->bounds [i].length;
5243 sizes [i + klass->rank] = array->bounds [i].lower_bound;
5245 o = mono_array_new_full_checked (domain, klass, sizes, (intptr_t*)sizes + klass->rank, &error);
5246 mono_error_raise_exception (&error); /* FIXME don't raise here */
5248 if (klass->element_class->valuetype) {
5249 if (klass->element_class->has_references)
5250 mono_value_copy_array (o, 0, mono_array_addr_with_size_fast (array, 0, 0), mono_array_length (array));
5252 mono_gc_memmove_atomic (&o->vector, &array->vector, size);
5254 mono_array_memcpy_refs (o, 0, array, 0, mono_array_length (array));
5257 mono_gc_memmove_atomic (&o->vector, &array->vector, size);
5265 * @array: the array to clone
5267 * Returns: A newly created array who is a shallow copy of @array
5270 mono_array_clone (MonoArray *array)
5272 MONO_REQ_GC_UNSAFE_MODE;
5274 return mono_array_clone_in_domain (((MonoObject *)array)->vtable->domain, array);
5277 /* helper macros to check for overflow when calculating the size of arrays */
5278 #ifdef MONO_BIG_ARRAYS
5279 #define MYGUINT64_MAX 0x0000FFFFFFFFFFFFUL
5280 #define MYGUINT_MAX MYGUINT64_MAX
5281 #define CHECK_ADD_OVERFLOW_UN(a,b) \
5282 (G_UNLIKELY ((guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a)))
5283 #define CHECK_MUL_OVERFLOW_UN(a,b) \
5284 (G_UNLIKELY (((guint64)(a) > 0) && ((guint64)(b) > 0) && \
5285 ((guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a)))))
5287 #define MYGUINT32_MAX 4294967295U
5288 #define MYGUINT_MAX MYGUINT32_MAX
5289 #define CHECK_ADD_OVERFLOW_UN(a,b) \
5290 (G_UNLIKELY ((guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a)))
5291 #define CHECK_MUL_OVERFLOW_UN(a,b) \
5292 (G_UNLIKELY (((guint32)(a) > 0) && ((guint32)(b) > 0) && \
5293 ((guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a)))))
5297 mono_array_calc_byte_len (MonoClass *klass, uintptr_t len, uintptr_t *res)
5299 MONO_REQ_GC_NEUTRAL_MODE;
5303 byte_len = mono_array_element_size (klass);
5304 if (CHECK_MUL_OVERFLOW_UN (byte_len, len))
5307 if (CHECK_ADD_OVERFLOW_UN (byte_len, MONO_SIZEOF_MONO_ARRAY))
5309 byte_len += MONO_SIZEOF_MONO_ARRAY;
5317 * mono_array_new_full:
5318 * @domain: domain where the object is created
5319 * @array_class: array class
5320 * @lengths: lengths for each dimension in the array
5321 * @lower_bounds: lower bounds for each dimension in the array (may be NULL)
5323 * This routine creates a new array objects with the given dimensions,
5324 * lower bounds and type.
5327 mono_array_new_full (MonoDomain *domain, MonoClass *array_class, uintptr_t *lengths, intptr_t *lower_bounds)
5330 MonoArray *array = mono_array_new_full_checked (domain, array_class, lengths, lower_bounds, &error);
5331 mono_error_raise_exception (&error);
5337 mono_array_new_full_checked (MonoDomain *domain, MonoClass *array_class, uintptr_t *lengths, intptr_t *lower_bounds, MonoError *error)
5339 MONO_REQ_GC_UNSAFE_MODE;
5341 uintptr_t byte_len = 0, len, bounds_size;
5344 MonoArrayBounds *bounds;
5348 mono_error_init (error);
5350 if (!array_class->inited)
5351 mono_class_init (array_class);
5355 /* A single dimensional array with a 0 lower bound is the same as an szarray */
5356 if (array_class->rank == 1 && ((array_class->byval_arg.type == MONO_TYPE_SZARRAY) || (lower_bounds && lower_bounds [0] == 0))) {
5358 if (len > MONO_ARRAY_MAX_INDEX) {
5359 mono_error_set_generic_error (error, "System", "OverflowException", "");
5364 bounds_size = sizeof (MonoArrayBounds) * array_class->rank;
5366 for (i = 0; i < array_class->rank; ++i) {
5367 if (lengths [i] > MONO_ARRAY_MAX_INDEX) {
5368 mono_error_set_generic_error (error, "System", "OverflowException", "");
5371 if (CHECK_MUL_OVERFLOW_UN (len, lengths [i])) {
5372 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", MONO_ARRAY_MAX_SIZE);
5379 if (!mono_array_calc_byte_len (array_class, len, &byte_len)) {
5380 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", MONO_ARRAY_MAX_SIZE);
5386 if (CHECK_ADD_OVERFLOW_UN (byte_len, 3)) {
5387 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", MONO_ARRAY_MAX_SIZE);
5390 byte_len = (byte_len + 3) & ~3;
5391 if (CHECK_ADD_OVERFLOW_UN (byte_len, bounds_size)) {
5392 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", MONO_ARRAY_MAX_SIZE);
5395 byte_len += bounds_size;
5398 * Following three lines almost taken from mono_object_new ():
5399 * they need to be kept in sync.
5401 vtable = mono_class_vtable_full (domain, array_class, error);
5402 return_val_if_nok (error, NULL);
5405 o = (MonoObject *)mono_gc_alloc_array (vtable, byte_len, len, bounds_size);
5407 o = (MonoObject *)mono_gc_alloc_vector (vtable, byte_len, len);
5409 if (G_UNLIKELY (!o)) {
5410 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", byte_len);
5414 array = (MonoArray*)o;
5416 bounds = array->bounds;
5419 for (i = 0; i < array_class->rank; ++i) {
5420 bounds [i].length = lengths [i];
5422 bounds [i].lower_bound = lower_bounds [i];
5431 * @domain: domain where the object is created
5432 * @eclass: element class
5433 * @n: number of array elements
5435 * This routine creates a new szarray with @n elements of type @eclass.
5438 mono_array_new (MonoDomain *domain, MonoClass *eclass, uintptr_t n)
5440 MONO_REQ_GC_UNSAFE_MODE;
5446 ac = mono_array_class_get (eclass, 1);
5449 MonoVTable *vtable = mono_class_vtable_full (domain, ac, &error);
5450 mono_error_raise_exception (&error); /* FIXME don't raise here */
5452 arr = mono_array_new_specific_checked (vtable, n, &error);
5453 mono_error_raise_exception (&error); /* FIXME don't raise here */
5459 * mono_array_new_specific:
5460 * @vtable: a vtable in the appropriate domain for an initialized class
5461 * @n: number of array elements
5463 * This routine is a fast alternative to mono_array_new() for code which
5464 * can be sure about the domain it operates in.
5467 mono_array_new_specific (MonoVTable *vtable, uintptr_t n)
5470 MonoArray *arr = mono_array_new_specific_checked (vtable, n, &error);
5471 mono_error_raise_exception (&error); /* FIXME don't raise here */
5477 mono_array_new_specific_checked (MonoVTable *vtable, uintptr_t n, MonoError *error)
5479 MONO_REQ_GC_UNSAFE_MODE;
5484 mono_error_init (error);
5486 if (G_UNLIKELY (n > MONO_ARRAY_MAX_INDEX)) {
5487 mono_error_set_generic_error (error, "System", "OverflowException", "");
5491 if (!mono_array_calc_byte_len (vtable->klass, n, &byte_len)) {
5492 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", MONO_ARRAY_MAX_SIZE);
5495 o = (MonoObject *)mono_gc_alloc_vector (vtable, byte_len, n);
5497 if (G_UNLIKELY (!o)) {
5498 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", byte_len);
5502 return (MonoArray*)o;
5506 ves_icall_array_new_specific (MonoVTable *vtable, uintptr_t n)
5509 MonoArray *arr = mono_array_new_specific_checked (vtable, n, &error);
5510 mono_error_raise_exception (&error);
5516 * mono_string_new_utf16:
5517 * @text: a pointer to an utf16 string
5518 * @len: the length of the string
5520 * Returns: A newly created string object which contains @text.
5523 mono_string_new_utf16 (MonoDomain *domain, const guint16 *text, gint32 len)
5525 MONO_REQ_GC_UNSAFE_MODE;
5528 MonoString *res = NULL;
5529 res = mono_string_new_utf16_checked (domain, text, len, &error);
5530 mono_error_raise_exception (&error);
5536 * mono_string_new_utf16_checked:
5537 * @text: a pointer to an utf16 string
5538 * @len: the length of the string
5539 * @error: written on error.
5541 * Returns: A newly created string object which contains @text.
5542 * On error, returns NULL and sets @error.
5545 mono_string_new_utf16_checked (MonoDomain *domain, const guint16 *text, gint32 len, MonoError *error)
5547 MONO_REQ_GC_UNSAFE_MODE;
5551 mono_error_init (error);
5553 s = mono_string_new_size_checked (domain, len, error);
5555 memcpy (mono_string_chars (s), text, len * 2);
5561 * mono_string_new_utf32:
5562 * @text: a pointer to an utf32 string
5563 * @len: the length of the string
5565 * Returns: A newly created string object which contains @text.
5568 mono_string_new_utf32 (MonoDomain *domain, const mono_unichar4 *text, gint32 len)
5570 MONO_REQ_GC_UNSAFE_MODE;
5574 mono_unichar2 *utf16_output = NULL;
5575 gint32 utf16_len = 0;
5576 GError *gerror = NULL;
5577 glong items_written;
5579 utf16_output = g_ucs4_to_utf16 (text, len, NULL, &items_written, &gerror);
5582 g_error_free (gerror);
5584 while (utf16_output [utf16_len]) utf16_len++;
5586 s = mono_string_new_size_checked (domain, utf16_len, &error);
5587 mono_error_raise_exception (&error); /* FIXME don't raise here */
5589 memcpy (mono_string_chars (s), utf16_output, utf16_len * 2);
5591 g_free (utf16_output);
5597 * mono_string_new_size:
5598 * @text: a pointer to an utf16 string
5599 * @len: the length of the string
5601 * Returns: A newly created string object of @len
5604 mono_string_new_size (MonoDomain *domain, gint32 len)
5607 MonoString *str = mono_string_new_size_checked (domain, len, &error);
5608 mono_error_raise_exception (&error);
5614 mono_string_new_size_checked (MonoDomain *domain, gint32 len, MonoError *error)
5616 MONO_REQ_GC_UNSAFE_MODE;
5622 mono_error_init (error);
5624 /* check for overflow */
5625 if (len < 0 || len > ((SIZE_MAX - G_STRUCT_OFFSET (MonoString, chars) - 8) / 2)) {
5626 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", -1);
5630 size = (G_STRUCT_OFFSET (MonoString, chars) + (((size_t)len + 1) * 2));
5631 g_assert (size > 0);
5633 vtable = mono_class_vtable (domain, mono_defaults.string_class);
5636 s = (MonoString *)mono_gc_alloc_string (vtable, size, len);
5638 if (G_UNLIKELY (!s)) {
5639 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", size);
5647 * mono_string_new_len:
5648 * @text: a pointer to an utf8 string
5649 * @length: number of bytes in @text to consider
5651 * Returns: A newly created string object which contains @text.
5654 mono_string_new_len (MonoDomain *domain, const char *text, guint length)
5656 MONO_REQ_GC_UNSAFE_MODE;
5659 GError *eg_error = NULL;
5660 MonoString *o = NULL;
5662 glong items_written;
5664 mono_error_init (&error);
5666 ut = eg_utf8_to_utf16_with_nuls (text, length, NULL, &items_written, &eg_error);
5669 o = mono_string_new_utf16_checked (domain, ut, items_written, &error);
5671 g_error_free (eg_error);
5675 mono_error_raise_exception (&error); /* FIXME don't raise here */
5681 * @text: a pointer to an utf8 string
5683 * Returns: A newly created string object which contains @text.
5685 * This function asserts if it cannot allocate a new string.
5687 * @deprecated Use mono_string_new_checked in new code.
5690 mono_string_new (MonoDomain *domain, const char *text)
5693 MonoString *res = NULL;
5694 res = mono_string_new_checked (domain, text, &error);
5695 mono_error_assert_ok (&error);
5700 * mono_string_new_checked:
5701 * @text: a pointer to an utf8 string
5702 * @merror: set on error
5704 * Returns: A newly created string object which contains @text.
5705 * On error returns NULL and sets @merror.
5708 mono_string_new_checked (MonoDomain *domain, const char *text, MonoError *error)
5710 MONO_REQ_GC_UNSAFE_MODE;
5712 GError *eg_error = NULL;
5713 MonoString *o = NULL;
5715 glong items_written;
5718 mono_error_init (error);
5722 ut = g_utf8_to_utf16 (text, l, NULL, &items_written, &eg_error);
5725 o = mono_string_new_utf16_checked (domain, ut, items_written, error);
5727 g_error_free (eg_error);
5730 mono_error_raise_exception (error);
5732 /*FIXME g_utf8_get_char, g_utf8_next_char and g_utf8_validate are not part of eglib.*/
5737 MonoString *o = NULL;
5739 if (!g_utf8_validate (text, -1, &end)) {
5740 mono_error_set_argument (error, "text", "Not a valid utf8 string");
5744 len = g_utf8_strlen (text, -1);
5745 o = mono_string_new_size_checked (domain, len, error);
5748 str = mono_string_chars (o);
5750 while (text < end) {
5751 *str++ = g_utf8_get_char (text);
5752 text = g_utf8_next_char (text);
5761 * mono_string_new_wrapper:
5762 * @text: pointer to utf8 characters.
5764 * Helper function to create a string object from @text in the current domain.
5767 mono_string_new_wrapper (const char *text)
5769 MONO_REQ_GC_UNSAFE_MODE;
5771 MonoDomain *domain = mono_domain_get ();
5774 return mono_string_new (domain, text);
5781 * @class: the class of the value
5782 * @value: a pointer to the unboxed data
5784 * Returns: A newly created object which contains @value.
5787 mono_value_box (MonoDomain *domain, MonoClass *klass, gpointer value)
5789 MONO_REQ_GC_UNSAFE_MODE;
5796 g_assert (klass->valuetype);
5797 if (mono_class_is_nullable (klass))
5798 return mono_nullable_box ((guint8 *)value, klass);
5800 vtable = mono_class_vtable (domain, klass);
5803 size = mono_class_instance_size (klass);
5804 res = mono_object_new_alloc_specific_checked (vtable, &error);
5805 mono_error_raise_exception (&error); /* FIXME don't raise here */
5807 size = size - sizeof (MonoObject);
5810 g_assert (size == mono_class_value_size (klass, NULL));
5811 mono_gc_wbarrier_value_copy ((char *)res + sizeof (MonoObject), value, 1, klass);
5813 #if NO_UNALIGNED_ACCESS
5814 mono_gc_memmove_atomic ((char *)res + sizeof (MonoObject), value, size);
5818 *((guint8 *) res + sizeof (MonoObject)) = *(guint8 *) value;
5821 *(guint16 *)((guint8 *) res + sizeof (MonoObject)) = *(guint16 *) value;
5824 *(guint32 *)((guint8 *) res + sizeof (MonoObject)) = *(guint32 *) value;
5827 *(guint64 *)((guint8 *) res + sizeof (MonoObject)) = *(guint64 *) value;
5830 mono_gc_memmove_atomic ((char *)res + sizeof (MonoObject), value, size);
5834 if (klass->has_finalize) {
5835 mono_object_register_finalizer (res, &error);
5836 mono_error_raise_exception (&error); /* FIXME don't raise here */
5843 * @dest: destination pointer
5844 * @src: source pointer
5845 * @klass: a valuetype class
5847 * Copy a valuetype from @src to @dest. This function must be used
5848 * when @klass contains references fields.
5851 mono_value_copy (gpointer dest, gpointer src, MonoClass *klass)
5853 MONO_REQ_GC_UNSAFE_MODE;
5855 mono_gc_wbarrier_value_copy (dest, src, 1, klass);
5859 * mono_value_copy_array:
5860 * @dest: destination array
5861 * @dest_idx: index in the @dest array
5862 * @src: source pointer
5863 * @count: number of items
5865 * Copy @count valuetype items from @src to the array @dest at index @dest_idx.
5866 * This function must be used when @klass contains references fields.
5867 * Overlap is handled.
5870 mono_value_copy_array (MonoArray *dest, int dest_idx, gpointer src, int count)
5872 MONO_REQ_GC_UNSAFE_MODE;
5874 int size = mono_array_element_size (dest->obj.vtable->klass);
5875 char *d = mono_array_addr_with_size_fast (dest, size, dest_idx);
5876 g_assert (size == mono_class_value_size (mono_object_class (dest)->element_class, NULL));
5877 mono_gc_wbarrier_value_copy (d, src, count, mono_object_class (dest)->element_class);
5881 * mono_object_get_domain:
5882 * @obj: object to query
5884 * Returns: the MonoDomain where the object is hosted
5887 mono_object_get_domain (MonoObject *obj)
5889 MONO_REQ_GC_UNSAFE_MODE;
5891 return mono_object_domain (obj);
5895 * mono_object_get_class:
5896 * @obj: object to query
5898 * Use this function to obtain the `MonoClass*` for a given `MonoObject`.
5900 * Returns: the MonoClass of the object.
5903 mono_object_get_class (MonoObject *obj)
5905 MONO_REQ_GC_UNSAFE_MODE;
5907 return mono_object_class (obj);
5910 * mono_object_get_size:
5911 * @o: object to query
5913 * Returns: the size, in bytes, of @o
5916 mono_object_get_size (MonoObject* o)
5918 MONO_REQ_GC_UNSAFE_MODE;
5920 MonoClass* klass = mono_object_class (o);
5921 if (klass == mono_defaults.string_class) {
5922 return sizeof (MonoString) + 2 * mono_string_length ((MonoString*) o) + 2;
5923 } else if (o->vtable->rank) {
5924 MonoArray *array = (MonoArray*)o;
5925 size_t size = MONO_SIZEOF_MONO_ARRAY + mono_array_element_size (klass) * mono_array_length (array);
5926 if (array->bounds) {
5929 size += sizeof (MonoArrayBounds) * o->vtable->rank;
5933 return mono_class_instance_size (klass);
5938 * mono_object_unbox:
5939 * @obj: object to unbox
5941 * Returns: a pointer to the start of the valuetype boxed in this
5944 * This method will assert if the object passed is not a valuetype.
5947 mono_object_unbox (MonoObject *obj)
5949 MONO_REQ_GC_UNSAFE_MODE;
5951 /* add assert for valuetypes? */
5952 g_assert (obj->vtable->klass->valuetype);
5953 return ((char*)obj) + sizeof (MonoObject);
5957 * mono_object_isinst:
5959 * @klass: a pointer to a class
5961 * Returns: @obj if @obj is derived from @klass
5964 mono_object_isinst (MonoObject *obj, MonoClass *klass)
5966 MONO_REQ_GC_UNSAFE_MODE;
5969 mono_class_init (klass);
5971 if (mono_class_is_marshalbyref (klass) || (klass->flags & TYPE_ATTRIBUTE_INTERFACE))
5972 return mono_object_isinst_mbyref (obj, klass);
5977 return mono_class_is_assignable_from (klass, obj->vtable->klass) ? obj : NULL;
5981 mono_object_isinst_mbyref (MonoObject *obj, MonoClass *klass)
5983 MONO_REQ_GC_UNSAFE_MODE;
5993 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5994 if (MONO_VTABLE_IMPLEMENTS_INTERFACE (vt, klass->interface_id)) {
5998 /*If the above check fails we are in the slow path of possibly raising an exception. So it's ok to it this way.*/
5999 if (mono_class_has_variant_generic_params (klass) && mono_class_is_assignable_from (klass, obj->vtable->klass))
6002 MonoClass *oklass = vt->klass;
6003 if (mono_class_is_transparent_proxy (oklass))
6004 oklass = ((MonoTransparentProxy *)obj)->remote_class->proxy_class;
6006 mono_class_setup_supertypes (klass);
6007 if ((oklass->idepth >= klass->idepth) && (oklass->supertypes [klass->idepth - 1] == klass))
6010 #ifndef DISABLE_REMOTING
6011 if (vt->klass == mono_defaults.transparent_proxy_class && ((MonoTransparentProxy *)obj)->custom_type_info)
6013 MonoDomain *domain = mono_domain_get ();
6015 MonoObject *rp = (MonoObject *)((MonoTransparentProxy *)obj)->rp;
6016 MonoClass *rpklass = mono_defaults.iremotingtypeinfo_class;
6017 MonoMethod *im = NULL;
6020 im = mono_class_get_method_from_name (rpklass, "CanCastTo", -1);
6022 mono_raise_exception (mono_get_exception_not_supported ("Linked away."));
6023 im = mono_object_get_virtual_method (rp, im);
6026 pa [0] = mono_type_get_object_checked (domain, &klass->byval_arg, &error);
6027 mono_error_raise_exception (&error); /* FIXME don't raise here */
6030 res = mono_runtime_invoke_checked (im, rp, pa, &error);
6031 mono_error_raise_exception (&error); /* FIXME don't raise here */
6033 if (*(MonoBoolean *) mono_object_unbox(res)) {
6034 /* Update the vtable of the remote type, so it can safely cast to this new type */
6035 mono_upgrade_remote_class (domain, obj, klass);
6039 #endif /* DISABLE_REMOTING */
6044 * mono_object_castclass_mbyref:
6046 * @klass: a pointer to a class
6048 * Returns: @obj if @obj is derived from @klass, throws an exception otherwise
6051 mono_object_castclass_mbyref (MonoObject *obj, MonoClass *klass)
6053 MONO_REQ_GC_UNSAFE_MODE;
6055 if (!obj) return NULL;
6056 if (mono_object_isinst_mbyref (obj, klass)) return obj;
6058 mono_raise_exception (mono_exception_from_name (mono_defaults.corlib,
6060 "InvalidCastException"));
6065 MonoDomain *orig_domain;
6071 str_lookup (MonoDomain *domain, gpointer user_data)
6073 MONO_REQ_GC_UNSAFE_MODE;
6075 LDStrInfo *info = (LDStrInfo *)user_data;
6076 if (info->res || domain == info->orig_domain)
6078 info->res = (MonoString *)mono_g_hash_table_lookup (domain->ldstr_table, info->ins);
6082 mono_string_get_pinned (MonoString *str, MonoError *error)
6084 MONO_REQ_GC_UNSAFE_MODE;
6086 mono_error_init (error);
6088 /* We only need to make a pinned version of a string if this is a moving GC */
6089 if (!mono_gc_is_moving ())
6093 size = sizeof (MonoString) + 2 * (mono_string_length (str) + 1);
6094 news = (MonoString *)mono_gc_alloc_pinned_obj (((MonoObject*)str)->vtable, size);
6096 memcpy (mono_string_chars (news), mono_string_chars (str), mono_string_length (str) * 2);
6097 news->length = mono_string_length (str);
6099 mono_error_set_out_of_memory (error, "Could not allocate %i bytes", size);
6105 mono_string_is_interned_lookup (MonoString *str, int insert, MonoError *error)
6107 MONO_REQ_GC_UNSAFE_MODE;
6109 MonoGHashTable *ldstr_table;
6110 MonoString *s, *res;
6113 mono_error_init (error);
6115 domain = ((MonoObject *)str)->vtable->domain;
6116 ldstr_table = domain->ldstr_table;
6118 res = (MonoString *)mono_g_hash_table_lookup (ldstr_table, str);
6124 /* Allocate outside the lock */
6126 s = mono_string_get_pinned (str, error);
6127 return_val_if_nok (error, NULL);
6130 res = (MonoString *)mono_g_hash_table_lookup (ldstr_table, str);
6135 mono_g_hash_table_insert (ldstr_table, s, s);
6140 LDStrInfo ldstr_info;
6141 ldstr_info.orig_domain = domain;
6142 ldstr_info.ins = str;
6143 ldstr_info.res = NULL;
6145 mono_domain_foreach (str_lookup, &ldstr_info);
6146 if (ldstr_info.res) {
6148 * the string was already interned in some other domain:
6149 * intern it in the current one as well.
6151 mono_g_hash_table_insert (ldstr_table, str, str);
6161 * mono_string_is_interned:
6162 * @o: String to probe
6164 * Returns whether the string has been interned.
6167 mono_string_is_interned (MonoString *o)
6170 MonoString *result = mono_string_is_interned_lookup (o, FALSE, &error);
6171 /* This function does not fail. */
6172 mono_error_assert_ok (&error);
6177 * mono_string_intern:
6178 * @o: String to intern
6180 * Interns the string passed.
6181 * Returns: The interned string.
6184 mono_string_intern (MonoString *str)
6187 MonoString *result = mono_string_intern_checked (str, &error);
6188 mono_error_assert_ok (&error);
6193 * mono_string_intern_checked:
6194 * @o: String to intern
6195 * @error: set on error.
6197 * Interns the string passed.
6198 * Returns: The interned string. On failure returns NULL and sets @error
6201 mono_string_intern_checked (MonoString *str, MonoError *error)
6203 MONO_REQ_GC_UNSAFE_MODE;
6205 mono_error_init (error);
6207 return mono_string_is_interned_lookup (str, TRUE, error);
6212 * @domain: the domain where the string will be used.
6213 * @image: a metadata context
6214 * @idx: index into the user string table.
6216 * Implementation for the ldstr opcode.
6217 * Returns: a loaded string from the @image/@idx combination.
6220 mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 idx)
6222 MONO_REQ_GC_UNSAFE_MODE;
6224 if (image->dynamic) {
6225 MonoString *str = (MonoString *)mono_lookup_dynamic_token (image, MONO_TOKEN_STRING | idx, NULL);
6228 if (!mono_verifier_verify_string_signature (image, idx, NULL))
6229 return NULL; /*FIXME we should probably be raising an exception here*/
6230 return mono_ldstr_metadata_sig (domain, mono_metadata_user_string (image, idx));
6235 * mono_ldstr_metadata_sig
6236 * @domain: the domain for the string
6237 * @sig: the signature of a metadata string
6239 * Returns: a MonoString for a string stored in the metadata
6242 mono_ldstr_metadata_sig (MonoDomain *domain, const char* sig)
6244 MONO_REQ_GC_UNSAFE_MODE;
6247 const char *str = sig;
6248 MonoString *o, *interned;
6251 len2 = mono_metadata_decode_blob_size (str, &str);
6254 o = mono_string_new_utf16_checked (domain, (guint16*)str, len2, &error);
6255 mono_error_raise_exception (&error); /* FIXME don't raise here */
6256 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
6259 guint16 *p2 = (guint16*)mono_string_chars (o);
6260 for (i = 0; i < len2; ++i) {
6261 *p2 = GUINT16_FROM_LE (*p2);
6267 interned = (MonoString *)mono_g_hash_table_lookup (domain->ldstr_table, o);
6270 return interned; /* o will get garbage collected */
6272 o = mono_string_get_pinned (o, &error);
6273 mono_error_raise_exception (&error); /* FIXME don't raise here */
6276 interned = (MonoString *)mono_g_hash_table_lookup (domain->ldstr_table, o);
6278 mono_g_hash_table_insert (domain->ldstr_table, o, o);
6288 * mono_string_to_utf8:
6289 * @s: a System.String
6291 * Returns the UTF8 representation for @s.
6292 * The resulting buffer needs to be freed with mono_free().
6294 * @deprecated Use mono_string_to_utf8_checked to avoid having an exception arbritraly raised.
6297 mono_string_to_utf8 (MonoString *s)
6299 MONO_REQ_GC_UNSAFE_MODE;
6302 char *result = mono_string_to_utf8_checked (s, &error);
6304 if (!mono_error_ok (&error))
6305 mono_error_raise_exception (&error);
6310 * mono_string_to_utf8_checked:
6311 * @s: a System.String
6312 * @error: a MonoError.
6314 * Converts a MonoString to its UTF8 representation. May fail; check
6315 * @error to determine whether the conversion was successful.
6316 * The resulting buffer should be freed with mono_free().
6319 mono_string_to_utf8_checked (MonoString *s, MonoError *error)
6321 MONO_REQ_GC_UNSAFE_MODE;
6325 GError *gerror = NULL;
6327 mono_error_init (error);
6333 return g_strdup ("");
6335 as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, &written, &gerror);
6337 mono_error_set_argument (error, "string", "%s", gerror->message);
6338 g_error_free (gerror);
6341 /* g_utf16_to_utf8 may not be able to complete the convertion (e.g. NULL values were found, #335488) */
6342 if (s->length > written) {
6343 /* allocate the total length and copy the part of the string that has been converted */
6344 char *as2 = (char *)g_malloc0 (s->length);
6345 memcpy (as2, as, written);
6354 * mono_string_to_utf8_ignore:
6357 * Converts a MonoString to its UTF8 representation. Will ignore
6358 * invalid surrogate pairs.
6359 * The resulting buffer should be freed with mono_free().
6363 mono_string_to_utf8_ignore (MonoString *s)
6365 MONO_REQ_GC_UNSAFE_MODE;
6374 return g_strdup ("");
6376 as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, &written, NULL);
6378 /* g_utf16_to_utf8 may not be able to complete the convertion (e.g. NULL values were found, #335488) */
6379 if (s->length > written) {
6380 /* allocate the total length and copy the part of the string that has been converted */
6381 char *as2 = (char *)g_malloc0 (s->length);
6382 memcpy (as2, as, written);
6391 * mono_string_to_utf8_image_ignore:
6392 * @s: a System.String
6394 * Same as mono_string_to_utf8_ignore, but allocate the string from the image mempool.
6397 mono_string_to_utf8_image_ignore (MonoImage *image, MonoString *s)
6399 MONO_REQ_GC_UNSAFE_MODE;
6401 return mono_string_to_utf8_internal (NULL, image, s, TRUE, NULL);
6405 * mono_string_to_utf8_mp_ignore:
6406 * @s: a System.String
6408 * Same as mono_string_to_utf8_ignore, but allocate the string from a mempool.
6411 mono_string_to_utf8_mp_ignore (MonoMemPool *mp, MonoString *s)
6413 MONO_REQ_GC_UNSAFE_MODE;
6415 return mono_string_to_utf8_internal (mp, NULL, s, TRUE, NULL);
6420 * mono_string_to_utf16:
6423 * Return an null-terminated array of the utf-16 chars
6424 * contained in @s. The result must be freed with g_free().
6425 * This is a temporary helper until our string implementation
6426 * is reworked to always include the null terminating char.
6429 mono_string_to_utf16 (MonoString *s)
6431 MONO_REQ_GC_UNSAFE_MODE;
6438 as = (char *)g_malloc ((s->length * 2) + 2);
6439 as [(s->length * 2)] = '\0';
6440 as [(s->length * 2) + 1] = '\0';
6443 return (gunichar2 *)(as);
6446 memcpy (as, mono_string_chars(s), s->length * 2);
6447 return (gunichar2 *)(as);
6451 * mono_string_to_utf32:
6454 * Return an null-terminated array of the UTF-32 (UCS-4) chars
6455 * contained in @s. The result must be freed with g_free().
6458 mono_string_to_utf32 (MonoString *s)
6460 MONO_REQ_GC_UNSAFE_MODE;
6462 mono_unichar4 *utf32_output = NULL;
6463 GError *error = NULL;
6464 glong items_written;
6469 utf32_output = g_utf16_to_ucs4 (s->chars, s->length, NULL, &items_written, &error);
6472 g_error_free (error);
6474 return utf32_output;
6478 * mono_string_from_utf16:
6479 * @data: the UTF16 string (LPWSTR) to convert
6481 * Converts a NULL terminated UTF16 string (LPWSTR) to a MonoString.
6483 * Returns: a MonoString.
6486 mono_string_from_utf16 (gunichar2 *data)
6488 MONO_REQ_GC_UNSAFE_MODE;
6491 MonoString *res = NULL;
6492 MonoDomain *domain = mono_domain_get ();
6498 while (data [len]) len++;
6500 res = mono_string_new_utf16_checked (domain, data, len, &error);
6501 mono_error_raise_exception (&error); /* FIXME don't raise here */
6506 * mono_string_from_utf32:
6507 * @data: the UTF32 string (LPWSTR) to convert
6509 * Converts a UTF32 (UCS-4)to a MonoString.
6511 * Returns: a MonoString.
6514 mono_string_from_utf32 (mono_unichar4 *data)
6516 MONO_REQ_GC_UNSAFE_MODE;
6518 MonoString* result = NULL;
6519 mono_unichar2 *utf16_output = NULL;
6520 GError *error = NULL;
6521 glong items_written;
6527 while (data [len]) len++;
6529 utf16_output = g_ucs4_to_utf16 (data, len, NULL, &items_written, &error);
6532 g_error_free (error);
6534 result = mono_string_from_utf16 (utf16_output);
6535 g_free (utf16_output);
6540 mono_string_to_utf8_internal (MonoMemPool *mp, MonoImage *image, MonoString *s, gboolean ignore_error, MonoError *error)
6542 MONO_REQ_GC_UNSAFE_MODE;
6549 r = mono_string_to_utf8_ignore (s);
6551 r = mono_string_to_utf8_checked (s, error);
6552 if (!mono_error_ok (error))
6559 len = strlen (r) + 1;
6561 mp_s = (char *)mono_mempool_alloc (mp, len);
6563 mp_s = (char *)mono_image_alloc (image, len);
6565 memcpy (mp_s, r, len);
6573 * mono_string_to_utf8_image:
6574 * @s: a System.String
6576 * Same as mono_string_to_utf8, but allocate the string from the image mempool.
6579 mono_string_to_utf8_image (MonoImage *image, MonoString *s, MonoError *error)
6581 MONO_REQ_GC_UNSAFE_MODE;
6583 return mono_string_to_utf8_internal (NULL, image, s, FALSE, error);
6587 * mono_string_to_utf8_mp:
6588 * @s: a System.String
6590 * Same as mono_string_to_utf8, but allocate the string from a mempool.
6593 mono_string_to_utf8_mp (MonoMemPool *mp, MonoString *s, MonoError *error)
6595 MONO_REQ_GC_UNSAFE_MODE;
6597 return mono_string_to_utf8_internal (mp, NULL, s, FALSE, error);
6601 static MonoRuntimeExceptionHandlingCallbacks eh_callbacks;
6604 mono_install_eh_callbacks (MonoRuntimeExceptionHandlingCallbacks *cbs)
6606 eh_callbacks = *cbs;
6609 MonoRuntimeExceptionHandlingCallbacks *
6610 mono_get_eh_callbacks (void)
6612 return &eh_callbacks;
6616 * mono_raise_exception:
6617 * @ex: exception object
6619 * Signal the runtime that the exception @ex has been raised in unmanaged code.
6622 mono_raise_exception (MonoException *ex)
6624 MONO_REQ_GC_UNSAFE_MODE;
6627 * NOTE: Do NOT annotate this function with G_GNUC_NORETURN, since
6628 * that will cause gcc to omit the function epilog, causing problems when
6629 * the JIT tries to walk the stack, since the return address on the stack
6630 * will point into the next function in the executable, not this one.
6632 eh_callbacks.mono_raise_exception (ex);
6636 mono_raise_exception_with_context (MonoException *ex, MonoContext *ctx)
6638 MONO_REQ_GC_UNSAFE_MODE;
6640 eh_callbacks.mono_raise_exception_with_ctx (ex, ctx);
6644 * mono_wait_handle_new:
6645 * @domain: Domain where the object will be created
6646 * @handle: Handle for the wait handle
6648 * Returns: A new MonoWaitHandle created in the given domain for the given handle
6651 mono_wait_handle_new (MonoDomain *domain, HANDLE handle)
6653 MONO_REQ_GC_UNSAFE_MODE;
6656 MonoWaitHandle *res;
6657 gpointer params [1];
6658 static MonoMethod *handle_set;
6660 res = (MonoWaitHandle *)mono_object_new_checked (domain, mono_defaults.manualresetevent_class, &error);
6661 mono_error_raise_exception (&error); /* FIXME don't raise here */
6663 /* Even though this method is virtual, it's safe to invoke directly, since the object type matches. */
6665 handle_set = mono_class_get_property_from_name (mono_defaults.manualresetevent_class, "Handle")->set;
6667 params [0] = &handle;
6669 mono_runtime_invoke_checked (handle_set, res, params, &error);
6670 mono_error_raise_exception (&error); /* FIXME don't raise here */
6676 mono_wait_handle_get_handle (MonoWaitHandle *handle)
6678 MONO_REQ_GC_UNSAFE_MODE;
6680 static MonoClassField *f_safe_handle = NULL;
6683 if (!f_safe_handle) {
6684 f_safe_handle = mono_class_get_field_from_name (mono_defaults.manualresetevent_class, "safeWaitHandle");
6685 g_assert (f_safe_handle);
6688 mono_field_get_value ((MonoObject*)handle, f_safe_handle, &sh);
6694 mono_runtime_capture_context (MonoDomain *domain)
6696 MONO_REQ_GC_UNSAFE_MODE;
6698 RuntimeInvokeFunction runtime_invoke;
6700 if (!domain->capture_context_runtime_invoke || !domain->capture_context_method) {
6701 MonoMethod *method = mono_get_context_capture_method ();
6702 MonoMethod *wrapper;
6705 wrapper = mono_marshal_get_runtime_invoke (method, FALSE);
6706 domain->capture_context_runtime_invoke = mono_compile_method (wrapper);
6707 domain->capture_context_method = mono_compile_method (method);
6710 runtime_invoke = (RuntimeInvokeFunction)domain->capture_context_runtime_invoke;
6712 return runtime_invoke (NULL, NULL, NULL, domain->capture_context_method);
6715 * mono_async_result_new:
6716 * @domain:domain where the object will be created.
6717 * @handle: wait handle.
6718 * @state: state to pass to AsyncResult
6719 * @data: C closure data.
6721 * Creates a new MonoAsyncResult (AsyncResult C# class) in the given domain.
6722 * If the handle is not null, the handle is initialized to a MonOWaitHandle.
6726 mono_async_result_new (MonoDomain *domain, HANDLE handle, MonoObject *state, gpointer data, MonoObject *object_data)
6728 MONO_REQ_GC_UNSAFE_MODE;
6731 MonoAsyncResult *res = (MonoAsyncResult *)mono_object_new_checked (domain, mono_defaults.asyncresult_class, &error);
6732 mono_error_raise_exception (&error); /* FIXME don't raise here */
6733 MonoObject *context = mono_runtime_capture_context (domain);
6734 /* we must capture the execution context from the original thread */
6736 MONO_OBJECT_SETREF (res, execution_context, context);
6737 /* note: result may be null if the flow is suppressed */
6740 res->data = (void **)data;
6741 MONO_OBJECT_SETREF (res, object_data, object_data);
6742 MONO_OBJECT_SETREF (res, async_state, state);
6744 MONO_OBJECT_SETREF (res, handle, (MonoObject *) mono_wait_handle_new (domain, handle));
6746 res->sync_completed = FALSE;
6747 res->completed = FALSE;
6753 ves_icall_System_Runtime_Remoting_Messaging_AsyncResult_Invoke (MonoAsyncResult *ares)
6755 MONO_REQ_GC_UNSAFE_MODE;
6762 g_assert (ares->async_delegate);
6764 ac = (MonoAsyncCall*) ares->object_data;
6766 res = mono_runtime_delegate_invoke (ares->async_delegate, (void**) &ares->async_state, NULL);
6768 gpointer wait_event = NULL;
6770 ac->msg->exc = NULL;
6771 res = mono_message_invoke (ares->async_delegate, ac->msg, &ac->msg->exc, &ac->out_args);
6772 MONO_OBJECT_SETREF (ac, res, res);
6774 mono_monitor_enter ((MonoObject*) ares);
6775 ares->completed = 1;
6777 wait_event = mono_wait_handle_get_handle ((MonoWaitHandle*) ares->handle);
6778 mono_monitor_exit ((MonoObject*) ares);
6780 if (wait_event != NULL)
6781 SetEvent (wait_event);
6783 if (ac->cb_method) {
6784 mono_runtime_invoke_checked (ac->cb_method, ac->cb_target, (gpointer*) &ares, &error);
6785 mono_error_raise_exception (&error);
6793 mono_message_init (MonoDomain *domain,
6794 MonoMethodMessage *this_obj,
6795 MonoReflectionMethod *method,
6796 MonoArray *out_args)
6798 MONO_REQ_GC_UNSAFE_MODE;
6800 static MonoClass *object_array_klass;
6801 static MonoClass *byte_array_klass;
6802 static MonoClass *string_array_klass;
6804 MonoMethodSignature *sig = mono_method_signature (method->method);
6811 if (!object_array_klass) {
6814 klass = mono_array_class_get (mono_defaults.byte_class, 1);
6816 byte_array_klass = klass;
6818 klass = mono_array_class_get (mono_defaults.string_class, 1);
6820 string_array_klass = klass;
6822 klass = mono_array_class_get (mono_defaults.object_class, 1);
6825 mono_atomic_store_release (&object_array_klass, klass);
6828 MONO_OBJECT_SETREF (this_obj, method, method);
6830 arr = mono_array_new_specific_checked (mono_class_vtable (domain, object_array_klass), sig->param_count, &error);
6831 mono_error_raise_exception (&error); /* FIXME don't raise here */
6833 MONO_OBJECT_SETREF (this_obj, args, arr);
6835 arr = mono_array_new_specific_checked (mono_class_vtable (domain, byte_array_klass), sig->param_count, &error);
6836 mono_error_raise_exception (&error); /* FIXME don't raise here */
6838 MONO_OBJECT_SETREF (this_obj, arg_types, arr);
6840 this_obj->async_result = NULL;
6841 this_obj->call_type = CallType_Sync;
6843 names = g_new (char *, sig->param_count);
6844 mono_method_get_param_names (method->method, (const char **) names);
6846 arr = mono_array_new_specific_checked (mono_class_vtable (domain, string_array_klass), sig->param_count, &error);
6847 mono_error_raise_exception (&error); /* FIXME don't raise here */
6849 MONO_OBJECT_SETREF (this_obj, names, arr);
6851 for (i = 0; i < sig->param_count; i++) {
6852 name = mono_string_new (domain, names [i]);
6853 mono_array_setref (this_obj->names, i, name);
6857 for (i = 0, j = 0; i < sig->param_count; i++) {
6858 if (sig->params [i]->byref) {
6860 MonoObject* arg = (MonoObject *)mono_array_get (out_args, gpointer, j);
6861 mono_array_setref (this_obj->args, i, arg);
6865 if (!(sig->params [i]->attrs & PARAM_ATTRIBUTE_OUT))
6869 if (sig->params [i]->attrs & PARAM_ATTRIBUTE_OUT)
6872 mono_array_set (this_obj->arg_types, guint8, i, arg_type);
6876 #ifndef DISABLE_REMOTING
6878 * mono_remoting_invoke:
6879 * @real_proxy: pointer to a RealProxy object
6880 * @msg: The MonoMethodMessage to execute
6881 * @exc: used to store exceptions
6882 * @out_args: used to store output arguments
6884 * This is used to call RealProxy::Invoke(). RealProxy::Invoke() returns an
6885 * IMessage interface and it is not trivial to extract results from there. So
6886 * we call an helper method PrivateInvoke instead of calling
6887 * RealProxy::Invoke() directly.
6889 * Returns: the result object.
6892 mono_remoting_invoke (MonoObject *real_proxy, MonoMethodMessage *msg, MonoObject **exc, MonoArray **out_args, MonoError *error)
6894 MONO_REQ_GC_UNSAFE_MODE;
6897 MonoMethod *im = real_proxy->vtable->domain->private_invoke_method;
6902 mono_error_init (error);
6904 /*static MonoObject *(*invoke) (gpointer, gpointer, MonoObject **, MonoArray **) = NULL;*/
6907 im = mono_class_get_method_from_name (mono_defaults.real_proxy_class, "PrivateInvoke", 4);
6909 mono_error_set_generic_error (error, "System", "NotSupportedException", "Linked away.");
6912 real_proxy->vtable->domain->private_invoke_method = im;
6915 pa [0] = real_proxy;
6920 o = mono_runtime_try_invoke (im, NULL, pa, exc, error);
6921 return_val_if_nok (error, NULL);
6928 mono_message_invoke (MonoObject *target, MonoMethodMessage *msg,
6929 MonoObject **exc, MonoArray **out_args)
6931 MONO_REQ_GC_UNSAFE_MODE;
6933 static MonoClass *object_array_klass;
6937 MonoMethodSignature *sig;
6940 int i, j, outarg_count = 0;
6942 #ifndef DISABLE_REMOTING
6943 if (target && mono_object_is_transparent_proxy (target)) {
6944 MonoTransparentProxy* tp = (MonoTransparentProxy *)target;
6945 if (mono_class_is_contextbound (tp->remote_class->proxy_class) && tp->rp->context == (MonoObject *) mono_context_get ()) {
6946 target = tp->rp->unwrapped_server;
6948 ret = mono_remoting_invoke ((MonoObject *)tp->rp, msg, exc, out_args, &error);
6949 mono_error_raise_exception (&error); /* FIXME don't raise here */
6956 domain = mono_domain_get ();
6957 method = msg->method->method;
6958 sig = mono_method_signature (method);
6960 for (i = 0; i < sig->param_count; i++) {
6961 if (sig->params [i]->byref)
6965 if (!object_array_klass) {
6968 klass = mono_array_class_get (mono_defaults.object_class, 1);
6971 mono_memory_barrier ();
6972 object_array_klass = klass;
6975 arr = mono_array_new_specific_checked (mono_class_vtable (domain, object_array_klass), outarg_count, &error);
6976 mono_error_raise_exception (&error); /* FIXME don't raise here */
6978 mono_gc_wbarrier_generic_store (out_args, (MonoObject*) arr);
6981 ret = mono_runtime_invoke_array (method, method->klass->valuetype? mono_object_unbox (target): target, msg->args, exc);
6983 for (i = 0, j = 0; i < sig->param_count; i++) {
6984 if (sig->params [i]->byref) {
6986 arg = (MonoObject *)mono_array_get (msg->args, gpointer, i);
6987 mono_array_setref (*out_args, j, arg);
6996 * mono_object_to_string:
6998 * @exc: Any exception thrown by ToString (). May be NULL.
7000 * Returns: the result of calling ToString () on an object.
7003 mono_object_to_string (MonoObject *obj, MonoObject **exc)
7005 MONO_REQ_GC_UNSAFE_MODE;
7007 static MonoMethod *to_string = NULL;
7016 to_string = mono_class_get_method_from_name_flags (mono_get_object_class (), "ToString", 0, METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_PUBLIC);
7018 method = mono_object_get_virtual_method (obj, to_string);
7020 // Unbox value type if needed
7021 if (mono_class_is_valuetype (mono_method_get_class (method))) {
7022 target = mono_object_unbox (obj);
7026 s = (MonoString *) mono_runtime_try_invoke (method, target, NULL, exc, &error);
7027 if (*exc == NULL && !mono_error_ok (&error))
7028 *exc = (MonoObject*) mono_error_convert_to_exception (&error);
7030 mono_error_cleanup (&error);
7032 s = (MonoString *) mono_runtime_invoke_checked (method, target, NULL, &error);
7033 mono_error_raise_exception (&error); /* FIXME don't raise here */
7040 * mono_print_unhandled_exception:
7041 * @exc: The exception
7043 * Prints the unhandled exception.
7046 mono_print_unhandled_exception (MonoObject *exc)
7048 MONO_REQ_GC_UNSAFE_MODE;
7051 char *message = (char*)"";
7052 gboolean free_message = FALSE;
7055 if (exc == (MonoObject*)mono_object_domain (exc)->out_of_memory_ex) {
7056 message = g_strdup ("OutOfMemoryException");
7057 free_message = TRUE;
7058 } else if (exc == (MonoObject*)mono_object_domain (exc)->stack_overflow_ex) {
7059 message = g_strdup ("StackOverflowException"); //if we OVF, we can't expect to have stack space to JIT Exception::ToString.
7060 free_message = TRUE;
7063 if (((MonoException*)exc)->native_trace_ips) {
7064 message = mono_exception_get_native_backtrace ((MonoException*)exc);
7065 free_message = TRUE;
7067 MonoObject *other_exc = NULL;
7068 str = mono_object_to_string (exc, &other_exc);
7070 char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
7071 char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other_exc);
7073 message = g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n",
7074 original_backtrace, nested_backtrace);
7076 g_free (original_backtrace);
7077 g_free (nested_backtrace);
7078 free_message = TRUE;
7080 message = mono_string_to_utf8_checked (str, &error);
7081 if (!mono_error_ok (&error)) {
7082 mono_error_cleanup (&error);
7083 message = (char *) "";
7085 free_message = TRUE;
7092 * g_printerr ("\nUnhandled Exception: %s.%s: %s\n", exc->vtable->klass->name_space,
7093 * exc->vtable->klass->name, message);
7095 g_printerr ("\nUnhandled Exception:\n%s\n", message);
7102 * mono_delegate_ctor:
7103 * @this: pointer to an uninitialized delegate object
7104 * @target: target object
7105 * @addr: pointer to native code
7108 * Initialize a delegate and sets a specific method, not the one
7109 * associated with addr. This is useful when sharing generic code.
7110 * In that case addr will most probably not be associated with the
7111 * correct instantiation of the method.
7114 mono_delegate_ctor_with_method (MonoObject *this_obj, MonoObject *target, gpointer addr, MonoMethod *method)
7116 MONO_REQ_GC_UNSAFE_MODE;
7118 MonoDelegate *delegate = (MonoDelegate *)this_obj;
7120 g_assert (this_obj);
7123 g_assert (mono_class_has_parent (mono_object_class (this_obj), mono_defaults.multicastdelegate_class));
7126 delegate->method = method;
7128 mono_stats.delegate_creations++;
7130 #ifndef DISABLE_REMOTING
7131 if (target && target->vtable->klass == mono_defaults.transparent_proxy_class) {
7133 method = mono_marshal_get_remoting_invoke (method);
7134 delegate->method_ptr = mono_compile_method (method);
7135 MONO_OBJECT_SETREF (delegate, target, target);
7139 delegate->method_ptr = addr;
7140 MONO_OBJECT_SETREF (delegate, target, target);
7143 delegate->invoke_impl = arch_create_delegate_trampoline (delegate->object.vtable->domain, delegate->object.vtable->klass);
7144 if (callbacks.init_delegate)
7145 callbacks.init_delegate (delegate);
7149 * mono_delegate_ctor:
7150 * @this: pointer to an uninitialized delegate object
7151 * @target: target object
7152 * @addr: pointer to native code
7154 * This is used to initialize a delegate.
7157 mono_delegate_ctor (MonoObject *this_obj, MonoObject *target, gpointer addr)
7159 MONO_REQ_GC_UNSAFE_MODE;
7161 MonoDomain *domain = mono_domain_get ();
7163 MonoMethod *method = NULL;
7167 ji = mono_jit_info_table_find (domain, (char *)mono_get_addr_from_ftnptr (addr));
7169 if (!ji && domain != mono_get_root_domain ())
7170 ji = mono_jit_info_table_find (mono_get_root_domain (), (char *)mono_get_addr_from_ftnptr (addr));
7172 method = mono_jit_info_get_method (ji);
7173 g_assert (!method->klass->generic_container);
7176 mono_delegate_ctor_with_method (this_obj, target, addr, method);
7180 * mono_method_call_message_new:
7181 * @method: method to encapsulate
7182 * @params: parameters to the method
7183 * @invoke: optional, delegate invoke.
7184 * @cb: async callback delegate.
7185 * @state: state passed to the async callback.
7187 * Translates arguments pointers into a MonoMethodMessage.
7190 mono_method_call_message_new (MonoMethod *method, gpointer *params, MonoMethod *invoke,
7191 MonoDelegate **cb, MonoObject **state)
7193 MONO_REQ_GC_UNSAFE_MODE;
7197 MonoDomain *domain = mono_domain_get ();
7198 MonoMethodSignature *sig = mono_method_signature (method);
7199 MonoMethodMessage *msg;
7202 msg = (MonoMethodMessage *)mono_object_new_checked (domain, mono_defaults.mono_method_message_class, &error);
7203 mono_error_raise_exception (&error); /* FIXME don't raise here */
7206 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, invoke, NULL, &error);
7207 mono_error_raise_exception (&error); /* FIXME don't raise here */
7208 mono_message_init (domain, msg, rm, NULL);
7209 count = sig->param_count - 2;
7211 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, method, NULL, &error);
7212 mono_error_raise_exception (&error); /* FIXME don't raise here */
7213 mono_message_init (domain, msg, rm, NULL);
7214 count = sig->param_count;
7217 for (i = 0; i < count; i++) {
7222 if (sig->params [i]->byref)
7223 vpos = *((gpointer *)params [i]);
7227 klass = mono_class_from_mono_type (sig->params [i]);
7229 if (klass->valuetype)
7230 arg = mono_value_box (domain, klass, vpos);
7232 arg = *((MonoObject **)vpos);
7234 mono_array_setref (msg->args, i, arg);
7237 if (cb != NULL && state != NULL) {
7238 *cb = *((MonoDelegate **)params [i]);
7240 *state = *((MonoObject **)params [i]);
7247 * mono_method_return_message_restore:
7249 * Restore results from message based processing back to arguments pointers
7252 mono_method_return_message_restore (MonoMethod *method, gpointer *params, MonoArray *out_args)
7254 MONO_REQ_GC_UNSAFE_MODE;
7256 MonoMethodSignature *sig = mono_method_signature (method);
7257 int i, j, type, size, out_len;
7259 if (out_args == NULL)
7261 out_len = mono_array_length (out_args);
7265 for (i = 0, j = 0; i < sig->param_count; i++) {
7266 MonoType *pt = sig->params [i];
7271 mono_raise_exception (mono_get_exception_execution_engine ("The proxy call returned an incorrect number of output arguments"));
7273 arg = (char *)mono_array_get (out_args, gpointer, j);
7276 g_assert (type != MONO_TYPE_VOID);
7278 if (MONO_TYPE_IS_REFERENCE (pt)) {
7279 mono_gc_wbarrier_generic_store (*((MonoObject ***)params [i]), (MonoObject *)arg);
7282 MonoClass *klass = ((MonoObject*)arg)->vtable->klass;
7283 size = mono_class_value_size (klass, NULL);
7284 if (klass->has_references)
7285 mono_gc_wbarrier_value_copy (*((gpointer *)params [i]), arg + sizeof (MonoObject), 1, klass);
7287 mono_gc_memmove_atomic (*((gpointer *)params [i]), arg + sizeof (MonoObject), size);
7289 size = mono_class_value_size (mono_class_from_mono_type (pt), NULL);
7290 mono_gc_bzero_atomic (*((gpointer *)params [i]), size);
7299 #ifndef DISABLE_REMOTING
7302 * mono_load_remote_field:
7303 * @this: pointer to an object
7304 * @klass: klass of the object containing @field
7305 * @field: the field to load
7306 * @res: a storage to store the result
7308 * This method is called by the runtime on attempts to load fields of
7309 * transparent proxy objects. @this points to such TP, @klass is the class of
7310 * the object containing @field. @res is a storage location which can be
7311 * used to store the result.
7313 * Returns: an address pointing to the value of field.
7316 mono_load_remote_field (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, gpointer *res)
7318 MONO_REQ_GC_UNSAFE_MODE;
7322 static MonoMethod *getter = NULL;
7323 MonoDomain *domain = mono_domain_get ();
7324 MonoTransparentProxy *tp = (MonoTransparentProxy *) this_obj;
7325 MonoClass *field_class;
7326 MonoMethodMessage *msg;
7327 MonoArray *out_args;
7331 g_assert (mono_object_is_transparent_proxy (this_obj));
7332 g_assert (res != NULL);
7334 if (mono_class_is_contextbound (tp->remote_class->proxy_class) && tp->rp->context == (MonoObject *) mono_context_get ()) {
7335 mono_field_get_value (tp->rp->unwrapped_server, field, res);
7340 getter = mono_class_get_method_from_name (mono_defaults.object_class, "FieldGetter", -1);
7342 mono_raise_exception (mono_get_exception_not_supported ("Linked away."));
7345 field_class = mono_class_from_mono_type (field->type);
7347 msg = (MonoMethodMessage *)mono_object_new_checked (domain, mono_defaults.mono_method_message_class, &error);
7348 mono_error_raise_exception (&error); /* FIXME don't raise here */
7349 out_args = mono_array_new (domain, mono_defaults.object_class, 1);
7350 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, getter, NULL, &error);
7351 mono_error_raise_exception (&error); /* FIXME don't raise here */
7352 mono_message_init (domain, msg, rm, out_args);
7354 full_name = mono_type_get_full_name (klass);
7355 mono_array_setref (msg->args, 0, mono_string_new (domain, full_name));
7356 mono_array_setref (msg->args, 1, mono_string_new (domain, mono_field_get_name (field)));
7359 mono_remoting_invoke ((MonoObject *)(tp->rp), msg, &exc, &out_args, &error);
7360 mono_error_raise_exception (&error); /* FIXME don't raise here */
7362 if (exc) mono_raise_exception ((MonoException *)exc);
7364 if (mono_array_length (out_args) == 0)
7367 *res = mono_array_get (out_args, MonoObject *, 0); /* FIXME: GC write abrrier for res */
7369 if (field_class->valuetype) {
7370 return ((char *)*res) + sizeof (MonoObject);
7376 * mono_load_remote_field_new:
7381 * Missing documentation.
7384 mono_load_remote_field_new (MonoObject *this_obj, MonoClass *klass, MonoClassField *field)
7386 MONO_REQ_GC_UNSAFE_MODE;
7390 static MonoMethod *getter = NULL;
7391 MonoDomain *domain = mono_domain_get ();
7392 MonoTransparentProxy *tp = (MonoTransparentProxy *) this_obj;
7393 MonoClass *field_class;
7394 MonoMethodMessage *msg;
7395 MonoArray *out_args;
7396 MonoObject *exc, *res;
7399 g_assert (mono_object_is_transparent_proxy (this_obj));
7401 field_class = mono_class_from_mono_type (field->type);
7403 if (mono_class_is_contextbound (tp->remote_class->proxy_class) && tp->rp->context == (MonoObject *) mono_context_get ()) {
7405 if (field_class->valuetype) {
7406 res = mono_object_new_checked (domain, field_class, &error);
7407 mono_error_raise_exception (&error); /* FIXME don't raise here */
7408 val = ((gchar *) res) + sizeof (MonoObject);
7412 mono_field_get_value (tp->rp->unwrapped_server, field, val);
7417 getter = mono_class_get_method_from_name (mono_defaults.object_class, "FieldGetter", -1);
7419 mono_raise_exception (mono_get_exception_not_supported ("Linked away."));
7422 msg = (MonoMethodMessage *)mono_object_new_checked (domain, mono_defaults.mono_method_message_class, &error);
7423 mono_error_raise_exception (&error); /* FIXME don't raise here */
7424 out_args = mono_array_new (domain, mono_defaults.object_class, 1);
7426 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, getter, NULL, &error);
7427 mono_error_raise_exception (&error); /* FIXME don't raise here */
7428 mono_message_init (domain, msg, rm, out_args);
7430 full_name = mono_type_get_full_name (klass);
7431 mono_array_setref (msg->args, 0, mono_string_new (domain, full_name));
7432 mono_array_setref (msg->args, 1, mono_string_new (domain, mono_field_get_name (field)));
7435 mono_remoting_invoke ((MonoObject *)(tp->rp), msg, &exc, &out_args, &error);
7436 mono_error_raise_exception (&error); /* FIXME don't raise here */
7438 if (exc) mono_raise_exception ((MonoException *)exc);
7440 if (mono_array_length (out_args) == 0)
7443 res = mono_array_get (out_args, MonoObject *, 0);
7449 * mono_store_remote_field:
7450 * @this_obj: pointer to an object
7451 * @klass: klass of the object containing @field
7452 * @field: the field to load
7453 * @val: the value/object to store
7455 * This method is called by the runtime on attempts to store fields of
7456 * transparent proxy objects. @this_obj points to such TP, @klass is the class of
7457 * the object containing @field. @val is the new value to store in @field.
7460 mono_store_remote_field (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, gpointer val)
7462 MONO_REQ_GC_UNSAFE_MODE;
7466 static MonoMethod *setter = NULL;
7467 MonoDomain *domain = mono_domain_get ();
7468 MonoTransparentProxy *tp = (MonoTransparentProxy *) this_obj;
7469 MonoClass *field_class;
7470 MonoMethodMessage *msg;
7471 MonoArray *out_args;
7476 g_assert (mono_object_is_transparent_proxy (this_obj));
7478 field_class = mono_class_from_mono_type (field->type);
7480 if (mono_class_is_contextbound (tp->remote_class->proxy_class) && tp->rp->context == (MonoObject *) mono_context_get ()) {
7481 if (field_class->valuetype) mono_field_set_value (tp->rp->unwrapped_server, field, val);
7482 else mono_field_set_value (tp->rp->unwrapped_server, field, *((MonoObject **)val));
7487 setter = mono_class_get_method_from_name (mono_defaults.object_class, "FieldSetter", -1);
7489 mono_raise_exception (mono_get_exception_not_supported ("Linked away."));
7492 if (field_class->valuetype)
7493 arg = mono_value_box (domain, field_class, val);
7495 arg = *((MonoObject **)val);
7498 msg = (MonoMethodMessage *)mono_object_new_checked (domain, mono_defaults.mono_method_message_class, &error);
7499 mono_error_raise_exception (&error); /* FIXME don't raise here */
7500 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, setter, NULL, &error);
7501 mono_error_raise_exception (&error); /* FIXME don't raise here */
7502 mono_message_init (domain, msg, rm, NULL);
7504 full_name = mono_type_get_full_name (klass);
7505 mono_array_setref (msg->args, 0, mono_string_new (domain, full_name));
7506 mono_array_setref (msg->args, 1, mono_string_new (domain, mono_field_get_name (field)));
7507 mono_array_setref (msg->args, 2, arg);
7510 mono_remoting_invoke ((MonoObject *)(tp->rp), msg, &exc, &out_args, &error);
7511 mono_error_raise_exception (&error); /* FIXME don't raise here */
7513 if (exc) mono_raise_exception ((MonoException *)exc);
7517 * mono_store_remote_field_new:
7523 * Missing documentation
7526 mono_store_remote_field_new (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, MonoObject *arg)
7528 MONO_REQ_GC_UNSAFE_MODE;
7532 static MonoMethod *setter = NULL;
7533 MonoDomain *domain = mono_domain_get ();
7534 MonoTransparentProxy *tp = (MonoTransparentProxy *) this_obj;
7535 MonoClass *field_class;
7536 MonoMethodMessage *msg;
7537 MonoArray *out_args;
7541 g_assert (mono_object_is_transparent_proxy (this_obj));
7543 field_class = mono_class_from_mono_type (field->type);
7545 if (mono_class_is_contextbound (tp->remote_class->proxy_class) && tp->rp->context == (MonoObject *) mono_context_get ()) {
7546 if (field_class->valuetype) mono_field_set_value (tp->rp->unwrapped_server, field, ((gchar *) arg) + sizeof (MonoObject));
7547 else mono_field_set_value (tp->rp->unwrapped_server, field, arg);
7552 setter = mono_class_get_method_from_name (mono_defaults.object_class, "FieldSetter", -1);
7554 mono_raise_exception (mono_get_exception_not_supported ("Linked away."));
7557 msg = (MonoMethodMessage *)mono_object_new_checked (domain, mono_defaults.mono_method_message_class, &error);
7558 mono_error_raise_exception (&error); /* FIXME don't raise here */
7559 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, setter, NULL, &error);
7560 mono_error_raise_exception (&error); /* FIXME don't raise here */
7561 mono_message_init (domain, msg, rm, NULL);
7563 full_name = mono_type_get_full_name (klass);
7564 mono_array_setref (msg->args, 0, mono_string_new (domain, full_name));
7565 mono_array_setref (msg->args, 1, mono_string_new (domain, mono_field_get_name (field)));
7566 mono_array_setref (msg->args, 2, arg);
7569 mono_remoting_invoke ((MonoObject *)(tp->rp), msg, &exc, &out_args, &error);
7570 mono_error_raise_exception (&error); /* FIXME don't raise here */
7572 if (exc) mono_raise_exception ((MonoException *)exc);
7577 * mono_create_ftnptr:
7579 * Given a function address, create a function descriptor for it.
7580 * This is only needed on some platforms.
7583 mono_create_ftnptr (MonoDomain *domain, gpointer addr)
7585 return callbacks.create_ftnptr (domain, addr);
7589 * mono_get_addr_from_ftnptr:
7591 * Given a pointer to a function descriptor, return the function address.
7592 * This is only needed on some platforms.
7595 mono_get_addr_from_ftnptr (gpointer descr)
7597 return callbacks.get_addr_from_ftnptr (descr);
7601 * mono_string_chars:
7604 * Returns a pointer to the UCS16 characters stored in the MonoString
7607 mono_string_chars (MonoString *s)
7609 // MONO_REQ_GC_UNSAFE_MODE; //FIXME too much trouble for now
7615 * mono_string_length:
7618 * Returns the lenght in characters of the string
7621 mono_string_length (MonoString *s)
7623 MONO_REQ_GC_UNSAFE_MODE;
7629 * mono_array_length:
7630 * @array: a MonoArray*
7632 * Returns the total number of elements in the array. This works for
7633 * both vectors and multidimensional arrays.
7636 mono_array_length (MonoArray *array)
7638 MONO_REQ_GC_UNSAFE_MODE;
7640 return array->max_length;
7644 * mono_array_addr_with_size:
7645 * @array: a MonoArray*
7646 * @size: size of the array elements
7647 * @idx: index into the array
7649 * Use this function to obtain the address for the @idx item on the
7650 * @array containing elements of size @size.
7652 * This method performs no bounds checking or type checking.
7654 * Returns the address of the @idx element in the array.
7657 mono_array_addr_with_size (MonoArray *array, int size, uintptr_t idx)
7659 MONO_REQ_GC_UNSAFE_MODE;
7661 return ((char*)(array)->vector) + size * idx;
7666 mono_glist_to_array (GList *list, MonoClass *eclass)
7668 MonoDomain *domain = mono_domain_get ();
7675 len = g_list_length (list);
7676 res = mono_array_new (domain, eclass, len);
7678 for (i = 0; list; list = list->next, i++)
7679 mono_array_set (res, gpointer, i, list->data);
7686 * The following section is purely to declare prototypes and
7687 * document the API, as these C files are processed by our
7693 * @array: array to alter
7694 * @element_type: A C type name, this macro will use the sizeof(type) to determine the element size
7695 * @index: index into the array
7696 * @value: value to set
7698 * Value Type version: This sets the @index's element of the @array
7699 * with elements of size sizeof(type) to the provided @value.
7701 * This macro does not attempt to perform type checking or bounds checking.
7703 * Use this to set value types in a `MonoArray`.
7705 void mono_array_set(MonoArray *array, Type element_type, uintptr_t index, Value value)
7710 * mono_array_setref:
7711 * @array: array to alter
7712 * @index: index into the array
7713 * @value: value to set
7715 * Reference Type version: This sets the @index's element of the
7716 * @array with elements of size sizeof(type) to the provided @value.
7718 * This macro does not attempt to perform type checking or bounds checking.
7720 * Use this to reference types in a `MonoArray`.
7722 void mono_array_setref(MonoArray *array, uintptr_t index, MonoObject *object)
7728 * @array: array on which to operate on
7729 * @element_type: C element type (example: MonoString *, int, MonoObject *)
7730 * @index: index into the array
7732 * Use this macro to retrieve the @index element of an @array and
7733 * extract the value assuming that the elements of the array match
7734 * the provided type value.
7736 * This method can be used with both arrays holding value types and
7737 * reference types. For reference types, the @type parameter should
7738 * be a `MonoObject*` or any subclass of it, like `MonoString*`.
7740 * This macro does not attempt to perform type checking or bounds checking.
7742 * Returns: The element at the @index position in the @array.
7744 Type mono_array_get (MonoArray *array, Type element_type, uintptr_t index)