Skip to content
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

Implement support for UnsafeAccessor in the trimmer #88268

Merged
merged 53 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
200b988
First
vitek-karas Jun 14, 2023
bc3dac0
Methods
vitek-karas Jun 15, 2023
3983540
Refactoring and true method resolution
vitek-karas Jun 15, 2023
aa8b53a
Progress
vitek-karas Jun 16, 2023
27d4674
More tests
vitek-karas Jun 19, 2023
7725027
Tests
vitek-karas Jun 20, 2023
7bede4d
Tests
vitek-karas Jun 21, 2023
c83be6f
More
vitek-karas Jun 23, 2023
2e62970
Fixups after a recent fix in CoreCLR implementation
vitek-karas Jun 23, 2023
b7e1484
Revert to method group marking
vitek-karas Jun 26, 2023
b4fea74
Resolve inhertiance behavior inconsistencies
vitek-karas Jun 26, 2023
95f32b2
Simplify the code
vitek-karas Jun 26, 2023
8f2f52e
Add support for fields
vitek-karas Jun 26, 2023
686528a
Methods on value types
vitek-karas Jun 29, 2023
6f1ee84
Fields on value types
vitek-karas Jun 29, 2023
ca883bc
Requires tests
vitek-karas Jun 29, 2023
c7aaabc
DAM and UnsafeAccessor tests
vitek-karas Jun 30, 2023
93b53a3
Formatting
vitek-karas Jun 30, 2023
9fd8178
Formatting
vitek-karas Jun 30, 2023
e886374
Update src/coreclr/tools/Common/TypeSystem/IL/UnsafeAccessors.cs
vitek-karas Jun 30, 2023
c623196
Linker tests use both XUnit and VSTest format
agocke Jun 30, 2023
0b023e0
Add enablePublishTestResults
agocke Jun 30, 2023
820f14c
Fix API compat - hide the redefined BCL type
vitek-karas Jul 10, 2023
0738762
Exclude the new tests on mono
vitek-karas Jul 11, 2023
b70becc
First
vitek-karas Jun 14, 2023
cb6f71f
Methods
vitek-karas Jun 15, 2023
6638a05
Refactoring and true method resolution
vitek-karas Jun 15, 2023
75974af
Progress
vitek-karas Jun 16, 2023
6722a03
More tests
vitek-karas Jun 19, 2023
6376210
Tests
vitek-karas Jun 20, 2023
6e439de
Tests
vitek-karas Jun 21, 2023
a0b2295
More
vitek-karas Jun 23, 2023
43726d7
Fixups after a recent fix in CoreCLR implementation
vitek-karas Jun 23, 2023
74a5ee5
Revert to method group marking
vitek-karas Jun 26, 2023
47d82d0
Resolve inhertiance behavior inconsistencies
vitek-karas Jun 26, 2023
74d57c0
Simplify the code
vitek-karas Jun 26, 2023
4f945d4
Add support for fields
vitek-karas Jun 26, 2023
13972ca
Methods on value types
vitek-karas Jun 29, 2023
4ffa95d
Fields on value types
vitek-karas Jun 29, 2023
e9fcb68
Requires tests
vitek-karas Jun 29, 2023
ce9376b
DAM and UnsafeAccessor tests
vitek-karas Jun 30, 2023
f2c595a
Formatting
vitek-karas Jun 30, 2023
50a219a
Formatting
vitek-karas Jun 30, 2023
623a2f0
Update src/coreclr/tools/Common/TypeSystem/IL/UnsafeAccessors.cs
vitek-karas Jun 30, 2023
906ccb9
Linker tests use both XUnit and VSTest format
agocke Jun 30, 2023
7dbca19
Add enablePublishTestResults
agocke Jun 30, 2023
45cd27a
Fix API compat - hide the redefined BCL type
vitek-karas Jul 10, 2023
58741fb
Exclude the new tests on mono
vitek-karas Jul 11, 2023
344037f
Merge branch 'main' into TrimmerUnsafeAccessor
lambdageek Jul 17, 2023
ec66190
[mono] uncomment passing tests
lambdageek Jul 14, 2023
22023ed
Merge branch 'TrimmerUnsafeAccessor' of https://github.com/vitek-kara…
vitek-karas Jul 17, 2023
9d5e30f
Fixes after merge with main
vitek-karas Jul 17, 2023
b238760
Update src/tools/illink/src/linker/Linker.Steps/UnsafeAccessorMarker.cs
vitek-karas Jul 17, 2023
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
Prev Previous commit
Next Next commit
DAM and UnsafeAccessor tests
  • Loading branch information
