Skip to content

Commit 048a932

Browse files
committed
Preserve all InternalsVisibleTo attributes in ILLink Trimmer
Removes previous special handling to only keep IVT attributes for assemblies that were resolvable and marked at link time. This behaviour could cause a noticeable difference in behaviour at runtime without emitting any trim analysis warnings while providing no mechanism to retain the attributes. The removal of these attributes provides only a neglible size reduction. Fixes #92582
1 parent 1400c1e commit 048a932

File tree

2 files changed

+10
-49
lines changed

2 files changed

+10
-49
lines changed

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

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
using System.Diagnostics.CodeAnalysis;
3737
using System.Linq;
3838
using System.Reflection.Runtime.TypeParsing;
39-
using System.Runtime.CompilerServices;
4039
using System.Text.RegularExpressions;
4140
using ILLink.Shared;
4241
using ILLink.Shared.TrimAnalysis;
@@ -62,7 +61,6 @@ protected LinkContext Context {
6261
protected Queue<(MethodDefinition, DependencyInfo, MessageOrigin)> _methods;
6362
protected HashSet<(MethodDefinition, MarkScopeStack.Scope)> _virtual_methods;
6463
protected Queue<AttributeProviderPair> _assemblyLevelAttributes;
65-
readonly List<AttributeProviderPair> _ivt_attributes;
6664
protected Queue<(AttributeProviderPair, DependencyInfo, MarkScopeStack.Scope)> _lateMarkedAttributes;
6765
protected List<(TypeDefinition, MarkScopeStack.Scope)> _typesWithInterfaces;
6866
protected HashSet<AssemblyDefinition> _dynamicInterfaceCastableImplementationTypesDiscovered;
@@ -222,7 +220,6 @@ public MarkStep ()
222220
_methods = new Queue<(MethodDefinition, DependencyInfo, MessageOrigin)> ();
223221
_virtual_methods = new HashSet<(MethodDefinition, MarkScopeStack.Scope)> ();
224222
_assemblyLevelAttributes = new Queue<AttributeProviderPair> ();
225-
_ivt_attributes = new List<AttributeProviderPair> ();
226223
_lateMarkedAttributes = new Queue<(AttributeProviderPair, DependencyInfo, MarkScopeStack.Scope)> ();
227224
_typesWithInterfaces = new List<(TypeDefinition, MarkScopeStack.Scope)> ();
228225
_dynamicInterfaceCastableImplementationTypesDiscovered = new HashSet<AssemblyDefinition> ();
@@ -288,42 +285,6 @@ protected virtual void Complete ()
288285
}
289286
}
290287

291-
bool ProcessInternalsVisibleAttributes ()
292-
{
293-
bool marked_any = false;
294-
foreach (var attr in _ivt_attributes) {
295-
296-
var provider = attr.Provider;
297-
Debug.Assert (attr.Provider is ModuleDefinition or AssemblyDefinition);
298-
var assembly = (provider is ModuleDefinition module) ? module.Assembly : provider as AssemblyDefinition;
299-
300-
using var assemblyScope = ScopeStack.PushLocalScope (new MessageOrigin (assembly));
301-
302-
if (!Annotations.IsMarked (attr.Attribute) && IsInternalsVisibleAttributeAssemblyMarked (attr.Attribute)) {
303-
MarkCustomAttribute (attr.Attribute, new DependencyInfo (DependencyKind.AssemblyOrModuleAttribute, attr.Provider));
304-
marked_any = true;
305-
}
306-
}
307-
308-
return marked_any;
309-
310-
bool IsInternalsVisibleAttributeAssemblyMarked (CustomAttribute ca)
311-
{
312-
System.Reflection.AssemblyName an;
313-
try {
314-
an = new System.Reflection.AssemblyName ((string) ca.ConstructorArguments[0].Value);
315-
} catch {
316-
return false;
317-
}
318-
319-
var assembly = Context.GetLoadedAssembly (an.Name!);
320-
if (assembly == null)
321-
return false;
322-
323-
return Annotations.IsMarked (assembly.MainModule);
324-
}
325-
}
326-
327288
static bool TypeIsDynamicInterfaceCastableImplementation (TypeDefinition type)
328289
{
329290
if (!type.IsInterface || !type.HasInterfaces || !type.HasCustomAttributes)
@@ -416,8 +377,7 @@ void Process ()
416377
ProcessMarkedPending () ||
417378
ProcessLazyAttributes () ||
418379
ProcessLateMarkedAttributes () ||
419-
MarkFullyPreservedAssemblies () ||
420-
ProcessInternalsVisibleAttributes ()) ;
380+
MarkFullyPreservedAssemblies ()) ;
421381

422382
ProcessPendingTypeChecks ();
423383
}
@@ -1170,6 +1130,9 @@ protected virtual bool ShouldMarkCustomAttribute (CustomAttribute ca, ICustomAtt
11701130
case "System.Runtime.InteropServices.InterfaceTypeAttribute":
11711131
case "System.Runtime.InteropServices.GuidAttribute":
11721132
return true;
1133+
// May be implicitly used by the runtime
1134+
case "System.Runtime.CompilerServices.InternalsVisibleToAttribute":
1135+
return true;
11731136
}
11741137

11751138
TypeDefinition? type = Context.Resolve (attr_type);
@@ -1547,10 +1510,7 @@ bool ProcessLazyAttributes ()
15471510
if (IsAttributeRemoved (customAttribute, resolved.DeclaringType) && Annotations.GetAction (CustomAttributeSource.GetAssemblyFromCustomAttributeProvider (assemblyLevelAttribute.Provider)) == AssemblyAction.Link)
15481511
continue;
15491512

1550-
if (customAttribute.AttributeType.IsTypeOf ("System.Runtime.CompilerServices", "InternalsVisibleToAttribute") && !Annotations.IsMarked (customAttribute)) {
1551-
_ivt_attributes.Add (assemblyLevelAttribute);
1552-
continue;
1553-
} else if (!ShouldMarkTopLevelCustomAttribute (assemblyLevelAttribute, resolved)) {
1513+
if (!ShouldMarkTopLevelCustomAttribute (assemblyLevelAttribute, resolved)) {
15541514
skippedItems.Add (assemblyLevelAttribute);
15551515
continue;
15561516
}

src/tools/illink/test/Mono.Linker.Tests.Cases/Attributes/IVTUnused.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
using System.Runtime.CompilerServices;
21
using Mono.Linker.Tests.Cases.Expectations.Assertions;
32
using Mono.Linker.Tests.Cases.Expectations.Metadata;
43

54
#if IVT
6-
[assembly: InternalsVisibleTo ("missing")]
7-
[assembly: InternalsVisibleTo ("test-with-key, PublicKey=00240000")]
8-
5+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo ("missing")]
6+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo ("test-with-key, PublicKey=00240000")]
97
#endif
108

9+
[assembly: KeptAttributeAttribute ("System.Runtime.CompilerServices.InternalsVisibleToAttribute")]
10+
1111
namespace Mono.Linker.Tests.Cases.Attributes
1212
{
13+
[SetupLinkerArgument("--used-attrs-only", "true")]
1314
[Define ("IVT")]
1415
class IVTUnused
1516
{

0 commit comments

Comments
 (0)