clang API Documentation

NSErrorChecker.cpp
Go to the documentation of this file.
00001 //=- NSErrorChecker.cpp - Coding conventions for uses of NSError -*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines a CheckNSError, a flow-insenstive check
00011 //  that determines if an Objective-C class interface correctly returns
00012 //  a non-void return type.
00013 //
00014 //  File under feature request PR 2600.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #include "ClangSACheckers.h"
00019 #include "clang/AST/Decl.h"
00020 #include "clang/AST/DeclObjC.h"
00021 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00022 #include "clang/StaticAnalyzer/Core/Checker.h"
00023 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00024 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00025 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
00026 #include "llvm/ADT/SmallString.h"
00027 #include "llvm/Support/raw_ostream.h"
00028 
00029 using namespace clang;
00030 using namespace ento;
00031 
00032 static bool IsNSError(QualType T, IdentifierInfo *II);
00033 static bool IsCFError(QualType T, IdentifierInfo *II);
00034 
00035 //===----------------------------------------------------------------------===//
00036 // NSErrorMethodChecker
00037 //===----------------------------------------------------------------------===//
00038 
00039 namespace {
00040 class NSErrorMethodChecker
00041     : public Checker< check::ASTDecl<ObjCMethodDecl> > {
00042   mutable IdentifierInfo *II;
00043 
00044 public:
00045   NSErrorMethodChecker() : II(nullptr) {}
00046 
00047   void checkASTDecl(const ObjCMethodDecl *D,
00048                     AnalysisManager &mgr, BugReporter &BR) const;
00049 };
00050 }
00051 
00052 void NSErrorMethodChecker::checkASTDecl(const ObjCMethodDecl *D,
00053                                         AnalysisManager &mgr,
00054                                         BugReporter &BR) const {
00055   if (!D->isThisDeclarationADefinition())
00056     return;
00057   if (!D->getReturnType()->isVoidType())
00058     return;
00059 
00060   if (!II)
00061     II = &D->getASTContext().Idents.get("NSError"); 
00062 
00063   bool hasNSError = false;
00064   for (const auto *I : D->params())  {
00065     if (IsNSError(I->getType(), II)) {
00066       hasNSError = true;
00067       break;
00068     }
00069   }
00070 
00071   if (hasNSError) {
00072     const char *err = "Method accepting NSError** "
00073         "should have a non-void return value to indicate whether or not an "
00074         "error occurred";
00075     PathDiagnosticLocation L =
00076       PathDiagnosticLocation::create(D, BR.getSourceManager());
00077     BR.EmitBasicReport(D, this, "Bad return type when passing NSError**",
00078                        "Coding conventions (Apple)", err, L);
00079   }
00080 }
00081 
00082 //===----------------------------------------------------------------------===//
00083 // CFErrorFunctionChecker
00084 //===----------------------------------------------------------------------===//
00085 
00086 namespace {
00087 class CFErrorFunctionChecker
00088     : public Checker< check::ASTDecl<FunctionDecl> > {
00089   mutable IdentifierInfo *II;
00090 
00091 public:
00092   CFErrorFunctionChecker() : II(nullptr) {}
00093 
00094   void checkASTDecl(const FunctionDecl *D,
00095                     AnalysisManager &mgr, BugReporter &BR) const;
00096 };
00097 }
00098 
00099 void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D,
00100                                         AnalysisManager &mgr,
00101                                         BugReporter &BR) const {
00102   if (!D->doesThisDeclarationHaveABody())
00103     return;
00104   if (!D->getReturnType()->isVoidType())
00105     return;
00106 
00107   if (!II)
00108     II = &D->getASTContext().Idents.get("CFErrorRef"); 
00109 
00110   bool hasCFError = false;
00111   for (auto I : D->params())  {
00112     if (IsCFError(I->getType(), II)) {
00113       hasCFError = true;
00114       break;
00115     }
00116   }
00117 
00118   if (hasCFError) {
00119     const char *err = "Function accepting CFErrorRef* "
00120         "should have a non-void return value to indicate whether or not an "
00121         "error occurred";
00122     PathDiagnosticLocation L =
00123       PathDiagnosticLocation::create(D, BR.getSourceManager());
00124     BR.EmitBasicReport(D, this, "Bad return type when passing CFErrorRef*",
00125                        "Coding conventions (Apple)", err, L);
00126   }
00127 }
00128 
00129 //===----------------------------------------------------------------------===//
00130 // NSOrCFErrorDerefChecker
00131 //===----------------------------------------------------------------------===//
00132 
00133 namespace {
00134 
00135 class NSErrorDerefBug : public BugType {
00136 public:
00137   NSErrorDerefBug(const CheckerBase *Checker)
00138       : BugType(Checker, "NSError** null dereference",
00139                 "Coding conventions (Apple)") {}
00140 };
00141 
00142 class CFErrorDerefBug : public BugType {
00143 public:
00144   CFErrorDerefBug(const CheckerBase *Checker)
00145       : BugType(Checker, "CFErrorRef* null dereference",
00146                 "Coding conventions (Apple)") {}
00147 };
00148 
00149 }
00150 
00151 namespace {
00152 class NSOrCFErrorDerefChecker
00153     : public Checker< check::Location,
00154                         check::Event<ImplicitNullDerefEvent> > {
00155   mutable IdentifierInfo *NSErrorII, *CFErrorII;
00156   mutable std::unique_ptr<NSErrorDerefBug> NSBT;
00157   mutable std::unique_ptr<CFErrorDerefBug> CFBT;
00158 public:
00159   bool ShouldCheckNSError, ShouldCheckCFError;
00160   NSOrCFErrorDerefChecker() : NSErrorII(nullptr), CFErrorII(nullptr),
00161                               ShouldCheckNSError(0), ShouldCheckCFError(0) { }
00162 
00163   void checkLocation(SVal loc, bool isLoad, const Stmt *S,
00164                      CheckerContext &C) const;
00165   void checkEvent(ImplicitNullDerefEvent event) const;
00166 };
00167 }
00168 
00169 typedef llvm::ImmutableMap<SymbolRef, unsigned> ErrorOutFlag;
00170 REGISTER_TRAIT_WITH_PROGRAMSTATE(NSErrorOut, ErrorOutFlag)
00171 REGISTER_TRAIT_WITH_PROGRAMSTATE(CFErrorOut, ErrorOutFlag)
00172 
00173 template <typename T>
00174 static bool hasFlag(SVal val, ProgramStateRef state) {
00175   if (SymbolRef sym = val.getAsSymbol())
00176     if (const unsigned *attachedFlags = state->get<T>(sym))
00177       return *attachedFlags;
00178   return false;
00179 }
00180 
00181 template <typename T>
00182 static void setFlag(ProgramStateRef state, SVal val, CheckerContext &C) {
00183   // We tag the symbol that the SVal wraps.
00184   if (SymbolRef sym = val.getAsSymbol())
00185     C.addTransition(state->set<T>(sym, true));
00186 }
00187 
00188 static QualType parameterTypeFromSVal(SVal val, CheckerContext &C) {
00189   const StackFrameContext *
00190     SFC = C.getLocationContext()->getCurrentStackFrame();
00191   if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>()) {
00192     const MemRegion* R = X->getRegion();
00193     if (const VarRegion *VR = R->getAs<VarRegion>())
00194       if (const StackArgumentsSpaceRegion *
00195           stackReg = dyn_cast<StackArgumentsSpaceRegion>(VR->getMemorySpace()))
00196         if (stackReg->getStackFrame() == SFC)
00197           return VR->getValueType();
00198   }
00199 
00200   return QualType();
00201 }
00202 
00203 void NSOrCFErrorDerefChecker::checkLocation(SVal loc, bool isLoad,
00204                                             const Stmt *S,
00205                                             CheckerContext &C) const {
00206   if (!isLoad)
00207     return;
00208   if (loc.isUndef() || !loc.getAs<Loc>())
00209     return;
00210 
00211   ASTContext &Ctx = C.getASTContext();
00212   ProgramStateRef state = C.getState();
00213 
00214   // If we are loading from NSError**/CFErrorRef* parameter, mark the resulting
00215   // SVal so that we can later check it when handling the
00216   // ImplicitNullDerefEvent event.
00217   // FIXME: Cumbersome! Maybe add hook at construction of SVals at start of
00218   // function ?
00219 
00220   QualType parmT = parameterTypeFromSVal(loc, C);
00221   if (parmT.isNull())
00222     return;
00223 
00224   if (!NSErrorII)
00225     NSErrorII = &Ctx.Idents.get("NSError");
00226   if (!CFErrorII)
00227     CFErrorII = &Ctx.Idents.get("CFErrorRef");
00228 
00229   if (ShouldCheckNSError && IsNSError(parmT, NSErrorII)) {
00230     setFlag<NSErrorOut>(state, state->getSVal(loc.castAs<Loc>()), C);
00231     return;
00232   }
00233 
00234   if (ShouldCheckCFError && IsCFError(parmT, CFErrorII)) {
00235     setFlag<CFErrorOut>(state, state->getSVal(loc.castAs<Loc>()), C);
00236     return;
00237   }
00238 }
00239 
00240 void NSOrCFErrorDerefChecker::checkEvent(ImplicitNullDerefEvent event) const {
00241   if (event.IsLoad)
00242     return;
00243 
00244   SVal loc = event.Location;
00245   ProgramStateRef state = event.SinkNode->getState();
00246   BugReporter &BR = *event.BR;
00247 
00248   bool isNSError = hasFlag<NSErrorOut>(loc, state);
00249   bool isCFError = false;
00250   if (!isNSError)
00251     isCFError = hasFlag<CFErrorOut>(loc, state);
00252 
00253   if (!(isNSError || isCFError))
00254     return;
00255 
00256   // Storing to possible null NSError/CFErrorRef out parameter.
00257   SmallString<128> Buf;
00258   llvm::raw_svector_ostream os(Buf);
00259 
00260   os << "Potential null dereference.  According to coding standards ";
00261   os << (isNSError
00262          ? "in 'Creating and Returning NSError Objects' the parameter"
00263          : "documented in CoreFoundation/CFError.h the parameter");
00264 
00265   os  << " may be null";
00266 
00267   BugType *bug = nullptr;
00268   if (isNSError) {
00269     if (!NSBT)
00270       NSBT.reset(new NSErrorDerefBug(this));
00271     bug = NSBT.get();
00272   }
00273   else {
00274     if (!CFBT)
00275       CFBT.reset(new CFErrorDerefBug(this));
00276     bug = CFBT.get();
00277   }
00278   BugReport *report = new BugReport(*bug, os.str(), event.SinkNode);
00279   BR.emitReport(report);
00280 }
00281 
00282 static bool IsNSError(QualType T, IdentifierInfo *II) {
00283 
00284   const PointerType* PPT = T->getAs<PointerType>();
00285   if (!PPT)
00286     return false;
00287 
00288   const ObjCObjectPointerType* PT =
00289     PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
00290 
00291   if (!PT)
00292     return false;
00293 
00294   const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
00295 
00296   // FIXME: Can ID ever be NULL?
00297   if (ID)
00298     return II == ID->getIdentifier();
00299 
00300   return false;
00301 }
00302 
00303 static bool IsCFError(QualType T, IdentifierInfo *II) {
00304   const PointerType* PPT = T->getAs<PointerType>();
00305   if (!PPT) return false;
00306 
00307   const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
00308   if (!TT) return false;
00309 
00310   return TT->getDecl()->getIdentifier() == II;
00311 }
00312 
00313 void ento::registerNSErrorChecker(CheckerManager &mgr) {
00314   mgr.registerChecker<NSErrorMethodChecker>();
00315   NSOrCFErrorDerefChecker *checker =
00316       mgr.registerChecker<NSOrCFErrorDerefChecker>();
00317   checker->ShouldCheckNSError = true;
00318 }
00319 
00320 void ento::registerCFErrorChecker(CheckerManager &mgr) {
00321   mgr.registerChecker<CFErrorFunctionChecker>();
00322   NSOrCFErrorDerefChecker *checker =
00323       mgr.registerChecker<NSOrCFErrorDerefChecker>();
00324   checker->ShouldCheckCFError = true;
00325 }