Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 1 | //== 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 | |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 16 | #include "BasicObjCFoundationChecks.h" |
| 17 | |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 18 | #include "clang/Checker/PathSensitive/ExplodedGraph.h" |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 19 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 20 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
| 21 | #include "clang/Checker/PathSensitive/GRState.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 22 | #include "clang/Checker/BugReporter/BugType.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 23 | #include "clang/Checker/PathSensitive/MemRegion.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 24 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Ted Kremenek | 9705309 | 2010-01-26 22:59:55 +0000 | [diff] [blame] | 25 | #include "clang/Checker/Checkers/LocalCheckers.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 27 | #include "clang/AST/Expr.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 29 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 31 | using namespace clang; |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | class APIMisuse : public BugType { |
| 35 | public: |
| 36 | APIMisuse(const char* name) : BugType(name, "API Misuse (Apple)") {} |
| 37 | }; |
| 38 | } // end anonymous namespace |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Utility functions. |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 44 | static const ObjCInterfaceType* GetReceiverType(const ObjCMessageExpr* ME) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 45 | QualType T; |
| 46 | switch (ME->getReceiverKind()) { |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 47 | case ObjCMessageExpr::Instance: |
| 48 | T = ME->getInstanceReceiver()->getType(); |
| 49 | break; |
| 50 | |
| 51 | case ObjCMessageExpr::SuperInstance: |
| 52 | T = ME->getSuperType(); |
| 53 | break; |
| 54 | |
| 55 | case ObjCMessageExpr::Class: |
| 56 | case ObjCMessageExpr::SuperClass: |
| 57 | return 0; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 58 | } |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 60 | if (const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 61 | return PT->getInterfaceType(); |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | c1ff3cd | 2008-04-30 22:48:21 +0000 | [diff] [blame] | 63 | return NULL; |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 66 | static const char* GetReceiverNameType(const ObjCMessageExpr* ME) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 67 | if (const ObjCInterfaceType *ReceiverType = GetReceiverType(ME)) |
| 68 | return ReceiverType->getDecl()->getIdentifier()->getNameStart(); |
| 69 | return NULL; |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 70 | } |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 72 | static bool isNSString(llvm::StringRef ClassName) { |
| 73 | return ClassName == "NSString" || ClassName == "NSMutableString"; |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 76 | static inline bool isNil(SVal X) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | return isa<loc::ConcreteInt>(X); |
Ted Kremenek | e5d5c20 | 2008-03-27 21:15:17 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 80 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 81 | // NilArgChecker - Check for prohibited nil arguments to ObjC method calls. |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 82 | //===----------------------------------------------------------------------===// |
| 83 | |
Benjamin Kramer | cb9c074 | 2010-10-22 16:33:16 +0000 | [diff] [blame] | 84 | namespace { |
| 85 | class NilArgChecker : public CheckerVisitor<NilArgChecker> { |
| 86 | APIMisuse *BT; |
| 87 | void WarnNilArg(CheckerContext &C, const ObjCMessageExpr* ME, unsigned Arg); |
| 88 | public: |
| 89 | NilArgChecker() : BT(0) {} |
| 90 | static void *getTag() { static int x = 0; return &x; } |
| 91 | void PreVisitObjCMessageExpr(CheckerContext &C, const ObjCMessageExpr *ME); |
| 92 | }; |
| 93 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 95 | void NilArgChecker::WarnNilArg(CheckerContext &C, |
| 96 | const clang::ObjCMessageExpr *ME, |
| 97 | unsigned int Arg) |
| 98 | { |
| 99 | if (!BT) |
| 100 | BT = new APIMisuse("nil argument"); |
| 101 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame^] | 102 | if (ExplodedNode *N = C.generateSink()) { |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 103 | llvm::SmallString<128> sbuf; |
| 104 | llvm::raw_svector_ostream os(sbuf); |
| 105 | os << "Argument to '" << GetReceiverNameType(ME) << "' method '" |
| 106 | << ME->getSelector().getAsString() << "' cannot be nil"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 108 | RangedBugReport *R = new RangedBugReport(*BT, os.str(), N); |
| 109 | R->addRange(ME->getArg(Arg)->getSourceRange()); |
| 110 | C.EmitReport(R); |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 111 | } |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 114 | void NilArgChecker::PreVisitObjCMessageExpr(CheckerContext &C, |
| 115 | const ObjCMessageExpr *ME) |
| 116 | { |
| 117 | const ObjCInterfaceType *ReceiverType = GetReceiverType(ME); |
| 118 | if (!ReceiverType) |
| 119 | return; |
| 120 | |
| 121 | if (isNSString(ReceiverType->getDecl()->getIdentifier()->getName())) { |
| 122 | Selector S = ME->getSelector(); |
| 123 | |
| 124 | if (S.isUnarySelector()) |
| 125 | return; |
| 126 | |
| 127 | // FIXME: This is going to be really slow doing these checks with |
| 128 | // lexical comparisons. |
| 129 | |
| 130 | std::string NameStr = S.getAsString(); |
| 131 | llvm::StringRef Name(NameStr); |
| 132 | assert(!Name.empty()); |
| 133 | |
| 134 | // FIXME: Checking for initWithFormat: will not work in most cases |
| 135 | // yet because [NSString alloc] returns id, not NSString*. We will |
| 136 | // need support for tracking expected-type information in the analyzer |
| 137 | // to find these errors. |
| 138 | if (Name == "caseInsensitiveCompare:" || |
| 139 | Name == "compare:" || |
| 140 | Name == "compare:options:" || |
| 141 | Name == "compare:options:range:" || |
| 142 | Name == "compare:options:range:locale:" || |
| 143 | Name == "componentsSeparatedByCharactersInSet:" || |
| 144 | Name == "initWithFormat:") { |
| 145 | if (isNil(C.getState()->getSVal(ME->getArg(0)))) |
| 146 | WarnNilArg(C, ME, 0); |
| 147 | } |
| 148 | } |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 149 | } |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 150 | |
| 151 | //===----------------------------------------------------------------------===// |
| 152 | // Error reporting. |
| 153 | //===----------------------------------------------------------------------===// |
| 154 | |
| 155 | namespace { |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 156 | class CFNumberCreateChecker : public CheckerVisitor<CFNumberCreateChecker> { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 157 | APIMisuse* BT; |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 158 | IdentifierInfo* II; |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 159 | public: |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 160 | CFNumberCreateChecker() : BT(0), II(0) {} |
| 161 | ~CFNumberCreateChecker() {} |
| 162 | static void *getTag() { static int x = 0; return &x; } |
| 163 | void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 164 | private: |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 165 | void EmitError(const TypedRegion* R, const Expr* Ex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | uint64_t SourceSize, uint64_t TargetSize, uint64_t NumberKind); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 167 | }; |
| 168 | } // end anonymous namespace |
| 169 | |
| 170 | enum CFNumberType { |
| 171 | kCFNumberSInt8Type = 1, |
| 172 | kCFNumberSInt16Type = 2, |
| 173 | kCFNumberSInt32Type = 3, |
| 174 | kCFNumberSInt64Type = 4, |
| 175 | kCFNumberFloat32Type = 5, |
| 176 | kCFNumberFloat64Type = 6, |
| 177 | kCFNumberCharType = 7, |
| 178 | kCFNumberShortType = 8, |
| 179 | kCFNumberIntType = 9, |
| 180 | kCFNumberLongType = 10, |
| 181 | kCFNumberLongLongType = 11, |
| 182 | kCFNumberFloatType = 12, |
| 183 | kCFNumberDoubleType = 13, |
| 184 | kCFNumberCFIndexType = 14, |
| 185 | kCFNumberNSIntegerType = 15, |
| 186 | kCFNumberCGFloatType = 16 |
| 187 | }; |
| 188 | |
| 189 | namespace { |
| 190 | template<typename T> |
| 191 | class Optional { |
| 192 | bool IsKnown; |
| 193 | T Val; |
| 194 | public: |
| 195 | Optional() : IsKnown(false), Val(0) {} |
| 196 | Optional(const T& val) : IsKnown(true), Val(val) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 198 | bool isKnown() const { return IsKnown; } |
| 199 | |
| 200 | const T& getValue() const { |
| 201 | assert (isKnown()); |
| 202 | return Val; |
| 203 | } |
| 204 | |
| 205 | operator const T&() const { |
| 206 | return getValue(); |
| 207 | } |
| 208 | }; |
| 209 | } |
| 210 | |
| 211 | static Optional<uint64_t> GetCFNumberSize(ASTContext& Ctx, uint64_t i) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 212 | static const unsigned char FixedSize[] = { 8, 16, 32, 64, 32, 64 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 214 | if (i < kCFNumberCharType) |
| 215 | return FixedSize[i-1]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 217 | QualType T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 219 | switch (i) { |
| 220 | case kCFNumberCharType: T = Ctx.CharTy; break; |
| 221 | case kCFNumberShortType: T = Ctx.ShortTy; break; |
| 222 | case kCFNumberIntType: T = Ctx.IntTy; break; |
| 223 | case kCFNumberLongType: T = Ctx.LongTy; break; |
| 224 | case kCFNumberLongLongType: T = Ctx.LongLongTy; break; |
| 225 | case kCFNumberFloatType: T = Ctx.FloatTy; break; |
| 226 | case kCFNumberDoubleType: T = Ctx.DoubleTy; break; |
| 227 | case kCFNumberCFIndexType: |
| 228 | case kCFNumberNSIntegerType: |
| 229 | case kCFNumberCGFloatType: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | // FIXME: We need a way to map from names to Type*. |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 231 | default: |
| 232 | return Optional<uint64_t>(); |
| 233 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 235 | return Ctx.getTypeSize(T); |
| 236 | } |
| 237 | |
| 238 | #if 0 |
| 239 | static const char* GetCFNumberTypeStr(uint64_t i) { |
| 240 | static const char* Names[] = { |
| 241 | "kCFNumberSInt8Type", |
| 242 | "kCFNumberSInt16Type", |
| 243 | "kCFNumberSInt32Type", |
| 244 | "kCFNumberSInt64Type", |
| 245 | "kCFNumberFloat32Type", |
| 246 | "kCFNumberFloat64Type", |
| 247 | "kCFNumberCharType", |
| 248 | "kCFNumberShortType", |
| 249 | "kCFNumberIntType", |
| 250 | "kCFNumberLongType", |
| 251 | "kCFNumberLongLongType", |
| 252 | "kCFNumberFloatType", |
| 253 | "kCFNumberDoubleType", |
| 254 | "kCFNumberCFIndexType", |
| 255 | "kCFNumberNSIntegerType", |
| 256 | "kCFNumberCGFloatType" |
| 257 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 259 | return i <= kCFNumberCGFloatType ? Names[i-1] : "Invalid CFNumberType"; |
| 260 | } |
| 261 | #endif |
| 262 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 263 | void CFNumberCreateChecker::PreVisitCallExpr(CheckerContext &C, |
| 264 | const CallExpr *CE) |
| 265 | { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | const Expr* Callee = CE->getCallee(); |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 267 | const GRState *state = C.getState(); |
| 268 | SVal CallV = state->getSVal(Callee); |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 269 | const FunctionDecl* FD = CallV.getAsFunctionDecl(); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 271 | if (!FD) |
| 272 | return; |
| 273 | |
| 274 | ASTContext &Ctx = C.getASTContext(); |
| 275 | if (!II) |
| 276 | II = &Ctx.Idents.get("CFNumberCreate"); |
| 277 | |
| 278 | if (FD->getIdentifier() != II || CE->getNumArgs() != 3) |
| 279 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 281 | // Get the value of the "theType" argument. |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 282 | SVal TheTypeVal = state->getSVal(CE->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 284 | // FIXME: We really should allow ranges of valid theType values, and |
| 285 | // bifurcate the state appropriately. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 286 | nonloc::ConcreteInt* V = dyn_cast<nonloc::ConcreteInt>(&TheTypeVal); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 287 | if (!V) |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 288 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 290 | uint64_t NumberKind = V->getValue().getLimitedValue(); |
| 291 | Optional<uint64_t> TargetSize = GetCFNumberSize(Ctx, NumberKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 293 | // FIXME: In some cases we can emit an error. |
| 294 | if (!TargetSize.isKnown()) |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 295 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 297 | // Look at the value of the integer being passed by reference. Essentially |
| 298 | // we want to catch cases where the value passed in is not equal to the |
| 299 | // size of the type being created. |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 300 | SVal TheValueExpr = state->getSVal(CE->getArg(2)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 302 | // FIXME: Eventually we should handle arbitrary locations. We can do this |
| 303 | // by having an enhanced memory model that does low-level typing. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 304 | loc::MemRegionVal* LV = dyn_cast<loc::MemRegionVal>(&TheValueExpr); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 305 | if (!LV) |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 306 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Zhongxing Xu | 479529e | 2009-11-10 02:17:20 +0000 | [diff] [blame] | 308 | const TypedRegion* R = dyn_cast<TypedRegion>(LV->StripCasts()); |
Ted Kremenek | 5e77eba | 2009-07-29 18:17:40 +0000 | [diff] [blame] | 309 | if (!R) |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 310 | return; |
Ted Kremenek | 5e77eba | 2009-07-29 18:17:40 +0000 | [diff] [blame] | 311 | |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 312 | QualType T = Ctx.getCanonicalType(R->getValueType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 314 | // FIXME: If the pointee isn't an integer type, should we flag a warning? |
| 315 | // People can do weird stuff with pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
| 317 | if (!T->isIntegerType()) |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 318 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 320 | uint64_t SourceSize = Ctx.getTypeSize(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 322 | // CHECK: is SourceSize == TargetSize |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 323 | if (SourceSize == TargetSize) |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 324 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 326 | // Generate an error. Only generate a sink if 'SourceSize < TargetSize'; |
| 327 | // otherwise generate a regular node. |
| 328 | // |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 329 | // FIXME: We can actually create an abstract "CFNumber" object that has |
| 330 | // the bits initialized to the provided values. |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 331 | // |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame^] | 332 | if (ExplodedNode *N = SourceSize < TargetSize ? C.generateSink() |
| 333 | : C.generateNode()) { |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 334 | llvm::SmallString<128> sbuf; |
| 335 | llvm::raw_svector_ostream os(sbuf); |
| 336 | |
| 337 | os << (SourceSize == 8 ? "An " : "A ") |
| 338 | << SourceSize << " bit integer is used to initialize a CFNumber " |
| 339 | "object that represents " |
| 340 | << (TargetSize == 8 ? "an " : "a ") |
| 341 | << TargetSize << " bit integer. "; |
| 342 | |
| 343 | if (SourceSize < TargetSize) |
| 344 | os << (TargetSize - SourceSize) |
| 345 | << " bits of the CFNumber value will be garbage." ; |
| 346 | else |
| 347 | os << (SourceSize - TargetSize) |
| 348 | << " bits of the input integer will be lost."; |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 350 | if (!BT) |
| 351 | BT = new APIMisuse("Bad use of CFNumberCreate"); |
| 352 | |
| 353 | RangedBugReport *report = new RangedBugReport(*BT, os.str(), N); |
| 354 | report->addRange(CE->getArg(2)->getSourceRange()); |
| 355 | C.EmitReport(report); |
| 356 | } |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 359 | //===----------------------------------------------------------------------===// |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 360 | // CFRetain/CFRelease checking for null arguments. |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 361 | //===----------------------------------------------------------------------===// |
| 362 | |
| 363 | namespace { |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 364 | class CFRetainReleaseChecker : public CheckerVisitor<CFRetainReleaseChecker> { |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 365 | APIMisuse *BT; |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 366 | IdentifierInfo *Retain, *Release; |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 367 | public: |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 368 | CFRetainReleaseChecker(): BT(0), Retain(0), Release(0) {} |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 369 | static void *getTag() { static int x = 0; return &x; } |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 370 | void PreVisitCallExpr(CheckerContext& C, const CallExpr* CE); |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 371 | }; |
| 372 | } // end anonymous namespace |
| 373 | |
| 374 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 375 | void CFRetainReleaseChecker::PreVisitCallExpr(CheckerContext& C, |
| 376 | const CallExpr* CE) { |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 377 | // If the CallExpr doesn't have exactly 1 argument just give up checking. |
| 378 | if (CE->getNumArgs() != 1) |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 379 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 381 | // Get the function declaration of the callee. |
| 382 | const GRState* state = C.getState(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 383 | SVal X = state->getSVal(CE->getCallee()); |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 384 | const FunctionDecl* FD = X.getAsFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 386 | if (!FD) |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 387 | return; |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 388 | |
| 389 | if (!BT) { |
| 390 | ASTContext &Ctx = C.getASTContext(); |
| 391 | Retain = &Ctx.Idents.get("CFRetain"); |
| 392 | Release = &Ctx.Idents.get("CFRelease"); |
| 393 | BT = new APIMisuse("null passed to CFRetain/CFRelease"); |
| 394 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 396 | // Check if we called CFRetain/CFRelease. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | const IdentifierInfo *FuncII = FD->getIdentifier(); |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 398 | if (!(FuncII == Retain || FuncII == Release)) |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 399 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 401 | // FIXME: The rest of this just checks that the argument is non-null. |
| 402 | // It should probably be refactored and combined with AttrNonNullChecker. |
| 403 | |
| 404 | // Get the argument's value. |
| 405 | const Expr *Arg = CE->getArg(0); |
| 406 | SVal ArgVal = state->getSVal(Arg); |
| 407 | DefinedSVal *DefArgVal = dyn_cast<DefinedSVal>(&ArgVal); |
| 408 | if (!DefArgVal) |
| 409 | return; |
| 410 | |
| 411 | // Get a NULL value. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 412 | SValBuilder &svalBuilder = C.getSValBuilder(); |
| 413 | DefinedSVal zero = cast<DefinedSVal>(svalBuilder.makeZeroVal(Arg->getType())); |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 414 | |
| 415 | // Make an expression asserting that they're equal. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 416 | DefinedOrUnknownSVal ArgIsNull = svalBuilder.evalEQ(state, zero, *DefArgVal); |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 417 | |
| 418 | // Are they equal? |
| 419 | const GRState *stateTrue, *stateFalse; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 420 | llvm::tie(stateTrue, stateFalse) = state->assume(ArgIsNull); |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 421 | |
| 422 | if (stateTrue && !stateFalse) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame^] | 423 | ExplodedNode *N = C.generateSink(stateTrue); |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 424 | if (!N) |
| 425 | return; |
| 426 | |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 427 | const char *description = (FuncII == Retain) |
| 428 | ? "Null pointer argument in call to CFRetain" |
| 429 | : "Null pointer argument in call to CFRelease"; |
| 430 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 431 | EnhancedBugReport *report = new EnhancedBugReport(*BT, description, N); |
| 432 | report->addRange(Arg->getSourceRange()); |
| 433 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Arg); |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 434 | C.EmitReport(report); |
| 435 | return; |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 438 | // From here on, we know the argument is non-null. |
| 439 | C.addTransition(stateFalse); |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 443 | // Check for sending 'retain', 'release', or 'autorelease' directly to a Class. |
| 444 | //===----------------------------------------------------------------------===// |
| 445 | |
| 446 | namespace { |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 447 | class ClassReleaseChecker : public CheckerVisitor<ClassReleaseChecker> { |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 448 | Selector releaseS; |
| 449 | Selector retainS; |
| 450 | Selector autoreleaseS; |
| 451 | Selector drainS; |
| 452 | BugType *BT; |
| 453 | public: |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 454 | ClassReleaseChecker() |
| 455 | : BT(0) {} |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 456 | |
| 457 | static void *getTag() { static int x = 0; return &x; } |
| 458 | |
| 459 | void PreVisitObjCMessageExpr(CheckerContext &C, const ObjCMessageExpr *ME); |
| 460 | }; |
| 461 | } |
| 462 | |
| 463 | void ClassReleaseChecker::PreVisitObjCMessageExpr(CheckerContext &C, |
| 464 | const ObjCMessageExpr *ME) { |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 465 | |
| 466 | if (!BT) { |
| 467 | BT = new APIMisuse("message incorrectly sent to class instead of class " |
| 468 | "instance"); |
| 469 | |
| 470 | ASTContext &Ctx = C.getASTContext(); |
| 471 | releaseS = GetNullarySelector("release", Ctx); |
| 472 | retainS = GetNullarySelector("retain", Ctx); |
| 473 | autoreleaseS = GetNullarySelector("autorelease", Ctx); |
| 474 | drainS = GetNullarySelector("drain", Ctx); |
| 475 | } |
| 476 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 477 | ObjCInterfaceDecl *Class = 0; |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 478 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 479 | switch (ME->getReceiverKind()) { |
| 480 | case ObjCMessageExpr::Class: |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 481 | Class = ME->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 482 | break; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 483 | case ObjCMessageExpr::SuperClass: |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 484 | Class = ME->getSuperType()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 485 | break; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 486 | case ObjCMessageExpr::Instance: |
| 487 | case ObjCMessageExpr::SuperInstance: |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 488 | return; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 491 | Selector S = ME->getSelector(); |
Benjamin Kramer | 921ddc4 | 2009-11-20 10:03:00 +0000 | [diff] [blame] | 492 | if (!(S == releaseS || S == retainS || S == autoreleaseS || S == drainS)) |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 493 | return; |
| 494 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame^] | 495 | if (ExplodedNode *N = C.generateNode()) { |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 496 | llvm::SmallString<200> buf; |
| 497 | llvm::raw_svector_ostream os(buf); |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 499 | os << "The '" << S.getAsString() << "' message should be sent to instances " |
| 500 | "of class '" << Class->getName() |
| 501 | << "' and not the class directly"; |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 503 | RangedBugReport *report = new RangedBugReport(*BT, os.str(), N); |
| 504 | report->addRange(ME->getSourceRange()); |
| 505 | C.EmitReport(report); |
| 506 | } |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 510 | // Check registration. |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 511 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 512 | |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 513 | void clang::RegisterAppleChecks(GRExprEngine& Eng, const Decl &D) { |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 514 | Eng.registerCheck(new NilArgChecker()); |
| 515 | Eng.registerCheck(new CFNumberCreateChecker()); |
| 516 | RegisterNSErrorChecks(Eng.getBugReporter(), Eng, D); |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 517 | RegisterNSAutoreleasePoolChecks(Eng); |
Ted Kremenek | 2ce2baa | 2010-10-20 23:38:56 +0000 | [diff] [blame] | 518 | Eng.registerCheck(new CFRetainReleaseChecker()); |
| 519 | Eng.registerCheck(new ClassReleaseChecker()); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 520 | } |