diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/ThrowHelpers.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/ThrowHelpers.cs index d17c50873c7d..6b8dc76ca9e9 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/ThrowHelpers.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/ThrowHelpers.cs @@ -108,6 +108,11 @@ public static void ThrowInvalidProgramExceptionWithArgument(ExceptionStringID id throw TypeLoaderExceptionHelper.CreateInvalidProgramException(id, methodName); } + public static void ThrowMarshalDirectiveException(ExceptionStringID id) + { + throw TypeLoaderExceptionHelper.CreateMarshalDirectiveException(id); + } + public static void ThrowArgumentException() { throw new ArgumentException(); diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/TypeLoaderExceptionHelper.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/TypeLoaderExceptionHelper.cs index e33d5818a6f7..66b064e966f3 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/TypeLoaderExceptionHelper.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/TypeLoaderExceptionHelper.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Runtime.InteropServices; using Internal.TypeSystem; @@ -57,6 +58,11 @@ public static Exception CreateInvalidProgramException(ExceptionStringID id, stri throw new InvalidProgramException(SR.Format(GetFormatString(id), methodName)); } + public static Exception CreateMarshalDirectiveException(ExceptionStringID id) + { + throw new MarshalDirectiveException(GetFormatString(id)); + } + // TODO: move to a place where we can share this with the compiler private static string GetFormatString(ExceptionStringID id) { @@ -92,6 +98,10 @@ private static string GetFormatString(ExceptionStringID id) return SR.IO_FileNotFound_FileName; case ExceptionStringID.BadImageFormatGeneric: return SR.Arg_BadImageFormatException; + case ExceptionStringID.MarshalDirectiveGeneric: + return SR.Arg_BadImageFormatException; + case ExceptionStringID.MarshalDirectiveInvalidSizeParamIndex: + return SR.Arg_BadImageFormatException; default: Debug.Assert(false); return ""; diff --git a/src/coreclr/tools/Common/TypeSystem/Common/ExceptionStringID.cs b/src/coreclr/tools/Common/TypeSystem/Common/ExceptionStringID.cs index dd38a715c5fe..be4d8d340a98 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/ExceptionStringID.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/ExceptionStringID.cs @@ -41,5 +41,9 @@ public enum ExceptionStringID // BadImageFormatException BadImageFormatGeneric, BadImageFormatSpecific, + + // MarshalDirectiveException + MarshalDirectiveGeneric, + MarshalDirectiveInvalidSizeParamIndex, } } diff --git a/src/coreclr/tools/Common/TypeSystem/Common/ThrowHelper.cs b/src/coreclr/tools/Common/TypeSystem/Common/ThrowHelper.cs index 90c02f6ad3aa..401e0c6aa862 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/ThrowHelper.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/ThrowHelper.cs @@ -65,6 +65,12 @@ public static void ThrowBadImageFormatException(string message) throw new TypeSystemException.BadImageFormatException(message); } + [System.Diagnostics.DebuggerHidden] + public static void ThrowMarshalDirectiveException(ExceptionStringID id) + { + throw new TypeSystemException.MarshalDirectiveException(id); + } + private static partial class Format { public static string OwningModule(TypeDesc type) diff --git a/src/coreclr/tools/Common/TypeSystem/Common/TypeSystemException.cs b/src/coreclr/tools/Common/TypeSystem/Common/TypeSystemException.cs index 598f0f602748..39015e807d94 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/TypeSystemException.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/TypeSystemException.cs @@ -162,5 +162,13 @@ internal BadImageFormatException(string reason) } } + + public class MarshalDirectiveException : TypeSystemException + { + internal MarshalDirectiveException(ExceptionStringID id) + : base(id) + { + } + } } } diff --git a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/PInvokeILEmitter.cs b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/PInvokeILEmitter.cs index ee809d15c418..97a31d9dd7b7 100644 --- a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/PInvokeILEmitter.cs +++ b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/PInvokeILEmitter.cs @@ -412,11 +412,6 @@ public static MethodIL EmitIL(MethodDesc method, Debug.Assert(!String.IsNullOrEmpty(ex.Message)); return MarshalHelpers.EmitExceptionBody(ex.Message, method); } - catch (System.Runtime.InteropServices.MarshalDirectiveException ex) - { - Debug.Assert(!String.IsNullOrEmpty(ex.Message)); - return MarshalHelpers.EmitExceptionBody(ex, method); - } } private bool IsStubRequired() diff --git a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/StructMarshallingThunk.cs b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/StructMarshallingThunk.cs index dba8e975af39..cf346c068521 100644 --- a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/StructMarshallingThunk.cs +++ b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/StructMarshallingThunk.cs @@ -305,11 +305,6 @@ public override MethodIL EmitIL() Debug.Assert(!String.IsNullOrEmpty(ex.Message)); return MarshalHelpers.EmitExceptionBody(ex.Message, this); } - catch (System.Runtime.InteropServices.MarshalDirectiveException ex) - { - Debug.Assert(!String.IsNullOrEmpty(ex.Message)); - return MarshalHelpers.EmitExceptionBody(ex, this); - } } /// diff --git a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/TypeSystemThrowingILEmitter.cs b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/TypeSystemThrowingILEmitter.cs index 2a90036883da..ca24e143856b 100644 --- a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/TypeSystemThrowingILEmitter.cs +++ b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/TypeSystemThrowingILEmitter.cs @@ -66,6 +66,10 @@ public static MethodIL EmitIL(MethodDesc methodThatShouldThrow, TypeSystemExcept { helper = context.GetHelperEntryPoint("ThrowHelpers", "ThrowBadImageFormatException"); } + else if (exceptionType == typeof(TypeSystemException.MarshalDirectiveException)) + { + helper = context.GetHelperEntryPoint("ThrowHelpers", "ThrowMarshalDirectiveException"); + } else { throw new NotImplementedException(); diff --git a/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs b/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs index c1461c9704a1..b382eba65e2b 100644 --- a/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs +++ b/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs @@ -244,12 +244,12 @@ protected override void AllocAndTransformManagedToNative(ILCodeStream codeStream protected override void TransformNativeToManaged(ILCodeStream codeStream) { - throw new MarshalDirectiveException(); + ThrowHelper.ThrowMarshalDirectiveException(ExceptionStringID.MarshalDirectiveGeneric); } protected override void TransformManagedToNative(ILCodeStream codeStream) { - throw new MarshalDirectiveException(); + ThrowHelper.ThrowMarshalDirectiveException(ExceptionStringID.MarshalDirectiveGeneric); } protected override void EmitCleanupManaged(ILCodeStream codeStream) diff --git a/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs b/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs index e525ed99efd3..4b298fa4174c 100644 --- a/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs +++ b/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs @@ -1048,7 +1048,7 @@ protected virtual void EmitElementCount(ILCodeStream codeStream, MarshalDirectio if (index < 0 || index >= Marshallers.Length - 1) { - throw new MarshalDirectiveException("Invalid SizeParamIndex, must be between 0 and parameter count"); + ThrowHelper.ThrowMarshalDirectiveException(ExceptionStringID.MarshalDirectiveInvalidSizeParamIndex); } //zero-th index is for return type @@ -1068,7 +1068,8 @@ protected virtual void EmitElementCount(ILCodeStream codeStream, MarshalDirectio case TypeFlags.UIntPtr: break; default: - throw new MarshalDirectiveException("Invalid SizeParamIndex, parameter must be of type int/uint"); + ThrowHelper.ThrowMarshalDirectiveException(ExceptionStringID.MarshalDirectiveInvalidSizeParamIndex); + break; } // @TODO - We can use LoadManagedValue, but that requires byref arg propagation happen in a special setup stream