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