Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 1 | //=- NSErrorChecker.cpp - Coding conventions for uses of NSError -*- C++ -*-==// |
Ted Kremenek | fc3abeb | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 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 a CheckNSError, a flow-insenstive check |
| 11 | // that determines if an Objective-C class interface correctly returns |
| 12 | // a non-void return type. |
| 13 | // |
| 14 | // File under feature request PR 2600. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 18 | #include "ClangSACheckers.h" |
| 19 | #include "clang/StaticAnalyzer/Core/CheckerV2.h" |
| 20 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Ted Kremenek | fc3abeb | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 25 | #include "clang/AST/Decl.h" |
| 26 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | fc3abeb | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 29 | using namespace ento; |
Ted Kremenek | fc3abeb | 2008-09-18 06:33:41 +0000 | [diff] [blame] | 30 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 31 | static bool IsNSError(const ParmVarDecl *PD, IdentifierInfo *II); |
| 32 | static bool IsCFError(const ParmVarDecl *PD, IdentifierInfo *II); |
| 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // NSErrorMethodChecker |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 38 | namespace { |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 39 | class NSErrorMethodChecker |
| 40 | : public CheckerV2< check::ASTDecl<ObjCMethodDecl> > { |
| 41 | mutable IdentifierInfo *II; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 43 | public: |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 44 | NSErrorMethodChecker() : II(0) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 46 | void checkASTDecl(const ObjCMethodDecl *D, |
| 47 | AnalysisManager &mgr, BugReporter &BR) const; |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | void NSErrorMethodChecker::checkASTDecl(const ObjCMethodDecl *D, |
| 52 | AnalysisManager &mgr, |
| 53 | BugReporter &BR) const { |
| 54 | if (!D->isThisDeclarationADefinition()) |
| 55 | return; |
| 56 | if (!D->getResultType()->isVoidType()) |
| 57 | return; |
| 58 | |
| 59 | if (!II) |
| 60 | II = &D->getASTContext().Idents.get("NSError"); |
| 61 | |
| 62 | bool hasNSError = false; |
| 63 | for (ObjCMethodDecl::param_iterator |
| 64 | I = D->param_begin(), E = D->param_end(); I != E; ++I) { |
| 65 | if (IsNSError(*I, II)) { |
| 66 | hasNSError = true; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (hasNSError) { |
| 72 | const char *err = "Method accepting NSError** " |
| 73 | "should have a non-void return value to indicate whether or not an " |
| 74 | "error occurred"; |
| 75 | BR.EmitBasicReport("Bad return type when passing NSError**", |
| 76 | "Coding conventions (Apple)", err, D->getLocation()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | // CFErrorFunctionChecker |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | |
| 84 | namespace { |
| 85 | class CFErrorFunctionChecker |
| 86 | : public CheckerV2< check::ASTDecl<FunctionDecl> > { |
| 87 | mutable IdentifierInfo *II; |
| 88 | |
| 89 | public: |
| 90 | CFErrorFunctionChecker() : II(0) { } |
| 91 | |
| 92 | void checkASTDecl(const FunctionDecl *D, |
| 93 | AnalysisManager &mgr, BugReporter &BR) const; |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D, |
| 98 | AnalysisManager &mgr, |
| 99 | BugReporter &BR) const { |
| 100 | if (!D->isThisDeclarationADefinition()) |
| 101 | return; |
| 102 | if (!D->getResultType()->isVoidType()) |
| 103 | return; |
| 104 | |
| 105 | if (!II) |
| 106 | II = &D->getASTContext().Idents.get("CFErrorRef"); |
| 107 | |
| 108 | bool hasCFError = false; |
| 109 | for (FunctionDecl::param_const_iterator |
| 110 | I = D->param_begin(), E = D->param_end(); I != E; ++I) { |
| 111 | if (IsCFError(*I, II)) { |
| 112 | hasCFError = true; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (hasCFError) { |
| 118 | const char *err = "Function accepting CFErrorRef* " |
| 119 | "should have a non-void return value to indicate whether or not an " |
| 120 | "error occurred"; |
| 121 | BR.EmitBasicReport("Bad return type when passing CFErrorRef*", |
| 122 | "Coding conventions (Apple)", err, D->getLocation()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | // NSOrCFErrorDerefChecker |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
| 130 | namespace { |
| 131 | |
| 132 | class NSErrorDerefBug : public BugType { |
| 133 | public: |
| 134 | NSErrorDerefBug() : BugType("NSError** null dereference", |
| 135 | "Coding conventions (Apple)") {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 138 | class CFErrorDerefBug : public BugType { |
| 139 | public: |
| 140 | CFErrorDerefBug() : BugType("CFErrorRef* null dereference", |
| 141 | "Coding conventions (Apple)") {} |
| 142 | }; |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 146 | namespace { |
| 147 | class NSOrCFErrorDerefChecker |
| 148 | : public CheckerV2< check::Location, |
| 149 | check::Event<ImplicitNullDerefEvent> > { |
| 150 | mutable IdentifierInfo *NSErrorII, *CFErrorII; |
| 151 | public: |
| 152 | bool ShouldCheckNSError, ShouldCheckCFError; |
| 153 | NSOrCFErrorDerefChecker() : NSErrorII(0), CFErrorII(0), |
| 154 | ShouldCheckNSError(0), ShouldCheckCFError(0) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 156 | void checkLocation(SVal loc, bool isLoad, CheckerContext &C) const; |
| 157 | void checkEvent(ImplicitNullDerefEvent event) const; |
| 158 | }; |
| 159 | } |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 160 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 161 | namespace { struct NSErrorOut {}; } |
| 162 | namespace { struct CFErrorOut {}; } |
Ted Kremenek | 3aa89a9 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 163 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 164 | typedef llvm::ImmutableMap<SymbolRef, unsigned> ErrorOutFlag; |
| 165 | |
| 166 | namespace clang { |
| 167 | namespace ento { |
| 168 | template <> |
| 169 | struct GRStateTrait<NSErrorOut> : public GRStatePartialTrait<ErrorOutFlag> { |
| 170 | static void *GDMIndex() { static int index = 0; return &index; } |
| 171 | }; |
| 172 | template <> |
| 173 | struct GRStateTrait<CFErrorOut> : public GRStatePartialTrait<ErrorOutFlag> { |
| 174 | static void *GDMIndex() { static int index = 0; return &index; } |
| 175 | }; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | template <typename T> |
| 180 | static bool hasFlag(SVal val, const GRState *state) { |
| 181 | if (SymbolRef sym = val.getAsSymbol()) |
| 182 | if (const unsigned *attachedFlags = state->get<T>(sym)) |
| 183 | return *attachedFlags; |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | template <typename T> |
| 188 | static void setFlag(const GRState *state, SVal val, CheckerContext &C) { |
| 189 | // We tag the symbol that the SVal wraps. |
| 190 | if (SymbolRef sym = val.getAsSymbol()) |
| 191 | C.addTransition(state->set<T>(sym, true)); |
| 192 | } |
| 193 | |
| 194 | void NSOrCFErrorDerefChecker::checkLocation(SVal loc, bool isLoad, |
| 195 | CheckerContext &C) const { |
| 196 | if (!isLoad) |
| 197 | return; |
| 198 | if (loc.isUndef() || !isa<Loc>(loc)) |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 199 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 201 | const GRState *state = C.getState(); |
| 202 | |
| 203 | // If we are loading from NSError**/CFErrorRef* parameter, mark the resulting |
| 204 | // SVal so that we can later check it when handling the |
| 205 | // ImplicitNullDerefEvent event. |
| 206 | // FIXME: Cumbersome! Maybe add hook at construction of SVals at start of |
| 207 | // function ? |
| 208 | |
| 209 | const VarDecl *VD = loc.getAsVarDecl(); |
| 210 | if (!VD) return; |
| 211 | const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(VD); |
| 212 | if (!PD) return; |
| 213 | |
| 214 | if (!NSErrorII) |
| 215 | NSErrorII = &PD->getASTContext().Idents.get("NSError"); |
| 216 | if (!CFErrorII) |
| 217 | CFErrorII = &PD->getASTContext().Idents.get("CFErrorRef"); |
| 218 | |
| 219 | if (ShouldCheckNSError && IsNSError(PD, NSErrorII)) { |
| 220 | setFlag<NSErrorOut>(state, state->getSVal(cast<Loc>(loc)), C); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | if (ShouldCheckCFError && IsCFError(PD, CFErrorII)) { |
| 225 | setFlag<CFErrorOut>(state, state->getSVal(cast<Loc>(loc)), C); |
| 226 | return; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void NSOrCFErrorDerefChecker::checkEvent(ImplicitNullDerefEvent event) const { |
| 231 | if (event.IsLoad) |
Ted Kremenek | 3aa89a9 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 232 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 234 | SVal loc = event.Location; |
| 235 | const GRState *state = event.SinkNode->getState(); |
| 236 | BugReporter &BR = *event.BR; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 238 | bool isNSError = hasFlag<NSErrorOut>(loc, state); |
| 239 | bool isCFError = false; |
| 240 | if (!isNSError) |
| 241 | isCFError = hasFlag<CFErrorOut>(loc, state); |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 242 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 243 | if (!(isNSError || isCFError)) |
| 244 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 246 | // Storing to possible null NSError/CFErrorRef out parameter. |
| 247 | |
| 248 | // Emit an error. |
| 249 | std::string err; |
| 250 | llvm::raw_string_ostream os(err); |
| 251 | os << "Potential null dereference. According to coding standards "; |
| 252 | |
| 253 | if (isNSError) |
| 254 | os << "in 'Creating and Returning NSError Objects' the parameter '"; |
Ted Kremenek | 3aa89a9 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 255 | else |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 256 | os << "documented in CoreFoundation/CFError.h the parameter '"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 258 | os << "' may be null."; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 260 | BugType *bug = 0; |
| 261 | if (isNSError) |
| 262 | bug = new NSErrorDerefBug(); |
| 263 | else |
| 264 | bug = new CFErrorDerefBug(); |
| 265 | EnhancedBugReport *report = new EnhancedBugReport(*bug, os.str(), |
| 266 | event.SinkNode); |
| 267 | BR.EmitReport(report); |
Ted Kremenek | 3aa89a9 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 270 | static bool IsNSError(const ParmVarDecl *PD, IdentifierInfo *II) { |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 271 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 272 | const PointerType* PPT = PD->getType()->getAs<PointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 273 | if (!PPT) |
| 274 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 276 | const ObjCObjectPointerType* PT = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 277 | PPT->getPointeeType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 278 | |
| 279 | if (!PT) |
| 280 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 282 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 284 | // FIXME: Can ID ever be NULL? |
| 285 | if (ID) |
| 286 | return II == ID->getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 288 | return false; |
Ted Kremenek | f0673e4 | 2008-09-18 21:25:13 +0000 | [diff] [blame] | 289 | } |
Ted Kremenek | b42f482 | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 290 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 291 | static bool IsCFError(const ParmVarDecl *PD, IdentifierInfo *II) { |
| 292 | const PointerType* PPT = PD->getType()->getAs<PointerType>(); |
Ted Kremenek | 3aa89a9 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 293 | if (!PPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 295 | const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>(); |
Ted Kremenek | 3aa89a9 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 296 | if (!TT) return false; |
| 297 | |
Ted Kremenek | fc5d067 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 298 | return TT->getDecl()->getIdentifier() == II; |
Ted Kremenek | 3aa89a9 | 2008-10-01 23:24:09 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 301 | void ento::registerNSErrorChecker(CheckerManager &mgr) { |
| 302 | mgr.registerChecker<NSErrorMethodChecker>(); |
| 303 | NSOrCFErrorDerefChecker * |
| 304 | checker = mgr.registerChecker<NSOrCFErrorDerefChecker>(); |
| 305 | checker->ShouldCheckNSError = true; |
| 306 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Argyrios Kyrtzidis | 2c49ec7 | 2011-02-28 17:36:18 +0000 | [diff] [blame^] | 308 | void ento::registerCFErrorChecker(CheckerManager &mgr) { |
| 309 | mgr.registerChecker<CFErrorFunctionChecker>(); |
| 310 | NSOrCFErrorDerefChecker * |
| 311 | checker = mgr.registerChecker<NSOrCFErrorDerefChecker>(); |
| 312 | checker->ShouldCheckCFError = true; |
Ted Kremenek | b42f482 | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 313 | } |