Merge pull request #956 from ermshiperete/bug-xamarin-14987
[mono.git] / mono / metadata / marshal.c
index 2b8aed545eacc90b598708e4321a399f9719d680..792a1dcf37eae662456b75b0dfa42202ec2db2b0 100644 (file)
@@ -72,11 +72,12 @@ typedef struct _MonoRemotingMethods MonoRemotingMethods;
 /* 
  * This mutex protects the various marshalling related caches in MonoImage
  * and a few other data structures static to this file.
- * Note that when this lock is held it is not possible to take other runtime
- * locks like the loader lock.
+ *
+ * The marshal lock is a non-recursive complex lock that sits below the domain lock in the
+ * runtime locking latice. Which means it can take simple locks suck as the image lock.
  */
-#define mono_marshal_lock() EnterCriticalSection (&marshal_mutex)
-#define mono_marshal_unlock() LeaveCriticalSection (&marshal_mutex)
+#define mono_marshal_lock() mono_locks_acquire (&marshal_mutex, MarshalLock)
+#define mono_marshal_unlock() mono_locks_release (&marshal_mutex, MarshalLock)
 static CRITICAL_SECTION marshal_mutex;
 static gboolean marshal_mutex_initialized;
 
@@ -555,10 +556,13 @@ mono_delegate_free_ftnptr (MonoDelegate *delegate)
        if (ptr) {
                uint32_t gchandle;
                void **method_data;
+               MonoMethod *method;
+
                ji = mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (ptr));
                g_assert (ji);
 
-               method_data = ((MonoMethodWrapper*)ji->method)->method_data;
+               method = mono_jit_info_get_method (ji);
+               method_data = ((MonoMethodWrapper*)method)->method_data;
 
                /*the target gchandle is the first entry after size and the wrapper itself.*/
                gchandle = GPOINTER_TO_UINT (method_data [2]);
@@ -566,7 +570,7 @@ mono_delegate_free_ftnptr (MonoDelegate *delegate)
                if (gchandle)
                        mono_gchandle_free (gchandle);
 
-               mono_runtime_free_method (mono_object_domain (delegate), ji->method);
+               mono_runtime_free_method (mono_object_domain (delegate), method);
        }
 }
 
@@ -4222,8 +4226,9 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                closed_over_null = sig->param_count == mono_method_signature (del->method)->param_count;
 
        if (del && del->method && mono_method_signature (del->method)->param_count == sig->param_count + 1 && (del->method->flags & METHOD_ATTRIBUTE_STATIC)) {
+               g_assert (!callvirt);
                invoke_sig = mono_method_signature (del->method);
-               target_method = del->method;
+               target_method = NULL;
                static_method_with_first_arg_bound = TRUE;
        }
 
@@ -4251,12 +4256,20 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                res = check_generic_delegate_wrapper_cache (cache, orig_method, method, ctx);
                if (res)
                        return res;
