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