forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeHelpersTests.cs
More file actions
461 lines (378 loc) · 17.6 KB
/
RuntimeHelpersTests.cs
File metadata and controls
461 lines (378 loc) · 17.6 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Xunit;
namespace System.Runtime.CompilerServices.Tests
{
public static class RuntimeHelpersTests
{
[Fact]
public static void GetHashCodeTest()
{
// Int32 RuntimeHelpers.GetHashCode(Object)
object obj1 = new object();
int h1 = RuntimeHelpers.GetHashCode(obj1);
int h2 = RuntimeHelpers.GetHashCode(obj1);
Assert.Equal(h1, h2);
object obj2 = new object();
int h3 = RuntimeHelpers.GetHashCode(obj2);
Assert.NotEqual(h1, h3); // Could potentially clash but very unlikely
int i123 = 123;
int h4 = RuntimeHelpers.GetHashCode(i123);
Assert.NotEqual(i123.GetHashCode(), h4);
int h5 = RuntimeHelpers.GetHashCode(null);
Assert.Equal(0, h5);
}
public struct TestStruct
{
public int i1;
public int i2;
public override bool Equals(object obj)
{
if (!(obj is TestStruct))
return false;
TestStruct that = (TestStruct)obj;
return i1 == that.i1 && i2 == that.i2;
}
public override int GetHashCode() => i1 ^ i2;
}
[Fact]
public static unsafe void GetObjectValue()
{
// Object RuntimeHelpers.GetObjectValue(Object)
TestStruct t = new TestStruct() { i1 = 2, i2 = 4 };
object tOV = RuntimeHelpers.GetObjectValue(t);
Assert.Equal(t, (TestStruct)tOV);
object o = new object();
object oOV = RuntimeHelpers.GetObjectValue(o);
Assert.Equal(o, oOV);
int i = 3;
object iOV = RuntimeHelpers.GetObjectValue(i);
Assert.Equal(i, (int)iOV);
}
[Fact]
public static void EqualsTest()
{
// Boolean RuntimeHelpers.Equals(Object, Object)
Assert.True(RuntimeHelpers.Equals(Guid.Empty, Guid.Empty));
Assert.False(RuntimeHelpers.Equals(Guid.Empty, Guid.NewGuid()));
// Reference equal
object o = new object();
Assert.True(RuntimeHelpers.Equals(o, o));
// Type mismatch
Assert.False(RuntimeHelpers.Equals(Guid.Empty, string.Empty));
// Non value types
Assert.False(RuntimeHelpers.Equals(new object(), new object()));
Assert.False(RuntimeHelpers.Equals(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 }));
}
[Fact]
public static void InitializeArray()
{
// Void RuntimeHelpers.InitializeArray(Array, RuntimeFieldHandle)
char[] expected = new char[] { 'a', 'b', 'c' }; // Compiler will use RuntimeHelpers.InitializeArray these
}
[Fact]
public static void RunClassConstructor()
{
RuntimeTypeHandle t = typeof(HasCctor).TypeHandle;
RuntimeHelpers.RunClassConstructor(t);
Assert.Equal("Hello", HasCctorReceiver.S);
return;
}
internal class HasCctor
{
static HasCctor()
{
HasCctorReceiver.S = "Hello" + (Guid.NewGuid().ToString().Substring(string.Empty.Length, 0)); // Make sure the preinitialization optimization doesn't eat this.
}
}
internal class HasCctorReceiver
{
public static string S;
}
[Fact]
public static void PrepareMethod()
{
foreach (MethodInfo m in typeof(RuntimeHelpersTests).GetMethods())
RuntimeHelpers.PrepareMethod(m.MethodHandle);
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(default(RuntimeMethodHandle)));
if (RuntimeFeature.IsDynamicCodeSupported)
{
Assert.ThrowsAny<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(IList).GetMethod("Add").MethodHandle));
}
}
[Fact]
public static void PrepareGenericMethod()
{
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(default(RuntimeMethodHandle), null));
//
// Type instantiations
//
// Generic definition with instantiation is valid
RuntimeHelpers.PrepareMethod(typeof(List<>).GetMethod("Add").MethodHandle,
new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle });
// Instantiated method without instantiation is valid
RuntimeHelpers.PrepareMethod(typeof(List<int>).GetMethod("Add").MethodHandle,
null);
if (RuntimeFeature.IsDynamicCodeSupported)
{
// Generic definition without instantiation is invalid
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(List<>).GetMethod("Add").MethodHandle,
null));
// Wrong instantiation
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(List<>).GetMethod("Add").MethodHandle,
new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle, typeof(TestStruct).TypeHandle }));
}
//
// Method instantiations
//
// Generic definition with instantiation is valid
RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize").MethodHandle,
new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle });
// Instantiated method without instantiation is valid
RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize")
.MakeGenericMethod(new Type[] { typeof(TestStruct) }).MethodHandle,
null);
if (RuntimeFeature.IsDynamicCodeSupported)
{
// Generic definition without instantiation is invalid
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize").MethodHandle,
null));
// Wrong instantiation
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize").MethodHandle,
new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle, typeof(TestStruct).TypeHandle }));
}
}
[Fact]
public static void PrepareDelegate()
{
RuntimeHelpers.PrepareDelegate((Action)(() => { }));
RuntimeHelpers.PrepareDelegate((Func<int>)(() => 1) + (Func<int>)(() => 2));
RuntimeHelpers.PrepareDelegate(null);
}
[Fact]
public static void TryEnsureSufficientExecutionStack_SpaceAvailable_ReturnsTrue()
{
Assert.True(RuntimeHelpers.TryEnsureSufficientExecutionStack());
}
[Fact]
public static void TryEnsureSufficientExecutionStack_NoSpaceAvailable_ReturnsFalse()
{
FillStack(depth: 0);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void FillStack(int depth)
{
// This test will fail with a StackOverflowException if TryEnsureSufficientExecutionStack() doesn't
// return false. No exception is thrown and the test finishes when TryEnsureSufficientExecutionStack()
// returns true.
if (!RuntimeHelpers.TryEnsureSufficientExecutionStack())
{
Assert.Throws<InsufficientExecutionStackException>(() => RuntimeHelpers.EnsureSufficientExecutionStack());
return;
}
else if (depth < 2048)
{
FillStack(depth + 1);
}
}
public static IEnumerable<object[]> GetUninitializedObject_NegativeTestCases()
{
yield return new[] { typeof(string), typeof(ArgumentException) }; // variable-length type
yield return new[] { typeof(int[]), typeof(ArgumentException) }; // variable-length type
yield return new[] { typeof(int[,]), typeof(ArgumentException) }; // variable-length type
if (PlatformDetection.IsNonZeroLowerBoundArraySupported)
{
yield return new[] { Array.CreateInstance(typeof(int), new[] { 1 }, new[] { 1 }).GetType(), typeof(ArgumentException) }; // variable-length type (non-szarray)
}
yield return new[] { typeof(Array), typeof(MemberAccessException) }; // abstract type
yield return new[] { typeof(Enum), typeof(MemberAccessException) }; // abstract type
yield return new[] { typeof(Stream), typeof(MemberAccessException) }; // abstract type
yield return new[] { typeof(Buffer), typeof(MemberAccessException) }; // static type (runtime sees it as abstract)
yield return new[] { typeof(IDisposable), typeof(MemberAccessException) }; // interface type
yield return new[] { typeof(List<>), typeof(MemberAccessException) }; // open generic type
yield return new[] { typeof(List<>).GetGenericArguments()[0], PlatformDetection.IsMonoRuntime ? typeof(MemberAccessException) : typeof(ArgumentException) }; // 'T' placeholder typedesc
yield return new[] { typeof(Delegate), typeof(MemberAccessException) }; // abstract type
yield return new[] { typeof(void), typeof(ArgumentException) }; // explicit block in place
yield return new[] { typeof(int).MakePointerType(), typeof(ArgumentException) }; // pointer
yield return new[] { typeof(int).MakeByRefType(), typeof(ArgumentException) }; // byref
yield return new[] { FunctionPointerType(), typeof(ArgumentException) }; // function pointer
static unsafe Type FunctionPointerType() => typeof(delegate*<void>);
yield return new[] { typeof(ReadOnlySpan<int>), typeof(NotSupportedException) }; // byref-like type
yield return new[] { typeof(ArgIterator), typeof(NotSupportedException) }; // byref-like type
Type canonType = typeof(object).Assembly.GetType("System.__Canon", throwOnError: false);
if (canonType != null)
{
yield return new[] { typeof(List<>).MakeGenericType(canonType), typeof(NotSupportedException) }; // shared by generic instantiations
}
Type comObjType = typeof(object).Assembly.GetType("System.__ComObject", throwOnError: false);
if (comObjType != null)
{
yield return new[] { comObjType, typeof(NotSupportedException) }; // COM type
}
if (PlatformDetection.SupportsComInterop)
{
yield return new[] { typeof(WbemContext), typeof(NotSupportedException) }; // COM type
}
}
// This type definition is lifted from System.Management, just for testing purposes
[ClassInterface((short)0x0000)]
[Guid("674B6698-EE92-11D0-AD71-00C04FD8FDFF")]
[ComImport]
internal class WbemContext
{
}
internal class ClassWithBeforeFieldInitCctor
{
private static readonly int _theInt = GetInt();
private static int GetInt()
{
AppDomain.CurrentDomain.SetData("ClassWithBeforeFieldInitCctor_CctorRan", true);
return 0;
}
}
internal class ClassWithNormalCctor
{
#pragma warning disable CS0414 // unused private field
private static readonly int _theInt;
#pragma warning restore CS0414
static ClassWithNormalCctor()
{
AppDomain.CurrentDomain.SetData("ClassWithNormalCctor_CctorRan", true);
_theInt = 0;
}
}
[ActiveIssue("https://github.com/dotnet/runtime/issues/69919", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
[Fact]
public static void GetUninitializedObject_DoesNotRunBeforeFieldInitCctors()
{
object o = RuntimeHelpers.GetUninitializedObject(typeof(ClassWithBeforeFieldInitCctor));
Assert.IsType<ClassWithBeforeFieldInitCctor>(o);
Assert.Null(AppDomain.CurrentDomain.GetData("ClassWithBeforeFieldInitCctor_CctorRan"));
}
[Fact]
public static void GetUninitializedObject_RunsNormalStaticCtors()
{
object o = RuntimeHelpers.GetUninitializedObject(typeof(ClassWithNormalCctor));
Assert.IsType<ClassWithNormalCctor>(o);
Assert.Equal(true, AppDomain.CurrentDomain.GetData("ClassWithNormalCctor_CctorRan"));
}
[Theory]
[MemberData(nameof(GetUninitializedObject_NegativeTestCases))]
public static void GetUninitializedObject_InvalidArguments_ThrowsException(Type typeToInstantiate, Type expectedExceptionType)
{
Assert.Throws(expectedExceptionType, () => RuntimeHelpers.GetUninitializedObject(typeToInstantiate));
}
[Fact]
public static void GetUninitializedObject_DoesNotRunConstructor()
{
Assert.Equal(42, new ObjectWithDefaultCtor().Value);
Assert.Equal(0, ((ObjectWithDefaultCtor)RuntimeHelpers.GetUninitializedObject(typeof(ObjectWithDefaultCtor))).Value);
}
[Fact]
public static void GetUninitializedObject_Struct()
{
object o = RuntimeHelpers.GetUninitializedObject(typeof(Guid));
Assert.Equal(Guid.Empty, Assert.IsType<Guid>(o));
}
[Fact]
public static void GetUninitializedObject_Nullable()
{
// Nullable returns the underlying type instead
object o = RuntimeHelpers.GetUninitializedObject(typeof(int?));
Assert.Equal(0, Assert.IsType<int>(o));
}
private class ObjectWithDefaultCtor
{
public int Value = 42;
}
[Fact]
public static void IsReferenceOrContainsReferences()
{
Assert.False(RuntimeHelpers.IsReferenceOrContainsReferences<int>());
Assert.True(RuntimeHelpers.IsReferenceOrContainsReferences<string>());
Assert.False(RuntimeHelpers.IsReferenceOrContainsReferences<Guid>());
Assert.False(RuntimeHelpers.IsReferenceOrContainsReferences<StructWithoutReferences>());
Assert.True(RuntimeHelpers.IsReferenceOrContainsReferences<StructWithReferences>());
}
[Fact]
public static void ArrayGetSubArrayTest()
{
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Range range = Range.All;
Assert.Equal(a, RuntimeHelpers.GetSubArray(a, range));
range = new Range(Index.FromStart(1), Index.FromEnd(5));
Assert.Equal(new int [] { 2, 3, 4, 5}, RuntimeHelpers.GetSubArray(a, range));
range = new Range(Index.FromStart(0), Index.FromStart(a.Length + 1));
Assert.Throws<ArgumentOutOfRangeException>(() => { int [] array = RuntimeHelpers.GetSubArray(a, range); });
}
[Fact]
public static void ArrayGetSubArrayCoVarianceTest()
{
object[] arr = new string[10];
object[] slice = RuntimeHelpers.GetSubArray<object>(arr, new Range(Index.FromStart(1), Index.FromEnd(2)));
Assert.IsType<string[]>(slice);
uint[] arr2 = (uint[])(object)new int[10];
uint[] slice2 = RuntimeHelpers.GetSubArray<uint>(arr2, new Range(Index.FromStart(1), Index.FromEnd(2)));
Assert.IsType<int[]>(slice2);
}
[Fact]
public static void AllocateTypeAssociatedMemoryInvalidArguments()
{
Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(null, 10); });
Assert.Throws<ArgumentOutOfRangeException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), -1); });
}
[Fact]
public static unsafe void AllocateTypeAssociatedMemoryValidArguments()
{
IntPtr memory = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 32);
Assert.NotEqual(memory, IntPtr.Zero);
// Validate that the memory is zeroed out
Assert.True(new Span<byte>((void*)memory, 32).SequenceEqual(new byte[32]));
}
[StructLayoutAttribute(LayoutKind.Sequential)]
private struct StructWithoutReferences
{
public int a, b, c;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
private struct StructWithReferences
{
public int a, b, c;
public object d;
}
[Fact]
public static void FixedAddressValueTypeTest()
{
// Get addresses of static Age fields.
IntPtr fixedPtr1 = FixedClass.AddressOfFixedAge();
// Garbage collection.
GC.Collect(3, GCCollectionMode.Forced, true, true);
GC.WaitForPendingFinalizers();
// Get addresses of static Age fields after garbage collection.
IntPtr fixedPtr2 = FixedClass.AddressOfFixedAge();
Assert.Equal(fixedPtr1, fixedPtr2);
}
}
public struct Age
{
public int years;
public int months;
}
public class FixedClass
{
[FixedAddressValueType]
public static Age FixedAge;
public static unsafe IntPtr AddressOfFixedAge()
{
fixed (Age* pointer = &FixedAge)
{
return (IntPtr)pointer;
}
}
}
}