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"
|
2008-04-10 05:41:14 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
2008-03-08 04:57:30 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-02-15 06:36:46 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-07-12 02:37:32 +08:00
|
|
|
#include "llvm/ADT/ImmutableList.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-09-13 13:16:45 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-07-11 06:03:41 +08:00
|
|
|
|
2008-02-27 14:07:00 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
|
|
|
#include <sstream>
|
|
|
|
#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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Engine construction and deletion.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
MapTy M;
|
|
|
|
Checks::Factory F;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : F(Alloc) {}
|
|
|
|
|
|
|
|
virtual ~MappedBatchAuditor() {
|
|
|
|
llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (AlreadyVisited.count(check))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
AlreadyVisited.insert(check);
|
|
|
|
delete check;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EmitWarnings(BugReporter& BR) {
|
|
|
|
llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (AlreadyVisited.count(check))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
check->EmitWarnings(BR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
virtual bool Audit(NodeTy* N, GRStateManager& VMgr) {
|
2008-07-12 02:37:32 +08:00
|
|
|
Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
|
|
|
|
void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass());
|
|
|
|
MapTy::iterator MI = M.find(key);
|
|
|
|
|
|
|
|
if (MI == M.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool isSink = false;
|
|
|
|
|
|
|
|
for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I)
|
2008-07-22 08:46:16 +08:00
|
|
|
isSink |= (*I)->Audit(N, VMgr);
|
2008-07-12 02:37:32 +08:00
|
|
|
|
|
|
|
return isSink;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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
|
|
|
|
2008-07-03 04:13:38 +08:00
|
|
|
GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx,
|
2008-10-24 09:04:59 +08:00
|
|
|
LiveVariables& L,
|
2008-11-27 09:55:08 +08:00
|
|
|
StoreManagerCreator SMC,
|
|
|
|
ConstraintManagerCreator CMC)
|
2008-04-10 05:41:14 +08:00
|
|
|
: CoreEngine(cfg, CD, Ctx, *this),
|
|
|
|
G(CoreEngine.getGraph()),
|
2008-07-03 04:13:38 +08:00
|
|
|
Liveness(L),
|
2008-04-10 05:41:14 +08:00
|
|
|
Builder(NULL),
|
2008-11-27 09:55:08 +08:00
|
|
|
StateMgr(G.getContext(), SMC, CMC, G.getAllocator(), cfg, CD, L),
|
2008-04-10 05:41:14 +08:00
|
|
|
SymMgr(StateMgr.getSymbolManager()),
|
2008-05-02 02:33:28 +08:00
|
|
|
CurrentStmt(NULL),
|
|
|
|
NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL),
|
2008-07-03 04:13:38 +08:00
|
|
|
RaiseSel(GetNullarySelector("raise", G.getContext())) {}
|
2008-04-10 05:41:14 +08:00
|
|
|
|
2008-06-21 05:45:25 +08:00
|
|
|
GRExprEngine::~GRExprEngine() {
|
2008-04-10 05:41:14 +08:00
|
|
|
for (BugTypeSet::iterator I = BugTypes.begin(), E = BugTypes.end(); I!=E; ++I)
|
|
|
|
delete *I;
|
2008-07-12 02:37:32 +08:00
|
|
|
|
2008-05-02 02:33:28 +08:00
|
|
|
|
|
|
|
delete [] NSExceptionInstanceRaiseSelectors;
|
2008-04-10 05:41:14 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// SaveAndRestore - A utility class that uses RIIA to save and restore
|
|
|
|
// the value of a variable.
|
|
|
|
template<typename T>
|
|
|
|
struct VISIBILITY_HIDDEN SaveAndRestore {
|
|
|
|
SaveAndRestore(T& x) : X(x), old_value(x) {}
|
|
|
|
~SaveAndRestore() { X = old_value; }
|
|
|
|
T get() { return old_value; }
|
|
|
|
|
|
|
|
T& X;
|
|
|
|
T old_value;
|
|
|
|
};
|
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
// SaveOr - Similar to SaveAndRestore. Operates only on bools; the old
|
|
|
|
// value of a variable is saved, and during the dstor the old value is
|
|
|
|
// or'ed with the new value.
|
|
|
|
struct VISIBILITY_HIDDEN SaveOr {
|
|
|
|
SaveOr(bool& x) : X(x), old_value(x) { x = false; }
|
|
|
|
~SaveOr() { X |= old_value; }
|
|
|
|
|
|
|
|
bool& X;
|
|
|
|
bool old_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 05:24:01 +08:00
|
|
|
void GRExprEngine::EmitWarnings(BugReporterData& BRData) {
|
2008-04-10 05:41:14 +08:00
|
|
|
for (bug_type_iterator I = bug_types_begin(), E = bug_types_end(); I!=E; ++I){
|
2008-07-03 05:24:01 +08:00
|
|
|
GRBugReporter BR(BRData, *this);
|
2008-04-10 05:41:14 +08:00
|
|
|
(*I)->EmitWarnings(BR);
|
|
|
|
}
|
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
if (BatchAuditor) {
|
2008-07-03 05:24:01 +08:00
|
|
|
GRBugReporter BR(BRData, *this);
|
2008-07-12 02:37:32 +08:00
|
|
|
BatchAuditor->EmitWarnings(BR);
|
2008-04-10 05:41:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) {
|
2008-07-18 07:15:45 +08:00
|
|
|
StateMgr.TF = tf;
|
2008-08-16 08:49:49 +08:00
|
|
|
tf->RegisterChecks(*this);
|
|
|
|
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()));
|
|
|
|
|
|
|
|
((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C);
|
2008-04-10 05:41:14 +08:00
|
|
|
}
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* GRExprEngine::getInitialState() {
|
2008-08-20 00:51:45 +08:00
|
|
|
return StateMgr.getInitialState();
|
2008-02-05 05:59:01 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top-level transfer function logic (Dispatcher).
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
|
|
|
|
|
|
|
|
Builder = &builder;
|
2008-04-25 07:35:58 +08:00
|
|
|
EntryNode = builder.getLastNode();
|
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;
|
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());
|
2008-04-16 07:06:53 +08:00
|
|
|
|
2008-07-12 02:37:32 +08:00
|
|
|
// Create the cleaned state.
|
2008-04-25 07:35:58 +08:00
|
|
|
CleanedState = StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt,
|
|
|
|
Liveness, DeadSymbols);
|
2008-04-25 02:31:42 +08:00
|
|
|
|
|
|
|
// Process any special transfer function for dead symbols.
|
|
|
|
NodeSet Tmp;
|
|
|
|
|
|
|
|
if (DeadSymbols.empty())
|
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;
|
|
|
|
|
2008-07-18 07:15:45 +08:00
|
|
|
getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S,
|
2008-04-25 09:25:15 +08:00
|
|
|
CleanedState, DeadSymbols);
|
2008-04-25 07:35:58 +08:00
|
|
|
|
|
|
|
if (!Builder->BuildSinks && !Builder->HasGeneratedNode)
|
|
|
|
Tmp.Add(EntryNode);
|
2008-04-25 02:31:42 +08:00
|
|
|
}
|
2008-04-25 07:35:58 +08:00
|
|
|
|
|
|
|
bool HasAutoGenerated = false;
|
|
|
|
|
2008-04-25 02:31:42 +08:00
|
|
|
for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2008-04-25 07:35:58 +08:00
|
|
|
|
|
|
|
NodeSet Dst;
|
|
|
|
|
2008-04-25 02:31:42 +08:00
|
|
|
// Set the cleaned state.
|
2008-04-25 07:35:58 +08:00
|
|
|
Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I));
|
|
|
|
|
2008-04-25 02:31:42 +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
|
|
|
|
// auto-transitions for other cases.
|
|
|
|
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
|
|
|
}
|
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;
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
Builder = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
|
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Cases we intentionally have "default" handle:
|
|
|
|
// AddrLabelExpr, IntegerLiteral, CharacterLiteral
|
|
|
|
|
|
|
|
Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
|
|
|
|
break;
|
2008-04-22 12:56:29 +08:00
|
|
|
|
|
|
|
case Stmt::ArraySubscriptExprClass:
|
|
|
|
VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false);
|
|
|
|
break;
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
case Stmt::AsmStmtClass:
|
|
|
|
VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
BinaryOperator* B = cast<BinaryOperator>(S);
|
|
|
|
|
|
|
|
if (B->isLogicalOp()) {
|
|
|
|
VisitLogicalExpr(B, Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (B->getOpcode() == BinaryOperator::Comma) {
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(Pred);
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, B, Pred, BindExpr(St, B, GetSVal(St, B->getRHS())));
|
2008-04-16 07:06:53 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-11-15 03:47:18 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
|
|
|
|
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.
|
|
|
|
|
|
|
|
case Stmt::ChooseExprClass: { // __builtin_choose_expr
|
|
|
|
ChooseExpr* C = cast<ChooseExpr>(S);
|
|
|
|
VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::DeclRefExprClass:
|
2008-04-30 05:04:26 +08:00
|
|
|
VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false);
|
2008-04-16 07:06:53 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::DeclStmtClass:
|
|
|
|
VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
|
|
|
|
break;
|
|
|
|
|
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;
|
2008-04-16 07:06:53 +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;
|
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;
|
2008-04-22 07:43:38 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
case Stmt::ObjCMessageExprClass: {
|
|
|
|
VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::ReturnStmtClass:
|
|
|
|
VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
|
|
|
|
break;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
case Stmt::StmtExprClass: {
|
|
|
|
StmtExpr* SE = cast<StmtExpr>(S);
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(Pred);
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
// FIXME: Not certain if we can have empty StmtExprs. If so, we should
|
|
|
|
// probably just remove these from the CFG.
|
|
|
|
assert (!SE->getSubStmt()->body_empty());
|
|
|
|
|
|
|
|
if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin()))
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, SE, Pred, BindExpr(St, SE, GetSVal(St, LastExpr)));
|
2008-04-16 07:06:53 +08:00
|
|
|
else
|
|
|
|
Dst.Add(Pred);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2008-11-30 13:49:49 +08:00
|
|
|
|
|
|
|
case Stmt::StringLiteralClass:
|
|
|
|
VisitLValue(cast<StringLiteral>(S), Pred, Dst);
|
|
|
|
break;
|
2008-04-16 07:06:53 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::UnaryOperatorClass:
|
|
|
|
VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
void GRExprEngine::VisitLValue(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
Ex = Ex->IgnoreParens();
|
|
|
|
|
|
|
|
if (Ex != CurrentStmt && getCFG().isBlkExpr(Ex)) {
|
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Ex->getStmtClass()) {
|
2008-04-16 07:06:53 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::ArraySubscriptExprClass:
|
|
|
|
VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
2008-04-16 07:06:53 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::DeclRefExprClass:
|
|
|
|
VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
2008-04-16 07:06:53 +08:00
|
|
|
|
2008-10-17 08:03:18 +08:00
|
|
|
case Stmt::ObjCIvarRefExprClass:
|
|
|
|
VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::UnaryOperatorClass:
|
|
|
|
VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
2008-04-16 07:06:53 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case Stmt::MemberExprClass:
|
|
|
|
VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true);
|
|
|
|
return;
|
2008-10-18 01:24:14 +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;
|
|
|
|
|
2008-10-18 01:24:14 +08:00
|
|
|
case Stmt::ObjCPropertyRefExprClass:
|
|
|
|
// 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: {
|
|
|
|
const GRState* St = GetState(Pred);
|
|
|
|
SVal V = StateMgr.GetLValue(St, cast<StringLiteral>(Ex));
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
|
2008-10-25 22:18:57 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-10-18 12:08:49 +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.
|
2008-10-26 04:09:21 +08:00
|
|
|
assert ((Ex->getType()->isAggregateType() ||
|
|
|
|
Ex->getType()->isUnionType()) &&
|
|
|
|
"Other kinds of expressions with non-aggregate/union types do"
|
|
|
|
" not have lvalues.");
|
2008-10-18 12:08:49 +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) {
|
|
|
|
|
|
|
|
return BC.getNumVisited(B->getBlockID()) < 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Branch processing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* GRExprEngine::MarkBranch(const GRState* St,
|
2008-07-11 06:03:41 +08:00
|
|
|
Stmt* Terminator,
|
|
|
|
bool branchTaken) {
|
2008-02-27 03:05:15 +08:00
|
|
|
|
|
|
|
switch (Terminator->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
return St;
|
|
|
|
|
|
|
|
case Stmt::BinaryOperatorClass: { // '&&' and '||'
|
|
|
|
|
|
|
|
BinaryOperator* B = cast<BinaryOperator>(Terminator);
|
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
|
|
|
|
|
|
|
assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
|
|
|
|
(Op == BinaryOperator::LOr && !branchTaken)
|
|
|
|
? B->getRHS() : B->getLHS();
|
|
|
|
|
2008-10-30 13:33:54 +08:00
|
|
|
return BindBlkExpr(St, B, UndefinedVal(Ex));
|
2008-02-27 03:05:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::ConditionalOperatorClass: { // ?:
|
|
|
|
|
|
|
|
ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
|
|
|
|
|
|
|
|
// For ?, if branchTaken == true then the value is either the LHS or
|
|
|
|
// the condition itself. (GNU extension).
|
|
|
|
|
|
|
|
Expr* Ex;
|
|
|
|
|
|
|
|
if (branchTaken)
|
|
|
|
Ex = C->getLHS() ? C->getLHS() : C->getCond();
|
|
|
|
else
|
|
|
|
Ex = C->getRHS();
|
|
|
|
|
2008-10-30 13:33:54 +08:00
|
|
|
return BindBlkExpr(St, C, UndefinedVal(Ex));
|
2008-02-27 03:05:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::ChooseExprClass: { // ?:
|
|
|
|
|
|
|
|
ChooseExpr* C = cast<ChooseExpr>(Terminator);
|
|
|
|
|
|
|
|
Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
|
2008-10-30 13:33:54 +08:00
|
|
|
return BindBlkExpr(St, C, UndefinedVal(Ex));
|
2008-02-27 03:05:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term,
|
2008-02-22 02:02:17 +08:00
|
|
|
BranchNodeBuilder& builder) {
|
2008-01-31 07:03:39 +08:00
|
|
|
|
2008-02-12 03:21:59 +08:00
|
|
|
// Remove old bindings for subexpressions.
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* PrevState =
|
2008-07-11 06:03:41 +08:00
|
|
|
StateMgr.RemoveSubExprBindings(builder.getState());
|
2008-02-05 08:26:40 +08:00
|
|
|
|
2008-02-16 06:29:00 +08:00
|
|
|
// Check for NULL conditions; e.g. "for(;;)"
|
|
|
|
if (!Condition) {
|
|
|
|
builder.markInfeasible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = GetSVal(PrevState, Condition);
|
2008-01-31 07:03:39 +08:00
|
|
|
|
|
|
|
switch (V.getBaseKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case SVal::UnknownKind:
|
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;
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case SVal::UndefinedKind: {
|
2008-01-31 07:03:39 +08:00
|
|
|
NodeTy* N = builder.generateNode(PrevState, true);
|
|
|
|
|
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
2008-02-28 17:25:22 +08:00
|
|
|
UndefBranches.insert(N);
|
2008-01-31 07:03:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.markInfeasible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-02-13 02:08:17 +08:00
|
|
|
|
2008-03-01 04:27:50 +08:00
|
|
|
// Process the true branch.
|
2008-02-13 02:08:17 +08:00
|
|
|
|
2008-03-13 05:45:47 +08:00
|
|
|
bool isFeasible = false;
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = Assume(PrevState, V, true, isFeasible);
|
2008-03-01 04:27:50 +08:00
|
|
|
|
|
|
|
if (isFeasible)
|
|
|
|
builder.generateNode(MarkBranch(St, Term, true), true);
|
2008-02-13 02:08:17 +08:00
|
|
|
else
|
|
|
|
builder.markInfeasible(true);
|
2008-03-01 04:27:50 +08:00
|
|
|
|
|
|
|
// Process the false branch.
|
2008-01-30 07:32:35 +08:00
|
|
|
|
2008-03-01 04:27:50 +08:00
|
|
|
isFeasible = false;
|
|
|
|
St = Assume(PrevState, V, false, isFeasible);
|
2008-01-30 07:32:35 +08:00
|
|
|
|
2008-03-01 04:27:50 +08:00
|
|
|
if (isFeasible)
|
|
|
|
builder.generateNode(MarkBranch(St, Term, false), false);
|
2008-02-05 08:26:40 +08:00
|
|
|
else
|
|
|
|
builder.markInfeasible(false);
|
|
|
|
}
|
|
|
|
|
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.
|
2008-02-14 01:41:41 +08:00
|
|
|
void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
|
2008-02-13 08:24:44 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = builder.getState();
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = GetSVal(St, builder.getTarget());
|
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.
|
|
|
|
//
|
|
|
|
|
|
|
|
typedef IndirectGotoNodeBuilder::iterator iterator;
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<loc::GotoLabel>(V)) {
|
|
|
|
LabelStmt* L = cast<loc::GotoLabel>(V).getLabel();
|
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) {
|
|
|
|
builder.generateNode(I, St);
|
2008-02-13 08:24:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2008-02-14 01:27:37 +08:00
|
|
|
NodeTy* N = builder.generateNode(builder.begin(), St, true);
|
2008-02-28 17:25:22 +08:00
|
|
|
UndefBranches.insert(N);
|
2008-02-13 08:24:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is really a catch-all. We don't support symbolics yet.
|
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
assert (V.isUnknown());
|
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
|
|
|
builder.generateNode(I, St);
|
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,
|
|
|
|
NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
|
|
|
|
assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(Pred);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = GetBlkExprSVal(St, Ex);
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
assert (X.isUndef());
|
|
|
|
|
|
|
|
Expr* SE = (Expr*) cast<UndefinedVal>(X).getData();
|
|
|
|
|
|
|
|
assert (SE);
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
X = GetBlkExprSVal(St, SE);
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
// Make sure that we invalidate the previous binding.
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, StateMgr.BindExpr(St, Ex, X, true, 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.
|
|
|
|
void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
|
|
|
|
|
|
|
|
typedef SwitchNodeBuilder::iterator iterator;
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = builder.getState();
|
2008-02-19 06:57:02 +08:00
|
|
|
Expr* CondE = builder.getCondition();
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal CondV = GetSVal(St, CondE);
|
2008-02-14 07:08:21 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
if (CondV.isUndef()) {
|
2008-02-14 07:08:21 +08:00
|
|
|
NodeTy* N = builder.generateDefaultCaseNode(St, true);
|
2008-02-28 17:25:22 +08:00
|
|
|
UndefBranches.insert(N);
|
2008-02-14 07:08:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* DefaultSt = St;
|
2008-02-14 07:08:21 +08:00
|
|
|
|
|
|
|
// While most of this can be assumed (such as the signedness), having it
|
|
|
|
// just computed makes sure everything makes the same assumptions end-to-end.
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2008-03-06 02:54:05 +08:00
|
|
|
unsigned bits = getContext().getTypeSize(CondE->getType());
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
APSInt V1(bits, false);
|
|
|
|
APSInt V2 = V1;
|
2008-04-23 13:03:18 +08:00
|
|
|
bool DefaultFeasible = false;
|
2008-02-14 07:08:21 +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());
|
|
|
|
|
|
|
|
// Evaluate the case.
|
|
|
|
if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
|
|
|
|
assert (false && "Case condition must evaluate to an integer constant.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the RHS of the case, if it exists.
|
|
|
|
|
|
|
|
if (Expr* E = Case->getRHS()) {
|
|
|
|
if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
|
|
|
|
assert (false &&
|
|
|
|
"Case condition (RHS) must evaluate to an integer constant.");
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (V1 <= V2);
|
|
|
|
}
|
2008-03-18 06:17:56 +08:00
|
|
|
else
|
|
|
|
V2 = V1;
|
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.
|
2008-02-14 07:08:21 +08:00
|
|
|
|
2008-03-18 06:17:56 +08:00
|
|
|
do {
|
2008-10-17 13:57:07 +08:00
|
|
|
nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
|
2008-02-14 07:08:21 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
|
2008-02-14 07:08:21 +08:00
|
|
|
|
|
|
|
// Now "assume" that the case matches.
|
|
|
|
|
2008-03-13 05:45:47 +08:00
|
|
|
bool isFeasible = false;
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* StNew = Assume(St, Res, true, isFeasible);
|
2008-02-14 07:08:21 +08:00
|
|
|
|
|
|
|
if (isFeasible) {
|
|
|
|
builder.generateCaseStmtNode(I, StNew);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now "assume" that the case doesn't match. Add this state
|
|
|
|
// to the default state (if it is feasible).
|
|
|
|
|
2008-03-13 05:45:47 +08:00
|
|
|
isFeasible = false;
|
2008-02-15 03:37:24 +08:00
|
|
|
StNew = Assume(DefaultSt, Res, false, isFeasible);
|
2008-02-14 07:08:21 +08:00
|
|
|
|
2008-04-23 13:03:18 +08:00
|
|
|
if (isFeasible) {
|
|
|
|
DefaultFeasible = true;
|
2008-02-14 07:08:21 +08:00
|
|
|
DefaultSt = StNew;
|
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.
|
|
|
|
if (V1 == V2)
|
|
|
|
break;
|
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
++V1;
|
2008-03-18 06:18:22 +08:00
|
|
|
assert (V1 <= V2);
|
2008-02-14 07:08:21 +08:00
|
|
|
|
2008-03-18 06:17:56 +08:00
|
|
|
} while (true);
|
2008-02-14 07:08:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here, than we know that the default branch is
|
|
|
|
// possible.
|
2008-04-23 13:03:18 +08:00
|
|
|
if (DefaultFeasible) 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
|
|
|
|
2008-02-14 01:41:41 +08:00
|
|
|
void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
|
2008-02-22 02:02:17 +08:00
|
|
|
NodeSet& Dst) {
|
2008-02-19 08:22:37 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
assert (B->getOpcode() == BinaryOperator::LAnd ||
|
|
|
|
B->getOpcode() == BinaryOperator::LOr);
|
|
|
|
|
|
|
|
assert (B == CurrentStmt && getCFG().isBlkExpr(B));
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(Pred);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = GetBlkExprSVal(St, B);
|
2008-02-27 03:05:15 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
assert (X.isUndef());
|
2008-02-27 03:05:15 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData();
|
2008-02-27 03:05:15 +08:00
|
|
|
|
|
|
|
assert (Ex);
|
|
|
|
|
|
|
|
if (Ex == B->getRHS()) {
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
X = GetBlkExprSVal(St, Ex);
|
2008-02-27 03:05:15 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
// Handle undefined values.
|
2008-02-27 03:40:44 +08:00
|
|
|
|
2008-02-28 17:25:22 +08:00
|
|
|
if (X.isUndef()) {
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
|
2008-02-27 03:40:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
// the payoff is not likely to be large. Instead, we do eager evaluation.
|
|
|
|
|
|
|
|
bool isFeasible = false;
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* NewState = Assume(St, X, true, isFeasible);
|
2008-02-27 03:05:15 +08:00
|
|
|
|
|
|
|
if (isFeasible)
|
2008-03-22 05:30:14 +08:00
|
|
|
MakeNode(Dst, B, Pred,
|
2008-10-30 13:33:54 +08:00
|
|
|
BindBlkExpr(NewState, B, MakeConstantVal(1U, B)));
|
2008-02-27 03:05:15 +08:00
|
|
|
|
|
|
|
isFeasible = false;
|
|
|
|
NewState = Assume(St, X, false, isFeasible);
|
|
|
|
|
|
|
|
if (isFeasible)
|
2008-03-22 05:30:14 +08:00
|
|
|
MakeNode(Dst, B, Pred,
|
2008-10-30 13:33:54 +08:00
|
|
|
BindBlkExpr(NewState, B, MakeConstantVal(0U, B)));
|
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.
|
2008-02-05 08:26:40 +08:00
|
|
|
|
2008-02-27 03:05:15 +08:00
|
|
|
X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, B, Pred, BindBlkExpr(St, B, X));
|
2008-02-27 03:05:15 +08:00
|
|
|
}
|
2008-01-30 07:32:35 +08:00
|
|
|
}
|
2008-02-27 03:05:15 +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
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* Ex, NodeTy* Pred, NodeSet& Dst,
|
|
|
|
bool asLValue) {
|
2008-02-07 12:16:04 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = 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)) {
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = StateMgr.GetLValue(St, VD);
|
2008-10-17 10:20:14 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
if (asLValue)
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
|
2008-10-16 14:09:51 +08:00
|
|
|
else
|
|
|
|
EvalLoad(Dst, Ex, Pred, St, V);
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) {
|
|
|
|
assert(!asLValue && "EnumConstantDecl does not have lvalue.");
|
|
|
|
|
|
|
|
BasicValueFactory& BasicVals = StateMgr.getBasicVals();
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = nonloc::ConcreteInt(BasicVals.getValue(ED->getInitVal()));
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, BindExpr(St, 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);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = loc::FuncVal(FD);
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V));
|
2008-10-16 14:09:51 +08:00
|
|
|
return;
|
2008-04-30 05:04:26 +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
|
|
|
|
void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, NodeTy* Pred,
|
2008-10-16 14:09:51 +08:00
|
|
|
NodeSet& Dst, bool asLValue) {
|
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();
|
2008-04-30 05:04:26 +08:00
|
|
|
NodeSet Tmp;
|
2008-10-17 08:51:01 +08:00
|
|
|
Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal.
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2008-10-17 08:51:01 +08:00
|
|
|
for (NodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) {
|
2008-04-30 07:24:44 +08:00
|
|
|
NodeSet Tmp2;
|
2008-10-17 08:51:01 +08:00
|
|
|
Visit(Idx, *I1, Tmp2); // Evaluate the index.
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2008-04-30 07:24:44 +08:00
|
|
|
for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2!=E2; ++I2) {
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I2);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = StateMgr.GetLValue(St, GetSVal(St, Base), GetSVal(St, Idx));
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
if (asLValue)
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, A, *I2, BindExpr(St, A, V));
|
2008-04-30 07:24:44 +08:00
|
|
|
else
|
|
|
|
EvalLoad(Dst, A, *I2, St, V);
|
|
|
|
}
|
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.
|
|
|
|
void GRExprEngine::VisitMemberExpr(MemberExpr* M, NodeTy* Pred,
|
2008-10-16 14:09:51 +08:00
|
|
|
NodeSet& Dst, bool asLValue) {
|
2008-04-22 07:43:38 +08:00
|
|
|
|
|
|
|
Expr* Base = M->getBase()->IgnoreParens();
|
|
|
|
NodeSet Tmp;
|
2008-10-18 11:28:48 +08:00
|
|
|
|
|
|
|
if (M->isArrow())
|
|
|
|
Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f
|
|
|
|
else
|
|
|
|
VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f
|
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = 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
|
|
|
|
// later when using FieldOffset lvals (which we no longer have).
|
2008-10-22 17:00:19 +08:00
|
|
|
SVal L = StateMgr.GetLValue(St, GetSVal(St, Base), M->getMemberDecl());
|
2008-10-17 08:51:01 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
if (asLValue)
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, M, *I, BindExpr(St, M, L));
|
2008-10-16 14:09:51 +08:00
|
|
|
else
|
|
|
|
EvalLoad(Dst, M, *I, St, L);
|
2008-04-22 07:43:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
|
2008-10-17 13:57:07 +08:00
|
|
|
const GRState* St, SVal location, SVal Val) {
|
2008-04-17 02:39:06 +08:00
|
|
|
|
|
|
|
assert (Builder && "GRStmtNodeBuilder must be defined.");
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Evaluate the location (checks for bad dereferences).
|
|
|
|
St = EvalLocation(Ex, Pred, St, location);
|
|
|
|
|
|
|
|
if (!St)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Proceed with the store.
|
|
|
|
|
2008-04-17 02:39:06 +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);
|
2008-09-20 09:50:34 +08:00
|
|
|
SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind);
|
2008-04-24 04:12:28 +08:00
|
|
|
SaveOr OldHasGen(Builder->HasGeneratedNode);
|
2008-04-19 04:35:30 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
assert (!location.isUndef());
|
2008-09-20 09:50:34 +08:00
|
|
|
Builder->PointKind = ProgramPoint::PostStoreKind;
|
2008-04-17 04:40:59 +08:00
|
|
|
|
2008-07-18 07:15:45 +08:00
|
|
|
getTF().EvalStore(Dst, *this, *Builder, Ex, Pred, St, location, Val);
|
2008-04-17 02:39:06 +08:00
|
|
|
|
|
|
|
// Handle the case where no nodes where generated. Auto-generate that
|
|
|
|
// contains the updated state if we aren't generating sinks.
|
|
|
|
|
2008-04-19 04:35:30 +08:00
|
|
|
if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
|
2008-07-18 07:15:45 +08:00
|
|
|
getTF().GRTransferFuncs::EvalStore(Dst, *this, *Builder, Ex, Pred, St,
|
2008-04-30 05:04:26 +08:00
|
|
|
location, Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::EvalLoad(NodeSet& Dst, Expr* Ex, NodeTy* Pred,
|
2008-10-17 13:57:07 +08:00
|
|
|
const GRState* St, SVal location,
|
2008-07-11 06:03:41 +08:00
|
|
|
bool CheckOnly) {
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// Evaluate the location (checks for bad dereferences).
|
|
|
|
|
|
|
|
St = EvalLocation(Ex, Pred, St, location, true);
|
|
|
|
|
|
|
|
if (!St)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// FIXME: The "CheckOnly" option exists only because Array and Field
|
|
|
|
// loads aren't fully implemented. Eventually this option will go away.
|
2008-11-28 16:34:30 +08:00
|
|
|
assert(!CheckOnly);
|
2008-08-29 02:43:46 +08:00
|
|
|
|
2008-04-30 12:23:07 +08:00
|
|
|
if (CheckOnly)
|
2008-08-29 02:43:46 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, St, K);
|
2008-04-30 12:23:07 +08:00
|
|
|
else if (location.isUnknown()) {
|
|
|
|
// This is important. We must nuke the old binding.
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, UnknownVal()), K);
|
2008-04-30 12:23:07 +08:00
|
|
|
}
|
2008-11-28 16:34:30 +08:00
|
|
|
else {
|
|
|
|
SVal V = GetSVal(St, cast<Loc>(location), Ex->getType());
|
|
|
|
MakeNode(Dst, Ex, Pred, BindExpr(St, Ex, V), K);
|
|
|
|
}
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
|
|
|
|
2008-09-20 09:50:34 +08:00
|
|
|
void GRExprEngine::EvalStore(NodeSet& Dst, Expr* Ex, Expr* StoreE, NodeTy* Pred,
|
2008-10-17 13:57:07 +08:00
|
|
|
const GRState* St, SVal location, SVal Val) {
|
2008-09-20 09:50:34 +08:00
|
|
|
|
|
|
|
NodeSet TmpDst;
|
|
|
|
EvalStore(TmpDst, StoreE, Pred, St, location, Val);
|
|
|
|
|
|
|
|
for (NodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I)
|
|
|
|
MakeNode(Dst, Ex, *I, (*I)->getState());
|
|
|
|
}
|
|
|
|
|
2008-11-15 03:47:18 +08:00
|
|
|
const GRState* GRExprEngine::EvalLocation(Stmt* Ex, NodeTy* Pred,
|
2008-10-30 13:33:54 +08:00
|
|
|
const GRState* St,
|
|
|
|
SVal location, bool isLoad) {
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// Check for loads/stores from/to undefined values.
|
|
|
|
if (location.isUndef()) {
|
2008-06-18 13:34:07 +08:00
|
|
|
ProgramPoint::Kind K =
|
|
|
|
isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
|
|
|
|
|
|
|
|
if (NodeTy* Succ = Builder->generateNode(Ex, St, Pred, K)) {
|
2008-04-30 05:04:26 +08:00
|
|
|
Succ->markAsSink();
|
|
|
|
UndefDeref.insert(Succ);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for loads/stores from/to unknown locations. Treat as No-Ops.
|
|
|
|
if (location.isUnknown())
|
|
|
|
return St;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// We add these assumptions.
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
Loc LV = cast<Loc>(location);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// "Assume" that the pointer is not NULL.
|
|
|
|
|
|
|
|
bool isFeasibleNotNull = false;
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* StNotNull = Assume(St, LV, true, isFeasibleNotNull);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// "Assume" that the pointer is NULL.
|
|
|
|
|
|
|
|
bool isFeasibleNull = false;
|
2008-09-19 07:09:54 +08:00
|
|
|
GRStateRef StNull = GRStateRef(Assume(St, LV, false, isFeasibleNull),
|
|
|
|
getStateManager());
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
if (isFeasibleNull) {
|
|
|
|
|
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);
|
2008-09-19 07:09:54 +08:00
|
|
|
StNull = StNull.set<GRState::NullDerefTag>(PersistentLV);
|
|
|
|
|
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.
|
|
|
|
|
2008-06-18 13:34:07 +08:00
|
|
|
ProgramPoint::Kind K =
|
|
|
|
isLoad ? ProgramPoint::PostLoadKind : ProgramPoint::PostStmtKind;
|
|
|
|
|
|
|
|
NodeTy* NullNode = Builder->generateNode(Ex, StNull, Pred, K);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
if (NullNode) {
|
|
|
|
|
|
|
|
NullNode->markAsSink();
|
|
|
|
|
|
|
|
if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
|
|
|
|
else ExplicitNullDeref.insert(NullNode);
|
|
|
|
}
|
|
|
|
}
|
2008-11-08 11:45:42 +08:00
|
|
|
|
|
|
|
// Check for out-of-bound array access.
|
|
|
|
if (isFeasibleNotNull && isa<loc::MemRegionVal>(LV)) {
|
|
|
|
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
|
|
|
|
|
|
|
bool isFeasibleInBound = false;
|
|
|
|
const GRState* StInBound = AssumeInBound(StNotNull, Idx, NumElements,
|
|
|
|
true, isFeasibleInBound);
|
|
|
|
|
|
|
|
bool isFeasibleOutBound = false;
|
|
|
|
const GRState* StOutBound = AssumeInBound(StNotNull, Idx, NumElements,
|
|
|
|
false, isFeasibleOutBound);
|
|
|
|
|
2008-11-22 21:21:46 +08:00
|
|
|
if (isFeasibleOutBound) {
|
|
|
|
// Report warning.
|
|
|
|
|
2008-11-23 13:52:28 +08:00
|
|
|
// Make sink node manually.
|
|
|
|
ProgramPoint::Kind K = isLoad ? ProgramPoint::PostLoadKind
|
|
|
|
: ProgramPoint::PostStoreKind;
|
|
|
|
|
|
|
|
NodeTy* OOBNode = Builder->generateNode(Ex, StOutBound, Pred, K);
|
|
|
|
|
|
|
|
if (OOBNode) {
|
|
|
|
OOBNode->markAsSink();
|
|
|
|
|
|
|
|
if (isFeasibleInBound)
|
|
|
|
ImplicitOOBMemAccesses.insert(OOBNode);
|
|
|
|
else
|
|
|
|
ExplicitOOBMemAccesses.insert(OOBNode);
|
|
|
|
}
|
2008-11-22 21:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return isFeasibleInBound ? StInBound : NULL;
|
2008-11-08 11:45:42 +08:00
|
|
|
}
|
|
|
|
}
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
return isFeasibleNotNull ? StNotNull : NULL;
|
2008-04-17 02:39:06 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function: Function calls.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-19 09:44:53 +08:00
|
|
|
void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
|
2008-02-22 02:02:17 +08:00
|
|
|
CallExpr::arg_iterator AI,
|
|
|
|
CallExpr::arg_iterator AE,
|
2008-10-28 08:22:11 +08:00
|
|
|
NodeSet& Dst)
|
|
|
|
{
|
|
|
|
// Determine the type of function we're calling (if available).
|
|
|
|
const FunctionTypeProto *Proto = NULL;
|
|
|
|
QualType FnType = CE->getCallee()->IgnoreParens()->getType();
|
|
|
|
if (const PointerType *FnTypePtr = FnType->getAsPointerType())
|
|
|
|
Proto = FnTypePtr->getPointeeType()->getAsFunctionTypeProto();
|
|
|
|
|
|
|
|
VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitCallRec(CallExpr* CE, NodeTy* Pred,
|
|
|
|
CallExpr::arg_iterator AI,
|
|
|
|
CallExpr::arg_iterator AE,
|
|
|
|
NodeSet& Dst, const FunctionTypeProto *Proto,
|
|
|
|
unsigned ParamIdx) {
|
2008-02-19 09:44:53 +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();
|
|
|
|
|
|
|
|
NodeSet DstTmp;
|
|
|
|
if (VisitAsLvalue)
|
|
|
|
VisitLValue(*AI, Pred, DstTmp);
|
|
|
|
else
|
|
|
|
Visit(*AI, Pred, DstTmp);
|
2008-02-22 02:02:17 +08:00
|
|
|
++AI;
|
2008-02-19 09:44:53 +08:00
|
|
|
|
2008-03-05 06:01:56 +08:00
|
|
|
for (NodeSet::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);
|
2008-02-19 09:44:53 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here we have processed all of the arguments. Evaluate
|
|
|
|
// the callee expression.
|
2008-03-04 00:47:31 +08:00
|
|
|
|
2008-02-26 05:16:03 +08:00
|
|
|
NodeSet DstTmp;
|
2008-04-24 04:12:28 +08:00
|
|
|
Expr* Callee = CE->getCallee()->IgnoreParens();
|
2008-03-04 00:47:31 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
Visit(Callee, Pred, DstTmp);
|
2008-03-04 00:47:31 +08:00
|
|
|
|
2008-02-19 09:44:53 +08:00
|
|
|
// Finally, evaluate the function call.
|
2008-02-22 02:02:17 +08:00
|
|
|
for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*DI);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal L = GetSVal(St, 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).
|
|
|
|
|
|
|
|
// Check for undefined control-flow or calls to NULL.
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (L.isUndef() || isa<loc::ConcreteInt>(L)) {
|
2008-02-19 09:44:53 +08:00
|
|
|
NodeTy* N = Builder->generateNode(CE, St, *DI);
|
2008-03-05 06:01:56 +08:00
|
|
|
|
2008-03-01 07:53:11 +08:00
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
BadCalls.insert(N);
|
|
|
|
}
|
2008-03-05 06:01:56 +08:00
|
|
|
|
2008-02-19 09:44:53 +08:00
|
|
|
continue;
|
2008-03-06 05:15:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for the "noreturn" attribute.
|
|
|
|
|
|
|
|
SaveAndRestore<bool> OldSink(Builder->BuildSinks);
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<loc::FuncVal>(L)) {
|
2008-03-15 05:58:42 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
FunctionDecl* FD = cast<loc::FuncVal>(L).getDecl();
|
2008-03-15 05:58:42 +08:00
|
|
|
|
|
|
|
if (FD->getAttr<NoReturnAttr>())
|
2008-03-06 05:15:02 +08:00
|
|
|
Builder->BuildSinks = true;
|
2008-03-15 05:58:42 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
default:
|
|
|
|
break;
|
2008-03-15 07:25:49 +08:00
|
|
|
|
2008-03-15 05:58:42 +08:00
|
|
|
case 4:
|
2008-03-15 07:25:49 +08:00
|
|
|
if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true;
|
2008-10-07 18:06:03 +08:00
|
|
|
else if (!memcmp(s, "error", 5)) {
|
2008-10-09 11:19:06 +08:00
|
|
|
if (CE->getNumArgs() > 0) {
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = GetSVal(St, *CE->arg_begin());
|
2008-10-09 11:19:06 +08:00
|
|
|
// FIXME: use Assume to inspect the possible symbolic value of
|
|
|
|
// X. Also check the specific signature of error().
|
2008-10-17 13:57:07 +08:00
|
|
|
nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X);
|
2008-10-09 11:19:06 +08:00
|
|
|
if (CI && CI->getValue() != 0)
|
2008-10-07 18:06:03 +08:00
|
|
|
Builder->BuildSinks = true;
|
2008-10-09 11:19:06 +08:00
|
|
|
}
|
2008-10-07 18:06:03 +08:00
|
|
|
}
|
2008-03-15 07:25:49 +08:00
|
|
|
break;
|
2008-04-22 13:37:33 +08:00
|
|
|
|
|
|
|
case 6:
|
2008-05-17 08:42:01 +08:00
|
|
|
if (!memcmp(s, "Assert", 6)) {
|
|
|
|
Builder->BuildSinks = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-05-01 23:55:59 +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;
|
|
|
|
|
2008-04-23 08:41:25 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true;
|
2008-04-22 13:37:33 +08:00
|
|
|
break;
|
2008-04-22 14:09:33 +08:00
|
|
|
|
2008-05-01 01:54:04 +08:00
|
|
|
case 8:
|
|
|
|
if (!memcmp(s ,"db_error", 8)) Builder->BuildSinks = true;
|
|
|
|
break;
|
2008-05-02 01:52:49 +08:00
|
|
|
|
|
|
|
case 12:
|
|
|
|
if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true;
|
|
|
|
break;
|
2008-05-01 01:54:04 +08:00
|
|
|
|
2008-09-19 10:30:47 +08:00
|
|
|
case 13:
|
|
|
|
if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true;
|
|
|
|
break;
|
|
|
|
|
2008-04-22 14:09:33 +08:00
|
|
|
case 14:
|
2008-10-30 08:00:57 +08:00
|
|
|
if (!memcmp(s, "dtrace_assfail", 14) ||
|
|
|
|
!memcmp(s, "yy_fatal_error", 14))
|
|
|
|
Builder->BuildSinks = true;
|
2008-04-22 14:09:33 +08:00
|
|
|
break;
|
2008-05-17 08:33:23 +08:00
|
|
|
|
|
|
|
case 26:
|
2008-07-19 00:28:33 +08:00
|
|
|
if (!memcmp(s, "_XCAssertionFailureHandler", 26) ||
|
|
|
|
!memcmp(s, "_DTAssertionFailureHandler", 26))
|
2008-05-17 08:40:45 +08:00
|
|
|
Builder->BuildSinks = true;
|
2008-07-19 00:28:33 +08:00
|
|
|
|
2008-05-17 08:33:23 +08:00
|
|
|
break;
|
2008-03-15 05:58:42 +08:00
|
|
|
}
|
2008-04-22 14:09:33 +08:00
|
|
|
|
2008-03-15 05:58:42 +08:00
|
|
|
}
|
|
|
|
}
|
2008-03-06 05:15:02 +08:00
|
|
|
|
|
|
|
// Evaluate the call.
|
2008-04-24 04:12:28 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<loc::FuncVal>(L)) {
|
2008-03-05 06:01:56 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
IdentifierInfo* Info = cast<loc::FuncVal>(L).getDecl()->getIdentifier();
|
2008-03-05 06:01:56 +08:00
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
if (unsigned id = Info->getBuiltinID())
|
2008-03-06 06:59:42 +08:00
|
|
|
switch (id) {
|
|
|
|
case Builtin::BI__builtin_expect: {
|
|
|
|
// For __builtin_expect, just return the value of the subexpression.
|
|
|
|
assert (CE->arg_begin() != CE->arg_end());
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = GetSVal(St, *(CE->arg_begin()));
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, CE, *DI, BindExpr(St, CE, X));
|
2008-03-06 06:59:42 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-02 08:35:01 +08:00
|
|
|
case Builtin::BI__builtin_alloca: {
|
|
|
|
// FIXME: Refactor into StoreManager itself?
|
|
|
|
MemRegionManager& RM = getStateManager().getRegionManager();
|
|
|
|
const MemRegion* R =
|
2008-11-13 15:58:20 +08:00
|
|
|
RM.getAllocaRegion(CE, Builder->getCurrentBlockCount());
|
2008-11-24 17:44:56 +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 = GetSVal(St, *(CE->arg_begin()));
|
|
|
|
St = getStoreManager().setExtent(St, R, Extent);
|
|
|
|
|
2008-11-02 08:35:01 +08:00
|
|
|
MakeNode(Dst, CE, *DI, BindExpr(St, CE, loc::MemRegionVal(R)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-06 06:59:42 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-02-22 03:46:04 +08:00
|
|
|
}
|
2008-03-05 06:01:56 +08:00
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
// Check any arguments passed-by-value against being undefined.
|
2008-03-05 06:01:56 +08:00
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
bool badArg = false;
|
|
|
|
|
|
|
|
for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
|
|
|
|
I != E; ++I) {
|
2008-03-05 06:01:56 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (GetSVal(GetState(*DI), *I).isUndef()) {
|
2008-04-24 04:12:28 +08:00
|
|
|
NodeTy* N = Builder->generateNode(CE, GetState(*DI), *DI);
|
|
|
|
|
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
UndefArgs[N] = *I;
|
2008-03-05 06:01:56 +08:00
|
|
|
}
|
|
|
|
|
2008-04-24 04:12:28 +08:00
|
|
|
badArg = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-04-16 07:06:53 +08:00
|
|
|
}
|
2008-04-24 04:12:28 +08:00
|
|
|
|
|
|
|
if (badArg)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Dispatch to the plug-in transfer function.
|
|
|
|
|
|
|
|
unsigned size = Dst.size();
|
|
|
|
SaveOr OldHasGen(Builder->HasGeneratedNode);
|
|
|
|
EvalCall(Dst, CE, L, *DI);
|
|
|
|
|
|
|
|
// Handle the case where no nodes where generated. Auto-generate that
|
|
|
|
// contains the updated state if we aren't generating sinks.
|
|
|
|
|
|
|
|
if (!Builder->BuildSinks && Dst.size() == size &&
|
|
|
|
!Builder->HasGeneratedNode)
|
|
|
|
MakeNode(Dst, CE, *DI, St);
|
2008-04-16 07:06:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-17 08:03:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function: Objective-C ivar references.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex,
|
|
|
|
NodeTy* Pred, NodeSet& Dst,
|
|
|
|
bool asLValue) {
|
|
|
|
|
|
|
|
Expr* Base = cast<Expr>(Ex->getBase());
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(Base, Pred, Tmp);
|
|
|
|
|
|
|
|
for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
|
|
|
const GRState* St = GetState(*I);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal BaseVal = GetSVal(St, Base);
|
|
|
|
SVal location = StateMgr.GetLValue(St, Ex->getDecl(), BaseVal);
|
2008-10-17 08:03:18 +08:00
|
|
|
|
|
|
|
if (asLValue)
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, Ex, *I, BindExpr(St, Ex, location));
|
2008-10-17 08:03:18 +08:00
|
|
|
else
|
|
|
|
EvalLoad(Dst, Ex, *I, St, location);
|
|
|
|
}
|
2008-11-13 03:24:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function: Objective-C fast enumeration 'for' statements.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S,
|
|
|
|
NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
|
|
|
|
// ObjCForCollectionStmts are processed in two places. This method
|
|
|
|
// handles the case where an ObjCForCollectionStmt* occurs as one of the
|
|
|
|
// statements within a basic block. This transfer function does two things:
|
|
|
|
//
|
|
|
|
// (1) binds the next container value to 'element'. This creates a new
|
|
|
|
// node in the ExplodedGraph.
|
|
|
|
//
|
|
|
|
// (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating
|
|
|
|
// whether or not the container has any more elements. This value
|
|
|
|
// will be tested in ProcessBranch. We need to explicitly bind
|
|
|
|
// this value because a container can contain nil elements.
|
|
|
|
//
|
|
|
|
// FIXME: Eventually this logic should actually do dispatches to
|
|
|
|
// 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration).
|
|
|
|
// This will require simulating a temporary NSFastEnumerationState, either
|
|
|
|
// through an SVal or through the use of MemRegions. This value can
|
|
|
|
// be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop
|
|
|
|
// terminates we reclaim the temporary (it goes out of scope) and we
|
|
|
|
// we can test if the SVal is 0 or if the MemRegion is null (depending
|
|
|
|
// on what approach we take).
|
|
|
|
//
|
|
|
|
// For now: simulate (1) by assigning either a symbol or nil if the
|
|
|
|
// container is empty. Thus this transfer function will by default
|
|
|
|
// result in state splitting.
|
|
|
|
|
2008-11-15 03:47:18 +08:00
|
|
|
Stmt* elem = S->getElement();
|
|
|
|
SVal ElementV;
|
2008-11-13 03:24:17 +08:00
|
|
|
|
|
|
|
if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) {
|
2008-11-15 03:47:18 +08:00
|
|
|
VarDecl* ElemD = cast<VarDecl>(DS->getSolitaryDecl());
|
2008-11-13 03:24:17 +08:00
|
|
|
assert (ElemD->getInit() == 0);
|
2008-11-15 03:47:18 +08:00
|
|
|
ElementV = getStateManager().GetLValue(GetState(Pred), ElemD);
|
|
|
|
VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV);
|
|
|
|
return;
|
2008-11-13 03:24:17 +08:00
|
|
|
}
|
2008-11-15 03:47:18 +08:00
|
|
|
|
|
|
|
NodeSet Tmp;
|
|
|
|
VisitLValue(cast<Expr>(elem), Pred, Tmp);
|
2008-11-13 03:24:17 +08:00
|
|
|
|
2008-11-15 03:47:18 +08:00
|
|
|
for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
|
|
|
|
const GRState* state = GetState(*I);
|
|
|
|
VisitObjCForCollectionStmtAux(S, *I, Dst, GetSVal(state, elem));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S,
|
|
|
|
NodeTy* Pred, NodeSet& Dst,
|
|
|
|
SVal ElementV) {
|
|
|
|
|
|
|
|
|
2008-11-13 03:24:17 +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();
|
|
|
|
GRStateRef state = GRStateRef(EvalLocation(elem, Pred, GetState(Pred),
|
|
|
|
ElementV, false),
|
|
|
|
getStateManager());
|
2008-11-13 03:24:17 +08:00
|
|
|
|
2008-11-15 03:47:18 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
// Handle the case where the container still has elements.
|
2008-11-15 03:47:18 +08:00
|
|
|
QualType IntTy = getContext().IntTy;
|
2008-11-13 03:24:17 +08:00
|
|
|
SVal TrueV = NonLoc::MakeVal(getBasicVals(), 1, IntTy);
|
|
|
|
GRStateRef hasElems = state.BindExpr(S, TrueV);
|
|
|
|
|
|
|
|
// Handle the case where the container has no elements.
|
2008-11-13 05:12:46 +08:00
|
|
|
SVal FalseV = NonLoc::MakeVal(getBasicVals(), 0, IntTy);
|
|
|
|
GRStateRef noElems = state.BindExpr(S, FalseV);
|
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.
|
|
|
|
QualType T = R->getType(getContext());
|
|
|
|
assert (Loc::IsLocType(T));
|
|
|
|
unsigned Count = Builder->getCurrentBlockCount();
|
|
|
|
loc::SymbolVal SymV(SymMgr.getConjuredSymbol(elem, T, Count));
|
|
|
|
hasElems = hasElems.BindLoc(ElementV, SymV);
|
|
|
|
|
|
|
|
// Bind the location to 'nil' on the false branch.
|
|
|
|
SVal nilV = loc::ConcreteInt(getBasicVals().getValue(0, T));
|
|
|
|
noElems = noElems.BindLoc(ElementV, nilV);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, NodeTy* Pred,
|
|
|
|
NodeSet& Dst){
|
|
|
|
|
|
|
|
VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(),
|
|
|
|
Pred, Dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME,
|
2008-10-31 15:26:14 +08:00
|
|
|
ObjCMessageExpr::arg_iterator AI,
|
|
|
|
ObjCMessageExpr::arg_iterator AE,
|
|
|
|
NodeTy* Pred, NodeSet& Dst) {
|
2008-04-16 07:06:53 +08:00
|
|
|
if (AI == AE) {
|
|
|
|
|
|
|
|
// Process the receiver.
|
|
|
|
|
|
|
|
if (Expr* Receiver = ME->getReceiver()) {
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(Receiver, Pred, Tmp);
|
|
|
|
|
|
|
|
for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
|
|
|
|
VisitObjCMessageExprDispatchHelper(ME, *NI, Dst);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VisitObjCMessageExprDispatchHelper(ME, Pred, Dst);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(*AI, Pred, Tmp);
|
|
|
|
|
|
|
|
++AI;
|
|
|
|
|
|
|
|
for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
|
|
|
|
VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME,
|
|
|
|
NodeTy* Pred,
|
|
|
|
NodeSet& Dst) {
|
|
|
|
|
|
|
|
// FIXME: More logic for the processing the method call.
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(Pred);
|
2008-05-02 02:33:28 +08:00
|
|
|
bool RaisesException = false;
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
if (Expr* Receiver = ME->getReceiver()) {
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal L = GetSVal(St, Receiver);
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
// Check for undefined control-flow or calls to NULL.
|
|
|
|
|
|
|
|
if (L.isUndef()) {
|
|
|
|
NodeTy* N = Builder->generateNode(ME, St, Pred);
|
|
|
|
|
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
UndefReceivers.insert(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2008-05-02 02:33:28 +08:00
|
|
|
|
|
|
|
// Check if the "raise" message was sent.
|
|
|
|
if (ME->getSelector() == RaiseSel)
|
|
|
|
RaisesException = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
IdentifierInfo* ClsName = ME->getClassName();
|
|
|
|
Selector S = ME->getSelector();
|
|
|
|
|
|
|
|
// Check for special instance methods.
|
|
|
|
|
|
|
|
if (!NSExceptionII) {
|
|
|
|
ASTContext& Ctx = getContext();
|
|
|
|
|
|
|
|
NSExceptionII = &Ctx.Idents.get("NSException");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClsName == NSExceptionII) {
|
|
|
|
|
|
|
|
enum { NUM_RAISE_SELECTORS = 2 };
|
|
|
|
|
|
|
|
// Lazily create a cache of the selectors.
|
|
|
|
|
|
|
|
if (!NSExceptionInstanceRaiseSelectors) {
|
|
|
|
|
|
|
|
ASTContext& Ctx = getContext();
|
|
|
|
|
|
|
|
NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS];
|
|
|
|
|
|
|
|
llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II;
|
|
|
|
unsigned idx = 0;
|
|
|
|
|
|
|
|
// raise:format:
|
2008-05-03 01:12:56 +08:00
|
|
|
II.push_back(&Ctx.Idents.get("raise"));
|
|
|
|
II.push_back(&Ctx.Idents.get("format"));
|
2008-05-02 02:33:28 +08:00
|
|
|
NSExceptionInstanceRaiseSelectors[idx++] =
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i)
|
|
|
|
if (S == NSExceptionInstanceRaiseSelectors[i]) {
|
|
|
|
RaisesException = true; break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-16 07:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for any arguments that are uninitialized/undefined.
|
|
|
|
|
|
|
|
for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (GetSVal(St, *I).isUndef()) {
|
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.
|
|
|
|
NodeTy* N = Builder->generateNode(ME, St, Pred);
|
|
|
|
|
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
MsgExprUndefArgs[N] = *I;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
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.
|
|
|
|
|
|
|
|
SaveAndRestore<bool> OldSink(Builder->BuildSinks);
|
|
|
|
|
|
|
|
if (RaisesException)
|
|
|
|
Builder->BuildSinks = true;
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
// Dispatch to plug-in transfer function.
|
|
|
|
|
|
|
|
unsigned size = Dst.size();
|
2008-04-24 04:12:28 +08:00
|
|
|
SaveOr OldHasGen(Builder->HasGeneratedNode);
|
2008-04-19 04:35:30 +08:00
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
EvalObjCMessageExpr(Dst, ME, Pred);
|
|
|
|
|
|
|
|
// Handle the case where no nodes where generated. Auto-generate that
|
|
|
|
// contains the updated state if we aren't generating sinks.
|
|
|
|
|
2008-04-19 04:35:30 +08:00
|
|
|
if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode)
|
2008-04-16 07:06:53 +08:00
|
|
|
MakeNode(Dst, ME, Pred, St);
|
2008-02-19 09:44:53 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 07:06:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer functions: Miscellaneous statements.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
|
2008-02-20 02:52:54 +08:00
|
|
|
NodeSet S1;
|
|
|
|
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);
|
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
// Check for casting to "void".
|
|
|
|
if (T->isVoidType()) {
|
2008-02-20 02:52:54 +08:00
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
for (NodeSet::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;
|
|
|
|
}
|
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
// FIXME: The rest of this should probably just go into EvalCall, and
|
|
|
|
// let the transfer function object be responsible for constructing
|
|
|
|
// nodes.
|
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
|
2008-01-24 10:02:54 +08:00
|
|
|
NodeTy* N = *I1;
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(N);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = GetSVal(St, Ex);
|
2008-04-23 05:10:18 +08:00
|
|
|
|
|
|
|
// Unknown?
|
|
|
|
|
|
|
|
if (V.isUnknown()) {
|
|
|
|
Dst.Add(N);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Undefined?
|
|
|
|
|
|
|
|
if (V.isUndef()) {
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
|
2008-04-23 05:10:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2008-09-20 04:51:22 +08:00
|
|
|
|
|
|
|
// For const casts, just propagate the value.
|
|
|
|
ASTContext& C = getContext();
|
|
|
|
|
|
|
|
if (C.getCanonicalType(T).getUnqualifiedType() ==
|
|
|
|
C.getCanonicalType(ExTy).getUnqualifiedType()) {
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
|
2008-09-20 04:51:22 +08:00
|
|
|
continue;
|
|
|
|
}
|
2008-04-23 05:10:18 +08:00
|
|
|
|
|
|
|
// Check for casts from pointers to integers.
|
2008-10-17 13:57:07 +08:00
|
|
|
if (T->isIntegerType() && Loc::IsLocType(ExTy)) {
|
2008-04-23 05:10:18 +08:00
|
|
|
unsigned bits = getContext().getTypeSize(ExTy);
|
|
|
|
|
|
|
|
// FIXME: Determine if the number of bits of the target type is
|
|
|
|
// equal or exceeds the number of bits to store the pointer value.
|
|
|
|
// If not, flag an error.
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
V = nonloc::LocAsInteger::Make(getBasicVals(), cast<Loc>(V), bits);
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
|
2008-04-23 05:10:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for casts from integers to pointers.
|
2008-10-17 13:57:07 +08:00
|
|
|
if (Loc::IsLocType(T) && ExTy->isIntegerType())
|
|
|
|
if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&V)) {
|
2008-04-23 05:10:18 +08:00
|
|
|
// Just unpackage the lval and return it.
|
2008-10-17 13:57:07 +08:00
|
|
|
V = LV->getLoc();
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
|
2008-04-23 05:10:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2008-10-23 11:10:39 +08:00
|
|
|
|
2008-11-14 17:23:38 +08:00
|
|
|
// Check for casts from array type to pointer type.
|
2008-10-23 11:10:39 +08:00
|
|
|
if (ExTy->isArrayType()) {
|
2008-11-15 13:00:27 +08:00
|
|
|
assert(T->isPointerType());
|
2008-10-23 11:10:39 +08:00
|
|
|
V = StateMgr.ArrayToPointer(V);
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, CastE, N, BindExpr(St, CastE, V));
|
2008-10-23 11:10:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-16 12:07:26 +08:00
|
|
|
// Check for casts from AllocaRegion pointer to typed pointer.
|
|
|
|
if (isa<loc::MemRegionVal>(V)) {
|
|
|
|
assert(Loc::IsLocType(T));
|
|
|
|
assert(Loc::IsLocType(ExTy));
|
|
|
|
|
|
|
|
// Delegate to store manager.
|
2008-11-16 15:06:26 +08:00
|
|
|
std::pair<const GRState*, SVal> Res =
|
|
|
|
getStoreManager().CastRegion(St, V, T, CastE);
|
|
|
|
|
|
|
|
const GRState* NewSt = Res.first;
|
|
|
|
SVal NewPtr = Res.second;
|
2008-11-16 12:07:26 +08:00
|
|
|
|
|
|
|
// If no new region is created, fall through to the default case.
|
|
|
|
if (NewSt != St) {
|
2008-11-16 15:06:26 +08:00
|
|
|
MakeNode(Dst, CastE, N, BindExpr(NewSt, CastE, NewPtr));
|
2008-11-16 12:07:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
// All other cases.
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, CastE, N, BindExpr(St, CastE, EvalCast(V, CastE->getType())));
|
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,
|
2008-11-07 18:38:33 +08:00
|
|
|
NodeTy* Pred, NodeSet& Dst,
|
|
|
|
bool asLValue) {
|
2008-10-28 05:54:31 +08:00
|
|
|
InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens());
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(ILE, Pred, Tmp);
|
|
|
|
|
|
|
|
for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) {
|
2008-11-07 18:38:33 +08:00
|
|
|
const GRState* St = GetState(*I);
|
|
|
|
SVal ILV = GetSVal(St, ILE);
|
|
|
|
St = StateMgr.BindCompoundLiteral(St, CL, ILV);
|
2008-10-28 05:54:31 +08:00
|
|
|
|
2008-11-07 18:38:33 +08:00
|
|
|
if (asLValue)
|
|
|
|
MakeNode(Dst, CL, *I, BindExpr(St, CL, StateMgr.GetLValue(St, CL)));
|
|
|
|
else
|
|
|
|
MakeNode(Dst, CL, *I, BindExpr(St, CL, ILV));
|
2008-10-28 05:54:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-23 06:25:27 +08:00
|
|
|
void GRExprEngine::VisitDeclStmt(DeclStmt* DS, NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
|
2008-10-07 02:43:53 +08:00
|
|
|
// The CFG has one DeclStmt per Decl.
|
|
|
|
ScopedDecl* D = *DS->decl_begin();
|
2008-01-25 04:55:43 +08:00
|
|
|
|
2008-08-29 02:34:26 +08:00
|
|
|
if (!D || !isa<VarDecl>(D))
|
2008-04-23 06:25:27 +08:00
|
|
|
return;
|
|
|
|
|
2008-12-09 06:47:34 +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.
|
|
|
|
NodeSet Tmp;
|
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
if (InitEx)
|
|
|
|
Visit(InitEx, Pred, Tmp);
|
2008-08-29 02:34:26 +08:00
|
|
|
|
|
|
|
if (Tmp.empty())
|
|
|
|
Tmp.Add(Pred);
|
2008-04-23 06:25:27 +08:00
|
|
|
|
|
|
|
for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I);
|
2008-11-13 03:24:17 +08:00
|
|
|
unsigned Count = Builder->getCurrentBlockCount();
|
|
|
|
|
|
|
|
if (InitEx) {
|
|
|
|
SVal InitVal = GetSVal(St, InitEx);
|
|
|
|
QualType T = VD->getType();
|
|
|
|
|
|
|
|
// Recover some path-sensitivity if a scalar value evaluated to
|
|
|
|
// UnknownVal.
|
|
|
|
if (InitVal.isUnknown()) {
|
|
|
|
if (Loc::IsLocType(T)) {
|
2008-12-05 10:27:51 +08:00
|
|
|
SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
|
2008-11-13 03:24:17 +08:00
|
|
|
InitVal = loc::SymbolVal(Sym);
|
|
|
|
}
|
2008-11-13 14:10:40 +08:00
|
|
|
else if (T->isIntegerType() && T->isScalarType()) {
|
2008-12-05 10:27:51 +08:00
|
|
|
SymbolRef Sym = SymMgr.getConjuredSymbol(InitEx, Count);
|
2008-11-13 03:24:17 +08:00
|
|
|
InitVal = nonloc::SymbolVal(Sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
St = StateMgr.BindDecl(St, VD, &InitVal, Count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
St = StateMgr.BindDecl(St, VD, 0, Count);
|
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.
|
|
|
|
|
|
|
|
Expr* SE = VLA->getSizeExpr();
|
|
|
|
SVal Size = GetSVal(St, SE);
|
2008-12-09 08:44:16 +08:00
|
|
|
|
|
|
|
if (Size.isUndef()) {
|
|
|
|
if (NodeTy* N = Builder->generateNode(DS, St, Pred)) {
|
|
|
|
N->markAsSink();
|
|
|
|
ExplicitBadSizedVLA.insert(N);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2008-12-09 06:47:34 +08:00
|
|
|
|
|
|
|
bool isFeasibleZero = false;
|
|
|
|
const GRState* ZeroSt = Assume(St, Size, false, isFeasibleZero);
|
|
|
|
|
|
|
|
bool isFeasibleNotZero = false;
|
|
|
|
St = Assume(St, Size, true, isFeasibleNotZero);
|
|
|
|
|
|
|
|
if (isFeasibleZero) {
|
|
|
|
if (NodeTy* N = Builder->generateNode(DS, ZeroSt, Pred)) {
|
|
|
|
N->markAsSink();
|
2008-12-09 08:44:16 +08:00
|
|
|
if (isFeasibleNotZero) ImplicitBadSizedVLA.insert(N);
|
|
|
|
else ExplicitBadSizedVLA.insert(N);
|
2008-12-09 06:47:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isFeasibleNotZero)
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-13 03:24:17 +08:00
|
|
|
|
2008-08-29 02:34:26 +08:00
|
|
|
MakeNode(Dst, DS, *I, St);
|
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;
|
|
|
|
GRExprEngine::NodeTy* N;
|
|
|
|
InitListExpr::reverse_iterator Itr;
|
|
|
|
|
|
|
|
InitListWLItem(GRExprEngine::NodeTy* n, llvm::ImmutableList<SVal> vals,
|
|
|
|
InitListExpr::reverse_iterator itr)
|
|
|
|
: Vals(vals), N(n), Itr(itr) {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-30 13:02:23 +08:00
|
|
|
void GRExprEngine::VisitInitListExpr(InitListExpr* E, NodeTy* Pred,
|
|
|
|
NodeSet& Dst) {
|
|
|
|
|
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());
|
2008-10-31 01:47:32 +08:00
|
|
|
unsigned NumInitElements = E->getNumInits();
|
2008-10-30 13:02:23 +08:00
|
|
|
|
2008-10-30 13:35:59 +08:00
|
|
|
if (T->isArrayType() || T->isStructureType()) {
|
2008-10-30 13:02:23 +08:00
|
|
|
|
2008-10-31 07:14:36 +08:00
|
|
|
llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList();
|
2008-10-31 01:47:32 +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) {
|
|
|
|
SVal V = NonLoc::MakeCompoundVal(T, StartVals, getBasicVals());
|
|
|
|
MakeNode(Dst, E, Pred, BindExpr(state, E, V));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a worklist to process the initializers.
|
|
|
|
llvm::SmallVector<InitListWLItem, 10> WorkList;
|
|
|
|
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();
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(*X.Itr, X.N, Tmp);
|
|
|
|
|
|
|
|
InitListExpr::reverse_iterator NewItr = X.Itr + 1;
|
2008-10-30 13:02:23 +08:00
|
|
|
|
2008-10-31 01:47:32 +08:00
|
|
|
for (NodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
|
|
|
|
// Get the last initializer value.
|
|
|
|
state = GetState(*NI);
|
|
|
|
SVal InitV = GetSVal(state, cast<Expr>(*X.Itr));
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
if (NewItr == ItrEnd) {
|
2008-10-31 11:01:26 +08:00
|
|
|
// Now we have a list holding all init values. Make CompoundValData.
|
2008-10-31 01:47:32 +08:00
|
|
|
SVal V = NonLoc::MakeCompoundVal(T, NewVals, getBasicVals());
|
2008-10-30 13:02:23 +08:00
|
|
|
|
2008-10-31 01:47:32 +08:00
|
|
|
// Make final state and node.
|
2008-10-31 02:37:08 +08:00
|
|
|
MakeNode(Dst, E, *NI, BindExpr(state, 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-31 02:34:31 +08:00
|
|
|
|
|
|
|
return;
|
2008-10-30 13:02:23 +08:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:10:40 +08:00
|
|
|
if (T->isUnionType() || T->isVectorType()) {
|
|
|
|
// FIXME: to be implemented.
|
|
|
|
// Note: That vectors can return true for T->isIntegerType()
|
|
|
|
MakeNode(Dst, E, Pred, state);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-30 13:02:23 +08:00
|
|
|
if (Loc::IsLocType(T) || T->isIntegerType()) {
|
|
|
|
assert (E->getNumInits() == 1);
|
|
|
|
NodeSet Tmp;
|
|
|
|
Expr* Init = E->getInit(0);
|
|
|
|
Visit(Init, Pred, Tmp);
|
|
|
|
for (NodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) {
|
|
|
|
state = GetState(*I);
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, E, *I, BindExpr(state, E, GetSVal(state, 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,
|
|
|
|
NodeTy* Pred,
|
|
|
|
NodeSet& Dst) {
|
|
|
|
QualType T = Ex->getTypeOfArgument();
|
2008-03-15 11:13:20 +08:00
|
|
|
uint64_t amt;
|
2008-02-22 02:15:29 +08:00
|
|
|
|
2008-03-15 11:13:20 +08:00
|
|
|
if (Ex->isSizeOf()) {
|
|
|
|
|
|
|
|
// FIXME: Add support for VLAs.
|
|
|
|
if (!T.getTypePtr()->isConstantSizeType())
|
|
|
|
return;
|
|
|
|
|
2008-05-01 05:31:12 +08:00
|
|
|
// Some code tries to take the sizeof an ObjCInterfaceType, relying that
|
|
|
|
// the compiler has laid out its representation. Just report Unknown
|
|
|
|
// for these.
|
|
|
|
if (T->isObjCInterfaceType())
|
|
|
|
return;
|
|
|
|
|
2008-03-15 11:13:20 +08:00
|
|
|
amt = 1; // Handle sizeof(void)
|
|
|
|
|
|
|
|
if (T != getContext().VoidTy)
|
|
|
|
amt = getContext().getTypeSize(T) / 8;
|
|
|
|
|
|
|
|
}
|
|
|
|
else // Get alignment of the type.
|
2008-03-15 11:13:55 +08:00
|
|
|
amt = getContext().getTypeAlign(T) / 8;
|
2008-02-13 03:49:57 +08:00
|
|
|
|
2008-03-22 05:30:14 +08:00
|
|
|
MakeNode(Dst, Ex, Pred,
|
2008-10-30 13:33:54 +08:00
|
|
|
BindExpr(GetState(Pred), Ex,
|
|
|
|
NonLoc::MakeVal(getBasicVals(), amt, Ex->getType())));
|
2008-04-22 07:43:38 +08:00
|
|
|
}
|
|
|
|
|
2008-02-20 12:02:35 +08:00
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
|
2008-10-16 14:09:51 +08:00
|
|
|
NodeSet& Dst, bool asLValue) {
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2008-02-20 12:02:35 +08:00
|
|
|
switch (U->getOpcode()) {
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2008-06-20 01:55:38 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case UnaryOperator::Deref: {
|
|
|
|
|
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(Ex, Pred, Tmp);
|
|
|
|
|
|
|
|
for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal location = GetSVal(St, Ex);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
if (asLValue)
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, U, *I, BindExpr(St, U, location));
|
2008-04-30 05:04:26 +08:00
|
|
|
else
|
2008-05-21 23:48:33 +08:00
|
|
|
EvalLoad(Dst, U, *I, St, location);
|
2008-04-30 05:04:26 +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
|
|
|
}
|
2008-05-01 05:45:55 +08:00
|
|
|
|
2008-06-20 01:55:38 +08:00
|
|
|
case UnaryOperator::Real: {
|
|
|
|
|
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(Ex, Pred, Tmp);
|
|
|
|
|
|
|
|
for (NodeSet::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For all other types, UnaryOperator::Real is an identity operation.
|
|
|
|
assert (U->getType() == Ex->getType());
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I);
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
|
2008-06-20 01:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnaryOperator::Imag: {
|
|
|
|
|
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(Ex, Pred, Tmp);
|
|
|
|
|
|
|
|
for (NodeSet::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For all other types, UnaryOperator::Float returns 0.
|
|
|
|
assert (Ex->getType()->isIntegerType());
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = NonLoc::MakeVal(getBasicVals(), 0, Ex->getType());
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, U, *I, BindExpr(St, U, X));
|
2008-06-20 01:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Just report "Unknown" for OffsetOf.
|
2008-05-01 05:45:55 +08:00
|
|
|
case UnaryOperator::OffsetOf:
|
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
|
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: {
|
2008-02-20 12:02:35 +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();
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(Ex, Pred, Tmp);
|
2008-02-20 12:02:35 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I);
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, U, *I, BindExpr(St, U, GetSVal(St, Ex)));
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UnaryOperator::AddrOf: {
|
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
assert(!asLValue);
|
2008-04-30 05:04:26 +08:00
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
|
|
|
NodeSet Tmp;
|
2008-10-16 14:09:51 +08:00
|
|
|
VisitLValue(Ex, Pred, Tmp);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = GetSVal(St, Ex);
|
2008-10-30 13:33:54 +08:00
|
|
|
St = BindExpr(St, U, V);
|
2008-04-30 05:04:26 +08:00
|
|
|
MakeNode(Dst, U, *I, St);
|
2008-02-22 03:15:37 +08:00
|
|
|
}
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-02-16 06:09:30 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case UnaryOperator::LNot:
|
|
|
|
case UnaryOperator::Minus:
|
|
|
|
case UnaryOperator::Not: {
|
2008-02-16 06:09:30 +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();
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(Ex, Pred, Tmp);
|
2008-02-16 06:09:30 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
for (NodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) {
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I);
|
2008-09-30 13:32:44 +08:00
|
|
|
|
|
|
|
// Get the value of the subexpression.
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = GetSVal(St, Ex);
|
2008-09-30 13:32:44 +08:00
|
|
|
|
2008-11-15 08:20:05 +08:00
|
|
|
if (V.isUnknownOrUndef()) {
|
|
|
|
MakeNode(Dst, U, *I, BindExpr(St, U, V));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-15 12:01:56 +08:00
|
|
|
// QualType DstT = getContext().getCanonicalType(U->getType());
|
|
|
|
// QualType SrcT = getContext().getCanonicalType(Ex->getType());
|
|
|
|
//
|
|
|
|
// if (DstT != SrcT) // Perform promotions.
|
|
|
|
// V = EvalCast(V, DstT);
|
|
|
|
//
|
|
|
|
// if (V.isUnknownOrUndef()) {
|
|
|
|
// MakeNode(Dst, U, *I, BindExpr(St, U, V));
|
|
|
|
// continue;
|
|
|
|
// }
|
2008-01-24 16:20:02 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
switch (U->getOpcode()) {
|
|
|
|
default:
|
|
|
|
assert(false && "Invalid Opcode.");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UnaryOperator::Not:
|
2008-10-01 08:21:14 +08:00
|
|
|
// FIXME: Do we need to handle promotions?
|
2008-10-30 13:33:54 +08:00
|
|
|
St = BindExpr(St, U, EvalComplement(cast<NonLoc>(V)));
|
2008-04-30 05:04:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UnaryOperator::Minus:
|
2008-10-01 08:21:14 +08:00
|
|
|
// FIXME: Do we need to handle promotions?
|
2008-10-30 13:33:54 +08:00
|
|
|
St = BindExpr(St, U, EvalMinus(U, cast<NonLoc>(V)));
|
2008-04-30 05:04:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UnaryOperator::LNot:
|
|
|
|
|
|
|
|
// 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".
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<Loc>(V)) {
|
|
|
|
loc::ConcreteInt X(getBasicVals().getZeroWithPtrWidth());
|
|
|
|
SVal Result = EvalBinOp(BinaryOperator::EQ, cast<Loc>(V), X);
|
2008-10-30 13:33:54 +08:00
|
|
|
St = BindExpr(St, U, Result);
|
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()));
|
2008-07-18 05:27:31 +08:00
|
|
|
#if 0
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X);
|
|
|
|
St = SetSVal(St, U, Result);
|
2008-07-18 05:27:31 +08:00
|
|
|
#else
|
2008-10-17 13:57:07 +08:00
|
|
|
EvalBinOp(Dst, U, BinaryOperator::EQ, cast<NonLoc>(V), X, *I);
|
2008-07-18 05:27:31 +08:00
|
|
|
continue;
|
|
|
|
#endif
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2008-02-20 12:12:31 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
MakeNode(Dst, U, *I, St);
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
NodeSet Tmp;
|
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
2008-10-16 14:09:51 +08:00
|
|
|
VisitLValue(Ex, Pred, Tmp);
|
2008-02-07 09:08:27 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) {
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V1 = GetSVal(St, Ex);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// Perform a load.
|
|
|
|
NodeSet Tmp2;
|
|
|
|
EvalLoad(Tmp2, Ex, *I, St, V1);
|
2008-04-22 12:56:29 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
for (NodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) {
|
|
|
|
|
|
|
|
St = GetState(*I2);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V2 = GetSVal(St, Ex);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// Propagate unknown and undefined values.
|
|
|
|
if (V2.isUnknownOrUndef()) {
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, U, *I2, BindExpr(St, U, V2));
|
2008-04-30 05:04:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
2008-04-22 07:43:38 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Handle all other values.
|
2008-04-22 07:43:38 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
|
|
|
|
: BinaryOperator::Sub;
|
2008-04-22 07:43:38 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal Result = EvalBinOp(Op, V2, MakeConstantVal(1U, U));
|
2008-10-30 13:33:54 +08:00
|
|
|
St = BindExpr(St, U, U->isPostfix() ? V2 : Result);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// Perform the store.
|
2008-04-30 12:23:07 +08:00
|
|
|
EvalStore(Dst, U, *I2, St, 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
|
|
|
}
|
|
|
|
|
2008-03-18 05:11:24 +08:00
|
|
|
void GRExprEngine::VisitAsmStmt(AsmStmt* A, NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A,
|
|
|
|
AsmStmt::outputs_iterator I,
|
|
|
|
AsmStmt::outputs_iterator E,
|
|
|
|
NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
if (I == E) {
|
|
|
|
VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeSet Tmp;
|
2008-10-16 14:09:51 +08:00
|
|
|
VisitLValue(*I, Pred, Tmp);
|
2008-03-18 05:11:24 +08:00
|
|
|
|
|
|
|
++I;
|
|
|
|
|
|
|
|
for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
|
|
|
|
VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A,
|
|
|
|
AsmStmt::inputs_iterator I,
|
|
|
|
AsmStmt::inputs_iterator E,
|
|
|
|
NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
if (I == E) {
|
|
|
|
|
|
|
|
// 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.
|
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.
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(Pred);
|
2008-03-18 05:11:24 +08:00
|
|
|
|
|
|
|
for (AsmStmt::outputs_iterator OI = A->begin_outputs(),
|
|
|
|
OE = A->end_outputs(); OI != OE; ++OI) {
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = GetSVal(St, *OI);
|
|
|
|
assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
|
2008-03-18 05:11:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<Loc>(X))
|
2008-10-30 13:33:54 +08:00
|
|
|
St = BindLoc(St, cast<Loc>(X), UnknownVal());
|
2008-03-18 05:11:24 +08:00
|
|
|
}
|
|
|
|
|
2008-03-22 05:30:14 +08:00
|
|
|
MakeNode(Dst, A, Pred, St);
|
2008-03-18 05:11:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(*I, Pred, Tmp);
|
|
|
|
|
|
|
|
++I;
|
|
|
|
|
|
|
|
for (NodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI)
|
|
|
|
VisitAsmStmtHelperInputs(A, I, E, *NI, Dst);
|
|
|
|
}
|
|
|
|
|
2008-04-17 07:05:51 +08:00
|
|
|
void GRExprEngine::EvalReturn(NodeSet& Dst, ReturnStmt* S, NodeTy* Pred) {
|
|
|
|
assert (Builder && "GRStmtNodeBuilder must be defined.");
|
|
|
|
|
|
|
|
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);
|
2008-04-17 07:05:51 +08:00
|
|
|
|
2008-04-19 04:35:30 +08:00
|
|
|
// Handle the case where no nodes where generated.
|
2008-04-17 07:05:51 +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));
|
|
|
|
}
|
|
|
|
|
2008-03-31 23:02:58 +08:00
|
|
|
void GRExprEngine::VisitReturnStmt(ReturnStmt* S, NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
|
|
|
|
Expr* R = S->getRetValue();
|
|
|
|
|
|
|
|
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
|
|
|
|
2008-11-21 08:27:44 +08:00
|
|
|
NodeSet Tmp;
|
|
|
|
Visit(R, Pred, Tmp);
|
2008-10-04 13:50:14 +08:00
|
|
|
|
2008-11-21 08:27:44 +08:00
|
|
|
for (NodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
|
|
|
|
SVal X = GetSVal((*I)->getState(), R);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
if (R && getStateManager().hasStackStorage(R)) {
|
|
|
|
// Create a special node representing the error.
|
|
|
|
if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
|
|
|
|
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()) {
|
|
|
|
if (NodeTy* N = Builder->generateNode(S, GetState(*I), *I)) {
|
|
|
|
N->markAsSink();
|
|
|
|
RetsUndef.insert(N);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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-10-21 07:40:25 +08:00
|
|
|
const GRState* GRExprEngine::CheckDivideZero(Expr* Ex, const GRState* St,
|
|
|
|
NodeTy* Pred, SVal Denom) {
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// Divide by undefined? (potentially zero)
|
|
|
|
|
|
|
|
if (Denom.isUndef()) {
|
|
|
|
NodeTy* DivUndef = Builder->generateNode(Ex, St, Pred);
|
|
|
|
|
|
|
|
if (DivUndef) {
|
|
|
|
DivUndef->markAsSink();
|
|
|
|
ExplicitBadDivides.insert(DivUndef);
|
|
|
|
}
|
|
|
|
|
2008-10-21 07:40:25 +08:00
|
|
|
return 0;
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for divide/remainder-by-zero.
|
|
|
|
// First, "assume" that the denominator is 0 or undefined.
|
|
|
|
|
|
|
|
bool isFeasibleZero = false;
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* ZeroSt = Assume(St, Denom, false, isFeasibleZero);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
// Second, "assume" that the denominator cannot be 0.
|
|
|
|
|
|
|
|
bool isFeasibleNotZero = false;
|
|
|
|
St = Assume(St, Denom, true, isFeasibleNotZero);
|
|
|
|
|
|
|
|
// Create the node for the divide-by-zero (if it occurred).
|
|
|
|
|
|
|
|
if (isFeasibleZero)
|
|
|
|
if (NodeTy* DivZeroNode = Builder->generateNode(Ex, ZeroSt, Pred)) {
|
|
|
|
DivZeroNode->markAsSink();
|
|
|
|
|
|
|
|
if (isFeasibleNotZero)
|
|
|
|
ImplicitBadDivides.insert(DivZeroNode);
|
|
|
|
else
|
|
|
|
ExplicitBadDivides.insert(DivZeroNode);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-10-21 07:40:25 +08:00
|
|
|
return isFeasibleNotZero ? St : 0;
|
2008-04-30 05:04:26 +08:00
|
|
|
}
|
|
|
|
|
2008-02-14 01:41:41 +08:00
|
|
|
void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
|
2008-02-14 07:08:21 +08:00
|
|
|
GRExprEngine::NodeTy* Pred,
|
|
|
|
GRExprEngine::NodeSet& Dst) {
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
NodeSet Tmp1;
|
|
|
|
Expr* LHS = B->getLHS()->IgnoreParens();
|
|
|
|
Expr* RHS = B->getRHS()->IgnoreParens();
|
2008-02-07 09:08:27 +08:00
|
|
|
|
2008-12-06 10:39:30 +08:00
|
|
|
// FIXME: Add proper support for ObjCKVCRefExpr.
|
|
|
|
if (isa<ObjCKVCRefExpr>(LHS)) {
|
|
|
|
Visit(RHS, Pred, Dst);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
for (NodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal LeftV = GetSVal((*I1)->getState(), LHS);
|
2008-01-16 08:53:15 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Process the RHS.
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
NodeSet Tmp2;
|
|
|
|
Visit(RHS, *I1, Tmp2);
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// With both the LHS and RHS evaluated, process the operation itself.
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
for (NodeSet::iterator I2=Tmp2.begin(), E2=Tmp2.end(); I2 != E2; ++I2) {
|
2008-02-07 06:50:25 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = GetState(*I2);
|
2008-10-21 07:40:25 +08:00
|
|
|
const GRState* OldSt = St;
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal RightV = GetSVal(St, RHS);
|
2008-02-07 06:50:25 +08:00
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
switch (Op) {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-01-24 03:59:44 +08:00
|
|
|
case BinaryOperator::Assign: {
|
2008-02-22 02:02:17 +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();
|
2008-03-13 05:45:47 +08:00
|
|
|
|
2008-11-13 14:10:40 +08:00
|
|
|
if (RightV.isUnknown() && (Loc::IsLocType(T) ||
|
|
|
|
(T->isScalarType() && T->isIntegerType()))) {
|
2008-03-13 05:45:47 +08:00
|
|
|
unsigned Count = Builder->getCurrentBlockCount();
|
2008-12-05 10:27:51 +08:00
|
|
|
SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), Count);
|
2008-03-13 05:45:47 +08:00
|
|
|
|
2008-11-13 14:10:40 +08:00
|
|
|
RightV = Loc::IsLocType(T)
|
2008-10-17 13:57:07 +08:00
|
|
|
? cast<SVal>(loc::SymbolVal(Sym))
|
|
|
|
: cast<SVal>(nonloc::SymbolVal(Sym));
|
2008-03-13 05:45:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate the effects of a "store": bind the value of the RHS
|
|
|
|
// to the L-Value represented by the LHS.
|
2008-04-17 02:21:25 +08:00
|
|
|
|
2008-10-30 13:33:54 +08:00
|
|
|
EvalStore(Dst, B, LHS, *I2, BindExpr(St, B, RightV), LeftV, RightV);
|
2008-04-17 02:21:25 +08:00
|
|
|
continue;
|
2008-01-24 03:59:44 +08:00
|
|
|
}
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case BinaryOperator::Div:
|
|
|
|
case BinaryOperator::Rem:
|
2008-01-24 07:38:00 +08:00
|
|
|
|
2008-10-21 07:40:25 +08:00
|
|
|
// Special checking for integer denominators.
|
2008-11-13 14:10:40 +08:00
|
|
|
if (RHS->getType()->isIntegerType() &&
|
|
|
|
RHS->getType()->isScalarType()) {
|
|
|
|
|
2008-10-21 07:40:25 +08:00
|
|
|
St = CheckDivideZero(B, St, *I2, RightV);
|
|
|
|
if (!St) continue;
|
|
|
|
}
|
2008-02-27 06:27:51 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// FALL-THROUGH.
|
|
|
|
|
|
|
|
default: {
|
|
|
|
|
|
|
|
if (B->isAssignmentOp())
|
|
|
|
break;
|
2008-02-19 08:22:37 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Process non-assignements except commas or short-circuited
|
|
|
|
// logical expressions (LAnd and LOr).
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal Result = EvalBinOp(Op, LeftV, RightV);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
|
|
|
if (Result.isUnknown()) {
|
2008-10-21 07:40:25 +08:00
|
|
|
if (OldSt != St) {
|
|
|
|
// Generate a new node if we have already created a new state.
|
|
|
|
MakeNode(Dst, B, *I2, St);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Dst.Add(*I2);
|
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) {
|
|
|
|
|
|
|
|
// The operands were *not* undefined, but the result is undefined.
|
|
|
|
// This is a special node that should be flagged as an error.
|
|
|
|
|
|
|
|
if (NodeTy* UndefNode = Builder->generateNode(B, St, *I2)) {
|
|
|
|
UndefNode->markAsSink();
|
|
|
|
UndefResults.insert(UndefNode);
|
|
|
|
}
|
|
|
|
|
2008-03-09 16:12:37 +08:00
|
|
|
continue;
|
2008-02-19 08:22:37 +08:00
|
|
|
}
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Otherwise, create a new node.
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-10-30 13:33:54 +08:00
|
|
|
MakeNode(Dst, B, *I2, BindExpr(St, B, Result));
|
2008-04-30 05:04:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (B->isCompoundAssignmentOp());
|
|
|
|
|
2008-10-28 07:02:39 +08:00
|
|
|
if (Op >= BinaryOperator::AndAssign) {
|
|
|
|
Op = (BinaryOperator::Opcode) (Op - (BinaryOperator::AndAssign -
|
|
|
|
BinaryOperator::And));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Op = (BinaryOperator::Opcode) (Op - BinaryOperator::MulAssign);
|
|
|
|
}
|
2008-02-22 02:02:17 +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.
|
|
|
|
NodeSet Tmp3;
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal location = GetSVal(St, LHS);
|
2008-04-30 05:04:26 +08:00
|
|
|
EvalLoad(Tmp3, LHS, *I2, St, location);
|
|
|
|
|
|
|
|
for (NodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; ++I3) {
|
|
|
|
|
|
|
|
St = GetState(*I3);
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = GetSVal(St, LHS);
|
2008-04-30 05:04:26 +08:00
|
|
|
|
2008-10-21 07:40:25 +08:00
|
|
|
// Check for divide-by-zero.
|
|
|
|
if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
|
2008-11-13 14:10:40 +08:00
|
|
|
&& RHS->getType()->isIntegerType()
|
|
|
|
&& RHS->getType()->isScalarType()) {
|
2008-10-21 07:40:25 +08:00
|
|
|
|
|
|
|
// CheckDivideZero returns a new state where the denominator
|
|
|
|
// is assumed to be non-zero.
|
|
|
|
St = CheckDivideZero(B, St, *I3, RightV);
|
|
|
|
|
|
|
|
if (!St)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Propagate undefined values (left-side).
|
|
|
|
if (V.isUndef()) {
|
2008-10-30 13:33:54 +08:00
|
|
|
EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, V), location, V);
|
2008-04-30 05:04:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Propagate unknown values (left and right-side).
|
|
|
|
if (RightV.isUnknown() || V.isUnknown()) {
|
2008-10-30 13:33:54 +08:00
|
|
|
EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, UnknownVal()), location,
|
2008-04-30 05:04:26 +08:00
|
|
|
UnknownVal());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point:
|
|
|
|
//
|
|
|
|
// The LHS is not Undef/Unknown.
|
|
|
|
// The RHS is not Unknown.
|
|
|
|
|
|
|
|
// Get the computation type.
|
|
|
|
QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
|
2008-11-15 12:01:56 +08:00
|
|
|
CTy = getContext().getCanonicalType(CTy);
|
|
|
|
|
|
|
|
QualType LTy = getContext().getCanonicalType(LHS->getType());
|
|
|
|
QualType RTy = getContext().getCanonicalType(RHS->getType());
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Perform promotions.
|
2008-11-15 12:01:56 +08:00
|
|
|
if (LTy != CTy) V = EvalCast(V, CTy);
|
|
|
|
if (RTy != CTy) RightV = EvalCast(RightV, CTy);
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
// Evaluate operands and promote to result type.
|
2008-10-21 07:40:25 +08:00
|
|
|
if (RightV.isUndef()) {
|
2008-09-20 09:50:34 +08:00
|
|
|
// Propagate undefined values (right-side).
|
2008-10-30 13:33:54 +08:00
|
|
|
EvalStore(Dst,B, LHS, *I3, BindExpr(St, B, RightV), location, RightV);
|
2008-04-30 05:04:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-15 12:01:56 +08:00
|
|
|
// Compute the result of the operation.
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
|
2008-02-22 02:43:30 +08:00
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
if (Result.isUndef()) {
|
|
|
|
// The operands were not undefined, but the result is undefined.
|
|
|
|
if (NodeTy* UndefNode = Builder->generateNode(B, St, *I3)) {
|
|
|
|
UndefNode->markAsSink();
|
|
|
|
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.
|
2008-11-15 12:01:56 +08:00
|
|
|
|
|
|
|
SVal LHSVal;
|
|
|
|
|
2008-11-23 13:52:28 +08:00
|
|
|
if (Result.isUnknown() && (Loc::IsLocType(CTy)
|
|
|
|
|| (CTy->isScalarType() && CTy->isIntegerType()))) {
|
2008-10-22 03:49:01 +08:00
|
|
|
|
2008-10-21 07:13:25 +08:00
|
|
|
unsigned Count = Builder->getCurrentBlockCount();
|
|
|
|
|
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.
|
2008-12-05 10:27:51 +08:00
|
|
|
SymbolRef Sym = SymMgr.getConjuredSymbol(B->getRHS(), LTy, Count);
|
2008-11-15 12:01:56 +08:00
|
|
|
LHSVal = Loc::IsLocType(LTy)
|
2008-10-21 07:13:25 +08:00
|
|
|
? cast<SVal>(loc::SymbolVal(Sym))
|
2008-11-15 12:01:56 +08:00
|
|
|
: cast<SVal>(nonloc::SymbolVal(Sym));
|
|
|
|
|
2008-11-23 13:52:28 +08:00
|
|
|
// However, we need to convert the symbol to the computation type.
|
2008-11-15 12:01:56 +08:00
|
|
|
Result = (LTy == CTy) ? LHSVal : EvalCast(LHSVal,CTy);
|
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.
|
|
|
|
LHSVal = (LTy == CTy) ? Result : EvalCast(Result,LTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
EvalStore(Dst, B, LHS, *I3, BindExpr(St, B, Result), 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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer-function Helpers.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
void GRExprEngine::EvalBinOp(ExplodedNodeSet<GRState>& Dst, Expr* Ex,
|
2008-07-18 05:27:31 +08:00
|
|
|
BinaryOperator::Opcode Op,
|
2008-10-17 13:57:07 +08:00
|
|
|
NonLoc L, NonLoc R,
|
2008-08-13 12:27:00 +08:00
|
|
|
ExplodedNode<GRState>* Pred) {
|
2008-07-18 13:53:58 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
GRStateSet OStates;
|
2008-07-18 13:53:58 +08:00
|
|
|
EvalBinOp(OStates, GetState(Pred), Ex, Op, L, R);
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
for (GRStateSet::iterator I=OStates.begin(), E=OStates.end(); I!=E; ++I)
|
2008-07-18 13:53:58 +08:00
|
|
|
MakeNode(Dst, Ex, Pred, *I);
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
void GRExprEngine::EvalBinOp(GRStateSet& OStates, const GRState* St,
|
2008-07-18 13:53:58 +08:00
|
|
|
Expr* Ex, BinaryOperator::Opcode Op,
|
2008-10-17 13:57:07 +08:00
|
|
|
NonLoc L, NonLoc R) {
|
2008-07-18 05:27:31 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
GRStateSet::AutoPopulate AP(OStates, St);
|
2008-11-15 08:20:05 +08:00
|
|
|
if (R.isValid()) getTF().EvalBinOpNN(OStates, *this, St, Ex, Op, L, R);
|
2008-07-18 05:27:31 +08:00
|
|
|
}
|
|
|
|
|
2008-01-17 02:18:48 +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<>
|
2008-02-14 01:41:41 +08:00
|
|
|
struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
|
2008-01-17 05:46:15 +08:00
|
|
|
public DefaultDOTGraphTraits {
|
2008-02-09 05:10:02 +08:00
|
|
|
|
2008-02-15 06:54:53 +08:00
|
|
|
static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
|
|
|
|
|
|
|
|
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\"";
|
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
if (GraphPrintCheckerState->isNoReturnCall(N))
|
|
|
|
return "color=\"blue\",style=\"filled\"";
|
|
|
|
|
2008-02-15 06:54:53 +08:00
|
|
|
return "";
|
|
|
|
}
|
2008-02-06 11:56:15 +08:00
|
|
|
|
2008-02-14 01:41:41 +08:00
|
|
|
static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
|
2008-01-17 05:46:15 +08:00
|
|
|
std::ostringstream Out;
|
2008-01-24 06:30:44 +08:00
|
|
|
|
|
|
|
// Program Location.
|
2008-01-17 05:46:15 +08:00
|
|
|
ProgramPoint Loc = N->getLocation();
|
|
|
|
|
|
|
|
switch (Loc.getKind()) {
|
|
|
|
case ProgramPoint::BlockEntranceKind:
|
|
|
|
Out << "Block Entrance: B"
|
|
|
|
<< cast<BlockEntrance>(Loc).getBlock()->getBlockID();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ProgramPoint::BlockExitKind:
|
|
|
|
assert (false);
|
|
|
|
break;
|
|
|
|
|
2008-04-30 05:04:26 +08:00
|
|
|
case ProgramPoint::PostLoadKind:
|
2008-06-18 13:34:07 +08:00
|
|
|
case ProgramPoint::PostPurgeDeadSymbolsKind:
|
2008-01-17 05:46:15 +08:00
|
|
|
case ProgramPoint::PostStmtKind: {
|
2008-03-08 04:57:30 +08:00
|
|
|
const PostStmt& L = cast<PostStmt>(Loc);
|
|
|
|
Stmt* S = L.getStmt();
|
|
|
|
SourceLocation SLoc = S->getLocStart();
|
|
|
|
|
|
|
|
Out << S->getStmtClassName() << ' ' << (void*) S << ' ';
|
2008-09-13 13:16:45 +08:00
|
|
|
llvm::raw_os_ostream OutS(Out);
|
|
|
|
S->printPretty(OutS);
|
|
|
|
OutS.flush();
|
2008-01-25 06:27:20 +08:00
|
|
|
|
2008-03-09 11:30:59 +08:00
|
|
|
if (SLoc.isFileID()) {
|
|
|
|
Out << "\\lline="
|
|
|
|
<< GraphPrintSourceManager->getLineNumber(SLoc) << " col="
|
2008-10-24 12:33:15 +08:00
|
|
|
<< GraphPrintSourceManager->getColumnNumber(SLoc) << "\\l";
|
2008-03-09 11:30:59 +08:00
|
|
|
}
|
2008-02-07 13:48:01 +08:00
|
|
|
|
2008-03-01 07:14:48 +08:00
|
|
|
if (GraphPrintCheckerState->isImplicitNullDeref(N))
|
2008-02-07 13:48:01 +08:00
|
|
|
Out << "\\|Implicit-Null Dereference.\\l";
|
2008-03-01 07:14:48 +08:00
|
|
|
else if (GraphPrintCheckerState->isExplicitNullDeref(N))
|
2008-02-07 14:04:18 +08:00
|
|
|
Out << "\\|Explicit-Null Dereference.\\l";
|
2008-03-01 07:14:48 +08:00
|
|
|
else if (GraphPrintCheckerState->isUndefDeref(N))
|
2008-02-28 17:25:22 +08:00
|
|
|
Out << "\\|Dereference of undefialied value.\\l";
|
2008-03-01 07:14:48 +08:00
|
|
|
else if (GraphPrintCheckerState->isUndefStore(N))
|
2008-10-17 13:57:07 +08:00
|
|
|
Out << "\\|Store to Undefined Loc.";
|
2008-03-08 03:04:53 +08:00
|
|
|
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.";
|
2008-03-01 07:14:48 +08:00
|
|
|
else if (GraphPrintCheckerState->isUndefResult(N))
|
2008-02-29 04:32:03 +08:00
|
|
|
Out << "\\|Result of operation is undefined.";
|
|
|
|
else if (GraphPrintCheckerState->isNoReturnCall(N))
|
|
|
|
Out << "\\|Call to function marked \"noreturn\".";
|
2008-03-01 07:14:48 +08:00
|
|
|
else if (GraphPrintCheckerState->isBadCall(N))
|
|
|
|
Out << "\\|Call to NULL/Undefined.";
|
2008-03-01 07:53:11 +08:00
|
|
|
else if (GraphPrintCheckerState->isUndefArg(N))
|
|
|
|
Out << "\\|Argument in call is undefined";
|
2008-02-07 13:48:01 +08:00
|
|
|
|
2008-01-17 05:46:15 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
const BlockEdge& E = cast<BlockEdge>(Loc);
|
|
|
|
Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
|
|
|
|
<< E.getDst()->getBlockID() << ')';
|
2008-01-31 07:03:39 +08:00
|
|
|
|
|
|
|
if (Stmt* T = E.getSrc()->getTerminator()) {
|
2008-03-08 04:57:30 +08:00
|
|
|
|
|
|
|
SourceLocation SLoc = T->getLocStart();
|
|
|
|
|
2008-01-31 07:03:39 +08:00
|
|
|
Out << "\\|Terminator: ";
|
2008-03-08 04:57:30 +08:00
|
|
|
|
2008-09-13 13:16:45 +08:00
|
|
|
llvm::raw_os_ostream OutS(Out);
|
|
|
|
E.getSrc()->printTerminator(OutS);
|
|
|
|
OutS.flush();
|
2008-01-31 07:03:39 +08:00
|
|
|
|
2008-03-09 11:30:59 +08:00
|
|
|
if (SLoc.isFileID()) {
|
|
|
|
Out << "\\lline="
|
|
|
|
<< GraphPrintSourceManager->getLineNumber(SLoc) << " col="
|
|
|
|
<< GraphPrintSourceManager->getColumnNumber(SLoc);
|
|
|
|
}
|
2008-03-08 04:57:30 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
if (isa<SwitchStmt>(T)) {
|
|
|
|
Stmt* Label = E.getDst()->getLabel();
|
|
|
|
|
|
|
|
if (Label) {
|
|
|
|
if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
|
|
|
|
Out << "\\lcase ";
|
2008-09-13 13:16:45 +08:00
|
|
|
llvm::raw_os_ostream OutS(Out);
|
|
|
|
C->getLHS()->printPretty(OutS);
|
|
|
|
OutS.flush();
|
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
if (Stmt* RHS = C->getRHS()) {
|
|
|
|
Out << " .. ";
|
2008-09-13 13:16:45 +08:00
|
|
|
RHS->printPretty(OutS);
|
|
|
|
OutS.flush();
|
2008-02-14 07:08:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Out << ":";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert (isa<DefaultStmt>(Label));
|
|
|
|
Out << "\\ldefault:";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
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
|
|
|
|
Out << "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << "\\l";
|
|
|
|
}
|
2008-01-31 07:24:39 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-28 18:21:43 +08:00
|
|
|
Out << "\\|StateID: " << (void*) N->getState() << "\\|";
|
2008-02-09 05:10:02 +08:00
|
|
|
|
2008-08-16 08:49:49 +08:00
|
|
|
GRStateRef state(N->getState(), GraphPrintCheckerState->getStateManager());
|
|
|
|
state.printDOT(Out);
|
2008-01-17 05:46:15 +08:00
|
|
|
|
2008-01-24 06:30:44 +08:00
|
|
|
Out << "\\l";
|
2008-01-17 05:46:15 +08:00
|
|
|
return Out.str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end llvm namespace
|
|
|
|
#endif
|
|
|
|
|
2008-03-08 06:58:01 +08:00
|
|
|
#ifndef NDEBUG
|
2008-03-13 01:18:20 +08:00
|
|
|
|
|
|
|
template <typename ITERATOR>
|
|
|
|
GRExprEngine::NodeTy* GetGraphNode(ITERATOR I) { return *I; }
|
|
|
|
|
|
|
|
template <>
|
|
|
|
GRExprEngine::NodeTy*
|
|
|
|
GetGraphNode<llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator>
|
|
|
|
(llvm::DenseMap<GRExprEngine::NodeTy*, Expr*>::iterator I) {
|
|
|
|
return I->first;
|
|
|
|
}
|
|
|
|
|
2008-03-08 06:58:01 +08:00
|
|
|
template <typename ITERATOR>
|
2008-04-19 03:23:43 +08:00
|
|
|
static void AddSources(std::vector<GRExprEngine::NodeTy*>& Sources,
|
2008-03-13 01:18:20 +08:00
|
|
|
ITERATOR I, ITERATOR E) {
|
|
|
|
|
2008-09-17 02:44:52 +08:00
|
|
|
llvm::SmallSet<ProgramPoint,10> CachedSources;
|
2008-03-08 06:58:01 +08:00
|
|
|
|
2008-03-13 01:18:20 +08:00
|
|
|
for ( ; I != E; ++I ) {
|
|
|
|
GRExprEngine::NodeTy* N = GetGraphNode(I);
|
2008-09-17 02:44:52 +08:00
|
|
|
ProgramPoint P = N->getLocation();
|
2008-03-13 01:18:20 +08:00
|
|
|
|
2008-09-17 02:44:52 +08:00
|
|
|
if (CachedSources.count(P))
|
2008-03-13 01:18:20 +08:00
|
|
|
continue;
|
|
|
|
|
2008-09-17 02:44:52 +08:00
|
|
|
CachedSources.insert(P);
|
2008-03-13 01:18:20 +08:00
|
|
|
Sources.push_back(N);
|
|
|
|
}
|
2008-03-08 06:58:01 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void GRExprEngine::ViewGraph(bool trim) {
|
2008-03-12 02:25:33 +08:00
|
|
|
#ifndef NDEBUG
|
2008-03-08 06:58:01 +08:00
|
|
|
if (trim) {
|
2008-04-19 03:23:43 +08:00
|
|
|
std::vector<NodeTy*> Src;
|
|
|
|
|
|
|
|
// Fixme: Migrate over to the new way of adding nodes.
|
2008-03-13 01:18:20 +08:00
|
|
|
AddSources(Src, null_derefs_begin(), null_derefs_end());
|
|
|
|
AddSources(Src, undef_derefs_begin(), undef_derefs_end());
|
|
|
|
AddSources(Src, explicit_bad_divides_begin(), explicit_bad_divides_end());
|
|
|
|
AddSources(Src, undef_results_begin(), undef_results_end());
|
|
|
|
AddSources(Src, bad_calls_begin(), bad_calls_end());
|
|
|
|
AddSources(Src, undef_arg_begin(), undef_arg_end());
|
2008-03-15 02:14:50 +08:00
|
|
|
AddSources(Src, undef_branches_begin(), undef_branches_end());
|
2008-03-13 01:18:20 +08:00
|
|
|
|
2008-04-19 03:23:43 +08:00
|
|
|
// The new way.
|
|
|
|
for (BugTypeSet::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I)
|
|
|
|
(*I)->GetErrorNodes(Src);
|
|
|
|
|
|
|
|
|
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");
|
2008-03-12 02:25:33 +08:00
|
|
|
|
|
|
|
GraphPrintCheckerState = NULL;
|
|
|
|
GraphPrintSourceManager = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::ViewGraph(NodeTy** Beg, NodeTy** End) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
GraphPrintCheckerState = this;
|
|
|
|
GraphPrintSourceManager = &getContext().getSourceManager();
|
2008-08-16 08:49:49 +08:00
|
|
|
|
2008-03-12 02:25:33 +08:00
|
|
|
GRExprEngine::GraphTy* TrimmedG = G.Trim(Beg, End);
|
|
|
|
|
|
|
|
if (!TrimmedG)
|
|
|
|
llvm::cerr << "warning: Trimmed ExplodedGraph is empty.\n";
|
|
|
|
else {
|
|
|
|
llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine");
|
|
|
|
delete TrimmedG;
|
|
|
|
}
|
2008-03-08 06:58:01 +08:00
|
|
|
|
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
|
|
|
}
|