-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Fix feature switch IL when feature attributes are removed #104995
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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> |
Oops, something went wrong.
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.
How do we guarantee this will be called at least once on a given method before the
CodeRewriterStep
?If I understand the repro for this problem, the callsites in trimmed assemblies are all removed due to branch removal, so we won't mark them and thus won't call this either. This leaves
copy
assemblies and their references. I don't remember exactly what type of processing we do there - if we end up callingMarkMethod
then this should be fine.I guess the reason why this works is:
If the
CoreRewriterStep
gets to process a method, that method had to be marked (otherwise it would have been removed) and thusMarkMethod
ended up calling thisTryGetFeatureCheckValue
and thus populate the cache within.I must admit this makes me a bit nervous - it feels pretty fragile. Maybe we should make the link from MarkMethod to this a bit more obvious - currently it goes through GetMethodAction which doesn't "seem" like being related to this that much.
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.
Yes, exactly.
It is a bit fragile. I thought of a way to get to to
TryGetFeatureCheck
in CodeRewriterStep that's not cached - it's possible to stub a body (with XML orSetAction
) without also setting a stubbed value (even though we usually do both). If that's done for a method that also has a feature attribute, the stub value might not have been precomputed.I added a test for this scenario and added a call to
TryGetMethodStubValue
inMarkMethod
. I think that addresses your concern.