Skip to content

Commit 93b98eb

Browse files
committedNov 23, 2022
[analyzer] getBinding should auto-detect type only if it was not given
Casting a pointer to a suitably large integral type by reinterpret-cast should result in the same value as by using the `__builtin_bit_cast()`. The compiler exploits this: https://godbolt.org/z/zMP3sG683 However, the analyzer does not bind the same symbolic value to these expressions, resulting in weird situations, such as failing equality checks and even results in crashes: https://godbolt.org/z/oeMP7cj8q Previously, in the `RegionStoreManager::getBinding()` even if `T` was non-null, we replaced it with `TVR->getValueType()` in case the `MR` was `TypedValueRegion`. It doesn't make much sense to auto-detect the type if the type is already given. By not doing the auto-detection, we would just do the right thing and perform the load by that type. This means that we will cast the value to that type. So, in this patch, I'm proposing to do auto-detection only if the type was null. Here is a snippet of code, annotated by the previous and new dump values. `LocAsInteger` should wrap the `SymRegion`, since we want to load the address as if it was an integer. In none of the following cases should type auto-detection be triggered, hence we should eventually reach an `evalCast()` to lazily cast the loaded value into that type. ```lang=C++ void LValueToRValueBitCast_dumps(void *p, char (*array)[8]) { clang_analyzer_dump(p); // remained: &SymRegion{reg_$0<void * p>} clang_analyzer_dump(array); // remained: {{&SymRegion{reg_$1<char (*)[8] array>} clang_analyzer_dump((unsigned long)p); // remained: {{&SymRegion{reg_$0<void * p>} [as 64 bit integer]}} clang_analyzer_dump(__builtin_bit_cast(unsigned long, p)); <--------- change #1 // previously: {{&SymRegion{reg_$0<void * p>}}} // now: {{&SymRegion{reg_$0<void * p>} [as 64 bit integer]}} clang_analyzer_dump((unsigned long)array); // remained: {{&SymRegion{reg_$1<char (*)[8] array>} [as 64 bit integer]}} clang_analyzer_dump(__builtin_bit_cast(unsigned long, array)); <--------- change #2 // previously: {{&SymRegion{reg_$1<char (*)[8] array>}}} // now: {{&SymRegion{reg_$1<char (*)[8] array>} [as 64 bit integer]}} } ``` Reviewed By: xazax.hun Differential Revision: https://reviews.llvm.org/D136603
1 parent aeecef0 commit 93b98eb

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed
 

‎clang/lib/StaticAnalyzer/Core/RegionStore.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -1414,19 +1414,20 @@ SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T)
14141414
return UnknownVal();
14151415
}
14161416

1417-
if (!isa<TypedValueRegion>(MR)) {
1418-
if (T.isNull()) {
1419-
if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR))
1420-
T = TR->getLocationType()->getPointeeType();
1421-
else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1422-
T = SR->getPointeeStaticType();
1423-
}
1424-
assert(!T.isNull() && "Unable to auto-detect binding type!");
1425-
assert(!T->isVoidType() && "Attempting to dereference a void pointer!");
1417+
// Auto-detect the binding type.
1418+
if (T.isNull()) {
1419+
if (const auto *TVR = dyn_cast<TypedValueRegion>(MR))
1420+
T = TVR->getValueType();
1421+
else if (const auto *TR = dyn_cast<TypedRegion>(MR))
1422+
T = TR->getLocationType()->getPointeeType();
1423+
else if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
1424+
T = SR->getPointeeStaticType();
1425+
}
1426+
assert(!T.isNull() && "Unable to auto-detect binding type!");
1427+
assert(!T->isVoidType() && "Attempting to dereference a void pointer!");
1428+
1429+
if (!isa<TypedValueRegion>(MR))
14261430
MR = GetElementZeroRegion(cast<SubRegion>(MR), T);
1427-
} else {
1428-
T = cast<TypedValueRegion>(MR)->getValueType();
1429-
}
14301431

14311432
// FIXME: Perhaps this method should just take a 'const MemRegion*' argument
14321433
// instead of 'Loc', and have the other Loc cases handled at a higher level.

