opnsense-src/lib/Checker/ExplodedGraph.cpp

282 lines
8.3 KiB
C++
Raw Normal View History

2009-06-02 13:58:47 -04: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."
//
//===----------------------------------------------------------------------===//
2010-02-16 04:31:36 -05:00
#include "clang/Checker/PathSensitive/ExplodedGraph.h"
#include "clang/Checker/PathSensitive/GRState.h"
2009-06-02 13:58:47 -04:00
#include "clang/AST/Stmt.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include <vector>
using namespace clang;
//===----------------------------------------------------------------------===//
// Node auditing.
//===----------------------------------------------------------------------===//
// An out of line virtual method to provide a home for the class vtable.
2009-10-14 14:03:49 -04:00
ExplodedNode::Auditor::~Auditor() {}
2009-06-02 13:58:47 -04:00
#ifndef NDEBUG
2009-10-14 14:03:49 -04:00
static ExplodedNode::Auditor* NodeAuditor = 0;
2009-06-02 13:58:47 -04:00
#endif
2009-10-14 14:03:49 -04:00
void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) {
2009-06-02 13:58:47 -04:00
#ifndef NDEBUG
NodeAuditor = A;
#endif
}
//===----------------------------------------------------------------------===//
2009-10-14 14:03:49 -04:00
// ExplodedNode.
2009-06-02 13:58:47 -04:00
//===----------------------------------------------------------------------===//
2009-10-14 14:03:49 -04:00
static inline BumpVector<ExplodedNode*>& getVector(void* P) {
return *reinterpret_cast<BumpVector<ExplodedNode*>*>(P);
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
void ExplodedNode::addPredecessor(ExplodedNode* V, ExplodedGraph &G) {
2009-06-02 13:58:47 -04:00
assert (!V->isSink());
2009-10-14 14:03:49 -04:00
Preds.addNode(V, G);
V->Succs.addNode(this, G);
2009-06-02 13:58:47 -04:00
#ifndef NDEBUG
if (NodeAuditor) NodeAuditor->AddEdge(V, this);
#endif
}
2009-10-14 14:03:49 -04:00
void ExplodedNode::NodeGroup::addNode(ExplodedNode* N, ExplodedGraph &G) {
assert((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
assert(!getFlag());
2009-06-02 13:58:47 -04:00
if (getKind() == Size1) {
2009-10-14 14:03:49 -04:00
if (ExplodedNode* NOld = getNode()) {
BumpVectorContext &Ctx = G.getNodeAllocator();
BumpVector<ExplodedNode*> *V =
G.getAllocator().Allocate<BumpVector<ExplodedNode*> >();
new (V) BumpVector<ExplodedNode*>(Ctx, 4);
assert((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
V->push_back(NOld, Ctx);
V->push_back(N, Ctx);
2009-06-02 13:58:47 -04:00
P = reinterpret_cast<uintptr_t>(V) | SizeOther;
2009-10-14 14:03:49 -04:00
assert(getPtr() == (void*) V);
assert(getKind() == SizeOther);
2009-06-02 13:58:47 -04:00
}
else {
P = reinterpret_cast<uintptr_t>(N);
2009-10-14 14:03:49 -04:00
assert(getKind() == Size1);
2009-06-02 13:58:47 -04:00
}
}
else {
2009-10-14 14:03:49 -04:00
assert(getKind() == SizeOther);
getVector(getPtr()).push_back(N, G.getNodeAllocator());
2009-06-02 13:58:47 -04:00
}
}
2009-10-14 14:03:49 -04:00
unsigned ExplodedNode::NodeGroup::size() const {
2009-06-02 13:58:47 -04:00
if (getFlag())
return 0;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (getKind() == Size1)
return getNode() ? 1 : 0;
else
return getVector(getPtr()).size();
}
2009-10-14 14:03:49 -04:00
ExplodedNode **ExplodedNode::NodeGroup::begin() const {
2009-06-02 13:58:47 -04:00
if (getFlag())
return NULL;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (getKind() == Size1)
2009-10-14 14:03:49 -04:00
return (ExplodedNode**) (getPtr() ? &P : NULL);
2009-06-02 13:58:47 -04:00
else
2009-10-14 14:03:49 -04:00
return const_cast<ExplodedNode**>(&*(getVector(getPtr()).begin()));
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
ExplodedNode** ExplodedNode::NodeGroup::end() const {
2009-06-02 13:58:47 -04:00
if (getFlag())
return NULL;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (getKind() == Size1)
2009-10-14 14:03:49 -04:00
return (ExplodedNode**) (getPtr() ? &P+1 : NULL);
2009-06-02 13:58:47 -04:00
else {
// Dereferencing end() is undefined behaviour. The vector is not empty, so
// we can dereference the last elem and then add 1 to the result.
2009-10-14 14:03:49 -04:00
return const_cast<ExplodedNode**>(getVector(getPtr()).end());
2009-06-02 13:58:47 -04:00
}
}
2009-10-14 14:03:49 -04:00
ExplodedNode *ExplodedGraph::getNode(const ProgramPoint& L,
const GRState* State, bool* IsNew) {
// Profile 'State' to determine if we already have an existing node.
llvm::FoldingSetNodeID profile;
void* InsertPos = 0;
NodeTy::Profile(profile, L, State);
NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
if (!V) {
// Allocate a new node.
V = (NodeTy*) getAllocator().Allocate<NodeTy>();
new (V) NodeTy(L, State);
// Insert the node into the node set and return it.
Nodes.InsertNode(V, InsertPos);
++NumNodes;
if (IsNew) *IsNew = true;
}
else
if (IsNew) *IsNew = false;
return V;
}
std::pair<ExplodedGraph*, InterExplodedGraphMap*>
ExplodedGraph::Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd,
llvm::DenseMap<const void*, const void*> *InverseMap) const {
if (NBeg == NEnd)
return std::make_pair((ExplodedGraph*) 0,
(InterExplodedGraphMap*) 0);
assert (NBeg < NEnd);
llvm::OwningPtr<InterExplodedGraphMap> M(new InterExplodedGraphMap());
ExplodedGraph* G = TrimInternal(NBeg, NEnd, M.get(), InverseMap);
return std::make_pair(static_cast<ExplodedGraph*>(G), M.take());
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
ExplodedGraph*
ExplodedGraph::TrimInternal(const ExplodedNode* const* BeginSources,
const ExplodedNode* const* EndSources,
InterExplodedGraphMap* M,
llvm::DenseMap<const void*, const void*> *InverseMap) const {
typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty;
2009-06-02 13:58:47 -04:00
Pass1Ty Pass1;
2009-10-14 14:03:49 -04:00
typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty;
2009-06-02 13:58:47 -04:00
Pass2Ty& Pass2 = M->M;
2009-10-14 14:03:49 -04:00
llvm::SmallVector<const ExplodedNode*, 10> WL1, WL2;
2009-06-02 13:58:47 -04:00
// ===- Pass 1 (reverse DFS) -===
2009-10-14 14:03:49 -04:00
for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) {
2009-06-02 13:58:47 -04:00
assert(*I);
WL1.push_back(*I);
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Process the first worklist until it is empty. Because it is a std::list
// it acts like a FIFO queue.
while (!WL1.empty()) {
2009-10-14 14:03:49 -04:00
const ExplodedNode *N = WL1.back();
2009-06-02 13:58:47 -04:00
WL1.pop_back();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Have we already visited this node? If so, continue to the next one.
if (Pass1.count(N))
continue;
// Otherwise, mark this node as visited.
Pass1.insert(N);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// If this is a root enqueue it to the second worklist.
if (N->Preds.empty()) {
WL2.push_back(N);
continue;
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Visit our predecessors and enqueue them.
2009-10-14 14:03:49 -04:00
for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I)
2009-06-02 13:58:47 -04:00
WL1.push_back(*I);
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// We didn't hit a root? Return with a null pointer for the new graph.
if (WL2.empty())
return 0;
// Create an empty graph.
2009-10-14 14:03:49 -04:00
ExplodedGraph* G = MakeEmptyGraph();
// ===- Pass 2 (forward DFS to construct the new graph) -===
2009-06-02 13:58:47 -04:00
while (!WL2.empty()) {
2009-10-14 14:03:49 -04:00
const ExplodedNode* N = WL2.back();
2009-06-02 13:58:47 -04:00
WL2.pop_back();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Skip this node if we have already processed it.
if (Pass2.find(N) != Pass2.end())
continue;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Create the corresponding node in the new graph and record the mapping
// from the old node to the new node.
2009-10-14 14:03:49 -04:00
ExplodedNode* NewN = G->getNode(N->getLocation(), N->State, NULL);
2009-06-02 13:58:47 -04:00
Pass2[N] = NewN;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Also record the reverse mapping from the new node to the old node.
if (InverseMap) (*InverseMap)[NewN] = N;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// If this node is a root, designate it as such in the graph.
if (N->Preds.empty())
G->addRoot(NewN);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// In the case that some of the intended predecessors of NewN have already
// been created, we should hook them up as predecessors.
// Walk through the predecessors of 'N' and hook up their corresponding
// nodes in the new graph (if any) to the freshly created node.
2009-10-14 14:03:49 -04:00
for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
2009-06-02 13:58:47 -04:00
Pass2Ty::iterator PI = Pass2.find(*I);
if (PI == Pass2.end())
continue;
2009-10-14 14:03:49 -04:00
NewN->addPredecessor(PI->second, *G);
2009-06-02 13:58:47 -04:00
}
// 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.
2009-10-14 14:03:49 -04:00
for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
Pass2Ty::iterator PI = Pass2.find(*I);
2009-06-02 13:58:47 -04:00
if (PI != Pass2.end()) {
2009-10-14 14:03:49 -04:00
PI->second->addPredecessor(NewN, *G);
2009-06-02 13:58:47 -04:00
continue;
}
// Enqueue nodes to the worklist that were marked during pass 1.
if (Pass1.count(*I))
WL2.push_back(*I);
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Finally, explictly mark all nodes without any successors as sinks.
if (N->isSink())
NewN->markAsSink();
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
return G;
}
2009-10-14 14:03:49 -04:00
ExplodedNode*
InterExplodedGraphMap::getMappedNode(const ExplodedNode* N) const {
2009-11-18 09:59:57 -05:00
llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::const_iterator I =
2009-06-02 13:58:47 -04:00
M.find(N);
return I == M.end() ? 0 : I->second;
}