blob: 907f516ac3f7ebbd95b0246b4f0dbebc6570fdc5 [file] [log] [blame]
Zhongxing Xud02174c2009-11-24 04:45:44 +00001//===--- CallAndMessageChecker.cpp ------------------------------*- C++ -*--==//
Zhongxing Xu8958fff2009-11-03 06:46:03 +00002//
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 Xud02174c2009-11-24 04:45:44 +000010// This defines CallAndMessageChecker, a builtin checker that checks for various
11// errors of call and objc message expressions.
Zhongxing Xu8958fff2009-11-03 06:46:03 +000012//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +000015#include "ClangSACheckers.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/AST/ParentMap.h"
17#include "clang/Basic/TargetInfo.h"
18#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000019#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +000020#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rosef540c542012-07-26 21:39:41 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Benjamin Kramera93d0f22012-12-01 17:12:56 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu8958fff2009-11-03 06:46:03 +000025
26using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000027using namespace ento;
Zhongxing Xu8958fff2009-11-03 06:46:03 +000028
Ted Kremenekf493f492009-11-11 05:50:44 +000029namespace {
Stephen Hines651f13c2014-04-23 16:59:28 -070030
31struct ChecksFilter {
32 DefaultBool Check_CallAndMessageUnInitRefArg;
33 DefaultBool Check_CallAndMessageChecker;
34
35 CheckName CheckName_CallAndMessageUnInitRefArg;
36 CheckName CheckName_CallAndMessageChecker;
37};
38
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000039class CallAndMessageChecker
Jordan Rosefa220f52013-08-09 00:55:47 +000040 : public Checker< check::PreStmt<CallExpr>,
41 check::PreStmt<CXXDeleteExpr>,
42 check::PreObjCMessage,
Jordan Rosefe6a0112012-07-02 19:28:21 +000043 check::PreCall > {
Stephen Hines651f13c2014-04-23 16:59:28 -070044 mutable std::unique_ptr<BugType> BT_call_null;
45 mutable std::unique_ptr<BugType> BT_call_undef;
46 mutable std::unique_ptr<BugType> BT_cxx_call_null;
47 mutable std::unique_ptr<BugType> BT_cxx_call_undef;
48 mutable std::unique_ptr<BugType> BT_call_arg;
49 mutable std::unique_ptr<BugType> BT_cxx_delete_undef;
50 mutable std::unique_ptr<BugType> BT_msg_undef;
51 mutable std::unique_ptr<BugType> BT_objc_prop_undef;
52 mutable std::unique_ptr<BugType> BT_objc_subscript_undef;
53 mutable std::unique_ptr<BugType> BT_msg_arg;
54 mutable std::unique_ptr<BugType> BT_msg_ret;
55 mutable std::unique_ptr<BugType> BT_call_few_args;
56
Ted Kremenekf493f492009-11-11 05:50:44 +000057public:
Stephen Hines651f13c2014-04-23 16:59:28 -070058 ChecksFilter Filter;
Zhongxing Xu2055eff2009-11-24 07:06:39 +000059
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +000060 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Jordan Rosefa220f52013-08-09 00:55:47 +000061 void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const;
Jordan Rosede507ea2012-07-02 19:28:04 +000062 void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
Jordan Rosefe6a0112012-07-02 19:28:21 +000063 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Ted Kremenekfee96e02009-11-24 21:41:28 +000064
Ted Kremenekc79d7d42009-11-21 01:25:37 +000065private:
Stephen Hines651f13c2014-04-23 16:59:28 -070066 bool PreVisitProcessArg(CheckerContext &C, SVal V, SourceRange ArgRange,
67 const Expr *ArgEx, bool IsFirstArgument,
68 bool CheckUninitFields, const CallEvent &Call,
69 std::unique_ptr<BugType> &BT,
70 const ParmVarDecl *ParamDecl) const;
Ted Kremenek81337162010-03-18 03:22:29 +000071
Jordan Rose9da59a62012-08-03 23:08:49 +000072 static void emitBadCall(BugType *BT, CheckerContext &C, const Expr *BadE);
Jordan Rosede507ea2012-07-02 19:28:04 +000073 void emitNilReceiverBug(CheckerContext &C, const ObjCMethodCall &msg,
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +000074 ExplodedNode *N) const;
Ted Kremenek091b5882010-03-18 02:17:27 +000075
Ted Kremenek18c66fd2011-08-15 22:09:50 +000076 void HandleNilReceiver(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +000077 ProgramStateRef state,
Jordan Rosede507ea2012-07-02 19:28:04 +000078 const ObjCMethodCall &msg) const;
Ted Kremenek091b5882010-03-18 02:17:27 +000079
Stephen Hines651f13c2014-04-23 16:59:28 -070080 void LazyInit_BT(const char *desc, std::unique_ptr<BugType> &BT) const {
Ted Kremenek81337162010-03-18 03:22:29 +000081 if (!BT)
Stephen Hines651f13c2014-04-23 16:59:28 -070082 BT.reset(new BuiltinBug(this, desc));
Ted Kremenek091b5882010-03-18 02:17:27 +000083 }
Stephen Hines651f13c2014-04-23 16:59:28 -070084 bool uninitRefOrPointer(CheckerContext &C, const SVal &V,
85 const SourceRange &ArgRange,
86 const Expr *ArgEx, std::unique_ptr<BugType> &BT,
87 const ParmVarDecl *ParamDecl, const char *BD) const;
Ted Kremenekf493f492009-11-11 05:50:44 +000088};
89} // end anonymous namespace
90
Jordan Rose9da59a62012-08-03 23:08:49 +000091void CallAndMessageChecker::emitBadCall(BugType *BT, CheckerContext &C,
92 const Expr *BadE) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +000093 ExplodedNode *N = C.generateSink();
Ted Kremenekc79d7d42009-11-21 01:25:37 +000094 if (!N)
95 return;
Ted Kremenek091b5882010-03-18 02:17:27 +000096
Anna Zakse172e8b2011-08-17 23:00:25 +000097 BugReport *R = new BugReport(*BT, BT->getName(), N);
Jordan Rose9da59a62012-08-03 23:08:49 +000098 if (BadE) {
99 R->addRange(BadE->getSourceRange());
Jordan Rose44ec3f02013-01-26 01:28:23 +0000100 if (BadE->isGLValue())
101 BadE = bugreporter::getDerefExpr(BadE);
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000102 bugreporter::trackNullOrUndefValue(N, BadE, *R);
Jordan Rose9da59a62012-08-03 23:08:49 +0000103 }
Jordan Rose785950e2012-11-02 01:53:40 +0000104 C.emitReport(R);
Ted Kremenekc79d7d42009-11-21 01:25:37 +0000105}
106
Benjamin Kramerda885362012-09-10 11:57:16 +0000107static StringRef describeUninitializedArgumentInCall(const CallEvent &Call,
108 bool IsFirstArgument) {
Jordan Rose8919e682012-07-18 21:59:51 +0000109 switch (Call.getKind()) {
110 case CE_ObjCMessage: {
111 const ObjCMethodCall &Msg = cast<ObjCMethodCall>(Call);
112 switch (Msg.getMessageKind()) {
113 case OCM_Message:
114 return "Argument in message expression is an uninitialized value";
115 case OCM_PropertyAccess:
116 assert(Msg.isSetter() && "Getters have no args");
117 return "Argument for property setter is an uninitialized value";
118 case OCM_Subscript:
119 if (Msg.isSetter() && IsFirstArgument)
120 return "Argument for subscript setter is an uninitialized value";
121 return "Subscript index is an uninitialized value";
122 }
123 llvm_unreachable("Unknown message kind.");
124 }
125 case CE_Block:
126 return "Block call argument is an uninitialized value";
127 default:
128 return "Function call argument is an uninitialized value";
129 }
130}
131
Stephen Hines651f13c2014-04-23 16:59:28 -0700132bool CallAndMessageChecker::uninitRefOrPointer(CheckerContext &C,
133 const SVal &V,
134 const SourceRange &ArgRange,
135 const Expr *ArgEx,
136 std::unique_ptr<BugType> &BT,
137 const ParmVarDecl *ParamDecl,
138 const char *BD) const {
139 if (!Filter.Check_CallAndMessageUnInitRefArg)
140 return false;
141
142 // No parameter declaration available, i.e. variadic function argument.
143 if(!ParamDecl)
144 return false;
145
146 // If parameter is declared as pointer to const in function declaration,
147 // then check if corresponding argument in function call is
148 // pointing to undefined symbol value (uninitialized memory).
149 StringRef Message;
150
151 if (ParamDecl->getType()->isPointerType()) {
152 Message = "Function call argument is a pointer to uninitialized value";
153 } else if (ParamDecl->getType()->isReferenceType()) {
154 Message = "Function call argument is an uninitialized value";
155 } else
156 return false;
157
158 if(!ParamDecl->getType()->getPointeeType().isConstQualified())
159 return false;
160
161 if (const MemRegion *SValMemRegion = V.getAsRegion()) {
162 const ProgramStateRef State = C.getState();
163 const SVal PSV = State->getSVal(SValMemRegion);
164 if (PSV.isUndef()) {
165 if (ExplodedNode *N = C.generateSink()) {
166 LazyInit_BT(BD, BT);
167 BugReport *R = new BugReport(*BT, Message, N);
168 R->addRange(ArgRange);
169 if (ArgEx) {
170 bugreporter::trackNullOrUndefValue(N, ArgEx, *R);
171 }
172 C.emitReport(R);
173 }
174 return true;
175 }
176 }
177 return false;
178}
179
Ted Kremenek81337162010-03-18 03:22:29 +0000180bool CallAndMessageChecker::PreVisitProcessArg(CheckerContext &C,
Stephen Hines651f13c2014-04-23 16:59:28 -0700181 SVal V,
182 SourceRange ArgRange,
183 const Expr *ArgEx,
Jordan Rose8919e682012-07-18 21:59:51 +0000184 bool IsFirstArgument,
Stephen Hines651f13c2014-04-23 16:59:28 -0700185 bool CheckUninitFields,
Jordan Rose8919e682012-07-18 21:59:51 +0000186 const CallEvent &Call,
Stephen Hines651f13c2014-04-23 16:59:28 -0700187 std::unique_ptr<BugType> &BT,
188 const ParmVarDecl *ParamDecl
189 ) const {
190 const char *BD = "Uninitialized argument value";
191
192 if (uninitRefOrPointer(C, V, ArgRange, ArgEx, BT, ParamDecl, BD))
193 return true;
194
Ted Kremenek81337162010-03-18 03:22:29 +0000195 if (V.isUndef()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000196 if (ExplodedNode *N = C.generateSink()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700197 LazyInit_BT(BD, BT);
Ted Kremenek81337162010-03-18 03:22:29 +0000198
199 // Generate a report for this bug.
Stephen Hines651f13c2014-04-23 16:59:28 -0700200 StringRef Desc =
201 describeUninitializedArgumentInCall(Call, IsFirstArgument);
Jordan Rose8919e682012-07-18 21:59:51 +0000202 BugReport *R = new BugReport(*BT, Desc, N);
Stephen Hines651f13c2014-04-23 16:59:28 -0700203 R->addRange(ArgRange);
204 if (ArgEx)
205 bugreporter::trackNullOrUndefValue(N, ArgEx, *R);
Jordan Rose785950e2012-11-02 01:53:40 +0000206 C.emitReport(R);
Ted Kremenek81337162010-03-18 03:22:29 +0000207 }
208 return true;
209 }
210
Stephen Hines651f13c2014-04-23 16:59:28 -0700211 if (!CheckUninitFields)
Ted Kremeneke4d653b2012-03-05 23:57:14 +0000212 return false;
David Blaikie5251abe2013-02-20 05:52:05 +0000213
David Blaikiedc84cd52013-02-20 22:23:23 +0000214 if (Optional<nonloc::LazyCompoundVal> LV =
David Blaikie5251abe2013-02-20 05:52:05 +0000215 V.getAs<nonloc::LazyCompoundVal>()) {
Ted Kremenek81337162010-03-18 03:22:29 +0000216
217 class FindUninitializedField {
218 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000219 SmallVector<const FieldDecl *, 10> FieldChain;
Ted Kremenek81337162010-03-18 03:22:29 +0000220 private:
Ted Kremenek81337162010-03-18 03:22:29 +0000221 StoreManager &StoreMgr;
222 MemRegionManager &MrMgr;
223 Store store;
224 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +0000225 FindUninitializedField(StoreManager &storeMgr,
Ted Kremenek81337162010-03-18 03:22:29 +0000226 MemRegionManager &mrMgr, Store s)
Benjamin Kramerfacde172012-06-06 17:32:50 +0000227 : StoreMgr(storeMgr), MrMgr(mrMgr), store(s) {}
Ted Kremenek81337162010-03-18 03:22:29 +0000228
Ted Kremenek96979342011-08-12 20:02:48 +0000229 bool Find(const TypedValueRegion *R) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000230 QualType T = R->getValueType();
Ted Kremenek81337162010-03-18 03:22:29 +0000231 if (const RecordType *RT = T->getAsStructureType()) {
232 const RecordDecl *RD = RT->getDecl()->getDefinition();
233 assert(RD && "Referred record has no definition");
Stephen Hines651f13c2014-04-23 16:59:28 -0700234 for (const auto *I : RD->fields()) {
235 const FieldRegion *FR = MrMgr.getFieldRegion(I, R);
236 FieldChain.push_back(I);
David Blaikie262bc182012-04-30 02:36:29 +0000237 T = I->getType();
Ted Kremenek81337162010-03-18 03:22:29 +0000238 if (T->getAsStructureType()) {
239 if (Find(FR))
240 return true;
241 }
242 else {
Anna Zaks14374252012-01-12 02:22:40 +0000243 const SVal &V = StoreMgr.getBinding(store, loc::MemRegionVal(FR));
Ted Kremenek81337162010-03-18 03:22:29 +0000244 if (V.isUndef())
245 return true;
246 }
247 FieldChain.pop_back();
248 }
249 }
250
251 return false;
252 }
253 };
254
255 const LazyCompoundValData *D = LV->getCVData();
Benjamin Kramerfacde172012-06-06 17:32:50 +0000256 FindUninitializedField F(C.getState()->getStateManager().getStoreManager(),
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000257 C.getSValBuilder().getRegionManager(),
Ted Kremenek81337162010-03-18 03:22:29 +0000258 D->getStore());
259
260 if (F.Find(D->getRegion())) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000261 if (ExplodedNode *N = C.generateSink()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700262 LazyInit_BT(BD, BT);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000263 SmallString<512> Str;
Ted Kremenek81337162010-03-18 03:22:29 +0000264 llvm::raw_svector_ostream os(Str);
265 os << "Passed-by-value struct argument contains uninitialized data";
266
267 if (F.FieldChain.size() == 1)
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000268 os << " (e.g., field: '" << *F.FieldChain[0] << "')";
Ted Kremenek81337162010-03-18 03:22:29 +0000269 else {
270 os << " (e.g., via the field chain: '";
271 bool first = true;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000272 for (SmallVectorImpl<const FieldDecl *>::iterator
Ted Kremenek81337162010-03-18 03:22:29 +0000273 DI = F.FieldChain.begin(), DE = F.FieldChain.end(); DI!=DE;++DI){
274 if (first)
275 first = false;
276 else
277 os << '.';
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000278 os << **DI;
Ted Kremenek81337162010-03-18 03:22:29 +0000279 }
280 os << "')";
281 }
282
283 // Generate a report for this bug.
Anna Zakse172e8b2011-08-17 23:00:25 +0000284 BugReport *R = new BugReport(*BT, os.str(), N);
Stephen Hines651f13c2014-04-23 16:59:28 -0700285 R->addRange(ArgRange);
Ted Kremenek81337162010-03-18 03:22:29 +0000286
287 // FIXME: enhance track back for uninitialized value for arbitrary
288 // memregions
Jordan Rose785950e2012-11-02 01:53:40 +0000289 C.emitReport(R);
Ted Kremenek81337162010-03-18 03:22:29 +0000290 }
291 return true;
292 }
293 }
294
295 return false;
296}
297
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +0000298void CallAndMessageChecker::checkPreStmt(const CallExpr *CE,
299 CheckerContext &C) const{
Ted Kremenek091b5882010-03-18 02:17:27 +0000300
Ted Kremenekc79d7d42009-11-21 01:25:37 +0000301 const Expr *Callee = CE->getCallee()->IgnoreParens();
Jordan Rose55037cd2012-07-02 19:27:46 +0000302 ProgramStateRef State = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +0000303 const LocationContext *LCtx = C.getLocationContext();
Jordan Rose55037cd2012-07-02 19:27:46 +0000304 SVal L = State->getSVal(Callee, LCtx);
Ted Kremenek091b5882010-03-18 02:17:27 +0000305
Ted Kremenekc79d7d42009-11-21 01:25:37 +0000306 if (L.isUndef()) {
307 if (!BT_call_undef)
Stephen Hines651f13c2014-04-23 16:59:28 -0700308 BT_call_undef.reset(new BuiltinBug(
309 this, "Called function pointer is an uninitalized pointer value"));
Jordan Rose9da59a62012-08-03 23:08:49 +0000310 emitBadCall(BT_call_undef.get(), C, Callee);
Ted Kremenekc79d7d42009-11-21 01:25:37 +0000311 return;
312 }
Ted Kremenek091b5882010-03-18 02:17:27 +0000313
Jordan Rosea8695182012-08-04 01:04:52 +0000314 ProgramStateRef StNonNull, StNull;
Stephen Hines651f13c2014-04-23 16:59:28 -0700315 std::tie(StNonNull, StNull) = State->assume(L.castAs<DefinedOrUnknownSVal>());
Jordan Rosea8695182012-08-04 01:04:52 +0000316
Jordan Rosea8695182012-08-04 01:04:52 +0000317 if (StNull && !StNonNull) {
Ted Kremenekc79d7d42009-11-21 01:25:37 +0000318 if (!BT_call_null)
Stephen Hines651f13c2014-04-23 16:59:28 -0700319 BT_call_null.reset(new BuiltinBug(
320 this, "Called function pointer is null (null dereference)"));
Jordan Rose9da59a62012-08-03 23:08:49 +0000321 emitBadCall(BT_call_null.get(), C, Callee);
Jordan Rose74536242013-10-02 01:20:28 +0000322 return;
Ted Kremenek091b5882010-03-18 02:17:27 +0000323 }
Jordan Rose7f660852012-08-15 21:56:23 +0000324
325 C.addTransition(StNonNull);
Jordan Rosefe6a0112012-07-02 19:28:21 +0000326}
Ted Kremenek091b5882010-03-18 02:17:27 +0000327
Jordan Rosefa220f52013-08-09 00:55:47 +0000328void CallAndMessageChecker::checkPreStmt(const CXXDeleteExpr *DE,
329 CheckerContext &C) const {
330
331 SVal Arg = C.getSVal(DE->getArgument());
332 if (Arg.isUndef()) {
333 StringRef Desc;
334 ExplodedNode *N = C.generateSink();
335 if (!N)
336 return;
337 if (!BT_cxx_delete_undef)
Stephen Hines651f13c2014-04-23 16:59:28 -0700338 BT_cxx_delete_undef.reset(
339 new BuiltinBug(this, "Uninitialized argument value"));
Jordan Rosefa220f52013-08-09 00:55:47 +0000340 if (DE->isArrayFormAsWritten())
341 Desc = "Argument to 'delete[]' is uninitialized";
342 else
343 Desc = "Argument to 'delete' is uninitialized";
344 BugType *BT = BT_cxx_delete_undef.get();
345 BugReport *R = new BugReport(*BT, Desc, N);
346 bugreporter::trackNullOrUndefValue(N, DE, *R);
347 C.emitReport(R);
348 return;
349 }
350}
351
352
Jordan Rosefe6a0112012-07-02 19:28:21 +0000353void CallAndMessageChecker::checkPreCall(const CallEvent &Call,
354 CheckerContext &C) const {
Jordan Rose7f660852012-08-15 21:56:23 +0000355 ProgramStateRef State = C.getState();
356
Ted Kremenekfc999ac2012-07-26 00:22:32 +0000357 // If this is a call to a C++ method, check if the callee is null or
358 // undefined.
Jordan Rose9da59a62012-08-03 23:08:49 +0000359 if (const CXXInstanceCall *CC = dyn_cast<CXXInstanceCall>(&Call)) {
Ted Kremenekfc999ac2012-07-26 00:22:32 +0000360 SVal V = CC->getCXXThisVal();
361 if (V.isUndef()) {
362 if (!BT_cxx_call_undef)
Stephen Hines651f13c2014-04-23 16:59:28 -0700363 BT_cxx_call_undef.reset(
364 new BuiltinBug(this, "Called C++ object pointer is uninitialized"));
Jordan Rose9da59a62012-08-03 23:08:49 +0000365 emitBadCall(BT_cxx_call_undef.get(), C, CC->getCXXThisExpr());
Ted Kremenekfc999ac2012-07-26 00:22:32 +0000366 return;
367 }
Jordan Rosea8695182012-08-04 01:04:52 +0000368
Jordan Rosea8695182012-08-04 01:04:52 +0000369 ProgramStateRef StNonNull, StNull;
Stephen Hines651f13c2014-04-23 16:59:28 -0700370 std::tie(StNonNull, StNull) =
David Blaikie5251abe2013-02-20 05:52:05 +0000371 State->assume(V.castAs<DefinedOrUnknownSVal>());
Jordan Rosea8695182012-08-04 01:04:52 +0000372
Jordan Rosea8695182012-08-04 01:04:52 +0000373 if (StNull && !StNonNull) {
Ted Kremenekfc999ac2012-07-26 00:22:32 +0000374 if (!BT_cxx_call_null)
Stephen Hines651f13c2014-04-23 16:59:28 -0700375 BT_cxx_call_null.reset(
376 new BuiltinBug(this, "Called C++ object pointer is null"));
Jordan Rose9da59a62012-08-03 23:08:49 +0000377 emitBadCall(BT_cxx_call_null.get(), C, CC->getCXXThisExpr());
Ted Kremenekfc999ac2012-07-26 00:22:32 +0000378 return;
379 }
Jordan Rose7f660852012-08-15 21:56:23 +0000380
381 State = StNonNull;
Ted Kremenekfc999ac2012-07-26 00:22:32 +0000382 }
383
Pavel Labath37926da2013-06-19 08:19:56 +0000384 const Decl *D = Call.getDecl();
Stephen Hines651f13c2014-04-23 16:59:28 -0700385 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
386 if (FD) {
Pavel Labath37926da2013-06-19 08:19:56 +0000387 // If we have a declaration, we can make sure we pass enough parameters to
388 // the function.
389 unsigned Params = FD->getNumParams();
390 if (Call.getNumArgs() < Params) {
391 ExplodedNode *N = C.generateSink();
392 if (!N)
393 return;
394
395 LazyInit_BT("Function call with too few arguments", BT_call_few_args);
396
397 SmallString<512> Str;
398 llvm::raw_svector_ostream os(Str);
399 os << "Function taking " << Params << " argument"
400 << (Params == 1 ? "" : "s") << " is called with less ("
401 << Call.getNumArgs() << ")";
402
403 BugReport *R = new BugReport(*BT_call_few_args, os.str(), N);
404 C.emitReport(R);
405 }
406 }
407
Jordan Rosefe6a0112012-07-02 19:28:21 +0000408 // Don't check for uninitialized field values in arguments if the
409 // caller has a body that is available and we have the chance to inline it.
410 // This is a hack, but is a reasonable compromise betweens sometimes warning
411 // and sometimes not depending on if we decide to inline a function.
Jordan Rosefe6a0112012-07-02 19:28:21 +0000412 const bool checkUninitFields =
Ted Kremenekfc999ac2012-07-26 00:22:32 +0000413 !(C.getAnalysisManager().shouldInlineCall() && (D && D->getBody()));
Jordan Rosefe6a0112012-07-02 19:28:21 +0000414
Stephen Hines651f13c2014-04-23 16:59:28 -0700415 std::unique_ptr<BugType> *BT;
Jordan Rose8919e682012-07-18 21:59:51 +0000416 if (isa<ObjCMethodCall>(Call))
Jordan Rosefe6a0112012-07-02 19:28:21 +0000417 BT = &BT_msg_arg;
Jordan Rose8919e682012-07-18 21:59:51 +0000418 else
Jordan Rosefe6a0112012-07-02 19:28:21 +0000419 BT = &BT_call_arg;
Jordan Rosefe6a0112012-07-02 19:28:21 +0000420
Stephen Hines651f13c2014-04-23 16:59:28 -0700421 for (unsigned i = 0, e = Call.getNumArgs(); i != e; ++i) {
422 const ParmVarDecl *ParamDecl = NULL;
423 if(FD && i < FD->getNumParams())
424 ParamDecl = FD->getParamDecl(i);
Jordan Rose8919e682012-07-18 21:59:51 +0000425 if (PreVisitProcessArg(C, Call.getArgSVal(i), Call.getArgSourceRange(i),
426 Call.getArgExpr(i), /*IsFirstArgument=*/i == 0,
Stephen Hines651f13c2014-04-23 16:59:28 -0700427 checkUninitFields, Call, *BT, ParamDecl))
Jordan Rosefe6a0112012-07-02 19:28:21 +0000428 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700429 }
Jordan Rose7f660852012-08-15 21:56:23 +0000430
431 // If we make it here, record our assumptions about the callee.
432 C.addTransition(State);
Ted Kremenek64fa8582009-11-21 00:49:41 +0000433}
434
Jordan Rosede507ea2012-07-02 19:28:04 +0000435void CallAndMessageChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +0000436 CheckerContext &C) const {
Jordan Rosede507ea2012-07-02 19:28:04 +0000437 SVal recVal = msg.getReceiverSVal();
438 if (recVal.isUndef()) {
439 if (ExplodedNode *N = C.generateSink()) {
440 BugType *BT = 0;
Jordan Rose8919e682012-07-18 21:59:51 +0000441 switch (msg.getMessageKind()) {
442 case OCM_Message:
Jordan Rosede507ea2012-07-02 19:28:04 +0000443 if (!BT_msg_undef)
Stephen Hines651f13c2014-04-23 16:59:28 -0700444 BT_msg_undef.reset(new BuiltinBug(this,
445 "Receiver in message expression "
Jordan Rosede507ea2012-07-02 19:28:04 +0000446 "is an uninitialized value"));
447 BT = BT_msg_undef.get();
Jordan Rose8919e682012-07-18 21:59:51 +0000448 break;
449 case OCM_PropertyAccess:
450 if (!BT_objc_prop_undef)
Stephen Hines651f13c2014-04-23 16:59:28 -0700451 BT_objc_prop_undef.reset(new BuiltinBug(
452 this, "Property access on an uninitialized object pointer"));
Jordan Rose8919e682012-07-18 21:59:51 +0000453 BT = BT_objc_prop_undef.get();
454 break;
455 case OCM_Subscript:
456 if (!BT_objc_subscript_undef)
Stephen Hines651f13c2014-04-23 16:59:28 -0700457 BT_objc_subscript_undef.reset(new BuiltinBug(
458 this, "Subscript access on an uninitialized object pointer"));
Jordan Rose8919e682012-07-18 21:59:51 +0000459 BT = BT_objc_subscript_undef.get();
460 break;
Jordan Rosede507ea2012-07-02 19:28:04 +0000461 }
Jordan Rose8919e682012-07-18 21:59:51 +0000462 assert(BT && "Unknown message kind.");
463
Jordan Rosede507ea2012-07-02 19:28:04 +0000464 BugReport *R = new BugReport(*BT, BT->getName(), N);
Jordan Rose8919e682012-07-18 21:59:51 +0000465 const ObjCMessageExpr *ME = msg.getOriginExpr();
466 R->addRange(ME->getReceiverRange());
Ted Kremenek64fa8582009-11-21 00:49:41 +0000467
Jordan Rosede507ea2012-07-02 19:28:04 +0000468 // FIXME: getTrackNullOrUndefValueVisitor can't handle "super" yet.
Jordan Rose8919e682012-07-18 21:59:51 +0000469 if (const Expr *ReceiverE = ME->getInstanceReceiver())
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000470 bugreporter::trackNullOrUndefValue(N, ReceiverE, *R);
Jordan Rose785950e2012-11-02 01:53:40 +0000471 C.emitReport(R);
Jordan Rosede507ea2012-07-02 19:28:04 +0000472 }
473 return;
474 } else {
475 // Bifurcate the state into nil and non-nil ones.
David Blaikie5251abe2013-02-20 05:52:05 +0000476 DefinedOrUnknownSVal receiverVal = recVal.castAs<DefinedOrUnknownSVal>();
Jordan Rosede507ea2012-07-02 19:28:04 +0000477
478 ProgramStateRef state = C.getState();
479 ProgramStateRef notNilState, nilState;
Stephen Hines651f13c2014-04-23 16:59:28 -0700480 std::tie(notNilState, nilState) = state->assume(receiverVal);
Jordan Rosede507ea2012-07-02 19:28:04 +0000481
482 // Handle receiver must be nil.
483 if (nilState && !notNilState) {
484 HandleNilReceiver(C, state, msg);
Ted Kremenekc79d7d42009-11-21 01:25:37 +0000485 return;
486 }
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +0000487 }
Zhongxing Xua46e4d92009-12-02 05:49:12 +0000488}
Zhongxing Xu2055eff2009-11-24 07:06:39 +0000489
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000490void CallAndMessageChecker::emitNilReceiverBug(CheckerContext &C,
Jordan Rosede507ea2012-07-02 19:28:04 +0000491 const ObjCMethodCall &msg,
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +0000492 ExplodedNode *N) const {
Ted Kremenek091b5882010-03-18 02:17:27 +0000493
Ted Kremenekfee96e02009-11-24 21:41:28 +0000494 if (!BT_msg_ret)
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +0000495 BT_msg_ret.reset(
Stephen Hines651f13c2014-04-23 16:59:28 -0700496 new BuiltinBug(this, "Receiver in message expression is 'nil'"));
Ted Kremenek091b5882010-03-18 02:17:27 +0000497
Jordan Rose8919e682012-07-18 21:59:51 +0000498 const ObjCMessageExpr *ME = msg.getOriginExpr();
499
Anna Zaks841f1682013-04-03 19:28:19 +0000500 QualType ResTy = msg.getResultType();
501
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000502 SmallString<200> buf;
Ted Kremenekfee96e02009-11-24 21:41:28 +0000503 llvm::raw_svector_ostream os(buf);
Stephen Hines651f13c2014-04-23 16:59:28 -0700504 os << "The receiver of message '";
505 ME->getSelector().print(os);
506 os << "' is nil";
Anna Zaks841f1682013-04-03 19:28:19 +0000507 if (ResTy->isReferenceType()) {
508 os << ", which results in forming a null reference";
509 } else {
510 os << " and returns a value of type '";
511 msg.getResultType().print(os, C.getLangOpts());
512 os << "' that will be garbage";
513 }
Ted Kremenek091b5882010-03-18 02:17:27 +0000514
Anna Zakse172e8b2011-08-17 23:00:25 +0000515 BugReport *report = new BugReport(*BT_msg_ret, os.str(), N);
Jordan Rose8919e682012-07-18 21:59:51 +0000516 report->addRange(ME->getReceiverRange());
Jordan Rosede507ea2012-07-02 19:28:04 +0000517 // FIXME: This won't track "self" in messages to super.
Jordan Rose8919e682012-07-18 21:59:51 +0000518 if (const Expr *receiver = ME->getInstanceReceiver()) {
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000519 bugreporter::trackNullOrUndefValue(N, receiver, *report);
Douglas Gregor04badcf2010-04-21 00:45:42 +0000520 }
Jordan Rose785950e2012-11-02 01:53:40 +0000521 C.emitReport(report);
Ted Kremenekfee96e02009-11-24 21:41:28 +0000522}
523
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000524static bool supportsNilWithFloatRet(const llvm::Triple &triple) {
Bob Wilson8f1e6562012-01-31 23:52:54 +0000525 return (triple.getVendor() == llvm::Triple::Apple &&
Cameron Esfahani57b1da12013-09-14 01:09:11 +0000526 (triple.isiOS() || !triple.isMacOSXVersionLT(10,5)));
Ted Kremenekf81330c2009-11-24 22:48:18 +0000527}
528
Ted Kremenekfee96e02009-11-24 21:41:28 +0000529void CallAndMessageChecker::HandleNilReceiver(CheckerContext &C,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000530 ProgramStateRef state,
Jordan Rosede507ea2012-07-02 19:28:04 +0000531 const ObjCMethodCall &Msg) const {
Argyrios Kyrtzidis432424d2011-01-25 00:03:53 +0000532 ASTContext &Ctx = C.getASTContext();
Stephen Hines651f13c2014-04-23 16:59:28 -0700533 static CheckerProgramPointTag Tag(this, "NilReceiver");
Ted Kremenek091b5882010-03-18 02:17:27 +0000534
Ted Kremenekfee96e02009-11-24 21:41:28 +0000535 // Check the return type of the message expression. A message to nil will
536 // return different values depending on the return type and the architecture.
Jordan Rosede507ea2012-07-02 19:28:04 +0000537 QualType RetTy = Msg.getResultType();
Ted Kremenekf81330c2009-11-24 22:48:18 +0000538 CanQualType CanRetTy = Ctx.getCanonicalType(RetTy);
Ted Kremenek5eca4822012-01-06 22:09:28 +0000539 const LocationContext *LCtx = C.getLocationContext();
Ted Kremenekfee96e02009-11-24 21:41:28 +0000540
Douglas Gregorfb87b892010-04-26 21:31:17 +0000541 if (CanRetTy->isStructureOrClassType()) {
Ted Kremenek4a037c72011-10-28 19:05:10 +0000542 // Structure returns are safe since the compiler zeroes them out.
Jordan Rosede507ea2012-07-02 19:28:04 +0000543 SVal V = C.getSValBuilder().makeZeroVal(RetTy);
Anna Zaks4a49df32013-03-27 17:35:58 +0000544 C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V), &Tag);
Ted Kremenekfee96e02009-11-24 21:41:28 +0000545 return;
546 }
547
Ted Kremenek4a037c72011-10-28 19:05:10 +0000548 // Other cases: check if sizeof(return type) > sizeof(void*)
Anna Zaksa2a86032011-11-01 22:41:01 +0000549 if (CanRetTy != Ctx.VoidTy && C.getLocationContext()->getParentMap()
Jordan Rosede507ea2012-07-02 19:28:04 +0000550 .isConsumedExpr(Msg.getOriginExpr())) {
Ted Kremenekfee96e02009-11-24 21:41:28 +0000551 // Compute: sizeof(void *) and sizeof(return type)
Ted Kremenek091b5882010-03-18 02:17:27 +0000552 const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
Ted Kremenekf81330c2009-11-24 22:48:18 +0000553 const uint64_t returnTypeSize = Ctx.getTypeSize(CanRetTy);
Ted Kremenekfee96e02009-11-24 21:41:28 +0000554
Anna Zaks841f1682013-04-03 19:28:19 +0000555 if (CanRetTy.getTypePtr()->isReferenceType()||
556 (voidPtrSize < returnTypeSize &&
557 !(supportsNilWithFloatRet(Ctx.getTargetInfo().getTriple()) &&
558 (Ctx.FloatTy == CanRetTy ||
559 Ctx.DoubleTy == CanRetTy ||
560 Ctx.LongDoubleTy == CanRetTy ||
561 Ctx.LongLongTy == CanRetTy ||
562 Ctx.UnsignedLongLongTy == CanRetTy)))) {
Anna Zaks4a49df32013-03-27 17:35:58 +0000563 if (ExplodedNode *N = C.generateSink(state, 0 , &Tag))
Jordan Rosede507ea2012-07-02 19:28:04 +0000564 emitNilReceiverBug(C, Msg, N);
Ted Kremenekfee96e02009-11-24 21:41:28 +0000565 return;
566 }
567
568 // Handle the safe cases where the return value is 0 if the
569 // receiver is nil.
570 //
571 // FIXME: For now take the conservative approach that we only
572 // return null values if we *know* that the receiver is nil.
573 // This is because we can have surprises like:
574 //
575 // ... = [[NSScreens screens] objectAtIndex:0];
576 //
577 // What can happen is that [... screens] could return nil, but
578 // it most likely isn't nil. We should assume the semantics
579 // of this case unless we have *a lot* more knowledge.
580 //
Jordan Rosede507ea2012-07-02 19:28:04 +0000581 SVal V = C.getSValBuilder().makeZeroVal(RetTy);
Anna Zaks4a49df32013-03-27 17:35:58 +0000582 C.addTransition(state->BindExpr(Msg.getOriginExpr(), LCtx, V), &Tag);
Ted Kremenekfee96e02009-11-24 21:41:28 +0000583 return;
584 }
Ted Kremenek091b5882010-03-18 02:17:27 +0000585
Anna Zaks0bd6b112011-10-26 21:06:34 +0000586 C.addTransition(state);
Zhongxing Xu8958fff2009-11-03 06:46:03 +0000587}
Argyrios Kyrtzidisd84f4222011-02-28 01:28:13 +0000588
Stephen Hines651f13c2014-04-23 16:59:28 -0700589#define REGISTER_CHECKER(name) \
590 void ento::register##name(CheckerManager &mgr) { \
591 CallAndMessageChecker *Checker = \
592 mgr.registerChecker<CallAndMessageChecker>(); \
593 Checker->Filter.Check_##name = true; \
594 Checker->Filter.CheckName_##name = mgr.getCurrentCheckName(); \
595 }
596
597REGISTER_CHECKER(CallAndMessageUnInitRefArg)
598REGISTER_CHECKER(CallAndMessageChecker)