Skip to content

Commit 1fe928b

Browse files
committed
ILLink: Respect DisableRuntimeMarshalling
The linker will no longer do additional marking in `ProcessInteropMethod` when the methods owning assembly has `[assembly: System.Runtime.CompilerServices.DisableRuntimeMarshalling]`
1 parent 6a23f1c commit 1fe928b

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

src/tools/illink/src/linker/Linker.Steps/MarkStep.cs

+3
Original file line numberDiff line numberDiff line change
@@ -3362,6 +3362,9 @@ void ProcessInteropMethod (MethodDefinition method, MessageOrigin origin)
33623362
}
33633363
}
33643364

3365+
if (method.AssemblyHasDisableRuntimeMarshalling ())
3366+
return;
3367+
33653368
TypeDefinition? returnTypeDefinition = Context.TryResolve (method.ReturnType);
33663369

33673370
const bool includeStaticFields = false;

src/tools/illink/src/linker/Linker/MethodDefinitionExtensions.cs

+15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ public static bool IsIntrinsic (this MethodDefinition method)
3535
return false;
3636
}
3737

38+
public static bool AssemblyHasDisableRuntimeMarshalling (this MethodDefinition method)
39+
{
40+
var assembly = method.Module.Assembly;
41+
if (!assembly.HasCustomAttributes)
42+
return false;
43+
44+
foreach (var ca in assembly.CustomAttributes) {
45+
var caType = ca.AttributeType;
46+
if (caType.Name == "DisableRuntimeMarshallingAttribute" && caType.Namespace == "System.Runtime.CompilerServices")
47+
return true;
48+
}
49+
50+
return false;
51+
}
52+
3853
public static bool IsPropertyMethod (this MethodDefinition md)
3954
{
4055
return (md.SemanticsAttributes & MethodSemanticsAttributes.Getter) != 0 ||

src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/Interop/PInvoke/PInvokeTests.cs

+6
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,11 @@ public Task UnusedPInvoke ()
4646
{
4747
return RunTest ();
4848
}
49+
50+
[Fact]
51+
public Task RespectsDisableRuntimeMarshalling ()
52+
{
53+
return RunTest ();
54+
}
4955
}
5056
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Runtime.InteropServices;
5+
6+
namespace Mono.Linker.Tests.Cases.Interop.PInvoke.Dependencies;
7+
8+
public class AssemblyWithoutDisableRuntimeMarshalling
9+
{
10+
[DllImport ("Unused")]
11+
public static extern B SomeMethod ();
12+
13+
public class B;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Runtime.CompilerServices;
5+
using System.Runtime.InteropServices;
6+
using Mono.Linker.Tests.Cases.Expectations.Assertions;
7+
using Mono.Linker.Tests.Cases.Expectations.Metadata;
8+
using Mono.Linker.Tests.Cases.Interop.PInvoke.Dependencies;
9+
10+
#if INCLUDE_DISABLE_RUNTIME_MARSHALLING_ATTRIBUTE
11+
[assembly: System.Runtime.CompilerServices.DisableRuntimeMarshalling]
12+
#endif
13+
14+
[assembly: KeptAttributeAttribute(typeof(DisableRuntimeMarshallingAttribute))]
15+
16+
namespace Mono.Linker.Tests.Cases.Interop.PInvoke;
17+
18+
[SetupCompileBefore ("WithoutDisable.dll", new [] { typeof(AssemblyWithoutDisableRuntimeMarshalling) })]
19+
[Define ("INCLUDE_DISABLE_RUNTIME_MARSHALLING_ATTRIBUTE")]
20+
[KeptModuleReference ("Unused")]
21+
[KeptMemberInAssembly ("WithoutDisable.dll", typeof (AssemblyWithoutDisableRuntimeMarshalling.B), ".ctor()")]
22+
public class RespectsDisableRuntimeMarshalling
23+
{
24+
public static void Main ()
25+
{
26+
var a = SomeMethod ();
27+
var b = AssemblyWithoutDisableRuntimeMarshalling.SomeMethod ();
28+
}
29+
30+
// The .ctor() will not be marked due to DisableRuntimeMarshalling
31+
[Kept]
32+
class A;
33+
34+
[Kept]
35+
[DllImport ("Unused")]
36+
static extern A SomeMethod ();
37+
}

0 commit comments

Comments
 (0)