#include "mono/utils/mono-counters.h"
#include "mono/utils/mono-tls.h"
#include "mono/utils/mono-memory-model.h"
+#include "mono/utils/atomic.h"
#include <string.h>
#include <errno.h>
/*
* This mutex protects the various marshalling related caches in MonoImage
* and a few other data structures static to this file.
- * Note that when this lock is held it is not possible to take other runtime
- * locks like the loader lock.
+ *
+ * The marshal lock is a non-recursive complex lock that sits below the domain lock in the
+ * runtime locking latice. Which means it can take simple locks suck as the image lock.
*/
-#define mono_marshal_lock() EnterCriticalSection (&marshal_mutex)
-#define mono_marshal_unlock() LeaveCriticalSection (&marshal_mutex)
+#define mono_marshal_lock() mono_locks_acquire (&marshal_mutex, MarshalLock)
+#define mono_marshal_unlock() mono_locks_release (&marshal_mutex, MarshalLock)
static CRITICAL_SECTION marshal_mutex;
static gboolean marshal_mutex_initialized;
static void
mono_struct_delete_old (MonoClass *klass, char *ptr);
-void *
+MONO_API void *
mono_marshal_string_to_utf16 (MonoString *s);
static void *
static void
mono_byvalarray_to_array (MonoArray *arr, gpointer native_arr, MonoClass *eltype, guint32 elnum);
+static void
+mono_byvalarray_to_byte_array (MonoArray *arr, gpointer native_arr, guint32 elnum);
+
static void
mono_array_to_byvalarray (gpointer native_arr, MonoArray *arr, MonoClass *eltype, guint32 elnum);
+static void
+mono_array_to_byte_byvalarray (gpointer native_arr, MonoArray *arr, guint32 elnum);
static MonoAsyncResult *
mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params);
static MonoObject *
mono_remoting_wrapper (MonoMethod *method, gpointer *params);
-void
+MONO_API void
mono_upgrade_remote_class_wrapper (MonoReflectionType *rtype, MonoTransparentProxy *tproxy);
#endif
return sig;
}
+void
+mono_marshal_init_tls (void)
+{
+ mono_native_tls_alloc (&last_error_tls_id, NULL);
+ mono_native_tls_alloc (&load_type_info_tls_id, NULL);
+}
+
void
mono_marshal_init (void)
{
module_initialized = TRUE;
InitializeCriticalSection (&marshal_mutex);
marshal_mutex_initialized = TRUE;
- mono_native_tls_alloc (&last_error_tls_id, NULL);
- mono_native_tls_alloc (&load_type_info_tls_id, NULL);
register_icall (ves_icall_System_Threading_Thread_ResetAbort, "ves_icall_System_Threading_Thread_ResetAbort", "void", TRUE);
register_icall (mono_marshal_string_to_utf16, "mono_marshal_string_to_utf16", "ptr obj", FALSE);
register_icall (mono_array_to_lparray, "mono_array_to_lparray", "ptr object", FALSE);
register_icall (mono_free_lparray, "mono_free_lparray", "void object ptr", FALSE);
register_icall (mono_byvalarray_to_array, "mono_byvalarray_to_array", "void object ptr ptr int32", FALSE);
+ register_icall (mono_byvalarray_to_byte_array, "mono_byvalarray_to_byte_array", "void object ptr int32", FALSE);
register_icall (mono_array_to_byvalarray, "mono_array_to_byvalarray", "void ptr object ptr int32", FALSE);
+ register_icall (mono_array_to_byte_byvalarray, "mono_array_to_byte_byvalarray", "void ptr object int32", FALSE);
register_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", "ptr object", FALSE);
register_icall (mono_ftnptr_to_delegate, "mono_ftnptr_to_delegate", "object ptr ptr", FALSE);
register_icall (mono_marshal_asany, "mono_marshal_asany", "ptr object int32 int32", FALSE);
if (ptr) {
uint32_t gchandle;
void **method_data;
+ MonoMethod *method;
+
ji = mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (ptr));
g_assert (ji);
- method_data = ((MonoMethodWrapper*)ji->method)->method_data;
+ method = mono_jit_info_get_method (ji);
+ method_data = ((MonoMethodWrapper*)method)->method_data;
/*the target gchandle is the first entry after size and the wrapper itself.*/
gchandle = GPOINTER_TO_UINT (method_data [2]);
if (gchandle)
mono_gchandle_free (gchandle);
- mono_runtime_free_method (mono_object_domain (delegate), ji->method);
+ mono_runtime_free_method (mono_object_domain (delegate), method);
}
}
gpointer
mono_array_to_lparray (MonoArray *array)
{
+#ifndef DISABLE_COM
gpointer *nativeArray = NULL;
int nativeArraySize = 0;
int i = 0;
MonoClass *klass;
+#endif
if (!array)
return NULL;
case MONO_TYPE_VOID:
g_assert_not_reached ();
break;
-#ifndef DISABLE_COM
case MONO_TYPE_CLASS:
nativeArraySize = array->max_length;
nativeArray = malloc(sizeof(gpointer) * nativeArraySize);
for(i = 0; i < nativeArraySize; ++i)
nativeArray[i] = ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal(((gpointer*)array->vector)[i]);
return nativeArray;
-#endif
case MONO_TYPE_U1:
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_I1:
g_assert_not_reached ();
}
+static void
+mono_byvalarray_to_byte_array (MonoArray *arr, gpointer native_arr, guint32 elnum)
+{
+ mono_byvalarray_to_array (arr, native_arr, mono_defaults.byte_class, elnum);
+}
+
static void
mono_array_to_byvalarray (gpointer native_arr, MonoArray *arr, MonoClass *elclass, guint32 elnum)
{
}
}
+static void
+mono_array_to_byte_byvalarray (gpointer native_arr, MonoArray *arr, guint32 elnum)
+{
+ mono_array_to_byvalarray (native_arr, arr, mono_defaults.byte_class, elnum);
+}
+
void
mono_string_utf8_to_builder (MonoStringBuilder *sb, char *text)
{
mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_ptr (mb, mono_defaults.byte_class);
mono_mb_emit_icon (mb, mspec->data.array_data.num_elem);
- mono_mb_emit_icall (mb, mono_byvalarray_to_array);
+ mono_mb_emit_icall (mb, mono_byvalarray_to_byte_array);
break;
}
case MONO_MARSHAL_CONV_STR_BYVALSTR:
mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
- mono_mb_emit_ptr (mb, mono_defaults.byte_class);
mono_mb_emit_icon (mb, mspec->data.array_data.num_elem);
- mono_mb_emit_icall (mb, mono_array_to_byvalarray);
+ mono_mb_emit_icall (mb, mono_array_to_byte_byvalarray);
mono_mb_patch_short_branch (mb, pos);
break;
}
}
case MONO_TYPE_OBJECT: {
#ifndef DISABLE_COM
- mono_init_com_types ();
if (to_object) {
static MonoMethod *variant_clear = NULL;
static MonoMethod *get_object_for_native_variant = NULL;
if (!variant_clear)
- variant_clear = mono_class_get_method_from_name (mono_defaults.variant_class, "Clear", 0);
+ variant_clear = mono_class_get_method_from_name (mono_class_get_variant_class (), "Clear", 0);
if (!get_object_for_native_variant)
get_object_for_native_variant = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForNativeVariant", 1);
mono_mb_emit_ldloc (mb, 1);
res = mono_marshal_get_wrapper_info (wrapper);
if (res == NULL)
return wrapper;
- return res;
+ if (wrapper->is_inflated)
+ /*
+ * A method cannot be inflated and a wrapper at the same time, so the wrapper info
+ * contains an uninflated method.
+ */
+ return mono_class_inflate_generic_method (res, mono_method_get_context (wrapper));
+ else
+ return res;
case MONO_WRAPPER_MANAGED_TO_NATIVE:
info = mono_marshal_get_wrapper_info (wrapper);
- if (info && (info->subtype == WRAPPER_SUBTYPE_NONE || info->subtype == WRAPPER_SUBTYPE_NATIVE_FUNC_AOT))
+ if (info && (info->subtype == WRAPPER_SUBTYPE_NONE || info->subtype == WRAPPER_SUBTYPE_NATIVE_FUNC_AOT || info->subtype == WRAPPER_SUBTYPE_PINVOKE))
return info->d.managed_to_native.method;
else
return NULL;
return klass;
}
+/*
+ * Wrappers for generic methods should be instances of generic wrapper methods, i.e .the wrapper for Sort<int> should be
+ * an instance of the wrapper for Sort<T>. This is required for full-aot to work.
+ */
+
+/*
+ * check_generic_wrapper_cache:
+ *
+ * Check CACHE for the wrapper of the generic instance ORIG_METHOD, and return it if it is found.
+ * KEY should be the key for ORIG_METHOD in the cache, while DEF_KEY should be the key of its
+ * generic method definition.
+ */
+static MonoMethod*
+check_generic_wrapper_cache (GHashTable *cache, MonoMethod *orig_method, gpointer key, gpointer def_key)
+{
+ MonoMethod *res;
+ MonoMethod *inst, *def;
+ MonoGenericContext *ctx;
+ MonoMethod *def_method;
+
+ g_assert (orig_method->is_inflated);
+ def_method = ((MonoMethodInflated*)orig_method)->declaring;
+ ctx = mono_method_get_context (orig_method);
+
+ /*
+ * Look for the instance
+ */
+ res = mono_marshal_find_in_cache (cache, key);
+ if (res)
+ return res;
+
+ /*
+ * Look for the definition
+ */
+ def = mono_marshal_find_in_cache (cache, def_key);
+ if (def) {
+ inst = mono_class_inflate_generic_method (def, ctx);
+ /* Cache it */
+ mono_memory_barrier ();
+ mono_marshal_lock ();
+ res = g_hash_table_lookup (cache, key);
+ if (!res) {
+ g_hash_table_insert (cache, key, inst);
+ res = inst;
+ }
+ mono_marshal_unlock ();
+ return res;
+ }
+ return NULL;
+}
+
+static MonoMethod*
+cache_generic_wrapper (GHashTable *cache, MonoMethod *orig_method, MonoMethod *def, MonoGenericContext *ctx, gpointer key)
+{
+ MonoMethod *inst, *res;
+
+ /*
+ * We use the same cache for the generic definition and the instances.
+ */
+ inst = mono_class_inflate_generic_method (def, ctx);
+ mono_memory_barrier ();
+ mono_marshal_lock ();
+ res = g_hash_table_lookup (cache, key);
+ if (!res) {
+ g_hash_table_insert (cache, key, inst);
+ res = inst;
+ }
+ mono_marshal_unlock ();
+ return res;
+}
+
static MonoMethod*
check_generic_delegate_wrapper_cache (GHashTable *cache, MonoMethod *orig_method, MonoMethod *def_method, MonoGenericContext *ctx)
{
/* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
#ifndef DISABLE_COM
- if (mono_class_is_com_object (method->klass) || method->klass == mono_defaults.com_object_class) {
+ if (mono_class_is_com_object (method->klass) || method->klass == mono_class_get_com_object_class ()) {
MonoVTable *vtable = mono_class_vtable (mono_domain_get (), method->klass);
g_assert (vtable); /*FIXME do proper error handling*/
MonoGenericContext *ctx = NULL;
MonoGenericContainer *container = NULL;
MonoMethod *orig_method = NULL;
+ WrapperInfo *info;
/*
* If the delegate target is null, and the target method is not static, a virtual
closed_over_null = sig->param_count == mono_method_signature (del->method)->param_count;
if (del && del->method && mono_method_signature (del->method)->param_count == sig->param_count + 1 && (del->method->flags & METHOD_ATTRIBUTE_STATIC)) {
+ g_assert (!callvirt);
invoke_sig = mono_method_signature (del->method);
- target_method = del->method;
+ target_method = NULL;
static_method_with_first_arg_bound = TRUE;
}
res = check_generic_delegate_wrapper_cache (cache, orig_method, method, ctx);
if (res)
return res;
- } else if (callvirt || static_method_with_first_arg_bound) {
+ } else if (static_method_with_first_arg_bound) {
+ cache = get_cache (&method->klass->image->delegate_bound_static_invoke_cache,
+ (GHashFunc)mono_signature_hash,
+ (GCompareFunc)mono_metadata_signature_equal);
+ /*
+ * The wrapper is based on sig+invoke_sig, but sig can be derived from invoke_sig.
+ */
+ res = mono_marshal_find_in_cache (cache, invoke_sig);
+ if (res)
+ return res;
+ } else if (callvirt) {
GHashTable **cache_ptr;
- if (static_method_with_first_arg_bound)
- cache_ptr = &method->klass->image->delegate_bound_static_invoke_cache;
- else
- cache_ptr = &method->klass->image->delegate_abstract_invoke_cache;
+
+ cache_ptr = &method->klass->image->delegate_abstract_invoke_cache;
/* We need to cache the signature+method pair */
mono_marshal_lock ();
if (!static_method_with_first_arg_bound)
invoke_sig = static_sig;
- name = mono_signature_to_name (sig, "invoke");
+ if (static_method_with_first_arg_bound)
+ name = mono_signature_to_name (invoke_sig, "invoke_bound");
+ else if (closed_over_null)
+ name = mono_signature_to_name (invoke_sig, "invoke_closed_over_null");
+ else if (callvirt)
+ name = mono_signature_to_name (invoke_sig, "invoke_callvirt");
+ else
+ name = mono_signature_to_name (sig, "invoke");
if (ctx)
mb = mono_mb_new (method->klass, name, MONO_WRAPPER_DELEGATE_INVOKE);
else
def = mono_mb_create_and_cache (cache, method->klass, mb, sig, sig->param_count + 16);
res = cache_generic_delegate_wrapper (cache, orig_method, def, ctx);
- } else if (static_method_with_first_arg_bound || callvirt) {
+ } else if (static_method_with_first_arg_bound) {
+ res = mono_mb_create_and_cache (cache, invoke_sig, mb, sig, sig->param_count + 16);
+
+ info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_DELEGATE_INVOKE_BOUND);
+ mono_marshal_set_wrapper_info (res, info);
+ } else if (callvirt) {
// From mono_mb_create_and_cache
newm = mono_mb_create_method (mb, sig, sig->param_count + 16);
/*We perform double checked locking, so must fence before publishing*/
if (static_method_with_first_arg_bound)
new_key->sig = signature_dup (del->method->klass->image, key.sig);
g_hash_table_insert (cache, new_key, res);
- mono_marshal_set_wrapper_info (res, new_key);
+
+ info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_DELEGATE_INVOKE_VIRTUAL);
+ mono_marshal_set_wrapper_info (res, info);
+
mono_marshal_unlock ();
} else {
mono_marshal_unlock ();
}
} else {
res = mono_mb_create_and_cache (cache, sig, mb, sig, sig->param_count + 16);
+
+ info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_NONE);
+ mono_marshal_set_wrapper_info (res, info);
}
mono_mb_free (mb);
static MonoType*
get_runtime_invoke_type (MonoType *t, gboolean ret)
{
- if (t->byref)
+ if (t->byref) {
+ if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t)))
+ return t;
/* Can't share this with 'I' as that needs another indirection */
- return t;
+ return &mono_defaults.int_class->this_arg;
+ }
if (MONO_TYPE_IS_REFERENCE (t))
return &mono_defaults.object_class->byval_arg;
handle_enum:
switch (t->type) {
+ /* Can't share these as the argument needs to be loaded using sign/zero extension */
+ /*
case MONO_TYPE_U1:
return &mono_defaults.sbyte_class->byval_arg;
case MONO_TYPE_U2:
return &mono_defaults.int16_class->byval_arg;
case MONO_TYPE_U4:
return &mono_defaults.int32_class->byval_arg;
+ */
case MONO_TYPE_U8:
return &mono_defaults.int64_class->byval_arg;
case MONO_TYPE_BOOLEAN:
- return &mono_defaults.sbyte_class->byval_arg;
+ return &mono_defaults.byte_class->byval_arg;
case MONO_TYPE_CHAR:
- return &mono_defaults.int16_class->byval_arg;
+ return &mono_defaults.uint16_class->byval_arg;
case MONO_TYPE_U:
case MONO_TYPE_PTR:
return &mono_defaults.int_class->byval_arg;
/* Can't share this as we push a string as this */
need_direct_wrapper = TRUE;
} else {
- if (method->klass->valuetype && mono_method_signature (method)->hasthis) {
- /*
- * Valuetype methods receive a managed pointer as the this argument.
- * Create a new signature to reflect this.
- */
- callsig = signature_dup_add_this (method->klass->image, mono_method_signature (method), method->klass);
- /* Can't share this as it would be shared with static methods taking an IntPtr argument */
- need_direct_wrapper = TRUE;
- } else {
- if (method->dynamic)
- callsig = signature_dup (method->klass->image, mono_method_signature (method));
- else
- callsig = mono_method_signature (method);
- }
+ if (method->dynamic)
+ callsig = signature_dup (method->klass->image, mono_method_signature (method));
+ else
+ callsig = mono_method_signature (method);
}
target_klass = get_wrapper_target_class (method->klass->image);
callsig = mono_marshal_get_runtime_invoke_sig (callsig);
- cache = get_cache (&target_klass->image->runtime_invoke_cache,
- (GHashFunc)mono_signature_hash,
- (GCompareFunc)runtime_invoke_signature_equal);
+ if (method->klass->valuetype && mono_method_signature (method)->hasthis)
+ /* These have a different csig */
+ cache = get_cache (&target_klass->image->runtime_invoke_vtype_cache,
+ (GHashFunc)mono_signature_hash,
+ (GCompareFunc)runtime_invoke_signature_equal);
+ else
+ cache = get_cache (&target_klass->image->runtime_invoke_cache,
+ (GHashFunc)mono_signature_hash,
+ (GCompareFunc)runtime_invoke_signature_equal);
/* from mono_marshal_find_in_cache */
mono_marshal_lock ();
csig->ret = &mono_defaults.object_class->byval_arg;
if (method->klass->valuetype && mono_method_signature (method)->hasthis)
- csig->params [0] = callsig->params [0];
+ csig->params [0] = get_runtime_invoke_type (&method->klass->this_arg, FALSE);
else
csig->params [0] = &mono_defaults.object_class->byval_arg;
csig->params [1] = &mono_defaults.int_class->byval_arg;
mono_mb_emit_byte (mb, CEE_RET);
#endif /* DISABLE_JIT */
- mono_loader_lock ();
+ mono_marshal_lock ();
/* double-checked locking */
if (!method) {
method = mono_mb_create_method (mb, csig, 16);
info = mono_wrapper_info_create (method, WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC);
mono_marshal_set_wrapper_info (method, info);
}
- mono_loader_unlock ();
+ mono_marshal_unlock ();
mono_mb_free (mb);
mono_mb_emit_byte (mb, CEE_LDIND_REF);
}
break;
+ case MONO_TYPE_VAR:
+ case MONO_TYPE_MVAR:
+ mono_mb_emit_op (mb, CEE_LDOBJ, klass);
+ break;
default:
g_warning ("type %x not implemented", type->type);
g_assert_not_reached ();
mono_mb_emit_op (mb, CEE_STOBJ, klass);
break;
case MONO_TYPE_GENERICINST:
+ case MONO_TYPE_VAR:
+ case MONO_TYPE_MVAR:
mono_mb_emit_op (mb, CEE_STOBJ, klass);
break;
default:
break;
}
+ if (t->byref && (t->attrs & PARAM_ATTRIBUTE_IN) && !(t->attrs & PARAM_ATTRIBUTE_OUT))
+ break;
+
/* Check for null */
mono_mb_emit_ldarg (mb, argnum);
pos2 = mono_mb_emit_branch (mb, CEE_BRFALSE);
}
#endif
- if (t->byref && !t->attrs & PARAM_ATTRIBUTE_IN && t->attrs & PARAM_ATTRIBUTE_OUT)
+ if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && (t->attrs & PARAM_ATTRIBUTE_OUT))
break;
if (conv == -1) {
static MonoMethod *get_object_for_native_variant = NULL;
static MonoMethod *get_native_variant_for_object = NULL;
- mono_init_com_types ();
-
if (!get_object_for_native_variant)
get_object_for_native_variant = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForNativeVariant", 1);
g_assert (get_object_for_native_variant);
switch (action) {
case MARSHAL_ACTION_CONV_IN: {
- conv_arg = mono_mb_add_local (mb, &mono_defaults.variant_class->byval_arg);
+ conv_arg = mono_mb_add_local (mb, &mono_class_get_variant_class ()->byval_arg);
if (t->byref)
- *conv_arg_type = &mono_defaults.variant_class->this_arg;
+ *conv_arg_type = &mono_class_get_variant_class ()->this_arg;
else
- *conv_arg_type = &mono_defaults.variant_class->byval_arg;
+ *conv_arg_type = &mono_class_get_variant_class ()->byval_arg;
if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && t->attrs & PARAM_ATTRIBUTE_OUT)
break;
static MonoMethod *variant_clear = NULL;
if (!variant_clear)
- variant_clear = mono_class_get_method_from_name (mono_defaults.variant_class, "Clear", 0);
+ variant_clear = mono_class_get_method_from_name (mono_class_get_variant_class (), "Clear", 0);
g_assert (variant_clear);
conv_arg = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
if (t->byref)
- *conv_arg_type = &mono_defaults.variant_class->this_arg;
+ *conv_arg_type = &mono_class_get_variant_class ()->this_arg;
else
- *conv_arg_type = &mono_defaults.variant_class->byval_arg;
+ *conv_arg_type = &mono_class_get_variant_class ()->byval_arg;
if (t->byref && !(t->attrs & PARAM_ATTRIBUTE_IN) && t->attrs & PARAM_ATTRIBUTE_OUT)
break;
pinvoke = TRUE;
if (!piinfo->addr) {
- if (pinvoke)
+ if (pinvoke) {
if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
exc_arg = "Method contains unsupported native code";
- else
+ else if (!aot)
mono_lookup_pinvoke_call (method, &exc_class, &exc_arg);
- else
+ } else {
piinfo->addr = mono_lookup_internal_call (method);
+ }
}
/* hack - redirect certain string constructors to CreateString */
mb, csig, csig->param_count + 16);
mono_mb_free (mb);
- info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_NONE);
+ info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_PINVOKE);
info->d.managed_to_native.method = method;
mono_marshal_set_wrapper_info (res, info);
emit_marshal (m, i, sig->params [i], mspecs [i + 1], 0, &csig->params [i], MARSHAL_ACTION_MANAGED_CONV_IN);
}
}
+
+ if (!sig->ret->byref) {
+ switch (sig->ret->type) {
+ case MONO_TYPE_STRING:
+ csig->ret = &mono_defaults.int_class->byval_arg;
+ break;
+ default:
+ break;
+ }
+ }
#else
MonoMethodSignature *sig, *csig;
int i, *tmp_locals;
MonoBoolean set_last_error = 0;
MonoBoolean best_fit_mapping = 0;
MonoBoolean throw_on_unmappable = 0;
+ MonoError error;
- mono_reflection_create_custom_attr_data_args (mono_defaults.corlib, attr->ctor, attr->data, attr->data_size, &typed_args, &named_args, &arginfo);
-
+ mono_reflection_create_custom_attr_data_args (mono_defaults.corlib, attr->ctor, attr->data, attr->data_size, &typed_args, &named_args, &arginfo, &error);
+ g_assert (mono_error_ok (&error));
g_assert (mono_array_length (typed_args) == 1);
/* typed args */
sig = mono_method_signature (method);
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_MANAGED);
-#ifndef DISABLE_JIT
param_count = sig->param_count + sig->hasthis;
+#ifndef DISABLE_JIT
for (i = 0; i < param_count; i++)
mono_mb_emit_ldarg (mb, i);
static MonoMethodSignature *isint_sig = NULL;
GHashTable *cache;
MonoMethod *res;
- int pos_was_ok, pos_failed, pos_end, pos_end2;
+ int pos_was_ok, pos_end;
+#ifndef DISABLE_REMOTING
+ int pos_end2, pos_failed;
+#endif
char *name;
MonoMethodBuilder *mb;
static MonoMethodSignature *castclass_sig = NULL;
GHashTable *cache;
MonoMethod *res;
+#ifndef DISABLE_REMOTING
int pos_was_ok, pos_was_ok2;
+#endif
char *name;
MonoMethodBuilder *mb;
WrapperInfo *info;
WrapperInfo *info;
MonoMethodSignature *sig;
MonoMethod *res;
+ MonoGenericContext *ctx = NULL;
+ MonoMethod *orig_method = NULL;
+ MonoGenericContainer *container = NULL;
+
+ if (method->is_inflated && !mono_method_get_context (method)->method_inst) {
+ orig_method = method;
+ ctx = &((MonoMethodInflated*)method)->context;
+ method = ((MonoMethodInflated*)method)->declaring;
+ container = mono_method_get_generic_container (method);
+ if (!container)
+ container = method->klass->generic_container;
+ g_assert (container);
+ }
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_UNKNOWN);
#ifndef DISABLE_JIT
info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_SYNCHRONIZED_INNER);
info->d.synchronized_inner.method = method;
mono_marshal_set_wrapper_info (res, info);
+ if (ctx)
+ res = mono_class_inflate_generic_method (res, ctx);
return res;
}
MonoMethod *res;
GHashTable *cache;
int i, pos, this_local, ret_local = 0;
+ MonoGenericContext *ctx = NULL;
+ MonoMethod *orig_method = NULL;
+ MonoGenericContainer *container = NULL;
g_assert (method);
if (method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED)
return method;
- cache = get_cache (&method->klass->image->synchronized_cache, mono_aligned_addr_hash, NULL);
- if ((res = mono_marshal_find_in_cache (cache, method)))
- return res;
+ /* FIXME: Support generic methods too */
+ if (method->is_inflated && !mono_method_get_context (method)->method_inst) {
+ orig_method = method;
+ ctx = &((MonoMethodInflated*)method)->context;
+ method = ((MonoMethodInflated*)method)->declaring;
+ container = mono_method_get_generic_container (method);
+ if (!container)
+ container = method->klass->generic_container;
+ g_assert (container);
+ }
+
+ /*
+ * Check cache
+ */
+ if (ctx) {
+ cache = get_cache (&method->klass->image->synchronized_generic_cache, mono_aligned_addr_hash, NULL);
+ res = check_generic_wrapper_cache (cache, orig_method, orig_method, method);
+ if (res)
+ return res;
+ } else {
+ cache = get_cache (&method->klass->image->synchronized_cache, mono_aligned_addr_hash, NULL);
+ if ((res = mono_marshal_find_in_cache (cache, method)))
+ return res;
+ }
sig = signature_dup (method->klass->image, mono_method_signature (method));
sig->pinvoke = 0;
clause->flags = MONO_EXCEPTION_CLAUSE_FINALLY;
#endif
- mono_loader_lock ();
+ mono_marshal_lock ();
if (!enter_method) {
MonoMethodDesc *desc;
mono_method_desc_free (desc);
desc = mono_method_desc_new ("Type:GetTypeFromHandle", FALSE);
- gettypefromhandle_method = mono_method_desc_search_in_class (desc, mono_defaults.monotype_class->parent);
+ gettypefromhandle_method = mono_method_desc_search_in_class (desc, mono_defaults.systemtype_class);
g_assert (gettypefromhandle_method);
mono_method_desc_free (desc);
}
- mono_loader_unlock ();
+ mono_marshal_unlock ();
#ifndef DISABLE_JIT
/* Push this or the type object */
for (i = 0; i < sig->param_count; i++)
mono_mb_emit_ldarg (mb, i + (sig->hasthis == TRUE));
- mono_mb_emit_managed_call (mb, method, NULL);
+ if (ctx)
+ mono_mb_emit_managed_call (mb, mono_class_inflate_generic_method (method, &container->context), NULL);
+ else
+ mono_mb_emit_managed_call (mb, method, NULL);
if (!MONO_TYPE_IS_VOID (sig->ret))
mono_mb_emit_stloc (mb, ret_local);
mono_mb_set_clauses (mb, 1, clause);
#endif
- res = mono_mb_create_and_cache (cache, method,
- mb, sig, sig->param_count + 16);
+ if (ctx) {
+ MonoMethod *def;
+ def = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
+ res = cache_generic_wrapper (cache, orig_method, def, ctx, orig_method);
+ } else {
+ res = mono_mb_create_and_cache (cache, method,
+ mb, sig, sig->param_count + 16);
+ }
mono_mb_free (mb);
return res;
return ret;
}
+/*
+ * mono_marshal_get_array_accessor_wrapper:
+ *
+ * Return a wrapper which just calls METHOD, which should be an Array Get/Set/Address method.
+ */
+MonoMethod *
+mono_marshal_get_array_accessor_wrapper (MonoMethod *method)
+{
+ MonoMethodSignature *sig;
+ MonoMethodBuilder *mb;
+ MonoMethod *res;
+ GHashTable *cache;
+ int i;
+ MonoGenericContext *ctx = NULL;
+ MonoMethod *orig_method = NULL;
+ MonoGenericContainer *container = NULL;
+ WrapperInfo *info;
+
+ /*
+ * These wrappers are needed to avoid the JIT replacing the calls to these methods with intrinsics
+ * inside runtime invoke wrappers, thereby making the wrappers not unshareable.
+ * FIXME: Use generic methods.
+ */
+ /*
+ * Check cache
+ */
+ if (ctx) {
+ cache = NULL;
+ g_assert_not_reached ();
+ } else {
+ cache = get_cache (&method->klass->image->array_accessor_cache, mono_aligned_addr_hash, NULL);
+ if ((res = mono_marshal_find_in_cache (cache, method)))
+ return res;
+ }
+
+ sig = signature_dup (method->klass->image, mono_method_signature (method));
+ sig->pinvoke = 0;
+
+ mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_UNKNOWN);
+
+#ifndef DISABLE_JIT
+ /* Call the method */
+ if (sig->hasthis)
+ mono_mb_emit_ldarg (mb, 0);
+ for (i = 0; i < sig->param_count; i++)
+ mono_mb_emit_ldarg (mb, i + (sig->hasthis == TRUE));
+
+ if (ctx)
+ mono_mb_emit_managed_call (mb, mono_class_inflate_generic_method (method, &container->context), NULL);
+ else
+ mono_mb_emit_managed_call (mb, method, NULL);
+ mono_mb_emit_byte (mb, CEE_RET);
+#endif
+
+ if (ctx) {
+ MonoMethod *def;
+ def = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
+ res = cache_generic_wrapper (cache, orig_method, def, ctx, orig_method);
+ } else {
+ res = mono_mb_create_and_cache (cache, method,
+ mb, sig, sig->param_count + 16);
+ info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_ARRAY_ACCESSOR);
+ info->d.array_accessor.method = method;
+ mono_marshal_set_wrapper_info (res, info);
+ }
+ mono_mb_free (mb);
+
+ return res;
+}
+
void*
mono_marshal_alloc (gulong size)
{
element_size = mono_array_element_size (src->obj.vtable->klass);
/* no references should be involved */
- source_addr = mono_array_addr_with_size (src, element_size, start_index);
+ source_addr = mono_array_addr_with_size_fast (src, element_size, start_index);
memcpy (dest, source_addr, length * element_size);
}
element_size = mono_array_element_size (dest->obj.vtable->klass);
/* no references should be involved */
- dest_addr = mono_array_addr_with_size (dest, element_size, start_index);
+ dest_addr = mono_array_addr_with_size_fast (dest, element_size, start_index);
memcpy (dest_addr, src, length * element_size);
}
layout = (klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK);
- if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
+ if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
+ return sizeof (gpointer);
+ } else if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
gchar *msg;
MonoException *exc;
void*
ves_icall_System_Runtime_InteropServices_Marshal_UnsafeAddrOfPinnedArrayElement (MonoArray *arrayobj, int index)
{
- return mono_array_addr_with_size (arrayobj, mono_array_element_size (arrayobj->obj.vtable->klass), index);
+ return mono_array_addr_with_size_fast (arrayobj, mono_array_element_size (arrayobj->obj.vtable->klass), index);
}
MonoDelegate*
if (!klass->inited)
mono_class_init (klass);
- mono_loader_lock ();
-
- if (klass->marshal_info) {
- mono_loader_unlock ();
+ if (klass->marshal_info)
return klass->marshal_info;
- }
/*
* This function can recursively call itself, so we keep the list of classes which are
case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
size = mono_marshal_type_size (field->type, info->fields [j].mspec,
&align, TRUE, klass->unicode);
- min_align = packing;
+ min_align = MAX (align, min_align);
info->fields [j].offset = field->offset - sizeof (MonoObject);
info->native_size = MAX (info->native_size, info->fields [j].offset + size);
break;
* If the provided Size is equal or larger than the calculated size, and there
* was no Pack attribute, we set min_align to 1 to avoid native_size being increased
*/
- if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
+ if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
if (native_size && native_size == info->native_size && klass->packing_size == 0)
min_align = 1;
+ else
+ min_align = MIN (min_align, packing);
+ }
}
if (info->native_size & (min_align - 1)) {
loads_list = g_slist_remove (loads_list, klass);
mono_native_tls_set_value (load_type_info_tls_id, loads_list);
- /*We do double-checking locking on marshal_info */
- mono_memory_barrier ();
-
- klass->marshal_info = info;
-
- mono_loader_unlock ();
+ mono_marshal_lock ();
+ if (!klass->marshal_info) {
+ /*We do double-checking locking on marshal_info */
+ mono_memory_barrier ();
+ klass->marshal_info = info;
+ }
+ mono_marshal_unlock ();
return klass->marshal_info;
}
*/
if (image->runtime_invoke_direct_cache)
g_hash_table_remove (image->runtime_invoke_direct_cache, method);
- if (image->delegate_bound_static_invoke_cache)
- g_hash_table_foreach_remove (image->delegate_bound_static_invoke_cache, signature_method_pair_matches_method, method);
if (image->delegate_abstract_invoke_cache)
g_hash_table_foreach_remove (image->delegate_abstract_invoke_cache, signature_method_pair_matches_method, method);
g_hash_table_remove (method->klass->image->delegate_invoke_cache, sig);
if (sig && method->klass->image->runtime_invoke_cache)
g_hash_table_remove (method->klass->image->runtime_invoke_cache, sig);
+ if (sig && method->klass->image->runtime_invoke_vtype_cache)
+ g_hash_table_remove (method->klass->image->runtime_invoke_vtype_cache, sig);
/*
* indexed by SignatureMethodPair
g_hash_table_foreach_remove (method->klass->image->delegate_abstract_invoke_cache,
signature_method_pair_matches_signature, (gpointer)sig);
- if (sig && method->klass->image->delegate_bound_static_invoke_cache)
- g_hash_table_foreach_remove (method->klass->image->delegate_bound_static_invoke_cache,
- signature_method_pair_matches_signature, (gpointer)sig);
-
/*
* indexed by MonoMethod pointers
*/