* Author:
* Paolo Molaro (lupus@ximian.com)
*
- * (C) 2002 Ximian, Inc. http://www.ximian.com
+ * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
*
*/
#include "config.h"
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+
#include "object.h"
#include "loader.h"
#include "cil-coff.h"
#include "mono/metadata/string-icalls.h"
#include "mono/metadata/attrdefs.h"
#include "mono/metadata/gc-internal.h"
+#include "mono/metadata/cominterop.h"
#include "mono/utils/mono-counters.h"
#include <string.h>
#include <errno.h>
MONO_MARSHAL_SERIALIZE /* Value needs to be serialized into the new domain */
} MonoXDomainMarshalType;
-typedef enum {
- MONO_COM_DEFAULT,
- MONO_COM_MS
-} MonoCOMProvider;
-
-static MonoCOMProvider com_provider = MONO_COM_DEFAULT;
-
enum {
#include "mono/cil/opcode.def"
LAST = 0xff
#define mono_marshal_lock() EnterCriticalSection (&marshal_mutex)
#define mono_marshal_unlock() LeaveCriticalSection (&marshal_mutex)
static CRITICAL_SECTION marshal_mutex;
-
-/* This mutex protects the various cominterop related caches in MonoImage */
-#define mono_cominterop_lock() EnterCriticalSection (&cominterop_mutex)
-#define mono_cominterop_unlock() LeaveCriticalSection (&cominterop_mutex)
-static CRITICAL_SECTION cominterop_mutex;
+static gboolean marshal_mutex_initialized;
static guint32 last_error_tls_id;
static guint32 load_type_info_tls_id;
static void
-delegate_hash_table_add (MonoDelegate *d);
+delegate_hash_table_add (MonoDelegate *d, MonoObject **target_loc);
static void
emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object);
static gpointer
mono_string_to_lpstr (MonoString *string_obj);
-static MonoString *
-mono_string_from_bstr (gpointer bstr);
-
-static void
-mono_free_bstr (gpointer bstr);
-
static MonoStringBuilder *
mono_string_utf8_to_builder2 (char *text);
static void
mono_marshal_set_last_error_windows (int error);
-static void
-mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethodPInvoke *piinfo, MonoMarshalSpec **mspecs, gpointer func, gboolean check_exceptions);
-
static void init_safe_handle (void);
+static void runtime_invoke_reset_abort (MonoException *ex);
+
/* MonoMethod pointers to SafeHandle::DangerousAddRef and ::DangerousRelease */
static MonoMethod *sh_dangerous_add_ref;
static MonoMethod *sh_dangerous_release;
int sigsize;
res = mono_metadata_signature_alloc (image, sig->param_count);
- sigsize = sizeof (MonoMethodSignature) + ((sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *));
+ sigsize = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
memcpy (res, sig, sigsize);
return res;
}
-static MonoMethodSignature*
-signature_no_pinvoke (MonoMethod *method)
+MonoMethodSignature*
+mono_signature_no_pinvoke (MonoMethod *method)
{
MonoMethodSignature *sig = mono_method_signature (method);
if (sig->pinvoke) {
return sig;
}
-/* Begin COM Interop related stuff until seperate file */
-
-
-/* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
-#ifdef PLATFORM_WIN32
-#define STDCALL __stdcall
-#else
-#define STDCALL
-#endif
-
-/* Upon creation of a CCW, only allocate a weak handle and set the
- * reference count to 0. If the unmanaged client code decides to addref and
- * hold onto the CCW, I then allocate a strong handle. Once the reference count
- * goes back to 0, convert back to a weak handle.
- */
-typedef struct {
- guint32 ref_count;
- guint32 gc_handle;
- GHashTable* vtable_hash;
-} MonoCCW;
-
-/* This type is the actual pointer passed to unmanaged code
- * to represent a COM interface.
- */
-typedef struct {
- gpointer vtable;
- MonoCCW* ccw;
-} MonoCCWInterface;
-
-#ifndef DISABLE_COM
-
-/* IUnknown */
-static int STDCALL cominterop_ccw_addref (MonoCCWInterface* ccwe);
-
-static int STDCALL cominterop_ccw_release (MonoCCWInterface* ccwe);
-
-static int STDCALL cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv);
-
-/* IDispatch */
-static int STDCALL cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo);
-
-static int STDCALL cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo);
-
-static int STDCALL cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
- gunichar2** rgszNames, guint32 cNames,
- guint32 lcid, gint32 *rgDispId);
-
-static int STDCALL cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
- gpointer riid, guint32 lcid,
- guint16 wFlags, gpointer pDispParams,
- gpointer pVarResult, gpointer pExcepInfo,
- guint32 *puArgErr);
-
-static MonoMethod *
-cominterop_get_managed_wrapper_adjusted (MonoMethod *method);
-
-static gpointer
-cominterop_get_ccw (MonoObject* object, MonoClass* itf);
-
-static MonoObject*
-cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify);
-
-/**
- * cominterop_method_signature:
- * @method: a method
- *
- * Returns: the corresponding unmanaged method signature for a managed COM
- * method.
- */
-static MonoMethodSignature*
-cominterop_method_signature (MonoMethod* method)
-{
- MonoMethodSignature *res;
- MonoImage *image = method->klass->image;
- MonoMethodSignature *sig = mono_method_signature (method);
- gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
- int sigsize;
- int i;
- int param_count = sig->param_count + 1; // convert this arg into IntPtr arg
-
- if (!preserve_sig &&!MONO_TYPE_IS_VOID (sig->ret))
- param_count++;
-
- res = mono_metadata_signature_alloc (image, param_count);
- sigsize = sizeof (MonoMethodSignature) + ((sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *));
- memcpy (res, sig, sigsize);
-
- // now move args forward one
- for (i = sig->param_count-1; i >= 0; i--)
- res->params[i+1] = sig->params[i];
-
- // first arg is interface pointer
- res->params[0] = &mono_defaults.int_class->byval_arg;
-
- if (preserve_sig) {
- res->ret = sig->ret;
- }
- else {
- // last arg is return type
- if (!MONO_TYPE_IS_VOID (sig->ret)) {
- res->params[param_count-1] = mono_metadata_type_dup (image->mempool, sig->ret);
- res->params[param_count-1]->byref = 1;
- res->params[param_count-1]->attrs = PARAM_ATTRIBUTE_OUT;
- }
-
- // return type is always int32 (HRESULT)
- res->ret = &mono_defaults.int32_class->byval_arg;
- }
-
- // no pinvoke
- res->pinvoke = FALSE;
-
- // no hasthis
- res->hasthis = 0;
-
- // set param_count
- res->param_count = param_count;
-
- // STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM
-#ifdef PLATFORM_WIN32
- res->call_convention = MONO_CALL_STDCALL;
-#else
- res->call_convention = MONO_CALL_C;
-#endif
-
- return res;
-}
-
-/**
- * cominterop_get_function_pointer:
- * @itf: a pointer to the COM interface
- * @slot: the vtable slot of the method pointer to return
- *
- * Returns: the unmanaged vtable function pointer from the interface
- */
-static gpointer
-cominterop_get_function_pointer (gpointer itf, int slot)
-{
- gpointer func;
- func = *((*(gpointer**)itf)+slot);
- return func;
-}
-
-/**
- * cominterop_object_is_com_object:
- * @obj: a pointer to the object
- *
- * Returns: a value indicating if the object is a
- * Runtime Callable Wrapper (RCW) for a COM object
- */
-static gboolean
-cominterop_object_is_rcw (MonoObject *obj)
-{
- MonoClass *klass = NULL;
- MonoRealProxy* real_proxy = NULL;
- if (!obj)
- return FALSE;
- klass = mono_object_class (obj);
- if (klass != mono_defaults.transparent_proxy_class)
- return FALSE;
-
- real_proxy = ((MonoTransparentProxy*)obj)->rp;
- if (!real_proxy)
- return FALSE;
-
- klass = mono_object_class (real_proxy);
- return (klass && klass == mono_defaults.com_interop_proxy_class);
-}
-
-static int
-cominterop_get_com_slot_begin (MonoClass* klass)
-{
- static MonoClass *interface_type_attribute = NULL;
- MonoCustomAttrInfo *cinfo = NULL;
- MonoInterfaceTypeAttribute* itf_attr = NULL;
-
- if (!interface_type_attribute)
- interface_type_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "InterfaceTypeAttribute");
- cinfo = mono_custom_attrs_from_class (klass);
- if (cinfo) {
- itf_attr = (MonoInterfaceTypeAttribute*)mono_custom_attrs_get_attr (cinfo, interface_type_attribute);
- if (!cinfo->cached)
- mono_custom_attrs_free (cinfo);
- }
-
- if (itf_attr && itf_attr->intType == 1)
- return 3; /* 3 methods in IUnknown*/
- else
- return 7; /* 7 methods in IDispatch*/
-}
-
-/**
- * cominterop_get_method_interface:
- * @method: method being called
- *
- * Returns: the MonoClass* representing the interface on which
- * the method is defined.
- */
-static MonoClass*
-cominterop_get_method_interface (MonoMethod* method)
-{
- MonoClass *ic = method->klass;
-
- /* if method is on a class, we need to look up interface method exists on */
- if (!MONO_CLASS_IS_INTERFACE(method->klass)) {
- GPtrArray *ifaces = mono_class_get_implemented_interfaces (method->klass);
- if (ifaces) {
- int i;
- for (i = 0; i < ifaces->len; ++i) {
- int offset;
- ic = g_ptr_array_index (ifaces, i);
- offset = mono_class_interface_offset (method->klass, ic);
- if (method->slot >= offset && method->slot < offset + ic->method.count)
- break;
- ic = NULL;
- }
- g_ptr_array_free (ifaces, TRUE);
- }
- }
-
- g_assert (ic);
- g_assert (MONO_CLASS_IS_INTERFACE (ic));
-
- return ic;
-}
-
-/**
- * cominterop_get_com_slot_for_method:
- * @method: a method
- *
- * Returns: the method's slot in the COM interface vtable
- */
-static int
-cominterop_get_com_slot_for_method (MonoMethod* method)
-{
- guint32 slot = method->slot;
- MonoClass *ic = method->klass;
-
- /* if method is on a class, we need to look up interface method exists on */
- if (!MONO_CLASS_IS_INTERFACE(ic)) {
- int offset = 0;
- ic = cominterop_get_method_interface (method);
- offset = mono_class_interface_offset (method->klass, ic);
- g_assert(offset >= 0);
- slot -= offset;
- }
-
- g_assert (ic);
- g_assert (MONO_CLASS_IS_INTERFACE (ic));
-
- return slot + cominterop_get_com_slot_begin (ic);
-}
-
-
-static void
-cominterop_mono_string_to_guid (const MonoString* string, guint8 *guid);
-
-static gboolean
-cominterop_class_guid (MonoClass* klass, guint8* guid)
-{
- static MonoClass *GuidAttribute = NULL;
- MonoCustomAttrInfo *cinfo;
-
- /* Handle the GuidAttribute */
- if (!GuidAttribute)
- GuidAttribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "GuidAttribute");
-
- cinfo = mono_custom_attrs_from_class (klass);
- if (cinfo) {
- MonoReflectionGuidAttribute *attr = (MonoReflectionGuidAttribute*)mono_custom_attrs_get_attr (cinfo, GuidAttribute);
-
- if (!attr)
- return FALSE;
- if (!cinfo->cached)
- mono_custom_attrs_free (cinfo);
-
- cominterop_mono_string_to_guid (attr->guid, guid);
- return TRUE;
- }
- return FALSE;
-}
-
-/**
- * cominterop_get_interface:
- * @obj: managed wrapper object containing COM object
- * @ic: interface type to retrieve for COM object
- *
- * Returns: the COM interface requested
- */
-static gpointer
-cominterop_get_interface (MonoComObject* obj, MonoClass* ic, gboolean throw_exception)
-{
- gpointer itf = NULL;
-
- g_assert (ic);
- g_assert (MONO_CLASS_IS_INTERFACE (ic));
-
- mono_cominterop_lock ();
- if (obj->itf_hash)
- itf = g_hash_table_lookup (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id));
- mono_cominterop_unlock ();
-
- if (!itf) {
- guint8 iid [16];
- int found = cominterop_class_guid (ic, iid);
- int hr;
- g_assert(found);
- hr = ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (obj->iunknown, iid, &itf);
- if (hr < 0 && throw_exception) {
- static MonoMethod* throw_exception_for_hr = NULL;
- MonoException* ex;
- void* params[1] = {&hr};
- if (!throw_exception_for_hr)
- throw_exception_for_hr = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetExceptionForHR", 1);
- ex = (MonoException*)mono_runtime_invoke (throw_exception_for_hr, NULL, params, NULL);
- mono_raise_exception (ex);
- }
-
- if (hr >= 0 && itf) {
- mono_cominterop_lock ();
- if (!obj->itf_hash)
- obj->itf_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
- g_hash_table_insert (obj->itf_hash, GUINT_TO_POINTER ((guint)ic->interface_id), itf);
- mono_cominterop_unlock ();
- }
-
- }
- if (throw_exception)
- g_assert (itf);
-
- return itf;
-}
-
-static int
-cominterop_get_hresult_for_exception (MonoException* exc)
-{
- int hr = 0;
- return hr;
-}
-
-#endif /* DISABLE_COM */
-
void
mono_marshal_init (void)
{
static gboolean module_initialized = FALSE;
if (!module_initialized) {
- char* com_provider_env = NULL;
module_initialized = TRUE;
InitializeCriticalSection (&marshal_mutex);
- InitializeCriticalSection (&cominterop_mutex);
+ marshal_mutex_initialized = TRUE;
last_error_tls_id = TlsAlloc ();
load_type_info_tls_id = TlsAlloc ();
- com_provider_env = getenv ("MONO_COM");
- if (com_provider_env && !strcmp(com_provider_env, "MS"))
- com_provider = MONO_COM_MS;
-
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_marshal_string_to_utf16_copy, "mono_marshal_string_to_utf16_copy", "ptr obj", FALSE);
register_icall (mono_string_new_wrapper, "mono_string_new_wrapper", "obj ptr", FALSE);
register_icall (mono_string_to_utf8, "mono_string_to_utf8", "ptr obj", FALSE);
register_icall (mono_string_to_lpstr, "mono_string_to_lpstr", "ptr obj", FALSE);
- register_icall (mono_string_to_bstr, "mono_string_to_bstr", "ptr obj", FALSE);
- register_icall (mono_string_from_bstr, "mono_string_from_bstr", "obj ptr", FALSE);
- register_icall (mono_free_bstr, "mono_free_bstr", "void ptr", FALSE);
register_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", "ptr object", FALSE);
register_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", "ptr object", FALSE);
register_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", "ptr object", FALSE);
register_icall (mono_array_to_savearray, "mono_array_to_savearray", "ptr object", 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_array_to_byvalarray, "mono_array_to_byvalarray", "void ptr object ptr int32", FALSE);
register_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", "ptr object", FALSE);
register_icall (mono_context_set, "mono_context_set", "void object", FALSE);
register_icall (mono_upgrade_remote_class_wrapper, "mono_upgrade_remote_class_wrapper", "void object object", FALSE);
register_icall (type_from_handle, "type_from_handle", "object ptr", FALSE);
- register_icall (mono_gc_wbarrier_generic_store, "wb_generic", "void ptr object", FALSE);
-#ifndef DISABLE_COM
- register_icall (cominterop_get_method_interface, "cominterop_get_method_interface", "ptr ptr", FALSE);
- register_icall (cominterop_get_function_pointer, "cominterop_get_function_pointer", "ptr ptr int32", FALSE);
- register_icall (cominterop_object_is_rcw, "cominterop_object_is_rcw", "int32 object", FALSE);
- register_icall (cominterop_get_ccw, "cominterop_get_ccw", "ptr object ptr", FALSE);
- register_icall (cominterop_get_ccw_object, "cominterop_get_ccw_object", "object ptr int32", FALSE);
- register_icall (cominterop_get_hresult_for_exception, "cominterop_get_hresult_for_exception", "int32 object", FALSE);
- register_icall (cominterop_get_interface, "cominterop_get_interface", "ptr object ptr int32", FALSE);
-#endif /* DISABLE_COM */
+ register_icall (mono_gc_wbarrier_generic_nostore, "wb_generic", "void ptr", FALSE);
+ register_icall (runtime_invoke_reset_abort, "runtime_invoke_reset_abort", "void object", FALSE);
+
+ mono_cominterop_init ();
}
}
void
mono_marshal_cleanup (void)
{
+ mono_cominterop_cleanup ();
+
TlsFree (load_type_info_tls_id);
TlsFree (last_error_tls_id);
DeleteCriticalSection (&marshal_mutex);
- DeleteCriticalSection (&cominterop_mutex);
+ marshal_mutex_initialized = FALSE;
}
MonoClass *byte_array_class;
{
MonoMethod *method, *wrapper;
MonoClass *klass;
+ MonoObject **target_loc;
if (!delegate)
return NULL;
method = delegate->method;
- wrapper = mono_marshal_get_managed_wrapper (method, klass, delegate->target);
+ if (mono_method_signature (method)->pinvoke) {
+ const char *exc_class, *exc_arg;
+ gpointer ftnptr;
+
+ ftnptr = mono_lookup_pinvoke_call (method, &exc_class, &exc_arg);
+ if (!ftnptr) {
+ g_assert (exc_class);
+ mono_raise_exception (mono_exception_from_name_msg (mono_defaults.corlib, "System", exc_class, exc_arg));
+ }
+ return ftnptr;
+ }
+
+ if (delegate->target) {
+ /* Produce a location which can be embedded in JITted code */
+ target_loc = mono_gc_alloc_fixed (sizeof (MonoObject*), NULL);
+ *target_loc = delegate->target;
+ } else {
+ target_loc = NULL;
+ }
+
+ wrapper = mono_marshal_get_managed_wrapper (method, klass, target_loc);
- delegate->delegate_trampoline = mono_compile_method (wrapper);
+ delegate->delegate_trampoline = mono_compile_method (wrapper);
// Add the delegate to the delegate hash table
- delegate_hash_table_add (delegate);
+ delegate_hash_table_add (delegate, target_loc);
/* when the object is collected, collect the dynamic method, too */
mono_object_register_finalizer ((MonoObject*)delegate);
* object pointer itself, otherwise we use a GC handle.
*/
static GHashTable *delegate_hash_table;
+/* Contains root locations pointing to the this arguments of delegates */
+static MonoGHashTable *delegate_target_locations;
static GHashTable *
delegate_hash_table_new (void) {
static void
delegate_hash_table_remove (MonoDelegate *d)
{
+ MonoObject **target_loc;
#ifdef HAVE_MOVING_COLLECTOR
guint32 gchandle;
#endif
gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, d->delegate_trampoline));
#endif
g_hash_table_remove (delegate_hash_table, d->delegate_trampoline);
+ if (delegate_target_locations)
+ target_loc = mono_g_hash_table_lookup (delegate_target_locations, d->delegate_trampoline);
+ else
+ target_loc = NULL;
+ if (target_loc)
+ mono_g_hash_table_remove (delegate_target_locations, d->delegate_trampoline);
mono_marshal_unlock ();
+ if (target_loc) {
+ mono_gc_free_fixed (target_loc);
+ }
#ifdef HAVE_MOVING_COLLECTOR
mono_gchandle_free (gchandle);
#endif
}
static void
-delegate_hash_table_add (MonoDelegate *d)
+delegate_hash_table_add (MonoDelegate *d, MonoObject **target_loc)
{
#ifdef HAVE_MOVING_COLLECTOR
guint32 gchandle = mono_gchandle_new_weakref ((MonoObject*)d, FALSE);
+ guint32 old_gchandle;
#endif
mono_marshal_lock ();
if (delegate_hash_table == NULL)
delegate_hash_table = delegate_hash_table_new ();
+ if (delegate_target_locations == NULL) {
+ /* Has to be conservative as the values are not object references */
+ delegate_target_locations = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_CONSERVATIVE_GC);
+ MONO_GC_REGISTER_ROOT (delegate_target_locations);
+ }
#ifdef HAVE_MOVING_COLLECTOR
+ old_gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table, d->delegate_trampoline));
g_hash_table_insert (delegate_hash_table, d->delegate_trampoline, GUINT_TO_POINTER (gchandle));
+ if (old_gchandle)
+ mono_gchandle_free (old_gchandle);
#else
g_hash_table_insert (delegate_hash_table, d->delegate_trampoline, d);
#endif
+ if (target_loc)
+ /* This keeps target_loc alive for Boehm */
+ mono_g_hash_table_insert (delegate_target_locations, d->delegate_trampoline, target_loc);
mono_marshal_unlock ();
}
#endif
MonoDelegate *d;
+ if (ftn == NULL)
+ return NULL;
+
mono_marshal_lock ();
if (delegate_hash_table == NULL)
delegate_hash_table = delegate_hash_table_new ();
gpointer
mono_array_to_lparray (MonoArray *array)
{
+ gpointer *nativeArray = NULL;
+ int nativeArraySize = 0;
+
+ int i = 0;
+ MonoClass *klass;
+
if (!array)
return NULL;
- /* fixme: maybe we need to make a copy */
+ klass = array->obj.vtable->klass;
+
+ switch (klass->element_class->byval_arg.type) {
+ case MONO_TYPE_VOID:
+ g_assert_not_reached ();
+ break;
+ 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;
+ case MONO_TYPE_U1:
+ case MONO_TYPE_BOOLEAN:
+ case MONO_TYPE_I1:
+ case MONO_TYPE_U2:
+ case MONO_TYPE_CHAR:
+ case MONO_TYPE_I2:
+ case MONO_TYPE_I:
+ case MONO_TYPE_U:
+ case MONO_TYPE_I4:
+ case MONO_TYPE_U4:
+ case MONO_TYPE_U8:
+ case MONO_TYPE_I8:
+ case MONO_TYPE_R4:
+ case MONO_TYPE_R8:
+ case MONO_TYPE_VALUETYPE:
+ /* nothing to do */
+ break;
+ case MONO_TYPE_GENERICINST:
+ case MONO_TYPE_PTR:
+ case MONO_TYPE_OBJECT:
+ case MONO_TYPE_ARRAY:
+ case MONO_TYPE_SZARRAY:
+ case MONO_TYPE_STRING:
+ default:
+ g_warning ("type 0x%x not handled", klass->element_class->byval_arg.type);
+ g_assert_not_reached ();
+ }
+
return array->vector;
}
+void
+mono_free_lparray (MonoArray *array, gpointer* nativeArray)
+{
+ MonoClass *klass;
+ int i = 0;
+
+ if (!array)
+ return;
+
+ if (!nativeArray)
+ return;
+
+ klass = array->obj.vtable->klass;
+
+ switch (klass->element_class->byval_arg.type) {
+ case MONO_TYPE_CLASS:
+ for(i = 0; i < array->max_length; ++i)
+ mono_marshal_free_ccw(nativeArray[i]);
+ free(nativeArray);
+ break;
+ }
+}
+
static void
mono_byvalarray_to_array (MonoArray *arr, gpointer native_arr, MonoClass *elclass, guint32 elnum)
{
mono_string_builder_to_utf8 (MonoStringBuilder *sb)
{
GError *error = NULL;
- glong *res;
- gchar *tmp;
+ gchar *tmp, *res = NULL;
if (!sb)
return NULL;
sb->cached_str = NULL;
}
- res = mono_marshal_alloc (mono_stringbuilder_capacity (sb) + 1);
-
- tmp = g_utf16_to_utf8 (mono_string_chars (sb->str), sb->length, NULL, res, &error);
+ tmp = g_utf16_to_utf8 (mono_string_chars (sb->str), sb->length, NULL, NULL, &error);
if (error) {
g_error_free (error);
- mono_marshal_free (res);
mono_raise_exception (mono_get_exception_execution_engine ("Failed to convert StringBuilder from utf16 to utf8"));
} else {
+ res = mono_marshal_alloc (mono_stringbuilder_capacity (sb) + 1);
memcpy (res, tmp, sb->length + 1);
g_free (tmp);
}
return NULL;
}
-typedef gpointer (*SysAllocStringLenFunc)(gunichar* str, guint32 len);
-typedef guint32 (*SysStringLenFunc)(gpointer bstr);
-typedef void (*SysFreeStringFunc)(gunichar* str);
-
-static SysAllocStringLenFunc sys_alloc_string_len_ms = NULL;
-static SysStringLenFunc sys_string_len_ms = NULL;
-static SysFreeStringFunc sys_free_string_ms = NULL;
-
-static gboolean
-init_com_provider_ms (void)
+/**
+ * mono_string_to_byvalstr:
+ * @dst: Where to store the null-terminated utf8 decoded string.
+ * @src: the MonoString to copy.
+ * @size: the maximum number of bytes to copy.
+ *
+ * Copies the MonoString pointed to by @src as a utf8 string
+ * into @dst, it copies at most @size bytes into the destination.
+ */
+void
+mono_string_to_byvalstr (gpointer dst, MonoString *src, int size)
{
- static gboolean initialized = FALSE;
- char *error_msg;
- MonoDl *module = NULL;
- const char* scope = "liboleaut32.so";
-
- if (initialized)
- return TRUE;
+ char *s;
+ int len;
- module = mono_dl_open(scope, MONO_DL_LAZY, &error_msg);
- if (error_msg) {
- g_warning ("Error loading COM support library '%s': %s", scope, error_msg);
- g_assert_not_reached ();
- return FALSE;
- }
- error_msg = mono_dl_symbol (module, "SysAllocStringLen", (gpointer*)&sys_alloc_string_len_ms);
- if (error_msg) {
- g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysAllocStringLen", scope, error_msg);
- g_assert_not_reached ();
- return FALSE;
- }
+ g_assert (dst != NULL);
+ g_assert (size > 0);
- error_msg = mono_dl_symbol (module, "SysStringLen", (gpointer*)&sys_string_len_ms);
- if (error_msg) {
- g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysStringLen", scope, error_msg);
- g_assert_not_reached ();
- return FALSE;
- }
-
- error_msg = mono_dl_symbol (module, "SysFreeString", (gpointer*)&sys_free_string_ms);
- if (error_msg) {
- g_warning ("Error loading entry point '%s' in COM support library '%s': %s", "SysFreeString", scope, error_msg);
- g_assert_not_reached ();
- return FALSE;
- }
-
- initialized = TRUE;
- return TRUE;
-}
-
-gpointer
-mono_string_to_bstr (MonoString *string_obj)
-{
- if (!string_obj)
- return NULL;
-#ifdef PLATFORM_WIN32
- return SysAllocStringLen (mono_string_chars (string_obj), mono_string_length (string_obj));
-#else
- if (com_provider == MONO_COM_DEFAULT) {
- int slen = mono_string_length (string_obj);
- /* allocate len + 1 utf16 characters plus 4 byte integer for length*/
- char *ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
- if (ret == NULL)
- return NULL;
- memcpy (ret + sizeof(guint32), mono_string_chars (string_obj), slen * sizeof(gunichar2));
- * ((guint32 *) ret) = slen * sizeof(gunichar2);
- ret [4 + slen * sizeof(gunichar2)] = 0;
- ret [5 + slen * sizeof(gunichar2)] = 0;
-
- return ret + 4;
- } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
- gpointer ret = NULL;
- gunichar* str = NULL;
- guint32 len;
- len = mono_string_length (string_obj);
- str = g_utf16_to_ucs4 (mono_string_chars (string_obj), len,
- NULL, NULL, NULL);
- ret = sys_alloc_string_len_ms (str, len);
- g_free(str);
- return ret;
- } else {
- g_assert_not_reached ();
- }
-#endif
-}
-
-MonoString *
-mono_string_from_bstr (gpointer bstr)
-{
- if (!bstr)
- return NULL;
-#ifdef PLATFORM_WIN32
- return mono_string_new_utf16 (mono_domain_get (), bstr, SysStringLen (bstr));
-#else
- if (com_provider == MONO_COM_DEFAULT) {
- return mono_string_new_utf16 (mono_domain_get (), bstr, *((guint32 *)bstr - 1) / sizeof(gunichar2));
- } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
- MonoString* str = NULL;
- glong written = 0;
- gunichar2* utf16 = NULL;
-
- utf16 = g_ucs4_to_utf16 (bstr, sys_string_len_ms (bstr), NULL, &written, NULL);
- str = mono_string_new_utf16 (mono_domain_get (), utf16, written);
- g_free (utf16);
- return str;
- } else {
- g_assert_not_reached ();
- }
-
-#endif
-}
-
-void
-mono_free_bstr (gpointer bstr)
-{
- if (!bstr)
- return;
-#ifdef PLATFORM_WIN32
- SysFreeString ((BSTR)bstr);
-#else
- if (com_provider == MONO_COM_DEFAULT) {
- g_free (((char *)bstr) - 4);
- } else if (com_provider == MONO_COM_MS && init_com_provider_ms ()) {
- sys_free_string_ms (bstr);
- } else {
- g_assert_not_reached ();
- }
-
-#endif
-}
-
-/**
- * mono_string_to_byvalstr:
- * @dst: Where to store the null-terminated utf8 decoded string.
- * @src: the MonoString to copy.
- * @size: the maximum number of bytes to copy.
- *
- * Copies the MonoString pointed to by @src as a utf8 string
- * into @dst, it copies at most @size bytes into the destination.
- */
-void
-mono_string_to_byvalstr (gpointer dst, MonoString *src, int size)
-{
- char *s;
- int len;
-
- g_assert (dst != NULL);
- g_assert (size > 0);
-
- memset (dst, 0, size);
- if (!src)
- return;
+ memset (dst, 0, size);
+ if (!src)
+ return;
s = mono_string_to_utf8 (src);
len = MIN (size, strlen (s));
return mono_mb_emit_branch (mb, branch_code);
}
-#ifndef DISABLE_COM
-
-static void
-mono_mb_emit_cominterop_call (MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethod* method)
-{
- // get function pointer from 1st arg, the COM interface pointer
- mono_mb_emit_ldarg (mb, 0);
- mono_mb_emit_icon (mb, cominterop_get_com_slot_for_method (method));
- mono_mb_emit_icall (mb, cominterop_get_function_pointer);
-
- mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
- mono_mb_emit_byte (mb, CEE_MONO_SAVE_LMF);
- mono_mb_emit_calli (mb, sig);
- mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
- mono_mb_emit_byte (mb, CEE_MONO_RESTORE_LMF);
-}
-
-#endif /* DISABLE_COM */
-
static void
mono_mb_emit_exception_marshal_directive (MonoMethodBuilder *mb, const char *msg)
{
return CEE_LDIND_R8;
case MONO_TYPE_VALUETYPE:
if (type->data.klass->enumtype) {
- type = type->data.klass->enum_basetype;
+ type = mono_class_enum_basetype (type->data.klass);
goto handle_enum;
}
return CEE_LDOBJ;
return CEE_STIND_R8;
case MONO_TYPE_VALUETYPE:
if (type->data.klass->enumtype) {
- type = type->data.klass->enum_basetype;
+ type = mono_class_enum_basetype (type->data.klass);
goto handle_enum;
}
return CEE_STOBJ;
#ifndef DISABLE_COM
case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN:
- case MONO_MARSHAL_CONV_OBJECT_IDISPATCH: {
- static MonoClass* com_interop_proxy_class = NULL;
- static MonoMethod* com_interop_proxy_get_proxy = NULL;
- static MonoMethod* get_transparent_proxy = NULL;
- int real_proxy;
- guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
- MonoClass *klass = NULL;
-
- /* COM types are initialized lazily */
- mono_init_com_types ();
-
- klass = mono_class_from_mono_type (type);
-
- mono_mb_emit_ldloc (mb, 1);
- mono_mb_emit_byte (mb, CEE_LDNULL);
- mono_mb_emit_byte (mb, CEE_STIND_REF);
-
- mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_byte (mb, CEE_LDIND_I);
- pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- /* load dst to store later */
- mono_mb_emit_ldloc (mb, 1);
-
- mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_byte (mb, CEE_LDIND_I);
- mono_mb_emit_icon (mb, TRUE);
- mono_mb_emit_icall (mb, cominterop_get_ccw_object);
- pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
- if (!com_interop_proxy_class)
- com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
- if (!com_interop_proxy_get_proxy)
- com_interop_proxy_get_proxy = mono_class_get_method_from_name_flags (com_interop_proxy_class, "GetProxy", 2, METHOD_ATTRIBUTE_PRIVATE);
- if (!get_transparent_proxy)
- get_transparent_proxy = mono_class_get_method_from_name (mono_defaults.real_proxy_class, "GetTransparentProxy", 0);
-
- real_proxy = mono_mb_add_local (mb, &com_interop_proxy_class->byval_arg);
-
- mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_byte (mb, CEE_LDIND_I);
- mono_mb_emit_ptr (mb, &mono_defaults.com_object_class->byval_arg);
- mono_mb_emit_icall (mb, type_from_handle);
- mono_mb_emit_managed_call (mb, com_interop_proxy_get_proxy, NULL);
- mono_mb_emit_managed_call (mb, get_transparent_proxy, NULL);
- if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
- g_assert (klass);
- mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
- }
- mono_mb_emit_byte (mb, CEE_STIND_REF);
- pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
-
- /* is already managed object */
- mono_mb_patch_short_branch (mb, pos_ccw);
- mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_byte (mb, CEE_LDIND_I);
- mono_mb_emit_icon (mb, TRUE);
- mono_mb_emit_icall (mb, cominterop_get_ccw_object);
-
- if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
- g_assert (klass);
- mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
- }
- mono_mb_emit_byte (mb, CEE_STIND_REF);
-
- mono_mb_patch_short_branch (mb, pos_end);
- /* case if null */
- mono_mb_patch_short_branch (mb, pos_null);
+ case MONO_MARSHAL_CONV_OBJECT_IDISPATCH:
+ mono_cominterop_emit_ptr_to_object_conv (mb, type, conv, mspec);
break;
- }
#endif /* DISABLE_COM */
case MONO_MARSHAL_CONV_SAFEHANDLE: {
return mono_array_to_savearray;
case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
return mono_array_to_lparray;
+ case MONO_MARSHAL_FREE_LPARRAY:
+ return mono_free_lparray;
case MONO_MARSHAL_CONV_DEL_FTN:
return mono_delegate_to_ftnptr;
case MONO_MARSHAL_CONV_FTN_DEL:
#ifndef DISABLE_COM
case MONO_MARSHAL_CONV_OBJECT_INTERFACE:
case MONO_MARSHAL_CONV_OBJECT_IDISPATCH:
- case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN: {
- guint32 pos_null = 0, pos_rcw = 0, pos_end = 0;
-
- /* COM types are initialized lazily */
- mono_init_com_types ();
-
-
- mono_mb_emit_ldloc (mb, 1);
- mono_mb_emit_icon (mb, 0);
- mono_mb_emit_byte (mb, CEE_CONV_U);
- mono_mb_emit_byte (mb, CEE_STIND_I);
-
- mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
- // if null just break, dst was already inited to 0
- pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
- mono_mb_emit_icall (mb, cominterop_object_is_rcw);
- pos_rcw = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- // load dst to store later
- mono_mb_emit_ldloc (mb, 1);
-
- // load src
- mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
- mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
- /* load the RCW from the ComInteropProxy*/
- mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoComInteropProxy, com_object));
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
- if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE) {
- mono_mb_emit_ptr (mb, mono_type_get_class (type));
- mono_mb_emit_icon (mb, TRUE);
- mono_mb_emit_icall (mb, cominterop_get_interface);
-
- }
- else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN) {
- static MonoProperty* iunknown = NULL;
-
- if (!iunknown)
- iunknown = mono_class_get_property_from_name (mono_defaults.com_object_class, "IUnknown");
- mono_mb_emit_managed_call (mb, iunknown->get, NULL);
- }
- else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH) {
- static MonoProperty* idispatch = NULL;
-
- if (!idispatch)
- idispatch = mono_class_get_property_from_name (mono_defaults.com_object_class, "IDispatch");
- mono_mb_emit_managed_call (mb, idispatch->get, NULL);
- }
- else {
- g_assert_not_reached ();
- }
- mono_mb_emit_byte (mb, CEE_STIND_I);
- pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
-
- // if not rcw
- mono_mb_patch_short_branch (mb, pos_rcw);
- /* load dst to store later */
- mono_mb_emit_ldloc (mb, 1);
- /* load src */
- mono_mb_emit_ldloc (mb, 0);
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
- if (conv == MONO_MARSHAL_CONV_OBJECT_INTERFACE)
- mono_mb_emit_ptr (mb, mono_type_get_class (type));
- else if (conv == MONO_MARSHAL_CONV_OBJECT_IUNKNOWN)
- mono_mb_emit_ptr (mb, mono_defaults.iunknown_class);
- else if (conv == MONO_MARSHAL_CONV_OBJECT_IDISPATCH)
- mono_mb_emit_ptr (mb, mono_defaults.idispatch_class);
- else
- g_assert_not_reached ();
- mono_mb_emit_icall (mb, cominterop_get_ccw);
- mono_mb_emit_byte (mb, CEE_STIND_I);
-
- mono_mb_patch_short_branch (mb, pos_end);
- mono_mb_patch_short_branch (mb, pos_null);
+ case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN:
+ mono_cominterop_emit_object_to_ptr_conv (mb, type, conv, mspec);
break;
- }
#endif /* DISABLE_COM */
case MONO_MARSHAL_CONV_SAFEHANDLE: {
int src_var, dst_var;
if (ftype->data.klass->enumtype) {
- ftype = ftype->data.klass->enum_basetype;
+ ftype = mono_class_enum_basetype (ftype->data.klass);
goto handle_enum;
}
emit_thread_interrupt_checkpoint_call (mb, mono_thread_force_interruption_checkpoint);
}
+void
+mono_marshal_emit_thread_interrupt_checkpoint (MonoMethodBuilder *mb)
+{
+ emit_thread_interrupt_checkpoint (mb);
+}
+
static MonoAsyncResult *
mono_delegate_begin_invoke (MonoDelegate *delegate, gpointer *params)
{
return *var;
}
-static inline MonoMethod*
+GHashTable*
+mono_marshal_get_cache (GHashTable **var, GHashFunc hash_func, GCompareFunc equal_func)
+{
+ return get_cache (var, hash_func, equal_func);
+}
+
+MonoMethod*
mono_marshal_find_in_cache (GHashTable *cache, gpointer key)
{
MonoMethod *res;
}
/* Create the method from the builder and place it in the cache */
-static inline MonoMethod*
+MonoMethod*
mono_mb_create_and_cache (GHashTable *cache, gpointer key,
MonoMethodBuilder *mb, MonoMethodSignature *sig,
int max_stack)
g_assert (method && method->klass->parent == mono_defaults.multicastdelegate_class &&
!strcmp (method->name, "BeginInvoke"));
- sig = signature_no_pinvoke (method);
+ sig = mono_signature_no_pinvoke (method);
cache = get_cache (&method->klass->image->delegate_begin_invoke_cache,
(GHashFunc)mono_signature_hash,
method = mono_class_get_method_from_name (klass, "EndInvoke", -1);
g_assert (method != NULL);
- sig = signature_no_pinvoke (method);
+ sig = mono_signature_no_pinvoke (method);
msg = mono_method_call_message_new (method, params, NULL, NULL, NULL);
g_assert (method && method->klass->parent == mono_defaults.multicastdelegate_class &&
!strcmp (method->name, "EndInvoke"));
- sig = signature_no_pinvoke (method);
+ sig = mono_signature_no_pinvoke (method);
cache = get_cache (&method->klass->image->delegate_end_invoke_cache,
(GHashFunc)mono_signature_hash,
return res;
}
-#ifndef DISABLE_COM
-
-/**
- * cominterop_get_native_wrapper_adjusted:
- * @method: managed COM Interop method
- *
- * Returns: the generated method to call with signature matching
- * the unmanaged COM Method signature
- */
-static MonoMethod *
-cominterop_get_native_wrapper_adjusted (MonoMethod *method)
+MonoMethod *
+mono_marshal_get_remoting_invoke (MonoMethod *method)
{
+ MonoMethodSignature *sig;
+ MonoMethodBuilder *mb;
MonoMethod *res;
- MonoMethodBuilder *mb_native;
- MonoMarshalSpec **mspecs;
- MonoMethodSignature *sig, *sig_native;
- MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *) method;
- int i;
+ int params_var;
- sig = mono_method_signature (method);
+ g_assert (method);
- // create unmanaged wrapper
- mb_native = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_NATIVE);
- sig_native = cominterop_method_signature (method);
+ if (method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE || method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE)
+ return method;
- mspecs = g_new (MonoMarshalSpec*, sig_native->param_count+1);
- memset (mspecs, 0, sizeof(MonoMarshalSpec*)*(sig_native->param_count+1));
+ /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
+ if ((method->klass->is_com_object || method->klass == mono_defaults.com_object_class) && !mono_class_vtable (mono_domain_get (), method->klass)->remote) {
+#ifndef DISABLE_COM
+ return mono_cominterop_get_invoke (method);
+#else
+ g_assert_not_reached ();
+#endif
+ }
- mono_method_get_marshal_info (method, mspecs);
+ sig = mono_signature_no_pinvoke (method);
- // move managed args up one
- for (i = sig->param_count; i >= 1; i--)
- mspecs[i+1] = mspecs[i];
+ /* we cant remote methods without this pointer */
+ if (!sig->hasthis)
+ return method;
- // first arg is IntPtr for interface
- mspecs[1] = NULL;
+ if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_REMOTING_INVOKE)))
+ return res;
- if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG)) {
- // move return spec to last param
- if (!MONO_TYPE_IS_VOID (sig->ret))
- mspecs[sig_native->param_count] = mspecs[0];
+ mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE);
+ mb->method->save_lmf = 1;
- mspecs[0] = NULL;
- }
+ params_var = mono_mb_emit_save_args (mb, sig, TRUE);
- for (i = 1; i < sig_native->param_count; i++) {
- int mspec_index = i + 1;
- if (mspecs[mspec_index] == NULL) {
- // default object to VARIANT
- if (sig_native->params[i]->type == MONO_TYPE_OBJECT) {
- mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
- mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
- }
- else if (sig_native->params[i]->type == MONO_TYPE_STRING) {
- mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
- mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
- }
- else if (sig_native->params[i]->type == MONO_TYPE_CLASS) {
- mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
- mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
- }
- }
- }
+ mono_mb_emit_ptr (mb, method);
+ mono_mb_emit_ldloc (mb, params_var);
+ mono_mb_emit_icall (mb, mono_remoting_wrapper);
+ emit_thread_interrupt_checkpoint (mb);
- if (method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG) {
- // move return spec to last param
- if (!MONO_TYPE_IS_VOID (sig->ret) && mspecs[0] == NULL) {
- // default object to VARIANT
- if (sig->ret->type == MONO_TYPE_OBJECT) {
- mspecs[0] = g_new0 (MonoMarshalSpec, 1);
- mspecs[0]->native = MONO_NATIVE_STRUCT;
- }
- else if (sig->ret->type == MONO_TYPE_STRING) {
- mspecs[0] = g_new0 (MonoMarshalSpec, 1);
- mspecs[0]->native = MONO_NATIVE_BSTR;
- }
- else if (sig->ret->type == MONO_TYPE_CLASS) {
- mspecs[0] = g_new0 (MonoMarshalSpec, 1);
- mspecs[0]->native = MONO_NATIVE_INTERFACE;
- }
- }
+ if (sig->ret->type == MONO_TYPE_VOID) {
+ mono_mb_emit_byte (mb, CEE_POP);
+ mono_mb_emit_byte (mb, CEE_RET);
+ } else {
+ mono_mb_emit_restore_result (mb, sig->ret);
}
- mono_marshal_emit_native_wrapper (method->klass->image, mb_native, sig_native, piinfo, mspecs, piinfo->addr, TRUE);
-
- res = mono_mb_create_method (mb_native, sig_native, sig_native->param_count + 16);
-
- mono_mb_free (mb_native);
-
- for (i = sig_native->param_count; i >= 0; i--)
- if (mspecs [i])
- mono_metadata_free_marshal_spec (mspecs [i]);
- g_free (mspecs);
+ res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16);
+ mono_mb_free (mb);
return res;
}
-/**
- * cominterop_get_native_wrapper:
- * @method: managed method
- *
- * Returns: the generated method to call
+/* mono_get_xdomain_marshal_type()
+ * Returns the kind of marshalling that a type needs for cross domain calls.
*/
-static MonoMethod *
-cominterop_get_native_wrapper (MonoMethod *method)
-{
- MonoMethod *res;
- GHashTable *cache;
- MonoMethodBuilder *mb;
- MonoMethodSignature *sig, *csig;
-
- g_assert (method);
-
- cache = get_cache (&method->klass->image->cominterop_wrapper_cache, mono_aligned_addr_hash, NULL);
- if ((res = mono_marshal_find_in_cache (cache, method)))
- return res;
-
- mono_init_com_types ();
-
- if (!method->klass->vtable)
- mono_class_setup_vtable (method->klass);
-
- if (!method->klass->methods)
- mono_class_setup_methods (method->klass);
-
- sig = mono_method_signature (method);
- mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);
-
- /* if method klass is import, that means method
- * is really a com call. let interop system emit it.
- */
- if (MONO_CLASS_IS_IMPORT(method->klass)) {
- /* FIXME: we have to call actual class .ctor
- * instead of just __ComObject .ctor.
- */
- if (!strcmp(method->name, ".ctor")) {
- static MonoMethod *ctor = NULL;
-
- if (!ctor)
- ctor = mono_class_get_method_from_name (mono_defaults.com_object_class, ".ctor", 0);
- mono_mb_emit_ldarg (mb, 0);
- mono_mb_emit_managed_call (mb, ctor, NULL);
- mono_mb_emit_byte (mb, CEE_RET);
- }
- else {
- static MonoMethod * ThrowExceptionForHR = NULL;
- MonoMethod *adjusted_method;
- int retval = 0;
- int ptr_this;
- int i;
- gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-
- // add local variables
- ptr_this = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
- if (!MONO_TYPE_IS_VOID (sig->ret))
- retval = mono_mb_add_local (mb, sig->ret);
-
- // get the type for the interface the method is defined on
- // and then get the underlying COM interface for that type
- mono_mb_emit_ldarg (mb, 0);
- mono_mb_emit_ptr (mb, method);
- mono_mb_emit_icall (mb, cominterop_get_method_interface);
- mono_mb_emit_icon (mb, TRUE);
- mono_mb_emit_icall (mb, cominterop_get_interface);
- mono_mb_emit_stloc (mb, ptr_this);
-
- // arg 1 is unmanaged this pointer
- mono_mb_emit_ldloc (mb, ptr_this);
-
- // load args
- for (i = 1; i <= sig->param_count; i++)
- mono_mb_emit_ldarg (mb, i);
-
- // push managed return value as byref last argument
- if (!MONO_TYPE_IS_VOID (sig->ret) && !preserve_sig)
- mono_mb_emit_ldloc_addr (mb, retval);
-
- adjusted_method = cominterop_get_native_wrapper_adjusted (method);
- mono_mb_emit_managed_call (mb, adjusted_method, NULL);
-
- if (!preserve_sig) {
- if (!ThrowExceptionForHR)
- ThrowExceptionForHR = mono_class_get_method_from_name (mono_defaults.marshal_class, "ThrowExceptionForHR", 1);
- mono_mb_emit_managed_call (mb, ThrowExceptionForHR, NULL);
-
- // load return value managed is expecting
- if (!MONO_TYPE_IS_VOID (sig->ret))
- mono_mb_emit_ldloc (mb, retval);
- }
-
- mono_mb_emit_byte (mb, CEE_RET);
- }
-
-
- }
- /* Does this case ever get hit? */
- else {
- char *msg = g_strdup ("non imported interfaces on \
- imported classes is not yet implemented.");
- mono_mb_emit_exception (mb, "NotSupportedException", msg);
- }
- csig = signature_dup (method->klass->image, sig);
- csig->pinvoke = 0;
- res = mono_mb_create_and_cache (cache, method,
- mb, csig, csig->param_count + 16);
- mono_mb_free (mb);
- return res;
-}
-
-/**
- * cominterop_get_invoke:
- * @method: managed method
- *
- * Returns: the generated method that calls the underlying __ComObject
- * rather than the proxy object.
- */
-static MonoMethod *
-cominterop_get_invoke (MonoMethod *method)
-{
- MonoMethodSignature *sig;
- MonoMethodBuilder *mb;
- MonoMethod *res;
- int i, temp_obj;
- GHashTable* cache = get_cache (&method->klass->image->cominterop_invoke_cache, mono_aligned_addr_hash, NULL);
-
- g_assert (method);
-
- if ((res = mono_marshal_find_in_cache (cache, method)))
- return res;
-
- sig = signature_no_pinvoke (method);
-
- /* we cant remote methods without this pointer */
- if (!sig->hasthis)
- return method;
-
- mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP_INVOKE);
-
- /* get real proxy object, which is a ComInteropProxy in this case*/
- temp_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
- mono_mb_emit_ldarg (mb, 0);
- mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
- /* load the RCW from the ComInteropProxy*/
- mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoComInteropProxy, com_object));
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
- /* load args and make the call on the RCW */
- for (i = 1; i <= sig->param_count; i++)
- mono_mb_emit_ldarg (mb, i);
-
- if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
- MonoMethod * native_wrapper = cominterop_get_native_wrapper(method);
- mono_mb_emit_managed_call (mb, native_wrapper, NULL);
- }
- else {
- if (method->flags & METHOD_ATTRIBUTE_VIRTUAL)
- mono_mb_emit_op (mb, CEE_CALLVIRT, method);
- else
- mono_mb_emit_op (mb, CEE_CALL, method);
- }
-
- if (!strcmp(method->name, ".ctor")) {
- static MonoClass *com_interop_proxy_class = NULL;
- static MonoMethod *cache_proxy = NULL;
-
- if (!com_interop_proxy_class)
- com_interop_proxy_class = mono_class_from_name (mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
- if (!cache_proxy)
- cache_proxy = mono_class_get_method_from_name (com_interop_proxy_class, "CacheProxy", 0);
-
- mono_mb_emit_ldarg (mb, 0);
- mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoTransparentProxy, rp));
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
- mono_mb_emit_managed_call (mb, cache_proxy, NULL);
- }
-
- emit_thread_interrupt_checkpoint (mb);
-
- mono_mb_emit_byte (mb, CEE_RET);
-
- res = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
- mono_mb_free (mb);
-
- return res;
-}
-
-#endif /* DISABLE_COM */
-
-/* Maps a managed object to its unmanaged representation
- * i.e. it's COM Callable Wrapper (CCW).
- * Key: MonoObject*
- * Value: MonoCCW*
- */
-static GHashTable* ccw_hash = NULL;
-
-/* Maps a CCW interface to it's containing CCW.
- * Note that a CCW support many interfaces.
- * Key: MonoCCW*
- * Value: MonoCCWInterface*
- */
-static GHashTable* ccw_interface_hash = NULL;
-
-/* Maps the IUnknown value of a RCW to
- * it's MonoComInteropProxy*.
- * Key: void*
- * Value: gchandle
- */
-static GHashTable* rcw_hash = NULL;
-
-MonoMethod *
-mono_marshal_get_remoting_invoke (MonoMethod *method)
-{
- MonoMethodSignature *sig;
- MonoMethodBuilder *mb;
- MonoMethod *res;
- int params_var;
-
- g_assert (method);
-
- if (method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE || method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE)
- return method;
-
- /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
- if ((method->klass->is_com_object || method->klass == mono_defaults.com_object_class) && !mono_class_vtable (mono_domain_get (), method->klass)->remote) {
-#ifndef DISABLE_COM
- return cominterop_get_invoke(method);
-#else
- g_assert_not_reached ();
-#endif
- }
-
- sig = signature_no_pinvoke (method);
-
- /* we cant remote methods without this pointer */
- if (!sig->hasthis)
- return method;
-
- if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_REMOTING_INVOKE)))
- return res;
-
- mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE);
- mb->method->save_lmf = 1;
-
- params_var = mono_mb_emit_save_args (mb, sig, TRUE);
-
- mono_mb_emit_ptr (mb, method);
- mono_mb_emit_ldloc (mb, params_var);
- mono_mb_emit_icall (mb, mono_remoting_wrapper);
- emit_thread_interrupt_checkpoint (mb);
-
- if (sig->ret->type == MONO_TYPE_VOID) {
- mono_mb_emit_byte (mb, CEE_POP);
- mono_mb_emit_byte (mb, CEE_RET);
- } else {
- mono_mb_emit_restore_result (mb, sig->ret);
- }
-
- res = mono_remoting_mb_create_and_cache (method, mb, sig, sig->param_count + 16);
- mono_mb_free (mb);
-
- return res;
-}
-
-/* mono_get_xdomain_marshal_type()
- * Returns the kind of marshalling that a type needs for cross domain calls.
- */
-static MonoXDomainMarshalType
-mono_get_xdomain_marshal_type (MonoType *t)
+static MonoXDomainMarshalType
+mono_get_xdomain_marshal_type (MonoType *t)
{
switch (t->type) {
case MONO_TYPE_VOID:
MonoClass *ret_class = NULL;
int loc_array=0, loc_return=0, loc_serialized_exc=0;
MonoExceptionClause *main_clause;
- MonoMethodHeader *header;
int pos, pos_leave;
gboolean copy_return;
/* try */
- mono_loader_lock ();
main_clause = mono_image_alloc0 (method->klass->image, sizeof (MonoExceptionClause));
- mono_loader_unlock ();
main_clause->try_offset = mono_mb_get_label (mb);
/* Clean the call context */
mono_mb_emit_byte (mb, CEE_RET);
+ mono_mb_set_clauses (mb, 1, main_clause);
+
res = mono_remoting_mb_create_and_cache (method, mb, csig, csig->param_count + 16);
mono_mb_free (mb);
- header = ((MonoMethodNormal *)res)->header;
- header->num_clauses = 1;
- header->clauses = main_clause;
-
return res;
}
if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_XDOMAIN_INVOKE)))
return res;
- sig = signature_no_pinvoke (method);
+ sig = mono_signature_no_pinvoke (method);
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_XDOMAIN_INVOKE);
mb->method->save_lmf = 1;
return mono_marshal_get_xappdomain_invoke (method);
} else if (target_type == MONO_REMOTING_TARGET_COMINTEROP) {
#ifndef DISABLE_COM
- return cominterop_get_invoke (method);
+ return mono_cominterop_get_invoke (method);
#else
g_assert_not_reached ();
#endif
} else {
return mono_marshal_get_remoting_invoke (method);
}
+ /* Not erached */
+ return NULL;
}
G_GNUC_UNUSED static gpointer
if ((res = mono_marshal_remoting_find_in_cache (method, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)))
return res;
- sig = signature_no_pinvoke (method);
+ sig = mono_signature_no_pinvoke (method);
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK);
g_assert (method && method->klass->parent == mono_defaults.multicastdelegate_class &&
!strcmp (method->name, "Invoke"));
- sig = signature_no_pinvoke (method);
+ sig = mono_signature_no_pinvoke (method);
if (callvirt) {
/* We need to cache the signature+method pair */
if (callvirt) {
// From mono_mb_create_and_cache
+ mb->skip_visibility = 1;
newm = mono_mb_create_method (mb, sig, sig->param_count + 16);
- newm->skip_visibility = 1;
/*We perform double checked locking, so must fence before publishing*/
mono_memory_barrier ();
mono_marshal_lock ();
mono_free_method (newm);
}
} else {
+ mb->skip_visibility = 1;
res = mono_mb_create_and_cache (cache, sig, mb, sig, sig->param_count + 16);
- res->skip_visibility = 1;
}
mono_mb_free (mb);
int i;
res = mono_metadata_signature_alloc (klass->image, sig->param_count + 1);
- memcpy (res, sig, sizeof (MonoMethodSignature));
+ memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
res->param_count = sig->param_count + 1;
res->hasthis = FALSE;
for (i = sig->param_count - 1; i >= 0; i --)
return callsig;
}
-static inline MonoType*
-get_basic_type (MonoType *t)
+static MonoType*
+get_runtime_invoke_type (MonoType *t, gboolean ret)
{
+ if (t->byref) {
+ if (t->type == MONO_TYPE_GENERICINST)
+ return t;
+ else
+ return &mono_defaults.int_class->byval_arg;
+ }
+
+ if (MONO_TYPE_IS_REFERENCE (t))
+ return &mono_defaults.object_class->byval_arg;
+
+ if (ret)
+ /* The result needs to be boxed */
+ return t;
+
+handle_enum:
switch (t->type) {
case MONO_TYPE_U1:
return &mono_defaults.sbyte_class->byval_arg;
case MONO_TYPE_U8:
return &mono_defaults.int64_class->byval_arg;
case MONO_TYPE_BOOLEAN:
- return &mono_defaults.byte_class->byval_arg;
+ return &mono_defaults.sbyte_class->byval_arg;
case MONO_TYPE_CHAR:
return &mono_defaults.int16_class->byval_arg;
case MONO_TYPE_U:
+ case MONO_TYPE_PTR:
return &mono_defaults.int_class->byval_arg;
case MONO_TYPE_VALUETYPE:
- if (t->data.klass->enumtype)
- return mono_type_get_underlying_type (t);
- else
- return t;
+ if (t->data.klass->enumtype) {
+ t = mono_class_enum_basetype (t->data.klass);
+ goto handle_enum;
+ }
+ return t;
default:
return t;
}
}
-static inline gboolean
-runtime_invoke_type_equal (MonoType *t1, MonoType *t2)
+/*
+ * mono_marshal_get_runtime_invoke_sig:
+ *
+ * Return a common signature used for sharing runtime invoke wrappers.
+ */
+static MonoMethodSignature*
+mono_marshal_get_runtime_invoke_sig (MonoMethodSignature *sig)
{
- if (MONO_TYPE_IS_REFERENCE (t1) && MONO_TYPE_IS_REFERENCE (t2))
- return TRUE;
- else if (t1->byref != t2->byref)
- return FALSE;
- else if (t1->byref && t2->byref)
- return TRUE;
- else if (t1->type == MONO_TYPE_PTR && t2->type == MONO_TYPE_PTR)
- return TRUE;
- else {
- t1 = get_basic_type (t1);
- t2 = get_basic_type (t2);
+ MonoMethodSignature *res = mono_metadata_signature_dup (sig);
+ int i;
- return mono_metadata_type_equal (t1, t2);
- }
+ res->ret = get_runtime_invoke_type (sig->ret, TRUE);
+ for (i = 0; i < res->param_count; ++i)
+ res->params [i] = get_runtime_invoke_type (sig->params [i], FALSE);
+
+ return res;
}
-static inline guint
-runtime_invoke_type_hash (MonoType *t1)
+static gboolean
+runtime_invoke_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
{
- if (MONO_TYPE_IS_REFERENCE (t1))
- return 0;
- else if (t1->byref)
- return 1;
- else if (t1->type == MONO_TYPE_PTR)
- return 2;
+ /* Can't share wrappers which return a vtype since it needs to be boxed */
+ if (sig1->ret != sig2->ret && !(MONO_TYPE_IS_REFERENCE (sig1->ret) && MONO_TYPE_IS_REFERENCE (sig2->ret)) && !mono_metadata_type_equal (sig1->ret, sig2->ret))
+ return FALSE;
else
- return mono_metadata_type_hash (get_basic_type (t1));
+ return mono_metadata_signature_equal (sig1, sig2);
}
/*
- * runtime_invoke_signature_equal:
+ * get_wrapper_target_class:
*
- * Same as mono_metadata_signature_equal, but consider reference types equal.
+ * Return the class where a wrapper method should be placed.
*/
-static gboolean
-runtime_invoke_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
+static MonoClass*
+get_wrapper_target_class (MonoImage *image)
{
- int i;
-
- if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
- return FALSE;
-
- if (sig1->generic_param_count != sig2->generic_param_count)
- return FALSE;
+ MonoClass *klass;
/*
- * We're just comparing the signatures of two methods here:
- *
- * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
- * U and V are equal here.
- *
- * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
+ * Notes:
+ * - can't put all wrappers into an mscorlib class, because they reference
+ * metadata (signature) so they should be put into the same image as the
+ * method they wrap, so they are unloaded together.
+ * - putting them into a class with a type initalizer could cause the
+ * initializer to be executed which can be a problem if the wrappers are
+ * shared.
+ * - putting them into an inflated class can cause problems if the the
+ * class is deleted because it references an image which is unloaded.
+ * To avoid these problems, we put the wrappers into the <Module> class of
+ * the image.
*/
+ if (image->dynamic)
+ klass = ((MonoDynamicImage*)image)->wrappers_type;
+ else
+ klass = mono_class_get (image, mono_metadata_make_token (MONO_TABLE_TYPEDEF, 1));
+ g_assert (klass);
- for (i = 0; i < sig1->param_count; i++) {
- MonoType *p1 = sig1->params[i];
- MonoType *p2 = sig2->params[i];
-
- /* if (p1->attrs != p2->attrs)
- return FALSE;
- */
- if (!runtime_invoke_type_equal (p1, p2))
- return FALSE;
- }
-
- if (!runtime_invoke_type_equal (sig1->ret, sig2->ret))
- return FALSE;
- return TRUE;
+ return klass;
}
-static guint
-runtime_invoke_signature_hash (MonoMethodSignature *sig)
+static void
+runtime_invoke_reset_abort (MonoException *ex)
{
- guint i, res = sig->ret->type;
-
- for (i = 0; i < sig->param_count; i++)
- res = (res << 5) - res + runtime_invoke_type_hash (sig->params[i]);
-
- return res;
+ if (ex->object.vtable->klass == mono_defaults.threadabortexception_class)
+ ves_icall_System_Threading_Thread_ResetAbort ();
}
/*
* MonoObject *runtime_invoke (MonoObject *this, void **params, MonoObject **exc, void* method)
*
* we also catch exceptions if exc != null
+ * If VIRTUAL is TRUE, then METHOD is invoked virtually on THIS. This is useful since
+ * it means that the compiled code for METHOD does not have to be looked up
+ * before calling the runtime invoke wrapper. In this case, the wrapper ignores
+ * its METHOD argument.
*/
MonoMethod *
-mono_marshal_get_runtime_invoke (MonoMethod *method)
+mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual)
{
MonoMethodSignature *sig, *csig, *callsig;
MonoExceptionClause *clause;
- MonoMethodHeader *header;
MonoMethodBuilder *mb;
GHashTable *cache = NULL;
MonoClass *target_klass;
static MonoString *string_dummy = NULL;
static MonoMethodSignature *cctor_signature = NULL;
static MonoMethodSignature *finalize_signature = NULL;
- int i, pos, posna;
+ int i, pos;
char *name;
gboolean need_direct_wrapper = FALSE;
+ int *tmp_nullable_locals;
g_assert (method);
finalize_signature->hasthis = 1;
}
+ if (virtual)
+ need_direct_wrapper = TRUE;
+
+ /*
+ * Use a separate cache indexed by methods to speed things up and to avoid the
+ * boundless mempool growth caused by the signature_dup stuff below.
+ */
+ if (virtual)
+ cache = get_cache (&method->klass->image->runtime_invoke_vcall_cache, mono_aligned_addr_hash, NULL);
+ else
+ cache = get_cache (&method->klass->image->runtime_invoke_direct_cache, mono_aligned_addr_hash, NULL);
+ res = mono_marshal_find_in_cache (cache, method);
+ if (res)
+ return res;
+
if (method->klass->rank && (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
(method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
/*
*/
need_direct_wrapper = TRUE;
}
-
+
if (method->string_ctor) {
callsig = lookup_string_ctor_signature (mono_method_signature (method));
if (!callsig)
callsig = add_string_ctor_signature (method);
+ /* 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) {
/*
}
}
+#if 0
+ /* Vtypes/nullables/Byrefs cause too many problems */
+ for (i = 0; i < callsig->param_count; ++i) {
+ if (MONO_TYPE_ISSTRUCT (callsig->params [i]) || callsig->params [i]->byref)
+ need_direct_wrapper = TRUE;
+ }
+#endif
+
/*
* We try to share runtime invoke wrappers between different methods but have to
* be careful about methods whose klass has a type cctor, since putting the wrapper
need_direct_wrapper = TRUE;
*/
- /*
- * Can't put these wrappers into object, since they reference non-corlib
- * metadata (callsig).
- */
- target_klass = method->klass;
+ target_klass = get_wrapper_target_class (method->klass->image);
}
}
if (need_direct_wrapper) {
- cache = get_cache (&target_klass->image->runtime_invoke_direct_cache, mono_aligned_addr_hash, NULL);
- res = mono_marshal_find_in_cache (cache, method);
+ /* Already searched at the start */
} else {
+ callsig = mono_marshal_get_runtime_invoke_sig (callsig);
+
cache = get_cache (&target_klass->image->runtime_invoke_cache,
- (GHashFunc)runtime_invoke_signature_hash,
+ (GHashFunc)mono_signature_hash,
(GCompareFunc)runtime_invoke_signature_equal);
/* from mono_marshal_find_in_cache */
mono_marshal_lock ();
res = g_hash_table_lookup (cache, callsig);
mono_marshal_unlock ();
- }
- if (res) {
- return res;
+ if (res) {
+ g_free (callsig);
+ return res;
+ }
+
+ // FIXME: When to free callsig ?
}
-
+
/* to make it work with our special string constructors */
if (!string_dummy) {
MONO_GC_REGISTER_ROOT (string_dummy);
csig->params [2] = &mono_defaults.int_class->byval_arg;
csig->params [3] = &mono_defaults.int_class->byval_arg;
- name = mono_signature_to_name (callsig, "runtime_invoke");
+ name = mono_signature_to_name (callsig, virtual ? "runtime_invoke_virtual" : "runtime_invoke");
mb = mono_mb_new (target_klass, name, MONO_WRAPPER_RUNTIME_INVOKE);
g_free (name);
emit_thread_force_interrupt_checkpoint (mb);
+ if (virtual) {
+ g_assert (sig->hasthis);
+ g_assert (method->flags & METHOD_ATTRIBUTE_VIRTUAL);
+ }
+
if (sig->hasthis) {
if (method->string_ctor) {
mono_mb_emit_ptr (mb, string_dummy);
}
}
+ tmp_nullable_locals = g_new0 (int, sig->param_count);
+
for (i = 0; i < sig->param_count; i++) {
MonoType *t = sig->params [i];
int type;
if (t->byref) {
mono_mb_emit_byte (mb, CEE_LDIND_I);
+ /* A Nullable<T> type don't have a boxed form, it's either null or a boxed T.
+ * So to make this work we unbox it to a local variablee and push a reference to that.
+ */
+ if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
+ tmp_nullable_locals [i] = mono_mb_add_local (mb, &mono_class_from_mono_type (t)->byval_arg);
+
+ mono_mb_emit_op (mb, CEE_UNBOX_ANY, mono_class_from_mono_type (t));
+ mono_mb_emit_stloc (mb, tmp_nullable_locals [i]);
+ mono_mb_emit_ldloc_addr (mb, tmp_nullable_locals [i]);
+ }
continue;
}
break;
case MONO_TYPE_GENERICINST:
if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
- mono_mb_emit_byte (mb, CEE_LDIND_I);
+ mono_mb_emit_byte (mb, mono_type_to_ldind (sig->params [i]));
break;
}
/* fall through */
case MONO_TYPE_VALUETYPE:
if (type == MONO_TYPE_VALUETYPE && t->data.klass->enumtype) {
- type = t->data.klass->enum_basetype->type;
+ type = mono_class_enum_basetype (t->data.klass)->type;
goto handle_enum;
}
mono_mb_emit_byte (mb, CEE_LDIND_I);
}
}
- if (need_direct_wrapper) {
+ if (virtual) {
+ mono_mb_emit_op (mb, CEE_CALLVIRT, method);
+ } else if (need_direct_wrapper) {
mono_mb_emit_op (mb, CEE_CALL, method);
} else {
mono_mb_emit_ldarg (mb, 3);
g_assert_not_reached ();
}
-
switch (sig->ret->type) {
case MONO_TYPE_VOID:
if (!method->string_ctor)
/* nothing to do */
break;
case MONO_TYPE_PTR:
+ /* The result is an IntPtr */
+ mono_mb_emit_op (mb, CEE_BOX, mono_defaults.int_class);
+ break;
default:
g_assert_not_reached ();
}
mono_mb_emit_stloc (mb, 0);
+
+ /* Convert back nullable-byref arguments */
+ for (i = 0; i < sig->param_count; i++) {
+ MonoType *t = sig->params [i];
+
+ /*
+ * Box the result and put it back into the array, the caller will have
+ * to obtain it from there.
+ */
+ if (t->byref && t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
+ mono_mb_emit_ldarg (mb, 1);
+ mono_mb_emit_icon (mb, sizeof (gpointer) * i);
+ mono_mb_emit_byte (mb, CEE_ADD);
+
+ mono_mb_emit_ldloc (mb, tmp_nullable_locals [i]);
+ mono_mb_emit_op (mb, CEE_BOX, mono_class_from_mono_type (t));
+
+ mono_mb_emit_byte (mb, CEE_STIND_REF);
+ }
+ }
pos = mono_mb_emit_branch (mb, CEE_LEAVE);
- mono_loader_lock ();
clause = mono_image_alloc0 (target_klass->image, sizeof (MonoExceptionClause));
- mono_loader_unlock ();
clause->flags = MONO_EXCEPTION_CLAUSE_FILTER;
clause->try_len = mono_mb_get_label (mb);
mono_mb_emit_byte (mb, CEE_LDNULL);
mono_mb_emit_stloc (mb, 0);
- /* Check for the abort exception */
+ /* Check and reset abort exception */
+ /* Done in a separate function to reduce code size */
mono_mb_emit_ldloc (mb, 1);
- mono_mb_emit_op (mb, CEE_ISINST, mono_defaults.threadabortexception_class);
- posna = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- /* Delay the abort exception */
- mono_mb_emit_icall (mb, ves_icall_System_Threading_Thread_ResetAbort);
+ mono_mb_emit_icall (mb, runtime_invoke_reset_abort);
- mono_mb_patch_short_branch (mb, posna);
mono_mb_emit_branch (mb, CEE_LEAVE);
clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
+ mono_mb_set_clauses (mb, 1, clause);
+
/* return result */
mono_mb_patch_branch (mb, pos);
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_RET);
if (need_direct_wrapper) {
+ mb->skip_visibility = 1;
res = mono_mb_create_and_cache (cache, method, mb, csig, sig->param_count + 16);
} else {
/* taken from mono_mb_create_and_cache */
res = newm;
g_hash_table_insert (cache, callsig, res);
/* Can't insert it into wrapper_hash since the key is a signature */
+ g_hash_table_insert (method->klass->image->runtime_invoke_direct_cache, method, res);
} else {
mono_free_method (newm);
}
mono_mb_free (mb);
- header = ((MonoMethodNormal *)res)->header;
- header->num_clauses = 1;
- header->clauses = clause;
-
return res;
}
-/*
- * mono_marshal_get_static_rgctx_invoke:
- * @method: a method
- *
- * Generates a wrapper for calling a generic shared method, either
- * static or generic. We need this for virtual generic method lookup
- * and ldftn when we do generic code sharing. Instead of producing
- * the address of the method we produce the address of a wrapper for
- * the method because the wrapper passes the (method) runtime generic
- * context argument which calli cannot do.
- */
-MonoMethod *
-mono_marshal_get_static_rgctx_invoke (MonoMethod *method)
+static void
+mono_mb_emit_auto_layout_exception (MonoMethodBuilder *mb, MonoClass *klass)
{
- static gboolean inited = FALSE;
- static int num_wrappers = 0;
-
- MonoMethodBuilder *mb;
- MonoMethod *res;
- MonoClass *target_klass = method->klass;
- MonoMethodSignature *sig = mono_method_signature (method);
- int i;
- char *name;
- GHashTable *cache;
- MonoImage *image = method->klass->image;
-
- cache = get_cache (&image->static_rgctx_invoke_cache, mono_aligned_addr_hash, NULL);
- if ((res = mono_marshal_find_in_cache (cache, method)))
- return res;
-
- if (!inited) {
- mono_counters_register ("static rgctx invoke wrappers",
- MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_wrappers);
- inited = TRUE;
- }
- ++num_wrappers;
-
- name = mono_signature_to_name (mono_method_signature (method), "static_rgctx_invoke");
- mb = mono_mb_new (target_klass, name, MONO_WRAPPER_STATIC_RGCTX_INVOKE);
- g_free (name);
-
- for (i = 0; i < sig->param_count + sig->hasthis; i++)
- mono_mb_emit_ldarg (mb, i);
- mono_mb_emit_op (mb, CEE_CALL, method);
- mono_mb_emit_byte (mb, CEE_RET);
-
- res = mono_mb_create_and_cache (cache, method, mb, mono_method_signature (method),
- sig->param_count + sig->hasthis + 4);
- res->skip_visibility = TRUE;
- res->flags = method->flags;
-
- mono_mb_free (mb);
-
- return res;
-}
-
-static void
-mono_mb_emit_auto_layout_exception (MonoMethodBuilder *mb, MonoClass *klass)
-{
- char *msg = g_strdup_printf ("The type `%s.%s' layout needs to be Sequential or Explicit",
- klass->name_space, klass->name);
+ char *msg = g_strdup_printf ("The type `%s.%s' layout needs to be Sequential or Explicit",
+ klass->name_space, klass->name);
mono_mb_emit_exception_marshal_directive (mb, msg);
}
return res;
}
-typedef struct {
- MonoMethodBuilder *mb;
- MonoMethodSignature *sig;
- MonoMethodPInvoke *piinfo;
- int *orig_conv_args; /* Locals containing the original values of byref args */
- int retobj_var;
- MonoClass *retobj_class;
- MonoMethodSignature *csig; /* Might need to be changed due to MarshalAs directives */
- MonoImage *image; /* The image to use for looking up custom marshallers */
-} EmitMarshalContext;
-
-typedef enum {
- /*
- * This is invoked to convert arguments from the current types to
- * the underlying types expected by the platform routine. If required,
- * the methods create a temporary variable with the proper type, and return
- * the location for it (either the passed argument, or the newly allocated
- * local slot).
- */
- MARSHAL_ACTION_CONV_IN,
-
- /*
- * This operation is called to push the actual value that was optionally
- * converted on the first stage
- */
- MARSHAL_ACTION_PUSH,
-
- /*
- * Convert byref arguments back or free resources allocated during the
- * CONV_IN stage
- */
- MARSHAL_ACTION_CONV_OUT,
-
- /*
- * The result from the unmanaged call is at the top of the stack when
- * this action is invoked. The result should be stored in the
- * third local variable slot.
- */
- MARSHAL_ACTION_CONV_RESULT,
-
- MARSHAL_ACTION_MANAGED_CONV_IN,
- MARSHAL_ACTION_MANAGED_CONV_OUT,
- MARSHAL_ACTION_MANAGED_CONV_RESULT
-} MarshalAction;
-
static int
emit_marshal_custom (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
mono_mb_emit_ldloc (mb, conv_arg);
mono_mb_emit_op (mb, CEE_CALLVIRT, marshal_native_to_managed);
mono_mb_emit_byte (mb, CEE_STIND_REF);
- } else {
+ } else if (t->attrs & PARAM_ATTRIBUTE_OUT) {
mono_mb_emit_ldstr (mb, g_strdup (spec->data.custom_data.cookie));
mono_mb_emit_op (mb, CEE_CALL, get_instance);
MonoClass *klass;
int pos = 0, pos2;
- klass = t->data.klass;
+ klass = mono_class_from_mono_type (t);
switch (action) {
case MARSHAL_ACTION_CONV_IN:
MarshalAction action)
{
MonoMethodBuilder *mb = m->mb;
- MonoClass *klass = t->data.klass;
+ MonoClass *klass = mono_class_from_mono_type (t);
int pos, pos2, loc;
- if (mono_class_from_mono_type (t) == mono_defaults.object_class) {
- mono_raise_exception (mono_get_exception_not_implemented ("Marshalling of type object is not implemented"));
- }
-
switch (action) {
case MARSHAL_ACTION_CONV_IN:
*conv_arg_type = &mono_defaults.int_class->byval_arg;
conv_arg = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
m->orig_conv_args [argnum] = 0;
-
+
+ if (mono_class_from_mono_type (t) == mono_defaults.object_class) {
+ char *msg = g_strdup_printf ("Marshalling of type object is not implemented");
+ mono_mb_emit_exception_marshal_directive (mb, msg);
+ break;
+ }
+
if (klass->delegate) {
if (t->byref) {
if (!(t->attrs & PARAM_ATTRIBUTE_OUT)) {
/* allocate a new object */
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
- mono_mb_emit_op (mb, CEE_MONO_NEWOBJ, t->data.klass);
+ mono_mb_emit_op (mb, CEE_MONO_NEWOBJ, klass);
mono_mb_emit_byte (mb, CEE_STIND_REF);
}
mono_mb_emit_stloc (mb, 0);
/* emit valuetype conversion code */
- emit_struct_conv (mb, t->data.klass, TRUE);
+ emit_struct_conv (mb, klass, TRUE);
/* Free the structure returned by the native code */
emit_struct_free (mb, klass, conv_arg);
return conv_arg;
}
-#ifndef DISABLE_COM
-
-static int
-emit_marshal_com_interface (EmitMarshalContext *m, int argnum, MonoType *t,
- MonoMarshalSpec *spec,
- int conv_arg, MonoType **conv_arg_type,
- MarshalAction action)
-{
- MonoMethodBuilder *mb = m->mb;
- MonoClass *klass = t->data.klass;
- static MonoMethod* get_object_for_iunknown = NULL;
- static MonoMethod* get_iunknown_for_object_internal = NULL;
- static MonoMethod* get_com_interface_for_object_internal = NULL;
- static MonoMethod* get_idispatch_for_object_internal = NULL;
- static MonoMethod* marshal_release = NULL;
- static MonoMethod* AddRef = NULL;
- if (!get_object_for_iunknown)
- get_object_for_iunknown = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetObjectForIUnknown", 1);
- if (!get_iunknown_for_object_internal)
- get_iunknown_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetIUnknownForObjectInternal", 1);
- if (!get_idispatch_for_object_internal)
- get_idispatch_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetIDispatchForObjectInternal", 1);
- if (!get_com_interface_for_object_internal)
- get_com_interface_for_object_internal = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetComInterfaceForObjectInternal", 2);
- if (!marshal_release)
- marshal_release = mono_class_get_method_from_name (mono_defaults.marshal_class, "Release", 1);
-
- /* COM types are initialized lazily */
- mono_init_com_types ();
-
- switch (action) {
- case MARSHAL_ACTION_CONV_IN: {
- guint32 pos_null = 0;
-
- *conv_arg_type = &mono_defaults.int_class->byval_arg;
- conv_arg = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-
- mono_mb_emit_ptr (mb, NULL);
- mono_mb_emit_stloc (mb, conv_arg);
-
- /* we dont need any conversions for out parameters */
- if (t->byref && t->attrs & PARAM_ATTRIBUTE_OUT)
- break;
-
- mono_mb_emit_ldarg (mb, argnum);
- if (t->byref)
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
- /* if null just break, conv arg was already inited to 0 */
- pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- mono_mb_emit_ldarg (mb, argnum);
- if (t->byref)
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
-
- if (klass && klass != mono_defaults.object_class) {
- mono_mb_emit_ptr (mb, t);
- mono_mb_emit_icall (mb, type_from_handle);
- mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
- }
- else if (spec->native == MONO_NATIVE_IUNKNOWN)
- mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
- else if (spec->native == MONO_NATIVE_IDISPATCH)
- mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
- else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
- mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
- else
- g_assert_not_reached ();
- mono_mb_emit_stloc (mb, conv_arg);
- mono_mb_patch_short_branch (mb, pos_null);
- break;
- }
-
- case MARSHAL_ACTION_CONV_OUT: {
- if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT)) {
- int ccw_obj;
- guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
- ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
-
- mono_mb_emit_ldarg (mb, argnum);
- mono_mb_emit_byte (mb, CEE_LDNULL);
- mono_mb_emit_byte (mb, CEE_STIND_REF);
-
- mono_mb_emit_ldloc (mb, conv_arg);
- pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- mono_mb_emit_ldloc (mb, conv_arg);
- mono_mb_emit_icon (mb, TRUE);
- mono_mb_emit_icall (mb, cominterop_get_ccw_object);
- mono_mb_emit_stloc (mb, ccw_obj);
- mono_mb_emit_ldloc (mb, ccw_obj);
- pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
- mono_mb_emit_ldarg (mb, argnum);
- mono_mb_emit_ldloc (mb, conv_arg);
- mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);
-
- if (klass && klass != mono_defaults.object_class)
- mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
- mono_mb_emit_byte (mb, CEE_STIND_REF);
-
- pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
-
- /* is already managed object */
- mono_mb_patch_short_branch (mb, pos_ccw);
- mono_mb_emit_ldarg (mb, argnum);
- mono_mb_emit_ldloc (mb, ccw_obj);
-
- if (klass && klass != mono_defaults.object_class)
- mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
- mono_mb_emit_byte (mb, CEE_STIND_REF);
-
- mono_mb_patch_short_branch (mb, pos_end);
-
- /* need to call Release to follow COM rules of ownership */
- mono_mb_emit_ldloc (mb, conv_arg);
- mono_mb_emit_managed_call (mb, marshal_release, NULL);
- mono_mb_emit_byte (mb, CEE_POP);
-
- /* case if null */
- mono_mb_patch_short_branch (mb, pos_null);
- }
- break;
- }
- case MARSHAL_ACTION_PUSH:
- if (t->byref)
- mono_mb_emit_ldloc_addr (mb, conv_arg);
- else
- mono_mb_emit_ldloc (mb, conv_arg);
- break;
-
- case MARSHAL_ACTION_CONV_RESULT: {
- int ccw_obj, ret_ptr;
- guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
- ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
- ret_ptr = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
-
- /* store return value */
- mono_mb_emit_stloc (mb, ret_ptr);
-
- mono_mb_emit_ldloc (mb, ret_ptr);
- pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- mono_mb_emit_ldloc (mb, ret_ptr);
- mono_mb_emit_icon (mb, TRUE);
- mono_mb_emit_icall (mb, cominterop_get_ccw_object);
- mono_mb_emit_stloc (mb, ccw_obj);
- mono_mb_emit_ldloc (mb, ccw_obj);
- pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
- mono_mb_emit_ldloc (mb, ret_ptr);
- mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);
-
- if (klass && klass != mono_defaults.object_class)
- mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
- mono_mb_emit_stloc (mb, 3);
-
- pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
-
- /* is already managed object */
- mono_mb_patch_short_branch (mb, pos_ccw);
- mono_mb_emit_ldloc (mb, ccw_obj);
-
- if (klass && klass != mono_defaults.object_class)
- mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
- mono_mb_emit_stloc (mb, 3);
-
- mono_mb_patch_short_branch (mb, pos_end);
-
- /* need to call Release to follow COM rules of ownership */
- mono_mb_emit_ldloc (mb, ret_ptr);
- mono_mb_emit_managed_call (mb, marshal_release, NULL);
- mono_mb_emit_byte (mb, CEE_POP);
-
- /* case if null */
- mono_mb_patch_short_branch (mb, pos_null);
- break;
- }
-
- case MARSHAL_ACTION_MANAGED_CONV_IN: {
- int ccw_obj;
- guint32 pos_null = 0, pos_ccw = 0, pos_end = 0;
- ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
-
- klass = mono_class_from_mono_type (t);
- conv_arg = mono_mb_add_local (mb, &klass->byval_arg);
- *conv_arg_type = &mono_defaults.int_class->byval_arg;
-
- mono_mb_emit_byte (mb, CEE_LDNULL);
- mono_mb_emit_stloc (mb, conv_arg);
- if (t->attrs & PARAM_ATTRIBUTE_OUT)
- break;
-
- mono_mb_emit_ldarg (mb, argnum);
- if (t->byref)
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
- pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- mono_mb_emit_ldarg (mb, argnum);
- if (t->byref)
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
- mono_mb_emit_icon (mb, TRUE);
- mono_mb_emit_icall (mb, cominterop_get_ccw_object);
- mono_mb_emit_stloc (mb, ccw_obj);
- mono_mb_emit_ldloc (mb, ccw_obj);
- pos_ccw = mono_mb_emit_short_branch (mb, CEE_BRTRUE_S);
-
-
- mono_mb_emit_ldarg (mb, argnum);
- if (t->byref)
- mono_mb_emit_byte (mb, CEE_LDIND_REF);
- mono_mb_emit_managed_call (mb, get_object_for_iunknown, NULL);
-
- if (klass && klass != mono_defaults.object_class)
- mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
- mono_mb_emit_stloc (mb, conv_arg);
- pos_end = mono_mb_emit_short_branch (mb, CEE_BR_S);
-
- /* is already managed object */
- mono_mb_patch_short_branch (mb, pos_ccw);
- mono_mb_emit_ldloc (mb, ccw_obj);
- if (klass && klass != mono_defaults.object_class)
- mono_mb_emit_op (mb, CEE_CASTCLASS, klass);
- mono_mb_emit_stloc (mb, conv_arg);
-
- mono_mb_patch_short_branch (mb, pos_end);
- /* case if null */
- mono_mb_patch_short_branch (mb, pos_null);
- break;
- }
-
- case MARSHAL_ACTION_MANAGED_CONV_OUT: {
- if (t->byref && t->attrs & PARAM_ATTRIBUTE_OUT) {
- guint32 pos_null = 0;
-
- if (!AddRef)
- AddRef = mono_class_get_method_from_name (mono_defaults.marshal_class, "AddRef", 1);
-
- mono_mb_emit_ldloc (mb, conv_arg);
- /* if null just break, conv arg was already inited to 0 */
- pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- /* to store later */
- mono_mb_emit_ldarg (mb, argnum);
- mono_mb_emit_ldloc (mb, conv_arg);
- if (klass && klass != mono_defaults.object_class) {
- mono_mb_emit_ptr (mb, t);
- mono_mb_emit_icall (mb, type_from_handle);
- mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
- }
- else if (spec->native == MONO_NATIVE_IUNKNOWN)
- mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
- else if (spec->native == MONO_NATIVE_IDISPATCH)
- mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
- else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
- mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
- else
- g_assert_not_reached ();
- mono_mb_emit_byte (mb, CEE_STIND_I);
-
- mono_mb_emit_ldarg (mb, argnum);
- mono_mb_emit_byte (mb, CEE_LDIND_I);
- mono_mb_emit_managed_call (mb, AddRef, NULL);
- mono_mb_emit_byte (mb, CEE_POP);
-
- mono_mb_patch_short_branch (mb, pos_null);
- }
- break;
- }
-
- case MARSHAL_ACTION_MANAGED_CONV_RESULT: {
- guint32 pos_null = 0;
- int ccw_obj;
- ccw_obj = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
-
- if (!AddRef)
- AddRef = mono_class_get_method_from_name (mono_defaults.marshal_class, "AddRef", 1);
-
- /* store return value */
- mono_mb_emit_stloc (mb, ccw_obj);
-
- mono_mb_emit_ldloc (mb, ccw_obj);
-
- /* if null just break, conv arg was already inited to 0 */
- pos_null = mono_mb_emit_short_branch (mb, CEE_BRFALSE_S);
-
- /* to store later */
- mono_mb_emit_ldloc (mb, ccw_obj);
- if (klass && klass != mono_defaults.object_class) {
- mono_mb_emit_ptr (mb, t);
- mono_mb_emit_icall (mb, type_from_handle);
- mono_mb_emit_managed_call (mb, get_com_interface_for_object_internal, NULL);
- }
- else if (spec->native == MONO_NATIVE_IUNKNOWN)
- mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
- else if (spec->native == MONO_NATIVE_IDISPATCH)
- mono_mb_emit_managed_call (mb, get_idispatch_for_object_internal, NULL);
- else if (!klass && spec->native == MONO_NATIVE_INTERFACE)
- mono_mb_emit_managed_call (mb, get_iunknown_for_object_internal, NULL);
- else
- g_assert_not_reached ();
- mono_mb_emit_stloc (mb, 3);
- mono_mb_emit_ldloc (mb, 3);
-
- mono_mb_emit_managed_call (mb, AddRef, NULL);
- mono_mb_emit_byte (mb, CEE_POP);
-
- mono_mb_patch_short_branch (mb, pos_null);
- break;
- }
-
- default:
- g_assert_not_reached ();
- }
-
- return conv_arg;
-}
-
-#endif /* DISABLE_COM */
-
static int
emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
MonoMarshalSpec *spec,
mono_mb_patch_branch (mb, label1);
mono_mb_patch_branch (mb, label3);
}
+
+ if (klass->element_class->blittable) {
+ /* free memory allocated (if any) by MONO_MARSHAL_CONV_ARRAY_LPARRAY */
+
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_ldloc (mb, conv_arg);
+ mono_mb_emit_icall (mb, conv_to_icall (MONO_MARSHAL_FREE_LPARRAY));
+ }
+
break;
case MARSHAL_ACTION_PUSH:
switch (action) {
case MARSHAL_ACTION_CONV_IN: {
MonoType *local_type;
- int variant_bool = 0;
- if (!t->byref)
- break;
+ int label_false;
+ guint8 ldc_op = CEE_LDC_I4_1;
+
if (spec == NULL) {
local_type = &mono_defaults.int32_class->byval_arg;
} else {
break;
case MONO_NATIVE_VARIANTBOOL:
local_type = &mono_defaults.int16_class->byval_arg;
- variant_bool = 1;
+ ldc_op = CEE_LDC_I4_M1;
+ break;
+ case MONO_NATIVE_BOOLEAN:
+ local_type = &mono_defaults.int32_class->byval_arg;
break;
default:
g_warning ("marshalling bool as native type %x is currently not supported", spec->native);
break;
}
}
- *conv_arg_type = &mono_defaults.int_class->byval_arg;
+ if (t->byref)
+ *conv_arg_type = &mono_defaults.int_class->byval_arg;
+ else
+ *conv_arg_type = local_type;
conv_arg = mono_mb_add_local (mb, local_type);
+
mono_mb_emit_ldarg (mb, argnum);
- mono_mb_emit_byte (mb, CEE_LDIND_I1);
- if (variant_bool)
- mono_mb_emit_byte (mb, CEE_NEG);
+ if (t->byref)
+ mono_mb_emit_byte (mb, CEE_LDIND_I1);
+ label_false = mono_mb_emit_branch (mb, CEE_BRFALSE);
+ mono_mb_emit_byte (mb, ldc_op);
mono_mb_emit_stloc (mb, conv_arg);
+ mono_mb_patch_branch (mb, label_false);
+
break;
}
case MARSHAL_ACTION_CONV_OUT:
+ {
+ int label_false, label_end;
if (!t->byref)
break;
+
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_ldloc (mb, conv_arg);
- if (spec != NULL && spec->native == MONO_NATIVE_VARIANTBOOL)
- mono_mb_emit_byte (mb, CEE_NEG);
+
+ label_false = mono_mb_emit_branch (mb, CEE_BRFALSE);
+ mono_mb_emit_byte (mb, CEE_LDC_I4_1);
+
+ label_end = mono_mb_emit_branch (mb, CEE_BR);
+ mono_mb_patch_branch (mb, label_false);
+ mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+ mono_mb_patch_branch (mb, label_end);
+
mono_mb_emit_byte (mb, CEE_STIND_I1);
break;
+ }
case MARSHAL_ACTION_PUSH:
if (t->byref)
mono_mb_emit_ldloc_addr (mb, conv_arg);
+ else if (conv_arg)
+ mono_mb_emit_ldloc (mb, conv_arg);
else
mono_mb_emit_ldarg (mb, argnum);
break;
mono_mb_emit_stloc (mb, 3);
break;
- default:
- g_assert_not_reached ();
- }
-
- return conv_arg;
-}
+ case MARSHAL_ACTION_MANAGED_CONV_IN: {
+ MonoClass* conv_arg_class = mono_defaults.int32_class;
+ guint8 ldop = CEE_LDIND_I4;
+ int label_null, label_false;
-static int
-emit_marshal_ptr (EmitMarshalContext *m, int argnum, MonoType *t,
- MonoMarshalSpec *spec, int conv_arg,
- MonoType **conv_arg_type, MarshalAction action)
-{
- MonoMethodBuilder *mb = m->mb;
+ conv_arg = mono_mb_add_local (mb, &mono_defaults.boolean_class->byval_arg);
- switch (action) {
- case MARSHAL_ACTION_CONV_IN:
- if (MONO_TYPE_ISSTRUCT (t->data.type)) {
- char *msg = g_strdup_printf ("Can not marshal 'parameter #%d': Pointers can not reference marshaled structures. Use byref instead.", argnum + 1);
- mono_mb_emit_exception_marshal_directive (m->mb, msg);
- }
- break;
+ if (spec) {
+ switch (spec->native) {
+ case MONO_NATIVE_I1:
+ case MONO_NATIVE_U1:
+ conv_arg_class = mono_defaults.byte_class;
+ ldop = CEE_LDIND_I1;
+ break;
+ case MONO_NATIVE_VARIANTBOOL:
+ conv_arg_class = mono_defaults.int16_class;
+ ldop = CEE_LDIND_I2;
+ break;
+ case MONO_NATIVE_BOOLEAN:
+ break;
+ default:
+ g_warning ("marshalling bool as native type %x is currently not supported", spec->native);
+ }
+ }
+
+ if (t->byref)
+ *conv_arg_type = &conv_arg_class->this_arg;
+ else
+ *conv_arg_type = &conv_arg_class->byval_arg;
+
+
+ mono_mb_emit_ldarg (mb, argnum);
+
+ /* Check null */
+ if (t->byref) {
+
+ label_null = mono_mb_emit_branch (mb, CEE_BRFALSE);
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, ldop);
+ }
+
+ label_false = mono_mb_emit_branch (mb, CEE_BRFALSE);
+ mono_mb_emit_byte (mb, CEE_LDC_I4_1);
+ mono_mb_emit_stloc (mb, conv_arg);
+ mono_mb_patch_branch (mb, label_false);
+
+ if (t->byref)
+ mono_mb_patch_branch (mb, label_null);
+ break;
+ }
+
+ case MARSHAL_ACTION_MANAGED_CONV_OUT: {
+ guint8 stop = CEE_STIND_I4;
+ guint8 ldc_op = CEE_LDC_I4_1;
+ int label_null,label_false, label_end;;
+
+ if (!t->byref)
+ break;
+ if (spec) {
+ switch (spec->native) {
+ case MONO_NATIVE_I1:
+ case MONO_NATIVE_U1:
+ stop = CEE_STIND_I1;
+ break;
+ case MONO_NATIVE_VARIANTBOOL:
+ stop = CEE_STIND_I2;
+ ldc_op = CEE_LDC_I4_M1;
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* Check null */
+ mono_mb_emit_ldarg (mb, argnum);
+ label_null = mono_mb_emit_branch (mb, CEE_BRFALSE);
+
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_ldloc (mb, conv_arg);
+
+ label_false = mono_mb_emit_branch (mb, CEE_BRFALSE);
+ mono_mb_emit_byte (mb, ldc_op);
+ label_end = mono_mb_emit_branch (mb, CEE_BR);
+
+ mono_mb_patch_branch (mb, label_false);
+ mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+ mono_mb_patch_branch (mb, label_end);
+
+ mono_mb_emit_byte (mb, stop);
+ mono_mb_patch_branch (mb, label_null);
+ break;
+ }
+
+ default:
+ g_assert_not_reached ();
+ }
+
+ return conv_arg;
+}
+
+static int
+emit_marshal_ptr (EmitMarshalContext *m, int argnum, MonoType *t,
+ MonoMarshalSpec *spec, int conv_arg,
+ MonoType **conv_arg_type, MarshalAction action)
+{
+ MonoMethodBuilder *mb = m->mb;
+
+ switch (action) {
+ case MARSHAL_ACTION_CONV_IN:
+ if (MONO_TYPE_ISSTRUCT (t->data.type) && !mono_class_from_mono_type (t->data.type)->blittable) {
+ char *msg = g_strdup_printf ("Can not marshal 'parameter #%d': Pointers can not reference marshaled structures. Use byref instead.", argnum + 1);
+ mono_mb_emit_exception_marshal_directive (m->mb, msg);
+ }
+ break;
case MARSHAL_ACTION_PUSH:
mono_mb_emit_ldarg (mb, argnum);
if (spec && (spec->native == MONO_NATIVE_IUNKNOWN ||
spec->native == MONO_NATIVE_IDISPATCH ||
spec->native == MONO_NATIVE_INTERFACE))
- return emit_marshal_com_interface (m, argnum, t, spec, conv_arg, conv_arg_type, action);
+ return mono_cominterop_emit_marshal_com_interface (m, argnum, t, spec, conv_arg, conv_arg_type, action);
+ if (spec && (spec->native == MONO_NATIVE_SAFEARRAY) &&
+ (spec->data.safearray_data.elem_type == MONO_VARIANT_VARIANT) &&
+ (action == MARSHAL_ACTION_CONV_OUT))
+ return mono_cominterop_emit_marshal_safearray (m, argnum, t, spec, conv_arg, conv_arg_type, action);
#endif
- if (mono_defaults.safehandle_class != NULL &&
+ if (mono_defaults.safehandle_class != NULL && t->data.klass &&
mono_class_is_subclass_of (t->data.klass, mono_defaults.safehandle_class, FALSE))
return emit_marshal_safehandle (m, argnum, t, spec, conv_arg, conv_arg_type, action);
case MONO_TYPE_U8:
case MONO_TYPE_FNPTR:
return emit_marshal_scalar (m, argnum, t, spec, conv_arg, conv_arg_type, action);
+ case MONO_TYPE_GENERICINST:
+ if (mono_type_generic_inst_is_valuetype (t))
+ return emit_marshal_vtype (m, argnum, t, spec, conv_arg, conv_arg_type, action);
+ else
+ return emit_marshal_object (m, argnum, t, spec, conv_arg, conv_arg_type, action);
}
return conv_arg;
* @sig: The signature of the native function
* @piinfo: Marshalling information
* @mspecs: Marshalling information
- * @func: the native function to call
+ * @aot: whenever the created method will be compiled by the AOT compiler
+ * @method: if non-NULL, the pinvoke method to call
* @check_exceptions: Whenever to check for pending exceptions after the native call
*
* generates IL code for the pinvoke wrapper, the generated code calls @func.
*/
-static void
-mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethodPInvoke *piinfo, MonoMarshalSpec **mspecs, gpointer func, gboolean check_exceptions)
+void
+mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethodPInvoke *piinfo, MonoMarshalSpec **mspecs, gpointer func, gboolean aot, gboolean check_exceptions)
{
EmitMarshalContext m;
MonoMethodSignature *csig;
if (sig->hasthis)
mono_mb_emit_byte (mb, CEE_LDARG_0);
-
for (i = 0; i < sig->param_count; i++) {
emit_marshal (&m, i + sig->hasthis, sig->params [i], mspecs [i + 1], tmp_locals [i], NULL, MARSHAL_ACTION_PUSH);
}
#endif
}
else {
- mono_mb_emit_native_call (mb, csig, func);
+ if (aot) {
+ /* Reuse the ICALL_ADDR opcode for pinvokes too */
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_op (mb, CEE_MONO_ICALL_ADDR, &piinfo->method);
+ mono_mb_emit_calli (mb, csig);
+ } else {
+ mono_mb_emit_native_call (mb, csig, func);
+ }
}
/* Set LastError if needed */
case MONO_TYPE_VALUETYPE:
klass = sig->ret->data.klass;
if (klass->enumtype) {
- type = sig->ret->data.klass->enum_basetype->type;
+ type = mono_class_enum_basetype (sig->ret->data.klass)->type;
goto handle_enum;
}
emit_marshal (&m, 0, sig->ret, spec, 0, NULL, MARSHAL_ACTION_CONV_RESULT);
case MONO_TYPE_SZARRAY:
case MONO_TYPE_CHAR:
case MONO_TYPE_PTR:
+ case MONO_TYPE_GENERICINST:
emit_marshal (&m, 0, sig->ret, spec, 0, NULL, MARSHAL_ACTION_CONV_RESULT);
break;
case MONO_TYPE_TYPEDBYREF:
g_assert (method != NULL);
g_assert (mono_method_signature (method)->pinvoke);
- cache = method->klass->image->native_wrapper_cache;
+ if (aot)
+ cache = get_cache (&method->klass->image->native_wrapper_aot_cache, mono_aligned_addr_hash, NULL);
+ else
+ cache = get_cache (&method->klass->image->native_wrapper_cache, mono_aligned_addr_hash, NULL);
if ((res = mono_marshal_find_in_cache (cache, method)))
return res;
if (MONO_CLASS_IS_IMPORT (method->klass)) {
#ifndef DISABLE_COM
- return cominterop_get_native_wrapper (method);
+ return mono_cominterop_get_native_wrapper (method);
#else
g_assert_not_reached ();
#endif
}
g_assert (pinvoke);
- g_assert (piinfo->addr);
+ if (!aot)
+ g_assert (piinfo->addr);
mspecs = g_new (MonoMarshalSpec*, sig->param_count + 1);
mono_method_get_marshal_info (method, mspecs);
- mono_marshal_emit_native_wrapper (mb->method->klass->image, mb, sig, piinfo, mspecs, piinfo->addr, check_exceptions);
+ mono_marshal_emit_native_wrapper (mb->method->klass->image, mb, sig, piinfo, mspecs, piinfo->addr, aot, check_exceptions);
csig = signature_dup (method->klass->image, sig);
csig->pinvoke = 0;
GHashTable *cache;
char *name;
- cache = image->native_wrapper_cache;
+ cache = get_cache (&image->native_wrapper_cache, mono_aligned_addr_hash, NULL);
if ((res = mono_marshal_find_in_cache (cache, func)))
return res;
mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_MANAGED_TO_NATIVE);
mb->method->save_lmf = 1;
- mono_marshal_emit_native_wrapper (image, mb, sig, piinfo, mspecs, func, TRUE);
+ mono_marshal_emit_native_wrapper (image, mb, sig, piinfo, mspecs, func, FALSE, TRUE);
csig = signature_dup (image, sig);
csig->pinvoke = 0;
return res;
}
-/* FIXME: moving GC */
-static void
-mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *invoke_sig, MonoMarshalSpec **mspecs, EmitMarshalContext* m, MonoMethod *method, MonoObject* this)
+/*
+ * mono_marshal_emit_managed_wrapper:
+ *
+ * Emit the body of a native-to-managed wrapper. INVOKE_SIG is the signature of
+ * the delegate which wraps the managed method to be called. For closed delegates,
+ * it could have fewer parameters than the method it wraps.
+ * THIS_LOC is the memory location where the target of the delegate is stored.
+ */
+void
+mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *invoke_sig, MonoMarshalSpec **mspecs, EmitMarshalContext* m, MonoMethod *method, MonoObject** this_loc)
{
MonoMethodSignature *sig, *csig;
int i, *tmp_locals;
+ gboolean closed = FALSE;
sig = m->sig;
csig = m->csig;
/* allocate local 2 (boolean) delete_old */
mono_mb_add_local (mb, &mono_defaults.boolean_class->byval_arg);
+ if (!sig->hasthis && sig->param_count != invoke_sig->param_count) {
+ /* Closed delegate */
+ g_assert (sig->param_count == invoke_sig->param_count + 1);
+ closed = TRUE;
+ /* Use a new signature without the first argument */
+ sig = mono_metadata_signature_dup (sig);
+ memmove (&sig->params [0], &sig->params [1], (sig->param_count - 1) * sizeof (MonoType*));
+ sig->param_count --;
+ }
+
if (!MONO_TYPE_IS_VOID(sig->ret)) {
/* allocate local 3 to store the return value */
mono_mb_add_local (mb, sig->ret);
tmp_locals = alloca (sizeof (int) * sig->param_count);
for (i = 0; i < sig->param_count; i ++) {
MonoType *t = sig->params [i];
-
+
switch (t->type) {
case MONO_TYPE_OBJECT:
case MONO_TYPE_CLASS:
case MONO_TYPE_ARRAY:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_STRING:
+ case MONO_TYPE_BOOLEAN:
tmp_locals [i] = emit_marshal (m, i, sig->params [i], mspecs [i + 1], 0, &csig->params [i], MARSHAL_ACTION_MANAGED_CONV_IN);
break;
emit_thread_interrupt_checkpoint (mb);
- /* fixme: howto handle this ? */
if (sig->hasthis) {
- if (this) {
- /* FIXME: need a solution for the moving GC here */
- mono_mb_emit_ptr (mb, this);
+ if (this_loc) {
+ mono_mb_emit_ptr (mb, this_loc);
+ mono_mb_emit_byte (mb, CEE_LDIND_REF);
} else {
/* fixme: */
g_assert_not_reached ();
}
- }
+ } else if (closed) {
+ mono_mb_emit_ptr (mb, this_loc);
+ mono_mb_emit_byte (mb, CEE_LDIND_REF);
+ }
for (i = 0; i < sig->param_count; i++) {
MonoType *t = sig->params [i];
if (mspecs [0] && mspecs [0]->native == MONO_NATIVE_CUSTOM) {
emit_marshal (m, 0, sig->ret, mspecs [0], 0, NULL, MARSHAL_ACTION_MANAGED_CONV_RESULT);
- }
- else
- if (!sig->ret->byref) {
+ } else if (!sig->ret->byref) {
switch (sig->ret->type) {
case MONO_TYPE_VOID:
break;
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_I1:
case MONO_TYPE_U1:
+ case MONO_TYPE_CHAR:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_I4:
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_OBJECT:
case MONO_TYPE_STRING:
+ case MONO_TYPE_BOOLEAN:
emit_marshal (m, i, t, mspecs [i + 1], tmp_locals [i], NULL, MARSHAL_ACTION_MANAGED_CONV_OUT);
break;
}
mono_mb_emit_ldloc (mb, 3);
mono_mb_emit_byte (mb, CEE_RET);
}
+
+ if (closed)
+ g_free (sig);
}
* generates IL code to call managed methods from unmanaged code
*/
MonoMethod *
-mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, MonoObject *this)
+mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, MonoObject **this_loc)
{
static MonoClass *UnmanagedFunctionPointerAttribute;
MonoMethodSignature *sig, *csig, *invoke_sig;
* options.
*/
cache = get_cache (&method->klass->image->managed_wrapper_cache, mono_aligned_addr_hash, NULL);
- if (!this && (res = mono_marshal_find_in_cache (cache, method)))
+ if (!this_loc && (res = mono_marshal_find_in_cache (cache, method)))
return res;
- invoke = mono_class_get_method_from_name (delegate_klass, "Invoke", mono_method_signature (method)->param_count);
+ invoke = mono_get_delegate_invoke (delegate_klass);
invoke_sig = mono_method_signature (invoke);
mspecs = g_new0 (MonoMarshalSpec*, mono_method_signature (invoke)->param_count + 1);
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
-
/* we copy the signature, so that we can modify it */
- if (this)
+ if (this_loc)
/* Need to free this later */
- csig = mono_metadata_signature_dup (sig);
+ csig = mono_metadata_signature_dup (invoke_sig);
else
- csig = signature_dup (method->klass->image, sig);
+ csig = signature_dup (method->klass->image, invoke_sig);
csig->hasthis = 0;
csig->pinvoke = 1;
}
}
- mono_marshal_emit_managed_wrapper (mb, invoke_sig, mspecs, &m, method, this);
+ mono_marshal_emit_managed_wrapper (mb, invoke_sig, mspecs, &m, method, this_loc);
- if (!this)
+ if (!this_loc)
res = mono_mb_create_and_cache (cache, method,
mb, csig, sig->param_count + 16);
else {
mono_mb_emit_byte (mb, CEE_RET);
- res = mono_mb_create_method (mb, signature_no_pinvoke (stoptr), 0);
+ res = mono_mb_create_method (mb, mono_signature_no_pinvoke (stoptr), 0);
mono_mb_free (mb);
klass->marshal_info->str_to_ptr = res;
MonoMethod *
mono_marshal_get_synchronized_wrapper (MonoMethod *method)
{
- static MonoMethod *enter_method, *exit_method;
+ static MonoMethod *enter_method, *exit_method, *gettypefromhandle_method;
MonoMethodSignature *sig;
MonoExceptionClause *clause;
- MonoMethodHeader *header;
MonoMethodBuilder *mb;
MonoMethod *res;
GHashTable *cache;
/* this */
this_local = mono_mb_add_local (mb, &mono_defaults.object_class->byval_arg);
- mono_loader_lock ();
clause = mono_image_alloc0 (method->klass->image, sizeof (MonoExceptionClause));
- mono_loader_unlock ();
clause->flags = MONO_EXCEPTION_CLAUSE_FINALLY;
+ mono_loader_lock ();
+
if (!enter_method) {
MonoMethodDesc *desc;
enter_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
g_assert (enter_method);
mono_method_desc_free (desc);
+
desc = mono_method_desc_new ("Monitor:Exit", FALSE);
exit_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
g_assert (exit_method);
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);
+ g_assert (gettypefromhandle_method);
+ mono_method_desc_free (desc);
}
+ mono_loader_unlock ();
+
/* Push this or the type object */
if (method->flags & METHOD_ATTRIBUTE_STATIC) {
- /*
- * GetTypeFromHandle isn't called as a managed method because it has
- * a funky calling sequence, e.g. ldtoken+GetTypeFromHandle gets
- * transformed into something else by the JIT.
- */
- mono_mb_emit_ptr (mb, &method->klass->byval_arg);
- mono_mb_emit_icall (mb, type_from_handle);
+ /* We have special handling for this in the JIT */
+ int index = mono_mb_add_data (mb, method->klass);
+ mono_mb_add_data (mb, mono_defaults.typehandle_class);
+ mono_mb_emit_byte (mb, CEE_LDTOKEN);
+ mono_mb_emit_i4 (mb, index);
+
+ mono_mb_emit_managed_call (mb, gettypefromhandle_method, NULL);
}
else
mono_mb_emit_ldarg (mb, 0);
mono_mb_emit_ldloc (mb, ret_local);
mono_mb_emit_byte (mb, CEE_RET);
+ mono_mb_set_clauses (mb, 1, clause);
+
res = mono_mb_create_and_cache (cache, method,
mb, sig, sig->param_count + 16);
mono_mb_free (mb);
- header = ((MonoMethodNormal *)res)->header;
- header->num_clauses = 1;
- header->clauses = clause;
-
return res;
}
return ret;
}
-MonoMethod*
-mono_marshal_get_write_barrier (void)
-{
- static MonoMethod* ret = NULL;
- MonoMethodSignature *sig;
- MonoMethodBuilder *mb;
- int max_stack = 2;
-
- if (ret)
- return ret;
-
- mb = mono_mb_new (mono_defaults.object_class, "writebarrier", MONO_WRAPPER_WRITE_BARRIER);
-
- sig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
-
- /* void writebarrier (MonoObject** addr, MonoObject* obj) */
- sig->ret = &mono_defaults.void_class->byval_arg;
- sig->params [0] = &mono_defaults.object_class->this_arg;
- sig->params [1] = &mono_defaults.object_class->byval_arg;
-
- /* just the store right now: add an hook for the GC to use, maybe something
- * that can be used for stelemref as well
- * We need a write barrier variant to be used with struct copies as well, though
- * there are also other approaches possible, like writing a wrapper specific to
- * the struct or to the reference pattern in the struct...
- * Depending on the GC, we may want variants that take the object we store to
- * when it is available.
- */
- mono_mb_emit_ldarg (mb, 0);
- mono_mb_emit_ldarg (mb, 1);
- mono_mb_emit_icall (mb, mono_gc_wbarrier_generic_store);
- /*mono_mb_emit_byte (mb, CEE_STIND_REF);*/
-
- mono_mb_emit_byte (mb, CEE_RET);
-
- ret = mono_mb_create_method (mb, sig, max_stack);
- mono_mb_free (mb);
- return ret;
-}
-
void*
mono_marshal_alloc (gulong size)
{
}
}
-MonoString *
-ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR (gpointer ptr)
+guint32
+ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
{
MONO_ARCH_SAVE_REGS;
- return mono_string_from_bstr(ptr);
+ return (GPOINTER_TO_INT (TlsGetValue (last_error_tls_id)));
}
-gpointer
-ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR (MonoString* ptr)
+guint32
+ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rtype)
{
+ MonoClass *klass;
+ MonoType *type;
+ guint32 layout;
+
MONO_ARCH_SAVE_REGS;
- return mono_string_to_bstr(ptr);
-}
+ MONO_CHECK_ARG_NULL (rtype);
-typedef struct
-{
- int (STDCALL *QueryInterface)(gpointer pUnk, gpointer riid, gpointer* ppv);
- int (STDCALL *AddRef)(gpointer pUnk);
- int (STDCALL *Release)(gpointer pUnk);
-} MonoIUnknown;
+ type = rtype->type;
+ klass = mono_class_from_mono_type (type);
+ layout = (klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK);
+
+ if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
+ gchar *msg;
+ MonoException *exc;
+
+ msg = g_strdup_printf ("Type %s cannot be marshaled as an unmanaged structure.", klass->name);
+ exc = mono_get_exception_argument ("t", msg);
+ g_free (msg);
+ mono_raise_exception (exc);
+ }
+
+
+ return mono_class_native_size (klass, NULL);
+}
void
-ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (gpointer ptr)
+ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr (MonoObject *obj, gpointer dst, MonoBoolean delete_old)
{
+ MonoMethod *method;
+ gpointer pa [3];
+
MONO_ARCH_SAVE_REGS;
- mono_free_bstr (ptr);
-}
+ MONO_CHECK_ARG_NULL (obj);
+ MONO_CHECK_ARG_NULL (dst);
-int
-ves_icall_System_Runtime_InteropServices_Marshal_AddRefInternal (gpointer pUnk)
-{
- g_assert (pUnk);
- return (*(MonoIUnknown**)pUnk)->AddRef(pUnk);
-}
+ method = mono_marshal_get_struct_to_ptr (obj->vtable->klass);
-int
-ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointer pUnk, gpointer riid, gpointer* ppv)
-{
- g_assert (pUnk);
- return (*(MonoIUnknown**)pUnk)->QueryInterface(pUnk, riid, ppv);
+ pa [0] = obj;
+ pa [1] = &dst;
+ pa [2] = &delete_old;
+
+ mono_runtime_invoke (method, NULL, pa, NULL);
}
-int
-ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk)
+static void
+ptr_to_structure (gpointer src, MonoObject *dst)
{
- g_assert (pUnk);
- return (*(MonoIUnknown**)pUnk)->Release(pUnk);
-}
+ MonoMethod *method;
+ gpointer pa [2];
-#ifndef DISABLE_COM
+ method = mono_marshal_get_ptr_to_struct (dst->vtable->klass);
-static void*
-cominterop_get_idispatch_for_object (MonoObject* object)
-{
- if (!object)
- return NULL;
+ pa [0] = &src;
+ pa [1] = dst;
- if (cominterop_object_is_rcw (object)) {
- return cominterop_get_interface (((MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp)->com_object,
- mono_defaults.idispatch_class, TRUE);
- }
- else {
- return cominterop_get_ccw (object, mono_defaults.idispatch_class);
- }
+ mono_runtime_invoke (method, NULL, pa, NULL);
}
-#endif
-
-void*
-ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal (MonoObject* object)
+void
+ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure (gpointer src, MonoObject *dst)
{
-#ifndef DISABLE_COM
- if (!object)
- return NULL;
+ MonoType *t;
- if (cominterop_object_is_rcw (object)) {
- MonoClass *klass = NULL;
- MonoRealProxy* real_proxy = NULL;
- if (!object)
- return NULL;
- klass = mono_object_class (object);
- if (klass != mono_defaults.transparent_proxy_class) {
- g_assert_not_reached ();
- return NULL;
- }
+ MONO_ARCH_SAVE_REGS;
- real_proxy = ((MonoTransparentProxy*)object)->rp;
- if (!real_proxy) {
- g_assert_not_reached ();
- return NULL;
- }
+ MONO_CHECK_ARG_NULL (src);
+ MONO_CHECK_ARG_NULL (dst);
+
+ t = mono_type_get_underlying_type (mono_class_get_type (dst->vtable->klass));
- klass = mono_object_class (real_proxy);
- if (klass != mono_defaults.com_interop_proxy_class) {
- g_assert_not_reached ();
- return NULL;
- }
+ if (t->type == MONO_TYPE_VALUETYPE) {
+ MonoException *exc;
+ gchar *tmp;
- if (!((MonoComInteropProxy*)real_proxy)->com_object) {
- g_assert_not_reached ();
- return NULL;
- }
+ tmp = g_strdup_printf ("Destination is a boxed value type.");
+ exc = mono_get_exception_argument ("dst", tmp);
+ g_free (tmp);
- return ((MonoComInteropProxy*)real_proxy)->com_object->iunknown;
- }
- else {
- return cominterop_get_ccw (object, mono_defaults.iunknown_class);
+ mono_raise_exception (exc);
+ return;
}
-#else
- g_assert_not_reached ();
-#endif
+
+ ptr_to_structure (src, dst);
}
-MonoObject*
-ves_icall_System_Runtime_InteropServices_Marshal_GetObjectForCCW (void* pUnk)
+MonoObject *
+ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type (gpointer src, MonoReflectionType *type)
{
-#ifndef DISABLE_COM
- MonoObject* object = NULL;
-
- if (!pUnk)
- return NULL;
+ MonoDomain *domain = mono_domain_get ();
+ MonoObject *res;
- /* see if it is a CCW */
- object = cominterop_get_ccw_object ((MonoCCWInterface*)pUnk, TRUE);
-
- return object;
-#else
- g_assert_not_reached ();
-#endif
-}
-
-void*
-ves_icall_System_Runtime_InteropServices_Marshal_GetIDispatchForObjectInternal (MonoObject* object)
-{
-#ifndef DISABLE_COM
- return cominterop_get_idispatch_for_object (object);
-#else
- g_assert_not_reached ();
-#endif
-}
-
-void*
-ves_icall_System_Runtime_InteropServices_Marshal_GetCCW (MonoObject* object, MonoReflectionType* type)
-{
-#ifndef DISABLE_COM
- MonoClass* klass = NULL;
- void* itf = NULL;
- g_assert (type);
- g_assert (type->type);
- klass = mono_type_get_class (type->type);
- g_assert (klass);
- itf = cominterop_get_ccw (object, klass);
- g_assert (itf);
- return itf;
-#else
- g_assert_not_reached ();
-#endif
-}
-
-
-MonoBoolean
-ves_icall_System_Runtime_InteropServices_Marshal_IsComObject (MonoObject* object)
-{
-#ifndef DISABLE_COM
- return (MonoBoolean)cominterop_object_is_rcw (object);
-#else
- g_assert_not_reached ();
-#endif
-}
-
-gint32
-ves_icall_System_Runtime_InteropServices_Marshal_ReleaseComObjectInternal (MonoObject* object)
-{
-#ifndef DISABLE_COM
- MonoComInteropProxy* proxy = NULL;
- gint32 ref_count = 0;
-
- g_assert (object);
- g_assert (cominterop_object_is_rcw (object));
-
- proxy = (MonoComInteropProxy*)((MonoTransparentProxy*)object)->rp;
- g_assert (proxy);
-
- ref_count = InterlockedDecrement (&proxy->ref_count);
- g_assert (ref_count >= 0);
-
- if (ref_count == 0)
- ves_icall_System_ComObject_ReleaseInterfaces (proxy->com_object);
-
- return ref_count;
-#else
- g_assert_not_reached ();
-#endif
-}
-
-guint32
-ves_icall_System_Runtime_InteropServices_Marshal_GetComSlotForMethodInfoInternal (MonoReflectionMethod *m)
-{
- MONO_ARCH_SAVE_REGS;
-
-#ifndef DISABLE_COM
- return cominterop_get_com_slot_for_method (m->method);
-#else
- g_assert_not_reached ();
-#endif
-}
-
-guint32
-ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
-{
- MONO_ARCH_SAVE_REGS;
-
- return (GPOINTER_TO_INT (TlsGetValue (last_error_tls_id)));
-}
-
-guint32
-ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rtype)
-{
- MonoClass *klass;
- MonoType *type;
- guint32 layout;
-
- MONO_ARCH_SAVE_REGS;
-
- MONO_CHECK_ARG_NULL (rtype);
-
- type = rtype->type;
- klass = mono_class_from_mono_type (type);
- layout = (klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK);
-
- if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
- gchar *msg;
- MonoException *exc;
-
- msg = g_strdup_printf ("Type %s cannot be marshaled as an unmanaged structure.", klass->name);
- exc = mono_get_exception_argument ("t", msg);
- g_free (msg);
- mono_raise_exception (exc);
- }
-
-
- return mono_class_native_size (klass, NULL);
-}
-
-void
-ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr (MonoObject *obj, gpointer dst, MonoBoolean delete_old)
-{
- MonoMethod *method;
- gpointer pa [3];
-
- MONO_ARCH_SAVE_REGS;
-
- MONO_CHECK_ARG_NULL (obj);
- MONO_CHECK_ARG_NULL (dst);
-
- method = mono_marshal_get_struct_to_ptr (obj->vtable->klass);
-
- pa [0] = obj;
- pa [1] = &dst;
- pa [2] = &delete_old;
-
- mono_runtime_invoke (method, NULL, pa, NULL);
-}
-
-static void
-ptr_to_structure (gpointer src, MonoObject *dst)
-{
- MonoMethod *method;
- gpointer pa [2];
-
- method = mono_marshal_get_ptr_to_struct (dst->vtable->klass);
-
- pa [0] = &src;
- pa [1] = dst;
-
- mono_runtime_invoke (method, NULL, pa, NULL);
-}
-
-void
-ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure (gpointer src, MonoObject *dst)
-{
- MonoType *t;
-
- MONO_ARCH_SAVE_REGS;
-
- MONO_CHECK_ARG_NULL (src);
- MONO_CHECK_ARG_NULL (dst);
-
- t = mono_type_get_underlying_type (mono_class_get_type (dst->vtable->klass));
-
- if (t->type == MONO_TYPE_VALUETYPE) {
- MonoException *exc;
- gchar *tmp;
-
- tmp = g_strdup_printf ("Destination is a boxed value type.");
- exc = mono_get_exception_argument ("dst", tmp);
- g_free (tmp);
-
- mono_raise_exception (exc);
- return;
- }
-
- ptr_to_structure (src, dst);
-}
-
-MonoObject *
-ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type (gpointer src, MonoReflectionType *type)
-{
- MonoDomain *domain = mono_domain_get ();
- MonoObject *res;
-
- MONO_ARCH_SAVE_REGS;
+ MONO_ARCH_SAVE_REGS;
MONO_CHECK_ARG_NULL (src);
MONO_CHECK_ARG_NULL (type);
while ((field = mono_class_get_fields (klass, &iter))) {
if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
continue;
- if (!strcmp (fname, field->name)) {
+ if (!strcmp (fname, mono_field_get_name (field))) {
match_index = i;
break;
}
return mono_ftnptr_to_delegate (mono_type_get_class (type->type), ftn);
}
-/* Only used for COM RCWs */
-MonoObject *
-ves_icall_System_ComObject_CreateRCW (MonoReflectionType *type)
-{
- MonoClass *klass;
- MonoDomain *domain;
- MonoObject *obj;
-
- MONO_ARCH_SAVE_REGS;
-
- domain = mono_object_domain (type);
- klass = mono_class_from_mono_type (type->type);
-
- /* call mono_object_new_alloc_specific instead of mono_object_new
- * because we want to actually create object. mono_object_new checks
- * to see if type is import and creates transparent proxy. this method
- * is called by the corresponding real proxy to create the real RCW.
- * Constructor does not need to be called. Will be called later.
- */
- obj = mono_object_new_alloc_specific (mono_class_vtable (domain, klass));
- return obj;
-}
-
-static gboolean
-cominterop_finalizer (gpointer key, gpointer value, gpointer user_data)
-{
- ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (value);
- return TRUE;
-}
-
-void
-ves_icall_System_ComObject_ReleaseInterfaces (MonoComObject* obj)
-{
- g_assert(obj);
- if (obj->itf_hash) {
- guint32 gchandle = 0;
- mono_cominterop_lock ();
- gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash, obj->iunknown));
- if (gchandle) {
- mono_gchandle_free (gchandle);
- g_hash_table_remove (rcw_hash, obj->iunknown);
- }
-
- g_hash_table_foreach_remove (obj->itf_hash, cominterop_finalizer, NULL);
- ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (obj->iunknown);
- obj->itf_hash = obj->iunknown = NULL;
- mono_cominterop_unlock ();
- }
-}
-
-gpointer
-ves_icall_System_ComObject_GetInterfaceInternal (MonoComObject* obj, MonoReflectionType* type, MonoBoolean throw_exception)
-{
-#ifndef DISABLE_COM
- return cominterop_get_interface (obj, mono_type_get_class (type->type), (gboolean)throw_exception);
-#else
- g_assert_not_reached ();
-#endif
-}
-
-void
-ves_icall_Mono_Interop_ComInteropProxy_AddProxy (gpointer pUnk, MonoComInteropProxy* proxy)
-{
-#ifndef DISABLE_COM
- guint32 gchandle = 0;
- if (!rcw_hash) {
- mono_cominterop_lock ();
- rcw_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
- mono_cominterop_unlock ();
- }
-
- gchandle = mono_gchandle_new_weakref ((MonoObject*)proxy, FALSE);
-
- mono_cominterop_lock ();
- g_hash_table_insert (rcw_hash, pUnk, GUINT_TO_POINTER (gchandle));
- mono_cominterop_unlock ();
-#else
- g_assert_not_reached ();
-#endif
-}
-
-MonoComInteropProxy*
-ves_icall_Mono_Interop_ComInteropProxy_FindProxy (gpointer pUnk)
-{
-#ifndef DISABLE_COM
- MonoComInteropProxy* proxy = NULL;
- guint32 gchandle = 0;
-
- mono_cominterop_lock ();
- if (rcw_hash)
- gchandle = GPOINTER_TO_UINT (g_hash_table_lookup (rcw_hash, pUnk));
- mono_cominterop_unlock ();
- if (gchandle) {
- proxy = (MonoComInteropProxy*)mono_gchandle_get_target (gchandle);
- /* proxy is null means we need to free up old RCW */
- if (!proxy) {
- mono_gchandle_free (gchandle);
- g_hash_table_remove (rcw_hash, pUnk);
- }
- }
- return proxy;
-#else
- g_assert_not_reached ();
-#endif
-}
-
/**
* mono_marshal_is_loading_type_info:
*
layout = klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
/* The mempool is protected by the loader lock */
- info = mono_image_alloc0 (klass->image, sizeof (MonoMarshalType) + sizeof (MonoMarshalField) * count);
+ info = mono_image_alloc0 (klass->image, MONO_SIZEOF_MARSHAL_TYPE + sizeof (MonoMarshalField) * count);
info->num_fields = count;
/* Try to find a size for this type in metadata */
if (mono_field_is_deleted (field))
continue;
if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
- mono_metadata_field_info_with_mempool (klass->image->mempool, klass->image, mono_metadata_token_index (mono_class_get_field_token (field)) - 1,
+ mono_metadata_field_info_with_mempool (klass->image, mono_metadata_token_index (mono_class_get_field_token (field)) - 1,
NULL, NULL, &info->fields [j].mspec);
info->fields [j].field = field;
if ((mono_class_num_fields (klass) == 1) && (klass->instance_size == sizeof (MonoObject)) &&
- (strcmp (field->name, "$PRIVATE$") == 0)) {
+ (strcmp (mono_field_get_name (field), "$PRIVATE$") == 0)) {
/* This field is a hack inserted by MCS to empty structures */
continue;
}
info->native_size &= ~(min_align - 1);
}
+ info->min_align = min_align;
+
/* Update the class's blittable info, if the layouts don't match */
if (info->native_size != mono_class_value_size (klass, NULL))
klass->blittable = FALSE;
}
if (align)
- *align = klass->min_align;
+ *align = klass->marshal_info->min_align;
return klass->marshal_info->native_size;
}
+/* __alignof__ returns the preferred alignment of values not the actual alignment used by
+ the compiler so is wrong e.g. for Linux where doubles are aligned on a 4 byte boundary
+ but __alignof__ returns 8 - using G_STRUCT_OFFSET works better */
+#define ALIGNMENT(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
+
/*
* mono_type_native_stack_size:
* @t: the type to return the size it uses on the stack
align = &tmp;
if (t->byref) {
- *align = 4;
- return 4;
+ *align = sizeof (gpointer);
+ return sizeof (gpointer);
}
switch (t->type){
case MONO_TYPE_U2:
case MONO_TYPE_I4:
case MONO_TYPE_U4:
+ *align = 4;
+ return 4;
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_STRING:
case MONO_TYPE_PTR:
case MONO_TYPE_FNPTR:
case MONO_TYPE_ARRAY:
- *align = 4;
- return 4;
+ *align = sizeof (gpointer);
+ return sizeof (gpointer);
case MONO_TYPE_R4:
*align = 4;
return 4;
+ case MONO_TYPE_R8:
+ *align = ALIGNMENT (gdouble);
+ return 8;
case MONO_TYPE_I8:
case MONO_TYPE_U8:
- case MONO_TYPE_R8:
- *align = 4;
+ *align = ALIGNMENT (glong);
return 8;
+ case MONO_TYPE_GENERICINST:
+ if (!mono_type_generic_inst_is_valuetype (t)) {
+ *align = sizeof (gpointer);
+ return sizeof (gpointer);
+ }
+ /* Fall through */
case MONO_TYPE_TYPEDBYREF:
case MONO_TYPE_VALUETYPE: {
guint32 size;
+ MonoClass *klass = mono_class_from_mono_type (t);
- if (t->data.klass->enumtype)
- return mono_type_native_stack_size (t->data.klass->enum_basetype, align);
+ if (klass->enumtype)
+ return mono_type_native_stack_size (mono_class_enum_basetype (klass), align);
else {
- size = mono_class_native_size (t->data.klass, align);
+ size = mono_class_native_size (klass, align);
*align = *align + 3;
*align &= ~3;
return 0;
}
-/* __alignof__ returns the preferred alignment of values not the actual alignment used by
- the compiler so is wrong e.g. for Linux where doubles are aligned on a 4 byte boundary
- but __alignof__ returns 8 - using G_STRUCT_OFFSET works better */
-#define ALIGNMENT(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
-
gint32
mono_marshal_type_size (MonoType *type, MonoMarshalSpec *mspec, guint32 *align,
gboolean as_field, gboolean unicode)
mono_mb_emit_managed_call (mb, method, NULL);
mono_mb_emit_byte (mb, CEE_RET);
- res = mono_mb_create_method (mb, csig, csig->param_count + 16);
-
/* We can corlib internal methods */
- res->skip_visibility = TRUE;
+ mb->skip_visibility = TRUE;
+
+ res = mono_mb_create_method (mb, csig, csig->param_count + 16);
mono_mb_free (mb);
memset (dest, 0, length);
}
-/* Put COM Interop related stuff here */
-
-#ifndef DISABLE_COM
-
-/**
- * cominterop_get_ccw_object:
- * @ccw_entry: a pointer to the CCWEntry
- * @verify: verify ccw_entry is in fact a ccw
- *
- * Returns: the corresponding object for the CCW
- */
-static MonoObject*
-cominterop_get_ccw_object (MonoCCWInterface* ccw_entry, gboolean verify)
-{
- MonoCCW *ccw = NULL;
-
- /* no CCW's exist yet */
- if (!ccw_interface_hash)
- return NULL;
-
- if (verify) {
- ccw = g_hash_table_lookup (ccw_interface_hash, ccw_entry);
- }
- else {
- ccw = ccw_entry->ccw;
- g_assert (ccw);
- }
- if (ccw)
- return mono_gchandle_get_target (ccw->gc_handle);
- else
- return NULL;
-}
-
-static void
-cominterop_setup_marshal_context (EmitMarshalContext *m, MonoMethod *method)
-{
- MonoMethodSignature *sig, *csig;
- sig = mono_method_signature (method);
- /* we copy the signature, so that we can modify it */
- /* FIXME: which to use? */
- csig = signature_dup (method->klass->image, sig);
- /* csig = mono_metadata_signature_dup (sig); */
-
- /* STDCALL on windows, CDECL everywhere else to work with XPCOM and MainWin COM */
-#ifdef PLATFORM_WIN32
- csig->call_convention = MONO_CALL_STDCALL;
-#else
- csig->call_convention = MONO_CALL_C;
-#endif
- csig->hasthis = 0;
- csig->pinvoke = 1;
-
- m->image = method->klass->image;
- m->piinfo = NULL;
- m->retobj_var = 0;
- m->sig = sig;
- m->csig = csig;
-}
-
-/**
- * cominterop_get_ccw:
- * @object: a pointer to the object
- * @itf: interface type needed
- *
- * Returns: a value indicating if the object is a
- * Runtime Callable Wrapper (RCW) for a COM object
- */
-static gpointer
-cominterop_get_ccw (MonoObject* object, MonoClass* itf)
-{
- int i;
- MonoCCW *ccw = NULL;
- MonoCCWInterface* ccw_entry = NULL;
- gpointer *vtable = NULL;
- static gpointer iunknown[3] = {NULL, NULL, NULL};
- static gpointer idispatch[4] = {NULL, NULL, NULL, NULL};
- MonoClass* iface = NULL;
- MonoClass* klass = NULL;
- EmitMarshalContext m;
- int start_slot = 3;
- int method_count = 0;
- GList *ccw_list, *ccw_list_item;
- MonoCustomAttrInfo *cinfo = NULL;
-
- if (!object)
- return NULL;
-
- klass = mono_object_get_class (object);
-
- if (!ccw_hash)
- ccw_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
- if (!ccw_interface_hash)
- ccw_interface_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-
- ccw_list = g_hash_table_lookup (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
-
- ccw_list_item = ccw_list;
- while (ccw_list_item) {
- MonoCCW* ccw_iter = ccw_list_item->data;
- if (mono_gchandle_get_target (ccw_iter->gc_handle) == object) {
- ccw = ccw_iter;
- break;
- }
- ccw_list_item = g_list_next(ccw_list_item);
- }
-
- if (!iunknown [0]) {
- iunknown [0] = cominterop_ccw_queryinterface;
- iunknown [1] = cominterop_ccw_addref;
- iunknown [2] = cominterop_ccw_release;
- }
-
- if (!idispatch [0]) {
- idispatch [0] = cominterop_ccw_get_type_info_count;
- idispatch [1] = cominterop_ccw_get_type_info;
- idispatch [2] = cominterop_ccw_get_ids_of_names;
- idispatch [3] = cominterop_ccw_invoke;
- }
-
- if (!ccw) {
- ccw = g_new0 (MonoCCW, 1);
- ccw->vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
- ccw->ref_count = 0;
- /* just alloc a weak handle until we are addref'd*/
- ccw->gc_handle = mono_gchandle_new_weakref (object, FALSE);
-
- if (!ccw_list) {
- ccw_list = g_list_alloc ();
- ccw_list->data = ccw;
- }
- else
- ccw_list = g_list_append (ccw_list, ccw);
- g_hash_table_insert (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)), ccw_list);
- /* register for finalization to clean up ccw */
- mono_object_register_finalizer (object);
- }
-
- cinfo = mono_custom_attrs_from_class (itf);
- if (cinfo) {
- static MonoClass* coclass_attribute = NULL;
- if (!coclass_attribute)
- coclass_attribute = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "CoClassAttribute");
- if (mono_custom_attrs_has_attr (cinfo, coclass_attribute)) {
- g_assert(itf->interface_count && itf->interfaces[0]);
- itf = itf->interfaces[0];
- }
- if (!cinfo->cached)
- mono_custom_attrs_free (cinfo);
- }
-
- iface = itf;
- if (iface == mono_defaults.iunknown_class) {
- start_slot = 3;
- }
- else if (iface == mono_defaults.idispatch_class) {
- start_slot = 7;
- }
- else {
- method_count += iface->method.count;
- start_slot = cominterop_get_com_slot_begin (iface);
- iface = NULL;
- }
-
- ccw_entry = g_hash_table_lookup (ccw->vtable_hash, itf);
-
- if (!ccw_entry) {
- int vtable_index = method_count-1+start_slot;
- mono_loader_lock ();
- vtable = mono_image_alloc0 (klass->image, sizeof (gpointer)*(method_count+start_slot));
- mono_loader_unlock ();
- memcpy (vtable, iunknown, sizeof (iunknown));
- if (start_slot == 7)
- memcpy (vtable+3, idispatch, sizeof (idispatch));
-
- iface = itf;
- for (i = iface->method.count-1; i >= 0;i--) {
- int param_index = 0;
- MonoMethodBuilder *mb;
- MonoMarshalSpec ** mspecs;
- MonoMethod *wrapper_method, *adjust_method;
- MonoMethod *method = iface->methods [i];
- MonoMethodSignature* sig_adjusted;
- MonoMethodSignature* sig = mono_method_signature (method);
- gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-
-
- mb = mono_mb_new (iface, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
- adjust_method = cominterop_get_managed_wrapper_adjusted (method);
- sig_adjusted = mono_method_signature (adjust_method);
-
- mspecs = g_new (MonoMarshalSpec*, sig_adjusted->param_count + 1);
- mono_method_get_marshal_info (method, mspecs);
-
-
- /* move managed args up one */
- for (param_index = sig->param_count; param_index >= 1; param_index--) {
- int mspec_index = param_index+1;
- mspecs [mspec_index] = mspecs [param_index];
-
- if (mspecs[mspec_index] == NULL) {
- if (sig_adjusted->params[param_index]->type == MONO_TYPE_OBJECT) {
- mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
- mspecs[mspec_index]->native = MONO_NATIVE_STRUCT;
- }
- else if (sig_adjusted->params[param_index]->type == MONO_TYPE_STRING) {
- mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
- mspecs[mspec_index]->native = MONO_NATIVE_BSTR;
- }
- else if (sig_adjusted->params[param_index]->type == MONO_TYPE_CLASS) {
- mspecs[mspec_index] = g_new0 (MonoMarshalSpec, 1);
- mspecs[mspec_index]->native = MONO_NATIVE_INTERFACE;
- }
- }
- }
-
- /* first arg is IntPtr for interface */
- mspecs [1] = NULL;
-
- /* move return spec to last param */
- if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret)) {
- if (mspecs [0] == NULL) {
- if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_OBJECT) {
- mspecs[0] = g_new0 (MonoMarshalSpec, 1);
- mspecs[0]->native = MONO_NATIVE_STRUCT;
- }
- else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_STRING) {
- mspecs[0] = g_new0 (MonoMarshalSpec, 1);
- mspecs[0]->native = MONO_NATIVE_BSTR;
- }
- else if (sig_adjusted->params[sig_adjusted->param_count-1]->type == MONO_TYPE_CLASS) {
- mspecs[0] = g_new0 (MonoMarshalSpec, 1);
- mspecs[0]->native = MONO_NATIVE_INTERFACE;
- }
- }
-
- mspecs [sig_adjusted->param_count] = mspecs [0];
- mspecs [0] = NULL;
- }
-
- cominterop_setup_marshal_context (&m, adjust_method);
- m.mb = mb;
- mono_marshal_emit_managed_wrapper (mb, sig_adjusted, mspecs, &m, adjust_method, NULL);
- mono_loader_lock ();
- mono_marshal_lock ();
- wrapper_method = mono_mb_create_method (mb, m.csig, m.csig->param_count + 16);
- mono_marshal_unlock ();
- mono_loader_unlock ();
-
- /* skip visiblity since we call internal methods */
- wrapper_method->skip_visibility = TRUE;
-
- vtable [vtable_index--] = mono_compile_method (wrapper_method);
-
-
- for (param_index = sig_adjusted->param_count; param_index >= 0; param_index--)
- if (mspecs [param_index])
- mono_metadata_free_marshal_spec (mspecs [param_index]);
- g_free (mspecs);
- }
-
- ccw_entry = g_new0 (MonoCCWInterface, 1);
- ccw_entry->ccw = ccw;
- ccw_entry->vtable = vtable;
- g_hash_table_insert (ccw->vtable_hash, itf, ccw_entry);
- g_hash_table_insert (ccw_interface_hash, ccw_entry, ccw);
- }
-
- return ccw_entry;
-}
-
-static gboolean
-mono_marshal_free_ccw_entry (gpointer key, gpointer value, gpointer user_data)
-{
- g_assert (value);
- g_free (value);
- return TRUE;
-}
-
-/**
- * mono_marshal_free_ccw:
- * @object: the mono object
- *
- * Returns: whether the object had a CCW
- */
-gboolean
-mono_marshal_free_ccw (MonoObject* object)
-{
- GList *ccw_list, *ccw_list_orig, *ccw_list_item;
- /* no ccw's were created */
- if (!ccw_hash || g_hash_table_size (ccw_hash) == 0)
- return FALSE;
-
- /* need to cache orig list address to remove from hash_table if empty */
- mono_cominterop_lock ();
- ccw_list = ccw_list_orig = g_hash_table_lookup (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
- mono_cominterop_unlock ();
-
- if (!ccw_list)
- return FALSE;
-
- ccw_list_item = ccw_list;
- while (ccw_list_item) {
- MonoCCW* ccw_iter = ccw_list_item->data;
- MonoObject* handle_target = mono_gchandle_get_target (ccw_iter->gc_handle);
-
- /* Looks like the GC NULLs the weakref handle target before running the
- * finalizer. So if we get a NULL target, destroy the CCW as well. */
- if (!handle_target || handle_target == object) {
- /* remove all interfaces */
- g_hash_table_foreach_remove (ccw_iter->vtable_hash, mono_marshal_free_ccw_entry, NULL);
- g_hash_table_destroy (ccw_iter->vtable_hash);
-
- /* get next before we delete */
- ccw_list_item = g_list_next(ccw_list_item);
-
- /* remove ccw from list */
- ccw_list = g_list_remove (ccw_list, ccw_iter);
- g_free (ccw_iter);
- }
- else
- ccw_list_item = g_list_next(ccw_list_item);
- }
-
- /* if list is empty remove original address from hash */
- if (g_list_length (ccw_list) == 0)
- g_hash_table_remove (ccw_hash, GINT_TO_POINTER (mono_object_hash (object)));
-
-
- return TRUE;
-}
-
-/**
- * cominterop_get_managed_wrapper_adjusted:
- * @method: managed COM Interop method
- *
- * Returns: the generated method to call with signature matching
- * the unmanaged COM Method signature
- */
-static MonoMethod *
-cominterop_get_managed_wrapper_adjusted (MonoMethod *method)
-{
- static MonoMethod *get_hr_for_exception = NULL;
- MonoMethod *res = NULL;
- MonoMethodBuilder *mb;
- MonoMarshalSpec **mspecs;
- MonoMethodSignature *sig, *sig_native;
- MonoExceptionClause *main_clause = NULL;
- MonoMethodHeader *header;
- int pos_leave;
- int hr = 0;
- int i;
- gboolean preserve_sig = method->iflags & METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG;
-
- if (!get_hr_for_exception)
- get_hr_for_exception = mono_class_get_method_from_name (mono_defaults.marshal_class, "GetHRForException", -1);
-
- sig = mono_method_signature (method);
-
- /* create unmanaged wrapper */
- mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_COMINTEROP);
-
- sig_native = cominterop_method_signature (method);
-
- mspecs = g_new0 (MonoMarshalSpec*, sig_native->param_count+1);
-
- mono_method_get_marshal_info (method, mspecs);
-
- /* move managed args up one */
- for (i = sig->param_count; i >= 1; i--)
- mspecs [i+1] = mspecs [i];
-
- /* first arg is IntPtr for interface */
- mspecs [1] = NULL;
-
- /* move return spec to last param */
- if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
- mspecs [sig_native->param_count] = mspecs [0];
-
- mspecs [0] = NULL;
-
- if (!preserve_sig) {
- hr = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
-
- /* try */
- main_clause = g_new0 (MonoExceptionClause, 1);
- main_clause->try_offset = mono_mb_get_label (mb);
- }
-
- /* load last param to store result if not preserve_sig and not void */
- if (!preserve_sig && !MONO_TYPE_IS_VOID (sig->ret))
- mono_mb_emit_ldarg (mb, sig_native->param_count-1);
-
- /* the CCW -> object conversion */
- mono_mb_emit_ldarg (mb, 0);
- mono_mb_emit_icon (mb, FALSE);
- mono_mb_emit_icall (mb, cominterop_get_ccw_object);
-
- for (i = 0; i < sig->param_count; i++)
- mono_mb_emit_ldarg (mb, i+1);
-
- mono_mb_emit_managed_call (mb, method, NULL);
-
- if (!preserve_sig) {
- /* store result if not preserve_sig and we have one */
- if (!MONO_TYPE_IS_VOID (sig->ret))
- mono_mb_emit_byte (mb, mono_type_to_stind (sig->ret));
-
- pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
-
- /* Main exception catch */
- main_clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
- main_clause->try_len = mono_mb_get_pos (mb) - main_clause->try_offset;
- main_clause->data.catch_class = mono_defaults.object_class;
-
- /* handler code */
- main_clause->handler_offset = mono_mb_get_label (mb);
- mono_mb_emit_managed_call (mb, get_hr_for_exception, NULL);
- mono_mb_emit_stloc (mb, hr);
- mono_mb_emit_branch (mb, CEE_LEAVE);
- main_clause->handler_len = mono_mb_get_pos (mb) - main_clause->handler_offset;
- /* end catch */
-
- mono_mb_patch_branch (mb, pos_leave);
-
- mono_mb_emit_ldloc (mb, hr);
- }
-
- mono_mb_emit_byte (mb, CEE_RET);
-
- mono_loader_lock ();
- mono_marshal_lock ();
- res = mono_mb_create_method (mb, sig_native, sig_native->param_count + 16);
- mono_marshal_unlock ();
- mono_loader_unlock ();
-
- mono_mb_free (mb);
-
- for (i = sig_native->param_count; i >= 0; i--)
- if (mspecs [i])
- mono_metadata_free_marshal_spec (mspecs [i]);
- g_free (mspecs);
-
- if (!preserve_sig) {
- header = ((MonoMethodNormal *)res)->header;
- header->num_clauses = 1;
- header->clauses = main_clause;
- }
-
- return res;
-}
-
-/**
- * cominterop_mono_string_to_guid:
- *
- * Converts the standard string representation of a GUID
- * to a 16 byte Microsoft GUID.
- */
-static void
-cominterop_mono_string_to_guid (const MonoString* string, guint8 *guid) {
- gunichar2 * chars = mono_string_chars (string);
- int i = 0;
- static guint8 indexes[16] = {7, 5, 3, 1, 12, 10, 17, 15, 20, 22, 25, 27, 29, 31, 33, 35};
-
- for (i = 0; i < sizeof(indexes); i++)
- guid [i] = g_unichar_xdigit_value (chars [indexes [i]]) + (g_unichar_xdigit_value (chars [indexes [i] - 1]) << 4);
-}
-
-static gboolean
-cominterop_class_guid_equal (guint8* guid, MonoClass* klass)
-{
- guint8 klass_guid [16];
- if (cominterop_class_guid (klass, klass_guid))
- return !memcmp (guid, klass_guid, sizeof (klass_guid));
- return FALSE;
-}
-
-static int STDCALL
-cominterop_ccw_addref (MonoCCWInterface* ccwe)
-{
- gint32 ref_count = 0;
- MonoCCW* ccw = ccwe->ccw;
- g_assert (ccw);
- g_assert (ccw->gc_handle);
- g_assert (ccw->ref_count >= 0);
- ref_count = InterlockedIncrement ((gint32*)&ccw->ref_count);
- if (ref_count == 1) {
- guint32 oldhandle = ccw->gc_handle;
- g_assert (oldhandle);
- /* since we now have a ref count, alloc a strong handle*/
- ccw->gc_handle = mono_gchandle_new (mono_gchandle_get_target (oldhandle), FALSE);
- mono_gchandle_free (oldhandle);
- }
- return ref_count;
-}
-
-static int STDCALL
-cominterop_ccw_release (MonoCCWInterface* ccwe)
-{
- gint32 ref_count = 0;
- MonoCCW* ccw = ccwe->ccw;
- g_assert (ccw);
- g_assert (ccw->ref_count > 0);
- ref_count = InterlockedDecrement ((gint32*)&ccw->ref_count);
- if (ref_count == 0) {
- /* allow gc of object */
- guint32 oldhandle = ccw->gc_handle;
- g_assert (oldhandle);
- ccw->gc_handle = mono_gchandle_new_weakref (mono_gchandle_get_target (oldhandle), FALSE);
- mono_gchandle_free (oldhandle);
- }
- return ref_count;
-}
-
-#define MONO_S_OK 0x00000000L
-#define MONO_E_NOINTERFACE 0x80004002L
-#define MONO_E_NOTIMPL 0x80004001L
-
-static int STDCALL
-cominterop_ccw_queryinterface (MonoCCWInterface* ccwe, guint8* riid, gpointer* ppv)
-{
- GPtrArray *ifaces;
- MonoClass *itf = NULL;
- int i;
- MonoCCW* ccw = ccwe->ccw;
- MonoClass* klass = NULL;
- MonoObject* object = mono_gchandle_get_target (ccw->gc_handle);
-
- g_assert (object);
- klass = mono_object_class (object);
-
- if (ppv)
- *ppv = NULL;
-
- /* handle IUnknown special */
- if (cominterop_class_guid_equal (riid, mono_defaults.iunknown_class)) {
- *ppv = cominterop_get_ccw (object, mono_defaults.iunknown_class);
- /* remember to addref on QI */
- cominterop_ccw_addref (*ppv);
- return MONO_S_OK;
- }
-
- /* handle IDispatch special */
- if (cominterop_class_guid_equal (riid, mono_defaults.idispatch_class)) {
- *ppv = cominterop_get_ccw (object, mono_defaults.idispatch_class);
- /* remember to addref on QI */
- cominterop_ccw_addref (*ppv);
- return MONO_S_OK;
- }
-
- ifaces = mono_class_get_implemented_interfaces (klass);
- if (ifaces) {
- for (i = 0; i < ifaces->len; ++i) {
- MonoClass *ic = NULL;
- ic = g_ptr_array_index (ifaces, i);
- if (cominterop_class_guid_equal (riid, ic)) {
- itf = ic;
- break;
- }
- }
- g_ptr_array_free (ifaces, TRUE);
- }
- if (itf) {
- *ppv = cominterop_get_ccw (object, itf);
- /* remember to addref on QI */
- cominterop_ccw_addref (*ppv);
- return MONO_S_OK;
- }
-
- return MONO_E_NOINTERFACE;
-}
-
-static int STDCALL
-cominterop_ccw_get_type_info_count (MonoCCWInterface* ccwe, guint32 *pctinfo)
-{
- return MONO_E_NOTIMPL;
-}
-
-static int STDCALL
-cominterop_ccw_get_type_info (MonoCCWInterface* ccwe, guint32 iTInfo, guint32 lcid, gpointer *ppTInfo)
-{
- return MONO_E_NOTIMPL;
-}
-
-static int STDCALL
-cominterop_ccw_get_ids_of_names (MonoCCWInterface* ccwe, gpointer riid,
- gunichar2** rgszNames, guint32 cNames,
- guint32 lcid, gint32 *rgDispId)
-{
- return MONO_E_NOTIMPL;
-}
-
-static int STDCALL
-cominterop_ccw_invoke (MonoCCWInterface* ccwe, guint32 dispIdMember,
- gpointer riid, guint32 lcid,
- guint16 wFlags, gpointer pDispParams,
- gpointer pVarResult, gpointer pExcepInfo,
- guint32 *puArgErr)
-{
- return MONO_E_NOTIMPL;
-}
-
-#else /* DISABLE_COM */
-
-gboolean
-mono_marshal_free_ccw (MonoObject* object)
-{
- return FALSE;
-}
-
-#endif /* DISABLE_COM */
-
void
mono_marshal_find_nonzero_bit_offset (guint8 *buf, int len, int *byte_offset, guint8 *bitmask)
{
MonoMethodBuilder *mb;
MonoMethodSignature *sig, *csig;
MonoExceptionClause *clause;
- MonoMethodHeader *header;
MonoImage *image;
MonoClass *klass;
GHashTable *cache;
/* dup & extend signature */
csig = mono_metadata_signature_alloc (image, param_count);
- sig_size = sizeof (MonoMethodSignature) + ((sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *));
+ sig_size = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
memcpy (csig, sig, sig_size);
csig->param_count = param_count;
csig->hasthis = 0;
}
/* setup exception param as byref+[out] */
- csig->params [param_count - 1] = mono_metadata_type_dup (image->mempool,
+ csig->params [param_count - 1] = mono_metadata_type_dup (image,
&mono_defaults.exception_class->byval_arg);
csig->params [param_count - 1]->byref = 1;
csig->params [param_count - 1]->attrs = PARAM_ATTRIBUTE_OUT;
mono_mb_emit_byte (mb, CEE_STIND_REF);
/* try */
- mono_loader_lock ();
clause = mono_image_alloc0 (image, sizeof (MonoExceptionClause));
- mono_loader_unlock ();
clause->try_offset = mono_mb_get_label (mb);
/* push method's args */
clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
+ mono_mb_set_clauses (mb, 1, clause);
+
mono_mb_patch_branch (mb, pos_leave);
/* end-try */
res = mono_mb_create_and_cache (cache, method, mb, csig, param_count + 16);
mono_mb_free (mb);
- header = ((MonoMethodNormal *)res)->header;
- header->num_clauses = 1;
- header->clauses = clause;
-
return res;
}
+
+/*
+ * mono_marshal_free_dynamic_wrappers:
+ *
+ * Free wrappers of the dynamic method METHOD.
+ */
+void
+mono_marshal_free_dynamic_wrappers (MonoMethod *method)
+{
+ g_assert (method->dynamic);
+
+ mono_marshal_lock ();
+ /*
+ * FIXME: We currently leak the wrappers. Freeing them would be tricky as
+ * they could be shared with other methods ?
+ */
+ if (method->klass->image->runtime_invoke_direct_cache)
+ g_hash_table_remove (method->klass->image->runtime_invoke_direct_cache, method);
+ mono_marshal_unlock ();
+}
+
+/*
+ * mono_marshal_free_inflated_wrappers:
+ *
+ * Free wrappers of the inflated method METHOD.
+ */
+
+static gboolean
+signature_method_pair_matches_signature (gpointer key, gpointer value, gpointer user_data)
+{
+ SignatureMethodPair *pair = (SignatureMethodPair*)key;
+ MonoMethodSignature *sig = (MonoMethodSignature*)user_data;
+
+ return mono_metadata_signature_equal (pair->sig, sig);
+}
+
+void
+mono_marshal_free_inflated_wrappers (MonoMethod *method)
+{
+ MonoMethodSignature *sig = method->signature;
+
+ g_assert (method->is_inflated);
+
+ /* Ignore calls occuring late during cleanup. */
+ if (!marshal_mutex_initialized)
+ return;
+
+ mono_marshal_lock ();
+ /*
+ * FIXME: We currently leak the wrappers. Freeing them would be tricky as
+ * they could be shared with other methods ?
+ */
+
+ /*
+ * indexed by MonoMethodSignature
+ */
+ /* FIXME: This could remove unrelated wrappers as well */
+ if (sig && method->klass->image->delegate_begin_invoke_cache)
+ g_hash_table_remove (method->klass->image->delegate_begin_invoke_cache, sig);
+ if (sig && method->klass->image->delegate_end_invoke_cache)
+ g_hash_table_remove (method->klass->image->delegate_end_invoke_cache, sig);
+ if (sig && method->klass->image->delegate_invoke_cache)
+ 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);
+
+ /*
+ * indexed by SignatureMethodPair
+ */
+ if (sig && method->klass->image->delegate_abstract_invoke_cache)
+ g_hash_table_foreach_remove (method->klass->image->delegate_abstract_invoke_cache,
+ signature_method_pair_matches_signature, (gpointer)sig);
+
+ /*
+ * indexed by MonoMethod pointers
+ */
+ if (method->klass->image->runtime_invoke_direct_cache)
+ g_hash_table_remove (method->klass->image->runtime_invoke_direct_cache, method);
+ if (method->klass->image->managed_wrapper_cache)
+ g_hash_table_remove (method->klass->image->managed_wrapper_cache, method);
+ if (method->klass->image->native_wrapper_cache)
+ g_hash_table_remove (method->klass->image->native_wrapper_cache, method);
+ if (method->klass->image->remoting_invoke_cache)
+ g_hash_table_remove (method->klass->image->remoting_invoke_cache, method);
+ if (method->klass->image->synchronized_cache)
+ g_hash_table_remove (method->klass->image->synchronized_cache, method);
+ if (method->klass->image->unbox_wrapper_cache)
+ g_hash_table_remove (method->klass->image->unbox_wrapper_cache, method);
+ if (method->klass->image->cominterop_invoke_cache)
+ g_hash_table_remove (method->klass->image->cominterop_invoke_cache, method);
+ if (method->klass->image->cominterop_wrapper_cache)
+ g_hash_table_remove (method->klass->image->cominterop_wrapper_cache, method);
+ if (method->klass->image->thunk_invoke_cache)
+ g_hash_table_remove (method->klass->image->thunk_invoke_cache, method);
+
+ mono_marshal_unlock ();
+}