2011-01-12 03:45:25 +08:00
|
|
|
//== ObjCSelfInitChecker.cpp - Checker for 'self' initialization -*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines ObjCSelfInitChecker, a builtin check that checks for uses of
|
|
|
|
// 'self' before proper initialization.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// This checks initialization methods to verify that they assign 'self' to the
|
|
|
|
// result of an initialization call (e.g. [super init], or [self initWith..])
|
|
|
|
// before using 'self' or any instance variable.
|
|
|
|
//
|
2011-04-15 13:22:18 +08:00
|
|
|
// To perform the required checking, values are tagged with flags that indicate
|
2011-01-12 03:45:25 +08:00
|
|
|
// 1) if the object is the one pointed to by 'self', and 2) if the object
|
|
|
|
// is the result of an initializer (e.g. [super init]).
|
|
|
|
//
|
|
|
|
// Uses of an object that is true for 1) but not 2) trigger a diagnostic.
|
|
|
|
// The uses that are currently checked are:
|
|
|
|
// - Using instance variables.
|
|
|
|
// - Returning the object.
|
|
|
|
//
|
|
|
|
// Note that we don't check for an invalid 'self' that is the receiver of an
|
|
|
|
// obj-c message expression to cut down false positives where logging functions
|
|
|
|
// get information from self (like its class) or doing "invalidation" on self
|
|
|
|
// when the initialization fails.
|
|
|
|
//
|
|
|
|
// Because the object that 'self' points to gets invalidated when a call
|
|
|
|
// receives a reference to 'self', the checker keeps track and passes the flags
|
|
|
|
// for 1) and 2) to the new object that 'self' points to after the call.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-18 05:39:17 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2012-07-27 05:39:41 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2011-02-23 01:30:38 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
2012-12-02 01:54:07 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-01-12 03:45:25 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND);
|
|
|
|
static bool isInitializationMethod(const ObjCMethodDecl *MD);
|
2012-07-03 03:28:04 +08:00
|
|
|
static bool isInitMessage(const ObjCMethodCall &Msg);
|
2011-01-12 03:45:25 +08:00
|
|
|
static bool isSelfVar(SVal location, CheckerContext &C);
|
|
|
|
|
|
|
|
namespace {
|
2012-07-03 03:28:21 +08:00
|
|
|
class ObjCSelfInitChecker : public Checker< check::PostObjCMessage,
|
2011-02-23 01:30:38 +08:00
|
|
|
check::PostStmt<ObjCIvarRefExpr>,
|
|
|
|
check::PreStmt<ReturnStmt>,
|
2012-07-03 03:28:21 +08:00
|
|
|
check::PreCall,
|
|
|
|
check::PostCall,
|
2012-05-09 05:19:21 +08:00
|
|
|
check::Location,
|
|
|
|
check::Bind > {
|
2014-05-07 11:30:04 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT;
|
2014-05-07 01:33:42 +08:00
|
|
|
|
|
|
|
void checkForInvalidSelf(const Expr *E, CheckerContext &C,
|
|
|
|
const char *errorStr) const;
|
|
|
|
|
2011-01-12 03:45:25 +08:00
|
|
|
public:
|
2014-05-07 11:30:04 +08:00
|
|
|
ObjCSelfInitChecker() {}
|
2012-07-03 03:28:04 +08:00
|
|
|
void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const;
|
2011-02-23 01:30:38 +08:00
|
|
|
void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const;
|
|
|
|
void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
|
2011-10-06 08:43:15 +08:00
|
|
|
void checkLocation(SVal location, bool isLoad, const Stmt *S,
|
|
|
|
CheckerContext &C) const;
|
2012-05-09 05:19:21 +08:00
|
|
|
void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
|
2012-03-06 02:58:25 +08:00
|
|
|
|
2012-07-03 03:28:21 +08:00
|
|
|
void checkPreCall(const CallEvent &CE, CheckerContext &C) const;
|
|
|
|
void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
|
2012-03-06 02:58:25 +08:00
|
|
|
|
2012-09-08 09:47:11 +08:00
|
|
|
void printState(raw_ostream &Out, ProgramStateRef State,
|
2014-03-15 12:29:04 +08:00
|
|
|
const char *NL, const char *Sep) const override;
|
2011-01-12 03:45:25 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
namespace {
|
|
|
|
enum SelfFlagEnum {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// No flag set.
|
2011-02-23 01:30:38 +08:00
|
|
|
SelfFlag_None = 0x0,
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Value came from 'self'.
|
2011-02-23 01:30:38 +08:00
|
|
|
SelfFlag_Self = 0x1,
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Value came from the result of an initializer (e.g. [super init]).
|
2011-02-23 01:30:38 +08:00
|
|
|
SelfFlag_InitRes = 0x2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-11-02 09:54:42 +08:00
|
|
|
REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned)
|
|
|
|
REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool)
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A call receiving a reference to 'self' invalidates the object that
|
2012-11-02 09:54:42 +08:00
|
|
|
/// 'self' contains. This keeps the "self flags" assigned to the 'self'
|
|
|
|
/// object before the call so we can assign them to the new object that 'self'
|
|
|
|
/// points to after the call.
|
|
|
|
REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, unsigned)
|
2011-01-12 03:45:25 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) {
|
2011-01-12 03:45:25 +08:00
|
|
|
if (SymbolRef sym = val.getAsSymbol())
|
|
|
|
if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
|
|
|
|
return (SelfFlagEnum)*attachedFlags;
|
|
|
|
return SelfFlag_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
|
|
|
|
return getSelfFlags(val, C.getState());
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
static void addSelfFlag(ProgramStateRef state, SVal val,
|
2011-02-12 11:03:54 +08:00
|
|
|
SelfFlagEnum flag, CheckerContext &C) {
|
2011-02-05 13:54:53 +08:00
|
|
|
// We tag the symbol that the SVal wraps.
|
2012-12-13 08:42:19 +08:00
|
|
|
if (SymbolRef sym = val.getAsSymbol()) {
|
2012-09-08 09:47:28 +08:00
|
|
|
state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag);
|
2012-12-13 08:42:19 +08:00
|
|
|
C.addTransition(state);
|
|
|
|
}
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
|
|
|
|
return getSelfFlags(val, C) & flag;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns true of the value of the expression is the object that 'self'
|
2011-01-12 03:45:25 +08:00
|
|
|
/// points to and is an object that did not come from the result of calling
|
|
|
|
/// an initializer.
|
|
|
|
static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal exprVal = C.getSVal(E);
|
2011-01-12 03:45:25 +08:00
|
|
|
if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
|
|
|
|
return false; // value did not come from 'self'.
|
|
|
|
if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
|
|
|
|
return false; // 'self' is properly initialized.
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-07 01:33:42 +08:00
|
|
|
void ObjCSelfInitChecker::checkForInvalidSelf(const Expr *E, CheckerContext &C,
|
|
|
|
const char *errorStr) const {
|
2011-01-12 03:45:25 +08:00
|
|
|
if (!E)
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-02-12 11:03:54 +08:00
|
|
|
if (!C.getState()->get<CalledInit>())
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-01-12 03:45:25 +08:00
|
|
|
if (!isInvalidSelf(E, C))
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-01-12 03:45:25 +08:00
|
|
|
// Generate an error node.
|
2015-09-17 06:03:05 +08:00
|
|
|
ExplodedNode *N = C.generateErrorNode();
|
2011-01-12 03:45:25 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
2014-05-07 11:30:04 +08:00
|
|
|
if (!BT)
|
|
|
|
BT.reset(new BugType(this, "Missing \"self = [(super or self) init...]\"",
|
|
|
|
categories::CoreFoundationObjectiveC));
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(llvm::make_unique<BugReport>(*BT, errorStr, N));
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
|
2011-02-23 01:30:38 +08:00
|
|
|
CheckerContext &C) const {
|
2011-01-12 03:45:25 +08:00
|
|
|
// When encountering a message that does initialization (init rule),
|
|
|
|
// tag the return value so that we know later on that if self has this value
|
|
|
|
// then it is properly initialized.
|
|
|
|
|
|
|
|
// FIXME: A callback should disable checkers at the start of functions.
|
|
|
|
if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
|
2012-02-04 10:31:37 +08:00
|
|
|
C.getCurrentAnalysisDeclContext()->getDecl())))
|
2011-01-12 03:45:25 +08:00
|
|
|
return;
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
if (isInitMessage(Msg)) {
|
2011-01-12 03:45:25 +08:00
|
|
|
// Tag the return value as the result of an initializer.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-02-12 11:03:54 +08:00
|
|
|
// FIXME this really should be context sensitive, where we record
|
|
|
|
// the current stack frame (for IPA). Also, we need to clean this
|
|
|
|
// value out when we return from this method.
|
|
|
|
state = state->set<CalledInit>(true);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal V = C.getSVal(Msg.getOriginExpr());
|
2011-02-12 11:03:54 +08:00
|
|
|
addSelfFlag(state, V, SelfFlag_InitRes, C);
|
2011-01-12 03:45:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't check for an invalid 'self' in an obj-c message expression to cut
|
|
|
|
// down false positives where logging functions get information from self
|
|
|
|
// (like its class) or doing "invalidation" on self when the initialization
|
|
|
|
// fails.
|
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E,
|
|
|
|
CheckerContext &C) const {
|
2011-01-12 03:45:25 +08:00
|
|
|
// FIXME: A callback should disable checkers at the start of functions.
|
|
|
|
if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
|
2012-02-04 10:31:37 +08:00
|
|
|
C.getCurrentAnalysisDeclContext()->getDecl())))
|
2011-01-12 03:45:25 +08:00
|
|
|
return;
|
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
checkForInvalidSelf(
|
|
|
|
E->getBase(), C,
|
|
|
|
"Instance variable used while 'self' is not set to the result of "
|
2014-05-07 01:33:42 +08:00
|
|
|
"'[(super or self) init...]'");
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S,
|
|
|
|
CheckerContext &C) const {
|
2011-01-12 03:45:25 +08:00
|
|
|
// FIXME: A callback should disable checkers at the start of functions.
|
|
|
|
if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
|
2012-02-04 10:31:37 +08:00
|
|
|
C.getCurrentAnalysisDeclContext()->getDecl())))
|
2011-01-12 03:45:25 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
checkForInvalidSelf(S->getRetValue(), C,
|
2014-02-12 05:49:21 +08:00
|
|
|
"Returning 'self' while it is not set to the result of "
|
2014-05-07 01:33:42 +08:00
|
|
|
"'[(super or self) init...]'");
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:21 +08:00
|
|
|
// When a call receives a reference to 'self', [Pre/Post]Call pass
|
|
|
|
// the SelfFlags from the object 'self' points to before the call to the new
|
2011-02-05 13:54:53 +08:00
|
|
|
// object after the call. This is to avoid invalidation of 'self' by logging
|
|
|
|
// functions.
|
|
|
|
// Another common pattern in classes with multiple initializers is to put the
|
|
|
|
// subclass's common initialization bits into a static function that receives
|
|
|
|
// the value of 'self', e.g:
|
|
|
|
// @code
|
|
|
|
// if (!(self = [super init]))
|
|
|
|
// return nil;
|
|
|
|
// if (!(self = _commonInit(self)))
|
|
|
|
// return nil;
|
|
|
|
// @endcode
|
|
|
|
// Until we can use inter-procedural analysis, in such a call, transfer the
|
|
|
|
// SelfFlags to the result of the call.
|
2011-01-12 03:45:25 +08:00
|
|
|
|
2012-07-03 03:28:21 +08:00
|
|
|
void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE,
|
2011-02-23 01:30:38 +08:00
|
|
|
CheckerContext &C) const {
|
2012-07-03 03:28:21 +08:00
|
|
|
// FIXME: A callback should disable checkers at the start of functions.
|
|
|
|
if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
|
|
|
|
C.getCurrentAnalysisDeclContext()->getDecl())))
|
|
|
|
return;
|
2012-03-06 02:58:25 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-03-06 02:58:25 +08:00
|
|
|
unsigned NumArgs = CE.getNumArgs();
|
2012-04-17 05:51:09 +08:00
|
|
|
// If we passed 'self' as and argument to the call, record it in the state
|
|
|
|
// to be propagated after the call.
|
|
|
|
// Note, we could have just given up, but try to be more optimistic here and
|
|
|
|
// assume that the functions are going to continue initialization or will not
|
|
|
|
// modify self.
|
2012-03-06 02:58:25 +08:00
|
|
|
for (unsigned i = 0; i < NumArgs; ++i) {
|
|
|
|
SVal argV = CE.getArgSVal(i);
|
2011-01-12 03:45:25 +08:00
|
|
|
if (isSelfVar(argV, C)) {
|
2013-02-20 13:52:05 +08:00
|
|
|
unsigned selfFlags = getSelfFlags(state->getSVal(argV.castAs<Loc>()), C);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
|
2011-01-12 03:45:25 +08:00
|
|
|
return;
|
2011-02-05 13:54:53 +08:00
|
|
|
} else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
|
2011-02-23 01:30:38 +08:00
|
|
|
unsigned selfFlags = getSelfFlags(argV, C);
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
|
2011-02-05 13:54:53 +08:00
|
|
|
return;
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:21 +08:00
|
|
|
void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE,
|
2011-02-23 01:30:38 +08:00
|
|
|
CheckerContext &C) const {
|
2012-07-03 03:28:21 +08:00
|
|
|
// FIXME: A callback should disable checkers at the start of functions.
|
|
|
|
if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
|
|
|
|
C.getCurrentAnalysisDeclContext()->getDecl())))
|
|
|
|
return;
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-07-03 03:28:21 +08:00
|
|
|
SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>();
|
|
|
|
if (!prevFlags)
|
|
|
|
return;
|
|
|
|
state = state->remove<PreCallSelfFlags>();
|
|
|
|
|
2012-03-06 02:58:25 +08:00
|
|
|
unsigned NumArgs = CE.getNumArgs();
|
|
|
|
for (unsigned i = 0; i < NumArgs; ++i) {
|
|
|
|
SVal argV = CE.getArgSVal(i);
|
2011-01-12 03:45:25 +08:00
|
|
|
if (isSelfVar(argV, C)) {
|
2012-04-17 05:51:09 +08:00
|
|
|
// If the address of 'self' is being passed to the call, assume that the
|
|
|
|
// 'self' after the call will have the same flags.
|
|
|
|
// EX: log(&self)
|
2013-02-20 13:52:05 +08:00
|
|
|
addSelfFlag(state, state->getSVal(argV.castAs<Loc>()), prevFlags, C);
|
2011-01-12 03:45:25 +08:00
|
|
|
return;
|
2011-02-05 13:54:53 +08:00
|
|
|
} else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
|
2012-04-17 05:51:09 +08:00
|
|
|
// If 'self' is passed to the call by value, assume that the function
|
|
|
|
// returns 'self'. So assign the flags, which were set on 'self' to the
|
|
|
|
// return value.
|
|
|
|
// EX: self = performMoreInitialization(self)
|
2012-11-03 07:49:29 +08:00
|
|
|
addSelfFlag(state, CE.getReturnValue(), prevFlags, C);
|
2011-02-05 13:54:53 +08:00
|
|
|
return;
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-03 07:49:29 +08:00
|
|
|
|
|
|
|
C.addTransition(state);
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
|
2011-10-06 08:43:15 +08:00
|
|
|
const Stmt *S,
|
2011-02-23 01:30:38 +08:00
|
|
|
CheckerContext &C) const {
|
2012-12-13 08:42:19 +08:00
|
|
|
if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
|
|
|
|
C.getCurrentAnalysisDeclContext()->getDecl())))
|
|
|
|
return;
|
|
|
|
|
2011-01-12 03:45:25 +08:00
|
|
|
// Tag the result of a load from 'self' so that we can easily know that the
|
|
|
|
// value is the object that 'self' points to.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2011-01-12 03:45:25 +08:00
|
|
|
if (isSelfVar(location, C))
|
2013-02-20 13:52:05 +08:00
|
|
|
addSelfFlag(state, state->getSVal(location.castAs<Loc>()), SelfFlag_Self,
|
|
|
|
C);
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
|
2012-05-09 05:19:21 +08:00
|
|
|
|
|
|
|
void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
// Allow assignment of anything to self. Self is a local variable in the
|
|
|
|
// initializer, so it is legal to assign anything to it, like results of
|
2015-09-08 11:50:52 +08:00
|
|
|
// static functions/method calls. After self is assigned something we cannot
|
2012-05-09 05:19:21 +08:00
|
|
|
// reason about, stop enforcing the rules.
|
|
|
|
// (Only continue checking if the assigned value should be treated as self.)
|
|
|
|
if ((isSelfVar(loc, C)) &&
|
|
|
|
!hasSelfFlag(val, SelfFlag_InitRes, C) &&
|
|
|
|
!hasSelfFlag(val, SelfFlag_Self, C) &&
|
|
|
|
!isSelfVar(val, C)) {
|
|
|
|
|
|
|
|
// Stop tracking the checker-specific state in the state.
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
State = State->remove<CalledInit>();
|
|
|
|
if (SymbolRef sym = loc.getAsSymbol())
|
|
|
|
State = State->remove<SelfFlag>(sym);
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-08 09:47:11 +08:00
|
|
|
void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State,
|
|
|
|
const char *NL, const char *Sep) const {
|
2012-11-02 09:54:42 +08:00
|
|
|
SelfFlagTy FlagMap = State->get<SelfFlag>();
|
2012-09-08 09:47:11 +08:00
|
|
|
bool DidCallInit = State->get<CalledInit>();
|
|
|
|
SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>();
|
|
|
|
|
|
|
|
if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
|
|
|
|
return;
|
|
|
|
|
2014-02-18 02:25:34 +08:00
|
|
|
Out << Sep << NL << *this << " :" << NL;
|
2012-09-08 09:47:11 +08:00
|
|
|
|
|
|
|
if (DidCallInit)
|
|
|
|
Out << " An init method has been called." << NL;
|
|
|
|
|
|
|
|
if (PreCallFlags != SelfFlag_None) {
|
|
|
|
if (PreCallFlags & SelfFlag_Self) {
|
|
|
|
Out << " An argument of the current call came from the 'self' variable."
|
|
|
|
<< NL;
|
|
|
|
}
|
|
|
|
if (PreCallFlags & SelfFlag_InitRes) {
|
|
|
|
Out << " An argument of the current call came from an init method."
|
|
|
|
<< NL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << NL;
|
2012-11-02 09:54:42 +08:00
|
|
|
for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end();
|
|
|
|
I != E; ++I) {
|
2012-09-08 09:47:11 +08:00
|
|
|
Out << I->first << " : ";
|
|
|
|
|
|
|
|
if (I->second == SelfFlag_None)
|
|
|
|
Out << "none";
|
|
|
|
|
|
|
|
if (I->second & SelfFlag_Self)
|
|
|
|
Out << "self variable";
|
|
|
|
|
|
|
|
if (I->second & SelfFlag_InitRes) {
|
|
|
|
if (I->second != SelfFlag_InitRes)
|
|
|
|
Out << " | ";
|
|
|
|
Out << "result of init method";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << NL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-12 03:45:25 +08:00
|
|
|
// FIXME: A callback should disable checkers at the start of functions.
|
|
|
|
static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
|
|
|
|
if (!ND)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
|
|
|
|
if (!MD)
|
|
|
|
return false;
|
|
|
|
if (!isInitializationMethod(MD))
|
|
|
|
return false;
|
|
|
|
|
2011-01-26 07:54:44 +08:00
|
|
|
// self = [super init] applies only to NSObject subclasses.
|
|
|
|
// For instance, NSProxy doesn't implement -init.
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext &Ctx = MD->getASTContext();
|
2011-01-26 07:54:44 +08:00
|
|
|
IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
|
2011-08-13 07:37:29 +08:00
|
|
|
ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass();
|
2011-01-26 07:54:44 +08:00
|
|
|
for ( ; ID ; ID = ID->getSuperClass()) {
|
|
|
|
IdentifierInfo *II = ID->getIdentifier();
|
|
|
|
|
|
|
|
if (II == NSObjectII)
|
|
|
|
break;
|
|
|
|
}
|
2015-12-28 21:06:58 +08:00
|
|
|
return ID != nullptr;
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns true if the location is 'self'.
|
2011-01-12 03:45:25 +08:00
|
|
|
static bool isSelfVar(SVal location, CheckerContext &C) {
|
2015-09-08 11:50:52 +08:00
|
|
|
AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext();
|
2011-01-12 03:45:25 +08:00
|
|
|
if (!analCtx->getSelfDecl())
|
|
|
|
return false;
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!location.getAs<loc::MemRegionVal>())
|
2011-01-12 03:45:25 +08:00
|
|
|
return false;
|
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
loc::MemRegionVal MRV = location.castAs<loc::MemRegionVal>();
|
2012-04-17 05:51:09 +08:00
|
|
|
if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts()))
|
2011-01-12 03:45:25 +08:00
|
|
|
return (DR->getDecl() == analCtx->getSelfDecl());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isInitializationMethod(const ObjCMethodDecl *MD) {
|
2011-03-02 09:50:55 +08:00
|
|
|
return MD->getMethodFamily() == OMF_init;
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
static bool isInitMessage(const ObjCMethodCall &Call) {
|
|
|
|
return Call.getMethodFamily() == OMF_init;
|
2011-01-12 03:45:25 +08:00
|
|
|
}
|
2011-02-23 01:30:38 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Registration.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ento::registerObjCSelfInitChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ObjCSelfInitChecker>();
|
|
|
|
}
|