1 //=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines UndefResultChecker, a builtin check in ExprEngine that
11 // performs checks for undefined results of non-assignment binary operators.
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
28 class UndefResultChecker
29 : public Checker< check::PostStmt<BinaryOperator> > {
31 mutable std::unique_ptr<BugType> BT;
34 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
36 } // end anonymous namespace
38 static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex) {
39 ProgramStateRef state = C.getState();
40 const LocationContext *LCtx = C.getLocationContext();
42 if (!isa<ArraySubscriptExpr>(Ex))
45 SVal Loc = state->getSVal(Ex, LCtx);
49 const MemRegion *MR = Loc.castAs<loc::MemRegionVal>().getRegion();
50 const ElementRegion *ER = dyn_cast<ElementRegion>(MR);
54 DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
55 DefinedOrUnknownSVal NumElements = C.getStoreManager().getSizeInElements(
56 state, ER->getSuperRegion(), ER->getValueType());
57 ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements, true);
58 ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements, false);
59 return StOutBound && !StInBound;
62 static bool isShiftOverflow(const BinaryOperator *B, CheckerContext &C) {
63 return C.isGreaterOrEqual(
64 B->getRHS(), C.getASTContext().getIntWidth(B->getLHS()->getType()));
67 void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
68 CheckerContext &C) const {
69 ProgramStateRef state = C.getState();
70 const LocationContext *LCtx = C.getLocationContext();
71 if (state->getSVal(B, LCtx).isUndef()) {
73 // Do not report assignments of uninitialized values inside swap functions.
74 // This should allow to swap partially uninitialized structs
76 if (const FunctionDecl *EnclosingFunctionDecl =
77 dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
78 if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
81 // Generate an error node.
82 ExplodedNode *N = C.generateErrorNode();
88 new BuiltinBug(this, "Result of operation is garbage or undefined"));
90 SmallString<256> sbuf;
91 llvm::raw_svector_ostream OS(sbuf);
92 const Expr *Ex = nullptr;
95 if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
96 Ex = B->getLHS()->IgnoreParenCasts();
99 else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
100 Ex = B->getRHS()->IgnoreParenCasts();
105 OS << "The " << (isLeft ? "left" : "right") << " operand of '"
106 << BinaryOperator::getOpcodeStr(B->getOpcode())
107 << "' is a garbage value";
108 if (isArrayIndexOutOfBounds(C, Ex))
109 OS << " due to array index out of bounds";
111 // Neither operand was undefined, but the result is undefined.
112 if ((B->getOpcode() == BinaryOperatorKind::BO_Shl ||
113 B->getOpcode() == BinaryOperatorKind::BO_Shr) &&
114 C.isNegative(B->getRHS())) {
115 OS << "The result of the "
116 << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left"
118 << " shift is undefined because the right operand is negative";
119 } else if ((B->getOpcode() == BinaryOperatorKind::BO_Shl ||
120 B->getOpcode() == BinaryOperatorKind::BO_Shr) &&
121 isShiftOverflow(B, C)) {
123 OS << "The result of the "
124 << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left"
126 << " shift is undefined due to shifting by ";
128 SValBuilder &SB = C.getSValBuilder();
129 const llvm::APSInt *I =
130 SB.getKnownValue(C.getState(), C.getSVal(B->getRHS()));
132 OS << "a value that is";
133 else if (I->isUnsigned())
134 OS << '\'' << I->getZExtValue() << "\', which is";
136 OS << '\'' << I->getSExtValue() << "\', which is";
138 OS << " greater or equal to the width of type '"
139 << B->getLHS()->getType().getAsString() << "'.";
140 } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl &&
141 C.isNegative(B->getLHS())) {
142 OS << "The result of the left shift is undefined because the left "
143 "operand is negative";
145 OS << "The result of the '"
146 << BinaryOperator::getOpcodeStr(B->getOpcode())
147 << "' expression is undefined";
150 auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N);
152 report->addRange(Ex->getSourceRange());
153 bugreporter::trackNullOrUndefValue(N, Ex, *report);
156 bugreporter::trackNullOrUndefValue(N, B, *report);
158 C.emitReport(std::move(report));
162 void ento::registerUndefResultChecker(CheckerManager &mgr) {
163 mgr.registerChecker<UndefResultChecker>();