From 1c5f6bbf4340b55c96179dc28be509a3128a9596 Mon Sep 17 00:00:00 2001 From: lhu Date: Mon, 20 Jan 2025 12:17:41 -0500 Subject: [PATCH] Verify off heap entry during update and free offheap array Verify the consistency (dataAddr, size and related proxy array object) of off heap Entry before freeing the off heap array or updating related proxy object. Signed-off-by: lhu --- runtime/gc_check/CheckEngine.cpp | 13 +++++++++++-- runtime/gc_glue_java/ArrayletObjectModel.hpp | 18 +++++++++--------- runtime/gc_vlhgc/CopyForwardScheme.cpp | 5 ++--- runtime/gc_vlhgc/GlobalMarkingScheme.cpp | 3 ++- runtime/gc_vlhgc/WriteOnceCompactor.cpp | 2 +- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/runtime/gc_check/CheckEngine.cpp b/runtime/gc_check/CheckEngine.cpp index f0a11217e2f..533c5bc8cea 100644 --- a/runtime/gc_check/CheckEngine.cpp +++ b/runtime/gc_check/CheckEngine.cpp @@ -468,10 +468,19 @@ GC_CheckEngine::checkJ9Object(J9JavaVM *javaVM, J9Object* objectPtr, J9MM_Iterat if (extensions->isVirtualLargeObjectHeapEnabled && extensions->objectModel.isIndexable(objectPtr)) { /* Check that the indexable object has the correct data address pointer */ void *dataAddr = extensions->indexableObjectModel.getDataAddrForIndexableObject((J9IndexableObject *)objectPtr); - bool isValidDataAddr = extensions->largeObjectVirtualMemory->getSparseDataPool()->isValidDataPtr(dataAddr); - if (!isValidDataAddr && !extensions->indexableObjectModel.isValidDataAddr((J9IndexableObject *)objectPtr, dataAddr, isValidDataAddr)) { + bool isDataAddrForOffHeapObject = false; + bool isValidDataAddr = extensions->indexableObjectModel.isValidDataAddr((J9IndexableObject *)objectPtr, dataAddr, &isDataAddrForOffHeapObject); + if (isDataAddrForOffHeapObject) { + isValidDataAddr = extensions->largeObjectVirtualMemory->getSparseDataPool()->isValidDataPtr(dataAddr, objectPtr, extensions->indexableObjectModel.getDataSizeInBytes((J9IndexableObject *)objectPtr)); + } + if (!isValidDataAddr) { return J9MODRON_GCCHK_RC_INVALID_INDEXABLE_DATA_ADDRESS; } +// +// bool isValidDataAddr = extensions->largeObjectVirtualMemory->getSparseDataPool()->isValidDataPtr(dataAddr); +// if (!isValidDataAddr && !extensions->indexableObjectModel.isValidDataAddr((J9IndexableObject *)objectPtr, dataAddr, isValidDataAddr)) { +// return J9MODRON_GCCHK_RC_INVALID_INDEXABLE_DATA_ADDRESS; +// } } #endif /* defined(J9VM_ENV_DATA64) */ diff --git a/runtime/gc_glue_java/ArrayletObjectModel.hpp b/runtime/gc_glue_java/ArrayletObjectModel.hpp index 7203e9694d3..fceb0f4d0b8 100644 --- a/runtime/gc_glue_java/ArrayletObjectModel.hpp +++ b/runtime/gc_glue_java/ArrayletObjectModel.hpp @@ -947,17 +947,17 @@ class GC_ArrayletObjectModel : public GC_ArrayletObjectModelBase * Checks that the dataAddr field of the indexable object is correct. * this method is supposed to be called only if offheap is enabled. * - * @param arrayPtr Pointer to the indexable object - * @param isValidDataAddrForOffHeapObject Boolean to determine whether the given indexable object is off heap - * @return if the dataAddr field of the indexable object is correct + * @param arrayPtr[in] Pointer to the indexable object + * @param isDataAddrForOffHeapObject[out] set true if the given indexable object is off heap + * @return if the dataAddr field of the indexable object is correct((not heap object case), return false if indexable object is off heap */ MMINLINE bool - isValidDataAddr(J9IndexableObject *arrayPtr, bool isValidDataAddrForOffHeapObject) + isValidDataAddr(J9IndexableObject *arrayPtr, bool *isDataAddrForOffHeapObject) { bool isValidDataAddress = true; if (_isIndexableDataAddrPresent) { void *dataAddr = getDataAddrForIndexableObject(arrayPtr); - isValidDataAddress = isValidDataAddr(arrayPtr, dataAddr, isValidDataAddrForOffHeapObject); + isValidDataAddress = isValidDataAddr(arrayPtr, dataAddr, isDataAddrForOffHeapObject); } return isValidDataAddress; } @@ -967,11 +967,11 @@ class GC_ArrayletObjectModel : public GC_ArrayletObjectModelBase * this method is supposed to be called only if offheap is enabled * * @param arrayPtr Pointer to the indexable object - * @param isValidDataAddrForOffHeapObject Boolean to determine whether the given indexable object is off heap - * @return if the dataAddr field of the indexable object is correct + * @param isDataAddrForOffHeapObject[out] set true if the given indexable object is off heap + * @return if the dataAddr field of the indexable object is correct(not heap object case), return false if indexable object is off heap */ MMINLINE bool - isValidDataAddr(J9IndexableObject *arrayPtr, void *dataAddr, bool isValidDataAddrForOffHeapObject) + isValidDataAddr(J9IndexableObject *arrayPtr, void *dataAddr, bool *isDataAddrForOffHeapObject) { bool isValidDataAddress = false; uintptr_t dataSizeInBytes = getDataSizeInBytes(arrayPtr); @@ -981,7 +981,7 @@ class GC_ArrayletObjectModel : public GC_ArrayletObjectModelBase } else if (dataSizeInBytes < _omrVM->_arrayletLeafSize) { isValidDataAddress = (dataAddr == (void *)((uintptr_t)arrayPtr + contiguousIndexableHeaderSize())); } else { - isValidDataAddress = isValidDataAddrForOffHeapObject; + *isDataAddrForOffHeapObject = true; } return isValidDataAddress; diff --git a/runtime/gc_vlhgc/CopyForwardScheme.cpp b/runtime/gc_vlhgc/CopyForwardScheme.cpp index f87df16ed88..e1bd90f2f7c 100644 --- a/runtime/gc_vlhgc/CopyForwardScheme.cpp +++ b/runtime/gc_vlhgc/CopyForwardScheme.cpp @@ -4096,7 +4096,7 @@ class MM_CopyForwardSchemeRootClearer : public MM_RootScanner Assert_MM_mustBeClass(_extensions->objectModel.getPreservedClass(&forwardedHeader)); env->_copyForwardStats._offHeapRegionsCleared += 1; void *dataAddr = _extensions->indexableObjectModel.getDataAddrForContiguous((J9IndexableObject *)objectPtr); - _extensions->largeObjectVirtualMemory->freeSparseRegionAndUnmapFromHeapObject(_env, dataAddr); + _extensions->largeObjectVirtualMemory->freeSparseRegionAndUnmapFromHeapObject(_env, dataAddr, objectPtr, _extensions->indexableObjectModel.getDataSizeInBytes((J9IndexableObject *)objectPtr)); *sparseHeapAllocation = false; } else { void *dataAddr = _extensions->indexableObjectModel.getDataAddrForContiguous((J9IndexableObject *)fwdOjectPtr); @@ -4104,8 +4104,7 @@ class MM_CopyForwardSchemeRootClearer : public MM_RootScanner /* There might be the case that GC finds a floating arraylet, which was a result of an allocation * failure (reason why this GC cycle is happening). */ - _extensions->largeObjectVirtualMemory->updateSparseDataEntryAfterObjectHasMoved(dataAddr, fwdOjectPtr); - } + _extensions->largeObjectVirtualMemory->updateSparseDataEntryAfterObjectHasMoved(dataAddr, objectPtr, _extensions->indexableObjectModel.getDataSizeInBytes((J9IndexableObject *)fwdOjectPtr), fwdOjectPtr); } } } } diff --git a/runtime/gc_vlhgc/GlobalMarkingScheme.cpp b/runtime/gc_vlhgc/GlobalMarkingScheme.cpp index b30b8e35426..336a04330ca 100644 --- a/runtime/gc_vlhgc/GlobalMarkingScheme.cpp +++ b/runtime/gc_vlhgc/GlobalMarkingScheme.cpp @@ -1378,7 +1378,8 @@ class MM_GlobalMarkingSchemeRootClearer : public MM_RootScanner env->_markVLHGCStats._offHeapRegionsCleared += 1; void *dataAddr = _extensions->indexableObjectModel.getDataAddrForContiguous((J9IndexableObject *)objectPtr); if (NULL != dataAddr) { - _extensions->largeObjectVirtualMemory->freeSparseRegionAndUnmapFromHeapObject(_env, dataAddr); + _extensions->largeObjectVirtualMemory->freeSparseRegionAndUnmapFromHeapObject(_env, dataAddr, objectPtr, _extensions->indexableObjectModel.getDataSizeInBytes((J9IndexableObject *)objectPtr)); + *sparseHeapAllocation = false; } } diff --git a/runtime/gc_vlhgc/WriteOnceCompactor.cpp b/runtime/gc_vlhgc/WriteOnceCompactor.cpp index 83e0af2ae89..29416892bf9 100644 --- a/runtime/gc_vlhgc/WriteOnceCompactor.cpp +++ b/runtime/gc_vlhgc/WriteOnceCompactor.cpp @@ -1697,7 +1697,7 @@ class MM_WriteOnceCompactFixupRoots : public MM_RootScanner { /* There might be the case that GC finds a floating arraylet, which was a result of an allocation * failure (reason why this GC cycle is happening). */ - _extensions->largeObjectVirtualMemory->updateSparseDataEntryAfterObjectHasMoved(dataAddr, fwdOjectPtr); + _extensions->largeObjectVirtualMemory->updateSparseDataEntryAfterObjectHasMoved(dataAddr, objectPtr, _extensions->indexableObjectModel.getDataSizeInBytes((J9IndexableObject *)fwdOjectPtr), fwdOjectPtr); } } }