[flang-commits] [flang] [flang][OpenMP] Fix use-after-free in OMPFunctionFiltering (PR #84373)

via flang-commits flang-commits at lists.llvm.org
Thu Mar 7 12:14:24 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

Author: Krzysztof Parzyszek (kparzysz)

<details>
<summary>Changes</summary>

Erasing the element of the range that it being iterated on can invalidate the range. Instead of erasing function as we see them, store them in a separate list, then erase them after the range has been traversed.

This was detected by address sanitizer.

---
Full diff: https://github.com/llvm/llvm-project/pull/84373.diff


1 Files Affected:

- (modified) flang/lib/Optimizer/Transforms/OMPFunctionFiltering.cpp (+5-1) 


``````````diff
diff --git a/flang/lib/Optimizer/Transforms/OMPFunctionFiltering.cpp b/flang/lib/Optimizer/Transforms/OMPFunctionFiltering.cpp
index 466bf53e8dbd60..f8bede88293ff5 100644
--- a/flang/lib/Optimizer/Transforms/OMPFunctionFiltering.cpp
+++ b/flang/lib/Optimizer/Transforms/OMPFunctionFiltering.cpp
@@ -40,6 +40,8 @@ class OMPFunctionFilteringPass
     if (!op || !op.getIsTargetDevice())
       return;
 
+    llvm::SmallVector<func::FuncOp> removedFuncs;
+
     op->walk<WalkOrder::PreOrder>([&](func::FuncOp funcOp) {
       // Do not filter functions with target regions inside, because they have
       // to be available for both host and device so that regular and reverse
@@ -80,12 +82,14 @@ class OMPFunctionFilteringPass
           callOp->erase();
         }
         if (!hasTargetRegion)
-          funcOp.erase();
+          removedFuncs.push_back(funcOp);
         else if (declareTargetOp)
           declareTargetOp.setDeclareTarget(declareType,
                                            omp::DeclareTargetCaptureClause::to);
       }
     });
+    for (func::FuncOp f : removedFuncs)
+      f.erase();
   }
 };
 } // namespace

``````````

</details>


https://github.com/llvm/llvm-project/pull/84373


More information about the flang-commits mailing list