Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix bugs found when trying to actually use this logic
1. It works now!
  • Loading branch information
davidwrighton committed Jul 11, 2024
commit d359ed9d05bd4066ae88b5c95b429f69ef0599f7
2 changes: 1 addition & 1 deletion src/coreclr/vm/ceeload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ Module::Module(Assembly *pAssembly, PEAssembly *pPEAssembly)
m_pAssembly = pAssembly;
m_pPEAssembly = pPEAssembly;
m_dwTransientFlags = CLASSES_FREED;
m_pDynamicMetadata = NULL;

pPEAssembly->AddRef();
}
Expand Down Expand Up @@ -3748,7 +3749,6 @@ ReflectionModule::ReflectionModule(Assembly *pAssembly, PEAssembly *pPEAssembly)
m_pInMemoryWriter = NULL;
m_sdataSection = NULL;
m_pCeeFileGen = NULL;
m_pDynamicMetadata = NULL;
}

HRESULT STDMETHODCALLTYPE CreateICeeGen(REFIID riid, void **pCeeGen);
Expand Down
2 changes: 1 addition & 1 deletion src/native/managed/cdacreader/src/Contracts/Loader_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ AvailableMetadataType ILoader.GetAvailableMetadataType(ModuleHandle handle)
TargetPointer ILoader.GetReadWriteSavedMetadataAddress(ModuleHandle handle, out ulong size)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
TargetPointer result = module.DynamicMetadata + (ulong)_target.GetTypeInfo(DataType.pointer)!.Size!.Value;
TargetPointer result = module.DynamicMetadata + (ulong)_target.PointerSize;
size = _target.Read<uint>(module.DynamicMetadata);
return result;
}
Expand Down
13 changes: 3 additions & 10 deletions src/native/managed/cdacreader/src/Contracts/RuntimeTypeSystem_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ public ReadOnlySpan<MethodTableHandle> GetInstantiation(MethodTableHandle method
return default;

TargetPointer perInstInfo = methodTable.PerInstInfo;
var typeInfo = _target.GetTypeInfo(DataType.pointer);
uint? size = typeInfo.Size;
TargetPointer genericsDictInfo = _target.ReadPointer(perInstInfo - size!.Value);
TargetPointer genericsDictInfo = perInstInfo - (ulong)_target.PointerSize;

TargetPointer dictionaryPointer = _target.ReadPointer(perInstInfo);

Expand All @@ -179,12 +177,7 @@ public ReadOnlySpan<MethodTableHandle> GetInstantiation(MethodTableHandle method

public bool IsGenericTypeDefinition(MethodTableHandle methodTableHandle) => _methodTables[methodTableHandle.Address].Flags.IsGenericTypeDefinition;

TypeHandle IRuntimeTypeSystem.TypeHandleFromAddress(TargetPointer address)
{
return TypeHandleFromAddress(address);
}

private static TypeHandle TypeHandleFromAddress(TargetPointer address)
public TypeHandle TypeHandleFromAddress(TargetPointer address)
{
if (address == 0)
return default;
Expand All @@ -195,7 +188,7 @@ private static TypeHandle TypeHandleFromAddress(TargetPointer address)
}
else
{
return new TypeHandle(new MethodTableHandle(address));
return new TypeHandle(GetMethodTableHandle(address));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/native/managed/cdacreader/src/Data/MethodTableArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public MethodTableArray(Target target, (TargetPointer ptr, int size) key)

for (int i = 0; i < key.size; i++)
{
instantiationSpan[i] = new MethodTableHandle(targetPointerSpan[i]);
instantiationSpan[i] = target.Contracts.RuntimeTypeSystem.GetMethodTableHandle(targetPointerSpan[i]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ private int RowCount(MetadataTable table)

private bool TryReadTableEntry(ReadOnlySpan<byte> bytes, MetadataColumnIndex column, out uint value)
{
if (ColumnOffset(column) == 0)
throw new ArgumentOutOfRangeException(nameof(column));

int size = ColumnSize(column);
ReadOnlySpan<byte> singleColumn = bytes.Slice(ColumnOffset(column), size);
if (size == 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ private static ColumnType[] GetColumnTypes()

columnTypes[(int)MetadataColumnIndex.ImplMap_MappingFlags] = ColumnType.TwoByteConstant;
columnTypes[(int)MetadataColumnIndex.ImplMap_MemberForwarded] = ColumnType.Token;
columnTypes[(int)MetadataColumnIndex.ImplMap_ImportName] = ColumnType.TwoByteConstant;
columnTypes[(int)MetadataColumnIndex.ImplMap_ImportName] = ColumnType.Utf8String;
columnTypes[(int)MetadataColumnIndex.ImplMap_ImportScope] = ColumnType.Token;

columnTypes[(int)MetadataColumnIndex.FieldRva_Rva] = ColumnType.TwoByteConstant;
columnTypes[(int)MetadataColumnIndex.FieldRva_Rva] = ColumnType.FourByteConstant;
columnTypes[(int)MetadataColumnIndex.FieldRva_Field] = ColumnType.Token;

columnTypes[(int)MetadataColumnIndex.ENCLog_Token] = ColumnType.FourByteConstant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ private static bool IsSigned<T>() where T : struct, INumberBase<T>, IMinMaxValue
}
private static bool TryReadCore<T>(ReadOnlySpan<byte> bytes, out T value) where T : struct, IBinaryInteger<T>, IMinMaxValue<T>
{
return T.TryReadLittleEndian(bytes, IsSigned<T>(), out value);
return T.TryReadLittleEndian(bytes.Slice(0, Unsafe.SizeOf<T>()), IsSigned<T>(), out value);
}

private static T ReadLittleEndian<T>(ReadOnlySpan<byte> bytes) where T : struct, IBinaryInteger<T>, IMinMaxValue<T>
{
if (!T.TryReadLittleEndian(bytes, IsSigned<T>(), out T value))
if (!TryReadCore<T>(bytes, out T value))
throw new ArgumentOutOfRangeException(nameof(value));
return value;
}
Expand Down
21 changes: 12 additions & 9 deletions src/native/managed/cdacreader/src/Helpers/EcmaMetadataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ReadOnlySpan<byte> Row
{
get
{
return TableData.Span.Slice((int)(RowSize * Rid), (int)RowSize);
return TableData.Span.Slice((int)(RowSize * (Rid - 1)), (int)RowSize);
}
}
}
Expand All @@ -44,7 +44,7 @@ public EcmaMetadataReader(ReadOnlyMemory<byte> imageMemory)
{
columnSize = new int[(int)MetadataColumnIndex.Count];
columnOffset = new int[(int)MetadataColumnIndex.Count];
rowSize = new int[(int)MetadataTable.Count + 1];
rowSize = new int[(int)MetadataTable.Count];
columnTokenDecode = Array.Empty<Func<uint, uint>>();

ReadOnlySpan<byte> image = imageMemory.Span;
Expand All @@ -64,7 +64,7 @@ public EcmaMetadataReader(ReadOnlyMemory<byte> imageMemory)
throw new ArgumentException(nameof(imageMemory));
}

string metadataVersion = Encoding.UTF8.GetString(versionName.Slice(nullTerminatorIndex));
string metadataVersion = Encoding.UTF8.GetString(versionName.Slice(0, nullTerminatorIndex));

int currentOffset = 16 + versionSize;

Expand Down Expand Up @@ -120,7 +120,7 @@ public EcmaMetadataReader(ReadOnlyMemory<byte> imageMemory)
int[] tableRowCounts = new int[(int)MetadataTable.Count];
bool[] isSorted = new bool[(int)MetadataTable.Count];
int currentTablesOffset = 24;
for (int i = 0; i < tables.Length; i++)
for (int i = 0; i < (int)MetadataTable.Count; i++)
{
if ((validTables & ((ulong)1 << i)) != 0)
{
Expand Down Expand Up @@ -156,15 +156,15 @@ public EcmaMetadataReader(ReadOnlyMemory<byte> imageMemory)

// Init will compute row sizes, which is necessary for actually computing the tableData

for (int i = 0; i < tables.Length; i++)
for (int i = 0; i < (int)MetadataTable.Count; i++)
{
checked
{
if ((validTables & ((ulong)1 << i)) != 0)
{
int tableSize = checked(rowSize![i] * _ecmaMetadata.Schema.RowCount[i]);
tableData[i] = TablesHeap.Slice(currentTablesOffset, tableSize);
currentTablesOffset += AlignUp(tableSize, 4);
currentTablesOffset += tableSize;
}
}
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public EcmaMetadataReader(EcmaMetadata ecmaMetadata)
columnTokenDecode = Array.Empty<Func<uint, uint>>();
columnSize = new int[(int)MetadataColumnIndex.Count];
columnOffset = new int[(int)MetadataColumnIndex.Count];
rowSize = new int[(int)MetadataTable.Count + 1];
rowSize = new int[(int)MetadataTable.Count];

_ecmaMetadata = ecmaMetadata;
Init();
Expand Down Expand Up @@ -232,10 +232,12 @@ void ComputeColumnSizesAndOffsets()
for (int i = 0; i < (int)MetadataColumnIndex.Count; i++)
{
MetadataColumnIndex column = (MetadataColumnIndex)i;
if (currentTable != ColumnTable(column))
MetadataTable newColumnTable = ColumnTable(column);
if (currentTable != newColumnTable)
{
if (prevColumn.HasValue)
rowSize[(int)ColumnTable(prevColumn.Value)] = ComputeColumnEnd(prevColumn.Value);
rowSize[(int)currentTable] = ComputeColumnEnd(prevColumn.Value);
currentTable = newColumnTable;
columnOffset[i] = 0;
}
else
Expand Down Expand Up @@ -359,6 +361,7 @@ public bool TryFindRowFromCursor(EcmaMetadataCursor tableCursor, MetadataColumnI
{
return true;
}
foundRow.Rid += 1;
}
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/native/managed/cdacreader/src/Helpers/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal enum MetadataTable
GenericParam = 0x2a,
MethodSpec = 0x2b,
GenericParamConstraint = 0x2c,
Count = 0x2c
Count = 0x2d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Count = 0x2d
Count

}

internal class EcmaMetadata
Expand Down
6 changes: 6 additions & 0 deletions src/native/managed/cdacreader/src/Legacy/TypeNameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ private void OpenGenericArgument()
{
TypeString.Append(',');
}
else
{
FirstInstArg = false;
}

TypeString.Append(UseAngleBracketsForGenerics ? '<' : '[');
PushOpenGenericArgument();
Expand Down Expand Up @@ -319,6 +323,8 @@ private void AddAssemblySpec(string? assemblySpec)
State = ParseState.AssemSpec;
if (!string.IsNullOrEmpty(assemblySpec))
{
TypeString.Append(", ");

if (InstNesting > 0)
EscapeEmbeddedAssemblyName(assemblySpec);
else
Expand Down
28 changes: 9 additions & 19 deletions src/native/managed/cdacreader/src/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ private static DataType GetDataType(string type)
return DataType.Unknown;
}

public int PointerSize => _config.PointerSize;

public T Read<T>(ulong address) where T : unmanaged, IBinaryInteger<T>, IMinMaxValue<T>
{
if (!TryRead(address, _config.IsLittleEndian, _reader, out T value))
Expand Down Expand Up @@ -447,10 +449,10 @@ public TValue GetOrAdd<TKey, TValue>(TKey key) where TValue : IData<TValue, TKey
if (TryGet(key, out TValue? result))
return result!;

Dictionary<(TKey, Type), object?> dictionary = (Dictionary<(TKey, Type), object?>)_customKeyReadDataByAddress[typeof(TKey)];
Dictionary<TKey, TValue> dictionary = (Dictionary<TKey, TValue>)_customKeyReadDataByAddress[typeof(Tuple<TKey, TValue>)];

TValue constructed = TValue.Create(_target, key);
if (dictionary.TryAdd((key, typeof(TValue)), constructed))
if (dictionary.TryAdd(key, constructed))
return constructed;

bool found = TryGet(key, out result);
Expand All @@ -475,25 +477,13 @@ public bool TryGet<T>(ulong address, [NotNullWhen(true)] out T? data)

public bool TryGet<TKey, TValue>(TKey key, [NotNullWhen(true)] out TValue? data) where TValue : IData<TValue, TKey> where TKey : notnull, IEquatable<TKey>
{
data = default;

if (!_customKeyReadDataByAddress.TryGetValue(typeof(TKey), out var dictionaryObject))
{
dictionaryObject = new Dictionary<(TKey, Type), TValue>();
_customKeyReadDataByAddress.Add(typeof(TKey), dictionaryObject);
}
Dictionary<(TKey, Type), object?> dictionary = (Dictionary<(TKey, Type), object?>)dictionaryObject;

if (!dictionary.TryGetValue((key, typeof(TValue)), out object? dataObj))
return false;

if (dataObj is TValue dataMaybe)
if (!_customKeyReadDataByAddress.TryGetValue(typeof(Tuple<TKey, TValue>), out var dictionaryObject))
{
data = dataMaybe;
return true;
dictionaryObject = new Dictionary<TKey, TValue>();
_customKeyReadDataByAddress.Add(typeof(Tuple<TKey, TValue>), dictionaryObject);
}

return false;
Dictionary<TKey, TValue> dictionary = (Dictionary<TKey, TValue>)dictionaryObject;
return dictionary.TryGetValue(key, out data);
}
}

Expand Down