Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 1 | //=== 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 Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 10 | // This defines UndefResultChecker, a builtin check in ExprEngine that |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 11 | // performs checks for undefined results of non-assignment binary operators. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | d4d3cee | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | d4d3cee | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 25 | using namespace ento; |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 26 | |
| 27 | namespace { |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 28 | class UndefResultChecker |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 29 | : public Checker< check::PostStmt<BinaryOperator> > { |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 30 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 31 | mutable std::unique_ptr<BugType> BT; |
| 32 | |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 33 | public: |
Argyrios Kyrtzidis | d4d3cee | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 34 | void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const; |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 35 | }; |
| 36 | } // end anonymous namespace |
| 37 | |
Daniel Marjamaki | e97838f | 2017-02-27 10:44:24 +0000 | [diff] [blame] | 38 | static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex) { |
| 39 | ProgramStateRef state = C.getState(); |
Daniel Marjamaki | e97838f | 2017-02-27 10:44:24 +0000 | [diff] [blame] | 40 | |
| 41 | if (!isa<ArraySubscriptExpr>(Ex)) |
| 42 | return false; |
| 43 | |
George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 44 | SVal Loc = C.getSVal(Ex); |
Daniel Marjamaki | e97838f | 2017-02-27 10:44:24 +0000 | [diff] [blame] | 45 | 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 Marjamaki | d3d8368 | 2017-10-11 14:49:35 +0000 | [diff] [blame] | 61 | static bool isShiftOverflow(const BinaryOperator *B, CheckerContext &C) { |
| 62 | return C.isGreaterOrEqual( |
| 63 | B->getRHS(), C.getASTContext().getIntWidth(B->getLHS()->getType())); |
| 64 | } |
| 65 | |
Gabor Horvath | 596fcb1 | 2018-01-22 13:32:10 +0000 | [diff] [blame] | 66 | static 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())); |
| 72 | return (unsigned)RHS->getZExtValue() > LHS->countLeadingZeros(); |
| 73 | } |
| 74 | |
Argyrios Kyrtzidis | d4d3cee | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 75 | void UndefResultChecker::checkPostStmt(const BinaryOperator *B, |
| 76 | CheckerContext &C) const { |
George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 77 | if (C.getSVal(B).isUndef()) { |
Anna Zaks | 0325646 | 2013-06-18 23:16:15 +0000 | [diff] [blame] | 78 | |
| 79 | // Do not report assignments of uninitialized values inside swap functions. |
| 80 | // This should allow to swap partially uninitialized structs |
| 81 | // (radar://14129997) |
| 82 | if (const FunctionDecl *EnclosingFunctionDecl = |
| 83 | dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl())) |
| 84 | if (C.getCalleeName(EnclosingFunctionDecl) == "swap") |
| 85 | return; |
| 86 | |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 87 | // Generate an error node. |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 88 | ExplodedNode *N = C.generateErrorNode(); |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 89 | if (!N) |
| 90 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 91 | |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 92 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 93 | BT.reset( |
| 94 | new BuiltinBug(this, "Result of operation is garbage or undefined")); |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 95 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 96 | SmallString<256> sbuf; |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 97 | llvm::raw_svector_ostream OS(sbuf); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 98 | const Expr *Ex = nullptr; |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 99 | bool isLeft = true; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 100 | |
George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 101 | if (C.getSVal(B->getLHS()).isUndef()) { |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 102 | Ex = B->getLHS()->IgnoreParenCasts(); |
| 103 | isLeft = true; |
| 104 | } |
George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 105 | else if (C.getSVal(B->getRHS()).isUndef()) { |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 106 | Ex = B->getRHS()->IgnoreParenCasts(); |
| 107 | isLeft = false; |
| 108 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 109 | |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 110 | if (Ex) { |
Daniel Marjamaki | d3d8368 | 2017-10-11 14:49:35 +0000 | [diff] [blame] | 111 | OS << "The " << (isLeft ? "left" : "right") << " operand of '" |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 112 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
| 113 | << "' is a garbage value"; |
Daniel Marjamaki | e97838f | 2017-02-27 10:44:24 +0000 | [diff] [blame] | 114 | if (isArrayIndexOutOfBounds(C, Ex)) |
| 115 | OS << " due to array index out of bounds"; |
Daniel Marjamaki | d3d8368 | 2017-10-11 14:49:35 +0000 | [diff] [blame] | 116 | } else { |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 117 | // Neither operand was undefined, but the result is undefined. |
Daniel Marjamaki | d3d8368 | 2017-10-11 14:49:35 +0000 | [diff] [blame] | 118 | if ((B->getOpcode() == BinaryOperatorKind::BO_Shl || |
| 119 | B->getOpcode() == BinaryOperatorKind::BO_Shr) && |
| 120 | C.isNegative(B->getRHS())) { |
| 121 | OS << "The result of the " |
| 122 | << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left" |
| 123 | : "right") |
| 124 | << " shift is undefined because the right operand is negative"; |
| 125 | } else if ((B->getOpcode() == BinaryOperatorKind::BO_Shl || |
| 126 | B->getOpcode() == BinaryOperatorKind::BO_Shr) && |
| 127 | isShiftOverflow(B, C)) { |
| 128 | |
| 129 | OS << "The result of the " |
| 130 | << ((B->getOpcode() == BinaryOperatorKind::BO_Shl) ? "left" |
| 131 | : "right") |
| 132 | << " shift is undefined due to shifting by "; |
| 133 | |
| 134 | SValBuilder &SB = C.getSValBuilder(); |
| 135 | const llvm::APSInt *I = |
| 136 | SB.getKnownValue(C.getState(), C.getSVal(B->getRHS())); |
| 137 | if (!I) |
| 138 | OS << "a value that is"; |
| 139 | else if (I->isUnsigned()) |
| 140 | OS << '\'' << I->getZExtValue() << "\', which is"; |
| 141 | else |
| 142 | OS << '\'' << I->getSExtValue() << "\', which is"; |
| 143 | |
| 144 | OS << " greater or equal to the width of type '" |
| 145 | << B->getLHS()->getType().getAsString() << "'."; |
Gabor Horvath | 4581f74 | 2017-10-30 17:06:42 +0000 | [diff] [blame] | 146 | } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl && |
| 147 | C.isNegative(B->getLHS())) { |
| 148 | OS << "The result of the left shift is undefined because the left " |
| 149 | "operand is negative"; |
Gabor Horvath | 596fcb1 | 2018-01-22 13:32:10 +0000 | [diff] [blame] | 150 | } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl && |
| 151 | isLeftShiftResultUnrepresentable(B, C)) { |
| 152 | ProgramStateRef State = C.getState(); |
| 153 | SValBuilder &SB = C.getSValBuilder(); |
| 154 | const llvm::APSInt *LHS = |
| 155 | SB.getKnownValue(State, C.getSVal(B->getLHS())); |
| 156 | const llvm::APSInt *RHS = |
| 157 | SB.getKnownValue(State, C.getSVal(B->getRHS())); |
| 158 | OS << "The result of the left shift is undefined due to shifting \'" |
| 159 | << LHS->getSExtValue() << "\' by \'" << RHS->getZExtValue() |
| 160 | << "\', which is unrepresentable in the unsigned version of " |
| 161 | << "the return type \'" << B->getLHS()->getType().getAsString() |
| 162 | << "\'"; |
Daniel Marjamaki | d3d8368 | 2017-10-11 14:49:35 +0000 | [diff] [blame] | 163 | } else { |
| 164 | OS << "The result of the '" |
| 165 | << BinaryOperator::getOpcodeStr(B->getOpcode()) |
| 166 | << "' expression is undefined"; |
| 167 | } |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 168 | } |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 169 | auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N); |
Ted Kremenek | 33e88a7 | 2009-11-29 06:37:44 +0000 | [diff] [blame] | 170 | if (Ex) { |
| 171 | report->addRange(Ex->getSourceRange()); |
Jordan Rose | a0f7d35 | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 172 | bugreporter::trackNullOrUndefValue(N, Ex, *report); |
Ted Kremenek | 33e88a7 | 2009-11-29 06:37:44 +0000 | [diff] [blame] | 173 | } |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 174 | else |
Jordan Rose | a0f7d35 | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 175 | bugreporter::trackNullOrUndefValue(N, B, *report); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 176 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 177 | C.emitReport(std::move(report)); |
Zhongxing Xu | c6123a1 | 2009-11-24 08:24:26 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
Argyrios Kyrtzidis | d4d3cee | 2011-02-28 01:27:22 +0000 | [diff] [blame] | 180 | |
| 181 | void ento::registerUndefResultChecker(CheckerManager &mgr) { |
| 182 | mgr.registerChecker<UndefResultChecker>(); |
| 183 | } |