[xbuild] Use the env var $MSBuildExtensionsPath before trying other paths.
[mono.git] / mono / metadata / marshal.c
index 80dbcd87b337f1b91a188fe37c3b0f264fd6bfc7..0e6a259a4376a1f5abaf7352ece91310eae26458 100644 (file)
@@ -86,6 +86,9 @@ delegate_hash_table_add (MonoDelegate *d);
 static void
 emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object);
 
+static void
+emit_struct_conv_full (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object, MonoMarshalNative string_encoding);
+
 static void 
 mono_struct_delete_old (MonoClass *klass, char *ptr);
 
@@ -104,6 +107,9 @@ mono_string_utf8_to_builder2 (char *text);
 static MonoStringBuilder *
 mono_string_utf16_to_builder2 (gunichar2 *text);
 
+static MonoString*
+mono_string_new_len_wrapper (const char *text, guint length);
+
 static void
 mono_byvalarray_to_array (MonoArray *arr, gpointer native_arr, MonoClass *eltype, guint32 elnum);
 
@@ -204,6 +210,7 @@ mono_marshal_init (void)
                register_icall (mono_string_to_utf16, "mono_string_to_utf16", "ptr obj", FALSE);
                register_icall (mono_string_from_utf16, "mono_string_from_utf16", "obj ptr", FALSE);
                register_icall (mono_string_new_wrapper, "mono_string_new_wrapper", "obj ptr", FALSE);
+               register_icall (mono_string_new_len_wrapper, "mono_string_new_len_wrapper", "obj ptr int", 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_ansibstr, "mono_string_to_ansibstr", "ptr object", FALSE);
@@ -968,6 +975,12 @@ mono_string_to_byvalwstr (gpointer dst, MonoString *src, int size)
        *((gunichar2 *) dst + len) = 0;
 }
 
+static MonoString*
+mono_string_new_len_wrapper (const char *text, guint length)
+{
+       return mono_string_new_len (mono_domain_get (), text, length);
+}
+
 static int
 mono_mb_emit_proxy_check (MonoMethodBuilder *mb, int branch_code)
 {
@@ -1018,10 +1031,23 @@ mono_mb_emit_contextbound_check (MonoMethodBuilder *mb, int branch_code)
        return mono_mb_emit_branch (mb, branch_code);
 }
 
+/*
+ * mono_mb_emit_exception_marshal_directive:
+ *
+ *   This function assumes ownership of MSG, which should be malloc-ed.
+ */
 static void
-mono_mb_emit_exception_marshal_directive (MonoMethodBuilder *mb, const char *msg)
+mono_mb_emit_exception_marshal_directive (MonoMethodBuilder *mb, char *msg)
 {
-       mono_mb_emit_exception_full (mb, "System.Runtime.InteropServices", "MarshalDirectiveException", msg);
+       char *s;
+
+       if (!mb->dynamic) {
+               s = mono_image_strdup (mb->method->klass->image, msg);
+               g_free (msg);
+       } else {
+               s = g_strdup (msg);
+       }
+       mono_mb_emit_exception_full (mb, "System.Runtime.InteropServices", "MarshalDirectiveException", s);
 }
 
 guint
@@ -1398,7 +1424,6 @@ emit_ptr_to_object_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
                char *msg = g_strdup_printf ("marshaling conversion %d not implemented", conv);
 
                mono_mb_emit_exception_marshal_directive (mb, msg);
-               g_free (msg);
                break;
        }
        }
@@ -1412,12 +1437,14 @@ conv_to_icall (MonoMarshalConv conv)
                return mono_marshal_string_to_utf16;            
        case MONO_MARSHAL_CONV_LPWSTR_STR:
                return mono_string_from_utf16;
-       case MONO_MARSHAL_CONV_LPSTR_STR:
+       case MONO_MARSHAL_CONV_LPTSTR_STR:
 #ifdef TARGET_WIN32
                return mono_string_from_utf16;
 #else
                return mono_string_new_wrapper;
 #endif
+       case MONO_MARSHAL_CONV_LPSTR_STR:
+               return mono_string_new_wrapper;
        case MONO_MARSHAL_CONV_STR_LPTSTR:
 #ifdef TARGET_WIN32
                return mono_marshal_string_to_utf16;
@@ -1744,7 +1771,8 @@ emit_object_to_ptr_conv (MonoMethodBuilder *mb, MonoType *type, MonoMarshalConv
 }
 
 static void
-emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
+emit_struct_conv_full (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object,
+                                          MonoMarshalNative string_encoding)
 {
        MonoMarshalType *info;
        int i;
@@ -1847,8 +1875,18 @@ emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
                        case MONO_TYPE_R8:
                                mono_mb_emit_ldloc (mb, 1);
                                mono_mb_emit_ldloc (mb, 0);
-                               mono_mb_emit_byte (mb, mono_type_to_ldind (ftype));
-                               mono_mb_emit_byte (mb, mono_type_to_stind (ftype));
+                               if (t == MONO_TYPE_CHAR && ntype == MONO_NATIVE_U1 && string_encoding != MONO_NATIVE_LPWSTR) {
+                                       if (to_object) {
+                                               mono_mb_emit_byte (mb, CEE_LDIND_U1);
+                                               mono_mb_emit_byte (mb, CEE_STIND_I2);
+                                       } else {
+                                               mono_mb_emit_byte (mb, CEE_LDIND_U2);
+                                               mono_mb_emit_byte (mb, CEE_STIND_I1);
+                                       }
+                               } else {
+                                       mono_mb_emit_byte (mb, mono_type_to_ldind (ftype));
+                                       mono_mb_emit_byte (mb, mono_type_to_stind (ftype));
+                               }
                                break;
                        case MONO_TYPE_VALUETYPE: {
                                int src_var, dst_var;
@@ -1953,6 +1991,12 @@ emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
        }
 }
 
+static void
+emit_struct_conv (MonoMethodBuilder *mb, MonoClass *klass, gboolean to_object)
+{
+       emit_struct_conv_full (mb, klass, to_object, -1);
+}
+
 static void
 emit_struct_free (MonoMethodBuilder *mb, MonoClass *klass, int struct_var)
 {
@@ -2175,6 +2219,7 @@ mono_marshal_get_string_to_ptr_conv (MonoMethodPInvoke *piinfo, MonoMarshalSpec
        case MONO_NATIVE_LPWSTR:
                return MONO_MARSHAL_CONV_STR_LPWSTR;
        case MONO_NATIVE_LPSTR:
+       case MONO_NATIVE_VBBYREFSTR:
                return MONO_MARSHAL_CONV_STR_LPSTR;
        case MONO_NATIVE_LPTSTR:
                return MONO_MARSHAL_CONV_STR_LPTSTR;
@@ -2217,6 +2262,7 @@ mono_marshal_get_ptr_to_string_conv (MonoMethodPInvoke *piinfo, MonoMarshalSpec
                *need_free = FALSE;
                return MONO_MARSHAL_CONV_LPWSTR_STR;
        case MONO_NATIVE_LPSTR:
+       case MONO_NATIVE_VBBYREFSTR:
                return MONO_MARSHAL_CONV_LPSTR_STR;
        case MONO_NATIVE_LPTSTR:
                return MONO_MARSHAL_CONV_LPTSTR_STR;
@@ -3979,6 +4025,8 @@ mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del)
                        res = newm;
                        new_key = g_new0 (SignatureMethodPair, 1);
                        *new_key = key;
+                       if (static_method_with_first_arg_bound)
+                               new_key->sig = signature_dup (del->method->klass->image, key.sig);
                        g_hash_table_insert (cache, new_key, res);
                        mono_marshal_set_wrapper_info (res, new_key);
                        mono_marshal_unlock ();
@@ -4012,7 +4060,7 @@ signature_dup_add_this (MonoMethodSignature *sig, MonoClass *klass)
        res->hasthis = FALSE;
        for (i = sig->param_count - 1; i >= 0; i --)
                res->params [i + 1] = sig->params [i];
-       res->params [0] = &mono_ptr_class_get (&klass->byval_arg)->byval_arg;
+       res->params [0] = klass->valuetype ? &klass->this_arg : &klass->byval_arg;
 
        return res;
 }
@@ -4559,6 +4607,11 @@ mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual)
        csig->params [1] = &mono_defaults.int_class->byval_arg;
        csig->params [2] = &mono_defaults.int_class->byval_arg;
        csig->params [3] = &mono_defaults.int_class->byval_arg;
+       csig->pinvoke = 1;
+#if TARGET_WIN32
+       /* This is called from runtime code so it has to be cdecl */
+       csig->call_convention = MONO_CALL_C;
+#endif
 
        name = mono_signature_to_name (callsig, virtual ? "runtime_invoke_virtual" : "runtime_invoke");
        mb = mono_mb_new (target_klass, name,  MONO_WRAPPER_RUNTIME_INVOKE);