-       } else if (callvirt || static_method_with_first_arg_bound) {
+       } else if (static_method_with_first_arg_bound) {
+               cache = get_cache (&method->klass->image->delegate_bound_static_invoke_cache,
+                                                  (GHashFunc)mono_signature_hash, 
+                                                  (GCompareFunc)mono_metadata_signature_equal);
+               /*
+                * The wrapper is based on sig+invoke_sig, but sig can be derived from invoke_sig.
+                */
+               res = mono_marshal_find_in_cache (cache, invoke_sig);
+               if (res)
+                       return res;
+       } else if (callvirt) {
                GHashTable **cache_ptr;
-               if (static_method_with_first_arg_bound)
-                       cache_ptr = &method->klass->image->delegate_bound_static_invoke_cache;
-               else
-                       cache_ptr = &method->klass->image->delegate_abstract_invoke_cache;
+
+               cache_ptr = &method->klass->image->delegate_abstract_invoke_cache;
 
                /* We need to cache the signature+method pair */
                mono_marshal_lock ();
@@ -4284,7 +4297,11 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                invoke_sig = static_sig;
 
        if (static_method_with_first_arg_bound)
-               name = mono_signature_to_name (sig, "invoke_bound_");
+               name = mono_signature_to_name (invoke_sig, "invoke_bound");
+       else if (closed_over_null)
+               name = mono_signature_to_name (invoke_sig, "invoke_closed_over_null");
+       else if (callvirt)
+               name = mono_signature_to_name (invoke_sig, "invoke_callvirt");
        else
                name = mono_signature_to_name (sig, "invoke");
        if (ctx)
@@ -4406,7 +4423,12 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
 
                def = mono_mb_create_and_cache (cache, method->klass, mb, sig, sig->param_count + 16);
                res = cache_generic_delegate_wrapper (cache, orig_method, def, ctx);
-       } else if (static_method_with_first_arg_bound || callvirt) {
+       } else if (static_method_with_first_arg_bound) {
+               res = mono_mb_create_and_cache (cache, invoke_sig, mb, sig, sig->param_count + 16);
+
+               info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_DELEGATE_INVOKE_BOUND);
+               mono_marshal_set_wrapper_info (res, info);
+       } else if (callvirt) {
                // From mono_mb_create_and_cache
                newm = mono_mb_create_method (mb, sig, sig->param_count + 16);
                /*We perform double checked locking, so must fence before publishing*/
@@ -4421,7 +4443,7 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                                new_key->sig = signature_dup (del->method->klass->image, key.sig);
                        g_hash_table_insert (cache, new_key, res);
 
-                       info = mono_wrapper_info_create (res, callvirt ? WRAPPER_SUBTYPE_DELEGATE_INVOKE_VIRTUAL : WRAPPER_SUBTYPE_DELEGATE_INVOKE_BOUND);
+                       info = mono_wrapper_info_create (res, WRAPPER_SUBTYPE_DELEGATE_INVOKE_VIRTUAL);
                        mono_marshal_set_wrapper_info (res, info);
 
                        mono_marshal_unlock ();
@@ -5192,14 +5214,14 @@ mono_marshal_get_runtime_invoke_dynamic (void)
        mono_mb_emit_byte (mb, CEE_RET);
 #endif /* DISABLE_JIT */
 
-       mono_loader_lock ();
+       mono_marshal_lock ();
        /* double-checked locking */
        if (!method) {
                method = mono_mb_create_method (mb, csig, 16);
                info = mono_wrapper_info_create (method, WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC);
                mono_marshal_set_wrapper_info (method, info);
        }
-       mono_loader_unlock ();
+       mono_marshal_unlock ();
 
        mono_mb_free (mb);
 
@@ -6421,6 +6443,9 @@ emit_marshal_vtype (EmitMarshalContext *m, int argnum, MonoType *t,
                        break;
                }
 
+               if (t->byref && (t->attrs & PARAM_ATTRIBUTE_IN) && !(t->attrs & PARAM_ATTRIBUTE_OUT))
+                       break;
+
                /* Check for null */
                mono_mb_emit_ldarg (mb, argnum);
                pos2 = mono_mb_emit_branch (mb, CEE_BRFALSE);
@@ -8860,13 +8885,14 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
                pinvoke = TRUE;
 
        if (!piinfo->addr) {
-               if (pinvoke)
+               if (pinvoke) {
                        if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
                                exc_arg = "Method contains unsupported native code";
-                       else
+                       else if (!aot)
                                mono_lookup_pinvoke_call (method, &exc_class, &exc_arg);
-               else
+               } else {
                        piinfo->addr = mono_lookup_internal_call (method);
+               }
        }
 
        /* hack - redirect certain string constructors to CreateString */
@@ -9177,6 +9203,16 @@ mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *i
                        emit_marshal (m, i, sig->params [i], mspecs [i + 1], 0, &csig->params [i], MARSHAL_ACTION_MANAGED_CONV_IN);
                }
        }
+
+       if (!sig->ret->byref) {
+               switch (sig->ret->type) {
+               case MONO_TYPE_STRING:
+                       csig->ret = &mono_defaults.int_class->byval_arg;
+                       break;
+               default:
+                       break;
+               }
+       }
 #else
        MonoMethodSignature *sig, *csig;
        int i, *tmp_locals;
@@ -9624,8 +9660,8 @@ mono_marshal_get_vtfixup_ftnptr (MonoImage *image, guint32 token, guint16 type)
        sig = mono_method_signature (method);
        mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_MANAGED);
 
-#ifndef DISABLE_JIT
        param_count = sig->param_count + sig->hasthis;
+#ifndef DISABLE_JIT
        for (i = 0; i < param_count; i++)
                mono_mb_emit_ldarg (mb, i);
 
@@ -10431,7 +10467,7 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *method)
        clause->flags = MONO_EXCEPTION_CLAUSE_FINALLY;
 #endif
 
-       mono_loader_lock ();
+       mono_marshal_lock ();
 
        if (!enter_method) {
                MonoMethodDesc *desc;
@@ -10447,12 +10483,12 @@ mono_marshal_get_synchronized_wrapper (MonoMethod *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);
+               gettypefromhandle_method = mono_method_desc_search_in_class (desc, mono_defaults.systemtype_class);
                g_assert (gettypefromhandle_method);
                mono_method_desc_free (desc);
        }
 
-       mono_loader_unlock ();
+       mono_marshal_unlock ();
 
 #ifndef DISABLE_JIT
        /* Push this or the type object */
