Skip to content

Commit

Permalink
Allow stripping resources with ignore-descriptors (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbomer authored Apr 29, 2020
1 parent 4d6d8ad commit e0465e6
Show file tree
Hide file tree
Showing 35 changed files with 265 additions and 63 deletions.
9 changes: 6 additions & 3 deletions src/linker/Linker.Steps/BodySubstituterStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ public BodySubstituterStep (XPathDocument document, string resourceName, Assembl

protected override void Process ()
{
if (!string.IsNullOrEmpty (_resourceName) && Context.StripSubstitutions)
Context.Annotations.AddResourceToRemove (_resourceAssembly, _resourceName);

if (!string.IsNullOrEmpty (_resourceName) && Context.IgnoreSubstitutions)
return;

try {
ReadSubstitutions (_document);

if (!string.IsNullOrEmpty (_resourceName) && Context.StripResources)
Context.Annotations.AddResourceToRemove (_resourceAssembly, _resourceName);
} catch (Exception ex) when (!(ex is XmlResolutionException)) {
throw new XmlResolutionException ($"Failed to process XML substitution: '{_xmlDocumentLocation}'", ex);
}
Expand Down
9 changes: 6 additions & 3 deletions src/linker/Linker.Steps/ResolveFromXmlStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ protected override void Process ()
if (!nav.MoveToChild ("linker", _ns))
return;

if (!string.IsNullOrEmpty (_resourceName) && Context.StripDescriptors)
Context.Annotations.AddResourceToRemove (_resourceAssembly, _resourceName);

if (!string.IsNullOrEmpty (_resourceName) && Context.IgnoreDescriptors)
return;

try {
ProcessAssemblies (Context, nav.SelectChildren ("assembly", _ns));

if (!string.IsNullOrEmpty (_resourceName) && Context.StripResources)
Context.Annotations.AddResourceToRemove (_resourceAssembly, _resourceName);
} catch (Exception ex) when (!(ex is XmlResolutionException)) {
throw new XmlResolutionException (string.Format ("Failed to process XML description: {0}", _xmlDocumentLocation), ex);
}
Expand Down
31 changes: 20 additions & 11 deletions src/linker/Linker/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ protected int SetupContext (ILogger customLogger = null)
var set_optimizations = new List<(CodeOptimizations, string, bool)> ();
bool dumpDependencies = false;
string dependenciesFileName = null;
bool ignoreDescriptors = false;
bool removeCAS = true;
bool new_mvid_used = false;
bool deterministic_used = false;
Expand Down Expand Up @@ -246,8 +245,14 @@ protected int SetupContext (ILogger customLogger = null)

continue;

case "--strip-resources":
if (!GetBoolParam (token, l => context.StripResources = l))
case "--strip-descriptors":
if (!GetBoolParam (token, l => context.StripDescriptors = l))
return -1;

continue;

case "--strip-substitutions":
if (!GetBoolParam (token, l => context.StripSubstitutions = l))
return -1;

continue;
Expand Down Expand Up @@ -320,7 +325,13 @@ protected int SetupContext (ILogger customLogger = null)
continue;

case "--ignore-descriptors":
if (!GetBoolParam (token, l => ignoreDescriptors = l))
if (!GetBoolParam (token, l => context.IgnoreDescriptors = l))
return -1;

continue;

case "--ignore-substitutions":
if (!GetBoolParam (token, l => context.IgnoreSubstitutions = l))
return -1;

continue;
Expand Down Expand Up @@ -507,7 +518,7 @@ protected int SetupContext (ILogger customLogger = null)

continue;
case "z":
if (!GetBoolParam (token, l => ignoreDescriptors = !l))
if (!GetBoolParam (token, l => context.IgnoreDescriptors = !l))
return -1;

continue;
Expand Down Expand Up @@ -573,9 +584,6 @@ protected int SetupContext (ILogger customLogger = null)
foreach (var file in body_substituter_steps)
AddBodySubstituterStep (p, file);

if (ignoreDescriptors)
p.RemoveStep (typeof (BlacklistStep));

if (context.DeterministicOutput)
p.RemoveStep (typeof (RegenerateGuidStep));

Expand Down Expand Up @@ -622,7 +630,7 @@ protected int SetupContext (ILogger customLogger = null)
// [mono only] ResolveFromXApiStep [optional, possibly many]
// LoadReferencesStep
// [mono only] LoadI18nAssemblies
// BlacklistStep [optional]
// BlacklistStep
// dynamically adds steps:
// ResolveFromXmlStep [optional, possibly many]
// BodySubstituterStep [optional, possibly many]
Expand Down Expand Up @@ -922,7 +930,6 @@ protected virtual LinkContext GetDefaultContext (Pipeline pipeline)
#endif
UserAction = AssemblyAction.Link,
OutputDirectory = "output",
StripResources = true
};
return context;
}
Expand Down Expand Up @@ -1011,9 +1018,11 @@ static void Usage ()
Console.WriteLine (" --keep-dep-attributes Keep attributes used for manual dependency tracking. Defaults to false");
Console.WriteLine (" --feature FEATURE VALUE Apply any optimizations defined when this feature setting is a constant known at link time");
Console.WriteLine (" --new-mvid Generate a new guid for each linked assembly (short -g). Defaults to true");
Console.WriteLine (" --strip-resources Remove XML descriptor resources for linked assemblies. Defaults to true");
Console.WriteLine (" --strip-descriptors Remove XML descriptor resources for linked assemblies. Defaults to true");
Console.WriteLine (" --strip-security Remove metadata and code related to Code Access Security. Defaults to true");
Console.WriteLine (" --substitutions FILE Configuration file with field or methods substitution rules");
Console.WriteLine (" --ignore-substitutions Skips reading embedded substitutions. Defaults to false");
Console.WriteLine (" --strip-substitutions Remove XML substitution resources for linked assemblies. Defaults to true");
Console.WriteLine (" --used-attrs-only Attribute usage is removed if the attribute type is not used. Defaults to false");
Console.WriteLine (" --attribute-defs FILE Supplementary custom attribute definitions for attributes controlling the linker behavior.");

Expand Down
11 changes: 9 additions & 2 deletions src/linker/Linker/LinkContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ public bool IgnoreUnresolved {

public bool KeepDependencyAttributes { get; set; }

public bool StripResources { get; set; }
public bool IgnoreDescriptors { get; set; }

public bool IgnoreSubstitutions { get; set; }

public bool StripDescriptors { get; set; }

public bool StripSubstitutions { get; set; }

public Dictionary<string, bool> FeatureSettings { get; private set; }

Expand Down Expand Up @@ -201,7 +207,8 @@ public LinkContext (Pipeline pipeline, AssemblyResolver resolver, ReaderParamete
Tracer = factory.CreateTracer (this);
ReflectionPatternRecorder = new LoggingReflectionPatternRecorder (this);
MarkedKnownMembers = new KnownMembers ();
StripResources = true;
StripDescriptors = true;
StripSubstitutions = true;
PInvokes = new List<PInvokeInfo> ();

// See https://github.com/mono/linker/issues/612
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Mono.Linker.Tests.Cases.Expectations.Metadata
{
public sealed class IgnoreDescriptorsAttribute : BaseMetadataAttribute
{
public readonly bool Value;

public IgnoreDescriptorsAttribute (bool value)
{
Value = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Mono.Linker.Tests.Cases.Expectations.Metadata
{
public sealed class IgnoreSubstitutionsAttribute : BaseMetadataAttribute
{
public readonly bool Value;

public IgnoreSubstitutionsAttribute (bool value)
{
Value = value;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Mono.Linker.Tests.Cases.Expectations.Metadata
{
public sealed class StripResourcesAttribute : BaseMetadataAttribute
public sealed class StripDescriptorsAttribute : BaseMetadataAttribute
{
public readonly bool Value;

public StripResourcesAttribute (bool value)
public StripDescriptorsAttribute (bool value)
{
Value = value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Mono.Linker.Tests.Cases.Expectations.Metadata
{
public sealed class StripSubstitutionsAttribute : BaseMetadataAttribute
{
public readonly bool Value;

public StripSubstitutionsAttribute (bool value)
{
Value = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Mono.Linker.Tests.Cases.LinkXml
[SetupCompileBefore ("CopyLibrary.dll",
new[] { "Dependencies/EmbeddedLinkXmlFromCopyAssemblyIsProcessed/CopyLibrary.cs" },
resources: new[] { "Dependencies/EmbeddedLinkXmlFromCopyAssemblyIsProcessed/CopyLibrary.xml" })]
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]
[SetupLinkerAction ("copy", "CopyLibrary")]

[KeptTypeInAssembly ("CopyLibrary.dll", typeof (CopyLibrary))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Mono.Linker.Tests.Cases.LinkXml
new[] { "Dependencies/EmbeddedLinkXmlPreservesAdditionalAssemblyWithOverriddenMethod/Library2.cs" },
new[] { "Base.dll" },
addAsReference: false)]
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]

[KeptMemberInAssembly ("Library1.dll", typeof (Library1), "VirtualMethodFromBase()")]
[KeptMemberInAssembly ("Library1.dll", typeof (Library1Secondary), "VirtualMethodFromBase()")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mono.Linker.Tests.Cases.PreserveDependencies
/// <summary>
/// This is an acceptable bug with the currently implementation. Embedded link xml files will not be processed
/// </summary>
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]
[SetupCompileBefore ("FakeSystemAssembly.dll", new[] { "Dependencies/PreserveDependencyAttribute.cs" })]
[SetupCompileBefore ("base.dll", new[] { "Dependencies/PreserveDependencyMethodInNonReferencedAssemblyBase.cs" })]
[SetupCompileBefore (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mono.Linker.Tests.Cases.PreserveDependencies
/// <summary>
/// This test is here to ensure that link xml embedded in an assembly used by a [PreserveDependency] is not processed if the dependency is not used
/// </summary>
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]
[SetupCompileBefore ("FakeSystemAssembly.dll", new[] { "Dependencies/PreserveDependencyAttribute.cs" })]
[SetupCompileBefore ("base.dll", new[] { "Dependencies/PreserveDependencyMethodInNonReferencedAssemblyBase.cs" })]
[SetupCompileBefore (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<linker>
<assembly fullname="test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<type fullname="Mono.Linker.Tests.Cases.Resources.EmbeddedLinkXmlFileIsNotProcessedWhenBlacklistStepIsDisabled/Unused" />
<type fullname="Mono.Linker.Tests.Cases.Resources.EmbeddedLinkXmlFileIsNotProcessedWithIgnoreDescriptors/Unused" />
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<linker>
<assembly fullname="test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<type fullname="Mono.Linker.Tests.Cases.Resources.EmbeddedLinkXmlFileIsNotProcessedWithIgnoreDescriptorsAndRemoved/Unused" />
</assembly>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Mono.Linker.Tests.Cases.Resources
new[] { "Dependencies/EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfActionIsCopy_Lib1.cs" },
resources: new[] { "Dependencies/EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfActionIsCopy_Lib1.xml" })]
[SetupLinkerAction ("copy", "EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfActionIsCopy_Lib1")]
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]

[KeptResourceInAssembly ("EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfActionIsCopy_Lib1.dll", "EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfActionIsCopy_Lib1.xml")]
[KeptMemberInAssembly ("EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfActionIsCopy_Lib1.dll", typeof (EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfActionIsCopy_Lib1), "Unused()")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mono.Linker.Tests.Cases.Resources
"library.dll",
new[] { "Dependencies/EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfNameDoesNotMatchAnAssembly_Lib1.cs" },
resources: new[] { "Dependencies/EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfNameDoesNotMatchAnAssembly_Lib1_NotMatchingName.xml" })]
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]

[KeptResourceInAssembly ("library.dll", "EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfNameDoesNotMatchAnAssembly_Lib1_NotMatchingName.xml")]
[RemovedMemberInAssembly ("library.dll", typeof (EmbeddedLinkXmlFileInReferencedAssemblyIsNotProcessedIfNameDoesNotMatchAnAssembly_Lib1), "Unused()")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Mono.Linker.Tests.Cases.Resources
new[] { "Dependencies/EmbeddedLinkXmlFileInReferencedAssemblyIsProcessedIfActionIsLink_Lib1.cs" },
resources: new[] { "Dependencies/EmbeddedLinkXmlFileInReferencedAssemblyIsProcessedIfActionIsLink_Lib1.xml" })]
[SetupLinkerAction ("link", "EmbeddedLinkXmlFileInReferencedAssemblyIsProcessedIfActionIsLink_Lib1")]
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]

[RemovedResourceInAssembly ("EmbeddedLinkXmlFileInReferencedAssemblyIsProcessedIfActionIsLink_Lib1.dll", "EmbeddedLinkXmlFileInReferencedAssemblyIsProcessedIfActionIsLink_Lib1.xml")]
[KeptMemberInAssembly ("EmbeddedLinkXmlFileInReferencedAssemblyIsProcessedIfActionIsLink_Lib1.dll", typeof (EmbeddedLinkXmlFileInReferencedAssemblyIsProcessedIfActionIsLink_Lib1), "Unused()")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Mono.Linker.Tests.Cases.Resources
{
[SetupLinkerCoreAction ("link")]
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]
[SetupCompileResource ("Dependencies/EmbeddedLinkXmlFileIsNotProcessedIfNameDoesNotMatchAnAssembly.xml", "NotMatchingAnAssemblyName.xml")]
[SkipPeVerify]
[KeptResource ("NotMatchingAnAssemblyName.xml")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Mono.Linker.Tests.Cases.Resources
{
[SetupLinkerCoreAction ("link")]
[IncludeBlacklistStep (false)]
[IgnoreDescriptors (true)]
[StripDescriptors (false)]

// We need to rename the resource so that it matches the name of an assembly being processed. This is a requriement of the black list step
[SetupCompileResource ("Dependencies/EmbeddedLinkXmlFileIsNotProcessedWhenBlacklistStepIsDisabled.xml", "test.xml")]
[SetupCompileResource ("Dependencies/EmbeddedLinkXmlFileIsNotProcessedWithIgnoreDescriptors.xml", "test.xml")]
[SkipPeVerify]
[KeptResource ("test.xml")]
public class EmbeddedLinkXmlFileIsNotProcessedWhenBlacklistStepIsDisabled
public class EmbeddedLinkXmlFileIsNotProcessedWithIgnoreDescriptors
{
public static void Main ()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.Resources
{
[SetupLinkerCoreAction ("link")]
[IgnoreDescriptors (true)]
[StripDescriptors (true)]

// We need to rename the resource so that it matches the name of an assembly being processed. This is a requriement of the black list step
[SetupCompileResource ("Dependencies/EmbeddedLinkXmlFileIsNotProcessedWithIgnoreDescriptorsAndRemoved.xml", "test.xml")]
[SkipPeVerify]
[RemovedResourceInAssembly ("test.exe", "test.xml")]
public class EmbeddedLinkXmlFileIsNotProcessedWithIgnoreDescriptorsAndRemoved
{
public static void Main ()
{
}

public class Unused
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Mono.Linker.Tests.Cases.Resources
{
[SetupLinkerCoreAction ("link")]
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]

// We need to rename the resource so that it matches the name of an assembly being processed. This is a requriement of the black list step
[SetupCompileResource ("Dependencies/EmbeddedLinkXmlFileIsProcessed.xml", "test.xml")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Mono.Linker.Tests.Cases.Resources
{
[SetupLinkerCoreAction ("link")]
[IncludeBlacklistStep (true)]
[StripResources (false)]
[IgnoreDescriptors (false)]
[StripDescriptors (false)]

// We need to rename the resource so that it matches the name of an assembly being processed. This is a requriement of the black list step
[SetupCompileResource ("Dependencies/EmbeddedLinkXmlFileIsProcessedAndKept.xml", "test.xml")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Mono.Linker.Tests.Cases.Resources
{
[SetupLinkerCoreAction ("link")]
[IncludeBlacklistStep (true)]
[IgnoreDescriptors (false)]

// We need to rename the resource so that it matches the name of an assembly being processed. This is a requriement of the black list step
[SetupCompileResource ("Dependencies/NonLinkerEmbeddedResourceHasNoImpact.xml", "test.xml")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<linker>
<assembly fullname="test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<type fullname="Mono.Linker.Tests.Cases.Substitutions.EmbeddedSubstitutionsKept">
<method signature="System.Void ConvertToThrowMethod()" body="remove" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<linker>
<assembly fullname="test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<type fullname="Mono.Linker.Tests.Cases.Substitutions.EmbeddedSubstitutionsNotProcessedWithIgnoreSubstitutions">
<method signature="System.Void ConvertToThrowMethod()" body="remove" />
</type>
</assembly>
</linker>
Loading

0 comments on commit e0465e6

Please sign in to comment.