mirror of
https://github.com/OPM/ResInsight.git
synced 2026-11-24 14:58:13 -06:00
#14183 Fix crash in AABB cell search tree from unsynchronized lazy build
RigMainGrid::findIntersectingCells is const and lazily builds the mutable m_cellSearchTree without synchronization, yet it is called from many multi-threaded (OpenMP) query paths. When two threads race the isNull check, both build a new tree and the cvf::ref reassignment deletes the first tree while it is still being built, leading to a use-after-free and a segfault deep in AABBTree::buildTree (BoundingBox::addValid reading a dangling leaf box). Serialize building of the cell search tree with a mutex, both in computeCachedData (which nulls and rebuilds the tree) and in the lazy build in findIntersectingCells. The query also keeps a local reference to the tree so it stays alive if another thread rebuilds m_cellSearchTree during the read-only intersection query.
This commit is contained in:
1 parent
6a5f19d5a0
commit
f1218e527f
2 files changed
+24
-6
No files matched your search
@@ -273,8 +273,14 @@ void RigMainGrid::computeCachedData( std::string* aabbTreeInfo )
|
||||
initAllSubGridsParentGridPointer();
|
||||
initAllSubCellsMainGridCellIndex();
|
||||
|
||||
m_cellSearchTree = nullptr;
|
||||
doBuildCellSearchTree( aabbTreeInfo );
|
||||
{
|
||||
// Serialize (re)building of the cell search tree, as it may be accessed concurrently from
|
||||
// multi-threaded queries (e.g. findIntersectingCells). Building without this guard can cause
|
||||
// a use-after-free / data race on the lazily built tree.
|
||||
std::scoped_lock<std::mutex> lock( m_cellSearchTreeMutex );
|
||||
m_cellSearchTree = nullptr;
|
||||
doBuildCellSearchTree( aabbTreeInfo );
|
||||
}
|
||||
|
||||
computeBoundingBox();
|
||||
}
|
||||
@@ -839,15 +845,25 @@ std::tuple<QString, double, cvf::StructGridInterface::FaceType> RigMainGrid::min
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::vector<size_t> RigMainGrid::findIntersectingCells( const cvf::BoundingBox& inputBB ) const
|
||||
{
|
||||
if ( m_cellSearchTree.isNull() )
|
||||
cvf::ref<cvf::BoundingBoxTree> searchTree;
|
||||
{
|
||||
doBuildCellSearchTree();
|
||||
// Lazily build the cell search tree under a lock to avoid a use-after-free / data race when
|
||||
// findIntersectingCells is called concurrently from multiple threads (e.g. OpenMP regions).
|
||||
std::scoped_lock<std::mutex> lock( m_cellSearchTreeMutex );
|
||||
if ( m_cellSearchTree.isNull() )
|
||||
{
|
||||
doBuildCellSearchTree();
|
||||
}
|
||||
|
||||
// Keep a local reference so the tree stays alive if another thread rebuilds m_cellSearchTree
|
||||
// while this thread performs the read-only intersection query below.
|
||||
searchTree = m_cellSearchTree;
|
||||
}
|
||||
|
||||
std::vector<size_t> cellIndices;
|
||||
if ( m_cellSearchTree.notNull() )
|
||||
if ( searchTree.notNull() )
|
||||
{
|
||||
m_cellSearchTree->findIntersections( inputBB, &cellIndices );
|
||||
searchTree->findIntersections( inputBB, &cellIndices );
|
||||
}
|
||||
return cellIndices;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
class RigActiveCellInfo;
|
||||
@@ -169,4 +170,5 @@ private:
|
||||
mutable bool m_isFaceNormalsOutwards;
|
||||
mutable bool m_isFaceNormalsOutwardsComputed;
|
||||
mutable cvf::ref<cvf::BoundingBoxTree> m_cellSearchTree;
|
||||
mutable std::mutex m_cellSearchTreeMutex;
|
||||
};
|
||||
Reference in new issue
Block a user