2008-02-15 06:13:12 +08:00
|
|
|
//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
|
2008-01-31 10:35:41 +08:00
|
|
|
//
|
2008-01-31 14:49:09 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
2008-01-16 07:55:06 +08:00
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-02-15 06:13:12 +08:00
|
|
|
// This file defines a meta-engine for path-sensitive dataflow analysis that
|
|
|
|
// is built on GREngine, but provides the boilerplate to execute transfer
|
|
|
|
// functions and build the ExplodedGraph at the expression level.
|
2008-01-16 07:55:06 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-15 06:13:12 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
2009-02-14 09:43:44 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
|
2009-07-23 05:43:51 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/Checker.h"
|
2009-04-26 09:32:48 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
|
|
|
#include "clang/AST/StmtObjC.h"
|
2009-06-14 09:54:56 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2009-04-26 09:32:48 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-03-08 04:57:30 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2009-03-11 10:41:36 +08:00
|
|
|
#include "clang/Basic/PrettyStackTrace.h"
|
2008-07-12 02:37:32 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-09-13 13:16:45 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-08-23 20:08:50 +08:00
|
|
|
#include "llvm/ADT/ImmutableList.h"
|
2008-07-11 06:03:41 +08:00
|
|
|
|
2008-02-27 14:07:00 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
|
|
|
#endif
|
|
|
|
|
2008-02-15 06:16:04 +08:00
|
|
|
using namespace clang;
|
|
|
|
using llvm::dyn_cast;
|
|
|
|
using llvm::cast;
|
|
|
|
using llvm::APSInt;
|
2008-01-24 03:59:44 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-07-23 05:43:51 +08:00
|
|
|
// Batch auditor. DEPRECATED.
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck {
|
|
|
|
typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks;
|
|
|
|
typedef llvm::DenseMap<void*,Checks> MapTy;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
MapTy M;
|
|
|
|
Checks::Factory F;
|
2009-03-31 01:53:05 +08:00
|
|
|
Checks AllStmts;
|
2008-07-12 02:37:32 +08:00
|
|
|
|
|
|
|
public:
|
2009-03-31 01:53:05 +08:00
|
|
|
MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) :
|
|
|
|
F(Alloc), AllStmts(F.GetEmptyList()) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
virtual ~MappedBatchAuditor() {
|
|
|
|
llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
|
|
|
|
for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){
|
|
|
|
|
|
|
|
GRSimpleAPICheck* check = *I;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
if (AlreadyVisited.count(check))
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
AlreadyVisited.insert(check);
|
|
|
|
delete check;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-31 01:53:05 +08:00
|
|
|
void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) {
|
2008-07-12 02:37:32 +08:00
|
|
|
assert (A && "Check cannot be null.");
|
|
|
|
void* key = reinterpret_cast<void*>((uintptr_t) C);
|
|
|
|
MapTy::iterator I = M.find(key);
|
|
|
|
M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-31 01:53:05 +08:00
|
|
|
void AddCheck(GRSimpleAPICheck *A) {
|
|
|
|
assert (A && "Check cannot be null.");
|
2009-09-09 23:08:12 +08:00
|
|
|
AllStmts = F.Concat(A, AllStmts);
|
2009-03-31 01:53:05 +08:00
|
|
|
}
|
2009-02-05 07:49:09 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
virtual bool Audit(ExplodedNode* N, GRStateManager& VMgr) {
|
2009-03-31 01:53:05 +08:00
|
|
|
// First handle the auditors that accept all statements.
|
|
|
|
bool isSink = false;
|
|
|
|
for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I)
|
|
|
|
isSink |= (*I)->Audit(N, VMgr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-31 01:53:05 +08:00
|
|
|
// Next handle the auditors that accept only specific statements.
|
2009-07-23 06:35:28 +08:00
|
|
|
const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
|
2008-07-12 02:37:32 +08:00
|
|
|
void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
|
|
|
|
MapTy::iterator MI = M.find(key);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (MI != M.end()) {
|
2009-03-31 01:53:05 +08:00
|
|
|
for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
|
|
|
|
isSink |= (*I)->Audit(N, VMgr);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
return isSink;
|
2008-07-12 02:37:32 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-07-23 05:43:51 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Checker worklist routines.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
void GRExprEngine::CheckerVisit(Stmt *S, ExplodedNodeSet &Dst,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet &Src, bool isPrevisit) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 05:43:51 +08:00
|
|
|
if (Checkers.empty()) {
|
|
|
|
Dst = Src;
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
|
|
|
ExplodedNodeSet *PrevSet = &Src;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 05:43:51 +08:00
|
|
|
for (std::vector<Checker*>::iterator I = Checkers.begin(), E = Checkers.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
ExplodedNodeSet *CurrSet = (I+1 == E) ? &Dst
|
2009-08-06 20:48:26 +08:00
|
|
|
: (PrevSet == &Tmp) ? &Src : &Tmp;
|
2009-07-23 05:43:51 +08:00
|
|
|
CurrSet->clear();
|
|
|
|
Checker *checker = *I;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
|
2009-07-23 05:43:51 +08:00
|
|
|
NI != NE; ++NI)
|
|
|
|
checker->GR_Visit(*CurrSet, *Builder, *this, S, *NI, isPrevisit);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 05:43:51 +08:00
|
|
|
// Update which NodeSet is the current one.
|
|
|
|
PrevSet = CurrSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't autotransition. The CheckerContext objects should do this
|
|
|
|
// automatically.
|
|
|
|
}
|
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Engine construction and deletion.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
|
|
|
|
IdentifierInfo* II = &Ctx.Idents.get(name);
|
|
|
|
return Ctx.Selectors.getSelector(0, &II);
|
|
|
|
}
|
|
|
|
|
2008-03-10 02:05:48 +08:00
|
|
|
|
2009-08-25 14:51:30 +08:00
|
|
|
GRExprEngine::GRExprEngine(AnalysisManager &mgr)
|
2009-08-15 11:17:38 +08:00
|
|
|
: AMgr(mgr),
|
2009-09-09 23:08:12 +08:00
|
|
|
CoreEngine(mgr.getASTContext(), *this),
|
2008-04-10 05:41:14 +08:00
|
|
|
G(CoreEngine.getGraph()),
|
|
|
|
Builder(NULL),
|
2009-09-09 23:08:12 +08:00
|
|
|
StateMgr(G.getContext(), mgr.getStoreManagerCreator(),
|
2009-08-25 14:51:30 +08:00
|
|
|
mgr.getConstraintManagerCreator(), G.getAllocator()),
|
2008-04-10 05:41:14 +08:00
|
|
|
SymMgr(StateMgr.getSymbolManager()),
|
2009-04-10 00:46:55 +08:00
|
|
|
ValMgr(StateMgr.getValueManager()),
|
2009-07-16 09:32:00 +08:00
|
|
|
SVator(ValMgr.getSValuator()),
|
2008-05-02 02:33:28 +08:00
|
|
|
CurrentStmt(NULL),
|
2008-12-22 16:30:52 +08:00
|
|
|
NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
|
2009-09-09 23:08:12 +08:00
|
|
|
RaiseSel(GetNullarySelector("raise", G.getContext())),
|
2009-08-25 14:51:30 +08:00
|
|
|
BR(mgr, *this) {}
|
2008-04-10 05:41:14 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
GRExprEngine::~GRExprEngine() {
|
2009-02-05 07:49:09 +08:00
|
|
|
BR.FlushReports();
|
2008-05-02 02:33:28 +08:00
|
|
|
delete [] NSExceptionInstanceRaiseSelectors;
|
2009-07-23 05:43:51 +08:00
|
|
|
for (std::vector<Checker*>::iterator I=Checkers.begin(), E=Checkers.end();
|
|
|
|
I!=E; ++I)
|
|
|
|
delete *I;
|
2008-04-10 05:41:14 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
|
2008-04-10 05:41:14 +08:00
|
|
|
void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
|
2008-07-18 07:15:45 +08:00
|
|
|
StateMgr.TF = tf;
|
2009-02-05 07:49:09 +08:00
|
|
|
tf->RegisterChecks(getBugReporter());
|
2008-08-16 08:49:49 +08:00
|
|
|
tf->RegisterPrinters(getStateManager().Printers);
|
2008-04-10 05:41:14 +08:00
|
|
|
}
|
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
|
|
|
|
if (!BatchAuditor)
|
|
|
|
BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
|
2008-04-10 05:41:14 +08:00
|
|
|
}
|
|
|
|
|
2009-03-31 01:53:05 +08:00
|
|
|
void GRExprEngine::AddCheck(GRSimpleAPICheck *A) {
|
|
|
|
if (!BatchAuditor)
|
|
|
|
BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator()));
|
|
|
|
|
|
|
|
((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A);
|
|
|
|
}
|
|
|
|
|
2009-08-17 14:19:58 +08:00
|
|
|
const GRState* GRExprEngine::getInitialState(const LocationContext *InitLoc) {
|
|
|
|
const GRState *state = StateMgr.getInitialState(InitLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-10 08:59:50 +08:00
|
|
|
// Precondition: the first argument of 'main' is an integer guaranteed
|
|
|
|
// to be > 0.
|
|
|
|
// FIXME: It would be nice if we had a more general mechanism to add
|
|
|
|
// such preconditions. Some day.
|
2009-08-21 11:05:36 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(InitLoc->getDecl()))
|
2009-04-10 08:59:50 +08:00
|
|
|
if (strcmp(FD->getIdentifier()->getName(), "main") == 0 &&
|
|
|
|
FD->getNumParams() > 0) {
|
|
|
|
const ParmVarDecl *PD = FD->getParamDecl(0);
|
|
|
|
QualType T = PD->getType();
|
|
|
|
if (T->isIntegerType())
|
2009-08-22 06:28:32 +08:00
|
|
|
if (const MemRegion *R = state->getRegion(PD, InitLoc)) {
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = state->getSVal(loc::MemRegionVal(R));
|
2009-05-20 17:00:16 +08:00
|
|
|
SVal Constraint = EvalBinOp(state, BinaryOperator::GT, V,
|
2009-04-10 08:59:50 +08:00
|
|
|
ValMgr.makeZeroVal(T),
|
2009-09-09 23:08:12 +08:00
|
|
|
getContext().IntTy);
|
2009-06-19 06:57:13 +08:00
|
|
|
|
|
|
|
if (const GRState *newState = state->assume(Constraint, true))
|
|
|
|
state = newState;
|
2009-04-10 08:59:50 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-10 08:59:50 +08:00
|
|
|
return state;
|
2008-02-05 05:59:01 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top-level transfer function logic (Dispatcher).
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::ProcessStmt(Stmt* S, GRStmtNodeBuilder& builder) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-11 10:41:36 +08:00
|
|
|
PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
|
|
|
|
S->getLocStart(),
|
|
|
|
"Error evaluating statement");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
Builder = &builder;
|
2008-04-25 07:35:58 +08:00
|
|
|
EntryNode = builder.getLastNode();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-18 05:27:31 +08:00
|
|
|
// FIXME: Consolidate.
|
2008-04-16 07:06:53 +08:00
|
|
|
CurrentStmt = S;
|
2008-07-18 05:27:31 +08:00
|
|
|
StateMgr.CurrentStmt = S;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// Set up our simple checks.
|
2008-07-12 02:37:32 +08:00
|
|
|
if (BatchAuditor)
|
|
|
|
Builder->setAuditor(BatchAuditor.get());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Create the cleaned state.
|
|
|
|
SymbolReaper SymReaper(*AMgr.getLiveVariables(), SymMgr);
|
2009-08-27 14:55:26 +08:00
|
|
|
CleanedState = AMgr.shouldPurgeDead()
|
|
|
|
? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, SymReaper)
|
|
|
|
: EntryNode->getState();
|
2009-01-22 06:26:05 +08:00
|
|
|
|
2008-04-25 02:31:42 +08:00
|
|
|
// Process any special transfer function for dead symbols.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-22 06:26:05 +08:00
|
|
|
if (!SymReaper.hasDeadSymbols())
|
2008-04-25 07:35:58 +08:00
|
|
|
Tmp.Add(EntryNode);
|
2008-04-25 02:31:42 +08:00
|
|
|
else {
|
|
|
|
SaveAndRestore<bool> OldSink(Builder->BuildSinks);
|
2008-04-25 07:35:58 +08:00
|
|
|
SaveOr OldHasGen(Builder->HasGeneratedNode);
|
|
|
|
|
2008-06-18 13:34:07 +08:00
|
|
|
SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols);
|
|
|
|
Builder->PurgingDeadSymbols = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
|
2009-01-22 06:26:05 +08:00
|
|
|
CleanedState, SymReaper);
|
2008-04-25 07:35:58 +08:00
|
|
|
|
|
|
|
if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
|
|
|
|
Tmp.Add(EntryNode);
|
2008-04-25 02:31:42 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-25 07:35:58 +08:00
|
|
|
bool HasAutoGenerated = false;
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2008-04-25 07:35:58 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Dst;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Set the cleaned state.
|
2008-04-25 07:35:58 +08:00
|
|
|
Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Visit the statement.
|
2008-04-25 07:35:58 +08:00
|
|
|
Visit(S, *I, Dst);
|
|
|
|
|
|
|
|
// Do we need to auto-generate a node? We only need to do this to generate
|
|
|
|
// a node with a "cleaned" state; GRCoreEngine will actually handle
|
2009-09-09 23:08:12 +08:00
|
|
|
// auto-transitions for other cases.
|
2008-04-25 07:35:58 +08:00
|
|
|
if (Dst.size() == 1 && *Dst.begin() == EntryNode
|
|
|
|
&& !Builder->HasGeneratedNode && !HasAutoGenerated) {
|
|
|
|
HasAutoGenerated = true;
|
|
|
|
builder.generateNode(S, GetState(EntryNode), *I);
|
|
|
|
}
|
2008-04-25 02:31:42 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// NULL out these variables to cleanup.
|
2008-04-25 07:35:58 +08:00
|
|
|
CleanedState = NULL;
|
|
|
|
EntryNode = NULL;
|
2008-07-18 05:27:31 +08:00
|
|
|
|
|
|
|
// FIXME: Consolidate.
|
|
|
|
StateMgr.CurrentStmt = 0;
|
|
|
|
CurrentStmt = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
Builder = NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
|
2009-03-11 10:41:36 +08:00
|
|
|
PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
|
|
|
|
S->getLocStart(),
|
|
|
|
"Error evaluating statement");
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// FIXME: add metadata to the CFG so that we can disable
|
|
|
|
// this check when we KNOW that there is no block-level subexpression.
|
|
|
|
// The motivation is that this check requires a hashtable lookup.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-25 11:33:41 +08:00
|
|
|
if (S != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S)) {
|
2008-04-16 07:06:53 +08:00
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
switch (S->getStmtClass()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
default:
|
|
|
|
// Cases we intentionally have "default" handle:
|
|
|
|
// AddrLabelExpr, IntegerLiteral, CharacterLiteral
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-22 12:56:29 +08:00
|
|
|
case Stmt::ArraySubscriptExprClass:
|
|
|
|
VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::AsmStmtClass:
|
|
|
|
VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
BinaryOperator* B = cast<BinaryOperator>(S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
if (B->isLogicalOp()) {
|
|
|
|
VisitLogicalExpr(B, Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (B->getOpcode() == BinaryOperator::Comma) {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(Pred);
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, B, Pred, state->BindExpr(B, state->getSVal(B->getRHS())));
|
2008-04-16 07:06:53 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-11-15 03:47:18 +08:00
|
|
|
|
2009-08-25 14:51:30 +08:00
|
|
|
if (AMgr.shouldEagerlyAssume() && (B->isRelationalOp() || B->isEqualityOp())) {
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2009-02-26 06:32:02 +08:00
|
|
|
VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S));
|
2009-02-26 06:32:02 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-11-15 03:47:18 +08:00
|
|
|
|
2008-11-15 00:09:21 +08:00
|
|
|
case Stmt::CallExprClass:
|
|
|
|
case Stmt::CXXOperatorCallExprClass: {
|
2008-04-16 07:06:53 +08:00
|
|
|
CallExpr* C = cast<CallExpr>(S);
|
|
|
|
VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
|
2008-11-15 03:47:18 +08:00
|
|
|
break;
|
2008-04-16 07:06:53 +08:00
|
|
|
}
|
2008-11-15 03:47:18 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// FIXME: ChooseExpr is really a constant. We need to fix
|
|
|
|
// the CFG do not model them as explicit control-flow.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::ChooseExprClass: { // __builtin_choose_expr
|
|
|
|
ChooseExpr* C = cast<ChooseExpr>(S);
|
|
|
|
VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::CompoundAssignOperatorClass:
|
|
|
|
VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
|
|
|
|
break;
|
2008-11-07 18:38:33 +08:00
|
|
|
|
|
|
|
case Stmt::CompoundLiteralExprClass:
|
|
|
|
VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::ConditionalOperatorClass: { // '?' operator
|
|
|
|
ConditionalOperator* C = cast<ConditionalOperator>(S);
|
|
|
|
VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::DeclRefExprClass:
|
2009-01-06 13:10:23 +08:00
|
|
|
case Stmt::QualifiedDeclRefExprClass:
|
2008-04-30 05:04:26 +08:00
|
|
|
VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
|
2008-04-16 07:06:53 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::DeclStmtClass:
|
|
|
|
VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-19 07:01:59 +08:00
|
|
|
case Stmt::ImplicitCastExprClass:
|
2008-10-28 23:36:24 +08:00
|
|
|
case Stmt::CStyleCastExprClass: {
|
2008-08-19 07:01:59 +08:00
|
|
|
CastExpr* C = cast<CastExpr>(S);
|
2008-04-16 07:06:53 +08:00
|
|
|
VisitCast(C, C->getSubExpr(), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
2008-10-30 13:02:23 +08:00
|
|
|
|
|
|
|
case Stmt::InitListExprClass:
|
|
|
|
VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 08:03:18 +08:00
|
|
|
case Stmt::MemberExprClass:
|
2008-04-22 07:43:38 +08:00
|
|
|
VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 08:03:18 +08:00
|
|
|
case Stmt::ObjCIvarRefExprClass:
|
|
|
|
VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false);
|
|
|
|
break;
|
2008-11-13 03:24:17 +08:00
|
|
|
|
|
|
|
case Stmt::ObjCForCollectionStmtClass:
|
|
|
|
VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::ObjCMessageExprClass: {
|
|
|
|
VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-10 04:18:58 +08:00
|
|
|
case Stmt::ObjCAtThrowStmtClass: {
|
|
|
|
// FIXME: This is not complete. We basically treat @throw as
|
|
|
|
// an abort.
|
|
|
|
SaveAndRestore<bool> OldSink(Builder->BuildSinks);
|
|
|
|
Builder->BuildSinks = true;
|
|
|
|
MakeNode(Dst, S, Pred, GetState(Pred));
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::ParenExprClass:
|
2008-04-22 12:56:29 +08:00
|
|
|
Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst);
|
2008-04-16 07:06:53 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::ReturnStmtClass:
|
|
|
|
VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 01:56:53 +08:00
|
|
|
case Stmt::SizeOfAlignOfExprClass:
|
|
|
|
VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst);
|
2008-04-16 07:06:53 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::StmtExprClass: {
|
|
|
|
StmtExpr* SE = cast<StmtExpr>(S);
|
2009-02-14 13:55:08 +08:00
|
|
|
|
|
|
|
if (SE->getSubStmt()->body_empty()) {
|
|
|
|
// Empty statement expression.
|
|
|
|
assert(SE->getType() == getContext().VoidTy
|
|
|
|
&& "Empty statement expression must have void type.");
|
|
|
|
Dst.Add(Pred);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 13:55:08 +08:00
|
|
|
if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
|
|
|
|
const GRState* state = GetState(Pred);
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, SE, Pred, state->BindExpr(SE, state->getSVal(LastExpr)));
|
2009-02-14 13:55:08 +08:00
|
|
|
}
|
2008-04-16 07:06:53 +08:00
|
|
|
else
|
|
|
|
Dst.Add(Pred);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-11-30 13:49:49 +08:00
|
|
|
|
|
|
|
case Stmt::StringLiteralClass:
|
|
|
|
VisitLValue(cast<StringLiteral>(S), Pred, Dst);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-19 07:49:26 +08:00
|
|
|
case Stmt::UnaryOperatorClass: {
|
|
|
|
UnaryOperator *U = cast<UnaryOperator>(S);
|
2009-08-25 14:51:30 +08:00
|
|
|
if (AMgr.shouldEagerlyAssume() && (U->getOpcode() == UnaryOperator::LNot)) {
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2009-03-19 07:49:26 +08:00
|
|
|
VisitUnaryOperator(U, Pred, Tmp, false);
|
|
|
|
EvalEagerlyAssume(Dst, Tmp, U);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
VisitUnaryOperator(U, Pred, Dst, false);
|
2008-04-30 05:04:26 +08:00
|
|
|
break;
|
2009-03-19 07:49:26 +08:00
|
|
|
}
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void GRExprEngine::VisitLValue(Expr* Ex, ExplodedNode* Pred,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet& Dst) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
Ex = Ex->IgnoreParens();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-25 11:33:41 +08:00
|
|
|
if (Ex != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)) {
|
2008-04-30 05:04:26 +08:00
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
switch (Ex->getStmtClass()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::ArraySubscriptExprClass:
|
|
|
|
VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::DeclRefExprClass:
|
2009-01-06 13:10:23 +08:00
|
|
|
case Stmt::QualifiedDeclRefExprClass:
|
2008-04-30 05:04:26 +08:00
|
|
|
VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 08:03:18 +08:00
|
|
|
case Stmt::ObjCIvarRefExprClass:
|
|
|
|
VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::UnaryOperatorClass:
|
|
|
|
VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::MemberExprClass:
|
|
|
|
VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 05:54:31 +08:00
|
|
|
case Stmt::CompoundLiteralExprClass:
|
2008-11-07 18:38:33 +08:00
|
|
|
VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true);
|
2008-10-28 05:54:31 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 01:24:14 +08:00
|
|
|
case Stmt::ObjCPropertyRefExprClass:
|
2009-08-21 01:02:02 +08:00
|
|
|
case Stmt::ObjCImplicitSetterGetterRefExprClass:
|
2008-10-18 01:24:14 +08:00
|
|
|
// FIXME: Property assignments are lvalues, but not really "locations".
|
|
|
|
// e.g.: self.x = something;
|
|
|
|
// Here the "self.x" really can translate to a method call (setter) when
|
|
|
|
// the assignment is made. Moreover, the entire assignment expression
|
|
|
|
// evaluate to whatever "something" is, not calling the "getter" for
|
|
|
|
// the property (which would make sense since it can have side effects).
|
|
|
|
// We'll probably treat this as a location, but not one that we can
|
|
|
|
// take the address of. Perhaps we need a new SVal class for cases
|
|
|
|
// like thsis?
|
|
|
|
// Note that we have a similar problem for bitfields, since they don't
|
|
|
|
// have "locations" in the sense that we can take their address.
|
|
|
|
Dst.Add(Pred);
|
2008-10-18 12:08:49 +08:00
|
|
|
return;
|
2008-10-25 22:18:57 +08:00
|
|
|
|
|
|
|
case Stmt::StringLiteralClass: {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(Pred);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = state->getLValue(cast<StringLiteral>(Ex));
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
|
2008-10-25 22:18:57 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 12:15:35 +08:00
|
|
|
default:
|
|
|
|
// Arbitrary subexpressions can return aggregate temporaries that
|
|
|
|
// can be used in a lvalue context. We need to enhance our support
|
|
|
|
// of such temporaries in both the environment and the store, so right
|
|
|
|
// now we just do a regular visit.
|
2009-01-31 01:31:00 +08:00
|
|
|
assert ((Ex->getType()->isAggregateType()) &&
|
2008-10-26 04:09:21 +08:00
|
|
|
"Other kinds of expressions with non-aggregate/union types do"
|
|
|
|
" not have lvalues.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 12:15:35 +08:00
|
|
|
Visit(Ex, Pred, Dst);
|
2008-04-16 07:06:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Block entrance. (Update counters).
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*,
|
2008-04-16 07:06:53 +08:00
|
|
|
GRBlockCounter BC) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
return BC.getNumVisited(B->getBlockID()) < 3;
|
|
|
|
}
|
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Generic node creation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* GRExprEngine::MakeNode(ExplodedNodeSet& Dst, Stmt* S,
|
|
|
|
ExplodedNode* Pred, const GRState* St,
|
|
|
|
ProgramPoint::Kind K, const void *tag) {
|
2009-04-11 08:11:10 +08:00
|
|
|
assert (Builder && "GRStmtNodeBuilder not present.");
|
|
|
|
SaveAndRestore<const void*> OldTag(Builder->Tag);
|
|
|
|
Builder->Tag = tag;
|
|
|
|
return Builder->MakeNode(Dst, S, Pred, St, K);
|
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Branch processing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* GRExprEngine::MarkBranch(const GRState* state,
|
2008-07-11 06:03:41 +08:00
|
|
|
Stmt* Terminator,
|
|
|
|
bool branchTaken) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
switch (Terminator->getStmtClass()) {
|
|
|
|
default:
|
2009-02-13 09:45:31 +08:00
|
|
|
return state;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
case Stmt::BinaryOperatorClass: { // '&&' and '||'
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
BinaryOperator* B = cast<BinaryOperator>(Terminator);
|
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
// For &&, if we take the true branch, then the value of the whole
|
|
|
|
// expression is that of the RHS expression.
|
|
|
|
//
|
|
|
|
// For ||, if we take the false branch, then the value of the whole
|
|
|
|
// expression is that of the RHS expression.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
|
2009-09-09 23:08:12 +08:00
|
|
|
(Op == BinaryOperator::LOr && !branchTaken)
|
2008-02-27 03:05:15 +08:00
|
|
|
? B->getRHS() : B->getLHS();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-28 06:17:37 +08:00
|
|
|
return state->BindExpr(B, UndefinedVal(Ex));
|
2008-02-27 03:05:15 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
case Stmt::ConditionalOperatorClass: { // ?:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
// For ?, if branchTaken == true then the value is either the LHS or
|
|
|
|
// the condition itself. (GNU extension).
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
Expr* Ex;
|
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
if (branchTaken)
|
2009-09-09 23:08:12 +08:00
|
|
|
Ex = C->getLHS() ? C->getLHS() : C->getCond();
|
2008-02-27 03:05:15 +08:00
|
|
|
else
|
|
|
|
Ex = C->getRHS();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-28 06:17:37 +08:00
|
|
|
return state->BindExpr(C, UndefinedVal(Ex));
|
2008-02-27 03:05:15 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
case Stmt::ChooseExprClass: { // ?:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
ChooseExpr* C = cast<ChooseExpr>(Terminator);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
|
2009-08-28 06:17:37 +08:00
|
|
|
return state->BindExpr(C, UndefinedVal(Ex));
|
2008-02-27 03:05:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-14 00:32:54 +08:00
|
|
|
/// RecoverCastedSymbol - A helper function for ProcessBranch that is used
|
|
|
|
/// to try to recover some path-sensitivity for casts of symbolic
|
|
|
|
/// integers that promote their values (which are currently not tracked well).
|
|
|
|
/// This function returns the SVal bound to Condition->IgnoreCasts if all the
|
|
|
|
// cast(s) did was sign-extend the original value.
|
|
|
|
static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state,
|
|
|
|
Stmt* Condition, ASTContext& Ctx) {
|
|
|
|
|
|
|
|
Expr *Ex = dyn_cast<Expr>(Condition);
|
|
|
|
if (!Ex)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
uint64_t bits = 0;
|
|
|
|
bool bitsInit = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-14 00:32:54 +08:00
|
|
|
while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
|
|
|
|
QualType T = CE->getType();
|
|
|
|
|
|
|
|
if (!T->isIntegerType())
|
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-14 00:32:54 +08:00
|
|
|
uint64_t newBits = Ctx.getTypeSize(T);
|
|
|
|
if (!bitsInit || newBits < bits) {
|
|
|
|
bitsInit = true;
|
|
|
|
bits = newBits;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-14 00:32:54 +08:00
|
|
|
Ex = CE->getSubExpr();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We reached a non-cast. Is it a symbolic value?
|
|
|
|
QualType T = Ex->getType();
|
|
|
|
|
|
|
|
if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
|
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-19 07:58:37 +08:00
|
|
|
return state->getSVal(Ex);
|
2009-03-14 00:32:54 +08:00
|
|
|
}
|
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
|
2009-08-06 20:48:26 +08:00
|
|
|
GRBranchNodeBuilder& builder) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-16 06:29:00 +08:00
|
|
|
// Check for NULL conditions; e.g. "for(;;)"
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!Condition) {
|
2008-02-16 06:29:00 +08:00
|
|
|
builder.markInfeasible(false);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-11 11:54:24 +08:00
|
|
|
PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
|
|
|
|
Condition->getLocStart(),
|
|
|
|
"Error evaluating branch");
|
2009-08-27 09:39:13 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
const GRState* PrevState = builder.getState();
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = PrevState->getSVal(Condition);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-31 07:03:39 +08:00
|
|
|
switch (V.getBaseKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
2009-03-14 00:32:54 +08:00
|
|
|
case SVal::UnknownKind: {
|
|
|
|
if (Expr *Ex = dyn_cast<Expr>(Condition)) {
|
|
|
|
if (Ex->getType()->isIntegerType()) {
|
|
|
|
// Try to recover some path-sensitivity. Right now casts of symbolic
|
|
|
|
// integers that promote their values are currently not tracked well.
|
|
|
|
// If 'Condition' is such an expression, try and recover the
|
|
|
|
// underlying value and use that instead.
|
|
|
|
SVal recovered = RecoverCastedSymbol(getStateManager(),
|
|
|
|
builder.getState(), Condition,
|
|
|
|
getContext());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-14 00:32:54 +08:00
|
|
|
if (!recovered.isUnknown()) {
|
|
|
|
V = recovered;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:40:44 +08:00
|
|
|
builder.generateNode(MarkBranch(PrevState, Term, true), true);
|
|
|
|
builder.generateNode(MarkBranch(PrevState, Term, false), false);
|
2008-01-31 07:03:39 +08:00
|
|
|
return;
|
2009-03-14 00:32:54 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
case SVal::UndefinedKind: {
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* N = builder.generateNode(PrevState, true);
|
2008-01-31 07:03:39 +08:00
|
|
|
|
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
2008-02-28 17:25:22 +08:00
|
|
|
UndefBranches.insert(N);
|
2008-01-31 07:03:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-31 07:03:39 +08:00
|
|
|
builder.markInfeasible(false);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-01-31 07:03:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-01 04:27:50 +08:00
|
|
|
// Process the true branch.
|
2009-07-21 02:44:36 +08:00
|
|
|
if (builder.isFeasible(true)) {
|
|
|
|
if (const GRState *state = PrevState->assume(V, true))
|
|
|
|
builder.generateNode(MarkBranch(state, Term, true), true);
|
|
|
|
else
|
|
|
|
builder.markInfeasible(true);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Process the false branch.
|
2009-07-21 02:44:36 +08:00
|
|
|
if (builder.isFeasible(false)) {
|
|
|
|
if (const GRState *state = PrevState->assume(V, false))
|
|
|
|
builder.generateNode(MarkBranch(state, Term, false), false);
|
|
|
|
else
|
|
|
|
builder.markInfeasible(false);
|
|
|
|
}
|
2008-02-05 08:26:40 +08:00
|
|
|
}
|
|
|
|
|
2008-02-14 01:41:41 +08:00
|
|
|
/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
|
2008-02-13 08:24:44 +08:00
|
|
|
/// nodes by processing the 'effects' of a computed goto jump.
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& builder) {
|
2008-02-13 08:24:44 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
const GRState *state = builder.getState();
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = state->getSVal(builder.getTarget());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
// Three possibilities:
|
|
|
|
//
|
|
|
|
// (1) We know the computed label.
|
2008-02-28 17:25:22 +08:00
|
|
|
// (2) The label is NULL (or some other constant), or Undefined.
|
2008-02-13 08:24:44 +08:00
|
|
|
// (3) We have no clue about the label. Dispatch to all targets.
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
typedef GRIndirectGotoNodeBuilder::iterator iterator;
|
2008-02-13 08:24:44 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<loc::GotoLabel>(V)) {
|
|
|
|
LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
|
2008-02-14 01:27:37 +08:00
|
|
|
if (I.getLabel() == L) {
|
2009-02-13 09:45:31 +08:00
|
|
|
builder.generateNode(I, state);
|
2008-02-13 08:24:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
assert (false && "No block with label.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
|
2008-02-13 08:24:44 +08:00
|
|
|
// Dispatch to the first target and mark it as a sink.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* N = builder.generateNode(builder.begin(), state, true);
|
2008-02-28 17:25:22 +08:00
|
|
|
UndefBranches.insert(N);
|
2008-02-13 08:24:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
// This is really a catch-all. We don't support symbolics yet.
|
2009-04-24 01:49:43 +08:00
|
|
|
// FIXME: Implement dispatch for symbolic pointers.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
|
2009-02-13 09:45:31 +08:00
|
|
|
builder.generateNode(I, state);
|
2008-02-13 08:24:44 +08:00
|
|
|
}
|
2008-02-05 08:26:40 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred, ExplodedNodeSet& Dst) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-25 11:33:41 +08:00
|
|
|
assert (Ex == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(Pred);
|
2009-08-27 09:39:13 +08:00
|
|
|
SVal X = state->getSVal(Ex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
assert (X.isUndef());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 01:10:32 +08:00
|
|
|
Expr *SE = (Expr*) cast<UndefinedVal>(X).getData();
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(SE);
|
2009-08-27 09:39:13 +08:00
|
|
|
X = state->getSVal(SE);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// Make sure that we invalidate the previous binding.
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true));
|
2008-04-16 07:06:53 +08:00
|
|
|
}
|
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
|
|
|
|
/// nodes by processing the 'effects' of a switch statement.
|
2009-09-09 23:08:12 +08:00
|
|
|
void GRExprEngine::ProcessSwitch(GRSwitchNodeBuilder& builder) {
|
|
|
|
typedef GRSwitchNodeBuilder::iterator iterator;
|
|
|
|
const GRState* state = builder.getState();
|
2008-02-19 06:57:02 +08:00
|
|
|
Expr* CondE = builder.getCondition();
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal CondV = state->getSVal(CondE);
|
2008-02-14 07:08:21 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
if (CondV.isUndef()) {
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* N = builder.generateDefaultCaseNode(state, true);
|
2008-02-28 17:25:22 +08:00
|
|
|
UndefBranches.insert(N);
|
2008-02-14 07:08:21 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
const GRState* DefaultSt = state;
|
2009-06-19 06:57:13 +08:00
|
|
|
bool defaultIsFeasible = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
|
2008-02-14 07:08:21 +08:00
|
|
|
CaseStmt* Case = cast<CaseStmt>(I.getCase());
|
2009-01-17 09:54:16 +08:00
|
|
|
|
|
|
|
// Evaluate the LHS of the case value.
|
|
|
|
Expr::EvalResult V1;
|
2009-09-09 23:08:12 +08:00
|
|
|
bool b = Case->getLHS()->Evaluate(V1, getContext());
|
|
|
|
|
2009-01-17 09:54:16 +08:00
|
|
|
// Sanity checks. These go away in Release builds.
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(b && V1.Val.isInt() && !V1.HasSideEffects
|
2009-01-17 09:54:16 +08:00
|
|
|
&& "Case condition must evaluate to an integer constant.");
|
2009-09-09 23:08:12 +08:00
|
|
|
b = b; // silence unused variable warning
|
|
|
|
assert(V1.Val.getInt().getBitWidth() ==
|
2009-01-17 09:54:16 +08:00
|
|
|
getContext().getTypeSize(CondE->getType()));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
// Get the RHS of the case, if it exists.
|
2009-01-17 09:54:16 +08:00
|
|
|
Expr::EvalResult V2;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
if (Expr* E = Case->getRHS()) {
|
2009-01-17 09:54:16 +08:00
|
|
|
b = E->Evaluate(V2, getContext());
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(b && V2.Val.isInt() && !V2.HasSideEffects
|
2009-01-17 09:54:16 +08:00
|
|
|
&& "Case condition must evaluate to an integer constant.");
|
|
|
|
b = b; // silence unused variable warning
|
2008-02-14 07:08:21 +08:00
|
|
|
}
|
2008-03-18 06:17:56 +08:00
|
|
|
else
|
|
|
|
V2 = V1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
// FIXME: Eventually we should replace the logic below with a range
|
|
|
|
// comparison, rather than concretize the values within the range.
|
2008-02-22 02:02:17 +08:00
|
|
|
// This should be easy once we have "ranges" for NonLVals.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-18 06:17:56 +08:00
|
|
|
do {
|
2009-09-09 23:08:12 +08:00
|
|
|
nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt()));
|
2009-05-20 17:00:16 +08:00
|
|
|
SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal,
|
2009-03-26 11:35:11 +08:00
|
|
|
getContext().IntTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Now "assume" that the case matches.
|
2009-06-19 06:57:13 +08:00
|
|
|
if (const GRState* stateNew = state->assume(Res, true)) {
|
|
|
|
builder.generateCaseStmtNode(I, stateNew);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
// If CondV evaluates to a constant, then we know that this
|
|
|
|
// is the *only* case that we can take, so stop evaluating the
|
|
|
|
// others.
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<nonloc::ConcreteInt>(CondV))
|
2008-02-14 07:08:21 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
// Now "assume" that the case doesn't match. Add this state
|
|
|
|
// to the default state (if it is feasible).
|
2009-06-19 06:57:13 +08:00
|
|
|
if (const GRState *stateNew = DefaultSt->assume(Res, false)) {
|
|
|
|
defaultIsFeasible = true;
|
|
|
|
DefaultSt = stateNew;
|
2008-04-23 13:03:18 +08:00
|
|
|
}
|
2008-02-14 07:08:21 +08:00
|
|
|
|
2008-03-18 06:17:56 +08:00
|
|
|
// Concretize the next value in the range.
|
2009-01-17 09:54:16 +08:00
|
|
|
if (V1.Val.getInt() == V2.Val.getInt())
|
2008-03-18 06:17:56 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 09:54:16 +08:00
|
|
|
++V1.Val.getInt();
|
|
|
|
assert (V1.Val.getInt() <= V2.Val.getInt());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-18 06:17:56 +08:00
|
|
|
} while (true);
|
2008-02-14 07:08:21 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
// If we reach here, than we know that the default branch is
|
2009-09-09 23:08:12 +08:00
|
|
|
// possible.
|
2009-06-19 06:57:13 +08:00
|
|
|
if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt);
|
2008-02-14 07:08:21 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer functions: logical operations ('&&', '||').
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-14 07:08:21 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 01:10:32 +08:00
|
|
|
assert(B->getOpcode() == BinaryOperator::LAnd ||
|
|
|
|
B->getOpcode() == BinaryOperator::LOr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-25 11:33:41 +08:00
|
|
|
assert(B == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(Pred);
|
2009-08-27 09:39:13 +08:00
|
|
|
SVal X = state->getSVal(B);
|
2009-06-20 01:10:32 +08:00
|
|
|
assert(X.isUndef());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 01:10:32 +08:00
|
|
|
assert(Ex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
if (Ex == B->getRHS()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-27 09:39:13 +08:00
|
|
|
X = state->getSVal(Ex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
// Handle undefined values.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
if (X.isUndef()) {
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, B, Pred, state->BindExpr(B, X));
|
2008-02-27 03:40:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
// We took the RHS. Because the value of the '&&' or '||' expression must
|
|
|
|
// evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
|
|
|
|
// or 1. Alternatively, we could take a lazy approach, and calculate this
|
|
|
|
// value later when necessary. We don't have the machinery in place for
|
|
|
|
// this right now, and since most logical expressions are used for branches,
|
2009-09-09 23:08:12 +08:00
|
|
|
// the payoff is not likely to be large. Instead, we do eager evaluation.
|
2009-06-19 06:57:13 +08:00
|
|
|
if (const GRState *newState = state->assume(X, true))
|
2009-09-09 23:08:12 +08:00
|
|
|
MakeNode(Dst, B, Pred,
|
2009-08-28 06:17:37 +08:00
|
|
|
newState->BindExpr(B, ValMgr.makeIntVal(1U, B->getType())));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
if (const GRState *newState = state->assume(X, false))
|
2009-09-09 23:08:12 +08:00
|
|
|
MakeNode(Dst, B, Pred,
|
2009-08-28 06:17:37 +08:00
|
|
|
newState->BindExpr(B, ValMgr.makeIntVal(0U, B->getType())));
|
2008-02-05 08:26:40 +08:00
|
|
|
}
|
|
|
|
else {
|
2008-02-27 03:05:15 +08:00
|
|
|
// We took the LHS expression. Depending on whether we are '&&' or
|
|
|
|
// '||' we know what the value of the expression is via properties of
|
|
|
|
// the short-circuiting.
|
2009-09-09 23:08:12 +08:00
|
|
|
X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U,
|
2009-06-23 17:02:15 +08:00
|
|
|
B->getType());
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, B, Pred, state->BindExpr(B, X));
|
2008-02-27 03:05:15 +08:00
|
|
|
}
|
2008-01-30 07:32:35 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-17 02:39:06 +08:00
|
|
|
// Transfer functions: Loads and stores.
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-16 07:55:06 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void GRExprEngine::VisitDeclRefExpr(DeclRefExpr *Ex, ExplodedNode *Pred,
|
2009-08-22 06:28:32 +08:00
|
|
|
ExplodedNodeSet &Dst, bool asLValue) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(Pred);
|
2008-10-16 14:09:51 +08:00
|
|
|
|
2008-10-22 00:13:35 +08:00
|
|
|
const NamedDecl* D = Ex->getDecl();
|
2008-10-16 14:09:51 +08:00
|
|
|
|
|
|
|
if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
|
2009-08-22 06:28:32 +08:00
|
|
|
SVal V = state->getLValue(VD, Pred->getLocationContext());
|
2008-10-17 10:20:14 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
if (asLValue)
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
|
2009-05-08 02:27:16 +08:00
|
|
|
ProgramPoint::PostLValueKind);
|
2008-10-16 14:09:51 +08:00
|
|
|
else
|
2009-02-13 09:45:31 +08:00
|
|
|
EvalLoad(Dst, Ex, Pred, state, V);
|
2008-10-16 14:09:51 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
|
|
|
|
assert(!asLValue && "EnumConstantDecl does not have lvalue.");
|
|
|
|
|
2009-06-23 14:13:19 +08:00
|
|
|
SVal V = ValMgr.makeIntVal(ED->getInitVal());
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V));
|
2008-10-16 14:09:51 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
|
2008-11-15 10:35:08 +08:00
|
|
|
assert(asLValue);
|
2009-04-20 13:24:46 +08:00
|
|
|
SVal V = ValMgr.getFunctionPointer(FD);
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V),
|
2009-05-08 02:27:16 +08:00
|
|
|
ProgramPoint::PostLValueKind);
|
2008-10-16 14:09:51 +08:00
|
|
|
return;
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
assert (false &&
|
|
|
|
"ValueDecl support for this ValueDecl not implemented.");
|
2008-02-07 12:16:04 +08:00
|
|
|
}
|
|
|
|
|
2008-04-22 12:56:29 +08:00
|
|
|
/// VisitArraySubscriptExpr - Transfer function for array accesses
|
2009-09-09 23:08:12 +08:00
|
|
|
void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst, bool asLValue){
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-22 12:56:29 +08:00
|
|
|
Expr* Base = A->getBase()->IgnoreParens();
|
2008-04-30 07:24:44 +08:00
|
|
|
Expr* Idx = A->getIdx()->IgnoreParens();
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-24 10:23:11 +08:00
|
|
|
if (Base->getType()->isVectorType()) {
|
|
|
|
// For vector types get its lvalue.
|
|
|
|
// FIXME: This may not be correct. Is the rvalue of a vector its location?
|
|
|
|
// In fact, I think this is just a hack. We need to get the right
|
|
|
|
// semantics.
|
|
|
|
VisitLValue(Base, Pred, Tmp);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2009-02-24 10:23:11 +08:00
|
|
|
Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
|
|
|
|
ExplodedNodeSet Tmp2;
|
2008-10-17 08:51:01 +08:00
|
|
|
Visit(Idx, *I1, Tmp2); // Evaluate the index.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I2=Tmp2.begin(),E2=Tmp2.end();I2!=E2; ++I2) {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I2);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = state->getLValue(A->getType(), state->getSVal(Base),
|
|
|
|
state->getSVal(Idx));
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
if (asLValue)
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, A, *I2, state->BindExpr(A, V),
|
2009-05-08 02:27:16 +08:00
|
|
|
ProgramPoint::PostLValueKind);
|
2008-04-30 07:24:44 +08:00
|
|
|
else
|
2009-02-13 09:45:31 +08:00
|
|
|
EvalLoad(Dst, A, *I2, state, V);
|
2008-04-30 07:24:44 +08:00
|
|
|
}
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
2008-04-22 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
2008-04-22 07:43:38 +08:00
|
|
|
/// VisitMemberExpr - Transfer function for member expressions.
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::VisitMemberExpr(MemberExpr* M, ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst, bool asLValue) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-22 07:43:38 +08:00
|
|
|
Expr* Base = M->getBase()->IgnoreParens();
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (M->isArrow())
|
2008-10-18 11:28:48 +08:00
|
|
|
Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
|
|
|
|
else
|
|
|
|
VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-21 07:49:58 +08:00
|
|
|
FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl());
|
|
|
|
if (!Field) // FIXME: skipping member expressions for non-fields
|
|
|
|
return;
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2008-10-17 08:51:01 +08:00
|
|
|
// FIXME: Should we insert some assumption logic in here to determine
|
|
|
|
// if "Base" is a valid piece of memory? Before we put this assumption
|
2008-12-21 07:49:58 +08:00
|
|
|
// later when using FieldOffset lvals (which we no longer have).
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal L = state->getLValue(state->getSVal(Base), Field);
|
2008-10-17 08:51:01 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
if (asLValue)
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, M, *I, state->BindExpr(M, L),
|
2009-05-08 02:27:16 +08:00
|
|
|
ProgramPoint::PostLValueKind);
|
2008-10-16 14:09:51 +08:00
|
|
|
else
|
2009-02-13 09:45:31 +08:00
|
|
|
EvalLoad(Dst, M, *I, state, L);
|
2008-04-22 07:43:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
/// EvalBind - Handle the semantics of binding a value to a specific location.
|
|
|
|
/// This method is used by EvalStore and (soon) VisitDeclStmt, and others.
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::EvalBind(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred,
|
2009-07-22 05:03:30 +08:00
|
|
|
const GRState* state, SVal location, SVal Val) {
|
2009-02-13 09:45:31 +08:00
|
|
|
|
2009-02-14 09:43:44 +08:00
|
|
|
const GRState* newState = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 09:43:44 +08:00
|
|
|
if (location.isUnknown()) {
|
|
|
|
// We know that the new state will be the same as the old state since
|
|
|
|
// the location of the binding is "unknown". Consequently, there
|
|
|
|
// is no reason to just create a new node.
|
|
|
|
newState = state;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We are binding to a value other than 'unknown'. Perform the binding
|
|
|
|
// using the StoreManager.
|
2009-07-22 05:03:30 +08:00
|
|
|
newState = state->bindLoc(cast<Loc>(location), Val);
|
2009-02-14 09:43:44 +08:00
|
|
|
}
|
2009-02-13 09:45:31 +08:00
|
|
|
|
2009-02-14 09:43:44 +08:00
|
|
|
// The next thing to do is check if the GRTransferFuncs object wants to
|
|
|
|
// update the state based on the new binding. If the GRTransferFunc object
|
|
|
|
// doesn't do anything, just auto-propagate the current state.
|
|
|
|
GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex,
|
|
|
|
newState != state);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 09:43:44 +08:00
|
|
|
getTF().EvalBind(BuilderRef, location, Val);
|
2009-02-13 09:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EvalStore - Handle the semantics of a store via an assignment.
|
|
|
|
/// @param Dst The node set to store generated state nodes
|
|
|
|
/// @param Ex The expression representing the location of the store
|
|
|
|
/// @param state The current simulation state
|
|
|
|
/// @param location The location to store the value
|
|
|
|
/// @param Val The value to be stored
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred,
|
2009-04-11 08:11:10 +08:00
|
|
|
const GRState* state, SVal location, SVal Val,
|
|
|
|
const void *tag) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-17 02:39:06 +08:00
|
|
|
assert (Builder && "GRStmtNodeBuilder must be defined.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Evaluate the location (checks for bad dereferences).
|
2009-04-11 08:11:10 +08:00
|
|
|
Pred = EvalLocation(Ex, Pred, state, location, tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
if (!Pred)
|
2008-04-30 05:04:26 +08:00
|
|
|
return;
|
2008-04-19 04:35:30 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
assert (!location.isUndef());
|
2009-02-13 09:45:31 +08:00
|
|
|
state = GetState(Pred);
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Proceed with the store.
|
2009-02-13 09:45:31 +08:00
|
|
|
SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
|
2009-04-11 08:11:10 +08:00
|
|
|
SaveAndRestore<const void*> OldTag(Builder->Tag);
|
|
|
|
Builder->PointKind = ProgramPoint::PostStoreKind;
|
|
|
|
Builder->Tag = tag;
|
2009-02-13 09:45:31 +08:00
|
|
|
EvalBind(Dst, Ex, Pred, state, location, Val);
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::EvalLoad(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred,
|
2009-04-11 08:11:10 +08:00
|
|
|
const GRState* state, SVal location,
|
|
|
|
const void *tag) {
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Evaluate the location (checks for bad dereferences).
|
2009-04-11 08:11:10 +08:00
|
|
|
Pred = EvalLocation(Ex, Pred, state, location, tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
if (!Pred)
|
2008-04-30 05:04:26 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
state = GetState(Pred);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Proceed with the load.
|
2008-08-29 02:43:46 +08:00
|
|
|
ProgramPoint::Kind K = ProgramPoint::PostLoadKind;
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// FIXME: Currently symbolic analysis "generates" new symbols
|
|
|
|
// for the contents of values. We need a better approach.
|
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
if (location.isUnknown()) {
|
2008-04-30 12:23:07 +08:00
|
|
|
// This is important. We must nuke the old binding.
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, UnknownVal()),
|
2009-08-25 14:51:30 +08:00
|
|
|
K, tag);
|
2008-04-30 12:23:07 +08:00
|
|
|
}
|
2008-11-28 16:34:30 +08:00
|
|
|
else {
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = state->getSVal(cast<Loc>(location), Ex->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-11 12:38:49 +08:00
|
|
|
// Casts can create weird scenarios where a location must be implicitly
|
|
|
|
// converted to something else. For example:
|
|
|
|
//
|
|
|
|
// void *x;
|
|
|
|
// int *y = (int*) &x; // void** -> int* cast.
|
|
|
|
// invalidate(y); // 'x' now binds to a symbolic region
|
|
|
|
// int z = *y;
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2009-07-14 09:12:46 +08:00
|
|
|
//if (isa<Loc>(V) && !Loc::IsLocType(Ex->getType())) {
|
|
|
|
// V = EvalCast(V, Ex->getType());
|
|
|
|
//}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V), K, tag);
|
2008-11-28 16:34:30 +08:00
|
|
|
}
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, Expr* StoreE,
|
|
|
|
ExplodedNode* Pred, const GRState* state,
|
2009-08-06 20:48:26 +08:00
|
|
|
SVal location, SVal Val, const void *tag) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet TmpDst;
|
2009-04-11 08:11:10 +08:00
|
|
|
EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag);
|
2008-09-20 09:50:34 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
|
2009-04-11 08:11:10 +08:00
|
|
|
MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag);
|
2008-09-20 09:50:34 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* GRExprEngine::EvalLocation(Stmt* Ex, ExplodedNode* Pred,
|
|
|
|
const GRState* state, SVal location,
|
|
|
|
const void *tag) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
SaveAndRestore<const void*> OldTag(Builder->Tag);
|
|
|
|
Builder->Tag = tag;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Check for loads/stores from/to undefined values.
|
2008-04-30 05:04:26 +08:00
|
|
|
if (location.isUndef()) {
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* N =
|
2009-02-13 09:45:31 +08:00
|
|
|
Builder->generateNode(Ex, state, Pred,
|
2008-12-17 06:02:27 +08:00
|
|
|
ProgramPoint::PostUndefLocationCheckFailedKind);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
UndefDeref.insert(N);
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
return 0;
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Check for loads/stores from/to unknown locations. Treat as No-Ops.
|
|
|
|
if (location.isUnknown())
|
2008-12-17 06:02:27 +08:00
|
|
|
return Pred;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// During a load, one of two possible situations arise:
|
|
|
|
// (1) A crash, because the location (pointer) was NULL.
|
|
|
|
// (2) The location (pointer) is not NULL, and the dereference works.
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2008-04-30 05:04:26 +08:00
|
|
|
// We add these assumptions.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
Loc LV = cast<Loc>(location);
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// "Assume" that the pointer is not NULL.
|
2009-06-19 06:57:13 +08:00
|
|
|
const GRState *StNotNull = state->assume(LV, true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// "Assume" that the pointer is NULL.
|
2009-06-19 06:57:13 +08:00
|
|
|
const GRState *StNull = state->assume(LV, false);
|
2009-04-03 15:33:13 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (StNull) {
|
2008-09-19 07:09:54 +08:00
|
|
|
// Use the Generic Data Map to mark in the state what lval was null.
|
2008-10-17 13:57:07 +08:00
|
|
|
const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV);
|
2009-06-18 09:23:53 +08:00
|
|
|
StNull = StNull->set<GRState::NullDerefTag>(PersistentLV);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// We don't use "MakeNode" here because the node will be a sink
|
|
|
|
// and we have no intention of processing it later.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* NullNode =
|
2009-09-09 23:08:12 +08:00
|
|
|
Builder->generateNode(Ex, StNull, Pred,
|
2008-12-17 06:02:27 +08:00
|
|
|
ProgramPoint::PostNullCheckFailedKind);
|
2008-06-18 13:34:07 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (NullNode) {
|
|
|
|
NullNode->markAsSink();
|
2009-06-19 06:57:13 +08:00
|
|
|
if (StNotNull) ImplicitNullDeref.insert(NullNode);
|
2008-04-30 05:04:26 +08:00
|
|
|
else ExplicitNullDeref.insert(NullNode);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
if (!StNotNull)
|
|
|
|
return NULL;
|
2008-11-08 11:45:42 +08:00
|
|
|
|
2009-08-01 13:59:39 +08:00
|
|
|
// FIXME: Temporarily disable out-of-bounds checking until we make
|
|
|
|
// the logic reflect recent changes to CastRegion and friends.
|
|
|
|
#if 0
|
2008-11-08 11:45:42 +08:00
|
|
|
// Check for out-of-bound array access.
|
2008-12-17 06:02:27 +08:00
|
|
|
if (isa<loc::MemRegionVal>(LV)) {
|
2008-11-08 11:45:42 +08:00
|
|
|
const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
|
|
|
|
if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
|
|
|
|
// Get the index of the accessed element.
|
|
|
|
SVal Idx = ER->getIndex();
|
|
|
|
// Get the extent of the array.
|
2008-11-24 15:02:06 +08:00
|
|
|
SVal NumElements = getStoreManager().getSizeInElements(StNotNull,
|
|
|
|
ER->getSuperRegion());
|
2008-11-08 11:45:42 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
const GRState * StInBound = StNotNull->assumeInBound(Idx, NumElements,
|
2009-06-19 06:57:13 +08:00
|
|
|
true);
|
2009-09-09 23:08:12 +08:00
|
|
|
const GRState* StOutBound = StNotNull->assumeInBound(Idx, NumElements,
|
2009-06-19 06:57:13 +08:00
|
|
|
false);
|
2008-11-08 11:45:42 +08:00
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
if (StOutBound) {
|
2008-12-17 06:02:27 +08:00
|
|
|
// Report warning. Make sink node manually.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* OOBNode =
|
2008-12-17 06:02:27 +08:00
|
|
|
Builder->generateNode(Ex, StOutBound, Pred,
|
|
|
|
ProgramPoint::PostOutOfBoundsCheckFailedKind);
|
2008-11-23 13:52:28 +08:00
|
|
|
|
|
|
|
if (OOBNode) {
|
|
|
|
OOBNode->markAsSink();
|
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
if (StInBound)
|
2008-11-23 13:52:28 +08:00
|
|
|
ImplicitOOBMemAccesses.insert(OOBNode);
|
|
|
|
else
|
|
|
|
ExplicitOOBMemAccesses.insert(OOBNode);
|
|
|
|
}
|
2008-11-22 21:21:46 +08:00
|
|
|
}
|
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
if (!StInBound)
|
|
|
|
return NULL;
|
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
StNotNull = StInBound;
|
2008-11-08 11:45:42 +08:00
|
|
|
}
|
|
|
|
}
|
2009-08-01 13:59:39 +08:00
|
|
|
#endif
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
// Generate a new node indicating the checks succeed.
|
|
|
|
return Builder->generateNode(Ex, StNotNull, Pred,
|
|
|
|
ProgramPoint::PostLocationChecksSucceedKind);
|
2008-04-17 02:39:06 +08:00
|
|
|
}
|
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function: OSAtomics.
|
|
|
|
//
|
|
|
|
// FIXME: Eventually refactor into a more "plugin" infrastructure.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Mac OS X:
|
|
|
|
// http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3
|
|
|
|
// atomic.3.html
|
|
|
|
//
|
2009-08-06 09:32:16 +08:00
|
|
|
static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet& Dst,
|
2009-04-11 08:11:10 +08:00
|
|
|
GRExprEngine& Engine,
|
2009-08-06 20:48:26 +08:00
|
|
|
GRStmtNodeBuilder& Builder,
|
2009-09-09 23:08:12 +08:00
|
|
|
CallExpr* CE, SVal L,
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode* Pred) {
|
2009-04-11 08:11:10 +08:00
|
|
|
|
|
|
|
// Not enough arguments to match OSAtomicCompareAndSwap?
|
|
|
|
if (CE->getNumArgs() != 3)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
ASTContext &C = Engine.getContext();
|
|
|
|
Expr *oldValueExpr = CE->getArg(0);
|
|
|
|
QualType oldValueType = C.getCanonicalType(oldValueExpr->getType());
|
|
|
|
|
|
|
|
Expr *newValueExpr = CE->getArg(1);
|
|
|
|
QualType newValueType = C.getCanonicalType(newValueExpr->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// Do the types of 'oldValue' and 'newValue' match?
|
|
|
|
if (oldValueType != newValueType)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
Expr *theValueExpr = CE->getArg(2);
|
2009-08-01 13:59:39 +08:00
|
|
|
const PointerType *theValueType =
|
|
|
|
theValueExpr->getType()->getAs<PointerType>();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// theValueType not a pointer?
|
|
|
|
if (!theValueType)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
QualType theValueTypePointee =
|
|
|
|
C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// The pointee must match newValueType and oldValueType.
|
|
|
|
if (theValueTypePointee != newValueType)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
static unsigned magic_load = 0;
|
|
|
|
static unsigned magic_store = 0;
|
|
|
|
|
|
|
|
const void *OSAtomicLoadTag = &magic_load;
|
|
|
|
const void *OSAtomicStoreTag = &magic_store;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// Load 'theValue'.
|
|
|
|
const GRState *state = Pred->getState();
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2009-06-19 07:58:37 +08:00
|
|
|
SVal location = state->getSVal(theValueExpr);
|
2009-04-11 08:11:10 +08:00
|
|
|
Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag);
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end();
|
2009-04-11 08:11:10 +08:00
|
|
|
I != E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode *N = *I;
|
2009-04-11 08:11:10 +08:00
|
|
|
const GRState *stateLoad = N->getState();
|
2009-06-19 06:57:13 +08:00
|
|
|
SVal theValueVal = stateLoad->getSVal(theValueExpr);
|
|
|
|
SVal oldValueVal = stateLoad->getSVal(oldValueExpr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-21 04:38:59 +08:00
|
|
|
// FIXME: Issue an error.
|
|
|
|
if (theValueVal.isUndef() || oldValueVal.isUndef()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
return false;
|
2009-07-21 04:38:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 02:44:25 +08:00
|
|
|
SValuator &SVator = Engine.getSValuator();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// Perform the comparison.
|
2009-08-26 02:44:25 +08:00
|
|
|
SVal Cmp = SVator.EvalBinOp(stateLoad, BinaryOperator::EQ, theValueVal,
|
2009-06-26 08:05:51 +08:00
|
|
|
oldValueVal, Engine.getContext().IntTy);
|
2009-06-19 06:57:13 +08:00
|
|
|
|
|
|
|
const GRState *stateEqual = stateLoad->assume(Cmp, true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// Were they equal?
|
2009-06-19 06:57:13 +08:00
|
|
|
if (stateEqual) {
|
2009-04-11 08:11:10 +08:00
|
|
|
// Perform the store.
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNodeSet TmpStore;
|
2009-07-22 05:03:30 +08:00
|
|
|
SVal val = stateEqual->getSVal(newValueExpr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Handle implicit value casts.
|
|
|
|
if (const TypedRegion *R =
|
|
|
|
dyn_cast_or_null<TypedRegion>(location.getAsRegion())) {
|
2009-08-26 02:44:25 +08:00
|
|
|
llvm::tie(state, val) = SVator.EvalCast(val, state, R->getValueType(C),
|
|
|
|
newValueExpr->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location,
|
2009-07-22 05:03:30 +08:00
|
|
|
val, OSAtomicStoreTag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// Now bind the result of the comparison.
|
2009-08-06 09:32:16 +08:00
|
|
|
for (ExplodedNodeSet::iterator I2 = TmpStore.begin(),
|
2009-04-11 08:11:10 +08:00
|
|
|
E2 = TmpStore.end(); I2 != E2; ++I2) {
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode *predNew = *I2;
|
2009-04-11 08:11:10 +08:00
|
|
|
const GRState *stateNew = predNew->getState();
|
|
|
|
SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType());
|
2009-08-28 06:17:37 +08:00
|
|
|
Engine.MakeNode(Dst, CE, predNew, stateNew->BindExpr(CE, Res));
|
2009-04-11 08:11:10 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// Were they not equal?
|
2009-06-19 06:57:13 +08:00
|
|
|
if (const GRState *stateNotEqual = stateLoad->assume(Cmp, false)) {
|
2009-04-11 08:11:10 +08:00
|
|
|
SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType());
|
2009-08-28 06:17:37 +08:00
|
|
|
Engine.MakeNode(Dst, CE, N, stateNotEqual->BindExpr(CE, Res));
|
2009-04-11 08:11:10 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
static bool EvalOSAtomic(ExplodedNodeSet& Dst,
|
2009-04-11 08:11:10 +08:00
|
|
|
GRExprEngine& Engine,
|
2009-08-06 20:48:26 +08:00
|
|
|
GRStmtNodeBuilder& Builder,
|
2009-04-11 08:11:10 +08:00
|
|
|
CallExpr* CE, SVal L,
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode* Pred) {
|
2009-04-20 13:24:46 +08:00
|
|
|
const FunctionDecl* FD = L.getAsFunctionDecl();
|
|
|
|
if (!FD)
|
2009-04-11 08:11:10 +08:00
|
|
|
return false;
|
2009-04-20 13:24:46 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
const char *FName = FD->getNameAsCString();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// Check for compare and swap.
|
2009-04-11 08:54:13 +08:00
|
|
|
if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 ||
|
|
|
|
strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0)
|
2009-04-11 08:11:10 +08:00
|
|
|
return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred);
|
2009-04-11 08:54:13 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// FIXME: Other atomics.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function: Function calls.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-04 10:13:36 +08:00
|
|
|
static void MarkNoReturnFunction(const FunctionDecl *FD, CallExpr *CE,
|
2009-09-09 23:08:12 +08:00
|
|
|
const GRState *state,
|
2009-09-04 10:13:36 +08:00
|
|
|
GRStmtNodeBuilder *Builder) {
|
2009-09-04 10:17:35 +08:00
|
|
|
if (!FD)
|
|
|
|
return;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (FD->getAttr<NoReturnAttr>() ||
|
2009-09-04 10:13:36 +08:00
|
|
|
FD->getAttr<AnalyzerNoReturnAttr>())
|
|
|
|
Builder->BuildSinks = true;
|
|
|
|
else {
|
|
|
|
// HACK: Some functions are not marked noreturn, and don't return.
|
|
|
|
// Here are a few hardwired ones. If this takes too long, we can
|
|
|
|
// potentially cache these results.
|
|
|
|
const char* s = FD->getIdentifier()->getName();
|
|
|
|
unsigned n = strlen(s);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
switch (n) {
|
|
|
|
default:
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
case 4:
|
|
|
|
if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
|
|
|
|
else if (!memcmp(s, "error", 5)) {
|
|
|
|
if (CE->getNumArgs() > 0) {
|
|
|
|
SVal X = state->getSVal(*CE->arg_begin());
|
|
|
|
// FIXME: use Assume to inspect the possible symbolic value of
|
|
|
|
// X. Also check the specific signature of error().
|
|
|
|
nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
|
|
|
|
if (CI && CI->getValue() != 0)
|
|
|
|
Builder->BuildSinks = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
if (!memcmp(s, "Assert", 6)) {
|
|
|
|
Builder->BuildSinks = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
// FIXME: This is just a wrapper around throwing an exception.
|
|
|
|
// Eventually inter-procedural analysis should handle this easily.
|
|
|
|
if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true;
|
|
|
|
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
case 7:
|
|
|
|
if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
case 8:
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!memcmp(s ,"db_error", 8) ||
|
2009-09-04 10:13:36 +08:00
|
|
|
!memcmp(s, "__assert", 8))
|
|
|
|
Builder->BuildSinks = true;
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
case 12:
|
|
|
|
if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
case 13:
|
|
|
|
if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
case 14:
|
|
|
|
if (!memcmp(s, "dtrace_assfail", 14) ||
|
|
|
|
!memcmp(s, "yy_fatal_error", 14))
|
|
|
|
Builder->BuildSinks = true;
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
case 26:
|
|
|
|
if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
|
|
|
|
!memcmp(s, "_DTAssertionFailureHandler", 26) ||
|
|
|
|
!memcmp(s, "_TSAssertionFailureHandler", 26))
|
|
|
|
Builder->BuildSinks = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-04 10:13:36 +08:00
|
|
|
}
|
|
|
|
}
|
2009-04-11 08:11:10 +08:00
|
|
|
|
2009-09-05 13:00:57 +08:00
|
|
|
bool GRExprEngine::EvalBuiltinFunction(const FunctionDecl *FD, CallExpr *CE,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
if (!FD)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-05 13:00:57 +08:00
|
|
|
unsigned id = FD->getBuiltinID(getContext());
|
|
|
|
if (!id)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const GRState *state = Pred->getState();
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case Builtin::BI__builtin_expect: {
|
|
|
|
// For __builtin_expect, just return the value of the subexpression.
|
2009-09-09 23:08:12 +08:00
|
|
|
assert (CE->arg_begin() != CE->arg_end());
|
2009-09-05 13:00:57 +08:00
|
|
|
SVal X = state->getSVal(*(CE->arg_begin()));
|
|
|
|
MakeNode(Dst, CE, Pred, state->BindExpr(CE, X));
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-05 13:00:57 +08:00
|
|
|
case Builtin::BI__builtin_alloca: {
|
|
|
|
// FIXME: Refactor into StoreManager itself?
|
|
|
|
MemRegionManager& RM = getStateManager().getRegionManager();
|
|
|
|
const MemRegion* R =
|
|
|
|
RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-05 13:00:57 +08:00
|
|
|
// Set the extent of the region in bytes. This enables us to use the
|
|
|
|
// SVal of the argument directly. If we save the extent in bits, we
|
|
|
|
// cannot represent values like symbol*8.
|
|
|
|
SVal Extent = state->getSVal(*(CE->arg_begin()));
|
|
|
|
state = getStoreManager().setExtent(state, R, Extent);
|
|
|
|
MakeNode(Dst, CE, Pred, state->BindExpr(CE, loc::MemRegionVal(R)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void GRExprEngine::EvalCall(ExplodedNodeSet& Dst, CallExpr* CE, SVal L,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred) {
|
2009-04-11 08:11:10 +08:00
|
|
|
assert (Builder && "GRStmtNodeBuilder must be defined.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
// FIXME: Allow us to chain together transfer functions.
|
|
|
|
if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred))
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred);
|
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::VisitCall(CallExpr* CE, ExplodedNode* Pred,
|
2008-02-22 02:02:17 +08:00
|
|
|
CallExpr::arg_iterator AI,
|
|
|
|
CallExpr::arg_iterator AE,
|
2009-09-09 23:08:12 +08:00
|
|
|
ExplodedNodeSet& Dst) {
|
2008-10-28 08:22:11 +08:00
|
|
|
// Determine the type of function we're calling (if available).
|
2009-02-27 07:50:07 +08:00
|
|
|
const FunctionProtoType *Proto = NULL;
|
2008-10-28 08:22:11 +08:00
|
|
|
QualType FnType = CE->getCallee()->IgnoreParens()->getType();
|
2009-07-30 05:53:49 +08:00
|
|
|
if (const PointerType *FnTypePtr = FnType->getAs<PointerType>())
|
2009-02-27 07:50:07 +08:00
|
|
|
Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType();
|
2008-10-28 08:22:11 +08:00
|
|
|
|
|
|
|
VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
|
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::VisitCallRec(CallExpr* CE, ExplodedNode* Pred,
|
2008-10-28 08:22:11 +08:00
|
|
|
CallExpr::arg_iterator AI,
|
|
|
|
CallExpr::arg_iterator AE,
|
2009-09-09 23:08:12 +08:00
|
|
|
ExplodedNodeSet& Dst,
|
|
|
|
const FunctionProtoType *Proto,
|
2008-10-28 08:22:11 +08:00
|
|
|
unsigned ParamIdx) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
// Process the arguments.
|
|
|
|
if (AI != AE) {
|
2008-10-28 08:22:11 +08:00
|
|
|
// If the call argument is being bound to a reference parameter,
|
|
|
|
// visit it as an lvalue, not an rvalue.
|
|
|
|
bool VisitAsLvalue = false;
|
|
|
|
if (Proto && ParamIdx < Proto->getNumArgs())
|
|
|
|
VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType();
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
ExplodedNodeSet DstTmp;
|
2008-10-28 08:22:11 +08:00
|
|
|
if (VisitAsLvalue)
|
2009-09-09 23:08:12 +08:00
|
|
|
VisitLValue(*AI, Pred, DstTmp);
|
2008-10-28 08:22:11 +08:00
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
Visit(*AI, Pred, DstTmp);
|
2008-02-22 02:02:17 +08:00
|
|
|
++AI;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE;
|
|
|
|
++DI)
|
2008-10-28 08:22:11 +08:00
|
|
|
VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-19 09:44:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here we have processed all of the arguments. Evaluate
|
|
|
|
// the callee expression.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet DstTmp;
|
2008-04-24 04:12:28 +08:00
|
|
|
Expr* Callee = CE->getCallee()->IgnoreParens();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 05:43:51 +08:00
|
|
|
{ // Enter new scope to make the lifetime of 'DstTmp2' bounded.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet DstTmp2;
|
2009-07-23 05:43:51 +08:00
|
|
|
Visit(Callee, Pred, DstTmp2);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 05:43:51 +08:00
|
|
|
// Perform the previsit of the CallExpr, storing the results in DstTmp.
|
|
|
|
CheckerVisit(CE, DstTmp, DstTmp2, true);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-19 09:44:53 +08:00
|
|
|
// Finally, evaluate the function call.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end();
|
2009-09-05 13:00:57 +08:00
|
|
|
DI != DE; ++DI) {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*DI);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal L = state->getSVal(Callee);
|
2008-02-19 09:44:53 +08:00
|
|
|
|
2008-03-04 00:47:31 +08:00
|
|
|
// FIXME: Add support for symbolic function calls (calls involving
|
|
|
|
// function pointer values that are symbolic).
|
2009-09-02 16:10:35 +08:00
|
|
|
|
2008-03-06 05:15:02 +08:00
|
|
|
// Check for the "noreturn" attribute.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-06 05:15:02 +08:00
|
|
|
SaveAndRestore<bool> OldSink(Builder->BuildSinks);
|
2009-04-20 13:24:46 +08:00
|
|
|
const FunctionDecl* FD = L.getAsFunctionDecl();
|
2009-09-04 10:17:35 +08:00
|
|
|
|
|
|
|
MarkNoReturnFunction(FD, CE, state, Builder);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-06 05:15:02 +08:00
|
|
|
// Evaluate the call.
|
2009-09-05 14:46:12 +08:00
|
|
|
if (EvalBuiltinFunction(FD, CE, *DI, Dst))
|
2009-09-05 13:00:57 +08:00
|
|
|
continue;
|
2008-04-24 04:12:28 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Dispatch to the plug-in transfer function.
|
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
unsigned size = Dst.size();
|
|
|
|
SaveOr OldHasGen(Builder->HasGeneratedNode);
|
|
|
|
EvalCall(Dst, CE, L, *DI);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
// Handle the case where no nodes where generated. Auto-generate that
|
|
|
|
// contains the updated state if we aren't generating sinks.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
if (!Builder->BuildSinks && Dst.size() == size &&
|
|
|
|
!Builder->HasGeneratedNode)
|
2009-02-13 09:45:31 +08:00
|
|
|
MakeNode(Dst, CE, *DI, state);
|
2008-04-16 07:06:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-17 08:03:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function: Objective-C ivar references.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-02-26 06:32:02 +08:00
|
|
|
|
2009-03-01 04:50:43 +08:00
|
|
|
static std::pair<const void*,const void*> EagerlyAssumeTag
|
|
|
|
= std::pair<const void*,const void*>(&EagerlyAssumeTag,0);
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::EvalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src, Expr *Ex) {
|
|
|
|
for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
|
|
|
|
ExplodedNode *Pred = *I;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-26 07:32:10 +08:00
|
|
|
// Test if the previous node was as the same expression. This can happen
|
|
|
|
// when the expression fails to evaluate to anything meaningful and
|
|
|
|
// (as an optimization) we don't generate a node.
|
2009-09-09 23:08:12 +08:00
|
|
|
ProgramPoint P = Pred->getLocation();
|
2009-02-26 07:32:10 +08:00
|
|
|
if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Dst.Add(Pred);
|
2009-02-26 07:32:10 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-02-26 07:32:10 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
const GRState* state = Pred->getState();
|
|
|
|
SVal V = state->getSVal(Ex);
|
2009-03-26 11:35:11 +08:00
|
|
|
if (isa<nonloc::SymExprVal>(V)) {
|
2009-02-26 06:32:02 +08:00
|
|
|
// First assume that the condition is true.
|
2009-06-19 06:57:13 +08:00
|
|
|
if (const GRState *stateTrue = state->assume(V, true)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
stateTrue = stateTrue->BindExpr(Ex,
|
2009-08-27 09:39:13 +08:00
|
|
|
ValMgr.makeIntVal(1U, Ex->getType()));
|
2009-09-09 23:08:12 +08:00
|
|
|
Dst.Add(Builder->generateNode(PostStmtCustom(Ex,
|
2009-08-15 11:17:38 +08:00
|
|
|
&EagerlyAssumeTag, Pred->getLocationContext()),
|
2009-02-26 06:32:02 +08:00
|
|
|
stateTrue, Pred));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-26 06:32:02 +08:00
|
|
|
// Next, assume that the condition is false.
|
2009-06-19 06:57:13 +08:00
|
|
|
if (const GRState *stateFalse = state->assume(V, false)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
stateFalse = stateFalse->BindExpr(Ex,
|
2009-08-27 09:39:13 +08:00
|
|
|
ValMgr.makeIntVal(0U, Ex->getType()));
|
2009-08-15 11:17:38 +08:00
|
|
|
Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag,
|
|
|
|
Pred->getLocationContext()),
|
2009-02-26 06:32:02 +08:00
|
|
|
stateFalse, Pred));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Dst.Add(Pred);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function: Objective-C ivar references.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-10-17 08:03:18 +08:00
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred, ExplodedNodeSet& Dst,
|
2008-10-17 08:03:18 +08:00
|
|
|
bool asLValue) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 08:03:18 +08:00
|
|
|
Expr* Base = cast<Expr>(Ex->getBase());
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-10-17 08:03:18 +08:00
|
|
|
Visit(Base, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal BaseVal = state->getSVal(Base);
|
|
|
|
SVal location = state->getLValue(Ex->getDecl(), BaseVal);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 08:03:18 +08:00
|
|
|
if (asLValue)
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, Ex, *I, state->BindExpr(Ex, location));
|
2008-10-17 08:03:18 +08:00
|
|
|
else
|
2009-02-13 09:45:31 +08:00
|
|
|
EvalLoad(Dst, Ex, *I, state, location);
|
2008-10-17 08:03:18 +08:00
|
|
|
}
|
2008-11-13 03:24:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function: Objective-C fast enumeration 'for' statements.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred, ExplodedNodeSet& Dst) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
// 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.
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2008-11-13 03:24:17 +08:00
|
|
|
// 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.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-15 03:47:18 +08:00
|
|
|
Stmt* elem = S->getElement();
|
|
|
|
SVal ElementV;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
|
2009-03-28 14:33:19 +08:00
|
|
|
VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl());
|
2008-11-13 03:24:17 +08:00
|
|
|
assert (ElemD->getInit() == 0);
|
2009-08-22 06:28:32 +08:00
|
|
|
ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext());
|
2008-11-15 03:47:18 +08:00
|
|
|
VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
|
|
|
|
return;
|
2008-11-13 03:24:17 +08:00
|
|
|
}
|
2008-11-15 03:47:18 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-11-15 03:47:18 +08:00
|
|
|
VisitLValue(cast<Expr>(elem), Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
|
2008-11-15 03:47:18 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-06-20 01:10:32 +08:00
|
|
|
VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem));
|
2008-11-15 03:47:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred, ExplodedNodeSet& Dst,
|
2008-11-15 03:47:18 +08:00
|
|
|
SVal ElementV) {
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
|
2008-11-15 03:47:18 +08:00
|
|
|
// Get the current state. Use 'EvalLocation' to determine if it is a null
|
|
|
|
// pointer, etc.
|
|
|
|
Stmt* elem = S->getElement();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV);
|
|
|
|
if (!Pred)
|
2008-11-15 03:47:18 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-18 09:23:53 +08:00
|
|
|
const GRState *state = GetState(Pred);
|
2008-11-15 03:47:18 +08:00
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
// Handle the case where the container still has elements.
|
2009-06-23 17:02:15 +08:00
|
|
|
SVal TrueV = ValMgr.makeTruthVal(1);
|
2009-08-28 06:17:37 +08:00
|
|
|
const GRState *hasElems = state->BindExpr(S, TrueV);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
// Handle the case where the container has no elements.
|
2009-06-23 17:02:15 +08:00
|
|
|
SVal FalseV = ValMgr.makeTruthVal(0);
|
2009-08-28 06:17:37 +08:00
|
|
|
const GRState *noElems = state->BindExpr(S, FalseV);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-15 03:47:18 +08:00
|
|
|
if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV))
|
|
|
|
if (const TypedRegion* R = dyn_cast<TypedRegion>(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.
|
2009-05-09 11:57:34 +08:00
|
|
|
QualType T = R->getValueType(getContext());
|
2008-11-15 03:47:18 +08:00
|
|
|
assert (Loc::IsLocType(T));
|
|
|
|
unsigned Count = Builder->getCurrentBlockCount();
|
2009-04-09 14:49:52 +08:00
|
|
|
SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count);
|
2009-06-23 17:02:15 +08:00
|
|
|
SVal V = ValMgr.makeLoc(Sym);
|
2009-06-18 09:23:53 +08:00
|
|
|
hasElems = hasElems->bindLoc(ElementV, V);
|
2008-11-15 03:47:18 +08:00
|
|
|
|
|
|
|
// Bind the location to 'nil' on the false branch.
|
2009-09-09 23:08:12 +08:00
|
|
|
SVal nilV = ValMgr.makeIntVal(0, T);
|
|
|
|
noElems = noElems->bindLoc(ElementV, nilV);
|
2008-11-15 03:47:18 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-13 05:12:46 +08:00
|
|
|
// Create the new nodes.
|
|
|
|
MakeNode(Dst, S, Pred, hasElems);
|
|
|
|
MakeNode(Dst, S, Pred, noElems);
|
2008-10-17 08:03:18 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function: Objective-C message expressions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst){
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
|
|
|
|
Pred, Dst);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
|
2008-10-31 15:26:14 +08:00
|
|
|
ObjCMessageExpr::arg_iterator AI,
|
|
|
|
ObjCMessageExpr::arg_iterator AE,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred, ExplodedNodeSet& Dst) {
|
2008-04-16 07:06:53 +08:00
|
|
|
if (AI == AE) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// Process the receiver.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
if (Expr* Receiver = ME->getReceiver()) {
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-04-16 07:06:53 +08:00
|
|
|
Visit(Receiver, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
|
2009-07-23 12:41:06 +08:00
|
|
|
VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 12:41:06 +08:00
|
|
|
VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
|
2008-04-16 07:06:53 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-04-16 07:06:53 +08:00
|
|
|
Visit(*AI, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
++AI;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
|
2008-04-16 07:06:53 +08:00
|
|
|
VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// FIXME: More logic for the processing the method call.
|
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(Pred);
|
2008-05-02 02:33:28 +08:00
|
|
|
bool RaisesException = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
if (Expr* Receiver = ME->getReceiver()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal L = state->getSVal(Receiver);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Check for undefined control-flow.
|
2008-04-16 07:06:53 +08:00
|
|
|
if (L.isUndef()) {
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* N = Builder->generateNode(ME, state, Pred);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
UndefReceivers.insert(N);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// "Assume" that the receiver is not NULL.
|
2009-06-19 06:57:13 +08:00
|
|
|
const GRState *StNotNull = state->assume(L, true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// "Assume" that the receiver is NULL.
|
2009-06-19 06:57:13 +08:00
|
|
|
const GRState *StNull = state->assume(L, false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
if (StNull) {
|
2009-04-09 13:45:56 +08:00
|
|
|
QualType RetTy = ME->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-19 12:06:22 +08:00
|
|
|
// Check if the receiver was nil and the return value a struct.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (RetTy->isRecordType()) {
|
2009-04-09 08:00:02 +08:00
|
|
|
if (BR.getParentMap().isConsumedExpr(ME)) {
|
2009-04-08 11:07:17 +08:00
|
|
|
// The [0 ...] expressions will return garbage. Flag either an
|
|
|
|
// explicit or implicit error. Because of the structure of this
|
|
|
|
// function we currently do not bifurfacte the state graph at
|
|
|
|
// this point.
|
|
|
|
// FIXME: We should bifurcate and fill the returned struct with
|
2009-09-09 23:08:12 +08:00
|
|
|
// garbage.
|
2009-08-06 20:48:26 +08:00
|
|
|
if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) {
|
2009-04-08 11:07:17 +08:00
|
|
|
N->markAsSink();
|
2009-06-19 06:57:13 +08:00
|
|
|
if (StNotNull)
|
2009-04-08 11:07:17 +08:00
|
|
|
NilReceiverStructRetImplicit.insert(N);
|
2009-04-09 14:02:06 +08:00
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
NilReceiverStructRetExplicit.insert(N);
|
2009-04-08 11:07:17 +08:00
|
|
|
}
|
|
|
|
}
|
2009-04-09 08:00:02 +08:00
|
|
|
}
|
2009-04-09 13:45:56 +08:00
|
|
|
else {
|
2009-04-09 08:00:02 +08:00
|
|
|
ASTContext& Ctx = getContext();
|
2009-04-09 13:45:56 +08:00
|
|
|
if (RetTy != Ctx.VoidTy) {
|
|
|
|
if (BR.getParentMap().isConsumedExpr(ME)) {
|
|
|
|
// sizeof(void *)
|
|
|
|
const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy);
|
|
|
|
// sizeof(return type)
|
|
|
|
const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType());
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
if (voidPtrSize < returnTypeSize) {
|
2009-08-06 20:48:26 +08:00
|
|
|
if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) {
|
2009-04-09 13:45:56 +08:00
|
|
|
N->markAsSink();
|
2009-09-09 23:08:12 +08:00
|
|
|
if (StNotNull)
|
2009-04-09 13:45:56 +08:00
|
|
|
NilReceiverLargerThanVoidPtrRetImplicit.insert(N);
|
2009-04-09 14:02:06 +08:00
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
NilReceiverLargerThanVoidPtrRetExplicit.insert(N);
|
2009-04-09 13:45:56 +08:00
|
|
|
}
|
|
|
|
}
|
2009-06-19 06:57:13 +08:00
|
|
|
else if (!StNotNull) {
|
2009-04-09 13:45:56 +08:00
|
|
|
// Handle the safe cases where the return value is 0 if the
|
|
|
|
// receiver is nil.
|
|
|
|
//
|
|
|
|
// FIXME: For now take the conservative approach that we only
|
|
|
|
// return null values if we *know* that the receiver is nil.
|
|
|
|
// This is because we can have surprises like:
|
|
|
|
//
|
|
|
|
// ... = [[NSScreens screens] objectAtIndex:0];
|
|
|
|
//
|
|
|
|
// What can happen is that [... screens] could return nil, but
|
|
|
|
// it most likely isn't nil. We should assume the semantics
|
|
|
|
// of this case unless we have *a lot* more knowledge.
|
|
|
|
//
|
2009-04-10 00:46:55 +08:00
|
|
|
SVal V = ValMgr.makeZeroVal(ME->getType());
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, ME, Pred, StNull->BindExpr(ME, V));
|
2009-04-09 12:06:51 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-04-09 02:51:08 +08:00
|
|
|
}
|
2009-02-19 12:06:22 +08:00
|
|
|
}
|
|
|
|
}
|
2009-04-09 02:51:08 +08:00
|
|
|
// We have handled the cases where the receiver is nil. The remainder
|
2009-04-09 14:02:06 +08:00
|
|
|
// of this method should assume that the receiver is not nil.
|
|
|
|
if (!StNotNull)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-09 02:51:08 +08:00
|
|
|
state = StNotNull;
|
2009-02-19 12:06:22 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
// Check if the "raise" message was sent.
|
|
|
|
if (ME->getSelector() == RaiseSel)
|
|
|
|
RaisesException = true;
|
|
|
|
}
|
|
|
|
else {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
IdentifierInfo* ClsName = ME->getClassName();
|
|
|
|
Selector S = ME->getSelector();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
// Check for special instance methods.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (!NSExceptionII) {
|
2008-05-02 02:33:28 +08:00
|
|
|
ASTContext& Ctx = getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
NSExceptionII = &Ctx.Idents.get("NSException");
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
if (ClsName == NSExceptionII) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
enum { NUM_RAISE_SELECTORS = 2 };
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
// Lazily create a cache of the selectors.
|
|
|
|
|
|
|
|
if (!NSExceptionInstanceRaiseSelectors) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
ASTContext& Ctx = getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
|
|
|
|
unsigned idx = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// raise:format:
|
2008-05-03 01:12:56 +08:00
|
|
|
II.push_back(&Ctx.Idents.get("raise"));
|
2009-09-09 23:08:12 +08:00
|
|
|
II.push_back(&Ctx.Idents.get("format"));
|
2008-05-02 02:33:28 +08:00
|
|
|
NSExceptionInstanceRaiseSelectors[idx++] =
|
2009-09-09 23:08:12 +08:00
|
|
|
Ctx.Selectors.getSelector(II.size(), &II[0]);
|
|
|
|
|
|
|
|
// raise:format::arguments:
|
2008-05-03 01:12:56 +08:00
|
|
|
II.push_back(&Ctx.Idents.get("arguments"));
|
2008-05-02 02:33:28 +08:00
|
|
|
NSExceptionInstanceRaiseSelectors[idx++] =
|
|
|
|
Ctx.Selectors.getSelector(II.size(), &II[0]);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
|
|
|
|
if (S == NSExceptionInstanceRaiseSelectors[i]) {
|
|
|
|
RaisesException = true; break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-16 07:06:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// Check for any arguments that are uninitialized/undefined.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
|
|
|
|
I != E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 01:10:32 +08:00
|
|
|
if (state->getSVal(*I).isUndef()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// Generate an error node for passing an uninitialized/undefined value
|
|
|
|
// as an argument to a message expression. This node is a sink.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* N = Builder->generateNode(ME, state, Pred);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
MsgExprUndefArgs[N] = *I;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-05-02 02:33:28 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
// Check if we raise an exception. For now treat these as sinks. Eventually
|
|
|
|
// we will want to handle exceptions properly.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
SaveAndRestore<bool> OldSink(Builder->BuildSinks);
|
|
|
|
|
|
|
|
if (RaisesException)
|
|
|
|
Builder->BuildSinks = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// Dispatch to plug-in transfer function.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
unsigned size = Dst.size();
|
2008-04-24 04:12:28 +08:00
|
|
|
SaveOr OldHasGen(Builder->HasGeneratedNode);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
EvalObjCMessageExpr(Dst, ME, Pred);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// Handle the case where no nodes where generated. Auto-generate that
|
|
|
|
// contains the updated state if we aren't generating sinks.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-19 04:35:30 +08:00
|
|
|
if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
|
2009-02-13 09:45:31 +08:00
|
|
|
MakeNode(Dst, ME, Pred, state);
|
2008-02-19 09:44:53 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer functions: Miscellaneous statements.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, ExplodedNode* Pred, ExplodedNodeSet& Dst){
|
|
|
|
ExplodedNodeSet S1;
|
2008-02-20 02:52:54 +08:00
|
|
|
QualType T = CastE->getType();
|
2008-10-21 14:54:23 +08:00
|
|
|
QualType ExTy = Ex->getType();
|
2008-10-22 16:02:16 +08:00
|
|
|
|
2008-10-31 15:26:14 +08:00
|
|
|
if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
|
2008-10-28 03:41:14 +08:00
|
|
|
T = ExCast->getTypeAsWritten();
|
|
|
|
|
2008-10-22 16:02:16 +08:00
|
|
|
if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType())
|
2008-10-16 14:09:51 +08:00
|
|
|
VisitLValue(Ex, Pred, S1);
|
2008-03-05 06:16:08 +08:00
|
|
|
else
|
|
|
|
Visit(Ex, Pred, S1);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
// Check for casting to "void".
|
2009-09-09 23:08:12 +08:00
|
|
|
if (T->isVoidType()) {
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
|
2008-02-20 02:52:54 +08:00
|
|
|
Dst.Add(*I1);
|
|
|
|
|
2008-01-24 10:02:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-22 05:03:30 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
|
|
|
|
ExplodedNode* N = *I1;
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(N);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = state->getSVal(Ex);
|
2009-07-22 05:03:30 +08:00
|
|
|
const SValuator::CastResult &Res = SVator.EvalCast(V, state, T, ExTy);
|
2009-08-28 06:17:37 +08:00
|
|
|
state = Res.getState()->BindExpr(CastE, Res.getSVal());
|
2009-07-22 05:03:30 +08:00
|
|
|
MakeNode(Dst, CastE, N, state);
|
2008-01-24 10:02:54 +08:00
|
|
|
}
|
2008-01-25 04:55:43 +08:00
|
|
|
}
|
|
|
|
|
2008-10-28 05:54:31 +08:00
|
|
|
void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL,
|
2009-09-09 23:08:12 +08:00
|
|
|
ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst,
|
2008-11-07 18:38:33 +08:00
|
|
|
bool asLValue) {
|
2008-10-28 05:54:31 +08:00
|
|
|
InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-10-28 05:54:31 +08:00
|
|
|
Visit(ILE, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal ILV = state->getSVal(ILE);
|
|
|
|
state = state->bindCompoundLiteral(CL, ILV);
|
2008-10-28 05:54:31 +08:00
|
|
|
|
2008-11-07 18:38:33 +08:00
|
|
|
if (asLValue)
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL)));
|
2008-11-07 18:38:33 +08:00
|
|
|
else
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV));
|
2008-10-28 05:54:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-22 06:28:32 +08:00
|
|
|
void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred,
|
2009-09-09 23:08:12 +08:00
|
|
|
ExplodedNodeSet& Dst) {
|
2008-04-23 06:25:27 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// The CFG has one DeclStmt per Decl.
|
2009-01-20 09:17:11 +08:00
|
|
|
Decl* D = *DS->decl_begin();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-29 02:34:26 +08:00
|
|
|
if (!D || !isa<VarDecl>(D))
|
2008-04-23 06:25:27 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
const VarDecl* VD = dyn_cast<VarDecl>(D);
|
2008-11-13 03:24:17 +08:00
|
|
|
Expr* InitEx = const_cast<Expr*>(VD->getInit());
|
2008-04-23 06:25:27 +08:00
|
|
|
|
|
|
|
// FIXME: static variables may have an initializer, but the second
|
|
|
|
// time a function is called those values may not be current.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-04-23 06:25:27 +08:00
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
if (InitEx)
|
|
|
|
Visit(InitEx, Pred, Tmp);
|
2009-07-18 07:48:26 +08:00
|
|
|
else
|
2008-08-29 02:34:26 +08:00
|
|
|
Tmp.Add(Pred);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2008-11-13 03:24:17 +08:00
|
|
|
unsigned Count = Builder->getCurrentBlockCount();
|
2008-12-20 14:32:12 +08:00
|
|
|
|
2008-12-09 06:47:34 +08:00
|
|
|
// Check if 'VD' is a VLA and if so check if has a non-zero size.
|
|
|
|
QualType T = getContext().getCanonicalType(VD->getType());
|
|
|
|
if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
|
|
|
|
// FIXME: Handle multi-dimensional VLAs.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-09 06:47:34 +08:00
|
|
|
Expr* SE = VLA->getSizeExpr();
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal Size = state->getSVal(SE);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-09 08:44:16 +08:00
|
|
|
if (Size.isUndef()) {
|
2009-08-06 20:48:26 +08:00
|
|
|
if (ExplodedNode* N = Builder->generateNode(DS, state, Pred)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
N->markAsSink();
|
2008-12-09 08:44:16 +08:00
|
|
|
ExplicitBadSizedVLA.insert(N);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
const GRState* zeroState = state->assume(Size, false);
|
2009-06-19 06:57:13 +08:00
|
|
|
state = state->assume(Size, true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
if (zeroState) {
|
2009-08-06 20:48:26 +08:00
|
|
|
if (ExplodedNode* N = Builder->generateNode(DS, zeroState, Pred)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
N->markAsSink();
|
2009-06-19 06:57:13 +08:00
|
|
|
if (state)
|
|
|
|
ImplicitBadSizedVLA.insert(N);
|
|
|
|
else
|
|
|
|
ExplicitBadSizedVLA.insert(N);
|
2008-12-09 06:47:34 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
if (!state)
|
2009-09-09 23:08:12 +08:00
|
|
|
continue;
|
2008-12-09 06:47:34 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 09:54:57 +08:00
|
|
|
// Decls without InitExpr are not initialized explicitly.
|
2009-08-22 06:28:32 +08:00
|
|
|
const LocationContext *LC = (*I)->getLocationContext();
|
|
|
|
|
2009-02-14 09:54:57 +08:00
|
|
|
if (InitEx) {
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal InitVal = state->getSVal(InitEx);
|
2009-02-14 09:54:57 +08:00
|
|
|
QualType T = VD->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 09:54:57 +08:00
|
|
|
// Recover some path-sensitivity if a scalar value evaluated to
|
|
|
|
// UnknownVal.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (InitVal.isUnknown() ||
|
2009-03-11 10:24:48 +08:00
|
|
|
!getConstraintManager().canReasonAbout(InitVal)) {
|
2009-04-10 06:22:44 +08:00
|
|
|
InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2009-08-22 06:28:32 +08:00
|
|
|
state = state->bindDecl(VD, LC, InitVal);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-14 09:54:57 +08:00
|
|
|
// The next thing to do is check if the GRTransferFuncs object wants to
|
|
|
|
// update the state based on the new binding. If the GRTransferFunc
|
|
|
|
// object doesn't do anything, just auto-propagate the current state.
|
|
|
|
GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true);
|
2009-08-22 06:28:32 +08:00
|
|
|
getTF().EvalBind(BuilderRef, loc::MemRegionVal(state->getRegion(VD, LC)),
|
2009-09-09 23:08:12 +08:00
|
|
|
InitVal);
|
|
|
|
}
|
2009-02-14 09:54:57 +08:00
|
|
|
else {
|
2009-08-22 06:28:32 +08:00
|
|
|
state = state->bindDeclWithNoInit(VD, LC);
|
2009-02-14 09:54:57 +08:00
|
|
|
MakeNode(Dst, DS, *I, state);
|
|
|
|
}
|
2008-04-23 06:25:27 +08:00
|
|
|
}
|
|
|
|
}
|
2008-02-05 08:26:40 +08:00
|
|
|
|
2008-10-31 01:47:32 +08:00
|
|
|
namespace {
|
|
|
|
// This class is used by VisitInitListExpr as an item in a worklist
|
|
|
|
// for processing the values contained in an InitListExpr.
|
|
|
|
class VISIBILITY_HIDDEN InitListWLItem {
|
|
|
|
public:
|
|
|
|
llvm::ImmutableList<SVal> Vals;
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* N;
|
2008-10-31 01:47:32 +08:00
|
|
|
InitListExpr::reverse_iterator Itr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals,
|
|
|
|
InitListExpr::reverse_iterator itr)
|
2008-10-31 01:47:32 +08:00
|
|
|
: Vals(vals), N(n), Itr(itr) {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void GRExprEngine::VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet& Dst) {
|
2008-10-30 13:02:23 +08:00
|
|
|
|
2008-10-31 07:14:36 +08:00
|
|
|
const GRState* state = GetState(Pred);
|
2008-11-13 13:05:34 +08:00
|
|
|
QualType T = getContext().getCanonicalType(E->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned NumInitElements = E->getNumInits();
|
2008-10-30 13:02:23 +08:00
|
|
|
|
2009-07-29 04:46:55 +08:00
|
|
|
if (T->isArrayType() || T->isStructureType() ||
|
|
|
|
T->isUnionType() || T->isVectorType()) {
|
2008-10-30 13:02:23 +08:00
|
|
|
|
2008-10-31 07:14:36 +08:00
|
|
|
llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-31 07:14:36 +08:00
|
|
|
// Handle base case where the initializer has no elements.
|
|
|
|
// e.g: static int* myArray[] = {};
|
|
|
|
if (NumInitElements == 0) {
|
2009-06-23 17:02:15 +08:00
|
|
|
SVal V = ValMgr.makeCompoundVal(T, StartVals);
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, E, Pred, state->BindExpr(E, V));
|
2008-10-31 07:14:36 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2008-10-31 07:14:36 +08:00
|
|
|
// Create a worklist to process the initializers.
|
|
|
|
llvm::SmallVector<InitListWLItem, 10> WorkList;
|
2009-09-09 23:08:12 +08:00
|
|
|
WorkList.reserve(NumInitElements);
|
|
|
|
WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin()));
|
2008-10-31 01:47:32 +08:00
|
|
|
InitListExpr::reverse_iterator ItrEnd = E->rend();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-31 07:14:36 +08:00
|
|
|
// Process the worklist until it is empty.
|
2008-10-31 01:47:32 +08:00
|
|
|
while (!WorkList.empty()) {
|
|
|
|
InitListWLItem X = WorkList.back();
|
|
|
|
WorkList.pop_back();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-10-31 01:47:32 +08:00
|
|
|
Visit(*X.Itr, X.N, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-31 01:47:32 +08:00
|
|
|
InitListExpr::reverse_iterator NewItr = X.Itr + 1;
|
2008-10-30 13:02:23 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
|
2008-10-31 01:47:32 +08:00
|
|
|
// Get the last initializer value.
|
|
|
|
state = GetState(*NI);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal InitV = state->getSVal(cast<Expr>(*X.Itr));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-31 01:47:32 +08:00
|
|
|
// Construct the new list of values by prepending the new value to
|
|
|
|
// the already constructed list.
|
|
|
|
llvm::ImmutableList<SVal> NewVals =
|
|
|
|
getBasicVals().consVals(InitV, X.Vals);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-31 01:47:32 +08:00
|
|
|
if (NewItr == ItrEnd) {
|
2008-10-31 11:01:26 +08:00
|
|
|
// Now we have a list holding all init values. Make CompoundValData.
|
2009-06-23 17:02:15 +08:00
|
|
|
SVal V = ValMgr.makeCompoundVal(T, NewVals);
|
2008-10-30 13:02:23 +08:00
|
|
|
|
2008-10-31 01:47:32 +08:00
|
|
|
// Make final state and node.
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, E, *NI, state->BindExpr(E, V));
|
2008-10-31 01:47:32 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Still some initializer values to go. Push them onto the worklist.
|
|
|
|
WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-31 02:34:31 +08:00
|
|
|
return;
|
2008-10-30 13:02:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Loc::IsLocType(T) || T->isIntegerType()) {
|
|
|
|
assert (E->getNumInits() == 1);
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-10-30 13:02:23 +08:00
|
|
|
Expr* Init = E->getInit(0);
|
|
|
|
Visit(Init, Pred, Tmp);
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
|
2008-10-30 13:02:23 +08:00
|
|
|
state = GetState(*I);
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init)));
|
2008-10-30 13:02:23 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf("InitListExpr type = %s\n", T.getAsString().c_str());
|
|
|
|
assert(0 && "unprocessed InitListExpr type");
|
|
|
|
}
|
2008-02-05 08:26:40 +08:00
|
|
|
|
2008-11-12 01:56:53 +08:00
|
|
|
/// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type).
|
|
|
|
void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst) {
|
2008-11-12 01:56:53 +08:00
|
|
|
QualType T = Ex->getTypeOfArgument();
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t amt;
|
|
|
|
|
2008-03-15 11:13:20 +08:00
|
|
|
if (Ex->isSizeOf()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (T == getContext().VoidTy) {
|
2008-12-16 02:51:00 +08:00
|
|
|
// sizeof(void) == 1 byte.
|
|
|
|
amt = 1;
|
|
|
|
}
|
|
|
|
else if (!T.getTypePtr()->isConstantSizeType()) {
|
|
|
|
// FIXME: Add support for VLAs.
|
2008-03-15 11:13:20 +08:00
|
|
|
return;
|
2008-12-16 02:51:00 +08:00
|
|
|
}
|
|
|
|
else if (T->isObjCInterfaceType()) {
|
|
|
|
// Some code tries to take the sizeof an ObjCInterfaceType, relying that
|
|
|
|
// the compiler has laid out its representation. Just report Unknown
|
2009-09-09 23:08:12 +08:00
|
|
|
// for these.
|
2008-05-01 05:31:12 +08:00
|
|
|
return;
|
2008-12-16 02:51:00 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// All other cases.
|
2008-03-15 11:13:20 +08:00
|
|
|
amt = getContext().getTypeSize(T) / 8;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-03-15 11:13:20 +08:00
|
|
|
}
|
|
|
|
else // Get alignment of the type.
|
2008-03-15 11:13:55 +08:00
|
|
|
amt = getContext().getTypeAlign(T) / 8;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-22 05:30:14 +08:00
|
|
|
MakeNode(Dst, Ex, Pred,
|
2009-08-28 06:17:37 +08:00
|
|
|
GetState(Pred)->BindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType())));
|
2008-04-22 07:43:38 +08:00
|
|
|
}
|
|
|
|
|
2008-02-20 12:02:35 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst, bool asLValue) {
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2008-02-20 12:02:35 +08:00
|
|
|
switch (U->getOpcode()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-20 12:02:35 +08:00
|
|
|
default:
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case UnaryOperator::Deref: {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-04-30 05:04:26 +08:00
|
|
|
Visit(Ex, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal location = state->getSVal(Ex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
if (asLValue)
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, U, *I, state->BindExpr(U, location),
|
2009-05-08 02:27:16 +08:00
|
|
|
ProgramPoint::PostLValueKind);
|
2008-04-30 05:04:26 +08:00
|
|
|
else
|
2009-02-13 09:45:31 +08:00
|
|
|
EvalLoad(Dst, U, *I, state, location);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
return;
|
2008-02-22 02:02:17 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-20 01:55:38 +08:00
|
|
|
case UnaryOperator::Real: {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-20 01:55:38 +08:00
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-06-20 01:55:38 +08:00
|
|
|
Visit(Ex, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
// FIXME: We don't have complex SValues yet.
|
2008-06-20 01:55:38 +08:00
|
|
|
if (Ex->getType()->isAnyComplexType()) {
|
|
|
|
// Just report "Unknown."
|
|
|
|
Dst.Add(*I);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-20 01:55:38 +08:00
|
|
|
// For all other types, UnaryOperator::Real is an identity operation.
|
|
|
|
assert (U->getType() == Ex->getType());
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2008-06-20 01:55:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-20 01:55:38 +08:00
|
|
|
case UnaryOperator::Imag: {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-20 01:55:38 +08:00
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-06-20 01:55:38 +08:00
|
|
|
Visit(Ex, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2008-10-17 13:57:07 +08:00
|
|
|
// FIXME: We don't have complex SValues yet.
|
2008-06-20 01:55:38 +08:00
|
|
|
if (Ex->getType()->isAnyComplexType()) {
|
|
|
|
// Just report "Unknown."
|
|
|
|
Dst.Add(*I);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-20 01:55:38 +08:00
|
|
|
// For all other types, UnaryOperator::Float returns 0.
|
|
|
|
assert (Ex->getType()->isIntegerType());
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-06-23 17:02:15 +08:00
|
|
|
SVal X = ValMgr.makeZeroVal(Ex->getType());
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, U, *I, state->BindExpr(U, X));
|
2008-06-20 01:55:38 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-20 01:55:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// FIXME: Just report "Unknown" for OffsetOf.
|
2008-05-01 05:45:55 +08:00
|
|
|
case UnaryOperator::OffsetOf:
|
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH.
|
2008-04-30 05:04:26 +08:00
|
|
|
case UnaryOperator::Extension: {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Unary "+" is a no-op, similar to a parentheses. We still have places
|
|
|
|
// where it may be a block-level expression, so we need to
|
|
|
|
// generate an extra node that just propagates the value of the
|
|
|
|
// subexpression.
|
|
|
|
|
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-04-30 05:04:26 +08:00
|
|
|
Visit(Ex, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex)));
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case UnaryOperator::AddrOf: {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
assert(!asLValue);
|
2008-04-30 05:04:26 +08:00
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-10-16 14:09:51 +08:00
|
|
|
VisitLValue(Ex, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = state->getSVal(Ex);
|
2009-08-28 06:17:37 +08:00
|
|
|
state = state->BindExpr(U, V);
|
2009-02-13 09:45:31 +08:00
|
|
|
MakeNode(Dst, U, *I, state);
|
2008-02-22 03:15:37 +08:00
|
|
|
}
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return;
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case UnaryOperator::LNot:
|
|
|
|
case UnaryOperator::Minus:
|
|
|
|
case UnaryOperator::Not: {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
assert (!asLValue);
|
2008-04-30 05:04:26 +08:00
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-04-30 05:04:26 +08:00
|
|
|
Visit(Ex, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-30 13:32:44 +08:00
|
|
|
// Get the value of the subexpression.
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = state->getSVal(Ex);
|
2008-09-30 13:32:44 +08:00
|
|
|
|
2008-11-15 08:20:05 +08:00
|
|
|
if (V.isUnknownOrUndef()) {
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, U, *I, state->BindExpr(U, V));
|
2008-11-15 08:20:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-15 12:01:56 +08:00
|
|
|
// QualType DstT = getContext().getCanonicalType(U->getType());
|
|
|
|
// QualType SrcT = getContext().getCanonicalType(Ex->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2008-11-15 12:01:56 +08:00
|
|
|
// if (DstT != SrcT) // Perform promotions.
|
2009-09-09 23:08:12 +08:00
|
|
|
// V = EvalCast(V, DstT);
|
|
|
|
//
|
2008-11-15 12:01:56 +08:00
|
|
|
// if (V.isUnknownOrUndef()) {
|
|
|
|
// MakeNode(Dst, U, *I, BindExpr(St, U, V));
|
|
|
|
// continue;
|
|
|
|
// }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
switch (U->getOpcode()) {
|
|
|
|
default:
|
|
|
|
assert(false && "Invalid Opcode.");
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case UnaryOperator::Not:
|
2008-10-01 08:21:14 +08:00
|
|
|
// FIXME: Do we need to handle promotions?
|
2009-08-28 06:17:37 +08:00
|
|
|
state = state->BindExpr(U, EvalComplement(cast<NonLoc>(V)));
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case UnaryOperator::Minus:
|
2008-10-01 08:21:14 +08:00
|
|
|
// FIXME: Do we need to handle promotions?
|
2009-08-28 06:17:37 +08:00
|
|
|
state = state->BindExpr(U, EvalMinus(cast<NonLoc>(V)));
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UnaryOperator::LNot:
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
|
|
|
|
//
|
|
|
|
// Note: technically we do "E == 0", but this is the same in the
|
|
|
|
// transfer functions as "0 == E".
|
2009-06-26 08:05:51 +08:00
|
|
|
SVal Result;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<Loc>(V)) {
|
2009-06-23 17:02:15 +08:00
|
|
|
Loc X = ValMgr.makeNull();
|
2009-06-26 08:05:51 +08:00
|
|
|
Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X,
|
|
|
|
U->getType());
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
|
|
|
else {
|
2008-11-15 12:01:56 +08:00
|
|
|
nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
|
2009-06-26 08:05:51 +08:00
|
|
|
Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X,
|
|
|
|
U->getType());
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-28 06:17:37 +08:00
|
|
|
state = state->BindExpr(U, Result);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
MakeNode(Dst, U, *I, state);
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-01-24 10:28:56 +08:00
|
|
|
}
|
2008-02-27 15:04:16 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Handle ++ and -- (both pre- and post-increment).
|
2008-04-22 07:43:38 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
assert (U->isIncrementDecrementOp());
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-04-30 05:04:26 +08:00
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
2008-10-16 14:09:51 +08:00
|
|
|
VisitLValue(Ex, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V1 = state->getSVal(Ex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Perform a load.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp2;
|
2009-02-13 09:45:31 +08:00
|
|
|
EvalLoad(Tmp2, Ex, *I, state, V1);
|
2008-04-22 12:56:29 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
state = GetState(*I2);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V2 = state->getSVal(Ex);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Propagate unknown and undefined values.
|
2008-04-30 05:04:26 +08:00
|
|
|
if (V2.isUnknownOrUndef()) {
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, U, *I2, state->BindExpr(U, V2));
|
2008-04-30 05:04:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Handle all other values.
|
2008-04-30 05:04:26 +08:00
|
|
|
BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
|
|
|
|
: BinaryOperator::Sub;
|
2009-03-11 11:54:24 +08:00
|
|
|
|
2009-08-05 10:51:59 +08:00
|
|
|
// If the UnaryOperator has non-location type, use its type to create the
|
|
|
|
// constant value. If the UnaryOperator has location type, create the
|
|
|
|
// constant with int type and pointer width.
|
|
|
|
SVal RHS;
|
|
|
|
|
|
|
|
if (U->getType()->isAnyPointerType())
|
|
|
|
RHS = ValMgr.makeIntValWithPtrWidth(1, false);
|
|
|
|
else
|
|
|
|
RHS = ValMgr.makeIntVal(1, U->getType());
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
SVal Result = EvalBinOp(state, Op, V2, RHS, U->getType());
|
|
|
|
|
2009-03-21 04:10:45 +08:00
|
|
|
// Conjure a new symbol if necessary to recover precision.
|
2009-04-22 06:38:05 +08:00
|
|
|
if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){
|
2009-04-10 06:22:44 +08:00
|
|
|
Result = ValMgr.getConjuredSymbolVal(Ex,
|
|
|
|
Builder->getCurrentBlockCount());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-22 06:38:05 +08:00
|
|
|
// If the value is a location, ++/-- should always preserve
|
2009-06-26 08:05:51 +08:00
|
|
|
// non-nullness. Check if the original value was non-null, and if so
|
2009-09-09 23:08:12 +08:00
|
|
|
// propagate that constraint.
|
2009-04-22 06:38:05 +08:00
|
|
|
if (Loc::IsLocType(U->getType())) {
|
2009-05-20 17:00:16 +08:00
|
|
|
SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2,
|
2009-04-22 06:38:05 +08:00
|
|
|
ValMgr.makeZeroVal(U->getType()),
|
2009-09-09 23:08:12 +08:00
|
|
|
getContext().IntTy);
|
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
if (!state->assume(Constraint, true)) {
|
2009-04-22 06:38:05 +08:00
|
|
|
// It isn't feasible for the original value to be null.
|
|
|
|
// Propagate this constraint.
|
2009-05-20 17:00:16 +08:00
|
|
|
Constraint = EvalBinOp(state, BinaryOperator::EQ, Result,
|
2009-04-22 06:38:05 +08:00
|
|
|
ValMgr.makeZeroVal(U->getType()),
|
|
|
|
getContext().IntTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-19 06:57:13 +08:00
|
|
|
state = state->assume(Constraint, false);
|
|
|
|
assert(state);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
}
|
2009-04-22 06:38:05 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-28 06:17:37 +08:00
|
|
|
state = state->BindExpr(U, U->isPostfix() ? V2 : Result);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Perform the store.
|
2009-02-13 09:45:31 +08:00
|
|
|
EvalStore(Dst, U, *I2, state, V1, Result);
|
2008-02-07 09:08:27 +08:00
|
|
|
}
|
2008-04-22 07:43:38 +08:00
|
|
|
}
|
2008-02-07 09:08:27 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::VisitAsmStmt(AsmStmt* A, ExplodedNode* Pred, ExplodedNodeSet& Dst) {
|
2008-03-18 05:11:24 +08:00
|
|
|
VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-03-18 05:11:24 +08:00
|
|
|
|
|
|
|
void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
|
|
|
|
AsmStmt::outputs_iterator I,
|
|
|
|
AsmStmt::outputs_iterator E,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred, ExplodedNodeSet& Dst) {
|
2008-03-18 05:11:24 +08:00
|
|
|
if (I == E) {
|
|
|
|
VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-10-16 14:09:51 +08:00
|
|
|
VisitLValue(*I, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-18 05:11:24 +08:00
|
|
|
++I;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
|
2008-03-18 05:11:24 +08:00
|
|
|
VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
|
|
|
|
AsmStmt::inputs_iterator I,
|
|
|
|
AsmStmt::inputs_iterator E,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred, ExplodedNodeSet& Dst) {
|
2008-03-18 05:11:24 +08:00
|
|
|
if (I == E) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-18 05:11:24 +08:00
|
|
|
// We have processed both the inputs and the outputs. All of the outputs
|
2008-10-17 13:57:07 +08:00
|
|
|
// should evaluate to Locs. Nuke all of their values.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-18 05:11:24 +08:00
|
|
|
// FIXME: Some day in the future it would be nice to allow a "plug-in"
|
|
|
|
// which interprets the inline asm and stores proper results in the
|
|
|
|
// outputs.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(Pred);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-18 05:11:24 +08:00
|
|
|
for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
|
|
|
|
OE = A->end_outputs(); OI != OE; ++OI) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
SVal X = state->getSVal(*OI);
|
2008-10-17 13:57:07 +08:00
|
|
|
assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<Loc>(X))
|
2009-06-20 01:10:32 +08:00
|
|
|
state = state->bindLoc(cast<Loc>(X), UnknownVal());
|
2008-03-18 05:11:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
MakeNode(Dst, A, Pred, state);
|
2008-03-18 05:11:24 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-03-18 05:11:24 +08:00
|
|
|
Visit(*I, Pred, Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-18 05:11:24 +08:00
|
|
|
++I;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 11:02:58 +08:00
|
|
|
for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI!=NE; ++NI)
|
2008-03-18 05:11:24 +08:00
|
|
|
VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
|
|
|
|
}
|
|
|
|
|
2009-09-03 11:02:58 +08:00
|
|
|
void GRExprEngine::EvalReturn(ExplodedNodeSet& Dst, ReturnStmt* S,
|
|
|
|
ExplodedNode* Pred) {
|
2008-04-17 07:05:51 +08:00
|
|
|
assert (Builder && "GRStmtNodeBuilder must be defined.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
unsigned size = Dst.size();
|
2008-04-19 04:35:30 +08:00
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
SaveAndRestore<bool> OldSink(Builder->BuildSinks);
|
|
|
|
SaveOr OldHasGen(Builder->HasGeneratedNode);
|
2008-04-19 04:35:30 +08:00
|
|
|
|
2008-07-18 07:15:45 +08:00
|
|
|
getTF().EvalReturn(Dst, *this, *Builder, S, Pred);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-19 04:35:30 +08:00
|
|
|
// Handle the case where no nodes where generated.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-19 04:35:30 +08:00
|
|
|
if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
|
2008-04-17 07:05:51 +08:00
|
|
|
MakeNode(Dst, S, Pred, GetState(Pred));
|
|
|
|
}
|
|
|
|
|
2009-09-03 11:02:58 +08:00
|
|
|
void GRExprEngine::VisitReturnStmt(ReturnStmt* S, ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst) {
|
2008-03-31 23:02:58 +08:00
|
|
|
|
|
|
|
Expr* R = S->getRetValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-31 23:02:58 +08:00
|
|
|
if (!R) {
|
2008-04-17 07:05:51 +08:00
|
|
|
EvalReturn(Dst, S, Pred);
|
2008-03-31 23:02:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-04-17 07:05:51 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp;
|
2008-11-21 08:27:44 +08:00
|
|
|
Visit(R, Pred, Tmp);
|
2008-10-04 13:50:14 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal X = (*I)->getState()->getSVal(R);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-21 08:27:44 +08:00
|
|
|
// Check if we return the address of a stack variable.
|
|
|
|
if (isa<loc::MemRegionVal>(X)) {
|
|
|
|
// Determine if the value is on the stack.
|
|
|
|
const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-24 02:05:21 +08:00
|
|
|
if (R && R->hasStackStorage()) {
|
2008-11-21 08:27:44 +08:00
|
|
|
// Create a special node representing the error.
|
2009-08-06 20:48:26 +08:00
|
|
|
if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) {
|
2008-11-21 08:27:44 +08:00
|
|
|
N->markAsSink();
|
|
|
|
RetsStackAddr.insert(N);
|
2008-03-31 23:02:58 +08:00
|
|
|
}
|
2008-11-21 08:27:44 +08:00
|
|
|
continue;
|
2008-03-31 23:02:58 +08:00
|
|
|
}
|
|
|
|
}
|
2008-11-21 08:27:44 +08:00
|
|
|
// Check if we return an undefined value.
|
|
|
|
else if (X.isUndef()) {
|
2009-08-06 20:48:26 +08:00
|
|
|
if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) {
|
2008-11-21 08:27:44 +08:00
|
|
|
N->markAsSink();
|
|
|
|
RetsUndef.insert(N);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-17 07:05:51 +08:00
|
|
|
EvalReturn(Dst, S, *I);
|
2008-11-21 08:27:44 +08:00
|
|
|
}
|
2008-03-31 23:02:58 +08:00
|
|
|
}
|
2008-03-25 08:34:37 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer functions: Binary operators.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-14 01:41:41 +08:00
|
|
|
void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred,
|
|
|
|
ExplodedNodeSet& Dst) {
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp1;
|
2008-04-30 05:04:26 +08:00
|
|
|
Expr* LHS = B->getLHS()->IgnoreParens();
|
|
|
|
Expr* RHS = B->getRHS()->IgnoreParens();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-21 01:02:02 +08:00
|
|
|
// FIXME: Add proper support for ObjCImplicitSetterGetterRefExpr.
|
|
|
|
if (isa<ObjCImplicitSetterGetterRefExpr>(LHS)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Visit(RHS, Pred, Dst);
|
2008-12-06 10:39:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-07 09:08:27 +08:00
|
|
|
if (B->isAssignmentOp())
|
2008-10-16 14:09:51 +08:00
|
|
|
VisitLValue(LHS, Pred, Tmp1);
|
2008-02-07 09:08:27 +08:00
|
|
|
else
|
2008-04-30 05:04:26 +08:00
|
|
|
Visit(LHS, Pred, Tmp1);
|
2008-01-16 08:53:15 +08:00
|
|
|
|
2009-09-03 11:02:58 +08:00
|
|
|
for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1!=E1; ++I1) {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal LeftV = (*I1)->getState()->getSVal(LHS);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Process the RHS.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp2;
|
2008-04-30 05:04:26 +08:00
|
|
|
Visit(RHS, *I1, Tmp2);
|
2009-09-02 21:26:26 +08:00
|
|
|
|
|
|
|
ExplodedNodeSet CheckedSet;
|
|
|
|
CheckerVisit(B, CheckedSet, Tmp2, true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// With both the LHS and RHS evaluated, process the operation itself.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end();
|
2009-09-02 21:26:26 +08:00
|
|
|
I2 != E2; ++I2) {
|
2008-02-07 06:50:25 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
const GRState* state = GetState(*I2);
|
|
|
|
const GRState* OldSt = state;
|
2008-10-21 07:40:25 +08:00
|
|
|
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal RightV = state->getSVal(RHS);
|
2008-02-07 06:50:25 +08:00
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
switch (Op) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-24 03:59:44 +08:00
|
|
|
case BinaryOperator::Assign: {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-13 05:45:47 +08:00
|
|
|
// EXPERIMENTAL: "Conjured" symbols.
|
2008-10-18 06:23:12 +08:00
|
|
|
// FIXME: Handle structs.
|
|
|
|
QualType T = RHS->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if ((RightV.isUnknown() ||
|
|
|
|
!getConstraintManager().canReasonAbout(RightV))
|
|
|
|
&& (Loc::IsLocType(T) ||
|
2009-03-11 10:24:48 +08:00
|
|
|
(T->isScalarType() && T->isIntegerType()))) {
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned Count = Builder->getCurrentBlockCount();
|
2009-04-10 06:22:44 +08:00
|
|
|
RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count);
|
2008-03-13 05:45:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-13 05:45:47 +08:00
|
|
|
// Simulate the effects of a "store": bind the value of the RHS
|
2009-09-09 23:08:12 +08:00
|
|
|
// to the L-Value represented by the LHS.
|
|
|
|
EvalStore(Dst, B, LHS, *I2, state->BindExpr(B, RightV),
|
2009-08-25 14:51:30 +08:00
|
|
|
LeftV, RightV);
|
2008-04-17 02:21:25 +08:00
|
|
|
continue;
|
2008-01-24 03:59:44 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// FALL-THROUGH.
|
|
|
|
|
|
|
|
default: {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
if (B->isAssignmentOp())
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-26 08:05:51 +08:00
|
|
|
// Process non-assignments except commas or short-circuited
|
2009-09-09 23:08:12 +08:00
|
|
|
// logical expressions (LAnd and LOr).
|
2009-05-20 17:00:16 +08:00
|
|
|
SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
if (Result.isUnknown()) {
|
2009-02-13 09:45:31 +08:00
|
|
|
if (OldSt != state) {
|
2008-10-21 07:40:25 +08:00
|
|
|
// Generate a new node if we have already created a new state.
|
2009-02-13 09:45:31 +08:00
|
|
|
MakeNode(Dst, B, *I2, state);
|
2008-10-21 07:40:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Dst.Add(*I2);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// The operands were *not* undefined, but the result is undefined.
|
|
|
|
// This is a special node that should be flagged as an error.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-27 09:39:13 +08:00
|
|
|
if (ExplodedNode* UndefNode = Builder->generateNode(B, state, *I2)){
|
2009-09-09 23:08:12 +08:00
|
|
|
UndefNode->markAsSink();
|
2008-04-30 05:04:26 +08:00
|
|
|
UndefResults.insert(UndefNode);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 16:12:37 +08:00
|
|
|
continue;
|
2008-02-19 08:22:37 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Otherwise, create a new node.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-28 06:17:37 +08:00
|
|
|
MakeNode(Dst, B, *I2, state->BindExpr(B, Result));
|
2008-04-30 05:04:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
assert (B->isCompoundAssignmentOp());
|
|
|
|
|
2009-02-07 08:52:24 +08:00
|
|
|
switch (Op) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Invalid opcode for compound assignment.");
|
|
|
|
case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break;
|
|
|
|
case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break;
|
|
|
|
case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break;
|
|
|
|
case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break;
|
|
|
|
case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break;
|
|
|
|
case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break;
|
|
|
|
case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break;
|
|
|
|
case BinaryOperator::AndAssign: Op = BinaryOperator::And; break;
|
|
|
|
case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break;
|
|
|
|
case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break;
|
2008-10-28 07:02:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Perform a load (the LHS). This performs the checks for
|
|
|
|
// null dereferences, and so on.
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNodeSet Tmp3;
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal location = state->getSVal(LHS);
|
2009-02-13 09:45:31 +08:00
|
|
|
EvalLoad(Tmp3, LHS, *I2, state, location);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
for (ExplodedNodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3;
|
2009-09-02 21:26:26 +08:00
|
|
|
++I3) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-13 09:45:31 +08:00
|
|
|
state = GetState(*I3);
|
2009-06-20 01:10:32 +08:00
|
|
|
SVal V = state->getSVal(LHS);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Propagate undefined values (left-side).
|
2008-04-30 05:04:26 +08:00
|
|
|
if (V.isUndef()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, V),
|
2009-08-25 14:51:30 +08:00
|
|
|
location, V);
|
2008-04-30 05:04:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Propagate unknown values (left and right-side).
|
|
|
|
if (RightV.isUnknown() || V.isUnknown()) {
|
2009-08-28 06:17:37 +08:00
|
|
|
EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, UnknownVal()),
|
2009-08-27 09:39:13 +08:00
|
|
|
location, UnknownVal());
|
2008-04-30 05:04:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point:
|
|
|
|
//
|
|
|
|
// The LHS is not Undef/Unknown.
|
|
|
|
// The RHS is not Unknown.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Get the computation type.
|
2009-07-22 05:03:30 +08:00
|
|
|
QualType CTy =
|
|
|
|
cast<CompoundAssignOperator>(B)->getComputationResultType();
|
2008-11-15 12:01:56 +08:00
|
|
|
CTy = getContext().getCanonicalType(CTy);
|
2009-03-28 09:22:36 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
QualType CLHSTy =
|
|
|
|
cast<CompoundAssignOperator>(B)->getComputationLHSType();
|
|
|
|
CLHSTy = getContext().getCanonicalType(CLHSTy);
|
2009-03-28 09:22:36 +08:00
|
|
|
|
2008-11-15 12:01:56 +08:00
|
|
|
QualType LTy = getContext().getCanonicalType(LHS->getType());
|
|
|
|
QualType RTy = getContext().getCanonicalType(RHS->getType());
|
2009-03-28 09:22:36 +08:00
|
|
|
|
|
|
|
// Promote LHS.
|
2009-07-22 05:03:30 +08:00
|
|
|
llvm::tie(state, V) = SVator.EvalCast(V, state, CLHSTy, LTy);
|
2009-03-28 09:22:36 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Evaluate operands and promote to result type.
|
|
|
|
if (RightV.isUndef()) {
|
|
|
|
// Propagate undefined values (right-side).
|
2009-08-28 06:17:37 +08:00
|
|
|
EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, RightV), location,
|
2009-08-27 09:39:13 +08:00
|
|
|
RightV);
|
2008-04-30 05:04:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Compute the result of the operation.
|
2009-07-22 05:03:30 +08:00
|
|
|
SVal Result;
|
|
|
|
llvm::tie(state, Result) = SVator.EvalCast(EvalBinOp(state, Op, V,
|
|
|
|
RightV, CTy),
|
|
|
|
state, B->getType(), CTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
if (Result.isUndef()) {
|
|
|
|
// The operands were not undefined, but the result is undefined.
|
2009-08-06 20:48:26 +08:00
|
|
|
if (ExplodedNode* UndefNode = Builder->generateNode(B, state, *I3)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
UndefNode->markAsSink();
|
2008-04-30 05:04:26 +08:00
|
|
|
UndefResults.insert(UndefNode);
|
2008-02-29 04:32:03 +08:00
|
|
|
}
|
2008-04-17 02:21:25 +08:00
|
|
|
continue;
|
2008-02-07 06:50:25 +08:00
|
|
|
}
|
2008-10-21 07:13:25 +08:00
|
|
|
|
|
|
|
// EXPERIMENTAL: "Conjured" symbols.
|
|
|
|
// FIXME: Handle structs.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-15 12:01:56 +08:00
|
|
|
SVal LHSVal;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if ((Result.isUnknown() ||
|
2009-03-11 10:24:48 +08:00
|
|
|
!getConstraintManager().canReasonAbout(Result))
|
2009-09-09 23:08:12 +08:00
|
|
|
&& (Loc::IsLocType(CTy)
|
2009-03-11 10:24:48 +08:00
|
|
|
|| (CTy->isScalarType() && CTy->isIntegerType()))) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-21 07:13:25 +08:00
|
|
|
unsigned Count = Builder->getCurrentBlockCount();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-15 12:01:56 +08:00
|
|
|
// The symbolic value is actually for the type of the left-hand side
|
|
|
|
// expression, not the computation type, as this is the value the
|
|
|
|
// LValue on the LHS will bind to.
|
2009-04-10 06:22:44 +08:00
|
|
|
LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-23 13:52:28 +08:00
|
|
|
// However, we need to convert the symbol to the computation type.
|
2009-07-22 05:03:30 +08:00
|
|
|
llvm::tie(state, Result) = SVator.EvalCast(LHSVal, state, CTy, LTy);
|
2008-10-21 07:13:25 +08:00
|
|
|
}
|
2008-11-15 12:01:56 +08:00
|
|
|
else {
|
|
|
|
// The left-hand side may bind to a different value then the
|
|
|
|
// computation type.
|
2009-07-22 05:03:30 +08:00
|
|
|
llvm::tie(state, LHSVal) = SVator.EvalCast(Result, state, LTy, CTy);
|
2008-11-15 12:01:56 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, Result),
|
2009-08-25 14:51:30 +08:00
|
|
|
location, LHSVal);
|
2008-01-24 03:59:44 +08:00
|
|
|
}
|
2008-01-16 08:53:15 +08:00
|
|
|
}
|
2008-01-16 07:55:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-18 05:27:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-15 06:36:46 +08:00
|
|
|
// Visualization.
|
2008-01-17 02:18:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-17 05:46:15 +08:00
|
|
|
#ifndef NDEBUG
|
2008-02-14 01:41:41 +08:00
|
|
|
static GRExprEngine* GraphPrintCheckerState;
|
2008-03-08 04:57:30 +08:00
|
|
|
static SourceManager* GraphPrintSourceManager;
|
2008-01-31 07:24:39 +08:00
|
|
|
|
2008-01-17 05:46:15 +08:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
2009-08-06 20:48:26 +08:00
|
|
|
struct VISIBILITY_HIDDEN DOTGraphTraits<ExplodedNode*> :
|
2008-01-17 05:46:15 +08:00
|
|
|
public DefaultDOTGraphTraits {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
static std::string getNodeAttributes(const ExplodedNode* N, void*) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-15 06:54:53 +08:00
|
|
|
if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
|
2008-02-19 08:22:37 +08:00
|
|
|
GraphPrintCheckerState->isExplicitNullDeref(N) ||
|
2008-02-28 17:25:22 +08:00
|
|
|
GraphPrintCheckerState->isUndefDeref(N) ||
|
|
|
|
GraphPrintCheckerState->isUndefStore(N) ||
|
|
|
|
GraphPrintCheckerState->isUndefControlFlow(N) ||
|
2008-03-08 03:04:53 +08:00
|
|
|
GraphPrintCheckerState->isExplicitBadDivide(N) ||
|
|
|
|
GraphPrintCheckerState->isImplicitBadDivide(N) ||
|
2008-03-01 07:14:48 +08:00
|
|
|
GraphPrintCheckerState->isUndefResult(N) ||
|
2008-03-01 07:53:11 +08:00
|
|
|
GraphPrintCheckerState->isBadCall(N) ||
|
|
|
|
GraphPrintCheckerState->isUndefArg(N))
|
2008-02-15 06:54:53 +08:00
|
|
|
return "color=\"red\",style=\"filled\"";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
if (GraphPrintCheckerState->isNoReturnCall(N))
|
|
|
|
return "color=\"blue\",style=\"filled\"";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-15 06:54:53 +08:00
|
|
|
return "";
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
static std::string getNodeLabel(const ExplodedNode* N, void*,bool ShortNames){
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-25 07:06:47 +08:00
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream Out(sbuf);
|
2008-01-24 06:30:44 +08:00
|
|
|
|
|
|
|
// Program Location.
|
2008-01-17 05:46:15 +08:00
|
|
|
ProgramPoint Loc = N->getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-17 05:46:15 +08:00
|
|
|
switch (Loc.getKind()) {
|
|
|
|
case ProgramPoint::BlockEntranceKind:
|
2009-09-09 23:08:12 +08:00
|
|
|
Out << "Block Entrance: B"
|
2008-01-17 05:46:15 +08:00
|
|
|
<< cast<BlockEntrance>(Loc).getBlock()->getBlockID();
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-17 05:46:15 +08:00
|
|
|
case ProgramPoint::BlockExitKind:
|
|
|
|
assert (false);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-17 05:46:15 +08:00
|
|
|
default: {
|
2009-07-23 06:35:28 +08:00
|
|
|
if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) {
|
|
|
|
const Stmt* S = L->getStmt();
|
2008-12-17 06:02:27 +08:00
|
|
|
SourceLocation SLoc = S->getLocStart();
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
|
2009-06-30 09:26:17 +08:00
|
|
|
LangOptions LO; // FIXME.
|
|
|
|
S->printPretty(Out, 0, PrintingPolicy(LO));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (SLoc.isFileID()) {
|
2008-12-17 06:02:27 +08:00
|
|
|
Out << "\\lline="
|
2009-02-04 08:55:58 +08:00
|
|
|
<< GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
|
|
|
|
<< " col="
|
|
|
|
<< GraphPrintSourceManager->getInstantiationColumnNumber(SLoc)
|
|
|
|
<< "\\l";
|
2008-12-17 06:02:27 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 06:35:28 +08:00
|
|
|
if (isa<PreStmt>(Loc))
|
2009-09-09 23:08:12 +08:00
|
|
|
Out << "\\lPreStmt\\l;";
|
2009-07-23 06:35:28 +08:00
|
|
|
else if (isa<PostLoad>(Loc))
|
2009-05-08 02:27:16 +08:00
|
|
|
Out << "\\lPostLoad\\l;";
|
|
|
|
else if (isa<PostStore>(Loc))
|
|
|
|
Out << "\\lPostStore\\l";
|
|
|
|
else if (isa<PostLValue>(Loc))
|
|
|
|
Out << "\\lPostLValue\\l";
|
|
|
|
else if (isa<PostLocationChecksSucceed>(Loc))
|
|
|
|
Out << "\\lPostLocationChecksSucceed\\l";
|
|
|
|
else if (isa<PostNullCheckFailed>(Loc))
|
|
|
|
Out << "\\lPostNullCheckFailed\\l";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
if (GraphPrintCheckerState->isImplicitNullDeref(N))
|
|
|
|
Out << "\\|Implicit-Null Dereference.\\l";
|
|
|
|
else if (GraphPrintCheckerState->isExplicitNullDeref(N))
|
|
|
|
Out << "\\|Explicit-Null Dereference.\\l";
|
|
|
|
else if (GraphPrintCheckerState->isUndefDeref(N))
|
|
|
|
Out << "\\|Dereference of undefialied value.\\l";
|
|
|
|
else if (GraphPrintCheckerState->isUndefStore(N))
|
|
|
|
Out << "\\|Store to Undefined Loc.";
|
|
|
|
else if (GraphPrintCheckerState->isExplicitBadDivide(N))
|
|
|
|
Out << "\\|Explicit divide-by zero or undefined value.";
|
|
|
|
else if (GraphPrintCheckerState->isImplicitBadDivide(N))
|
|
|
|
Out << "\\|Implicit divide-by zero or undefined value.";
|
|
|
|
else if (GraphPrintCheckerState->isUndefResult(N))
|
|
|
|
Out << "\\|Result of operation is undefined.";
|
|
|
|
else if (GraphPrintCheckerState->isNoReturnCall(N))
|
|
|
|
Out << "\\|Call to function marked \"noreturn\".";
|
|
|
|
else if (GraphPrintCheckerState->isBadCall(N))
|
|
|
|
Out << "\\|Call to NULL/Undefined.";
|
|
|
|
else if (GraphPrintCheckerState->isUndefArg(N))
|
|
|
|
Out << "\\|Argument in call is undefined";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:02:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-17 05:46:15 +08:00
|
|
|
const BlockEdge& E = cast<BlockEdge>(Loc);
|
|
|
|
Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
|
|
|
|
<< E.getDst()->getBlockID() << ')';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-31 07:03:39 +08:00
|
|
|
if (Stmt* T = E.getSrc()->getTerminator()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-08 04:57:30 +08:00
|
|
|
SourceLocation SLoc = T->getLocStart();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-31 07:03:39 +08:00
|
|
|
Out << "\\|Terminator: ";
|
2009-06-30 09:26:17 +08:00
|
|
|
LangOptions LO; // FIXME.
|
|
|
|
E.getSrc()->printTerminator(Out, LO);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-09 11:30:59 +08:00
|
|
|
if (SLoc.isFileID()) {
|
|
|
|
Out << "\\lline="
|
2009-02-04 08:55:58 +08:00
|
|
|
<< GraphPrintSourceManager->getInstantiationLineNumber(SLoc)
|
|
|
|
<< " col="
|
|
|
|
<< GraphPrintSourceManager->getInstantiationColumnNumber(SLoc);
|
2008-03-09 11:30:59 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
if (isa<SwitchStmt>(T)) {
|
|
|
|
Stmt* Label = E.getDst()->getLabel();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (Label) {
|
2008-02-14 07:08:21 +08:00
|
|
|
if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
|
|
|
|
Out << "\\lcase ";
|
2009-06-30 09:26:17 +08:00
|
|
|
LangOptions LO; // FIXME.
|
|
|
|
C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
if (Stmt* RHS = C->getRHS()) {
|
|
|
|
Out << " .. ";
|
2009-06-30 09:26:17 +08:00
|
|
|
RHS->printPretty(Out, 0, PrintingPolicy(LO));
|
2008-02-14 07:08:21 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
Out << ":";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert (isa<DefaultStmt>(Label));
|
|
|
|
Out << "\\ldefault:";
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2008-02-14 07:08:21 +08:00
|
|
|
Out << "\\l(implicit) default:";
|
|
|
|
}
|
|
|
|
else if (isa<IndirectGotoStmt>(T)) {
|
2008-01-31 07:03:39 +08:00
|
|
|
// FIXME
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Out << "\\lCondition: ";
|
|
|
|
if (*E.getSrc()->succ_begin() == E.getDst())
|
|
|
|
Out << "true";
|
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
Out << "false";
|
2008-01-31 07:03:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-31 07:03:39 +08:00
|
|
|
Out << "\\l";
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
if (GraphPrintCheckerState->isUndefControlFlow(N)) {
|
|
|
|
Out << "\\|Control-flow based on\\lUndefined value.\\l";
|
2008-01-31 07:24:39 +08:00
|
|
|
}
|
2008-01-17 05:46:15 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-28 18:21:43 +08:00
|
|
|
Out << "\\|StateID: " << (void*) N->getState() << "\\|";
|
2008-02-09 05:10:02 +08:00
|
|
|
|
2009-06-18 09:23:53 +08:00
|
|
|
const GRState *state = N->getState();
|
|
|
|
state->printDOT(Out);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-24 06:30:44 +08:00
|
|
|
Out << "\\l";
|
2008-01-17 05:46:15 +08:00
|
|
|
return Out.str();
|
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
} // end llvm namespace
|
2008-01-17 05:46:15 +08:00
|
|
|
#endif
|
|
|
|
|
2008-03-08 06:58:01 +08:00
|
|
|
#ifndef NDEBUG
|
2008-03-13 01:18:20 +08:00
|
|
|
template <typename ITERATOR>
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* GetGraphNode(ITERATOR I) { return *I; }
|
2008-03-13 01:18:20 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
template <> ExplodedNode*
|
|
|
|
GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator>
|
|
|
|
(llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) {
|
2008-03-13 01:18:20 +08:00
|
|
|
return I->first;
|
|
|
|
}
|
2008-03-08 06:58:01 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void GRExprEngine::ViewGraph(bool trim) {
|
2009-09-09 23:08:12 +08:00
|
|
|
#ifndef NDEBUG
|
2008-03-08 06:58:01 +08:00
|
|
|
if (trim) {
|
2009-08-06 20:48:26 +08:00
|
|
|
std::vector<ExplodedNode*> Src;
|
2009-03-11 09:41:22 +08:00
|
|
|
|
|
|
|
// Flush any outstanding reports to make sure we cover all the nodes.
|
|
|
|
// This does not cause them to get displayed.
|
|
|
|
for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
|
|
|
|
const_cast<BugType*>(*I)->FlushReports(BR);
|
|
|
|
|
|
|
|
// Iterate through the reports and get their nodes.
|
|
|
|
for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) {
|
2009-09-03 11:02:58 +08:00
|
|
|
for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end();
|
2009-09-09 23:08:12 +08:00
|
|
|
I2!=E2; ++I2) {
|
2009-03-11 09:41:22 +08:00
|
|
|
const BugReportEquivClass& EQ = *I2;
|
|
|
|
const BugReport &R = **EQ.begin();
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode *N = const_cast<ExplodedNode*>(R.getEndNode());
|
2009-03-11 09:41:22 +08:00
|
|
|
if (N) Src.push_back(N);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-13 01:18:20 +08:00
|
|
|
ViewGraph(&Src[0], &Src[0]+Src.size());
|
2008-03-08 06:58:01 +08:00
|
|
|
}
|
2008-03-12 02:25:33 +08:00
|
|
|
else {
|
|
|
|
GraphPrintCheckerState = this;
|
|
|
|
GraphPrintSourceManager = &getContext().getSourceManager();
|
2008-08-14 05:24:49 +08:00
|
|
|
|
2008-03-08 06:58:01 +08:00
|
|
|
llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-12 02:25:33 +08:00
|
|
|
GraphPrintCheckerState = NULL;
|
|
|
|
GraphPrintSourceManager = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) {
|
2008-03-12 02:25:33 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
GraphPrintCheckerState = this;
|
|
|
|
GraphPrintSourceManager = &getContext().getSourceManager();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first);
|
2008-03-12 02:25:33 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
if (!TrimmedG.get())
|
2009-08-23 20:08:50 +08:00
|
|
|
llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
|
2009-02-05 07:49:09 +08:00
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
|
|
|
|
|
2008-01-31 07:24:39 +08:00
|
|
|
GraphPrintCheckerState = NULL;
|
2008-03-08 04:57:30 +08:00
|
|
|
GraphPrintSourceManager = NULL;
|
2008-02-15 06:36:46 +08:00
|
|
|
#endif
|
2008-01-17 02:18:48 +08:00
|
|
|
}
|