2011-03-01 01:36:18 +08:00
|
|
|
//=- NSErrorChecker.cpp - Coding conventions for uses of NSError -*- C++ -*-==//
|
2008-09-18 14:33:41 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a CheckNSError, a flow-insenstive check
|
|
|
|
// that determines if an Objective-C class interface correctly returns
|
|
|
|
// a non-void return type.
|
|
|
|
//
|
|
|
|
// File under feature request PR 2600.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-03-01 01:36:18 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2008-09-18 14:33:41 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-09-19 05:25:13 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2008-09-18 14:33:41 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-09-18 14:33:41 +08:00
|
|
|
|
2011-03-01 09:47:48 +08:00
|
|
|
static bool IsNSError(QualType T, IdentifierInfo *II);
|
|
|
|
static bool IsCFError(QualType T, IdentifierInfo *II);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NSErrorMethodChecker
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
namespace {
|
|
|
|
class NSErrorMethodChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::ASTDecl<ObjCMethodDecl> > {
|
2011-03-01 01:36:18 +08:00
|
|
|
mutable IdentifierInfo *II;
|
2008-09-19 05:25:13 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
public:
|
|
|
|
NSErrorMethodChecker() : II(0) { }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
void checkASTDecl(const ObjCMethodDecl *D,
|
|
|
|
AnalysisManager &mgr, BugReporter &BR) const;
|
|
|
|
};
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
void NSErrorMethodChecker::checkASTDecl(const ObjCMethodDecl *D,
|
|
|
|
AnalysisManager &mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
if (!D->isThisDeclarationADefinition())
|
|
|
|
return;
|
|
|
|
if (!D->getResultType()->isVoidType())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
if (!II)
|
|
|
|
II = &D->getASTContext().Idents.get("NSError");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
bool hasNSError = false;
|
2011-10-03 14:37:04 +08:00
|
|
|
for (ObjCMethodDecl::param_const_iterator
|
2011-03-01 01:36:18 +08:00
|
|
|
I = D->param_begin(), E = D->param_end(); I != E; ++I) {
|
2011-03-01 09:47:48 +08:00
|
|
|
if (IsNSError((*I)->getType(), II)) {
|
2011-03-01 01:36:18 +08:00
|
|
|
hasNSError = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-09-18 14:33:41 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
if (hasNSError) {
|
|
|
|
const char *err = "Method accepting NSError** "
|
|
|
|
"should have a non-void return value to indicate whether or not an "
|
|
|
|
"error occurred";
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation L =
|
|
|
|
PathDiagnosticLocation::create(D, BR.getSourceManager());
|
2011-03-01 01:36:18 +08:00
|
|
|
BR.EmitBasicReport("Bad return type when passing NSError**",
|
2011-09-21 05:38:35 +08:00
|
|
|
"Coding conventions (Apple)", err, L);
|
2011-03-01 01:36:18 +08:00
|
|
|
}
|
2008-09-19 05:25:13 +08:00
|
|
|
}
|
2008-09-18 14:33:41 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFErrorFunctionChecker
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
namespace {
|
|
|
|
class CFErrorFunctionChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::ASTDecl<FunctionDecl> > {
|
2011-03-01 01:36:18 +08:00
|
|
|
mutable IdentifierInfo *II;
|
2008-09-18 14:33:41 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
public:
|
|
|
|
CFErrorFunctionChecker() : II(0) { }
|
2008-10-02 07:24:09 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
void checkASTDecl(const FunctionDecl *D,
|
|
|
|
AnalysisManager &mgr, BugReporter &BR) const;
|
|
|
|
};
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D,
|
|
|
|
AnalysisManager &mgr,
|
|
|
|
BugReporter &BR) const {
|
2011-05-07 04:44:56 +08:00
|
|
|
if (!D->doesThisDeclarationHaveABody())
|
2011-03-01 01:36:18 +08:00
|
|
|
return;
|
|
|
|
if (!D->getResultType()->isVoidType())
|
2008-09-19 05:25:13 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
if (!II)
|
|
|
|
II = &D->getASTContext().Idents.get("CFErrorRef");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
bool hasCFError = false;
|
|
|
|
for (FunctionDecl::param_const_iterator
|
|
|
|
I = D->param_begin(), E = D->param_end(); I != E; ++I) {
|
2011-03-01 09:47:48 +08:00
|
|
|
if (IsCFError((*I)->getType(), II)) {
|
2011-03-01 01:36:18 +08:00
|
|
|
hasCFError = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasCFError) {
|
|
|
|
const char *err = "Function accepting CFErrorRef* "
|
|
|
|
"should have a non-void return value to indicate whether or not an "
|
|
|
|
"error occurred";
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation L =
|
|
|
|
PathDiagnosticLocation::create(D, BR.getSourceManager());
|
2011-03-01 01:36:18 +08:00
|
|
|
BR.EmitBasicReport("Bad return type when passing CFErrorRef*",
|
2011-09-21 05:38:35 +08:00
|
|
|
"Coding conventions (Apple)", err, L);
|
2008-10-02 07:24:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NSOrCFErrorDerefChecker
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
namespace {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
class NSErrorDerefBug : public BugType {
|
|
|
|
public:
|
|
|
|
NSErrorDerefBug() : BugType("NSError** null dereference",
|
|
|
|
"Coding conventions (Apple)") {}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
class CFErrorDerefBug : public BugType {
|
|
|
|
public:
|
|
|
|
CFErrorDerefBug() : BugType("CFErrorRef* null dereference",
|
|
|
|
"Coding conventions (Apple)") {}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class NSOrCFErrorDerefChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::Location,
|
2011-03-01 01:36:18 +08:00
|
|
|
check::Event<ImplicitNullDerefEvent> > {
|
|
|
|
mutable IdentifierInfo *NSErrorII, *CFErrorII;
|
|
|
|
public:
|
|
|
|
bool ShouldCheckNSError, ShouldCheckCFError;
|
|
|
|
NSOrCFErrorDerefChecker() : NSErrorII(0), CFErrorII(0),
|
|
|
|
ShouldCheckNSError(0), ShouldCheckCFError(0) { }
|
|
|
|
|
2011-10-06 08:43:15 +08:00
|
|
|
void checkLocation(SVal loc, bool isLoad, const Stmt *S,
|
|
|
|
CheckerContext &C) const;
|
2011-03-01 01:36:18 +08:00
|
|
|
void checkEvent(ImplicitNullDerefEvent event) const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace { struct NSErrorOut {}; }
|
|
|
|
namespace { struct CFErrorOut {}; }
|
|
|
|
|
|
|
|
typedef llvm::ImmutableMap<SymbolRef, unsigned> ErrorOutFlag;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace ento {
|
|
|
|
template <>
|
2011-08-16 06:09:50 +08:00
|
|
|
struct ProgramStateTrait<NSErrorOut> : public ProgramStatePartialTrait<ErrorOutFlag> {
|
2011-03-01 01:36:18 +08:00
|
|
|
static void *GDMIndex() { static int index = 0; return &index; }
|
|
|
|
};
|
|
|
|
template <>
|
2011-08-16 06:09:50 +08:00
|
|
|
struct ProgramStateTrait<CFErrorOut> : public ProgramStatePartialTrait<ErrorOutFlag> {
|
2011-03-01 01:36:18 +08:00
|
|
|
static void *GDMIndex() { static int index = 0; return &index; }
|
|
|
|
};
|
|
|
|
}
|
2008-09-18 14:33:41 +08:00
|
|
|
}
|
2008-09-19 05:25:13 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
template <typename T>
|
2011-08-16 06:09:50 +08:00
|
|
|
static bool hasFlag(SVal val, const ProgramState *state) {
|
2011-03-01 01:36:18 +08:00
|
|
|
if (SymbolRef sym = val.getAsSymbol())
|
|
|
|
if (const unsigned *attachedFlags = state->get<T>(sym))
|
|
|
|
return *attachedFlags;
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-19 05:25:13 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
template <typename T>
|
2011-08-16 06:09:50 +08:00
|
|
|
static void setFlag(const ProgramState *state, SVal val, CheckerContext &C) {
|
2011-03-01 01:36:18 +08:00
|
|
|
// We tag the symbol that the SVal wraps.
|
|
|
|
if (SymbolRef sym = val.getAsSymbol())
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state->set<T>(sym, true));
|
2011-03-01 01:36:18 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 09:59:41 +08:00
|
|
|
static QualType parameterTypeFromSVal(SVal val, CheckerContext &C) {
|
|
|
|
const StackFrameContext *
|
2011-10-27 05:06:44 +08:00
|
|
|
SFC = C.getLocationContext()->getCurrentStackFrame();
|
2011-03-01 09:47:48 +08:00
|
|
|
if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(&val)) {
|
|
|
|
const MemRegion* R = X->getRegion();
|
|
|
|
if (const VarRegion *VR = R->getAs<VarRegion>())
|
2011-03-01 09:59:41 +08:00
|
|
|
if (const StackArgumentsSpaceRegion *
|
|
|
|
stackReg = dyn_cast<StackArgumentsSpaceRegion>(VR->getMemorySpace()))
|
|
|
|
if (stackReg->getStackFrame() == SFC)
|
|
|
|
return VR->getValueType();
|
2011-03-01 09:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
void NSOrCFErrorDerefChecker::checkLocation(SVal loc, bool isLoad,
|
2011-10-06 08:43:15 +08:00
|
|
|
const Stmt *S,
|
2011-03-01 01:36:18 +08:00
|
|
|
CheckerContext &C) const {
|
|
|
|
if (!isLoad)
|
|
|
|
return;
|
|
|
|
if (loc.isUndef() || !isa<Loc>(loc))
|
|
|
|
return;
|
2008-10-02 07:24:09 +08:00
|
|
|
|
2011-03-01 09:47:48 +08:00
|
|
|
ASTContext &Ctx = C.getASTContext();
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2011-03-01 01:36:18 +08:00
|
|
|
|
|
|
|
// If we are loading from NSError**/CFErrorRef* parameter, mark the resulting
|
|
|
|
// SVal so that we can later check it when handling the
|
|
|
|
// ImplicitNullDerefEvent event.
|
|
|
|
// FIXME: Cumbersome! Maybe add hook at construction of SVals at start of
|
|
|
|
// function ?
|
2011-03-01 09:47:48 +08:00
|
|
|
|
2011-03-01 09:59:41 +08:00
|
|
|
QualType parmT = parameterTypeFromSVal(loc, C);
|
2011-03-01 09:47:48 +08:00
|
|
|
if (parmT.isNull())
|
|
|
|
return;
|
2011-03-01 01:36:18 +08:00
|
|
|
|
|
|
|
if (!NSErrorII)
|
2011-03-01 09:47:48 +08:00
|
|
|
NSErrorII = &Ctx.Idents.get("NSError");
|
2011-03-01 01:36:18 +08:00
|
|
|
if (!CFErrorII)
|
2011-03-01 09:47:48 +08:00
|
|
|
CFErrorII = &Ctx.Idents.get("CFErrorRef");
|
2011-03-01 01:36:18 +08:00
|
|
|
|
2011-03-01 09:47:48 +08:00
|
|
|
if (ShouldCheckNSError && IsNSError(parmT, NSErrorII)) {
|
2011-03-01 01:36:18 +08:00
|
|
|
setFlag<NSErrorOut>(state, state->getSVal(cast<Loc>(loc)), C);
|
|
|
|
return;
|
|
|
|
}
|
2008-10-02 07:24:09 +08:00
|
|
|
|
2011-03-01 09:47:48 +08:00
|
|
|
if (ShouldCheckCFError && IsCFError(parmT, CFErrorII)) {
|
2011-03-01 01:36:18 +08:00
|
|
|
setFlag<CFErrorOut>(state, state->getSVal(cast<Loc>(loc)), C);
|
|
|
|
return;
|
2008-10-02 07:24:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
void NSOrCFErrorDerefChecker::checkEvent(ImplicitNullDerefEvent event) const {
|
|
|
|
if (event.IsLoad)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
SVal loc = event.Location;
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = event.SinkNode->getState();
|
2011-03-01 01:36:18 +08:00
|
|
|
BugReporter &BR = *event.BR;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
bool isNSError = hasFlag<NSErrorOut>(loc, state);
|
|
|
|
bool isCFError = false;
|
|
|
|
if (!isNSError)
|
|
|
|
isCFError = hasFlag<CFErrorOut>(loc, state);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
if (!(isNSError || isCFError))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
// Storing to possible null NSError/CFErrorRef out parameter.
|
|
|
|
|
|
|
|
// Emit an error.
|
|
|
|
std::string err;
|
|
|
|
llvm::raw_string_ostream os(err);
|
|
|
|
os << "Potential null dereference. According to coding standards ";
|
|
|
|
|
|
|
|
if (isNSError)
|
|
|
|
os << "in 'Creating and Returning NSError Objects' the parameter '";
|
|
|
|
else
|
|
|
|
os << "documented in CoreFoundation/CFError.h the parameter '";
|
2008-09-19 05:25:13 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
os << "' may be null.";
|
2008-10-02 07:24:09 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
BugType *bug = 0;
|
|
|
|
if (isNSError)
|
|
|
|
bug = new NSErrorDerefBug();
|
|
|
|
else
|
|
|
|
bug = new CFErrorDerefBug();
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report = new BugReport(*bug, os.str(),
|
2011-03-01 01:36:18 +08:00
|
|
|
event.SinkNode);
|
|
|
|
BR.EmitReport(report);
|
|
|
|
}
|
|
|
|
|
2011-03-01 09:47:48 +08:00
|
|
|
static bool IsNSError(QualType T, IdentifierInfo *II) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 09:47:48 +08:00
|
|
|
const PointerType* PPT = T->getAs<PointerType>();
|
2009-07-11 07:34:53 +08:00
|
|
|
if (!PPT)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-11 07:34:53 +08:00
|
|
|
const ObjCObjectPointerType* PT =
|
2009-09-22 07:43:11 +08:00
|
|
|
PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
|
2009-07-11 07:34:53 +08:00
|
|
|
|
|
|
|
if (!PT)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-11 07:34:53 +08:00
|
|
|
const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-11 07:34:53 +08:00
|
|
|
// FIXME: Can ID ever be NULL?
|
|
|
|
if (ID)
|
|
|
|
return II == ID->getIdentifier();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-11 07:34:53 +08:00
|
|
|
return false;
|
2008-09-19 05:25:13 +08:00
|
|
|
}
|
2008-09-19 07:09:54 +08:00
|
|
|
|
2011-03-01 09:47:48 +08:00
|
|
|
static bool IsCFError(QualType T, IdentifierInfo *II) {
|
|
|
|
const PointerType* PPT = T->getAs<PointerType>();
|
2008-10-02 07:24:09 +08:00
|
|
|
if (!PPT) return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-22 07:43:11 +08:00
|
|
|
const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
|
2008-10-02 07:24:09 +08:00
|
|
|
if (!TT) return false;
|
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
return TT->getDecl()->getIdentifier() == II;
|
2008-10-02 07:24:09 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
void ento::registerNSErrorChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<NSErrorMethodChecker>();
|
|
|
|
NSOrCFErrorDerefChecker *
|
|
|
|
checker = mgr.registerChecker<NSOrCFErrorDerefChecker>();
|
|
|
|
checker->ShouldCheckNSError = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
void ento::registerCFErrorChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<CFErrorFunctionChecker>();
|
|
|
|
NSOrCFErrorDerefChecker *
|
|
|
|
checker = mgr.registerChecker<NSOrCFErrorDerefChecker>();
|
|
|
|
checker->ShouldCheckCFError = true;
|
2008-09-19 07:09:54 +08:00
|
|
|
}
|