Skip to content

Commit e8dd078

Browse files
authored
Merge pull request #80678 from tlakollo/SyncILLink
Sync ILLink
2 parents 36ad988 + 06bf1c6 commit e8dd078

File tree

4 files changed

+265
-21
lines changed

4 files changed

+265
-21
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,7 @@ void ProcessAnalysisAnnotationsForField (FieldDefinition field, DependencyKind d
17931793
case DependencyKind.DynamicDependency:
17941794
case DependencyKind.DynamicallyAccessedMember:
17951795
case DependencyKind.InteropMethodDependency:
1796+
case DependencyKind.Ldtoken:
17961797
if (isReflectionAccessCoveredByDAM = Annotations.FlowAnnotations.ShouldWarnWhenAccessedForReflection (field))
17971798
Context.LogWarning (origin, DiagnosticId.DynamicallyAccessedMembersFieldAccessedViaReflection, field.GetDisplayName ());
17981799

src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DataFlowTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ public Task CompilerGeneratedCodeAccessedViaReflection ()
100100
return RunTest ();
101101
}
102102

103+
[Fact]
104+
public Task ConstructedTypesDataFlow ()
105+
{
106+
return RunTest ();
107+
}
108+
103109
[Fact]
104110
public Task DynamicDependencyDataflow ()
105111
{

src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/AnnotatedMembersAccessedViaReflection.cs

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public static void Main ()
3232
AnnotatedGenerics.Test ();
3333
AnnotationOnGenerics.Test ();
3434
AnnotationOnInteropMethod.Test ();
35-
AccessThroughLdToken.Test ();
3635
}
3736

3837
class AnnotatedField
@@ -128,6 +127,17 @@ static void DynamicallyAccessedMembersNestedTypes2 ()
128127
typeof (AnnotatedField).RequiresNonPublicNestedTypes ();
129128
}
130129

130+
static void PotentialWriteAccess (ref Type type)
131+
{
132+
}
133+
134+
// https://github.com/dotnet/linker/issues/3172
135+
[ExpectedWarning ("IL2110", nameof (AnnotatedField._annotatedField), ProducedBy = ProducedBy.Trimmer)]
136+
static void LdToken ()
137+
{
138+
Expression<Action> a = () => PotentialWriteAccess (ref _annotatedField);
139+
}
140+
131141
[UnconditionalSuppressMessage ("test", "IL2026")]
132142
public static void Test ()
133143
{
@@ -143,6 +153,7 @@ public static void Test ()
143153
DynamicallyAccessedMembersAll2 ();
144154
DynamicallyAccessedMembersNestedTypes1 ();
145155
DynamicallyAccessedMembersNestedTypes2 ();
156+
LdToken ();
146157
}
147158
}
148159

@@ -251,6 +262,14 @@ static void DynamicallyAccessedMembersAll2 ()
251262
typeof (AnnotatedMethodParameters).RequiresAll ();
252263
}
253264

265+
// https://github.com/dotnet/linker/issues/3172
266+
[ExpectedWarning ("IL2111", nameof (MethodWithSingleAnnotatedParameter), ProducedBy = ProducedBy.Trimmer)]
267+
[ExpectedWarning ("IL2067", nameof (MethodWithSingleAnnotatedParameter), ProducedBy = ProducedBy.Analyzer)]
268+
static void LdToken ()
269+
{
270+
Expression<Action<Type>> _ = (Type t) => MethodWithSingleAnnotatedParameter (t);
271+
}
272+
254273
[UnconditionalSuppressMessage ("test", "IL2026")]
255274
public static void Test ()
256275
{
@@ -265,6 +284,7 @@ public static void Test ()
265284
Ldvirtftn ();
266285
DynamicallyAccessedMembersAll1 ();
267286
DynamicallyAccessedMembersAll2 ();
287+
LdToken ();
268288
}
269289
}
270290

@@ -363,6 +383,18 @@ static void LdftnOnVirtual ()
363383
var _ = new Func<Type> ((new AnnotatedMethodReturnValue ()).VirtualMethodWithAnnotatedReturnValue);
364384
}
365385

386+
static void LdTokenOnStatic ()
387+
{
388+
Expression<Action> _ = () => StaticMethodWithAnnotatedReturnValue ();
389+
}
390+
391+
// https://github.com/dotnet/linker/issues/3172
392+
[ExpectedWarning ("IL2111", nameof (VirtualMethodWithAnnotatedReturnValue), ProducedBy = ProducedBy.Trimmer)]
393+
static void LdTokenOnVirtual ()
394+
{
395+
Expression<Action<AnnotatedMethodReturnValue>> _ = (a) => a.VirtualMethodWithAnnotatedReturnValue ();
396+
}
397+
366398
[UnconditionalSuppressMessage ("test", "IL2026")]
367399
public static void Test ()
368400
{
@@ -380,6 +412,8 @@ public static void Test ()
380412
LdftnOnStatic ();
381413
LdftnOnInstance ();
382414
LdftnOnVirtual ();
415+
LdTokenOnStatic ();
416+
LdTokenOnVirtual ();
383417
}
384418
}
385419