‎clang/test/Analysis/ptr-arith.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// RUN: %clang_analyze_cc1 -Wno-unused-value -std=c++14 -analyzer-checker=core,debug.ExprInspection,alpha.core.PointerArithm -verify %s
1+
// RUN: %clang_analyze_cc1 -Wno-unused-value -std=c++14 -verify %s -triple x86_64-pc-linux-gnu \
2+
// RUN: -analyzer-checker=core,debug.ExprInspection,alpha.core.PointerArithm
3+
4+
// RUN: %clang_analyze_cc1 -Wno-unused-value -std=c++14 -verify %s -triple x86_64-pc-linux-gnu \
5+
// RUN: -analyzer-config support-symbolic-integer-casts=true \
6+
// RUN: -analyzer-checker=core,debug.ExprInspection,alpha.core.PointerArithm
27

38
template <typename T> void clang_analyzer_dump(T);
49

@@ -141,3 +146,22 @@ int parse(parse_t *p) {
141146
return bits->b; // no-warning
142147
}
143148
} // namespace Bug_55934
149+
150+
void LValueToRValueBitCast_dumps(void *p, char (*array)[8]) {
151+
clang_analyzer_dump(p);
152+
clang_analyzer_dump(array);
153+
// expected-warning@-2 {{&SymRegion{reg_$0<void * p>}}}
154+
// expected-warning@-2 {{&SymRegion{reg_$1<char (*)[8] array>}}}
155+
clang_analyzer_dump((unsigned long)p);
156+
clang_analyzer_dump(__builtin_bit_cast(unsigned long, p));
157+
// expected-warning@-2 {{&SymRegion{reg_$0<void * p>} [as 64 bit integer]}}
158+
// expected-warning@-2 {{&SymRegion{reg_$0<void * p>} [as 64 bit integer]}}
159+
clang_analyzer_dump((unsigned long)array);
160+
clang_analyzer_dump(__builtin_bit_cast(unsigned long, array));
161+
// expected-warning@-2 {{&SymRegion{reg_$1<char (*)[8] array>} [as 64 bit integer]}}
162+
// expected-warning@-2 {{&SymRegion{reg_$1<char (*)[8] array>} [as 64 bit integer]}}
163+
}
164+
165+
unsigned long ptr_arithmetic(void *p) {
166+
return __builtin_bit_cast(unsigned long, p) + 1; // no-crash
167+
}

‎clang/test/Analysis/svalbuilder-float-cast.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
// RUN: %clang_analyze_cc1 -analyzer-checker debug.ExprInspection -Wno-deprecated-non-prototype -verify %s
2+
// RUN: %clang_analyze_cc1 -analyzer-checker debug.ExprInspection -Wno-deprecated-non-prototype -verify %s \
3+
// RUN: -analyzer-config support-symbolic-integer-casts=true
4+
25
void clang_analyzer_denote(int, const char *);
36
void clang_analyzer_express(int);
7+
void clang_analyzer_dump(int);
8+
void clang_analyzer_dump_ptr(int *);
49

510
void SymbolCast_of_float_type_aux(int *p) {
11+
clang_analyzer_dump_ptr(p); // expected-warning {{&x}}
12+
clang_analyzer_dump(*p); // expected-warning {{Unknown}}
13+
// Storing to the memory region of 'float x' as 'int' will
14+
// materialize a fresh conjured symbol to regain accuracy.
615
*p += 0;
7-
// FIXME: Ideally, all unknown values should be symbolicated.
8-
clang_analyzer_denote(*p, "$x"); // expected-warning{{Not a symbol}}
16+
clang_analyzer_dump_ptr(p); // expected-warning {{&x}}
17+
clang_analyzer_dump(*p); // expected-warning {{conj_$0{int}}
18+
clang_analyzer_denote(*p, "$x");
919

1020
*p += 1;
1121
// This should NOT be (float)$x + 1. Symbol $x was never casted to float.
12-
// FIXME: Ideally, this should be $x + 1.
13-
clang_analyzer_express(*p); // expected-warning{{Not a symbol}}
22+
clang_analyzer_express(*p); // expected-warning{{$x + 1}}
1423
}
1524

1625
void SymbolCast_of_float_type(void) {

0 commit comments

Comments
 (0)