Skip to content

Commit dd01633

Browse files
authored
[analyzer] Fix crash in BasicValueFactory.cpp with __int128_t integers (#67212)
This change avoids a crash in BasicValueFactory by checking the bit width of an APSInt to avoid calling getZExtValue if greater than 64-bits. This was caught by our internal, randomized test generator. Clang invocation clang -cc1 -analyzer-checker=optin.portability.UnixAPI case.c <src-root>/llvm/include/llvm/ADT/APInt.h:1488: uint64_t llvm::APInt::getZExtValue() const: Assertion `getActiveBits() <= 64 && "Too many bits for uint64_t"' failed. ... #9 <address> llvm::APInt::getZExtValue() const <src-root>/llvm/include/llvm/ADT/APInt.h:1488:5 clang::BinaryOperatorKind, llvm::APSInt const&, llvm::APSInt const&) <src-root>/clang/lib/StaticAnalyzer/Core/BasicValueFactory.cpp:307:37 llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>, clang::BinaryOperatorKind, clang::ento::NonLoc, clang::ento::NonLoc, clang::QualType) <src-root>/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp:531:31 llvm::IntrusiveRefCntPtr<clang::ento::ProgramState const>, clang::BinaryOperatorKind, clang::ento::SVal, clang::ento::SVal, clang::QualType) <src-root>/clang/lib/StaticAnalyzer/Core/SValBuilder.cpp:532:26 ...
1 parent 263a00f commit dd01633

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

clang/docs/ReleaseNotes.rst

+8
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,14 @@ Static Analyzer
538538
Read the PR for the details.
539539
(`#66086 <https://github.com/llvm/llvm-project/pull/66086>`_)
540540

541+
- A few crashes have been found and fixed using randomized testing related
542+
to the use of ``_BitInt()`` in tidy checks and in clang analysis. See
543+
`#67212 <https://github.com/llvm/llvm-project/pull/67212>`_,
544+
`#66782 <https://github.com/llvm/llvm-project/pull/66782>`_,
545+
`#65889 <https://github.com/llvm/llvm-project/pull/65889>`_,
546+
`#65888 <https://github.com/llvm/llvm-project/pull/65888>`_, and
547+
`#65887 <https://github.com/llvm/llvm-project/pull/65887>`_
548+
541549
.. _release-notes-sanitizers:
542550

543551
Sanitizers

clang/lib/StaticAnalyzer/Core/BasicValueFactory.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
272272
// FIXME: This logic should probably go higher up, where we can
273273
// test these conditions symbolically.
274274

275-
if (V2.isSigned() && V2.isNegative())
275+
if (V2.isNegative() || V2.getBitWidth() > 64)
276276
return nullptr;
277277

278278
uint64_t Amt = V2.getZExtValue();
@@ -287,7 +287,7 @@ BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
287287
// FIXME: This logic should probably go higher up, where we can
288288
// test these conditions symbolically.
289289

290-
if (V2.isSigned() && V2.isNegative())
290+
if (V2.isNegative() || V2.getBitWidth() > 64)
291291
return nullptr;
292292

293293
uint64_t Amt = V2.getZExtValue();

clang/test/Analysis/int128-nocrash.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=optin.portability.UnixAPI \
2+
// RUN: -triple x86_64-pc-linux-gnu -x c %s
3+
4+
// Don't crash!
5+
// expected-no-diagnostics
6+
const __int128_t a = ( (__int128_t)1 << 64 );
7+
const _BitInt(72) b = ( 1 << 72 );
8+
9+
void int128() {
10+
2 >> a;
11+
}
12+
13+
void withbitint() {
14+
2 >> b;
15+
}

0 commit comments

Comments
 (0)