Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / metadata / icall.c
index c1134ddf0459b630843ab44dc8a2022184398f97..f55ed1f1847600caf96c9aced596651945e01320 100644 (file)
@@ -1,5 +1,5 @@
-/*
- * icall.c:
+/**
+ * \file
  *
  * Authors:
  *   Dietmar Maurer (dietmar@ximian.com)
 #include <sys/utsname.h>
 #endif
 
-extern MonoString* ves_icall_System_Environment_GetOSVersionString (void);
+extern MonoStringHandle ves_icall_System_Environment_GetOSVersionString (MonoError *error);
 
 ICALL_EXPORT MonoReflectionAssemblyHandle ves_icall_System_Reflection_Assembly_GetCallingAssembly (MonoError *error);
 
@@ -118,6 +118,9 @@ static GENERATE_GET_CLASS_WITH_CACHE (property_info, "System.Reflection", "Prope
 static GENERATE_GET_CLASS_WITH_CACHE (event_info, "System.Reflection", "EventInfo")
 static GENERATE_GET_CLASS_WITH_CACHE (module, "System.Reflection", "Module")
 
+static void
+array_set_value_impl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, MonoError *error);
+
 static MonoArrayHandle
 type_array_from_modifiers (MonoImage *image, MonoType *type, int optional, MonoError *error);
 
@@ -220,9 +223,15 @@ ves_icall_System_Array_GetValue (MonoArray *arr, MonoArray *idxs)
 }
 
 ICALL_EXPORT void
-ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32 pos)
+ves_icall_System_Array_SetValueImpl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, MonoError *error)
+{
+       error_init (error);
+       array_set_value_impl (arr, value, pos, error);
+}
+
+static void
+array_set_value_impl (MonoArrayHandle arr, MonoObjectHandle value, guint32 pos, MonoError *error)
 {
-       MonoError error;
        MonoClass *ac, *vc, *ec;
        gint32 esize, vsize;
        gpointer *ea, *va;
@@ -232,49 +241,49 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
        gint64 i64 = 0;
        gdouble r64 = 0;
 
-       error_init (&error);
+       uint32_t arr_gchandle = 0;
+       uint32_t value_gchandle = 0;
 
-       if (value)
-               vc = value->vtable->klass;
+       error_init (error);
+
+       if (!MONO_HANDLE_IS_NULL (value))
+               vc = mono_handle_class (value);
        else
                vc = NULL;
 
-       ac = arr->obj.vtable->klass;
+       ac = mono_handle_class (arr);
        ec = ac->element_class;
 
        esize = mono_array_element_size (ac);
-       ea = (gpointer*)((char*)arr->vector + (pos * esize));
-       va = (gpointer*)((char*)value + sizeof (MonoObject));
+       ea = mono_array_handle_pin_with_size (arr, esize, pos, &arr_gchandle);
 
        if (mono_class_is_nullable (ec)) {
-               mono_nullable_init ((guint8*)ea, value, ec);
-               return;
+               mono_nullable_init_from_handle ((guint8*)ea, value, ec);
+               goto leave;
        }
 
-       if (!value) {
+       if (MONO_HANDLE_IS_NULL (value)) {
                mono_gc_bzero_atomic (ea, esize);
-               return;
+               goto leave;
        }
 
-#define NO_WIDENING_CONVERSION G_STMT_START{\
-       mono_set_pending_exception (mono_get_exception_argument ( \
-               "value", "not a widening conversion")); \
-       return; \
-}G_STMT_END
+#define NO_WIDENING_CONVERSION G_STMT_START{                           \
+               mono_error_set_argument (error, "value", "not a widening conversion"); \
+               goto leave;                                                     \
+       }G_STMT_END
 
-#define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{\
-               if (esize < vsize + (extra)) {                                                    \
-                       mono_set_pending_exception (mono_get_exception_argument (       \
-                       "value", "not a widening conversion")); \
-                       return;                                                                 \
-               } \
-}G_STMT_END
+#define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{                 \
+               if (esize < vsize + (extra)) {                          \
+                       mono_error_set_argument (error, "value", "not a widening conversion"); \
+                       goto leave;                                             \
+               }                                                       \
+       }G_STMT_END
 
-#define INVALID_CAST G_STMT_START{ \
+#define INVALID_CAST G_STMT_START{                                     \
                mono_get_runtime_callbacks ()->set_cast_details (vc, ec); \
-       mono_set_pending_exception (mono_get_exception_invalid_cast ()); \
-       return; \
-}G_STMT_END
+               mono_error_set_invalid_cast (error);                    \
+               goto leave;                                                     \
+       }G_STMT_END
 
        /* Check element (destination) type. */
        switch (ec->byval_arg.type) {
@@ -310,29 +319,34 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
                break;
        }
 
+       MonoObjectHandle inst = mono_object_handle_isinst (value, ec, error);
+       if (!is_ok (error))
+               goto leave;
+       gboolean castOk = !MONO_HANDLE_IS_NULL (inst);
+
        if (!ec->valuetype) {
-               gboolean castOk = (NULL != mono_object_isinst_checked (value, ec, &error));
-               if (mono_error_set_pending_exception (&error))
-                       return;
                if (!castOk)
                        INVALID_CAST;
-               mono_gc_wbarrier_set_arrayref (arr, ea, (MonoObject*)value);
-               return;
+               MONO_HANDLE_ARRAY_SETREF (arr, pos, value);
+               goto leave;
        }
 
-       if (mono_object_isinst_checked (value, ec, &error)) {
+       if (castOk) {
+               va = mono_object_handle_pin_unbox (value, &value_gchandle);
                if (ec->has_references)
-                       mono_value_copy (ea, (char*)value + sizeof (MonoObject), ec);
+                       mono_value_copy (ea, va, ec);
                else
-                       mono_gc_memmove_atomic (ea, (char *)value + sizeof (MonoObject), esize);
-               return;
+                       mono_gc_memmove_atomic (ea, va, esize);
+               mono_gchandle_free (value_gchandle);
+               value_gchandle = 0;
+               goto leave;
        }
-       if (mono_error_set_pending_exception (&error))
-               return;
 
        if (!vc->valuetype)
                INVALID_CAST;
 
+       va = mono_object_handle_pin_unbox (value, &value_gchandle);
+
        vsize = mono_class_instance_size (vc) - sizeof (MonoObject);
 
        et = ec->byval_arg.type;
@@ -352,7 +366,7 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
        case MONO_TYPE_CHAR: \
                CHECK_WIDENING_CONVERSION(0); \
                *(etype *) ea = (etype) u64; \
-               return; \
+               goto leave; \
        /* You can't assign a signed value to an unsigned array. */ \
        case MONO_TYPE_I1: \
        case MONO_TYPE_I2: \
@@ -373,7 +387,7 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
        case MONO_TYPE_I8: \
                CHECK_WIDENING_CONVERSION(0); \
                *(etype *) ea = (etype) i64; \
-               return; \
+               goto leave; \
        /* You can assign an unsigned value to a signed array if the array's */ \
        /* element size is larger than the value size. */ \
        case MONO_TYPE_U1: \
@@ -383,7 +397,7 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
        case MONO_TYPE_CHAR: \
                CHECK_WIDENING_CONVERSION(1); \
                *(etype *) ea = (etype) u64; \
-               return; \
+               goto leave; \
        /* You can't assign a floating point number to an integer array. */ \
        case MONO_TYPE_R4: \
        case MONO_TYPE_R8: \
@@ -397,7 +411,7 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
        case MONO_TYPE_R8: \
                CHECK_WIDENING_CONVERSION(0); \
                *(etype *) ea = (etype) r64; \
-               return; \
+               goto leave; \
        /* All integer values fit into a floating point array, so we don't */ \
        /* need to CHECK_WIDENING_CONVERSION here. */ \
        case MONO_TYPE_I1: \
@@ -405,14 +419,14 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
        case MONO_TYPE_I4: \
        case MONO_TYPE_I8: \
                *(etype *) ea = (etype) i64; \
-               return; \
+               goto leave; \
        case MONO_TYPE_U1: \
        case MONO_TYPE_U2: \
        case MONO_TYPE_U4: \
        case MONO_TYPE_U8: \
        case MONO_TYPE_CHAR: \
                *(etype *) ea = (etype) u64; \
-               return; \
+               goto leave; \
        } \
 }G_STMT_END
 
@@ -498,8 +512,8 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
        }
 
        INVALID_CAST;
-       /* Not reached, INVALID_CAST does not return. Just to avoid a compiler warning ... */
-       return;
+       /* Not reached, INVALID_CAST does fall thru. */
+       g_assert_not_reached ();
 
 #undef INVALID_CAST
 #undef NO_WIDENING_CONVERSION
@@ -507,51 +521,71 @@ ves_icall_System_Array_SetValueImpl (MonoArray *arr, MonoObject *value, guint32
 #undef ASSIGN_UNSIGNED
 #undef ASSIGN_SIGNED
 #undef ASSIGN_REAL
+leave:
+       if (arr_gchandle)
+               mono_gchandle_free (arr_gchandle);
+       if (value_gchandle)
+               mono_gchandle_free (value_gchandle);
+       return;
 }
 
 ICALL_EXPORT void 
