Skip to content

Commit fe8b223

Browse files
vabridgerseinvbri
authored andcommitted
[analyzer] Fix "RhsLoc and LhsLoc bitwidth must be same"
clang: <root>/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp:727: void assertEqualBitWidths(clang::ento::ProgramStateRef, clang::ento::Loc, clang::ento::Loc): Assertion `RhsBitwidth == LhsBitwidth && "RhsLoc and LhsLoc bitwidth must be same!"' This change adjusts the bitwidth of the smaller operand for an evalBinOp as a result of a comparison operation. This can occur in the specific case represented by the test cases for a target with different pointer sizes. Reviewed By: NoQ Differential Revision: https://reviews.llvm.org/D122513
1 parent 5898979 commit fe8b223

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

clang/lib/StaticAnalyzer/Core/SValBuilder.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,11 @@ SVal SValBuilder::evalCastSubKind(loc::ConcreteInt V, QualType CastTy,
682682
}
683683

684684
// Pointer to any pointer.
685-
if (Loc::isLocType(CastTy))
686-
return V;
685+
if (Loc::isLocType(CastTy)) {
686+
llvm::APSInt Value = V.getValue();
687+
BasicVals.getAPSIntType(CastTy).apply(Value);
688+
return loc::ConcreteInt(BasicVals.getValue(Value));
689+
}
687690

688691
// Pointer to whatever else.
689692
return UnknownVal();

clang/test/Analysis/addrspace-null.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %clang_analyze_cc1 -triple amdgcn-unknown-unknown \
2+
// RUN: -analyze -analyzer-checker=core -DAMDGCN_TRIPLE \
3+
// RUN: -analyze -analyzer-checker=debug.ExprInspection \
4+
// RUN: -Wno-implicit-int -Wno-int-conversion -verify %s
5+
//
6+
// RUN: %clang_analyze_cc1 -triple amdgcn-unknown-unknown \
7+
// RUN: -analyze -analyzer-checker=core -DDEFAULT_TRIPLE \
8+
// RUN: -analyze -analyzer-checker=debug.ExprInspection \
9+
// RUN: -Wno-implicit-int -Wno-int-conversion -verify %s
10+
11+
// From https://llvm.org/docs/AMDGPUUsage.html#address-spaces,
12+
// select address space 3 (local), since the pointer size is
13+
// different than Generic.
14+
15+
// expected-no-diagnostics
16+
17+
#define DEVICE __attribute__((address_space(3)))
18+
19+
#if defined(AMDGCN_TRIPLE)
20+
// this crashes
21+
int fn1() {
22+
int val = 0;
23+
DEVICE int *dptr = val;
24+
return dptr == (void *)0;
25+
}
26+
27+
// does not crash
28+
int fn2() {
29+
int val = 0;
30+
DEVICE int *dptr = val;
31+
return dptr == (DEVICE void *)0;
32+
}
33+
34+
// this crashes
35+
int fn3() {
36+
int val = 0;
37+
int *dptr = val;
38+
return dptr == (DEVICE void *)0;
39+
}
40+
#endif
41+
42+
// does not crash
43+
int fn4() {
44+
int val = 0;
45+
int *dptr = val;
46+
return dptr == (void *)0;
47+
}

0 commit comments

Comments
 (0)