Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ public virtual TargetPointer AllocateMemory(uint size)
/// <summary>
/// Returns the information about the given well-known data type in the target process
/// </summary>
/// <param name="type">The name of the well known type</param>
/// <param name="typeName">The name of the well known type</param>
/// <returns>The information about the given type in the target process</returns>
public abstract TypeInfo GetTypeInfo(DataType type);
public abstract TypeInfo GetTypeInfo(string typeName);

/// <summary>
Comment thread
max-charlamb marked this conversation as resolved.
/// Get the data cache for the target
Expand Down Expand Up @@ -304,10 +304,6 @@ public readonly record struct FieldInfo
/// </summary>
public int Offset { get; init; }
/// <summary>
/// The well known data type of the field in the target process
/// </summary>
public readonly DataType Type { get; init; }
/// <summary>
/// The name of the well known data type of the field in the target process, or null
/// if the target data descriptor did not record a name
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,9 @@ public enum DataType
CardTableInfo,
RegionFreeList,
}

public static class DataTypeTargetExtensions
{
public static Target.TypeInfo GetTypeInfo(this Target target, DataType type)
=> target.GetTypeInfo(type.ToString());
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ private readonly struct Configuration
private readonly DataTargetDelegates _dataTargetDelegates;
private readonly Dictionary<string, string> _contracts = [];
private readonly IReadOnlyDictionary<string, GlobalValue> _globals = new Dictionary<string, GlobalValue>();
private readonly Dictionary<DataType, Target.TypeInfo> _knownTypes = [];
private readonly Dictionary<string, Target.TypeInfo> _types = [];

public override ContractRegistry Contracts { get; }
Expand Down Expand Up @@ -133,7 +132,7 @@ private ContractDescriptorTarget(Descriptor[] descriptors, DataTargetDelegates d
_contracts = [];

// Set pointer type size
_knownTypes[DataType.pointer] = new TypeInfo { Size = (uint)_config.PointerSize };
_types[DataType.pointer.ToString()] = new TypeInfo { Size = (uint)_config.PointerSize };

HashSet<string> seenTypeNames = new HashSet<string>();
HashSet<string> seenGlobalNames = new HashSet<string>();
Expand Down Expand Up @@ -170,7 +169,6 @@ private ContractDescriptorTarget(Descriptor[] descriptors, DataTargetDelegates d
fieldInfos[fieldName] = new Target.FieldInfo()
{
Offset = field.Offset,
Type = field.Type is null ? DataType.Unknown : GetDataType(field.Type),
Comment thread
rcj1 marked this conversation as resolved.
TypeName = field.Type
};
}
Expand All @@ -183,15 +181,7 @@ private ContractDescriptorTarget(Descriptor[] descriptors, DataTargetDelegates d
}
seenTypeNames.Add(name);

DataType dataType = GetDataType(name);
if (dataType is not DataType.Unknown)
{
_knownTypes[dataType] = typeInfo;
}
else
{
_types[name] = typeInfo;
}
_types[name] = typeInfo;
}
}

Expand Down Expand Up @@ -382,14 +372,6 @@ private static bool TryReadContractDescriptor(
return true;
}

private static DataType GetDataType(string type)
{
if (Enum.TryParse(type, false, out DataType dataType) && Enum.IsDefined(dataType))
return dataType;

return DataType.Unknown;
}

public override int PointerSize => _config.PointerSize;
public override bool IsLittleEndian => _config.IsLittleEndian;

Expand Down Expand Up @@ -583,7 +565,7 @@ public override TargetPointer ReadPointerFromSpan(ReadOnlySpan<byte> bytes)

