5 * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
6 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
7 * Copyright 2011-2014 Xamarin, Inc (http://www.xamarin.com)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include "mono/metadata/handle.h"
15 #include "mono/metadata/remoting.h"
16 #include "mono/metadata/marshal.h"
17 #include "mono/metadata/marshal-internals.h"
18 #include "mono/metadata/abi-details.h"
19 #include "mono/metadata/cominterop.h"
20 #include "mono/metadata/tabledefs.h"
21 #include "mono/metadata/exception.h"
22 #include "mono/metadata/debug-helpers.h"
23 #include "mono/metadata/reflection-internals.h"
26 MONO_MARSHAL_NONE, /* No marshalling needed */
27 MONO_MARSHAL_COPY, /* Can be copied by value to the new domain */
28 MONO_MARSHAL_COPY_OUT, /* out parameter that needs to be copied back to the original instance */
29 MONO_MARSHAL_SERIALIZE /* Value needs to be serialized into the new domain */
30 } MonoXDomainMarshalType;
32 #ifndef DISABLE_REMOTING
34 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
38 #include "mono/cil/opcode.def"
43 struct _MonoRemotingMethods {
45 MonoMethod *invoke_with_check;
46 MonoMethod *xdomain_invoke;
47 MonoMethod *xdomain_dispatch;
50 typedef struct _MonoRemotingMethods MonoRemotingMethods;
53 mono_remoting_wrapper (MonoMethod *method, gpointer *params);
56 mono_marshal_set_domain_by_id (gint32 id, MonoBoolean push);
59 mono_marshal_check_domain_image (gint32 domain_id, MonoImage *image);
62 mono_upgrade_remote_class_wrapper (MonoReflectionType *rtype, MonoTransparentProxy *tproxy);
64 static MonoXDomainMarshalType
65 mono_get_xdomain_marshal_type (MonoType *t);
68 mono_marshal_xdomain_copy_out_value (MonoObject *src, MonoObject *dst);
70 static MonoReflectionType *
71 type_from_handle (MonoType *handle);
73 /* Class lazy loading functions */
74 static GENERATE_GET_CLASS_WITH_CACHE (remoting_services, "System.Runtime.Remoting", "RemotingServices")
75 static GENERATE_GET_CLASS_WITH_CACHE (call_context, "System.Runtime.Remoting.Messaging", "CallContext")
76 static GENERATE_GET_CLASS_WITH_CACHE (context, "System.Runtime.Remoting.Contexts", "Context")
78 static mono_mutex_t remoting_mutex;
79 static gboolean remoting_mutex_inited;
81 static MonoClass *byte_array_class;
83 static MonoMethod *method_rs_serialize, *method_rs_deserialize, *method_exc_fixexc, *method_rs_appdomain_target;
84 static MonoMethod *method_set_call_context, *method_needs_context_sink, *method_rs_serialize_exc;
88 mono_compile_method_icall (MonoMethod *method);
91 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
93 MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
95 mono_register_jit_icall (func, name, sig, save);
101 g_assert (remoting_mutex_inited);
102 mono_os_mutex_lock (&remoting_mutex);
106 remoting_unlock (void)
108 g_assert (remoting_mutex_inited);
109 mono_os_mutex_unlock (&remoting_mutex);
113 * Return the hash table pointed to by VAR, lazily creating it if neccesary.
116 get_cache (GHashTable **var, GHashFunc hash_func, GCompareFunc equal_func)
122 g_hash_table_new (hash_func, equal_func);
123 mono_memory_barrier ();
132 get_cache_full (GHashTable **var, GHashFunc hash_func, GCompareFunc equal_func, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
138 g_hash_table_new_full (hash_func, equal_func, key_destroy_func, value_destroy_func);
139 mono_memory_barrier ();
148 mono_remoting_init (void)
150 mono_os_mutex_init (&remoting_mutex);
151 remoting_mutex_inited = TRUE;
155 mono_remoting_marshal_init (void)
159 static gboolean module_initialized = FALSE;
160 static gboolean icalls_registered = FALSE;
162 if (module_initialized)
165 byte_array_class = mono_array_class_get (mono_defaults.byte_class, 1);
168 klass = mono_class_get_remoting_services_class ();
169 method_rs_serialize = mono_class_get_method_from_name (klass, "SerializeCallData", -1);
170 g_assert (method_rs_serialize);
171 method_rs_deserialize = mono_class_get_method_from_name (klass, "DeserializeCallData", -1);
172 g_assert (method_rs_deserialize);
173 method_rs_serialize_exc = mono_class_get_method_from_name (klass, "SerializeExceptionData", -1);
174 g_assert (method_rs_serialize_exc);
176 klass = mono_defaults.real_proxy_class;
177 method_rs_appdomain_target = mono_class_get_method_from_name (klass, "GetAppDomainTarget", -1);
178 g_assert (method_rs_appdomain_target);
180 klass = mono_defaults.exception_class;
181 method_exc_fixexc = mono_class_get_method_from_name (klass, "FixRemotingException", -1);
182 g_assert (method_exc_fixexc);
184 klass = mono_class_get_call_context_class ();
185 method_set_call_context = mono_class_get_method_from_name (klass, "SetCurrentCallContext", -1);
186 g_assert (method_set_call_context);
188 klass = mono_class_get_context_class ();
189 method_needs_context_sink = mono_class_get_method_from_name (klass, "get_NeedsContextSink", -1);
190 g_assert (method_needs_context_sink);
195 if (!icalls_registered) {
196 register_icall (type_from_handle, "type_from_handle", "object ptr", FALSE);
197 register_icall (mono_marshal_set_domain_by_id, "mono_marshal_set_domain_by_id", "int32 int32 int32", FALSE);
198 register_icall (mono_marshal_check_domain_image, "mono_marshal_check_domain_image", "int32 int32 ptr", FALSE);
199 register_icall (ves_icall_mono_marshal_xdomain_copy_value, "ves_icall_mono_marshal_xdomain_copy_value", "object object", FALSE);
200 register_icall (mono_marshal_xdomain_copy_out_value, "mono_marshal_xdomain_copy_out_value", "void object object", FALSE);
201 register_icall (mono_remoting_wrapper, "mono_remoting_wrapper", "object ptr ptr", FALSE);
202 register_icall (mono_upgrade_remote_class_wrapper, "mono_upgrade_remote_class_wrapper", "void object object", FALSE);
205 register_icall (mono_compile_method_icall, "mono_compile_method_icall", "ptr ptr", FALSE);
210 icalls_registered = TRUE;
212 mono_loader_unlock ();
214 module_initialized = TRUE;
217 /* This is an icall, it will return NULL and set pending exception on failure */
218 static MonoReflectionType *
219 type_from_handle (MonoType *handle)
222 MonoReflectionType *ret;
223 MonoDomain *domain = mono_domain_get ();
224 MonoClass *klass = mono_class_from_mono_type (handle);
226 mono_class_init (klass);
228 ret = mono_type_get_object_checked (domain, handle, &error);
229 mono_error_set_pending_exception (&error);
236 mono_mb_emit_proxy_check (MonoMethodBuilder *mb, int branch_code)
239 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoObject, vtable));
240 mono_mb_emit_byte (mb, CEE_LDIND_I);
241 mono_mb_emit_icon (mb, MONO_STRUCT_OFFSET (MonoVTable, klass));
242 mono_mb_emit_byte (mb, CEE_ADD);
243 mono_mb_emit_byte (mb, CEE_LDIND_I);
244 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
245 mono_mb_emit_byte (mb, CEE_MONO_CLASSCONST);
246 mono_mb_emit_i4 (mb, mono_mb_add_data (mb, mono_defaults.transparent_proxy_class));
247 pos = mono_mb_emit_branch (mb, branch_code);
252 mono_mb_emit_xdomain_check (MonoMethodBuilder *mb, int branch_code)
255 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoTransparentProxy, rp));
256 mono_mb_emit_byte (mb, CEE_LDIND_REF);
257 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoRealProxy, target_domain_id));
258 mono_mb_emit_byte (mb, CEE_LDIND_I4);
259 mono_mb_emit_icon (mb, -1);
260 pos = mono_mb_emit_branch (mb, branch_code);
265 mono_mb_emit_contextbound_check (MonoMethodBuilder *mb, int branch_code)
267 static int offset = -1;
271 mono_marshal_find_bitfield_offset (MonoClass, contextbound, &offset, &mask);
273 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
274 mono_mb_emit_byte (mb, CEE_LDIND_REF);
275 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
276 mono_mb_emit_byte (mb, CEE_LDIND_REF);
277 mono_mb_emit_ldflda (mb, offset);
278 mono_mb_emit_byte (mb, CEE_LDIND_U1);
279 mono_mb_emit_icon (mb, mask);
280 mono_mb_emit_byte (mb, CEE_AND);
281 mono_mb_emit_icon (mb, 0);
282 return mono_mb_emit_branch (mb, branch_code);
284 #endif /* !DISABLE_JIT */
286 static inline MonoMethod*
287 mono_marshal_remoting_find_in_cache (MonoMethod *method, int wrapper_type)
289 MonoMethod *res = NULL;
290 MonoRemotingMethods *wrps = NULL;
292 mono_marshal_lock_internal ();
293 if (mono_method_get_wrapper_cache (method)->remoting_invoke_cache)
294 wrps = (MonoRemotingMethods *)g_hash_table_lookup (mono_method_get_wrapper_cache (method)->remoting_invoke_cache, method);
297 switch (wrapper_type) {
298 case MONO_WRAPPER_REMOTING_INVOKE: res = wrps->invoke; break;
299 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: res = wrps->invoke_with_check; break;
300 case MONO_WRAPPER_XDOMAIN_INVOKE: res = wrps->xdomain_invoke; break;
301 case MONO_WRAPPER_XDOMAIN_DISPATCH: res = wrps->xdomain_dispatch; break;
305 /* it is important to do the unlock after the load from wrps, since in
306 * mono_remoting_mb_create_and_cache () we drop the marshal lock to be able
307 * to take the loader lock and some other thread may set the fields.
309 mono_marshal_unlock_internal ();
313 /* Create the method from the builder and place it in the cache */
314 static inline MonoMethod*
315 mono_remoting_mb_create_and_cache (MonoMethod *key, MonoMethodBuilder *mb,
316 MonoMethodSignature *sig, int max_stack, WrapperInfo *info)
318 MonoMethod **res = NULL;
319 MonoRemotingMethods *wrps;
322 cache = get_cache_full (&mono_method_get_wrapper_cache (key)->remoting_invoke_cache, mono_aligned_addr_hash, NULL, NULL, g_free);
324 mono_marshal_lock_internal ();
325 wrps = (MonoRemotingMethods *)g_hash_table_lookup (cache, key);
327 wrps = g_new0 (MonoRemotingMethods, 1);
328 g_hash_table_insert (cache, key, wrps);
331 switch (mb->method->wrapper_type) {
332 case MONO_WRAPPER_REMOTING_INVOKE: res = &wrps->invoke; break;
333 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: res = &wrps->invoke_with_check; break;
334 case MONO_WRAPPER_XDOMAIN_INVOKE: res = &wrps->xdomain_invoke; break;
335 case MONO_WRAPPER_XDOMAIN_DISPATCH: res = &wrps->xdomain_dispatch; break;
336 default: g_assert_not_reached (); break;
338 mono_marshal_unlock_internal ();
342 newm = mono_mb_create_method (mb, sig, max_stack);
344 mono_marshal_lock_internal ();
347 mono_marshal_set_wrapper_info (*res, info);
348 mono_marshal_unlock_internal ();
350 mono_marshal_unlock_internal ();
351 mono_free_method (newm);
359 mono_remoting_wrapper (MonoMethod *method, gpointer *params)
362 MonoMethodMessage *msg;
363 MonoTransparentProxy *this_obj;
364 MonoObject *res, *exc;
367 this_obj = *((MonoTransparentProxy **)params [0]);
370 g_assert (mono_object_is_transparent_proxy (this_obj));
372 /* skip the this pointer */
375 if (mono_class_is_contextbound (this_obj->remote_class->proxy_class) && this_obj->rp->context == (MonoObject *) mono_context_get ())
378 MonoMethodSignature *sig = mono_method_signature (method);
379 int count = sig->param_count;
380 gpointer* mparams = (gpointer*) alloca(count*sizeof(gpointer));
382 for (i=0; i<count; i++) {
383 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
384 if (klass->valuetype) {
385 if (sig->params [i]->byref) {
386 mparams[i] = *((gpointer *)params [i]);
388 /* runtime_invoke expects a boxed instance */
389 if (mono_class_is_nullable (mono_class_from_mono_type (sig->params [i]))) {
390 mparams[i] = mono_nullable_box ((guint8 *)params [i], klass, &error);
394 mparams[i] = params [i];
397 mparams[i] = *((gpointer**)params [i]);
401 res = mono_runtime_invoke_checked (method, method->klass->valuetype? mono_object_unbox ((MonoObject*)this_obj): this_obj, mparams, &error);
408 msg = mono_method_call_message_new (method, params, NULL, NULL, NULL, &error);
412 res = mono_remoting_invoke ((MonoObject *)this_obj->rp, msg, &exc, &out_args, &error);
418 mono_error_set_exception_instance (&error, (MonoException *)exc);
422 mono_method_return_message_restore (method, params, out_args, &error);
423 if (!is_ok (&error)) goto fail;
427 /* This icall will be called from managed code, and more over
428 * from a protected wrapper so interruptions such as pending
429 * exceptions will not be honored. (See
430 * is_running_protected_wrapper () in threads.c and
431 * mono_marshal_get_remoting_invoke () in remoting.c)
433 mono_error_raise_exception (&error); /* OK to throw, see note */
439 * mono_marshal_get_remoting_invoke:
442 mono_marshal_get_remoting_invoke (MonoMethod *method)
444 MonoMethodSignature *sig;
445 MonoMethodBuilder *mb;
452 if (method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE || method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE)
455 /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
457 if (mono_class_is_com_object (method->klass) || method->klass == mono_class_try_get_com_object_class ()) {
458 MonoVTable *vtable = mono_class_vtable (mono_domain_get (), method->klass);
459 g_assert (vtable); /*FIXME do proper error handling*/
461 if (!mono_vtable_is_remote (vtable)) {
462 return mono_cominterop_get_invoke (method);
467 sig = mono_signature_no_pinvoke (method);
469 /* we cant remote methods without this pointer */
473 if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_REMOTING_INVOKE)))
476 mono_remoting_marshal_init ();
478 mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE);
481 mb->method->save_lmf = 1;
483 params_var = mono_mb_emit_save_args (mb, sig, TRUE);
485 mono_mb_emit_ptr (mb, method);
486 mono_mb_emit_ldloc (mb, params_var);
487 mono_mb_emit_icall (mb, mono_remoting_wrapper);
488 // FIXME: this interrupt checkpoint code is a no-op since 'mb'
489 // is a MONO_WRAPPER_REMOTING_INVOKE, and
490 // mono_thread_interruption_checkpoint_request (FALSE)
491 // considers such wrappers "protected" and always returns
492 // NULL as if there's no pending interruption.
493 mono_marshal_emit_thread_interrupt_checkpoint (mb);
495 if (sig->ret->type == MONO_TYPE_VOID) {
496 mono_mb_emit_byte (mb, CEE_POP);
497 mono_mb_emit_byte (mb, CEE_RET);
499 mono_mb_emit_restore_result (mb, sig->ret);
503 info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
504 info->d.remoting.method = method;
505 res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16, info);
511 /* mono_marshal_xdomain_copy_out_value()
512 * Copies the contents of the src instance into the dst instance. src and dst
513 * must have the same type, and if they are arrays, the same size.
515 * This is an icall, it may use mono_error_set_pending_exception
518 mono_marshal_xdomain_copy_out_value (MonoObject *src, MonoObject *dst)
521 if (src == NULL || dst == NULL) return;
523 g_assert (mono_object_class (src) == mono_object_class (dst));
525 switch (mono_object_class (src)->byval_arg.type) {
526 case MONO_TYPE_ARRAY:
527 case MONO_TYPE_SZARRAY: {
528 int mt = mono_get_xdomain_marshal_type (&(mono_object_class (src)->element_class->byval_arg));
529 if (mt == MONO_MARSHAL_SERIALIZE) return;
530 if (mt == MONO_MARSHAL_COPY) {
531 int i, len = mono_array_length ((MonoArray *)dst);
532 for (i = 0; i < len; i++) {
533 MonoObject *item = (MonoObject *)mono_array_get ((MonoArray *)src, gpointer, i);
534 MonoObject *item_copy = mono_marshal_xdomain_copy_value (item, &error);
535 if (mono_error_set_pending_exception (&error))
537 mono_array_setref ((MonoArray *)dst, i, item_copy);
540 mono_array_full_copy ((MonoArray *)src, (MonoArray *)dst);
551 #if !defined (DISABLE_JIT)
553 mono_marshal_emit_xdomain_copy_value (MonoMethodBuilder *mb, MonoClass *pclass)
555 mono_mb_emit_icall (mb, ves_icall_mono_marshal_xdomain_copy_value);
556 mono_mb_emit_op (mb, CEE_CASTCLASS, pclass);
560 mono_marshal_emit_xdomain_copy_out_value (MonoMethodBuilder *mb, MonoClass *pclass)
562 mono_mb_emit_icall (mb, mono_marshal_xdomain_copy_out_value);
566 /* mono_marshal_supports_fast_xdomain()
567 * Returns TRUE if the method can use the fast xdomain wrapper.
570 mono_marshal_supports_fast_xdomain (MonoMethod *method)
572 return !mono_class_is_contextbound (method->klass) &&
573 !((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && (strcmp (".ctor", method->name) == 0));
577 mono_marshal_set_domain_by_id (gint32 id, MonoBoolean push)
579 MonoDomain *current_domain = mono_domain_get ();
580 MonoDomain *domain = mono_domain_get_by_id (id);
582 if (!domain || !mono_domain_set (domain, FALSE)) {
583 mono_set_pending_exception (mono_get_exception_appdomain_unloaded ());
588 mono_thread_push_appdomain_ref (domain);
590 mono_thread_pop_appdomain_ref ();
592 return current_domain->domain_id;
595 #if !defined (DISABLE_JIT)
597 mono_marshal_emit_switch_domain (MonoMethodBuilder *mb)
599 mono_mb_emit_icall (mb, mono_marshal_set_domain_by_id);
603 mono_compile_method_icall (MonoMethod *method)
606 gpointer result = mono_compile_method_checked (method, &error);
607 mono_error_set_pending_exception (&error);
611 /* mono_marshal_emit_load_domain_method ()
612 * Loads into the stack a pointer to the code of the provided method for
613 * the current domain.
616 mono_marshal_emit_load_domain_method (MonoMethodBuilder *mb, MonoMethod *method)
618 /* We need a pointer to the method for the running domain (not the domain
619 * that compiles the method).
621 mono_mb_emit_ptr (mb, method);
622 mono_mb_emit_icall (mb, mono_compile_method_icall);
626 /* mono_marshal_check_domain_image ()
627 * Returns TRUE if the image is loaded in the specified
628 * application domain.
631 mono_marshal_check_domain_image (gint32 domain_id, MonoImage *image)
636 MonoDomain *domain = mono_domain_get_by_id (domain_id);
640 mono_domain_assemblies_lock (domain);
641 for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
642 ass = (MonoAssembly *)tmp->data;
643 if (ass->image == image)
646 mono_domain_assemblies_unlock (domain);
651 /* mono_marshal_get_xappdomain_dispatch ()
652 * Generates a method that dispatches a method call from another domain into
653 * the current domain.
656 mono_marshal_get_xappdomain_dispatch (MonoMethod *method, int *marshal_types, int complex_count, int complex_out_count, int ret_marshal_type)
658 MonoMethodSignature *sig, *csig;
659 MonoMethodBuilder *mb;
661 int i, j, param_index, copy_locals_base;
662 MonoClass *ret_class = NULL;
663 int loc_array=0, loc_return=0, loc_serialized_exc=0;
664 MonoExceptionClause *main_clause;
666 gboolean copy_return;
669 if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_XDOMAIN_DISPATCH)))
672 sig = mono_method_signature (method);
673 copy_return = (sig->ret->type != MONO_TYPE_VOID && ret_marshal_type != MONO_MARSHAL_SERIALIZE);
676 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3 + sig->param_count - complex_count);
677 csig->params [j++] = &mono_defaults.object_class->byval_arg;
678 csig->params [j++] = &byte_array_class->this_arg;
679 csig->params [j++] = &byte_array_class->this_arg;
680 for (i = 0; i < sig->param_count; i++) {
681 if (marshal_types [i] != MONO_MARSHAL_SERIALIZE)
682 csig->params [j++] = sig->params [i];
685 csig->ret = sig->ret;
687 csig->ret = &mono_defaults.void_class->byval_arg;
689 csig->hasthis = FALSE;
691 mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_XDOMAIN_DISPATCH);
692 mb->method->save_lmf = 1;
697 loc_serialized_exc = mono_mb_add_local (mb, &byte_array_class->byval_arg);
698 if (complex_count > 0)
699 loc_array = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
700 if (sig->ret->type != MONO_TYPE_VOID) {
701 loc_return = mono_mb_add_local (mb, sig->ret);
702 ret_class = mono_class_from_mono_type (sig->ret);
707 main_clause = (MonoExceptionClause *)mono_image_alloc0 (method->klass->image, sizeof (MonoExceptionClause));
708 main_clause->try_offset = mono_mb_get_label (mb);
710 /* Clean the call context */
712 mono_mb_emit_byte (mb, CEE_LDNULL);
713 mono_mb_emit_managed_call (mb, method_set_call_context, NULL);
714 mono_mb_emit_byte (mb, CEE_POP);
716 /* Deserialize call data */
718 mono_mb_emit_ldarg (mb, 1);
719 mono_mb_emit_byte (mb, CEE_LDIND_REF);
720 mono_mb_emit_byte (mb, CEE_DUP);
721 pos = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
723 mono_marshal_emit_xdomain_copy_value (mb, byte_array_class);
724 mono_mb_emit_managed_call (mb, method_rs_deserialize, NULL);
726 if (complex_count > 0)
727 mono_mb_emit_stloc (mb, loc_array);
729 mono_mb_emit_byte (mb, CEE_POP);
731 mono_mb_patch_short_branch (mb, pos);
733 /* Get the target object */
735 mono_mb_emit_ldarg (mb, 0);
736 mono_mb_emit_managed_call (mb, method_rs_appdomain_target, NULL);
738 /* Load the arguments */
740 copy_locals_base = mb->locals;
741 param_index = 3; // Index of the first non-serialized parameter of this wrapper
743 for (i = 0; i < sig->param_count; i++) {
744 MonoType *pt = sig->params [i];
745 MonoClass *pclass = mono_class_from_mono_type (pt);
746 switch (marshal_types [i]) {
747 case MONO_MARSHAL_SERIALIZE: {
748 /* take the value from the serialized array */
749 mono_mb_emit_ldloc (mb, loc_array);
750 mono_mb_emit_icon (mb, j++);
752 if (pclass->valuetype) {
753 mono_mb_emit_byte (mb, CEE_LDELEM_REF);
754 mono_mb_emit_op (mb, CEE_UNBOX, pclass);
756 mono_mb_emit_op (mb, CEE_LDELEMA, pclass);
759 if (pclass->valuetype) {
760 mono_mb_emit_byte (mb, CEE_LDELEM_REF);
761 mono_mb_emit_op (mb, CEE_UNBOX, pclass);
762 mono_mb_emit_op (mb, CEE_LDOBJ, pclass);
764 mono_mb_emit_byte (mb, CEE_LDELEM_REF);
765 if (pclass != mono_defaults.object_class) {
766 mono_mb_emit_op (mb, CEE_CASTCLASS, pclass);
772 case MONO_MARSHAL_COPY_OUT: {
773 /* Keep a local copy of the value since we need to copy it back after the call */
774 int copy_local = mono_mb_add_local (mb, &(pclass->byval_arg));
775 mono_mb_emit_ldarg (mb, param_index++);
776 mono_marshal_emit_xdomain_copy_value (mb, pclass);
777 mono_mb_emit_byte (mb, CEE_DUP);
778 mono_mb_emit_stloc (mb, copy_local);
781 case MONO_MARSHAL_COPY: {
782 mono_mb_emit_ldarg (mb, param_index);
784 mono_mb_emit_byte (mb, CEE_DUP);
785 mono_mb_emit_byte (mb, CEE_DUP);
786 mono_mb_emit_byte (mb, CEE_LDIND_REF);
787 mono_marshal_emit_xdomain_copy_value (mb, pclass);
788 mono_mb_emit_byte (mb, CEE_STIND_REF);
790 mono_marshal_emit_xdomain_copy_value (mb, pclass);
795 case MONO_MARSHAL_NONE:
796 mono_mb_emit_ldarg (mb, param_index++);
801 /* Make the call to the real object */
803 mono_marshal_emit_thread_force_interrupt_checkpoint (mb);
805 mono_mb_emit_op (mb, CEE_CALLVIRT, method);
807 if (sig->ret->type != MONO_TYPE_VOID)
808 mono_mb_emit_stloc (mb, loc_return);
810 /* copy back MONO_MARSHAL_COPY_OUT parameters */
814 for (i = 0; i < sig->param_count; i++) {
815 if (marshal_types [i] == MONO_MARSHAL_SERIALIZE) continue;
816 if (marshal_types [i] == MONO_MARSHAL_COPY_OUT) {
817 mono_mb_emit_ldloc (mb, copy_locals_base + (j++));
818 mono_mb_emit_ldarg (mb, param_index);
819 mono_marshal_emit_xdomain_copy_out_value (mb, mono_class_from_mono_type (sig->params [i]));
824 /* Serialize the return values */
826 if (complex_out_count > 0) {
827 /* Reset parameters in the array that don't need to be serialized back */
829 for (i = 0; i < sig->param_count; i++) {
830 if (marshal_types[i] != MONO_MARSHAL_SERIALIZE) continue;
831 if (!sig->params [i]->byref) {
832 mono_mb_emit_ldloc (mb, loc_array);
833 mono_mb_emit_icon (mb, j);
834 mono_mb_emit_byte (mb, CEE_LDNULL);
835 mono_mb_emit_byte (mb, CEE_STELEM_REF);
840 /* Add the return value to the array */
842 if (ret_marshal_type == MONO_MARSHAL_SERIALIZE) {
843 mono_mb_emit_ldloc (mb, loc_array);
844 mono_mb_emit_icon (mb, complex_count); /* The array has an additional slot to hold the ret value */
845 mono_mb_emit_ldloc (mb, loc_return);
847 g_assert (ret_class); /*FIXME properly fail here*/
848 if (ret_class->valuetype) {
849 mono_mb_emit_op (mb, CEE_BOX, ret_class);
851 mono_mb_emit_byte (mb, CEE_STELEM_REF);
856 mono_mb_emit_ldarg (mb, 1);
857 mono_mb_emit_ldloc (mb, loc_array);
858 mono_mb_emit_managed_call (mb, method_rs_serialize, NULL);
859 mono_mb_emit_byte (mb, CEE_STIND_REF);
860 } else if (ret_marshal_type == MONO_MARSHAL_SERIALIZE) {
861 mono_mb_emit_ldarg (mb, 1);
862 mono_mb_emit_ldloc (mb, loc_return);
863 if (ret_class->valuetype) {
864 mono_mb_emit_op (mb, CEE_BOX, ret_class);
866 mono_mb_emit_managed_call (mb, method_rs_serialize, NULL);
867 mono_mb_emit_byte (mb, CEE_STIND_REF);
869 mono_mb_emit_ldarg (mb, 1);
870 mono_mb_emit_byte (mb, CEE_LDNULL);
871 mono_mb_emit_managed_call (mb, method_rs_serialize, NULL);
872 mono_mb_emit_byte (mb, CEE_STIND_REF);
875 mono_mb_emit_ldarg (mb, 2);
876 mono_mb_emit_byte (mb, CEE_LDNULL);
877 mono_mb_emit_byte (mb, CEE_STIND_REF);
878 pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
880 /* Main exception catch */
881 main_clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
882 main_clause->try_len = mono_mb_get_pos (mb) - main_clause->try_offset;
883 main_clause->data.catch_class = mono_defaults.object_class;
886 main_clause->handler_offset = mono_mb_get_label (mb);
887 mono_mb_emit_managed_call (mb, method_rs_serialize_exc, NULL);
888 mono_mb_emit_stloc (mb, loc_serialized_exc);
889 mono_mb_emit_ldarg (mb, 2);
890 mono_mb_emit_ldloc (mb, loc_serialized_exc);
891 mono_mb_emit_byte (mb, CEE_STIND_REF);
892 mono_mb_emit_branch (mb, CEE_LEAVE);
893 main_clause->handler_len = mono_mb_get_pos (mb) - main_clause->handler_offset;
896 mono_mb_patch_branch (mb, pos_leave);
899 mono_mb_emit_ldloc (mb, loc_return);
901 mono_mb_emit_byte (mb, CEE_RET);
903 mono_mb_set_clauses (mb, 1, main_clause);
906 info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
907 info->d.remoting.method = method;
908 res = mono_remoting_mb_create_and_cache (method, mb, csig, csig->param_count + 16, info);
915 * mono_marshal_get_xappdomain_invoke:
916 * Generates a fast remoting wrapper for cross app domain calls.
919 mono_marshal_get_xappdomain_invoke (MonoMethod *method)
921 MonoMethodSignature *sig;
922 MonoMethodBuilder *mb;
924 int i, j, complex_count, complex_out_count, copy_locals_base;
926 MonoClass *ret_class = NULL;
927 MonoMethod *xdomain_method;
928 int ret_marshal_type = MONO_MARSHAL_NONE;
929 int loc_array=0, loc_serialized_data=-1, loc_real_proxy;
930 int loc_old_domainid, loc_domainid, loc_return=0, loc_serialized_exc=0, loc_context;
931 int pos, pos_dispatch, pos_noex;
932 gboolean copy_return = FALSE;
937 if (method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE || method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE)
940 /* we cant remote methods without this pointer */
941 if (!mono_method_signature (method)->hasthis)
944 mono_remoting_marshal_init ();
946 if (!mono_marshal_supports_fast_xdomain (method))
947 return mono_marshal_get_remoting_invoke (method);
949 if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_XDOMAIN_INVOKE)))
952 sig = mono_signature_no_pinvoke (method);
954 mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_XDOMAIN_INVOKE);
955 mb->method->save_lmf = 1;
957 /* Count the number of parameters that need to be serialized */
959 marshal_types = (int *)alloca (sizeof (int) * sig->param_count);
960 complex_count = complex_out_count = 0;
961 for (i = 0; i < sig->param_count; i++) {
962 MonoType *ptype = sig->params[i];
963 int mt = mono_get_xdomain_marshal_type (ptype);
965 /* If the [Out] attribute is applied to a parameter that can be internally copied,
966 * the copy will be made by reusing the original object instance
968 if ((ptype->attrs & PARAM_ATTRIBUTE_OUT) != 0 && mt == MONO_MARSHAL_COPY && !ptype->byref)
969 mt = MONO_MARSHAL_COPY_OUT;
970 else if (mt == MONO_MARSHAL_SERIALIZE) {
972 if (ptype->byref) complex_out_count++;
974 marshal_types [i] = mt;
977 if (sig->ret->type != MONO_TYPE_VOID) {
978 ret_marshal_type = mono_get_xdomain_marshal_type (sig->ret);
979 ret_class = mono_class_from_mono_type (sig->ret);
980 copy_return = ret_marshal_type != MONO_MARSHAL_SERIALIZE;
986 if (complex_count > 0)
987 loc_array = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
988 loc_serialized_data = mono_mb_add_local (mb, &byte_array_class->byval_arg);
989 loc_real_proxy = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
991 loc_return = mono_mb_add_local (mb, sig->ret);
992 loc_old_domainid = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
993 loc_domainid = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
994 loc_serialized_exc = mono_mb_add_local (mb, &byte_array_class->byval_arg);
995 loc_context = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
997 /* Save thread domain data */
999 mono_mb_emit_icall (mb, mono_context_get);
1000 mono_mb_emit_byte (mb, CEE_DUP);
1001 mono_mb_emit_stloc (mb, loc_context);
1003 /* If the thread is not running in the default context, it needs to go
1004 * through the whole remoting sink, since the context is going to change
1006 mono_mb_emit_managed_call (mb, method_needs_context_sink, NULL);
1007 pos = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
1009 /* Another case in which the fast path can't be used: when the target domain
1010 * has a different image for the same assembly.
1013 /* Get the target domain id */
1015 mono_mb_emit_ldarg (mb, 0);
1016 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoTransparentProxy, rp));
1017 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1018 mono_mb_emit_byte (mb, CEE_DUP);
1019 mono_mb_emit_stloc (mb, loc_real_proxy);
1021 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoRealProxy, target_domain_id));
1022 mono_mb_emit_byte (mb, CEE_LDIND_I4);
1023 mono_mb_emit_stloc (mb, loc_domainid);
1025 /* Check if the target domain has the same image for the required assembly */
1027 mono_mb_emit_ldloc (mb, loc_domainid);
1028 mono_mb_emit_ptr (mb, method->klass->image);
1029 mono_mb_emit_icall (mb, mono_marshal_check_domain_image);
1030 pos_dispatch = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
1032 /* Use the whole remoting sink to dispatch this message */
1034 mono_mb_patch_short_branch (mb, pos);
1036 mono_mb_emit_ldarg (mb, 0);
1037 for (i = 0; i < sig->param_count; i++)
1038 mono_mb_emit_ldarg (mb, i + 1);
1040 mono_mb_emit_managed_call (mb, mono_marshal_get_remoting_invoke (method), NULL);
1041 mono_mb_emit_byte (mb, CEE_RET);
1042 mono_mb_patch_short_branch (mb, pos_dispatch);
1044 /* Create the array that will hold the parameters to be serialized */
1046 if (complex_count > 0) {
1047 mono_mb_emit_icon (mb, (ret_marshal_type == MONO_MARSHAL_SERIALIZE && complex_out_count > 0) ? complex_count + 1 : complex_count); /* +1 for the return type */
1048 mono_mb_emit_op (mb, CEE_NEWARR, mono_defaults.object_class);
1051 for (i = 0; i < sig->param_count; i++) {
1053 if (marshal_types [i] != MONO_MARSHAL_SERIALIZE) continue;
1054 pclass = mono_class_from_mono_type (sig->params[i]);
1055 mono_mb_emit_byte (mb, CEE_DUP);
1056 mono_mb_emit_icon (mb, j);
1057 mono_mb_emit_ldarg (mb, i + 1); /* 0=this */
1058 if (sig->params[i]->byref) {
1059 if (pclass->valuetype)
1060 mono_mb_emit_op (mb, CEE_LDOBJ, pclass);
1062 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1064 if (pclass->valuetype)
1065 mono_mb_emit_op (mb, CEE_BOX, pclass);
1066 mono_mb_emit_byte (mb, CEE_STELEM_REF);
1069 mono_mb_emit_stloc (mb, loc_array);
1071 /* Serialize parameters */
1073 mono_mb_emit_ldloc (mb, loc_array);
1074 mono_mb_emit_managed_call (mb, method_rs_serialize, NULL);
1075 mono_mb_emit_stloc (mb, loc_serialized_data);
1077 mono_mb_emit_byte (mb, CEE_LDNULL);
1078 mono_mb_emit_managed_call (mb, method_rs_serialize, NULL);
1079 mono_mb_emit_stloc (mb, loc_serialized_data);
1084 mono_mb_emit_ldloc (mb, loc_domainid);
1085 mono_mb_emit_byte (mb, CEE_LDC_I4_1);
1086 mono_marshal_emit_switch_domain (mb);
1087 mono_mb_emit_stloc (mb, loc_old_domainid);
1089 /* Load the arguments */
1091 mono_mb_emit_ldloc (mb, loc_real_proxy);
1092 mono_mb_emit_ldloc_addr (mb, loc_serialized_data);
1093 mono_mb_emit_ldloc_addr (mb, loc_serialized_exc);
1095 copy_locals_base = mb->locals;
1096 for (i = 0; i < sig->param_count; i++) {
1097 switch (marshal_types [i]) {
1098 case MONO_MARSHAL_SERIALIZE:
1100 case MONO_MARSHAL_COPY: {
1101 mono_mb_emit_ldarg (mb, i+1);
1102 if (sig->params [i]->byref) {
1103 /* make a local copy of the byref parameter. The real parameter
1104 * will be updated after the xdomain call
1106 MonoClass *pclass = mono_class_from_mono_type (sig->params [i]);
1107 int copy_local = mono_mb_add_local (mb, &(pclass->byval_arg));
1108 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1109 mono_mb_emit_stloc (mb, copy_local);
1110 mono_mb_emit_ldloc_addr (mb, copy_local);
1114 case MONO_MARSHAL_COPY_OUT:
1115 case MONO_MARSHAL_NONE:
1116 mono_mb_emit_ldarg (mb, i+1);
1121 /* Make the call to the invoke wrapper in the target domain */
1123 xdomain_method = mono_marshal_get_xappdomain_dispatch (method, marshal_types, complex_count, complex_out_count, ret_marshal_type);
1124 mono_marshal_emit_load_domain_method (mb, xdomain_method);
1125 mono_mb_emit_calli (mb, mono_method_signature (xdomain_method));
1128 mono_mb_emit_stloc (mb, loc_return);
1132 mono_mb_emit_ldloc (mb, loc_old_domainid);
1133 mono_mb_emit_byte (mb, CEE_LDC_I4_0);
1134 mono_marshal_emit_switch_domain (mb);
1135 mono_mb_emit_byte (mb, CEE_POP);
1137 /* Restore thread domain data */
1139 mono_mb_emit_ldloc (mb, loc_context);
1140 mono_mb_emit_icall (mb, mono_context_set);
1142 /* if (loc_serialized_exc != null) ... */
1144 mono_mb_emit_ldloc (mb, loc_serialized_exc);
1145 pos_noex = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
1147 mono_mb_emit_ldloc (mb, loc_serialized_exc);
1148 mono_marshal_emit_xdomain_copy_value (mb, byte_array_class);
1149 mono_mb_emit_managed_call (mb, method_rs_deserialize, NULL);
1150 mono_mb_emit_op (mb, CEE_CASTCLASS, mono_defaults.exception_class);
1151 mono_mb_emit_managed_call (mb, method_exc_fixexc, NULL);
1152 mono_mb_emit_byte (mb, CEE_THROW);
1153 mono_mb_patch_short_branch (mb, pos_noex);
1155 /* copy back non-serialized output parameters */
1158 for (i = 0; i < sig->param_count; i++) {
1159 if (!sig->params [i]->byref || marshal_types [i] != MONO_MARSHAL_COPY) continue;
1160 mono_mb_emit_ldarg (mb, i + 1);
1161 mono_mb_emit_ldloc (mb, copy_locals_base + (j++));
1162 mono_marshal_emit_xdomain_copy_value (mb, mono_class_from_mono_type (sig->params [i]));
1163 mono_mb_emit_byte (mb, CEE_STIND_REF);
1166 /* Deserialize out parameters */
1168 if (complex_out_count > 0) {
1169 mono_mb_emit_ldloc (mb, loc_serialized_data);
1170 mono_marshal_emit_xdomain_copy_value (mb, byte_array_class);
1171 mono_mb_emit_managed_call (mb, method_rs_deserialize, NULL);
1172 mono_mb_emit_stloc (mb, loc_array);
1174 /* Copy back output parameters and return type */
1177 for (i = 0; i < sig->param_count; i++) {
1178 if (marshal_types [i] != MONO_MARSHAL_SERIALIZE) continue;
1179 if (sig->params[i]->byref) {
1180 MonoClass *pclass = mono_class_from_mono_type (sig->params [i]);
1181 mono_mb_emit_ldarg (mb, i + 1);
1182 mono_mb_emit_ldloc (mb, loc_array);
1183 mono_mb_emit_icon (mb, j);
1184 mono_mb_emit_byte (mb, CEE_LDELEM_REF);
1185 if (pclass->valuetype) {
1186 mono_mb_emit_op (mb, CEE_UNBOX, pclass);
1187 mono_mb_emit_op (mb, CEE_LDOBJ, pclass);
1188 mono_mb_emit_op (mb, CEE_STOBJ, pclass);
1190 if (pclass != mono_defaults.object_class)
1191 mono_mb_emit_op (mb, CEE_CASTCLASS, pclass);
1192 mono_mb_emit_byte (mb, CEE_STIND_REF);
1198 if (ret_marshal_type == MONO_MARSHAL_SERIALIZE) {
1199 mono_mb_emit_ldloc (mb, loc_array);
1200 mono_mb_emit_icon (mb, complex_count);
1201 mono_mb_emit_byte (mb, CEE_LDELEM_REF);
1202 if (ret_class->valuetype) {
1203 mono_mb_emit_op (mb, CEE_UNBOX, ret_class);
1204 mono_mb_emit_op (mb, CEE_LDOBJ, ret_class);
1207 } else if (ret_marshal_type == MONO_MARSHAL_SERIALIZE) {
1208 mono_mb_emit_ldloc (mb, loc_serialized_data);
1209 mono_marshal_emit_xdomain_copy_value (mb, byte_array_class);
1210 mono_mb_emit_managed_call (mb, method_rs_deserialize, NULL);
1211 if (ret_class->valuetype) {
1212 mono_mb_emit_op (mb, CEE_UNBOX, ret_class);
1213 mono_mb_emit_op (mb, CEE_LDOBJ, ret_class);
1214 } else if (ret_class != mono_defaults.object_class) {
1215 mono_mb_emit_op (mb, CEE_CASTCLASS, ret_class);
1218 mono_mb_emit_ldloc (mb, loc_serialized_data);
1219 mono_mb_emit_byte (mb, CEE_DUP);
1220 pos = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
1221 mono_marshal_emit_xdomain_copy_value (mb, byte_array_class);
1223 mono_mb_patch_short_branch (mb, pos);
1224 mono_mb_emit_managed_call (mb, method_rs_deserialize, NULL);
1225 mono_mb_emit_byte (mb, CEE_POP);
1229 mono_mb_emit_ldloc (mb, loc_return);
1230 if (ret_marshal_type == MONO_MARSHAL_COPY)
1231 mono_marshal_emit_xdomain_copy_value (mb, ret_class);
1234 mono_mb_emit_byte (mb, CEE_RET);
1235 #endif /* DISABLE_JIT */
1237 info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
1238 info->d.remoting.method = method;
1239 res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16, info);
1246 * mono_marshal_get_remoting_invoke_for_target:
1249 mono_marshal_get_remoting_invoke_for_target (MonoMethod *method, MonoRemotingTarget target_type)
1251 if (target_type == MONO_REMOTING_TARGET_APPDOMAIN) {
1252 return mono_marshal_get_xappdomain_invoke (method);
1253 } else if (target_type == MONO_REMOTING_TARGET_COMINTEROP) {
1255 return mono_cominterop_get_invoke (method);
1257 g_assert_not_reached ();
1260 return mono_marshal_get_remoting_invoke (method);
1266 G_GNUC_UNUSED static gpointer
1267 mono_marshal_load_remoting_wrapper (MonoRealProxy *rp, MonoMethod *method)
1270 MonoMethod *marshal_method = NULL;
1271 if (rp->target_domain_id != -1)
1272 marshal_method = mono_marshal_get_xappdomain_invoke (method);
1274 marshal_method = mono_marshal_get_remoting_invoke (method);
1275 gpointer compiled_ptr = mono_compile_method_checked (marshal_method, &error);
1276 mono_error_assert_ok (&error);
1277 return compiled_ptr;
1281 * mono_marshal_get_remoting_invoke_with_check:
1284 mono_marshal_get_remoting_invoke_with_check (MonoMethod *method)
1286 MonoMethodSignature *sig;
1287 MonoMethodBuilder *mb;
1288 MonoMethod *res, *native;
1290 int i, pos, pos_rem;
1294 if (method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
1297 /* we cant remote methods without this pointer */
1298 g_assert (mono_method_signature (method)->hasthis);
1300 if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)))
1303 sig = mono_signature_no_pinvoke (method);
1305 mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK);
1308 for (i = 0; i <= sig->param_count; i++)
1309 mono_mb_emit_ldarg (mb, i);
1311 mono_mb_emit_ldarg (mb, 0);
1312 pos = mono_mb_emit_proxy_check (mb, CEE_BNE_UN);
1314 if (mono_marshal_supports_fast_xdomain (method)) {
1315 mono_mb_emit_ldarg (mb, 0);
1316 pos_rem = mono_mb_emit_xdomain_check (mb, CEE_BEQ);
1318 /* wrapper for cross app domain calls */
1319 native = mono_marshal_get_xappdomain_invoke (method);
1320 mono_mb_emit_managed_call (mb, native, mono_method_signature (native));
1321 mono_mb_emit_byte (mb, CEE_RET);
1323 mono_mb_patch_branch (mb, pos_rem);
1325 /* wrapper for normal remote calls */
1326 native = mono_marshal_get_remoting_invoke (method);
1327 mono_mb_emit_managed_call (mb, native, mono_method_signature (native));
1328 mono_mb_emit_byte (mb, CEE_RET);
1331 mono_mb_patch_branch (mb, pos);
1332 mono_mb_emit_managed_call (mb, method, mono_method_signature (method));
1333 mono_mb_emit_byte (mb, CEE_RET);
1336 info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
1337 info->d.remoting.method = method;
1338 res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16, info);
1345 * mono_marshal_get_ldfld_wrapper:
1346 * \param type the type of the field
1348 * This method generates a function which can be use to load a field with type
1349 * \p type from an object. The generated function has the following signature:
1351 * <code><i>type</i> ldfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset)</code>
1354 mono_marshal_get_ldfld_wrapper (MonoType *type)
1356 MonoMethodSignature *sig;
1357 MonoMethodBuilder *mb;
1363 int t, pos0, pos1 = 0;
1364 static MonoMethod* tp_load = NULL;
1366 type = mono_type_get_underlying_type (type);
1371 if (type->type == MONO_TYPE_SZARRAY) {
1372 klass = mono_defaults.array_class;
1373 } else if (type->type == MONO_TYPE_VALUETYPE) {
1374 klass = type->data.klass;
1375 } else if (t == MONO_TYPE_OBJECT || t == MONO_TYPE_CLASS || t == MONO_TYPE_STRING) {
1376 klass = mono_defaults.object_class;
1377 } else if (t == MONO_TYPE_PTR || t == MONO_TYPE_FNPTR) {
1378 klass = mono_defaults.int_class;
1379 } else if (t == MONO_TYPE_GENERICINST) {
1380 if (mono_type_generic_inst_is_valuetype (type))
1381 klass = mono_class_from_mono_type (type);
1383 klass = mono_defaults.object_class;
1385 klass = mono_class_from_mono_type (type);
1388 klass = mono_defaults.int_class;
1391 cache = get_cache (&klass->image->ldfld_wrapper_cache, mono_aligned_addr_hash, NULL);
1392 if ((res = mono_marshal_find_in_cache (cache, klass)))
1395 #ifndef DISABLE_REMOTING
1397 tp_load = mono_class_get_method_from_name (mono_defaults.transparent_proxy_class, "LoadRemoteFieldNew", -1);
1398 g_assert (tp_load != NULL);
1402 /* we add the %p pointer value of klass because class names are not unique */
1403 name = g_strdup_printf ("__ldfld_wrapper_%p_%s.%s", klass, klass->name_space, klass->name);
1404 mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_LDFLD);
1407 sig = mono_metadata_signature_alloc (mono_defaults.corlib, 4);
1408 sig->params [0] = &mono_defaults.object_class->byval_arg;
1409 sig->params [1] = &mono_defaults.int_class->byval_arg;
1410 sig->params [2] = &mono_defaults.int_class->byval_arg;
1411 sig->params [3] = &mono_defaults.int_class->byval_arg;
1412 sig->ret = &klass->byval_arg;
1415 mono_mb_emit_ldarg (mb, 0);
1416 pos0 = mono_mb_emit_proxy_check (mb, CEE_BNE_UN);
1418 #ifndef DISABLE_REMOTING
1419 mono_mb_emit_ldarg (mb, 0);
1420 mono_mb_emit_ldarg (mb, 1);
1421 mono_mb_emit_ldarg (mb, 2);
1423 mono_mb_emit_managed_call (mb, tp_load, NULL);
1426 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
1427 csig->params [0] = &mono_defaults.object_class->byval_arg;
1428 csig->params [1] = &mono_defaults.int_class->byval_arg;
1429 csig->params [2] = &mono_defaults.int_class->byval_arg;
1430 csig->ret = &klass->this_arg;
1433 mono_mb_emit_native_call (mb, csig, mono_load_remote_field_new);
1434 mono_marshal_emit_thread_interrupt_checkpoint (mb);
1437 if (klass->valuetype) {
1438 mono_mb_emit_op (mb, CEE_UNBOX, klass);
1439 pos1 = mono_mb_emit_branch (mb, CEE_BR);
1441 mono_mb_emit_byte (mb, CEE_RET);
1445 mono_mb_patch_branch (mb, pos0);
1447 mono_mb_emit_ldarg (mb, 0);
1448 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
1449 mono_mb_emit_byte (mb, CEE_MONO_OBJADDR);
1450 mono_mb_emit_ldarg (mb, 3);
1451 mono_mb_emit_byte (mb, CEE_ADD);
1453 if (klass->valuetype)
1454 mono_mb_patch_branch (mb, pos1);
1459 case MONO_TYPE_BOOLEAN:
1460 case MONO_TYPE_CHAR:
1469 case MONO_TYPE_ARRAY:
1470 case MONO_TYPE_SZARRAY:
1471 case MONO_TYPE_OBJECT:
1472 case MONO_TYPE_CLASS:
1473 case MONO_TYPE_STRING:
1477 case MONO_TYPE_FNPTR:
1478 mono_mb_emit_byte (mb, mono_type_to_ldind (type));
1480 case MONO_TYPE_VALUETYPE:
1481 g_assert (!klass->enumtype);
1482 mono_mb_emit_op (mb, CEE_LDOBJ, klass);
1484 case MONO_TYPE_GENERICINST:
1485 if (mono_type_generic_inst_is_valuetype (type)) {
1486 mono_mb_emit_op (mb, CEE_LDOBJ, klass);
1488 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1492 case MONO_TYPE_MVAR:
1493 mono_mb_emit_op (mb, CEE_LDOBJ, klass);
1496 g_warning ("type %x not implemented", type->type);
1497 g_assert_not_reached ();
1500 mono_mb_emit_byte (mb, CEE_RET);
1501 #endif /* DISABLE_JIT */
1503 info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
1504 info->d.proxy.klass = klass;
1505 res = mono_mb_create_and_cache_full (cache, klass,
1506 mb, sig, sig->param_count + 16, info, NULL);
1513 * mono_marshal_get_ldflda_wrapper:
1514 * @type: the type of the field
1516 * This method generates a function which can be used to load a field address
1517 * from an object. The generated function has the following signature:
1518 * gpointer ldflda_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset);
1521 mono_marshal_get_ldflda_wrapper (MonoType *type)
1523 MonoMethodSignature *sig;
1524 MonoMethodBuilder *mb;
1530 int t, pos0, pos1, pos2, pos3;
1532 type = mono_type_get_underlying_type (type);
1536 if (type->type == MONO_TYPE_SZARRAY) {
1537 klass = mono_defaults.array_class;
1538 } else if (type->type == MONO_TYPE_VALUETYPE) {
1539 klass = type->data.klass;
1540 } else if (t == MONO_TYPE_OBJECT || t == MONO_TYPE_CLASS || t == MONO_TYPE_STRING) {
1541 klass = mono_defaults.object_class;
1542 } else if (t == MONO_TYPE_PTR || t == MONO_TYPE_FNPTR) {
1543 klass = mono_defaults.int_class;
1544 } else if (t == MONO_TYPE_GENERICINST) {
1545 if (mono_type_generic_inst_is_valuetype (type))
1546 klass = mono_class_from_mono_type (type);
1548 klass = mono_defaults.object_class;
1550 klass = mono_class_from_mono_type (type);
1553 klass = mono_defaults.int_class;
1556 cache = get_cache (&klass->image->ldflda_wrapper_cache, mono_aligned_addr_hash, NULL);
1557 if ((res = mono_marshal_find_in_cache (cache, klass)))
1560 /* we add the %p pointer value of klass because class names are not unique */
1561 name = g_strdup_printf ("__ldflda_wrapper_%p_%s.%s", klass, klass->name_space, klass->name);
1562 mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_LDFLDA);
1565 sig = mono_metadata_signature_alloc (mono_defaults.corlib, 4);
1566 sig->params [0] = &mono_defaults.object_class->byval_arg;
1567 sig->params [1] = &mono_defaults.int_class->byval_arg;
1568 sig->params [2] = &mono_defaults.int_class->byval_arg;
1569 sig->params [3] = &mono_defaults.int_class->byval_arg;
1570 sig->ret = &mono_defaults.int_class->byval_arg;
1573 /* if typeof (this) != transparent_proxy goto pos0 */
1574 mono_mb_emit_ldarg (mb, 0);
1575 pos0 = mono_mb_emit_proxy_check (mb, CEE_BNE_UN);
1577 /* if same_appdomain goto pos1 */
1578 mono_mb_emit_ldarg (mb, 0);
1579 pos1 = mono_mb_emit_xdomain_check (mb, CEE_BEQ);
1581 mono_mb_emit_exception_full (mb, "System", "InvalidOperationException", "Attempt to load field address from object in another appdomain.");
1583 /* same app domain */
1584 mono_mb_patch_branch (mb, pos1);
1586 /* if typeof (this) != contextbound goto pos2 */
1587 mono_mb_emit_ldarg (mb, 0);
1588 pos2 = mono_mb_emit_contextbound_check (mb, CEE_BEQ);
1590 /* if this->rp->context == mono_context_get goto pos3 */
1591 mono_mb_emit_ldarg (mb, 0);
1592 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoTransparentProxy, rp));
1593 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1594 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoRealProxy, context));
1595 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1596 mono_mb_emit_icall (mb, mono_context_get);
1597 pos3 = mono_mb_emit_branch (mb, CEE_BEQ);
1599 mono_mb_emit_exception_full (mb, "System", "InvalidOperationException", "Attempt to load field address from object in another context.");
1601 mono_mb_patch_branch (mb, pos2);
1602 mono_mb_patch_branch (mb, pos3);
1604 /* return the address of the field from this->rp->unwrapped_server */
1605 mono_mb_emit_ldarg (mb, 0);
1606 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoTransparentProxy, rp));
1607 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1608 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoRealProxy, unwrapped_server));
1609 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1610 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
1611 mono_mb_emit_byte (mb, CEE_MONO_OBJADDR);
1612 mono_mb_emit_ldarg (mb, 3);
1613 mono_mb_emit_byte (mb, CEE_ADD);
1614 mono_mb_emit_byte (mb, CEE_RET);
1616 /* not a proxy: return the address of the field directly */
1617 mono_mb_patch_branch (mb, pos0);
1619 mono_mb_emit_ldarg (mb, 0);
1620 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
1621 mono_mb_emit_byte (mb, CEE_MONO_OBJADDR);
1622 mono_mb_emit_ldarg (mb, 3);
1623 mono_mb_emit_byte (mb, CEE_ADD);
1625 mono_mb_emit_byte (mb, CEE_RET);
1628 info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
1629 info->d.proxy.klass = klass;
1630 res = mono_mb_create_and_cache_full (cache, klass,
1631 mb, sig, sig->param_count + 16,
1640 * mono_marshal_get_stfld_wrapper:
1641 * \param type the type of the field
1643 * This method generates a function which can be use to store a field with type
1644 * \p type. The generated function has the following signature:
1646 * <code>void stfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset, <i>type</i> val)</code>
1649 mono_marshal_get_stfld_wrapper (MonoType *type)
1651 MonoMethodSignature *sig;
1652 MonoMethodBuilder *mb;
1659 static MonoMethod *tp_store = NULL;
1661 type = mono_type_get_underlying_type (type);
1665 if (type->type == MONO_TYPE_SZARRAY) {
1666 klass = mono_defaults.array_class;
1667 } else if (type->type == MONO_TYPE_VALUETYPE) {
1668 klass = type->data.klass;
1669 } else if (t == MONO_TYPE_OBJECT || t == MONO_TYPE_CLASS || t == MONO_TYPE_STRING) {
1670 klass = mono_defaults.object_class;
1671 } else if (t == MONO_TYPE_PTR || t == MONO_TYPE_FNPTR) {
1672 klass = mono_defaults.int_class;
1673 } else if (t == MONO_TYPE_GENERICINST) {
1674 if (mono_type_generic_inst_is_valuetype (type))
1675 klass = mono_class_from_mono_type (type);
1677 klass = mono_defaults.object_class;
1679 klass = mono_class_from_mono_type (type);
1682 klass = mono_defaults.int_class;
1685 cache = get_cache (&klass->image->stfld_wrapper_cache, mono_aligned_addr_hash, NULL);
1686 if ((res = mono_marshal_find_in_cache (cache, klass)))
1689 #ifndef DISABLE_REMOTING
1691 tp_store = mono_class_get_method_from_name (mono_defaults.transparent_proxy_class, "StoreRemoteField", -1);
1692 g_assert (tp_store != NULL);
1696 /* we add the %p pointer value of klass because class names are not unique */
1697 name = g_strdup_printf ("__stfld_wrapper_%p_%s.%s", klass, klass->name_space, klass->name);
1698 mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_STFLD);
1701 sig = mono_metadata_signature_alloc (mono_defaults.corlib, 5);
1702 sig->params [0] = &mono_defaults.object_class->byval_arg;
1703 sig->params [1] = &mono_defaults.int_class->byval_arg;
1704 sig->params [2] = &mono_defaults.int_class->byval_arg;
1705 sig->params [3] = &mono_defaults.int_class->byval_arg;
1706 sig->params [4] = &klass->byval_arg;
1707 sig->ret = &mono_defaults.void_class->byval_arg;
1710 mono_mb_emit_ldarg (mb, 0);
1711 pos = mono_mb_emit_proxy_check (mb, CEE_BNE_UN);
1713 #ifndef DISABLE_REMOTING
1714 mono_mb_emit_ldarg (mb, 0);
1715 mono_mb_emit_ldarg (mb, 1);
1716 mono_mb_emit_ldarg (mb, 2);
1717 mono_mb_emit_ldarg (mb, 4);
1718 if (klass->valuetype)
1719 mono_mb_emit_op (mb, CEE_BOX, klass);
1721 mono_mb_emit_managed_call (mb, tp_store, NULL);
1723 mono_mb_emit_byte (mb, CEE_RET);
1726 mono_mb_patch_branch (mb, pos);
1728 mono_mb_emit_ldarg (mb, 0);
1729 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
1730 mono_mb_emit_byte (mb, CEE_MONO_OBJADDR);
1731 mono_mb_emit_ldarg (mb, 3);
1732 mono_mb_emit_byte (mb, CEE_ADD);
1733 mono_mb_emit_ldarg (mb, 4);
1738 case MONO_TYPE_BOOLEAN:
1739 case MONO_TYPE_CHAR:
1748 case MONO_TYPE_ARRAY:
1749 case MONO_TYPE_SZARRAY:
1750 case MONO_TYPE_OBJECT:
1751 case MONO_TYPE_CLASS:
1752 case MONO_TYPE_STRING:
1756 case MONO_TYPE_FNPTR:
1757 mono_mb_emit_byte (mb, mono_type_to_stind (type));
1759 case MONO_TYPE_VALUETYPE:
1760 g_assert (!klass->enumtype);
1761 mono_mb_emit_op (mb, CEE_STOBJ, klass);
1763 case MONO_TYPE_GENERICINST:
1765 case MONO_TYPE_MVAR:
1766 mono_mb_emit_op (mb, CEE_STOBJ, klass);
1769 g_warning ("type %x not implemented", type->type);
1770 g_assert_not_reached ();
1773 mono_mb_emit_byte (mb, CEE_RET);
1776 info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
1777 info->d.proxy.klass = klass;
1778 res = mono_mb_create_and_cache_full (cache, klass,
1779 mb, sig, sig->param_count + 16,
1787 * mono_marshal_get_proxy_cancast:
1790 mono_marshal_get_proxy_cancast (MonoClass *klass)
1792 static MonoMethodSignature *isint_sig = NULL;
1796 int pos_failed, pos_end;
1797 char *name, *klass_name;
1798 MonoMethod *can_cast_to;
1799 MonoMethodDesc *desc;
1800 MonoMethodBuilder *mb;
1802 cache = get_cache (&klass->image->proxy_isinst_cache, mono_aligned_addr_hash, NULL);
1803 if ((res = mono_marshal_find_in_cache (cache, klass)))
1807 isint_sig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
1808 isint_sig->params [0] = &mono_defaults.object_class->byval_arg;
1809 isint_sig->ret = &mono_defaults.object_class->byval_arg;
1810 isint_sig->pinvoke = 0;
1813 klass_name = mono_type_full_name (&klass->byval_arg);
1814 name = g_strdup_printf ("__proxy_isinst_wrapper_%s", klass_name);
1815 mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_PROXY_ISINST);
1816 g_free (klass_name);
1819 mb->method->save_lmf = 1;
1822 /* get the real proxy from the transparent proxy*/
1823 mono_mb_emit_ldarg (mb, 0);
1824 mono_mb_emit_ldflda (mb, MONO_STRUCT_OFFSET (MonoTransparentProxy, rp));
1825 mono_mb_emit_byte (mb, CEE_LDIND_REF);
1827 /* get the reflection type from the type handle */
1828 mono_mb_emit_ptr (mb, &klass->byval_arg);
1829 mono_mb_emit_icall (mb, type_from_handle);
1831 mono_mb_emit_ldarg (mb, 0);
1833 /* make the call to CanCastTo (type, ob) */
1834 desc = mono_method_desc_new ("IRemotingTypeInfo:CanCastTo", FALSE);
1835 can_cast_to = mono_method_desc_search_in_class (desc, mono_defaults.iremotingtypeinfo_class);
1836 g_assert (can_cast_to);
1837 mono_method_desc_free (desc);
1838 mono_mb_emit_op (mb, CEE_CALLVIRT, can_cast_to);
1840 pos_failed = mono_mb_emit_branch (mb, CEE_BRFALSE);
1842 /* Upgrade the proxy vtable by calling: mono_upgrade_remote_class_wrapper (type, ob)*/
1843 mono_mb_emit_ptr (mb, &klass->byval_arg);
1844 mono_mb_emit_icall (mb, type_from_handle);
1845 mono_mb_emit_ldarg (mb, 0);
1847 mono_mb_emit_icall (mb, mono_upgrade_remote_class_wrapper);
1848 mono_marshal_emit_thread_interrupt_checkpoint (mb);
1850 mono_mb_emit_ldarg (mb, 0);
1851 pos_end = mono_mb_emit_branch (mb, CEE_BR);
1855 mono_mb_patch_branch (mb, pos_failed);
1856 mono_mb_emit_byte (mb, CEE_LDNULL);
1860 mono_mb_patch_branch (mb, pos_end);
1861 mono_mb_emit_byte (mb, CEE_RET);
1864 info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
1865 info->d.proxy.klass = klass;
1866 res = mono_mb_create_and_cache_full (cache, klass, mb, isint_sig, isint_sig->param_count + 16, info, NULL);
1873 mono_upgrade_remote_class_wrapper (MonoReflectionType *rtype_raw, MonoTransparentProxy *tproxy_raw)
1876 MONO_HANDLE_DCL (MonoReflectionType, rtype);
1877 MONO_HANDLE_DCL (MonoTransparentProxy, tproxy);
1878 MonoDomain *domain = MONO_HANDLE_DOMAIN (tproxy);
1879 MonoClass *klass = mono_class_from_mono_type (MONO_HANDLE_GETVAL (rtype, type));
1880 mono_upgrade_remote_class (domain, MONO_HANDLE_CAST (MonoObject, tproxy), klass, &error);
1884 #else /* DISABLE_REMOTING */
1887 mono_remoting_init (void)
1891 #endif /* DISABLE_REMOTING */
1893 /* mono_get_xdomain_marshal_type()
1894 * Returns the kind of marshalling that a type needs for cross domain calls.
1896 static MonoXDomainMarshalType
1897 mono_get_xdomain_marshal_type (MonoType *t)
1900 case MONO_TYPE_VOID:
1901 g_assert_not_reached ();
1905 case MONO_TYPE_BOOLEAN:
1908 case MONO_TYPE_CHAR:
1915 return MONO_MARSHAL_NONE;
1916 case MONO_TYPE_STRING:
1917 return MONO_MARSHAL_COPY;
1918 case MONO_TYPE_ARRAY:
1919 case MONO_TYPE_SZARRAY: {
1920 MonoClass *elem_class = mono_class_from_mono_type (t)->element_class;
1921 if (mono_get_xdomain_marshal_type (&(elem_class->byval_arg)) != MONO_MARSHAL_SERIALIZE)
1922 return MONO_MARSHAL_COPY;
1928 return MONO_MARSHAL_SERIALIZE;
1931 /* Replace the given array element by a copy in the current domain */
1933 xdomain_copy_array_element_inplace (MonoArrayHandle arr, int i, MonoError *error)
1935 HANDLE_FUNCTION_ENTER ();
1937 MonoObjectHandle item = MONO_HANDLE_NEW (MonoObject, NULL);
1938 MONO_HANDLE_ARRAY_GETREF (item, arr, i);
1940 MonoObjectHandle item_copy = mono_marshal_xdomain_copy_value_handle (item, error);
1943 MONO_HANDLE_ARRAY_SETREF (arr, i, item_copy);
1945 HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
1949 * mono_marshal_xdomain_copy_value_handle:
1950 * \param val The value to copy.
1951 * \param error set on failure.
1952 * Makes a copy of \p val suitable for the current domain.
1953 * On failure returns NULL and sets \p error.
1956 mono_marshal_xdomain_copy_value_handle (MonoObjectHandle val, MonoError *error)
1959 MonoObjectHandle result = MONO_HANDLE_NEW (MonoObject, NULL);
1960 if (MONO_HANDLE_IS_NULL (val))
1963 MonoDomain *domain = mono_domain_get ();
1965 MonoClass *klass = mono_handle_class (val);
1967 switch (klass->byval_arg.type) {
1968 case MONO_TYPE_VOID:
1969 g_assert_not_reached ();
1973 case MONO_TYPE_BOOLEAN:
1976 case MONO_TYPE_CHAR:
1982 case MONO_TYPE_R8: {
1983 uint32_t gchandle = mono_gchandle_from_handle (val, TRUE);
1984 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 */
1985 mono_gchandle_free (gchandle);
1988 MONO_HANDLE_ASSIGN (result, res);
1991 case MONO_TYPE_STRING: {
1992 MonoStringHandle str = MONO_HANDLE_CAST (MonoString, val);
1993 uint32_t gchandle = mono_gchandle_from_handle (val, TRUE);
1994 MonoStringHandle res = mono_string_new_utf16_handle (domain, mono_string_chars (MONO_HANDLE_RAW (str)), mono_string_handle_length (str), error);
1995 mono_gchandle_free (gchandle);
1998 MONO_HANDLE_ASSIGN (result, res);
2001 case MONO_TYPE_ARRAY:
2002 case MONO_TYPE_SZARRAY: {
2003 MonoArrayHandle arr = MONO_HANDLE_CAST (MonoArray, val);
2004 MonoXDomainMarshalType mt = mono_get_xdomain_marshal_type (&klass->element_class->byval_arg);
2005 if (mt == MONO_MARSHAL_SERIALIZE)
2007 MonoArrayHandle acopy = mono_array_clone_in_domain (domain, arr, error);
2011 if (mt == MONO_MARSHAL_COPY) {
2012 int i, len = mono_array_handle_length (acopy);
2013 for (i = 0; i < len; i++) {
2014 if (!xdomain_copy_array_element_inplace (acopy, i, error))
2018 MONO_HANDLE_ASSIGN (result, acopy);
2029 /* mono_marshal_xdomain_copy_value
2030 * Makes a copy of "val" suitable for the current domain.
2033 mono_marshal_xdomain_copy_value (MonoObject* val_raw, MonoError *error)
2035 HANDLE_FUNCTION_ENTER ();
2036 /* FIXME callers of mono_marshal_xdomain_copy_value should use handles */
2037 MONO_HANDLE_DCL (MonoObject, val);
2038 MonoObjectHandle result = mono_marshal_xdomain_copy_value_handle (val, error);
2039 HANDLE_FUNCTION_RETURN_OBJ (result);
2042 /* mono_marshal_xdomain_copy_value
2043 * Makes a copy of "val" suitable for the current domain.
2046 ves_icall_mono_marshal_xdomain_copy_value (MonoObject *val)
2049 MonoObject *result = mono_marshal_xdomain_copy_value (val, &error);
2050 mono_error_set_pending_exception (&error);