From 99f44a5847b8e172ff5a1085f9fc7e941fb99a81 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Thu, 3 Apr 2025 13:32:19 -0700 Subject: [PATCH 1/2] Update error message when the name of the method is known. --- src/coreclr/dlls/mscorrc/mscorrc.rc | 1 + src/coreclr/dlls/mscorrc/resource.h | 1 + src/coreclr/vm/dllimport.cpp | 9 +++++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/coreclr/dlls/mscorrc/mscorrc.rc b/src/coreclr/dlls/mscorrc/mscorrc.rc index a858eeff594308..8660552b5a38ef 100644 --- a/src/coreclr/dlls/mscorrc/mscorrc.rc +++ b/src/coreclr/dlls/mscorrc/mscorrc.rc @@ -151,6 +151,7 @@ STRINGTABLE DISCARDABLE BEGIN IDS_EE_THREAD_APARTMENT_NOT_SUPPORTED "The system does not support the %1 thread apartment." IDS_EE_NDIRECT_UNSUPPORTED_SIG "Method's type signature is not PInvoke compatible." + IDS_EE_NDIRECT_NAMED_UNSUPPORTED_SIG "Method '%1.%2', has a signature that is not PInvoke compatible." IDS_EE_COM_UNSUPPORTED_SIG "Method's type signature is not Interop compatible." IDS_EE_COM_UNSUPPORTED_TYPE "The method returned a COM Variant type that is not Interop compatible." IDS_EE_MULTIPLE_CALLCONV_UNSUPPORTED "Multiple unmanaged calling conventions are specified. Only a single calling convention is supported." diff --git a/src/coreclr/dlls/mscorrc/resource.h b/src/coreclr/dlls/mscorrc/resource.h index 964cb7128325d9..e8019d9babedd1 100644 --- a/src/coreclr/dlls/mscorrc/resource.h +++ b/src/coreclr/dlls/mscorrc/resource.h @@ -35,6 +35,7 @@ #define IDS_DS_DSOTHREADMODEL 0x1707 #define IDS_EE_NDIRECT_UNSUPPORTED_SIG 0x1708 +#define IDS_EE_NDIRECT_NAMED_UNSUPPORTED_SIG 0x1709 #define IDS_EE_NDIRECT_BADNATL 0x170a #define IDS_EE_NDIRECT_LOADLIB_WIN 0x170b #define IDS_EE_NDIRECT_GETPROCADDRESS_WIN 0x170c diff --git a/src/coreclr/vm/dllimport.cpp b/src/coreclr/vm/dllimport.cpp index 7f16554ce79196..cb491c8e39dcb3 100644 --- a/src/coreclr/vm/dllimport.cpp +++ b/src/coreclr/vm/dllimport.cpp @@ -3415,7 +3415,6 @@ BOOL NDirect::MarshalingRequired( return FALSE; } - // factorization of CreateNDirectStubWorker static MarshalInfo::MarshalType DoMarshalReturnValue(MetaSig& msig, mdParamDef* params, @@ -4291,7 +4290,13 @@ static void CreateNDirectStubAccessMetadata( // P/Invoke marked with UnmanagedCallersOnlyAttribute is not // presently supported. if (pMD->HasUnmanagedCallersOnlyAttribute()) - COMPlusThrow(kNotSupportedException, IDS_EE_NDIRECT_UNSUPPORTED_SIG); + { + SString namespaceOrClassName; + SString methodName; + pMD->GetMethodInfoNoSig(namespaceOrClassName, methodName); + COMPlusThrow(kNotSupportedException, IDS_EE_NDIRECT_NAMED_UNSUPPORTED_SIG, + namespaceOrClassName.GetUnicode(), methodName.GetUnicode()); + } // Check to see if we need to do LCID conversion. lcidArg = GetLCIDParameterIndex(pMD); From 95b23a50695e83cfeb462e2b28dd1b49fc4ba2f0 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Thu, 3 Apr 2025 14:19:05 -0700 Subject: [PATCH 2/2] Create specific error for DllImport and UnmanagedCallersOnly. --- src/coreclr/dlls/mscorrc/mscorrc.rc | 2 +- src/coreclr/dlls/mscorrc/resource.h | 2 +- src/coreclr/vm/dllimport.cpp | 2 +- src/mono/mono/metadata/marshal.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/coreclr/dlls/mscorrc/mscorrc.rc b/src/coreclr/dlls/mscorrc/mscorrc.rc index 8660552b5a38ef..cb371763691e65 100644 --- a/src/coreclr/dlls/mscorrc/mscorrc.rc +++ b/src/coreclr/dlls/mscorrc/mscorrc.rc @@ -151,7 +151,7 @@ STRINGTABLE DISCARDABLE BEGIN IDS_EE_THREAD_APARTMENT_NOT_SUPPORTED "The system does not support the %1 thread apartment." IDS_EE_NDIRECT_UNSUPPORTED_SIG "Method's type signature is not PInvoke compatible." - IDS_EE_NDIRECT_NAMED_UNSUPPORTED_SIG "Method '%1.%2', has a signature that is not PInvoke compatible." + IDS_EE_NDIRECT_UNSUPPORTED_UNMANAGEDCALLERSONLY "Method '%1.%2' cannot be marked with both DllImportAttribute and UnmanagedCallersOnlyAttribute." IDS_EE_COM_UNSUPPORTED_SIG "Method's type signature is not Interop compatible." IDS_EE_COM_UNSUPPORTED_TYPE "The method returned a COM Variant type that is not Interop compatible." IDS_EE_MULTIPLE_CALLCONV_UNSUPPORTED "Multiple unmanaged calling conventions are specified. Only a single calling convention is supported." diff --git a/src/coreclr/dlls/mscorrc/resource.h b/src/coreclr/dlls/mscorrc/resource.h index e8019d9babedd1..fc103677bcd4b2 100644 --- a/src/coreclr/dlls/mscorrc/resource.h +++ b/src/coreclr/dlls/mscorrc/resource.h @@ -35,7 +35,7 @@ #define IDS_DS_DSOTHREADMODEL 0x1707 #define IDS_EE_NDIRECT_UNSUPPORTED_SIG 0x1708 -#define IDS_EE_NDIRECT_NAMED_UNSUPPORTED_SIG 0x1709 +#define IDS_EE_NDIRECT_UNSUPPORTED_UNMANAGEDCALLERSONLY 0x1709 #define IDS_EE_NDIRECT_BADNATL 0x170a #define IDS_EE_NDIRECT_LOADLIB_WIN 0x170b #define IDS_EE_NDIRECT_GETPROCADDRESS_WIN 0x170c diff --git a/src/coreclr/vm/dllimport.cpp b/src/coreclr/vm/dllimport.cpp index cb491c8e39dcb3..858c42329c0720 100644 --- a/src/coreclr/vm/dllimport.cpp +++ b/src/coreclr/vm/dllimport.cpp @@ -4294,7 +4294,7 @@ static void CreateNDirectStubAccessMetadata( SString namespaceOrClassName; SString methodName; pMD->GetMethodInfoNoSig(namespaceOrClassName, methodName); - COMPlusThrow(kNotSupportedException, IDS_EE_NDIRECT_NAMED_UNSUPPORTED_SIG, + COMPlusThrow(kNotSupportedException, IDS_EE_NDIRECT_UNSUPPORTED_UNMANAGEDCALLERSONLY, namespaceOrClassName.GetUnicode(), methodName.GetUnicode()); } diff --git a/src/mono/mono/metadata/marshal.c b/src/mono/mono/metadata/marshal.c index 03379d5c2fd9fe..e68fba963e70e6 100644 --- a/src/mono/mono/metadata/marshal.c +++ b/src/mono/mono/metadata/marshal.c @@ -3582,7 +3582,7 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions, * In AOT mode and embedding scenarios, it is possible that the icall is not registered in the runtime doing the AOT compilation. * Emit a wrapper that throws a NotSupportedException. */ - get_marshal_cb ()->mb_emit_exception (mb, "System", "NotSupportedException", "Method canot be marked with both DllImportAttribute and UnmanagedCallersOnlyAttribute"); + get_marshal_cb ()->mb_emit_exception (mb, "System", "NotSupportedException", "Method cannot be marked with both DllImportAttribute and UnmanagedCallersOnlyAttribute"); goto emit_exception_for_error; } else if (!pinvoke && !piinfo->addr && !aot) { /* if there's no code but the error isn't set, just use a fairly generic exception. */