ROSE  0.9.12.28
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 #include <Partitioner2/Utility.h>
8 
9 #include <Sawyer/BiMap.h>
10 #include <Sawyer/Graph.h>
11 #include <Sawyer/GraphIteratorSet.h>
12 #include <Sawyer/GraphIteratorBiMap.h>
13 #include <Sawyer/HashMap.h>
14 #include <Sawyer/Map.h>
15 #include <Sawyer/Optional.h>
16 
17 #include <boost/serialization/access.hpp>
18 #include <list>
19 #include <ostream>
20 
21 namespace Rose {
22 namespace BinaryAnalysis {
23 namespace Partitioner2 {
24 
26 class CfgVertex {
27  VertexType type_; // type of vertex, special or not
28  rose_addr_t startVa_; // address of start of basic block
29  BasicBlock::Ptr bblock_; // basic block, or null if only a place holder
30  FunctionSet owningFunctions_; // functions to which vertex belongs
31 
32 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
33 private:
34  friend class boost::serialization::access;
35 
36  template<class S>
37  void serialize(S &s, const unsigned /*version*/) {
38  s & BOOST_SERIALIZATION_NVP(type_);
39  s & BOOST_SERIALIZATION_NVP(startVa_);
40  s & BOOST_SERIALIZATION_NVP(bblock_);
41  s & BOOST_SERIALIZATION_NVP(owningFunctions_);
42  }
43 #endif
44 
45 public:
46  // intentionally undocumented. Necessary for serialization of Sawyer::Container::Graph
47  CfgVertex()
48  : type_(V_USER_DEFINED), startVa_(0) {}
49 
50 public:
52  explicit CfgVertex(rose_addr_t startVa): type_(V_BASIC_BLOCK), startVa_(startVa) {}
53 
55  explicit CfgVertex(const BasicBlock::Ptr &bb): type_(V_BASIC_BLOCK), bblock_(bb) {
56  ASSERT_not_null(bb);
57  startVa_ = bb->address();
58  }
59 
61  /*implicit*/ CfgVertex(VertexType type): type_(type), startVa_(0) {
62  ASSERT_forbid2(type==V_BASIC_BLOCK, "this constructor does not create basic block or placeholder vertices");
63  }
64 
66  VertexType type() const { return type_; }
67 
76  rose_addr_t address() const {
77  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
78  return startVa_;
79  }
80  void address(rose_addr_t va) {
81  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
82  startVa_ = va;
83  }
90 
96 
105 
112  const BasicBlock::Ptr& bblock() const {
113  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
114  return bblock_;
115  }
116  void bblock(const BasicBlock::Ptr &bb) {
117  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
118  bblock_ = bb;
119  }
125  bool insertOwningFunction(const Function::Ptr &function) {
126  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
127  ASSERT_not_null(function);
128  return owningFunctions_.insert(function);
129  }
130 
135  void eraseOwningFunction(const Function::Ptr &function) {
136  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
137  if (function != NULL)
138  owningFunctions_.erase(function);
139  }
140 
144  bool isOwningFunction(const Function::Ptr &function) const {
145  return owningFunctions_.exists(function);
146  }
147 
149  size_t nOwningFunctions() const {
150  return owningFunctions_.size();
151  }
152 
160  const FunctionSet& owningFunctions() const {
161  return owningFunctions_;
162  }
164  return owningFunctions_;
165  }
171  Function::Ptr isEntryBlock() const;
172 
176  void nullify() {
177  ASSERT_require(V_BASIC_BLOCK==type_);
178  bblock_ = BasicBlock::Ptr();
179  }
180 };
181 
183 class CfgEdge {
184 private:
185  EdgeType type_;
186  Confidence confidence_;
187 
188 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
189 private:
190  friend class boost::serialization::access;
191 
192  template<class S>
193  void serialize(S &s, const unsigned /*version*/) {
194  s & BOOST_SERIALIZATION_NVP(type_);
195  s & BOOST_SERIALIZATION_NVP(confidence_);
196  }
197 #endif
198 
199 public:
201  CfgEdge(): type_(E_NORMAL), confidence_(ASSUMED) {}
202 
204  /*implicit*/ CfgEdge(EdgeType type, Confidence confidence=ASSUMED): type_(type), confidence_(confidence) {}
205 
207  EdgeType type() const { return type_; }
208 
214  Confidence confidence() const { return confidence_; }
215  void confidence(Confidence c) { confidence_ = c; }
217 };
218 
221 
223 bool sortEdgesBySrc(const ControlFlowGraph::ConstEdgeIterator&, const ControlFlowGraph::ConstEdgeIterator&);
224 
226 bool sortEdgesByDst(const ControlFlowGraph::ConstEdgeIterator&, const ControlFlowGraph::ConstEdgeIterator&);
227 
229 bool sortEdgesById(const ControlFlowGraph::ConstEdgeIterator&, const ControlFlowGraph::ConstEdgeIterator&);
230 
232 bool sortVerticesByAddress(const ControlFlowGraph::ConstVertexIterator&, const ControlFlowGraph::ConstVertexIterator&);
233 
235 bool sortVerticesById(const ControlFlowGraph::ConstVertexIterator&, const ControlFlowGraph::ConstVertexIterator&);
236 
238 std::ostream& operator<<(std::ostream&, const ControlFlowGraph::Vertex&);
239 
241 std::ostream& operator<<(std::ostream&, const ControlFlowGraph::Edge&);
242 
245 
249 typedef std::list<ControlFlowGraph::VertexIterator> CfgVertexList;
250 typedef std::list<ControlFlowGraph::ConstVertexIterator> CfgConstVertexList;
256 typedef std::list<ControlFlowGraph::EdgeIterator> CfgEdgeList;
257 typedef std::list<ControlFlowGraph::ConstEdgeIterator> CfgConstEdgeList;
275 typedef Sawyer::Container::GraphIteratorBiMap<ControlFlowGraph::ConstVertexIterator,
276  ControlFlowGraph::ConstVertexIterator> CfgVertexMap;
277 
285 public:
288 
298  rose_addr_t startVa;
300  AttachedBasicBlock(Partitioner *partitioner, rose_addr_t startVa, const BasicBlock::Ptr &bblock)
301  : partitioner(partitioner), startVa(startVa), bblock(bblock) {}
302  };
303 
314  rose_addr_t startVa;
316  DetachedBasicBlock(Partitioner *partitioner, rose_addr_t startVa, const BasicBlock::Ptr &bblock)
317  : partitioner(partitioner), startVa(startVa), bblock(bblock) {}
318  };
319 
321  virtual bool operator()(bool chain, const AttachedBasicBlock&) = 0;
322 
324  virtual bool operator()(bool chain, const DetachedBasicBlock&) = 0;
325 };
326 
328 // Control flow graph utility functions
330 
335 void insertCfg(ControlFlowGraph &dst, const ControlFlowGraph &src, CfgVertexMap &vmap /*out*/);
336 
341 CfgConstEdgeSet findBackEdges(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &begin);
342 
346 CfgConstEdgeSet findCallEdges(const ControlFlowGraph::ConstVertexIterator &callSite);
347 
351 CfgConstVertexSet findCalledFunctions(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &callSite);
352 
362 CfgConstEdgeSet findCallReturnEdges(const Partitioner&, const ControlFlowGraph&);
363 CfgConstEdgeSet findCallReturnEdges(const ControlFlowGraph::ConstVertexIterator &callSite);
369 CfgConstVertexSet findFunctionReturns(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &beginVertex);
370 
382 Sawyer::Container::Map<Function::Ptr, CfgConstEdgeSet> findFunctionReturnEdges(const Partitioner&, const ControlFlowGraph&);
388 void eraseEdges(ControlFlowGraph&, const CfgConstEdgeSet &toErase);
389 
391 CfgConstVertexSet findIncidentVertices(const CfgConstEdgeSet&);
392 
399 CfgConstVertexSet findDetachedVertices(const ControlFlowGraph&);
400 CfgConstVertexSet findDetachedVertices(const CfgConstVertexSet &vertices);
407 CfgConstVertexSet forwardMapped(const CfgConstVertexSet&, const CfgVertexMap&);
408 
413 CfgConstVertexSet reverseMapped(const CfgConstVertexSet&, const CfgVertexMap&);
414 
421 void expandFunctionReturnEdges(const Partitioner&, ControlFlowGraph &cfg/*in,out*/);
422 
437 ControlFlowGraph functionCfgByErasure(const ControlFlowGraph &gcfg, const Function::Ptr &function,
438  ControlFlowGraph::VertexIterator &entry/*out*/);
439 
454 ControlFlowGraph functionCfgByReachability(const ControlFlowGraph &gcfg, const Function::Ptr &function,
455  const ControlFlowGraph::ConstVertexIterator &gcfgEntry);
456 
457 } // namespace
458 } // namespace
459 } // namespace
460 
461 #endif
CfgVertex(VertexType type)
Construct a special vertex.
size_t size() const
Size of the set.
Definition: Set.h:168
Sawyer::Optional< rose_addr_t > optionalAddress() const
Safe version of starting address.
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:50
CfgConstEdgeSet findCallReturnEdges(const Partitioner &, const ControlFlowGraph &)
Return outgoing call-return edges.
Sawyer::Container::GraphIteratorSet< ControlFlowGraph::VertexIterator > CfgVertexSet
Set of CFG vertex pointers.
void address(rose_addr_t va)
Property: starting address.
bool insert(const Value &value)
Insert a value.
Definition: Set.h:222
bool sortVerticesById(const ControlFlowGraph::ConstVertexIterator &, const ControlFlowGraph::ConstVertexIterator &)
Sort vertices by vertex ID number.
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.
bool sortEdgesById(const ControlFlowGraph::ConstEdgeIterator &, const ControlFlowGraph::ConstEdgeIterator &)
Sort edges by edge ID number.
Set of graph edge or vertex pointers (iterators).
void eraseEdges(ControlFlowGraph &, const CfgConstEdgeSet &toErase)
Erase multiple edges.
Confidence confidence() const
Property: Confidence.
Sawyer::Optional< rose_addr_t > optionalLastAddress() const
Address of last item in the vertex.
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.
Sawyer::Container::GraphIteratorSet< ControlFlowGraph::ConstVertexIterator > CfgConstVertexSet
Set of CFG vertex pointers.
VertexType
Partitioner control flow vertex types.
Definition: BasicTypes.h:39
bool sortVerticesByAddress(const ControlFlowGraph::ConstVertexIterator &, const ControlFlowGraph::ConstVertexIterator &)
Sort vertices by address.
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.
Bidirectional map of graph edge or vertex 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:75
rose_addr_t address() const
Property: starting address.
bool sortEdgesBySrc(const ControlFlowGraph::ConstEdgeIterator &, const ControlFlowGraph::ConstEdgeIterator &)
Sort edges by source vertex address.
bool sortEdgesByDst(const ControlFlowGraph::ConstEdgeIterator &, const ControlFlowGraph::ConstEdgeIterator &)
Sort edges by target vertex address.
Confidence
How sure are we of something.
Definition: BasicTypes.h:74
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.
Sawyer::Container::GraphIteratorSet< ControlFlowGraph::EdgeIterator > CfgEdgeSet
Set of CFG edge pointers.
A basic block or placeholder for a basic block.
Definition: BasicTypes.h:40
Normal control flow edge, nothing special.
Definition: BasicTypes.h:51
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.
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:131
Base class for reference counted objects.
Definition: SharedObject.h:64
CfgConstVertexSet findIncidentVertices(const CfgConstEdgeSet &)
Vertices that are incident to specified edges.
Sawyer::Container::GraphIteratorBiMap< ControlFlowGraph::ConstVertexIterator, ControlFlowGraph::ConstVertexIterator > CfgVertexMap
Map vertices from one CFG to another CFG and vice versa.
Function::Ptr isEntryBlock() const
Is block a function entry block?
const Partitioner * partitioner
Partitioner in which change occurred.
CfgConstVertexSet findCalledFunctions(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &callSite)
Find called functions.
Sawyer::Container::GraphIteratorSet< ControlFlowGraph::ConstEdgeIterator > CfgConstEdgeSet
Set of CFG edge pointers.
CfgConstEdgeSet findBackEdges(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &begin)
Find back edges.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:316
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.