Skip to content

Add tests for dataflow with invalid annotations #101214

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

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public bool ShouldWarnWhenAccessedForReflection(FieldDesc field)

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

if (type.IsWellKnownType(WellKnownType.String))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public Task UnusedComInterfaceIsKept ()
return RunTest (allowMissingWarnings: true);
}

[Fact]
public Task UnusedComInterfaceIsRemoved ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just updating the generated test sources to include the new test added in #101087.

{
return RunTest (allowMissingWarnings: true);
}

[Fact]
public Task UnusedExplicitInterfaceHasMethodPreservedViaXml ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static void Main ()

TestStringEmpty ();

AnnotationOnUnsupportedField.Test ();
WriteArrayField.Test ();
AccessReturnedInstanceField.Test ();
}
Expand Down Expand Up @@ -321,6 +322,39 @@ class TypeStore
public static Type _staticTypeWithPublicParameterlessConstructor;
}

class AnnotationOnUnsupportedField
{
class UnsupportedType
{
}

static UnsupportedType GetUnsupportedTypeInstance () => null;

[ExpectedWarning ("IL2097")]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static UnsupportedType unsupportedTypeInstance;

[ExpectedWarning ("IL2098")]
static void RequirePublicFields (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
UnsupportedType unsupportedTypeInstance)
{
}

[ExpectedWarning ("IL2077", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
static void TestFlowOutOfField ()
{
RequirePublicFields (unsupportedTypeInstance);
}

[ExpectedWarning ("IL2074", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
public static void Test () {
var t = GetUnsupportedTypeInstance ();
unsupportedTypeInstance = t;
TestFlowOutOfField ();
}
}

class WriteArrayField
{
static Type[] ArrayField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public static void Main ()
instance.UnknownValueToUnAnnotatedParameterOnInterestingMethod ();
instance.WriteToParameterOnInstanceMethod (null);
instance.LongWriteToParameterOnInstanceMethod (0, 0, 0, 0, null);
instance.UnsupportedParameterType (null);

ParametersPassedToInstanceCtor (typeof (TestType), typeof (TestType));

Expand All @@ -44,7 +43,7 @@ public static void Main ()
#if !NATIVEAOT
TestVarargsMethod (typeof (TestType), __arglist (0, 1, 2));
#endif

AnnotationOnUnsupportedParameter.Test ();
WriteCapturedParameter.Test ();
}

Expand Down Expand Up @@ -199,11 +198,6 @@ private void UnknownValueToUnAnnotatedParameterOnInterestingMethod ()
RequirePublicParameterlessConstructorAndNothing (typeof (TestType), this.GetType ());
}

[ExpectedWarning ("IL2098", "p1", nameof (UnsupportedParameterType))]
private void UnsupportedParameterType ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] object p1)
{
}

private class InstanceCtor
{
[ExpectedWarning ("IL2067", nameof (DataFlowTypeExtensions.RequiresPublicConstructors))]
Expand Down Expand Up @@ -243,6 +237,37 @@ static void TestVarargsMethod ([DynamicallyAccessedMembers (DynamicallyAccessedM
{
}

class AnnotationOnUnsupportedParameter
{
class UnsupportedType ()
{
}

static UnsupportedType GetUnsupportedTypeInstance () => null;

[ExpectedWarning ("IL2098", nameof (UnsupportedType))]
[ExpectedWarning ("IL2067", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
static void RequirePublicMethods (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
UnsupportedType unsupportedTypeInstance)
{
RequirePublicFields (unsupportedTypeInstance);
}

[ExpectedWarning ("IL2098", nameof (UnsupportedType))]
static void RequirePublicFields (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
UnsupportedType unsupportedTypeInstance)
{
}

[ExpectedWarning ("IL2072", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
public static void Test () {
var t = GetUnsupportedTypeInstance ();
RequirePublicMethods (t);
}
}

class WriteCapturedParameter
{
[ExpectedWarning ("IL2072", nameof (GetUnknownType), "parameter")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void Main ()

instance.ReturnWithRequirementsAlwaysThrows ();

UnsupportedReturnType ();
UnsupportedReturnTypeAndParameter (null);
AnnotationOnUnsupportedReturnType.Test ();
}

static Type NoRequirements ()
Expand Down Expand Up @@ -181,15 +181,57 @@ Type ReturnWithRequirementsAlwaysThrows ()
throw new NotImplementedException ();
}

[ExpectedWarning ("IL2106", nameof (UnsupportedReturnType))]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static object UnsupportedReturnType () => null;

[ExpectedWarning ("IL2106", nameof (UnsupportedReturnTypeAndParameter))]
[ExpectedWarning ("IL2098", nameof (UnsupportedReturnTypeAndParameter))]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static object UnsupportedReturnTypeAndParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] object param) => null;

class AnnotationOnUnsupportedReturnType
{
class UnsupportedType
{
[ExpectedWarning ("IL2082", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
public UnsupportedType () {
RequirePublicFields (this);
}
}

static UnsupportedType GetUnsupportedTypeInstance () => null;

[ExpectedWarning ("IL2106")]
// Linker and NativeAot should not produce IL2073
// They produce dataflow warnings despite the invalid annotations.
// https://github.com/dotnet/runtime/issues/101211
[ExpectedWarning ("IL2073", ProducedBy = Tool.Trimmer | Tool.NativeAot)]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static UnsupportedType GetWithPublicMethods () {
return GetUnsupportedTypeInstance ();
}

[ExpectedWarning ("IL2098")]
static void RequirePublicFields (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
UnsupportedType unsupportedTypeInstance)
{
}

[ExpectedWarning ("IL2072", ProducedBy = Tool.Analyzer)] // https://github.com/dotnet/runtime/issues/101211
static void TestMethodReturnValue () {
var t = GetWithPublicMethods ();
RequirePublicFields (t);
}

static void TestCtorReturnValue () {
var t = new UnsupportedType ();
RequirePublicFields (t);
}

public static void Test () {
TestMethodReturnValue ();
TestCtorReturnValue ();
}
}

class TestType
{
public TestType () { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void Main ()
PropagateToThisWithSetters ();
AssignToThis ();

TestAnnotationOnNonTypeMethod ();
AnnotationOnUnsupportedThisParameter.Test ();
TestUnknownThis ();
TestFromParameterToThis (null);
TestFromFieldToThis ();
Expand Down Expand Up @@ -88,11 +88,56 @@ static void AssignToThis ()
s.AssignToThisCaptured ();
}

static void TestAnnotationOnNonTypeMethod ()
class AnnotationOnUnsupportedThisParameter
{
var t = new NonTypeType ();
t.GetMethod ("foo");
NonTypeType.StaticMethod ();
class UnsupportedType
{
// The AttributeTargets don't support constructors.
// [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
// public UnsupportedType () {
// RequirePublicFields (this);
// }

[ExpectedWarning ("IL2041")]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
public MethodInfo GetMethod (string name)
{
RequirePublicFields (this);
return null;
}

[ExpectedWarning ("IL2041")]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
public static void StaticMethod ()
{
}
}

// Note: this returns a new instance, so that the GetMethod body is considered reachable.
// If nothing created an instance, ILLink would remove the GetMethod body and RequirePublicFields.
static UnsupportedType GetUnsupportedTypeInstance () => new ();

[ExpectedWarning ("IL2098")]
static void RequirePublicFields (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
UnsupportedType unsupportedTypeInstance) { }

[ExpectedWarning ("IL2075", nameof (UnsupportedType), nameof (UnsupportedType.GetMethod), ProducedBy = Tool.Analyzer)] // BUG
static void TestMethodThisParameter () {
var t = GetUnsupportedTypeInstance ();
t.GetMethod ("foo");
}

// static void TestConstructorThisParameter () {
// new UnsupportedType ();
// }

public static void Test ()
{
TestMethodThisParameter ();
// TestConstructorThisParameter ();
UnsupportedType.StaticMethod ();
}
}

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

class NonTypeType
{
[ExpectedWarning ("IL2041")]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
public MethodInfo GetMethod (string name)
{
return null;
}

[ExpectedWarning ("IL2041")]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
public static void StaticMethod ()
{
}
}

struct StructType
{
int f;
Expand Down