Skip to content

Commit 4bf5b95

Browse files
committed
[flang][OpenMP] Fix use-after-free in OMPFunctionFiltering
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.
1 parent 954f891 commit 4bf5b95

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

flang/lib/Optimizer/Transforms/OMPFunctionFiltering.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class OMPFunctionFilteringPass
4040
if (!op || !op.getIsTargetDevice())
4141
return;
4242

43+
llvm::SmallVector<func::FuncOp> removedFuncs;
44+
4345
op->walk<WalkOrder::PreOrder>([&](func::FuncOp funcOp) {
4446
// Do not filter functions with target regions inside, because they have
4547
// to be available for both host and device so that regular and reverse
@@ -80,12 +82,14 @@ class OMPFunctionFilteringPass
8082
callOp->erase();
8183
}
8284
if (!hasTargetRegion)
83-
funcOp.erase();
85+
removedFuncs.push_back(funcOp);
8486
else if (declareTargetOp)
8587
declareTargetOp.setDeclareTarget(declareType,
8688
omp::DeclareTargetCaptureClause::to);
8789
}
8890
});
91+
for (func::FuncOp f : removedFuncs)
92+
f.erase();
8993
}
9094
};
9195
} // namespace

0 commit comments

Comments
 (0)