Skip to content

Commit

Permalink
Delegate actual creation of exceptions to classlib
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 committed May 10, 2021
1 parent b82a5d8 commit 302c163
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ public enum ExceptionStringID
// BadImageFormatException
BadImageFormatGeneric,
BadImageFormatSpecific,

// MarshalDirectiveException
MarshalDirectiveGeneric,
MarshalDirectiveInvalidSizeParamIndex,
}
}
6 changes: 6 additions & 0 deletions src/coreclr/tools/Common/TypeSystem/Common/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,13 @@ internal BadImageFormatException(string reason)

}
}

public class MarshalDirectiveException : TypeSystemException
{
internal MarshalDirectiveException(ExceptionStringID id)
: base(id)
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 302c163

Please sign in to comment.