[docs] Update formatting in mono-api-security.
[mono.git] / mono / metadata / remoting.c
index 2963b62681f5a0177329b669606eb4b685fb4e16..aee1b27ca833f4bf7646a6627711920bfcb09de8 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * remoting.c: Remoting support
+/**
+ * \file
+ * Remoting support
  * 
  * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
 
 #include "config.h"
 
+#include "mono/metadata/handle.h"
 #include "mono/metadata/remoting.h"
 #include "mono/metadata/marshal.h"
+#include "mono/metadata/marshal-internals.h"
 #include "mono/metadata/abi-details.h"
 #include "mono/metadata/cominterop.h"
 #include "mono/metadata/tabledefs.h"
@@ -68,9 +71,9 @@ 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 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;
@@ -411,7 +414,7 @@ mono_remoting_wrapper (MonoMethod *method, gpointer *params)
                goto fail;
 
        if (exc) {
-               mono_error_init (&error);
+               error_init (&error);
                mono_error_set_exception_instance (&error, (MonoException *)exc);
                goto fail;
        }
@@ -432,6 +435,9 @@ fail:
 } 
 
 
+/**
+ * mono_marshal_get_remoting_invoke:
+ */
 MonoMethod *
 mono_marshal_get_remoting_invoke (MonoMethod *method)
 {
@@ -905,7 +911,8 @@ mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, in
        return res;
 }
 
-/* mono_marshal_get_xappdomain_invoke ()
+/**
+ * mono_marshal_get_xappdomain_invoke:
  * Generates a fast remoting wrapper for cross app domain calls.
  */
 MonoMethod *
@@ -1235,6 +1242,9 @@ mono_marshal_get_xappdomain_invoke (MonoMethod *method)
        return res;
 }
 
+/**
+ * mono_marshal_get_remoting_invoke_for_target:
+ */
 MonoMethod *
 mono_marshal_get_remoting_invoke_for_target (MonoMethod *method, MonoRemotingTarget target_type)
 {
@@ -1267,6 +1277,9 @@ mono_marshal_load_remoting_wrapper (MonoRealProxy *rp, MonoMethod *method)
        return compiled_ptr;
 }
 
+/**
+ * mono_marshal_get_remoting_invoke_with_check:
+ */
 MonoMethod *
 mono_marshal_get_remoting_invoke_with_check (MonoMethod *method)
 {
@@ -1328,13 +1341,14 @@ mono_marshal_get_remoting_invoke_with_check (MonoMethod *method)
        return res;
 }
 
-/*
+/**
  * mono_marshal_get_ldfld_wrapper:
- * @type: the type of the field
+ * \param type the type of the field
  *
  * This method generates a function which can be use to load a field with type
- * @type from an object. The generated function has the following signature:
- * <@type> ldfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset)
+ * \p type from an object. The generated function has the following signature:
+ *
+ * <code><i>type</i> ldfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset)</code>
  */
 MonoMethod *
 mono_marshal_get_ldfld_wrapper (MonoType *type)
@@ -1622,13 +1636,14 @@ mono_marshal_get_ldflda_wrapper (MonoType *type)
 }
 
 
-/*
+/**
  * mono_marshal_get_stfld_wrapper:
- * @type: the type of the field
+ * \param type the type of the field
  *
  * This method generates a function which can be use to store a field with type
- * @type. The generated function has the following signature:
- * void stfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset, <@type> val)
+ * \p type. The generated function has the following signature:
+ *
+ * <code>void stfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset, <i>type</i> val)</code>
  */
 MonoMethod *
 mono_marshal_get_stfld_wrapper (MonoType *type)
@@ -1768,6 +1783,9 @@ mono_marshal_get_stfld_wrapper (MonoType *type)
        return res;
 }
 
+/**
+ * mono_marshal_get_proxy_cancast:
+ */
 MonoMethod *
 mono_marshal_get_proxy_cancast (MonoClass *klass)
 {
@@ -1910,19 +1928,43 @@ mono_get_xdomain_marshal_type (MonoType *t)
        return MONO_MARSHAL_SERIALIZE;
 }
 
-/* mono_marshal_xdomain_copy_value
- * Makes a copy of "val" suitable for the current domain.
+/* Replace the given array element by a copy in the current domain */
+static gboolean
+xdomain_copy_array_element_inplace (MonoArrayHandle arr, int i, MonoError *error)
+{
+       HANDLE_FUNCTION_ENTER ();
+       error_init (error);
+       MonoObjectHandle item = MONO_HANDLE_NEW (MonoObject, NULL);
+       MONO_HANDLE_ARRAY_GETREF (item, arr, i);
+       
+       MonoObjectHandle item_copy = mono_marshal_xdomain_copy_value_handle (item, error);
+       if (!is_ok (error))
+               goto leave;
+       MONO_HANDLE_ARRAY_SETREF (arr, i, item_copy);
+leave:
+       HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
+}
+
+/**
+ * mono_marshal_xdomain_copy_value_handle:
+ * \param val The value to copy.
+ * \param error set on failure.
+ * Makes a copy of \p val suitable for the current domain.
+ * On failure returns NULL and sets \p error.
  */
