2011-08-20 14:00:03 +08:00
|
|
|
//=-- ExprEngineObjC.cpp - ExprEngine support for Objective-C ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines ExprEngine's support for Objective-C expressions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-01-28 20:06:22 +08:00
|
|
|
#include "clang/AST/StmtObjC.h"
|
2011-08-20 14:00:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2012-07-03 03:27:35 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/Calls.h"
|
2011-08-20 14:00:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
void ExprEngine::VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr *Ex,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = Pred->getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
|
|
|
SVal baseVal = state->getSVal(Ex->getBase(), LCtx);
|
2011-08-20 14:00:03 +08:00
|
|
|
SVal location = state->getLValue(Ex->getDecl(), baseVal);
|
|
|
|
|
|
|
|
ExplodedNodeSet dstIvar;
|
2011-10-25 05:19:59 +08:00
|
|
|
StmtNodeBuilder Bldr(Pred, dstIvar, *currentBuilderContext);
|
2012-01-07 06:09:28 +08:00
|
|
|
Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, location));
|
2011-08-20 14:00:03 +08:00
|
|
|
|
|
|
|
// Perform the post-condition check of the ObjCIvarRefExpr and store
|
|
|
|
// the created nodes in 'Dst'.
|
|
|
|
getCheckerManager().runCheckersForPostStmt(Dst, dstIvar, Ex, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprEngine::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
getCheckerManager().runCheckersForPreStmt(Dst, Pred, S, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprEngine::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
|
|
|
|
// ObjCForCollectionStmts are processed in two places. This method
|
|
|
|
// handles the case where an ObjCForCollectionStmt* occurs as one of the
|
|
|
|
// statements within a basic block. This transfer function does two things:
|
|
|
|
//
|
|
|
|
// (1) binds the next container value to 'element'. This creates a new
|
|
|
|
// node in the ExplodedGraph.
|
|
|
|
//
|
|
|
|
// (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
|
|
|
|
// whether or not the container has any more elements. This value
|
|
|
|
// will be tested in ProcessBranch. We need to explicitly bind
|
|
|
|
// this value because a container can contain nil elements.
|
|
|
|
//
|
|
|
|
// FIXME: Eventually this logic should actually do dispatches to
|
|
|
|
// 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
|
|
|
|
// This will require simulating a temporary NSFastEnumerationState, either
|
|
|
|
// through an SVal or through the use of MemRegions. This value can
|
|
|
|
// be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
|
|
|
|
// terminates we reclaim the temporary (it goes out of scope) and we
|
|
|
|
// we can test if the SVal is 0 or if the MemRegion is null (depending
|
|
|
|
// on what approach we take).
|
|
|
|
//
|
|
|
|
// For now: simulate (1) by assigning either a symbol or nil if the
|
|
|
|
// container is empty. Thus this transfer function will by default
|
|
|
|
// result in state splitting.
|
2011-10-25 02:26:19 +08:00
|
|
|
|
2011-08-20 14:00:03 +08:00
|
|
|
const Stmt *elem = S->getElement();
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = Pred->getState();
|
2011-08-20 14:00:03 +08:00
|
|
|
SVal elementV;
|
|
|
|
|
|
|
|
if (const DeclStmt *DS = dyn_cast<DeclStmt>(elem)) {
|
|
|
|
const VarDecl *elemD = cast<VarDecl>(DS->getSingleDecl());
|
|
|
|
assert(elemD->getInit() == 0);
|
|
|
|
elementV = state->getLValue(elemD, Pred->getLocationContext());
|
|
|
|
}
|
|
|
|
else {
|
2012-01-07 06:09:28 +08:00
|
|
|
elementV = state->getSVal(elem, Pred->getLocationContext());
|
2011-08-20 14:00:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ExplodedNodeSet dstLocation;
|
2012-04-07 06:10:18 +08:00
|
|
|
evalLocation(dstLocation, S, elem, Pred, state, elementV, NULL, false);
|
2012-06-12 00:40:41 +08:00
|
|
|
|
|
|
|
ExplodedNodeSet Tmp;
|
|
|
|
StmtNodeBuilder Bldr(Pred, Tmp, *currentBuilderContext);
|
|
|
|
|
2011-08-20 14:00:03 +08:00
|
|
|
for (ExplodedNodeSet::iterator NI = dstLocation.begin(),
|
|
|
|
NE = dstLocation.end(); NI!=NE; ++NI) {
|
|
|
|
Pred = *NI;
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = Pred->getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2011-08-20 14:00:03 +08:00
|
|
|
|
|
|
|
// Handle the case where the container still has elements.
|
|
|
|
SVal TrueV = svalBuilder.makeTruthVal(1);
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef hasElems = state->BindExpr(S, LCtx, TrueV);
|
2011-08-20 14:00:03 +08:00
|
|
|
|
|
|
|
// Handle the case where the container has no elements.
|
|
|
|
SVal FalseV = svalBuilder.makeTruthVal(0);
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef noElems = state->BindExpr(S, LCtx, FalseV);
|
2011-08-20 14:00:03 +08:00
|
|
|
|
|
|
|
if (loc::MemRegionVal *MV = dyn_cast<loc::MemRegionVal>(&elementV))
|
|
|
|
if (const TypedValueRegion *R =
|
|
|
|
dyn_cast<TypedValueRegion>(MV->getRegion())) {
|
|
|
|
// FIXME: The proper thing to do is to really iterate over the
|
|
|
|
// container. We will do this with dispatch logic to the store.
|
|
|
|
// For now, just 'conjure' up a symbolic value.
|
|
|
|
QualType T = R->getValueType();
|
|
|
|
assert(Loc::isLocType(T));
|
2011-10-25 02:26:19 +08:00
|
|
|
unsigned Count = currentBuilderContext->getCurrentBlockCount();
|
2012-02-18 07:13:45 +08:00
|
|
|
SymbolRef Sym = SymMgr.getConjuredSymbol(elem, LCtx, T, Count);
|
2011-08-20 14:00:03 +08:00
|
|
|
SVal V = svalBuilder.makeLoc(Sym);
|
|
|
|
hasElems = hasElems->bindLoc(elementV, V);
|
|
|
|
|
|
|
|
// Bind the location to 'nil' on the false branch.
|
|
|
|
SVal nilV = svalBuilder.makeIntVal(0, T);
|
|
|
|
noElems = noElems->bindLoc(elementV, nilV);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new nodes.
|
2011-10-25 02:26:19 +08:00
|
|
|
Bldr.generateNode(S, Pred, hasElems);
|
|
|
|
Bldr.generateNode(S, Pred, noElems);
|
2011-08-20 14:00:03 +08:00
|
|
|
}
|
2012-06-12 00:40:41 +08:00
|
|
|
|
|
|
|
// Finally, run any custom checkers.
|
|
|
|
// FIXME: Eventually all pre- and post-checks should live in VisitStmt.
|
|
|
|
getCheckerManager().runCheckersForPostStmt(Dst, Tmp, S, *this);
|
2011-08-20 14:00:03 +08:00
|
|
|
}
|
|
|
|
|
2012-06-23 01:15:58 +08:00
|
|
|
static bool isSubclass(const ObjCInterfaceDecl *Class, IdentifierInfo *II) {
|
|
|
|
if (!Class)
|
|
|
|
return false;
|
|
|
|
if (Class->getIdentifier() == II)
|
|
|
|
return true;
|
|
|
|
return isSubclass(Class->getSuperClass(), II);
|
|
|
|
}
|
|
|
|
|
2012-07-03 03:27:56 +08:00
|
|
|
void ExprEngine::VisitObjCMessage(const ObjCMethodCall &msg,
|
2011-08-20 14:00:03 +08:00
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
|
|
|
|
// Handle the previsits checks.
|
|
|
|
ExplodedNodeSet dstPrevisit;
|
2012-07-03 03:28:16 +08:00
|
|
|
getCheckerManager().runCheckersForPreObjCMessage(dstPrevisit, Pred,
|
2011-08-20 14:00:03 +08:00
|
|
|
msg, *this);
|
2012-07-03 03:28:16 +08:00
|
|
|
ExplodedNodeSet dstGenericPrevisit;
|
|
|
|
getCheckerManager().runCheckersForPreCall(dstGenericPrevisit, dstPrevisit,
|
|
|
|
msg, *this);
|
|
|
|
|
2011-08-20 14:00:03 +08:00
|
|
|
// Proceed with evaluate the message expression.
|
|
|
|
ExplodedNodeSet dstEval;
|
2012-07-03 03:28:16 +08:00
|
|
|
StmtNodeBuilder Bldr(dstGenericPrevisit, dstEval, *currentBuilderContext);
|
2011-10-25 02:26:19 +08:00
|
|
|
|
2012-07-03 03:28:16 +08:00
|
|
|
for (ExplodedNodeSet::iterator DI = dstGenericPrevisit.begin(),
|
|
|
|
DE = dstGenericPrevisit.end(); DI != DE; ++DI) {
|
2011-08-20 14:00:03 +08:00
|
|
|
ExplodedNode *Pred = *DI;
|
|
|
|
|
2012-07-03 03:27:56 +08:00
|
|
|
if (msg.isInstanceMessage()) {
|
|
|
|
SVal recVal = msg.getReceiverSVal();
|
2011-08-20 14:00:03 +08:00
|
|
|
if (!recVal.isUndef()) {
|
|
|
|
// Bifurcate the state into nil and non-nil ones.
|
|
|
|
DefinedOrUnknownSVal receiverVal = cast<DefinedOrUnknownSVal>(recVal);
|
|
|
|
|
2012-07-03 03:27:56 +08:00
|
|
|
ProgramStateRef state = Pred->getState();
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef notNilState, nilState;
|
2011-08-20 14:00:03 +08:00
|
|
|
llvm::tie(notNilState, nilState) = state->assume(receiverVal);
|
|
|
|
|
|
|
|
// There are three cases: can be nil or non-nil, must be nil, must be
|
|
|
|
// non-nil. We ignore must be nil, and merge the rest two into non-nil.
|
2012-07-03 03:27:56 +08:00
|
|
|
// FIXME: This ignores many potential bugs (<rdar://problem/11733396>).
|
|
|
|
// Revisit once we have lazier constraints.
|
2011-08-20 14:00:03 +08:00
|
|
|
if (nilState && !notNilState) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the "raise" message was sent.
|
|
|
|
assert(notNilState);
|
2012-07-20 07:38:13 +08:00
|
|
|
if (msg.getSelector() == RaiseSel) {
|
|
|
|
// If we raise an exception, for now treat it as a sink.
|
|
|
|
// Eventually we will want to handle exceptions properly.
|
|
|
|
Bldr.generateNode(currentStmt, Pred, Pred->getState(), true);
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-20 14:00:03 +08:00
|
|
|
|
2012-07-20 07:38:13 +08:00
|
|
|
// Generate a transition to non-Nil state.
|
|
|
|
if (notNilState != state)
|
|
|
|
Pred = Bldr.generateNode(currentStmt, Pred, notNilState);
|
2011-08-20 14:00:03 +08:00
|
|
|
}
|
2012-07-03 03:27:56 +08:00
|
|
|
} else {
|
2012-06-23 01:15:58 +08:00
|
|
|
// Check for special class methods.
|
2012-07-03 03:27:56 +08:00
|
|
|
if (const ObjCInterfaceDecl *Iface = msg.getReceiverInterface()) {
|
2012-06-23 01:15:58 +08:00
|
|
|
if (!NSExceptionII) {
|
2011-08-20 14:00:03 +08:00
|
|
|
ASTContext &Ctx = getContext();
|
2012-06-23 01:15:58 +08:00
|
|
|
NSExceptionII = &Ctx.Idents.get("NSException");
|
2011-08-20 14:00:03 +08:00
|
|
|
}
|
|
|
|
|
2012-06-23 01:15:58 +08:00
|
|
|
if (isSubclass(Iface, NSExceptionII)) {
|
|
|
|
enum { NUM_RAISE_SELECTORS = 2 };
|
|
|
|
|
|
|
|
// Lazily create a cache of the selectors.
|
|
|
|
if (!NSExceptionInstanceRaiseSelectors) {
|
|
|
|
ASTContext &Ctx = getContext();
|
|
|
|
NSExceptionInstanceRaiseSelectors =
|
|
|
|
new Selector[NUM_RAISE_SELECTORS];
|
|
|
|
SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
|
|
|
|
unsigned idx = 0;
|
|
|
|
|
|
|
|
// raise:format:
|
|
|
|
II.push_back(&Ctx.Idents.get("raise"));
|
|
|
|
II.push_back(&Ctx.Idents.get("format"));
|
|
|
|
NSExceptionInstanceRaiseSelectors[idx++] =
|
|
|
|
Ctx.Selectors.getSelector(II.size(), &II[0]);
|
|
|
|
|
|
|
|
// raise:format:arguments:
|
|
|
|
II.push_back(&Ctx.Idents.get("arguments"));
|
|
|
|
NSExceptionInstanceRaiseSelectors[idx++] =
|
|
|
|
Ctx.Selectors.getSelector(II.size(), &II[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Selector S = msg.getSelector();
|
2012-07-20 07:38:13 +08:00
|
|
|
bool RaisesException = false;
|
2012-06-23 01:15:58 +08:00
|
|
|
for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i) {
|
|
|
|
if (S == NSExceptionInstanceRaiseSelectors[i]) {
|
|
|
|
RaisesException = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-08-20 14:00:03 +08:00
|
|
|
}
|
2012-07-20 07:38:13 +08:00
|
|
|
if (RaisesException) {
|
|
|
|
// If we raise an exception, for now treat it as a sink.
|
|
|
|
// Eventually we will want to handle exceptions properly.
|
|
|
|
Bldr.generateNode(currentStmt, Pred, Pred->getState(), true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-06-23 01:15:58 +08:00
|
|
|
}
|
2011-08-20 14:00:03 +08:00
|
|
|
}
|
|
|
|
}
|
2012-07-26 08:27:51 +08:00
|
|
|
// Evaluate the call.
|
|
|
|
defaultEvalCall(Bldr, Pred, msg);
|
|
|
|
|
2011-08-20 14:00:03 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:16 +08:00
|
|
|
ExplodedNodeSet dstPostvisit;
|
|
|
|
getCheckerManager().runCheckersForPostCall(dstPostvisit, dstEval, msg, *this);
|
|
|
|
|
2011-08-20 14:00:03 +08:00
|
|
|
// Finally, perform the post-condition check of the ObjCMessageExpr and store
|
|
|
|
// the created nodes in 'Dst'.
|
2012-07-03 03:28:16 +08:00
|
|
|
getCheckerManager().runCheckersForPostObjCMessage(Dst, dstPostvisit,
|
|
|
|
msg, *this);
|
2011-08-20 14:00:03 +08:00
|
|
|
}
|