Skip to content

Copy ctor handling for crossgen2 #40113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/coreclr/src/tools/Common/TypeSystem/Common/MethodDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public sealed partial class MethodSignature : TypeSystemEntity
// Value of <see cref="EmbeddedSignatureData.index" /> for any custom modifiers on the return type
public const string IndexOfCustomModifiersOnReturnType = "0.1.1.1";

// Value of <see cref="EmbeddedSignatureData.index" /> for any custom modifiers on
// SomeStruct when SomeStruct *, or SomeStruct & is the type of a parameter or return type
// Parameter index 0 represents the return type, and indices 1-n represent the parameters to the signature
public static string GetIndexOfCustomModifierOnPointedAtTypeByParameterIndex(int parameterIndex)
{
return $"0.1.1.2.{(parameterIndex + 1).ToStringInvariant()}.1";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not reviewing this part. I don't want to spend time learning how this address is computed because that code upsets me.

}

public MethodSignature(MethodSignatureFlags flags, int genericParameterCount, TypeDesc returnType, TypeDesc[] parameters, EmbeddedSignatureData[] embeddedSignatureData = null)
{
_flags = flags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,44 @@ internal static TypeDesc GetNativeTypeFromMarshallerKind(TypeDesc type,
}
}

private static bool HasCopyConstructorCustomModifier(int? parameterIndex,
EmbeddedSignatureData[] customModifierData)
{
if (!parameterIndex.HasValue || customModifierData == null)
return false;

string customModifierIndex = MethodSignature.GetIndexOfCustomModifierOnPointedAtTypeByParameterIndex(parameterIndex.Value);
foreach (var customModifier in customModifierData)
{
if (customModifier.kind != EmbeddedSignatureDataKind.RequiredCustomModifier)
continue;

if (customModifier.index != customModifierIndex)
continue;

var customModifierType = customModifier.type as DefType;
if (customModifierType == null)
continue;

if ((customModifierType.Namespace == "System.Runtime.CompilerServices" && customModifierType.Name == "IsCopyConstructed") ||
(customModifierType.Namespace == "Microsoft.VisualC" && customModifierType.Name == "NeedsCopyConstructorModifier"))
{
return true;
}
}

return false;
}

internal static MarshallerKind GetMarshallerKind(
TypeDesc type,
MarshalAsDescriptor marshalAs,
bool isReturn,
bool isAnsi,
MarshallerType marshallerType,
out MarshallerKind elementMarshallerKind)
TypeDesc type,
int? parameterIndex,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation looks off.

