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 8 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
118 changes: 118 additions & 0 deletions docs/design/datacontracts/Loader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# 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`

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

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
58 changes: 53 additions & 5 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 All @@ -1726,7 +1775,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 @@ -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
}
22 changes: 22 additions & 0 deletions src/coreclr/debug/runtimeinfo/datadescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ 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*/, 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*/, 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_TYPES_END()

CDAC_GLOBALS_BEGIN()
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/vm/ceeload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,13 @@ 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;

m_Crst.Init(CrstModule);
m_LookupTableCrst.Init(CrstModuleLookupTable, CrstFlags(CRST_UNSAFE_ANYMODE | CRST_DEBUGGER_THREAD));
Expand Down
22 changes: 22 additions & 0 deletions 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 Down Expand Up @@ -1602,6 +1603,27 @@ 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 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_
64 changes: 64 additions & 0 deletions src/native/managed/cdacreader/src/Contracts/Loader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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
}

internal enum ModuleLookupTable
{
FieldDefToDesc,
ManifestModuleReferences,
MemberRefToDesc,
MethodDefToDesc,
TypeDefToMethodTable,
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 bool IsReflectionEmit(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 IDictionary<ModuleLookupTable, TargetPointer> GetLookupTables(ModuleHandle handle) => throw new NotImplementedException();
}

internal readonly struct Loader : ILoader
{
// Everything throws NotImplementedException
}
Loading
Loading