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 6 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
126 changes: 126 additions & 0 deletions docs/design/datacontracts/Loader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# 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
Copy link
Member Author

Choose a reason for hiding this comment

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

This was the only value I found in dotnet/diagnostics and microsoft/clrmd that was actually used. And it was just to print an explicit IS_EDIT_AND_CONTINUE as part of dumpmodule (also prints the raw flag values).

}

enum ModuleLookupTable
{
FieldDefToDesc,
ManifestModuleReferences,
MemberRefToDesc,
MethodDefToDesc,
TypeDefToMethodTable,
TypeRefToMethodTable,
}
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
```

``` csharp
ModuleHandle GetModuleHandle(TargetPointer);
TargetPointer GetAssembly(ModuleHandle handle);
ModuleFlags GetFlags(ModuleHandle handle);
TargetPointer GetLoaderAllocator(ModuleHandle handle);
TargetPointer GetThunkHeap(ModuleHandle handle);
bool IsReflectionEmit(ModuleHandle handle);
TargetPointer GetILBase(ModuleHandle handle);
TargetPointer GetMetadataAddress(ModuleHandle handle, out ulong size);
IDictionary<ModuleLookupTable, TargetPointer> GetLookupTables(ModuleHandle handle);
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
```

## Version 1

Data descriptors used:
- `Module`
- `PEAssembly`
- `PEImage`
- `PEImageLayout`

``` 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 */);
}

bool IsReflectionEmit(ModuleHandle handle)
{
TargetPointer peAssembly = target.ReadPointer(handle.Address + /* Module::PEAssembly offset */);
TargetPointer peImage = target.ReadPointer(peAssembly + /* PEAssembly::PEImage offset */);
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
return peImage == TargetPointer.Null;
}