@@ -11649,7 +11685,7 @@ ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged (MonoArray *s
        element_size = mono_array_element_size (src->obj.vtable->klass);
 
        /* no references should be involved */
-       source_addr = mono_array_addr_with_size (src, element_size, start_index);
+       source_addr = mono_array_addr_with_size_fast (src, element_size, start_index);
 
        memcpy (dest, source_addr, length * element_size);
 }
@@ -11678,7 +11714,7 @@ ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged (gpointer s
        element_size = mono_array_element_size (dest->obj.vtable->klass);
          
        /* no references should be involved */
-       dest_addr = mono_array_addr_with_size (dest, element_size, start_index);
+       dest_addr = mono_array_addr_with_size_fast (dest, element_size, start_index);
 
        memcpy (dest_addr, src, length * element_size);
 }
@@ -11768,7 +11804,9 @@ ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rty
 
        layout = (klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK);
 
-       if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
+       if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
+               return sizeof (gpointer);
+       } else if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
                gchar *msg;
                MonoException *exc;
 
@@ -12126,7 +12164,7 @@ ves_icall_System_Runtime_InteropServices_Marshal_ReAllocCoTaskMem (gpointer ptr,
 void*
 ves_icall_System_Runtime_InteropServices_Marshal_UnsafeAddrOfPinnedArrayElement (MonoArray *arrayobj, int index)
 {
-       return mono_array_addr_with_size (arrayobj, mono_array_element_size (arrayobj->obj.vtable->klass), index);
+       return mono_array_addr_with_size_fast (arrayobj, mono_array_element_size (arrayobj->obj.vtable->klass), index);
 }
 
 MonoDelegate*
@@ -12181,12 +12219,8 @@ mono_marshal_load_type_info (MonoClass* klass)
        if (!klass->inited)
                mono_class_init (klass);
 
-       mono_loader_lock ();
-
-       if (klass->marshal_info) {
-               mono_loader_unlock ();
+       if (klass->marshal_info)
                return klass->marshal_info;
-       }
 
        /*
         * This function can recursively call itself, so we keep the list of classes which are
@@ -12262,7 +12296,7 @@ mono_marshal_load_type_info (MonoClass* klass)
                case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
                        size = mono_marshal_type_size (field->type, info->fields [j].mspec, 
                                                       &align, TRUE, klass->unicode);
-                       min_align = packing;
+                       min_align = MAX (align, min_align);
                        info->fields [j].offset = field->offset - sizeof (MonoObject);
                        info->native_size = MAX (info->native_size, info->fields [j].offset + size);
                        break;
@@ -12276,9 +12310,12 @@ mono_marshal_load_type_info (MonoClass* klass)
                 * If the provided Size is equal or larger than the calculated size, and there
                 * was no Pack attribute, we set min_align to 1 to avoid native_size being increased
                 */
-               if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
+               if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
                        if (native_size && native_size == info->native_size && klass->packing_size == 0)
                                min_align = 1;
+                       else
+                               min_align = MIN (min_align, packing);
+               }
        }
 
        if (info->native_size & (min_align - 1)) {
@@ -12301,12 +12338,13 @@ mono_marshal_load_type_info (MonoClass* klass)
        loads_list = g_slist_remove (loads_list, klass);
        mono_native_tls_set_value (load_type_info_tls_id, loads_list);
 
-       /*We do double-checking locking on marshal_info */
-       mono_memory_barrier ();
-
-       klass->marshal_info = info;
-
-       mono_loader_unlock ();
+       mono_marshal_lock ();
+       if (!klass->marshal_info) {
+               /*We do double-checking locking on marshal_info */
+               mono_memory_barrier ();
+               klass->marshal_info = info;
+       }
+       mono_marshal_unlock ();
 
        return klass->marshal_info;
 }
@@ -12915,8 +12953,6 @@ mono_marshal_free_dynamic_wrappers (MonoMethod *method)
         */
        if (image->runtime_invoke_direct_cache)
                g_hash_table_remove (image->runtime_invoke_direct_cache, method);
-       if (image->delegate_bound_static_invoke_cache)
-               g_hash_table_foreach_remove (image->delegate_bound_static_invoke_cache, signature_method_pair_matches_method, method);
        if (image->delegate_abstract_invoke_cache)
                g_hash_table_foreach_remove (image->delegate_abstract_invoke_cache, signature_method_pair_matches_method, method);
 
@@ -12977,10 +13013,6 @@ mono_marshal_free_inflated_wrappers (MonoMethod *method)
                g_hash_table_foreach_remove (method->klass->image->delegate_abstract_invoke_cache,
                                             signature_method_pair_matches_signature, (gpointer)sig);
 
-       if (sig && method->klass->image->delegate_bound_static_invoke_cache)
-                g_hash_table_foreach_remove (method->klass->image->delegate_bound_static_invoke_cache,
-                                             signature_method_pair_matches_signature, (gpointer)sig);
-
         /*
          * indexed by MonoMethod pointers
          */