-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flang][OpenMP] Fix use-after-free in OMPFunctionFiltering #84373
Conversation
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.
@llvm/pr-subscribers-flang-openmp @llvm/pr-subscribers-flang-fir-hlfir Author: Krzysztof Parzyszek (kparzysz) ChangesErasing 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:
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
|
986f335
to
4bf5b95
Compare
Thank you for noticing this issue. I think we can simplify the fix a bit, though. The docs for |
Done, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM
When walking over functions (in pre-order), if the function being visited needs to be erased, skip visiting its regions.
This was detected by address sanitizer.