EmbeddedSignatureData[] customModifierData,
MarshalAsDescriptor marshalAs,
bool isReturn,
bool isAnsi,
MarshallerType marshallerType,
out MarshallerKind elementMarshallerKind)
{
elementMarshallerKind = MarshallerKind.Invalid;

Expand All @@ -183,6 +214,12 @@ internal static MarshallerKind GetMarshallerKind(

type = type.GetParameterType();

if (!type.IsPrimitive && type.IsValueType && marshallerType != MarshallerType.Field
&& HasCopyConstructorCustomModifier(parameterIndex, customModifierData))
{
return MarshallerKind.BlittableValueClassWithCopyCtor;
}

// Compat note: CLR allows ref returning blittable structs for IJW
if (isReturn)
return MarshallerKind.Invalid;
Expand Down Expand Up @@ -444,7 +481,15 @@ internal static MarshallerKind GetMarshallerKind(
else if (type.IsPointer)
{
if (nativeType == NativeTypeKind.Default)
{
var pointedAtType = type.GetParameterType();
if (!pointedAtType.IsPrimitive && !type.IsEnum && marshallerType != MarshallerType.Field
&& HasCopyConstructorCustomModifier(parameterIndex, customModifierData))
{
return MarshallerKind.BlittableValueClassWithCopyCtor;
}
return MarshallerKind.BlittableValue;
}
else
return MarshallerKind.Invalid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static bool IsBlittableType(TypeDesc type)

MarshallerKind marshallerKind = MarshalHelpers.GetMarshallerKind(
field.FieldType,
parameterIndex : null,
customModifierData: null,
field.GetMarshalAsDescriptor(),
isReturn: false,
isAnsi: mdType.PInvokeStringFormat == PInvokeStringFormat.AnsiClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ enum MarshallerKind
AsAnyA,
AsAnyW,
ComInterface,
BlittableValueClassWithCopyCtor,
Invalid
}
public enum MarshalDirection
Expand Down Expand Up @@ -271,6 +272,8 @@ protected Marshaller()
/// <param name="parameterType">type of the parameter to marshal</param>
/// <returns>The created Marshaller</returns>
public static Marshaller CreateMarshaller(TypeDesc parameterType,
int? parameterIndex,
EmbeddedSignatureData[] customModifierData,
MarshallerType marshallerType,
MarshalAsDescriptor marshalAs,
MarshalDirection direction,
Expand All @@ -286,6 +289,8 @@ public static Marshaller CreateMarshaller(TypeDesc parameterType,
{
MarshallerKind elementMarshallerKind;
MarshallerKind marshallerKind = MarshalHelpers.GetMarshallerKind(parameterType,
parameterIndex,
customModifierData,
marshalAs,
isReturn,
flags.CharSet == CharSet.Ansi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public static Marshaller[] GetMarshallersForMethod(MethodDesc targetMethod)

TypeDesc parameterType = (i == 0) ? methodSig.ReturnType : methodSig[i - 1]; //first item is the return type
marshallers[i] = CreateMarshaller(parameterType,
parameterIndex,
methodSig.GetEmbeddedSignatureData(),
MarshallerType.Argument,
parameterMetadata.MarshalAsDescriptor,
direction,
Expand Down Expand Up @@ -121,6 +123,8 @@ public static bool IsMarshallingRequired(MethodSignature methodSig, ParameterMet

MarshallerKind marshallerKind = MarshalHelpers.GetMarshallerKind(
parameterType,
parameterIndex: i,
customModifierData: methodSig.GetEmbeddedSignatureData(),
parameterMetadata.MarshalAsDescriptor,
parameterMetadata.Return,
isAnsi: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
{
ret
}

.method public hidebysig instance int32 modopt([CoreTestAssembly]System.Void) & Method3(int32 modopt(FooModifier)*, int32 modopt(FooModifier)*) cil managed
{
ret
}
}

.class private auto ansi beforefieldinit Atom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public void TestSignatureMatchesModOptAtStartOfSigAndAfterByRef()
Assert.Equal("OptionalCustomModifier0.1.1.1CharOptionalCustomModifier0.1.1.2.1.1VoidOptionalCustomModifier0.1.2.1FooModifier", GetModOptMethodSignatureInfo(methodWithModOptAtStartOfSigAndAfterByRef));
}

[Fact]
public void TestSignatureMatchesModoptOnPointerOrRefModifiedType()
{
MetadataType modOptTester = _testModule.GetType("", "ModOptTester");
MethodSignature methodWithModOpt = modOptTester.GetMethods().Single(m => string.Equals(m.Name, "Method3")).Signature;
Assert.Equal(MethodSignature.GetIndexOfCustomModifierOnPointedAtTypeByParameterIndex(0), methodWithModOpt.GetEmbeddedSignatureData()[0].index);
Assert.Equal(MethodSignature.GetIndexOfCustomModifierOnPointedAtTypeByParameterIndex(1), methodWithModOpt.GetEmbeddedSignatureData()[1].index);
Assert.Equal(MethodSignature.GetIndexOfCustomModifierOnPointedAtTypeByParameterIndex(2), methodWithModOpt.GetEmbeddedSignatureData()[2].index);
}

[Fact]
public void TestSignatureMatches()
{
Expand Down
45 changes: 45 additions & 0 deletions src/tests/Interop/PInvoke/Miscellaneous/CopyCtor/CopyCtorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using TestLibrary;

static unsafe class CopyCtor
{
public static unsafe int StructWithCtorTest(StructWithCtor* ptrStruct, ref StructWithCtor refStruct)
{
if (ptrStruct->_instanceField != 1)
return 1;
if (refStruct._instanceField != 2)
return 2;

if (StructWithCtor.CopyCtorCallCount != 2)
return 3;
if (StructWithCtor.DtorCallCount != 2)
return 4;


return 100;
}

[UnmanagedFunctionPointerAttribute(CallingConvention.StdCall)]
public delegate int TestDelegate(StructWithCtor* ptrStruct, ref StructWithCtor refStruct);

public static unsafe int Main()
{
TestDelegate del = (TestDelegate)StructWithCtorTest;
StructWithCtor s1 = new StructWithCtor();
StructWithCtor s2 = new StructWithCtor();
s1._instanceField = 1;
s2._instanceField = 2;
int returnVal = FunctionPointer.Call_FunctionPointer(Marshal.GetFunctionPointerForDelegate(del), &s1, ref s2);

GC.KeepAlive(del);

return returnVal;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="CopyCtorTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="CopyCtorUtil.ilproj" />
</ItemGroup>
</Project>
64 changes: 64 additions & 0 deletions src/tests/Interop/PInvoke/Miscellaneous/CopyCtor/CopyCtorUtil.il
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

.assembly extern mscorlib { }
.assembly extern System.Runtime.CompilerServices.VisualC { }
.assembly CopyCtorUtil { }

.class public sealed sequential ansi beforefieldinit StructWithCtor
extends [mscorlib]System.ValueType
{
.field public int32 _instanceField
.field public static int32 CopyCtorCallCount
.field public static int32 DtorCallCount

.method public specialname static void '<MarshalCopy>'(valuetype StructWithCtor* A_0,
valuetype StructWithCtor* A_1) cil managed
{
ldarg.0
ldarg.1
ldfld int32 StructWithCtor::_instanceField
stfld int32 StructWithCtor::_instanceField

ldsfld int32 StructWithCtor::CopyCtorCallCount
ldc.i4.1
add
stsfld int32 StructWithCtor::CopyCtorCallCount
ret
}

.method public specialname static void '<MarshalDestroy>'(valuetype StructWithCtor* A_0) cil managed
{
ldsfld int32 StructWithCtor::DtorCallCount
ldc.i4.1
add
stsfld int32 StructWithCtor::DtorCallCount
ret
}
}

.class public auto ansi beforefieldinit FunctionPointer
extends [mscorlib]System.Object
{
.method hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ret
}

.method public hidebysig static int32 Call_FunctionPointer(native int fptr,
valuetype StructWithCtor* arg1,
valuetype StructWithCtor& arg2) cil managed
{
.maxstack 8
ldarg.1
ldarg.2
ldarg.0
calli unmanaged stdcall int32 (valuetype StructWithCtor modreq([mscorlib]System.Runtime.CompilerServices.IsCopyConstructed)*, valuetype StructWithCtor modreq([mscorlib]System.Runtime.CompilerServices.IsCopyConstructed)&)
ret
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.IL">
<PropertyGroup>
<OutputType>library</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="CopyCtorUtil.il" />
</ItemGroup>
</Project>