From f1218e527f18b40c8ce0df2ef786201bf4f3358a Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Tue, 9 Jun 2026 11:36:54 +0200 Subject: [PATCH] #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. --- .../ReservoirDataModel/RigMainGrid.cpp | 28 +++++++++++++++---- .../ReservoirDataModel/RigMainGrid.h | 2 ++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp b/ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp index 9113bce090..b8d83bed0f 100644 --- a/ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp +++ b/ApplicationLibCode/ReservoirDataModel/RigMainGrid.cpp @@ -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 lock( m_cellSearchTreeMutex ); + m_cellSearchTree = nullptr; + doBuildCellSearchTree( aabbTreeInfo ); + } computeBoundingBox(); } @@ -839,15 +845,25 @@ std::tuple RigMainGrid::min //-------------------------------------------------------------------------------------------------- std::vector RigMainGrid::findIntersectingCells( const cvf::BoundingBox& inputBB ) const { - if ( m_cellSearchTree.isNull() ) + cvf::ref 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 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 cellIndices; - if ( m_cellSearchTree.notNull() ) + if ( searchTree.notNull() ) { - m_cellSearchTree->findIntersections( inputBB, &cellIndices ); + searchTree->findIntersections( inputBB, &cellIndices ); } return cellIndices; } diff --git a/ApplicationLibCode/ReservoirDataModel/RigMainGrid.h b/ApplicationLibCode/ReservoirDataModel/RigMainGrid.h index dc401f997e..e7eca8ec7f 100644 --- a/ApplicationLibCode/ReservoirDataModel/RigMainGrid.h +++ b/ApplicationLibCode/ReservoirDataModel/RigMainGrid.h @@ -29,6 +29,7 @@ #include +#include #include class RigActiveCellInfo; @@ -169,4 +170,5 @@ private: mutable bool m_isFaceNormalsOutwards; mutable bool m_isFaceNormalsOutwardsComputed; mutable cvf::ref m_cellSearchTree; + mutable std::mutex m_cellSearchTreeMutex; };