Merge pull request #496 from nicolas-raoul/unit-test-for-issue2907
[mono.git] / mono / metadata / object.c
index 4ffd33ffcf714214afc1eedbf35f22ac0476bb90..48ced0faa557ffa8dadb067608dd54e67db2053e 100644 (file)
@@ -43,6 +43,7 @@
 #include <mono/utils/strenc.h>
 #include <mono/utils/mono-counters.h>
 #include <mono/utils/mono-error-internals.h>
+#include <mono/utils/mono-memory-model.h>
 #include "cominterop.h"
 
 #ifdef HAVE_BOEHM_GC
@@ -100,7 +101,8 @@ mono_runtime_object_init (MonoObject *this)
        MonoClass *klass = this->vtable->klass;
 
        method = mono_class_get_method_from_name (klass, ".ctor", 0);
-       g_assert (method);
+       if (!method)
+               g_error ("Could not lookup zero argument constructor for class %s", mono_type_get_full_name (klass));
 
        if (method->klass->valuetype)
                this = mono_object_unbox (this);
@@ -223,8 +225,9 @@ get_type_init_exception_for_vtable (MonoVTable *vtable)
        MonoException *ex;
        gchar *full_name;
 
-       g_assert (vtable->init_failed);
-
+       if (!vtable->init_failed)
+               g_error ("Trying to get the init exception for a non-failed vtable of class %s", mono_type_get_full_name (klass));
+       
        /* 
         * If the initializing thread was rudely aborted, the exception is not stored
         * in the hash.
@@ -1083,9 +1086,8 @@ mono_method_get_imt_slot (MonoMethod *method)
        hashes = hashes_start;
 
        if (! MONO_CLASS_IS_INTERFACE (method->klass)) {
-               printf ("mono_method_get_imt_slot: %s.%s.%s is not an interface MonoMethod\n",
+               g_error ("mono_method_get_imt_slot: %s.%s.%s is not an interface MonoMethod",
                                method->klass->name_space, method->klass->name, method->name);
-               g_assert_not_reached ();
        }
        
        /* Initialize hashes */
@@ -1997,21 +1999,23 @@ mono_class_create_runtime_vtable (MonoDomain *domain, MonoClass *class, gboolean
                                gsize default_bitmap [4] = {0};
                                gsize *bitmap;
                                int max_set = 0;
+                               int numbits;
                                MonoClass *fclass;
                                if (mono_type_is_reference (field->type)) {
                                        default_bitmap [0] = 1;
-                                       max_set = 1;
+                                       numbits = 1;
                                        bitmap = default_bitmap;
                                } else if (mono_type_is_struct (field->type)) {
                                        fclass = mono_class_from_mono_type (field->type);
                                        bitmap = compute_class_bitmap (fclass, default_bitmap, sizeof (default_bitmap) * 8, 0, &max_set, FALSE);
+                                       numbits = max_set + 1;
                                } else {
                                        default_bitmap [0] = 0;
-                                       max_set = 0;
+                                       numbits = 0;
                                        bitmap = default_bitmap;
                                }
                                size = mono_type_size (field->type, &align);
-                               offset = mono_alloc_special_static_data (special_static, size, align, (uintptr_t*)bitmap, max_set);
+                               offset = mono_alloc_special_static_data (special_static, size, align, (uintptr_t*)bitmap, numbits);
                                if (!domain->special_static_fields)
                                        domain->special_static_fields = g_hash_table_new (NULL, NULL);
                                g_hash_table_insert (domain->special_static_fields, field, GUINT_TO_POINTER (offset));
@@ -2534,7 +2538,9 @@ mono_remote_class (MonoDomain *domain, MonoString *class_name, MonoClass *proxy_
        rc->default_vtable = NULL;
        rc->xdomain_vtable = NULL;
        rc->proxy_class_name = name;
+#ifndef DISABLE_PERFCOUNTERS
        mono_perfcounters->loader_bytes += mono_string_length (class_name) + 1;
+#endif
 
        g_hash_table_insert (domain->proxy_vtable_hash, key, rc);
 
@@ -2613,9 +2619,11 @@ mono_remote_class_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, Mon
                MonoClass *klass;
                type = ((MonoReflectionType *)rp->class_to_proxy)->type;
                klass = mono_class_from_mono_type (type);
+#ifndef DISABLE_COM
                if ((klass->is_com_object || (mono_defaults.com_object_class && klass == mono_defaults.com_object_class)) && !mono_class_vtable (mono_domain_get (), klass)->remote)
                        remote_class->default_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_COMINTEROP);
                else
+#endif
                        remote_class->default_vtable = mono_class_proxy_vtable (domain, remote_class, MONO_REMOTING_TARGET_UNKNOWN);
        }
        
@@ -2959,8 +2967,7 @@ handle_enum:
                t = type->data.generic_class->container_class->byval_arg.type;
                goto handle_enum;
        default:
-               g_warning ("got type %x", type->type);
-               g_assert_not_reached ();
+               g_error ("got type %x", type->type);
        }
 }
 
