blob: f30f32e959d299920991bd9cd0adbdf96e3a03cc [file] [log] [blame]
Zhongxing Xuc6123a12009-11-24 08:24:26 +00001//=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek3a0678e2015-09-08 03:50:52 +000010// This defines UndefResultChecker, a builtin check in ExprEngine that
Zhongxing Xuc6123a12009-11-24 08:24:26 +000011// performs checks for undefined results of non-assignment binary operators.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000015#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000022#include "llvm/Support/raw_ostream.h"
Zhongxing Xuc6123a12009-11-24 08:24:26 +000023
24using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000025using namespace ento;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000026
27namespace {
Ted Kremenek3a0678e2015-09-08 03:50:52 +000028class UndefResultChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000029 : public Checker< check::PostStmt<BinaryOperator> > {
Zhongxing Xuc6123a12009-11-24 08:24:26 +000030
Ahmed Charlesb8984322014-03-07 20:03:18 +000031 mutable std::unique_ptr<BugType> BT;
32
Zhongxing Xuc6123a12009-11-24 08:24:26 +000033public:
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000034 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000035};
36} // end anonymous namespace
37
Daniel Marjamakie97838f2017-02-27 10:44:24 +000038static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex) {
39 ProgramStateRef state = C.getState();
Daniel Marjamakie97838f2017-02-27 10:44:24 +000040
41 if (!isa<ArraySubscriptExpr>(Ex))
42 return false;
43
George Karpenkovd703ec92018-01-17 20:27:29 +000044 SVal Loc = C.getSVal(Ex);
Daniel Marjamakie97838f2017-02-27 10:44:24 +000045 if (!Loc.isValid())
46 return false;
47
48 const MemRegion *MR = Loc.castAs<loc::MemRegionVal>().getRegion();
49 const ElementRegion *ER = dyn_cast<ElementRegion>(MR);
50 if (!ER)
51 return false;
52
53 DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
54 DefinedOrUnknownSVal NumElements = C.getStoreManager().getSizeInElements(
55 state, ER->getSuperRegion(), ER->getValueType());
56 ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements, true);
57 ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements, false);
58 return StOutBound && !StInBound;
59}
60
Daniel Marjamakid3d83682017-10-11 14:49:35 +000061static bool isShiftOverflow(const BinaryOperator *B, CheckerContext &C) {
62 return C.isGreaterOrEqual(
63 B->getRHS(), C.getASTContext().getIntWidth(B->getLHS()->getType()));
64}
65
Gabor Horvath596fcb12018-01-22 13:32:10 +000066static bool isLeftShiftResultUnrepresentable(const BinaryOperator *B,
67 CheckerContext &C) {
68 SValBuilder &SB = C.getSValBuilder();
69 ProgramStateRef State = C.getState();
70 const llvm::APSInt *LHS = SB.getKnownValue(State, C.getSVal(B->getLHS()));
71 const llvm::APSInt *RHS = SB.getKnownValue(State, C.getSVal(B->getRHS()));
George Karpenkov9ff67a92018-08-29 20:29:59 +000072 assert(LHS && RHS && "Values unknown, inconsistent state");
Gabor Horvath596fcb12018-01-22 13:32:10 +000073 return (unsigned)RHS->getZExtValue() > LHS->countLeadingZeros();
74}
75
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +000076void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
77 CheckerContext &C) const {
George Karpenkovd703ec92018-01-17 20:27:29 +000078 if (C.getSVal(B).isUndef()) {
Anna Zaks03256462013-06-18 23:16:15 +000079
80 // Do not report assignments of uninitialized values inside swap functions.
81 // This should allow to swap partially uninitialized structs
82 // (radar://14129997)
83 if (const FunctionDecl *EnclosingFunctionDecl =
84 dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
85 if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
86 return;
87
Zhongxing Xuc6123a12009-11-24 08:24:26 +000088 // Generate an error node.
Devin Coughline39bd402015-09-16 22:03:05 +000089 ExplodedNode *N = C.generateErrorNode();
Zhongxing Xuc6123a12009-11-24 08:24:26 +000090 if (!N)
91 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +000092
Zhongxing Xuc6123a12009-11-24 08:24:26 +000093 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000094 BT.reset(
95 new BuiltinBug(this, "Result of operation is garbage or undefined"));
Zhongxing Xuc6123a12009-11-24 08:24:26 +000096
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000097 SmallString<256> sbuf;
Zhongxing Xuc6123a12009-11-24 08:24:26 +000098 llvm::raw_svector_ostream OS(sbuf);
Craig Topper0dbb7832014-05-27 02:45:47 +000099 const Expr *Ex = nullptr;
Zhongxing Xuc6123a12009-11-24 08:24:26 +0000100 bool isLeft = true;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000101
George Karpenkovd703ec92018-01-17 20:27:29 +0000102 if (C.getSVal(B->getLHS()).isUndef()) {
Zhongxing Xuc6123a12009-11-24 08:24:26 +0000103 Ex = B->getLHS()->IgnoreParenCasts();
104 isLeft = true;
105 }
George Karpenkovd703ec92018-01-17 20:27:29 +0000106 else if (C.getSVal(B->getRHS()).isUndef()) {
Zhongxing Xuc6123a12009-11-24 08:24:26 +0000107 Ex = B->getRHS()->IgnoreParenCasts();
108 isLeft = false;
109 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000110
Zhongxing Xuc6123a12009-11-24 08:24:26 +0000111 if (Ex) {
Daniel Marjamakid3d83682017-10-11 14:49:35 +0000112 OS << "The " << (isLeft ? "left" : "right") << " operand of '"
Zhongxing Xuc6123a12009-11-24 08:24:26 +0000113 << BinaryOperator::getOpcodeStr(B->getOpcode())
114 << "' is a garbage value";
Daniel Marjamakie97838f2017-02-27 10:44:24 +0000115 if (isArrayIndexOutOfBounds(C, Ex))
116 OS << " due to array index out of bounds";
Daniel Marjamakid3d83682017-10-11 14:49:35 +0000117 } else {
Zhongxing Xuc6123a12009-11-24 08:24:26 +0000118 // Neither operand was undefined, but the result is undefined.
Daniel Marjamakid3d83682017-10-11 14:49:35 +0000119 if ((B->getOpcode() == BinaryOperatorKind::BO_Shl ||
120 B->getOpcode() == BinaryOperatorKind::BO_Shr) &&
121 C.isNegative(B->getRHS())) {
122 OS << "The result of the "
123 << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left"
124 : "right")
125 << " shift is undefined because the right operand is negative";
George Karpenkov09c6b502018-08-22 23:17:02 +0000126 Ex = B->getRHS();
Daniel Marjamakid3d83682017-10-11 14:49:35 +0000127 } else if ((B->getOpcode() == BinaryOperatorKind::BO_Shl ||
128 B->getOpcode() == BinaryOperatorKind::BO_Shr) &&
129 isShiftOverflow(B, C)) {
130
131 OS << "The result of the "
132 << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left"
133 : "right")
134 << " shift is undefined due to shifting by ";
George Karpenkov09c6b502018-08-22 23:17:02 +0000135 Ex = B->getRHS();
Daniel Marjamakid3d83682017-10-11 14:49:35 +0000136
137 SValBuilder &SB = C.getSValBuilder();
138 const llvm::APSInt *I =
139 SB.getKnownValue(C.getState(), C.getSVal(B->getRHS()));
140 if (!I)
141 OS << "a value that is";
142 else if (I->isUnsigned())
143 OS << '\'' << I->getZExtValue() << "\', which is";
144 else
145 OS << '\'' << I->getSExtValue() << "\', which is";
146
147 OS << " greater or equal to the width of type '"
148 << B->getLHS()->getType().getAsString() << "'.";
Gabor Horvath4581f742017-10-30 17:06:42 +0000149 } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl &&
150 C.isNegative(B->getLHS())) {
151 OS << "The result of the left shift is undefined because the left "
152 "operand is negative";
George Karpenkov09c6b502018-08-22 23:17:02 +0000153 Ex = B->getLHS();
Gabor Horvath596fcb12018-01-22 13:32:10 +0000154 } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl &&
155 isLeftShiftResultUnrepresentable(B, C)) {
156 ProgramStateRef State = C.getState();
157 SValBuilder &SB = C.getSValBuilder();
158 const llvm::APSInt *LHS =
159 SB.getKnownValue(State, C.getSVal(B->getLHS()));
160 const llvm::APSInt *RHS =
161 SB.getKnownValue(State, C.getSVal(B->getRHS()));
162 OS << "The result of the left shift is undefined due to shifting \'"
163 << LHS->getSExtValue() << "\' by \'" << RHS->getZExtValue()
164 << "\', which is unrepresentable in the unsigned version of "
165 << "the return type \'" << B->getLHS()->getType().getAsString()
166 << "\'";
George Karpenkov09c6b502018-08-22 23:17:02 +0000167 Ex = B->getLHS();
Daniel Marjamakid3d83682017-10-11 14:49:35 +0000168 } else {
169 OS << "The result of the '"
170 << BinaryOperator::getOpcodeStr(B->getOpcode())
171 << "' expression is undefined";
172 }
Zhongxing Xuc6123a12009-11-24 08:24:26 +0000173 }
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000174 auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N);
Ted Kremenek33e88a72009-11-29 06:37:44 +0000175 if (Ex) {
176 report->addRange(Ex->getSourceRange());
George Karpenkovb2cf0062018-10-23 18:24:53 +0000177 bugreporter::trackExpressionValue(N, Ex, *report);
Ted Kremenek33e88a72009-11-29 06:37:44 +0000178 }
Zhongxing Xuc6123a12009-11-24 08:24:26 +0000179 else
George Karpenkovb2cf0062018-10-23 18:24:53 +0000180 bugreporter::trackExpressionValue(N, B, *report);
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000181
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000182 C.emitReport(std::move(report));
Zhongxing Xuc6123a12009-11-24 08:24:26 +0000183 }
184}
Argyrios Kyrtzidisd4d3cee2011-02-28 01:27:22 +0000185
186void ento::registerUndefResultChecker(CheckerManager &mgr) {
187 mgr.registerChecker<UndefResultChecker>();
188}