-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Max Charlamb
committed
Dec 2, 2024
1 parent
15c0dac
commit 7a104d0
Showing
5 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// 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 Microsoft.Diagnostics.DataContractReader.Data; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Microsoft.Diagnostics.DataContractReader.Tests; | ||
|
||
using MockGCCover = MockDescriptors.GCCover; | ||
|
||
public class GCCoverTests | ||
{ | ||
internal static Target CreateTarget( | ||
MockTarget.Architecture arch, | ||
MockGCCover builder, | ||
Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = null) | ||
{ | ||
TestPlaceholderTarget target = new TestPlaceholderTarget(arch, builder.Builder.GetReadContext().ReadFromTarget, builder.Types, builder.Globals); | ||
|
||
mockRuntimeTypeSystem ??= new Mock<IRuntimeTypeSystem>(); | ||
|
||
IContractFactory<IGCCover> gcCoverFactory = new GCCoverFactory(); | ||
|
||
ContractRegistry reg = Mock.Of<ContractRegistry>( | ||
c => c.GCCover == gcCoverFactory.CreateContract(target, 1) | ||
&& c.RuntimeTypeSystem == mockRuntimeTypeSystem.Object); | ||
target.SetContracts(reg); | ||
return target; | ||
} | ||
|
||
[Theory] | ||
[ClassData(typeof(MockTarget.StdArch))] | ||
public void GetGCCoverageInfo_Explicit(MockTarget.Architecture arch) | ||
{ | ||
GetGCCoverageInfoExplicitHelper(arch, new TargetPointer(0x1234_5678)); | ||
GetGCCoverageInfoExplicitHelper(arch, TargetPointer.Null); | ||
} | ||
|
||
private void GetGCCoverageInfoExplicitHelper(MockTarget.Architecture arch, TargetPointer expectedGCCoverageInfo) | ||
{ | ||
MockGCCover mockGCCover = new(arch); | ||
|
||
NativeCodeVersionHandle codeVersionHandle = mockGCCover.AddExplicitNativeCodeVersion(expectedGCCoverageInfo); | ||
|
||
var target = CreateTarget(arch, mockGCCover); | ||
|
||
// TEST | ||
|
||
var gcCover = target.Contracts.GCCover; | ||
Assert.NotNull(gcCover); | ||
|
||
TargetPointer? actualGCCoverageInfo = gcCover.GetGCCoverageInfo(codeVersionHandle); | ||
|
||
Assert.NotNull(actualGCCoverageInfo); | ||
Assert.Equal(expectedGCCoverageInfo, actualGCCoverageInfo); | ||
} | ||
|
||
[Theory] | ||
[ClassData(typeof(MockTarget.StdArch))] | ||
public void GetGCCoverageInfo_Synthetic(MockTarget.Architecture arch) | ||
{ | ||
GetGCCoverageInfoSyntheticHelper(arch, new TargetPointer(0x1234_5678)); | ||
GetGCCoverageInfoSyntheticHelper(arch, TargetPointer.Null); | ||
} | ||
|
||
|
||
private void GetGCCoverageInfoSyntheticHelper(MockTarget.Architecture arch, TargetPointer expectedGCCoverageInfo) | ||
{ | ||
Mock<IRuntimeTypeSystem> mockRTS = new Mock<IRuntimeTypeSystem>(); | ||
|
||
TargetPointer methodDesc = new(/* arbitrary */ 0x1234_5678); | ||
MockSyntheticGCCoverageInfo(mockRTS, methodDesc, expectedGCCoverageInfo); | ||
|
||
NativeCodeVersionHandle codeVersionHandle = NativeCodeVersionHandle.OfSynthetic(methodDesc); | ||
|
||
var target = CreateTarget(arch, new MockGCCover(arch), mockRTS); | ||
|
||
// TEST | ||
|
||
var gcCover = target.Contracts.GCCover; | ||
Assert.NotNull(gcCover); | ||
|
||
TargetPointer? actualGCCoverageInfo = gcCover.GetGCCoverageInfo(codeVersionHandle); | ||
|
||
Assert.NotNull(actualGCCoverageInfo); | ||
Assert.Equal(expectedGCCoverageInfo, actualGCCoverageInfo); | ||
} | ||
|
||
private void MockSyntheticGCCoverageInfo(Mock<IRuntimeTypeSystem> mockRTS, TargetPointer methodDescAddress, TargetPointer expectedGCCoverageInfo) | ||
{ | ||
MethodDescHandle methodDescHandle = new MethodDescHandle(methodDescAddress); | ||
mockRTS.Setup(rts => rts | ||
.GetMethodDescHandle(methodDescAddress)) | ||
.Returns(methodDescHandle); | ||
mockRTS.Setup(rts => rts | ||
.GetGCCoverageInfo(methodDescHandle)) | ||
.Returns(expectedGCCoverageInfo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/native/managed/cdacreader/tests/MockDescriptors/MockDescriptors.GCCover.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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 Microsoft.Diagnostics.DataContractReader.Contracts; | ||
|
||
namespace Microsoft.Diagnostics.DataContractReader.Tests; | ||
|
||
internal partial class MockDescriptors | ||
{ | ||
public class GCCover | ||
{ | ||
private const ulong DefaultAllocationRangeStart = 0x0011_1000; | ||
private const ulong DefaultAllocationRangeEnd = 0x00012_0000; | ||
|
||
internal readonly MockMemorySpace.Builder Builder; | ||
|
||
internal Dictionary<DataType, Target.TypeInfo> Types { get; } | ||
internal (string Name, ulong Value)[] Globals { get; } | ||
|
||
private CodeVersions _codeVersions { get; } | ||
public GCCover(MockTarget.Architecture arch) | ||
: this(new MockMemorySpace.Builder(new TargetTestHelpers(arch))) | ||
{ } | ||
|
||
public GCCover(MockMemorySpace.Builder builder) | ||
{ | ||
Builder = builder; | ||
|
||
_codeVersions = new CodeVersions(Builder); | ||
|
||
Types = CodeVersions.GetTypes(builder.TargetTestHelpers); | ||
} | ||
|
||
public NativeCodeVersionHandle AddExplicitNativeCodeVersion(TargetPointer gcCoverPointer) | ||
{ | ||
TargetPointer nativeCodeVersionNode = _codeVersions.AddNativeCodeVersionNode(); | ||
_codeVersions.FillNativeCodeVersionNode( | ||
nativeCodeVersionNode, | ||
TargetPointer.Null, | ||
TargetCodePointer.Null, | ||
TargetPointer.Null, | ||
true, | ||
new(1), | ||
gcCoverPointer); | ||
|
||
return NativeCodeVersionHandle.OfExplicit(nativeCodeVersionNode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters