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