-ves_icall_System_Array_SetValue (MonoArray *arr, MonoObject *value,
-                                MonoArray *idxs)
+ves_icall_System_Array_SetValue (MonoArrayHandle arr, MonoObjectHandle value,
+                                MonoArrayHandle idxs, MonoError *error)
 {
+       MonoArrayBounds dim;
        MonoClass *ac, *ic;
-       gint32 i, pos, *ind;
+       gint32 idx;
+       gint32 i, pos;
 
-       MONO_CHECK_ARG_NULL (idxs,);
+       error_init (error);
+
+       if (MONO_HANDLE_IS_NULL (idxs)) {
+               mono_error_set_argument_null (error, "idxs", "");
+               return;
+       }
 
-       ic = idxs->obj.vtable->klass;
-       ac = arr->obj.vtable->klass;
+       ic = mono_handle_class (idxs);
+       ac = mono_handle_class (arr);
 
        g_assert (ic->rank == 1);
-       if (idxs->bounds != NULL || idxs->max_length != ac->rank) {
-               mono_set_pending_exception (mono_get_exception_argument (NULL, NULL));
+       if (mono_handle_array_has_bounds (idxs) || MONO_HANDLE_GETVAL (idxs, max_length) != ac->rank) {
+               mono_error_set_argument (error, "idxs", "");
                return;
        }
 
-       ind = (gint32 *)idxs->vector;
-
-       if (arr->bounds == NULL) {
-               if (*ind < 0 || *ind >= arr->max_length) {
-                       mono_set_pending_exception (mono_get_exception_index_out_of_range ());
+       if (!mono_handle_array_has_bounds (arr)) {
+               MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, 0);
+               if (idx < 0 || idx >= MONO_HANDLE_GETVAL (arr, max_length)) {
+                       mono_error_set_exception_instance (error, mono_get_exception_index_out_of_range ());
                        return;
                }
 
-               ves_icall_System_Array_SetValueImpl (arr, value, *ind);
+               array_set_value_impl (arr, value, idx, error);
                return;
        }
        
-       for (i = 0; i < ac->rank; i++)
-               if ((ind [i] < arr->bounds [i].lower_bound) ||
-                   (ind [i] >= (mono_array_lower_bound_t)arr->bounds [i].length + arr->bounds [i].lower_bound)) {
-                       mono_set_pending_exception (mono_get_exception_index_out_of_range ());
+       for (i = 0; i < ac->rank; i++) {
+               mono_handle_array_get_bounds_dim (arr, i, &dim);
+               MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, i);
+               if ((idx < dim.lower_bound) ||
+                   (idx >= (mono_array_lower_bound_t)dim.length + dim.lower_bound)) {
+                       mono_error_set_exception_instance (error, mono_get_exception_index_out_of_range ());
                        return;
                }
+       }
 
-       pos = ind [0] - arr->bounds [0].lower_bound;
-       for (i = 1; i < ac->rank; i++)
-               pos = pos * arr->bounds [i].length + ind [i] - 
-                       arr->bounds [i].lower_bound;
 
-       ves_icall_System_Array_SetValueImpl (arr, value, pos);
+       MONO_HANDLE_ARRAY_GETVAL  (idx, idxs, gint32, 0);
+       mono_handle_array_get_bounds_dim (arr, 0, &dim);
+       pos = idx - dim.lower_bound;
+       for (i = 1; i < ac->rank; i++) {
+               mono_handle_array_get_bounds_dim (arr, i, &dim);
+               MONO_HANDLE_ARRAY_GETVAL (idx, idxs, gint32, i);
+               pos = pos * dim.length + idx - dim.lower_bound;
+       }
+
+       array_set_value_impl (arr, value, pos, error);
 }
 
 ICALL_EXPORT MonoArray *
@@ -582,6 +616,11 @@ ves_icall_System_Array_CreateInstanceImpl (MonoReflectionType *type, MonoArray *
        if (mono_error_set_pending_exception (&error))
                return NULL;
 
+       if (klass->element_class->byval_arg.type == MONO_TYPE_VOID) {
+               mono_set_pending_exception (mono_get_exception_not_supported ("Arrays of System.Void are not supported."));
+               return NULL;
+       }
+
        if (bounds && (mono_array_length (bounds) == 1) && (mono_array_get (bounds, gint32, 0) != 0))
                /* vectors are not the same as one dimensional arrays with no-zero bounds */
                bounded = TRUE;
@@ -727,14 +766,6 @@ ves_icall_System_Array_ClearInternal (MonoArray *arr, int idx, int length)
        mono_gc_bzero_atomic (mono_array_addr_with_size_fast (arr, sz, idx), length * sz);
 }
 
-ICALL_EXPORT MonoArray*
-ves_icall_System_Array_Clone (MonoArray *arr)
-{
-       MonoError error;
-       MonoArray *result = mono_array_clone_checked (arr, &error);
-       mono_error_set_pending_exception (&error);
-       return result;
-}
 
 ICALL_EXPORT gboolean
 ves_icall_System_Array_FastCopy (MonoArray *source, int source_idx, MonoArray* dest, int dest_idx, int length)
@@ -983,6 +1014,8 @@ ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_SufficientExecutionStac
 {
 #if defined(TARGET_WIN32) || defined(HOST_WIN32)
        // It does not work on win32
+#elif defined(TARGET_ANDROID) || defined(__linux__)
+       // No need for now
 #else
        guint8 *stack_addr;
        guint8 *current;
@@ -1223,7 +1256,13 @@ ves_icall_System_Object_GetType (MonoObjectHandle obj, MonoError *error)
        if (mono_class_is_transparent_proxy (klass)) {
                MonoTransparentProxyHandle proxy_obj = MONO_HANDLE_CAST (MonoTransparentProxy, obj);
                MonoRemoteClass *remote_class = MONO_HANDLE_GETVAL (proxy_obj, remote_class);
-               MonoType *proxy_type = &remote_class->proxy_class->byval_arg;
+               /* If it's a transparent proxy for an interface, return the
+                * interface type, not the unhelpful proxy_class class (which
+                * is just MarshalByRefObject). */
+               MonoType *proxy_type =
+                       mono_remote_class_is_interface_proxy (remote_class) ?
+                       &remote_class->interfaces[0]->byval_arg :
+                       &remote_class->proxy_class->byval_arg;
                return mono_type_get_object_handle (domain, proxy_type, error);
        } else
 #endif
@@ -1916,32 +1955,27 @@ ves_icall_MonoField_GetValueInternal (MonoReflectionField *field, MonoObject *ob
 }
 
 ICALL_EXPORT void
-ves_icall_MonoField_SetValueInternal (MonoReflectionField *field, MonoObject *obj, MonoObject *value)
+ves_icall_MonoField_SetValueInternal (MonoReflectionFieldHandle field, MonoObjectHandle obj, MonoObjectHandle value, MonoError  *error)
 {
-       MonoError error;
-       MonoClassField *cf = field->field;
-       MonoType *type;
-       gchar *v;
+       MonoClassField *cf = MONO_HANDLE_GETVAL (field, field);
 
-       if (field->klass->image->assembly->ref_only) {
-               mono_set_pending_exception (mono_get_exception_invalid_operation (
-                                       "It is illegal to set the value on a field on a type loaded using the ReflectionOnly methods."));
+       MonoClass *field_klass = MONO_HANDLE_GETVAL (field, klass);
+       if (field_klass->image->assembly->ref_only) {
+               mono_error_set_invalid_operation (error, "It is illegal to set the value on a field on a type loaded using the ReflectionOnly methods.");
                return;
        }
 
        if (mono_security_core_clr_enabled () &&
-           !mono_security_core_clr_ensure_reflection_access_field (cf, &error)) {
-               mono_error_set_pending_exception (&error);
+           !mono_security_core_clr_ensure_reflection_access_field (cf, error)) {
                return;
        }
 
-       type = mono_field_get_type_checked (cf, &error);
-       if (!mono_error_ok (&error)) {
-               mono_error_set_pending_exception (&error);
-               return;
-       }
+       MonoType *type = mono_field_get_type_checked (cf, error);
+       return_if_nok (error);
 
-       v = (gchar *) value;
+       gboolean isref = FALSE;
+       uint32_t value_gchandle = 0;
+       gchar *v = NULL;
        if (!type->byref) {
                switch (type->type) {
                case MONO_TYPE_U1:
@@ -1960,8 +1994,9 @@ ves_icall_MonoField_SetValueInternal (MonoReflectionField *field, MonoObject *ob
                case MONO_TYPE_R8:
                case MONO_TYPE_VALUETYPE:
                case MONO_TYPE_PTR:
-                       if (v != NULL)
-                               v += sizeof (MonoObject);
+                       isref = FALSE;
+                       if (!MONO_HANDLE_IS_NULL (value))
+                               v = mono_object_handle_pin_unbox (value, &value_gchandle);
                        break;
                case MONO_TYPE_STRING:
                case MONO_TYPE_OBJECT:
@@ -1969,6 +2004,7 @@ ves_icall_MonoField_SetValueInternal (MonoReflectionField *field, MonoObject *ob
                case MONO_TYPE_ARRAY:
                case MONO_TYPE_SZARRAY:
                        /* Do nothing */
+                       isref = TRUE;
                        break;
                case MONO_TYPE_GENERICINST: {
                        MonoGenericClass *gclass = type->data.generic_class;
@@ -1976,26 +2012,29 @@ ves_icall_MonoField_SetValueInternal (MonoReflectionField *field, MonoObject *ob
 
                        if (mono_class_is_nullable (mono_class_from_mono_type (type))) {
                                MonoClass *nklass = mono_class_from_mono_type (type);
-                               MonoObject *nullable;
 
                                /* 
                                 * Convert the boxed vtype into a Nullable structure.
                                 * This is complicated by the fact that Nullables have
                                 * a variable structure.
                                 */
-                               nullable = mono_object_new_checked (mono_domain_get (), nklass, &error);
-                               if (!mono_error_ok (&error)) {
-                                       mono_error_set_pending_exception (&error);
-                                       return;
-                               }
+                               MonoObjectHandle nullable = MONO_HANDLE_NEW (MonoObject, mono_object_new_checked (mono_domain_get (), nklass, error));
+                               return_if_nok (error);
 
-                               mono_nullable_init ((guint8 *)mono_object_unbox (nullable), value, nklass);
+                               uint32_t nullable_gchandle = 0;
+                               guint8 *nval = mono_object_handle_pin_unbox (nullable, &nullable_gchandle);
+                               mono_nullable_init_from_handle (nval, value, nklass);
 
-                               v = (gchar *)mono_object_unbox (nullable);
+                               isref = FALSE;
+                               value_gchandle = nullable_gchandle;
+                               v = (gchar*)nval;
+                       }
+                       else {
+                               isref = !gclass->container_class->valuetype;
+                               if (!isref && !MONO_HANDLE_IS_NULL (value)) {
+                                       v = mono_object_handle_pin_unbox (value, &value_gchandle);
+                               };
                        }
-                       else 
-                               if (gclass->container_class->valuetype && (v != NULL))
-                                       v += sizeof (MonoObject);
                        break;
                }
                default:
@@ -2005,22 +2044,35 @@ ves_icall_MonoField_SetValueInternal (MonoReflectionField *field, MonoObject *ob
                }
        }
 
+       /* either value is a reference type, or it's a value type and we pinned
+        * it and v points to the payload. */
+       g_assert ((isref && v == NULL && value_gchandle == 0) ||
+                 (!isref && v != NULL && value_gchandle != 0) ||
+                 (!isref && v == NULL && value_gchandle == 0));
+
        if (type->attrs & FIELD_ATTRIBUTE_STATIC) {
-               MonoVTable *vtable = mono_class_vtable_full (mono_object_domain (field), cf->parent, &error);
-               if (!is_ok (&error)) {
-                       mono_error_set_pending_exception (&error);
-                       return;
-               }
+               MonoVTable *vtable = mono_class_vtable_full (MONO_HANDLE_DOMAIN (field), cf->parent, error);
+               if (!is_ok (error))
+                       goto leave;
+
                if (!vtable->initialized) {
-                       if (!mono_runtime_class_init_full (vtable, &error)) {
-                               mono_error_set_pending_exception (&error);
-                               return;
-                       }
+                       if (!mono_runtime_class_init_full (vtable, error))
+                               goto leave;
                }
-               mono_field_static_set_value (vtable, cf, v);
+               if (isref)
+                       mono_field_static_set_value (vtable, cf, MONO_HANDLE_RAW (value)); /* FIXME make mono_field_static_set_value work with handles for value */
+               else
+                       mono_field_static_set_value (vtable, cf, v);
        } else {
-               mono_field_set_value (obj, cf, v);
+
+               if (isref)
+                       MONO_HANDLE_SET_FIELD_REF (obj, cf, value);
+               else
+                       mono_field_set_value (MONO_HANDLE_RAW (obj), cf, v); /* FIXME: make mono_field_set_value take a handle for obj */
        }
+leave:
+       if (value_gchandle)
+               mono_gchandle_free (value_gchandle);
 }
 
 ICALL_EXPORT void
@@ -2165,7 +2217,7 @@ typedef enum {
 ICALL_EXPORT void
 ves_icall_MonoPropertyInfo_get_property_info (MonoReflectionPropertyHandle property, MonoPropertyInfo *info, PInfo req_info, MonoError *error)
 {
-       mono_error_init (error);
+       error_init (error);
        MonoDomain *domain = MONO_HANDLE_DOMAIN (property); 
        const MonoProperty *pproperty = MONO_HANDLE_GETVAL (property, property);
 
@@ -2227,75 +2279,83 @@ ves_icall_MonoPropertyInfo_get_property_info (MonoReflectionPropertyHandle prope
         */
 }
 
+static gboolean
+add_event_other_methods_to_array (MonoDomain *domain, MonoMethod *m, MonoArrayHandle dest, int i, MonoError *error)
+{
+       HANDLE_FUNCTION_ENTER ();
+       error_init (error);
+       MonoReflectionMethodHandle rm = mono_method_get_object_handle (domain, m, NULL, error);
+       if (!is_ok (error))
+               goto leave;
+       MONO_HANDLE_ARRAY_SETREF (dest, i, rm);
+leave:
+       HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
+}
+
 ICALL_EXPORT void
-ves_icall_MonoEventInfo_get_event_info (MonoReflectionMonoEvent *event, MonoEventInfo *info)
+ves_icall_MonoEventInfo_get_event_info (MonoReflectionMonoEventHandle ref_event, MonoEventInfo *info, MonoError *error)
 {
-       MonoError error;
-       MonoReflectionType *rt;
-       MonoReflectionMethod *rm;
-       MonoDomain *domain = mono_object_domain (event); 
+       error_init (error);
+       MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_event); 
 
-       rt = mono_type_get_object_checked (domain, &event->klass->byval_arg, &error);
-       if (mono_error_set_pending_exception (&error))
-               return;
+       MonoClass *klass = MONO_HANDLE_GETVAL (ref_event, klass);
+       MonoEvent *event = MONO_HANDLE_GETVAL (ref_event, event);
 
-       MONO_STRUCT_SETREF (info, reflected_type, rt);
+       MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, &klass->byval_arg, error);
+       return_if_nok (error);
+       MONO_STRUCT_SETREF (info, reflected_type, MONO_HANDLE_RAW (rt));
 
-       rt = mono_type_get_object_checked (domain, &event->event->parent->byval_arg, &error);
-       if (mono_error_set_pending_exception (&error))
-               return;
+       rt = mono_type_get_object_handle (domain, &event->parent->byval_arg, error);
+       return_if_nok (error);
+       MONO_STRUCT_SETREF (info, declaring_type, MONO_HANDLE_RAW (rt));
 
-       MONO_STRUCT_SETREF (info, declaring_type, rt);
+       MonoStringHandle ev_name = mono_string_new_handle (domain, event->name, error);
+       return_if_nok (error);
+       MONO_STRUCT_SETREF (info, name, MONO_HANDLE_RAW (ev_name));
 
-       MONO_STRUCT_SETREF (info, name, mono_string_new (domain, event->event->name));
-       info->attrs = event->event->attrs;
+       info->attrs = event->attrs;
 
-       if (event->event->add) {
-               rm = mono_method_get_object_checked (domain, event->event->add, NULL, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return;
+       MonoReflectionMethodHandle rm;
+       if (event->add) {
+               rm = mono_method_get_object_handle (domain, event->add, NULL, error);
+               return_if_nok (error);
        } else {
-               rm = NULL;
+               rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
        }
 
-       MONO_STRUCT_SETREF (info, add_method, rm);
+       MONO_STRUCT_SETREF (info, add_method, MONO_HANDLE_RAW (rm));
 
-       if (event->event->remove) {
-               rm = mono_method_get_object_checked (domain, event->event->remove, NULL, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return;
+       if (event->remove) {
+               rm = mono_method_get_object_handle (domain, event->remove, NULL, error);
+               return_if_nok (error);
        } else {
-               rm = NULL;
+               rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
        }
 
-       MONO_STRUCT_SETREF (info, remove_method, rm);
+       MONO_STRUCT_SETREF (info, remove_method, MONO_HANDLE_RAW (rm));
 
-       if (event->event->raise) {
-               rm = mono_method_get_object_checked (domain, event->event->raise, NULL, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return;
+       if (event->raise) {
+               rm = mono_method_get_object_handle (domain, event->raise, NULL, error);
+               return_if_nok (error);
        } else {
-               rm = NULL;
+               rm = MONO_HANDLE_NEW (MonoReflectionMethod, NULL);
        }
 
-       MONO_STRUCT_SETREF (info, raise_method, rm);
+       MONO_STRUCT_SETREF (info, raise_method, MONO_HANDLE_RAW (rm));
 
 #ifndef MONO_SMALL_CONFIG
-       if (event->event->other) {
+       if (event->other) {
                int i, n = 0;
-               while (event->event->other [n])
+               while (event->other [n])
                        n++;
-               MonoArray *info_arr = mono_array_new_checked (domain, mono_defaults.method_info_class, n, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return;
-               MONO_STRUCT_SETREF (info, other_methods, info_arr);
+               MonoArrayHandle info_arr = mono_array_new_handle (domain, mono_defaults.method_info_class, n, error);
+               return_if_nok (error);
+
+               MONO_STRUCT_SETREF (info, other_methods, MONO_HANDLE_RAW  (info_arr));
 
-               for (i = 0; i < n; i++) {
-                       rm = mono_method_get_object_checked (domain, event->event->other [i], NULL, &error);
-                       if (mono_error_set_pending_exception (&error))
+               for (i = 0; i < n; i++)
+                       if (!add_event_other_methods_to_array (domain, event->other [i], info_arr, i, error))
                                return;
-                       mono_array_setref (info->other_methods, i, rm);
-               }
        }               
 #endif
 }
@@ -2571,6 +2631,18 @@ ves_icall_RuntimeTypeHandle_IsPrimitive (MonoReflectionTypeHandle ref_type, Mono
        return (!type->byref && (((type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8)) || (type->type == MONO_TYPE_I) || (type->type == MONO_TYPE_U)));
 }
 
+ICALL_EXPORT MonoBoolean
+ves_icall_RuntimeTypeHandle_HasReferences (MonoReflectionTypeHandle ref_type, MonoError *error)
+{
+       error_init (error);
+       MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
+       MonoClass *klass;
+
+       klass = mono_class_from_mono_type (type);
+       mono_class_init (klass);
+       return klass->has_references;
+}
+
 ICALL_EXPORT MonoBoolean
 ves_icall_RuntimeTypeHandle_IsByRef (MonoReflectionTypeHandle ref_type, MonoError *error)
 {
@@ -3276,110 +3348,123 @@ ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this_arg, Mo
 }
 
 #ifndef DISABLE_REMOTING
-ICALL_EXPORT MonoObject *
-ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this_arg, MonoArray *params, MonoArray **outArgs) 
+static void
+internal_execute_field_getter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArray **outArgs, MonoError *error)
 {
-       MonoError error;
-       MonoDomain *domain = mono_object_domain (method); 
-       MonoMethod *m = method->method;
-       MonoMethodSignature *sig = mono_method_signature (m);
+       error_init (error);
        MonoArray *out_args;
-       MonoObject *result;
-       int i, j, outarg_count = 0;
-
-       if (m->klass == mono_defaults.object_class) {
-               if (!strcmp (m->name, "FieldGetter")) {
-                       MonoClass *k = mono_object_class (this_arg);
-                       MonoString *name;
-                       char *str;
+       MonoClass *k = mono_object_class (this_arg);
+       MonoString *name;
+       char *str;
                        
-                       /* If this is a proxy, then it must be a CBO */
-                       if (mono_class_is_transparent_proxy (k)) {
-                               MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
-                               this_arg = tp->rp->unwrapped_server;
-                               g_assert (this_arg);
-                               k = mono_object_class (this_arg);
-                       }
+       /* If this is a proxy, then it must be a CBO */
+       if (mono_class_is_transparent_proxy (k)) {
+               MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
+               this_arg = tp->rp->unwrapped_server;
+               g_assert (this_arg);
+               k = mono_object_class (this_arg);
+       }
                        
-                       name = mono_array_get (params, MonoString *, 1);
-                       str = mono_string_to_utf8_checked (name, &error);
-                       if (mono_error_set_pending_exception (&error))
-                               return NULL;
+       name = mono_array_get (params, MonoString *, 1);
+       str = mono_string_to_utf8_checked (name, error);
+       return_if_nok (error);
                
-                       do {
-                               MonoClassField* field = mono_class_get_field_from_name (k, str);
-                               if (field) {
-                                       g_free (str);
-                                       MonoClass *field_klass =  mono_class_from_mono_type (field->type);
-                                       if (field_klass->valuetype) {
-                                               result = mono_value_box_checked (domain, field_klass, (char *)this_arg + field->offset, &error);
-                                               if (mono_error_set_pending_exception (&error))
-                                                       return NULL;
-                                       } else 
-                                               result = (MonoObject *)*((gpointer *)((char *)this_arg + field->offset));
-                               
-                                       out_args = mono_array_new_checked (domain, mono_defaults.object_class, 1, &error);
-                                       if (mono_error_set_pending_exception (&error))
-                                               return NULL;
-                                       mono_gc_wbarrier_generic_store (outArgs, (MonoObject*) out_args);
-                                       mono_array_setref (out_args, 0, result);
-                                       return NULL;
-                               }
-                               k = k->parent;
-                       } while (k);
-
+       do {
+               MonoClassField* field = mono_class_get_field_from_name (k, str);
+               if (field) {
                        g_free (str);
-                       g_assert_not_reached ();
+                       MonoClass *field_klass =  mono_class_from_mono_type (field->type);
+                       MonoObject *result;
+                       if (field_klass->valuetype) {
+                               result = mono_value_box_checked (domain, field_klass, (char *)this_arg + field->offset, error);
+                               return_if_nok (error);
+                       } else 
+                               result = (MonoObject *)*((gpointer *)((char *)this_arg + field->offset));
+
+                       out_args = mono_array_new_checked (domain, mono_defaults.object_class, 1, error);
+                       return_if_nok (error);
+                       mono_gc_wbarrier_generic_store (outArgs, (MonoObject*) out_args);
+                       mono_array_setref (out_args, 0, result);
+                       return;
+               }
+               k = k->parent;
+       } while (k);
 
-               } else if (!strcmp (m->name, "FieldSetter")) {
-                       MonoClass *k = mono_object_class (this_arg);
-                       MonoString *name;
-                       guint32 size;
-                       gint32 align;
-                       char *str;
+       g_free (str);
+       g_assert_not_reached ();
+}
+
+static void
+internal_execute_field_setter (MonoDomain *domain, MonoObject *this_arg, MonoArray *params, MonoArray **outArgs, MonoError *error)
+{
+       error_init (error);
+       MonoArray *out_args;
+       MonoClass *k = mono_object_class (this_arg);
+       MonoString *name;
+       guint32 size;
+       gint32 align;
+       char *str;
                        
-                       /* If this is a proxy, then it must be a CBO */
-                       if (mono_class_is_transparent_proxy (k)) {
-                               MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
-                               this_arg = tp->rp->unwrapped_server;
-                               g_assert (this_arg);
-                               k = mono_object_class (this_arg);
-                       }
+       /* If this is a proxy, then it must be a CBO */
+       if (mono_class_is_transparent_proxy (k)) {
+               MonoTransparentProxy *tp = (MonoTransparentProxy*) this_arg;
+               this_arg = tp->rp->unwrapped_server;
+               g_assert (this_arg);
+               k = mono_object_class (this_arg);
+       }
                        
-                       name = mono_array_get (params, MonoString *, 1);
-                       str = mono_string_to_utf8_checked (name, &error);
-                       if (mono_error_set_pending_exception (&error))
-                               return NULL;
+       name = mono_array_get (params, MonoString *, 1);
+       str = mono_string_to_utf8_checked (name, error);
+       return_if_nok (error);
                
-                       do {
-                               MonoClassField* field = mono_class_get_field_from_name (k, str);
-                               if (field) {
-                                       g_free (str);
-                                       MonoClass *field_klass =  mono_class_from_mono_type (field->type);
-                                       MonoObject *val = (MonoObject *)mono_array_get (params, gpointer, 2);
-
-                                       if (field_klass->valuetype) {
-                                               size = mono_type_size (field->type, &align);
-                                               g_assert (size == mono_class_value_size (field_klass, NULL));
-                                               mono_gc_wbarrier_value_copy ((char *)this_arg + field->offset, (char*)val + sizeof (MonoObject), 1, field_klass);
-                                       } else {
-                                               mono_gc_wbarrier_set_field (this_arg, (char*)this_arg + field->offset, val);
-                                       }
-                               
-                                       out_args = mono_array_new_checked (domain, mono_defaults.object_class, 0, &error);
-                                       if (mono_error_set_pending_exception (&error))
-                                               return NULL;
-                                       mono_gc_wbarrier_generic_store (outArgs, (MonoObject*) out_args);
+       do {
+               MonoClassField* field = mono_class_get_field_from_name (k, str);
+               if (field) {
+                       g_free (str);
+                       MonoClass *field_klass =  mono_class_from_mono_type (field->type);
+                       MonoObject *val = (MonoObject *)mono_array_get (params, gpointer, 2);
 
-                                       return NULL;
-                               }
+                       if (field_klass->valuetype) {
+                               size = mono_type_size (field->type, &align);
+                               g_assert (size == mono_class_value_size (field_klass, NULL));
+                               mono_gc_wbarrier_value_copy ((char *)this_arg + field->offset, (char*)val + sizeof (MonoObject), 1, field_klass);
+                       } else {
+                               mono_gc_wbarrier_set_field (this_arg, (char*)this_arg + field->offset, val);
+                       }
+
+                       out_args = mono_array_new_checked (domain, mono_defaults.object_class, 0, error);
+                       return_if_nok (error);
+                       mono_gc_wbarrier_generic_store (outArgs, (MonoObject*) out_args);
+                       return;
+               }
                                
-                               k = k->parent;
-                       } while (k);
+               k = k->parent;
+       } while (k);
 
-                       g_free (str);
-                       g_assert_not_reached ();
+       g_free (str);
+       g_assert_not_reached ();
+}
+
+ICALL_EXPORT MonoObject *
+ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this_arg, MonoArray *params, MonoArray **outArgs) 
+{
+       MonoError error;
+       MonoDomain *domain = mono_object_domain (method); 
+       MonoMethod *m = method->method;
+       MonoMethodSignature *sig = mono_method_signature (m);
+       MonoArray *out_args;
+       MonoObject *result;
+       int i, j, outarg_count = 0;
 
+       if (m->klass == mono_defaults.object_class) {
+               if (!strcmp (m->name, "FieldGetter")) {
+                       internal_execute_field_getter (domain, this_arg, params, outArgs, &error);
+                       mono_error_set_pending_exception (&error);
+                       return NULL;
+               } else if (!strcmp (m->name, "FieldSetter")) {
+                       internal_execute_field_setter (domain, this_arg, params, outArgs, &error);
+                       mono_error_set_pending_exception (&error);
+                       return NULL;
                }
        }
 
@@ -5386,7 +5471,7 @@ image_get_type (MonoDomain *domain, MonoImage *image, MonoTableInfo *tdef, int t
 
                MONO_HANDLE_ARRAY_SETREF (res, count, rt);
        } else {
-               MonoException *ex = mono_error_convert_to_exception (error);
+               MonoException *ex = mono_error_convert_to_exception (&klass_error);
                MONO_HANDLE_ARRAY_SETRAW (exceptions, count, ex);
        }
        HANDLE_FUNCTION_RETURN ();
@@ -6085,6 +6170,7 @@ check_for_invalid_type (MonoClass *klass, MonoError *error)
        name = mono_type_get_full_name (klass);
        mono_error_set_type_load_name (error, name, g_strdup (""), "");
 }
+
 ICALL_EXPORT MonoReflectionTypeHandle
 ves_icall_RuntimeType_make_array_type (MonoReflectionTypeHandle ref_type, int rank, MonoError *error)
 {
@@ -6102,6 +6188,11 @@ ves_icall_RuntimeType_make_array_type (MonoReflectionTypeHandle ref_type, int ra
        else
                aklass = mono_bounded_array_class_get (klass, rank, TRUE);
 
+       if (mono_class_has_failure (aklass)) {
+               mono_error_set_for_class_failure (error, aklass);
+               return MONO_HANDLE_CAST (MonoReflectionType, NULL_HANDLE);
+       }
+
        MonoDomain *domain = MONO_HANDLE_DOMAIN (ref_type);
        return mono_type_get_object_handle (domain, &aklass->byval_arg, error);
 }
@@ -6145,99 +6236,92 @@ ves_icall_RuntimeType_MakePointerType (MonoReflectionTypeHandle ref_type, MonoEr
        return mono_type_get_object_handle (domain, &pklass->byval_arg, error);
 }
 
-ICALL_EXPORT MonoObject *
-ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
-                                                  MonoReflectionMethod *info, MonoBoolean throwOnBindFailure)
+ICALL_EXPORT MonoObjectHandle
+ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionTypeHandle ref_type, MonoObjectHandle target,
+                                                  MonoReflectionMethodHandle info, MonoBoolean throwOnBindFailure, MonoError *error)
 {
-       MonoError error;
-       MonoClass *delegate_class = mono_class_from_mono_type (type->type);
-       MonoObject *delegate;
+       MonoType *type = MONO_HANDLE_GETVAL (ref_type, type);
+       MonoClass *delegate_class = mono_class_from_mono_type (type);
        gpointer func;
-       MonoMethod *method = info->method;
+       MonoMethod *method = MONO_HANDLE_GETVAL (info, method);
        MonoMethodSignature *sig = mono_method_signature(method);
 
-       mono_class_init_checked (delegate_class, &error);
-       if (mono_error_set_pending_exception (&error))
-               return NULL;
+       mono_class_init_checked (delegate_class, error);
+       return_val_if_nok (error, NULL_HANDLE);
 
        if (!(delegate_class->parent == mono_defaults.multicastdelegate_class)) {
                /* FIXME improve this exception message */
-               mono_error_set_execution_engine (&error, "file %s: line %d (%s): assertion failed: (%s)", __FILE__, __LINE__,
+               mono_error_set_execution_engine (error, "file %s: line %d (%s): assertion failed: (%s)", __FILE__, __LINE__,
                                                 __func__,
                                                 "delegate_class->parent == mono_defaults.multicastdelegate_class");
-               mono_error_set_pending_exception (&error);
-               return NULL;
+               return NULL_HANDLE;
        }
 
        if (mono_security_core_clr_enabled ()) {
-               if (!mono_security_core_clr_ensure_delegate_creation (method, &error)) {
+               MonoError security_error;
+               if (!mono_security_core_clr_ensure_delegate_creation (method, &security_error)) {
                        if (throwOnBindFailure)
-                               mono_error_set_pending_exception (&error);
+                               mono_error_move (error, &security_error);
                        else
-                               mono_error_cleanup (&error);
-                       return NULL;
+                               mono_error_cleanup (&security_error);
+                       return NULL_HANDLE;
                }
        }
 
        if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
                if (!method->is_inflated) {
-                       mono_set_pending_exception(mono_get_exception_argument("method", " Cannot bind to the target method because its signature differs from that of the delegate type"));
-                       return NULL;
+                       mono_error_set_argument (error, "method", " Cannot bind to the target method because its signature differs from that of the delegate type");
+                       return NULL_HANDLE;
                }
        }
 
-       delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error);
-       if (mono_error_set_pending_exception (&error))
-               return NULL;
+       MonoObjectHandle delegate = MONO_HANDLE_NEW (MonoObject, mono_object_new_checked (MONO_HANDLE_DOMAIN (ref_type), delegate_class, error));
+       return_val_if_nok (error, NULL_HANDLE);
 
        if (method_is_dynamic (method)) {
                /* Creating a trampoline would leak memory */
-               func = mono_compile_method_checked (method, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return NULL;
+               func = mono_compile_method_checked (method, error);
+               return_val_if_nok (error, NULL_HANDLE);
        } else {
-               if (target && method->flags & METHOD_ATTRIBUTE_VIRTUAL && method->klass != mono_object_class (target))
-                       method = mono_object_get_virtual_method (target, method);
-               gpointer trampoline = mono_runtime_create_jump_trampoline (mono_domain_get (), method, TRUE, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return NULL;
+               if (!MONO_HANDLE_IS_NULL (target) && method->flags & METHOD_ATTRIBUTE_VIRTUAL && method->klass != mono_handle_class (target)) {
+                       method = mono_object_handle_get_virtual_method (target, method, error);
+                       return_val_if_nok (error, NULL_HANDLE);
+               }
+               gpointer trampoline = mono_runtime_create_jump_trampoline (mono_domain_get (), method, TRUE, error);
+               return_val_if_nok (error, NULL_HANDLE);
                func = mono_create_ftnptr (mono_domain_get (), trampoline);
        }
 
-       mono_delegate_ctor_with_method (delegate, target, func, method, &error);
-       if (mono_error_set_pending_exception (&error))
-               return NULL;
+       mono_delegate_ctor_with_method (delegate, target, func, method, error);
+       return_val_if_nok (error, NULL_HANDLE);
        return delegate;
 }
 
-ICALL_EXPORT MonoMulticastDelegate *
-ves_icall_System_Delegate_AllocDelegateLike_internal (MonoDelegate *delegate)
+ICALL_EXPORT MonoMulticastDelegateHandle
+ves_icall_System_Delegate_AllocDelegateLike_internal (MonoDelegateHandle delegate, MonoError *error)
 {
-       MonoError error;
-       MonoMulticastDelegate *ret;
+       error_init (error);
 
-       g_assert (mono_class_has_parent (mono_object_class (delegate), mono_defaults.multicastdelegate_class));
+       MonoClass *klass = mono_handle_class (delegate);
+       g_assert (mono_class_has_parent (klass, mono_defaults.multicastdelegate_class));
 
-       ret = (MonoMulticastDelegate*) mono_object_new_checked (mono_object_domain (delegate), mono_object_class (delegate), &error);
-       if (mono_error_set_pending_exception (&error))
-               return NULL;
+       MonoMulticastDelegateHandle ret = MONO_HANDLE_NEW (MonoMulticastDelegate,  mono_object_new_checked (MONO_HANDLE_DOMAIN (delegate), klass, error));
+       return_val_if_nok (error, MONO_HANDLE_CAST (MonoMulticastDelegate, NULL_HANDLE));
 
-       ret->delegate.invoke_impl = mono_runtime_create_delegate_trampoline (mono_object_class (delegate));
+       MONO_HANDLE_SETVAL (MONO_HANDLE_CAST (MonoDelegate, ret), invoke_impl, gpointer, mono_runtime_create_delegate_trampoline (klass));
 
        return ret;
 }
 
-ICALL_EXPORT MonoReflectionMethod*
-ves_icall_System_Delegate_GetVirtualMethod_internal (MonoDelegate *delegate)
+ICALL_EXPORT MonoReflectionMethodHandle
+ves_icall_System_Delegate_GetVirtualMethod_internal (MonoDelegateHandle delegate, MonoError *error)
 {
-       MonoReflectionMethod *ret = NULL;
-       MonoError error;
-       MonoMethod *m;
+       error_init (error);
 
-       m = mono_object_get_virtual_method (delegate->target, delegate->method);
-       ret = mono_method_get_object_checked (mono_domain_get (), m, m->klass, &error);
-       mono_error_set_pending_exception (&error);
-       return ret;
+       MonoObjectHandle delegate_target = MONO_HANDLE_NEW_GET (MonoObject, delegate, target);
+       MonoMethod *m = mono_object_handle_get_virtual_method (delegate_target, MONO_HANDLE_GETVAL (delegate, method), error);
+       return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
+       return mono_method_get_object_handle (mono_domain_get (), m, m->klass, error);
 }
 
 /* System.Buffer */
@@ -6381,6 +6465,8 @@ ICALL_EXPORT MonoReflectionType *
 ves_icall_Remoting_RealProxy_InternalGetProxyType (MonoTransparentProxy *tp)
 {
        MonoError error;
+       g_assert (tp != NULL && mono_object_class (tp) == mono_defaults.transparent_proxy_class);
+       g_assert (tp->remote_class != NULL && tp->remote_class->proxy_class != NULL);
        MonoReflectionType *ret = mono_type_get_object_checked (mono_object_domain (tp), &tp->remote_class->proxy_class->byval_arg, &error);
        mono_error_set_pending_exception (&error);
 
@@ -6390,19 +6476,21 @@ ves_icall_Remoting_RealProxy_InternalGetProxyType (MonoTransparentProxy *tp)
 
 /* System.Environment */
 
-MonoString*
-ves_icall_System_Environment_get_UserName (void)
+MonoStringHandle
+ves_icall_System_Environment_get_UserName (MonoError *error)
 {
+       error_init (error);
        /* using glib is more portable */
-       return mono_string_new (mono_domain_get (), g_get_user_name ());
+       return mono_string_new_handle (mono_domain_get (), g_get_user_name (), error);
 }
 
 #ifndef HOST_WIN32
-static MonoString *
-mono_icall_get_machine_name (void)
+static MonoStringHandle
+mono_icall_get_machine_name (MonoError *error)
 {
+       error_init (error);
 #if !defined(DISABLE_SOCKETS)
-       MonoString *result;
+       MonoStringHandle result;
        char *buf;
        int n;
 #if defined _SC_HOST_NAME_MAX
@@ -6414,22 +6502,23 @@ mono_icall_get_machine_name (void)
        
        if (gethostname (buf, n) == 0){
                buf [n] = 0;
-               result = mono_string_new (mono_domain_get (), buf);
+               result = mono_string_new_handle (mono_domain_get (), buf, error);
        } else
-               result = NULL;
+               result = MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
        g_free (buf);
        
        return result;
 #else
-       return mono_string_new (mono_domain_get (), "mono");
+       return mono_string_new_handle (mono_domain_get (), "mono", error);
 #endif
 }
 #endif /* !HOST_WIN32 */
 
-ICALL_EXPORT MonoString *
-ves_icall_System_Environment_get_MachineName (void)
+ICALL_EXPORT MonoStringHandle
+ves_icall_System_Environment_get_MachineName (MonoError *error)
 {
-       return mono_icall_get_machine_name ();
+       error_init (error);
+       return mono_icall_get_machine_name (error);
 }
 
 #ifndef HOST_WIN32
@@ -6460,17 +6549,18 @@ ves_icall_System_Environment_get_Platform (void)
 }
 
 #ifndef HOST_WIN32
-static inline MonoString *
-mono_icall_get_new_line (void)
+static inline MonoStringHandle
+mono_icall_get_new_line (MonoError *error)
 {
-       return mono_string_new (mono_domain_get (), "\n");
+       error_init (error);
+       return mono_string_new_handle (mono_domain_get (), "\n", error);
 }
 #endif /* !HOST_WIN32 */
 
-ICALL_EXPORT MonoString *
-ves_icall_System_Environment_get_NewLine (void)
+ICALL_EXPORT MonoStringHandle
+ves_icall_System_Environment_get_NewLine (MonoError *error)
 {
-       return mono_icall_get_new_line ();
+       return mono_icall_get_new_line (error);
 }
 
 #ifndef HOST_WIN32
@@ -6501,7 +6591,7 @@ ves_icall_System_Environment_GetIs64BitOperatingSystem (void)
 ICALL_EXPORT MonoStringHandle
 ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_name, MonoError *error)
 {
-       const gchar *value;
+       gchar *value;
 
        if (utf8_name == NULL)
                return NULL_HANDLE_STRING;
@@ -6511,7 +6601,9 @@ ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_na
        if (value == 0)
                return NULL_HANDLE_STRING;
        
-       return mono_string_new_handle (mono_domain_get (), value, error);
+       MonoStringHandle res = mono_string_new_handle (mono_domain_get (), value, error);
+       g_free (value);
+       return res;
 }
 
 /*
@@ -6539,7 +6631,7 @@ char **environ;
 #endif
 
 ICALL_EXPORT MonoArray *
-ves_icall_System_Environment_GetCoomandLineArgs (void)
+ves_icall_System_Environment_GetCommandLineArgs (void)
 {
        MonoError error;
        MonoArray *result = mono_runtime_get_main_args_checked (&error);
@@ -6549,29 +6641,32 @@ ves_icall_System_Environment_GetCoomandLineArgs (void)
 
 #ifndef HOST_WIN32
 static MonoArray *
-mono_icall_get_environment_variable_names (void)
+mono_icall_get_environment_variable_names (MonoError *error)
 {
-       MonoError error;
        MonoArray *names;
        MonoDomain *domain;
        MonoString *str;
        gchar **e, **parts;
        int n;
 
+       error_init (error);
        n = 0;
        for (e = environ; *e != 0; ++ e)
                ++ n;
 
        domain = mono_domain_get ();
-       names = mono_array_new_checked (domain, mono_defaults.string_class, n, &error);
-       if (mono_error_set_pending_exception (&error))
-               return NULL;
+       names = mono_array_new_checked (domain, mono_defaults.string_class, n, error);
+       return_val_if_nok (error, NULL);
 
        n = 0;
        for (e = environ; *e != 0; ++ e) {
                parts = g_strsplit (*e, "=", 2);
                if (*parts != 0) {
-                       str = mono_string_new (domain, *parts);
+                       str = mono_string_new_checked (domain, *parts, error);
+                       if (!is_ok (error)) {
+                               g_strfreev (parts);
+                               return NULL;
+                       }
                        mono_array_setref (names, n, str);
                }
 
@@ -6587,7 +6682,10 @@ mono_icall_get_environment_variable_names (void)
 ICALL_EXPORT MonoArray *
 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
 {
-       return mono_icall_get_environment_variable_names ();
+       MonoError error;
+       MonoArray *result = mono_icall_get_environment_variable_names (&error);
+       mono_error_set_pending_exception (&error);
+       return result;
 }
 
 #ifndef HOST_WIN32
@@ -6631,11 +6729,6 @@ ves_icall_System_Environment_Exit (int result)
 {
        mono_environment_exitcode_set (result);
 
-/* FIXME: There are some cleanup hangs that should be worked out, but
- * if the program is going to exit, everything will be cleaned up when
- * NaCl exits anyway.
- */
-#ifndef __native_client__
        if (!mono_runtime_try_shutdown ())
                mono_thread_exit ();
 
@@ -6643,7 +6736,6 @@ ves_icall_System_Environment_Exit (int result)
        mono_thread_suspend_all_other_threads ();
 
        mono_runtime_quit ();
-#endif
 
        /* we may need to do some cleanup here... */
        exit (result);
@@ -6656,18 +6748,19 @@ ves_icall_System_Environment_GetGacPath (MonoError *error)
 }
 
 #ifndef HOST_WIN32
-static inline MonoString *
-mono_icall_get_windows_folder_path (int folder)
+static inline MonoStringHandle
+mono_icall_get_windows_folder_path (int folder, MonoError *error)
 {
+       error_init (error);
        g_warning ("ves_icall_System_Environment_GetWindowsFolderPath should only be called on Windows!");
-       return mono_string_new (mono_domain_get (), "");
+       return mono_string_new_handle (mono_domain_get (), "", error);
 }
 #endif /* !HOST_WIN32 */
 
-ICALL_EXPORT MonoString*
-ves_icall_System_Environment_GetWindowsFolderPath (int folder)
+ICALL_EXPORT MonoStringHandle
+ves_icall_System_Environment_GetWindowsFolderPath (int folder, MonoError *error)
 {
-       return mono_icall_get_windows_folder_path (folder);
+       return mono_icall_get_windows_folder_path (folder, error);
 }
 
 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
@@ -6784,9 +6877,10 @@ static const char *encodings [] = {
  * 1 at entry, and we can not compute a suitable code page number,
  * returns the code page as a string
  */
-ICALL_EXPORT MonoString*
-ves_icall_System_Text_EncodingHelper_InternalCodePage (gint32 *int_code_page
+ICALL_EXPORT MonoStringHandle
+ves_icall_System_Text_EncodingHelper_InternalCodePage (gint32 *int_code_page, MonoError *error)
 {
+       error_init (error);
        const char *cset;
        const char *p;
        char *c;
@@ -6828,9 +6922,9 @@ ves_icall_System_Text_EncodingHelper_InternalCodePage (gint32 *int_code_page)
        g_free (codepage);
        
        if (want_name && *int_code_page == -1)
-               return mono_string_new (mono_domain_get (), cset);
+               return mono_string_new_handle (mono_domain_get (), cset, error);
        else
-               return NULL;
+               return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
 }
 
 ICALL_EXPORT MonoBoolean
@@ -6846,17 +6940,19 @@ ves_icall_System_Environment_get_HasShutdownStarted (void)
 }
 
 #ifndef HOST_WIN32
-static inline void
-mono_icall_broadcast_setting_change (void)
+static inline MonoBoolean
+mono_icall_broadcast_setting_change (MonoError *error)
 {
-       return;
+       error_init (error);
+       return TRUE;
 }
 #endif /* !HOST_WIN32 */
 
 ICALL_EXPORT void
-ves_icall_System_Environment_BroadcastSettingChange (void)
+ves_icall_System_Environment_BroadcastSettingChange (MonoError *error)
 {
-       mono_icall_broadcast_setting_change ();
+       error_init (error);
+       mono_icall_broadcast_setting_change (error);
 }
 
 ICALL_EXPORT
@@ -6875,54 +6971,56 @@ ves_icall_System_Runtime_Versioning_VersioningHelper_GetRuntimeId (void)
 
 #ifndef DISABLE_REMOTING
 ICALL_EXPORT MonoBoolean
-ves_icall_IsTransparentProxy (MonoObject *proxy)
+ves_icall_IsTransparentProxy (MonoObjectHandle proxy, MonoError *error)
 {
-       if (!proxy)
+       error_init (error);
+       if (MONO_HANDLE_IS_NULL (proxy))
                return 0;
 
-       if (mono_object_is_transparent_proxy (proxy))
+       if (mono_class_is_transparent_proxy (mono_handle_class (proxy)))
                return 1;
 
        return 0;
 }
 
-ICALL_EXPORT MonoReflectionMethod *
+ICALL_EXPORT MonoReflectionMethodHandle
 ves_icall_Remoting_RemotingServices_GetVirtualMethod (
-       MonoReflectionType *rtype, MonoReflectionMethod *rmethod)
+       MonoReflectionTypeHandle rtype, MonoReflectionMethodHandle rmethod, MonoError *error)
 {
-       MonoReflectionMethod *ret = NULL;
-       MonoError error;
-
-       MonoClass *klass;
-       MonoMethod *method;
-       MonoMethod **vtable;
-       MonoMethod *res = NULL;
+       MonoReflectionMethodHandle ret = MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
 
-       MONO_CHECK_ARG_NULL (rtype, NULL);
-       MONO_CHECK_ARG_NULL (rmethod, NULL);
+       error_init (error);
+       if (MONO_HANDLE_IS_NULL (rtype)) {
+               mono_error_set_argument_null (error, "type", "");
+               return ret;
+       }
+       if (MONO_HANDLE_IS_NULL (rmethod)) {
+               mono_error_set_argument_null (error, "method", "");
+               return ret;
+       }
 
-       method = rmethod->method;
-       klass = mono_class_from_mono_type (rtype->type);
-       mono_class_init_checked (klass, &error);
-       if (mono_error_set_pending_exception (&error))
-               return NULL;
+       MonoMethod *method = MONO_HANDLE_GETVAL (rmethod, method);
+       MonoType *type = MONO_HANDLE_GETVAL (rtype, type);
+       MonoClass *klass = mono_class_from_mono_type (type);
+       mono_class_init_checked (klass, error);
+       return_val_if_nok (error, ret);
 
        if (MONO_CLASS_IS_INTERFACE (klass))
-               return NULL;
+               return ret;
 
        if (method->flags & METHOD_ATTRIBUTE_STATIC)
-               return NULL;
+               return ret;
 
        if ((method->flags & METHOD_ATTRIBUTE_FINAL) || !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
                if (klass == method->klass || mono_class_is_subclass_of (klass, method->klass, FALSE))
-                       return rmethod;
-               else
-                       return NULL;
+                       ret = rmethod;
+               return ret;
        }
 
        mono_class_setup_vtable (klass);
-       vtable = klass->vtable;
+       MonoMethod **vtable = klass->vtable;
 
+       MonoMethod *res = NULL;
        if (mono_class_is_interface (method->klass)) {
                gboolean variance_used = FALSE;
                /*MS fails with variant interfaces but it's the right thing to do anyway.*/
@@ -6931,33 +7029,27 @@ ves_icall_Remoting_RemotingServices_GetVirtualMethod (
                        res = vtable [offs + method->slot];
        } else {
                if (!(klass == method->klass || mono_class_is_subclass_of (klass, method->klass, FALSE)))
-                       return NULL;
+                       return ret;
 
                if (method->slot != -1)
                        res = vtable [method->slot];
        }
 
        if (!res)
-               return NULL;
+               return ret;
 
-       ret = mono_method_get_object_checked (mono_domain_get (), res, NULL, &error);
-       mono_error_set_pending_exception (&error);
+       ret = mono_method_get_object_handle (mono_domain_get (), res, NULL, error);
        return ret;
 }
 
 ICALL_EXPORT void
-ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionType *type, MonoBoolean enable)
+ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type, MonoBoolean enable, MonoError *error)
 {
-       MonoError error;
-       MonoClass *klass;
-       MonoVTable* vtable;
+       error_init (error);
 
-       klass = mono_class_from_mono_type (type->type);
-       vtable = mono_class_vtable_full (mono_domain_get (), klass, &error);
-       if (!is_ok (&error)) {
-               mono_error_set_pending_exception (&error);
-               return;
-       }
+       MonoClass *klass = mono_class_from_mono_type (MONO_HANDLE_GETVAL (type, type));
+       MonoVTable *vtable = mono_class_vtable_full (mono_domain_get (), klass, error);
+       return_if_nok (error);
 
        mono_vtable_set_is_remote (vtable, enable);
 }
@@ -6965,48 +7057,38 @@ ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (Mo
 #else /* DISABLE_REMOTING */
 
 ICALL_EXPORT void
-ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionType *type, MonoBoolean enable)
+ves_icall_System_Runtime_Activation_ActivationServices_EnableProxyActivation (MonoReflectionTypeHandle type, MonoBoolean enable, MonoError *error)
 {
+       error_init (error);
        g_assert_not_reached ();
 }
 
 #endif
 
-ICALL_EXPORT MonoObject *
-ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionType *type)
+ICALL_EXPORT MonoObjectHandle
+ves_icall_System_Runtime_Activation_ActivationServices_AllocateUninitializedClassInstance (MonoReflectionTypeHandle type, MonoError *error)
 {
-       MonoError error;
-       MonoClass *klass;
-       MonoDomain *domain;
-       MonoObject *ret;
+       error_init (error);
        
-       domain = mono_object_domain (type);
-       klass = mono_class_from_mono_type (type->type);
-       mono_class_init_checked (klass, &error);
-       if (mono_error_set_pending_exception (&error))
-               return NULL;
+       MonoDomain *domain = MONO_HANDLE_DOMAIN (type);
+       MonoClass *klass = mono_class_from_mono_type (MONO_HANDLE_GETVAL (type, type));
+       mono_class_init_checked (klass, error);
+       return_val_if_nok (error, NULL_HANDLE);
 
        if (MONO_CLASS_IS_INTERFACE (klass) || mono_class_is_abstract (klass)) {
-               mono_set_pending_exception (mono_get_exception_argument ("type", "Type cannot be instantiated"));
-               return NULL;
+               mono_error_set_argument (error, "type", "Type cannot be instantiated");
+               return NULL_HANDLE;
        }
 
        if (klass->rank >= 1) {
                g_assert (klass->rank == 1);
-               ret = (MonoObject *) mono_array_new_checked (domain, klass->element_class, 0, &error);
-               mono_error_set_pending_exception (&error);
-               return ret;
+               return MONO_HANDLE_CAST (MonoObject, mono_array_new_handle (domain, klass->element_class, 0, error));
        } else {
-               MonoVTable *vtable = mono_class_vtable_full (domain, klass, &error);
-               if (!is_ok (&error)) {
-                       mono_error_set_pending_exception (&error);
-                       return NULL;
-               }
-               /* Bypass remoting object creation check */
-               ret = mono_object_new_alloc_specific_checked (vtable, &error);
-               mono_error_set_pending_exception (&error);
+               MonoVTable *vtable = mono_class_vtable_full (domain, klass, error);
+               return_val_if_nok (error, NULL_HANDLE);
 
-               return ret;
+               /* Bypass remoting object creation check */
+               return MONO_HANDLE_NEW (MonoObject, mono_object_new_alloc_specific_checked (vtable, error));
        }
 }
 
@@ -7050,59 +7132,60 @@ ves_icall_System_IO_DriveInfo_GetDriveType (MonoString *root_path_name)
 #endif /* PLATFORM_NO_DRIVEINFO */
 
 ICALL_EXPORT gpointer
-ves_icall_RuntimeMethodHandle_GetFunctionPointer (MonoMethod *method)
+ves_icall_RuntimeMethodHandle_GetFunctionPointer (MonoMethod *method, MonoError *error)
 {
-       MonoError error;
-       gpointer result = mono_compile_method_checked (method, &error);
-       mono_error_set_pending_exception (&error);
-       return result;
+       error_init (error);
+       return mono_compile_method_checked (method, error);
 }
 
-ICALL_EXPORT MonoString *
-ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (void)
+ICALL_EXPORT MonoStringHandle
+ves_icall_System_Configuration_DefaultConfig_get_machine_config_path (MonoError *error)
 {
-       MonoString *mcpath;
+       error_init (error);
        gchar *path;
 
        path = g_build_path (G_DIR_SEPARATOR_S, mono_get_config_dir (), "mono", mono_get_runtime_info ()->framework_version, "machine.config", NULL);
 
        mono_icall_make_platform_path (path);
 
-       mcpath = mono_string_new (mono_domain_get (), path);
+       MonoStringHandle mcpath = mono_string_new_handle (mono_domain_get (), path, error);
        g_free (path);
 
+       mono_error_assert_ok (error);
+
        return mcpath;
 }
 
-/* this is an icall */
-static MonoString *
-get_bundled_app_config (void)
+static MonoStringHandle
+ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_app_config (MonoError *error)
 {
-       MonoError error;
+       error_init (error);
        const gchar *app_config;
        MonoDomain *domain;
-       MonoString *file;
        gchar *config_file_name, *config_file_path;
        gsize len, config_file_path_length, config_ext_length;
        gchar *module;
 
        domain = mono_domain_get ();
-       file = domain->setup->configuration_file;
-       if (!file || file->length == 0)
+       MonoStringHandle file = MONO_HANDLE_NEW (MonoString, domain->setup->configuration_file);
+       if (MONO_HANDLE_IS_NULL (file) || MONO_HANDLE_GETVAL (file, length) == 0)
                return NULL;
 
        // Retrieve config file and remove the extension
-       config_file_name = mono_string_to_utf8_checked (file, &error);
-       if (mono_error_set_pending_exception (&error))
-               return NULL;
+       config_file_name = mono_string_handle_to_utf8 (file, error);
+       return_val_if_nok (error, MONO_HANDLE_CAST (MonoString, NULL_HANDLE));
+
        config_file_path = mono_portability_find_file (config_file_name, TRUE);
        if (!config_file_path)
                config_file_path = config_file_name;
 
        config_file_path_length = strlen (config_file_path);
        config_ext_length = strlen (".config");
-       if (config_file_path_length <= config_ext_length)
-               return NULL;
+       if (config_file_path_length <= config_ext_length) {
+               if (config_file_name != config_file_path)
+                       g_free (config_file_name);
+               return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
+       }
 
        len = config_file_path_length - config_ext_length;
        module = (gchar *)g_malloc0 (len + 1);
@@ -7116,9 +7199,9 @@ get_bundled_app_config (void)
        g_free (config_file_path);
 
        if (!app_config)
-               return NULL;
+               return MONO_HANDLE_CAST (MonoString, NULL_HANDLE);
 
-       return mono_string_new (mono_domain_get (), app_config);
+       return mono_string_new_handle (mono_domain_get (), app_config, error);
 }
 
 static MonoStringHandle
@@ -7154,17 +7237,17 @@ ves_icall_System_Configuration_InternalConfigurationHost_get_bundled_machine_con
 }
 
 
-ICALL_EXPORT MonoString *
-ves_icall_System_Web_Util_ICalls_get_machine_install_dir (void)
+ICALL_EXPORT MonoStringHandle
+ves_icall_System_Web_Util_ICalls_get_machine_install_dir (MonoError *error)
 {
-       MonoString *ipath;
+       error_init (error);
        gchar *path;
 
        path = g_path_get_dirname (mono_get_config_dir ());
 
        mono_icall_make_platform_path (path);
 
-       ipath = mono_string_new (mono_domain_get (), path);
+       MonoStringHandle ipath = mono_string_new_handle (mono_domain_get (), path, error);
        g_free (path);
 
        return ipath;
@@ -7262,10 +7345,20 @@ ves_icall_MonoMethod_get_base_method (MonoReflectionMethodHandle m, gboolean def
 
        MonoMethod *base = mono_method_get_base_method (method, definition, error);
        return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
-       if (base == method)
-               return m;
-       else
-               return mono_method_get_object_handle (mono_domain_get (), base, NULL, error);
+       if (base == method) {
+               /* we want to short-circuit and return 'm' here. But we should
+                  return the same method object that
+                  mono_method_get_object_handle, below would return.  Since
+                  that call takes NULL for the reftype argument, it will take
+                  base->klass as the reflected type for the MonoMethod.  So we
+                  need to check that m also has base->klass as the reflected
+                  type. */
+               MonoReflectionTypeHandle orig_reftype = MONO_HANDLE_NEW_GET (MonoReflectionType, m, reftype);
+               MonoClass *orig_klass = mono_class_from_mono_type (MONO_HANDLE_GETVAL (orig_reftype, type));
+               if (base->klass == orig_klass)
+                       return m;
+       }
+       return mono_method_get_object_handle (mono_domain_get (), base, NULL, error);
 }
 
 ICALL_EXPORT MonoStringHandle
@@ -7319,12 +7412,6 @@ mono_ArgIterator_IntGetNextArg (MonoArgIterator *iter)
        iter->args = (guint8*)(((gsize)iter->args + (align) - 1) & ~(align - 1));
 #endif
        res.value = iter->args;
-#if defined(__native_client__) && SIZEOF_REGISTER == 8
-       /* Values are stored as 8 byte register sized objects, but 'value'
-        * is dereferenced as a pointer in other routines.
-        */
-       res.value = (char*)res.value + 4;
-#endif
 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
        if (arg_size <= sizeof (gpointer)) {
                int dummy;
@@ -7454,38 +7541,34 @@ prelink_method (MonoMethod *method, MonoError *error)
                return;
        mono_lookup_pinvoke_call (method, &exc_class, &exc_arg);
        if (exc_class) {
-               mono_error_set_exception_instance (error,
-                       mono_exception_from_name_msg (mono_defaults.corlib, "System", exc_class, exc_arg));
+               mono_error_set_generic_error (error, "System", exc_class, "%s", exc_arg);
                return;
        }
        /* create the wrapper, too? */
 }
 
 ICALL_EXPORT void
-ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethod *method)
+ves_icall_System_Runtime_InteropServices_Marshal_Prelink (MonoReflectionMethodHandle method, MonoError *error)
 {
-       MonoError error;
+       error_init (error);
 
-       prelink_method (method->method, &error);
-       mono_error_set_pending_exception (&error);
+       prelink_method (MONO_HANDLE_GETVAL (method, method), error);
 }
 
 ICALL_EXPORT void
-ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionType *type)
+ves_icall_System_Runtime_InteropServices_Marshal_PrelinkAll (MonoReflectionTypeHandle type, MonoError *error)
 {
-       MonoError error;
-       MonoClass *klass = mono_class_from_mono_type (type->type);
+       error_init (error);
+       MonoClass *klass = mono_class_from_mono_type (MONO_HANDLE_GETVAL (type, type));
        MonoMethod* m;
        gpointer iter = NULL;
 
-       mono_class_init_checked (klass, &error);
-       if (mono_error_set_pending_exception (&error))
-               return;
+       mono_class_init_checked (klass, error);
+       return_if_nok (error);
 
        while ((m = mono_class_get_methods (klass, &iter))) {
-               prelink_method (m, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return;
+               prelink_method (m, error);
+               return_if_nok (error);
        }
 }
 
@@ -7677,60 +7760,48 @@ property_info_get_default_value (MonoReflectionProperty *property)
 }
 
 ICALL_EXPORT MonoBoolean
-custom_attrs_defined_internal (MonoObject *obj, MonoReflectionType *attr_type)
+ves_icall_MonoCustomAttrs_IsDefinedInternal (MonoObjectHandle obj, MonoReflectionTypeHandle attr_type, MonoError *error)
 {
-       MonoError error;
-       MonoClass *attr_class = mono_class_from_mono_type (attr_type->type);
-       MonoCustomAttrInfo *cinfo;
-       gboolean found;
+       error_init (error);
+       MonoClass *attr_class = mono_class_from_mono_type (MONO_HANDLE_GETVAL (attr_type, type));
 
-       mono_class_init_checked (attr_class, &error);
-       if (mono_error_set_pending_exception (&error))
-               return FALSE;
+       mono_class_init_checked (attr_class, error);
+       return_val_if_nok (error, FALSE);
+
+       MonoCustomAttrInfo *cinfo = mono_reflection_get_custom_attrs_info_checked (obj, error);
+       return_val_if_nok (error, FALSE);
 
-       cinfo = mono_reflection_get_custom_attrs_info_checked (obj, &error);
-       if (!is_ok (&error)) {
-               mono_error_set_pending_exception (&error);
-               return FALSE;
-       }
        if (!cinfo)
                return FALSE;
-       found = mono_custom_attrs_has_attr (cinfo, attr_class);
+       gboolean found = mono_custom_attrs_has_attr (cinfo, attr_class);
        if (!cinfo->cached)
                mono_custom_attrs_free (cinfo);
        return found;
 }
 
-ICALL_EXPORT MonoArray*
-custom_attrs_get_by_type (MonoObject *obj, MonoReflectionType *attr_type)
+ICALL_EXPORT MonoArrayHandle
+ves_icall_MonoCustomAttrs_GetCustomAttributesInternal (MonoObjectHandle obj, MonoReflectionTypeHandle attr_type, mono_bool pseudoattrs, MonoError *error)
 {
-       MonoClass *attr_class = attr_type ? mono_class_from_mono_type (attr_type->type) : NULL;
-       MonoArray *res;
-       MonoError error;
+       MonoClass *attr_class;
+       if (MONO_HANDLE_IS_NULL (attr_type))
+               attr_class = NULL;
+       else
+               attr_class = mono_class_from_mono_type (MONO_HANDLE_GETVAL (attr_type, type));
 
        if (attr_class) {
-               mono_class_init_checked (attr_class, &error);
-               if (mono_error_set_pending_exception (&error))
-                       return NULL;
-       }
-
-       res = mono_reflection_get_custom_attrs_by_type (obj, attr_class, &error);
-       if (!mono_error_ok (&error)) {
-               mono_error_set_pending_exception (&error);
-               return NULL;
+               mono_class_init_checked (attr_class, error);
+               if (!is_ok (error))
+                       return MONO_HANDLE_CAST (MonoArray, NULL_HANDLE);
        }
 
-       return res;
+       return mono_reflection_get_custom_attrs_by_type_handle (obj, attr_class, error);
 }
 
-ICALL_EXPORT MonoArray*
-ves_icall_MonoCustomAttrs_GetCustomAttributesDataInternal (MonoObject *obj)
+ICALL_EXPORT MonoArrayHandle
+ves_icall_MonoCustomAttrs_GetCustomAttributesDataInternal (MonoObjectHandle obj, MonoError *error)
 {
-       MonoError error;
-       MonoArray *result;
-       result = mono_reflection_get_custom_attrs_data_checked (obj, &error);
-       mono_error_set_pending_exception (&error);
-       return result;
+       error_init (error);
+       return mono_reflection_get_custom_attrs_data_checked (obj, error);
 }
 
 
@@ -7801,7 +7872,7 @@ ves_icall_System_Runtime_InteropServices_Marshal_GetRawIUnknownForComObjectNoAdd
 }
 
 ICALL_EXPORT MonoObject*
-ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_GetRestrictedErrorInfo()
+ves_icall_System_Runtime_InteropServices_WindowsRuntime_UnsafeNativeMethods_GetRestrictedErrorInfo(void)
 {
        mono_set_pending_exception(mono_get_exception_not_implemented("System.Runtime.InteropServices.WindowsRuntime.UnsafeNativeMethods.GetRestrictedErrorInfo internal call is not implemented."));
        return NULL;
@@ -8062,12 +8133,12 @@ mono_icall_cleanup (void)
 
 /**
  * mono_add_internal_call:
- * @name: method specification to surface to the managed world
- * @method: pointer to a C method to invoke when the method is called
+ * \param name method specification to surface to the managed world
+ * \param method pointer to a C method to invoke when the method is called
  *
- * This method surfaces the C function pointed by @method as a method
+ * This method surfaces the C function pointed by \p method as a method
  * that has been surfaced in managed code with the method specified in
- * @name as an internal call.
+ * \p name as an internal call.
  *
  * Internal calls are surfaced to all app domains loaded and they are
  * accessibly by a type with the specified name.
@@ -8078,15 +8149,16 @@ mono_icall_cleanup (void)
  *
  * For example, the following are all valid declarations:
  *
- * "MyApp.Services.ScriptService:Accelerate"
- * "MyApp.Services.ScriptService:Slowdown(int,bool)"
+ * \c MyApp.Services.ScriptService:Accelerate
+ *
+ * \c MyApp.Services.ScriptService:Slowdown(int,bool)
  *
  * You use method parameters in cases where there might be more than
  * one surface method to managed code.  That way you can register different
  * internal calls for different method overloads.
  *
  * The internal calls are invoked with no marshalling.   This means that .NET
- * types like System.String are exposed as `MonoString *` parameters.   This is
+ * types like \c System.String are exposed as \c MonoString* parameters.   This is
  * different than the way that strings are surfaced in P/Invoke.
  *
  * For more information on how the parameters are marshalled, see the
@@ -8246,11 +8318,10 @@ no_icall_table (void)
 
 /**
  * mono_lookup_internal_call_full:
- * @method: the method to look up
- * @uses_handles: out argument if method needs handles around managed objects.
- *
- * Returns a pointer to the icall code for the given method.  If
- * uses_handles is not NULL, it will be set to TRUE if the method
+ * \param method the method to look up
+ * \param uses_handles out argument if method needs handles around managed objects.
+ * \returns a pointer to the icall code for the given method.  If
+ * \p uses_handles is not NULL, it will be set to TRUE if the method
  * needs managed objects wrapped using the infrastructure in handle.h
  *
  * If the method is not found, warns and returns NULL.
@@ -8379,6 +8450,9 @@ mono_lookup_internal_call_full (MonoMethod *method, mono_bool *uses_handles)
 #endif
 }
 
+/**
+ * mono_lookup_internal_call:
+ */
 gpointer
 mono_lookup_internal_call (MonoMethod *method)
 {