diff --git a/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/SOSDacImpl.cs b/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/SOSDacImpl.cs index f1ee4780f5968e..a460f874014c36 100644 --- a/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/SOSDacImpl.cs +++ b/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/SOSDacImpl.cs @@ -399,15 +399,12 @@ int ISOSDacInterface.GetAssemblyData(ClrDataAddress domain, ClrDataAddress assem int ISOSDacInterface.GetAssemblyList(ClrDataAddress addr, int count, [In, MarshalUsing(CountElementName = "count"), Out] ClrDataAddress[]? values, int* pNeeded) { - if (addr == 0) - { - return HResults.E_INVALIDARG; - } - int hr = HResults.S_OK; try { + if (addr == 0) + throw new ArgumentException(); TargetPointer appDomain = addr.ToTargetPointer(_target); TargetPointer systemDomainPtr = _target.ReadGlobalPointer(Constants.Globals.SystemDomain); ClrDataAddress systemDomain = _target.ReadPointer(systemDomainPtr).ToClrDataAddress(_target); @@ -485,13 +482,11 @@ int ISOSDacInterface.GetAssemblyLocation(ClrDataAddress assembly, int count, cha => _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) { - if (assembly == 0) - { - return HResults.E_INVALIDARG; - } int hr = HResults.S_OK; try { + if (assembly == 0) + throw new ArgumentException(); if (modules is not null && modules.Length > 0 && count > 0) { TargetPointer addr = assembly.ToTargetPointer(_target); @@ -641,12 +636,10 @@ int ISOSDacInterface.GetDacModuleHandle(void* phModule) int ISOSDacInterface.GetDomainFromContext(ClrDataAddress context, ClrDataAddress* domain) { int hr = HResults.S_OK; - if (context == 0 || domain == null) - { - return HResults.E_INVALIDARG; - } try { + if (context == 0 || domain == null) + throw new ArgumentException(); *domain = context; } catch (System.Exception ex) @@ -889,14 +882,11 @@ int ISOSDacInterface.GetFieldDescData(ClrDataAddress fieldDesc, DacpFieldDescDat int ISOSDacInterface.GetFrameName(ClrDataAddress vtable, uint count, char* frameName, uint* pNeeded) { - if (vtable == 0) - { - return HResults.E_INVALIDARG; - } - int hr = HResults.S_OK; try { + if (vtable == 0) + throw new ArgumentException(); IStackWalk stackWalk = _target.Contracts.StackWalk; string name = stackWalk.GetFrameName(new(vtable)); @@ -943,13 +933,10 @@ int ISOSDacInterface.GetGCHeapData(DacpGcHeapData* data) { int hr = HResults.S_OK; - if (data == null) - { - return HResults.E_INVALIDARG; - } - try { + if (data == null) + throw new ArgumentException(); IGC gc = _target.Contracts.GC; string[] heapType = gc.GetGCIdentifiers(); if (!heapType.Contains(GCIdentifiers.Workstation) && !heapType.Contains(GCIdentifiers.Server)) @@ -1502,24 +1489,20 @@ int ISOSDacInterface.GetHillClimbingLogEntry(ClrDataAddress addr, void* data) int ISOSDacInterface.GetILForModule(ClrDataAddress moduleAddr, int rva, ClrDataAddress* il) { int hr = HResults.S_OK; - if (moduleAddr == 0 || il == null) + try { - hr = HResults.E_INVALIDARG; + if (moduleAddr == 0 || il == null) + throw new ArgumentException(); + + Contracts.ILoader loader = _target.Contracts.Loader; + TargetPointer module = moduleAddr.ToTargetPointer(_target); + Contracts.ModuleHandle moduleHandle = loader.GetModuleHandleFromModulePtr(module); + TargetPointer peAssemblyPtr = loader.GetPEAssembly(moduleHandle); + *il = loader.GetILAddr(peAssemblyPtr, rva).ToClrDataAddress(_target); } - else + catch (System.Exception ex) { - try - { - Contracts.ILoader loader = _target.Contracts.Loader; - TargetPointer module = moduleAddr.ToTargetPointer(_target); - Contracts.ModuleHandle moduleHandle = loader.GetModuleHandleFromModulePtr(module); - TargetPointer peAssemblyPtr = loader.GetPEAssembly(moduleHandle); - *il = loader.GetILAddr(peAssemblyPtr, rva).ToClrDataAddress(_target); - } - catch (System.Exception ex) - { - hr = ex.HResult; - } + hr = ex.HResult; } #if DEBUG if (_legacyImpl is not null) @@ -1554,14 +1537,11 @@ private TargetPointer DecodeJump64(TargetPointer pThunk) } int ISOSDacInterface.GetJumpThunkTarget(void* ctx, ClrDataAddress* targetIP, ClrDataAddress* targetMD) { - if (ctx == null || targetIP == null || targetMD == null) - { - return HResults.E_INVALIDARG; - } - int hr = HResults.S_OK; try { + if (ctx == null || targetIP == null || targetMD == null) + throw new ArgumentException(); // API is implemented for x64 only if (_target.Contracts.RuntimeInfo.GetTargetArchitecture() != RuntimeInfoArchitecture.X64) throw Marshal.GetExceptionForHR(HResults.E_FAIL)!; @@ -1607,24 +1587,17 @@ int ISOSDacInterface.GetJumpThunkTarget(void* ctx, ClrDataAddress* targetIP, Clr } int ISOSDacInterface.GetMethodDescData(ClrDataAddress addr, ClrDataAddress ip, DacpMethodDescData* data, uint cRevertedRejitVersions, DacpReJitData* rgRevertedRejitData, uint* pcNeededRevertedRejitData) { - if (addr == 0) - { - return HResults.E_INVALIDARG; - } - if (cRevertedRejitVersions != 0 && rgRevertedRejitData == null) - { - return HResults.E_INVALIDARG; - } - if (rgRevertedRejitData != null && pcNeededRevertedRejitData == null) - { - // If you're asking for reverted rejit data, you'd better ask for the number of - // elements we return - return HResults.E_INVALIDARG; - } - int hr = HResults.S_OK; try { + if (addr == 0) + throw new ArgumentException(); + if (cRevertedRejitVersions != 0 && rgRevertedRejitData == null) + throw new ArgumentException(); + if (rgRevertedRejitData != null && pcNeededRevertedRejitData == null) + // If you're asking for reverted rejit data, you'd better ask for the number of + // elements we return + throw new ArgumentException(); Contracts.IRuntimeTypeSystem rtsContract = _target.Contracts.RuntimeTypeSystem; TargetPointer methodDesc = addr.ToTargetPointer(_target); @@ -1907,40 +1880,37 @@ private void CopyNativeCodeVersionToReJitData( int ISOSDacInterface.GetMethodDescFromToken(ClrDataAddress moduleAddr, uint token, ClrDataAddress* methodDesc) { int hr = HResults.S_OK; - if (moduleAddr == 0 || methodDesc == null) - hr = HResults.E_INVALIDARG; - else + try { - try - { - Contracts.ILoader loader = _target.Contracts.Loader; - TargetPointer module = moduleAddr.ToTargetPointer(_target); - Contracts.ModuleHandle moduleHandle = loader.GetModuleHandleFromModulePtr(module); - Contracts.ModuleLookupTables lookupTables = loader.GetLookupTables(moduleHandle); - switch ((CorTokenType)token & CorTokenType.typeMask) - { - case CorTokenType.mdtFieldDef: - *methodDesc = loader.GetModuleLookupMapElement(lookupTables.FieldDefToDesc, token, out var _).ToClrDataAddress(_target); - break; - case CorTokenType.mdtMethodDef: - *methodDesc = loader.GetModuleLookupMapElement(lookupTables.MethodDefToDesc, token, out var _).ToClrDataAddress(_target); - break; - case CorTokenType.mdtTypeDef: - *methodDesc = loader.GetModuleLookupMapElement(lookupTables.TypeDefToMethodTable, token, out var _).ToClrDataAddress(_target); - break; - case CorTokenType.mdtTypeRef: - *methodDesc = loader.GetModuleLookupMapElement(lookupTables.TypeRefToMethodTable, token, out var _).ToClrDataAddress(_target); - break; - default: - hr = HResults.E_INVALIDARG; - break; - } - } - catch (System.Exception ex) + if (moduleAddr == 0 || methodDesc == null) + throw new ArgumentException(); + + Contracts.ILoader loader = _target.Contracts.Loader; + TargetPointer module = moduleAddr.ToTargetPointer(_target); + Contracts.ModuleHandle moduleHandle = loader.GetModuleHandleFromModulePtr(module); + Contracts.ModuleLookupTables lookupTables = loader.GetLookupTables(moduleHandle); + switch ((CorTokenType)token & CorTokenType.typeMask) { - hr = ex.HResult; + case CorTokenType.mdtFieldDef: + *methodDesc = loader.GetModuleLookupMapElement(lookupTables.FieldDefToDesc, token, out var _).ToClrDataAddress(_target); + break; + case CorTokenType.mdtMethodDef: + *methodDesc = loader.GetModuleLookupMapElement(lookupTables.MethodDefToDesc, token, out var _).ToClrDataAddress(_target); + break; + case CorTokenType.mdtTypeDef: + *methodDesc = loader.GetModuleLookupMapElement(lookupTables.TypeDefToMethodTable, token, out var _).ToClrDataAddress(_target); + break; + case CorTokenType.mdtTypeRef: + *methodDesc = loader.GetModuleLookupMapElement(lookupTables.TypeRefToMethodTable, token, out var _).ToClrDataAddress(_target); + break; + default: + throw new ArgumentException(); } } + catch (System.Exception ex) + { + hr = ex.HResult; + } #if DEBUG if (_legacyImpl is not null) { @@ -1957,14 +1927,13 @@ int ISOSDacInterface.GetMethodDescFromToken(ClrDataAddress moduleAddr, uint toke } int ISOSDacInterface.GetMethodDescName(ClrDataAddress addr, uint count, char* name, uint* pNeeded) { - if (addr == 0) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; if (pNeeded != null) *pNeeded = 0; try { + if (addr == 0) + throw new ArgumentException(); TargetPointer methodDesc = addr.ToTargetPointer(_target); StringBuilder stringBuilder = new StringBuilder(); Contracts.IRuntimeTypeSystem rtsContract = _target.Contracts.RuntimeTypeSystem; @@ -2082,13 +2051,12 @@ int ISOSDacInterface.GetMethodDescPtrFromFrame(ClrDataAddress frameAddr, ClrData } int ISOSDacInterface.GetMethodDescPtrFromIP(ClrDataAddress ip, ClrDataAddress* ppMD) { - if (ip == 0 || ppMD == null) - return HResults.E_INVALIDARG; - int hr = HResults.E_NOTIMPL; try { + if (ip == 0 || ppMD == null) + throw new ArgumentException(); IExecutionManager executionManager = _target.Contracts.ExecutionManager; IRuntimeTypeSystem rts = _target.Contracts.RuntimeTypeSystem; @@ -2157,12 +2125,11 @@ int ISOSDacInterface.GetMethodDescTransparencyData(ClrDataAddress methodDesc, Da int ISOSDacInterface.GetMethodTableData(ClrDataAddress mt, DacpMethodTableData* data) { - if (mt == 0 || data == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (mt == 0 || data == null) + throw new ArgumentException(); Contracts.IRuntimeTypeSystem contract = _target.Contracts.RuntimeTypeSystem; Contracts.TypeHandle methodTable = contract.GetTypeHandle(mt.ToTargetPointer(_target)); @@ -2271,12 +2238,11 @@ int ISOSDacInterface.GetMethodTableFieldData(ClrDataAddress mt, DacpMethodTableF } int ISOSDacInterface.GetMethodTableForEEClass(ClrDataAddress eeClassReallyCanonMT, ClrDataAddress* value) { - if (eeClassReallyCanonMT == 0 || value == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (eeClassReallyCanonMT == 0 || value == null) + throw new ArgumentException(); Contracts.IRuntimeTypeSystem contract = _target.Contracts.RuntimeTypeSystem; Contracts.TypeHandle methodTableHandle = contract.GetTypeHandle(eeClassReallyCanonMT.ToTargetPointer(_target)); *value = methodTableHandle.Address.ToClrDataAddress(_target); @@ -2478,12 +2444,11 @@ int ISOSDacInterface.GetModule(ClrDataAddress addr, out IXCLRDataModule? mod) int ISOSDacInterface.GetModuleData(ClrDataAddress moduleAddr, DacpModuleData* data) { - if (moduleAddr == 0 || data == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (moduleAddr == 0 || data == null) + throw new ArgumentException(); Contracts.ILoader contract = _target.Contracts.Loader; Contracts.ModuleHandle handle = contract.GetModuleHandleFromModulePtr(moduleAddr.ToTargetPointer(_target)); @@ -2573,12 +2538,11 @@ int ISOSDacInterface.GetModuleData(ClrDataAddress moduleAddr, DacpModuleData* da int ISOSDacInterface.GetNestedExceptionData(ClrDataAddress exception, ClrDataAddress* exceptionObject, ClrDataAddress* nextNestedException) { - if (exception == 0 || exceptionObject == null || nextNestedException == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (exception == 0 || exceptionObject == null || nextNestedException == null) + throw new ArgumentException(); Contracts.IException contract = _target.Contracts.Exception; TargetPointer exceptionObjectLocal = contract.GetNestedExceptionInfo( exception.ToTargetPointer(_target), @@ -2613,12 +2577,11 @@ int ISOSDacInterface.GetObjectClassName(ClrDataAddress obj, uint count, char* cl int ISOSDacInterface.GetObjectData(ClrDataAddress objAddr, DacpObjectData* data) { - if (objAddr == 0 || data == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (objAddr == 0 || data == null) + throw new ArgumentException(); Contracts.IObject objectContract = _target.Contracts.Object; Contracts.IRuntimeTypeSystem runtimeTypeSystemContract = _target.Contracts.RuntimeTypeSystem; @@ -2724,12 +2687,11 @@ int ISOSDacInterface.GetObjectData(ClrDataAddress objAddr, DacpObjectData* data) int ISOSDacInterface.GetObjectStringData(ClrDataAddress obj, uint count, char* stringData, uint* pNeeded) { - if (obj == 0 || (stringData == null && pNeeded == null) || (stringData is not null && count <= 0)) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (obj == 0 || (stringData == null && pNeeded == null) || (stringData is not null && count <= 0)) + throw new ArgumentException(); Contracts.IObject contract = _target.Contracts.Object; string str = contract.GetStringValue(obj.ToTargetPointer(_target)); OutputBufferHelpers.CopyStringToBuffer(stringData, count, pNeeded, str); @@ -2864,12 +2826,11 @@ int ISOSDacInterface.GetOOMStaticData(DacpOomData* data) int ISOSDacInterface.GetPEFileBase(ClrDataAddress addr, ClrDataAddress* peBase) { - if (addr == 0 || peBase == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (addr == 0 || peBase == null) + throw new ArgumentException(); Contracts.ILoader contract = _target.Contracts.Loader; Contracts.ModuleHandle handle = contract.GetModuleHandleFromModulePtr(addr.ToTargetPointer(_target)); Contracts.ModuleFlags flags = contract.GetFlags(handle); @@ -2903,12 +2864,12 @@ int ISOSDacInterface.GetPEFileBase(ClrDataAddress addr, ClrDataAddress* peBase) int ISOSDacInterface.GetPEFileName(ClrDataAddress addr, uint count, char* fileName, uint* pNeeded) { - if (addr == 0 || (fileName == null && pNeeded == null) || (fileName is not null && count <= 0)) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (addr == 0 || (fileName == null && pNeeded == null) || (fileName is not null && count <= 0)) + throw new ArgumentException(); + Contracts.ILoader contract = _target.Contracts.Loader; Contracts.ModuleHandle handle = contract.GetModuleHandleFromModulePtr(addr.ToTargetPointer(_target)); string path = contract.GetPath(handle); @@ -2918,9 +2879,7 @@ int ISOSDacInterface.GetPEFileName(ClrDataAddress addr, uint count, char* fileNa { Contracts.ModuleFlags flags = contract.GetFlags(handle); if (!flags.HasFlag(Contracts.ModuleFlags.ReflectionEmit)) - { - return HResults.E_NOTIMPL; - } + throw new NotImplementedException(); } OutputBufferHelpers.CopyStringToBuffer(fileName, count, pNeeded, path); @@ -3002,12 +2961,11 @@ int ISOSDacInterface.GetThreadAllocData(ClrDataAddress thread, void* data) int ISOSDacInterface.GetThreadData(ClrDataAddress thread, DacpThreadData* data) { - if (thread == 0 || data == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (thread == 0 || data == null) + throw new ArgumentException(); Contracts.IThread contract = _target.Contracts.Thread; Contracts.ThreadData threadData = contract.GetThreadData(thread.ToTargetPointer(_target)); data->corThreadId = (int)threadData.Id; @@ -3066,10 +3024,10 @@ int ISOSDacInterface.GetThreadData(ClrDataAddress thread, DacpThreadData* data) int ISOSDacInterface.GetThreadFromThinlockID(uint thinLockId, ClrDataAddress* pThread) { int hr = HResults.S_OK; - if (pThread == null) - hr = HResults.E_INVALIDARG; try { + if (pThread == null) + throw new ArgumentException(); TargetPointer threadPtr = _target.Contracts.Thread.IdToThread(thinLockId); *pThread = threadPtr.ToClrDataAddress(_target); } @@ -3125,12 +3083,11 @@ int ISOSDacInterface.GetThreadpoolData(void* data) int ISOSDacInterface.GetThreadStoreData(DacpThreadStoreData* data) { - if (data == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (data == null) + throw new ArgumentException(); Contracts.IThread thread = _target.Contracts.Thread; Contracts.ThreadStoreData threadStoreData = thread.GetThreadStoreData(); data->threadCount = threadStoreData.ThreadCount; @@ -3176,12 +3133,11 @@ int ISOSDacInterface.GetThreadStoreData(DacpThreadStoreData* data) int ISOSDacInterface.GetTLSIndex(uint* pIndex) { - if (pIndex == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (pIndex == null) + throw new ArgumentException(); uint TlsIndexBase = _target.Read(_target.ReadGlobalPointer(Constants.Globals.TlsIndexBase)); uint OffsetOfCurrentThreadInfo = _target.Read(_target.ReadGlobalPointer(Constants.Globals.OffsetOfCurrentThreadInfo)); uint CombinedTlsIndex = TlsIndexBase + (OffsetOfCurrentThreadInfo << 16) + 0x80000000; @@ -3207,12 +3163,11 @@ int ISOSDacInterface.GetTLSIndex(uint* pIndex) } int ISOSDacInterface.GetUsefulGlobals(DacpUsefulGlobalsData* data) { - if (data == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (data == null) + throw new ArgumentException(); data->ArrayMethodTable = _target.ReadPointer( _target.ReadGlobalPointer(Constants.Globals.ObjectArrayMethodTable)) .ToClrDataAddress(_target); @@ -3306,42 +3261,36 @@ int ISOSDacInterface.TraverseModuleMap(ModuleMapType mmt, ClrDataAddress moduleA { int hr = HResults.S_OK; IEnumerable<(TargetPointer Address, uint Index)> elements = Enumerable.Empty<(TargetPointer, uint)>(); - if (moduleAddr == 0) - hr = HResults.E_INVALIDARG; - else + try { - try + if (moduleAddr == 0) + throw new ArgumentException(); + + Contracts.ILoader loader = _target.Contracts.Loader; + TargetPointer moduleAddrPtr = moduleAddr.ToTargetPointer(_target); + Contracts.ModuleHandle moduleHandle = loader.GetModuleHandleFromModulePtr(moduleAddrPtr); + Contracts.ModuleLookupTables lookupTables = loader.GetLookupTables(moduleHandle); + switch (mmt) { - Contracts.ILoader loader = _target.Contracts.Loader; - TargetPointer moduleAddrPtr = moduleAddr.ToTargetPointer(_target); - Contracts.ModuleHandle moduleHandle = loader.GetModuleHandleFromModulePtr(moduleAddrPtr); - Contracts.ModuleLookupTables lookupTables = loader.GetLookupTables(moduleHandle); - switch (mmt) - { - case ModuleMapType.TYPEDEFTOMETHODTABLE: - elements = loader.EnumerateModuleLookupMap(lookupTables.TypeDefToMethodTable); - break; - case ModuleMapType.TYPEREFTOMETHODTABLE: - elements = loader.EnumerateModuleLookupMap(lookupTables.TypeRefToMethodTable); - break; - default: - hr = HResults.E_INVALIDARG; - break; - } - if (hr == HResults.S_OK) - { - foreach ((TargetPointer element, uint index) in elements) - { - // Call the callback with each element - pCallback(index, element.ToClrDataAddress(_target).Value, token); - } - } + case ModuleMapType.TYPEDEFTOMETHODTABLE: + elements = loader.EnumerateModuleLookupMap(lookupTables.TypeDefToMethodTable); + break; + case ModuleMapType.TYPEREFTOMETHODTABLE: + elements = loader.EnumerateModuleLookupMap(lookupTables.TypeRefToMethodTable); + break; + default: + throw new ArgumentException(); } - catch (System.Exception ex) + foreach ((TargetPointer element, uint index) in elements) { - hr = ex.HResult; + // Call the callback with each element + pCallback(index, element.ToClrDataAddress(_target).Value, token); } } + catch (System.Exception ex) + { + hr = ex.HResult; + } #if DEBUG if (_legacyImpl is not null) { @@ -3722,12 +3671,11 @@ int ISOSDacInterface6.GetMethodTableCollectibleData(ClrDataAddress mt, DacpMetho #region ISOSDacInterface7 int ISOSDacInterface7.GetPendingReJITID(ClrDataAddress methodDesc, int* pRejitId) { - if (methodDesc == 0 || pRejitId == null) - return HResults.E_INVALIDARG; - int hr = HResults.S_OK; try { + if (methodDesc == 0 || pRejitId == null) + throw new ArgumentException(); Contracts.IReJIT rejitContract = _target.Contracts.ReJIT; Contracts.ICodeVersions codeVersionsContract = _target.Contracts.CodeVersions; TargetPointer methodDescPtr = methodDesc.ToTargetPointer(_target); @@ -4274,25 +4222,23 @@ int ISOSDacInterface13.LockedFlush() int ISOSDacInterface14.GetStaticBaseAddress(ClrDataAddress methodTable, ClrDataAddress* nonGCStaticsAddress, ClrDataAddress* GCStaticsAddress) { int hr = HResults.S_OK; - if (nonGCStaticsAddress == null && GCStaticsAddress == null) - hr = HResults.E_POINTER; - else if (methodTable == 0) - hr = HResults.E_INVALIDARG; - else + try { - try - { - Contracts.IRuntimeTypeSystem rtsContract = _target.Contracts.RuntimeTypeSystem; - Contracts.TypeHandle typeHandle = rtsContract.GetTypeHandle(methodTable.ToTargetPointer(_target)); - if (GCStaticsAddress != null) - *GCStaticsAddress = rtsContract.GetGCStaticsBasePointer(typeHandle).ToClrDataAddress(_target); - if (nonGCStaticsAddress != null) - *nonGCStaticsAddress = rtsContract.GetNonGCStaticsBasePointer(typeHandle).ToClrDataAddress(_target); - } - catch (System.Exception ex) - { - hr = ex.HResult; - } + if (nonGCStaticsAddress == null && GCStaticsAddress == null) + throw new NullReferenceException(); + if (methodTable == 0) + throw new ArgumentException(); + + Contracts.IRuntimeTypeSystem rtsContract = _target.Contracts.RuntimeTypeSystem; + Contracts.TypeHandle typeHandle = rtsContract.GetTypeHandle(methodTable.ToTargetPointer(_target)); + if (GCStaticsAddress != null) + *GCStaticsAddress = rtsContract.GetGCStaticsBasePointer(typeHandle).ToClrDataAddress(_target); + if (nonGCStaticsAddress != null) + *nonGCStaticsAddress = rtsContract.GetNonGCStaticsBasePointer(typeHandle).ToClrDataAddress(_target); + } + catch (System.Exception ex) + { + hr = ex.HResult; } #if DEBUG if (_legacyImpl14 is not null) @@ -4315,39 +4261,37 @@ int ISOSDacInterface14.GetStaticBaseAddress(ClrDataAddress methodTable, ClrDataA int ISOSDacInterface14.GetThreadStaticBaseAddress(ClrDataAddress methodTable, ClrDataAddress thread, ClrDataAddress* nonGCStaticsAddress, ClrDataAddress* GCStaticsAddress) { int hr = HResults.S_OK; - if (nonGCStaticsAddress == null && GCStaticsAddress == null) - hr = HResults.E_POINTER; - else if (methodTable == 0 || thread == 0) - hr = HResults.E_INVALIDARG; - else + try { - try + if (nonGCStaticsAddress == null && GCStaticsAddress == null) + throw new NullReferenceException(); + if (methodTable == 0 || thread == 0) + throw new ArgumentException(); + + Contracts.IRuntimeTypeSystem rtsContract = _target.Contracts.RuntimeTypeSystem; + TargetPointer methodTablePtr = methodTable.ToTargetPointer(_target); + TargetPointer threadPtr = thread.ToTargetPointer(_target); + Contracts.TypeHandle typeHandle = rtsContract.GetTypeHandle(methodTablePtr); + ushort numThreadStaticFields = rtsContract.GetNumThreadStaticFields(typeHandle); + if (numThreadStaticFields == 0) { - Contracts.IRuntimeTypeSystem rtsContract = _target.Contracts.RuntimeTypeSystem; - TargetPointer methodTablePtr = methodTable.ToTargetPointer(_target); - TargetPointer threadPtr = thread.ToTargetPointer(_target); - Contracts.TypeHandle typeHandle = rtsContract.GetTypeHandle(methodTablePtr); - ushort numThreadStaticFields = rtsContract.GetNumThreadStaticFields(typeHandle); - if (numThreadStaticFields == 0) - { - if (GCStaticsAddress != null) - *GCStaticsAddress = 0; - if (nonGCStaticsAddress != null) - *nonGCStaticsAddress = 0; - } - else - { - if (GCStaticsAddress != null) - *GCStaticsAddress = rtsContract.GetGCThreadStaticsBasePointer(typeHandle, threadPtr).ToClrDataAddress(_target); - if (nonGCStaticsAddress != null) - *nonGCStaticsAddress = rtsContract.GetNonGCThreadStaticsBasePointer(typeHandle, threadPtr).ToClrDataAddress(_target); - } + if (GCStaticsAddress != null) + *GCStaticsAddress = 0; + if (nonGCStaticsAddress != null) + *nonGCStaticsAddress = 0; } - catch (System.Exception ex) + else { - hr = ex.HResult; + if (GCStaticsAddress != null) + *GCStaticsAddress = rtsContract.GetGCThreadStaticsBasePointer(typeHandle, threadPtr).ToClrDataAddress(_target); + if (nonGCStaticsAddress != null) + *nonGCStaticsAddress = rtsContract.GetNonGCThreadStaticsBasePointer(typeHandle, threadPtr).ToClrDataAddress(_target); } } + catch (System.Exception ex) + { + hr = ex.HResult; + } #if DEBUG if (_legacyImpl14 is not null) { @@ -4372,27 +4316,24 @@ int ISOSDacInterface14.GetThreadStaticBaseAddress(ClrDataAddress methodTable, Cl int ISOSDacInterface14.GetMethodTableInitializationFlags(ClrDataAddress methodTable, MethodTableInitializationFlags* initializationStatus) { int hr = HResults.S_OK; - if (methodTable == 0) - hr = HResults.E_INVALIDARG; - else if (initializationStatus == null) - hr = HResults.E_POINTER; + try + { + if (methodTable == 0) + throw new ArgumentException(); + if (initializationStatus == null) + throw new NullReferenceException(); - else + Contracts.IRuntimeTypeSystem rtsContract = _target.Contracts.RuntimeTypeSystem; + Contracts.TypeHandle methodTableHandle = rtsContract.GetTypeHandle(methodTable.ToTargetPointer(_target)); + *initializationStatus = (MethodTableInitializationFlags)0; + if (rtsContract.IsClassInited(methodTableHandle)) + *initializationStatus = MethodTableInitializationFlags.MethodTableInitialized; + if (rtsContract.IsInitError(methodTableHandle)) + *initializationStatus |= MethodTableInitializationFlags.MethodTableInitializationFailed; + } + catch (System.Exception ex) { - try - { - Contracts.IRuntimeTypeSystem rtsContract = _target.Contracts.RuntimeTypeSystem; - Contracts.TypeHandle methodTableHandle = rtsContract.GetTypeHandle(methodTable.ToTargetPointer(_target)); - *initializationStatus = (MethodTableInitializationFlags)0; - if (rtsContract.IsClassInited(methodTableHandle)) - *initializationStatus = MethodTableInitializationFlags.MethodTableInitialized; - if (rtsContract.IsInitError(methodTableHandle)) - *initializationStatus |= MethodTableInitializationFlags.MethodTableInitializationFailed; - } - catch (System.Exception ex) - { - hr = ex.HResult; - } + hr = ex.HResult; } #if DEBUG if (_legacyImpl14 is not null) @@ -4570,18 +4511,26 @@ int ISOSEnum.Reset() int ISOSEnum.GetCount(uint* pCount) { - if (pCount == null) - return HResults.E_POINTER; + int hr = HResults.S_OK; + try + { + if (pCount == null) + throw new NullReferenceException(); #if DEBUG - if (_legacyMethodEnum is not null) + if (_legacyMethodEnum is not null) + { + uint countLocal; + _legacyMethodEnum.GetCount(&countLocal); + Debug.Assert(countLocal == (uint)_methods.Count); + } +#endif + *pCount = (uint)_methods.Count; + } + catch (System.Exception ex) { - uint countLocal; - _legacyMethodEnum.GetCount(&countLocal); - Debug.Assert(countLocal == (uint)_methods.Count); + hr = ex.HResult; } -#endif - *pCount = (uint)_methods.Count; - return HResults.S_OK; + return hr; } }