Skip to content

Commit 44fa31f

Browse files
author
Gabor Marton
committed
[Analyzer][solver] Fix inconsistent equivalence class data
https://bugs.llvm.org/show_bug.cgi?id=51109 When we merged two classes, `*this` became an obsolete representation of the new `State`. This is b/c the member relations had changed during the previous merge of another member of the same class in a way that `*this` had no longer any members. (`mergeImpl` might keep the member relations to `Other` and could dissolve `*this`.) Differential Revision: https://reviews.llvm.org/D106285
1 parent 24ffb98 commit 44fa31f

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,10 @@ class EquivalenceClass : public llvm::FoldingSetNode {
600600
areEqual(ProgramStateRef State, SymbolRef First, SymbolRef Second);
601601

602602
/// Iterate over all symbols and try to simplify them.
603-
LLVM_NODISCARD ProgramStateRef simplify(SValBuilder &SVB,
604-
RangeSet::Factory &F,
605-
ProgramStateRef State);
603+
LLVM_NODISCARD static inline ProgramStateRef simplify(SValBuilder &SVB,
604+
RangeSet::Factory &F,
605+
ProgramStateRef State,
606+
EquivalenceClass Class);
606607

607608
void dumpToStream(ProgramStateRef State, raw_ostream &os) const;
608609
LLVM_DUMP_METHOD void dump(ProgramStateRef State) const {
@@ -1696,7 +1697,7 @@ bool ConstraintAssignor::assignSymExprToConst(const SymExpr *Sym,
16961697
ClassMembersTy Members = State->get<ClassMembers>();
16971698
for (std::pair<EquivalenceClass, SymbolSet> ClassToSymbolSet : Members) {
16981699
EquivalenceClass Class = ClassToSymbolSet.first;
1699-
State = Class.simplify(Builder, RangeFactory, State);
1700+
State = EquivalenceClass::simplify(Builder, RangeFactory, State, Class);
17001701
if (!State)
17011702
return false;
17021703
SimplifiedClasses.insert(Class);
@@ -1710,7 +1711,7 @@ bool ConstraintAssignor::assignSymExprToConst(const SymExpr *Sym,
17101711
EquivalenceClass Class = ClassConstraint.first;
17111712
if (SimplifiedClasses.count(Class)) // Already simplified.
17121713
continue;
1713-
State = Class.simplify(Builder, RangeFactory, State);
1714+
State = EquivalenceClass::simplify(Builder, RangeFactory, State, Class);
17141715
if (!State)
17151716
return false;
17161717
}
@@ -2090,18 +2091,17 @@ inline Optional<bool> EquivalenceClass::areEqual(ProgramStateRef State,
20902091
// class to this class. This way, we simplify not just the symbols but the
20912092
// classes as well: we strive to keep the number of the classes to be the
20922093
// absolute minimum.
2093-
LLVM_NODISCARD ProgramStateRef EquivalenceClass::simplify(
2094-
SValBuilder &SVB, RangeSet::Factory &F, ProgramStateRef State) {
2095-
SymbolSet ClassMembers = getClassMembers(State);
2094+
LLVM_NODISCARD ProgramStateRef
2095+
EquivalenceClass::simplify(SValBuilder &SVB, RangeSet::Factory &F,
2096+
ProgramStateRef State, EquivalenceClass Class) {
2097+
SymbolSet ClassMembers = Class.getClassMembers(State);
20962098
for (const SymbolRef &MemberSym : ClassMembers) {
20972099
SymbolRef SimplifiedMemberSym = ento::simplify(State, MemberSym);
20982100
if (SimplifiedMemberSym && MemberSym != SimplifiedMemberSym) {
2099-
EquivalenceClass ClassOfSimplifiedSym =
2100-
EquivalenceClass::find(State, SimplifiedMemberSym);
21012101
// The simplified symbol should be the member of the original Class,
21022102
// however, it might be in another existing class at the moment. We
21032103
// have to merge these classes.
2104-
State = merge(F, State, ClassOfSimplifiedSym);
2104+
State = merge(F, State, MemberSym, SimplifiedMemberSym);
21052105
if (!State)
21062106
return nullptr;
21072107
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_analyze_cc1 %s \
2+
// RUN: -analyzer-checker=core \
3+
// RUN: -analyzer-checker=debug.ExprInspection \
4+
// RUN: -verify
5+
6+
// Here, we test that symbol simplification in the solver does not produce any
7+
// crashes.
8+
// https://bugs.llvm.org/show_bug.cgi?id=51109
9+
10+
// expected-no-diagnostics
11+
12+
int a, b, c, d;
13+
void f() {
14+
a = -1;
15+
d = b * a;
16+
a = d / c;
17+
if (a < 7 / b)
18+
return;
19+
if (d *a / c < 7 / b)
20+
return;
21+
if (b == 1 && c == -1)
22+
return;
23+
}

0 commit comments

Comments
 (0)