blob: 08cff0fd429262e3a3bd0a5d8d8e819581f011e8 [file] [log] [blame]
Ted Kremenekc0414922008-03-27 07:25:52 +00001//== BasicObjCFoundationChecks.cpp - Simple Apple-Foundation checks -*- C++ -*--
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines BasicObjCFoundationChecks, a class that encapsulates
11// a set of simple checks to run on Objective-C code using Apple's Foundation
12// classes.
13//
14//===----------------------------------------------------------------------===//
15
Argyrios Kyrtzidis9d4d4f92011-02-16 01:40:52 +000016#include "ClangSACheckers.h"
Ted Kremenek6fa1dae2011-03-17 04:01:35 +000017#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Jordy Rose087611e2011-09-02 08:02:59 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h"
Ted Kremenek001fd5b2011-08-15 22:09:50 +000024#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000025#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
26#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000027#include "clang/AST/DeclObjC.h"
Ted Kremenekc0414922008-03-27 07:25:52 +000028#include "clang/AST/Expr.h"
Steve Naroff021ca182008-05-29 21:12:08 +000029#include "clang/AST/ExprObjC.h"
Ted Kremenekc0414922008-03-27 07:25:52 +000030#include "clang/AST/ASTContext.h"
Ted Kremenekc0414922008-03-27 07:25:52 +000031
Ted Kremenekc0414922008-03-27 07:25:52 +000032using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000033using namespace ento;
Ted Kremeneka4d60b62008-03-27 17:17:22 +000034
Ted Kremenekbd2c8002010-10-20 23:38:56 +000035namespace {
36class APIMisuse : public BugType {
37public:
38 APIMisuse(const char* name) : BugType(name, "API Misuse (Apple)") {}
39};
40} // end anonymous namespace
41
42//===----------------------------------------------------------------------===//
43// Utility functions.
44//===----------------------------------------------------------------------===//
45
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +000046static const char* GetReceiverNameType(const ObjCMessage &msg) {
Anders Carlsson3c50aea2011-03-08 20:05:26 +000047 if (const ObjCInterfaceDecl *ID = msg.getReceiverInterface())
48 return ID->getIdentifier()->getNameStart();
49 return 0;
Ted Kremenek276278e2008-03-27 22:05:32 +000050}
Ted Kremeneka4d60b62008-03-27 17:17:22 +000051
Anders Carlsson3c50aea2011-03-08 20:05:26 +000052static bool isReceiverClassOrSuperclass(const ObjCInterfaceDecl *ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000053 StringRef ClassName) {
Anders Carlsson3c50aea2011-03-08 20:05:26 +000054 if (ID->getIdentifier()->getName() == ClassName)
55 return true;
56
57 if (const ObjCInterfaceDecl *Super = ID->getSuperClass())
58 return isReceiverClassOrSuperclass(Super, ClassName);
59
60 return false;
Ted Kremenekc0414922008-03-27 07:25:52 +000061}
62
Zhongxing Xu27f17422008-10-17 05:57:07 +000063static inline bool isNil(SVal X) {
Mike Stump11289f42009-09-09 15:08:12 +000064 return isa<loc::ConcreteInt>(X);
Ted Kremenek27156c82008-03-27 21:15:17 +000065}
66
Ted Kremenekc0414922008-03-27 07:25:52 +000067//===----------------------------------------------------------------------===//
Ted Kremenekbd2c8002010-10-20 23:38:56 +000068// NilArgChecker - Check for prohibited nil arguments to ObjC method calls.
Ted Kremenekc0414922008-03-27 07:25:52 +000069//===----------------------------------------------------------------------===//
70
Benjamin Kramer2fc373e2010-10-22 16:33:16 +000071namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000072 class NilArgChecker : public Checker<check::PreObjCMessage> {
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +000073 mutable llvm::OwningPtr<APIMisuse> BT;
74
75 void WarnNilArg(CheckerContext &C,
76 const ObjCMessage &msg, unsigned Arg) const;
77
Benjamin Kramer2fc373e2010-10-22 16:33:16 +000078 public:
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +000079 void checkPreObjCMessage(ObjCMessage msg, CheckerContext &C) const;
Benjamin Kramer2fc373e2010-10-22 16:33:16 +000080 };
81}
Mike Stump11289f42009-09-09 15:08:12 +000082
Ted Kremenekbd2c8002010-10-20 23:38:56 +000083void NilArgChecker::WarnNilArg(CheckerContext &C,
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +000084 const ObjCMessage &msg,
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +000085 unsigned int Arg) const
Ted Kremenekbd2c8002010-10-20 23:38:56 +000086{
87 if (!BT)
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +000088 BT.reset(new APIMisuse("nil argument"));
Ted Kremenekbd2c8002010-10-20 23:38:56 +000089
Ted Kremenek750b7ac2010-12-20 21:19:09 +000090 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenekbd2c8002010-10-20 23:38:56 +000091 llvm::SmallString<128> sbuf;
92 llvm::raw_svector_ostream os(sbuf);
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +000093 os << "Argument to '" << GetReceiverNameType(msg) << "' method '"
94 << msg.getSelector().getAsString() << "' cannot be nil";
Mike Stump11289f42009-09-09 15:08:12 +000095
Anna Zaks3a6bdf82011-08-17 23:00:25 +000096 BugReport *R = new BugReport(*BT, os.str(), N);
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +000097 R->addRange(msg.getArgSourceRange(Arg));
Ted Kremenekbd2c8002010-10-20 23:38:56 +000098 C.EmitReport(R);
Ted Kremenek276278e2008-03-27 22:05:32 +000099 }
Ted Kremenek276278e2008-03-27 22:05:32 +0000100}
101
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000102void NilArgChecker::checkPreObjCMessage(ObjCMessage msg,
103 CheckerContext &C) const {
Anders Carlsson3c50aea2011-03-08 20:05:26 +0000104 const ObjCInterfaceDecl *ID = msg.getReceiverInterface();
105 if (!ID)
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000106 return;
107
Anders Carlsson3c50aea2011-03-08 20:05:26 +0000108 if (isReceiverClassOrSuperclass(ID, "NSString")) {
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000109 Selector S = msg.getSelector();
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000110
111 if (S.isUnarySelector())
112 return;
113
114 // FIXME: This is going to be really slow doing these checks with
115 // lexical comparisons.
116
117 std::string NameStr = S.getAsString();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000118 StringRef Name(NameStr);
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000119 assert(!Name.empty());
120
121 // FIXME: Checking for initWithFormat: will not work in most cases
122 // yet because [NSString alloc] returns id, not NSString*. We will
123 // need support for tracking expected-type information in the analyzer
124 // to find these errors.
125 if (Name == "caseInsensitiveCompare:" ||
126 Name == "compare:" ||
127 Name == "compare:options:" ||
128 Name == "compare:options:range:" ||
129 Name == "compare:options:range:locale:" ||
130 Name == "componentsSeparatedByCharactersInSet:" ||
131 Name == "initWithFormat:") {
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000132 if (isNil(msg.getArgSVal(0, C.getState())))
133 WarnNilArg(C, msg, 0);
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000134 }
135 }
Ted Kremenekc0414922008-03-27 07:25:52 +0000136}
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000137
138//===----------------------------------------------------------------------===//
139// Error reporting.
140//===----------------------------------------------------------------------===//
141
142namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000143class CFNumberCreateChecker : public Checker< check::PreStmt<CallExpr> > {
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000144 mutable llvm::OwningPtr<APIMisuse> BT;
145 mutable IdentifierInfo* II;
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000146public:
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000147 CFNumberCreateChecker() : II(0) {}
148
149 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
150
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000151private:
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000152 void EmitError(const TypedRegion* R, const Expr *Ex,
Mike Stump11289f42009-09-09 15:08:12 +0000153 uint64_t SourceSize, uint64_t TargetSize, uint64_t NumberKind);
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000154};
155} // end anonymous namespace
156
157enum CFNumberType {
158 kCFNumberSInt8Type = 1,
159 kCFNumberSInt16Type = 2,
160 kCFNumberSInt32Type = 3,
161 kCFNumberSInt64Type = 4,
162 kCFNumberFloat32Type = 5,
163 kCFNumberFloat64Type = 6,
164 kCFNumberCharType = 7,
165 kCFNumberShortType = 8,
166 kCFNumberIntType = 9,
167 kCFNumberLongType = 10,
168 kCFNumberLongLongType = 11,
169 kCFNumberFloatType = 12,
170 kCFNumberDoubleType = 13,
171 kCFNumberCFIndexType = 14,
172 kCFNumberNSIntegerType = 15,
173 kCFNumberCGFloatType = 16
174};
175
176namespace {
177 template<typename T>
178 class Optional {
179 bool IsKnown;
180 T Val;
181 public:
182 Optional() : IsKnown(false), Val(0) {}
183 Optional(const T& val) : IsKnown(true), Val(val) {}
Mike Stump11289f42009-09-09 15:08:12 +0000184
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000185 bool isKnown() const { return IsKnown; }
186
187 const T& getValue() const {
188 assert (isKnown());
189 return Val;
190 }
191
192 operator const T&() const {
193 return getValue();
194 }
195 };
196}
197
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000198static Optional<uint64_t> GetCFNumberSize(ASTContext &Ctx, uint64_t i) {
Nuno Lopescfca1f02009-12-23 17:49:57 +0000199 static const unsigned char FixedSize[] = { 8, 16, 32, 64, 32, 64 };
Mike Stump11289f42009-09-09 15:08:12 +0000200
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000201 if (i < kCFNumberCharType)
202 return FixedSize[i-1];
Mike Stump11289f42009-09-09 15:08:12 +0000203
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000204 QualType T;
Mike Stump11289f42009-09-09 15:08:12 +0000205
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000206 switch (i) {
207 case kCFNumberCharType: T = Ctx.CharTy; break;
208 case kCFNumberShortType: T = Ctx.ShortTy; break;
209 case kCFNumberIntType: T = Ctx.IntTy; break;
210 case kCFNumberLongType: T = Ctx.LongTy; break;
211 case kCFNumberLongLongType: T = Ctx.LongLongTy; break;
212 case kCFNumberFloatType: T = Ctx.FloatTy; break;
213 case kCFNumberDoubleType: T = Ctx.DoubleTy; break;
214 case kCFNumberCFIndexType:
215 case kCFNumberNSIntegerType:
216 case kCFNumberCGFloatType:
Mike Stump11289f42009-09-09 15:08:12 +0000217 // FIXME: We need a way to map from names to Type*.
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000218 default:
219 return Optional<uint64_t>();
220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000222 return Ctx.getTypeSize(T);
223}
224
225#if 0
226static const char* GetCFNumberTypeStr(uint64_t i) {
227 static const char* Names[] = {
228 "kCFNumberSInt8Type",
229 "kCFNumberSInt16Type",
230 "kCFNumberSInt32Type",
231 "kCFNumberSInt64Type",
232 "kCFNumberFloat32Type",
233 "kCFNumberFloat64Type",
234 "kCFNumberCharType",
235 "kCFNumberShortType",
236 "kCFNumberIntType",
237 "kCFNumberLongType",
238 "kCFNumberLongLongType",
239 "kCFNumberFloatType",
240 "kCFNumberDoubleType",
241 "kCFNumberCFIndexType",
242 "kCFNumberNSIntegerType",
243 "kCFNumberCGFloatType"
244 };
Mike Stump11289f42009-09-09 15:08:12 +0000245
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000246 return i <= kCFNumberCGFloatType ? Names[i-1] : "Invalid CFNumberType";
247}
248#endif
249
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000250void CFNumberCreateChecker::checkPreStmt(const CallExpr *CE,
251 CheckerContext &C) const {
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000252 const Expr *Callee = CE->getCallee();
Ted Kremenek001fd5b2011-08-15 22:09:50 +0000253 const ProgramState *state = C.getState();
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000254 SVal CallV = state->getSVal(Callee);
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000255 const FunctionDecl *FD = CallV.getAsFunctionDecl();
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000256
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000257 if (!FD)
258 return;
259
260 ASTContext &Ctx = C.getASTContext();
261 if (!II)
262 II = &Ctx.Idents.get("CFNumberCreate");
263
264 if (FD->getIdentifier() != II || CE->getNumArgs() != 3)
265 return;
Mike Stump11289f42009-09-09 15:08:12 +0000266
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000267 // Get the value of the "theType" argument.
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000268 SVal TheTypeVal = state->getSVal(CE->getArg(1));
Mike Stump11289f42009-09-09 15:08:12 +0000269
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000270 // FIXME: We really should allow ranges of valid theType values, and
271 // bifurcate the state appropriately.
Zhongxing Xu27f17422008-10-17 05:57:07 +0000272 nonloc::ConcreteInt* V = dyn_cast<nonloc::ConcreteInt>(&TheTypeVal);
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000273 if (!V)
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000274 return;
Mike Stump11289f42009-09-09 15:08:12 +0000275
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000276 uint64_t NumberKind = V->getValue().getLimitedValue();
277 Optional<uint64_t> TargetSize = GetCFNumberSize(Ctx, NumberKind);
Mike Stump11289f42009-09-09 15:08:12 +0000278
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000279 // FIXME: In some cases we can emit an error.
280 if (!TargetSize.isKnown())
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000281 return;
Mike Stump11289f42009-09-09 15:08:12 +0000282
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000283 // Look at the value of the integer being passed by reference. Essentially
284 // we want to catch cases where the value passed in is not equal to the
285 // size of the type being created.
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000286 SVal TheValueExpr = state->getSVal(CE->getArg(2));
Mike Stump11289f42009-09-09 15:08:12 +0000287
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000288 // FIXME: Eventually we should handle arbitrary locations. We can do this
289 // by having an enhanced memory model that does low-level typing.
Zhongxing Xu27f17422008-10-17 05:57:07 +0000290 loc::MemRegionVal* LV = dyn_cast<loc::MemRegionVal>(&TheValueExpr);
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000291 if (!LV)
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000292 return;
Mike Stump11289f42009-09-09 15:08:12 +0000293
Ted Kremenek8df44b262011-08-12 20:02:48 +0000294 const TypedValueRegion* R = dyn_cast<TypedValueRegion>(LV->stripCasts());
Ted Kremenek87a7a452009-07-29 18:17:40 +0000295 if (!R)
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000296 return;
Ted Kremenek87a7a452009-07-29 18:17:40 +0000297
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000298 QualType T = Ctx.getCanonicalType(R->getValueType());
Mike Stump11289f42009-09-09 15:08:12 +0000299
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000300 // FIXME: If the pointee isn't an integer type, should we flag a warning?
301 // People can do weird stuff with pointers.
Mike Stump11289f42009-09-09 15:08:12 +0000302
303 if (!T->isIntegerType())
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000304 return;
Mike Stump11289f42009-09-09 15:08:12 +0000305
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000306 uint64_t SourceSize = Ctx.getTypeSize(T);
Mike Stump11289f42009-09-09 15:08:12 +0000307
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000308 // CHECK: is SourceSize == TargetSize
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000309 if (SourceSize == TargetSize)
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000310 return;
Mike Stump11289f42009-09-09 15:08:12 +0000311
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000312 // Generate an error. Only generate a sink if 'SourceSize < TargetSize';
313 // otherwise generate a regular node.
314 //
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000315 // FIXME: We can actually create an abstract "CFNumber" object that has
316 // the bits initialized to the provided values.
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000317 //
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000318 if (ExplodedNode *N = SourceSize < TargetSize ? C.generateSink()
319 : C.generateNode()) {
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000320 llvm::SmallString<128> sbuf;
321 llvm::raw_svector_ostream os(sbuf);
322
323 os << (SourceSize == 8 ? "An " : "A ")
324 << SourceSize << " bit integer is used to initialize a CFNumber "
325 "object that represents "
326 << (TargetSize == 8 ? "an " : "a ")
327 << TargetSize << " bit integer. ";
328
329 if (SourceSize < TargetSize)
330 os << (TargetSize - SourceSize)
331 << " bits of the CFNumber value will be garbage." ;
332 else
333 os << (SourceSize - TargetSize)
334 << " bits of the input integer will be lost.";
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000335
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000336 if (!BT)
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000337 BT.reset(new APIMisuse("Bad use of CFNumberCreate"));
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000338
Anna Zaks3a6bdf82011-08-17 23:00:25 +0000339 BugReport *report = new BugReport(*BT, os.str(), N);
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000340 report->addRange(CE->getArg(2)->getSourceRange());
341 C.EmitReport(report);
342 }
Ted Kremenekcf1ab192008-06-26 23:59:48 +0000343}
344
Ted Kremenek1f352db2008-07-22 16:21:24 +0000345//===----------------------------------------------------------------------===//
Jordy Rose40c5c242010-07-06 02:34:42 +0000346// CFRetain/CFRelease checking for null arguments.
Ted Kremenekc057f412009-07-14 00:43:42 +0000347//===----------------------------------------------------------------------===//
348
349namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000350class CFRetainReleaseChecker : public Checker< check::PreStmt<CallExpr> > {
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000351 mutable llvm::OwningPtr<APIMisuse> BT;
352 mutable IdentifierInfo *Retain, *Release;
Ted Kremenekc057f412009-07-14 00:43:42 +0000353public:
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000354 CFRetainReleaseChecker(): Retain(0), Release(0) {}
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000355 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Ted Kremenekc057f412009-07-14 00:43:42 +0000356};
357} // end anonymous namespace
358
359
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000360void CFRetainReleaseChecker::checkPreStmt(const CallExpr *CE,
361 CheckerContext &C) const {
Ted Kremenekc057f412009-07-14 00:43:42 +0000362 // If the CallExpr doesn't have exactly 1 argument just give up checking.
363 if (CE->getNumArgs() != 1)
Jordy Rose40c5c242010-07-06 02:34:42 +0000364 return;
Mike Stump11289f42009-09-09 15:08:12 +0000365
Jordy Rose40c5c242010-07-06 02:34:42 +0000366 // Get the function declaration of the callee.
Ted Kremenek001fd5b2011-08-15 22:09:50 +0000367 const ProgramState *state = C.getState();
Ted Kremenek57f09892010-02-08 16:18:51 +0000368 SVal X = state->getSVal(CE->getCallee());
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000369 const FunctionDecl *FD = X.getAsFunctionDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenekc057f412009-07-14 00:43:42 +0000371 if (!FD)
Jordy Rose40c5c242010-07-06 02:34:42 +0000372 return;
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000373
374 if (!BT) {
375 ASTContext &Ctx = C.getASTContext();
376 Retain = &Ctx.Idents.get("CFRetain");
377 Release = &Ctx.Idents.get("CFRelease");
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000378 BT.reset(new APIMisuse("null passed to CFRetain/CFRelease"));
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000379 }
Mike Stump11289f42009-09-09 15:08:12 +0000380
Jordy Rose40c5c242010-07-06 02:34:42 +0000381 // Check if we called CFRetain/CFRelease.
Mike Stump11289f42009-09-09 15:08:12 +0000382 const IdentifierInfo *FuncII = FD->getIdentifier();
Ted Kremenekc057f412009-07-14 00:43:42 +0000383 if (!(FuncII == Retain || FuncII == Release))
Jordy Rose40c5c242010-07-06 02:34:42 +0000384 return;
Mike Stump11289f42009-09-09 15:08:12 +0000385
Jordy Rose40c5c242010-07-06 02:34:42 +0000386 // FIXME: The rest of this just checks that the argument is non-null.
387 // It should probably be refactored and combined with AttrNonNullChecker.
388
389 // Get the argument's value.
390 const Expr *Arg = CE->getArg(0);
391 SVal ArgVal = state->getSVal(Arg);
392 DefinedSVal *DefArgVal = dyn_cast<DefinedSVal>(&ArgVal);
393 if (!DefArgVal)
394 return;
395
396 // Get a NULL value.
Ted Kremenek90af9092010-12-02 07:49:45 +0000397 SValBuilder &svalBuilder = C.getSValBuilder();
398 DefinedSVal zero = cast<DefinedSVal>(svalBuilder.makeZeroVal(Arg->getType()));
Jordy Rose40c5c242010-07-06 02:34:42 +0000399
400 // Make an expression asserting that they're equal.
Ted Kremenek90af9092010-12-02 07:49:45 +0000401 DefinedOrUnknownSVal ArgIsNull = svalBuilder.evalEQ(state, zero, *DefArgVal);
Jordy Rose40c5c242010-07-06 02:34:42 +0000402
403 // Are they equal?
Ted Kremenek001fd5b2011-08-15 22:09:50 +0000404 const ProgramState *stateTrue, *stateFalse;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000405 llvm::tie(stateTrue, stateFalse) = state->assume(ArgIsNull);
Jordy Rose40c5c242010-07-06 02:34:42 +0000406
407 if (stateTrue && !stateFalse) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000408 ExplodedNode *N = C.generateSink(stateTrue);
Jordy Rose40c5c242010-07-06 02:34:42 +0000409 if (!N)
410 return;
411
Ted Kremenekc057f412009-07-14 00:43:42 +0000412 const char *description = (FuncII == Retain)
413 ? "Null pointer argument in call to CFRetain"
414 : "Null pointer argument in call to CFRelease";
415
Anna Zaks3a6bdf82011-08-17 23:00:25 +0000416 BugReport *report = new BugReport(*BT, description, N);
Jordy Rose40c5c242010-07-06 02:34:42 +0000417 report->addRange(Arg->getSourceRange());
Anna Zaksf86615c2011-08-19 22:33:38 +0000418 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Arg));
Jordy Rose40c5c242010-07-06 02:34:42 +0000419 C.EmitReport(report);
420 return;
Ted Kremenekc057f412009-07-14 00:43:42 +0000421 }
422
Jordy Rose40c5c242010-07-06 02:34:42 +0000423 // From here on, we know the argument is non-null.
424 C.addTransition(stateFalse);
Ted Kremenekc057f412009-07-14 00:43:42 +0000425}
426
427//===----------------------------------------------------------------------===//
Ted Kremeneka4f7c182009-11-20 05:27:05 +0000428// Check for sending 'retain', 'release', or 'autorelease' directly to a Class.
429//===----------------------------------------------------------------------===//
430
431namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000432class ClassReleaseChecker : public Checker<check::PreObjCMessage> {
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000433 mutable Selector releaseS;
434 mutable Selector retainS;
435 mutable Selector autoreleaseS;
436 mutable Selector drainS;
437 mutable llvm::OwningPtr<BugType> BT;
Ted Kremeneka4f7c182009-11-20 05:27:05 +0000438
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000439public:
440 void checkPreObjCMessage(ObjCMessage msg, CheckerContext &C) const;
Ted Kremeneka4f7c182009-11-20 05:27:05 +0000441};
442}
443
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000444void ClassReleaseChecker::checkPreObjCMessage(ObjCMessage msg,
445 CheckerContext &C) const {
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000446
447 if (!BT) {
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000448 BT.reset(new APIMisuse("message incorrectly sent to class instead of class "
449 "instance"));
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000450
451 ASTContext &Ctx = C.getASTContext();
452 releaseS = GetNullarySelector("release", Ctx);
453 retainS = GetNullarySelector("retain", Ctx);
454 autoreleaseS = GetNullarySelector("autorelease", Ctx);
455 drainS = GetNullarySelector("drain", Ctx);
456 }
457
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000458 if (msg.isInstanceMessage())
Ted Kremeneka4f7c182009-11-20 05:27:05 +0000459 return;
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000460 const ObjCInterfaceDecl *Class = msg.getReceiverInterface();
461 assert(Class);
Douglas Gregor9a129192010-04-21 00:45:42 +0000462
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000463 Selector S = msg.getSelector();
Benjamin Kramer7d875c72009-11-20 10:03:00 +0000464 if (!(S == releaseS || S == retainS || S == autoreleaseS || S == drainS))
Ted Kremeneka4f7c182009-11-20 05:27:05 +0000465 return;
466
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000467 if (ExplodedNode *N = C.generateNode()) {
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000468 llvm::SmallString<200> buf;
469 llvm::raw_svector_ostream os(buf);
Ted Kremenekf5735152009-11-23 22:22:01 +0000470
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000471 os << "The '" << S.getAsString() << "' message should be sent to instances "
472 "of class '" << Class->getName()
473 << "' and not the class directly";
Ted Kremeneka4f7c182009-11-20 05:27:05 +0000474
Anna Zaks3a6bdf82011-08-17 23:00:25 +0000475 BugReport *report = new BugReport(*BT, os.str(), N);
Argyrios Kyrtzidis37ab7262011-01-25 00:03:53 +0000476 report->addRange(msg.getSourceRange());
Ted Kremenekbd2c8002010-10-20 23:38:56 +0000477 C.EmitReport(report);
478 }
Ted Kremeneka4f7c182009-11-20 05:27:05 +0000479}
480
481//===----------------------------------------------------------------------===//
Anders Carlssond91d5f12011-03-13 20:35:21 +0000482// Check for passing non-Objective-C types to variadic methods that expect
483// only Objective-C types.
484//===----------------------------------------------------------------------===//
485
486namespace {
487class VariadicMethodTypeChecker : public Checker<check::PreObjCMessage> {
488 mutable Selector arrayWithObjectsS;
489 mutable Selector dictionaryWithObjectsAndKeysS;
490 mutable Selector setWithObjectsS;
491 mutable Selector initWithObjectsS;
492 mutable Selector initWithObjectsAndKeysS;
493 mutable llvm::OwningPtr<BugType> BT;
494
495 bool isVariadicMessage(const ObjCMessage &msg) const;
496
497public:
498 void checkPreObjCMessage(ObjCMessage msg, CheckerContext &C) const;
499};
500}
501
502/// isVariadicMessage - Returns whether the given message is a variadic message,
503/// where all arguments must be Objective-C types.
504bool
505VariadicMethodTypeChecker::isVariadicMessage(const ObjCMessage &msg) const {
506 const ObjCMethodDecl *MD = msg.getMethodDecl();
Ted Kremenekced5fea2011-04-12 21:47:05 +0000507
508 if (!MD || !MD->isVariadic() || isa<ObjCProtocolDecl>(MD->getDeclContext()))
Anders Carlssond91d5f12011-03-13 20:35:21 +0000509 return false;
510
511 Selector S = msg.getSelector();
512
513 if (msg.isInstanceMessage()) {
514 // FIXME: Ideally we'd look at the receiver interface here, but that's not
515 // useful for init, because alloc returns 'id'. In theory, this could lead
516 // to false positives, for example if there existed a class that had an
517 // initWithObjects: implementation that does accept non-Objective-C pointer
518 // types, but the chance of that happening is pretty small compared to the
519 // gains that this analysis gives.
520 const ObjCInterfaceDecl *Class = MD->getClassInterface();
521
522 // -[NSArray initWithObjects:]
523 if (isReceiverClassOrSuperclass(Class, "NSArray") &&
524 S == initWithObjectsS)
525 return true;
526
527 // -[NSDictionary initWithObjectsAndKeys:]
528 if (isReceiverClassOrSuperclass(Class, "NSDictionary") &&
529 S == initWithObjectsAndKeysS)
530 return true;
531
532 // -[NSSet initWithObjects:]
533 if (isReceiverClassOrSuperclass(Class, "NSSet") &&
534 S == initWithObjectsS)
535 return true;
536 } else {
537 const ObjCInterfaceDecl *Class = msg.getReceiverInterface();
538
539 // -[NSArray arrayWithObjects:]
540 if (isReceiverClassOrSuperclass(Class, "NSArray") &&
541 S == arrayWithObjectsS)
542 return true;
543
544 // -[NSDictionary dictionaryWithObjectsAndKeys:]
545 if (isReceiverClassOrSuperclass(Class, "NSDictionary") &&
546 S == dictionaryWithObjectsAndKeysS)
547 return true;
548
549 // -[NSSet setWithObjects:]
550 if (isReceiverClassOrSuperclass(Class, "NSSet") &&
551 S == setWithObjectsS)
552 return true;
553 }
554
555 return false;
556}
557
558void VariadicMethodTypeChecker::checkPreObjCMessage(ObjCMessage msg,
559 CheckerContext &C) const {
560 if (!BT) {
561 BT.reset(new APIMisuse("Arguments passed to variadic method aren't all "
562 "Objective-C pointer types"));
563
564 ASTContext &Ctx = C.getASTContext();
565 arrayWithObjectsS = GetUnarySelector("arrayWithObjects", Ctx);
566 dictionaryWithObjectsAndKeysS =
567 GetUnarySelector("dictionaryWithObjectsAndKeys", Ctx);
568 setWithObjectsS = GetUnarySelector("setWithObjects", Ctx);
569
570 initWithObjectsS = GetUnarySelector("initWithObjects", Ctx);
571 initWithObjectsAndKeysS = GetUnarySelector("initWithObjectsAndKeys", Ctx);
572 }
573
574 if (!isVariadicMessage(msg))
575 return;
576
577 // We are not interested in the selector arguments since they have
578 // well-defined types, so the compiler will issue a warning for them.
579 unsigned variadicArgsBegin = msg.getSelector().getNumArgs();
580
581 // We're not interested in the last argument since it has to be nil or the
582 // compiler would have issued a warning for it elsewhere.
583 unsigned variadicArgsEnd = msg.getNumArgs() - 1;
584
585 if (variadicArgsEnd <= variadicArgsBegin)
586 return;
587
588 // Verify that all arguments have Objective-C types.
Ted Kremenek066b2262011-03-14 19:50:37 +0000589 llvm::Optional<ExplodedNode*> errorNode;
Ted Kremenek001fd5b2011-08-15 22:09:50 +0000590 const ProgramState *state = C.getState();
Ted Kremenek066b2262011-03-14 19:50:37 +0000591
Anders Carlssond91d5f12011-03-13 20:35:21 +0000592 for (unsigned I = variadicArgsBegin; I != variadicArgsEnd; ++I) {
593 QualType ArgTy = msg.getArgType(I);
594 if (ArgTy->isObjCObjectPointerType())
595 continue;
596
Anders Carlssond1f65f62011-04-19 01:16:46 +0000597 // Block pointers are treaded as Objective-C pointers.
598 if (ArgTy->isBlockPointerType())
599 continue;
600
Ted Kremenek4ceebbf2011-03-16 00:22:51 +0000601 // Ignore pointer constants.
602 if (isa<loc::ConcreteInt>(msg.getArgSVal(I, state)))
603 continue;
Ted Kremenek6fa1dae2011-03-17 04:01:35 +0000604
Ted Kremenek70727342011-03-17 04:10:25 +0000605 // Ignore pointer types annotated with 'NSObject' attribute.
606 if (C.getASTContext().isObjCNSObjectType(ArgTy))
607 continue;
608
Ted Kremenek6fa1dae2011-03-17 04:01:35 +0000609 // Ignore CF references, which can be toll-free bridged.
Ted Kremenekc85964e2011-07-16 19:50:32 +0000610 if (coreFoundation::isCFObjectRef(ArgTy))
Ted Kremenek6fa1dae2011-03-17 04:01:35 +0000611 continue;
Ted Kremenek4ceebbf2011-03-16 00:22:51 +0000612
Ted Kremenek066b2262011-03-14 19:50:37 +0000613 // Generate only one error node to use for all bug reports.
614 if (!errorNode.hasValue()) {
615 errorNode = C.generateNode();
616 }
617
618 if (!errorNode.getValue())
Anders Carlssond91d5f12011-03-13 20:35:21 +0000619 continue;
620
621 llvm::SmallString<128> sbuf;
622 llvm::raw_svector_ostream os(sbuf);
623
624 if (const char *TypeName = GetReceiverNameType(msg))
625 os << "Argument to '" << TypeName << "' method '";
626 else
627 os << "Argument to method '";
628
629 os << msg.getSelector().getAsString()
630 << "' should be an Objective-C pointer type, not '"
631 << ArgTy.getAsString() << "'";
632
Anna Zaks3a6bdf82011-08-17 23:00:25 +0000633 BugReport *R = new BugReport(*BT, os.str(),
Ted Kremenek066b2262011-03-14 19:50:37 +0000634 errorNode.getValue());
Anders Carlssond91d5f12011-03-13 20:35:21 +0000635 R->addRange(msg.getArgSourceRange(I));
636 C.EmitReport(R);
637 }
638}
639
640//===----------------------------------------------------------------------===//
Ted Kremenek1f352db2008-07-22 16:21:24 +0000641// Check registration.
Ted Kremenekc057f412009-07-14 00:43:42 +0000642//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis9d4d4f92011-02-16 01:40:52 +0000643
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +0000644void ento::registerNilArgChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000645 mgr.registerChecker<NilArgChecker>();
Argyrios Kyrtzidis9d4d4f92011-02-16 01:40:52 +0000646}
647
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +0000648void ento::registerCFNumberCreateChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000649 mgr.registerChecker<CFNumberCreateChecker>();
Argyrios Kyrtzidis9d4d4f92011-02-16 01:40:52 +0000650}
651
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +0000652void ento::registerCFRetainReleaseChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000653 mgr.registerChecker<CFRetainReleaseChecker>();
Ted Kremenek1f352db2008-07-22 16:21:24 +0000654}
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +0000655
656void ento::registerClassReleaseChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdd058d82011-02-23 00:16:10 +0000657 mgr.registerChecker<ClassReleaseChecker>();
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +0000658}
Anders Carlssond91d5f12011-03-13 20:35:21 +0000659
660void ento::registerVariadicMethodTypeChecker(CheckerManager &mgr) {
661 mgr.registerChecker<VariadicMethodTypeChecker>();
662}