-
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.
[cdac] Start Loader contract and implement ISOSDacInterface::GetModul…
…eData in cDAC (#104257) - Start a `Loader` contract - currently contains what is needed for GetModuleData - Implement `ISOSDacInterface::GetModuleData` in cDAC - Store base address and is reflection emit bit on `Module` for easier diagnostics access
1 parent
eae1542
commit 04a40c1
Showing
16 changed files
with
523 additions
and
15 deletions.
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
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,107 @@ | ||
# Contract Loader | ||
|
||
This contract is for getting information about loaded modules and assemblies | ||
|
||
## APIs of contract | ||
|
||
``` csharp | ||
readonly struct ModuleHandle | ||
{ | ||
// Opaque handle - no public members | ||
internal TargetPointer Address; | ||
} | ||
|
||
[Flags] | ||
enum ModuleFlags | ||
{ | ||
EditAndContinue = 0x00000008, // Edit and Continue is enabled for this module | ||
ReflectionEmit = 0x00000040, // Reflection.Emit was used to create this module | ||
} | ||
|
||
record struct ModuleLookupTables( | ||
TargetPointer FieldDefToDesc, | ||
TargetPointer ManifestModuleReferences, | ||
TargetPointer MemberRefToDesc, | ||
TargetPointer MethodDefToDesc, | ||
TargetPointer TypeDefToMethodTable, | ||
TargetPointer TypeRefToMethodTable); | ||
``` | ||
|
||
``` csharp | ||
ModuleHandle GetModuleHandle(TargetPointer); | ||
TargetPointer GetAssembly(ModuleHandle handle); | ||
ModuleFlags GetFlags(ModuleHandle handle); | ||
TargetPointer GetLoaderAllocator(ModuleHandle handle); | ||
TargetPointer GetThunkHeap(ModuleHandle handle); | ||
TargetPointer GetILBase(ModuleHandle handle); | ||
TargetPointer GetMetadataAddress(ModuleHandle handle, out ulong size); | ||
ModuleLookupTables GetLookupTables(ModuleHandle handle); | ||
``` | ||
|
||
## Version 1 | ||
|
||
Data descriptors used: | ||
- `Module` | ||
|
||
``` csharp | ||
ModuleHandle GetModuleHandle(TargetPointer modulePointer) | ||
{ | ||
return new ModuleHandle(modulePointer); | ||
} | ||
|
||
TargetPointer GetAssembly(ModuleHandle handle) | ||
{ | ||
return target.ReadPointer(handle.Address + /* Module::Assrembly offset */); | ||
} | ||
|
||
ModuleFlags GetFlags(ModuleHandle handle) | ||
{ | ||
return target.Read<uint>(handle.Address + /* Module::Flags offset */); | ||
} | ||
|
||
TargetPointer GetLoaderAllocator(ModuleHandle handle) | ||
{ | ||
return target.ReadPointer(handle.Address + /* Module::LoaderAllocator offset */); | ||
} | ||
|
||
TargetPointer GetThunkHeap(ModuleHandle handle) | ||
{ | ||
return target.ReadPointer(handle.Address + /* Module::ThunkHeap offset */); | ||
} | ||
|
||
TargetPointer GetILBase(ModuleHandle handle) | ||
{ | ||
return target.ReadPointer(handle.Address + /* Module::Base offset */); | ||
} | ||
|
||
TargetPointer GetMetadataAddress(ModuleHandle handle, out ulong size) | ||
{ | ||
TargetPointer baseAddress = GetILBase(handle); | ||
if (baseAddress == TargetPointer.Null) | ||
{ | ||
size = 0; | ||
return TargetPointer.Null; | ||
} | ||
|
||
// Read CLR header per https://learn.microsoft.com/windows/win32/debug/pe-format | ||
ulong clrHeaderRVA = ... | ||
|
||
// Read Metadata per ECMA-335 II.25.3.3 CLI Header | ||
ulong metadataDirectoryAddress = baseAddress + clrHeaderRva + /* offset to Metadata */ | ||
int rva = target.Read<int>(metadataDirectoryAddress); | ||
size = target.Read<int>(metadataDirectoryAddress + sizeof(int)); | ||
return baseAddress + rva; | ||
} | ||
|
||
ModuleLookupTables GetLookupTables(ModuleHandle handle) | ||
{ | ||
return new ModuleLookupTables( | ||
FieldDefToDescMap: target.ReadPointer(handle.Address + /* Module::FieldDefToDescMap */), | ||
ManifestModuleReferencesMap: target.ReadPointer(handle.Address + /* Module::ManifestModuleReferencesMap */), | ||
MemberRefToDescMap: target.ReadPointer(handle.Address + /* Module::MemberRefToDescMap */), | ||
MethodDefToDescMap: target.ReadPointer(handle.Address + /* Module::MethodDefToDescMap */), | ||
TypeDefToMethodTableMap: target.ReadPointer(handle.Address + /* Module::TypeDefToMethodTableMap */), | ||
TypeRefToMethodTableMap: target.ReadPointer(handle.Address + /* Module::TypeRefToMethodTableMap */)); | ||
} | ||
``` |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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 readonly struct ModuleHandle | ||
{ | ||
internal ModuleHandle(TargetPointer address) | ||
{ | ||
Address = address; | ||
} | ||
|
||
internal TargetPointer Address { get; } | ||
} | ||
|
||
[Flags] | ||
internal enum ModuleFlags | ||
{ | ||
EditAndContinue = 0x00000008, // Edit and Continue is enabled for this module | ||
ReflectionEmit = 0x00000040, // Reflection.Emit was used to create this module | ||
} | ||
|
||
internal record struct ModuleLookupTables( | ||
TargetPointer FieldDefToDesc, | ||
TargetPointer ManifestModuleReferences, | ||
TargetPointer MemberRefToDesc, | ||
TargetPointer MethodDefToDesc, | ||
TargetPointer TypeDefToMethodTable, | ||
TargetPointer TypeRefToMethodTable); | ||
|
||
internal interface ILoader : IContract | ||
{ | ||
static string IContract.Name => nameof(Loader); | ||
static IContract IContract.Create(Target target, int version) | ||
{ | ||
return version switch | ||
{ | ||
1 => new Loader_1(target), | ||
_ => default(Loader), | ||
}; | ||
} | ||
|
||
public virtual ModuleHandle GetModuleHandle(TargetPointer modulePointer) => throw new NotImplementedException(); | ||
|
||
public virtual TargetPointer GetAssembly(ModuleHandle handle) => throw new NotImplementedException(); | ||
public virtual ModuleFlags GetFlags(ModuleHandle handle) => throw new NotImplementedException(); | ||
public virtual TargetPointer GetLoaderAllocator(ModuleHandle handle) => throw new NotImplementedException(); | ||
public virtual TargetPointer GetThunkHeap(ModuleHandle handle) => throw new NotImplementedException(); | ||
|
||
public virtual TargetPointer GetILBase(ModuleHandle handle) => throw new NotImplementedException(); | ||
public virtual TargetPointer GetMetadataAddress(ModuleHandle handle, out ulong size) => throw new NotImplementedException(); | ||
|
||
public virtual ModuleLookupTables GetLookupTables(ModuleHandle handle) => throw new NotImplementedException(); | ||
} | ||
|
||
internal readonly struct Loader : ILoader | ||
{ | ||
// Everything throws NotImplementedException | ||
} |
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,73 @@ | ||
// 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 readonly struct Loader_1 : ILoader | ||
{ | ||
private readonly Target _target; | ||
|
||
internal Loader_1(Target target) | ||
{ | ||
_target = target; | ||
} | ||
|
||
ModuleHandle ILoader.GetModuleHandle(TargetPointer modulePointer) | ||
{ | ||
if (modulePointer == TargetPointer.Null) | ||
throw new ArgumentNullException(nameof(modulePointer)); | ||
|
||
return new ModuleHandle(modulePointer); | ||
} | ||
|
||
TargetPointer ILoader.GetAssembly(ModuleHandle handle) | ||
{ | ||
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address); | ||
return module.Assembly; | ||
} | ||
|
||
ModuleFlags ILoader.GetFlags(ModuleHandle handle) | ||
{ | ||
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address); | ||
return (ModuleFlags)module.Flags; | ||
} | ||
|
||
TargetPointer ILoader.GetLoaderAllocator(ModuleHandle handle) | ||
{ | ||
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address); | ||
return module.LoaderAllocator; | ||
} | ||
|
||
TargetPointer ILoader.GetThunkHeap(ModuleHandle handle) | ||
{ | ||
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address); | ||
return module.ThunkHeap; | ||
} | ||
|
||
TargetPointer ILoader.GetILBase(ModuleHandle handle) | ||
{ | ||
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address); | ||
return module.Base; | ||
} | ||
|
||
TargetPointer ILoader.GetMetadataAddress(ModuleHandle handle, out ulong size) | ||
{ | ||
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address); | ||
return module.GetLoadedMetadata(out size); | ||
} | ||
|
||
ModuleLookupTables ILoader.GetLookupTables(ModuleHandle handle) | ||
{ | ||
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address); | ||
return new ModuleLookupTables( | ||
module.FieldDefToDescMap, | ||
module.ManifestModuleReferencesMap, | ||
module.MemberRefToDescMap, | ||
module.MethodDefToDescMap, | ||
module.TypeDefToMethodTableMap, | ||
module.TypeRefToMethodTableMap); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Reflection.PortableExecutable; | ||
|
||
namespace Microsoft.Diagnostics.DataContractReader.Data; | ||
|
||
internal sealed class Module : IData<Module> | ||
{ | ||
static Module IData<Module>.Create(Target target, TargetPointer address) | ||
=> new Module(target, address); | ||
|
||
private readonly Target _target; | ||
|
||
public Module(Target target, TargetPointer address) | ||
{ | ||
_target = target; | ||
Target.TypeInfo type = target.GetTypeInfo(DataType.Module); | ||
|
||
Flags = target.Read<uint>(address + (ulong)type.Fields[nameof(Flags)].Offset); | ||
Assembly = target.ReadPointer(address + (ulong)type.Fields[nameof(Assembly)].Offset); | ||
Base = target.ReadPointer(address + (ulong)type.Fields[nameof(Base)].Offset); | ||
LoaderAllocator = target.ReadPointer(address + (ulong)type.Fields[nameof(LoaderAllocator)].Offset); | ||
ThunkHeap = target.ReadPointer(address + (ulong)type.Fields[nameof(ThunkHeap)].Offset); | ||
|
||
FieldDefToDescMap = target.ReadPointer(address + (ulong)type.Fields[nameof(FieldDefToDescMap)].Offset); | ||
ManifestModuleReferencesMap = target.ReadPointer(address + (ulong)type.Fields[nameof(ManifestModuleReferencesMap)].Offset); | ||
MemberRefToDescMap = target.ReadPointer(address + (ulong)type.Fields[nameof(MemberRefToDescMap)].Offset); | ||
MethodDefToDescMap = target.ReadPointer(address + (ulong)type.Fields[nameof(MethodDefToDescMap)].Offset); | ||
TypeDefToMethodTableMap = target.ReadPointer(address + (ulong)type.Fields[nameof(TypeDefToMethodTableMap)].Offset); | ||
TypeRefToMethodTableMap = target.ReadPointer(address + (ulong)type.Fields[nameof(TypeRefToMethodTableMap)].Offset); | ||
} | ||
|
||
public TargetPointer Assembly { get; init; } | ||
public uint Flags { get; init; } | ||
public TargetPointer Base { get; init; } | ||
public TargetPointer LoaderAllocator { get; init; } | ||
public TargetPointer ThunkHeap { get; init; } | ||
|
||
public TargetPointer FieldDefToDescMap { get; init; } | ||
public TargetPointer ManifestModuleReferencesMap { get; init; } | ||
public TargetPointer MemberRefToDescMap { get; init; } | ||
public TargetPointer MethodDefToDescMap { get; init; } | ||
public TargetPointer TypeDefToMethodTableMap { get; init; } | ||
public TargetPointer TypeRefToMethodTableMap { get; init; } | ||
|
||
private TargetPointer _metadataStart = TargetPointer.Null; | ||
private ulong _metadataSize; | ||
public TargetPointer GetLoadedMetadata(out ulong size) | ||
{ | ||
if (Base != TargetPointer.Null && _metadataStart == TargetPointer.Null && _metadataSize == 0) | ||
{ | ||
int peSignatureOffset = _target.Read<int>(Base + PEFormat.DosStub.PESignatureOffset); | ||
ulong headerOffset = Base + (ulong)peSignatureOffset; | ||
ushort magic = _target.Read<ushort>(headerOffset + PEFormat.PEHeader.MagicOffset); | ||
ulong clrHeaderOffset = magic == (ushort)PEMagic.PE32 | ||
? PEFormat.PEHeader.CLRRuntimeHeader32Offset | ||
: PEFormat.PEHeader.CLRRuntimeHeader32PlusOffset; | ||
int corHeaderRva = _target.Read<int>(headerOffset + clrHeaderOffset); | ||
|
||
// Read RVA and size of the metadata | ||
ulong metadataDirectoryAddress = Base + (ulong)corHeaderRva + PEFormat.CorHeader.MetadataOffset; | ||
_metadataStart = Base + (ulong)_target.Read<int>(metadataDirectoryAddress); | ||
_metadataSize = (ulong)_target.Read<int>(metadataDirectoryAddress + sizeof(int)); | ||
} | ||
|
||
size = _metadataSize; | ||
return _metadataStart; | ||
} | ||
|
||
// https://learn.microsoft.com/windows/win32/debug/pe-format | ||
private static class PEFormat | ||
{ | ||
private const int PESignatureSize = sizeof(int); | ||
private const int CoffHeaderSize = 20; | ||
|
||
public static class DosStub | ||
{ | ||
public const int PESignatureOffset = 0x3c; | ||
} | ||
|
||
public static class PEHeader | ||
{ | ||
private const ulong OptionalHeaderOffset = PESignatureSize + CoffHeaderSize; | ||
public const ulong MagicOffset = OptionalHeaderOffset; | ||
public const ulong CLRRuntimeHeader32Offset = OptionalHeaderOffset + 208; | ||
public const ulong CLRRuntimeHeader32PlusOffset = OptionalHeaderOffset + 224; | ||
} | ||
|
||
// See ECMA-335 II.25.3.3 CLI Header | ||
public static class CorHeader | ||
{ | ||
public const ulong MetadataOffset = 8; | ||
} | ||
} | ||
} |
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
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