-MonoObject *
-mono_marshal_xdomain_copy_value (MonoObject *val, MonoError *error)
+MonoObjectHandle
+mono_marshal_xdomain_copy_value_handle (MonoObjectHandle val, MonoError *error)
 {
-       mono_error_init (error);
-       MonoDomain *domain;
-       if (val == NULL) return NULL;
+       error_init (error);
+       MonoObjectHandle result = MONO_HANDLE_NEW (MonoObject, NULL);
+       if (MONO_HANDLE_IS_NULL (val))
+               goto leave;
+
+       MonoDomain *domain = mono_domain_get ();
 
-       domain = mono_domain_get ();
+       MonoClass *klass = mono_handle_class (val);
 
-       switch (mono_object_class (val)->byval_arg.type) {
+       switch (klass->byval_arg.type) {
        case MONO_TYPE_VOID:
                g_assert_not_reached ();
                break;
@@ -1938,40 +1980,63 @@ mono_marshal_xdomain_copy_value (MonoObject *val, MonoError *error)
        case MONO_TYPE_U8:
        case MONO_TYPE_R4:
        case MONO_TYPE_R8: {
-               MonoObject *res = mono_value_box_checked (domain, mono_object_class (val), ((char*)val) + sizeof(MonoObject), error);
-               return res;
-
+               uint32_t gchandle = mono_gchandle_from_handle (val, TRUE);
+               MonoObjectHandle res = MONO_HANDLE_NEW (MonoObject, mono_value_box_checked (domain, klass, ((char*)val) + sizeof(MonoObject), error)); /* FIXME use handles in mono_value_box_checked */
+               mono_gchandle_free (gchandle);
+               if (!is_ok (error))
+                       goto leave;
+               MONO_HANDLE_ASSIGN (result, res);
+               break;
        }
        case MONO_TYPE_STRING: {
-               MonoString *str = (MonoString *) val;
-               MonoObject *res = NULL;
-               res = (MonoObject *) mono_string_new_utf16_checked (domain, mono_string_chars (str), mono_string_length (str), error);
-               return res;
+               MonoStringHandle str = MONO_HANDLE_CAST (MonoString, val);
+               uint32_t gchandle = mono_gchandle_from_handle (val, TRUE);
+               MonoStringHandle res = mono_string_new_utf16_handle (domain, mono_string_chars (MONO_HANDLE_RAW (str)), mono_string_handle_length (str), error);
+               mono_gchandle_free (gchandle);
+               if (!is_ok (error))
+                       goto leave;
+               MONO_HANDLE_ASSIGN (result, res);
+               break;
        }
        case MONO_TYPE_ARRAY:
        case MONO_TYPE_SZARRAY: {
-               MonoArray *acopy;
-               MonoXDomainMarshalType mt = mono_get_xdomain_marshal_type (&(mono_object_class (val)->element_class->byval_arg));
-               if (mt == MONO_MARSHAL_SERIALIZE) return NULL;
-               acopy = mono_array_clone_in_domain (domain, (MonoArray *) val, error);
-               return_val_if_nok (error, NULL);
+               MonoArrayHandle arr = MONO_HANDLE_CAST (MonoArray, val);
+               MonoXDomainMarshalType mt = mono_get_xdomain_marshal_type (&klass->element_class->byval_arg);
+               if (mt == MONO_MARSHAL_SERIALIZE)
+                       goto leave;
+               MonoArrayHandle acopy = mono_array_clone_in_domain (domain, arr, error);
+               if (!is_ok (error))
+                       goto leave;
 
                if (mt == MONO_MARSHAL_COPY) {
-                       int i, len = mono_array_length (acopy);
+                       int i, len = mono_array_handle_length (acopy);
                        for (i = 0; i < len; i++) {
-                               MonoObject *item = (MonoObject *)mono_array_get (acopy, gpointer, i);
-                               MonoObject *item_copy = mono_marshal_xdomain_copy_value (item, error);
-                               return_val_if_nok (error, NULL);
-                               mono_array_setref (acopy, i, item_copy);
+                               if (!xdomain_copy_array_element_inplace (acopy, i, error))
+                                       goto leave;
                        }
                }
-               return (MonoObject *) acopy;
+               MONO_HANDLE_ASSIGN (result, acopy);
+               break;
        }
        default:
                break;
        }
 
-       return NULL;
+leave:
+       return result;
+}
+
+/* mono_marshal_xdomain_copy_value
+ * Makes a copy of "val" suitable for the current domain.
+ */
+MonoObject*
+mono_marshal_xdomain_copy_value (MonoObject* val_raw, MonoError *error)
+{
+       HANDLE_FUNCTION_ENTER ();
+       /* FIXME callers of mono_marshal_xdomain_copy_value should use handles */
+       MONO_HANDLE_DCL (MonoObject, val);
+       MonoObjectHandle result = mono_marshal_xdomain_copy_value_handle (val, error);
+       HANDLE_FUNCTION_RETURN_OBJ (result);
 }
 
 /* mono_marshal_xdomain_copy_value