2 * remoting.c: Remoting support
4 * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
5 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
6 * Copyright 2011-2014 Xamarin, Inc (http://www.xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #include "mono/metadata/handle.h"
14 #include "mono/metadata/remoting.h"
15 #include "mono/metadata/marshal.h"
16 #include "mono/metadata/marshal-internals.h"
17 #include "mono/metadata/abi-details.h"
18 #include "mono/metadata/cominterop.h"
19 #include "mono/metadata/tabledefs.h"
20 #include "mono/metadata/exception.h"
21 #include "mono/metadata/debug-helpers.h"
22 #include "mono/metadata/reflection-internals.h"
25 MONO_MARSHAL_NONE
, /* No marshalling needed */
26 MONO_MARSHAL_COPY
, /* Can be copied by value to the new domain */
27 MONO_MARSHAL_COPY_OUT
, /* out parameter that needs to be copied back to the original instance */
28 MONO_MARSHAL_SERIALIZE
/* Value needs to be serialized into the new domain */
29 } MonoXDomainMarshalType
;
31 #ifndef DISABLE_REMOTING
33 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
37 #include "mono/cil/opcode.def"
42 struct _MonoRemotingMethods
{
44 MonoMethod
*invoke_with_check
;
45 MonoMethod
*xdomain_invoke
;
46 MonoMethod
*xdomain_dispatch
;
49 typedef struct _MonoRemotingMethods MonoRemotingMethods
;
52 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
);
55 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
);
58 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
);
61 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype
, MonoTransparentProxy
*tproxy
);
63 static MonoXDomainMarshalType
64 mono_get_xdomain_marshal_type (MonoType
*t
);
67 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
);
69 static MonoReflectionType
*
70 type_from_handle (MonoType
*handle
);
72 /* Class lazy loading functions */
73 static GENERATE_GET_CLASS_WITH_CACHE (remoting_services
, "System.Runtime.Remoting", "RemotingServices")
74 static GENERATE_GET_CLASS_WITH_CACHE (call_context
, "System.Runtime.Remoting.Messaging", "CallContext")
75 static GENERATE_GET_CLASS_WITH_CACHE (context
, "System.Runtime.Remoting.Contexts", "Context")
77 static mono_mutex_t remoting_mutex
;
78 static gboolean remoting_mutex_inited
;
80 static MonoClass
*byte_array_class
;
82 static MonoMethod
*method_rs_serialize
, *method_rs_deserialize
, *method_exc_fixexc
, *method_rs_appdomain_target
;
83 static MonoMethod
*method_set_call_context
, *method_needs_context_sink
, *method_rs_serialize_exc
;
87 mono_compile_method_icall (MonoMethod
*method
);
90 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
92 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
94 mono_register_jit_icall (func
, name
, sig
, save
);
100 g_assert (remoting_mutex_inited
);
101 mono_os_mutex_lock (&remoting_mutex
);
105 remoting_unlock (void)
107 g_assert (remoting_mutex_inited
);
108 mono_os_mutex_unlock (&remoting_mutex
);
112 * Return the hash table pointed to by VAR, lazily creating it if neccesary.
115 get_cache (GHashTable
**var
, GHashFunc hash_func
, GCompareFunc equal_func
)
121 g_hash_table_new (hash_func
, equal_func
);
122 mono_memory_barrier ();
131 get_cache_full (GHashTable
**var
, GHashFunc hash_func
, GCompareFunc equal_func
, GDestroyNotify key_destroy_func
, GDestroyNotify value_destroy_func
)
137 g_hash_table_new_full (hash_func
, equal_func
, key_destroy_func
, value_destroy_func
);
138 mono_memory_barrier ();
147 mono_remoting_init (void)
149 mono_os_mutex_init (&remoting_mutex
);
150 remoting_mutex_inited
= TRUE
;
154 mono_remoting_marshal_init (void)
158 static gboolean module_initialized
= FALSE
;
159 static gboolean icalls_registered
= FALSE
;
161 if (module_initialized
)
164 byte_array_class
= mono_array_class_get (mono_defaults
.byte_class
, 1);
167 klass
= mono_class_get_remoting_services_class ();
168 method_rs_serialize
= mono_class_get_method_from_name (klass
, "SerializeCallData", -1);
169 g_assert (method_rs_serialize
);
170 method_rs_deserialize
= mono_class_get_method_from_name (klass
, "DeserializeCallData", -1);
171 g_assert (method_rs_deserialize
);
172 method_rs_serialize_exc
= mono_class_get_method_from_name (klass
, "SerializeExceptionData", -1);
173 g_assert (method_rs_serialize_exc
);
175 klass
= mono_defaults
.real_proxy_class
;
176 method_rs_appdomain_target
= mono_class_get_method_from_name (klass
, "GetAppDomainTarget", -1);
177 g_assert (method_rs_appdomain_target
);
179 klass
= mono_defaults
.exception_class
;
180 method_exc_fixexc
= mono_class_get_method_from_name (klass
, "FixRemotingException", -1);
181 g_assert (method_exc_fixexc
);
183 klass
= mono_class_get_call_context_class ();
184 method_set_call_context
= mono_class_get_method_from_name (klass
, "SetCurrentCallContext", -1);
185 g_assert (method_set_call_context
);
187 klass
= mono_class_get_context_class ();
188 method_needs_context_sink
= mono_class_get_method_from_name (klass
, "get_NeedsContextSink", -1);
189 g_assert (method_needs_context_sink
);
194 if (!icalls_registered
) {
195 register_icall (type_from_handle
, "type_from_handle", "object ptr", FALSE
);
196 register_icall (mono_marshal_set_domain_by_id
, "mono_marshal_set_domain_by_id", "int32 int32 int32", FALSE
);
197 register_icall (mono_marshal_check_domain_image
, "mono_marshal_check_domain_image", "int32 int32 ptr", FALSE
);
198 register_icall (ves_icall_mono_marshal_xdomain_copy_value
, "ves_icall_mono_marshal_xdomain_copy_value", "object object", FALSE
);
199 register_icall (mono_marshal_xdomain_copy_out_value
, "mono_marshal_xdomain_copy_out_value", "void object object", FALSE
);
200 register_icall (mono_remoting_wrapper
, "mono_remoting_wrapper", "object ptr ptr", FALSE
);
201 register_icall (mono_upgrade_remote_class_wrapper
, "mono_upgrade_remote_class_wrapper", "void object object", FALSE
);
204 register_icall (mono_compile_method_icall
, "mono_compile_method_icall", "ptr ptr", FALSE
);
209 icalls_registered
= TRUE
;
211 mono_loader_unlock ();
213 module_initialized
= TRUE
;
216 /* This is an icall, it will return NULL and set pending exception on failure */
217 static MonoReflectionType
*
218 type_from_handle (MonoType
*handle
)
221 MonoReflectionType
*ret
;
222 MonoDomain
*domain
= mono_domain_get ();
223 MonoClass
*klass
= mono_class_from_mono_type (handle
);
225 mono_class_init (klass
);
227 ret
= mono_type_get_object_checked (domain
, handle
, &error
);
228 mono_error_set_pending_exception (&error
);
235 mono_mb_emit_proxy_check (MonoMethodBuilder
*mb
, int branch_code
)
238 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoObject
, vtable
));
239 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
240 mono_mb_emit_icon (mb
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
241 mono_mb_emit_byte (mb
, CEE_ADD
);
242 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
243 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
244 mono_mb_emit_byte (mb
, CEE_MONO_CLASSCONST
);
245 mono_mb_emit_i4 (mb
, mono_mb_add_data (mb
, mono_defaults
.transparent_proxy_class
));
246 pos
= mono_mb_emit_branch (mb
, branch_code
);
251 mono_mb_emit_xdomain_check (MonoMethodBuilder
*mb
, int branch_code
)
254 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
255 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
256 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
257 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
258 mono_mb_emit_icon (mb
, -1);
259 pos
= mono_mb_emit_branch (mb
, branch_code
);
264 mono_mb_emit_contextbound_check (MonoMethodBuilder
*mb
, int branch_code
)
266 static int offset
= -1;
270 mono_marshal_find_bitfield_offset (MonoClass
, contextbound
, &offset
, &mask
);
272 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, remote_class
));
273 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
274 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRemoteClass
, proxy_class
));
275 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
276 mono_mb_emit_ldflda (mb
, offset
);
277 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
278 mono_mb_emit_icon (mb
, mask
);
279 mono_mb_emit_byte (mb
, CEE_AND
);
280 mono_mb_emit_icon (mb
, 0);
281 return mono_mb_emit_branch (mb
, branch_code
);
283 #endif /* !DISABLE_JIT */
285 static inline MonoMethod
*
286 mono_marshal_remoting_find_in_cache (MonoMethod
*method
, int wrapper_type
)
288 MonoMethod
*res
= NULL
;
289 MonoRemotingMethods
*wrps
= NULL
;
291 mono_marshal_lock_internal ();
292 if (mono_method_get_wrapper_cache (method
)->remoting_invoke_cache
)
293 wrps
= (MonoRemotingMethods
*)g_hash_table_lookup (mono_method_get_wrapper_cache (method
)->remoting_invoke_cache
, method
);
296 switch (wrapper_type
) {
297 case MONO_WRAPPER_REMOTING_INVOKE
: res
= wrps
->invoke
; break;
298 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= wrps
->invoke_with_check
; break;
299 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= wrps
->xdomain_invoke
; break;
300 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= wrps
->xdomain_dispatch
; break;
304 /* it is important to do the unlock after the load from wrps, since in
305 * mono_remoting_mb_create_and_cache () we drop the marshal lock to be able
306 * to take the loader lock and some other thread may set the fields.
308 mono_marshal_unlock_internal ();
312 /* Create the method from the builder and place it in the cache */
313 static inline MonoMethod
*
314 mono_remoting_mb_create_and_cache (MonoMethod
*key
, MonoMethodBuilder
*mb
,
315 MonoMethodSignature
*sig
, int max_stack
, WrapperInfo
*info
)
317 MonoMethod
**res
= NULL
;
318 MonoRemotingMethods
*wrps
;
321 cache
= get_cache_full (&mono_method_get_wrapper_cache (key
)->remoting_invoke_cache
, mono_aligned_addr_hash
, NULL
, NULL
, g_free
);
323 mono_marshal_lock_internal ();
324 wrps
= (MonoRemotingMethods
*)g_hash_table_lookup (cache
, key
);
326 wrps
= g_new0 (MonoRemotingMethods
, 1);
327 g_hash_table_insert (cache
, key
, wrps
);
330 switch (mb
->method
->wrapper_type
) {
331 case MONO_WRAPPER_REMOTING_INVOKE
: res
= &wrps
->invoke
; break;
332 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= &wrps
->invoke_with_check
; break;
333 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= &wrps
->xdomain_invoke
; break;
334 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= &wrps
->xdomain_dispatch
; break;
335 default: g_assert_not_reached (); break;
337 mono_marshal_unlock_internal ();
341 newm
= mono_mb_create_method (mb
, sig
, max_stack
);
343 mono_marshal_lock_internal ();
346 mono_marshal_set_wrapper_info (*res
, info
);
347 mono_marshal_unlock_internal ();
349 mono_marshal_unlock_internal ();
350 mono_free_method (newm
);
358 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
)
361 MonoMethodMessage
*msg
;
362 MonoTransparentProxy
*this_obj
;
363 MonoObject
*res
, *exc
;
366 this_obj
= *((MonoTransparentProxy
**)params
[0]);
369 g_assert (mono_object_is_transparent_proxy (this_obj
));
371 /* skip the this pointer */
374 if (mono_class_is_contextbound (this_obj
->remote_class
->proxy_class
) && this_obj
->rp
->context
== (MonoObject
*) mono_context_get ())
377 MonoMethodSignature
*sig
= mono_method_signature (method
);
378 int count
= sig
->param_count
;
379 gpointer
* mparams
= (gpointer
*) alloca(count
*sizeof(gpointer
));
381 for (i
=0; i
<count
; i
++) {
382 MonoClass
*klass
= mono_class_from_mono_type (sig
->params
[i
]);
383 if (klass
->valuetype
) {
384 if (sig
->params
[i
]->byref
) {
385 mparams
[i
] = *((gpointer
*)params
[i
]);
387 /* runtime_invoke expects a boxed instance */
388 if (mono_class_is_nullable (mono_class_from_mono_type (sig
->params
[i
]))) {
389 mparams
[i
] = mono_nullable_box ((guint8
*)params
[i
], klass
, &error
);
393 mparams
[i
] = params
[i
];
396 mparams
[i
] = *((gpointer
**)params
[i
]);
400 res
= mono_runtime_invoke_checked (method
, method
->klass
->valuetype
? mono_object_unbox ((MonoObject
*)this_obj
): this_obj
, mparams
, &error
);
407 msg
= mono_method_call_message_new (method
, params
, NULL
, NULL
, NULL
, &error
);
411 res
= mono_remoting_invoke ((MonoObject
*)this_obj
->rp
, msg
, &exc
, &out_args
, &error
);
416 mono_error_init (&error
);
417 mono_error_set_exception_instance (&error
, (MonoException
*)exc
);
421 mono_method_return_message_restore (method
, params
, out_args
, &error
);
422 if (!is_ok (&error
)) goto fail
;
426 /* This icall will be called from managed code, and more over
427 * from a protected wrapper so interruptions such as pending
428 * exceptions will not be honored. (See
429 * is_running_protected_wrapper () in threads.c and
430 * mono_marshal_get_remoting_invoke () in remoting.c)
432 mono_error_raise_exception (&error
); /* OK to throw, see note */
438 mono_marshal_get_remoting_invoke (MonoMethod
*method
)
440 MonoMethodSignature
*sig
;
441 MonoMethodBuilder
*mb
;
448 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
451 /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
453 if (mono_class_is_com_object (method
->klass
) || method
->klass
== mono_class_try_get_com_object_class ()) {
454 MonoVTable
*vtable
= mono_class_vtable (mono_domain_get (), method
->klass
);
455 g_assert (vtable
); /*FIXME do proper error handling*/
457 if (!mono_vtable_is_remote (vtable
)) {
458 return mono_cominterop_get_invoke (method
);
463 sig
= mono_signature_no_pinvoke (method
);
465 /* we cant remote methods without this pointer */
469 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE
)))
472 mono_remoting_marshal_init ();
474 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE
);
477 mb
->method
->save_lmf
= 1;
479 params_var
= mono_mb_emit_save_args (mb
, sig
, TRUE
);
481 mono_mb_emit_ptr (mb
, method
);
482 mono_mb_emit_ldloc (mb
, params_var
);
483 mono_mb_emit_icall (mb
, mono_remoting_wrapper
);
484 // FIXME: this interrupt checkpoint code is a no-op since 'mb'
485 // is a MONO_WRAPPER_REMOTING_INVOKE, and
486 // mono_thread_interruption_checkpoint_request (FALSE)
487 // considers such wrappers "protected" and always returns
488 // NULL as if there's no pending interruption.
489 mono_marshal_emit_thread_interrupt_checkpoint (mb
);
491 if (sig
->ret
->type
== MONO_TYPE_VOID
) {
492 mono_mb_emit_byte (mb
, CEE_POP
);
493 mono_mb_emit_byte (mb
, CEE_RET
);
495 mono_mb_emit_restore_result (mb
, sig
->ret
);
499 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
500 info
->d
.remoting
.method
= method
;
501 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16, info
);
507 /* mono_marshal_xdomain_copy_out_value()
508 * Copies the contents of the src instance into the dst instance. src and dst
509 * must have the same type, and if they are arrays, the same size.
511 * This is an icall, it may use mono_error_set_pending_exception
514 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
)
517 if (src
== NULL
|| dst
== NULL
) return;
519 g_assert (mono_object_class (src
) == mono_object_class (dst
));
521 switch (mono_object_class (src
)->byval_arg
.type
) {
522 case MONO_TYPE_ARRAY
:
523 case MONO_TYPE_SZARRAY
: {
524 int mt
= mono_get_xdomain_marshal_type (&(mono_object_class (src
)->element_class
->byval_arg
));
525 if (mt
== MONO_MARSHAL_SERIALIZE
) return;
526 if (mt
== MONO_MARSHAL_COPY
) {
527 int i
, len
= mono_array_length ((MonoArray
*)dst
);
528 for (i
= 0; i
< len
; i
++) {
529 MonoObject
*item
= (MonoObject
*)mono_array_get ((MonoArray
*)src
, gpointer
, i
);
530 MonoObject
*item_copy
= mono_marshal_xdomain_copy_value (item
, &error
);
531 if (mono_error_set_pending_exception (&error
))
533 mono_array_setref ((MonoArray
*)dst
, i
, item_copy
);
536 mono_array_full_copy ((MonoArray
*)src
, (MonoArray
*)dst
);
547 #if !defined (DISABLE_JIT)
549 mono_marshal_emit_xdomain_copy_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
551 mono_mb_emit_icall (mb
, ves_icall_mono_marshal_xdomain_copy_value
);
552 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
556 mono_marshal_emit_xdomain_copy_out_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
558 mono_mb_emit_icall (mb
, mono_marshal_xdomain_copy_out_value
);
562 /* mono_marshal_supports_fast_xdomain()
563 * Returns TRUE if the method can use the fast xdomain wrapper.
566 mono_marshal_supports_fast_xdomain (MonoMethod
*method
)
568 return !mono_class_is_contextbound (method
->klass
) &&
569 !((method
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) && (strcmp (".ctor", method
->name
) == 0));
573 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
)
575 MonoDomain
*current_domain
= mono_domain_get ();
576 MonoDomain
*domain
= mono_domain_get_by_id (id
);
578 if (!domain
|| !mono_domain_set (domain
, FALSE
)) {
579 mono_set_pending_exception (mono_get_exception_appdomain_unloaded ());
584 mono_thread_push_appdomain_ref (domain
);
586 mono_thread_pop_appdomain_ref ();
588 return current_domain
->domain_id
;
591 #if !defined (DISABLE_JIT)
593 mono_marshal_emit_switch_domain (MonoMethodBuilder
*mb
)
595 mono_mb_emit_icall (mb
, mono_marshal_set_domain_by_id
);
599 mono_compile_method_icall (MonoMethod
*method
)
602 gpointer result
= mono_compile_method_checked (method
, &error
);
603 mono_error_set_pending_exception (&error
);
607 /* mono_marshal_emit_load_domain_method ()
608 * Loads into the stack a pointer to the code of the provided method for
609 * the current domain.
612 mono_marshal_emit_load_domain_method (MonoMethodBuilder
*mb
, MonoMethod
*method
)
614 /* We need a pointer to the method for the running domain (not the domain
615 * that compiles the method).
617 mono_mb_emit_ptr (mb
, method
);
618 mono_mb_emit_icall (mb
, mono_compile_method_icall
);
622 /* mono_marshal_check_domain_image ()
623 * Returns TRUE if the image is loaded in the specified
624 * application domain.
627 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
)
632 MonoDomain
*domain
= mono_domain_get_by_id (domain_id
);
636 mono_domain_assemblies_lock (domain
);
637 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
638 ass
= (MonoAssembly
*)tmp
->data
;
639 if (ass
->image
== image
)
642 mono_domain_assemblies_unlock (domain
);
647 /* mono_marshal_get_xappdomain_dispatch ()
648 * Generates a method that dispatches a method call from another domain into
649 * the current domain.
652 mono_marshal_get_xappdomain_dispatch (MonoMethod
*method
, int *marshal_types
, int complex_count
, int complex_out_count
, int ret_marshal_type
)
654 MonoMethodSignature
*sig
, *csig
;
655 MonoMethodBuilder
*mb
;
657 int i
, j
, param_index
, copy_locals_base
;
658 MonoClass
*ret_class
= NULL
;
659 int loc_array
=0, loc_return
=0, loc_serialized_exc
=0;
660 MonoExceptionClause
*main_clause
;
662 gboolean copy_return
;
665 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_DISPATCH
)))
668 sig
= mono_method_signature (method
);
669 copy_return
= (sig
->ret
->type
!= MONO_TYPE_VOID
&& ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
);
672 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3 + sig
->param_count
- complex_count
);
673 csig
->params
[j
++] = &mono_defaults
.object_class
->byval_arg
;
674 csig
->params
[j
++] = &byte_array_class
->this_arg
;
675 csig
->params
[j
++] = &byte_array_class
->this_arg
;
676 for (i
= 0; i
< sig
->param_count
; i
++) {
677 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
)
678 csig
->params
[j
++] = sig
->params
[i
];
681 csig
->ret
= sig
->ret
;
683 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
685 csig
->hasthis
= FALSE
;
687 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_DISPATCH
);
688 mb
->method
->save_lmf
= 1;
693 loc_serialized_exc
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
694 if (complex_count
> 0)
695 loc_array
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
696 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
697 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
698 ret_class
= mono_class_from_mono_type (sig
->ret
);
703 main_clause
= (MonoExceptionClause
*)mono_image_alloc0 (method
->klass
->image
, sizeof (MonoExceptionClause
));
704 main_clause
->try_offset
= mono_mb_get_label (mb
);
706 /* Clean the call context */
708 mono_mb_emit_byte (mb
, CEE_LDNULL
);
709 mono_mb_emit_managed_call (mb
, method_set_call_context
, NULL
);
710 mono_mb_emit_byte (mb
, CEE_POP
);
712 /* Deserialize call data */
714 mono_mb_emit_ldarg (mb
, 1);
715 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
716 mono_mb_emit_byte (mb
, CEE_DUP
);
717 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
719 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
720 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
722 if (complex_count
> 0)
723 mono_mb_emit_stloc (mb
, loc_array
);
725 mono_mb_emit_byte (mb
, CEE_POP
);
727 mono_mb_patch_short_branch (mb
, pos
);
729 /* Get the target object */
731 mono_mb_emit_ldarg (mb
, 0);
732 mono_mb_emit_managed_call (mb
, method_rs_appdomain_target
, NULL
);
734 /* Load the arguments */
736 copy_locals_base
= mb
->locals
;
737 param_index
= 3; // Index of the first non-serialized parameter of this wrapper
739 for (i
= 0; i
< sig
->param_count
; i
++) {
740 MonoType
*pt
= sig
->params
[i
];
741 MonoClass
*pclass
= mono_class_from_mono_type (pt
);
742 switch (marshal_types
[i
]) {
743 case MONO_MARSHAL_SERIALIZE
: {
744 /* take the value from the serialized array */
745 mono_mb_emit_ldloc (mb
, loc_array
);
746 mono_mb_emit_icon (mb
, j
++);
748 if (pclass
->valuetype
) {
749 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
750 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
752 mono_mb_emit_op (mb
, CEE_LDELEMA
, pclass
);
755 if (pclass
->valuetype
) {
756 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
757 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
758 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
760 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
761 if (pclass
!= mono_defaults
.object_class
) {
762 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
768 case MONO_MARSHAL_COPY_OUT
: {
769 /* Keep a local copy of the value since we need to copy it back after the call */
770 int copy_local
= mono_mb_add_local (mb
, &(pclass
->byval_arg
));
771 mono_mb_emit_ldarg (mb
, param_index
++);
772 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
773 mono_mb_emit_byte (mb
, CEE_DUP
);
774 mono_mb_emit_stloc (mb
, copy_local
);
777 case MONO_MARSHAL_COPY
: {
778 mono_mb_emit_ldarg (mb
, param_index
);
780 mono_mb_emit_byte (mb
, CEE_DUP
);
781 mono_mb_emit_byte (mb
, CEE_DUP
);
782 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
783 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
784 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
786 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
791 case MONO_MARSHAL_NONE
:
792 mono_mb_emit_ldarg (mb
, param_index
++);
797 /* Make the call to the real object */
799 mono_marshal_emit_thread_force_interrupt_checkpoint (mb
);
801 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
803 if (sig
->ret
->type
!= MONO_TYPE_VOID
)
804 mono_mb_emit_stloc (mb
, loc_return
);
806 /* copy back MONO_MARSHAL_COPY_OUT parameters */
810 for (i
= 0; i
< sig
->param_count
; i
++) {
811 if (marshal_types
[i
] == MONO_MARSHAL_SERIALIZE
) continue;
812 if (marshal_types
[i
] == MONO_MARSHAL_COPY_OUT
) {
813 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
814 mono_mb_emit_ldarg (mb
, param_index
);
815 mono_marshal_emit_xdomain_copy_out_value (mb
, mono_class_from_mono_type (sig
->params
[i
]));
820 /* Serialize the return values */
822 if (complex_out_count
> 0) {
823 /* Reset parameters in the array that don't need to be serialized back */
825 for (i
= 0; i
< sig
->param_count
; i
++) {
826 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
827 if (!sig
->params
[i
]->byref
) {
828 mono_mb_emit_ldloc (mb
, loc_array
);
829 mono_mb_emit_icon (mb
, j
);
830 mono_mb_emit_byte (mb
, CEE_LDNULL
);
831 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
836 /* Add the return value to the array */
838 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
839 mono_mb_emit_ldloc (mb
, loc_array
);
840 mono_mb_emit_icon (mb
, complex_count
); /* The array has an additional slot to hold the ret value */
841 mono_mb_emit_ldloc (mb
, loc_return
);
843 g_assert (ret_class
); /*FIXME properly fail here*/
844 if (ret_class
->valuetype
) {
845 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
847 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
852 mono_mb_emit_ldarg (mb
, 1);
853 mono_mb_emit_ldloc (mb
, loc_array
);
854 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
855 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
856 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
857 mono_mb_emit_ldarg (mb
, 1);
858 mono_mb_emit_ldloc (mb
, loc_return
);
859 if (ret_class
->valuetype
) {
860 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
862 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
863 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
865 mono_mb_emit_ldarg (mb
, 1);
866 mono_mb_emit_byte (mb
, CEE_LDNULL
);
867 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
868 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
871 mono_mb_emit_ldarg (mb
, 2);
872 mono_mb_emit_byte (mb
, CEE_LDNULL
);
873 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
874 pos_leave
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
876 /* Main exception catch */
877 main_clause
->flags
= MONO_EXCEPTION_CLAUSE_NONE
;
878 main_clause
->try_len
= mono_mb_get_pos (mb
) - main_clause
->try_offset
;
879 main_clause
->data
.catch_class
= mono_defaults
.object_class
;
882 main_clause
->handler_offset
= mono_mb_get_label (mb
);
883 mono_mb_emit_managed_call (mb
, method_rs_serialize_exc
, NULL
);
884 mono_mb_emit_stloc (mb
, loc_serialized_exc
);
885 mono_mb_emit_ldarg (mb
, 2);
886 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
887 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
888 mono_mb_emit_branch (mb
, CEE_LEAVE
);
889 main_clause
->handler_len
= mono_mb_get_pos (mb
) - main_clause
->handler_offset
;
892 mono_mb_patch_branch (mb
, pos_leave
);
895 mono_mb_emit_ldloc (mb
, loc_return
);
897 mono_mb_emit_byte (mb
, CEE_RET
);
899 mono_mb_set_clauses (mb
, 1, main_clause
);
902 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
903 info
->d
.remoting
.method
= method
;
904 res
= mono_remoting_mb_create_and_cache (method
, mb
, csig
, csig
->param_count
+ 16, info
);
910 /* mono_marshal_get_xappdomain_invoke ()
911 * Generates a fast remoting wrapper for cross app domain calls.
914 mono_marshal_get_xappdomain_invoke (MonoMethod
*method
)
916 MonoMethodSignature
*sig
;
917 MonoMethodBuilder
*mb
;
919 int i
, j
, complex_count
, complex_out_count
, copy_locals_base
;
921 MonoClass
*ret_class
= NULL
;
922 MonoMethod
*xdomain_method
;
923 int ret_marshal_type
= MONO_MARSHAL_NONE
;
924 int loc_array
=0, loc_serialized_data
=-1, loc_real_proxy
;
925 int loc_old_domainid
, loc_domainid
, loc_return
=0, loc_serialized_exc
=0, loc_context
;
926 int pos
, pos_dispatch
, pos_noex
;
927 gboolean copy_return
= FALSE
;
932 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
935 /* we cant remote methods without this pointer */
936 if (!mono_method_signature (method
)->hasthis
)
939 mono_remoting_marshal_init ();
941 if (!mono_marshal_supports_fast_xdomain (method
))
942 return mono_marshal_get_remoting_invoke (method
);
944 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_INVOKE
)))
947 sig
= mono_signature_no_pinvoke (method
);
949 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_INVOKE
);
950 mb
->method
->save_lmf
= 1;
952 /* Count the number of parameters that need to be serialized */
954 marshal_types
= (int *)alloca (sizeof (int) * sig
->param_count
);
955 complex_count
= complex_out_count
= 0;
956 for (i
= 0; i
< sig
->param_count
; i
++) {
957 MonoType
*ptype
= sig
->params
[i
];
958 int mt
= mono_get_xdomain_marshal_type (ptype
);
960 /* If the [Out] attribute is applied to a parameter that can be internally copied,
961 * the copy will be made by reusing the original object instance
963 if ((ptype
->attrs
& PARAM_ATTRIBUTE_OUT
) != 0 && mt
== MONO_MARSHAL_COPY
&& !ptype
->byref
)
964 mt
= MONO_MARSHAL_COPY_OUT
;
965 else if (mt
== MONO_MARSHAL_SERIALIZE
) {
967 if (ptype
->byref
) complex_out_count
++;
969 marshal_types
[i
] = mt
;
972 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
973 ret_marshal_type
= mono_get_xdomain_marshal_type (sig
->ret
);
974 ret_class
= mono_class_from_mono_type (sig
->ret
);
975 copy_return
= ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
;
981 if (complex_count
> 0)
982 loc_array
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
983 loc_serialized_data
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
984 loc_real_proxy
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
986 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
987 loc_old_domainid
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
988 loc_domainid
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
989 loc_serialized_exc
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
990 loc_context
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
992 /* Save thread domain data */
994 mono_mb_emit_icall (mb
, mono_context_get
);
995 mono_mb_emit_byte (mb
, CEE_DUP
);
996 mono_mb_emit_stloc (mb
, loc_context
);
998 /* If the thread is not running in the default context, it needs to go
999 * through the whole remoting sink, since the context is going to change
1001 mono_mb_emit_managed_call (mb
, method_needs_context_sink
, NULL
);
1002 pos
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
1004 /* Another case in which the fast path can't be used: when the target domain
1005 * has a different image for the same assembly.
1008 /* Get the target domain id */
1010 mono_mb_emit_ldarg (mb
, 0);
1011 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1012 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1013 mono_mb_emit_byte (mb
, CEE_DUP
);
1014 mono_mb_emit_stloc (mb
, loc_real_proxy
);
1016 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
1017 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
1018 mono_mb_emit_stloc (mb
, loc_domainid
);
1020 /* Check if the target domain has the same image for the required assembly */
1022 mono_mb_emit_ldloc (mb
, loc_domainid
);
1023 mono_mb_emit_ptr (mb
, method
->klass
->image
);
1024 mono_mb_emit_icall (mb
, mono_marshal_check_domain_image
);
1025 pos_dispatch
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
1027 /* Use the whole remoting sink to dispatch this message */
1029 mono_mb_patch_short_branch (mb
, pos
);
1031 mono_mb_emit_ldarg (mb
, 0);
1032 for (i
= 0; i
< sig
->param_count
; i
++)
1033 mono_mb_emit_ldarg (mb
, i
+ 1);
1035 mono_mb_emit_managed_call (mb
, mono_marshal_get_remoting_invoke (method
), NULL
);
1036 mono_mb_emit_byte (mb
, CEE_RET
);
1037 mono_mb_patch_short_branch (mb
, pos_dispatch
);
1039 /* Create the array that will hold the parameters to be serialized */
1041 if (complex_count
> 0) {
1042 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 */
1043 mono_mb_emit_op (mb
, CEE_NEWARR
, mono_defaults
.object_class
);
1046 for (i
= 0; i
< sig
->param_count
; i
++) {
1048 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
1049 pclass
= mono_class_from_mono_type (sig
->params
[i
]);
1050 mono_mb_emit_byte (mb
, CEE_DUP
);
1051 mono_mb_emit_icon (mb
, j
);
1052 mono_mb_emit_ldarg (mb
, i
+ 1); /* 0=this */
1053 if (sig
->params
[i
]->byref
) {
1054 if (pclass
->valuetype
)
1055 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
1057 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1059 if (pclass
->valuetype
)
1060 mono_mb_emit_op (mb
, CEE_BOX
, pclass
);
1061 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
1064 mono_mb_emit_stloc (mb
, loc_array
);
1066 /* Serialize parameters */
1068 mono_mb_emit_ldloc (mb
, loc_array
);
1069 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
1070 mono_mb_emit_stloc (mb
, loc_serialized_data
);
1072 mono_mb_emit_byte (mb
, CEE_LDNULL
);
1073 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
1074 mono_mb_emit_stloc (mb
, loc_serialized_data
);
1079 mono_mb_emit_ldloc (mb
, loc_domainid
);
1080 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
1081 mono_marshal_emit_switch_domain (mb
);
1082 mono_mb_emit_stloc (mb
, loc_old_domainid
);
1084 /* Load the arguments */
1086 mono_mb_emit_ldloc (mb
, loc_real_proxy
);
1087 mono_mb_emit_ldloc_addr (mb
, loc_serialized_data
);
1088 mono_mb_emit_ldloc_addr (mb
, loc_serialized_exc
);
1090 copy_locals_base
= mb
->locals
;
1091 for (i
= 0; i
< sig
->param_count
; i
++) {
1092 switch (marshal_types
[i
]) {
1093 case MONO_MARSHAL_SERIALIZE
:
1095 case MONO_MARSHAL_COPY
: {
1096 mono_mb_emit_ldarg (mb
, i
+1);
1097 if (sig
->params
[i
]->byref
) {
1098 /* make a local copy of the byref parameter. The real parameter
1099 * will be updated after the xdomain call
1101 MonoClass
*pclass
= mono_class_from_mono_type (sig
->params
[i
]);
1102 int copy_local
= mono_mb_add_local (mb
, &(pclass
->byval_arg
));
1103 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1104 mono_mb_emit_stloc (mb
, copy_local
);
1105 mono_mb_emit_ldloc_addr (mb
, copy_local
);
1109 case MONO_MARSHAL_COPY_OUT
:
1110 case MONO_MARSHAL_NONE
:
1111 mono_mb_emit_ldarg (mb
, i
+1);
1116 /* Make the call to the invoke wrapper in the target domain */
1118 xdomain_method
= mono_marshal_get_xappdomain_dispatch (method
, marshal_types
, complex_count
, complex_out_count
, ret_marshal_type
);
1119 mono_marshal_emit_load_domain_method (mb
, xdomain_method
);
1120 mono_mb_emit_calli (mb
, mono_method_signature (xdomain_method
));
1123 mono_mb_emit_stloc (mb
, loc_return
);
1127 mono_mb_emit_ldloc (mb
, loc_old_domainid
);
1128 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1129 mono_marshal_emit_switch_domain (mb
);
1130 mono_mb_emit_byte (mb
, CEE_POP
);
1132 /* Restore thread domain data */
1134 mono_mb_emit_ldloc (mb
, loc_context
);
1135 mono_mb_emit_icall (mb
, mono_context_set
);
1137 /* if (loc_serialized_exc != null) ... */
1139 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
1140 pos_noex
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
1142 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
1143 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
1144 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
1145 mono_mb_emit_op (mb
, CEE_CASTCLASS
, mono_defaults
.exception_class
);
1146 mono_mb_emit_managed_call (mb
, method_exc_fixexc
, NULL
);
1147 mono_mb_emit_byte (mb
, CEE_THROW
);
1148 mono_mb_patch_short_branch (mb
, pos_noex
);
1150 /* copy back non-serialized output parameters */
1153 for (i
= 0; i
< sig
->param_count
; i
++) {
1154 if (!sig
->params
[i
]->byref
|| marshal_types
[i
] != MONO_MARSHAL_COPY
) continue;
1155 mono_mb_emit_ldarg (mb
, i
+ 1);
1156 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
1157 mono_marshal_emit_xdomain_copy_value (mb
, mono_class_from_mono_type (sig
->params
[i
]));
1158 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1161 /* Deserialize out parameters */
1163 if (complex_out_count
> 0) {
1164 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
1165 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
1166 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
1167 mono_mb_emit_stloc (mb
, loc_array
);
1169 /* Copy back output parameters and return type */
1172 for (i
= 0; i
< sig
->param_count
; i
++) {
1173 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
1174 if (sig
->params
[i
]->byref
) {
1175 MonoClass
*pclass
= mono_class_from_mono_type (sig
->params
[i
]);
1176 mono_mb_emit_ldarg (mb
, i
+ 1);
1177 mono_mb_emit_ldloc (mb
, loc_array
);
1178 mono_mb_emit_icon (mb
, j
);
1179 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
1180 if (pclass
->valuetype
) {
1181 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
1182 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
1183 mono_mb_emit_op (mb
, CEE_STOBJ
, pclass
);
1185 if (pclass
!= mono_defaults
.object_class
)
1186 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
1187 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1193 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
1194 mono_mb_emit_ldloc (mb
, loc_array
);
1195 mono_mb_emit_icon (mb
, complex_count
);
1196 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
1197 if (ret_class
->valuetype
) {
1198 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
1199 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
1202 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
1203 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
1204 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
1205 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
1206 if (ret_class
->valuetype
) {
1207 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
1208 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
1209 } else if (ret_class
!= mono_defaults
.object_class
) {
1210 mono_mb_emit_op (mb
, CEE_CASTCLASS
, ret_class
);
1213 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
1214 mono_mb_emit_byte (mb
, CEE_DUP
);
1215 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
1216 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
1218 mono_mb_patch_short_branch (mb
, pos
);
1219 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
1220 mono_mb_emit_byte (mb
, CEE_POP
);
1224 mono_mb_emit_ldloc (mb
, loc_return
);
1225 if (ret_marshal_type
== MONO_MARSHAL_COPY
)
1226 mono_marshal_emit_xdomain_copy_value (mb
, ret_class
);
1229 mono_mb_emit_byte (mb
, CEE_RET
);
1230 #endif /* DISABLE_JIT */
1232 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1233 info
->d
.remoting
.method
= method
;
1234 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16, info
);
1241 mono_marshal_get_remoting_invoke_for_target (MonoMethod
*method
, MonoRemotingTarget target_type
)
1243 if (target_type
== MONO_REMOTING_TARGET_APPDOMAIN
) {
1244 return mono_marshal_get_xappdomain_invoke (method
);
1245 } else if (target_type
== MONO_REMOTING_TARGET_COMINTEROP
) {
1247 return mono_cominterop_get_invoke (method
);
1249 g_assert_not_reached ();
1252 return mono_marshal_get_remoting_invoke (method
);
1258 G_GNUC_UNUSED
static gpointer
1259 mono_marshal_load_remoting_wrapper (MonoRealProxy
*rp
, MonoMethod
*method
)
1262 MonoMethod
*marshal_method
= NULL
;
1263 if (rp
->target_domain_id
!= -1)
1264 marshal_method
= mono_marshal_get_xappdomain_invoke (method
);
1266 marshal_method
= mono_marshal_get_remoting_invoke (method
);
1267 gpointer compiled_ptr
= mono_compile_method_checked (marshal_method
, &error
);
1268 mono_error_assert_ok (&error
);
1269 return compiled_ptr
;
1273 mono_marshal_get_remoting_invoke_with_check (MonoMethod
*method
)
1275 MonoMethodSignature
*sig
;
1276 MonoMethodBuilder
*mb
;
1277 MonoMethod
*res
, *native
;
1279 int i
, pos
, pos_rem
;
1283 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
1286 /* we cant remote methods without this pointer */
1287 g_assert (mono_method_signature (method
)->hasthis
);
1289 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)))
1292 sig
= mono_signature_no_pinvoke (method
);
1294 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
);
1297 for (i
= 0; i
<= sig
->param_count
; i
++)
1298 mono_mb_emit_ldarg (mb
, i
);
1300 mono_mb_emit_ldarg (mb
, 0);
1301 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
1303 if (mono_marshal_supports_fast_xdomain (method
)) {
1304 mono_mb_emit_ldarg (mb
, 0);
1305 pos_rem
= mono_mb_emit_xdomain_check (mb
, CEE_BEQ
);
1307 /* wrapper for cross app domain calls */
1308 native
= mono_marshal_get_xappdomain_invoke (method
);
1309 mono_mb_emit_managed_call (mb
, native
, mono_method_signature (native
));
1310 mono_mb_emit_byte (mb
, CEE_RET
);
1312 mono_mb_patch_branch (mb
, pos_rem
);
1314 /* wrapper for normal remote calls */
1315 native
= mono_marshal_get_remoting_invoke (method
);
1316 mono_mb_emit_managed_call (mb
, native
, mono_method_signature (native
));
1317 mono_mb_emit_byte (mb
, CEE_RET
);
1320 mono_mb_patch_branch (mb
, pos
);
1321 mono_mb_emit_managed_call (mb
, method
, mono_method_signature (method
));
1322 mono_mb_emit_byte (mb
, CEE_RET
);
1325 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1326 info
->d
.remoting
.method
= method
;
1327 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16, info
);
1334 * mono_marshal_get_ldfld_wrapper:
1335 * @type: the type of the field
1337 * This method generates a function which can be use to load a field with type
1338 * @type from an object. The generated function has the following signature:
1339 * <@type> ldfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset)
1342 mono_marshal_get_ldfld_wrapper (MonoType
*type
)
1344 MonoMethodSignature
*sig
;
1345 MonoMethodBuilder
*mb
;
1351 int t
, pos0
, pos1
= 0;
1352 static MonoMethod
* tp_load
= NULL
;
1354 type
= mono_type_get_underlying_type (type
);
1359 if (type
->type
== MONO_TYPE_SZARRAY
) {
1360 klass
= mono_defaults
.array_class
;
1361 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
1362 klass
= type
->data
.klass
;
1363 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
1364 klass
= mono_defaults
.object_class
;
1365 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
1366 klass
= mono_defaults
.int_class
;
1367 } else if (t
== MONO_TYPE_GENERICINST
) {
1368 if (mono_type_generic_inst_is_valuetype (type
))
1369 klass
= mono_class_from_mono_type (type
);
1371 klass
= mono_defaults
.object_class
;
1373 klass
= mono_class_from_mono_type (type
);
1376 klass
= mono_defaults
.int_class
;
1379 cache
= get_cache (&klass
->image
->ldfld_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
1380 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
1383 #ifndef DISABLE_REMOTING
1385 tp_load
= mono_class_get_method_from_name (mono_defaults
.transparent_proxy_class
, "LoadRemoteFieldNew", -1);
1386 g_assert (tp_load
!= NULL
);
1390 /* we add the %p pointer value of klass because class names are not unique */
1391 name
= g_strdup_printf ("__ldfld_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
1392 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLD
);
1395 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
1396 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
1397 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
1398 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
1399 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
1400 sig
->ret
= &klass
->byval_arg
;
1403 mono_mb_emit_ldarg (mb
, 0);
1404 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
1406 #ifndef DISABLE_REMOTING
1407 mono_mb_emit_ldarg (mb
, 0);
1408 mono_mb_emit_ldarg (mb
, 1);
1409 mono_mb_emit_ldarg (mb
, 2);
1411 mono_mb_emit_managed_call (mb
, tp_load
, NULL
);
1414 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
1415 csig->params [0] = &mono_defaults.object_class->byval_arg;
1416 csig->params [1] = &mono_defaults.int_class->byval_arg;
1417 csig->params [2] = &mono_defaults.int_class->byval_arg;
1418 csig->ret = &klass->this_arg;
1421 mono_mb_emit_native_call (mb, csig, mono_load_remote_field_new);
1422 mono_marshal_emit_thread_interrupt_checkpoint (mb);
1425 if (klass
->valuetype
) {
1426 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
1427 pos1
= mono_mb_emit_branch (mb
, CEE_BR
);
1429 mono_mb_emit_byte (mb
, CEE_RET
);
1433 mono_mb_patch_branch (mb
, pos0
);
1435 mono_mb_emit_ldarg (mb
, 0);
1436 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1437 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
1438 mono_mb_emit_ldarg (mb
, 3);
1439 mono_mb_emit_byte (mb
, CEE_ADD
);
1441 if (klass
->valuetype
)
1442 mono_mb_patch_branch (mb
, pos1
);
1447 case MONO_TYPE_BOOLEAN
:
1448 case MONO_TYPE_CHAR
:
1457 case MONO_TYPE_ARRAY
:
1458 case MONO_TYPE_SZARRAY
:
1459 case MONO_TYPE_OBJECT
:
1460 case MONO_TYPE_CLASS
:
1461 case MONO_TYPE_STRING
:
1465 case MONO_TYPE_FNPTR
:
1466 mono_mb_emit_byte (mb
, mono_type_to_ldind (type
));
1468 case MONO_TYPE_VALUETYPE
:
1469 g_assert (!klass
->enumtype
);
1470 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
1472 case MONO_TYPE_GENERICINST
:
1473 if (mono_type_generic_inst_is_valuetype (type
)) {
1474 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
1476 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1480 case MONO_TYPE_MVAR
:
1481 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
1484 g_warning ("type %x not implemented", type
->type
);
1485 g_assert_not_reached ();
1488 mono_mb_emit_byte (mb
, CEE_RET
);
1489 #endif /* DISABLE_JIT */
1491 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1492 info
->d
.proxy
.klass
= klass
;
1493 res
= mono_mb_create_and_cache_full (cache
, klass
,
1494 mb
, sig
, sig
->param_count
+ 16, info
, NULL
);
1501 * mono_marshal_get_ldflda_wrapper:
1502 * @type: the type of the field
1504 * This method generates a function which can be used to load a field address
1505 * from an object. The generated function has the following signature:
1506 * gpointer ldflda_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset);
1509 mono_marshal_get_ldflda_wrapper (MonoType
*type
)
1511 MonoMethodSignature
*sig
;
1512 MonoMethodBuilder
*mb
;
1518 int t
, pos0
, pos1
, pos2
, pos3
;
1520 type
= mono_type_get_underlying_type (type
);
1524 if (type
->type
== MONO_TYPE_SZARRAY
) {
1525 klass
= mono_defaults
.array_class
;
1526 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
1527 klass
= type
->data
.klass
;
1528 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
1529 klass
= mono_defaults
.object_class
;
1530 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
1531 klass
= mono_defaults
.int_class
;
1532 } else if (t
== MONO_TYPE_GENERICINST
) {
1533 if (mono_type_generic_inst_is_valuetype (type
))
1534 klass
= mono_class_from_mono_type (type
);
1536 klass
= mono_defaults
.object_class
;
1538 klass
= mono_class_from_mono_type (type
);
1541 klass
= mono_defaults
.int_class
;
1544 cache
= get_cache (&klass
->image
->ldflda_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
1545 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
1548 /* we add the %p pointer value of klass because class names are not unique */
1549 name
= g_strdup_printf ("__ldflda_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
1550 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLDA
);
1553 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
1554 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
1555 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
1556 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
1557 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
1558 sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
1561 /* if typeof (this) != transparent_proxy goto pos0 */
1562 mono_mb_emit_ldarg (mb
, 0);
1563 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
1565 /* if same_appdomain goto pos1 */
1566 mono_mb_emit_ldarg (mb
, 0);
1567 pos1
= mono_mb_emit_xdomain_check (mb
, CEE_BEQ
);
1569 mono_mb_emit_exception_full (mb
, "System", "InvalidOperationException", "Attempt to load field address from object in another appdomain.");
1571 /* same app domain */
1572 mono_mb_patch_branch (mb
, pos1
);
1574 /* if typeof (this) != contextbound goto pos2 */
1575 mono_mb_emit_ldarg (mb
, 0);
1576 pos2
= mono_mb_emit_contextbound_check (mb
, CEE_BEQ
);
1578 /* if this->rp->context == mono_context_get goto pos3 */
1579 mono_mb_emit_ldarg (mb
, 0);
1580 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1581 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1582 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRealProxy
, context
));
1583 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1584 mono_mb_emit_icall (mb
, mono_context_get
);
1585 pos3
= mono_mb_emit_branch (mb
, CEE_BEQ
);
1587 mono_mb_emit_exception_full (mb
, "System", "InvalidOperationException", "Attempt to load field address from object in another context.");
1589 mono_mb_patch_branch (mb
, pos2
);
1590 mono_mb_patch_branch (mb
, pos3
);
1592 /* return the address of the field from this->rp->unwrapped_server */
1593 mono_mb_emit_ldarg (mb
, 0);
1594 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1595 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1596 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRealProxy
, unwrapped_server
));
1597 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1598 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1599 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
1600 mono_mb_emit_ldarg (mb
, 3);
1601 mono_mb_emit_byte (mb
, CEE_ADD
);
1602 mono_mb_emit_byte (mb
, CEE_RET
);
1604 /* not a proxy: return the address of the field directly */
1605 mono_mb_patch_branch (mb
, pos0
);
1607 mono_mb_emit_ldarg (mb
, 0);
1608 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1609 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
1610 mono_mb_emit_ldarg (mb
, 3);
1611 mono_mb_emit_byte (mb
, CEE_ADD
);
1613 mono_mb_emit_byte (mb
, CEE_RET
);
1616 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1617 info
->d
.proxy
.klass
= klass
;
1618 res
= mono_mb_create_and_cache_full (cache
, klass
,
1619 mb
, sig
, sig
->param_count
+ 16,
1628 * mono_marshal_get_stfld_wrapper:
1629 * @type: the type of the field
1631 * This method generates a function which can be use to store a field with type
1632 * @type. The generated function has the following signature:
1633 * void stfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset, <@type> val)
1636 mono_marshal_get_stfld_wrapper (MonoType
*type
)
1638 MonoMethodSignature
*sig
;
1639 MonoMethodBuilder
*mb
;
1646 static MonoMethod
*tp_store
= NULL
;
1648 type
= mono_type_get_underlying_type (type
);
1652 if (type
->type
== MONO_TYPE_SZARRAY
) {
1653 klass
= mono_defaults
.array_class
;
1654 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
1655 klass
= type
->data
.klass
;
1656 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
1657 klass
= mono_defaults
.object_class
;
1658 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
1659 klass
= mono_defaults
.int_class
;
1660 } else if (t
== MONO_TYPE_GENERICINST
) {
1661 if (mono_type_generic_inst_is_valuetype (type
))
1662 klass
= mono_class_from_mono_type (type
);
1664 klass
= mono_defaults
.object_class
;
1666 klass
= mono_class_from_mono_type (type
);
1669 klass
= mono_defaults
.int_class
;
1672 cache
= get_cache (&klass
->image
->stfld_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
1673 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
1676 #ifndef DISABLE_REMOTING
1678 tp_store
= mono_class_get_method_from_name (mono_defaults
.transparent_proxy_class
, "StoreRemoteField", -1);
1679 g_assert (tp_store
!= NULL
);
1683 /* we add the %p pointer value of klass because class names are not unique */
1684 name
= g_strdup_printf ("__stfld_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
1685 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_STFLD
);
1688 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 5);
1689 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
1690 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
1691 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
1692 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
1693 sig
->params
[4] = &klass
->byval_arg
;
1694 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
1697 mono_mb_emit_ldarg (mb
, 0);
1698 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
1700 #ifndef DISABLE_REMOTING
1701 mono_mb_emit_ldarg (mb
, 0);
1702 mono_mb_emit_ldarg (mb
, 1);
1703 mono_mb_emit_ldarg (mb
, 2);
1704 mono_mb_emit_ldarg (mb
, 4);
1705 if (klass
->valuetype
)
1706 mono_mb_emit_op (mb
, CEE_BOX
, klass
);
1708 mono_mb_emit_managed_call (mb
, tp_store
, NULL
);
1710 mono_mb_emit_byte (mb
, CEE_RET
);
1713 mono_mb_patch_branch (mb
, pos
);
1715 mono_mb_emit_ldarg (mb
, 0);
1716 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1717 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
1718 mono_mb_emit_ldarg (mb
, 3);
1719 mono_mb_emit_byte (mb
, CEE_ADD
);
1720 mono_mb_emit_ldarg (mb
, 4);
1725 case MONO_TYPE_BOOLEAN
:
1726 case MONO_TYPE_CHAR
:
1735 case MONO_TYPE_ARRAY
:
1736 case MONO_TYPE_SZARRAY
:
1737 case MONO_TYPE_OBJECT
:
1738 case MONO_TYPE_CLASS
:
1739 case MONO_TYPE_STRING
:
1743 case MONO_TYPE_FNPTR
:
1744 mono_mb_emit_byte (mb
, mono_type_to_stind (type
));
1746 case MONO_TYPE_VALUETYPE
:
1747 g_assert (!klass
->enumtype
);
1748 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
1750 case MONO_TYPE_GENERICINST
:
1752 case MONO_TYPE_MVAR
:
1753 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
1756 g_warning ("type %x not implemented", type
->type
);
1757 g_assert_not_reached ();
1760 mono_mb_emit_byte (mb
, CEE_RET
);
1763 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1764 info
->d
.proxy
.klass
= klass
;
1765 res
= mono_mb_create_and_cache_full (cache
, klass
,
1766 mb
, sig
, sig
->param_count
+ 16,
1774 mono_marshal_get_proxy_cancast (MonoClass
*klass
)
1776 static MonoMethodSignature
*isint_sig
= NULL
;
1780 int pos_failed
, pos_end
;
1781 char *name
, *klass_name
;
1782 MonoMethod
*can_cast_to
;
1783 MonoMethodDesc
*desc
;
1784 MonoMethodBuilder
*mb
;
1786 cache
= get_cache (&klass
->image
->proxy_isinst_cache
, mono_aligned_addr_hash
, NULL
);
1787 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
1791 isint_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
1792 isint_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
1793 isint_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
1794 isint_sig
->pinvoke
= 0;
1797 klass_name
= mono_type_full_name (&klass
->byval_arg
);
1798 name
= g_strdup_printf ("__proxy_isinst_wrapper_%s", klass_name
);
1799 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_PROXY_ISINST
);
1800 g_free (klass_name
);
1803 mb
->method
->save_lmf
= 1;
1806 /* get the real proxy from the transparent proxy*/
1807 mono_mb_emit_ldarg (mb
, 0);
1808 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1809 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1811 /* get the reflection type from the type handle */
1812 mono_mb_emit_ptr (mb
, &klass
->byval_arg
);
1813 mono_mb_emit_icall (mb
, type_from_handle
);
1815 mono_mb_emit_ldarg (mb
, 0);
1817 /* make the call to CanCastTo (type, ob) */
1818 desc
= mono_method_desc_new ("IRemotingTypeInfo:CanCastTo", FALSE
);
1819 can_cast_to
= mono_method_desc_search_in_class (desc
, mono_defaults
.iremotingtypeinfo_class
);
1820 g_assert (can_cast_to
);
1821 mono_method_desc_free (desc
);
1822 mono_mb_emit_op (mb
, CEE_CALLVIRT
, can_cast_to
);
1824 pos_failed
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
1826 /* Upgrade the proxy vtable by calling: mono_upgrade_remote_class_wrapper (type, ob)*/
1827 mono_mb_emit_ptr (mb
, &klass
->byval_arg
);
1828 mono_mb_emit_icall (mb
, type_from_handle
);
1829 mono_mb_emit_ldarg (mb
, 0);
1831 mono_mb_emit_icall (mb
, mono_upgrade_remote_class_wrapper
);
1832 mono_marshal_emit_thread_interrupt_checkpoint (mb
);
1834 mono_mb_emit_ldarg (mb
, 0);
1835 pos_end
= mono_mb_emit_branch (mb
, CEE_BR
);
1839 mono_mb_patch_branch (mb
, pos_failed
);
1840 mono_mb_emit_byte (mb
, CEE_LDNULL
);
1844 mono_mb_patch_branch (mb
, pos_end
);
1845 mono_mb_emit_byte (mb
, CEE_RET
);
1848 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1849 info
->d
.proxy
.klass
= klass
;
1850 res
= mono_mb_create_and_cache_full (cache
, klass
, mb
, isint_sig
, isint_sig
->param_count
+ 16, info
, NULL
);
1857 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype_raw
, MonoTransparentProxy
*tproxy_raw
)
1860 MONO_HANDLE_DCL (MonoReflectionType
, rtype
);
1861 MONO_HANDLE_DCL (MonoTransparentProxy
, tproxy
);
1862 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (tproxy
);
1863 MonoClass
*klass
= mono_class_from_mono_type (MONO_HANDLE_GETVAL (rtype
, type
));
1864 mono_upgrade_remote_class (domain
, MONO_HANDLE_CAST (MonoObject
, tproxy
), klass
, &error
);
1868 #else /* DISABLE_REMOTING */
1871 mono_remoting_init (void)
1875 #endif /* DISABLE_REMOTING */
1877 /* mono_get_xdomain_marshal_type()
1878 * Returns the kind of marshalling that a type needs for cross domain calls.
1880 static MonoXDomainMarshalType
1881 mono_get_xdomain_marshal_type (MonoType
*t
)
1884 case MONO_TYPE_VOID
:
1885 g_assert_not_reached ();
1889 case MONO_TYPE_BOOLEAN
:
1892 case MONO_TYPE_CHAR
:
1899 return MONO_MARSHAL_NONE
;
1900 case MONO_TYPE_STRING
:
1901 return MONO_MARSHAL_COPY
;
1902 case MONO_TYPE_ARRAY
:
1903 case MONO_TYPE_SZARRAY
: {
1904 MonoClass
*elem_class
= mono_class_from_mono_type (t
)->element_class
;
1905 if (mono_get_xdomain_marshal_type (&(elem_class
->byval_arg
)) != MONO_MARSHAL_SERIALIZE
)
1906 return MONO_MARSHAL_COPY
;
1912 return MONO_MARSHAL_SERIALIZE
;
1915 /* Replace the given array element by a copy in the current domain */
1917 xdomain_copy_array_element_inplace (MonoArrayHandle arr
, int i
, MonoError
*error
)
1919 HANDLE_FUNCTION_ENTER ();
1920 mono_error_init (error
);
1921 MonoObjectHandle item
= MONO_HANDLE_NEW (MonoObject
, NULL
);
1922 MONO_HANDLE_ARRAY_GETREF (item
, arr
, i
);
1924 MonoObjectHandle item_copy
= mono_marshal_xdomain_copy_value_handle (item
, error
);
1927 MONO_HANDLE_ARRAY_SETREF (arr
, i
, item_copy
);
1929 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
1933 * mono_marshal_xdomain_copy_value_handle:
1934 * @val: The value to copy.
1935 * @error: set on failure.
1937 * Makes a copy of @val suitable for the current domain.
1938 * On failure returns NULL and sets @error.
1941 mono_marshal_xdomain_copy_value_handle (MonoObjectHandle val
, MonoError
*error
)
1943 mono_error_init (error
);
1944 MonoObjectHandle result
= MONO_HANDLE_NEW (MonoObject
, NULL
);
1945 if (MONO_HANDLE_IS_NULL (val
))
1948 MonoDomain
*domain
= mono_domain_get ();
1950 MonoClass
*klass
= mono_handle_class (val
);
1952 switch (klass
->byval_arg
.type
) {
1953 case MONO_TYPE_VOID
:
1954 g_assert_not_reached ();
1958 case MONO_TYPE_BOOLEAN
:
1961 case MONO_TYPE_CHAR
:
1967 case MONO_TYPE_R8
: {
1968 uint32_t gchandle
= mono_gchandle_from_handle (val
, TRUE
);
1969 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 */
1970 mono_gchandle_free (gchandle
);
1973 MONO_HANDLE_ASSIGN (result
, res
);
1976 case MONO_TYPE_STRING
: {
1977 MonoStringHandle str
= MONO_HANDLE_CAST (MonoString
, val
);
1978 uint32_t gchandle
= mono_gchandle_from_handle (val
, TRUE
);
1979 MonoStringHandle res
= mono_string_new_utf16_handle (domain
, mono_string_chars (MONO_HANDLE_RAW (str
)), mono_string_handle_length (str
), error
);
1980 mono_gchandle_free (gchandle
);
1983 MONO_HANDLE_ASSIGN (result
, res
);
1986 case MONO_TYPE_ARRAY
:
1987 case MONO_TYPE_SZARRAY
: {
1988 MonoArrayHandle arr
= MONO_HANDLE_CAST (MonoArray
, val
);
1989 MonoXDomainMarshalType mt
= mono_get_xdomain_marshal_type (&klass
->element_class
->byval_arg
);
1990 if (mt
== MONO_MARSHAL_SERIALIZE
)
1992 MonoArrayHandle acopy
= mono_array_clone_in_domain (domain
, arr
, error
);
1996 if (mt
== MONO_MARSHAL_COPY
) {
1997 int i
, len
= mono_array_handle_length (acopy
);
1998 for (i
= 0; i
< len
; i
++) {
1999 if (!xdomain_copy_array_element_inplace (acopy
, i
, error
))
2003 MONO_HANDLE_ASSIGN (result
, acopy
);
2014 /* mono_marshal_xdomain_copy_value
2015 * Makes a copy of "val" suitable for the current domain.
2018 mono_marshal_xdomain_copy_value (MonoObject
* val_raw
, MonoError
*error
)
2020 HANDLE_FUNCTION_ENTER ();
2021 /* FIXME callers of mono_marshal_xdomain_copy_value should use handles */
2022 MONO_HANDLE_DCL (MonoObject
, val
);
2023 MonoObjectHandle result
= mono_marshal_xdomain_copy_value_handle (val
, error
);
2024 HANDLE_FUNCTION_RETURN_OBJ (result
);
2027 /* mono_marshal_xdomain_copy_value
2028 * Makes a copy of "val" suitable for the current domain.
2031 ves_icall_mono_marshal_xdomain_copy_value (MonoObject
*val
)
2034 MonoObject
*result
= mono_marshal_xdomain_copy_value (val
, &error
);
2035 mono_error_set_pending_exception (&error
);