Skip to content

Commit 3b1836c

Browse files
authored
Add tests for dataflow with invalid annotations (#101214)
Adds some test coverage for #101211, including a case where ILLink/NativeAot have a similar issue.
1 parent 7deec69 commit 3b1836c

File tree

6 files changed

+170
-34
lines changed

6 files changed

+170
-34
lines changed

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/FlowAnnotations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public bool ShouldWarnWhenAccessedForReflection(FieldDesc field)
253253

254254
public static bool IsTypeInterestingForDataflow(TypeDesc type)
255255
{
256-
// NOTE: this method is not particulary fast. It's assumed that the caller limits
256+
// NOTE: this method is not particularly fast. It's assumed that the caller limits
257257
// calls to this method as much as possible.
258258

259259
if (type.IsWellKnownType(WellKnownType.String))

src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/generated/ILLink.RoslynAnalyzer.Tests.Generator/ILLink.RoslynAnalyzer.Tests.TestCaseGenerator/Inheritance.Interfaces.OnReferenceTypeTests.g.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ public Task UnusedComInterfaceIsKept ()
147147
return RunTest (allowMissingWarnings: true);
148148
}
149149

150+
[Fact]
151+
public Task UnusedComInterfaceIsRemoved ()
152+
{
153+
return RunTest (allowMissingWarnings: true);
154+
}
155+
150156
[Fact]
151157
public Task UnusedExplicitInterfaceHasMethodPreservedViaXml ()
152158
{

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static void Main ()
3939

4040
TestStringEmpty ();
4141

42+
AnnotationOnUnsupportedField.Test ();
4243
WriteArrayField.Test ();
4344
AccessReturnedInstanceField.Test ();
4445
}
@@ -321,6 +322,39 @@ class TypeStore
321322
public static Type _staticTypeWithPublicParameterlessConstructor;
322323
}
323324

