Skip to content

ILLink : Fix instantiation tracking issue with icall/pinvoke parameters #113437

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3369,6 +3369,11 @@ void ProcessInteropMethod (MethodDefinition method, MessageOrigin origin)
if (!returnTypeDefinition.IsImport) {
// What we keep here is correct most of the time, but not every time. Fine for now.
MarkDefaultConstructor (returnTypeDefinition, new DependencyInfo (DependencyKind.InteropMethodDependency, method), origin);

// We need to make sure the return type is marked as instantiated. If the type happens to have a .ctor() then we'll get lucky and that
// will trigger marking as instantiated. We need to also cover the case when a type doesn't have a .ctor()
MarkRequirementsForInstantiatedTypes (returnTypeDefinition);

MarkFields (returnTypeDefinition, includeStaticFields, new DependencyInfo (DependencyKind.InteropMethodDependency, method), origin);
}
}
Expand All @@ -3391,6 +3396,10 @@ void ProcessInteropMethod (MethodDefinition method, MessageOrigin origin)
MarkFields (paramTypeDefinition, includeStaticFields, new DependencyInfo (DependencyKind.InteropMethodDependency, method), origin);
if (pd.ParameterType.IsByReference) {
MarkDefaultConstructor (paramTypeDefinition, new DependencyInfo (DependencyKind.InteropMethodDependency, method), origin);

// We need to make sure the return type is marked as instantiated. If the type happens to have a .ctor() then we'll get lucky and that
// will trigger marking as instantiated. We need to also cover the case when a type doesn't have a .ctor()
MarkRequirementsForInstantiatedTypes (paramTypeDefinition);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.CompilerServices;
using Mono.Linker.Tests.Cases.Expectations.Assertions;

namespace Mono.Linker.Tests.Cases.Interop.InternalCalls;

public class OutTypesAreMarkedInstantiated
{
public static void Main ()
{
FooRefParameter refParmaeter = null;
InteropMethod (null, ref refParmaeter, out FooOutParameter outParameter);
UsedToMarkMethods (null, null, null, null);
}

[Kept]
[MethodImpl (MethodImplOptions.InternalCall)]
static extern FooReturn InteropMethod (FooNormalParameter normal, ref FooRefParameter @ref, out FooOutParameter @out);

[Kept]
static void UsedToMarkMethods (FooReturn f, FooNormalParameter n, FooRefParameter r, FooOutParameter o)
{
f.Method ();
n.Method ();
r.Method ();
o.Method ();
}
}


public class FooReturn
{
// Define a non-default constructor. This is required in order to trigger the bug
public FooReturn (int a)
{
}

// This method should not have it's body modified because the linker should consider it as instantiated
[Kept]
public void Method ()
{
throw new System.NotImplementedException ();
}
}

public class FooOutParameter
{
// Define a non-default constructor. This is required in order to trigger the bug
public FooOutParameter (int a)
{
}

// This method should not have it's body modified because the linker should consider it as instantiated
[Kept]
public void Method ()
{
throw new System.NotImplementedException ();
}
}

public class FooRefParameter
{
// Define a non-default constructor. This is required in order to trigger the bug
public FooRefParameter (int a)
{
}

// This method should not have it's body modified because the linker should consider it as instantiated
[Kept]
public void Method ()
{
throw new System.NotImplementedException ();
}
}

public class FooNormalParameter
{
[Kept]
// This parameter will not be marked as instantiated because it is not an out or ref parameter. This will lead to the linker applying the
// unreachable bodies optimization
[ExpectBodyModified]
public void Method ()
{
throw new System.NotImplementedException ();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.InteropServices;
using Mono.Linker.Tests.Cases.Expectations.Assertions;

namespace Mono.Linker.Tests.Cases.Interop.PInvoke;

[KeptModuleReference ("Unused")]
public class OutTypesAreMarkedInstantiated
{
public static void Main ()
{
FooRefParameter refParmaeter = null;
InteropMethod (null, ref refParmaeter, out FooOutParameter outParameter);
UsedToMarkMethods (null, null, null, null);
}

[Kept]
[DllImport ("Unused")]
static extern FooReturn InteropMethod (FooNormalParameter normal, ref FooRefParameter @ref, out FooOutParameter @out);

[Kept]
static void UsedToMarkMethods (FooReturn f, FooNormalParameter n, FooRefParameter r, FooOutParameter o)
{
f.Method ();
n.Method ();
r.Method ();
o.Method ();
}
}

public class FooReturn
{
// Define a non-default constructor. This is required in order to trigger the bug
public FooReturn (int a)
{
}

// This method should not have it's body modified because the linker should consider it as instantiated
[Kept]
public void Method ()
{
throw new System.NotImplementedException ();
}
}

public class FooOutParameter
{
// Define a non-default constructor. This is required in order to trigger the bug
public FooOutParameter (int a)
{
}

// This method should not have it's body modified because the linker should consider it as instantiated
[Kept]
public void Method ()
{
throw new System.NotImplementedException ();
}
}

public class FooRefParameter
{
// Define a non-default constructor. This is required in order to trigger the bug
public FooRefParameter (int a)
{
}

// This method should not have it's body modified because the linker should consider it as instantiated
[Kept]
public void Method ()
{
throw new System.NotImplementedException ();
}
}

public class FooNormalParameter
{
[Kept]
// This parameter will not be marked as instantiated because it is not an out or ref parameter. This will lead to the linker applying the
// unreachable bodies optimization
[ExpectBodyModified]
public void Method ()
{
throw new System.NotImplementedException ();
}
}
Loading