From a5742c2655fa3dd75963600386e788f35ba2b5a1 Mon Sep 17 00:00:00 2001 From: Sean Gillespie Date: Tue, 20 Jun 2017 15:24:05 -0700 Subject: [PATCH] [Local GC] Move handle creation/deletion profiler callbacks out of the GC (dotnet/coreclr#12314) * [Local GC] Move handle creation and handle destruction profiler callbacks out of the GC * Remove dead code * Move some handle-related code out of gcheaputilities.cpp and into gchandleutilities.cpp * Address code review feedback, remove some more dead code and add some style fixes * Refactor handle creation and destruction helpers to go through a common function Commit migrated from https://github.com/dotnet/coreclr/commit/e7725a6ff986879f8ff2fd9e09ab636545ba7bca --- src/coreclr/src/gc/handletable.cpp | 129 --------------------- src/coreclr/src/gc/handletable.h | 6 - src/coreclr/src/vm/CMakeLists.txt | 1 + src/coreclr/src/vm/gchandleutilities.cpp | 91 +++++++++++++++ src/coreclr/src/vm/gchandleutilities.h | 187 ++++++++++--------------------- src/coreclr/src/vm/gcheaputilities.cpp | 54 --------- 6 files changed, 154 insertions(+), 314 deletions(-) create mode 100644 src/coreclr/src/vm/gchandleutilities.cpp diff --git a/src/coreclr/src/gc/handletable.cpp b/src/coreclr/src/gc/handletable.cpp index 875e894..8c6c835 100644 --- a/src/coreclr/src/gc/handletable.cpp +++ b/src/coreclr/src/gc/handletable.cpp @@ -346,19 +346,6 @@ OBJECTHANDLE HndCreateHandle(HHANDLETABLE hTable, uint32_t uType, OBJECTREF obje // store the reference HndAssignHandle(handle, object); - -#if defined(ENABLE_PERF_COUNTERS) || defined(FEATURE_EVENT_TRACE) - g_dwHandles++; -#endif // ENABLE_PERF_COUNTERS || FEATURE_EVENT_TRACE - -#ifdef GC_PROFILING - { - BEGIN_PIN_PROFILER(CORProfilerTrackGC()); - g_profControlBlock.pProfInterface->HandleCreated((uintptr_t)handle, (ObjectID)OBJECTREF_TO_UNCHECKED_OBJECTREF(object)); - END_PIN_PROFILER(); - } -#endif //GC_PROFILING - STRESS_LOG2(LF_GC, LL_INFO1000, "CreateHandle: %p, type=%d\n", handle, uType); // return the result @@ -490,18 +477,6 @@ void HndDestroyHandle(HHANDLETABLE hTable, uint32_t uType, OBJECTHANDLE handle) // fetch the handle table pointer HandleTable *pTable = Table(hTable); -#ifdef GC_PROFILING - { - BEGIN_PIN_PROFILER(CORProfilerTrackGC()); - g_profControlBlock.pProfInterface->HandleDestroyed((uintptr_t)handle); - END_PIN_PROFILER(); - } -#endif //GC_PROFILING - -#if defined(ENABLE_PERF_COUNTERS) || defined(FEATURE_EVENT_TRACE) - g_dwHandles--; -#endif // ENABLE_PERF_COUNTERS || FEATURE_EVENT_TRACE - // sanity check the type index _ASSERTE(uType < pTable->uTypeCount); @@ -543,110 +518,6 @@ void HndDestroyHandleOfUnknownType(HHANDLETABLE hTable, OBJECTHANDLE handle) HndDestroyHandle(hTable, HandleFetchType(handle), handle); } - -/* - * HndCreateHandles - * - * Entrypoint for allocating handles in bulk. - * - */ -uint32_t HndCreateHandles(HHANDLETABLE hTable, uint32_t uType, OBJECTHANDLE *pHandles, uint32_t uCount) -{ - WRAPPER_NO_CONTRACT; - - // fetch the handle table pointer - HandleTable *pTable = Table(hTable); - - // sanity check the type index - _ASSERTE(uType < pTable->uTypeCount); - - // keep track of the number of handles we've allocated - uint32_t uSatisfied = 0; - - // if this is a large number of handles then bypass the cache - if (uCount > SMALL_ALLOC_COUNT) - { - CrstHolder ch(&pTable->Lock); - - // allocate handles in bulk from the main handle table - uSatisfied = TableAllocBulkHandles(pTable, uType, pHandles, uCount); - } - - // do we still need to get some handles? - if (uSatisfied < uCount) - { - // get some handles from the cache - uSatisfied += TableAllocHandlesFromCache(pTable, uType, pHandles + uSatisfied, uCount - uSatisfied); - } - -#if defined(ENABLE_PERF_COUNTERS) || defined(FEATURE_EVENT_TRACE) - g_dwHandles += uSatisfied; -#endif // ENABLE_PERF_COUNTERS || FEATURE_EVENT_TRACE - -#ifdef GC_PROFILING - { - BEGIN_PIN_PROFILER(CORProfilerTrackGC()); - for (uint32_t i = 0; i < uSatisfied; i++) - g_profControlBlock.pProfInterface->HandleCreated((uintptr_t)pHandles[i], 0); - END_PIN_PROFILER(); - } -#endif //GC_PROFILING - - // return the number of handles we allocated - return uSatisfied; -} - - -/* - * HndDestroyHandles - * - * Entrypoint for freeing handles in bulk. - * - */ -void HndDestroyHandles(HHANDLETABLE hTable, uint32_t uType, const OBJECTHANDLE *pHandles, uint32_t uCount) -{ - WRAPPER_NO_CONTRACT; - -#ifdef _DEBUG - ValidateAppDomainForHandle(pHandles[0]); -#endif - - // fetch the handle table pointer - HandleTable *pTable = Table(hTable); - - // sanity check the type index - _ASSERTE(uType < pTable->uTypeCount); - -#ifdef GC_PROFILING - { - BEGIN_PIN_PROFILER(CORProfilerTrackGC()); - for (uint32_t i = 0; i < uCount; i++) - g_profControlBlock.pProfInterface->HandleDestroyed((uintptr_t)pHandles[i]); - END_PIN_PROFILER(); - } -#endif - -#if defined(ENABLE_PERF_COUNTERS) || defined(FEATURE_EVENT_TRACE) - g_dwHandles -= uCount; -#endif // ENABLE_PERF_COUNTERS || FEATURE_EVENT_TRACE - - // is this a small number of handles? - if (uCount <= SMALL_ALLOC_COUNT) - { - // yes - free them via the handle cache - TableFreeHandlesToCache(pTable, uType, pHandles, uCount); - return; - } - - // acquire the handle manager lock - { - CrstHolder ch(&pTable->Lock); - - // free the unsorted handles in bulk to the main handle table - TableFreeBulkUnpreparedHandles(pTable, uType, pHandles, uCount); - } -} - /* * HndSetHandleExtraInfo * diff --git a/src/coreclr/src/gc/handletable.h b/src/coreclr/src/gc/handletable.h index ebf8c62..aff9f16 100644 --- a/src/coreclr/src/gc/handletable.h +++ b/src/coreclr/src/gc/handletable.h @@ -75,12 +75,6 @@ void HndDestroyHandle(HHANDLETABLE hTable, uint32_t uType, OBJECTHAND void HndDestroyHandleOfUnknownType(HHANDLETABLE hTable, OBJECTHANDLE handle); /* - * bulk handle allocation and deallocation - */ -uint32_t HndCreateHandles(HHANDLETABLE hTable, uint32_t uType, OBJECTHANDLE *pHandles, uint32_t uCount); -void HndDestroyHandles(HHANDLETABLE hTable, uint32_t uType, const OBJECTHANDLE *pHandles, uint32_t uCount); - -/* * owner data associated with handles */ void HndSetHandleExtraInfo(OBJECTHANDLE handle, uint32_t uType, uintptr_t lExtraInfo); diff --git a/src/coreclr/src/vm/CMakeLists.txt b/src/coreclr/src/vm/CMakeLists.txt index 7d5ea89..adb8409 100644 --- a/src/coreclr/src/vm/CMakeLists.txt +++ b/src/coreclr/src/vm/CMakeLists.txt @@ -70,6 +70,7 @@ set(VM_SOURCES_DAC_AND_WKS_COMMON fptrstubs.cpp frames.cpp gcheaputilities.cpp + gchandleutilities.cpp genericdict.cpp generics.cpp hash.cpp diff --git a/src/coreclr/src/vm/gchandleutilities.cpp b/src/coreclr/src/vm/gchandleutilities.cpp new file mode 100644 index 0000000..7433945 --- /dev/null +++ b/src/coreclr/src/vm/gchandleutilities.cpp @@ -0,0 +1,91 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#include "common.h" +#include "gchandleutilities.h" + +IGCHandleManager* g_pGCHandleManager = nullptr; + +// Debug-only validation for handle. + +void ValidateObjectAndAppDomain(OBJECTREF objRef, ADIndex appDomainIndex) +{ +#ifdef _DEBUG_IMPL + VALIDATEOBJECTREF(objRef); + + AppDomain *domain = SystemDomain::GetAppDomainAtIndex(appDomainIndex); + + // Access to a handle in an unloaded domain is not allowed + assert(domain != nullptr); + assert(!domain->NoAccessToHandleTable()); + +#if CHECK_APP_DOMAIN_LEAKS + if (g_pConfig->AppDomainLeaks() && objRef != NULL) + { + if (appDomainIndex.m_dwIndex) + { + objRef->TryAssignAppDomain(domain); + } + else + { + objRef->TrySetAppDomainAgile(); + } + } +#endif // CHECK_APP_DOMAIN_LEAKS +#endif // _DEBUG_IMPL +} + +void ValidateHandleAssignment(OBJECTHANDLE handle, OBJECTREF objRef) +{ +#ifdef _DEBUG_IMPL + _ASSERTE(handle); + +#ifdef DEBUG_DestroyedHandleValue + // Verify that we are not trying to access a freed handle. + _ASSERTE("Attempt to access destroyed handle." && *(_UNCHECKED_OBJECTREF*)handle != DEBUG_DestroyedHandleValue); +#endif + + IGCHandleManager *mgr = GCHandleUtilities::GetGCHandleManager(); + ADIndex appDomainIndex = ADIndex(reinterpret_cast(mgr->GetHandleContext(handle))); + + AppDomain *unloadingDomain = SystemDomain::AppDomainBeingUnloaded(); + if (unloadingDomain && unloadingDomain->GetIndex() == appDomainIndex && unloadingDomain->NoAccessToHandleTable()) + { + _ASSERTE (!"Access to a handle in unloaded domain is not allowed"); + } + + ValidateObjectAndAppDomain(objRef, appDomainIndex); +#endif // _DEBUG_IMPL +} + +void DiagHandleCreated(OBJECTHANDLE handle, OBJECTREF objRef) +{ +#if defined(ENABLE_PERF_COUNTERS) || defined(FEATURE_EVENT_TRACE) + g_dwHandles++; +#endif // defined(ENABLE_PERF_COUNTERS) || defined(FEATURE_EVENT_TRACE) + +#ifdef GC_PROFILING + BEGIN_PIN_PROFILER(CORProfilerTrackGC()); + g_profControlBlock.pProfInterface->HandleCreated((uintptr_t)handle, (ObjectID)OBJECTREF_TO_UNCHECKED_OBJECTREF(objRef)); + END_PIN_PROFILER(); +#else + UNREFERENCED_PARAMETER(handle); + UNREFERENCED_PARAMETER(objRef); +#endif // GC_PROFILING +} + +void DiagHandleDestroyed(OBJECTHANDLE handle) +{ +#ifdef GC_PROFILING + BEGIN_PIN_PROFILER(CORProfilerTrackGC()); + g_profControlBlock.pProfInterface->HandleDestroyed((uintptr_t)handle); + END_PIN_PROFILER(); +#else + UNREFERENCED_PARAMETER(handle); +#endif // GC_PROFILING + +#if defined(ENABLE_PERF_COUNTERS) || defined(FEATURE_EVENT_TRACE) + g_dwHandles--; +#endif // defined(ENABLE_PERF_COUNTERS) || defined(FEATURE_EVENT_TRACE) +} diff --git a/src/coreclr/src/vm/gchandleutilities.h b/src/coreclr/src/vm/gchandleutilities.h index 665c1da..762a37b 100644 --- a/src/coreclr/src/vm/gchandleutilities.h +++ b/src/coreclr/src/vm/gchandleutilities.h @@ -32,6 +32,8 @@ private: void ValidateObjectAndAppDomain(OBJECTREF objRef, ADIndex appDomainIndex); void ValidateHandleAssignment(OBJECTHANDLE handle, OBJECTREF objRef); +void DiagHandleCreated(OBJECTHANDLE handle, OBJECTREF object); +void DiagHandleDestroyed(OBJECTHANDLE handle); // Given a handle, returns an OBJECTREF for the object it refers to. inline OBJECTREF ObjectFromHandle(OBJECTHANDLE handle) @@ -67,104 +69,61 @@ inline BOOL ObjectHandleIsNull(OBJECTHANDLE handle) #ifndef DACCESS_COMPILE // Handle creation convenience functions - -inline OBJECTHANDLE CreateHandle(IGCHandleStore* store, OBJECTREF object) +inline OBJECTHANDLE CreateHandleCommon(IGCHandleStore* store, OBJECTREF object, HandleType type) { - OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), HNDTYPE_DEFAULT); + OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), type); if (!hnd) { COMPlusThrowOM(); } + DiagHandleCreated(hnd, object); return hnd; } -inline OBJECTHANDLE CreateWeakHandle(IGCHandleStore* store, OBJECTREF object) +inline OBJECTHANDLE CreateHandle(IGCHandleStore* store, OBJECTREF object) { - OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), HNDTYPE_WEAK_DEFAULT); - if (!hnd) - { - COMPlusThrowOM(); - } + return CreateHandleCommon(store, object, HNDTYPE_DEFAULT); +} - return hnd; +inline OBJECTHANDLE CreateWeakHandle(IGCHandleStore* store, OBJECTREF object) +{ + return CreateHandleCommon(store, object, HNDTYPE_WEAK_DEFAULT); } inline OBJECTHANDLE CreateShortWeakHandle(IGCHandleStore* store, OBJECTREF object) { - OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), HNDTYPE_WEAK_SHORT); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateHandleCommon(store, object, HNDTYPE_WEAK_SHORT); } inline OBJECTHANDLE CreateLongWeakHandle(IGCHandleStore* store, OBJECTREF object) { - OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), HNDTYPE_WEAK_LONG); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateHandleCommon(store, object, HNDTYPE_WEAK_LONG); } inline OBJECTHANDLE CreateStrongHandle(IGCHandleStore* store, OBJECTREF object) { - OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), HNDTYPE_STRONG); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateHandleCommon(store, object, HNDTYPE_STRONG); } inline OBJECTHANDLE CreatePinningHandle(IGCHandleStore* store, OBJECTREF object) { - OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), HNDTYPE_PINNED); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateHandleCommon(store, object, HNDTYPE_PINNED); } inline OBJECTHANDLE CreateAsyncPinningHandle(IGCHandleStore* store, OBJECTREF object) { - OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), HNDTYPE_ASYNCPINNED); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateHandleCommon(store, object, HNDTYPE_ASYNCPINNED); } inline OBJECTHANDLE CreateRefcountedHandle(IGCHandleStore* store, OBJECTREF object) { - OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), HNDTYPE_REFCOUNTED); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateHandleCommon(store, object, HNDTYPE_REFCOUNTED); } inline OBJECTHANDLE CreateSizedRefHandle(IGCHandleStore* store, OBJECTREF object) { - OBJECTHANDLE hnd = store->CreateHandleOfType(OBJECTREFToObject(object), HNDTYPE_SIZEDREF); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateHandleCommon(store, object, HNDTYPE_SIZEDREF); } inline OBJECTHANDLE CreateSizedRefHandle(IGCHandleStore* store, OBJECTREF object, int heapToAffinitizeTo) @@ -175,89 +134,58 @@ inline OBJECTHANDLE CreateSizedRefHandle(IGCHandleStore* store, OBJECTREF object COMPlusThrowOM(); } + DiagHandleCreated(hnd, object); return hnd; } // Global handle creation convenience functions - -inline OBJECTHANDLE CreateGlobalHandle(OBJECTREF object) +inline OBJECTHANDLE CreateGlobalHandleCommon(OBJECTREF object, HandleType type) { CONDITIONAL_CONTRACT_VIOLATION(ModeViolation, object == NULL); - OBJECTHANDLE hnd = GCHandleUtilities::GetGCHandleManager()->CreateGlobalHandleOfType(OBJECTREFToObject(object), HNDTYPE_DEFAULT); + OBJECTHANDLE hnd = GCHandleUtilities::GetGCHandleManager()->CreateGlobalHandleOfType(OBJECTREFToObject(object), type); if (!hnd) { COMPlusThrowOM(); } + DiagHandleCreated(hnd, object); return hnd; + } -inline OBJECTHANDLE CreateGlobalWeakHandle(OBJECTREF object) +inline OBJECTHANDLE CreateGlobalHandle(OBJECTREF object) { - OBJECTHANDLE hnd = GCHandleUtilities::GetGCHandleManager()->CreateGlobalHandleOfType(OBJECTREFToObject(object), HNDTYPE_WEAK_DEFAULT); - if (!hnd) - { - COMPlusThrowOM(); - } + return CreateGlobalHandleCommon(object, HNDTYPE_DEFAULT); +} - return hnd; +inline OBJECTHANDLE CreateGlobalWeakHandle(OBJECTREF object) +{ + return CreateGlobalHandleCommon(object, HNDTYPE_WEAK_DEFAULT); } inline OBJECTHANDLE CreateGlobalShortWeakHandle(OBJECTREF object) { - CONDITIONAL_CONTRACT_VIOLATION(ModeViolation, object == NULL); - OBJECTHANDLE hnd = GCHandleUtilities::GetGCHandleManager()->CreateGlobalHandleOfType(OBJECTREFToObject(object), HNDTYPE_WEAK_SHORT); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateGlobalHandleCommon(object, HNDTYPE_WEAK_SHORT); } inline OBJECTHANDLE CreateGlobalLongWeakHandle(OBJECTREF object) { - OBJECTHANDLE hnd = GCHandleUtilities::GetGCHandleManager()->CreateGlobalHandleOfType(OBJECTREFToObject(object), HNDTYPE_WEAK_LONG); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateGlobalHandleCommon(object, HNDTYPE_WEAK_LONG); } inline OBJECTHANDLE CreateGlobalStrongHandle(OBJECTREF object) { - CONDITIONAL_CONTRACT_VIOLATION(ModeViolation, object == NULL); - OBJECTHANDLE hnd = GCHandleUtilities::GetGCHandleManager()->CreateGlobalHandleOfType(OBJECTREFToObject(object), HNDTYPE_STRONG); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateGlobalHandleCommon(object, HNDTYPE_STRONG); } inline OBJECTHANDLE CreateGlobalPinningHandle(OBJECTREF object) { - OBJECTHANDLE hnd = GCHandleUtilities::GetGCHandleManager()->CreateGlobalHandleOfType(OBJECTREFToObject(object), HNDTYPE_PINNED); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateGlobalHandleCommon(object, HNDTYPE_PINNED); } inline OBJECTHANDLE CreateGlobalRefcountedHandle(OBJECTREF object) { - OBJECTHANDLE hnd = GCHandleUtilities::GetGCHandleManager()->CreateGlobalHandleOfType(OBJECTREFToObject(object), HNDTYPE_REFCOUNTED); - if (!hnd) - { - COMPlusThrowOM(); - } - - return hnd; + return CreateGlobalHandleCommon(object, HNDTYPE_REFCOUNTED); } // Special handle creation convenience functions @@ -271,6 +199,7 @@ inline OBJECTHANDLE CreateWinRTWeakHandle(IGCHandleStore* store, OBJECTREF objec COMPlusThrowOM(); } + DiagHandleCreated(hnd, object); return hnd; } #endif // FEATURE_COMINTEROP @@ -284,6 +213,7 @@ inline OBJECTHANDLE CreateVariableHandle(IGCHandleStore* store, OBJECTREF object COMPlusThrowOM(); } + DiagHandleCreated(hnd, object); return hnd; } @@ -316,8 +246,7 @@ inline void ResetOBJECTHANDLE(OBJECTHANDLE handle) } // Handle destruction convenience functions - -inline void DestroyHandle(OBJECTHANDLE handle) +inline void DestroyHandleCommon(OBJECTHANDLE handle, HandleType type) { CONTRACTL { @@ -329,91 +258,98 @@ inline void DestroyHandle(OBJECTHANDLE handle) } CONTRACTL_END; - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_DEFAULT); + DiagHandleDestroyed(handle); + GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, type); +} + +inline void DestroyHandle(OBJECTHANDLE handle) +{ + DestroyHandleCommon(handle, HNDTYPE_DEFAULT); } inline void DestroyWeakHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_WEAK_DEFAULT); + DestroyHandleCommon(handle, HNDTYPE_WEAK_DEFAULT); } inline void DestroyShortWeakHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_WEAK_SHORT); + DestroyHandleCommon(handle, HNDTYPE_WEAK_SHORT); } inline void DestroyLongWeakHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_WEAK_LONG); + DestroyHandleCommon(handle, HNDTYPE_WEAK_LONG); } inline void DestroyStrongHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_STRONG); + DestroyHandleCommon(handle, HNDTYPE_STRONG); } inline void DestroyPinningHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_PINNED); + DestroyHandleCommon(handle, HNDTYPE_PINNED); } inline void DestroyAsyncPinningHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_ASYNCPINNED); + DestroyHandleCommon(handle, HNDTYPE_ASYNCPINNED); } inline void DestroyRefcountedHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_REFCOUNTED); + DestroyHandleCommon(handle, HNDTYPE_REFCOUNTED); } inline void DestroyDependentHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_DEPENDENT); + DestroyHandleCommon(handle, HNDTYPE_DEPENDENT); } inline void DestroyVariableHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_VARIABLE); + DestroyHandleCommon(handle, HNDTYPE_VARIABLE); } inline void DestroyGlobalHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_DEFAULT); + DestroyHandleCommon(handle, HNDTYPE_DEFAULT); } inline void DestroyGlobalWeakHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_WEAK_DEFAULT); + DestroyHandleCommon(handle, HNDTYPE_WEAK_DEFAULT); } inline void DestroyGlobalShortWeakHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_WEAK_SHORT); + DestroyHandleCommon(handle, HNDTYPE_WEAK_SHORT); } inline void DestroyGlobalLongWeakHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_WEAK_LONG); + DestroyHandleCommon(handle, HNDTYPE_WEAK_LONG); } inline void DestroyGlobalStrongHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_STRONG); + DestroyHandleCommon(handle, HNDTYPE_STRONG); } inline void DestroyGlobalPinningHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_PINNED); + DestroyHandleCommon(handle, HNDTYPE_PINNED); } inline void DestroyGlobalRefcountedHandle(OBJECTHANDLE handle) { - GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_REFCOUNTED); + DestroyHandleCommon(handle, HNDTYPE_REFCOUNTED); } inline void DestroyTypedHandle(OBJECTHANDLE handle) { + DiagHandleDestroyed(handle); GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfUnknownType(handle); } @@ -440,6 +376,7 @@ inline void DestroyWinRTWeakHandle(OBJECTHANDLE handle) pWinRTWeakReference->Release(); } + DiagHandleDestroyed(handle); GCHandleUtilities::GetGCHandleManager()->DestroyHandleOfType(handle, HNDTYPE_WEAK_WINRT); } #endif diff --git a/src/coreclr/src/vm/gcheaputilities.cpp b/src/coreclr/src/vm/gcheaputilities.cpp index d0bfda6..3a6a42f 100644 --- a/src/coreclr/src/vm/gcheaputilities.cpp +++ b/src/coreclr/src/vm/gcheaputilities.cpp @@ -25,8 +25,6 @@ uint32_t* g_card_bundle_table = nullptr; // This is the global GC heap, maintained by the VM. GPTR_IMPL(IGCHeap, g_pGCHeap); -IGCHandleManager* g_pGCHandleManager = nullptr; - GcDacVars g_gc_dac_vars; GPTR_IMPL(GcDacVars, g_gcDacGlobals); @@ -38,55 +36,3 @@ bool g_sw_ww_enabled_for_gc_heap = false; #endif // FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP gc_alloc_context g_global_alloc_context = {}; - -// Debug-only validation for handle. - -void ValidateObjectAndAppDomain(OBJECTREF objRef, ADIndex appDomainIndex) -{ -#ifdef _DEBUG_IMPL - VALIDATEOBJECTREF(objRef); - - AppDomain *domain = SystemDomain::GetAppDomainAtIndex(appDomainIndex); - - // Access to a handle in an unloaded domain is not allowed - assert(domain != nullptr); - assert(!domain->NoAccessToHandleTable()); - -#if CHECK_APP_DOMAIN_LEAKS - if (g_pConfig->AppDomainLeaks() && objRef != NULL) - { - if (appDomainIndex.m_dwIndex) - { - objRef->TryAssignAppDomain(domain); - } - else - { - objRef->TrySetAppDomainAgile(); - } - } -#endif // CHECK_APP_DOMAIN_LEAKS -#endif // _DEBUG_IMPL -} - -void ValidateHandleAssignment(OBJECTHANDLE handle, OBJECTREF objRef) -{ -#ifdef _DEBUG_IMPL - _ASSERTE(handle); - -#ifdef DEBUG_DestroyedHandleValue - // Verify that we are not trying to access a freed handle. - _ASSERTE("Attempt to access destroyed handle." && *(_UNCHECKED_OBJECTREF*)handle != DEBUG_DestroyedHandleValue); -#endif - - IGCHandleManager *mgr = GCHandleUtilities::GetGCHandleManager(); - ADIndex appDomainIndex = ADIndex(reinterpret_cast(mgr->GetHandleContext(handle))); - - AppDomain *unloadingDomain = SystemDomain::AppDomainBeingUnloaded(); - if (unloadingDomain && unloadingDomain->GetIndex() == appDomainIndex && unloadingDomain->NoAccessToHandleTable()) - { - _ASSERTE (!"Access to a handle in unloaded domain is not allowed"); - } - - ValidateObjectAndAppDomain(objRef, appDomainIndex); -#endif // _DEBUG_IMPL -} -- 2.7.4