ROSE  0.9.11.56
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 #include <Sawyer/Optional.h>
13 
14 #include <boost/serialization/access.hpp>
15 #include <list>
16 #include <ostream>
17 
18 namespace Rose {
19 namespace BinaryAnalysis {
20 namespace Partitioner2 {
21 
23 class CfgVertex {
24  VertexType type_; // type of vertex, special or not
25  rose_addr_t startVa_; // address of start of basic block
26  BasicBlock::Ptr bblock_; // basic block, or null if only a place holder
27  FunctionSet owningFunctions_; // functions to which vertex belongs
28 
29 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
30 private:
31  friend class boost::serialization::access;
32 
33  template<class S>
34  void serialize(S &s, const unsigned /*version*/) {
35  s & BOOST_SERIALIZATION_NVP(type_);
36  s & BOOST_SERIALIZATION_NVP(startVa_);
37  s & BOOST_SERIALIZATION_NVP(bblock_);
38  s & BOOST_SERIALIZATION_NVP(owningFunctions_);
39  }
40 #endif
41 
42 public:
43  // intentionally undocumented. Necessary for serialization of Sawyer::Container::Graph
44  CfgVertex()
45  : type_(V_USER_DEFINED), startVa_(0) {}
46 
47 public:
49  explicit CfgVertex(rose_addr_t startVa): type_(V_BASIC_BLOCK), startVa_(startVa) {}
50 
52  explicit CfgVertex(const BasicBlock::Ptr &bb): type_(V_BASIC_BLOCK), bblock_(bb) {
53  ASSERT_not_null(bb);
54  startVa_ = bb->address();
55  }
56 
58  /*implicit*/ CfgVertex(VertexType type): type_(type), startVa_(0) {
59  ASSERT_forbid2(type==V_BASIC_BLOCK, "this constructor does not create basic block or placeholder vertices");
60  }
61 
63  VertexType type() const { return type_; }
64 
73  rose_addr_t address() const {
74  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
75  return startVa_;
76  }
77  void address(rose_addr_t va) {
78  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
79  startVa_ = va;
80  }
87 
93 
102 
109  const BasicBlock::Ptr& bblock() const {
110  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
111  return bblock_;
112  }
113  void bblock(const BasicBlock::Ptr &bb) {
114  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
115  bblock_ = bb;
116  }
122  bool insertOwningFunction(const Function::Ptr &function) {
123  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
124  ASSERT_not_null(function);
125  return owningFunctions_.insert(function);
126  }
127 
132  void eraseOwningFunction(const Function::Ptr &function) {
133  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
134  if (function != NULL)
135  owningFunctions_.erase(function);
136  }
137 
141  bool isOwningFunction(const Function::Ptr &function) const {
142  return owningFunctions_.exists(function);
143  }
144 
146  size_t nOwningFunctions() const {
147  return owningFunctions_.size();
148  }
149 
157  const FunctionSet& owningFunctions() const {
158  return owningFunctions_;
159  }
161  return owningFunctions_;
162  }
168  Function::Ptr isEntryBlock() const;
169 
173  void nullify() {
174  ASSERT_require(V_BASIC_BLOCK==type_);
175  bblock_ = BasicBlock::Ptr();
176  }
177 };
178 
180 class CfgEdge {
181 private:
182  EdgeType type_;
183  Confidence confidence_;
184 
185 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
186 private:
187  friend class boost::serialization::access;
188 
189  template<class S>
190  void serialize(S &s, const unsigned /*version*/) {
191  s & BOOST_SERIALIZATION_NVP(type_);
192  s & BOOST_SERIALIZATION_NVP(confidence_);
193  }
194 #endif
195 
196 public:
198  CfgEdge(): type_(E_NORMAL), confidence_(ASSUMED) {}
199 
201  /*implicit*/ CfgEdge(EdgeType type, Confidence confidence=ASSUMED): type_(type), confidence_(confidence) {}
202 
204  EdgeType type() const { return type_; }
205 
211  Confidence confidence() const { return confidence_; }
212  void confidence(Confidence c) { confidence_ = c; }
214 };
215 
218 
221 
225 typedef std::list<ControlFlowGraph::VertexIterator> CfgVertexList;
226 typedef std::list<ControlFlowGraph::ConstVertexIterator> CfgConstVertexList;
232 typedef std::list<ControlFlowGraph::EdgeIterator> CfgEdgeList;
233 typedef std::list<ControlFlowGraph::ConstEdgeIterator> CfgConstEdgeList;
239 typedef std::set<ControlFlowGraph::VertexIterator> CfgVertexSet;
240 typedef std::set<ControlFlowGraph::ConstVertexIterator> CfgConstVertexSet;
246 typedef std::set<ControlFlowGraph::EdgeIterator> CfgEdgeSet;
247 typedef std::set<ControlFlowGraph::ConstEdgeIterator> CfgConstEdgeSet;
252 
253 
254 
255 
263 public:
266 
276  rose_addr_t startVa;
278  AttachedBasicBlock(Partitioner *partitioner, rose_addr_t startVa, const BasicBlock::Ptr &bblock)
279  : partitioner(partitioner), startVa(startVa), bblock(bblock) {}
280  };
281 
292  rose_addr_t startVa;
294  DetachedBasicBlock(Partitioner *partitioner, rose_addr_t startVa, const BasicBlock::Ptr &bblock)
295  : partitioner(partitioner), startVa(startVa), bblock(bblock) {}
296  };
297 
299  virtual bool operator()(bool chain, const AttachedBasicBlock&) = 0;
300 
302  virtual bool operator()(bool chain, const DetachedBasicBlock&) = 0;
303 };
304 
306 // Control flow graph utility functions
308 
313 void insertCfg(ControlFlowGraph &dst, const ControlFlowGraph &src, CfgVertexMap &vmap /*out*/);
314 
319 CfgConstEdgeSet findBackEdges(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &begin);
320 
324 CfgConstEdgeSet findCallEdges(const ControlFlowGraph::ConstVertexIterator &callSite);
325 
329 CfgConstVertexSet findCalledFunctions(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &callSite);
330 
340 CfgConstEdgeSet findCallReturnEdges(const Partitioner&, const ControlFlowGraph&);
341 CfgConstEdgeSet findCallReturnEdges(const ControlFlowGraph::ConstVertexIterator &callSite);
347 CfgConstVertexSet findFunctionReturns(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &beginVertex);
348 
360 Sawyer::Container::Map<Function::Ptr, CfgConstEdgeSet> findFunctionReturnEdges(const Partitioner&, const ControlFlowGraph&);
366 void eraseEdges(ControlFlowGraph&, const CfgConstEdgeSet &toErase);
367 
369 CfgConstVertexSet findIncidentVertices(const CfgConstEdgeSet&);
370 
377 CfgConstVertexSet findDetachedVertices(const ControlFlowGraph&);
378 CfgConstVertexSet findDetachedVertices(const CfgConstVertexSet &vertices);
385 CfgConstVertexSet forwardMapped(const CfgConstVertexSet&, const CfgVertexMap&);
386 
391 CfgConstVertexSet reverseMapped(const CfgConstVertexSet&, const CfgVertexMap&);
392 
399 void expandFunctionReturnEdges(const Partitioner&, ControlFlowGraph &cfg/*in,out*/);
400 
415 ControlFlowGraph functionCfgByErasure(const ControlFlowGraph &gcfg, const Function::Ptr &function,
416  ControlFlowGraph::VertexIterator &entry/*out*/);
417 
432 ControlFlowGraph functionCfgByReachability(const ControlFlowGraph &gcfg, const Function::Ptr &function,
433  const ControlFlowGraph::ConstVertexIterator &gcfgEntry);
434 
435 } // namespace
436 } // namespace
437 } // namespace
438 
439 #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.
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.
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.
VertexType
Partitioner control flow vertex types.
Definition: BasicTypes.h:39
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:75
rose_addr_t address() const
Property: starting 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.
std::set< ControlFlowGraph::ConstEdgeIterator > CfgConstEdgeSet
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.
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:131
Base class for reference counted objects.
Definition: SharedObject.h:64
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:317
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.