@@ -563,6 +597,13 @@ static void DynamicallyAccessedFields ()
563597
typeof (AnnotatedProperty).RequiresNonPublicFields ();
564598
}
565599

600+
// Action delegate is not handled correctly https://github.com/dotnet/linker/issues/2561
601+
[ExpectedWarning ("IL2111", nameof (Property1WithAnnotation), ProducedBy = ProducedBy.Trimmer)]
602+
static void LdToken ()
603+
{
604+
Expression<Func<Type>> _ = () => Property1WithAnnotation;
605+
}
606+
566607
[UnconditionalSuppressMessage ("test", "IL2026")]
567608
public static void Test ()
568609
{
@@ -581,6 +622,7 @@ public static void Test ()
581622
DynamicallyAccessedMembersAll1 ();
582623
DynamicallyAccessedMembersAll2 ();
583624
DynamicallyAccessedFields ();
625+
LdToken ();
584626
}
585627
}
586628

@@ -623,13 +665,20 @@ static void DynamicallyAccessedMembersAll ()
623665
typeof (AnnotatedGenerics).RequiresAll ();
624666
}
625667

668+
[ExpectedWarning ("IL2091", nameof (GenericWithAnnotation))]
669+
static void LdToken<TUnknown> ()
670+
{
671+
Expression<Action> _ = () => GenericWithAnnotation<TUnknown> ();
672+
}
673+
626674
public static void Test ()
627675
{
628676
ReflectionOnly ();
629677
DynamicDependency ();
630678
DynamicallyAccessedMembers ();
631679
InstantiateGeneric ();
632680
DynamicallyAccessedMembersAll ();
681+
LdToken<string> ();
633682
}
634683
}
635684

@@ -693,6 +742,17 @@ static void DynamicallyAccessedMembersAll2 ()
693742
typeof (AnnotationOnGenerics).RequiresAll ();
694743
}
695744

745+
// https://github.com/dotnet/linker/issues/3172
746+
[ExpectedWarning ("IL2111", "GenericWithAnnotatedMethod", "AnnotatedMethod", ProducedBy = ProducedBy.Trimmer)]
747+
static void LdToken ()
748+
{
749+
// Note that this should warn even though the code looks "Correct"
750+
// That is because under the hood the expression tree create MethodInfo which is accessible by anything
751+
// which gets the expression tree as input (so some queryable) and that could invoke the method
752+
// with a different parameter value and thus violate the requirements.
753+
Expression<Action> _ = () => GenericWithAnnotatedMethod<TestType>.AnnotatedMethod (typeof (TestType));
754+
}
755+
696756
public static void Test ()
697757
{
698758
GenericTypeWithStaticMethodViaLdftn ();
@@ -702,6 +762,7 @@ public static void Test ()
702762
GenericMethodDynamicallyAccessedMembers ();
703763
DynamicallyAccessedMembersAll1 ();
704764
DynamicallyAccessedMembersAll2 ();
765+
LdToken ();
705766
}
706767
}
707768

@@ -713,12 +774,12 @@ struct ValueWithAnnotatedField
713774
public Type _typeField;
714775
}
715776

716-
// Analyzer doesnt take into account interop attributes https://github.com/dotnet/linker/issues/2562
777+
// Analyzer doesn't take into account interop attributes https://github.com/dotnet/linker/issues/2562
717778
[ExpectedWarning ("IL2110", nameof (ValueWithAnnotatedField._typeField), ProducedBy = ProducedBy.Trimmer)]
718779
[DllImport ("nonexistent")]
719780
static extern ValueWithAnnotatedField GetValueWithAnnotatedField ();
720781

721-
// Analyzer doesnt take into account interop attributes https://github.com/dotnet/linker/issues/2562
782+
// Analyzer doesn't take into account interop attributes https://github.com/dotnet/linker/issues/2562
722783
[ExpectedWarning ("IL2110", nameof (ValueWithAnnotatedField._typeField), ProducedBy = ProducedBy.Trimmer)]
723784
[DllImport ("nonexistent")]
724785
static extern void AcceptValueWithAnnotatedField (ValueWithAnnotatedField value);
@@ -730,24 +791,6 @@ public static void Test ()
730791
}
731792
}
732793

733-
class AccessThroughLdToken
734-
{
735-
public virtual Type PropertyWithLdToken {
736-
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
737-
get {
738-
return null;
739-
}
740-
}
741-
742-
// Action delegate is not handled correctly https://github.com/dotnet/linker/issues/2561
743-
[ExpectedWarning ("IL2111", nameof (PropertyWithLdToken), ProducedBy = ProducedBy.Trimmer)]
744-
[ExpectedWarning ("IL2111", nameof (PropertyWithLdToken), ProducedBy = ProducedBy.Trimmer)]
745-
public static void Test ()
746-
{
747-
Expression<Func<Type>> getter = () => (new AccessThroughLdToken ()).PropertyWithLdToken;
748-
}
749-
}
750-
751794
public class AnnotatedAttributeConstructorAttribute : Attribute
752795
{
753796
public AnnotatedAttributeConstructorAttribute ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type)

0 commit comments

Comments
 (0)