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