325+
class AnnotationOnUnsupportedField
326+
{
327+
class UnsupportedType
328+
{
329+
}
330+
331+
static UnsupportedType GetUnsupportedTypeInstance () => null;
332+
333+
[ExpectedWarning ("IL2097")]
334+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
335+
static UnsupportedType unsupportedTypeInstance;
336+
337+
[ExpectedWarning ("IL2098")]
338+
static void RequirePublicFields (
339+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
340+
UnsupportedType unsupportedTypeInstance)
341+
{
342+
}
343+
344+
[ExpectedWarning ("IL2077", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
345+
static void TestFlowOutOfField ()
346+
{
347+
RequirePublicFields (unsupportedTypeInstance);
348+
}
349+
350+
[ExpectedWarning ("IL2074", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
351+
public static void Test () {
352+
var t = GetUnsupportedTypeInstance ();
353+
unsupportedTypeInstance = t;
354+
TestFlowOutOfField ();
355+
}
356+
}
357+
324358
class WriteArrayField
325359
{
326360
static Type[] ArrayField;

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public static void Main ()
3535
instance.UnknownValueToUnAnnotatedParameterOnInterestingMethod ();
3636
instance.WriteToParameterOnInstanceMethod (null);
3737
instance.LongWriteToParameterOnInstanceMethod (0, 0, 0, 0, null);
38-
instance.UnsupportedParameterType (null);
3938

4039
ParametersPassedToInstanceCtor (typeof (TestType), typeof (TestType));
4140

@@ -44,7 +43,7 @@ public static void Main ()
4443
#if !NATIVEAOT
4544
TestVarargsMethod (typeof (TestType), __arglist (0, 1, 2));
4645
#endif
47-
46+
AnnotationOnUnsupportedParameter.Test ();
4847
WriteCapturedParameter.Test ();
4948
}
5049

@@ -199,11 +198,6 @@ private void UnknownValueToUnAnnotatedParameterOnInterestingMethod ()
199198
RequirePublicParameterlessConstructorAndNothing (typeof (TestType), this.GetType ());
200199
}
201200

202-
[ExpectedWarning ("IL2098", "p1", nameof (UnsupportedParameterType))]
203-
private void UnsupportedParameterType ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] object p1)
204-
{
205-
}
206-
207201
private class InstanceCtor
208202
{
209203
[ExpectedWarning ("IL2067", nameof (DataFlowTypeExtensions.RequiresPublicConstructors))]
@@ -243,6 +237,37 @@ static void TestVarargsMethod ([DynamicallyAccessedMembers (DynamicallyAccessedM
243237
{
244238
}
245239

240+
class AnnotationOnUnsupportedParameter
241+
{
242+
class UnsupportedType ()
243+
{
244+
}
245+
246+
static UnsupportedType GetUnsupportedTypeInstance () => null;
247+
248+
[ExpectedWarning ("IL2098", nameof (UnsupportedType))]
249+
[ExpectedWarning ("IL2067", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
250+
static void RequirePublicMethods (
251+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
252+
UnsupportedType unsupportedTypeInstance)
253+
{
254+
RequirePublicFields (unsupportedTypeInstance);
255+
}
256+
257+
[ExpectedWarning ("IL2098", nameof (UnsupportedType))]
258+
static void RequirePublicFields (
259+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
260+
UnsupportedType unsupportedTypeInstance)
261+
{
262+
}
263+
264+
[ExpectedWarning ("IL2072", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
265+
public static void Test () {
266+
var t = GetUnsupportedTypeInstance ();
267+
RequirePublicMethods (t);
268+
}
269+
}
270+
246271
class WriteCapturedParameter
247272
{
248273
[ExpectedWarning ("IL2072", nameof (GetUnknownType), "parameter")]

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

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public static void Main ()
3535

3636
instance.ReturnWithRequirementsAlwaysThrows ();
3737

38-
UnsupportedReturnType ();
3938
UnsupportedReturnTypeAndParameter (null);
39+
AnnotationOnUnsupportedReturnType.Test ();
4040
}
4141

4242
static Type NoRequirements ()
@@ -181,15 +181,57 @@ Type ReturnWithRequirementsAlwaysThrows ()
181181
throw new NotImplementedException ();
182182
}
183183

184-
[ExpectedWarning ("IL2106", nameof (UnsupportedReturnType))]
185-
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
186-
static object UnsupportedReturnType () => null;
187-
188184
[ExpectedWarning ("IL2106", nameof (UnsupportedReturnTypeAndParameter))]
189185
[ExpectedWarning ("IL2098", nameof (UnsupportedReturnTypeAndParameter))]
190186
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
191187
static object UnsupportedReturnTypeAndParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] object param) => null;
192188

189+
class AnnotationOnUnsupportedReturnType
190+
{
191+
class UnsupportedType
192+
{
193+
[ExpectedWarning ("IL2082", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
194+
public UnsupportedType () {
195+
RequirePublicFields (this);
196+
}
197+
}
198+
199+
static UnsupportedType GetUnsupportedTypeInstance () => null;
200+
201+
[ExpectedWarning ("IL2106")]
202+
// Linker and NativeAot should not produce IL2073
203+
// They produce dataflow warnings despite the invalid annotations.
204+
// https://github.com/dotnet/runtime/issues/101211
205+
[ExpectedWarning ("IL2073", ProducedBy = Tool.Trimmer | Tool.NativeAot)]
206+
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
207+
static UnsupportedType GetWithPublicMethods () {
208+
return GetUnsupportedTypeInstance ();
209+
}
210+
211+
[ExpectedWarning ("IL2098")]
212+
static void RequirePublicFields (
213+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
214+
UnsupportedType unsupportedTypeInstance)
215+
{
216+
}
217+
218+
[ExpectedWarning ("IL2072", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
219+
static void TestMethodReturnValue () {
220+
var t = GetWithPublicMethods ();
221+
RequirePublicFields (t);
222+
}
223+
224+
static void TestCtorReturnValue () {
225+
var t = new UnsupportedType ();
226+
RequirePublicFields (t);
227+
}
228+
229+
public static void Test () {
230+
TestMethodReturnValue ();
231+
TestCtorReturnValue ();
232+
}
233+
}
234+
193235
class TestType
194236
{
195237
public TestType () { }

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

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static void Main ()
2323
PropagateToThisWithSetters ();
2424
AssignToThis ();
2525

26-
TestAnnotationOnNonTypeMethod ();
26+
AnnotationOnUnsupportedThisParameter.Test ();
2727
TestUnknownThis ();
2828
TestFromParameterToThis (null);
2929
TestFromFieldToThis ();
@@ -88,11 +88,56 @@ static void AssignToThis ()
8888
s.AssignToThisCaptured ();
8989
}
9090

91-
static void TestAnnotationOnNonTypeMethod ()
91+
class AnnotationOnUnsupportedThisParameter
9292
{
93-
var t = new NonTypeType ();
94-
t.GetMethod ("foo");
95-
NonTypeType.StaticMethod ();
93+
class UnsupportedType
94+
{
95+
// The AttributeTargets don't support constructors.
96+
// [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
97+
// public UnsupportedType () {
98+
// RequirePublicFields (this);
99+
// }
100+
101+
[ExpectedWarning ("IL2041")]
102+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
103+
public MethodInfo GetMethod (string name)
104+
{
105+
RequirePublicFields (this);
106+
return null;
107+
}
108+
109+
[ExpectedWarning ("IL2041")]
110+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
111+
public static void StaticMethod ()
112+
{
113+
}
114+
}
115+
116+
// Note: this returns a new instance, so that the GetMethod body is considered reachable.
117+
// If nothing created an instance, ILLink would remove the GetMethod body and RequirePublicFields.
118+
static UnsupportedType GetUnsupportedTypeInstance () => new ();
119+
120+
[ExpectedWarning ("IL2098")]
121+
static void RequirePublicFields (
122+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
123+
UnsupportedType unsupportedTypeInstance) { }
124+
125+
[ExpectedWarning ("IL2075", nameof (UnsupportedType), nameof (UnsupportedType.GetMethod), ProducedBy = Tool.Analyzer)] // BUG
126+
static void TestMethodThisParameter () {
127+
var t = GetUnsupportedTypeInstance ();
128+
t.GetMethod ("foo");
129+
}
130+
131+
// static void TestConstructorThisParameter () {
132+
// new UnsupportedType ();
133+
// }
134+
135+
public static void Test ()
136+
{
137+
TestMethodThisParameter ();
138+
// TestConstructorThisParameter ();
139+
UnsupportedType.StaticMethod ();
140+
}
96141
}
97142

98143
[ExpectedWarning ("IL2065", nameof (MethodThisDataFlowTypeTest) + "." + nameof (MethodThisDataFlowTypeTest.RequireThisNonPublicMethods), "'this'")]
@@ -138,22 +183,6 @@ static void TestFromThisToOthers ()
138183
GetWithPublicMethods ().PropagateToThis ();
139184
}
140185

141-
class NonTypeType
142-
{
143-
[ExpectedWarning ("IL2041")]
144-
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
145-
public MethodInfo GetMethod (string name)
146-
{
147-
return null;
148-
}
149-
150-
[ExpectedWarning ("IL2041")]
151-
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
152-
public static void StaticMethod ()
153-
{
154-
}
155-
}
156-
157186
struct StructType
158187
{
159188
int f;

0 commit comments

Comments
 (0)