@@ -5333,7 +5386,7 @@ emit_marshal_custom (EmitMarshalContext *m, int argnum, MonoType *t,
        static MonoClass *ICustomMarshaler = NULL;
        static MonoMethod *cleanup_native, *cleanup_managed;
        static MonoMethod *marshal_managed_to_native, *marshal_native_to_managed;
-       MonoMethod *get_instance;
+       MonoMethod *get_instance = NULL;
        MonoMethodBuilder *mb = m->mb;
        char *exception_msg = NULL;
        guint32 loc1;
@@ -5341,7 +5394,10 @@ emit_marshal_custom (EmitMarshalContext *m, int argnum, MonoType *t,
 
        if (!ICustomMarshaler) {
                ICustomMarshaler = mono_class_from_name (mono_defaults.corlib, "System.Runtime.InteropServices", "ICustomMarshaler");
-               g_assert (ICustomMarshaler);
+               if (!ICustomMarshaler) {
+                       exception_msg = g_strdup ("Current profile doesn't support ICustomMarshaler");
+                       goto handle_exception;
+               }
 
                cleanup_native = mono_class_get_method_from_name (ICustomMarshaler, "CleanUpNativeData", 1);
                g_assert (cleanup_native);
@@ -5373,6 +5429,7 @@ emit_marshal_custom (EmitMarshalContext *m, int argnum, MonoType *t,
        if (!get_instance)
                exception_msg = g_strdup_printf ("Custom marshaler '%s' does not implement a static GetInstance method that takes a single string parameter and returns an ICustomMarshaler.", mklass->name);
 
+handle_exception:
        /* Throw exception and emit compensation code if neccesary */
        if (exception_msg) {
                switch (action) {
@@ -5997,7 +6054,27 @@ emit_marshal_string (EmitMarshalContext *m, int argnum, MonoType *t,
                        break;
                }
 
-               if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT)) {
+               if (encoding == MONO_NATIVE_VBBYREFSTR) {
+                       static MonoMethod *m;
+
+                       if (!m) {
+                               m = mono_class_get_method_from_name_flags (mono_defaults.string_class, "get_Length", -1, 0);
+                               g_assert (m);
+                       }
+
+                       /* 
+                        * Have to allocate a new string with the same length as the original, and
+                        * copy the contents of the buffer pointed to by CONV_ARG into it.
+                        */
+                       g_assert (t->byref);
+                       mono_mb_emit_ldarg (mb, argnum);
+                       mono_mb_emit_ldloc (mb, conv_arg);
+                       mono_mb_emit_ldarg (mb, argnum);
+                       mono_mb_emit_byte (mb, CEE_LDIND_I);                            
+                       mono_mb_emit_managed_call (mb, m, NULL);
+                       mono_mb_emit_icall (mb, mono_string_new_len_wrapper);
+                       mono_mb_emit_byte (mb, CEE_STIND_REF);
+               } else if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT)) {
                        mono_mb_emit_ldarg (mb, argnum);
                        mono_mb_emit_ldloc (mb, conv_arg);
                        mono_mb_emit_icall (mb, conv_to_icall (conv));
@@ -6014,7 +6091,7 @@ emit_marshal_string (EmitMarshalContext *m, int argnum, MonoType *t,
                break;
 
        case MARSHAL_ACTION_PUSH:
-               if (t->byref)
+               if (t->byref && encoding != MONO_NATIVE_VBBYREFSTR)
                        mono_mb_emit_ldloc_addr (mb, conv_arg);
                else
                        mono_mb_emit_ldloc (mb, conv_arg);
@@ -6203,7 +6280,7 @@ emit_marshal_safehandle (EmitMarshalContext *m, int argnum, MonoType *t,
                
                if (t->data.klass->flags & TYPE_ATTRIBUTE_ABSTRACT){
                        mono_mb_emit_byte (mb, CEE_POP);
-                       mono_mb_emit_exception_marshal_directive (mb, "Returned SafeHandles should not be abstract");
+                       mono_mb_emit_exception_marshal_directive (mb, g_strdup ("Returned SafeHandles should not be abstract"));
                        break;
                }
 
@@ -6263,8 +6340,8 @@ emit_marshal_handleref (EmitMarshalContext *m, int argnum, MonoType *t,
                *conv_arg_type = intptr_type;
 
                if (t->byref){
-                       mono_mb_emit_exception_marshal_directive (mb,
-                               "HandleRefs can not be returned from unmanaged code (or passed by ref)");
+                       char *msg = g_strdup ("HandleRefs can not be returned from unmanaged code (or passed by ref)");
+                       mono_mb_emit_exception_marshal_directive (mb, msg);
                        break;
                } 
                mono_mb_emit_ldarg_addr (mb, argnum);
@@ -6285,8 +6362,8 @@ emit_marshal_handleref (EmitMarshalContext *m, int argnum, MonoType *t,
        }
                
        case MARSHAL_ACTION_CONV_RESULT: {
-               mono_mb_emit_exception_marshal_directive (mb,
-                       "HandleRefs can not be returned from unmanaged code (or passed by ref)");
+               char *msg = g_strdup ("HandleRefs can not be returned from unmanaged code (or passed by ref)");
+               mono_mb_emit_exception_marshal_directive (mb, msg);
                break;
        }
                
@@ -6542,7 +6619,7 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
                                }
 
                                mono_mb_emit_ldloc (mb, conv_arg);
-                               mono_mb_emit_icall (mb, g_free);
+                               mono_mb_emit_icall (mb, mono_marshal_free);
 
                                mono_mb_patch_branch (mb, pos2);
                        }
@@ -6604,7 +6681,7 @@ emit_marshal_object (EmitMarshalContext *m, int argnum, MonoType *t,
        
                        /* Free the pointer allocated by unmanaged code */
                        mono_mb_emit_ldloc (mb, loc);
-                       mono_mb_emit_icall (mb, g_free);
+                       mono_mb_emit_icall (mb, mono_marshal_free);
                        mono_mb_patch_branch (mb, pos);
                }
                break;
@@ -6902,6 +6979,24 @@ emit_marshal_variant (EmitMarshalContext *m, int argnum, MonoType *t,
        return conv_arg;
 }
 
+static gboolean
+mono_pinvoke_is_unicode (MonoMethodPInvoke *piinfo)
+{
+       switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
+       case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
+               return FALSE;
+       case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
+               return TRUE;
+       case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
+       default:
+#ifdef TARGET_WIN32
+               return TRUE;
+#else
+               return FALSE;
+#endif
+       }
+}
+
 static int
 emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
                                        MonoMarshalSpec *spec, 
@@ -6972,6 +7067,8 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
 
                        if (is_string)
                                esize = sizeof (gpointer);
+                       else if (eklass == mono_defaults.char_class) /*can't call mono_marshal_type_size since it causes all sorts of asserts*/
+                               esize = mono_pinvoke_is_unicode (m->piinfo) ? 2 : 1;
                        else
                                esize = mono_class_native_size (eklass, NULL);
 
@@ -7024,7 +7121,7 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
                                mono_mb_emit_stloc (mb, 1);
 
                                /* emit valuetype conversion code */
-                               emit_struct_conv (mb, eklass, FALSE);
+                               emit_struct_conv_full (mb, eklass, FALSE, eklass == mono_defaults.char_class ? encoding : -1);
                        }
 
                        mono_mb_emit_add_to_local (mb, index_var, 1);
@@ -7061,6 +7158,8 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
                        eklass = klass->element_class;
                        if ((eklass == mono_defaults.stringbuilder_class) || (eklass == mono_defaults.string_class))
                                esize = sizeof (gpointer);
+                       else if (eklass == mono_defaults.char_class)
+                               esize = mono_pinvoke_is_unicode (m->piinfo) ? 2 : 1;
                        else
                                esize = mono_class_native_size (eklass, NULL);
                        src_ptr = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
@@ -7140,7 +7239,7 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
                                        mono_mb_emit_stloc (mb, 1);
 
                                        /* emit valuetype conversion code */
-                                       emit_struct_conv (mb, eklass, TRUE);
+                                       emit_struct_conv_full (mb, eklass, TRUE, eklass == mono_defaults.char_class ? encoding : -1);
                                }
 
                                if (need_free) {
@@ -7504,6 +7603,8 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
 
                if (is_string)
                        esize = sizeof (gpointer);
+               else if (eklass == mono_defaults.char_class)
+                       esize = mono_pinvoke_is_unicode (m->piinfo) ? 2 : 1;
                else
                        esize = mono_class_native_size (eklass, NULL);
 
@@ -9695,7 +9796,7 @@ mono_marshal_get_virtual_stelemref (MonoClass *array_class)
                return cached_methods [kind];
 
        name = g_strdup_printf ("virt_stelemref_%s", strelemref_wrapper_name [kind]);
-       mb = mono_mb_new_no_dup_name (mono_defaults.object_class, name, MONO_WRAPPER_STELEMREF);
+       mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_STELEMREF);
        g_free (name);
 
        if (!signature) {
@@ -10035,10 +10136,18 @@ mono_marshal_get_virtual_stelemref (MonoClass *array_class)
        res = mono_mb_create_method (mb, signature, 4);
        res->flags |= METHOD_ATTRIBUTE_VIRTUAL;
        mono_marshal_set_wrapper_info (res, GUINT_TO_POINTER (kind + 1));
-       cached_methods [kind] = res;
+
+       mono_marshal_lock ();
+       if (!cached_methods [kind]) {
+               cached_methods [kind] = res;
+               mono_marshal_unlock ();
+       } else {
+               mono_marshal_unlock ();
+               mono_free_method (res);
+       }
 
        mono_mb_free (mb);
-       return res;
+       return cached_methods [kind];
 }
 
 MonoMethod*