@@ -3395,6 +3402,9 @@ void
 mono_nullable_init (guint8 *buf, MonoObject *value, MonoClass *klass)
 {
        MonoClass *param_class = klass->cast_class;
+
+       mono_class_setup_fields_locking (klass);
+       g_assert (klass->fields_inited);
                                
        g_assert (mono_class_from_mono_type (klass->fields [0].type) == param_class);
        g_assert (mono_class_from_mono_type (klass->fields [1].type) == mono_defaults.boolean_class);
@@ -3423,6 +3433,9 @@ mono_nullable_box (guint8 *buf, MonoClass *klass)
 {
        MonoClass *param_class = klass->cast_class;
 
+       mono_class_setup_fields_locking (klass);
+       g_assert (klass->fields_inited);
+
        g_assert (mono_class_from_mono_type (klass->fields [0].type) == param_class);
        g_assert (mono_class_from_mono_type (klass->fields [1].type) == mono_defaults.boolean_class);
 
@@ -3474,9 +3487,11 @@ MonoObject*
 mono_runtime_delegate_invoke (MonoObject *delegate, void **params, MonoObject **exc)
 {
        MonoMethod *im;
+       MonoClass *klass = delegate->vtable->klass;
 
-       im = mono_get_delegate_invoke (delegate->vtable->klass);
-       g_assert (im);
+       im = mono_get_delegate_invoke (klass);
+       if (!im)
+               g_error ("Could not lookup delegate invoke method for delegate %s", mono_type_get_full_name (klass));
 
        return mono_runtime_invoke (im, delegate, params, exc);
 }
@@ -4935,7 +4950,7 @@ mono_string_new_len (MonoDomain *domain, const char *text, guint length)
        guint16 *ut;
        glong items_written;
 
-       ut = g_utf8_to_utf16 (text, length, NULL, &items_written, &error);
+       ut = eg_utf8_to_utf16_with_nuls (text, length, NULL, &items_written, &error);
 
        if (!error)
                o = mono_string_new_utf16 (domain, ut, items_written);
@@ -5695,26 +5710,13 @@ mono_raise_exception (MonoException *ex)
         * that will cause gcc to omit the function epilog, causing problems when
         * the JIT tries to walk the stack, since the return address on the stack
         * will point into the next function in the executable, not this one.
-        */
-
-       if (((MonoObject*)ex)->vtable->klass == mono_defaults.threadabortexception_class) {
-               MonoInternalThread *thread = mono_thread_internal_current ();
-               g_assert (ex->object.vtable->domain == mono_domain_get ());
-               MONO_OBJECT_SETREF (thread, abort_exc, ex);
-       }
-       
+        */     
        eh_callbacks.mono_raise_exception (ex);
 }
 
 void
 mono_raise_exception_with_context (MonoException *ex, MonoContext *ctx) 
 {
-       if (((MonoObject*)ex)->vtable->klass == mono_defaults.threadabortexception_class) {
-               MonoInternalThread *thread = mono_thread_internal_current ();
-               g_assert (ex->object.vtable->domain == mono_domain_get ());
-               MONO_OBJECT_SETREF (thread, abort_exc, ex);
-       }
-       
        eh_callbacks.mono_raise_exception_with_ctx (ex, ctx);
 }
 
@@ -5838,23 +5840,18 @@ mono_message_init (MonoDomain *domain,
        if (!object_array_klass) {
                MonoClass *klass;
 
-               klass = mono_array_class_get (mono_defaults.object_class, 1);
-               g_assert (klass);
-
-               mono_memory_barrier ();
-               object_array_klass = klass;
-
                klass = mono_array_class_get (mono_defaults.byte_class, 1);
                g_assert (klass);
-
-               mono_memory_barrier ();
                byte_array_klass = klass;
 
                klass = mono_array_class_get (mono_defaults.string_class, 1);
                g_assert (klass);
-
-               mono_memory_barrier ();
                string_array_klass = klass;
+
+               klass = mono_array_class_get (mono_defaults.object_class, 1);
+               g_assert (klass);
+
+               mono_atomic_store_release (&object_array_klass, klass);
        }
 
        MONO_OBJECT_SETREF (this, method, method);
@@ -6027,15 +6024,22 @@ mono_print_unhandled_exception (MonoObject *exc)
 
        if (exc == (MonoObject*)mono_object_domain (exc)->out_of_memory_ex) {
                message = g_strdup ("OutOfMemoryException");
+               free_message = TRUE;
        } else {
-               str = mono_object_to_string (exc, NULL);
-               if (str) {
-                       message = mono_string_to_utf8_checked (str, &error);
-                       if (!mono_error_ok (&error)) {
-                               mono_error_cleanup (&error);
-                               message = (char *) "";
-                       } else {
-                               free_message = TRUE;
+               
+               if (((MonoException*)exc)->native_trace_ips) {
+                       message = mono_exception_get_native_backtrace ((MonoException*)exc);
+                       free_message = TRUE;
+               } else {
+                       str = mono_object_to_string (exc, NULL);
+                       if (str) {
+                               message = mono_string_to_utf8_checked (str, &error);
+                               if (!mono_error_ok (&error)) {
+                                       mono_error_cleanup (&error);
+                                       message = (char *) "";
+                               } else {
+                                       free_message = TRUE;
+                               }
                        }
                }
        }
@@ -6044,7 +6048,7 @@ mono_print_unhandled_exception (MonoObject *exc)
         * g_printerr ("\nUnhandled Exception: %s.%s: %s\n", exc->vtable->klass->name_space, 
         *         exc->vtable->klass->name, message);
         */
-       g_printerr ("\nUnhandled Exception: %s\n", message);
+       g_printerr ("\nUnhandled Exception:\n%s\n", message);
        
        if (free_message)
                g_free (message);