Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cdac] Start Loader contract and implement ISOSDacInterface::GetModuleData in cDAC #104257

Merged
merged 16 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
2 changes: 1 addition & 1 deletion docs/design/datacontracts/Exception.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Contract Thread
# Contract Exception

This contract is for getting information about exceptions in the process.

Expand Down
107 changes: 107 additions & 0 deletions docs/design/datacontracts/Loader.md
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 */));
}
```
1 change: 1 addition & 0 deletions src/coreclr/debug/daccess/dacimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ class ClrDataAccess

HRESULT GetThreadDataImpl(CLRDATA_ADDRESS threadAddr, struct DacpThreadData *threadData);
HRESULT GetThreadStoreDataImpl(struct DacpThreadStoreData *data);
HRESULT GetModuleDataImpl(CLRDATA_ADDRESS addr, struct DacpModuleData *moduleData);
HRESULT GetNestedExceptionDataImpl(CLRDATA_ADDRESS exception, CLRDATA_ADDRESS *exceptionObject, CLRDATA_ADDRESS *nextNestedException);
HRESULT GetMethodTableDataImpl(CLRDATA_ADDRESS mt, struct DacpMethodTableData *data);
HRESULT GetMethodTableForEEClassImpl (CLRDATA_ADDRESS eeClassReallyMT, CLRDATA_ADDRESS *value);
Expand Down
63 changes: 55 additions & 8 deletions src/coreclr/debug/daccess/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1706,13 +1706,62 @@ ClrDataAccess::GetModule(CLRDATA_ADDRESS addr, IXCLRDataModule **mod)
}

HRESULT
ClrDataAccess::GetModuleData(CLRDATA_ADDRESS addr, struct DacpModuleData *ModuleData)
ClrDataAccess::GetModuleData(CLRDATA_ADDRESS addr, struct DacpModuleData* moduleData)
{
if (addr == 0 || ModuleData == NULL)
if (addr == 0 || moduleData == NULL)
return E_INVALIDARG;

SOSDacEnter();

if (m_cdacSos != NULL)
{
hr = m_cdacSos->GetModuleData(addr, moduleData);
if (FAILED(hr))
{
hr = GetModuleDataImpl(addr, moduleData);
}
#ifdef _DEBUG
else
{
DacpModuleData moduleDataLocal;
HRESULT hrLocal = GetModuleDataImpl(addr, &moduleDataLocal);
_ASSERTE(hr == hrLocal);
_ASSERTE(moduleData->Address == moduleDataLocal.Address);
_ASSERTE(moduleData->PEAssembly == moduleDataLocal.PEAssembly);
_ASSERTE(moduleData->ilBase == moduleDataLocal.ilBase);
_ASSERTE(moduleData->metadataStart == moduleDataLocal.metadataStart);
_ASSERTE(moduleData->metadataSize == moduleDataLocal.metadataSize);
_ASSERTE(moduleData->Assembly == moduleDataLocal.Assembly);
_ASSERTE(moduleData->bIsReflection == moduleDataLocal.bIsReflection);
_ASSERTE(moduleData->bIsPEFile == moduleDataLocal.bIsPEFile);
_ASSERTE(moduleData->dwBaseClassIndex == moduleDataLocal.dwBaseClassIndex);
_ASSERTE(moduleData->dwModuleID == moduleDataLocal.dwModuleID);
_ASSERTE(moduleData->dwTransientFlags == moduleDataLocal.dwTransientFlags);
_ASSERTE(moduleData->TypeDefToMethodTableMap == moduleDataLocal.TypeDefToMethodTableMap);
_ASSERTE(moduleData->TypeRefToMethodTableMap == moduleDataLocal.TypeRefToMethodTableMap);
_ASSERTE(moduleData->MethodDefToDescMap == moduleDataLocal.MethodDefToDescMap);
_ASSERTE(moduleData->FieldDefToDescMap == moduleDataLocal.FieldDefToDescMap);
_ASSERTE(moduleData->MemberRefToDescMap == moduleDataLocal.MemberRefToDescMap);
_ASSERTE(moduleData->FileReferencesMap == moduleDataLocal.FileReferencesMap);
_ASSERTE(moduleData->ManifestModuleReferencesMap == moduleDataLocal.ManifestModuleReferencesMap);
_ASSERTE(moduleData->LoaderAllocator == moduleDataLocal.LoaderAllocator);
_ASSERTE(moduleData->ThunkHeap == moduleDataLocal.ThunkHeap);
_ASSERTE(moduleData->dwModuleIndex == moduleDataLocal.dwModuleIndex);
}
#endif
}
else
{
hr = GetModuleDataImpl(addr, moduleData);
}

SOSDacLeave();
return hr;
}

HRESULT
ClrDataAccess::GetModuleDataImpl(CLRDATA_ADDRESS addr, struct DacpModuleData *ModuleData)
{
Module* pModule = PTR_Module(TO_TADDR(addr));

ZeroMemory(ModuleData,sizeof(DacpModuleData));
Expand All @@ -1721,7 +1770,7 @@ ClrDataAccess::GetModuleData(CLRDATA_ADDRESS addr, struct DacpModuleData *Module
COUNT_T metadataSize = 0;
if (!pModule->IsReflectionEmit())
{
ModuleData->ilBase = TO_CDADDR(dac_cast<TADDR>(pModule->GetPEAssembly()->GetLoadedLayout()->GetBase()));
ModuleData->ilBase = TO_CDADDR(dac_cast<TADDR>(pModule->m_baseAddress));
}

ModuleData->metadataStart = (CLRDATA_ADDRESS)dac_cast<TADDR>(pModule->GetPEAssembly()->GetLoadedMetadata(&metadataSize));
Expand Down Expand Up @@ -1754,8 +1803,7 @@ ClrDataAccess::GetModuleData(CLRDATA_ADDRESS addr, struct DacpModuleData *Module
}
EX_END_CATCH(SwallowAllExceptions)

SOSDacLeave();
return hr;
return S_OK;
}

HRESULT
Expand Down Expand Up @@ -2232,12 +2280,11 @@ ClrDataAccess::GetPEFileBase(CLRDATA_ADDRESS moduleAddr, CLRDATA_ADDRESS *base)
SOSDacEnter();

PTR_Module pModule = PTR_Module(TO_TADDR(moduleAddr));
PEAssembly* pPEAssembly = pModule->GetPEAssembly();

// More fields later?
if (!pPEAssembly->IsReflectionEmit())
if (!pModule->IsReflectionEmit())
{
*base = TO_CDADDR(dac_cast<TADDR>(pPEAssembly->GetLoadedLayout()->GetBase()));
*base = TO_CDADDR(dac_cast<TADDR>(pModule->m_baseAddress));
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/debug/runtimeinfo/contracts.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// so to conditionally include contracts, put additional contracts in a separate file
{
"Exception": 1,
"Loader": 1,
"RuntimeTypeSystem": 1,
"Thread": 1
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
}
18 changes: 18 additions & 0 deletions src/coreclr/debug/runtimeinfo/datadescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ CDAC_TYPE_BEGIN(GCHandle)
CDAC_TYPE_SIZE(sizeof(OBJECTHANDLE))
CDAC_TYPE_END(GCHandle)

// Loader

CDAC_TYPE_BEGIN(Module)
CDAC_TYPE_INDETERMINATE(Module)
CDAC_TYPE_FIELD(Module, /*pointer*/, Assembly, cdac_offsets<Module>::Assembly)
CDAC_TYPE_FIELD(Module, /*pointer*/, Base, cdac_offsets<Module>::Base)
CDAC_TYPE_FIELD(Module, /*pointer*/, Flags, cdac_offsets<Module>::Flags)
CDAC_TYPE_FIELD(Module, /*pointer*/, LoaderAllocator, cdac_offsets<Module>::LoaderAllocator)
CDAC_TYPE_FIELD(Module, /*pointer*/, ThunkHeap, cdac_offsets<Module>::ThunkHeap)

CDAC_TYPE_FIELD(Module, /*pointer*/, FieldDefToDescMap, cdac_offsets<Module>::FieldDefToDescMap)
CDAC_TYPE_FIELD(Module, /*pointer*/, ManifestModuleReferencesMap, cdac_offsets<Module>::ManifestModuleReferencesMap)
CDAC_TYPE_FIELD(Module, /*pointer*/, MemberRefToDescMap, cdac_offsets<Module>::MemberRefToDescMap)
CDAC_TYPE_FIELD(Module, /*pointer*/, MethodDefToDescMap, cdac_offsets<Module>::MethodDefToDescMap)
CDAC_TYPE_FIELD(Module, /*pointer*/, TypeDefToMethodTableMap, cdac_offsets<Module>::TypeDefToMethodTableMap)
CDAC_TYPE_FIELD(Module, /*pointer*/, TypeRefToMethodTableMap, cdac_offsets<Module>::TypeRefToMethodTableMap)
CDAC_TYPE_END(Module)

// Metadata

CDAC_TYPE_BEGIN(MethodTable)
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/vm/ceeload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,15 @@ void Module::Initialize(AllocMemTracker *pamTracker, LPCWSTR szName)
INSTANCE_CHECK;
STANDARD_VM_CHECK;
PRECONDITION(szName == NULL);
PRECONDITION(m_pPEAssembly->IsLoaded());
}
CONTRACTL_END;

m_loaderAllocator = GetAssembly()->GetLoaderAllocator();
m_pSimpleName = m_pPEAssembly->GetSimpleName();
m_baseAddress = m_pPEAssembly->HasLoadedPEImage() ? m_pPEAssembly->GetLoadedLayout()->GetBase() : NULL;
if (m_pPEAssembly->IsReflectionEmit())
m_dwTransientFlags |= IS_REFLECTION_EMIT;

m_Crst.Init(CrstModule);
m_LookupTableCrst.Init(CrstModuleLookupTable, CrstFlags(CRST_UNSAFE_ANYMODE | CRST_DEBUGGER_THREAD));
Expand Down
25 changes: 24 additions & 1 deletion src/coreclr/vm/ceeload.h
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ class Module : public ModuleBase
PTR_CUTF8 m_pSimpleName; // Cached simple name for better performance and easier diagnostics

PTR_PEAssembly m_pPEAssembly;
PTR_VOID m_baseAddress; // Cached base address for easier diagnostics

enum {
// These are the values set in m_dwTransientFlags.
Expand All @@ -615,6 +616,8 @@ class Module : public ModuleBase
IS_PROFILER_NOTIFIED = 0x00000010,
IS_ETW_NOTIFIED = 0x00000020,

IS_REFLECTION_EMIT = 0x00000040,

//
// Note: The values below must match the ones defined in
// cordbpriv.h for DebuggerAssemblyControlFlags when shifted
Expand Down Expand Up @@ -886,7 +889,7 @@ class Module : public ModuleBase
CodeVersionManager * GetCodeVersionManager();
#endif

BOOL IsReflectionEmit() const { WRAPPER_NO_CONTRACT; SUPPORTS_DAC; return GetPEAssembly()->IsReflectionEmit(); }
BOOL IsReflectionEmit() const { WRAPPER_NO_CONTRACT; SUPPORTS_DAC; return (m_dwTransientFlags & IS_REFLECTION_EMIT) != 0; }
BOOL IsSystem() { WRAPPER_NO_CONTRACT; SUPPORTS_DAC; return m_pPEAssembly->IsSystem(); }
// Returns true iff the debugger can see this module.
BOOL IsVisibleToDebugger();
Expand Down Expand Up @@ -1602,6 +1605,26 @@ class Module : public ModuleBase

uint32_t GetNativeMetadataAssemblyCount();
#endif // !defined(DACCESS_COMPILE)

template<typename T> friend struct ::cdac_offsets;
};

template<>
struct cdac_offsets<Module>
{
static constexpr size_t Assembly = offsetof(Module, m_pAssembly);
static constexpr size_t Base = offsetof(Module, m_baseAddress);
static constexpr size_t Flags = offsetof(Module, m_dwTransientFlags);
static constexpr size_t LoaderAllocator = offsetof(Module, m_loaderAllocator);
static constexpr size_t ThunkHeap = offsetof(Module, m_pThunkHeap);

// Lookup map pointers
static constexpr size_t FieldDefToDescMap = offsetof(Module, m_FieldDefToDescMap) + offsetof(LookupMap<PTR_FieldDesc>, pTable);
static constexpr size_t ManifestModuleReferencesMap = offsetof(Module, m_ManifestModuleReferencesMap) + offsetof(LookupMap<PTR_Module>, pTable);
static constexpr size_t MemberRefToDescMap = offsetof(Module, m_MemberRefMap) + offsetof(LookupMap<TADDR>, pTable);
static constexpr size_t MethodDefToDescMap = offsetof(Module, m_MethodDefToDescMap) + offsetof(LookupMap<PTR_MethodDesc>, pTable);
static constexpr size_t TypeDefToMethodTableMap = offsetof(Module, m_TypeDefToMethodTableMap) + offsetof(LookupMap<PTR_MethodTable>, pTable);
static constexpr size_t TypeRefToMethodTableMap = offsetof(Module, m_TypeRefToMethodTableMap) + offsetof(LookupMap<PTR_TypeRef>, pTable);
};

//
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/peassembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ class PEAssembly final
// assembly that created the dynamic assembly. If the creator assembly is dynamic itself, then its fallback
// load context would be propagated to the assembly being dynamically generated.
PTR_AssemblyBinder m_pFallbackBinder;

}; // class PEAssembly

typedef ReleaseHolder<PEAssembly> PEAssemblyHolder;
Expand Down
62 changes: 62 additions & 0 deletions src/native/managed/cdacreader/src/Contracts/Loader.cs
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
}
Loading
Loading