blob: 870e38e386bb277dd03c9c2bed481c4c6a307619 [file] [log] [blame]
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +00001//=- NSErrorChecker.cpp - Coding conventions for uses of NSError -*- C++ -*-==//
Ted Kremenekf45d18c2008-09-18 06:33:41 +00002//
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 Kyrtzidisb3d74da2011-02-28 17:36:18 +000018#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000019#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000020#include "clang/StaticAnalyzer/Core/CheckerManager.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000023#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenekf45d18c2008-09-18 06:33:41 +000024#include "clang/AST/DeclObjC.h"
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000025#include "clang/AST/Decl.h"
26#include "llvm/ADT/SmallVector.h"
Ted Kremenekf45d18c2008-09-18 06:33:41 +000027
28using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000029using namespace ento;
Ted Kremenekf45d18c2008-09-18 06:33:41 +000030
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +000031static bool IsNSError(QualType T, IdentifierInfo *II);
32static bool IsCFError(QualType T, IdentifierInfo *II);
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000033
34//===----------------------------------------------------------------------===//
35// NSErrorMethodChecker
36//===----------------------------------------------------------------------===//
37
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000038namespace {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000039class NSErrorMethodChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000040 : public Checker< check::ASTDecl<ObjCMethodDecl> > {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000041 mutable IdentifierInfo *II;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Ted Kremenekcfdf9b42008-09-18 21:25:13 +000043public:
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000044 NSErrorMethodChecker() : II(0) { }
Mike Stump1eb44332009-09-09 15:08:12 +000045
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000046 void checkASTDecl(const ObjCMethodDecl *D,
47 AnalysisManager &mgr, BugReporter &BR) const;
48};
49}
50
51void 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;
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +000063 for (ObjCMethodDecl::param_const_iterator
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000064 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +000065 if (IsNSError((*I)->getType(), II)) {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000066 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";
Anna Zaks590dd8e2011-09-20 21:38:35 +000075 PathDiagnosticLocation L =
76 PathDiagnosticLocation::create(D, BR.getSourceManager());
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000077 BR.EmitBasicReport("Bad return type when passing NSError**",
Anna Zaks590dd8e2011-09-20 21:38:35 +000078 "Coding conventions (Apple)", err, L);
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000079 }
80}
81
82//===----------------------------------------------------------------------===//
83// CFErrorFunctionChecker
84//===----------------------------------------------------------------------===//
85
86namespace {
87class CFErrorFunctionChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000088 : public Checker< check::ASTDecl<FunctionDecl> > {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +000089 mutable IdentifierInfo *II;
90
91public:
92 CFErrorFunctionChecker() : II(0) { }
93
94 void checkASTDecl(const FunctionDecl *D,
95 AnalysisManager &mgr, BugReporter &BR) const;
96};
97}
98
99void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D,
100 AnalysisManager &mgr,
101 BugReporter &BR) const {
Sean Hunt10620eb2011-05-06 20:44:56 +0000102 if (!D->doesThisDeclarationHaveABody())
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000103 return;
104 if (!D->getResultType()->isVoidType())
105 return;
106
107 if (!II)
108 II = &D->getASTContext().Idents.get("CFErrorRef");
109
110 bool hasCFError = false;
111 for (FunctionDecl::param_const_iterator
112 I = D->param_begin(), E = D->param_end(); I != E; ++I) {
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000113 if (IsCFError((*I)->getType(), II)) {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000114 hasCFError = true;
115 break;
116 }
117 }
118
119 if (hasCFError) {
120 const char *err = "Function accepting CFErrorRef* "
121 "should have a non-void return value to indicate whether or not an "
122 "error occurred";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000123 PathDiagnosticLocation L =
124 PathDiagnosticLocation::create(D, BR.getSourceManager());
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000125 BR.EmitBasicReport("Bad return type when passing CFErrorRef*",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000126 "Coding conventions (Apple)", err, L);
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000127 }
128}
129
130//===----------------------------------------------------------------------===//
131// NSOrCFErrorDerefChecker
132//===----------------------------------------------------------------------===//
133
134namespace {
135
136class NSErrorDerefBug : public BugType {
137public:
138 NSErrorDerefBug() : BugType("NSError** null dereference",
139 "Coding conventions (Apple)") {}
Mike Stump1eb44332009-09-09 15:08:12 +0000140};
141
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000142class CFErrorDerefBug : public BugType {
143public:
144 CFErrorDerefBug() : BugType("CFErrorRef* null dereference",
145 "Coding conventions (Apple)") {}
146};
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000147
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000148}
149
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000150namespace {
151class NSOrCFErrorDerefChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000152 : public Checker< check::Location,
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000153 check::Event<ImplicitNullDerefEvent> > {
154 mutable IdentifierInfo *NSErrorII, *CFErrorII;
155public:
156 bool ShouldCheckNSError, ShouldCheckCFError;
157 NSOrCFErrorDerefChecker() : NSErrorII(0), CFErrorII(0),
158 ShouldCheckNSError(0), ShouldCheckCFError(0) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Anna Zaks390909c2011-10-06 00:43:15 +0000160 void checkLocation(SVal loc, bool isLoad, const Stmt *S,
161 CheckerContext &C) const;
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000162 void checkEvent(ImplicitNullDerefEvent event) const;
163};
164}
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000165
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000166namespace { struct NSErrorOut {}; }
167namespace { struct CFErrorOut {}; }
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000168
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000169typedef llvm::ImmutableMap<SymbolRef, unsigned> ErrorOutFlag;
170
171namespace clang {
172namespace ento {
173 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000174 struct ProgramStateTrait<NSErrorOut> : public ProgramStatePartialTrait<ErrorOutFlag> {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000175 static void *GDMIndex() { static int index = 0; return &index; }
176 };
177 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000178 struct ProgramStateTrait<CFErrorOut> : public ProgramStatePartialTrait<ErrorOutFlag> {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000179 static void *GDMIndex() { static int index = 0; return &index; }
180 };
181}
182}
183
184template <typename T>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000185static bool hasFlag(SVal val, const ProgramState *state) {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000186 if (SymbolRef sym = val.getAsSymbol())
187 if (const unsigned *attachedFlags = state->get<T>(sym))
188 return *attachedFlags;
189 return false;
190}
191
192template <typename T>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000193static void setFlag(const ProgramState *state, SVal val, CheckerContext &C) {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000194 // We tag the symbol that the SVal wraps.
195 if (SymbolRef sym = val.getAsSymbol())
Anna Zaks0bd6b112011-10-26 21:06:34 +0000196 C.addTransition(state->set<T>(sym, true));
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000197}
198
Argyrios Kyrtzidis4ba48c42011-03-01 01:59:41 +0000199static QualType parameterTypeFromSVal(SVal val, CheckerContext &C) {
200 const StackFrameContext *
Anna Zaks39ac1872011-10-26 21:06:44 +0000201 SFC = C.getLocationContext()->getCurrentStackFrame();
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000202 if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(&val)) {
203 const MemRegion* R = X->getRegion();
204 if (const VarRegion *VR = R->getAs<VarRegion>())
Argyrios Kyrtzidis4ba48c42011-03-01 01:59:41 +0000205 if (const StackArgumentsSpaceRegion *
206 stackReg = dyn_cast<StackArgumentsSpaceRegion>(VR->getMemorySpace()))
207 if (stackReg->getStackFrame() == SFC)
208 return VR->getValueType();
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000209 }
210
211 return QualType();
212}
213
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000214void NSOrCFErrorDerefChecker::checkLocation(SVal loc, bool isLoad,
Anna Zaks390909c2011-10-06 00:43:15 +0000215 const Stmt *S,
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000216 CheckerContext &C) const {
217 if (!isLoad)
218 return;
219 if (loc.isUndef() || !isa<Loc>(loc))
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000220 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000222 ASTContext &Ctx = C.getASTContext();
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000223 const ProgramState *state = C.getState();
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000224
225 // If we are loading from NSError**/CFErrorRef* parameter, mark the resulting
226 // SVal so that we can later check it when handling the
227 // ImplicitNullDerefEvent event.
228 // FIXME: Cumbersome! Maybe add hook at construction of SVals at start of
229 // function ?
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000230
Argyrios Kyrtzidis4ba48c42011-03-01 01:59:41 +0000231 QualType parmT = parameterTypeFromSVal(loc, C);
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000232 if (parmT.isNull())
233 return;
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000234
235 if (!NSErrorII)
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000236 NSErrorII = &Ctx.Idents.get("NSError");
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000237 if (!CFErrorII)
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000238 CFErrorII = &Ctx.Idents.get("CFErrorRef");
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000239
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000240 if (ShouldCheckNSError && IsNSError(parmT, NSErrorII)) {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000241 setFlag<NSErrorOut>(state, state->getSVal(cast<Loc>(loc)), C);
242 return;
243 }
244
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000245 if (ShouldCheckCFError && IsCFError(parmT, CFErrorII)) {
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000246 setFlag<CFErrorOut>(state, state->getSVal(cast<Loc>(loc)), C);
247 return;
248 }
249}
250
251void NSOrCFErrorDerefChecker::checkEvent(ImplicitNullDerefEvent event) const {
252 if (event.IsLoad)
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000253 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000255 SVal loc = event.Location;
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000256 const ProgramState *state = event.SinkNode->getState();
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000257 BugReporter &BR = *event.BR;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000259 bool isNSError = hasFlag<NSErrorOut>(loc, state);
260 bool isCFError = false;
261 if (!isNSError)
262 isCFError = hasFlag<CFErrorOut>(loc, state);
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000263
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000264 if (!(isNSError || isCFError))
265 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000267 // Storing to possible null NSError/CFErrorRef out parameter.
268
269 // Emit an error.
270 std::string err;
271 llvm::raw_string_ostream os(err);
272 os << "Potential null dereference. According to coding standards ";
273
274 if (isNSError)
275 os << "in 'Creating and Returning NSError Objects' the parameter '";
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000276 else
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000277 os << "documented in CoreFoundation/CFError.h the parameter '";
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000279 os << "' may be null.";
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000281 BugType *bug = 0;
282 if (isNSError)
283 bug = new NSErrorDerefBug();
284 else
285 bug = new CFErrorDerefBug();
Anna Zakse172e8b2011-08-17 23:00:25 +0000286 BugReport *report = new BugReport(*bug, os.str(),
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000287 event.SinkNode);
288 BR.EmitReport(report);
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000289}
290
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000291static bool IsNSError(QualType T, IdentifierInfo *II) {
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000292
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000293 const PointerType* PPT = T->getAs<PointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000294 if (!PPT)
295 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Steve Naroff14108da2009-07-10 23:34:53 +0000297 const ObjCObjectPointerType* PT =
John McCall183700f2009-09-21 23:43:11 +0000298 PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000299
300 if (!PT)
301 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Steve Naroff14108da2009-07-10 23:34:53 +0000303 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Steve Naroff14108da2009-07-10 23:34:53 +0000305 // FIXME: Can ID ever be NULL?
306 if (ID)
307 return II == ID->getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Steve Naroff14108da2009-07-10 23:34:53 +0000309 return false;
Ted Kremenekcfdf9b42008-09-18 21:25:13 +0000310}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000311
Argyrios Kyrtzidis25a792b2011-03-01 01:47:48 +0000312static bool IsCFError(QualType T, IdentifierInfo *II) {
313 const PointerType* PPT = T->getAs<PointerType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000314 if (!PPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
John McCall183700f2009-09-21 23:43:11 +0000316 const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000317 if (!TT) return false;
318
Ted Kremenekcf118d42009-02-04 23:49:09 +0000319 return TT->getDecl()->getIdentifier() == II;
Ted Kremenekcc9ac412008-10-01 23:24:09 +0000320}
321
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000322void ento::registerNSErrorChecker(CheckerManager &mgr) {
323 mgr.registerChecker<NSErrorMethodChecker>();
324 NSOrCFErrorDerefChecker *
325 checker = mgr.registerChecker<NSOrCFErrorDerefChecker>();
326 checker->ShouldCheckNSError = true;
327}
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Argyrios Kyrtzidisb3d74da2011-02-28 17:36:18 +0000329void ento::registerCFErrorChecker(CheckerManager &mgr) {
330 mgr.registerChecker<CFErrorFunctionChecker>();
331 NSOrCFErrorDerefChecker *
332 checker = mgr.registerChecker<NSOrCFErrorDerefChecker>();
333 checker->ShouldCheckCFError = true;
Ted Kremenek7360fda2008-09-18 23:09:54 +0000334}