Skip to content

Commit a6816b9

Browse files
author
Balazs Benics
committed
[analyzer][solver] Fix assertion on (NonLoc, Op, Loc) expressions
Previously, the `SValBuilder` could not encounter expressions of the following kind: NonLoc OP Loc Loc OP NonLoc Where the `Op` is other than `BO_Add`. As of now, due to the smarter simplification and the fixedpoint iteration, it turns out we can. It can happen if the `Loc` was perfectly constrained to a concrete value (`nonloc::ConcreteInt`), thus the simplifier can do constant-folding in these cases as well. Unfortunately, this could cause assertion failures, since we assumed that the operator must be `BO_Add`, causing a crash. --- In the patch, I decided to preserve the original behavior (aka. swap the operands (if the operator is commutative), but if the `RHS` was a `loc::ConcreteInt` call `evalBinOpNN()`. I think this interpretation of the arithmetic expression is closer to reality. I also tried naively introducing a separate handler for `loc::ConcreteInt` RHS, before doing handling the more generic `Loc` RHS case. However, it broke the `zoo1backwards()` test in the `nullptr.cpp` file. This highlighted for me the importance to preserve the original behavior for the `BO_Add` at least. PS: Sorry for introducing yet another branch into this `evalBinOpXX` madness. I've got a couple of ideas about refactoring these. We'll see if I can get to it. The test file demonstrates the issue and makes sure nothing similar happens. The `no-crash` annotated lines show, where we crashed before applying this patch. Reviewed By: martong Differential Revision: https://reviews.llvm.org/D115149
1 parent 63a6348 commit a6816b9

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

clang/lib/StaticAnalyzer/Core/SValBuilder.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,23 @@ SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
459459
return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
460460
}
461461

462-
if (Optional<Loc> RV = rhs.getAs<Loc>()) {
463-
// Support pointer arithmetic where the addend is on the left
464-
// and the pointer on the right.
465-
assert(op == BO_Add);
462+
if (const Optional<Loc> RV = rhs.getAs<Loc>()) {
463+
const auto IsCommutative = [](BinaryOperatorKind Op) {
464+
return Op == BO_Mul || Op == BO_Add || Op == BO_And || Op == BO_Xor ||
465+
Op == BO_Or;
466+
};
467+
468+
if (IsCommutative(op)) {
469+
// Swap operands.
470+
return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
471+
}
466472

467-
// Commute the operands.
468-
return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
473+
// If the right operand is a concrete int location then we have nothing
474+
// better but to treat it as a simple nonloc.
475+
if (auto RV = rhs.getAs<loc::ConcreteInt>()) {
476+
const nonloc::ConcreteInt RhsAsLoc = makeIntVal(RV->getValue());
477+
return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), RhsAsLoc, type);
478+
}
469479
}
470480

471481
return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core %s \
2+
// RUN: -triple x86_64-pc-linux-gnu -verify
3+
4+
#define BINOP(OP) [](auto x, auto y) { return x OP y; }
5+
6+
template <typename BinOp>
7+
void nonloc_OP_loc(int *p, BinOp op) {
8+
long p_as_integer = (long)p;
9+
if (op(12, p_as_integer) != 11)
10+
return;
11+
12+
// Perfectly constrain 'p', thus 'p_as_integer', and trigger a simplification
13+
// of the previously recorded constraint.
14+
if (p) {
15+
// no-crash
16+
}
17+
if (p == (int *)0x404) {
18+
// no-crash
19+
}
20+
}
21+
22+
// Same as before, but the operands are swapped.
23+
template <typename BinOp>
24+
void loc_OP_nonloc(int *p, BinOp op) {
25+
long p_as_integer = (long)p;
26+
if (op(p_as_integer, 12) != 11)
27+
return;
28+
29+
if (p) {
30+
// no-crash
31+
}
32+
if (p == (int *)0x404) {
33+
// no-crash
34+
}
35+
}
36+
37+
void instantiate_tests_for_nonloc_OP_loc(int *p) {
38+
// Multiplicative and additive operators:
39+
nonloc_OP_loc(p, BINOP(*));
40+
nonloc_OP_loc(p, BINOP(/)); // no-crash
41+
nonloc_OP_loc(p, BINOP(%)); // no-crash
42+
nonloc_OP_loc(p, BINOP(+));
43+
nonloc_OP_loc(p, BINOP(-)); // no-crash
44+
45+
// Bitwise operators:
46+
// expected-warning@+2 {{The result of the left shift is undefined due to shifting by '1028', which is greater or equal to the width of type 'int' [core.UndefinedBinaryOperatorResult]}}
47+
// expected-warning@+2 {{The result of the right shift is undefined due to shifting by '1028', which is greater or equal to the width of type 'int' [core.UndefinedBinaryOperatorResult]}}
48+
nonloc_OP_loc(p, BINOP(<<)); // no-crash
49+
nonloc_OP_loc(p, BINOP(>>)); // no-crash
50+
nonloc_OP_loc(p, BINOP(&));
51+
nonloc_OP_loc(p, BINOP(^));
52+
nonloc_OP_loc(p, BINOP(|));
53+
}
54+
55+
void instantiate_tests_for_loc_OP_nonloc(int *p) {
56+
// Multiplicative and additive operators:
57+
loc_OP_nonloc(p, BINOP(*));
58+
loc_OP_nonloc(p, BINOP(/));
59+
loc_OP_nonloc(p, BINOP(%));
60+
loc_OP_nonloc(p, BINOP(+));
61+
loc_OP_nonloc(p, BINOP(-));
62+
63+
// Bitwise operators:
64+
loc_OP_nonloc(p, BINOP(<<));
65+
loc_OP_nonloc(p, BINOP(>>));
66+
loc_OP_nonloc(p, BINOP(&));
67+
loc_OP_nonloc(p, BINOP(^));
68+
loc_OP_nonloc(p, BINOP(|));
69+
}
70+
71+
// from: nullptr.cpp
72+
void zoo1backwards() {
73+
char **p = nullptr;
74+
// expected-warning@+1 {{Dereference of null pointer [core.NullDereference]}}
75+
*(0 + p) = nullptr; // warn
76+
**(0 + p) = 'a'; // no-warning: this should be unreachable
77+
}

0 commit comments

Comments
 (0)