public override TargetCodePointer ReadCodePointer(ulong address)
{
TypeInfo codePointerTypeInfo = GetTypeInfo(DataType.CodePointer);
TypeInfo codePointerTypeInfo = this.GetTypeInfo(DataType.CodePointer);
if (codePointerTypeInfo.Size is sizeof(uint))
{
return new TargetCodePointer(Read<uint>(address));
Expand All @@ -597,7 +579,7 @@ public override TargetCodePointer ReadCodePointer(ulong address)

public override bool TryReadCodePointer(ulong address, out TargetCodePointer value)
{
TypeInfo codePointerTypeInfo = GetTypeInfo(DataType.CodePointer);
TypeInfo codePointerTypeInfo = this.GetTypeInfo(DataType.CodePointer);
if (codePointerTypeInfo.Size is sizeof(uint))
{
if (TryRead<uint>(address, out uint val))
Expand Down Expand Up @@ -821,23 +803,11 @@ public bool TryReadStringGlobal(string name, [NotNullWhen(true)] out string? val

#endregion

public override TypeInfo GetTypeInfo(DataType type)
{
if (!_knownTypes.TryGetValue(type, out Target.TypeInfo typeInfo))
throw new InvalidOperationException($"Failed to get type info for '{type}'");

return typeInfo;
}

public Target.TypeInfo GetTypeInfo(string type)
public override Target.TypeInfo GetTypeInfo(string type)
{
if (_types.TryGetValue(type, out Target.TypeInfo typeInfo))
return typeInfo;

DataType dataType = GetDataType(type);
if (dataType is not DataType.Unknown)
return GetTypeInfo(dataType);

throw new InvalidOperationException($"Failed to get type info for '{type}'");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public unsafe partial class TargetTests
{
Size = 56,
Fields = new Dictionary<string, Target.FieldInfo> {
{ "Field1", new(){ Offset = 8, Type = DataType.uint16, TypeName = DataType.uint16.ToString() }},
{ "Field2", new(){ Offset = 16, Type = DataType.ObjectHandle, TypeName = DataType.ObjectHandle.ToString() }},
{ "Field1", new(){ Offset = 8, TypeName = DataType.uint16.ToString() }},
{ "Field2", new(){ Offset = 16, TypeName = DataType.ObjectHandle.ToString() }},
{ "Field3", new(){ Offset = 32 }}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public unsafe partial class TargetTests
{
Size = 56,
Fields = new Dictionary<string, Target.FieldInfo> {
{ "Field1", new(){ Offset = 8, Type = DataType.uint16, TypeName = DataType.uint16.ToString() }},
{ "Field2", new(){ Offset = 16, Type = DataType.ObjectHandle, TypeName = DataType.ObjectHandle.ToString() }},
{ "Field1", new(){ Offset = 8, TypeName = DataType.uint16.ToString() }},
{ "Field2", new(){ Offset = 16, TypeName = DataType.ObjectHandle.ToString() }},
{ "Field3", new(){ Offset = 32 }}
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/native/managed/cdac/tests/DebuggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static TestPlaceholderTarget BuildTarget(

TargetTestHelpers.LayoutResult debuggerLayout = GetDebuggerLayout(helpers);
TargetTestHelpers.LayoutResult debuggerRcThreadLayout = GetDebuggerRCThreadLayout(helpers);
builder.AddTypes(new()
builder.AddTypes(new Dictionary<DataType, Target.TypeInfo>
{
[DataType.Debugger] = new Target.TypeInfo() { Fields = debuggerLayout.Fields, Size = debuggerLayout.Stride },
[DataType.DebuggerRCThread] = new Target.TypeInfo() { Fields = debuggerRcThreadLayout.Fields, Size = debuggerRcThreadLayout.Stride },
Expand Down
13 changes: 6 additions & 7 deletions src/native/managed/cdac/tests/ExecutionManager/HashMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ private static Target CreateTarget(
MockTarget.Architecture arch,
Action<MockHashMapBuilder> configure)
{
MockMemorySpace.Builder builder = new(new TargetTestHelpers(arch));
MockHashMapBuilder hashMap = new(builder);
TestPlaceholderTarget.Builder builder = new(arch);
MockHashMapBuilder hashMap = new(builder.MemoryBuilder);
configure(hashMap);

return new TestPlaceholderTarget(
builder.TargetTestHelpers.Arch,
builder.GetMemoryContext().ReadFromTarget,
CreateContractTypes(hashMap),
CreateContractGlobals(hashMap));
return builder
.AddTypes(CreateContractTypes(hashMap))
.AddGlobals(CreateContractGlobals(hashMap))
.Build();
}

[Theory]
Expand Down
2 changes: 1 addition & 1 deletion src/native/managed/cdac/tests/GCMemoryRegionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static (string Name, ulong Value)[] BuildGlobals(
{
var result = new Dictionary<string, Target.FieldInfo>();
foreach (var (name, offset, type) in fields)
result[name] = new Target.FieldInfo { Offset = offset, Type = type };
result[name] = new Target.FieldInfo { Offset = offset, TypeName = type.ToString() };
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/native/managed/cdac/tests/MethodDescTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class MethodDescTests
Size = methodDescBuilder.AsyncMethodDataSize,
Fields = new Dictionary<string, Target.FieldInfo>
{
[nameof(Data.AsyncMethodData.Flags)] = new Target.FieldInfo { Offset = 0, Type = DataType.Unknown },
[nameof(Data.AsyncMethodData.Flags)] = new Target.FieldInfo { Offset = 0 },
},
},
[DataType.ArrayMethodDesc] = new Target.TypeInfo { Size = methodDescBuilder.ArrayMethodDescSize },
Expand Down
14 changes: 1 addition & 13 deletions src/native/managed/cdac/tests/TargetTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,6 @@ internal int SizeOfPrimitive(DataType type)
};
}

internal int SizeOfTypeInfo(Target.TypeInfo info)
{
int size = 0;
foreach (var (_, field) in info.Fields)
{
size = Math.Max(size, field.Offset + SizeOfPrimitive(field.Type));
}

return size;
}

#endregion Mock memory initialization

private static int AlignUp(int offset, int align)
Expand Down Expand Up @@ -215,7 +204,7 @@ private LayoutResult LayoutFieldsWorker(FieldLayout style, Field[] fields, ref i
};
fieldInfos[name] = new Target.FieldInfo {
Offset = offset,
Type = type,
TypeName = type.ToString(),
};
offset += size;
}
Expand Down Expand Up @@ -245,7 +234,6 @@ internal static Target.TypeInfo CreateTypeInfo(Layout layout)
fields[field.Name] = new Target.FieldInfo
{
Offset = field.Offset,
Type = DataType.Unknown,
};
}

Expand Down
17 changes: 12 additions & 5 deletions src/native/managed/cdac/tests/TestPlaceholderTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class TestPlaceholderTarget : Target
{
private ContractRegistry _contractRegistry;
private readonly Target.IDataCache _dataCache;
private readonly Dictionary<DataType, Target.TypeInfo> _typeInfoCache;
private readonly Dictionary<string, Target.TypeInfo> _typeInfoCache;
private readonly (string Name, ulong Value)[] _globals;
private readonly (string Name, string Value)[] _globalStrings;

Expand All @@ -33,7 +33,7 @@ internal class TestPlaceholderTarget : Target
private static readonly UTF8Encoding strictUTF8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
private static readonly UTF8Encoding looseUTF8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: false);

public TestPlaceholderTarget(MockTarget.Architecture arch, ReadFromTargetDelegate reader, Dictionary<DataType, Target.TypeInfo> types = null, (string Name, ulong Value)[] globals = null, (string Name, string Value)[] globalStrings = null, WriteToTargetDelegate? writer = null, AllocateMemoryDelegate? allocateMemory = null)
public TestPlaceholderTarget(MockTarget.Architecture arch, ReadFromTargetDelegate reader, Dictionary<string, Target.TypeInfo> types = null, (string Name, ulong Value)[] globals = null, (string Name, string Value)[] globalStrings = null, WriteToTargetDelegate? writer = null, AllocateMemoryDelegate? allocateMemory = null)
{
IsLittleEndian = arch.IsLittleEndian;
PointerSize = arch.Is64Bit ? 8 : 4;
Expand Down Expand Up @@ -77,7 +77,7 @@ internal class Builder
{
private readonly MockTarget.Architecture _arch;
private readonly MockMemorySpace.Builder _memBuilder;
private readonly Dictionary<DataType, Target.TypeInfo> _types = new();
private readonly Dictionary<string, Target.TypeInfo> _types = new();
private readonly List<(string Name, ulong Value)> _globals = new();
private readonly List<(string Name, string Value)> _globalStrings = new();
private readonly List<Action<TestContractRegistry>> _contractSetups = new();
Expand All @@ -94,6 +94,13 @@ public Builder(MockTarget.Architecture arch)
internal MockMemorySpace.Builder MemoryBuilder => _memBuilder;

public Builder AddTypes(Dictionary<DataType, Target.TypeInfo> types)
{
foreach (var kvp in types)
_types[kvp.Key.ToString()] = kvp.Value;
return this;
}

public Builder AddTypes(Dictionary<string, Target.TypeInfo> types)
{
foreach (var kvp in types)
_types[kvp.Key] = kvp.Value;
Expand Down Expand Up @@ -499,9 +506,9 @@ protected TargetCodePointer DefaultReadCodePointer(ulong address)

public override TargetPointer ReadPointerFromSpan(ReadOnlySpan<byte> bytes) => throw new NotImplementedException();

public override Target.TypeInfo GetTypeInfo(DataType dataType)
public override Target.TypeInfo GetTypeInfo(string typeName)
{
if (_typeInfoCache.TryGetValue(dataType, out var info))
if (_typeInfoCache.TryGetValue(typeName, out var info))
return info;

throw new NotImplementedException();
Expand Down
Loading