Merge pull request #2819 from BrzVlad/fix-major-log
[mono.git] / mono / metadata / remoting.c
index cbe8b3add942c42519b415dfbec92474e5ae30f1..6555cdec7a0d49f1e7da05a0cd841ca99bf47139 100644 (file)
@@ -5,6 +5,7 @@
  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  * Copyright 2011-2014 Xamarin, Inc (http://www.xamarin.com)
  *
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include "config.h"
@@ -16,6 +17,7 @@
 #include "mono/metadata/tabledefs.h"
 #include "mono/metadata/exception.h"
 #include "mono/metadata/debug-helpers.h"
+#include "mono/metadata/reflection-internals.h"
 
 typedef enum {
        MONO_MARSHAL_NONE,                      /* No marshalling needed */
@@ -65,6 +67,11 @@ mono_marshal_xdomain_copy_out_value (MonoObject *src, MonoObject *dst);
 static MonoReflectionType *
 type_from_handle (MonoType *handle);
 
+/* Class lazy loading functions */
+static GENERATE_GET_CLASS_WITH_CACHE (remoting_services, System.Runtime.Remoting, RemotingServices)
+static GENERATE_GET_CLASS_WITH_CACHE (call_context, System.Runtime.Remoting.Messaging, CallContext)
+static GENERATE_GET_CLASS_WITH_CACHE (context, System.Runtime.Remoting.Contexts, Context)
+
 static mono_mutex_t remoting_mutex;
 static gboolean remoting_mutex_inited;
 
@@ -152,7 +159,7 @@ mono_remoting_marshal_init (void)
        byte_array_class = mono_array_class_get (mono_defaults.byte_class, 1);
 
 #ifndef DISABLE_JIT
-       klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting", "RemotingServices");
+       klass = mono_class_get_remoting_services_class ();
        method_rs_serialize = mono_class_get_method_from_name (klass, "SerializeCallData", -1);
        g_assert (method_rs_serialize);
        method_rs_deserialize = mono_class_get_method_from_name (klass, "DeserializeCallData", -1);
@@ -168,11 +175,11 @@ mono_remoting_marshal_init (void)
        method_exc_fixexc = mono_class_get_method_from_name (klass, "FixRemotingException", -1);
        g_assert (method_exc_fixexc);
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "CallContext");
+       klass = mono_class_get_call_context_class ();
        method_set_call_context = mono_class_get_method_from_name (klass, "SetCurrentCallContext", -1);
        g_assert (method_set_call_context);
 
-       klass = mono_class_from_name (mono_defaults.corlib, "System.Runtime.Remoting.Contexts", "Context");
+       klass = mono_class_get_context_class ();
        method_needs_context_sink = mono_class_get_method_from_name (klass, "get_NeedsContextSink", -1);
        g_assert (method_needs_context_sink);
 #endif 
@@ -199,11 +206,17 @@ mono_remoting_marshal_init (void)
 static MonoReflectionType *
 type_from_handle (MonoType *handle)
 {
+       MonoError error;
+       MonoReflectionType *ret;
        MonoDomain *domain = mono_domain_get (); 
        MonoClass *klass = mono_class_from_mono_type (handle);
 
        mono_class_init (klass);
-       return mono_type_get_object (domain, handle);
+
+       ret = mono_type_get_object_checked (domain, handle, &error);
+       mono_error_raise_exception (&error); /* FIXME don't raise here */
+
+       return ret;
 }
 
 #ifndef DISABLE_JIT
@@ -333,6 +346,7 @@ mono_remoting_mb_create_and_cache (MonoMethod *key, MonoMethodBuilder *mb,
 static MonoObject *
 mono_remoting_wrapper (MonoMethod *method, gpointer *params)
 {
+       MonoError error;
        MonoMethodMessage *msg;
        MonoTransparentProxy *this_obj;
        MonoObject *res, *exc;
@@ -360,9 +374,10 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
                                        mparams[i] = *((gpointer *)params [i]);
                                } else {
                                        /* runtime_invoke expects a boxed instance */
-                                       if (mono_class_is_nullable (mono_class_from_mono_type (sig->params [i])))
-                                               mparams[i] = mono_nullable_box ((guint8 *)params [i], klass);
-                                       else
+                                       if (mono_class_is_nullable (mono_class_from_mono_type (sig->params [i]))) {
+                                               mparams[i] = mono_nullable_box ((guint8 *)params [i], klass, &error);
+                                               mono_error_raise_exception (&error); /* FIXME don't raise here */
+                                       } else
                                                mparams[i] = params [i];
                                }
                        } else {
@@ -370,12 +385,16 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
                        }
                }
 
-               return mono_runtime_invoke (method, method->klass->valuetype? mono_object_unbox ((MonoObject*)this_obj): this_obj, mparams, NULL);
+               res = mono_runtime_invoke_checked (method, method->klass->valuetype? mono_object_unbox ((MonoObject*)this_obj): this_obj, mparams, &error);
+               mono_error_raise_exception (&error); /* FIXME don't raise here */
+
+               return res;
        }
 
        msg = mono_method_call_message_new (method, params, NULL, NULL, NULL);
 
-       res = mono_remoting_invoke ((MonoObject *)this_obj->rp, msg, &exc, &out_args);
+       res = mono_remoting_invoke ((MonoObject *)this_obj->rp, msg, &exc, &out_args, &error);
+       mono_error_raise_exception (&error); /* FIXME don't raise here */
 
        if (exc)
                mono_raise_exception ((MonoException *)exc);
@@ -401,7 +420,7 @@ mono_marshal_get_remoting_invoke (MonoMethod *method)
 
        /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
 #ifndef DISABLE_COM
-       if (mono_class_is_com_object (method->klass) || method->klass == mono_class_get_com_object_class ()) {
+       if (mono_class_is_com_object (method->klass) || method->klass == mono_class_try_get_com_object_class ()) {
                MonoVTable *vtable = mono_class_vtable (mono_domain_get (), method->klass);
                g_assert (vtable); /*FIXME do proper error handling*/
 
@@ -1970,6 +1989,7 @@ mono_get_xdomain_marshal_type (MonoType *t)
 MonoObject *
 mono_marshal_xdomain_copy_value (MonoObject *val)
 {
+       MonoError error;
        MonoDomain *domain;
        if (val == NULL) return NULL;
 
@@ -1991,11 +2011,17 @@ mono_marshal_xdomain_copy_value (MonoObject *val)
        case MONO_TYPE_U8:
        case MONO_TYPE_R4:
        case MONO_TYPE_R8: {
-               return mono_value_box (domain, mono_object_class (val), ((char*)val) + sizeof(MonoObject));
+               MonoObject *res = mono_value_box_checked (domain, mono_object_class (val), ((char*)val) + sizeof(MonoObject), &error);
+               mono_error_raise_exception (&error); /* FIXME don't raise here */
+               return res;
+
        }
        case MONO_TYPE_STRING: {
                MonoString *str = (MonoString *) val;
-               return (MonoObject *) mono_string_new_utf16 (domain, mono_string_chars (str), mono_string_length (str));
+               MonoObject *res = NULL;
+               res = (MonoObject *) mono_string_new_utf16_checked (domain, mono_string_chars (str), mono_string_length (str), &error);
+               mono_error_raise_exception (&error); /* FIXME don't raise here */
+               return res;
        }
        case MONO_TYPE_ARRAY:
        case MONO_TYPE_SZARRAY: {