1 //=- NSErrorChecker.cpp - Coding conventions for uses of NSError -*- 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 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 
18 #include "ClangSACheckers.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
22 #include "clang/StaticAnalyzer/Core/Checker.h"
23 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Support/raw_ostream.h"
28 
29 using namespace clang;
30 using namespace ento;
31 
32 static bool IsNSError(QualType T, IdentifierInfo *II);
33 static bool IsCFError(QualType T, IdentifierInfo *II);
34 
35 //===----------------------------------------------------------------------===//
36 // NSErrorMethodChecker
37 //===----------------------------------------------------------------------===//
38 
39 namespace {
40 class NSErrorMethodChecker
41     : public Checker< check::ASTDecl<ObjCMethodDecl> > {
42   mutable IdentifierInfo *II;
43 
44 public:
45   NSErrorMethodChecker() : II(0) { }
46 
47   void checkASTDecl(const ObjCMethodDecl *D,
48                     AnalysisManager &mgr, BugReporter &BR) const;
49 };
50 }
51 
52 void NSErrorMethodChecker::checkASTDecl(const ObjCMethodDecl *D,
53                                         AnalysisManager &mgr,
54                                         BugReporter &BR) const {
55   if (!D->isThisDeclarationADefinition())
56     return;
57   if (!D->getReturnType()->isVoidType())
58     return;
59 
60   if (!II)
61     II = &D->getASTContext().Idents.get("NSError");
62 
63   bool hasNSError = false;
64   for (const auto *I : D->params())  {
65     if (IsNSError(I->getType(), 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     PathDiagnosticLocation L =
76       PathDiagnosticLocation::create(D, BR.getSourceManager());
77     BR.EmitBasicReport(D, this, "Bad return type when passing NSError**",
78                        "Coding conventions (Apple)", err, L);
79   }
80 }
81 
82 //===----------------------------------------------------------------------===//
83 // CFErrorFunctionChecker
84 //===----------------------------------------------------------------------===//
85 
86 namespace {
87 class CFErrorFunctionChecker
88     : public Checker< check::ASTDecl<FunctionDecl> > {
89   mutable IdentifierInfo *II;
90 
91 public:
92   CFErrorFunctionChecker() : II(0) { }
93 
94   void checkASTDecl(const FunctionDecl *D,
95                     AnalysisManager &mgr, BugReporter &BR) const;
96 };
97 }
98 
99 void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D,
100                                         AnalysisManager &mgr,
101                                         BugReporter &BR) const {
102   if (!D->doesThisDeclarationHaveABody())
103     return;
104   if (!D->getReturnType()->isVoidType())
105     return;
106 
107   if (!II)
108     II = &D->getASTContext().Idents.get("CFErrorRef");
109 
110   bool hasCFError = false;
111   for (auto I : D->params())  {
112     if (IsCFError(I->getType(), II)) {
113       hasCFError = true;
114       break;
115     }
116   }
117 
118   if (hasCFError) {
119     const char *err = "Function accepting CFErrorRef* "
120         "should have a non-void return value to indicate whether or not an "
121         "error occurred";
122     PathDiagnosticLocation L =
123       PathDiagnosticLocation::create(D, BR.getSourceManager());
124     BR.EmitBasicReport(D, this, "Bad return type when passing CFErrorRef*",
125                        "Coding conventions (Apple)", err, L);
126   }
127 }
128 
129 //===----------------------------------------------------------------------===//
130 // NSOrCFErrorDerefChecker
131 //===----------------------------------------------------------------------===//
132 
133 namespace {
134 
135 class NSErrorDerefBug : public BugType {
136 public:
137   NSErrorDerefBug(const CheckerBase *Checker)
138       : BugType(Checker, "NSError** null dereference",
139                 "Coding conventions (Apple)") {}
140 };
141 
142 class CFErrorDerefBug : public BugType {
143 public:
144   CFErrorDerefBug(const CheckerBase *Checker)
145       : BugType(Checker, "CFErrorRef* null dereference",
146                 "Coding conventions (Apple)") {}
147 };
148 
149 }
150 
151 namespace {
152 class NSOrCFErrorDerefChecker
153     : public Checker< check::Location,
154                         check::Event<ImplicitNullDerefEvent> > {
155   mutable IdentifierInfo *NSErrorII, *CFErrorII;
156 public:
157   bool ShouldCheckNSError, ShouldCheckCFError;
158   NSOrCFErrorDerefChecker() : NSErrorII(0), CFErrorII(0),
159                               ShouldCheckNSError(0), ShouldCheckCFError(0) { }
160 
161   void checkLocation(SVal loc, bool isLoad, const Stmt *S,
162                      CheckerContext &C) const;
163   void checkEvent(ImplicitNullDerefEvent event) const;
164 };
165 }
166 
167 typedef llvm::ImmutableMap<SymbolRef, unsigned> ErrorOutFlag;
168 REGISTER_TRAIT_WITH_PROGRAMSTATE(NSErrorOut, ErrorOutFlag)
169 REGISTER_TRAIT_WITH_PROGRAMSTATE(CFErrorOut, ErrorOutFlag)
170 
171 template <typename T>
172 static bool hasFlag(SVal val, ProgramStateRef state) {
173   if (SymbolRef sym = val.getAsSymbol())
174     if (const unsigned *attachedFlags = state->get<T>(sym))
175       return *attachedFlags;
176   return false;
177 }
178 
179 template <typename T>
180 static void setFlag(ProgramStateRef state, SVal val, CheckerContext &C) {
181   // We tag the symbol that the SVal wraps.
182   if (SymbolRef sym = val.getAsSymbol())
183     C.addTransition(state->set<T>(sym, true));
184 }
185 
186 static QualType parameterTypeFromSVal(SVal val, CheckerContext &C) {
187   const StackFrameContext *
188     SFC = C.getLocationContext()->getCurrentStackFrame();
189   if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>()) {
190     const MemRegion* R = X->getRegion();
191     if (const VarRegion *VR = R->getAs<VarRegion>())
192       if (const StackArgumentsSpaceRegion *
193           stackReg = dyn_cast<StackArgumentsSpaceRegion>(VR->getMemorySpace()))
194         if (stackReg->getStackFrame() == SFC)
195           return VR->getValueType();
196   }
197 
198   return QualType();
199 }
200 
201 void NSOrCFErrorDerefChecker::checkLocation(SVal loc, bool isLoad,
202                                             const Stmt *S,
203                                             CheckerContext &C) const {
204   if (!isLoad)
205     return;
206   if (loc.isUndef() || !loc.getAs<Loc>())
207     return;
208 
209   ASTContext &Ctx = C.getASTContext();
210   ProgramStateRef state = C.getState();
211 
212   // If we are loading from NSError**/CFErrorRef* parameter, mark the resulting
213   // SVal so that we can later check it when handling the
214   // ImplicitNullDerefEvent event.
215   // FIXME: Cumbersome! Maybe add hook at construction of SVals at start of
216   // function ?
217 
218   QualType parmT = parameterTypeFromSVal(loc, C);
219   if (parmT.isNull())
220     return;
221 
222   if (!NSErrorII)
223     NSErrorII = &Ctx.Idents.get("NSError");
224   if (!CFErrorII)
225     CFErrorII = &Ctx.Idents.get("CFErrorRef");
226 
227   if (ShouldCheckNSError && IsNSError(parmT, NSErrorII)) {
228     setFlag<NSErrorOut>(state, state->getSVal(loc.castAs<Loc>()), C);
229     return;
230   }
231 
232   if (ShouldCheckCFError && IsCFError(parmT, CFErrorII)) {
233     setFlag<CFErrorOut>(state, state->getSVal(loc.castAs<Loc>()), C);
234     return;
235   }
236 }
237 
238 void NSOrCFErrorDerefChecker::checkEvent(ImplicitNullDerefEvent event) const {
239   if (event.IsLoad)
240     return;
241 
242   SVal loc = event.Location;
243   ProgramStateRef state = event.SinkNode->getState();
244   BugReporter &BR = *event.BR;
245 
246   bool isNSError = hasFlag<NSErrorOut>(loc, state);
247   bool isCFError = false;
248   if (!isNSError)
249     isCFError = hasFlag<CFErrorOut>(loc, state);
250 
251   if (!(isNSError || isCFError))
252     return;
253 
254   // Storing to possible null NSError/CFErrorRef out parameter.
255   SmallString<128> Buf;
256   llvm::raw_svector_ostream os(Buf);
257 
258   os << "Potential null dereference.  According to coding standards ";
259   os << (isNSError
260          ? "in 'Creating and Returning NSError Objects' the parameter"
261          : "documented in CoreFoundation/CFError.h the parameter");
262 
263   os  << " may be null";
264 
265   BugType *bug = 0;
266   if (isNSError)
267     bug = new NSErrorDerefBug(this);
268   else
269     bug = new CFErrorDerefBug(this);
270   BugReport *report = new BugReport(*bug, os.str(), event.SinkNode);
271   BR.emitReport(report);
272 }
273 
274 static bool IsNSError(QualType T, IdentifierInfo *II) {
275 
276   const PointerType* PPT = T->getAs<PointerType>();
277   if (!PPT)
278     return false;
279 
280   const ObjCObjectPointerType* PT =
281     PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
282 
283   if (!PT)
284     return false;
285 
286   const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
287 
288   // FIXME: Can ID ever be NULL?
289   if (ID)
290     return II == ID->getIdentifier();
291 
292   return false;
293 }
294 
295 static bool IsCFError(QualType T, IdentifierInfo *II) {
296   const PointerType* PPT = T->getAs<PointerType>();
297   if (!PPT) return false;
298 
299   const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
300   if (!TT) return false;
301 
302   return TT->getDecl()->getIdentifier() == II;
303 }
304 
305 void ento::registerNSErrorChecker(CheckerManager &mgr) {
306   mgr.registerChecker<NSErrorMethodChecker>();
307   NSOrCFErrorDerefChecker *checker =
308       mgr.registerChecker<NSOrCFErrorDerefChecker>();
309   checker->ShouldCheckNSError = true;
310 }
311 
312 void ento::registerCFErrorChecker(CheckerManager &mgr) {
313   mgr.registerChecker<CFErrorFunctionChecker>();
314   NSOrCFErrorDerefChecker *checker =
315       mgr.registerChecker<NSOrCFErrorDerefChecker>();
316   checker->ShouldCheckCFError = true;
317 }
318