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/method-builder-ilgen-internals.h"
16 #include "mono/metadata/remoting.h"
17 #include "mono/metadata/marshal.h"
18 #include "mono/metadata/marshal-internals.h"
19 #include "mono/metadata/abi-details.h"
20 #include "mono/metadata/class-init.h"
21 #include "mono/metadata/cominterop.h"
22 #include "mono/metadata/tabledefs.h"
23 #include "mono/metadata/exception.h"
24 #include "mono/metadata/debug-helpers.h"
25 #include "mono/metadata/domain-internals.h"
26 #include "mono/metadata/reflection-internals.h"
27 #include "mono/metadata/assembly.h"
28 #include "icall-decl.h"
29 #include "icall-signatures.h"
32 MONO_MARSHAL_NONE
, /* No marshalling needed */
33 MONO_MARSHAL_COPY
, /* Can be copied by value to the new domain */
34 MONO_MARSHAL_COPY_OUT
, /* out parameter that needs to be copied back to the original instance */
35 MONO_MARSHAL_SERIALIZE
/* Value needs to be serialized into the new domain */
36 } MonoXDomainMarshalType
;
38 #ifndef DISABLE_REMOTING
40 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
44 #include "mono/cil/opcode.def"
49 struct _MonoRemotingMethods
{
51 MonoMethod
*invoke_with_check
;
52 MonoMethod
*xdomain_invoke
;
53 MonoMethod
*xdomain_dispatch
;
56 typedef struct _MonoRemotingMethods MonoRemotingMethods
;
59 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
);
61 static MonoException
*
62 mono_remoting_update_exception (MonoException
*exc
);
65 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
);
68 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
);
71 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype
, MonoTransparentProxy
*tproxy
);
73 static MonoXDomainMarshalType
74 mono_get_xdomain_marshal_type (MonoType
*t
);
77 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
);
79 static MonoReflectionType
*
80 type_from_handle (MonoType
*handle
);
83 mono_context_set_icall (MonoAppContext
*new_context
);
85 static MonoAppContext
*
86 mono_context_get_icall (void);
89 mono_marshal_xdomain_copy_value (MonoObject
* val_raw
, MonoError
*error
);
91 /* Class lazy loading functions */
92 static GENERATE_GET_CLASS_WITH_CACHE (remoting_services
, "System.Runtime.Remoting", "RemotingServices")
93 static GENERATE_GET_CLASS_WITH_CACHE (call_context
, "System.Runtime.Remoting.Messaging", "CallContext")
94 static GENERATE_GET_CLASS_WITH_CACHE (context
, "System.Runtime.Remoting.Contexts", "Context")
96 static mono_mutex_t remoting_mutex
;
97 static gboolean remoting_mutex_inited
;
99 static MonoClass
*byte_array_class
;
101 static MonoMethod
*method_rs_serialize
, *method_rs_deserialize
, *method_exc_fixexc
, *method_rs_appdomain_target
;
102 static MonoMethod
*method_set_call_context
, *method_needs_context_sink
, *method_rs_serialize_exc
;
106 mono_compile_method_icall (MonoMethod
*method
);
108 // func is an identifier, that names a function, and is also in jit-icall-reg.h,
109 // and therefore a field in mono_jit_icall_info and can be token pasted into an enum value.
111 // The name of func must be linkable for AOT, for example g_free does not work (monoeg_g_free instead),
112 // nor does the C++ overload fmod (mono_fmod instead). These functions therefore
113 // must be extern "C".
115 // This is not the same as other register_icall (last parameter NULL vs. #func)
116 #define register_icall(func, sig, save) \
117 (mono_register_jit_icall_info (&mono_get_jit_icall_info ()->func, func, #func, (sig), (save), NULL))
122 g_assert (remoting_mutex_inited
);
123 mono_os_mutex_lock (&remoting_mutex
);
127 remoting_unlock (void)
129 g_assert (remoting_mutex_inited
);
130 mono_os_mutex_unlock (&remoting_mutex
);
134 * Return the hash table pointed to by VAR, lazily creating it if neccesary.
137 get_cache (GHashTable
**var
, GHashFunc hash_func
, GCompareFunc equal_func
)
143 g_hash_table_new (hash_func
, equal_func
);
144 mono_memory_barrier ();
153 get_cache_full (GHashTable
**var
, GHashFunc hash_func
, GCompareFunc equal_func
, GDestroyNotify key_destroy_func
, GDestroyNotify value_destroy_func
)
159 g_hash_table_new_full (hash_func
, equal_func
, key_destroy_func
, value_destroy_func
);
160 mono_memory_barrier ();
169 mono_remoting_init (void)
171 mono_os_mutex_init (&remoting_mutex
);
172 remoting_mutex_inited
= TRUE
;
176 mono_remoting_marshal_init (void)
181 static gboolean module_initialized
= FALSE
;
182 static gboolean icalls_registered
= FALSE
;
184 if (module_initialized
)
187 byte_array_class
= mono_class_create_array (mono_defaults
.byte_class
, 1);
190 klass
= mono_class_get_remoting_services_class ();
191 method_rs_serialize
= mono_class_get_method_from_name_checked (klass
, "SerializeCallData", -1, 0, error
);
192 mono_error_assert_ok (error
);
193 g_assert (method_rs_serialize
);
194 method_rs_deserialize
= mono_class_get_method_from_name_checked (klass
, "DeserializeCallData", -1, 0, error
);
195 mono_error_assert_ok (error
);
196 g_assert (method_rs_deserialize
);
197 method_rs_serialize_exc
= mono_class_get_method_from_name_checked (klass
, "SerializeExceptionData", -1, 0, error
);
198 mono_error_assert_ok (error
);
199 g_assert (method_rs_serialize_exc
);
201 klass
= mono_defaults
.real_proxy_class
;
202 method_rs_appdomain_target
= mono_class_get_method_from_name_checked (klass
, "GetAppDomainTarget", -1, 0, error
);
203 mono_error_assert_ok (error
);
204 g_assert (method_rs_appdomain_target
);
206 klass
= mono_defaults
.exception_class
;
207 method_exc_fixexc
= mono_class_get_method_from_name_checked (klass
, "FixRemotingException", -1, 0, error
);
208 mono_error_assert_ok (error
);
209 g_assert (method_exc_fixexc
);
211 klass
= mono_class_get_call_context_class ();
212 method_set_call_context
= mono_class_get_method_from_name_checked (klass
, "SetCurrentCallContext", -1, 0, error
);
213 mono_error_assert_ok (error
);
214 g_assert (method_set_call_context
);
216 klass
= mono_class_get_context_class ();
217 method_needs_context_sink
= mono_class_get_method_from_name_checked (klass
, "get_NeedsContextSink", -1, 0, error
);
218 mono_error_assert_ok (error
);
219 g_assert (method_needs_context_sink
);
224 if (!icalls_registered
) {
225 register_icall (type_from_handle
, mono_icall_sig_object_ptr
, FALSE
);
226 register_icall (mono_marshal_set_domain_by_id
, mono_icall_sig_int32_int32_int32
, FALSE
);
227 register_icall (mono_marshal_check_domain_image
, mono_icall_sig_int32_int32_ptr
, FALSE
);
228 register_icall (ves_icall_mono_marshal_xdomain_copy_value
, mono_icall_sig_object_object
, FALSE
);
229 register_icall (mono_marshal_xdomain_copy_out_value
, mono_icall_sig_void_object_object
, FALSE
);
230 register_icall (mono_remoting_wrapper
, mono_icall_sig_object_ptr_ptr
, FALSE
);
231 register_icall (mono_remoting_update_exception
, mono_icall_sig_object_object
, FALSE
);
232 register_icall (mono_upgrade_remote_class_wrapper
, mono_icall_sig_void_object_object
, FALSE
);
235 register_icall (mono_compile_method_icall
, mono_icall_sig_ptr_ptr
, FALSE
);
238 register_icall (mono_context_get_icall
, mono_icall_sig_object
, FALSE
);
239 register_icall (mono_context_set_icall
, mono_icall_sig_void_object
, FALSE
);
242 icalls_registered
= TRUE
;
244 mono_loader_unlock ();
246 module_initialized
= TRUE
;
249 // This is an icall, it will return NULL and set pending exception (in
250 // mono_type_from_handle wrapper) on failure.
251 static MonoReflectionType
*
252 type_from_handle (MonoType
*handle
)
254 return mono_type_from_handle (handle
);
259 mono_mb_emit_proxy_check (MonoMethodBuilder
*mb
, int branch_code
)
262 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoObject
, vtable
));
263 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
264 mono_mb_emit_icon (mb
, MONO_STRUCT_OFFSET (MonoVTable
, klass
));
265 mono_mb_emit_byte (mb
, CEE_ADD
);
266 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
267 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
268 mono_mb_emit_byte (mb
, CEE_MONO_CLASSCONST
);
269 mono_mb_emit_i4 (mb
, mono_mb_add_data (mb
, mono_defaults
.transparent_proxy_class
));
270 pos
= mono_mb_emit_branch (mb
, branch_code
);
275 mono_mb_emit_xdomain_check (MonoMethodBuilder
*mb
, int branch_code
)
278 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
279 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
280 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
281 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
282 mono_mb_emit_icon (mb
, -1);
283 pos
= mono_mb_emit_branch (mb
, branch_code
);
288 mono_mb_emit_contextbound_check (MonoMethodBuilder
*mb
, int branch_code
)
290 static int offset
= -1;
294 mono_class_contextbound_bit_offset (&offset
, &mask
);
296 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, remote_class
));
297 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
298 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRemoteClass
, proxy_class
));
299 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
300 mono_mb_emit_ldflda (mb
, offset
);
301 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
302 mono_mb_emit_icon (mb
, mask
);
303 mono_mb_emit_byte (mb
, CEE_AND
);
304 mono_mb_emit_icon (mb
, 0);
305 return mono_mb_emit_branch (mb
, branch_code
);
307 #endif /* !DISABLE_JIT */
310 mono_marshal_remoting_find_in_cache (MonoMethod
*method
, int wrapper_type
)
312 MonoMethod
*res
= NULL
;
313 MonoRemotingMethods
*wrps
= NULL
;
315 mono_marshal_lock_internal ();
316 if (mono_method_get_wrapper_cache (method
)->remoting_invoke_cache
)
317 wrps
= (MonoRemotingMethods
*)g_hash_table_lookup (mono_method_get_wrapper_cache (method
)->remoting_invoke_cache
, method
);
320 switch (wrapper_type
) {
321 case MONO_WRAPPER_REMOTING_INVOKE
: res
= wrps
->invoke
; break;
322 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= wrps
->invoke_with_check
; break;
323 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= wrps
->xdomain_invoke
; break;
324 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= wrps
->xdomain_dispatch
; break;
328 /* it is important to do the unlock after the load from wrps, since in
329 * mono_remoting_mb_create_and_cache () we drop the marshal lock to be able
330 * to take the loader lock and some other thread may set the fields.
332 mono_marshal_unlock_internal ();
336 /* Create the method from the builder and place it in the cache */
338 mono_remoting_mb_create_and_cache (MonoMethod
*key
, MonoMethodBuilder
*mb
,
339 MonoMethodSignature
*sig
, int max_stack
, WrapperInfo
*info
)
341 MonoMethod
**res
= NULL
;
342 MonoRemotingMethods
*wrps
;
345 cache
= get_cache_full (&mono_method_get_wrapper_cache (key
)->remoting_invoke_cache
, mono_aligned_addr_hash
, NULL
, NULL
, g_free
);
347 mono_marshal_lock_internal ();
348 wrps
= (MonoRemotingMethods
*)g_hash_table_lookup (cache
, key
);
350 wrps
= g_new0 (MonoRemotingMethods
, 1);
351 g_hash_table_insert (cache
, key
, wrps
);
354 switch (mb
->method
->wrapper_type
) {
355 case MONO_WRAPPER_REMOTING_INVOKE
: res
= &wrps
->invoke
; break;
356 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= &wrps
->invoke_with_check
; break;
357 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= &wrps
->xdomain_invoke
; break;
358 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= &wrps
->xdomain_dispatch
; break;
359 default: g_assert_not_reached (); break;
361 mono_marshal_unlock_internal ();
365 newm
= mono_mb_create_method (mb
, sig
, max_stack
);
367 mono_marshal_lock_internal ();
370 mono_marshal_set_wrapper_info (*res
, info
);
371 mono_marshal_unlock_internal ();
373 mono_marshal_unlock_internal ();
374 mono_free_method (newm
);
382 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
)
385 MonoMethodMessage
*msg
;
386 MonoTransparentProxy
*this_obj
;
387 MonoObject
*res
, *exc
;
390 this_obj
= *((MonoTransparentProxy
**)params
[0]);
393 g_assert (mono_object_is_transparent_proxy (this_obj
));
395 /* skip the this pointer */
398 if (mono_class_is_contextbound (this_obj
->remote_class
->proxy_class
) && this_obj
->rp
->context
== (MonoObject
*) mono_context_get ())
401 MonoMethodSignature
*sig
= mono_method_signature_internal (method
);
402 int count
= sig
->param_count
;
403 gpointer
* mparams
= g_newa (gpointer
, count
);
405 for (i
=0; i
<count
; i
++) {
406 MonoClass
*klass
= mono_class_from_mono_type_internal (sig
->params
[i
]);
407 if (m_class_is_valuetype (klass
)) {
408 if (sig
->params
[i
]->byref
) {
409 mparams
[i
] = *((gpointer
*)params
[i
]);
411 /* runtime_invoke expects a boxed instance */
412 if (mono_class_is_nullable (mono_class_from_mono_type_internal (sig
->params
[i
]))) {
413 mparams
[i
] = mono_nullable_box ((guint8
*)params
[i
], klass
, error
);
414 goto_if_nok (error
, fail
);
416 mparams
[i
] = params
[i
];
419 mparams
[i
] = *((gpointer
**)params
[i
]);
423 res
= mono_runtime_invoke_checked (method
, m_class_is_valuetype (method
->klass
)? mono_object_unbox_internal ((MonoObject
*)this_obj
): this_obj
, mparams
, error
);
424 goto_if_nok (error
, fail
);
429 msg
= mono_method_call_message_new (method
, params
, NULL
, NULL
, NULL
, error
);
430 goto_if_nok (error
, fail
);
432 res
= mono_remoting_invoke ((MonoObject
*)this_obj
->rp
, msg
, &exc
, &out_args
, error
);
433 goto_if_nok (error
, fail
);
437 exc
= (MonoObject
*) mono_remoting_update_exception ((MonoException
*)exc
);
438 mono_error_set_exception_instance (error
, (MonoException
*)exc
);
442 mono_method_return_message_restore (method
, params
, out_args
, error
);
443 goto_if_nok (error
, fail
);
447 mono_error_set_pending_exception (error
);
452 * Handles exception transformation at appdomain call boundary.
453 * Note this is called from target appdomain inside xdomain wrapper, but from
454 * source domain in the mono_remoting_wrapper slowpath.
456 static MonoException
*
457 mono_remoting_update_exception (MonoException
*exc
)
459 MonoInternalThread
*thread
;
460 MonoClass
*klass
= mono_object_class (exc
);
462 /* Serialization error can only happen when still in the target appdomain */
463 if (!(mono_class_get_flags (klass
) & TYPE_ATTRIBUTE_SERIALIZABLE
)) {
465 char *aname
= mono_stringify_assembly_name (&m_class_get_image (klass
)->assembly
->aname
);
466 char *message
= g_strdup_printf ("Type '%s' in Assembly '%s' is not marked as serializable", m_class_get_name (klass
), aname
);
467 ret
= mono_get_exception_serialization (message
);
473 thread
= mono_thread_internal_current ();
474 if (mono_object_class (exc
) == mono_defaults
.threadabortexception_class
&&
475 thread
->flags
& MONO_THREAD_FLAG_APPDOMAIN_ABORT
) {
476 mono_thread_internal_reset_abort (thread
);
477 return mono_get_exception_appdomain_unloaded ();
484 * mono_marshal_get_remoting_invoke:
487 mono_marshal_get_remoting_invoke (MonoMethod
*method
, MonoError
*error
)
489 MonoMethodSignature
*sig
;
490 MonoMethodBuilder
*mb
;
498 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
501 /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
503 if (mono_class_is_com_object (method
->klass
) || method
->klass
== mono_class_try_get_com_object_class ()) {
504 MonoVTable
*vtable
= mono_class_vtable_checked (mono_domain_get (), method
->klass
, error
);
505 return_val_if_nok (error
, NULL
);
507 if (!mono_vtable_is_remote (vtable
)) {
508 return mono_cominterop_get_invoke (method
);
513 sig
= mono_signature_no_pinvoke (method
);
515 /* we cant remote methods without this pointer */
519 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE
)))
522 mono_remoting_marshal_init ();
524 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE
);
527 mb
->method
->save_lmf
= 1;
529 params_var
= mono_mb_emit_save_args (mb
, sig
, TRUE
);
531 mono_mb_emit_ptr (mb
, method
);
532 mono_mb_emit_ldloc (mb
, params_var
);
533 mono_mb_emit_icall (mb
, mono_remoting_wrapper
);
534 // FIXME: this interrupt checkpoint code is a no-op since 'mb'
535 // is a MONO_WRAPPER_REMOTING_INVOKE, and
536 // mono_thread_interruption_checkpoint_request (FALSE)
537 // considers such wrappers "protected" and always returns
538 // NULL as if there's no pending interruption.
539 mono_marshal_emit_thread_interrupt_checkpoint (mb
);
541 if (sig
->ret
->type
== MONO_TYPE_VOID
) {
542 mono_mb_emit_byte (mb
, CEE_POP
);
543 mono_mb_emit_byte (mb
, CEE_RET
);
545 mono_mb_emit_restore_result (mb
, sig
->ret
);
549 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
550 info
->d
.remoting
.method
= method
;
551 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16, info
);
557 /* mono_marshal_xdomain_copy_out_value()
558 * Copies the contents of the src instance into the dst instance. src and dst
559 * must have the same type, and if they are arrays, the same size.
561 * This is an icall, it may use mono_error_set_pending_exception
564 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
)
567 if (src
== NULL
|| dst
== NULL
) return;
569 g_assert (mono_object_class (src
) == mono_object_class (dst
));
571 switch (m_class_get_byval_arg (mono_object_class (src
))->type
) {
572 case MONO_TYPE_ARRAY
:
573 case MONO_TYPE_SZARRAY
: {
574 int mt
= mono_get_xdomain_marshal_type (m_class_get_byval_arg (m_class_get_element_class (mono_object_class (src
))));
575 if (mt
== MONO_MARSHAL_SERIALIZE
) return;
576 if (mt
== MONO_MARSHAL_COPY
) {
577 int i
, len
= mono_array_length_internal ((MonoArray
*)dst
);
578 for (i
= 0; i
< len
; i
++) {
579 MonoObject
*item
= (MonoObject
*)mono_array_get_internal ((MonoArray
*)src
, gpointer
, i
);
580 MonoObject
*item_copy
= mono_marshal_xdomain_copy_value (item
, error
);
581 if (mono_error_set_pending_exception (error
))
583 mono_array_setref_internal ((MonoArray
*)dst
, i
, item_copy
);
586 mono_array_full_copy ((MonoArray
*)src
, (MonoArray
*)dst
);
595 #if !defined (DISABLE_JIT)
597 mono_marshal_emit_xdomain_copy_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
599 mono_mb_emit_icall (mb
, ves_icall_mono_marshal_xdomain_copy_value
);
600 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
604 mono_marshal_emit_xdomain_copy_out_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
606 mono_mb_emit_icall (mb
, mono_marshal_xdomain_copy_out_value
);
610 /* mono_marshal_supports_fast_xdomain()
611 * Returns TRUE if the method can use the fast xdomain wrapper.
614 mono_marshal_supports_fast_xdomain (MonoMethod
*method
)
616 return !mono_class_is_contextbound (method
->klass
) &&
617 !((method
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) && (strcmp (".ctor", method
->name
) == 0));
621 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
)
623 MonoDomain
*current_domain
= mono_domain_get ();
624 MonoDomain
*domain
= mono_domain_get_by_id (id
);
626 if (!domain
|| !mono_domain_set_fast (domain
, FALSE
)) {
627 mono_set_pending_exception (mono_get_exception_appdomain_unloaded ());
632 mono_thread_push_appdomain_ref (domain
);
634 mono_thread_pop_appdomain_ref ();
636 return current_domain
->domain_id
;
639 #if !defined (DISABLE_JIT)
641 mono_marshal_emit_switch_domain (MonoMethodBuilder
*mb
)
643 mono_mb_emit_icall (mb
, mono_marshal_set_domain_by_id
);
647 mono_compile_method_icall (MonoMethod
*method
)
650 gpointer result
= mono_compile_method_checked (method
, error
);
651 mono_error_set_pending_exception (error
);
655 /* mono_marshal_emit_load_domain_method ()
656 * Loads into the stack a pointer to the code of the provided method for
657 * the current domain.
660 mono_marshal_emit_load_domain_method (MonoMethodBuilder
*mb
, MonoMethod
*method
)
662 /* We need a pointer to the method for the running domain (not the domain
663 * that compiles the method).
665 mono_mb_emit_ptr (mb
, method
);
666 mono_mb_emit_icall (mb
, mono_compile_method_icall
);
670 /* mono_marshal_check_domain_image ()
671 * Returns TRUE if the image is loaded in the specified
672 * application domain.
675 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
)
680 MonoDomain
*domain
= mono_domain_get_by_id (domain_id
);
684 mono_domain_assemblies_lock (domain
);
685 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
686 ass
= (MonoAssembly
*)tmp
->data
;
687 if (ass
->image
== image
)
690 mono_domain_assemblies_unlock (domain
);
695 /* mono_marshal_get_xappdomain_dispatch ()
696 * Generates a method that dispatches a method call from another domain into
697 * the current domain.
700 mono_marshal_get_xappdomain_dispatch (MonoMethod
*method
, int *marshal_types
, int complex_count
, int complex_out_count
, int ret_marshal_type
)
702 MonoMethodSignature
*sig
, *csig
;
703 MonoMethodBuilder
*mb
;
705 int i
, j
, param_index
, copy_locals_base
;
706 MonoClass
*ret_class
= NULL
;
707 int loc_array
=0, loc_return
=0, loc_serialized_exc
=0;
708 MonoExceptionClause
*main_clause
;
710 gboolean copy_return
;
713 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_DISPATCH
)))
716 sig
= mono_method_signature_internal (method
);
717 copy_return
= (sig
->ret
->type
!= MONO_TYPE_VOID
&& ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
);
720 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3 + sig
->param_count
- complex_count
);
721 csig
->params
[j
++] = mono_get_object_type ();
722 csig
->params
[j
++] = m_class_get_this_arg (byte_array_class
);
723 csig
->params
[j
++] = m_class_get_this_arg (byte_array_class
);
724 for (i
= 0; i
< sig
->param_count
; i
++) {
725 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
)
726 csig
->params
[j
++] = sig
->params
[i
];
729 csig
->ret
= sig
->ret
;
731 csig
->ret
= mono_get_void_type ();
733 csig
->hasthis
= FALSE
;
735 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_DISPATCH
);
736 mb
->method
->save_lmf
= 1;
741 loc_serialized_exc
= mono_mb_add_local (mb
, m_class_get_byval_arg (byte_array_class
));
742 if (complex_count
> 0)
743 loc_array
= mono_mb_add_local (mb
, mono_get_object_type ());
744 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
745 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
746 ret_class
= mono_class_from_mono_type_internal (sig
->ret
);
751 main_clause
= (MonoExceptionClause
*)mono_image_alloc0 (m_class_get_image (method
->klass
), sizeof (MonoExceptionClause
));
752 main_clause
->try_offset
= mono_mb_get_label (mb
);
754 /* Clean the call context */
756 mono_mb_emit_byte (mb
, CEE_LDNULL
);
757 mono_mb_emit_managed_call (mb
, method_set_call_context
, NULL
);
758 mono_mb_emit_byte (mb
, CEE_POP
);
760 /* Deserialize call data */
762 mono_mb_emit_ldarg (mb
, 1);
763 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
764 mono_mb_emit_byte (mb
, CEE_DUP
);
765 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
767 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
768 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
770 if (complex_count
> 0)
771 mono_mb_emit_stloc (mb
, loc_array
);
773 mono_mb_emit_byte (mb
, CEE_POP
);
775 mono_mb_patch_short_branch (mb
, pos
);
777 /* Get the target object */
779 mono_mb_emit_ldarg (mb
, 0);
780 mono_mb_emit_managed_call (mb
, method_rs_appdomain_target
, NULL
);
782 /* Load the arguments */
784 copy_locals_base
= mb
->locals
;
785 param_index
= 3; // Index of the first non-serialized parameter of this wrapper
787 for (i
= 0; i
< sig
->param_count
; i
++) {
788 MonoType
*pt
= sig
->params
[i
];
789 MonoClass
*pclass
= mono_class_from_mono_type_internal (pt
);
790 switch (marshal_types
[i
]) {
791 case MONO_MARSHAL_SERIALIZE
: {
792 /* take the value from the serialized array */
793 mono_mb_emit_ldloc (mb
, loc_array
);
794 mono_mb_emit_icon (mb
, j
++);
796 if (m_class_is_valuetype (pclass
)) {
797 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
798 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
800 mono_mb_emit_op (mb
, CEE_LDELEMA
, pclass
);
803 if (m_class_is_valuetype (pclass
)) {
804 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
805 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
806 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
808 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
809 if (pclass
!= mono_defaults
.object_class
) {
810 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
816 case MONO_MARSHAL_COPY_OUT
: {
817 /* Keep a local copy of the value since we need to copy it back after the call */
818 int copy_local
= mono_mb_add_local (mb
, m_class_get_byval_arg (pclass
));
819 mono_mb_emit_ldarg (mb
, param_index
++);
820 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
821 mono_mb_emit_byte (mb
, CEE_DUP
);
822 mono_mb_emit_stloc (mb
, copy_local
);
825 case MONO_MARSHAL_COPY
: {
826 mono_mb_emit_ldarg (mb
, param_index
);
828 mono_mb_emit_byte (mb
, CEE_DUP
);
829 mono_mb_emit_byte (mb
, CEE_DUP
);
830 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
831 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
832 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
834 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
839 case MONO_MARSHAL_NONE
:
840 mono_mb_emit_ldarg (mb
, param_index
++);
845 /* Make the call to the real object */
847 mono_marshal_emit_thread_force_interrupt_checkpoint (mb
);
849 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
851 if (sig
->ret
->type
!= MONO_TYPE_VOID
)
852 mono_mb_emit_stloc (mb
, loc_return
);
854 /* copy back MONO_MARSHAL_COPY_OUT parameters */
858 for (i
= 0; i
< sig
->param_count
; i
++) {
859 if (marshal_types
[i
] == MONO_MARSHAL_SERIALIZE
) continue;
860 if (marshal_types
[i
] == MONO_MARSHAL_COPY_OUT
) {
861 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
862 mono_mb_emit_ldarg (mb
, param_index
);
863 mono_marshal_emit_xdomain_copy_out_value (mb
, mono_class_from_mono_type_internal (sig
->params
[i
]));
868 /* Serialize the return values */
870 if (complex_out_count
> 0) {
871 /* Reset parameters in the array that don't need to be serialized back */
873 for (i
= 0; i
< sig
->param_count
; i
++) {
874 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
875 if (!sig
->params
[i
]->byref
) {
876 mono_mb_emit_ldloc (mb
, loc_array
);
877 mono_mb_emit_icon (mb
, j
);
878 mono_mb_emit_byte (mb
, CEE_LDNULL
);
879 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
884 /* Add the return value to the array */
886 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
887 mono_mb_emit_ldloc (mb
, loc_array
);
888 mono_mb_emit_icon (mb
, complex_count
); /* The array has an additional slot to hold the ret value */
889 mono_mb_emit_ldloc (mb
, loc_return
);
891 g_assert (ret_class
); /*FIXME properly fail here*/
892 if (m_class_is_valuetype (ret_class
)) {
893 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
895 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
900 mono_mb_emit_ldarg (mb
, 1);
901 mono_mb_emit_ldloc (mb
, loc_array
);
902 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
903 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
904 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
905 mono_mb_emit_ldarg (mb
, 1);
906 mono_mb_emit_ldloc (mb
, loc_return
);
907 if (m_class_is_valuetype (ret_class
)) {
908 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
910 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
911 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
913 mono_mb_emit_ldarg (mb
, 1);
914 mono_mb_emit_byte (mb
, CEE_LDNULL
);
915 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
916 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
919 mono_mb_emit_ldarg (mb
, 2);
920 mono_mb_emit_byte (mb
, CEE_LDNULL
);
921 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
922 pos_leave
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
924 /* Main exception catch */
925 main_clause
->flags
= MONO_EXCEPTION_CLAUSE_NONE
;
926 main_clause
->try_len
= mono_mb_get_pos (mb
) - main_clause
->try_offset
;
927 main_clause
->data
.catch_class
= mono_defaults
.object_class
;
930 main_clause
->handler_offset
= mono_mb_get_label (mb
);
932 mono_mb_emit_icall (mb
, mono_remoting_update_exception
);
933 mono_mb_emit_op (mb
, CEE_CASTCLASS
, mono_defaults
.exception_class
);
934 mono_mb_emit_managed_call (mb
, method_rs_serialize_exc
, NULL
);
935 mono_mb_emit_stloc (mb
, loc_serialized_exc
);
936 mono_mb_emit_ldarg (mb
, 2);
937 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
938 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
939 mono_mb_emit_branch (mb
, CEE_LEAVE
);
941 main_clause
->handler_len
= mono_mb_get_pos (mb
) - main_clause
->handler_offset
;
943 mono_mb_patch_branch (mb
, pos_leave
);
946 mono_mb_emit_ldloc (mb
, loc_return
);
948 mono_mb_emit_byte (mb
, CEE_RET
);
950 mono_mb_set_clauses (mb
, 1, main_clause
);
953 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
954 info
->d
.remoting
.method
= method
;
955 res
= mono_remoting_mb_create_and_cache (method
, mb
, csig
, csig
->param_count
+ 16, info
);
962 * mono_marshal_get_xappdomain_invoke:
963 * Generates a fast remoting wrapper for cross app domain calls.
966 mono_marshal_get_xappdomain_invoke (MonoMethod
*method
, MonoError
*error
)
968 MonoMethodSignature
*sig
;
969 MonoMethodBuilder
*mb
;
971 int i
, j
, complex_count
, complex_out_count
, copy_locals_base
;
973 MonoClass
*ret_class
= NULL
;
974 MonoMethod
*xdomain_method
;
975 int ret_marshal_type
= MONO_MARSHAL_NONE
;
976 int loc_array
=0, loc_serialized_data
=-1, loc_real_proxy
;
977 int loc_old_domainid
, loc_domainid
, loc_return
=0, loc_serialized_exc
=0, loc_context
;
978 int pos
, pos_dispatch
, pos_noex
;
979 gboolean copy_return
= FALSE
;
985 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
988 /* we cant remote methods without this pointer */
989 if (!mono_method_signature_internal (method
)->hasthis
)
992 mono_remoting_marshal_init ();
994 if (!mono_marshal_supports_fast_xdomain (method
))
995 return mono_marshal_get_remoting_invoke (method
, error
);
997 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_INVOKE
)))
1000 sig
= mono_signature_no_pinvoke (method
);
1002 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_INVOKE
);
1003 mb
->method
->save_lmf
= 1;
1005 /* Count the number of parameters that need to be serialized */
1007 marshal_types
= g_newa (int, sig
->param_count
);
1008 complex_count
= complex_out_count
= 0;
1009 gboolean has_byreflike
= FALSE
;
1010 for (i
= 0; i
< sig
->param_count
; i
++) {
1011 MonoType
*ptype
= sig
->params
[i
];
1012 int mt
= mono_get_xdomain_marshal_type (ptype
);
1014 /* If the [Out] attribute is applied to a parameter that can be internally copied,
1015 * the copy will be made by reusing the original object instance
1017 if ((ptype
->attrs
& PARAM_ATTRIBUTE_OUT
) != 0 && mt
== MONO_MARSHAL_COPY
&& !ptype
->byref
)
1018 mt
= MONO_MARSHAL_COPY_OUT
;
1019 else if (mt
== MONO_MARSHAL_SERIALIZE
) {
1021 if (ptype
->byref
) complex_out_count
++;
1023 marshal_types
[i
] = mt
;
1024 /* Can't make a xdomain wrapper for a method with an IsByRefLike result */
1025 if (!ptype
->byref
&& m_class_is_byreflike (mono_class_from_mono_type_internal (ptype
))) {
1026 has_byreflike
= TRUE
;
1030 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
1031 ret_marshal_type
= mono_get_xdomain_marshal_type (sig
->ret
);
1032 ret_class
= mono_class_from_mono_type_internal (sig
->ret
);
1033 copy_return
= ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
;
1036 /* Can't make a xdomain wrapper for a method with an IsByRefLike result */
1037 if (!sig
->ret
->byref
&& m_class_is_byreflike (mono_class_from_mono_type_internal (sig
->ret
))) {
1038 has_byreflike
= TRUE
;
1044 if (has_byreflike
) {
1045 /* Make a wrapper that throws a NotImplementedException if it's ever called */
1046 mono_mb_emit_exception (mb
, "NotImplementedException", "Cross AppDomain calls with IsByRefLike parameter or return types are not implemented");
1047 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1048 info
->d
.remoting
.method
= method
;
1049 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16, info
);
1055 MonoType
*object_type
= mono_get_object_type ();
1056 MonoType
*byte_array_type
= m_class_get_byval_arg (byte_array_class
);
1057 MonoType
*int32_type
= mono_get_int32_type ();
1058 if (complex_count
> 0)
1059 loc_array
= mono_mb_add_local (mb
, object_type
);
1060 loc_serialized_data
= mono_mb_add_local (mb
, byte_array_type
);
1061 loc_real_proxy
= mono_mb_add_local (mb
, object_type
);
1063 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
1064 loc_old_domainid
= mono_mb_add_local (mb
, int32_type
);
1065 loc_domainid
= mono_mb_add_local (mb
, int32_type
);
1066 loc_serialized_exc
= mono_mb_add_local (mb
, byte_array_type
);
1067 loc_context
= mono_mb_add_local (mb
, object_type
);
1069 /* Save thread domain data */
1071 mono_mb_emit_icall (mb
, mono_context_get_icall
);
1072 mono_mb_emit_byte (mb
, CEE_DUP
);
1073 mono_mb_emit_stloc (mb
, loc_context
);
1075 /* If the thread is not running in the default context, it needs to go
1076 * through the whole remoting sink, since the context is going to change
1078 mono_mb_emit_managed_call (mb
, method_needs_context_sink
, NULL
);
1079 pos
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
1081 /* Another case in which the fast path can't be used: when the target domain
1082 * has a different image for the same assembly.
1085 /* Get the target domain id */
1087 mono_mb_emit_ldarg (mb
, 0);
1088 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1089 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1090 mono_mb_emit_byte (mb
, CEE_DUP
);
1091 mono_mb_emit_stloc (mb
, loc_real_proxy
);
1093 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
1094 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
1095 mono_mb_emit_stloc (mb
, loc_domainid
);
1097 /* Check if the target domain has the same image for the required assembly */
1099 mono_mb_emit_ldloc (mb
, loc_domainid
);
1100 mono_mb_emit_ptr (mb
, m_class_get_image (method
->klass
));
1101 mono_mb_emit_icall (mb
, mono_marshal_check_domain_image
);
1102 pos_dispatch
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
1104 /* Use the whole remoting sink to dispatch this message */
1106 mono_mb_patch_short_branch (mb
, pos
);
1108 mono_mb_emit_ldarg (mb
, 0);
1109 for (i
= 0; i
< sig
->param_count
; i
++)
1110 mono_mb_emit_ldarg (mb
, i
+ 1);
1112 MonoMethod
* remoting_invoke_method
= mono_marshal_get_remoting_invoke (method
, error
);
1113 if (!is_ok (error
)) {
1117 mono_mb_emit_managed_call (mb
, remoting_invoke_method
, NULL
);
1118 mono_mb_emit_byte (mb
, CEE_RET
);
1119 mono_mb_patch_short_branch (mb
, pos_dispatch
);
1121 /* Create the array that will hold the parameters to be serialized */
1123 if (complex_count
> 0) {
1124 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 */
1125 mono_mb_emit_op (mb
, CEE_NEWARR
, mono_defaults
.object_class
);
1128 for (i
= 0; i
< sig
->param_count
; i
++) {
1130 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
1131 pclass
= mono_class_from_mono_type_internal (sig
->params
[i
]);
1132 mono_mb_emit_byte (mb
, CEE_DUP
);
1133 mono_mb_emit_icon (mb
, j
);
1134 mono_mb_emit_ldarg (mb
, i
+ 1); /* 0=this */
1135 if (sig
->params
[i
]->byref
) {
1136 if (m_class_is_valuetype (pclass
))
1137 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
1139 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1141 if (m_class_is_valuetype (pclass
))
1142 mono_mb_emit_op (mb
, CEE_BOX
, pclass
);
1143 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
1146 mono_mb_emit_stloc (mb
, loc_array
);
1148 /* Serialize parameters */
1150 mono_mb_emit_ldloc (mb
, loc_array
);
1151 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
1152 mono_mb_emit_stloc (mb
, loc_serialized_data
);
1154 mono_mb_emit_byte (mb
, CEE_LDNULL
);
1155 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
1156 mono_mb_emit_stloc (mb
, loc_serialized_data
);
1161 mono_mb_emit_ldloc (mb
, loc_domainid
);
1162 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
1163 mono_marshal_emit_switch_domain (mb
);
1164 mono_mb_emit_stloc (mb
, loc_old_domainid
);
1166 /* Load the arguments */
1168 mono_mb_emit_ldloc (mb
, loc_real_proxy
);
1169 mono_mb_emit_ldloc_addr (mb
, loc_serialized_data
);
1170 mono_mb_emit_ldloc_addr (mb
, loc_serialized_exc
);
1172 copy_locals_base
= mb
->locals
;
1173 for (i
= 0; i
< sig
->param_count
; i
++) {
1174 switch (marshal_types
[i
]) {
1175 case MONO_MARSHAL_SERIALIZE
:
1177 case MONO_MARSHAL_COPY
: {
1178 mono_mb_emit_ldarg (mb
, i
+1);
1179 if (sig
->params
[i
]->byref
) {
1180 /* make a local copy of the byref parameter. The real parameter
1181 * will be updated after the xdomain call
1183 MonoClass
*pclass
= mono_class_from_mono_type_internal (sig
->params
[i
]);
1184 int copy_local
= mono_mb_add_local (mb
, m_class_get_byval_arg (pclass
));
1185 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1186 mono_mb_emit_stloc (mb
, copy_local
);
1187 mono_mb_emit_ldloc_addr (mb
, copy_local
);
1191 case MONO_MARSHAL_COPY_OUT
:
1192 case MONO_MARSHAL_NONE
:
1193 mono_mb_emit_ldarg (mb
, i
+1);
1198 /* Make the call to the invoke wrapper in the target domain */
1200 xdomain_method
= mono_marshal_get_xappdomain_dispatch (method
, marshal_types
, complex_count
, complex_out_count
, ret_marshal_type
);
1201 mono_marshal_emit_load_domain_method (mb
, xdomain_method
);
1202 mono_mb_emit_calli (mb
, mono_method_signature_internal (xdomain_method
));
1205 mono_mb_emit_stloc (mb
, loc_return
);
1209 mono_mb_emit_ldloc (mb
, loc_old_domainid
);
1210 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1211 mono_marshal_emit_switch_domain (mb
);
1212 mono_mb_emit_byte (mb
, CEE_POP
);
1214 /* Restore thread domain data */
1216 mono_mb_emit_ldloc (mb
, loc_context
);
1217 mono_mb_emit_icall (mb
, mono_context_set_icall
);
1219 /* if (loc_serialized_exc != null) ... */
1221 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
1222 pos_noex
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
1224 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
1225 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
1226 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
1227 mono_mb_emit_op (mb
, CEE_CASTCLASS
, mono_defaults
.exception_class
);
1228 mono_mb_emit_managed_call (mb
, method_exc_fixexc
, NULL
);
1229 mono_mb_emit_byte (mb
, CEE_THROW
);
1230 mono_mb_patch_short_branch (mb
, pos_noex
);
1232 /* copy back non-serialized output parameters */
1235 for (i
= 0; i
< sig
->param_count
; i
++) {
1236 if (!sig
->params
[i
]->byref
|| marshal_types
[i
] != MONO_MARSHAL_COPY
) continue;
1237 mono_mb_emit_ldarg (mb
, i
+ 1);
1238 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
1239 mono_marshal_emit_xdomain_copy_value (mb
, mono_class_from_mono_type_internal (sig
->params
[i
]));
1240 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1243 /* Deserialize out parameters */
1245 if (complex_out_count
> 0) {
1246 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
1247 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
1248 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
1249 mono_mb_emit_stloc (mb
, loc_array
);
1251 /* Copy back output parameters and return type */
1254 for (i
= 0; i
< sig
->param_count
; i
++) {
1255 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
1256 if (sig
->params
[i
]->byref
) {
1257 MonoClass
*pclass
= mono_class_from_mono_type_internal (sig
->params
[i
]);
1258 mono_mb_emit_ldarg (mb
, i
+ 1);
1259 mono_mb_emit_ldloc (mb
, loc_array
);
1260 mono_mb_emit_icon (mb
, j
);
1261 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
1262 if (m_class_is_valuetype (pclass
)) {
1263 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
1264 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
1265 mono_mb_emit_op (mb
, CEE_STOBJ
, pclass
);
1267 if (pclass
!= mono_defaults
.object_class
)
1268 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
1269 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1275 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
1276 mono_mb_emit_ldloc (mb
, loc_array
);
1277 mono_mb_emit_icon (mb
, complex_count
);
1278 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
1279 if (m_class_is_valuetype (ret_class
)) {
1280 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
1281 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
1284 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
1285 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
1286 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
1287 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
1288 if (m_class_is_valuetype (ret_class
)) {
1289 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
1290 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
1291 } else if (ret_class
!= mono_defaults
.object_class
) {
1292 mono_mb_emit_op (mb
, CEE_CASTCLASS
, ret_class
);
1295 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
1296 mono_mb_emit_byte (mb
, CEE_DUP
);
1297 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
1298 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
1300 mono_mb_patch_short_branch (mb
, pos
);
1301 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
1302 mono_mb_emit_byte (mb
, CEE_POP
);
1306 mono_mb_emit_ldloc (mb
, loc_return
);
1307 if (ret_marshal_type
== MONO_MARSHAL_COPY
)
1308 mono_marshal_emit_xdomain_copy_value (mb
, ret_class
);
1311 mono_mb_emit_byte (mb
, CEE_RET
);
1312 #endif /* DISABLE_JIT */
1314 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1315 info
->d
.remoting
.method
= method
;
1316 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16, info
);
1323 * mono_marshal_get_remoting_invoke_for_target:
1326 mono_marshal_get_remoting_invoke_for_target (MonoMethod
*method
, MonoRemotingTarget target_type
, MonoError
*error
)
1329 if (target_type
== MONO_REMOTING_TARGET_APPDOMAIN
) {
1330 return mono_marshal_get_xappdomain_invoke (method
, error
);
1331 } else if (target_type
== MONO_REMOTING_TARGET_COMINTEROP
) {
1333 return mono_cominterop_get_invoke (method
);
1335 g_assert_not_reached ();
1338 return mono_marshal_get_remoting_invoke (method
, error
);
1344 G_GNUC_UNUSED
static gpointer
1345 mono_marshal_load_remoting_wrapper (MonoRealProxy
*rp
, MonoMethod
*method
)
1348 MonoMethod
*marshal_method
= NULL
;
1349 if (rp
->target_domain_id
!= -1)
1350 marshal_method
= mono_marshal_get_xappdomain_invoke (method
, error
);
1352 marshal_method
= mono_marshal_get_remoting_invoke (method
, error
);
1353 mono_error_assert_ok (error
);
1354 gpointer compiled_ptr
= mono_compile_method_checked (marshal_method
, error
);
1355 mono_error_assert_ok (error
);
1356 return compiled_ptr
;
1360 * mono_marshal_get_remoting_invoke_with_check:
1363 mono_marshal_get_remoting_invoke_with_check (MonoMethod
*method
, MonoError
*error
)
1365 MonoMethodSignature
*sig
;
1366 MonoMethodBuilder
*mb
;
1367 MonoMethod
*res
, *native
;
1369 int i
, pos
, pos_rem
;
1374 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
1377 /* we cant remote methods without this pointer */
1378 g_assert (mono_method_signature_internal (method
)->hasthis
);
1380 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)))
1383 sig
= mono_signature_no_pinvoke (method
);
1385 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
);
1388 for (i
= 0; i
<= sig
->param_count
; i
++)
1389 mono_mb_emit_ldarg (mb
, i
);
1391 mono_mb_emit_ldarg (mb
, 0);
1392 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
1394 if (mono_marshal_supports_fast_xdomain (method
)) {
1395 mono_mb_emit_ldarg (mb
, 0);
1396 pos_rem
= mono_mb_emit_xdomain_check (mb
, CEE_BEQ
);
1398 /* wrapper for cross app domain calls */
1399 native
= mono_marshal_get_xappdomain_invoke (method
, error
);
1400 if (!is_ok (error
)) {
1404 mono_mb_emit_managed_call (mb
, native
, mono_method_signature_internal (native
));
1405 mono_mb_emit_byte (mb
, CEE_RET
);
1407 mono_mb_patch_branch (mb
, pos_rem
);
1409 /* wrapper for normal remote calls */
1410 native
= mono_marshal_get_remoting_invoke (method
, error
);
1411 if (!is_ok (error
)) {
1415 mono_mb_emit_managed_call (mb
, native
, mono_method_signature_internal (native
));
1416 mono_mb_emit_byte (mb
, CEE_RET
);
1419 mono_mb_patch_branch (mb
, pos
);
1420 mono_mb_emit_managed_call (mb
, method
, mono_method_signature_internal (method
));
1421 mono_mb_emit_byte (mb
, CEE_RET
);
1424 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1425 info
->d
.remoting
.method
= method
;
1426 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16, info
);
1433 * mono_marshal_get_ldfld_wrapper:
1434 * \param type the type of the field
1436 * This method generates a function which can be use to load a field with type
1437 * \p type from an object. The generated function has the following signature:
1439 * <code><i>type</i> ldfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset)</code>
1442 mono_marshal_get_ldfld_wrapper (MonoType
*type
)
1444 MonoMethodSignature
*sig
;
1445 MonoMethodBuilder
*mb
;
1451 int t
, pos0
, pos1
= 0;
1452 static MonoMethod
* tp_load
= NULL
;
1454 type
= mono_type_get_underlying_type (type
);
1459 if (type
->type
== MONO_TYPE_SZARRAY
) {
1460 klass
= mono_defaults
.array_class
;
1461 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
1462 klass
= type
->data
.klass
;
1463 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
1464 klass
= mono_defaults
.object_class
;
1465 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
1466 klass
= mono_defaults
.int_class
;
1467 } else if (t
== MONO_TYPE_GENERICINST
) {
1468 if (mono_type_generic_inst_is_valuetype (type
))
1469 klass
= mono_class_from_mono_type_internal (type
);
1471 klass
= mono_defaults
.object_class
;
1473 klass
= mono_class_from_mono_type_internal (type
);
1476 klass
= mono_defaults
.int_class
;
1479 cache
= get_cache (&m_class_get_image (klass
)->ldfld_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
1480 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
1483 #ifndef DISABLE_REMOTING
1486 tp_load
= mono_class_get_method_from_name_checked (mono_defaults
.transparent_proxy_class
, "LoadRemoteFieldNew", -1, 0, error
);
1487 mono_error_assert_ok (error
);
1488 g_assert (tp_load
!= NULL
);
1492 /* we add the %p pointer value of klass because class names are not unique */
1493 name
= g_strdup_printf ("__ldfld_wrapper_%p_%s.%s", klass
, m_class_get_name_space (klass
), m_class_get_name (klass
));
1494 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLD
);
1497 MonoType
*object_type
= mono_get_object_type ();
1498 MonoType
*int_type
= mono_get_int_type ();
1499 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
1500 sig
->params
[0] = object_type
;
1501 sig
->params
[1] = int_type
;
1502 sig
->params
[2] = int_type
;
1503 sig
->params
[3] = int_type
;
1504 sig
->ret
= m_class_get_byval_arg (klass
);
1507 mono_mb_emit_ldarg (mb
, 0);
1508 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
1510 #ifndef DISABLE_REMOTING
1511 mono_mb_emit_ldarg (mb
, 0);
1512 mono_mb_emit_ldarg (mb
, 1);
1513 mono_mb_emit_ldarg (mb
, 2);
1515 mono_mb_emit_managed_call (mb
, tp_load
, NULL
);
1518 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
1519 csig->params [0] = mono_get_object_type ();
1520 csig->params [1] = mono_get_int_type ();
1521 csig->params [2] = mono_get_int_type ();
1522 csig->ret = m_class_get_this_arg (klass);
1525 mono_mb_emit_native_call (mb, csig, mono_load_remote_field_new);
1526 mono_marshal_emit_thread_interrupt_checkpoint (mb);
1529 if (m_class_is_valuetype (klass
)) {
1530 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
1531 pos1
= mono_mb_emit_branch (mb
, CEE_BR
);
1533 mono_mb_emit_byte (mb
, CEE_RET
);
1537 mono_mb_patch_branch (mb
, pos0
);
1539 mono_mb_emit_ldarg (mb
, 0);
1540 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1541 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
1542 mono_mb_emit_ldarg (mb
, 3);
1543 mono_mb_emit_byte (mb
, CEE_ADD
);
1545 if (m_class_is_valuetype (klass
))
1546 mono_mb_patch_branch (mb
, pos1
);
1551 case MONO_TYPE_BOOLEAN
:
1552 case MONO_TYPE_CHAR
:
1561 case MONO_TYPE_ARRAY
:
1562 case MONO_TYPE_SZARRAY
:
1563 case MONO_TYPE_OBJECT
:
1564 case MONO_TYPE_CLASS
:
1565 case MONO_TYPE_STRING
:
1569 case MONO_TYPE_FNPTR
:
1570 mono_mb_emit_byte (mb
, mono_type_to_ldind (type
));
1572 case MONO_TYPE_VALUETYPE
:
1573 g_assert (!m_class_is_enumtype (klass
));
1574 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
1576 case MONO_TYPE_GENERICINST
:
1577 if (mono_type_generic_inst_is_valuetype (type
)) {
1578 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
1580 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1584 case MONO_TYPE_MVAR
:
1585 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
1588 g_warning ("type %x not implemented", type
->type
);
1589 g_assert_not_reached ();
1592 mono_mb_emit_byte (mb
, CEE_RET
);
1593 #endif /* DISABLE_JIT */
1595 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1596 info
->d
.proxy
.klass
= klass
;
1597 res
= mono_mb_create_and_cache_full (cache
, klass
,
1598 mb
, sig
, sig
->param_count
+ 16, info
, NULL
);
1605 * mono_marshal_get_ldflda_wrapper:
1606 * @type: the type of the field
1608 * This method generates a function which can be used to load a field address
1609 * from an object. The generated function has the following signature:
1610 * gpointer ldflda_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset);
1613 mono_marshal_get_ldflda_wrapper (MonoType
*type
)
1615 MonoMethodSignature
*sig
;
1616 MonoMethodBuilder
*mb
;
1622 int t
, pos0
, pos1
, pos2
, pos3
;
1624 type
= mono_type_get_underlying_type (type
);
1628 if (type
->type
== MONO_TYPE_SZARRAY
) {
1629 klass
= mono_defaults
.array_class
;
1630 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
1631 klass
= type
->data
.klass
;
1632 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
1633 klass
= mono_defaults
.object_class
;
1634 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
1635 klass
= mono_defaults
.int_class
;
1636 } else if (t
== MONO_TYPE_GENERICINST
) {
1637 if (mono_type_generic_inst_is_valuetype (type
))
1638 klass
= mono_class_from_mono_type_internal (type
);
1640 klass
= mono_defaults
.object_class
;
1642 klass
= mono_class_from_mono_type_internal (type
);
1645 klass
= mono_defaults
.int_class
;
1648 cache
= get_cache (&m_class_get_image (klass
)->ldflda_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
1649 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
1652 mono_remoting_marshal_init ();
1654 /* we add the %p pointer value of klass because class names are not unique */
1655 name
= g_strdup_printf ("__ldflda_wrapper_%p_%s.%s", klass
, m_class_get_name_space (klass
), m_class_get_name (klass
));
1656 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLDA
);
1659 MonoType
*object_type
= mono_get_object_type ();
1660 MonoType
*int_type
= mono_get_int_type ();
1661 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
1662 sig
->params
[0] = object_type
;
1663 sig
->params
[1] = int_type
;
1664 sig
->params
[2] = int_type
;
1665 sig
->params
[3] = int_type
;
1666 sig
->ret
= int_type
;
1669 /* if typeof (this) != transparent_proxy goto pos0 */
1670 mono_mb_emit_ldarg (mb
, 0);
1671 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
1673 /* if same_appdomain goto pos1 */
1674 mono_mb_emit_ldarg (mb
, 0);
1675 pos1
= mono_mb_emit_xdomain_check (mb
, CEE_BEQ
);
1677 mono_mb_emit_exception_full (mb
, "System", "InvalidOperationException", "Attempt to load field address from object in another appdomain.");
1679 /* same app domain */
1680 mono_mb_patch_branch (mb
, pos1
);
1682 /* if typeof (this) != contextbound goto pos2 */
1683 mono_mb_emit_ldarg (mb
, 0);
1684 pos2
= mono_mb_emit_contextbound_check (mb
, CEE_BEQ
);
1686 /* if this->rp->context == mono_context_get goto pos3 */
1687 mono_mb_emit_ldarg (mb
, 0);
1688 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1689 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1690 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRealProxy
, context
));
1691 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1692 mono_mb_emit_icall (mb
, mono_context_get_icall
);
1693 pos3
= mono_mb_emit_branch (mb
, CEE_BEQ
);
1695 mono_mb_emit_exception_full (mb
, "System", "InvalidOperationException", "Attempt to load field address from object in another context.");
1697 mono_mb_patch_branch (mb
, pos2
);
1698 mono_mb_patch_branch (mb
, pos3
);
1700 /* return the address of the field from this->rp->unwrapped_server */
1701 mono_mb_emit_ldarg (mb
, 0);
1702 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1703 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1704 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoRealProxy
, unwrapped_server
));
1705 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1706 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1707 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
1708 mono_mb_emit_ldarg (mb
, 3);
1709 mono_mb_emit_byte (mb
, CEE_ADD
);
1710 mono_mb_emit_byte (mb
, CEE_RET
);
1712 /* not a proxy: return the address of the field directly */
1713 mono_mb_patch_branch (mb
, pos0
);
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
);
1721 mono_mb_emit_byte (mb
, CEE_RET
);
1724 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1725 info
->d
.proxy
.klass
= klass
;
1726 res
= mono_mb_create_and_cache_full (cache
, klass
,
1727 mb
, sig
, sig
->param_count
+ 16,
1736 * mono_marshal_get_stfld_wrapper:
1737 * \param type the type of the field
1739 * This method generates a function which can be use to store a field with type
1740 * \p type. The generated function has the following signature:
1742 * <code>void stfld_wrapper (MonoObject *this_obj, MonoClass *klass, MonoClassField *field, int offset, <i>type</i> val)</code>
1745 mono_marshal_get_stfld_wrapper (MonoType
*type
)
1747 MonoMethodSignature
*sig
;
1748 MonoMethodBuilder
*mb
;
1755 static MonoMethod
*tp_store
= NULL
;
1757 type
= mono_type_get_underlying_type (type
);
1761 if (type
->type
== MONO_TYPE_SZARRAY
) {
1762 klass
= mono_defaults
.array_class
;
1763 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
1764 klass
= type
->data
.klass
;
1765 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
1766 klass
= mono_defaults
.object_class
;
1767 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
1768 klass
= mono_defaults
.int_class
;
1769 } else if (t
== MONO_TYPE_GENERICINST
) {
1770 if (mono_type_generic_inst_is_valuetype (type
))
1771 klass
= mono_class_from_mono_type_internal (type
);
1773 klass
= mono_defaults
.object_class
;
1775 klass
= mono_class_from_mono_type_internal (type
);
1778 klass
= mono_defaults
.int_class
;
1781 cache
= get_cache (&m_class_get_image (klass
)->stfld_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
1782 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
1785 #ifndef DISABLE_REMOTING
1788 tp_store
= mono_class_get_method_from_name_checked (mono_defaults
.transparent_proxy_class
, "StoreRemoteField", -1, 0, error
);
1789 mono_error_assert_ok (error
);
1790 g_assert (tp_store
!= NULL
);
1794 /* we add the %p pointer value of klass because class names are not unique */
1795 name
= g_strdup_printf ("__stfld_wrapper_%p_%s.%s", klass
, m_class_get_name_space (klass
), m_class_get_name (klass
));
1796 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_STFLD
);
1800 MonoType
*object_type
= mono_get_object_type ();
1801 MonoType
*int_type
= mono_get_int_type ();
1802 MonoType
*void_type
= mono_get_void_type ();
1803 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 5);
1804 sig
->params
[0] = object_type
;
1805 sig
->params
[1] = int_type
;
1806 sig
->params
[2] = int_type
;
1807 sig
->params
[3] = int_type
;
1808 sig
->params
[4] = m_class_get_byval_arg (klass
);
1809 sig
->ret
= void_type
;
1812 mono_mb_emit_ldarg (mb
, 0);
1813 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
1815 #ifndef DISABLE_REMOTING
1816 mono_mb_emit_ldarg (mb
, 0);
1817 mono_mb_emit_ldarg (mb
, 1);
1818 mono_mb_emit_ldarg (mb
, 2);
1819 mono_mb_emit_ldarg (mb
, 4);
1820 if (m_class_is_valuetype (klass
))
1821 mono_mb_emit_op (mb
, CEE_BOX
, klass
);
1823 mono_mb_emit_managed_call (mb
, tp_store
, NULL
);
1825 mono_mb_emit_byte (mb
, CEE_RET
);
1828 mono_mb_patch_branch (mb
, pos
);
1830 mono_mb_emit_ldarg (mb
, 0);
1831 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1832 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
1833 mono_mb_emit_ldarg (mb
, 3);
1834 mono_mb_emit_byte (mb
, CEE_ADD
);
1835 mono_mb_emit_ldarg (mb
, 4);
1840 case MONO_TYPE_BOOLEAN
:
1841 case MONO_TYPE_CHAR
:
1850 case MONO_TYPE_ARRAY
:
1851 case MONO_TYPE_SZARRAY
:
1852 case MONO_TYPE_OBJECT
:
1853 case MONO_TYPE_CLASS
:
1854 case MONO_TYPE_STRING
:
1858 case MONO_TYPE_FNPTR
:
1859 mono_mb_emit_byte (mb
, mono_type_to_stind (type
));
1861 case MONO_TYPE_VALUETYPE
:
1862 g_assert (!m_class_is_enumtype (klass
));
1863 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
1865 case MONO_TYPE_GENERICINST
:
1867 case MONO_TYPE_MVAR
:
1868 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
1871 g_warning ("type %x not implemented", type
->type
);
1872 g_assert_not_reached ();
1875 mono_mb_emit_byte (mb
, CEE_RET
);
1878 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1879 info
->d
.proxy
.klass
= klass
;
1880 res
= mono_mb_create_and_cache_full (cache
, klass
,
1881 mb
, sig
, sig
->param_count
+ 16,
1889 * mono_marshal_get_proxy_cancast:
1892 mono_marshal_get_proxy_cancast (MonoClass
*klass
)
1894 static MonoMethodSignature
*isint_sig
= NULL
;
1898 int pos_failed
, pos_end
;
1899 char *name
, *klass_name
;
1900 MonoMethod
*can_cast_to
;
1901 MonoMethodDesc
*desc
;
1902 MonoMethodBuilder
*mb
;
1904 cache
= get_cache (&m_class_get_image (klass
)->proxy_isinst_cache
, mono_aligned_addr_hash
, NULL
);
1905 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
1909 isint_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
1910 isint_sig
->params
[0] = mono_get_object_type ();
1911 isint_sig
->ret
= mono_get_object_type ();
1912 isint_sig
->pinvoke
= 0;
1915 klass_name
= mono_type_full_name (m_class_get_byval_arg (klass
));
1916 name
= g_strdup_printf ("__proxy_isinst_wrapper_%s", klass_name
);
1917 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_PROXY_ISINST
);
1918 g_free (klass_name
);
1921 mb
->method
->save_lmf
= 1;
1925 mono_remoting_marshal_init (); // register icalls
1927 /* get the real proxy from the transparent proxy*/
1928 mono_mb_emit_ldarg (mb
, 0);
1929 mono_mb_emit_ldflda (mb
, MONO_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1930 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1932 /* get the reflection type from the type handle */
1933 mono_mb_emit_ptr (mb
, m_class_get_byval_arg (klass
));
1934 mono_mb_emit_icall (mb
, type_from_handle
);
1936 mono_mb_emit_ldarg (mb
, 0);
1938 /* make the call to CanCastTo (type, ob) */
1939 desc
= mono_method_desc_new ("IRemotingTypeInfo:CanCastTo", FALSE
);
1940 can_cast_to
= mono_method_desc_search_in_class (desc
, mono_defaults
.iremotingtypeinfo_class
);
1941 g_assert (can_cast_to
);
1942 mono_method_desc_free (desc
);
1943 mono_mb_emit_op (mb
, CEE_CALLVIRT
, can_cast_to
);
1945 pos_failed
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
1947 /* Upgrade the proxy vtable by calling: mono_upgrade_remote_class_wrapper (type, ob)*/
1948 mono_mb_emit_ptr (mb
, m_class_get_byval_arg (klass
));
1949 mono_mb_emit_icall (mb
, type_from_handle
);
1950 mono_mb_emit_ldarg (mb
, 0);
1952 mono_mb_emit_icall (mb
, mono_upgrade_remote_class_wrapper
);
1953 mono_marshal_emit_thread_interrupt_checkpoint (mb
);
1955 mono_mb_emit_ldarg (mb
, 0);
1956 pos_end
= mono_mb_emit_branch (mb
, CEE_BR
);
1960 mono_mb_patch_branch (mb
, pos_failed
);
1961 mono_mb_emit_byte (mb
, CEE_LDNULL
);
1965 mono_mb_patch_branch (mb
, pos_end
);
1966 mono_mb_emit_byte (mb
, CEE_RET
);
1969 info
= mono_wrapper_info_create (mb
, WRAPPER_SUBTYPE_NONE
);
1970 info
->d
.proxy
.klass
= klass
;
1971 res
= mono_mb_create_and_cache_full (cache
, klass
, mb
, isint_sig
, isint_sig
->param_count
+ 16, info
, NULL
);
1978 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype_raw
, MonoTransparentProxy
*tproxy_raw
)
1981 MONO_HANDLE_DCL (MonoReflectionType
, rtype
);
1982 MONO_HANDLE_DCL (MonoTransparentProxy
, tproxy
);
1983 MonoDomain
*domain
= MONO_HANDLE_DOMAIN (tproxy
);
1984 MonoClass
*klass
= mono_class_from_mono_type_internal (MONO_HANDLE_GETVAL (rtype
, type
));
1985 mono_upgrade_remote_class (domain
, MONO_HANDLE_CAST (MonoObject
, tproxy
), klass
, error
);
1989 #else /* DISABLE_REMOTING */
1992 mono_remoting_init (void)
1996 #endif /* DISABLE_REMOTING */
1998 /* mono_get_xdomain_marshal_type()
1999 * Returns the kind of marshalling that a type needs for cross domain calls.
2001 static MonoXDomainMarshalType
2002 mono_get_xdomain_marshal_type (MonoType
*t
)
2005 case MONO_TYPE_VOID
:
2006 g_assert_not_reached ();
2010 case MONO_TYPE_BOOLEAN
:
2013 case MONO_TYPE_CHAR
:
2020 return MONO_MARSHAL_NONE
;
2021 case MONO_TYPE_STRING
:
2022 return MONO_MARSHAL_COPY
;
2023 case MONO_TYPE_ARRAY
:
2024 case MONO_TYPE_SZARRAY
: {
2025 MonoClass
*elem_class
= m_class_get_element_class (mono_class_from_mono_type_internal (t
));
2026 if (mono_get_xdomain_marshal_type (m_class_get_byval_arg (elem_class
)) != MONO_MARSHAL_SERIALIZE
)
2027 return MONO_MARSHAL_COPY
;
2033 return MONO_MARSHAL_SERIALIZE
;
2036 /* Replace the given array element by a copy in the current domain */
2038 xdomain_copy_array_element_inplace (MonoArrayHandle arr
, int i
, MonoError
*error
)
2040 HANDLE_FUNCTION_ENTER ();
2042 MonoObjectHandle item
= MONO_HANDLE_NEW (MonoObject
, NULL
);
2043 MONO_HANDLE_ARRAY_GETREF (item
, arr
, i
);
2045 MonoObjectHandle item_copy
= mono_marshal_xdomain_copy_value_handle (item
, error
);
2046 goto_if_nok (error
, leave
);
2047 MONO_HANDLE_ARRAY_SETREF (arr
, i
, item_copy
);
2049 HANDLE_FUNCTION_RETURN_VAL (is_ok (error
));
2053 * mono_marshal_xdomain_copy_value_handle:
2054 * \param val The value to copy.
2055 * \param error set on failure.
2056 * Makes a copy of \p val suitable for the current domain.
2057 * On failure returns NULL and sets \p error.
2060 mono_marshal_xdomain_copy_value_handle (MonoObjectHandle val
, MonoError
*error
)
2063 MonoObjectHandle result
= MONO_HANDLE_NEW (MonoObject
, NULL
);
2064 if (MONO_HANDLE_IS_NULL (val
))
2068 domain
= mono_domain_get ();
2071 klass
= mono_handle_class (val
);
2073 switch (m_class_get_byval_arg (klass
)->type
) {
2074 case MONO_TYPE_VOID
:
2075 g_assert_not_reached ();
2079 case MONO_TYPE_BOOLEAN
:
2082 case MONO_TYPE_CHAR
:
2088 case MONO_TYPE_R8
: {
2089 uint32_t gchandle
= mono_gchandle_from_handle (val
, TRUE
);
2090 MonoObjectHandle res
= MONO_HANDLE_NEW (MonoObject
, mono_value_box_checked (domain
, klass
, ((char*)MONO_HANDLE_RAW (val
)) + sizeof(MonoObject
), error
)); /* FIXME use handles in mono_value_box_checked */
2091 mono_gchandle_free_internal (gchandle
);
2092 goto_if_nok (error
, leave
);
2093 MONO_HANDLE_ASSIGN (result
, res
);
2096 case MONO_TYPE_STRING
: {
2097 MonoStringHandle str
= MONO_HANDLE_CAST (MonoString
, val
);
2098 uint32_t gchandle
= mono_gchandle_from_handle (val
, TRUE
);
2099 MonoStringHandle res
= mono_string_new_utf16_handle (domain
, mono_string_chars_internal (MONO_HANDLE_RAW (str
)), mono_string_handle_length (str
), error
);
2100 mono_gchandle_free_internal (gchandle
);
2101 goto_if_nok (error
, leave
);
2102 MONO_HANDLE_ASSIGN (result
, res
);
2105 case MONO_TYPE_ARRAY
:
2106 case MONO_TYPE_SZARRAY
: {
2107 MonoArrayHandle arr
= MONO_HANDLE_CAST (MonoArray
, val
);
2108 MonoXDomainMarshalType mt
= mono_get_xdomain_marshal_type (m_class_get_byval_arg (m_class_get_element_class (klass
)));
2109 if (mt
== MONO_MARSHAL_SERIALIZE
)
2111 MonoArrayHandle acopy
= mono_array_clone_in_domain (domain
, arr
, error
);
2112 goto_if_nok (error
, leave
);
2114 if (mt
== MONO_MARSHAL_COPY
) {
2115 int i
, len
= mono_array_handle_length (acopy
);
2116 for (i
= 0; i
< len
; i
++) {
2117 if (!xdomain_copy_array_element_inplace (acopy
, i
, error
))
2121 MONO_HANDLE_ASSIGN (result
, acopy
);
2132 #ifndef DISABLE_REMOTING
2134 /* mono_marshal_xdomain_copy_value
2135 * Makes a copy of "val" suitable for the current domain.
2138 mono_marshal_xdomain_copy_value (MonoObject
* val_raw
, MonoError
*error
)
2140 HANDLE_FUNCTION_ENTER ();
2141 /* FIXME callers of mono_marshal_xdomain_copy_value should use handles */
2142 MONO_HANDLE_DCL (MonoObject
, val
);
2143 MonoObjectHandle result
= mono_marshal_xdomain_copy_value_handle (val
, error
);
2144 HANDLE_FUNCTION_RETURN_OBJ (result
);
2149 /* mono_marshal_xdomain_copy_value
2150 * Makes a copy of "val" suitable for the current domain.
2153 ves_icall_mono_marshal_xdomain_copy_value_impl (MonoObjectHandle val
, MonoError
*error
)
2155 return mono_marshal_xdomain_copy_value_handle (val
, error
);
2159 mono_context_set_icall (MonoAppContext
*new_context_raw
)
2161 HANDLE_FUNCTION_ENTER ();
2162 MONO_HANDLE_DCL (MonoAppContext
, new_context
);
2163 mono_context_set_handle (new_context
);
2164 HANDLE_FUNCTION_RETURN ();
2167 static MonoAppContext
*
2168 mono_context_get_icall (void)
2170 HANDLE_FUNCTION_ENTER ();
2171 MonoAppContextHandle context
= mono_context_get_handle ();
2172 HANDLE_FUNCTION_RETURN_OBJ (context
);