ROSE  0.10.12.0
ControlFlowGraph.h
1 #ifndef ROSE_Partitioner2_ControlFlowGraph_H
2 #define ROSE_Partitioner2_ControlFlowGraph_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include <Partitioner2/BasicBlock.h>
8 #include <Partitioner2/BasicTypes.h>
9 #include <Partitioner2/Function.h>
10 #include <Partitioner2/Utility.h>
11 
12 #include <Sawyer/BiMap.h>
13 #include <Sawyer/Graph.h>
14 #include <Sawyer/GraphIteratorSet.h>
15 #include <Sawyer/GraphIteratorBiMap.h>
16 #include <Sawyer/HashMap.h>
17 #include <Sawyer/Map.h>
18 #include <Sawyer/Optional.h>
19 
20 #include <boost/serialization/access.hpp>
21 #include <list>
22 #include <ostream>
23 
24 namespace Rose {
25 namespace BinaryAnalysis {
26 namespace Partitioner2 {
27 
29 class CfgVertex {
30  VertexType type_; // type of vertex, special or not
31  rose_addr_t startVa_; // address of start of basic block
32  BasicBlock::Ptr bblock_; // basic block, or null if only a place holder
33  FunctionSet owningFunctions_; // functions to which vertex belongs
34 
35 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
36 private:
37  friend class boost::serialization::access;
38 
39  template<class S>
40  void serialize(S &s, const unsigned /*version*/) {
41  s & BOOST_SERIALIZATION_NVP(type_);
42  s & BOOST_SERIALIZATION_NVP(startVa_);
43  s & BOOST_SERIALIZATION_NVP(bblock_);
44  s & BOOST_SERIALIZATION_NVP(owningFunctions_);
45  }
46 #endif
47 
48 public:
49  // intentionally undocumented. Necessary for serialization of Sawyer::Container::Graph
50  CfgVertex()
51  : type_(V_USER_DEFINED), startVa_(0) {}
52 
53 public:
55  explicit CfgVertex(rose_addr_t startVa): type_(V_BASIC_BLOCK), startVa_(startVa) {}
56 
58  explicit CfgVertex(const BasicBlock::Ptr &bb): type_(V_BASIC_BLOCK), bblock_(bb) {
59  ASSERT_not_null(bb);
60  startVa_ = bb->address();
61  }
62 
64  /*implicit*/ CfgVertex(VertexType type): type_(type), startVa_(0) {
65  ASSERT_forbid2(type==V_BASIC_BLOCK, "this constructor does not create basic block or placeholder vertices");
66  }
67 
69  VertexType type() const { return type_; }
70 
79  rose_addr_t address() const {
80  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
81  return startVa_;
82  }
83  void address(rose_addr_t va) {
84  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
85  startVa_ = va;
86  }
93 
99 
108 
115  const BasicBlock::Ptr& bblock() const {
116  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
117  return bblock_;
118  }
119  void bblock(const BasicBlock::Ptr &bb) {
120  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
121  bblock_ = bb;
122  }
128  bool insertOwningFunction(const Function::Ptr &function) {
129  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
130  ASSERT_not_null(function);
131  return owningFunctions_.insert(function);
132  }
133 
138  void eraseOwningFunction(const Function::Ptr &function) {
139  ASSERT_require(V_BASIC_BLOCK==type_ || V_USER_DEFINED==type_);
140  if (function != NULL)
141  owningFunctions_.erase(function);
142  }
143 
147  bool isOwningFunction(const Function::Ptr &function) const {
148  return owningFunctions_.exists(function);
149  }
150 
152  size_t nOwningFunctions() const {
153  return owningFunctions_.size();
154  }
155 
163  const FunctionSet& owningFunctions() const {
164  return owningFunctions_;
165  }
167  return owningFunctions_;
168  }
174  Function::Ptr isEntryBlock() const;
175 
179  void nullify() {
180  ASSERT_require(V_BASIC_BLOCK==type_);
181  bblock_ = BasicBlock::Ptr();
182  }
183 };
184 
186 class CfgEdge {
187 private:
188  EdgeType type_;
189  Confidence confidence_;
190 
191 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
192 private:
193  friend class boost::serialization::access;
194 
195  template<class S>
196  void serialize(S &s, const unsigned /*version*/) {
197  s & BOOST_SERIALIZATION_NVP(type_);
198  s & BOOST_SERIALIZATION_NVP(confidence_);
199  }
200 #endif
201 
202 public:
204  CfgEdge(): type_(E_NORMAL), confidence_(ASSUMED) {}
205 
207  /*implicit*/ CfgEdge(EdgeType type, Confidence confidence=ASSUMED): type_(type), confidence_(confidence) {}
208 
210  EdgeType type() const { return type_; }
211 
217  Confidence confidence() const { return confidence_; }
218  void confidence(Confidence c) { confidence_ = c; }
220 };
221 
224 
226 bool sortEdgesBySrc(const ControlFlowGraph::ConstEdgeIterator&, const ControlFlowGraph::ConstEdgeIterator&);
227 
229 bool sortEdgesByDst(const ControlFlowGraph::ConstEdgeIterator&, const ControlFlowGraph::ConstEdgeIterator&);
230 
232 bool sortEdgesById(const ControlFlowGraph::ConstEdgeIterator&, const ControlFlowGraph::ConstEdgeIterator&);
233 
235 bool sortVerticesByAddress(const ControlFlowGraph::ConstVertexIterator&, const ControlFlowGraph::ConstVertexIterator&);
236 
238 bool sortVerticesById(const ControlFlowGraph::ConstVertexIterator&, const ControlFlowGraph::ConstVertexIterator&);
239 
241 std::ostream& operator<<(std::ostream&, const ControlFlowGraph::Vertex&);
242 
244 std::ostream& operator<<(std::ostream&, const ControlFlowGraph::Edge&);
245 
248 
252 typedef std::list<ControlFlowGraph::VertexIterator> CfgVertexList;
253 typedef std::list<ControlFlowGraph::ConstVertexIterator> CfgConstVertexList;
259 typedef std::list<ControlFlowGraph::EdgeIterator> CfgEdgeList;
260 typedef std::list<ControlFlowGraph::ConstEdgeIterator> CfgConstEdgeList;
278 typedef Sawyer::Container::GraphIteratorBiMap<ControlFlowGraph::ConstVertexIterator,
279  ControlFlowGraph::ConstVertexIterator> CfgVertexMap;
280 
288 public:
291 
301  rose_addr_t startVa;
303  AttachedBasicBlock(Partitioner *partitioner, rose_addr_t startVa, const BasicBlock::Ptr &bblock)
304  : partitioner(partitioner), startVa(startVa), bblock(bblock) {}
305  };
306 
317  rose_addr_t startVa;
319  DetachedBasicBlock(Partitioner *partitioner, rose_addr_t startVa, const BasicBlock::Ptr &bblock)
320  : partitioner(partitioner), startVa(startVa), bblock(bblock) {}
321  };
322 
324  virtual bool operator()(bool chain, const AttachedBasicBlock&) = 0;
325 
327  virtual bool operator()(bool chain, const DetachedBasicBlock&) = 0;
328 };
329 
331 // Control flow graph utility functions
333 
338 void insertCfg(ControlFlowGraph &dst, const ControlFlowGraph &src, CfgVertexMap &vmap /*out*/);
339 
344 CfgConstEdgeSet findBackEdges(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &begin);
345 
349 CfgConstEdgeSet findCallEdges(const ControlFlowGraph::ConstVertexIterator &callSite);
350 
354 CfgConstVertexSet findCalledFunctions(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &callSite);
355 
365 CfgConstEdgeSet findCallReturnEdges(const Partitioner&, const ControlFlowGraph&);
366 CfgConstEdgeSet findCallReturnEdges(const ControlFlowGraph::ConstVertexIterator &callSite);
372 CfgConstVertexSet findFunctionReturns(const ControlFlowGraph &cfg, const ControlFlowGraph::ConstVertexIterator &beginVertex);
373 
385 Sawyer::Container::Map<Function::Ptr, CfgConstEdgeSet> findFunctionReturnEdges(const Partitioner&, const ControlFlowGraph&);
391 void eraseEdges(ControlFlowGraph&, const CfgConstEdgeSet &toErase);
392 
394 CfgConstVertexSet findIncidentVertices(const CfgConstEdgeSet&);
395 
402 CfgConstVertexSet findDetachedVertices(const ControlFlowGraph&);
403 CfgConstVertexSet findDetachedVertices(const CfgConstVertexSet &vertices);
410 CfgConstVertexSet forwardMapped(const CfgConstVertexSet&, const CfgVertexMap&);
411 
416 CfgConstVertexSet reverseMapped(const CfgConstVertexSet&, const CfgVertexMap&);
417 
424 void expandFunctionReturnEdges(const Partitioner&, ControlFlowGraph &cfg/*in,out*/);
425 
440 ControlFlowGraph functionCfgByErasure(const ControlFlowGraph &gcfg, const Function::Ptr &function,
441  ControlFlowGraph::VertexIterator &entry/*out*/);
442 
457 ControlFlowGraph functionCfgByReachability(const ControlFlowGraph &gcfg, const Function::Ptr &function,
458  const ControlFlowGraph::ConstVertexIterator &gcfgEntry);
459 
460 } // namespace
461 } // namespace
462 } // namespace
463 
464 #endif
465 #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:55
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:44
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:80
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:79
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:45
Normal control flow edge, nothing special.
Definition: BasicTypes.h:56
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:135
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:321
FunctionPtr Ptr
Shared-ownership pointer for function.
Definition: Function.h:52
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.