Skip to content

Commit

Permalink
rewrite all contract impls in terms of ITarget
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdageek committed Sep 12, 2024
1 parent bcb9eac commit f2844ae
Show file tree
Hide file tree
Showing 22 changed files with 114 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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;

namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal interface IRegistry
{
public IException Exception { get;}
public ILoader Loader { get; }
public IEcmaMetadata EcmaMetadata { get; }
public IObject Object { get; }
public IThread Thread { get; }
public IRuntimeTypeSystem RuntimeTypeSystem { get; }
public IDacStreams DacStreams { get; }
public ICodeVersions CodeVersions { get; }
public IPrecodeStubs PrecodeStubs { get; }
public IExecutionManager ExecutionManager { get; }
public IReJIT ReJIT { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Microsoft.Diagnostics.DataContractReader.Data;

public interface IData<TSelf> where TSelf : IData<TSelf>
internal interface IData<TSelf> where TSelf : IData<TSelf>
{
static abstract TSelf Create(ITarget target, TargetPointer address);
}
Original file line number Diff line number Diff line change
@@ -1,26 +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 System.Diagnostics.CodeAnalysis;
using System.Numerics;

namespace Microsoft.Diagnostics.DataContractReader;

public interface ITarget
internal interface ITarget
{
int PointerSize { get; }

TargetPointer ReadGlobalPointer(string global);

TargetPointer ReadPointer(ulong address);
public TargetCodePointer ReadCodePointer(ulong address);

void ReadBuffer(ulong address, Span<byte> buffer);

/// <summary>
/// Read a null-terminated UTF-16 string from the target in target endianness
/// </summary>
/// <param name="address">Address to start reading from</param>
/// <returns>String read from the target</returns>}
public string ReadUtf16String(ulong address);

T ReadGlobal<T>(string name) where T : struct, INumber<T>;

/// <summary>
/// Read a value from the target in target endianness
/// </summary>
/// <typeparam name="T">Type of value to read</typeparam>
/// <param name="address">Address to start reading from</param>
/// <returns>Value read from the target</returns>
public T Read<T>(ulong address) where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T>;

public bool IsAlignedToPointerSize(TargetPointer pointer);

TypeInfo GetTypeInfo(DataType type);

IDataCache ProcessedData { get; }

public interface IDataCache
{
T GetOrAdd<T>(TargetPointer address) where T : Data.IData<T>;
bool TryGet<T>(ulong address, [NotNullWhen(true)] out T? data);
}

public record struct TypeInfo
Expand All @@ -38,5 +63,5 @@ public record struct FieldInfo
public string? TypeName;
}


Contracts.IRegistry Contracts { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal readonly partial struct CodeVersions_1 : ICodeVersions
{
private readonly Target _target;
private readonly ITarget _target;


public CodeVersions_1(ITarget target)
{
_target = (Target)target;
_target = target;
}

NativeCodeVersionHandle ICodeVersions.GetNativeCodeVersionForIP(TargetCodePointer ip)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal class DacStreams_1 : IDacStreams
{
private readonly Target _target;
private readonly ITarget _target;

private const uint MiniMetadataSignature = 0x6d727473;
private const uint EENameStreamSignature = 0x614e4545;
Expand All @@ -28,7 +28,7 @@ internal class DacStreams_1 : IDacStreams

internal DacStreams_1(ITarget target)
{
_target = (Target)target;
_target = target;
}

public virtual string? StringFromEEAddress(TargetPointer address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal class EcmaMetadata_1 : IEcmaMetadata
{
private readonly Target target;
private readonly ITarget target;
private Dictionary<ModuleHandle, MetadataReaderProvider?> _metadata = new();

public EcmaMetadata_1(ITarget target)
{
this.target = (Target)target;
this.target = target;
}

public TargetSpan GetReadOnlyMetadataAddress(ModuleHandle handle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal readonly struct Exception_1 : IException
{
private readonly Target _target;
private readonly ITarget _target;

internal Exception_1(ITarget target)
{
_target = (Target)target;
_target = target;
}

TargetPointer IException.GetNestedExceptionInfo(TargetPointer exceptionInfoAddr, out TargetPointer nextNestedExceptionInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;
private class EEJitManager : JitManager
{
private readonly NibbleMap _nibbleMap;
public EEJitManager(Target target, NibbleMap nibbleMap) : base(target)
public EEJitManager(ITarget target, NibbleMap nibbleMap) : base(target)
{
_nibbleMap = nibbleMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;
internal sealed class NibbleMap
{

public static NibbleMap Create(Target target)
public static NibbleMap Create(ITarget target)
{
uint codeHeaderSize = (uint)target.PointerSize;
return new NibbleMap(target, codeHeaderSize);
}

private readonly Target _target;
private readonly ITarget _target;
private readonly uint _codeHeaderSize;
private NibbleMap(Target target, uint codeHeaderSize)
private NibbleMap(ITarget target, uint codeHeaderSize)
{
_target = target;
_codeHeaderSize = codeHeaderSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal readonly partial struct ExecutionManager_1 : IExecutionManager
{
internal readonly Target _target;
internal readonly ITarget _target;

// maps EECodeInfoHandle.Address (which is the CodeHeaderAddress) to the EECodeInfo
private readonly Dictionary<TargetPointer, EECodeInfo> _codeInfos = new();
Expand All @@ -20,7 +20,7 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

public ExecutionManager_1(ITarget target, Data.RangeSectionMap topRangeSectionMap)
{
_target = (Target)target;
_target = target;
_topRangeSectionMap = topRangeSectionMap;
_rangeSectionLookupAlgorithm = RangeSectionLookupAlgorithm.Create(_target);
NibbleMap nibbleMap = NibbleMap.Create(_target);
Expand Down Expand Up @@ -74,12 +74,12 @@ public ExMgrPtr Offset(int stride, int idx)
return new ExMgrPtr(RawValue.Value + (ulong)(stride * idx));
}

public T Load<T>(Target target) where T : Data.IData<T>
public T Load<T>(ITarget target) where T : Data.IData<T>
{
return target.ProcessedData.GetOrAdd<T>(Address);
}

public ExMgrPtr LoadPointer(Target target)
public ExMgrPtr LoadPointer(ITarget target)
{
return new ExMgrPtr(target.ReadPointer(Address));
}
Expand All @@ -97,7 +97,7 @@ private RangeSectionLookupAlgorithm(int mapLevels, int maxSetBit)
MapLevels = mapLevels;
MaxSetBit = maxSetBit;
}
public static RangeSectionLookupAlgorithm Create(Target target)
public static RangeSectionLookupAlgorithm Create(ITarget target)
{
if (target.PointerSize == 4)
{
Expand All @@ -123,7 +123,7 @@ private int EffectiveBitsForLevel(TargetCodePointer address, int level)
return checked((int)addressBitsUsedInLevel);
}

internal ExMgrPtr /*PTR_RangeSectionFragment*/ FindFragment(Target target, Data.RangeSectionMap topRangeSectionMap, TargetCodePointer jittedCodeAddress)
internal ExMgrPtr /*PTR_RangeSectionFragment*/ FindFragment(ITarget target, Data.RangeSectionMap topRangeSectionMap, TargetCodePointer jittedCodeAddress)
{
/* The outer levels are all pointer arrays to the next level down. Level 1 is an array of pointers to a RangeSectionFragment */
int topLevelIndex = EffectiveBitsForLevel(jittedCodeAddress, MapLevels);
Expand All @@ -150,9 +150,9 @@ private enum RangeSectionFlags : int
}
private abstract class JitManager
{
public Target Target { get; }
public ITarget Target { get; }

protected JitManager(Target target)
protected JitManager(ITarget target)
{
Target = target;
}
Expand All @@ -163,7 +163,7 @@ protected JitManager(Target target)

private class ReadyToRunJitManager : JitManager
{
public ReadyToRunJitManager(Target target) : base(target)
public ReadyToRunJitManager(ITarget target) : base(target)
{
}
public override bool GetMethodInfo(RangeSection rangeSection, TargetCodePointer jittedCodeAddress, [NotNullWhen(true)] out EECodeInfo? info)
Expand All @@ -189,13 +189,13 @@ public RangeSection(Data.RangeSection rangeSection)
internal bool IsRangeList => HasFlags(RangeSectionFlags.RangeList);
internal bool IsCodeHeap => HasFlags(RangeSectionFlags.CodeHeap);

internal static bool IsStubCodeBlock(Target target, TargetPointer codeHeaderIndirect)
internal static bool IsStubCodeBlock(ITarget target, TargetPointer codeHeaderIndirect)
{
uint stubCodeBlockLast = target.ReadGlobal<uint>(Constants.Globals.StubCodeBlockLast);
return codeHeaderIndirect.Value <= stubCodeBlockLast;
}

internal static RangeSection Find(Target target, Data.RangeSectionMap topRangeSectionMap, RangeSectionLookupAlgorithm rangeSectionLookup, TargetCodePointer jittedCodeAddress)
internal static RangeSection Find(ITarget target, Data.RangeSectionMap topRangeSectionMap, RangeSectionLookupAlgorithm rangeSectionLookup, TargetCodePointer jittedCodeAddress)
{
ExMgrPtr rangeSectionFragmentPtr = rangeSectionLookup.FindFragment(target, topRangeSectionMap, jittedCodeAddress);
if (rangeSectionFragmentPtr.IsNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal readonly struct Loader_1 : ILoader
{
private readonly Target _target;
private readonly ITarget _target;

internal Loader_1(ITarget target)
{
_target = (Target)target;
_target = target;
}

ModuleHandle ILoader.GetModuleHandle(TargetPointer modulePointer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal readonly struct Object_1 : IObject
{
private readonly Target _target;
private readonly ITarget _target;
private readonly ulong _methodTableOffset;
private readonly byte _objectToMethodTableUnmask;
private readonly TargetPointer _stringMethodTable;
Expand All @@ -32,7 +32,7 @@ public enum Bits

internal Object_1(ITarget target, ulong methodTableOffset, byte objectToMethodTableUnmask, TargetPointer stringMethodTable, TargetPointer syncTableEntries)
{
_target = (Target)target;
_target = target;
_methodTableOffset = methodTableOffset;
_stringMethodTable = stringMethodTable;
_objectToMethodTableUnmask = objectToMethodTableUnmask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal readonly struct PrecodeStubs_1 : IPrecodeStubs
{
private readonly Target _target;
private readonly ITarget _target;
internal readonly Data.PrecodeMachineDescriptor MachineDescriptor;

internal enum KnownPrecodeType
Expand All @@ -29,15 +29,15 @@ protected ValidPrecode(TargetPointer instrPointer, KnownPrecodeType precodeType)
PrecodeType = precodeType;
}

internal abstract TargetPointer GetMethodDesc(Target target, Data.PrecodeMachineDescriptor precodeMachineDescriptor);
internal abstract TargetPointer GetMethodDesc(ITarget target, Data.PrecodeMachineDescriptor precodeMachineDescriptor);

}

internal class StubPrecode : ValidPrecode
{
internal StubPrecode(TargetPointer instrPointer, KnownPrecodeType type = KnownPrecodeType.Stub) : base(instrPointer, type) { }

internal override TargetPointer GetMethodDesc(Target target, Data.PrecodeMachineDescriptor precodeMachineDescriptor)
internal override TargetPointer GetMethodDesc(ITarget target, Data.PrecodeMachineDescriptor precodeMachineDescriptor)
{
TargetPointer stubPrecodeDataAddress = InstrPointer + precodeMachineDescriptor.StubCodePageSize;
Data.StubPrecodeData stubPrecodeData = target.ProcessedData.GetOrAdd<Data.StubPrecodeData>(stubPrecodeDataAddress);
Expand All @@ -53,7 +53,7 @@ internal PInvokeImportPrecode(TargetPointer instrPointer) : base(instrPointer, K
internal sealed class FixupPrecode : ValidPrecode
{
internal FixupPrecode(TargetPointer instrPointer) : base(instrPointer, KnownPrecodeType.Fixup) { }
internal override TargetPointer GetMethodDesc(Target target, Data.PrecodeMachineDescriptor precodeMachineDescriptor)
internal override TargetPointer GetMethodDesc(ITarget target, Data.PrecodeMachineDescriptor precodeMachineDescriptor)
{
TargetPointer fixupPrecodeDataAddress = InstrPointer + precodeMachineDescriptor.StubCodePageSize;
Data.FixupPrecodeData fixupPrecodeData = target.ProcessedData.GetOrAdd<Data.FixupPrecodeData>(fixupPrecodeDataAddress);
Expand All @@ -66,7 +66,7 @@ internal sealed class ThisPtrRetBufPrecode : ValidPrecode // FIXME: is this a St
{
internal ThisPtrRetBufPrecode(TargetPointer instrPointer) : base(instrPointer, KnownPrecodeType.ThisPtrRetBuf) { }

internal override TargetPointer GetMethodDesc(Target target, Data.PrecodeMachineDescriptor precodeMachineDescriptor)
internal override TargetPointer GetMethodDesc(ITarget target, Data.PrecodeMachineDescriptor precodeMachineDescriptor)
{
throw new NotImplementedException(); // TODO(cdac)
}
Expand Down Expand Up @@ -159,7 +159,7 @@ internal ValidPrecode GetPrecodeFromEntryPoint(TargetCodePointer entryPoint)
}
public PrecodeStubs_1(ITarget target, Data.PrecodeMachineDescriptor precodeMachineDescriptor)
{
_target = (Target)target;
_target = target;
MachineDescriptor = precodeMachineDescriptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal readonly partial struct ReJIT_1 : IReJIT
{
internal readonly Target _target;
internal readonly ITarget _target;
private readonly Data.ProfControlBlock _profControlBlock;

public ReJIT_1(ITarget target, Data.ProfControlBlock profControlBlock)
{
_target = (Target)target;
_target = target;
_profControlBlock = profControlBlock;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Microsoft.Diagnostics.DataContractReader.Contracts;

internal sealed class Registry
internal sealed class Registry : IRegistry
{
// Contracts that have already been created for a target.
// Items should not be removed from this, only added.
Expand Down
Loading

0 comments on commit f2844ae

Please sign in to comment.