-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix feature switch IL when feature attributes are removed (#104995)
When removing `FeatureSwitchDefinition` and `FeatureGuard` attributes, we need to ensure that the body rewriting logic still respects these attributes even though the rewriting happens after attribute removal. Caching the substituted constant value fixes this. More detail: When feature switches or feature guards are substituted with a constant, the IL rewriting of the feature property happens after SweepStep, during CodeRewriterStep. At this point, any attributes marked for removal by RemoveAttributeInstancesAttribute have already been swept. When removing `FeatureSwitchDefinitionAttribute` and `FeatureGuardAttribute`, as we do with AggressiveAttributeTrimming, this means that CodeRewriterStep fails to substitute a constant for attributed feature check or feature guard properties. Normally, the attributed feature properties would be removed entirely when substituted, so CodeRewriterStep wouldn't even visit the property. However, it's possible that the caller of the property was a `copy` assembly where the callsite can't be rewritten to return a constant. When this happens, we can end up with a property that's not stubbed out during CodeRewriterStep, even though we didn't mark a callee of the property (because MarkStep did treat the property as a constant and didn't mark its instructions). When trying to write out the property's IL this results in a failure because we see a reference to a method that was removed. Fixes #104945
- Loading branch information
Showing
6 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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
22 changes: 22 additions & 0 deletions
22
...ools/illink/test/Mono.Linker.Tests.Cases/LinkAttributes/Dependencies/FeatureProperties.cs
This file contains 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,22 @@ | ||
// 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; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Mono.Linker.Tests.Cases.LinkAttributes.Dependencies | ||
{ | ||
public class FeatureProperties | ||
{ | ||
[FeatureSwitchDefinition ("FeatureSwitch")] | ||
public static bool FeatureSwitchDefinition => Removed (); | ||
|
||
[FeatureGuard (typeof (RequiresUnreferencedCodeAttribute))] | ||
public static bool FeatureGuard => Removed (); | ||
|
||
[FeatureSwitchDefinition ("StubbedFeatureSwitch")] | ||
public static bool StubbedFeatureSwitch => Removed (); | ||
|
||
static bool Removed () => true; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...link/test/Mono.Linker.Tests.Cases/LinkAttributes/FeatureAttributeRemovalInCopyAssembly.cs
This file contains 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,58 @@ | ||
// 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.Diagnostics.CodeAnalysis; | ||
using Mono.Linker.Tests.Cases.Expectations.Assertions; | ||
using Mono.Linker.Tests.Cases.Expectations.Metadata; | ||
using Mono.Linker.Tests.Cases.LinkAttributes.Dependencies; | ||
|
||
namespace Mono.Linker.Tests.Cases.LinkAttributes | ||
{ | ||
[KeptMember (".ctor()")] | ||
[ExpectedInstructionSequenceOnMemberInAssembly ("FeatureProperties.dll", typeof (FeatureProperties), "get_StubbedFeatureSwitch()", new[] { | ||
"ldc.i4.1", | ||
"ret", | ||
})] | ||
[SetupLinkAttributesFile ("TestRemoveFeatureAttributes.xml")] | ||
[SetupLinkerSubstitutionFile ("StubFeatureSwitch.xml")] | ||
[RemovedAttributeInAssembly ("FeatureProperties.dll", typeof (FeatureSwitchDefinitionAttribute), typeof (FeatureProperties), nameof (FeatureProperties.FeatureSwitchDefinition))] | ||
[RemovedAttributeInAssembly ("FeatureProperties.dll", typeof (FeatureGuardAttribute), typeof (FeatureProperties), nameof (FeatureProperties.FeatureGuard))] | ||
[RemovedAttributeInAssembly ("FeatureProperties.dll", typeof (FeatureSwitchDefinitionAttribute), typeof (FeatureProperties), nameof (FeatureProperties.StubbedFeatureSwitch))] | ||
[RemovedMemberInAssembly ("FeatureProperties.dll", typeof (FeatureProperties), "Removed()")] | ||
[SetupCompileBefore ("FeatureProperties.dll", new[] { "Dependencies/FeatureProperties.cs" })] | ||
[SetupLinkerArgument ("--feature", "FeatureSwitch", "false")] | ||
[SetupLinkerArgument ("--feature", "StubbedFeatureSwitch", "true")] | ||
[SetupLinkerAction ("copy", "test")] // prevent trimming calls to feature switch properties | ||
[IgnoreLinkAttributes (false)] | ||
[IgnoreSubstitutions (false)] | ||
class FeatureAttributeRemovalInCopyAssembly | ||
{ | ||
public static void Main () | ||
{ | ||
TestFeatureSwitch (); | ||
TestFeatureGuard (); | ||
TestStubbedFeatureSwitch (); | ||
} | ||
|
||
[Kept] | ||
static void TestFeatureSwitch () { | ||
if (FeatureProperties.FeatureSwitchDefinition) | ||
Unused (); | ||
} | ||
|
||
[Kept] | ||
static void TestFeatureGuard () { | ||
if (FeatureProperties.FeatureGuard) | ||
Unused (); | ||
} | ||
|
||
[Kept] | ||
static void TestStubbedFeatureSwitch () { | ||
if (FeatureProperties.StubbedFeatureSwitch) | ||
Unused (); | ||
} | ||
|
||
[Kept] | ||
static void Unused () { } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/tools/illink/test/Mono.Linker.Tests.Cases/LinkAttributes/StubFeatureSwitch.xml
This file contains 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,7 @@ | ||
<linker xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../src/ILLink.Shared/ILLink.LinkAttributes.xsd"> | ||
<assembly fullname="FeatureProperties"> | ||
<type fullname="Mono.Linker.Tests.Cases.LinkAttributes.Dependencies.FeatureProperties"> | ||
<method signature="System.Boolean get_StubbedFeatureSwitch()" body="stub" /> | ||
</type> | ||
</assembly> | ||
</linker> |
10 changes: 10 additions & 0 deletions
10
src/tools/illink/test/Mono.Linker.Tests.Cases/LinkAttributes/TestRemoveFeatureAttributes.xml
This file contains 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,10 @@ | ||
<linker xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../src/ILLink.Shared/ILLink.LinkAttributes.xsd"> | ||
<assembly fullname="System.Private.CoreLib"> | ||
<type fullname="System.Diagnostics.CodeAnalysis.FeatureSwitchDefinitionAttribute"> | ||
<attribute internal="RemoveAttributeInstances"/> | ||
</type> | ||
<type fullname="System.Diagnostics.CodeAnalysis.FeatureGuardAttribute"> | ||
<attribute internal="RemoveAttributeInstances"/> | ||
</type> | ||
</assembly> | ||
</linker> |