dotnet / dotnet/linker

Handling of byref return values and locals in data flow analysis

Open
#2,158 4 comments 0 reactions 0 assignees View on GitHub
area-Linker: DataFlow
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

This issue is now tracking all the various cases of `ref` locals and return values for both the linker an analyzer.
It's referred to from some tests, so just search the codebase for the issue number to find some of the known cases where it's problematic (definitely not all).

```C#
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static Type _annotatedField;

static ref Type GetAnnotatedRefType () { return ref _annotatedField; } // No warning currently

static void ByRefReturnValue ()
{
ref Type typeShouldHaveAllMethods = GetAnnotatedRefType();
typeShouldHaveAllMethods = typeof(TestType); // Doesn't apply annotations
_annotatedField.GetMethods (); // Doesn't warn, but now contains typeof(TestType)
}
```

We will need to think this through, how the system should work around byref values.
In a way handling of byref values needs to effectively work "in reverse". Meaning that we need to enforce when assigning to a byref value that the byref value has at least the same annotations as the source value. But then assigning a known type needs to apply annotations from the byref to the type... so it's like writing to an annotated field.

There's also a reflection based test which also needs to correctly warn:
```C#
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static Type _annotatedField;

[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static ref Type GetAnnotatedRefType () { return ref _annotatedField; }

delegate ref Type DelegateOverGetRefType ();
static void ByRefReturnValue ()
{
var d = (DelegateOverGetRefType)typeof (AnnotatedMethodReturnValue)
.GetMethod (nameof (GetAnnotatedRefType))
.CreateDelegate (typeof (DelegateOverGetRefType));

ref Type typeShouldHaveAllMethods = ref d ();
typeShouldHaveAllMethods = typeof (TestType); // Doesn't apply annotations
_annotatedField.GetMethods (); // Doesn't warn, but now contains typeof(TestType)
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.