1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
// 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.Globalization;
using System.Reflection;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
using Mono.Linker.Tests.Cases.Expectations.Metadata;
namespace Mono.Linker.Tests.Cases.DataFlow
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
class MethodByRefParameterDataFlow
{
public static void Main ()
{
Type typeWithMethods = _fieldWithMethods;
TestAssignStaticToAnnotatedRefParameter (ref typeWithMethods);
TestAssignParameterToAnnotatedRefParameter (ref typeWithMethods, typeof (TestType));
TestReadFromRefParameter ();
TestReadFromOutParameter_PassedTwice ();
TestReadFromRefParameter_MismatchOnOutput ();
TestReadFromRefParameter_MismatchOnOutput_PassedTwice ();
TestReadFromRefParameter_MismatchOnInput ();
TestReadFromRefParameter_MismatchOnInput_PassedTwice ();
Type nullType1 = null;
TestPassingRefParameter (ref nullType1);
Type nullType2 = null;
TestPassingRefParameter_Mismatch (ref nullType2);
Type nullType3 = null;
TestAssigningToRefParameter (nullType3, ref nullType3);
Type nullType4 = null;
TestAssigningToRefParameter_Mismatch (nullType4, ref nullType4);
TestPassingRefsWithImplicitThis ();
LocalMethodsAndLambdas.Test ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static Type _fieldWithMethods = null;
[ExpectedWarning ("IL2026", "Message for --TestType.Requires--")]
// https://github.com/dotnet/linker/issues/2158
// The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this".
// (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown)
[ExpectedWarning ("IL2065")]
static void TestAssignStaticToAnnotatedRefParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type type)
{
type = typeof (TestTypeWithRequires);
type.GetMethods (); // Should not warn
}
// The warning message is REALLY confusing (basically wrong) since it talks about "calling the method with wrong argument"
// which is definitely not the case here.
[ExpectedWarning ("IL2067", "typeWithFields")]
// https://github.com/dotnet/linker/issues/2158
// The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this".
// (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown)
[ExpectedWarning ("IL2065")]
static void TestAssignParameterToAnnotatedRefParameter (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type type,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type typeWithFields)
{
type = typeWithFields; // Should warn
type.GetMethods (); // Should not warn
}
class TestTypeWithRequires
{
[RequiresUnreferencedCode ("Message for --TestType.Requires--")]
public static void Requires () { }
}
static void TestReadFromRefParameter ()
{
Type typeWithMethods = null;
TryGetAnnotatedValue (ref typeWithMethods);
typeWithMethods.RequiresPublicMethods ();
}
static void TestReadFromOutParameter_PassedTwice ()
{
Type typeWithMethods = null;
TryGetAnnotatedValueFromValue (typeWithMethods, ref typeWithMethods);
typeWithMethods.RequiresPublicMethods ();
}
[ExpectedWarning ("IL2067", nameof (TryGetAnnotatedValue), "RequiresPublicFields")]
static void TestReadFromRefParameter_MismatchOnOutput ()
{
Type typeWithMethods = null;
TryGetAnnotatedValue (ref typeWithMethods);
typeWithMethods.RequiresPublicFields ();
}
[ExpectedWarning ("IL2067", nameof (TryGetAnnotatedValue), "RequiresPublicFields")]
static void TestReadFromRefParameter_MismatchOnOutput_PassedTwice ()
{
Type typeWithMethods = null;
TryGetAnnotatedValueFromValue (typeWithMethods, ref typeWithMethods);
typeWithMethods.RequiresPublicFields ();
}
[ExpectedWarning ("IL2072", nameof (TryGetAnnotatedValue))]
// https://github.com/dotnet/linker/issues/2632
// This second warning should not be generated, the value of typeWithMethods should have PublicMethods
// after the call with out parameter.
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Analyzer)]
static void TestReadFromRefParameter_MismatchOnInput ()
{
Type typeWithMethods = GetTypeWithFields ();
TryGetAnnotatedValue (ref typeWithMethods);
typeWithMethods.RequiresPublicMethods ();
}
[ExpectedWarning ("IL2072", nameof (TryGetAnnotatedValueFromValue))]
[ExpectedWarning ("IL2072", nameof (TryGetAnnotatedValueFromValue))]
// https://github.com/dotnet/linker/issues/2632
// This third warning should not be generated, the value of typeWithMethods should have PublicMethods
// after the call with ref parameter.
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Analyzer)]
static void TestReadFromRefParameter_MismatchOnInput_PassedTwice ()
{
Type typeWithMethods = GetTypeWithFields ();
TryGetAnnotatedValueFromValue (typeWithMethods, ref typeWithMethods);
typeWithMethods.RequiresPublicMethods ();
}
static void TestPassingRefParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type typeWithMethods)
{
TryGetAnnotatedValue (ref typeWithMethods);
}
[ExpectedWarning ("IL2067", "typeWithMethods", nameof (TryGetAnnotatedValue))]
[ExpectedWarning ("IL2067", "typeWithMethods", nameof (TryGetAnnotatedValue))]
static void TestPassingRefParameter_Mismatch ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] ref Type typeWithMethods)
{
TryGetAnnotatedValue (ref typeWithMethods);
}
static void TestAssigningToRefParameter (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type inputTypeWithMethods,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type outputTypeWithMethods)
{
outputTypeWithMethods = inputTypeWithMethods;
}
[ExpectedWarning ("IL2067", "inputTypeWithFields", "outputTypeWithMethods")]
static void TestAssigningToRefParameter_Mismatch (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type inputTypeWithFields,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type outputTypeWithMethods)
{
outputTypeWithMethods = inputTypeWithFields;
}
static bool TryGetAnnotatedValue ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type typeWithMethods)
{
typeWithMethods = null;
return false;
}
static bool TryGetAnnotatedValueFromValue (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type inValue,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type typeWithMethods)
{
typeWithMethods = inValue;
return false;
}
static void TestPassingRefsWithImplicitThis ()
{
var x = new InheritsFromType ();
var param1 = typeof (int);
var param2 = typeof (string);
x.MethodWithRefAndImplicitThis (ref param1, in param2, out var param3);
param1.RequiresPublicMethods ();
param2.RequiresAll ();
param3.RequiresPublicFields ();
}
[return: DynamicallyAccessedMembersAttribute (DynamicallyAccessedMemberTypes.PublicFields)]
static Type GetTypeWithFields () => null;
class TestType
{
}
static class LocalMethodsAndLambdas
{
static ref Type GetTypeRefWithoutAnnotations () { throw null; }
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static ref Type GetTypeRefWithMethods () { throw null; }
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicFields)]
static ref Type GetTypeRefWithMethodsAndFields () { throw null; }
[ExpectedWarning ("IL2067", "t", "InnerMethodWithDam")]
[ExpectedWarning ("IL2067", "tWithMethodsAndFields", "InnerMethodWithDam")]
[ExpectedWarning ("IL2072", nameof (GetTypeRefWithoutAnnotations), "InnerMethodWithDam")]
[ExpectedWarning ("IL2068", nameof (GetTypeRefWithMethodsAndFields), "InnerMethodWithDam")]
static void MethodWithLocaMethodWithDam (Type t, [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type tWithMethods, [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicFields)] Type tWithMethodsAndFields)
{
// 2067
InnerMethodWithDam (ref t);
// Okay
InnerMethodWithDam (ref tWithMethods);
// 2067 (but parameter of inner method targets parameter of outer method)
InnerMethodWithDam (ref tWithMethodsAndFields);
// 2072
InnerMethodWithDam (ref GetTypeRefWithoutAnnotations ());
// No warn
InnerMethodWithDam (ref GetTypeRefWithMethods ());
// 2068
InnerMethodWithDam (ref GetTypeRefWithMethodsAndFields ());
void InnerMethodWithDam ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type typeWithMethods)
{
}
}
public static void Test ()
{
MethodWithLocaMethodWithDam (null, null, null);
}
}
#region InheritsFromType
class InheritsFromType : Type
{
public void MethodWithRefAndImplicitThis ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type type1,
in Type type2,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] out Type type3)
{
type3 = typeof (int);
}
public override Assembly Assembly => throw new NotImplementedException ();
public override string AssemblyQualifiedName => throw new NotImplementedException ();
public override Type BaseType => throw new NotImplementedException ();
public override string FullName => throw new NotImplementedException ();
public override Guid GUID => throw new NotImplementedException ();
public override Module Module => throw new NotImplementedException ();
public override string Namespace => throw new NotImplementedException ();
public override Type UnderlyingSystemType => throw new NotImplementedException ();
public override string Name => throw new NotImplementedException ();
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
public override object[] GetCustomAttributes (bool inherit)
{
throw new NotImplementedException ();
}
public override object[] GetCustomAttributes (Type attributeType, bool inherit)
{
throw new NotImplementedException ();
}
public override Type GetElementType ()
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
public override EventInfo[] GetEvents (BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
public override FieldInfo GetField (string name, BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
public override FieldInfo[] GetFields (BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)]
public override Type GetInterface (string name, bool ignoreCase)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)]
public override Type[] GetInterfaces ()
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields | DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties | DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.NonPublicNestedTypes | DynamicallyAccessedMemberTypes.PublicNestedTypes)]
public override Type GetNestedType (string name, BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.NonPublicNestedTypes | DynamicallyAccessedMemberTypes.PublicNestedTypes)]
public override Type[] GetNestedTypes (BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.NonPublicProperties | DynamicallyAccessedMemberTypes.PublicProperties)]
public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)]
public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
{
throw new NotImplementedException ();
}
public override bool IsDefined (Type attributeType, bool inherit)
{
throw new NotImplementedException ();
}
protected override TypeAttributes GetAttributeFlagsImpl ()
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
{
throw new NotImplementedException ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
{
throw new NotImplementedException ();
}
protected override bool HasElementTypeImpl ()
{
throw new NotImplementedException ();
}
protected override bool IsArrayImpl ()
{
throw new NotImplementedException ();
}
protected override bool IsByRefImpl ()
{
throw new NotImplementedException ();
}
protected override bool IsCOMObjectImpl ()
{
throw new NotImplementedException ();
}
protected override bool IsPointerImpl ()
{
throw new NotImplementedException ();
}
protected override bool IsPrimitiveImpl ()
{
throw new NotImplementedException ();
}
}
#endregion
}
}
|