Skip to content

Commit

Permalink
Fix marshalling null delegates (dotnet#241)
Browse files Browse the repository at this point in the history
Null delegates marshal as null pointers and vice versa.
  • Loading branch information
MichalStrehovsky authored and jkotas committed Nov 25, 2019
1 parent 4842a7e commit 0953da2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/coreclr/src/tools/Common/TypeSystem/Interop/IL/Marshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1824,20 +1824,38 @@ class DelegateMarshaller : Marshaller
{
protected override void AllocAndTransformManagedToNative(ILCodeStream codeStream)
{
ILCodeLabel lNullPointer = _ilCodeStreams.Emitter.NewCodeLabel();
ILCodeLabel lDone = _ilCodeStreams.Emitter.NewCodeLabel();

LoadManagedValue(codeStream);
codeStream.Emit(ILOpcode.dup);
codeStream.Emit(ILOpcode.brfalse, lNullPointer);

codeStream.Emit(ILOpcode.call, _ilCodeStreams.Emitter.NewToken(
InteropTypes.GetMarshal(Context).GetKnownMethod("GetFunctionPointerForDelegate",
new MethodSignature(MethodSignatureFlags.Static, 0, Context.GetWellKnownType(WellKnownType.IntPtr),
new TypeDesc[] { Context.GetWellKnownType(WellKnownType.MulticastDelegate).BaseType }
))));

codeStream.Emit(ILOpcode.br, lDone);

codeStream.EmitLabel(lNullPointer);
codeStream.Emit(ILOpcode.pop);
codeStream.EmitLdc(0);
codeStream.Emit(ILOpcode.conv_i);

codeStream.EmitLabel(lDone);
StoreNativeValue(codeStream);
}

protected override void TransformNativeToManaged(ILCodeStream codeStream)
{
ILCodeLabel lNullPointer = _ilCodeStreams.Emitter.NewCodeLabel();
ILCodeLabel lDone = _ilCodeStreams.Emitter.NewCodeLabel();

LoadNativeValue(codeStream);
codeStream.Emit(ILOpcode.dup);
codeStream.Emit(ILOpcode.brfalse, lNullPointer);

TypeDesc systemType = Context.SystemModule.GetKnownType("System", "Type");

Expand All @@ -1850,6 +1868,13 @@ protected override void TransformNativeToManaged(ILCodeStream codeStream)
new TypeDesc[] { Context.GetWellKnownType(WellKnownType.IntPtr), systemType }
))));

codeStream.Emit(ILOpcode.br, lDone);

codeStream.EmitLabel(lNullPointer);
codeStream.Emit(ILOpcode.pop);
codeStream.Emit(ILOpcode.ldnull);

codeStream.EmitLabel(lDone);
StoreManagedValue(codeStream);
}

Expand Down

0 comments on commit 0953da2

Please sign in to comment.