| Zhongxing Xu | 72269ec | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 1 | //===--- CallAndMessageChecker.cpp ------------------------------*- C++ -*--==// | 
| Zhongxing Xu | ab162e1 | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 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 | // | 
| Zhongxing Xu | 72269ec | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 10 | // This defines CallAndMessageChecker, a builtin checker that checks for various | 
|  | 11 | // errors of call and objc message expressions. | 
| Zhongxing Xu | ab162e1 | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame^] | 15 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" | 
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ParentMap.h" | 
|  | 17 | #include "clang/Basic/TargetInfo.h" | 
|  | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" | 
| Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/Checker.h" | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" | 
| Jordan Rose | 4f7df9b | 2012-07-26 21:39:41 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" | 
| Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" | 
| Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" | 
| Zhongxing Xu | ab162e1 | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 26 |  | 
|  | 27 | using namespace clang; | 
| Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 28 | using namespace ento; | 
| Zhongxing Xu | ab162e1 | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 29 |  | 
| Ted Kremenek | 4325315 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 30 | namespace { | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 31 |  | 
|  | 32 | struct ChecksFilter { | 
|  | 33 | DefaultBool Check_CallAndMessageUnInitRefArg; | 
|  | 34 | DefaultBool Check_CallAndMessageChecker; | 
|  | 35 |  | 
|  | 36 | CheckName CheckName_CallAndMessageUnInitRefArg; | 
|  | 37 | CheckName CheckName_CallAndMessageChecker; | 
|  | 38 | }; | 
|  | 39 |  | 
| Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 40 | class CallAndMessageChecker | 
| Jordan Rose | 867b185e | 2013-08-09 00:55:47 +0000 | [diff] [blame] | 41 | : public Checker< check::PreStmt<CallExpr>, | 
|  | 42 | check::PreStmt<CXXDeleteExpr>, | 
|  | 43 | check::PreObjCMessage, | 
| Devin Coughlin | ca5ab2b | 2015-09-15 01:13:53 +0000 | [diff] [blame] | 44 | check::ObjCMessageNil, | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 45 | check::PreCall > { | 
| Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 46 | mutable std::unique_ptr<BugType> BT_call_null; | 
|  | 47 | mutable std::unique_ptr<BugType> BT_call_undef; | 
|  | 48 | mutable std::unique_ptr<BugType> BT_cxx_call_null; | 
|  | 49 | mutable std::unique_ptr<BugType> BT_cxx_call_undef; | 
|  | 50 | mutable std::unique_ptr<BugType> BT_call_arg; | 
|  | 51 | mutable std::unique_ptr<BugType> BT_cxx_delete_undef; | 
|  | 52 | mutable std::unique_ptr<BugType> BT_msg_undef; | 
|  | 53 | mutable std::unique_ptr<BugType> BT_objc_prop_undef; | 
|  | 54 | mutable std::unique_ptr<BugType> BT_objc_subscript_undef; | 
|  | 55 | mutable std::unique_ptr<BugType> BT_msg_arg; | 
|  | 56 | mutable std::unique_ptr<BugType> BT_msg_ret; | 
|  | 57 | mutable std::unique_ptr<BugType> BT_call_few_args; | 
|  | 58 |  | 
| Ted Kremenek | 4325315 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 59 | public: | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 60 | ChecksFilter Filter; | 
| Zhongxing Xu | 9e20079 | 2009-11-24 07:06:39 +0000 | [diff] [blame] | 61 |  | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 62 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; | 
| Jordan Rose | 867b185e | 2013-08-09 00:55:47 +0000 | [diff] [blame] | 63 | void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const; | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 64 | void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const; | 
| Devin Coughlin | ca5ab2b | 2015-09-15 01:13:53 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | /// Fill in the return value that results from messaging nil based on the | 
|  | 67 | /// return type and architecture and diagnose if the return value will be | 
|  | 68 | /// garbage. | 
|  | 69 | void checkObjCMessageNil(const ObjCMethodCall &msg, CheckerContext &C) const; | 
|  | 70 |  | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 71 | void checkPreCall(const CallEvent &Call, CheckerContext &C) const; | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 72 |  | 
| Ted Kremenek | caf2c51 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 73 | private: | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 74 | bool PreVisitProcessArg(CheckerContext &C, SVal V, SourceRange ArgRange, | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 75 | const Expr *ArgEx, int ArgumentNumber, | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 76 | bool CheckUninitFields, const CallEvent &Call, | 
|  | 77 | std::unique_ptr<BugType> &BT, | 
|  | 78 | const ParmVarDecl *ParamDecl) const; | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 79 |  | 
| Jordan Rose | 92e1449 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 80 | static void emitBadCall(BugType *BT, CheckerContext &C, const Expr *BadE); | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 81 | void emitNilReceiverBug(CheckerContext &C, const ObjCMethodCall &msg, | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 82 | ExplodedNode *N) const; | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 83 |  | 
| Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 84 | void HandleNilReceiver(CheckerContext &C, | 
| Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 85 | ProgramStateRef state, | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 86 | const ObjCMethodCall &msg) const; | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 87 |  | 
| Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 88 | void LazyInit_BT(const char *desc, std::unique_ptr<BugType> &BT) const { | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 89 | if (!BT) | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 90 | BT.reset(new BuiltinBug(this, desc)); | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 91 | } | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 92 | bool uninitRefOrPointer(CheckerContext &C, const SVal &V, | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 93 | SourceRange ArgRange, const Expr *ArgEx, | 
|  | 94 | std::unique_ptr<BugType> &BT, | 
|  | 95 | const ParmVarDecl *ParamDecl, const char *BD, | 
|  | 96 | int ArgumentNumber) const; | 
| Ted Kremenek | 4325315 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 97 | }; | 
|  | 98 | } // end anonymous namespace | 
|  | 99 |  | 
| Jordan Rose | 92e1449 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 100 | void CallAndMessageChecker::emitBadCall(BugType *BT, CheckerContext &C, | 
|  | 101 | const Expr *BadE) { | 
| Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 102 | ExplodedNode *N = C.generateErrorNode(); | 
| Ted Kremenek | caf2c51 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 103 | if (!N) | 
|  | 104 | return; | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 105 |  | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 106 | auto R = llvm::make_unique<BugReport>(*BT, BT->getName(), N); | 
| Jordan Rose | 92e1449 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 107 | if (BadE) { | 
|  | 108 | R->addRange(BadE->getSourceRange()); | 
| Jordan Rose | aea020f | 2013-01-26 01:28:23 +0000 | [diff] [blame] | 109 | if (BadE->isGLValue()) | 
|  | 110 | BadE = bugreporter::getDerefExpr(BadE); | 
| George Karpenkov | b2cf006 | 2018-10-23 18:24:53 +0000 | [diff] [blame] | 111 | bugreporter::trackExpressionValue(N, BadE, *R); | 
| Jordan Rose | 92e1449 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 112 | } | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 113 | C.emitReport(std::move(R)); | 
| Ted Kremenek | caf2c51 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 114 | } | 
|  | 115 |  | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 116 | static void describeUninitializedArgumentInCall(const CallEvent &Call, | 
|  | 117 | int ArgumentNumber, | 
|  | 118 | llvm::raw_svector_ostream &Os) { | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 119 | switch (Call.getKind()) { | 
|  | 120 | case CE_ObjCMessage: { | 
|  | 121 | const ObjCMethodCall &Msg = cast<ObjCMethodCall>(Call); | 
|  | 122 | switch (Msg.getMessageKind()) { | 
|  | 123 | case OCM_Message: | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 124 | Os << (ArgumentNumber + 1) << llvm::getOrdinalSuffix(ArgumentNumber + 1) | 
|  | 125 | << " argument in message expression is an uninitialized value"; | 
|  | 126 | return; | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 127 | case OCM_PropertyAccess: | 
|  | 128 | assert(Msg.isSetter() && "Getters have no args"); | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 129 | Os << "Argument for property setter is an uninitialized value"; | 
|  | 130 | return; | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 131 | case OCM_Subscript: | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 132 | if (Msg.isSetter() && (ArgumentNumber == 0)) | 
|  | 133 | Os << "Argument for subscript setter is an uninitialized value"; | 
|  | 134 | else | 
|  | 135 | Os << "Subscript index is an uninitialized value"; | 
|  | 136 | return; | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 137 | } | 
|  | 138 | llvm_unreachable("Unknown message kind."); | 
|  | 139 | } | 
|  | 140 | case CE_Block: | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 141 | Os << (ArgumentNumber + 1) << llvm::getOrdinalSuffix(ArgumentNumber + 1) | 
|  | 142 | << " block call argument is an uninitialized value"; | 
|  | 143 | return; | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 144 | default: | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 145 | Os << (ArgumentNumber + 1) << llvm::getOrdinalSuffix(ArgumentNumber + 1) | 
|  | 146 | << " function call argument is an uninitialized value"; | 
|  | 147 | return; | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 148 | } | 
|  | 149 | } | 
|  | 150 |  | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 151 | bool CallAndMessageChecker::uninitRefOrPointer( | 
|  | 152 | CheckerContext &C, const SVal &V, SourceRange ArgRange, const Expr *ArgEx, | 
|  | 153 | std::unique_ptr<BugType> &BT, const ParmVarDecl *ParamDecl, const char *BD, | 
|  | 154 | int ArgumentNumber) const { | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 155 | if (!Filter.Check_CallAndMessageUnInitRefArg) | 
|  | 156 | return false; | 
|  | 157 |  | 
|  | 158 | // No parameter declaration available, i.e. variadic function argument. | 
|  | 159 | if(!ParamDecl) | 
|  | 160 | return false; | 
|  | 161 |  | 
|  | 162 | // If parameter is declared as pointer to const in function declaration, | 
|  | 163 | // then check if corresponding argument in function call is | 
|  | 164 | // pointing to undefined symbol value (uninitialized memory). | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 165 | SmallString<200> Buf; | 
|  | 166 | llvm::raw_svector_ostream Os(Buf); | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 167 |  | 
|  | 168 | if (ParamDecl->getType()->isPointerType()) { | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 169 | Os << (ArgumentNumber + 1) << llvm::getOrdinalSuffix(ArgumentNumber + 1) | 
|  | 170 | << " function call argument is a pointer to uninitialized value"; | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 171 | } else if (ParamDecl->getType()->isReferenceType()) { | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 172 | Os << (ArgumentNumber + 1) << llvm::getOrdinalSuffix(ArgumentNumber + 1) | 
|  | 173 | << " function call argument is an uninitialized value"; | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 174 | } else | 
|  | 175 | return false; | 
|  | 176 |  | 
|  | 177 | if(!ParamDecl->getType()->getPointeeType().isConstQualified()) | 
|  | 178 | return false; | 
|  | 179 |  | 
|  | 180 | if (const MemRegion *SValMemRegion = V.getAsRegion()) { | 
|  | 181 | const ProgramStateRef State = C.getState(); | 
| Artem Dergachev | 3ef5deb | 2017-12-12 02:27:55 +0000 | [diff] [blame] | 182 | const SVal PSV = State->getSVal(SValMemRegion, C.getASTContext().CharTy); | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 183 | if (PSV.isUndef()) { | 
| Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 184 | if (ExplodedNode *N = C.generateErrorNode()) { | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 185 | LazyInit_BT(BD, BT); | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 186 | auto R = llvm::make_unique<BugReport>(*BT, Os.str(), N); | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 187 | R->addRange(ArgRange); | 
| George Karpenkov | b2cf006 | 2018-10-23 18:24:53 +0000 | [diff] [blame] | 188 | if (ArgEx) | 
|  | 189 | bugreporter::trackExpressionValue(N, ArgEx, *R); | 
|  | 190 |  | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 191 | C.emitReport(std::move(R)); | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 192 | } | 
|  | 193 | return true; | 
|  | 194 | } | 
|  | 195 | } | 
|  | 196 | return false; | 
|  | 197 | } | 
|  | 198 |  | 
| Benjamin Kramer | c55e997 | 2018-10-13 22:18:22 +0000 | [diff] [blame] | 199 | namespace { | 
| George Karpenkov | eae57a2 | 2018-08-29 20:29:39 +0000 | [diff] [blame] | 200 | class FindUninitializedField { | 
|  | 201 | public: | 
|  | 202 | SmallVector<const FieldDecl *, 10> FieldChain; | 
|  | 203 |  | 
|  | 204 | private: | 
|  | 205 | StoreManager &StoreMgr; | 
|  | 206 | MemRegionManager &MrMgr; | 
|  | 207 | Store store; | 
|  | 208 |  | 
|  | 209 | public: | 
|  | 210 | FindUninitializedField(StoreManager &storeMgr, MemRegionManager &mrMgr, | 
|  | 211 | Store s) | 
|  | 212 | : StoreMgr(storeMgr), MrMgr(mrMgr), store(s) {} | 
|  | 213 |  | 
|  | 214 | bool Find(const TypedValueRegion *R) { | 
|  | 215 | QualType T = R->getValueType(); | 
|  | 216 | if (const RecordType *RT = T->getAsStructureType()) { | 
|  | 217 | const RecordDecl *RD = RT->getDecl()->getDefinition(); | 
|  | 218 | assert(RD && "Referred record has no definition"); | 
|  | 219 | for (const auto *I : RD->fields()) { | 
|  | 220 | const FieldRegion *FR = MrMgr.getFieldRegion(I, R); | 
|  | 221 | FieldChain.push_back(I); | 
|  | 222 | T = I->getType(); | 
|  | 223 | if (T->getAsStructureType()) { | 
|  | 224 | if (Find(FR)) | 
|  | 225 | return true; | 
|  | 226 | } else { | 
|  | 227 | const SVal &V = StoreMgr.getBinding(store, loc::MemRegionVal(FR)); | 
|  | 228 | if (V.isUndef()) | 
|  | 229 | return true; | 
|  | 230 | } | 
|  | 231 | FieldChain.pop_back(); | 
|  | 232 | } | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | return false; | 
|  | 236 | } | 
|  | 237 | }; | 
| Benjamin Kramer | c55e997 | 2018-10-13 22:18:22 +0000 | [diff] [blame] | 238 | } // namespace | 
| George Karpenkov | eae57a2 | 2018-08-29 20:29:39 +0000 | [diff] [blame] | 239 |  | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 240 | bool CallAndMessageChecker::PreVisitProcessArg(CheckerContext &C, | 
|  | 241 | SVal V, | 
|  | 242 | SourceRange ArgRange, | 
|  | 243 | const Expr *ArgEx, | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 244 | int ArgumentNumber, | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 245 | bool CheckUninitFields, | 
|  | 246 | const CallEvent &Call, | 
|  | 247 | std::unique_ptr<BugType> &BT, | 
|  | 248 | const ParmVarDecl *ParamDecl | 
|  | 249 | ) const { | 
|  | 250 | const char *BD = "Uninitialized argument value"; | 
|  | 251 |  | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 252 | if (uninitRefOrPointer(C, V, ArgRange, ArgEx, BT, ParamDecl, BD, | 
|  | 253 | ArgumentNumber)) | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 254 | return true; | 
|  | 255 |  | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 256 | if (V.isUndef()) { | 
| Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 257 | if (ExplodedNode *N = C.generateErrorNode()) { | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 258 | LazyInit_BT(BD, BT); | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 259 | // Generate a report for this bug. | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 260 | SmallString<200> Buf; | 
|  | 261 | llvm::raw_svector_ostream Os(Buf); | 
|  | 262 | describeUninitializedArgumentInCall(Call, ArgumentNumber, Os); | 
|  | 263 | auto R = llvm::make_unique<BugReport>(*BT, Os.str(), N); | 
|  | 264 |  | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 265 | R->addRange(ArgRange); | 
|  | 266 | if (ArgEx) | 
| George Karpenkov | b2cf006 | 2018-10-23 18:24:53 +0000 | [diff] [blame] | 267 | bugreporter::trackExpressionValue(N, ArgEx, *R); | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 268 | C.emitReport(std::move(R)); | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 269 | } | 
|  | 270 | return true; | 
|  | 271 | } | 
|  | 272 |  | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 273 | if (!CheckUninitFields) | 
| Ted Kremenek | 6762a94 | 2012-03-05 23:57:14 +0000 | [diff] [blame] | 274 | return false; | 
| David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 275 |  | 
| George Karpenkov | eae57a2 | 2018-08-29 20:29:39 +0000 | [diff] [blame] | 276 | if (auto LV = V.getAs<nonloc::LazyCompoundVal>()) { | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 277 | const LazyCompoundValData *D = LV->getCVData(); | 
| Benjamin Kramer | d1d76b2 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 278 | FindUninitializedField F(C.getState()->getStateManager().getStoreManager(), | 
| Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 279 | C.getSValBuilder().getRegionManager(), | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 280 | D->getStore()); | 
|  | 281 |  | 
|  | 282 | if (F.Find(D->getRegion())) { | 
| Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 283 | if (ExplodedNode *N = C.generateErrorNode()) { | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 284 | LazyInit_BT(BD, BT); | 
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 285 | SmallString<512> Str; | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 286 | llvm::raw_svector_ostream os(Str); | 
|  | 287 | os << "Passed-by-value struct argument contains uninitialized data"; | 
|  | 288 |  | 
|  | 289 | if (F.FieldChain.size() == 1) | 
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 290 | os << " (e.g., field: '" << *F.FieldChain[0] << "')"; | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 291 | else { | 
|  | 292 | os << " (e.g., via the field chain: '"; | 
|  | 293 | bool first = true; | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 294 | for (SmallVectorImpl<const FieldDecl *>::iterator | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 295 | DI = F.FieldChain.begin(), DE = F.FieldChain.end(); DI!=DE;++DI){ | 
|  | 296 | if (first) | 
|  | 297 | first = false; | 
|  | 298 | else | 
|  | 299 | os << '.'; | 
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 300 | os << **DI; | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 301 | } | 
|  | 302 | os << "')"; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | // Generate a report for this bug. | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 306 | auto R = llvm::make_unique<BugReport>(*BT, os.str(), N); | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 307 | R->addRange(ArgRange); | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 308 |  | 
| George Karpenkov | 574d78e | 2018-08-29 22:48:50 +0000 | [diff] [blame] | 309 | if (ArgEx) | 
| George Karpenkov | b2cf006 | 2018-10-23 18:24:53 +0000 | [diff] [blame] | 310 | bugreporter::trackExpressionValue(N, ArgEx, *R); | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 311 | // FIXME: enhance track back for uninitialized value for arbitrary | 
|  | 312 | // memregions | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 313 | C.emitReport(std::move(R)); | 
| Ted Kremenek | c342c9c | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 314 | } | 
|  | 315 | return true; | 
|  | 316 | } | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | return false; | 
|  | 320 | } | 
|  | 321 |  | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 322 | void CallAndMessageChecker::checkPreStmt(const CallExpr *CE, | 
|  | 323 | CheckerContext &C) const{ | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 324 |  | 
| Ted Kremenek | caf2c51 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 325 | const Expr *Callee = CE->getCallee()->IgnoreParens(); | 
| Jordan Rose | 2995349 | 2012-07-02 19:27:46 +0000 | [diff] [blame] | 326 | ProgramStateRef State = C.getState(); | 
| Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 327 | const LocationContext *LCtx = C.getLocationContext(); | 
| Jordan Rose | 2995349 | 2012-07-02 19:27:46 +0000 | [diff] [blame] | 328 | SVal L = State->getSVal(Callee, LCtx); | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 329 |  | 
| Ted Kremenek | caf2c51 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 330 | if (L.isUndef()) { | 
|  | 331 | if (!BT_call_undef) | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 332 | BT_call_undef.reset(new BuiltinBug( | 
| Chih-Hung Hsieh | d089186 | 2016-03-23 16:14:12 +0000 | [diff] [blame] | 333 | this, "Called function pointer is an uninitialized pointer value")); | 
| Jordan Rose | 92e1449 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 334 | emitBadCall(BT_call_undef.get(), C, Callee); | 
| Ted Kremenek | caf2c51 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 335 | return; | 
|  | 336 | } | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 337 |  | 
| Jordan Rose | a01741f | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 338 | ProgramStateRef StNonNull, StNull; | 
| Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 339 | std::tie(StNonNull, StNull) = State->assume(L.castAs<DefinedOrUnknownSVal>()); | 
| Jordan Rose | a01741f | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 340 |  | 
| Jordan Rose | a01741f | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 341 | if (StNull && !StNonNull) { | 
| Ted Kremenek | caf2c51 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 342 | if (!BT_call_null) | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 343 | BT_call_null.reset(new BuiltinBug( | 
|  | 344 | this, "Called function pointer is null (null dereference)")); | 
| Jordan Rose | 92e1449 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 345 | emitBadCall(BT_call_null.get(), C, Callee); | 
| Jordan Rose | 44e066c | 2013-10-02 01:20:28 +0000 | [diff] [blame] | 346 | return; | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 347 | } | 
| Jordan Rose | 83e4049 | 2012-08-15 21:56:23 +0000 | [diff] [blame] | 348 |  | 
|  | 349 | C.addTransition(StNonNull); | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 350 | } | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 351 |  | 
| Jordan Rose | 867b185e | 2013-08-09 00:55:47 +0000 | [diff] [blame] | 352 | void CallAndMessageChecker::checkPreStmt(const CXXDeleteExpr *DE, | 
|  | 353 | CheckerContext &C) const { | 
|  | 354 |  | 
|  | 355 | SVal Arg = C.getSVal(DE->getArgument()); | 
|  | 356 | if (Arg.isUndef()) { | 
|  | 357 | StringRef Desc; | 
| Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 358 | ExplodedNode *N = C.generateErrorNode(); | 
| Jordan Rose | 867b185e | 2013-08-09 00:55:47 +0000 | [diff] [blame] | 359 | if (!N) | 
|  | 360 | return; | 
|  | 361 | if (!BT_cxx_delete_undef) | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 362 | BT_cxx_delete_undef.reset( | 
|  | 363 | new BuiltinBug(this, "Uninitialized argument value")); | 
| Jordan Rose | 867b185e | 2013-08-09 00:55:47 +0000 | [diff] [blame] | 364 | if (DE->isArrayFormAsWritten()) | 
|  | 365 | Desc = "Argument to 'delete[]' is uninitialized"; | 
|  | 366 | else | 
|  | 367 | Desc = "Argument to 'delete' is uninitialized"; | 
|  | 368 | BugType *BT = BT_cxx_delete_undef.get(); | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 369 | auto R = llvm::make_unique<BugReport>(*BT, Desc, N); | 
| George Karpenkov | b2cf006 | 2018-10-23 18:24:53 +0000 | [diff] [blame] | 370 | bugreporter::trackExpressionValue(N, DE, *R); | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 371 | C.emitReport(std::move(R)); | 
| Jordan Rose | 867b185e | 2013-08-09 00:55:47 +0000 | [diff] [blame] | 372 | return; | 
|  | 373 | } | 
|  | 374 | } | 
|  | 375 |  | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 376 | void CallAndMessageChecker::checkPreCall(const CallEvent &Call, | 
|  | 377 | CheckerContext &C) const { | 
| Jordan Rose | 83e4049 | 2012-08-15 21:56:23 +0000 | [diff] [blame] | 378 | ProgramStateRef State = C.getState(); | 
|  | 379 |  | 
| Ted Kremenek | faef9cb | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 380 | // If this is a call to a C++ method, check if the callee is null or | 
|  | 381 | // undefined. | 
| Jordan Rose | 92e1449 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 382 | if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) { | 
| Ted Kremenek | faef9cb | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 383 | SVal V = CC->getCXXThisVal(); | 
|  | 384 | if (V.isUndef()) { | 
|  | 385 | if (!BT_cxx_call_undef) | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 386 | BT_cxx_call_undef.reset( | 
|  | 387 | new BuiltinBug(this, "Called C++ object pointer is uninitialized")); | 
| Jordan Rose | 92e1449 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 388 | emitBadCall(BT_cxx_call_undef.get(), C, CC->getCXXThisExpr()); | 
| Ted Kremenek | faef9cb | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 389 | return; | 
|  | 390 | } | 
| Jordan Rose | a01741f | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 391 |  | 
| Jordan Rose | a01741f | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 392 | ProgramStateRef StNonNull, StNull; | 
| Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 393 | std::tie(StNonNull, StNull) = | 
| David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 394 | State->assume(V.castAs<DefinedOrUnknownSVal>()); | 
| Jordan Rose | a01741f | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 395 |  | 
| Jordan Rose | a01741f | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 396 | if (StNull && !StNonNull) { | 
| Ted Kremenek | faef9cb | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 397 | if (!BT_cxx_call_null) | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 398 | BT_cxx_call_null.reset( | 
|  | 399 | new BuiltinBug(this, "Called C++ object pointer is null")); | 
| Jordan Rose | 92e1449 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 400 | emitBadCall(BT_cxx_call_null.get(), C, CC->getCXXThisExpr()); | 
| Ted Kremenek | faef9cb | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 401 | return; | 
|  | 402 | } | 
| Jordan Rose | 83e4049 | 2012-08-15 21:56:23 +0000 | [diff] [blame] | 403 |  | 
|  | 404 | State = StNonNull; | 
| Ted Kremenek | faef9cb | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 405 | } | 
|  | 406 |  | 
| Pavel Labath | 963f91b | 2013-06-19 08:19:56 +0000 | [diff] [blame] | 407 | const Decl *D = Call.getDecl(); | 
| Devin Coughlin | 8693adf | 2016-11-15 18:40:46 +0000 | [diff] [blame] | 408 | if (D && (isa<FunctionDecl>(D) || isa<BlockDecl>(D))) { | 
|  | 409 | // If we have a function or block declaration, we can make sure we pass | 
|  | 410 | // enough parameters. | 
|  | 411 | unsigned Params = Call.parameters().size(); | 
| Pavel Labath | 963f91b | 2013-06-19 08:19:56 +0000 | [diff] [blame] | 412 | if (Call.getNumArgs() < Params) { | 
| Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 413 | ExplodedNode *N = C.generateErrorNode(); | 
| Pavel Labath | 963f91b | 2013-06-19 08:19:56 +0000 | [diff] [blame] | 414 | if (!N) | 
|  | 415 | return; | 
|  | 416 |  | 
|  | 417 | LazyInit_BT("Function call with too few arguments", BT_call_few_args); | 
|  | 418 |  | 
|  | 419 | SmallString<512> Str; | 
|  | 420 | llvm::raw_svector_ostream os(Str); | 
| Devin Coughlin | 8693adf | 2016-11-15 18:40:46 +0000 | [diff] [blame] | 421 | if (isa<FunctionDecl>(D)) { | 
|  | 422 | os << "Function "; | 
|  | 423 | } else { | 
|  | 424 | assert(isa<BlockDecl>(D)); | 
|  | 425 | os << "Block "; | 
|  | 426 | } | 
|  | 427 | os << "taking " << Params << " argument" | 
|  | 428 | << (Params == 1 ? "" : "s") << " is called with fewer (" | 
| Pavel Labath | 963f91b | 2013-06-19 08:19:56 +0000 | [diff] [blame] | 429 | << Call.getNumArgs() << ")"; | 
|  | 430 |  | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 431 | C.emitReport( | 
|  | 432 | llvm::make_unique<BugReport>(*BT_call_few_args, os.str(), N)); | 
| Pavel Labath | 963f91b | 2013-06-19 08:19:56 +0000 | [diff] [blame] | 433 | } | 
|  | 434 | } | 
|  | 435 |  | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 436 | // Don't check for uninitialized field values in arguments if the | 
|  | 437 | // caller has a body that is available and we have the chance to inline it. | 
|  | 438 | // This is a hack, but is a reasonable compromise betweens sometimes warning | 
|  | 439 | // and sometimes not depending on if we decide to inline a function. | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 440 | const bool checkUninitFields = | 
| Ted Kremenek | faef9cb | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 441 | !(C.getAnalysisManager().shouldInlineCall() && (D && D->getBody())); | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 442 |  | 
| Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 443 | std::unique_ptr<BugType> *BT; | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 444 | if (isa<ObjCMethodCall>(Call)) | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 445 | BT = &BT_msg_arg; | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 446 | else | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 447 | BT = &BT_call_arg; | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 448 |  | 
| Devin Coughlin | 8693adf | 2016-11-15 18:40:46 +0000 | [diff] [blame] | 449 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 450 | for (unsigned i = 0, e = Call.getNumArgs(); i != e; ++i) { | 
| Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 451 | const ParmVarDecl *ParamDecl = nullptr; | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 452 | if(FD && i < FD->getNumParams()) | 
|  | 453 | ParamDecl = FD->getParamDecl(i); | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 454 | if (PreVisitProcessArg(C, Call.getArgSVal(i), Call.getArgSourceRange(i), | 
| Daniel Marjamaki | 3d8d6ed | 2017-03-08 15:22:24 +0000 | [diff] [blame] | 455 | Call.getArgExpr(i), i, | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 456 | checkUninitFields, Call, *BT, ParamDecl)) | 
| Jordan Rose | 682b316 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 457 | return; | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 458 | } | 
| Jordan Rose | 83e4049 | 2012-08-15 21:56:23 +0000 | [diff] [blame] | 459 |  | 
|  | 460 | // If we make it here, record our assumptions about the callee. | 
|  | 461 | C.addTransition(State); | 
| Ted Kremenek | f7adea4 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 462 | } | 
|  | 463 |  | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 464 | void CallAndMessageChecker::checkPreObjCMessage(const ObjCMethodCall &msg, | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 465 | CheckerContext &C) const { | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 466 | SVal recVal = msg.getReceiverSVal(); | 
|  | 467 | if (recVal.isUndef()) { | 
| Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 468 | if (ExplodedNode *N = C.generateErrorNode()) { | 
| Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 469 | BugType *BT = nullptr; | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 470 | switch (msg.getMessageKind()) { | 
|  | 471 | case OCM_Message: | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 472 | if (!BT_msg_undef) | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 473 | BT_msg_undef.reset(new BuiltinBug(this, | 
|  | 474 | "Receiver in message expression " | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 475 | "is an uninitialized value")); | 
|  | 476 | BT = BT_msg_undef.get(); | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 477 | break; | 
|  | 478 | case OCM_PropertyAccess: | 
|  | 479 | if (!BT_objc_prop_undef) | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 480 | BT_objc_prop_undef.reset(new BuiltinBug( | 
|  | 481 | this, "Property access on an uninitialized object pointer")); | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 482 | BT = BT_objc_prop_undef.get(); | 
|  | 483 | break; | 
|  | 484 | case OCM_Subscript: | 
|  | 485 | if (!BT_objc_subscript_undef) | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 486 | BT_objc_subscript_undef.reset(new BuiltinBug( | 
|  | 487 | this, "Subscript access on an uninitialized object pointer")); | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 488 | BT = BT_objc_subscript_undef.get(); | 
|  | 489 | break; | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 490 | } | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 491 | assert(BT && "Unknown message kind."); | 
|  | 492 |  | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 493 | auto R = llvm::make_unique<BugReport>(*BT, BT->getName(), N); | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 494 | const ObjCMessageExpr *ME = msg.getOriginExpr(); | 
|  | 495 | R->addRange(ME->getReceiverRange()); | 
| Ted Kremenek | f7adea4 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 496 |  | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 497 | // FIXME: getTrackNullOrUndefValueVisitor can't handle "super" yet. | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 498 | if (const Expr *ReceiverE = ME->getInstanceReceiver()) | 
| George Karpenkov | b2cf006 | 2018-10-23 18:24:53 +0000 | [diff] [blame] | 499 | bugreporter::trackExpressionValue(N, ReceiverE, *R); | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 500 | C.emitReport(std::move(R)); | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 501 | } | 
|  | 502 | return; | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 503 | } | 
| Zhongxing Xu | af35329 | 2009-12-02 05:49:12 +0000 | [diff] [blame] | 504 | } | 
| Zhongxing Xu | 9e20079 | 2009-11-24 07:06:39 +0000 | [diff] [blame] | 505 |  | 
| Devin Coughlin | ca5ab2b | 2015-09-15 01:13:53 +0000 | [diff] [blame] | 506 | void CallAndMessageChecker::checkObjCMessageNil(const ObjCMethodCall &msg, | 
|  | 507 | CheckerContext &C) const { | 
|  | 508 | HandleNilReceiver(C, C.getState(), msg); | 
|  | 509 | } | 
|  | 510 |  | 
| Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 511 | void CallAndMessageChecker::emitNilReceiverBug(CheckerContext &C, | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 512 | const ObjCMethodCall &msg, | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 513 | ExplodedNode *N) const { | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 514 |  | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 515 | if (!BT_msg_ret) | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 516 | BT_msg_ret.reset( | 
| Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 517 | new BuiltinBug(this, "Receiver in message expression is 'nil'")); | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 518 |  | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 519 | const ObjCMessageExpr *ME = msg.getOriginExpr(); | 
|  | 520 |  | 
| Anna Zaks | c610bca | 2013-04-03 19:28:19 +0000 | [diff] [blame] | 521 | QualType ResTy = msg.getResultType(); | 
|  | 522 |  | 
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 523 | SmallString<200> buf; | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 524 | llvm::raw_svector_ostream os(buf); | 
| Aaron Ballman | b190f97 | 2014-01-03 17:59:55 +0000 | [diff] [blame] | 525 | os << "The receiver of message '"; | 
|  | 526 | ME->getSelector().print(os); | 
|  | 527 | os << "' is nil"; | 
| Anna Zaks | c610bca | 2013-04-03 19:28:19 +0000 | [diff] [blame] | 528 | if (ResTy->isReferenceType()) { | 
|  | 529 | os << ", which results in forming a null reference"; | 
|  | 530 | } else { | 
|  | 531 | os << " and returns a value of type '"; | 
|  | 532 | msg.getResultType().print(os, C.getLangOpts()); | 
|  | 533 | os << "' that will be garbage"; | 
|  | 534 | } | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 535 |  | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 536 | auto report = llvm::make_unique<BugReport>(*BT_msg_ret, os.str(), N); | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 537 | report->addRange(ME->getReceiverRange()); | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 538 | // FIXME: This won't track "self" in messages to super. | 
| Jordan Rose | 627b046 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 539 | if (const Expr *receiver = ME->getInstanceReceiver()) { | 
| George Karpenkov | b2cf006 | 2018-10-23 18:24:53 +0000 | [diff] [blame] | 540 | bugreporter::trackExpressionValue(N, receiver, *report); | 
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 541 | } | 
| Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 542 | C.emitReport(std::move(report)); | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 543 | } | 
|  | 544 |  | 
| Ted Kremenek | 90af909 | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 545 | static bool supportsNilWithFloatRet(const llvm::Triple &triple) { | 
| Bob Wilson | c333749 | 2012-01-31 23:52:54 +0000 | [diff] [blame] | 546 | return (triple.getVendor() == llvm::Triple::Apple && | 
| Tim Northover | 6f3ff22 | 2015-10-30 16:30:27 +0000 | [diff] [blame] | 547 | (triple.isiOS() || triple.isWatchOS() || | 
|  | 548 | !triple.isMacOSXVersionLT(10,5))); | 
| Ted Kremenek | 1fc1f20 | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 549 | } | 
|  | 550 |  | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 551 | void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C, | 
| Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 552 | ProgramStateRef state, | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 553 | const ObjCMethodCall &Msg) const { | 
| Argyrios Kyrtzidis | 37ab726 | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 554 | ASTContext &Ctx = C.getASTContext(); | 
| Anton Yartsev | 6a61922 | 2014-02-17 18:25:34 +0000 | [diff] [blame] | 555 | static CheckerProgramPointTag Tag(this, "NilReceiver"); | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 556 |  | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 557 | // Check the return type of the message expression.  A message to nil will | 
|  | 558 | // return different values depending on the return type and the architecture. | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 559 | QualType RetTy = Msg.getResultType(); | 
| Ted Kremenek | 1fc1f20 | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 560 | CanQualType CanRetTy = Ctx.getCanonicalType(RetTy); | 
| Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 561 | const LocationContext *LCtx = C.getLocationContext(); | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 562 |  | 
| Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 563 | if (CanRetTy->isStructureOrClassType()) { | 
| Ted Kremenek | a98358e | 2011-10-28 19:05:10 +0000 | [diff] [blame] | 564 | // Structure returns are safe since the compiler zeroes them out. | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 565 | SVal V = C.getSValBuilder().makeZeroVal(RetTy); | 
| Anna Zaks | 22fa6ec | 2013-03-27 17:35:58 +0000 | [diff] [blame] | 566 | C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V), &Tag); | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 567 | return; | 
|  | 568 | } | 
|  | 569 |  | 
| Ted Kremenek | a98358e | 2011-10-28 19:05:10 +0000 | [diff] [blame] | 570 | // Other cases: check if sizeof(return type) > sizeof(void*) | 
| Anna Zaks | 1c887b3 | 2011-11-01 22:41:01 +0000 | [diff] [blame] | 571 | if (CanRetTy != Ctx.VoidTy && C.getLocationContext()->getParentMap() | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 572 | .isConsumedExpr(Msg.getOriginExpr())) { | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 573 | // Compute: sizeof(void *) and sizeof(return type) | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 574 | const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy); | 
| Ted Kremenek | 1fc1f20 | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 575 | const uint64_t returnTypeSize = Ctx.getTypeSize(CanRetTy); | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 576 |  | 
| Anna Zaks | c610bca | 2013-04-03 19:28:19 +0000 | [diff] [blame] | 577 | if (CanRetTy.getTypePtr()->isReferenceType()|| | 
|  | 578 | (voidPtrSize < returnTypeSize && | 
|  | 579 | !(supportsNilWithFloatRet(Ctx.getTargetInfo().getTriple()) && | 
|  | 580 | (Ctx.FloatTy == CanRetTy || | 
|  | 581 | Ctx.DoubleTy == CanRetTy || | 
|  | 582 | Ctx.LongDoubleTy == CanRetTy || | 
|  | 583 | Ctx.LongLongTy == CanRetTy || | 
|  | 584 | Ctx.UnsignedLongLongTy == CanRetTy)))) { | 
| Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 585 | if (ExplodedNode *N = C.generateErrorNode(state, &Tag)) | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 586 | emitNilReceiverBug(C, Msg, N); | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 587 | return; | 
|  | 588 | } | 
|  | 589 |  | 
|  | 590 | // Handle the safe cases where the return value is 0 if the | 
|  | 591 | // receiver is nil. | 
|  | 592 | // | 
|  | 593 | // FIXME: For now take the conservative approach that we only | 
|  | 594 | // return null values if we *know* that the receiver is nil. | 
|  | 595 | // This is because we can have surprises like: | 
|  | 596 | // | 
|  | 597 | //   ... = [[NSScreens screens] objectAtIndex:0]; | 
|  | 598 | // | 
|  | 599 | // What can happen is that [... screens] could return nil, but | 
|  | 600 | // it most likely isn't nil.  We should assume the semantics | 
|  | 601 | // of this case unless we have *a lot* more knowledge. | 
|  | 602 | // | 
| Jordan Rose | 547060b | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 603 | SVal V = C.getSValBuilder().makeZeroVal(RetTy); | 
| Anna Zaks | 22fa6ec | 2013-03-27 17:35:58 +0000 | [diff] [blame] | 604 | C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V), &Tag); | 
| Ted Kremenek | 005e8a0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 605 | return; | 
|  | 606 | } | 
| Ted Kremenek | 9c05f4e | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 607 |  | 
| Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 608 | C.addTransition(state); | 
| Zhongxing Xu | ab162e1 | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 609 | } | 
| Argyrios Kyrtzidis | 6d6801c5 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 610 |  | 
| Jordan Rose | 821a3a0 | 2014-03-13 17:55:39 +0000 | [diff] [blame] | 611 | #define REGISTER_CHECKER(name)                                                 \ | 
|  | 612 | void ento::register##name(CheckerManager &mgr) {                             \ | 
|  | 613 | CallAndMessageChecker *Checker =                                           \ | 
|  | 614 | mgr.registerChecker<CallAndMessageChecker>();                          \ | 
|  | 615 | Checker->Filter.Check_##name = true;                                       \ | 
|  | 616 | Checker->Filter.CheckName_##name = mgr.getCurrentCheckName();              \ | 
|  | 617 | } | 
|  | 618 |  | 
|  | 619 | REGISTER_CHECKER(CallAndMessageUnInitRefArg) | 
|  | 620 | REGISTER_CHECKER(CallAndMessageChecker) |