Skip to content

ILLink: Respect DisableRuntimeMarshalling #113706

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
3 changes: 3 additions & 0 deletions src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3362,6 +3362,9 @@ void ProcessInteropMethod (MethodDefinition method, MessageOrigin origin)
}
}

if (method.AssemblyHasDisableRuntimeMarshalling ())
return;

TypeDefinition? returnTypeDefinition = Context.TryResolve (method.ReturnType);

const bool includeStaticFields = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ public static bool IsIntrinsic (this MethodDefinition method)
return false;
}

public static bool AssemblyHasDisableRuntimeMarshalling (this MethodDefinition method)
{
var assembly = method.Module.Assembly;
if (!assembly.HasCustomAttributes)
return false;

foreach (var ca in assembly.CustomAttributes) {
var caType = ca.AttributeType;
if (caType.Name == "DisableRuntimeMarshallingAttribute" && caType.Namespace == "System.Runtime.CompilerServices")
return true;
}

return false;
}

public static bool IsPropertyMethod (this MethodDefinition md)
{
return (md.SemanticsAttributes & MethodSemanticsAttributes.Getter) != 0 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ public Task UnusedPInvoke ()
{
return RunTest ();
}

[Fact]
public Task RespectsDisableRuntimeMarshalling ()
{
return RunTest ();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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;

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

public class AssemblyWithoutDisableRuntimeMarshalling
{
[DllImport ("Unused")]
public static extern B SomeMethod ();

public class B;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 System.Runtime.InteropServices;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;
using Mono.Linker.Tests.Cases.Interop.PInvoke.Dependencies;

#if INCLUDE_DISABLE_RUNTIME_MARSHALLING_ATTRIBUTE
[assembly: System.Runtime.CompilerServices.DisableRuntimeMarshalling]
#endif

[assembly: KeptAttributeAttribute(typeof(DisableRuntimeMarshallingAttribute))]

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

[SetupCompileBefore ("WithoutDisable.dll", new [] { typeof(AssemblyWithoutDisableRuntimeMarshalling) })]
[Define ("INCLUDE_DISABLE_RUNTIME_MARSHALLING_ATTRIBUTE")]
[KeptModuleReference ("Unused")]
[KeptMemberInAssembly ("WithoutDisable.dll", typeof (AssemblyWithoutDisableRuntimeMarshalling.B), ".ctor()")]
public class RespectsDisableRuntimeMarshalling
{
public static void Main ()
{
var a = SomeMethod ();
var b = AssemblyWithoutDisableRuntimeMarshalling.SomeMethod ();
}

// The .ctor() will not be marked due to DisableRuntimeMarshalling
[Kept]
class A;

[Kept]
[DllImport ("Unused")]
static extern A SomeMethod ();
}
Loading