Skip to content

Commit

Permalink
[clang][dataflow] Don't crash when BlockToState is called from unreac…
Browse files Browse the repository at this point in the history
…hable path (#65732)

When we call `getEnvironment`, `BlockToState[BlockId]` for the block can
return null even if CFCtx.isBlockReachable(B) returns true if it is
called from a particular block that is marked unreachable to the block.
  • Loading branch information
kinu committed Sep 8, 2023
1 parent df45557 commit 8e1d2f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
15 changes: 14 additions & 1 deletion clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,20 @@ const Environment *StmtToEnvMap::getEnvironment(const Stmt &S) const {
if (!CFCtx.isBlockReachable(*BlockIt->getSecond()))
return nullptr;
const auto &State = BlockToState[BlockIt->getSecond()->getBlockID()];
assert(State);
if (!(State)) {
LLVM_DEBUG({
// State can be null when this block is unreachable from the block that
// called this method.
bool hasUnreachableEdgeFromPred = false;
for (auto B : BlockIt->getSecond()->preds())
if (!B) {
hasUnreachableEdgeFromPred = true;
break;
}
assert(hasUnreachableEdgeFromPred);
});
return nullptr;
}
return &State->Env;
}

Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5853,4 +5853,24 @@ TEST(TransferTest, AnonymousStructWithReferenceField) {
});
}

TEST(TransferTest, EvaluateBlockWithUnreachablePreds) {
// This is a crash repro.
// `false` block may not have been processed when we try to evalute the `||`
// after visiting `true`, because it is not necessary (and therefore the edge
// is marked unreachable). Trying to get the analysis state via
// `getEnvironment` for the subexpression still should not crash.
std::string Code = R"(
int cast(int i) {
if ((i < 0 && true) || false) {
return 0;
}
return 0;
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {});
}

} // namespace

0 comments on commit 8e1d2f2

Please sign in to comment.