Skip to content

Commit be03d25

Browse files
NagyDonatVince Bridgers
andauthored
[analyzer] Retain address space information in getElementRegion (#151370)
The factory method `MemRegionManager::getElementRegion()` is the main way of constructing `ElementRegion` objects, which are widespread in the analysis and may represent array elements (as lvalues), pointer arithmetic and type conversions. This factory method used to strip all qualifiers from the type associated with the element (after canonicalizing it), but the address space qualifier can affect the size of a pointer type, so stripping it caused an assertion failure (in `evalBinOpLL`): clang: clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp:785: void assertEqualBitWidths(clang::ento::ProgramStateRef, clang::ento::Loc, clang::ento::Loc): Assertion `RhsBitwidth == LhsBitwidth && "RhsLoc and LhsLoc bitwidth must be same!"' failed. --------- Co-authored-by: Vince Bridgers <[email protected]>
1 parent 6b7a8e9 commit be03d25

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

clang/lib/StaticAnalyzer/Core/MemRegion.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,16 @@ MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
12191219
const ASTContext &Ctx) {
12201220
QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
12211221

1222+
// The address space must be preserved because some target-specific address
1223+
// spaces influence the size of the pointer value which is represented by the
1224+
// element region.
1225+
LangAS AS = elementType.getAddressSpace();
1226+
if (AS != LangAS::Default) {
1227+
Qualifiers Quals;
1228+
Quals.setAddressSpace(AS);
1229+
T = Ctx.getQualifiedType(T, Quals);
1230+
}
1231+
12221232
llvm::FoldingSetNodeID ID;
12231233
ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
12241234

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_analyze_cc1 -triple amdgcn-unknown-unknown \
2+
// RUN: -analyzer-checker=core -verify %s
3+
4+
// expected-no-diagnostics
5+
//
6+
// By default, pointers are 64-bits.
7+
#define ADDRESS_SPACE_32BITS __attribute__((address_space(3)))
8+
9+
int test(ADDRESS_SPACE_32BITS int *p, ADDRESS_SPACE_32BITS void *q) {
10+
return p == q; // no-crash
11+
}

0 commit comments

Comments
 (0)