Skip to content

Commit 82992c1

Browse files
vabridgerseinvbri
authored and
einvbri
committed
[analyzer] Do not use APInt methods on _BitInt() Types
evalIntegralCast is using APInt method to get the value of _BitInt() values after _BitInt() changes were introduced. Some of those methods assume values are less than or equal to 64-bits, which is not true for _BitInt() types. This change simply side steps that issue if the _BitInt() type is greater than 64 bits. This was caught with our internal randomized testing. <src-root>/llvm/include/llvm/ADT/APInt.h:1510: int64_t llvm::APInt::getSExtValue() const: Assertion `getSignificantBits() <= 64 && "Too many bits for int64_t"' failed.a ... #9 <address> llvm::APInt::getSExtValue() const <src-root>/llvm/include/llvm/ADT/APInt.h:1510:5 llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>, clang::ento::SVal, clang::QualType, clang::QualType) <src-root>/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp:607:24 clang::Expr const*, clang::ento::ExplodedNode*, clang::ento::ExplodedNodeSet&) <src-root>/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp:413:61 ... Fixes: llvm/llvm-project#61960 Reviewed By: donat.nagy
1 parent 710b5a1 commit 82992c1

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

clang/lib/StaticAnalyzer/Core/SValBuilder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,12 @@ SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
598598
APSIntType ToType(getContext().getTypeSize(castTy),
599599
castTy->isUnsignedIntegerType());
600600
llvm::APSInt ToTypeMax = ToType.getMaxValue();
601+
// With the introduction of _BitInt(), integral types can be
602+
// > 64 bits. So check for this and skip the size checks
603+
// falling back to making a non loc return type.
604+
if (ToTypeMax.getSignificantBits() > 64) {
605+
return makeNonLoc(se, originalTy, castTy);
606+
}
601607
NonLoc ToTypeMaxVal =
602608
makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue()
603609
: ToTypeMax.getSExtValue(),

clang/test/Analysis/bitint-no-crash.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core \
2+
// RUN: -analyzer-checker=debug.ExprInspection \
3+
// RUN: -verify %s
4+
5+
// Don't crash when using _BitInt()
6+
// expected-no-diagnostics
7+
_BitInt(256) a;
8+
_BitInt(129) b;
9+
void c() {
10+
b = a;
11+
}

0 commit comments

Comments
 (0)