Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions docs/design/datacontracts/EcmaMetadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This contract provides methods to get a view of the ECMA-335 metadata for a give
## APIs of contract

```csharp
bool HasReadWriteMetadata(TargetPointer peAssembly);
TargetSpan GetReadOnlyMetadataAddress(ModuleHandle handle);
TargetSpan GetReadWriteSavedMetadataAddress(ModuleHandle handle);
System.Reflection.Metadata.MetadataReader? GetMetadata(ModuleHandle handle);
Expand Down Expand Up @@ -47,6 +48,7 @@ Types from other contracts:
| `Module` | `MetadataGeneration` | `uint32` | Counter incremented each time a module's metadata changes |
| `Module` | `PEAssembly` | `pointer` | Pointer to the module's PE assembly |
| `PEAssembly` | `MDImport` | `pointer` | An `MDInternalRW` when module has writable metadata |
| `PEAssembly` | `MDImportIsRW` | `int32` | Whether the metadata import supports updates |
| `StgPool` | `DataSize` | `uint32` | Live byte count of the head segment |
| `StgPool` | `NextSegment` | `pointer` | Pointer to the next pool segment |
| `StgPool` | `SegData` | `pointer` | Pointer to the head segment's data |
Expand All @@ -71,6 +73,11 @@ using System.IO;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;

bool HasReadWriteMetadata(TargetPointer peAssembly)
{
return Target.Read<int>(peAssembly + /* PEAssembly::MDImportIsRW offset */) != 0;
}

TargetSpan GetReadOnlyMetadataAddress(ModuleHandle handle)
{
TargetPointer baseAddress = Target.ReadPointer(handle.Address + /* Module::Base offset */);
Expand Down
1 change: 1 addition & 0 deletions docs/design/datacontracts/data-descriptor-meanings.json
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@
"PatchpointInfo.Size": "Size in bytes of the fixed patchpoint header before its variable local-offset data",
"PEAssembly.AssemblyBinder": "Pointer to the assembly binder",
"PEAssembly.MDImport": "An `MDInternalRW` when module has writable metadata",
"PEAssembly.MDImportIsRW": "Whether the metadata import supports updates",
"PEAssembly.PEImage": "Pointer to the PE image",
"PEImage.FlatImageLayout": "Pointer to the PEImage's flat PEImageLayout (used when there is no loaded layout, e.g. webcil images)",
"PEImage.LoadedImageLayout": "Pointer to the loaded image layout",
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/debug/daccess/dacdbiimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7403,13 +7403,13 @@ HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::GetGCHeapInformation(OUT COR_HEAP
}


HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::GetPEFileMDInternalRW(VMPTR_PEAssembly vmPEAssembly, OUT TADDR* pAddrMDInternalRW)
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::HasReadWriteMetadata(VMPTR_PEAssembly vmPEAssembly, OUT BOOL* pHasReadWriteMetadata)
{
DD_ENTER_MAY_THROW;
if (pAddrMDInternalRW == NULL)
if (pHasReadWriteMetadata == NULL)
return E_INVALIDARG;
PEAssembly * pPEAssembly = vmPEAssembly.GetDacPtr();
*pAddrMDInternalRW = pPEAssembly->GetMDInternalRWAddress();
*pHasReadWriteMetadata = pPEAssembly->HasReadWriteMetadata();
return S_OK;
}

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/daccess/dacdbiimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class DacDbiInterfaceImpl :
HRESULT STDMETHODCALLTYPE GetTypeLayout(CORDB_ADDRESS id, COR_TYPE_LAYOUT *pLayout);
HRESULT STDMETHODCALLTYPE GetArrayLayout(CORDB_ADDRESS id, COR_ARRAY_LAYOUT *pLayout);
HRESULT STDMETHODCALLTYPE GetGCHeapInformation(OUT COR_HEAPINFO * pHeapInfo);
HRESULT STDMETHODCALLTYPE GetPEFileMDInternalRW(VMPTR_PEAssembly vmPEAssembly, OUT TADDR* pAddrMDInternalRW);
HRESULT STDMETHODCALLTYPE HasReadWriteMetadata(VMPTR_PEAssembly vmPEAssembly, OUT BOOL* pHasReadWriteMetadata);
#ifdef FEATURE_CODE_VERSIONING
HRESULT STDMETHODCALLTYPE GetActiveRejitILCodeVersionNode(VMPTR_Module vmModule, mdMethodDef methodTk, OUT VMPTR_ILCodeVersionNode* pVmILCodeVersionNode);
HRESULT STDMETHODCALLTYPE GetEnCILCodeAndSig(VMPTR_Module vmModule, mdMethodDef methodTk, SIZE_T enCVersion, OUT TargetBuffer * pCodeInfo, OUT mdSignature * pLocalSigToken);
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/debug/di/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,9 @@ void CordbModule::RefreshMetaData()
// So far we've only got a reader for in-memory-writable metadata (MDInternalRW implementation)
// We could make a reader for MDInternalRO, but no need yet. This also ensures we don't encroach into common
// scenario where we can map a file on disk.
TADDR remoteMDInternalRWAddr = (TADDR)NULL;
IfFailThrow(GetProcess()->GetDAC()->GetPEFileMDInternalRW(m_vmPEFile, &remoteMDInternalRWAddr));
if (remoteMDInternalRWAddr != (TADDR)NULL)
BOOL hasReadWriteMetadata = FALSE;
IfFailThrow(GetProcess()->GetDAC()->HasReadWriteMetadata(m_vmPEFile, &hasReadWriteMetadata));
if (hasReadWriteMetadata)
{
_ASSERTE(m_pIMImport == NULL);
ULONG32 cbSize = 0;
Expand Down
10 changes: 4 additions & 6 deletions src/coreclr/debug/inc/dacdbiinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -1959,16 +1959,14 @@ IDacDbiInterface : public IUnknown

virtual HRESULT STDMETHODCALLTYPE GetGCHeapInformation(OUT COR_HEAPINFO * pHeapInfo) = 0;

// If a PEAssembly has an RW capable IMDInternalImport, this gets the address of the MDInternalRW
// object which implements it.
// Determines whether a PEAssembly has an RW capable IMDInternalImport.
//
//
// Arguments:
// vmPEAssembly - target PEAssembly to get metadata MDInternalRW for.
// pAddrMDInternalRW - If a PEAssembly has an RW capable IMDInternalImport, this will be set to the address
// of the MDInternalRW object which implements it. Otherwise it will be NULL.
// vmPEAssembly - target PEAssembly to inspect.
// pHasReadWriteMetadata - whether the PEAssembly has an RW capable IMDInternalImport.
//
virtual HRESULT STDMETHODCALLTYPE GetPEFileMDInternalRW(VMPTR_PEAssembly vmPEAssembly, OUT TADDR* pAddrMDInternalRW) = 0;
virtual HRESULT STDMETHODCALLTYPE HasReadWriteMetadata(VMPTR_PEAssembly vmPEAssembly, OUT BOOL* pHasReadWriteMetadata) = 0;

// DEPRECATED - use GetActiveRejitILCodeVersionNode
// Retrieves the active ReJitInfo for a given module/methodDef, if it exists.
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/inc/dacdbi.idl
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ interface IDacDbiInterface : IUnknown
HRESULT GetGCHeapInformation([out] COR_HEAPINFO * pHeapInfo);

// PE File
HRESULT GetPEFileMDInternalRW([in] VMPTR_PEAssembly vmPEAssembly, [out] TADDR * pAddrMDInternalRW);
HRESULT HasReadWriteMetadata([in] VMPTR_PEAssembly vmPEAssembly, [out] BOOL * pHasReadWriteMetadata);
Comment thread
rcj1 marked this conversation as resolved.
Resolved

HRESULT AreOptimizationsDisabled([in] VMPTR_Module vmModule, [in] mdMethodDef methodTk, [out] BOOL * pOptimizationsDisabled);

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/vm/datadescriptor/datadescriptor.inc
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ CDAC_TYPE_BEGIN(PEAssembly)
CDAC_TYPE_INDETERMINATE(PEAssembly)
CDAC_TYPE_FIELD(PEAssembly, T_POINTER, PEImage, cdac_data<PEAssembly>::PEImage)
CDAC_TYPE_FIELD(PEAssembly, T_POINTER, AssemblyBinder, cdac_data<PEAssembly>::AssemblyBinder)
CDAC_TYPE_FIELD(PEAssembly, T_INT32, MDImportIsRW, cdac_data<PEAssembly>::MDImportIsRW)
CDAC_TYPE_FIELD(PEAssembly, T_POINTER, MDImport, cdac_data<PEAssembly>::MDImport)
CDAC_TYPE_END(PEAssembly)

Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/vm/peassembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ class PEAssembly final
IMetaDataImport2 *GetRWImporter();
#else
TADDR GetMDInternalRWAddress();
BOOL HasReadWriteMetadata()
{
LIMITED_METHOD_DAC_CONTRACT;
return m_MDImportIsRW_Debugger_Use_Only;
}
#endif // DACCESS_COMPILE

void ConvertMDInternalToReadWrite();
Expand Down Expand Up @@ -432,6 +437,7 @@ struct cdac_data<PEAssembly>
{
static constexpr size_t PEImage = offsetof(PEAssembly, m_PEImage);
static constexpr size_t AssemblyBinder = offsetof(PEAssembly, m_pAssemblyBinder);
static constexpr size_t MDImportIsRW = offsetof(PEAssembly, m_MDImportIsRW_Debugger_Use_Only);
#ifndef DACCESS_COMPILE
static constexpr size_t MDImport = offsetof(PEAssembly, m_pMDImport);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Diagnostics.DataContractReader.Contracts;
public interface IEcmaMetadata : IContract
{
static string IContract.Name { get; } = nameof(EcmaMetadata);
bool HasReadWriteMetadata(TargetPointer peAssembly) => throw new NotImplementedException();
TargetSpan GetReadOnlyMetadataAddress(ModuleHandle handle) => throw new NotImplementedException();
TargetSpan GetReadWriteSavedMetadataAddress(ModuleHandle handle) => throw new NotImplementedException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ public TargetSpan GetReadWriteSavedMetadataAddress(ModuleHandle handle)
return new TargetSpan(dynamicMetadata.Data, dynamicMetadata.Size);
}

public bool HasReadWriteMetadata(TargetPointer peAssembly)
{
Data.PEAssembly data = target.ProcessedData.GetOrAdd<Data.PEAssembly>(peAssembly);
return data.MDImportIsRW != 0;
}

private TargetEcmaMetadata GetTargetEcmaMetadata(ModuleHandle handle)
{
TargetPointer peAssemblyPtr = target.Contracts.Loader.GetPEAssembly(handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ internal sealed partial class PEAssembly : IData<PEAssembly>
{
[Field] public partial TargetPointer PEImage { get; }
[Field] public partial TargetPointer AssemblyBinder { get; }
[Field] public partial int MDImportIsRW { get; }
[Field] public partial TargetPointer MDImport { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5441,8 +5441,34 @@ public int GetGCHeapInformation(COR_HEAPINFO* pHeapInfo)
return hr;
}

public int GetPEFileMDInternalRW(ulong vmPEAssembly, ulong* pAddrMDInternalRW)
=> LegacyFallbackHelper.CanFallback() && _legacy is not null ? _legacy.GetPEFileMDInternalRW(vmPEAssembly, pAddrMDInternalRW) : HResults.E_NOTIMPL;
public int HasReadWriteMetadata(ulong vmPEAssembly, Interop.BOOL* pHasReadWriteMetadata)
{
int hr = HResults.S_OK;
try
{
if (pHasReadWriteMetadata is null)
throw new ArgumentException("Output pointer cannot be null.", nameof(pHasReadWriteMetadata));

*pHasReadWriteMetadata = Interop.BOOL.FALSE;
bool hasReadWriteMetadata = _target.Contracts.EcmaMetadata.HasReadWriteMetadata(new TargetPointer(vmPEAssembly));
*pHasReadWriteMetadata = hasReadWriteMetadata ? Interop.BOOL.TRUE : Interop.BOOL.FALSE;
}
catch (System.Exception ex)
{
hr = ex.HResult;
}
#if DEBUG
if (_legacy is not null)
{
Interop.BOOL resultLocal = default;
int hrLocal = _legacy.HasReadWriteMetadata(vmPEAssembly, pHasReadWriteMetadata is null ? null : &resultLocal);
Debug.ValidateHResult(hr, hrLocal);
if (hr == HResults.S_OK)
Debug.Assert(*pHasReadWriteMetadata == resultLocal, $"cDAC: {*pHasReadWriteMetadata}, DAC: {resultLocal}");
}
#endif
return hr;
}

public int AreOptimizationsDisabled(ulong vmModule, uint methodTk, Interop.BOOL* pOptimizationsDisabled)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ int EnumerateTypeHandleParams(ulong vmTypeHandle,
int GetGCHeapInformation(COR_HEAPINFO* pHeapInfo);

[PreserveSig]
int GetPEFileMDInternalRW(ulong vmPEAssembly, ulong* pAddrMDInternalRW);
int HasReadWriteMetadata(ulong vmPEAssembly, Interop.BOOL* pHasReadWriteMetadata);
Comment thread
rcj1 marked this conversation as resolved.
Resolved

[PreserveSig]
int AreOptimizationsDisabled(ulong vmModule, uint methodTk, Interop.BOOL* pOptimizationsDisabled);
Expand Down
57 changes: 57 additions & 0 deletions src/native/managed/cdac/tests/UnitTests/DacDbiImplTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,4 +1642,61 @@ public void GetMetadata_NullBuffer(MockTarget.Architecture arch)
int hr = dacDbi.GetMetadata(vmModule, null);
Assert.Equal(System.HResults.E_POINTER, hr);
}

[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void HasReadWriteMetadata_NullOutput_ReturnsError(MockTarget.Architecture arch)
{
DacDbiImpl dacDbi = new(new TestPlaceholderTarget.Builder(arch).Build(), legacyObj: null);

int hr = dacDbi.HasReadWriteMetadata(0x1000, null);

Assert.Equal(System.HResults.E_INVALIDARG, hr);
}

[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void HasReadWriteMetadata_ReturnsMetadataAvailability(MockTarget.Architecture arch)
{
TargetTestHelpers helpers = new(arch);
var targetBuilder = new TestPlaceholderTarget.Builder(arch);
MockMemorySpace.BumpAllocator allocator = targetBuilder.MemoryBuilder.CreateAllocator(0x1000, 0x2000);
TargetTestHelpers.LayoutResult peAssemblyLayout = helpers.LayoutFields([
new(nameof(Data.PEAssembly.PEImage), DataType.pointer),
new(nameof(Data.PEAssembly.AssemblyBinder), DataType.pointer),
new(nameof(Data.PEAssembly.MDImportIsRW), DataType.int32),
new(nameof(Data.PEAssembly.MDImport), DataType.pointer),
]);
var types = new Dictionary<DataType, Target.TypeInfo>
{
[DataType.PEAssembly] = new() { Fields = peAssemblyLayout.Fields, Size = peAssemblyLayout.Stride },
};
MockMemorySpace.HeapFragment readOnlyPEAssembly = allocator.Allocate(peAssemblyLayout.Stride, "ReadOnlyPEAssembly");
MockMemorySpace.HeapFragment readWritePEAssembly = allocator.Allocate(peAssemblyLayout.Stride, "ReadWritePEAssembly");
helpers.WritePointer(
readOnlyPEAssembly.Data.AsSpan().Slice(peAssemblyLayout.Fields[nameof(Data.PEAssembly.MDImport)].Offset, helpers.PointerSize),
0x1800);
helpers.Write(
readWritePEAssembly.Data.AsSpan().Slice(peAssemblyLayout.Fields[nameof(Data.PEAssembly.MDImportIsRW)].Offset, sizeof(int)),
1);
helpers.WritePointer(
readWritePEAssembly.Data.AsSpan().Slice(peAssemblyLayout.Fields[nameof(Data.PEAssembly.MDImport)].Offset, helpers.PointerSize),
0x1800);
TestPlaceholderTarget target = targetBuilder
.AddTypes(types)
.AddContract<IEcmaMetadata>(version: "c1")
.Build();
DacDbiImpl dacDbi = new(target, legacyObj: null);
Interop.BOOL result = Interop.BOOL.TRUE;

int hr = dacDbi.HasReadWriteMetadata(readOnlyPEAssembly.Address, &result);

Assert.Equal(System.HResults.S_OK, hr);
Assert.Equal(Interop.BOOL.FALSE, result);

hr = dacDbi.HasReadWriteMetadata(readWritePEAssembly.Address, &result);

Assert.Equal(System.HResults.S_OK, hr);
Assert.Equal(Interop.BOOL.TRUE, result);
}
}
Loading