ROSE  0.9.10.95
ControlFlowGraph.h
1 #ifndef ROSE_Partitioner2_ControlFlowGraph_H
2 #define ROSE_Partitioner2_ControlFlowGraph_H
3 
4 #include <Partitioner2/BasicBlock.h>
5 #include <Partitioner2/BasicTypes.h>
6 #include <Partitioner2/Function.h>
7 
8 #include <Sawyer/BiMap.h>
9 #include <Sawyer/Graph.h>
10 #include <Sawyer/HashMap.h>
11 #include <Sawyer/Map.h>
12 
13 #include <boost/serialization/access.hpp>
14 #include <list>
15 #include <ostream>
16 
17 namespace Rose {
18 namespace BinaryAnalysis {
19 namespace Partitioner2 {
20 
22 class CfgVertex {
23  VertexType type_; // type of vertex, special or not
24  rose_addr_t startVa_; // address of start of basic block
25  BasicBlock::Ptr bblock_; // basic block, or null if only a place holder
26  FunctionSet owningFunctions_; // functions to which vertex belongs
27 
28 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
29 private:
30  friend class boost::serialization::access;
31 
32  template<class S>
33  void serialize(S &s, const unsigned /*version*/) {
34  s & BOOST_SERIALIZATION_NVP(type_);
35  s & BOOST_SERIALIZATION_NVP(startVa_);
36  s & BOOST_SERIALIZATION_NVP(bblock_);
37  s & BOOST_SERIALIZATION_NVP(owningFunctions_);
38  }
39 #endif
40 
41 public:
42  // intentionally undocumented. Necessary for serialization of Sawyer::Container::Graph
43  CfgVertex()
44  : type_(V_USER_DEFINED), startVa_(0) {}
45 
46 public:
48  explicit CfgVertex(rose_addr_t startVa): type_(V_BASIC_BLOCK), startVa_(startVa) {}
49 
51  explicit CfgVertex(const BasicBlock::Ptr &bb): type_(V_BASIC_BLOCK), bblock_(bb) {
52  ASSERT_not_null(bb);
53  startVa_ = bb->address();
54  }
55 
57  /*implicit*/ CfgVertex(VertexType type): type_(type), startVa_(0) {
58  ASSERT_forbid2(type==V_BASIC_BLOCK, "this constructor does not create basic block or placeholder vertices");
59  }
60 
62  VertexType type() const { return type_; }
63 
69  rose_addr_t address() const {
70  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
71  return startVa_;
72  }
73  void address(rose_addr_t va) {
74  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
75  startVa_ = va;
76  }
87 
94  const BasicBlock::Ptr& bblock() const {
95  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
96  return bblock_;
97  }
98  void bblock(const BasicBlock::Ptr &bb) {
99  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
100  bblock_ = bb;
101  }
107  bool insertOwningFunction(const Function::Ptr &function) {
108  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
109  ASSERT_not_null(function);
110  return owningFunctions_.insert(function);
111  }
112 
117  void eraseOwningFunction(const Function::Ptr &function) {
118  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
119  if (function != NULL)
120  owningFunctions_.erase(function);
121  }
122 
126  bool isOwningFunction(const Function::Ptr &function) const {
127  return owningFunctions_.exists(function);
128  }
129 
131  size_t nOwningFunctions() const {
132  return owningFunctions_.size();
133  }
134 
142  const FunctionSet& owningFunctions() const {
143  return owningFunctions_;
144  }
146  return owningFunctions_;
147  }
153  Function::Ptr isEntryBlock() const;
154 
158  void nullify() {
159  ASSERT_require(V_BASIC_BLOCK==type_);
160  bblock_ = BasicBlock::Ptr();
161  }
162 };
163 
165 class CfgEdge {
166 private:
167  EdgeType type_;
168  Confidence confidence_;
169 
170 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
171 private:
172  friend class boost::serialization::access;
173 
174  template<class S>
175  void serialize(S &s, const unsigned /*version*/) {
176  s & BOOST_SERIALIZATION_NVP(type_);
177  s & BOOST_SERIALIZATION_NVP(confidence_);
178  }
179 #endif
180 
181 public:
183  CfgEdge(): type_(E_NORMAL), confidence_(ASSUMED) {}
184 
186  /*implicit*/ CfgEdge(EdgeType type, Confidence confidence=ASSUMED): type_(type), confidence_(confidence) {}
187 
189  EdgeType type() const { return type_; }
190 
196  Confidence confidence() const { return confidence_; }
197  void confidence(Confidence c) { confidence_ = c; }
199 };
200 
203 
206 
210 typedef std::list<ControlFlowGraph::VertexIterator> CfgVertexList;
211 typedef std::list<ControlFlowGraph::ConstVertexIterator> CfgConstVertexList;
217 typedef std::list<ControlFlowGraph::EdgeIterator> CfgEdgeList;
218 typedef std::list<ControlFlowGraph::ConstEdgeIterator> CfgConstEdgeList;
224 typedef std::set<ControlFlowGraph::VertexIterator> CfgVertexSet;
225 typedef std::set<ControlFlowGraph::ConstVertexIterator> CfgConstVertexSet;
231 typedef std::set<ControlFlowGraph::EdgeIterator> CfgEdgeSet;
232 typedef std::set<ControlFlowGraph::ConstEdgeIterator> CfgConstEdgeSet;
237 
238 
239 
240 
248 public:
251 
261  rose_addr_t startVa;
263  AttachedBasicBlock(Partitioner *partitioner, rose_addr_t startVa, const BasicBlock::Ptr &bblock)
264  : partitioner(partitioner), startVa(startVa), bblock(bblock) {}
265  };
266 
277  rose_addr_t startVa;
279  DetachedBasicBlock(Partitioner *partitioner, rose_addr_t startVa, const BasicBlock::Ptr &bblock)
280  : partitioner(partitioner), startVa(startVa), bblock(bblock) {}
281  };
282 
284  virtual bool operator()(bool chain, const AttachedBasicBlock&) = 0;
285 
287  virtual bool operator()(bool chain, const DetachedBasicBlock&) = 0;
288 };
289 
291 // Control flow graph utility functions
293 
298 void insertCfg(ControlFlowGraph &dst, const ControlFlowGraph &src, CfgVertexMap &vmap /*out*/);
299 
304 CfgConstEdgeSet findBackEdges(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &begin);
305 
309 CfgConstEdgeSet findCallEdges(const ControlFlowGraph::ConstVertexIterator &callSite);
310 
314 CfgConstVertexSet findCalledFunctions(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &callSite);
315 
325 CfgConstEdgeSet findCallReturnEdges(const Partitioner&, const ControlFlowGraph&);
326 CfgConstEdgeSet findCallReturnEdges(const ControlFlowGraph::ConstVertexIterator &callSite);
332 CfgConstVertexSet findFunctionReturns(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &beginVertex);
333 
345 Sawyer::Container::Map<Function::Ptr, CfgConstEdgeSet> findFunctionReturnEdges(const Partitioner&, const ControlFlowGraph&);
351 void eraseEdges(ControlFlowGraph&, const CfgConstEdgeSet &toErase);
352 
354 CfgConstVertexSet findIncidentVertices(const CfgConstEdgeSet&);
355 
362 CfgConstVertexSet findDetachedVertices(const ControlFlowGraph&);
363 CfgConstVertexSet findDetachedVertices(const CfgConstVertexSet &vertices);
370 CfgConstVertexSet forwardMapped(const CfgConstVertexSet&, const CfgVertexMap&);
371 
376 CfgConstVertexSet reverseMapped(const CfgConstVertexSet&, const CfgVertexMap&);
377 
384 void expandFunctionReturnEdges(const Partitioner&, ControlFlowGraph &cfg/*in,out*/);
385 
400 ControlFlowGraph functionCfgByErasure(const ControlFlowGraph &gcfg, const Function::Ptr &function,
401  ControlFlowGraph::VertexIterator &entry/*out*/);
402 
417 ControlFlowGraph functionCfgByReachability(const ControlFlowGraph &gcfg, const Function::Ptr &function,
418  const ControlFlowGraph::ConstVertexIterator &gcfgEntry);
419 
420 } // namespace
421 } // namespace
422 } // namespace
423 
424 #endif
CfgVertex(VertexType type)
Construct a special vertex.
size_t size() const
Size of the set.
Definition: Set.h:168
void eraseOwningFunction(const Function::Ptr &function)
Remove a function from the list of functions that own this vertex.
CfgVertex(const BasicBlock::Ptr &bb)
Construct a basic block vertex.
const BasicBlock::Ptr & bblock() const
Property: basic block.
Base class for CFG-adjustment callbacks.
EdgeType type() const
Return edge type.
VertexType type() const
Returns the vertex type.
std::list< ControlFlowGraph::ConstVertexIterator > CfgConstVertexList
List of CFG vertex pointers.
EdgeType
Partitioner control flow edge types.
Definition: BasicTypes.h:42
CfgConstEdgeSet findCallReturnEdges(const Partitioner &, const ControlFlowGraph &)
Return outgoing call-return edges.
void address(rose_addr_t va)
Property: starting address.
bool insert(const Value &value)
Insert a value.
Definition: Set.h:222
std::set< ControlFlowGraph::ConstVertexIterator > CfgConstVertexSet
Set of CFG vertex pointers.
bool isOwningFunction(const Function::Ptr &function) const
Determines if a function owns this vertex.
bool exists(const Value &value) const
Whether a value exists.
Definition: Set.h:139
Sawyer::Container::Graph< CfgVertex, CfgEdge > ControlFlowGraph
Control flow graph.
CfgVertex(rose_addr_t startVa)
Construct a basic block placeholder vertex.
void eraseEdges(ControlFlowGraph &, const CfgConstEdgeSet &toErase)
Erase multiple edges.
Confidence confidence() const
Property: Confidence.
size_t nOwningFunctions() const
Number of functions that own this vertex.
Sawyer::Container::HashMap< rose_addr_t, ControlFlowGraph::VertexIterator > CfgVertexIndex
Mapping from basic block starting address to CFG vertex.
Main namespace for the ROSE library.
VertexType
Partitioner control flow vertex types.
Definition: BasicTypes.h:31
Sawyer::Container::BiMap< ControlFlowGraph::ConstVertexIterator, ControlFlowGraph::ConstVertexIterator > CfgVertexMap
Map vertices from one CFG to another CFG and vice versa.
CfgConstVertexSet findFunctionReturns(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &beginVertex)
Find all function return vertices.
Sawyer::SharedPointer< CfgAdjustmentCallback > Ptr
Shared ownership pointer to CfgAdjustmentCallback.
std::list< ControlFlowGraph::ConstEdgeIterator > CfgConstEdgeList
List of CFG edge pointers.
CfgConstEdgeSet findCallEdges(const ControlFlowGraph::ConstVertexIterator &callSite)
Find function call edges.
std::list< ControlFlowGraph::EdgeIterator > CfgEdgeList
List of CFG edge pointers.
rose_addr_t startVa
Starting address for basic block or placeholder.
void expandFunctionReturnEdges(const Partitioner &, ControlFlowGraph &cfg)
Rewrite function return edges.
CfgConstVertexSet findDetachedVertices(const ControlFlowGraph &)
Find vertices that have zero degree.
CfgConstVertexSet reverseMapped(const CfgConstVertexSet &, const CfgVertexMap &)
Return corresponding iterators.
The value is an assumption without any proof.
Definition: BasicTypes.h:67
rose_addr_t address() const
Property: starting address.
Confidence
How sure are we of something.
Definition: BasicTypes.h:66
rose_addr_t startVa
Starting address for basic block or placeholder.
Sawyer::Container::Map< Function::Ptr, CfgConstEdgeSet > findFunctionReturnEdges(const Partitioner &)
Find function return edges organized by function.
void confidence(Confidence c)
Property: Confidence.
std::set< ControlFlowGraph::ConstEdgeIterator > CfgConstEdgeSet
Set of CFG edge pointers.
A basic block or placeholder for a basic block.
Definition: BasicTypes.h:32
Normal control flow edge, nothing special.
Definition: BasicTypes.h:43
const FunctionSet & owningFunctions() const
Property: Owning functions.
CfgEdge()
Construct a new normal edge.
ControlFlowGraph functionCfgByErasure(const ControlFlowGraph &gcfg, const Function::Ptr &function, ControlFlowGraph::VertexIterator &entry)
Generate a function control flow graph.
void nullify()
Turns a basic block vertex into a placeholder.
std::set< ControlFlowGraph::EdgeIterator > CfgEdgeSet
Set of CFG edge pointers.
bool erase(const Value &value)
Erase a value.
Definition: Set.h:242
std::list< ControlFlowGraph::VertexIterator > CfgVertexList
List of CFG vertex pointers.
BasicBlock::Ptr bblock
Optional basic block; otherwise a placeholder operation.
CfgEdge(EdgeType type, Confidence confidence=ASSUMED)
Construct an edge with a specified type and confidence.
BasicBlock::Ptr bblock
Optional basic block; otherwise a placeholder operation.
CfgConstVertexSet forwardMapped(const CfgConstVertexSet &, const CfgVertexMap &)
Return corresponding iterators.
Sawyer::SharedPointer< BasicBlock > Ptr
Shared pointer to a basic block.
Definition: BasicBlock.h:45
Base class for reference counted objects.
Definition: SharedObject.h:22
CfgConstVertexSet findIncidentVertices(const CfgConstEdgeSet &)
Vertices that are incident to specified edges.
Function::Ptr isEntryBlock() const
Is block a function entry block?
std::set< ControlFlowGraph::VertexIterator > CfgVertexSet
Set of CFG vertex pointers.
const Partitioner * partitioner
Partitioner in which change occurred.
CfgConstVertexSet findCalledFunctions(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &callSite)
Find called functions.
CfgConstEdgeSet findBackEdges(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &begin)
Find back edges.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:293
FunctionPtr Ptr
Shared-ownership pointer for function.
Definition: Function.h:48
const Partitioner * partitioner
Partitioner in which change occurred.
void insertCfg(ControlFlowGraph &dst, const ControlFlowGraph &src, CfgVertexMap &vmap)
Insert one control flow graph into another.
FunctionSet & owningFunctions()
Property: Owning functions.
void bblock(const BasicBlock::Ptr &bb)
Property: basic block.
ControlFlowGraph functionCfgByReachability(const ControlFlowGraph &gcfg, const Function::Ptr &function, const ControlFlowGraph::ConstVertexIterator &gcfgEntry)
Generate a function control flow graph.
Container associating values with keys.
Definition: Sawyer/Map.h:66
virtual bool operator()(bool chain, const AttachedBasicBlock &)=0
Called when basic block is attached or placeholder inserted.
bool insertOwningFunction(const Function::Ptr &function)
Add a function to the list of functions that own this vertex.
AddressIntervalSet addresses() const
Compute entire address set.