forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeVersionsTests.cs
More file actions
726 lines (619 loc) · 33.5 KB
/
CodeVersionsTests.cs
File metadata and controls
726 lines (619 loc) · 33.5 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Diagnostics.DataContractReader.Contracts;
using Microsoft.Diagnostics.DataContractReader.Contracts.Extensions;
using Moq;
using Xunit;
namespace Microsoft.Diagnostics.DataContractReader.Tests;
using static Microsoft.Diagnostics.DataContractReader.Tests.MockExtensions;
using MockCodeVersions = Microsoft.Diagnostics.Internal.RuntimeMemoryMocks.MockDescriptors.CodeVersions;
using Microsoft.Diagnostics.Internal.RuntimeMemoryMocks;
internal static class MockExtensions
{
internal class MockModule
{
public TargetPointer Address { get; set; }
public TargetPointer MethodDefToILCodeVersioningStateAddress { get; set; }
public Dictionary<uint, TargetPointer> MethodDefToILCodeVersioningStateTable {get; set;}
}
internal class MockMethodTable
{
public TargetPointer Address { get; set; }
public MockModule? Module {get; set; }
}
internal class MockMethodDesc
{
public TargetPointer Address { get; private set; }
public bool IsVersionable { get; private set; }
public uint RowId { get; set; }
public uint MethodToken => EcmaMetadataUtils.CreateMethodDef(RowId);
// n.b. in the real RuntimeTypeSystem_1 this is more complex
public TargetCodePointer NativeCode { get; private set; }
// only non-null if IsVersionable is true
public TargetPointer MethodDescVersioningState { get; private set; }
public MockMethodTable? MethodTable { get; set; }
public static MockMethodDesc CreateNonVersionable (TargetPointer selfAddress, TargetCodePointer nativeCode)
{
return new MockMethodDesc() {
Address = selfAddress,
IsVersionable = false,
NativeCode = nativeCode,
MethodDescVersioningState = TargetPointer.Null,
};
}
public static MockMethodDesc CreateVersionable (TargetPointer selfAddress, TargetPointer methodDescVersioningState, TargetCodePointer nativeCode = default)
{
return new MockMethodDesc() {
Address = selfAddress,
IsVersionable = true,
NativeCode = nativeCode,
MethodDescVersioningState = methodDescVersioningState,
};
}
}
internal class MockCodeBlockStart
{
public TargetCodePointer StartAddress { get; set;}
public uint Length { get; set; }
public MockMethodDesc MethodDesc {get; set;}
}
public static void AddCodeBlock(this Mock<IExecutionManager> mock, MockCodeBlockStart block)
{
CodeBlockHandle handle = new CodeBlockHandle(block.StartAddress.AsTargetPointer);
mock.Setup(e => e.GetCodeBlockHandle(It.Is<TargetCodePointer>(ip => ip >= block.StartAddress && ip < block.StartAddress + block.Length)))
.Returns(handle);
mock.Setup(e => e.GetStartAddress(handle)).Returns(block.StartAddress);
mock.Setup(e => e.GetMethodDesc(handle)).Returns(block.MethodDesc.Address);
}
public static void AddModule(this Mock<ILoader> mock, MockModule module)
{
Contracts.ModuleHandle handle = new Contracts.ModuleHandle(module.Address);
mock.Setup(l => l.GetModuleHandleFromModulePtr(module.Address)).Returns(handle);
mock.Setup(l => l.GetLookupTables(handle)).Returns(new ModuleLookupTables() {
MethodDefToILCodeVersioningState = module.MethodDefToILCodeVersioningStateAddress,
});
mock.Setup(l => l.GetModuleLookupMapElement(module.MethodDefToILCodeVersioningStateAddress, It.IsAny<uint>(), out It.Ref<TargetNUInt>.IsAny))
.Returns<TargetPointer, uint, TargetNUInt>((table, token, flags) =>
{
flags = new TargetNUInt(0);
if (module.MethodDefToILCodeVersioningStateTable.TryGetValue(EcmaMetadataUtils.GetRowId(token), out TargetPointer value))
{
return value;
}
throw new InvalidOperationException($"No token found for 0x{token:x} in table {table}");
});
}
public static void AddMethodDesc(this Mock<IRuntimeTypeSystem> mock, MockMethodDesc methodDesc)
{
MethodDescHandle handle = new MethodDescHandle(methodDesc.Address);
mock.Setup(r => r.GetMethodDescHandle(methodDesc.Address)).Returns(handle);
mock.Setup(r => r.IsVersionable(handle)).Returns(methodDesc.IsVersionable);
mock.Setup(r => r.GetNativeCode(handle)).Returns(methodDesc.NativeCode);
mock.Setup(r => r.GetMethodDescVersioningState(handle)).Returns(methodDesc.MethodDescVersioningState);
mock.Setup(r => r.GetMethodTable(handle)).Returns(() => methodDesc.MethodTable?.Address ?? throw new InvalidOperationException($"MethodTable not found for {methodDesc.Address}"));
mock.Setup(r => r.GetMethodToken(handle)).Returns(methodDesc.MethodToken);
}
public static void AddMethodTable(this Mock<IRuntimeTypeSystem> mock, MockCodeVersions builder, MockMethodTable methodTable)
{
TypeHandle handle = new TypeHandle(methodTable.Address);
mock.Setup(r => r.GetTypeHandle(methodTable.Address)).Returns<TargetPointer>(address =>
{
// this is not quite accurate on 32 bit architectures, but it's good enough for testing
ulong addressLowBits = (ulong)address & ((ulong)builder.Builder.TargetTestHelpers.PointerSize - 1);
// no typedescs for now, just method tables with 0 in the low bits
if (addressLowBits != 0)
{
throw new InvalidOperationException("Invalid type handle pointer");
}
return handle;
});
mock.Setup(r => r.GetModule(handle)).Returns(methodTable.Module?.Address ?? throw new InvalidOperationException($"Module not found for {handle.Address}"));
}
}
public class CodeVersionsTests
{
internal Target CreateTarget(
MockTarget.Architecture arch,
MockCodeVersions builder,
Mock<ILoader> mockLoader = null,
Mock<IExecutionManager> mockExecutionManager = null,
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = null)
{
mockLoader ??= new Mock<ILoader>();
mockExecutionManager ??= new Mock<IExecutionManager>();
mockRuntimeTypeSystem ??= new Mock<IRuntimeTypeSystem>();
TestPlaceholderTarget target = new TestPlaceholderTarget(
arch,
builder.Builder.GetMemoryContext().ReadFromTarget,
builder.Types);
IContractFactory<ICodeVersions> cvfactory = new CodeVersionsFactory();
ContractRegistry reg = Mock.Of<ContractRegistry>(
c => c.CodeVersions == cvfactory.CreateContract(target, 1)
&& c.RuntimeTypeSystem == mockRuntimeTypeSystem.Object
&& c.ExecutionManager == mockExecutionManager.Object
&& c.Loader == mockLoader.Object);
target.SetContracts(reg);
return target;
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetNativeCodeVersion_Null(MockTarget.Architecture arch)
{
var mockExecutionManager = new Mock<IExecutionManager>();
mockExecutionManager.Setup(e => e.GetCodeBlockHandle(TargetCodePointer.Null)).Returns(() => null);
var target = CreateTarget(arch, new MockCodeVersions(arch), mockExecutionManager: mockExecutionManager);
var codeVersions = target.Contracts.CodeVersions;
Assert.NotNull(codeVersions);
TargetCodePointer nullPointer = TargetCodePointer.Null;
var handle = codeVersions.GetNativeCodeVersionForIP(nullPointer);
Assert.False(handle.Valid);
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetNativeCodeVersion_OneVersion_NonVersionable(MockTarget.Architecture arch)
{
TargetCodePointer codeBlockStart = new TargetCodePointer(0x0a0a_0000);
MockMethodDesc oneMethod = MockMethodDesc.CreateNonVersionable(selfAddress: new TargetPointer(0x1a0a_0000), nativeCode: codeBlockStart);
MockCodeBlockStart oneBlock = new MockCodeBlockStart()
{
StartAddress = codeBlockStart,
Length = 0x100,
MethodDesc = oneMethod,
};
Mock<IExecutionManager> mockExecutionManager = new Mock<IExecutionManager>();
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new Mock<IRuntimeTypeSystem>();
mockExecutionManager.AddCodeBlock(oneBlock);
mockRuntimeTypeSystem.AddMethodDesc(oneMethod);
var target = CreateTarget(
arch,
new MockCodeVersions(arch),
mockExecutionManager: mockExecutionManager,
mockRuntimeTypeSystem: mockRuntimeTypeSystem);
// TEST
var codeVersions = target.Contracts.CodeVersions;
Assert.NotNull(codeVersions);
TargetCodePointer codeBlockEnd = codeBlockStart + oneBlock.Length;
for (TargetCodePointer ip = codeBlockStart; ip < codeBlockEnd; ip++)
{
var handle = codeVersions.GetNativeCodeVersionForIP(ip);
Assert.True(handle.Valid);
// FIXME: do we want to lock this down? it's part of the algorithm details, but maybe not part of the contract
//Assert.Equal(oneBlock.MethodDescAddress, handle.MethodDescAddress);
TargetCodePointer actualCodeStart = codeVersions.GetNativeCode(handle);
Assert.Equal(codeBlockStart, actualCodeStart);
}
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetNativeCodeVersion_OneVersion_Versionable(MockTarget.Architecture arch)
{
var builder = new MockCodeVersions(arch);
TargetPointer nativeCodeVersionNode = builder.AddNativeCodeVersionNode();
TargetPointer methodDescVersioningStateAddress = builder.AddMethodDescVersioningState(nativeCodeVersionNode, true);
TargetCodePointer codeBlockStart = new TargetCodePointer(0x0a0a_0000);
MockMethodDesc oneMethod = MockMethodDesc.CreateVersionable(selfAddress: new TargetPointer(0x1a0a_0000), methodDescVersioningState: methodDescVersioningStateAddress);
MockCodeBlockStart oneBlock = new MockCodeBlockStart()
{
StartAddress = codeBlockStart,
Length = 0x100,
MethodDesc = oneMethod,
};
builder.FillNativeCodeVersionNode(nativeCodeVersionNode, methodDesc: oneMethod.Address, nativeCode: codeBlockStart, next: TargetPointer.Null, isActive: false, ilVersionId: default);
Mock<IExecutionManager> mockExecutionManager = new Mock<IExecutionManager>();
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new Mock<IRuntimeTypeSystem>();
mockExecutionManager.AddCodeBlock(oneBlock);
mockRuntimeTypeSystem.AddMethodDesc(oneMethod);
var target = CreateTarget(
arch,
builder,
mockExecutionManager: mockExecutionManager,
mockRuntimeTypeSystem: mockRuntimeTypeSystem);
// TEST
var codeVersions = target.Contracts.CodeVersions;
Assert.NotNull(codeVersions);
TargetCodePointer codeBlockEnd = codeBlockStart + oneBlock.Length;
for (TargetCodePointer ip = codeBlockStart; ip < codeBlockEnd; ip++)
{
var handle = codeVersions.GetNativeCodeVersionForIP(ip);
Assert.True(handle.Valid);
// FIXME: do we want to lock this down? it's part of the algorithm details, but maybe not part of the contract
//Assert.Equal(oneBlock.MethodDescAddress, handle.MethodDescAddress);
TargetCodePointer actualCodeStart = codeVersions.GetNativeCode(handle);
Assert.Equal(codeBlockStart, actualCodeStart);
}
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetActiveNativeCodeVersion_DefaultCase(MockTarget.Architecture arch)
{
uint methodRowId = 0x25; // arbitrary
TargetCodePointer expectedNativeCodePointer = new TargetCodePointer(0x0700_abc0);
uint methodDefToken = EcmaMetadataUtils.CreateMethodDef(methodRowId);
var builder = new MockCodeVersions(arch);
var methodDescAddress = new TargetPointer(0x00aa_aa00);
var methodDescNilTokenAddress = new TargetPointer(0x00aa_bb00);
var moduleAddress = new TargetPointer(0x00ca_ca00);
TargetPointer versioningState = builder.AddILCodeVersioningState(
activeVersionKind: 0/*==unknown*/,
activeVersionNode: TargetPointer.Null,
activeVersionModule: moduleAddress,
activeVersionMethodDef: methodDefToken,
firstVersionNode: TargetPointer.Null);
var module = new MockModule() {
Address = moduleAddress,
MethodDefToILCodeVersioningStateAddress = new TargetPointer(0x00da_da00),
MethodDefToILCodeVersioningStateTable = new Dictionary<uint, TargetPointer>() {
{ methodRowId, versioningState}
},
};
var methodTable = new MockMethodTable() {
Address = new TargetPointer(0x00ba_ba00),
Module = module,
};
var method = MockMethodDesc.CreateVersionable(selfAddress: methodDescAddress, methodDescVersioningState: TargetPointer.Null, nativeCode: expectedNativeCodePointer);
method.MethodTable = methodTable;
method.RowId = methodRowId;
var methodNilToken = MockMethodDesc.CreateVersionable(selfAddress: methodDescNilTokenAddress, methodDescVersioningState: TargetPointer.Null, nativeCode: expectedNativeCodePointer);
methodNilToken.MethodTable = methodTable;
Mock<ILoader> mockLoader = new Mock<ILoader>();
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new Mock<IRuntimeTypeSystem>();
mockRuntimeTypeSystem.AddMethodDesc(method);
mockRuntimeTypeSystem.AddMethodDesc(methodNilToken);
mockRuntimeTypeSystem.AddMethodTable(builder, methodTable);
mockLoader.AddModule(module);
var target = CreateTarget(
arch,
builder,
mockLoader: mockLoader,
mockRuntimeTypeSystem: mockRuntimeTypeSystem);
// TEST
var codeVersions = target.Contracts.CodeVersions;
Assert.NotNull(codeVersions);
{
NativeCodeVersionHandle handle = codeVersions.GetActiveNativeCodeVersion(methodDescAddress);
Assert.True(handle.Valid);
Assert.Equal(methodDescAddress, handle.MethodDescAddress);
var actualCodeAddress = codeVersions.GetNativeCode(handle);
Assert.Equal(expectedNativeCodePointer, actualCodeAddress);
}
{
NativeCodeVersionHandle handle = codeVersions.GetActiveNativeCodeVersion(methodDescNilTokenAddress);
Assert.True(handle.Valid);
Assert.Equal(methodDescNilTokenAddress, handle.MethodDescAddress);
var actualCodeAddress = codeVersions.GetNativeCode(handle);
Assert.Equal(expectedNativeCodePointer, actualCodeAddress);
}
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetActiveNativeCodeVersion_IterateVersionNodes(MockTarget.Architecture arch)
{
GetActiveNativeCodeVersion_IterateVersionNodes_Impl(arch, shouldFindActiveCodeVersion: true);
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetActiveNativeCodeVersion_IterateVersionNodes_NoMatch(MockTarget.Architecture arch)
{
GetActiveNativeCodeVersion_IterateVersionNodes_Impl(arch, shouldFindActiveCodeVersion: false);
}
private void GetActiveNativeCodeVersion_IterateVersionNodes_Impl(MockTarget.Architecture arch, bool shouldFindActiveCodeVersion)
{
uint methodRowId = 0x25; // arbitrary
TargetCodePointer expectedNativeCodePointer = new TargetCodePointer(0x0700_abc0);
uint methodDefToken = 0x06000000 | methodRowId;
var builder = new MockCodeVersions(arch);
var methodDescAddress = new TargetPointer(0x00aa_aa00);
var moduleAddress = new TargetPointer(0x00ca_ca00);
TargetPointer versioningState = builder.AddILCodeVersioningState(
activeVersionKind: 0/*==unknown*/,
activeVersionNode: TargetPointer.Null,
activeVersionModule: moduleAddress,
activeVersionMethodDef: methodDefToken,
firstVersionNode: TargetPointer.Null);
var module = new MockModule()
{
Address = moduleAddress,
MethodDefToILCodeVersioningStateAddress = new TargetPointer(0x00da_da00),
MethodDefToILCodeVersioningStateTable = new Dictionary<uint, TargetPointer>() {
{ methodRowId, versioningState}
},
};
var methodTable = new MockMethodTable()
{
Address = new TargetPointer(0x00ba_ba00),
Module = module,
};
// Add the linked list of native code version nodes
int count = 3;
int activeIndex = shouldFindActiveCodeVersion ? count - 1 : -1;
(TargetPointer firstNode, TargetPointer activeVersionNode) = builder.AddNativeCodeVersionNodesForMethod(methodDescAddress, count, activeIndex, expectedNativeCodePointer, default);
TargetPointer methodDescVersioningStateAddress = builder.AddMethodDescVersioningState(nativeCodeVersionNode: firstNode, isDefaultVersionActive: false);
var methodDesc = MockMethodDesc.CreateVersionable(selfAddress: methodDescAddress, methodDescVersioningState: methodDescVersioningStateAddress, nativeCode: expectedNativeCodePointer);
methodDesc.MethodTable = methodTable;
methodDesc.RowId = methodRowId;
Mock<ILoader> mockLoader = new Mock<ILoader>();
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new Mock<IRuntimeTypeSystem>();
mockLoader.AddModule(module);
mockRuntimeTypeSystem.AddMethodDesc(methodDesc);
mockRuntimeTypeSystem.AddMethodTable(builder, methodTable);
var target = CreateTarget(
arch,
builder,
mockLoader: mockLoader,
mockRuntimeTypeSystem: mockRuntimeTypeSystem);
// TEST
var codeVersions = target.Contracts.CodeVersions;
Assert.NotNull(codeVersions);
NativeCodeVersionHandle handle = codeVersions.GetActiveNativeCodeVersion(methodDescAddress);
if (shouldFindActiveCodeVersion)
{
Assert.True(handle.Valid);
Assert.Equal(activeVersionNode, handle.CodeVersionNodeAddress);
var actualCodeAddress = codeVersions.GetNativeCode(handle);
Assert.Equal(expectedNativeCodePointer, actualCodeAddress);
}
else
{
Assert.False(handle.Valid);
}
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetActiveNativeCodeVersion_ExplicitILCodeVersion(MockTarget.Architecture arch)
{
GetActiveNativeCodeVersion_ExplicitILCodeVersion_Impl(arch, shouldFindActiveCodeVersion: true);
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetActiveNativeCodeVersion_ExplicitILCodeVersion_NoMatch(MockTarget.Architecture arch)
{
GetActiveNativeCodeVersion_ExplicitILCodeVersion_Impl(arch, shouldFindActiveCodeVersion: false);
}
private void GetActiveNativeCodeVersion_ExplicitILCodeVersion_Impl(MockTarget.Architecture arch, bool shouldFindActiveCodeVersion)
{
uint methodRowId = 0x25; // arbitrary
TargetCodePointer expectedNativeCodePointer = new TargetCodePointer(0x0700_abc0);
var builder = new MockCodeVersions(arch);
var methodDescAddress = new TargetPointer(0x00aa_aa00);
var moduleAddress = new TargetPointer(0x00ca_ca00);
TargetNUInt ilVersionId = new TargetNUInt(5);
TargetPointer ilVersionNode = builder.AddILCodeVersionNode(TargetPointer.Null, ilVersionId, /* kStateActive */ 0x00000002);
TargetPointer versioningState = builder.AddILCodeVersioningState(
activeVersionKind: 1 /* Explicit */,
activeVersionNode: ilVersionNode,
activeVersionModule: TargetPointer.Null,
activeVersionMethodDef: 0,
firstVersionNode: ilVersionNode);
var module = new MockModule()
{
Address = moduleAddress,
MethodDefToILCodeVersioningStateAddress = new TargetPointer(0x00da_da00),
MethodDefToILCodeVersioningStateTable = new Dictionary<uint, TargetPointer>() {
{ methodRowId, versioningState}
},
};
var methodTable = new MockMethodTable()
{
Address = new TargetPointer(0x00ba_ba00),
Module = module,
};
// Add the linked list of native code version nodes
int count = 3;
int activeIndex = shouldFindActiveCodeVersion ? count - 1 : -1;
TargetNUInt activeIlVersionId = shouldFindActiveCodeVersion ? ilVersionId : default;
(TargetPointer firstNode, TargetPointer activeVersionNode) = builder.AddNativeCodeVersionNodesForMethod(methodDescAddress, count, activeIndex, expectedNativeCodePointer, activeIlVersionId);
TargetPointer methodDescVersioningStateAddress = builder.AddMethodDescVersioningState(nativeCodeVersionNode: firstNode, isDefaultVersionActive: false);
var oneMethod = MockMethodDesc.CreateVersionable(selfAddress: methodDescAddress, methodDescVersioningState: methodDescVersioningStateAddress, nativeCode: expectedNativeCodePointer);
oneMethod.MethodTable = methodTable;
oneMethod.RowId = methodRowId;
Mock<ILoader> mockLoader = new Mock<ILoader>();
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new Mock<IRuntimeTypeSystem>();
mockLoader.AddModule(module);
mockRuntimeTypeSystem.AddMethodDesc(oneMethod);
mockRuntimeTypeSystem.AddMethodTable(builder, methodTable);
var target = CreateTarget(
arch,
builder,
mockLoader: mockLoader,
mockRuntimeTypeSystem: mockRuntimeTypeSystem);
// TEST
var codeVersions = target.Contracts.CodeVersions;
Assert.NotNull(codeVersions);
var handle = codeVersions.GetActiveNativeCodeVersion(methodDescAddress);
if (shouldFindActiveCodeVersion)
{
Assert.True(handle.Valid);
Assert.Equal(activeVersionNode, handle.CodeVersionNodeAddress);
var actualCodeAddress = codeVersions.GetNativeCode(handle);
Assert.Equal(expectedNativeCodePointer, actualCodeAddress);
}
else
{
Assert.False(handle.Valid);
}
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetILCodeVersions_SyntheticAndExplicit(MockTarget.Architecture arch)
{
uint methodRowId = 0x25; // arbitrary
TargetCodePointer expectedSyntheticCodePointer = new TargetCodePointer(0x0700_abc0);
TargetCodePointer expectedExplicitCodePointer = new TargetCodePointer(0x0780_abc0);
var builder = new MockCodeVersions(arch);
var methodDescAddress = new TargetPointer(0x00aa_aa00);
var moduleAddress = new TargetPointer(0x00ca_ca00);
TargetNUInt ilVersionId = new TargetNUInt(2);
TargetPointer ilVersionNode = builder.AddILCodeVersionNode(TargetPointer.Null, ilVersionId, /* kStateActive */ 0x00000002);
TargetPointer versioningState = builder.AddILCodeVersioningState(
activeVersionKind: 1 /* Explicit */,
activeVersionNode: ilVersionNode,
activeVersionModule: TargetPointer.Null,
activeVersionMethodDef: 0,
firstVersionNode: ilVersionNode);
var module = new MockModule()
{
Address = moduleAddress,
MethodDefToILCodeVersioningStateAddress = new TargetPointer(0x00da_da00),
MethodDefToILCodeVersioningStateTable = new Dictionary<uint, TargetPointer>() {
{ methodRowId, versioningState}
},
};
var methodTable = new MockMethodTable()
{
Address = new TargetPointer(0x00ba_ba00),
Module = module,
};
(TargetPointer firstNode, _) = builder.AddNativeCodeVersionNodesForMethod(methodDescAddress, 2, 1, expectedSyntheticCodePointer, new TargetNUInt(0));
(firstNode, _) = builder.AddNativeCodeVersionNodesForMethod(methodDescAddress, 2, 1, expectedExplicitCodePointer, ilVersionId, firstNode);
TargetPointer methodDescVersioningStateAddress = builder.AddMethodDescVersioningState(nativeCodeVersionNode: firstNode, isDefaultVersionActive: false);
var oneMethod = MockMethodDesc.CreateVersionable(selfAddress: methodDescAddress, methodDescVersioningState: methodDescVersioningStateAddress);
oneMethod.MethodTable = methodTable;
oneMethod.RowId = methodRowId;
Mock<ILoader> mockLoader = new Mock<ILoader>();
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new Mock<IRuntimeTypeSystem>();
mockLoader.AddModule(module);
mockRuntimeTypeSystem.AddMethodDesc(oneMethod);
mockRuntimeTypeSystem.AddMethodTable(builder, methodTable);
var target = CreateTarget(
arch,
builder,
mockLoader: mockLoader,
mockRuntimeTypeSystem: mockRuntimeTypeSystem);
// TEST
var codeVersions = target.Contracts.CodeVersions;
Assert.NotNull(codeVersions);
// Get all ILCodeVersions
List<ILCodeVersionHandle> ilCodeVersions = codeVersions.GetILCodeVersions(methodDescAddress).ToList();
Assert.Equal(2, ilCodeVersions.Count);
// Get the explicit ILCodeVersion and assert that it is in the list of ILCodeVersions
ILCodeVersionHandle explicitILCodeVersion = codeVersions.GetActiveILCodeVersion(methodDescAddress);
Assert.Contains(ilCodeVersions, ilcodeVersion => ilcodeVersion.Equals(explicitILCodeVersion));
Assert.Equal(expectedExplicitCodePointer, codeVersions.GetNativeCode(codeVersions.GetActiveNativeCodeVersionForILCodeVersion(methodDescAddress, explicitILCodeVersion)));
// Find the other ILCodeVersion (synthetic) and assert that it is valid.
ILCodeVersionHandle syntheticILcodeVersion = ilCodeVersions.Find(ilCodeVersion => !ilCodeVersion.Equals(explicitILCodeVersion));
Assert.True(syntheticILcodeVersion.IsValid);
Assert.Equal(expectedSyntheticCodePointer, codeVersions.GetNativeCode(codeVersions.GetActiveNativeCodeVersionForILCodeVersion(methodDescAddress, syntheticILcodeVersion)));
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void IlToNativeToIlCodeVersion_SyntheticAndExplicit(MockTarget.Architecture arch)
{
uint methodRowId = 0x25; // arbitrary
TargetCodePointer expectedSyntheticCodePointer = new TargetCodePointer(0x0700_abc0);
TargetCodePointer expectedExplicitCodePointer = new TargetCodePointer(0x0780_abc0);
var builder = new MockCodeVersions(arch);
var methodDescAddress = new TargetPointer(0x00aa_aa00);
var moduleAddress = new TargetPointer(0x00ca_ca00);
TargetNUInt ilVersionId = new TargetNUInt(2);
TargetPointer ilVersionNode = builder.AddILCodeVersionNode(TargetPointer.Null, ilVersionId, /* kStateActive */ 0x00000002);
TargetPointer versioningState = builder.AddILCodeVersioningState(
activeVersionKind: 1 /* Explicit */,
activeVersionNode: ilVersionNode,
activeVersionModule: TargetPointer.Null,
activeVersionMethodDef: 0,
firstVersionNode: ilVersionNode);
var module = new MockModule()
{
Address = moduleAddress,
MethodDefToILCodeVersioningStateAddress = new TargetPointer(0x00da_da00),
MethodDefToILCodeVersioningStateTable = new Dictionary<uint, TargetPointer>() {
{ methodRowId, versioningState}
},
};
var methodTable = new MockMethodTable()
{
Address = new TargetPointer(0x00ba_ba00),
Module = module,
};
(TargetPointer firstNode, _) = builder.AddNativeCodeVersionNodesForMethod(methodDescAddress, 2, 1, expectedSyntheticCodePointer, new TargetNUInt(0));
(firstNode, _) = builder.AddNativeCodeVersionNodesForMethod(methodDescAddress, 2, 1, expectedExplicitCodePointer, ilVersionId, firstNode);
TargetPointer methodDescVersioningStateAddress = builder.AddMethodDescVersioningState(nativeCodeVersionNode: firstNode, isDefaultVersionActive: false);
var oneMethod = MockMethodDesc.CreateVersionable(selfAddress: methodDescAddress, methodDescVersioningState: methodDescVersioningStateAddress);
oneMethod.MethodTable = methodTable;
oneMethod.RowId = methodRowId;
Mock<ILoader> mockLoader = new Mock<ILoader>();
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new Mock<IRuntimeTypeSystem>();
mockLoader.AddModule(module);
mockRuntimeTypeSystem.AddMethodDesc(oneMethod);
mockRuntimeTypeSystem.AddMethodTable(builder, methodTable);
var target = CreateTarget(
arch,
builder,
mockLoader: mockLoader,
mockRuntimeTypeSystem: mockRuntimeTypeSystem);
// TEST
var codeVersions = target.Contracts.CodeVersions;
Assert.NotNull(codeVersions);
// Get all ILCodeVersions
List<ILCodeVersionHandle> ilCodeVersions = codeVersions.GetILCodeVersions(methodDescAddress).ToList();
Assert.Equal(2, ilCodeVersions.Count);
// Get the explicit ILCodeVersion and assert that it is in the list of ILCodeVersions
ILCodeVersionHandle explicitILCodeVersion = codeVersions.GetActiveILCodeVersion(methodDescAddress);
Assert.Contains(ilCodeVersions, ilcodeVersion => ilcodeVersion.Equals(explicitILCodeVersion));
Assert.True(explicitILCodeVersion.IsValid);
// Find the other ILCodeVersion (synthetic) and assert that it is valid.
ILCodeVersionHandle syntheticILcodeVersion = ilCodeVersions.Find(ilCodeVersion => !ilCodeVersion.Equals(explicitILCodeVersion));
Assert.True(syntheticILcodeVersion.IsValid);
// Verify getting ILCode is equal to ILCode from NativeCode from ILCode.
NativeCodeVersionHandle explicitNativeCodeVersion = codeVersions.GetActiveNativeCodeVersionForILCodeVersion(methodDescAddress, explicitILCodeVersion);
Assert.True(explicitILCodeVersion.Equals(codeVersions.GetILCodeVersion(explicitNativeCodeVersion)));
NativeCodeVersionHandle syntheticNativeCodeVersion = codeVersions.GetActiveNativeCodeVersionForILCodeVersion(methodDescAddress, syntheticILcodeVersion);
Assert.True(syntheticILcodeVersion.Equals(codeVersions.GetILCodeVersion(syntheticNativeCodeVersion)));
}
private void GetGCStressCodeCopy_Impl(MockTarget.Architecture arch, bool returnsNull)
{
MockCodeVersions builder = new(arch);
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new();
// Setup synthetic NativeCodeVersion
TargetPointer expectedSyntheticCodeCopyAddr = returnsNull ? TargetPointer.Null : new(0x2345_6789);
TargetPointer syntheticMethodDescAddr = new(0x2345_8000);
NativeCodeVersionHandle syntheticHandle = NativeCodeVersionHandle.CreateSynthetic(syntheticMethodDescAddr);
MethodDescHandle methodDescHandle = new MethodDescHandle(syntheticMethodDescAddr);
mockRuntimeTypeSystem.Setup(rts => rts.GetMethodDescHandle(syntheticMethodDescAddr)).Returns(methodDescHandle);
mockRuntimeTypeSystem.Setup(rts => rts.GetGCStressCodeCopy(methodDescHandle)).Returns(expectedSyntheticCodeCopyAddr);
// Setup explicit NativeCodeVersion
TargetPointer? explicitGCCoverageInfoAddr = returnsNull ? TargetPointer.Null : new(0x1234_5678);
TargetPointer nativeCodeVersionNode = builder.AddNativeCodeVersionNode();
builder.FillNativeCodeVersionNode(
nativeCodeVersionNode,
methodDesc: TargetPointer.Null,
nativeCode: TargetCodePointer.Null,
next: TargetPointer.Null,
isActive: true,
ilVersionId: new(1),
gcCoverageInfo: explicitGCCoverageInfoAddr);
NativeCodeVersionHandle explicitHandle = NativeCodeVersionHandle.CreateExplicit(nativeCodeVersionNode);
var target = CreateTarget(arch, builder, mockRuntimeTypeSystem: mockRuntimeTypeSystem);
// TEST
var codeVersions = target.Contracts.CodeVersions;
TargetPointer actualSyntheticCodeCopyAddr = codeVersions.GetGCStressCodeCopy(syntheticHandle);
Assert.Equal(expectedSyntheticCodeCopyAddr, actualSyntheticCodeCopyAddr);
if(returnsNull)
{
TargetPointer actualExplicitCodeCopyAddr = codeVersions.GetGCStressCodeCopy(explicitHandle);
Assert.Equal(TargetPointer.Null, actualExplicitCodeCopyAddr);
}
else
{
Target.TypeInfo gcCoverageInfoType = target.GetTypeInfo(DataType.GCCoverageInfo);
TargetPointer expectedExplicitCodeCopyAddr = explicitGCCoverageInfoAddr.Value + (ulong)gcCoverageInfoType.Fields["SavedCode"].Offset;
TargetPointer actualExplicitCodeCopyAddr = codeVersions.GetGCStressCodeCopy(explicitHandle);
Assert.Equal(expectedExplicitCodeCopyAddr, actualExplicitCodeCopyAddr);
}
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetGCStressCodeCopy_Null(MockTarget.Architecture arch)
{
GetGCStressCodeCopy_Impl(arch, returnsNull: true);
}
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetGCStressCodeCopy_NotNull(MockTarget.Architecture arch)
{
GetGCStressCodeCopy_Impl(arch, returnsNull: false);
}
}