2008-01-13 12:56:13 +08:00
|
|
|
//=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- C++ -*------=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the template classes ExplodedNode and ExplodedGraph,
|
|
|
|
// which represent a path-sensitive, intra-procedural "exploded graph."
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/PathSensitive/ExplodedGraph.h"
|
2008-04-16 23:51:26 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
2008-03-08 06:58:01 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2008-01-13 12:56:13 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-08-27 09:56:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Node auditing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// An out of line virtual method to provide a home for the class vtable.
|
|
|
|
ExplodedNodeImpl::Auditor::~Auditor() {}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
static ExplodedNodeImpl::Auditor* NodeAuditor = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void ExplodedNodeImpl::SetAuditor(ExplodedNodeImpl::Auditor* A) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
NodeAuditor = A;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExplodedNodeImpl.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-13 12:56:13 +08:00
|
|
|
|
|
|
|
static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) {
|
|
|
|
return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P);
|
|
|
|
}
|
|
|
|
|
2008-08-27 09:27:52 +08:00
|
|
|
void ExplodedNodeImpl::addPredecessor(ExplodedNodeImpl* V) {
|
|
|
|
assert (!V->isSink());
|
|
|
|
Preds.addNode(V);
|
|
|
|
V->Succs.addNode(this);
|
2008-08-27 09:56:11 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (NodeAuditor) NodeAuditor->AddEdge(V, this);
|
|
|
|
#endif
|
2008-08-27 09:27:52 +08:00
|
|
|
}
|
|
|
|
|
2008-01-13 12:56:13 +08:00
|
|
|
void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) {
|
2008-03-06 03:08:55 +08:00
|
|
|
|
|
|
|
assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
|
2008-03-08 06:58:01 +08:00
|
|
|
assert (!getFlag());
|
2008-03-06 03:08:55 +08:00
|
|
|
|
2008-01-13 12:56:13 +08:00
|
|
|
if (getKind() == Size1) {
|
|
|
|
if (ExplodedNodeImpl* NOld = getNode()) {
|
|
|
|
std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>();
|
2008-03-06 03:08:55 +08:00
|
|
|
assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
|
2008-01-13 12:56:13 +08:00
|
|
|
V->push_back(NOld);
|
|
|
|
V->push_back(N);
|
2008-01-30 07:31:09 +08:00
|
|
|
P = reinterpret_cast<uintptr_t>(V) | SizeOther;
|
2008-03-06 03:08:55 +08:00
|
|
|
assert (getPtr() == (void*) V);
|
|
|
|
assert (getKind() == SizeOther);
|
2008-01-13 12:56:13 +08:00
|
|
|
}
|
2008-03-06 03:08:55 +08:00
|
|
|
else {
|
2008-01-13 12:56:13 +08:00
|
|
|
P = reinterpret_cast<uintptr_t>(N);
|
2008-03-06 03:08:55 +08:00
|
|
|
assert (getKind() == Size1);
|
|
|
|
}
|
2008-01-13 12:56:13 +08:00
|
|
|
}
|
2008-03-06 03:08:55 +08:00
|
|
|
else {
|
|
|
|
assert (getKind() == SizeOther);
|
2008-01-13 12:56:13 +08:00
|
|
|
getVector(getPtr()).push_back(N);
|
2008-03-06 03:08:55 +08:00
|
|
|
}
|
2008-01-13 12:56:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned ExplodedNodeImpl::NodeGroup::size() const {
|
2008-03-08 06:58:01 +08:00
|
|
|
if (getFlag())
|
|
|
|
return 0;
|
|
|
|
|
2008-01-13 12:56:13 +08:00
|
|
|
if (getKind() == Size1)
|
|
|
|
return getNode() ? 1 : 0;
|
|
|
|
else
|
|
|
|
return getVector(getPtr()).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const {
|
2008-03-08 06:58:01 +08:00
|
|
|
if (getFlag())
|
|
|
|
return NULL;
|
|
|
|
|
2008-01-13 12:56:13 +08:00
|
|
|
if (getKind() == Size1)
|
2008-03-08 06:58:01 +08:00
|
|
|
return (ExplodedNodeImpl**) (getPtr() ? &P : NULL);
|
2008-01-13 12:56:13 +08:00
|
|
|
else
|
|
|
|
return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const {
|
2008-03-08 06:58:01 +08:00
|
|
|
if (getFlag())
|
|
|
|
return NULL;
|
|
|
|
|
2008-01-13 12:56:13 +08:00
|
|
|
if (getKind() == Size1)
|
2008-03-08 06:58:01 +08:00
|
|
|
return (ExplodedNodeImpl**) (getPtr() ? &P+1 : NULL);
|
2008-04-21 07:54:24 +08:00
|
|
|
else {
|
|
|
|
// Dereferencing end() is undefined behaviour. The vector is not empty, so
|
2008-04-22 15:37:18 +08:00
|
|
|
// we can dereference the last elem and then add 1 to the result.
|
|
|
|
return const_cast<ExplodedNodeImpl**>(&getVector(getPtr()).back()) + 1;
|
2008-04-21 07:54:24 +08:00
|
|
|
}
|
2008-01-13 12:56:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ExplodedNodeImpl::NodeGroup::~NodeGroup() {
|
|
|
|
if (getKind() == SizeOther) delete &getVector(getPtr());
|
|
|
|
}
|
2008-03-08 06:58:01 +08:00
|
|
|
|
2009-01-24 08:55:43 +08:00
|
|
|
ExplodedGraphImpl*
|
|
|
|
ExplodedGraphImpl::Trim(const ExplodedNodeImpl* const* BeginSources,
|
2009-02-05 07:49:09 +08:00
|
|
|
const ExplodedNodeImpl* const* EndSources,
|
2009-02-18 11:48:14 +08:00
|
|
|
InterExplodedGraphMapImpl* M,
|
|
|
|
llvm::DenseMap<const void*, const void*> *InverseMap)
|
|
|
|
const {
|
2008-03-08 06:58:01 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
typedef llvm::DenseSet<const ExplodedNodeImpl*> Pass1Ty;
|
2008-03-08 06:58:01 +08:00
|
|
|
Pass1Ty Pass1;
|
2009-02-05 07:49:09 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
typedef llvm::DenseMap<const ExplodedNodeImpl*, ExplodedNodeImpl*> Pass2Ty;
|
2009-02-05 07:49:09 +08:00
|
|
|
Pass2Ty& Pass2 = M->M;
|
2008-03-08 06:58:01 +08:00
|
|
|
|
2009-03-13 07:41:59 +08:00
|
|
|
llvm::SmallVector<const ExplodedNodeImpl*, 10> WL1, WL2;
|
2008-03-08 06:58:01 +08:00
|
|
|
|
2009-03-13 07:41:59 +08:00
|
|
|
// ===- Pass 1 (reverse DFS) -===
|
2009-02-21 05:10:26 +08:00
|
|
|
for (const ExplodedNodeImpl* const* I = BeginSources; I != EndSources; ++I) {
|
|
|
|
assert(*I);
|
|
|
|
WL1.push_back(*I);
|
|
|
|
}
|
2008-03-08 06:58:01 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Process the first worklist until it is empty. Because it is a std::list
|
|
|
|
// it acts like a FIFO queue.
|
|
|
|
while (!WL1.empty()) {
|
|
|
|
const ExplodedNodeImpl *N = WL1.back();
|
|
|
|
WL1.pop_back();
|
2008-03-08 06:58:01 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Have we already visited this node? If so, continue to the next one.
|
|
|
|
if (Pass1.count(N))
|
|
|
|
continue;
|
2009-01-24 08:55:43 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Otherwise, mark this node as visited.
|
|
|
|
Pass1.insert(N);
|
|
|
|
|
|
|
|
// If this is a root enqueue it to the second worklist.
|
|
|
|
if (N->Preds.empty()) {
|
|
|
|
WL2.push_back(N);
|
|
|
|
continue;
|
2008-03-08 06:58:01 +08:00
|
|
|
}
|
2009-02-21 05:10:26 +08:00
|
|
|
|
|
|
|
// Visit our predecessors and enqueue them.
|
|
|
|
for (ExplodedNodeImpl** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I)
|
2009-03-13 07:41:59 +08:00
|
|
|
WL1.push_back(*I);
|
2008-03-08 06:58:01 +08:00
|
|
|
}
|
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// We didn't hit a root? Return with a null pointer for the new graph.
|
2008-03-08 06:58:01 +08:00
|
|
|
if (WL2.empty())
|
2009-02-05 07:49:09 +08:00
|
|
|
return 0;
|
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Create an empty graph.
|
2008-03-08 06:58:01 +08:00
|
|
|
ExplodedGraphImpl* G = MakeEmptyGraph();
|
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// ===- Pass 2 (forward DFS to construct the new graph) -===
|
2008-03-08 06:58:01 +08:00
|
|
|
while (!WL2.empty()) {
|
2009-01-24 08:55:43 +08:00
|
|
|
const ExplodedNodeImpl* N = WL2.back();
|
2008-03-08 06:58:01 +08:00
|
|
|
WL2.pop_back();
|
|
|
|
|
|
|
|
// Skip this node if we have already processed it.
|
|
|
|
if (Pass2.find(N) != Pass2.end())
|
|
|
|
continue;
|
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Create the corresponding node in the new graph and record the mapping
|
|
|
|
// from the old node to the new node.
|
2008-03-08 06:58:01 +08:00
|
|
|
ExplodedNodeImpl* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL);
|
|
|
|
Pass2[N] = NewN;
|
2009-02-21 05:10:26 +08:00
|
|
|
|
|
|
|
// Also record the reverse mapping from the new node to the old node.
|
2009-02-18 11:48:14 +08:00
|
|
|
if (InverseMap) (*InverseMap)[NewN] = N;
|
2008-03-08 06:58:01 +08:00
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// If this node is a root, designate it as such in the graph.
|
2008-03-08 06:58:01 +08:00
|
|
|
if (N->Preds.empty())
|
|
|
|
G->addRoot(NewN);
|
|
|
|
|
|
|
|
// In the case that some of the intended predecessors of NewN have already
|
|
|
|
// been created, we should hook them up as predecessors.
|
2009-02-21 05:10:26 +08:00
|
|
|
|
|
|
|
// Walk through the predecessors of 'N' and hook up their corresponding
|
|
|
|
// nodes in the new graph (if any) to the freshly created node.
|
2008-03-08 06:58:01 +08:00
|
|
|
for (ExplodedNodeImpl **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
|
|
|
|
Pass2Ty::iterator PI = Pass2.find(*I);
|
|
|
|
if (PI == Pass2.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
NewN->addPredecessor(PI->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the case that some of the intended successors of NewN have already
|
|
|
|
// been created, we should hook them up as successors. Otherwise, enqueue
|
|
|
|
// the new nodes from the original graph that should have nodes created
|
|
|
|
// in the new graph.
|
|
|
|
for (ExplodedNodeImpl **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
|
2009-02-21 05:10:26 +08:00
|
|
|
Pass2Ty::iterator PI = Pass2.find(*I);
|
2008-03-08 06:58:01 +08:00
|
|
|
if (PI != Pass2.end()) {
|
|
|
|
PI->second->addPredecessor(NewN);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue nodes to the worklist that were marked during pass 1.
|
2009-02-21 05:10:26 +08:00
|
|
|
if (Pass1.count(*I))
|
|
|
|
WL2.push_back(*I);
|
2008-03-08 06:58:01 +08:00
|
|
|
}
|
|
|
|
|
2009-02-21 05:10:26 +08:00
|
|
|
// Finally, explictly mark all nodes without any successors as sinks.
|
2008-03-08 06:58:01 +08:00
|
|
|
if (N->isSink())
|
|
|
|
NewN->markAsSink();
|
|
|
|
}
|
|
|
|
|
|
|
|
return G;
|
|
|
|
}
|
2009-02-05 07:49:09 +08:00
|
|
|
|
|
|
|
ExplodedNodeImpl*
|
|
|
|
InterExplodedGraphMapImpl::getMappedImplNode(const ExplodedNodeImpl* N) const {
|
|
|
|
llvm::DenseMap<const ExplodedNodeImpl*, ExplodedNodeImpl*>::iterator I =
|
|
|
|
M.find(N);
|
|
|
|
|
|
|
|
return I == M.end() ? 0 : I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
InterExplodedGraphMapImpl::InterExplodedGraphMapImpl() {}
|
|
|
|
|