Skip to content

Commit

Permalink
Cleanup marshalling handling
Browse files Browse the repository at this point in the history
Related to dotnet#167 and
Closes dotnet#168
  • Loading branch information
kant2002 committed May 20, 2021
1 parent 1a3f045 commit 3373627
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,15 @@ 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);
}
catch (TypeLoadException ex)
{
return MarshalHelpers.EmitExceptionBody(ex, method);
}
}

private bool IsStubRequired()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,15 @@ 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);
}
catch (TypeLoadException ex)
{
return MarshalHelpers.EmitExceptionBody(ex, this);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,25 @@ public static MethodIL EmitExceptionBody(string message, MethodDesc method)

return new PInvokeILStubMethodIL((ILStubMethodIL)emitter.Link(method), isStubRequired: true);
}

public static MethodIL EmitExceptionBody(Exception exception, MethodDesc method)
{
ILEmitter emitter = new ILEmitter();

TypeSystemContext context = method.Context;
MethodSignature ctorSignature = new MethodSignature(0, 0, context.GetWellKnownType(WellKnownType.Void),
new TypeDesc[] { context.GetWellKnownType(WellKnownType.String) });
Type exceptionType = exception.GetType();
MethodDesc exceptionCtor = method.Context.SystemModule
.GetKnownType(exceptionType.Namespace, exceptionType.Name)
.GetKnownMethod(".ctor", ctorSignature);

ILCodeStream codeStream = emitter.NewCodeStream();
codeStream.Emit(ILOpcode.ldstr, emitter.NewToken(exception.Message));
codeStream.Emit(ILOpcode.newobj, emitter.NewToken(exceptionCtor));
codeStream.Emit(ILOpcode.throw_);

return new PInvokeILStubMethodIL((ILStubMethodIL)emitter.Link(method), isStubRequired: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,6 @@ internal static MarshallerKind GetMarshallerKind(
{
case NativeTypeKind.Array:
{
if (isField || isReturn)
return MarshallerKind.Invalid;

var arrayType = (ArrayType)type;

elementMarshallerKind = GetArrayElementMarshallerKind(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ protected override void AllocAndTransformManagedToNative(ILCodeStream codeStream

protected override void TransformNativeToManaged(ILCodeStream codeStream)
{
throw new InvalidProgramException();
throw new MarshalDirectiveException();
}

protected override void TransformManagedToNative(ILCodeStream codeStream)
{
throw new InvalidProgramException();
throw new MarshalDirectiveException();
}

protected override void EmitCleanupManaged(ILCodeStream codeStream)
Expand Down
14 changes: 12 additions & 2 deletions src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,16 @@ protected TypeDesc NativeElementType
}
}

protected override void SetupArgumentsForReturnValueMarshalling()
{
throw new TypeLoadException();
}

protected override void SetupArgumentsForFieldMarshalling()
{
throw new TypeLoadException();
}

protected Marshaller GetElementMarshaller(MarshalDirection direction)
{
if (_elementMarshaller == null)
Expand Down Expand Up @@ -1038,7 +1048,7 @@ protected virtual void EmitElementCount(ILCodeStream codeStream, MarshalDirectio

if (index < 0 || index >= Marshallers.Length - 1)
{
throw new InvalidProgramException("Invalid SizeParamIndex, must be between 0 and parameter count");
throw new MarshalDirectiveException("Invalid SizeParamIndex, must be between 0 and parameter count");
}

//zero-th index is for return type
Expand All @@ -1058,7 +1068,7 @@ protected virtual void EmitElementCount(ILCodeStream codeStream, MarshalDirectio
case TypeFlags.UIntPtr:
break;
default:
throw new InvalidProgramException("Invalid SizeParamIndex, parameter must be of type int/uint");
throw new MarshalDirectiveException("Invalid SizeParamIndex, parameter must be of type int/uint");
}

// @TODO - We can use LoadManagedValue, but that requires byref arg propagation happen in a special setup stream
Expand Down
11 changes: 1 addition & 10 deletions src/tests/issues.targets
Original file line number Diff line number Diff line change
Expand Up @@ -1087,12 +1087,6 @@
<ExcludeList Include="$(XunitTestBinBase)/Interop/NativeLibrary/Callback/CallbackTests/*">
<Issue>https://github.com/dotnet/runtimelab/issues/206</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/Array/MarshalArrayAsField/AsByValArray/AsByValArrayTest/*">
<Issue>https://github.com/dotnet/runtimelab/issues/167</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/Array/MarshalArrayAsField/AsLPArray/AsLPArrayTest/*">
<Issue>https://github.com/dotnet/runtimelab/issues/168</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/Array/MarshalArrayAsParam/AsDefault/AsDefaultTest/*">
<Issue>https://github.com/dotnet/runtimelab/issues/176: VARIANT marshalling</Issue>
</ExcludeList>
Expand Down Expand Up @@ -1135,16 +1129,13 @@
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/Miscellaneous/CopyCtor/CopyCtorTest/*">
<Issue>https://github.com/dotnet/runtimelab/issues/155: C++/CLI</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/Miscellaneous/HandleRef/HandleRefTest/*">
<Issue>https://github.com/dotnet/runtimelab/issues/168</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/NativeCallManagedComVisible/**/*">
<Issue>https://github.com/dotnet/runtimelab/issues/155: COM</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/SafeHandles/SafeHandleTests/SafeHandleTests/*">
<Issue>https://github.com/dotnet/runtimelab/issues/168</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/SizeParamIndex/**/*">
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/SizeParamIndex/ReversePInvoke/**/*">
<Issue>https://github.com/dotnet/runtimelab/issues/167</Issue>
</ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/Interop/PInvoke/Varargs/VarargsTest/*">
Expand Down

0 comments on commit 3373627

Please sign in to comment.