vitek-karas committed Jul 10, 2023
commit c7aaabcab06501779042ac333d7b8b655a39f39c
1 change: 1 addition & 0 deletions src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,7 @@ void ProcessAnalysisAnnotationsForField (FieldDefinition field, DependencyKind d
case DependencyKind.DynamicallyAccessedMember:
case DependencyKind.InteropMethodDependency:
case DependencyKind.Ldtoken:
case DependencyKind.UnsafeAccessorTarget:
if (isReflectionAccessCoveredByDAM = Annotations.FlowAnnotations.ShouldWarnWhenAccessedForReflection (field))
Context.LogWarning (origin, DiagnosticId.DynamicallyAccessedMembersFieldAccessedViaReflection, field.GetDisplayName ());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public Task AnnotatedMembersAccessedViaReflection ()
return RunTest (nameof (AnnotatedMembersAccessedViaReflection));
}

[Fact]
public Task AnnotatedMembersAccessedViaUnsafeAccessor ()
{
return RunTest ();
}

[Fact]
public Task ApplyTypeAnnotations ()
{
Expand Down Expand Up @@ -296,4 +302,4 @@ public Task XmlAnnotations ()
return RunTest (nameof (XmlAnnotations));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Mono.Linker.Tests.Cases.Expectations.Assertions;

namespace Mono.Linker.Tests.Cases.DataFlow
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
class AnnotatedMembersAccessedViaUnsafeAccessor
{
// NativeAOT has basically the best behavior in these cases - it produces the same warnings
// as if the accessor has a direct access to the target member. So if the accessor is correctly annotated
// there will be no warnings.
//
// Trimmer is the worst - due to the fact that we mark all methods with a given name, we can't compare
// annotations and thus we need to warn on all annotated methods. In addition the accesses are modeled
// as reflection accesses so the warning codes are "Reflection" codes. When/If we implement
// correct target resolution, we should be able to emulate the behavior of NativeAOT
//
// Analyzer doesn't warn at all in these cases - analyzer simply can't resolve targets (at least sometimes)
// and so for now we're not doing anything with UnsafeAccessor in the analyzer.

public static void Main ()
{
MethodWithAnnotatedParameter (null, null);
StaticMethodWithAnnotatedReturnValue (null);
VirtualMethodWithAnnotatedReturnValue (null);
AnnotatedField (null);

MethodWithAnnotationMismatch (null, null);
FieldWithAnnotationMismatch (null);
}

class Target
{
private static void MethodWithAnnotatedParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type type) { }

[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
private static Type StaticMethodWithAnnotatedReturnValue () => null;

[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
protected virtual Type VirtualMethodWithAnnotatedReturnValue () => null;

[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
private static Type AnnotatedField;

private static void MethodWithAnnotationMismatch ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type type) { }

[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
private static Type FieldWithAnnotationMismatch;
}

[ExpectedWarning ("IL2111", ProducedBy = Tool.Trimmer)]
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod)]
extern static void MethodWithAnnotatedParameter (Target target, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type);

// No warning - reflection access to a static method with annotated return value is not a problem
[UnsafeAccessor (UnsafeAccessorKind.StaticMethod)]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
extern static Type StaticMethodWithAnnotatedReturnValue (Target target);

[ExpectedWarning ("IL2111", ProducedBy = Tool.Trimmer)]
[UnsafeAccessor (UnsafeAccessorKind.Method)]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
extern static Type VirtualMethodWithAnnotatedReturnValue (Target target);

[ExpectedWarning ("IL2110", ProducedBy = Tool.Trimmer)]
[UnsafeAccessor (UnsafeAccessorKind.StaticField)]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
extern static ref Type AnnotatedField (Target target);

[ExpectedWarning ("IL2111", ProducedBy = Tool.Trimmer)]
[ExpectedWarning ("IL2067", ProducedBy = Tool.NativeAot)]
[UnsafeAccessor (UnsafeAccessorKind.StaticMethod)]
extern static void MethodWithAnnotationMismatch (Target target, [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type type);

[ExpectedWarning ("IL2110", ProducedBy = Tool.Trimmer)]
[ExpectedWarning ("IL2078", ProducedBy = Tool.NativeAot)]
[UnsafeAccessor (UnsafeAccessorKind.StaticField)]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
extern static ref Type FieldWithAnnotationMismatch (Target target);
}
}