Skip to content
Merged
4 changes: 2 additions & 2 deletions docs/design/datacontracts/CodeVersions.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ IEnumerable<ILCodeVersionHandle> ICodeVersions.GetILCodeVersions(TargetPointer m
// CodeVersionManager::GetILCodeVersions
GetModuleAndMethodDesc(methodDesc, out TargetPointer module, out uint methodDefToken);

ModuleHandle moduleHandle = _target.Contracts.Loader.GetModuleHandle(module);
ModuleHandle moduleHandle = _target.Contracts.Loader.GetModuleHandleFromModulePtr(module);
TargetPointer ilCodeVersionTable = _target.Contracts.Loader.GetLookupTables(moduleHandle).MethodDefToILCodeVersioningState;
TargetPointer ilVersionStateAddress = _target.Contracts.Loader.GetModuleLookupMapElement(ilCodeVersionTable, methodDefToken, out var _);

Expand Down Expand Up @@ -247,7 +247,7 @@ bool ICodeVersions.CodeVersionManagerSupportsMethod(TargetPointer methodDescAddr
TypeHandle mt = rts.GetTypeHandle(mtAddr);
TargetPointer modAddr = rts.GetModule(mt);
ILoader loader = _target.Contracts.Loader;
ModuleHandle mod = loader.GetModuleHandle(modAddr);
ModuleHandle mod = loader.GetModuleHandleFromModulePtr(modAddr);
ModuleFlags modFlags = loader.GetFlags(mod);
if (modFlags.HasFlag(ModuleFlags.EditAndContinue))
return false;
Expand Down
18 changes: 16 additions & 2 deletions docs/design/datacontracts/Loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ record struct ModuleLookupTables(
```

``` csharp
ModuleHandle GetModuleHandle(TargetPointer module);
ModuleHandle GetModuleHandleFromModulePtr(TargetPointer module);
ModuleHandle GetModuleHandleFromAssemblyPtr(TargetPointer assemblyPointer);
TargetPointer GetModuleAddress(ModuleHandle handle);
IEnumerable<ModuleHandle> GetModules(TargetPointer appDomain, AssemblyIterationFlags iterationFlags);
TargetPointer GetRootAssembly();
TargetPointer GetAssembly(ModuleHandle handle);
Expand Down Expand Up @@ -146,11 +148,23 @@ private enum ModuleFlags_1 : uint

### Method Implementations
``` csharp
ModuleHandle GetModuleHandle(TargetPointer modulePointer)
ModuleHandle GetModuleHandleFromModulePtr(TargetPointer modulePointer)
{
return new ModuleHandle(modulePointer);
}

ModuleHandle ILoader.GetModuleHandleFromAssemblyPtr(TargetPointer assemblyPointer)
{
Data.Assembly assembly = // read Assembly object at assemblyPointer
return new ModuleHandle(assembly.Module);
}

TargetPointer ILoader.GetModuleAddress(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
return module.Address;
}

IEnumerable<ModuleHandle> GetModules(TargetPointer appDomain, AssemblyIterationFlags iterationFlags)
{
if (appDomain == TargetPointer.Null) throw new ArgumentException("appDomain must not be null");
Expand Down
2 changes: 1 addition & 1 deletion docs/design/datacontracts/RuntimeTypeSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ Determining if a method is in a collectible module:
{
MethodDesc md = _methodDescs[methodDesc.Address];
TargetPointer loaderModuleAddr = GetLoaderModule(md);
ModuleHandle mod = _target.Contracts.Loader.GetModuleHandle(loaderModuleAddr);
ModuleHandle mod = _target.Contracts.Loader.GetModuleHandleFromModulePtr(loaderModuleAddr);
return _target.Contracts.Loader.IsCollectible(mod);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public interface ILoader : IContract
{
static string IContract.Name => nameof(Loader);

ModuleHandle GetModuleHandle(TargetPointer modulePointer) => throw new NotImplementedException();
ModuleHandle GetModuleHandleFromModulePtr(TargetPointer modulePointer) => throw new NotImplementedException();
TargetPointer GetModuleAddress(ModuleHandle handle) => throw new NotImplementedException();
ModuleHandle GetModuleHandleFromAssemblyPtr(TargetPointer assemblyPointer) => throw new NotImplementedException();

IEnumerable<ModuleHandle> GetModules(TargetPointer appDomain, AssemblyIterationFlags iterationFlags) => throw new NotImplementedException();
TargetPointer GetRootAssembly() => throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ bool ICodeVersions.CodeVersionManagerSupportsMethod(TargetPointer methodDescAddr
TypeHandle mt = rts.GetTypeHandle(mtAddr);
TargetPointer modAddr = rts.GetModule(mt);
ILoader loader = _target.Contracts.Loader;
ModuleHandle mod = loader.GetModuleHandle(modAddr);
ModuleHandle mod = loader.GetModuleHandleFromModulePtr(modAddr);
ModuleFlags modFlags = loader.GetFlags(mod);
if (modFlags.HasFlag(ModuleFlags.EditAndContinue))
return false;
Expand Down Expand Up @@ -324,7 +324,7 @@ private TargetPointer GetILVersionStateAddress(TargetPointer module, uint method
if (methodDefToken == (uint)EcmaMetadataUtils.TokenType.mdtMethodDef)
return TargetPointer.Null;

ModuleHandle moduleHandle = _target.Contracts.Loader.GetModuleHandle(module);
ModuleHandle moduleHandle = _target.Contracts.Loader.GetModuleHandleFromModulePtr(module);
TargetPointer ilCodeVersionTable = _target.Contracts.Loader.GetLookupTables(moduleHandle).MethodDefToILCodeVersioningState;
TargetPointer ilVersionStateAddress = _target.Contracts.Loader.GetModuleLookupMapElement(ilCodeVersionTable, methodDefToken, out var _);
return ilVersionStateAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,31 @@ internal Loader_1(Target target)
_target = target;
}

ModuleHandle ILoader.GetModuleHandle(TargetPointer modulePointer)
ModuleHandle ILoader.GetModuleHandleFromModulePtr(TargetPointer modulePointer)
{
if (modulePointer == TargetPointer.Null)
throw new ArgumentNullException(nameof(modulePointer));

return new ModuleHandle(modulePointer);
}
ModuleHandle ILoader.GetModuleHandleFromAssemblyPtr(TargetPointer assemblyPointer)
{
if (assemblyPointer == TargetPointer.Null)
throw new ArgumentNullException(nameof(assemblyPointer));

Data.Assembly assembly = _target.ProcessedData.GetOrAdd<Data.Assembly>(assemblyPointer);
if (assembly.Module == TargetPointer.Null)
throw new InvalidOperationException("Assembly does not have a module associated with it.");

return new ModuleHandle(assembly.Module);
}
TargetPointer ILoader.GetModuleAddress(ModuleHandle handle)
{
if (handle.Address == TargetPointer.Null)
throw new ArgumentNullException(nameof(handle));

return handle.Address;
}

IEnumerable<ModuleHandle> ILoader.GetModules(TargetPointer appDomain, AssemblyIterationFlags iterationFlags)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ bool IRuntimeTypeSystem.IsCollectibleMethod(MethodDescHandle methodDesc)
{
MethodDesc md = _methodDescs[methodDesc.Address];
TargetPointer loaderModuleAddr = GetLoaderModule(md);
ModuleHandle mod = _target.Contracts.Loader.GetModuleHandle(loaderModuleAddr);
ModuleHandle mod = _target.Contracts.Loader.GetModuleHandleFromModulePtr(loaderModuleAddr);
return _target.Contracts.Loader.IsCollectible(mod);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ int IXCLRDataModule.GetFileName(uint bufLen, uint* nameLen, char* name)
try
{
Contracts.ILoader contract = _target.Contracts.Loader;
Contracts.ModuleHandle handle = contract.GetModuleHandle(_address);
Contracts.ModuleHandle handle = contract.GetModuleHandleFromModulePtr(_address);
string result = string.Empty;
try
{
Expand Down Expand Up @@ -180,7 +180,7 @@ int IXCLRDataModule.GetFlags(uint* flags)
try
{
Contracts.ILoader contract = _target.Contracts.Loader;
Contracts.ModuleHandle handle = contract.GetModuleHandle(_address);
Contracts.ModuleHandle handle = contract.GetModuleHandleFromModulePtr(_address);

ModuleFlags moduleFlags = contract.GetFlags(handle);
if ((moduleFlags & ModuleFlags.ReflectionEmit) != 0)
Expand Down Expand Up @@ -222,7 +222,7 @@ int IXCLRDataModule.StartEnumExtents(ulong* handle)
if (!_extentsSet)
{
Contracts.ILoader contract = _target.Contracts.Loader;
Contracts.ModuleHandle moduleHandle = contract.GetModuleHandle(_address);
Contracts.ModuleHandle moduleHandle = contract.GetModuleHandleFromModulePtr(_address);

TargetPointer peAssembly = contract.GetPEAssembly(moduleHandle);
if (peAssembly == 0)
Expand Down Expand Up @@ -268,7 +268,7 @@ int IXCLRDataModule.EnumExtent(ulong* handle, /*CLRDATA_MODULE_EXTENT*/ void* ex
try
{
Contracts.ILoader contract = _target.Contracts.Loader;
Contracts.ModuleHandle moduleHandle = contract.GetModuleHandle(_address);
Contracts.ModuleHandle moduleHandle = contract.GetModuleHandleFromModulePtr(_address);

if (!_extentsSet)
{
Expand Down Expand Up @@ -344,7 +344,7 @@ private int DacPrivateRequestGetModuleData(uint inBufferSize, byte* inBuffer, ui
Unsafe.InitBlock(getModuleData, 0, (uint)sizeof(DacpGetModuleData));

Contracts.ILoader contract = _target.Contracts.Loader;
Contracts.ModuleHandle moduleHandle = contract.GetModuleHandle(_address);
Contracts.ModuleHandle moduleHandle = contract.GetModuleHandleFromModulePtr(_address);
TargetPointer peAssembly = contract.GetPEAssembly(moduleHandle);

bool isReflectionEmit = (contract.GetFlags(moduleHandle) & ModuleFlags.ReflectionEmit) != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,55 @@ int ISOSDacInterface.GetAssemblyList(ClrDataAddress addr, int count, [In, Marsha
}
int ISOSDacInterface.GetAssemblyLocation(ClrDataAddress assembly, int count, char* location, uint* pNeeded)
=> _legacyImpl is not null ? _legacyImpl.GetAssemblyLocation(assembly, count, location, pNeeded) : HResults.E_NOTIMPL;
int ISOSDacInterface.GetAssemblyModuleList(ClrDataAddress assembly, uint count, [In, MarshalUsing(CountElementName = "count"), Out] ClrDataAddress[] modules, uint* pNeeded)
=> _legacyImpl is not null ? _legacyImpl.GetAssemblyModuleList(assembly, count, modules, pNeeded) : HResults.E_NOTIMPL;
int ISOSDacInterface.GetAssemblyModuleList(ClrDataAddress assembly, uint count, [In, MarshalUsing(CountElementName = "count"), Out] ClrDataAddress[]? modules, uint* pNeeded)
{
if (assembly == 0)
{
return HResults.E_INVALIDARG;
}
int hr = HResults.S_OK;
TargetPointer addr = 0;
try
{
if (modules is not null && modules.Length > 0 && count > 0)
{
addr = assembly.ToTargetPointer(_target);
Contracts.ILoader loader = _target.Contracts.Loader;
Contracts.ModuleHandle handle = loader.GetModuleHandleFromAssemblyPtr(addr);
TargetPointer modulePointer = loader.GetModuleAddress(handle);
modules[0] = modulePointer.ToClrDataAddress(_target);
}

if (pNeeded is not null)
{
*pNeeded = 1;
}
}
catch (System.Exception ex)
{
hr = ex.HResult;
}

#if DEBUG
if (_legacyImpl is not null)
{
ClrDataAddress[] modulesLocal = new ClrDataAddress[count];
uint neededLocal;
int hrLocal = _legacyImpl.GetAssemblyModuleList(assembly, count, modulesLocal, &neededLocal);
Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
if (hr == HResults.S_OK)
{
Debug.Assert(pNeeded == null || *pNeeded == neededLocal);
if (modules is not null && modules.Length > 0)
{
Debug.Assert(modules[0] == modulesLocal[0], $"cDAC: {modules[0]:x}, DAC: {modulesLocal[0]:x}");
}
}
}
#endif
return hr;

}
int ISOSDacInterface.GetAssemblyName(ClrDataAddress assembly, uint count, char* name, uint* pNeeded)
=> _legacyImpl is not null ? _legacyImpl.GetAssemblyName(assembly, count, name, pNeeded) : HResults.E_NOTIMPL;
int ISOSDacInterface.GetCCWData(ClrDataAddress ccw, void* data)
Expand Down Expand Up @@ -262,7 +309,10 @@ int ISOSDacInterface.GetDomainFromContext(ClrDataAddress context, ClrDataAddress
ClrDataAddress domainLocal;
int hrLocal = _legacyImpl.GetDomainFromContext(context, &domainLocal);
Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
Debug.Assert(domainLocal == context, $"cDAC: {context:x}, DAC: {domainLocal:x}");
if (hr == HResults.S_OK)
{
Debug.Assert(domainLocal == context, $"cDAC: {context:x}, DAC: {domainLocal:x}");
}
}
#endif
return hr;
Expand Down Expand Up @@ -767,7 +817,7 @@ int ISOSDacInterface.GetMethodDescName(ClrDataAddress addr, uint count, char* na
else
{
TargetPointer modulePtr = rtsContract.GetModule(rtsContract.GetTypeHandle(rtsContract.GetMethodTable(methodDescHandle)));
Contracts.ModuleHandle module = _target.Contracts.Loader.GetModuleHandle(modulePtr);
Contracts.ModuleHandle module = _target.Contracts.Loader.GetModuleHandleFromModulePtr(modulePtr);
string modulePath = _target.Contracts.Loader.GetPath(module);
ReadOnlySpan<char> moduleSpan = modulePath.AsSpan();
char directorySeparator = (char)_target.ReadGlobal<byte>(Constants.Globals.DirectorySeparator);
Expand Down Expand Up @@ -1076,7 +1126,7 @@ int ISOSDacInterface.GetModuleData(ClrDataAddress moduleAddr, DacpModuleData* da
try
{
Contracts.ILoader contract = _target.Contracts.Loader;
Contracts.ModuleHandle handle = contract.GetModuleHandle(moduleAddr.ToTargetPointer(_target));
Contracts.ModuleHandle handle = contract.GetModuleHandleFromModulePtr(moduleAddr.ToTargetPointer(_target));

data->Address = moduleAddr;
data->PEAssembly = moduleAddr; // Module address in .NET 9+ - correspondingly, SOS-DAC APIs for PE assemblies expect a module address
Expand Down Expand Up @@ -1366,7 +1416,7 @@ int ISOSDacInterface.GetPEFileBase(ClrDataAddress addr, ClrDataAddress* peBase)
try
{
Contracts.ILoader contract = _target.Contracts.Loader;
Contracts.ModuleHandle handle = contract.GetModuleHandle(addr.ToTargetPointer(_target));
Contracts.ModuleHandle handle = contract.GetModuleHandleFromModulePtr(addr.ToTargetPointer(_target));
Contracts.ModuleFlags flags = contract.GetFlags(handle);

if (!flags.HasFlag(Contracts.ModuleFlags.ReflectionEmit))
Expand Down Expand Up @@ -1405,7 +1455,7 @@ int ISOSDacInterface.GetPEFileName(ClrDataAddress addr, uint count, char* fileNa
try
{
Contracts.ILoader contract = _target.Contracts.Loader;
Contracts.ModuleHandle handle = contract.GetModuleHandle(addr.ToTargetPointer(_target));
Contracts.ModuleHandle handle = contract.GetModuleHandleFromModulePtr(addr.ToTargetPointer(_target));
string path = contract.GetPath(handle);

// Return not implemented for empty paths for non-reflection emit assemblies (for example, loaded from memory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private static unsafe void AddTypeString(Target target,

uint typeDefToken = runtimeTypeSystem.GetTypeDefToken(th);
TargetPointer modulePointer = target.Contracts.RuntimeTypeSystem.GetModule(th);
Contracts.ModuleHandle module = target.Contracts.Loader.GetModuleHandle(modulePointer);
Contracts.ModuleHandle module = target.Contracts.Loader.GetModuleHandleFromModulePtr(modulePointer);
MetadataReader internalTypeMetadata = target.Contracts.EcmaMetadata.GetMetadata(module)!;

TypeDefinition internalTypeDef = internalTypeMetadata.GetTypeDefinition((TypeDefinitionHandle)MetadataTokens.Handle((int)typeDefToken));
Expand Down Expand Up @@ -345,7 +345,7 @@ private static void AddType(Target target, StringBuilder stringBuilder, TypeHand
case CorElementType.Class:
uint typeDefToken = runtimeTypeSystem.GetTypeDefToken(typeHandle);
TargetPointer modulePointer = target.Contracts.RuntimeTypeSystem.GetModule(typeHandle);
Contracts.ModuleHandle module = target.Contracts.Loader.GetModuleHandle(modulePointer);
Contracts.ModuleHandle module = target.Contracts.Loader.GetModuleHandleFromModulePtr(modulePointer);
MetadataReader metadata = target.Contracts.EcmaMetadata.GetMetadata(module)!;
TypeDefinition typeDef = metadata.GetTypeDefinition((TypeDefinitionHandle)MetadataTokens.Handle((int)typeDefToken));
string _namespace = metadata.GetString(typeDef.Namespace);
Expand Down Expand Up @@ -390,7 +390,7 @@ private static void AddType(Target target, StringBuilder stringBuilder, TypeHand
case CorElementType.MVar:
case CorElementType.Var:
runtimeTypeSystem.IsGenericVariable(typeHandle, out TargetPointer genericVariableModulePointer, out uint typeVarToken);
Contracts.ModuleHandle genericVariableModule = target.Contracts.Loader.GetModuleHandle(genericVariableModulePointer);
Contracts.ModuleHandle genericVariableModule = target.Contracts.Loader.GetModuleHandleFromModulePtr(genericVariableModulePointer);
MetadataReader generatedVariableMetadata = target.Contracts.EcmaMetadata.GetMetadata(genericVariableModule)!;
GenericParameter genericVariable = generatedVariableMetadata.GetGenericParameter((GenericParameterHandle)MetadataTokens.Handle((int)typeVarToken));
stringBuilder.Append(generatedVariableMetadata.GetString(genericVariable.Name));
Expand Down
Loading
Loading