Merge pull request #5260 from BrzVlad/fix-handler-block
[mono.git] / mono / metadata / object.c
index 92aa723f20a73f61c7ffec75631e46b6ee77ec94..9a48d69e336752e91defb66bbe7023cb3f9d2437 100644 (file)
@@ -742,14 +742,12 @@ compute_class_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int
                size = max_size;
        }
 
-#ifdef HAVE_SGEN_GC
-       /*An Ephemeron cannot be marked by sgen*/
-       if (!static_fields && klass->image == mono_defaults.corlib && !strcmp ("Ephemeron", klass->name)) {
+       /* An Ephemeron cannot be marked by sgen */
+       if (mono_gc_is_moving () && !static_fields && klass->image == mono_defaults.corlib && !strcmp ("Ephemeron", klass->name)) {
                *max_set = 0;
                memset (bitmap, 0, size / 8);
                return bitmap;
        }
-#endif
 
        for (p = klass; p != NULL; p = p->parent) {
                gpointer iter = NULL;
@@ -1932,23 +1930,22 @@ mono_class_create_runtime_vtable (MonoDomain *domain, MonoClass *klass, MonoErro
        vt->domain = domain;
 
        mono_class_compute_gc_descriptor (klass);
-               /*
-                * We can't use typed allocation in the non-root domains, since the
-                * collector needs the GC descriptor stored in the vtable even after
-                * the mempool containing the vtable is destroyed when the domain is
-                * unloaded. An alternative might be to allocate vtables in the GC
-                * heap, but this does not seem to work (it leads to crashes inside
-                * libgc). If that approach is tried, two gc descriptors need to be
-                * allocated for each class: one for the root domain, and one for all
-                * other domains. The second descriptor should contain a bit for the
-                * vtable field in MonoObject, since we can no longer assume the 
-                * vtable is reachable by other roots after the appdomain is unloaded.
-                */
-#ifdef HAVE_BOEHM_GC
-       if (domain != mono_get_root_domain () && !mono_dont_free_domains)
+       /*
+        * For Boehm:
+        * We can't use typed allocation in the non-root domains, since the
+        * collector needs the GC descriptor stored in the vtable even after
+        * the mempool containing the vtable is destroyed when the domain is
+        * unloaded. An alternative might be to allocate vtables in the GC
+        * heap, but this does not seem to work (it leads to crashes inside
+        * libgc). If that approach is tried, two gc descriptors need to be
+        * allocated for each class: one for the root domain, and one for all
+        * other domains. The second descriptor should contain a bit for the
+        * vtable field in MonoObject, since we can no longer assume the
+        * vtable is reachable by other roots after the appdomain is unloaded.
+        */
+       if (!mono_gc_is_moving () && domain != mono_get_root_domain () && !mono_dont_free_domains)
                vt->gc_descr = MONO_GC_DESCRIPTOR_NULL;
        else
-#endif
                vt->gc_descr = klass->gc_descr;
 
        gc_bits = mono_gc_get_vtable_bits (klass);
@@ -2822,13 +2819,11 @@ do_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **ex
 
        error_init (error);
        
-       if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
-               mono_profiler_method_start_invoke (method);
+       MONO_PROFILER_RAISE (method_begin_invoke, (method));
 
        result = callbacks.runtime_invoke (method, obj, params, exc, error);
 
-       if (mono_profiler_get_events () & MONO_PROFILE_METHOD_EVENTS)
-               mono_profiler_method_end_invoke (method);
+       MONO_PROFILER_RAISE (method_end_invoke, (method));
 
        if (!mono_error_ok (error))
                return NULL;
@@ -3758,6 +3753,49 @@ mono_nullable_init (guint8 *buf, MonoObject *value, MonoClass *klass)
        }
 }
 
+/*
+ * mono_nullable_init_from_handle:
+ * @buf: The nullable structure to initialize.
+ * @value: the value to initialize from
+ * @klass: the type for the object
+ *
+ * Initialize the nullable structure pointed to by @buf from @value which
+ * should be a boxed value type.   The size of @buf should be able to hold
+ * as much data as the @klass->instance_size (which is the number of bytes
+ * that will be copies).
+ *
+ * Since Nullables have variable structure, we can not define a C
+ * structure for them.
+ */
+void
+mono_nullable_init_from_handle (guint8 *buf, MonoObjectHandle value, MonoClass *klass)
+{
+       MONO_REQ_GC_UNSAFE_MODE;
+
+       MonoClass *param_class = klass->cast_class;
+
+       mono_class_setup_fields (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);
+
+       *(guint8*)(buf + klass->fields [1].offset - sizeof (MonoObject)) = MONO_HANDLE_IS_NULL  (value) ? 0 : 1;
+       if (!MONO_HANDLE_IS_NULL (value)) {
+               uint32_t value_gchandle = 0;
+               gpointer src = mono_object_handle_pin_unbox (value, &value_gchandle);
+               if (param_class->has_references)
+                       mono_gc_wbarrier_value_copy (buf + klass->fields [0].offset - sizeof (MonoObject), src, 1, param_class);
+               else
+                       mono_gc_memmove_atomic (buf + klass->fields [0].offset - sizeof (MonoObject), src, mono_class_value_size (param_class, NULL));
+               mono_gchandle_free (value_gchandle);
+       } else {
+               mono_gc_bzero_atomic (buf + klass->fields [0].offset - sizeof (MonoObject), mono_class_value_size (param_class, NULL));
+       }
+}
+
+
+
 /**
  * mono_nullable_box:
  * \param buf The buffer representing the data to be boxed
@@ -5579,18 +5617,18 @@ mono_array_full_copy (MonoArray *src, MonoArray *dest)
 static void
 array_full_copy_unchecked_size (MonoArray *src, MonoArray *dest, MonoClass *klass, uintptr_t size)
 {
-#ifdef HAVE_SGEN_GC
-       if (klass->element_class->valuetype) {
-               if (klass->element_class->has_references)
-                       mono_value_copy_array (dest, 0, mono_array_addr_with_size_fast (src, 0, 0), mono_array_length (src));
-               else
-                       mono_gc_memmove_atomic (&dest->vector, &src->vector, size);
+       if (mono_gc_is_moving ()) {
+               if (klass->element_class->valuetype) {
+                       if (klass->element_class->has_references)
+                               mono_value_copy_array (dest, 0, mono_array_addr_with_size_fast (src, 0, 0), mono_array_length (src));
+                       else
+                               mono_gc_memmove_atomic (&dest->vector, &src->vector, size);
+               } else {
+                       mono_array_memcpy_refs (dest, 0, src, 0, mono_array_length (src));
+               }
        } else {
-               mono_array_memcpy_refs (dest, 0, src, 0, mono_array_length (src));
+               mono_gc_memmove_atomic (&dest->vector, &src->vector, size);
        }
-#else
-       mono_gc_memmove_atomic (&dest->vector, &src->vector, size);
-#endif
 }
 
 /**
@@ -6320,31 +6358,31 @@ mono_value_box_checked (MonoDomain *domain, MonoClass *klass, gpointer value, Mo
 
        size = size - sizeof (MonoObject);
 
-#ifdef HAVE_SGEN_GC
-       g_assert (size == mono_class_value_size (klass, NULL));
-       mono_gc_wbarrier_value_copy ((char *)res + sizeof (MonoObject), value, 1, klass);
-#else
+       if (mono_gc_is_moving ()) {
+               g_assert (size == mono_class_value_size (klass, NULL));
+               mono_gc_wbarrier_value_copy ((char *)res + sizeof (MonoObject), value, 1, klass);
+       } else {
 #if NO_UNALIGNED_ACCESS
-       mono_gc_memmove_atomic ((char *)res + sizeof (MonoObject), value, size);
-#else
-       switch (size) {
-       case 1:
-               *((guint8 *) res + sizeof (MonoObject)) = *(guint8 *) value;
-               break;
-       case 2:
-               *(guint16 *)((guint8 *) res + sizeof (MonoObject)) = *(guint16 *) value;
-               break;
-       case 4:
-               *(guint32 *)((guint8 *) res + sizeof (MonoObject)) = *(guint32 *) value;
-               break;
-       case 8:
-               *(guint64 *)((guint8 *) res + sizeof (MonoObject)) = *(guint64 *) value;
-               break;
-       default:
                mono_gc_memmove_atomic ((char *)res + sizeof (MonoObject), value, size);
-       }
-#endif
+#else
+               switch (size) {
+               case 1:
+                       *((guint8 *) res + sizeof (MonoObject)) = *(guint8 *) value;
+                       break;
+               case 2:
+                       *(guint16 *)((guint8 *) res + sizeof (MonoObject)) = *(guint16 *) value;
+                       break;
+               case 4:
+                       *(guint32 *)((guint8 *) res + sizeof (MonoObject)) = *(guint32 *) value;
+                       break;
+               case 8:
+                       *(guint64 *)((guint8 *) res + sizeof (MonoObject)) = *(guint64 *) value;
+                       break;
+               default:
+                       mono_gc_memmove_atomic ((char *)res + sizeof (MonoObject), value, size);
+               }
 #endif
+       }
        if (klass->has_finalize) {
                mono_object_register_finalizer (res);
                return_val_if_nok (error, NULL);
@@ -7817,43 +7855,44 @@ mono_print_unhandled_exception (MonoObject *exc)
  * On failure returns FALSE and sets \p error.
  */
 gboolean
-mono_delegate_ctor_with_method (MonoObject *this_obj, MonoObject *target, gpointer addr, MonoMethod *method, MonoError *error)
+mono_delegate_ctor_with_method (MonoObjectHandle this_obj, MonoObjectHandle target, gpointer addr, MonoMethod *method, MonoError *error)
 {
        MONO_REQ_GC_UNSAFE_MODE;
 
        error_init (error);
-       MonoDelegate *delegate = (MonoDelegate *)this_obj;
+       MonoDelegateHandle delegate = MONO_HANDLE_CAST (MonoDelegate, this_obj);
 
-       g_assert (this_obj);
+       g_assert (!MONO_HANDLE_IS_NULL (this_obj));
        g_assert (addr);
 
-       g_assert (mono_class_has_parent (mono_object_class (this_obj), mono_defaults.multicastdelegate_class));
+       MonoClass *klass = mono_handle_class (this_obj);
+       g_assert (mono_class_has_parent (klass, mono_defaults.multicastdelegate_class));
 
        if (method)
-               delegate->method = method;
+               MONO_HANDLE_SETVAL (delegate, method, MonoMethod*, method);
 
        mono_stats.delegate_creations++;
 
 #ifndef DISABLE_REMOTING
-       if (target && mono_object_is_transparent_proxy (target)) {
+       if (!MONO_HANDLE_IS_NULL (target) && mono_class_is_transparent_proxy (mono_handle_class (target))) {
                g_assert (method);
                method = mono_marshal_get_remoting_invoke (method);
 #ifdef ENABLE_INTERPRETER
                //g_error ("need RuntimeMethod in method_ptr when using interpreter");
 #endif
-               delegate->method_ptr = mono_compile_method_checked (method, error);
+               MONO_HANDLE_SETVAL (delegate, method_ptr, gpointer, mono_compile_method_checked (method, error));
                return_val_if_nok (error, FALSE);
-               MONO_OBJECT_SETREF (delegate, target, target);
+               MONO_HANDLE_SET (delegate, target, target);
        } else
 #endif
        {
-               delegate->method_ptr = addr;
-               MONO_OBJECT_SETREF (delegate, target, target);
+               MONO_HANDLE_SETVAL (delegate, method_ptr, gpointer, addr);
+               MONO_HANDLE_SET (delegate, target, target);
        }
 
-       delegate->invoke_impl = callbacks.create_delegate_trampoline (delegate->object.vtable->domain, delegate->object.vtable->klass);
+       MONO_HANDLE_SETVAL (delegate, invoke_impl, gpointer, callbacks.create_delegate_trampoline (MONO_HANDLE_DOMAIN (delegate), mono_handle_class (delegate)));
        if (callbacks.init_delegate)
-               callbacks.init_delegate (delegate);
+               callbacks.init_delegate (MONO_HANDLE_RAW (delegate)); /* FIXME: update init_delegate callback to take a MonoDelegateHandle */
        return TRUE;
 }
 
@@ -7867,7 +7906,7 @@ mono_delegate_ctor_with_method (MonoObject *this_obj, MonoObject *target, gpoint
  * On failure returns FALSE and sets \p error.
  */
 gboolean
-mono_delegate_ctor (MonoObject *this_obj, MonoObject *target, gpointer addr, MonoError *error)
+mono_delegate_ctor (MonoObjectHandle this_obj, MonoObjectHandle target, gpointer addr, MonoError *error)
 {
        MONO_REQ_GC_UNSAFE_MODE;