-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/tests/Interop/PInvoke/Miscellaneous/CopyCtor/CopyCtorTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/tests/Interop/PInvoke/Miscellaneous/CopyCtor/CopyCtorTest.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
64
src/tests/Interop/PInvoke/Miscellaneous/CopyCtor/CopyCtorUtil.il
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
|
8 changes: 8 additions & 0 deletions
8
src/tests/Interop/PInvoke/Miscellaneous/CopyCtor/CopyCtorUtil.ilproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.