From 704f023147b49692cec842427b116e9ee7e64305 Mon Sep 17 00:00:00 2001 From: Tom McDonald Date: Mon, 8 Nov 2021 13:12:57 -0800 Subject: [PATCH] Fix incorrect CCW decoding in GetObjectForCCW (#61193) * Fix incorrect CCW decoding in GetObjectForCCW * Fix linux/mac build --- src/coreclr/debug/daccess/dacdbiimpl.cpp | 7 ++++ src/coreclr/debug/daccess/dacimpl.h | 5 +++ src/coreclr/debug/daccess/request.cpp | 46 ++++++++++++++++++------ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/coreclr/debug/daccess/dacdbiimpl.cpp b/src/coreclr/debug/daccess/dacdbiimpl.cpp index c6af447d1b13a..ebb5d12c0ce3a 100644 --- a/src/coreclr/debug/daccess/dacdbiimpl.cpp +++ b/src/coreclr/debug/daccess/dacdbiimpl.cpp @@ -4898,6 +4898,10 @@ VMPTR_OBJECTHANDLE DacDbiInterfaceImpl::GetObjectForCCW(CORDB_ADDRESS ccwPtr) OBJECTHANDLE ohCCW = NULL; +#ifdef FEATURE_COMWRAPPERS + if (DACTryGetComWrappersHandleFromCCW(ccwPtr, &ohCCW) != S_OK) + { +#endif #ifdef FEATURE_COMINTEROP ComCallWrapper *pCCW = DACGetCCWFromAddress(ccwPtr); if (pCCW) @@ -4905,6 +4909,9 @@ VMPTR_OBJECTHANDLE DacDbiInterfaceImpl::GetObjectForCCW(CORDB_ADDRESS ccwPtr) ohCCW = pCCW->GetObjectHandle(); } #endif +#ifdef FEATURE_COMWRAPPERS + } +#endif VMPTR_OBJECTHANDLE vmObjHandle; vmObjHandle.SetDacTargetPtr(ohCCW); diff --git a/src/coreclr/debug/daccess/dacimpl.h b/src/coreclr/debug/daccess/dacimpl.h index 84d560e73ec03..948bef16e498d 100644 --- a/src/coreclr/debug/daccess/dacimpl.h +++ b/src/coreclr/debug/daccess/dacimpl.h @@ -1498,6 +1498,11 @@ class ClrDataAccess static LONG s_procInit; +protected: +#ifdef FEATURE_COMWRAPPERS + HRESULT DACTryGetComWrappersHandleFromCCW(CLRDATA_ADDRESS ccwPtr, OBJECTHANDLE* objHandle); +#endif + public: // APIs for picking up the info needed for a debugger to look up an ngen image or IL image // from it's search path. diff --git a/src/coreclr/debug/daccess/request.cpp b/src/coreclr/debug/daccess/request.cpp index 8d6ae0b3bb85f..4b6f3413b0df1 100644 --- a/src/coreclr/debug/daccess/request.cpp +++ b/src/coreclr/debug/daccess/request.cpp @@ -4156,12 +4156,18 @@ TADDR ClrDataAccess::DACGetManagedObjectWrapperFromCCW(CLRDATA_ADDRESS ccwPtr) return managedObjectWrapperPtr; } -HRESULT ClrDataAccess::DACTryGetComWrappersObjectFromCCW(CLRDATA_ADDRESS ccwPtr, OBJECTREF* objRef) +HRESULT ClrDataAccess::DACTryGetComWrappersHandleFromCCW(CLRDATA_ADDRESS ccwPtr, OBJECTHANDLE* objHandle) { - if (ccwPtr == 0 || objRef == NULL) - return E_INVALIDARG; + HRESULT hr = E_FAIL; + TADDR ccw, managedObjectWrapperPtr; + ULONG32 bytesRead = 0; + OBJECTHANDLE handle; - SOSDacEnter(); + if (ccwPtr == 0 || objHandle == NULL) + { + hr = E_INVALIDARG; + goto ErrExit; + } if (!DACIsComWrappersCCW(ccwPtr)) { @@ -4169,18 +4175,16 @@ HRESULT ClrDataAccess::DACTryGetComWrappersObjectFromCCW(CLRDATA_ADDRESS ccwPtr, goto ErrExit; } - TADDR ccw = CLRDATA_ADDRESS_TO_TADDR(ccwPtr); + ccw = CLRDATA_ADDRESS_TO_TADDR(ccwPtr); // Return ManagedObjectWrapper as an OBJECTHANDLE. (The OBJECTHANDLE is guaranteed to live at offset 0). - TADDR managedObjectWrapperPtr = DACGetManagedObjectWrapperFromCCW(ccwPtr); + managedObjectWrapperPtr = DACGetManagedObjectWrapperFromCCW(ccwPtr); if (managedObjectWrapperPtr == NULL) { hr = E_FAIL; goto ErrExit; } - ULONG32 bytesRead = 0; - OBJECTHANDLE handle; IfFailGo(m_pTarget->ReadVirtual(managedObjectWrapperPtr, (PBYTE)&handle, sizeof(OBJECTHANDLE), &bytesRead)); if (bytesRead != sizeof(OBJECTHANDLE)) { @@ -4188,9 +4192,31 @@ HRESULT ClrDataAccess::DACTryGetComWrappersObjectFromCCW(CLRDATA_ADDRESS ccwPtr, goto ErrExit; } - *objRef = ObjectFromHandle(handle); + *objHandle = handle; - SOSDacLeave(); + return S_OK; + +ErrExit: return hr; +} + +HRESULT ClrDataAccess::DACTryGetComWrappersObjectFromCCW(CLRDATA_ADDRESS ccwPtr, OBJECTREF* objRef) +{ + HRESULT hr = E_FAIL; + + if (ccwPtr == 0 || objRef == NULL) + { + hr = E_INVALIDARG; + goto ErrExit; + } + + OBJECTHANDLE handle; + if (DACTryGetComWrappersHandleFromCCW(ccwPtr, &handle) != S_OK) + { + hr = E_FAIL; + goto ErrExit; + } + + *objRef = ObjectFromHandle(handle); return S_OK;