TargetPointer GetILBase(ModuleHandle handle)
{
TargetPointer peAssembly = target.ReadPointer(handle.Address + /* Module::PEAssembly offset */);
TargetPointer peImage = target.ReadPointer(peAssembly + /* PEAssembly::PEImage offset */);
if (peImage == TargetPointer.Null)
return TargetPointer.Null;

TargetPointer layout = target.ReadPointer(peAssembly + /* PEImage::LoadedLayout offset */);
return target.ReadPointer(layout + /* PEImageLayout::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;
}

IDictionary<ModuleLookupTable, TargetPointer> GetLookupTables(ModuleHandle handle)
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
{
Dictionary<ModuleLookupTable, TargetPointer> tables = [];
tables[ModuleLookupTable.FieldDefToDesc] = target.ReadPointer(handle.Address + /* Module::FieldDefToDescMap */);
tables[ModuleLookupTable.ManifestModuleReferences] = target.ReadPointer(handle.Address + /* Module::ManifestModuleReferencesMap */);
tables[ModuleLookupTable.MemberRefToDesc] = target.ReadPointer(handle.Address + /* Module::MemberRefToDescMap */);
tables[ModuleLookupTable.MethodDefToDesc] = target.ReadPointer(handle.Address + /* Module::MethodDefToDescMap */);
tables[ModuleLookupTable.TypeDefToMethodTable] = target.ReadPointer(handle.Address + /* Module::TypeDefToMethodTableMap */);
tables[ModuleLookupTable.TypeRefToMethodTable] = target.ReadPointer(handle.Address + /* Module::TypeRefToMethodTableMap */);
return tables;
}
```
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);

BOOL IsExceptionFromManagedCode(EXCEPTION_RECORD * pExceptionRecord);
Expand Down
56 changes: 52 additions & 4 deletions src/coreclr/debug/daccess/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1711,13 +1711,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 Down Expand Up @@ -1759,8 +1808,7 @@ ClrDataAccess::GetModuleData(CLRDATA_ADDRESS addr, struct DacpModuleData *Module
}
EX_END_CATCH(SwallowAllExceptions)

SOSDacLeave();
return hr;
return S_OK;
}

HRESULT
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/debug/runtimeinfo/contracts.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
// so to conditionally include contracts, put additional contracts in a separate file
{
"Exception": 1,
"Thread": 1,
"SOSBreakingChangeVersion": 1 // example contract: "runtime exports an SOS breaking change version global"
"Loader": 1,
"Thread": 1
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
}
31 changes: 31 additions & 0 deletions src/coreclr/debug/runtimeinfo/datadescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,37 @@ CDAC_TYPE_BEGIN(GCHandle)
CDAC_TYPE_SIZE(sizeof(OBJECTHANDLE))
CDAC_TYPE_END(GCHandle)

CDAC_TYPE_BEGIN(Module)
CDAC_TYPE_INDETERMINATE(Module)
CDAC_TYPE_FIELD(Module, /*pointer*/, Assembly, cdac_offsets<Module>::Assembly)
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*/, PEAssembly, cdac_offsets<Module>::PEAssembly)
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)

CDAC_TYPE_BEGIN(PEAssembly)
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
CDAC_TYPE_INDETERMINATE(PEAssembly)
CDAC_TYPE_FIELD(PEAssembly, /*pointer*/, PEImage, cdac_offsets<PEAssembly>::PEImage)
CDAC_TYPE_END(PEAssembly)

CDAC_TYPE_BEGIN(PEImage)
CDAC_TYPE_INDETERMINATE(PEImage)
CDAC_TYPE_FIELD(PEImage, /*pointer*/, LoadedLayout, cdac_offsets<PEImage>::LoadedLayout)
CDAC_TYPE_END(PEImage)

CDAC_TYPE_BEGIN(PEImageLayout)
CDAC_TYPE_INDETERMINATE(PEImageLayout)
CDAC_TYPE_FIELD(PEImageLayout, /*pointer*/, Base, cdac_offsets<PEImageLayout>::Base)
CDAC_TYPE_END(PEImageLayout)

CDAC_TYPES_END()

CDAC_GLOBALS_BEGIN()
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/inc/pedecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,10 @@ class PEDecoder
FLAG_HAS_NO_READYTORUN_HEADER = 0x100,
};

protected:
TADDR m_base;

private:
COUNT_T m_size; // size of file on disk, as opposed to OptionalHeaders.SizeOfImage
ULONG m_flags;

Expand Down
20 changes: 20 additions & 0 deletions src/coreclr/vm/ceeload.h
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,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 Flags = offsetof(Module, m_dwTransientFlags);
static constexpr size_t LoaderAllocator = offsetof(Module, m_loaderAllocator);
static constexpr size_t PEAssembly = offsetof(Module, m_pPEAssembly);
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
7 changes: 7 additions & 0 deletions src/coreclr/vm/peassembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,15 @@ class PEAssembly final
// load context would be propagated to the assembly being dynamically generated.
PTR_AssemblyBinder m_pFallbackBinder;

template<typename T> friend struct ::cdac_offsets;
}; // class PEAssembly

template<>
struct cdac_offsets<PEAssembly>
{
static constexpr size_t PEImage = offsetof(PEAssembly, m_PEImage);
};

typedef ReleaseHolder<PEAssembly> PEAssemblyHolder;

#endif // PEASSEMBLY_H_
10 changes: 10 additions & 0 deletions src/coreclr/vm/peimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,16 @@ class PEImage final
SimpleRWLock *m_pLayoutLock;
PTR_PEImageLayout m_pLayouts[IMAGE_COUNT];
IMDInternalImport* m_pMDImport;

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

template<>
struct cdac_offsets<PEImage>
{
static constexpr size_t LoadedLayout = offsetof(PEImage, m_pLayouts) + PEImage::IMAGE_LOADED * sizeof(PTR_PEImageLayout);
static_assert(std::is_same<decltype(std::declval<PEImage>().m_pLayouts), PTR_PEImageLayout[PEImage::IMAGE_COUNT]>::value,
"PEImage::m_pLayouts is of type PTR_PEImageLayout[]");
};

FORCEINLINE void PEImageRelease(PEImage *i)
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/vm/peimagelayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ class PEImageLayout : public PEDecoder
Volatile<LONG> m_refCount;
public:
PEImage* m_pOwner;

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

template<>
struct cdac_offsets<PEImageLayout>
{
static constexpr size_t Base = offsetof(PEImageLayout, m_base);
};

typedef ReleaseHolder<PEImageLayout> PEImageLayoutHolder;
Expand Down
Loading
Loading