You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Warn when accessing DAM annotated method via reflection (#2145)
* Warn when accessing DAM annotated method via reflection
Any access to DAM annotated method must be validated by the trimmer. Normal calls and such are handled already, but direct reflection access or other indirect accesses can lead to trimmer allowing potential invocation of DAM annotated method without validating the annotations. This change will warn whenever such potential "leak" happens.
This applies to method parameter, method return value and field annotations.
Uses the same infra as RUC already just adds additional checks.
Tests for the new cases.
* Formatting
* Only warn on virtual methods with annotate return value
Annotated return value is mostly not problematic (the caller only "Reads" from it, which is always safe). The only case where it's problematic is if something would override the method at runtime (ref emit) in which case the trimmer can't validate the new code that it fulfills the return value requirements. But that can only happen if the method is virtual.
* PR feedback
* PR feedback
* Remove test code.
Copy file name to clipboardExpand all lines: docs/error-codes.md
+32-1Lines changed: 32 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1689,7 +1689,7 @@ The only scopes supported on global unconditional suppressions are 'module', 'ty
1689
1689
it is assumed that the suppression is put on the module. Global unconditional suppressions using invalid scopes are ignored.
1690
1690
1691
1691
```C#
1692
-
// Invalid scope 'method' used in 'UnconditionalSuppressMessageAttribute' on module 'Warning' with target 'MyTarget'.
1692
+
//IL2108: Invalid scope 'method' used in 'UnconditionalSuppressMessageAttribute' on module 'Warning' with target 'MyTarget'.
1693
1693
[module: UnconditionalSuppressMessage ("Test suppression with invalid scope", "IL2026", Scope="method", Target="MyTarget")]
1694
1694
1695
1695
classWarning
@@ -1722,6 +1722,37 @@ class Warning
1722
1722
classDerived : UnsafeClass {}
1723
1723
```
1724
1724
1725
+
#### `IL2110`: Trim analysis: Field 'field' with 'DynamicallyAccessedMembersAttribute' is accessed via reflection. Trimmer can't guarantee availability of the requirements of the field.
1726
+
1727
+
- Trimmer currently can't guarantee that all requirements of the `DynamicallyAccessedMembersAttribute` are fulfilled if the field is accessed via reflection.
// IL2110: Field '_field' with 'DynamicallyAccessedMembersAttribute' is accessed via reflection. Trimmer can't guarantee availability of the requirements of the field.
1736
+
typeof(Test).GetField("_field");
1737
+
}
1738
+
```
1739
+
1740
+
#### `IL2111`: Trim analysis: Method 'method' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the field.
1741
+
1742
+
- Trimmer currently can't guarantee that all requirements of the `DynamicallyAccessedMembersAttribute` are fulfilled if the method is accessed via reflection.
// IL2111: Method 'MethodWithRequirements' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the field.
1752
+
typeof(Test).GetMethod("MethodWithRequirements");
1753
+
}
1754
+
```
1755
+
1725
1756
## Single-File Warning Codes
1726
1757
1727
1758
#### `IL3000`: 'member' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'
$"Field '{field.GetDisplayName()}' with 'DynamicallyAccessedMembersAttribute' is accessed via reflection. Trimmer can't guarantee availability of the requirements of the field.",
$"Method '{method.GetDisplayName()}' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the field.",
0 commit comments