Zhongxing Xu | d02174c | 2009-11-24 04:45:44 +0000 | [diff] [blame] | 1 | //===--- CallAndMessageChecker.cpp ------------------------------*- C++ -*--==// |
Zhongxing Xu | 8958fff | 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 | d02174c | 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 | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Jordan Rose | f540c54 | 2012-07-26 21:39:41 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 20 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 21 | #include "clang/AST/ParentMap.h" |
| 22 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 26 | using namespace ento; |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 28 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 29 | class CallAndMessageChecker |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 30 | : public Checker< check::PreStmt<CallExpr>, check::PreObjCMessage, |
| 31 | check::PreCall > { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 32 | mutable OwningPtr<BugType> BT_call_null; |
| 33 | mutable OwningPtr<BugType> BT_call_undef; |
Ted Kremenek | fc999ac | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 34 | mutable OwningPtr<BugType> BT_cxx_call_null; |
| 35 | mutable OwningPtr<BugType> BT_cxx_call_undef; |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 36 | mutable OwningPtr<BugType> BT_call_arg; |
| 37 | mutable OwningPtr<BugType> BT_msg_undef; |
Ted Kremenek | b673a41 | 2012-02-18 20:53:30 +0000 | [diff] [blame] | 38 | mutable OwningPtr<BugType> BT_objc_prop_undef; |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 39 | mutable OwningPtr<BugType> BT_objc_subscript_undef; |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 40 | mutable OwningPtr<BugType> BT_msg_arg; |
| 41 | mutable OwningPtr<BugType> BT_msg_ret; |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 42 | public: |
Zhongxing Xu | 2055eff | 2009-11-24 07:06:39 +0000 | [diff] [blame] | 43 | |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 44 | void checkPreStmt(const CallExpr *CE, CheckerContext &C) const; |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 45 | void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const; |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 46 | void checkPreCall(const CallEvent &Call, CheckerContext &C) const; |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 48 | private: |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 49 | static bool PreVisitProcessArg(CheckerContext &C, SVal V, |
| 50 | SourceRange argRange, const Expr *argEx, |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 51 | bool IsFirstArgument, bool checkUninitFields, |
| 52 | const CallEvent &Call, OwningPtr<BugType> &BT); |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 53 | |
Jordan Rose | 9da59a6 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 54 | static void emitBadCall(BugType *BT, CheckerContext &C, const Expr *BadE); |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 55 | void emitNilReceiverBug(CheckerContext &C, const ObjCMethodCall &msg, |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 56 | ExplodedNode *N) const; |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 58 | void HandleNilReceiver(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 59 | ProgramStateRef state, |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 60 | const ObjCMethodCall &msg) const; |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 61 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 62 | static void LazyInit_BT(const char *desc, OwningPtr<BugType> &BT) { |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 63 | if (!BT) |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 64 | BT.reset(new BuiltinBug(desc)); |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 65 | } |
Ted Kremenek | f493f49 | 2009-11-11 05:50:44 +0000 | [diff] [blame] | 66 | }; |
| 67 | } // end anonymous namespace |
| 68 | |
Jordan Rose | 9da59a6 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 69 | void CallAndMessageChecker::emitBadCall(BugType *BT, CheckerContext &C, |
| 70 | const Expr *BadE) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 71 | ExplodedNode *N = C.generateSink(); |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 72 | if (!N) |
| 73 | return; |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 74 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 75 | BugReport *R = new BugReport(*BT, BT->getName(), N); |
Jordan Rose | 9da59a6 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 76 | if (BadE) { |
| 77 | R->addRange(BadE->getSourceRange()); |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 78 | bugreporter::trackNullOrUndefValue(N, BadE, *R); |
Jordan Rose | 9da59a6 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 79 | } |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 80 | C.EmitReport(R); |
| 81 | } |
| 82 | |
Benjamin Kramer | da88536 | 2012-09-10 11:57:16 +0000 | [diff] [blame] | 83 | static StringRef describeUninitializedArgumentInCall(const CallEvent &Call, |
| 84 | bool IsFirstArgument) { |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 85 | switch (Call.getKind()) { |
| 86 | case CE_ObjCMessage: { |
| 87 | const ObjCMethodCall &Msg = cast<ObjCMethodCall>(Call); |
| 88 | switch (Msg.getMessageKind()) { |
| 89 | case OCM_Message: |
| 90 | return "Argument in message expression is an uninitialized value"; |
| 91 | case OCM_PropertyAccess: |
| 92 | assert(Msg.isSetter() && "Getters have no args"); |
| 93 | return "Argument for property setter is an uninitialized value"; |
| 94 | case OCM_Subscript: |
| 95 | if (Msg.isSetter() && IsFirstArgument) |
| 96 | return "Argument for subscript setter is an uninitialized value"; |
| 97 | return "Subscript index is an uninitialized value"; |
| 98 | } |
| 99 | llvm_unreachable("Unknown message kind."); |
| 100 | } |
| 101 | case CE_Block: |
| 102 | return "Block call argument is an uninitialized value"; |
| 103 | default: |
| 104 | return "Function call argument is an uninitialized value"; |
| 105 | } |
| 106 | } |
| 107 | |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 108 | bool CallAndMessageChecker::PreVisitProcessArg(CheckerContext &C, |
Argyrios Kyrtzidis | 432424d | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 109 | SVal V, SourceRange argRange, |
| 110 | const Expr *argEx, |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 111 | bool IsFirstArgument, |
| 112 | bool checkUninitFields, |
| 113 | const CallEvent &Call, |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 114 | OwningPtr<BugType> &BT) { |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 115 | if (V.isUndef()) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 116 | if (ExplodedNode *N = C.generateSink()) { |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 117 | LazyInit_BT("Uninitialized argument value", BT); |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 118 | |
| 119 | // Generate a report for this bug. |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 120 | StringRef Desc = describeUninitializedArgumentInCall(Call, |
| 121 | IsFirstArgument); |
| 122 | BugReport *R = new BugReport(*BT, Desc, N); |
Argyrios Kyrtzidis | 432424d | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 123 | R->addRange(argRange); |
| 124 | if (argEx) |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 125 | bugreporter::trackNullOrUndefValue(N, argEx, *R); |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 126 | C.EmitReport(R); |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
Ted Kremenek | e4d653b | 2012-03-05 23:57:14 +0000 | [diff] [blame] | 131 | if (!checkUninitFields) |
| 132 | return false; |
| 133 | |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 134 | if (const nonloc::LazyCompoundVal *LV = |
| 135 | dyn_cast<nonloc::LazyCompoundVal>(&V)) { |
| 136 | |
| 137 | class FindUninitializedField { |
| 138 | public: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 139 | SmallVector<const FieldDecl *, 10> FieldChain; |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 140 | private: |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 141 | StoreManager &StoreMgr; |
| 142 | MemRegionManager &MrMgr; |
| 143 | Store store; |
| 144 | public: |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 145 | FindUninitializedField(StoreManager &storeMgr, |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 146 | MemRegionManager &mrMgr, Store s) |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 147 | : StoreMgr(storeMgr), MrMgr(mrMgr), store(s) {} |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 149 | bool Find(const TypedValueRegion *R) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 150 | QualType T = R->getValueType(); |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 151 | if (const RecordType *RT = T->getAsStructureType()) { |
| 152 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 153 | assert(RD && "Referred record has no definition"); |
| 154 | for (RecordDecl::field_iterator I = |
| 155 | RD->field_begin(), E = RD->field_end(); I!=E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 156 | const FieldRegion *FR = MrMgr.getFieldRegion(*I, R); |
| 157 | FieldChain.push_back(*I); |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 158 | T = I->getType(); |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 159 | if (T->getAsStructureType()) { |
| 160 | if (Find(FR)) |
| 161 | return true; |
| 162 | } |
| 163 | else { |
Anna Zaks | 1437425 | 2012-01-12 02:22:40 +0000 | [diff] [blame] | 164 | const SVal &V = StoreMgr.getBinding(store, loc::MemRegionVal(FR)); |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 165 | if (V.isUndef()) |
| 166 | return true; |
| 167 | } |
| 168 | FieldChain.pop_back(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return false; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | const LazyCompoundValData *D = LV->getCVData(); |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 177 | FindUninitializedField F(C.getState()->getStateManager().getStoreManager(), |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 178 | C.getSValBuilder().getRegionManager(), |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 179 | D->getStore()); |
| 180 | |
| 181 | if (F.Find(D->getRegion())) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 182 | if (ExplodedNode *N = C.generateSink()) { |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 183 | LazyInit_BT("Uninitialized argument value", BT); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 184 | SmallString<512> Str; |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 185 | llvm::raw_svector_ostream os(Str); |
| 186 | os << "Passed-by-value struct argument contains uninitialized data"; |
| 187 | |
| 188 | if (F.FieldChain.size() == 1) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 189 | os << " (e.g., field: '" << *F.FieldChain[0] << "')"; |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 190 | else { |
| 191 | os << " (e.g., via the field chain: '"; |
| 192 | bool first = true; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 193 | for (SmallVectorImpl<const FieldDecl *>::iterator |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 194 | DI = F.FieldChain.begin(), DE = F.FieldChain.end(); DI!=DE;++DI){ |
| 195 | if (first) |
| 196 | first = false; |
| 197 | else |
| 198 | os << '.'; |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 199 | os << **DI; |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 200 | } |
| 201 | os << "')"; |
| 202 | } |
| 203 | |
| 204 | // Generate a report for this bug. |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 205 | BugReport *R = new BugReport(*BT, os.str(), N); |
Argyrios Kyrtzidis | 432424d | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 206 | R->addRange(argRange); |
Ted Kremenek | 8133716 | 2010-03-18 03:22:29 +0000 | [diff] [blame] | 207 | |
| 208 | // FIXME: enhance track back for uninitialized value for arbitrary |
| 209 | // memregions |
| 210 | C.EmitReport(R); |
| 211 | } |
| 212 | return true; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 219 | void CallAndMessageChecker::checkPreStmt(const CallExpr *CE, |
| 220 | CheckerContext &C) const{ |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 222 | const Expr *Callee = CE->getCallee()->IgnoreParens(); |
Jordan Rose | 55037cd | 2012-07-02 19:27:46 +0000 | [diff] [blame] | 223 | ProgramStateRef State = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 224 | const LocationContext *LCtx = C.getLocationContext(); |
Jordan Rose | 55037cd | 2012-07-02 19:27:46 +0000 | [diff] [blame] | 225 | SVal L = State->getSVal(Callee, LCtx); |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 227 | if (L.isUndef()) { |
| 228 | if (!BT_call_undef) |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 229 | BT_call_undef.reset(new BuiltinBug("Called function pointer is an " |
| 230 | "uninitalized pointer value")); |
Jordan Rose | 9da59a6 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 231 | emitBadCall(BT_call_undef.get(), C, Callee); |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 232 | return; |
| 233 | } |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 234 | |
Jordan Rose | a869518 | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 235 | ProgramStateRef StNonNull, StNull; |
| 236 | llvm::tie(StNonNull, StNull) = State->assume(cast<DefinedOrUnknownSVal>(L)); |
| 237 | |
Jordan Rose | a869518 | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 238 | if (StNull && !StNonNull) { |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 239 | if (!BT_call_null) |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 240 | BT_call_null.reset( |
| 241 | new BuiltinBug("Called function pointer is null (null dereference)")); |
Jordan Rose | 9da59a6 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 242 | emitBadCall(BT_call_null.get(), C, Callee); |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 243 | } |
Jordan Rose | 7f66085 | 2012-08-15 21:56:23 +0000 | [diff] [blame] | 244 | |
| 245 | C.addTransition(StNonNull); |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 246 | } |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 247 | |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 248 | void CallAndMessageChecker::checkPreCall(const CallEvent &Call, |
| 249 | CheckerContext &C) const { |
Jordan Rose | 7f66085 | 2012-08-15 21:56:23 +0000 | [diff] [blame] | 250 | ProgramStateRef State = C.getState(); |
| 251 | |
Ted Kremenek | fc999ac | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 252 | // If this is a call to a C++ method, check if the callee is null or |
| 253 | // undefined. |
Jordan Rose | 9da59a6 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 254 | if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) { |
Ted Kremenek | fc999ac | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 255 | SVal V = CC->getCXXThisVal(); |
| 256 | if (V.isUndef()) { |
| 257 | if (!BT_cxx_call_undef) |
| 258 | BT_cxx_call_undef.reset(new BuiltinBug("Called C++ object pointer is " |
| 259 | "uninitialized")); |
Jordan Rose | 9da59a6 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 260 | emitBadCall(BT_cxx_call_undef.get(), C, CC->getCXXThisExpr()); |
Ted Kremenek | fc999ac | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 261 | return; |
| 262 | } |
Jordan Rose | a869518 | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 263 | |
Jordan Rose | a869518 | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 264 | ProgramStateRef StNonNull, StNull; |
| 265 | llvm::tie(StNonNull, StNull) = State->assume(cast<DefinedOrUnknownSVal>(V)); |
| 266 | |
Jordan Rose | a869518 | 2012-08-04 01:04:52 +0000 | [diff] [blame] | 267 | if (StNull && !StNonNull) { |
Ted Kremenek | fc999ac | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 268 | if (!BT_cxx_call_null) |
| 269 | BT_cxx_call_null.reset(new BuiltinBug("Called C++ object pointer " |
| 270 | "is null")); |
Jordan Rose | 9da59a6 | 2012-08-03 23:08:49 +0000 | [diff] [blame] | 271 | emitBadCall(BT_cxx_call_null.get(), C, CC->getCXXThisExpr()); |
Ted Kremenek | fc999ac | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 272 | return; |
| 273 | } |
Jordan Rose | 7f66085 | 2012-08-15 21:56:23 +0000 | [diff] [blame] | 274 | |
| 275 | State = StNonNull; |
Ted Kremenek | fc999ac | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 278 | // Don't check for uninitialized field values in arguments if the |
| 279 | // caller has a body that is available and we have the chance to inline it. |
| 280 | // This is a hack, but is a reasonable compromise betweens sometimes warning |
| 281 | // and sometimes not depending on if we decide to inline a function. |
| 282 | const Decl *D = Call.getDecl(); |
| 283 | const bool checkUninitFields = |
Ted Kremenek | fc999ac | 2012-07-26 00:22:32 +0000 | [diff] [blame] | 284 | !(C.getAnalysisManager().shouldInlineCall() && (D && D->getBody())); |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 285 | |
| 286 | OwningPtr<BugType> *BT; |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 287 | if (isa<ObjCMethodCall>(Call)) |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 288 | BT = &BT_msg_arg; |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 289 | else |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 290 | BT = &BT_call_arg; |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 291 | |
| 292 | for (unsigned i = 0, e = Call.getNumArgs(); i != e; ++i) |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 293 | if (PreVisitProcessArg(C, Call.getArgSVal(i), Call.getArgSourceRange(i), |
| 294 | Call.getArgExpr(i), /*IsFirstArgument=*/i == 0, |
| 295 | checkUninitFields, Call, *BT)) |
Jordan Rose | fe6a011 | 2012-07-02 19:28:21 +0000 | [diff] [blame] | 296 | return; |
Jordan Rose | 7f66085 | 2012-08-15 21:56:23 +0000 | [diff] [blame] | 297 | |
| 298 | // If we make it here, record our assumptions about the callee. |
| 299 | C.addTransition(State); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 302 | void CallAndMessageChecker::checkPreObjCMessage(const ObjCMethodCall &msg, |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 303 | CheckerContext &C) const { |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 304 | SVal recVal = msg.getReceiverSVal(); |
| 305 | if (recVal.isUndef()) { |
| 306 | if (ExplodedNode *N = C.generateSink()) { |
| 307 | BugType *BT = 0; |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 308 | switch (msg.getMessageKind()) { |
| 309 | case OCM_Message: |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 310 | if (!BT_msg_undef) |
| 311 | BT_msg_undef.reset(new BuiltinBug("Receiver in message expression " |
| 312 | "is an uninitialized value")); |
| 313 | BT = BT_msg_undef.get(); |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 314 | break; |
| 315 | case OCM_PropertyAccess: |
| 316 | if (!BT_objc_prop_undef) |
| 317 | BT_objc_prop_undef.reset(new BuiltinBug("Property access on an " |
| 318 | "uninitialized object " |
| 319 | "pointer")); |
| 320 | BT = BT_objc_prop_undef.get(); |
| 321 | break; |
| 322 | case OCM_Subscript: |
| 323 | if (!BT_objc_subscript_undef) |
| 324 | BT_objc_subscript_undef.reset(new BuiltinBug("Subscript access on an " |
| 325 | "uninitialized object " |
| 326 | "pointer")); |
| 327 | BT = BT_objc_subscript_undef.get(); |
| 328 | break; |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 329 | } |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 330 | assert(BT && "Unknown message kind."); |
| 331 | |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 332 | BugReport *R = new BugReport(*BT, BT->getName(), N); |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 333 | const ObjCMessageExpr *ME = msg.getOriginExpr(); |
| 334 | R->addRange(ME->getReceiverRange()); |
Ted Kremenek | 64fa858 | 2009-11-21 00:49:41 +0000 | [diff] [blame] | 335 | |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 336 | // FIXME: getTrackNullOrUndefValueVisitor can't handle "super" yet. |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 337 | if (const Expr *ReceiverE = ME->getInstanceReceiver()) |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 338 | bugreporter::trackNullOrUndefValue(N, ReceiverE, *R); |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 339 | C.EmitReport(R); |
| 340 | } |
| 341 | return; |
| 342 | } else { |
| 343 | // Bifurcate the state into nil and non-nil ones. |
| 344 | DefinedOrUnknownSVal receiverVal = cast<DefinedOrUnknownSVal>(recVal); |
| 345 | |
| 346 | ProgramStateRef state = C.getState(); |
| 347 | ProgramStateRef notNilState, nilState; |
| 348 | llvm::tie(notNilState, nilState) = state->assume(receiverVal); |
| 349 | |
| 350 | // Handle receiver must be nil. |
| 351 | if (nilState && !notNilState) { |
| 352 | HandleNilReceiver(C, state, msg); |
Ted Kremenek | c79d7d4 | 2009-11-21 01:25:37 +0000 | [diff] [blame] | 353 | return; |
| 354 | } |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 355 | } |
Zhongxing Xu | a46e4d9 | 2009-12-02 05:49:12 +0000 | [diff] [blame] | 356 | } |
Zhongxing Xu | 2055eff | 2009-11-24 07:06:39 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 358 | void CallAndMessageChecker::emitNilReceiverBug(CheckerContext &C, |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 359 | const ObjCMethodCall &msg, |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 360 | ExplodedNode *N) const { |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 361 | |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 362 | if (!BT_msg_ret) |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 363 | BT_msg_ret.reset( |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 364 | new BuiltinBug("Receiver in message expression is " |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 365 | "'nil' and returns a garbage value")); |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 366 | |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 367 | const ObjCMessageExpr *ME = msg.getOriginExpr(); |
| 368 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 369 | SmallString<200> buf; |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 370 | llvm::raw_svector_ostream os(buf); |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 371 | os << "The receiver of message '" << ME->getSelector().getAsString() |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 372 | << "' is nil and returns a value of type '"; |
| 373 | msg.getResultType().print(os, C.getLangOpts()); |
| 374 | os << "' that will be garbage"; |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 375 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 376 | BugReport *report = new BugReport(*BT_msg_ret, os.str(), N); |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 377 | report->addRange(ME->getReceiverRange()); |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 378 | // FIXME: This won't track "self" in messages to super. |
Jordan Rose | 8919e68 | 2012-07-18 21:59:51 +0000 | [diff] [blame] | 379 | if (const Expr *receiver = ME->getInstanceReceiver()) { |
Jordan Rose | a1f81bb | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 380 | bugreporter::trackNullOrUndefValue(N, receiver, *report); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 381 | } |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 382 | C.EmitReport(report); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 385 | static bool supportsNilWithFloatRet(const llvm::Triple &triple) { |
Bob Wilson | 8f1e656 | 2012-01-31 23:52:54 +0000 | [diff] [blame] | 386 | return (triple.getVendor() == llvm::Triple::Apple && |
| 387 | (triple.getOS() == llvm::Triple::IOS || |
| 388 | !triple.isMacOSXVersionLT(10,5))); |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 391 | void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 392 | ProgramStateRef state, |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 393 | const ObjCMethodCall &Msg) const { |
Argyrios Kyrtzidis | 432424d | 2011-01-25 00:03:53 +0000 | [diff] [blame] | 394 | ASTContext &Ctx = C.getASTContext(); |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 395 | |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 396 | // Check the return type of the message expression. A message to nil will |
| 397 | // return different values depending on the return type and the architecture. |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 398 | QualType RetTy = Msg.getResultType(); |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 399 | CanQualType CanRetTy = Ctx.getCanonicalType(RetTy); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 400 | const LocationContext *LCtx = C.getLocationContext(); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 402 | if (CanRetTy->isStructureOrClassType()) { |
Ted Kremenek | 4a037c7 | 2011-10-28 19:05:10 +0000 | [diff] [blame] | 403 | // Structure returns are safe since the compiler zeroes them out. |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 404 | SVal V = C.getSValBuilder().makeZeroVal(RetTy); |
| 405 | C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V)); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 406 | return; |
| 407 | } |
| 408 | |
Ted Kremenek | 4a037c7 | 2011-10-28 19:05:10 +0000 | [diff] [blame] | 409 | // Other cases: check if sizeof(return type) > sizeof(void*) |
Anna Zaks | a2a8603 | 2011-11-01 22:41:01 +0000 | [diff] [blame] | 410 | if (CanRetTy != Ctx.VoidTy && C.getLocationContext()->getParentMap() |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 411 | .isConsumedExpr(Msg.getOriginExpr())) { |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 412 | // Compute: sizeof(void *) and sizeof(return type) |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 413 | const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy); |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 414 | const uint64_t returnTypeSize = Ctx.getTypeSize(CanRetTy); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 416 | if (voidPtrSize < returnTypeSize && |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 417 | !(supportsNilWithFloatRet(Ctx.getTargetInfo().getTriple()) && |
Ted Kremenek | f81330c | 2009-11-24 22:48:18 +0000 | [diff] [blame] | 418 | (Ctx.FloatTy == CanRetTy || |
| 419 | Ctx.DoubleTy == CanRetTy || |
| 420 | Ctx.LongDoubleTy == CanRetTy || |
Ted Kremenek | 6123874 | 2010-09-30 00:37:10 +0000 | [diff] [blame] | 421 | Ctx.LongLongTy == CanRetTy || |
| 422 | Ctx.UnsignedLongLongTy == CanRetTy))) { |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 423 | if (ExplodedNode *N = C.generateSink(state)) |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 424 | emitNilReceiverBug(C, Msg, N); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
| 428 | // Handle the safe cases where the return value is 0 if the |
| 429 | // receiver is nil. |
| 430 | // |
| 431 | // FIXME: For now take the conservative approach that we only |
| 432 | // return null values if we *know* that the receiver is nil. |
| 433 | // This is because we can have surprises like: |
| 434 | // |
| 435 | // ... = [[NSScreens screens] objectAtIndex:0]; |
| 436 | // |
| 437 | // What can happen is that [... screens] could return nil, but |
| 438 | // it most likely isn't nil. We should assume the semantics |
| 439 | // of this case unless we have *a lot* more knowledge. |
| 440 | // |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 441 | SVal V = C.getSValBuilder().makeZeroVal(RetTy); |
| 442 | C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V)); |
Ted Kremenek | fee96e0 | 2009-11-24 21:41:28 +0000 | [diff] [blame] | 443 | return; |
| 444 | } |
Ted Kremenek | 091b588 | 2010-03-18 02:17:27 +0000 | [diff] [blame] | 445 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 446 | C.addTransition(state); |
Zhongxing Xu | 8958fff | 2009-11-03 06:46:03 +0000 | [diff] [blame] | 447 | } |
Argyrios Kyrtzidis | d84f422 | 2011-02-28 01:28:13 +0000 | [diff] [blame] | 448 | |
| 449 | void ento::registerCallAndMessageChecker(CheckerManager &mgr) { |
| 450 | mgr.registerChecker<CallAndMessageChecker>(); |
| 451 | } |