Skip to content

[clang][dataflow] Process terminator condition within transferCFGBlock(). #77750

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

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,12 @@ class TerminatorVisitor

private:
TerminatorVisitorRetTy extendFlowCondition(const Expr &Cond) {
// The terminator sub-expression might not be evaluated.
if (Env.getValue(Cond) == nullptr)
transfer(StmtToEnv, Cond, Env);

auto *Val = Env.get<BoolValue>(Cond);
// Value merging depends on flow conditions from different environments
// being mutually exclusive -- that is, they cannot both be true in their
// entirety (even if they may share some clauses). So, we need *some* value
// for the condition expression, even if just an atom.
if (Val == nullptr) {
Val = &Env.makeAtomicBoolValue();
Env.setValue(Cond, *Val);
}
// In transferCFGBlock(), we ensure that we always have a `Value` for the
// terminator condition, so assert this.
// We consciously assert ourselves instead of asserting via `cast()` so
// that we get a more meaningful line number if the assertion fails.
assert(Val != nullptr);

bool ConditionValue = true;
// The condition must be inverted for the successor that encompasses the
Expand Down Expand Up @@ -489,6 +482,31 @@ transferCFGBlock(const CFGBlock &Block, AnalysisContext &AC,
}
AC.Log.recordState(State);
}

// If we have a terminator, evaluate its condition.
// This `Expr` may not appear as a `CFGElement` anywhere else, and it's
// important that we evaluate it here (rather than while processing the
// terminator) so that we put the corresponding value in the right
// environment.
if (const Expr *TerminatorCond =
dyn_cast_or_null<Expr>(Block.getTerminatorCondition())) {
if (State.Env.getValue(*TerminatorCond) == nullptr)
// FIXME: This only runs the builtin transfer, not the analysis-specific
// transfer. Fixing this isn't trivial, as the analysis-specific transfer
// takes a `CFGElement` as input, but some expressions only show up as a
// terminator condition, but not as a `CFGElement`. The condition of an if
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CFGElement is cheap to construct. Can you just wrap the expression and pass it on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that's an idea!

I'd still like to leave this to a future patch, as it's a further change in behavior. Note that the code we had so far also only ran the builtin transfer; this FIXME just documents this previously undocumented deficiency, so we're not regressing in any way.

I'd like to make this change separately so that we can separate out any unwanted side-effects of the two changes.

// statement is one such example.
transfer(StmtToEnvMap(AC.CFCtx, AC.BlockStates), *TerminatorCond,
State.Env);

// If the transfer function didn't produce a value, create an atom so that
// we have *some* value for the condition expression. This ensures that
// when we extend the flow condition, it actually changes.
if (State.Env.getValue(*TerminatorCond) == nullptr)
State.Env.setValue(*TerminatorCond, State.Env.makeAtomicBoolValue());
AC.Log.recordState(State);
}

return State;
}

Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Analysis/FlowSensitive/LoggerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ recordState(Elements=1, Branches=0, Joins=0)
enterElement(b (ImplicitCastExpr, LValueToRValue, _Bool))
transfer()
recordState(Elements=2, Branches=0, Joins=0)
recordState(Elements=2, Branches=0, Joins=0)

enterBlock(3, false)
transferBranch(0)
Expand Down
31 changes: 31 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6408,4 +6408,35 @@ TEST(TransferTest, DifferentReferenceLocInJoin) {
});
}

// This test verifies correct modeling of a relational dependency that goes
// through unmodeled functions (the simple `cond()` in this case).
TEST(TransferTest, ConditionalRelation) {
std::string Code = R"(
bool cond();
void target() {
bool a = true;
bool b = true;
if (cond()) {
a = false;
if (cond()) {
b = false;
}
}
(void)0;
// [[p]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
auto &A = Env.arena();
auto &VarA = getValueForDecl<BoolValue>(ASTCtx, Env, "a").formula();
auto &VarB = getValueForDecl<BoolValue>(ASTCtx, Env, "b").formula();

EXPECT_FALSE(Env.allows(A.makeAnd(VarA, A.makeNot(VarB))));
});
}

} // namespace