ROSE  0.9.13.69
Partitioner.h
1 #ifndef ROSE_Partitioner2_Partitioner_H
2 #define ROSE_Partitioner2_Partitioner_H
3 
4 #include <Partitioner2/AddressUsageMap.h>
5 #include <Partitioner2/BasicBlock.h>
6 #include <Partitioner2/BasicTypes.h>
7 #include <Partitioner2/Config.h>
8 #include <Partitioner2/ControlFlowGraph.h>
9 #include <Partitioner2/DataBlock.h>
10 #include <Partitioner2/Function.h>
11 #include <Partitioner2/FunctionCallGraph.h>
12 #include <Partitioner2/InstructionProvider.h>
13 #include <Partitioner2/Modules.h>
14 #include <Partitioner2/Reference.h>
15 
16 #include <Sawyer/Attribute.h>
17 #include <Sawyer/Callbacks.h>
18 #include <Sawyer/IntervalSet.h>
19 #include <Sawyer/Map.h>
20 #include <Sawyer/Message.h>
21 #include <Sawyer/Optional.h>
22 #include <Sawyer/ProgressBar.h>
23 #include <Sawyer/SharedPointer.h>
24 
25 #include <BinaryUnparser.h>
26 #include <Progress.h>
27 
28 #include <boost/filesystem.hpp>
29 #include <boost/move/utility_core.hpp>
30 #include <boost/serialization/access.hpp>
31 #include <boost/serialization/split_member.hpp>
32 
33 #include <ostream>
34 #include <set>
35 #include <string>
36 #include <vector>
37 
38 // Derived classes needed for serialization
39 #include <BinaryYicesSolver.h>
40 #include <BinaryZ3Solver.h>
41 #include <DispatcherM68k.h>
42 #include <DispatcherPowerpc.h>
43 #include <DispatcherX86.h>
44 
45 // Define ROSE_PARTITIONER_MOVE if boost::move works. Mainly this is to work around a GCC bug that reports this error:
46 //
47 // prototype for
48 // 'Rose::BinaryAnalysis::Partitioner2::Partitioner::Partitioner(boost::rv<Rose::BinaryAnalysis::Partitioner2::Partitioner>&)'
49 // does not match any in class 'Rose::BinaryAnalysis::Partitioner2::Partitioner'
50 //
51 // followed by saying that the exact same signature is one of the candidates:
52 //
53 // candidates are:
54 // Rose::BinaryAnalysis::Partitioner2::Partitioner::Partitioner(boost::rv<Rose::BinaryAnalysis::Partitioner2::Partitioner>&)
55 //
56 // This is apparently GCC issue 49377 [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49377] fixed in GCC-6.1.0.
57 #if __cplusplus >= 201103L
58  #define ROSE_PARTITIONER_MOVE
59 #elif defined(__GNUC__)
60  #if __GNUC__ > 5
61  #define ROSE_PARTITIONER_MOVE
62  #elif BOOST_VERSION >= 106900 // 1.68.0 might be okay too, but ROSE blacklists it for other reasons
63  #define ROSE_PARTITIONER_MOVE
64  #endif
65 #endif
66 
67 namespace Rose {
68 namespace BinaryAnalysis {
69 
101 namespace Partitioner2 {
102 
316 class ROSE_DLL_API Partitioner: public Sawyer::Attribute::Storage<> { // final
317 #ifdef ROSE_PARTITIONER_MOVE
318  BOOST_MOVABLE_BUT_NOT_COPYABLE(Partitioner)
319 #endif
320 
321 public:
324  typedef std::vector<FunctionPrologueMatcher::Ptr> FunctionPrologueMatchers;
325  typedef std::vector<FunctionPaddingMatcher::Ptr> FunctionPaddingMatchers;
328  struct Thunk {
330  rose_addr_t target;
331  Thunk(const BasicBlock::Ptr &bblock, rose_addr_t target): bblock(bblock), target(target) {}
332  };
333 
336 
337 private:
338  BasePartitionerSettings settings_; // settings adjustable from the command-line
339  Configuration config_; // configuration information about functions, blocks, etc.
340  InstructionProvider::Ptr instructionProvider_; // cache for all disassembled instructions
341  MemoryMap::Ptr memoryMap_; // description of memory, especially insns and non-writable
342  ControlFlowGraph cfg_; // basic blocks that will become part of the ROSE AST
343  CfgVertexIndex vertexIndex_; // Vertex-by-address index for the CFG
344  AddressUsageMap aum_; // How addresses are used for each address represented by the CFG
345  SmtSolverPtr solver_; // Satisfiable modulo theory solver used by semantic expressions
346  Functions functions_; // List of all attached functions by entry address
347  bool autoAddCallReturnEdges_; // Add E_CALL_RETURN edges when blocks are attached to CFG?
348  bool assumeFunctionsReturn_; // Assume that unproven functions return to caller?
349  size_t stackDeltaInterproceduralLimit_; // Max depth of call stack when computing stack deltas
350  AddressNameMap addressNames_; // Names for various addresses
351  SemanticMemoryParadigm semanticMemoryParadigm_; // Slow and precise, or fast and imprecise?
352  Unparser::BasePtr unparser_; // For unparsing things to pseudo-assembly
353  Unparser::BasePtr insnUnparser_; // For unparsing single instructions in diagnostics
354 
355  // Callback lists
356  CfgAdjustmentCallbacks cfgAdjustmentCallbacks_;
357  BasicBlockCallbacks basicBlockCallbacks_;
358  FunctionPrologueMatchers functionPrologueMatchers_;
359  FunctionPaddingMatchers functionPaddingMatchers_;
360 
361  // Special CFG vertices.
362  ControlFlowGraph::VertexIterator undiscoveredVertex_;
363  ControlFlowGraph::VertexIterator indeterminateVertex_;
364  ControlFlowGraph::VertexIterator nonexistingVertex_;
365  static const size_t nSpecialVertices = 3;
366 
367  // Protects the following data members
368  mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
369  Progress::Ptr progress_; // Progress reporter to update, or null
370  mutable size_t cfgProgressTotal_; // Expected total for the CFG progress bar; initialized at first report
371 
372 
375  //
376  // Serialization
377  //
380 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
381 private:
382  friend class boost::serialization::access;
383 
384  template<class S>
385  void serializeCommon(S &s, const unsigned /*version*/) {
386  s.template register_type<InstructionSemantics2::SymbolicSemantics::SValue>();
387  s.template register_type<InstructionSemantics2::SymbolicSemantics::RiscOperators>();
388  s.template register_type<InstructionSemantics2::DispatcherX86>();
389  s.template register_type<InstructionSemantics2::DispatcherM68k>();
390  s.template register_type<InstructionSemantics2::DispatcherPowerpc>();
391  s.template register_type<SymbolicExpr::Interior>();
392  s.template register_type<SymbolicExpr::Leaf>();
393  s.template register_type<YicesSolver>();
394  s.template register_type<Z3Solver>();
395  s.template register_type<Semantics::SValue>();
396  s.template register_type<Semantics::MemoryListState>();
397  s.template register_type<Semantics::MemoryMapState>();
398  s.template register_type<Semantics::RegisterState>();
399  s.template register_type<Semantics::State>();
400  s.template register_type<Semantics::RiscOperators>();
401  s & BOOST_SERIALIZATION_NVP(settings_);
402  // s & config_; -- FIXME[Robb P Matzke 2016-11-08]
403  s & BOOST_SERIALIZATION_NVP(instructionProvider_);
404  s & BOOST_SERIALIZATION_NVP(memoryMap_);
405  s & BOOST_SERIALIZATION_NVP(cfg_);
406  // s & vertexIndex_; -- initialized by rebuildVertexIndices
407  s & BOOST_SERIALIZATION_NVP(aum_);
408  // s & BOOST_SERIALIZATION_NVP(solver_); -- not saved/restored in order to override from command-line
409  s & BOOST_SERIALIZATION_NVP(functions_);
410  s & BOOST_SERIALIZATION_NVP(autoAddCallReturnEdges_);
411  s & BOOST_SERIALIZATION_NVP(assumeFunctionsReturn_);
412  s & BOOST_SERIALIZATION_NVP(stackDeltaInterproceduralLimit_);
413  s & BOOST_SERIALIZATION_NVP(addressNames_);
414  s & BOOST_SERIALIZATION_NVP(semanticMemoryParadigm_);
415  // s & unparser_; -- not saved; restored from disassembler
416  // s & cfgAdjustmentCallbacks_; -- not saved/restored
417  // s & basicBlockCallbacks_; -- not saved/restored
418  // s & functionPrologueMatchers_; -- not saved/restored
419  // s & functionPaddingMatchers_; -- not saved/restored
420  // s & undiscoveredVertex_; -- initialized by rebuildVertexIndices
421  // s & indeterminateVertex_; -- initialized by rebuildVertexIndices
422  // s & nonexistingVertex_; -- initialized by rebuildVertexIndices
423  // s & progress_; -- not saved/restored
424  // s & cfgProgressTotal_; -- not saved/restored
425  }
426 
427  template<class S>
428  void save(S &s, const unsigned version) const {
429  const_cast<Partitioner*>(this)->serializeCommon(s, version);
430  }
431 
432  template<class S>
433  void load(S &s, const unsigned version) {
434  serializeCommon(s, version);
435  rebuildVertexIndices();
436  }
437 
438  BOOST_SERIALIZATION_SPLIT_MEMBER();
439 #endif
440 
441 
444  //
445  // Constructors
446  //
449 public:
454  Partitioner();
455 
460  Partitioner(Disassembler *disassembler, const MemoryMap::Ptr &map);
461 
462 #ifdef ROSE_PARTITIONER_MOVE
463 
464  Partitioner(BOOST_RV_REF(Partitioner));
465 
467  Partitioner& operator=(BOOST_RV_REF(Partitioner));
468 #else
469  // These are unsafe
470  Partitioner(const Partitioner&);
471  Partitioner& operator=(const Partitioner&);
472 #endif
473 
474  ~Partitioner();
475 
482  bool isDefaultConstructed() const { return instructionProvider_ == NULL; }
483 
485  void clear() /*final*/;
486 
494  Configuration& configuration() { return config_; }
495  const Configuration& configuration() const { return config_; }
503  InstructionProvider& instructionProvider() /*final*/ { return *instructionProvider_; }
504  const InstructionProvider& instructionProvider() const /*final*/ { return *instructionProvider_; }
516  MemoryMap::Ptr memoryMap() const /*final*/ { return memoryMap_; }
522  bool addressIsExecutable(rose_addr_t va) const /*final*/ {
523  return memoryMap_!=NULL && memoryMap_->at(va).require(MemoryMap::EXECUTABLE).exists();
524  }
525 
528  //
529  // Unparsing
530  //
533 
543  Unparser::BasePtr unparser() const /*final*/;
544  void unparser(const Unparser::BasePtr&) /*final*/;
556  Unparser::BasePtr insnUnparser() const /*final*/;
557  void insnUnparser(const Unparser::BasePtr&) /*final*/;
561  void configureInsnUnparser(const Unparser::BasePtr&) const /*final*/;
562 
571  std::string unparse(SgAsmInstruction*) const;
572  void unparse(std::ostream&, SgAsmInstruction*) const;
573  void unparse(std::ostream&, const BasicBlock::Ptr&) const;
574  void unparse(std::ostream&, const DataBlock::Ptr&) const;
575  void unparse(std::ostream&, const Function::Ptr&) const;
576  void unparse(std::ostream&) const;
579  //
582  // Partitioner CFG queries
583  //
586 public:
592  size_t nBytes() const /*final*/ { return aum_.size(); }
593 
601  ControlFlowGraph::VertexIterator undiscoveredVertex() /*final*/ {
602  return undiscoveredVertex_;
603  }
604  ControlFlowGraph::ConstVertexIterator undiscoveredVertex() const /*final*/ {
605  return undiscoveredVertex_;
606  }
619  ControlFlowGraph::VertexIterator indeterminateVertex() /*final*/ {
620  return indeterminateVertex_;
621  }
622  ControlFlowGraph::ConstVertexIterator indeterminateVertex() const /*final*/ {
623  return indeterminateVertex_;
624  }
636  ControlFlowGraph::VertexIterator nonexistingVertex() /*final*/ {
637  return nonexistingVertex_;
638  }
639  ControlFlowGraph::ConstVertexIterator nonexistingVertex() const /*final*/ {
640  return nonexistingVertex_;
641  }
650  const ControlFlowGraph& cfg() const /*final*/ { return cfg_; }
651 
658  const AddressUsageMap& aum() const /*final*/ { return aum_; }
659 
661  AddressUsageMap aum(const Function::Ptr&) const /*final*/;
662 
667  std::vector<AddressUser> users(rose_addr_t) const /*final*/;
668 
676  std::set<rose_addr_t> ghostSuccessors() const /*final*/;
677 
706  bool isEdgeIntraProcedural(ControlFlowGraph::ConstEdgeIterator edge,
707  const Function::Ptr &function = Function::Ptr()) const /*final*/;
708  bool isEdgeIntraProcedural(const ControlFlowGraph::Edge &edge,
709  const Function::Ptr &function = Function::Ptr()) const /*final*/;
746  bool isEdgeInterProcedural(ControlFlowGraph::ConstEdgeIterator edge,
747  const Function::Ptr &sourceFunction = Function::Ptr(),
748  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
749  bool isEdgeInterProcedural(const ControlFlowGraph::Edge &edge,
750  const Function::Ptr &sourceFunction = Function::Ptr(),
751  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
756  //
759  // Partitioner instruction operations
760  //
763 public:
769  size_t nInstructions() const /*final*/;
770 
780  AddressUser instructionExists(rose_addr_t startVa) const /*final*/ {
781  return aum_.findInstruction(startVa);
782  }
784  return aum_.findInstruction(insn);
785  }
794  ControlFlowGraph::ConstVertexIterator instructionVertex(rose_addr_t insnVa) const;
795 
804  std::vector<SgAsmInstruction*> instructionsOverlapping(const AddressInterval&) const /*final*/;
805 
814  std::vector<SgAsmInstruction*> instructionsSpanning(const AddressInterval&) const /*final*/;
815 
825  std::vector<SgAsmInstruction*> instructionsContainedIn(const AddressInterval&) const /*final*/;
826 
834  AddressInterval instructionExtent(SgAsmInstruction*) const /*final*/;
835 
848  SgAsmInstruction* discoverInstruction(rose_addr_t startVa) const /*final*/;
849 
854  CrossReferences instructionCrossReferences(const AddressIntervalSet &restriction) const /*final*/;
855 
856 
859  //
860  // Partitioner basic block placeholder operations
861  //
864 public:
874  size_t nPlaceholders() const /*final*/;
875 
884  bool placeholderExists(rose_addr_t startVa) const /*final*/;
885 
896  ControlFlowGraph::VertexIterator findPlaceholder(rose_addr_t startVa) /*final*/ {
897  if (Sawyer::Optional<ControlFlowGraph::VertexIterator> found = vertexIndex_.getOptional(startVa))
898  return *found;
899  return cfg_.vertices().end();
900  }
901  ControlFlowGraph::ConstVertexIterator findPlaceholder(rose_addr_t startVa) const /*final*/ {
902  if (Sawyer::Optional<ControlFlowGraph::VertexIterator> found = vertexIndex_.getOptional(startVa))
903  return *found;
904  return cfg_.vertices().end();
905  }
924  ControlFlowGraph::VertexIterator insertPlaceholder(rose_addr_t startVa) /*final*/;
925 
939  BasicBlock::Ptr erasePlaceholder(const ControlFlowGraph::ConstVertexIterator &placeholder) /*final*/;
940  BasicBlock::Ptr erasePlaceholder(rose_addr_t startVa) /*final*/;
945  //
948  // Partitioner basic block operations
949  //
952 public:
966  bool basicBlockSemanticsAutoDrop() const /*final*/ { return settings_.basicBlockSemanticsAutoDrop; }
978  void basicBlockDropSemantics() const /*final*/;
979 
988  size_t nBasicBlocks() const /*final*/;
989 
997  std::vector<BasicBlock::Ptr> basicBlocks() const /*final*/;
998 
1020  BasicBlock::Ptr basicBlockExists(rose_addr_t startVa) const /*final*/;
1021  BasicBlock::Ptr basicBlockExists(const BasicBlock::Ptr&) const /*final*/;
1033  std::vector<BasicBlock::Ptr> basicBlocksOverlapping(const AddressInterval&) const /*final*/;
1034 
1045  std::vector<BasicBlock::Ptr> basicBlocksSpanning(const AddressInterval&) const /*final*/;
1046 
1056  std::vector<BasicBlock::Ptr> basicBlocksContainedIn(const AddressInterval&) const /*final*/;
1057 
1064  BasicBlock::Ptr basicBlockContainingInstruction(rose_addr_t insnVa) const /*final*/;
1065 
1075  AddressIntervalSet basicBlockInstructionExtent(const BasicBlock::Ptr&) const /*final*/;
1076 
1082  AddressIntervalSet basicBlockDataExtent(const BasicBlock::Ptr&) const /*final*/;
1083 
1108  BasicBlock::Ptr detachBasicBlock(rose_addr_t startVa) /*final*/;
1109  BasicBlock::Ptr detachBasicBlock(const BasicBlock::Ptr &basicBlock) /*final*/;
1110  BasicBlock::Ptr detachBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder) /*final*/;
1125  ControlFlowGraph::VertexIterator truncateBasicBlock(const ControlFlowGraph::ConstVertexIterator &basicBlock,
1126  SgAsmInstruction *insn) /*final*/;
1127 
1160  void attachBasicBlock(const BasicBlock::Ptr&) /*final*/;
1161  void attachBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder, const BasicBlock::Ptr&) /*final*/;
1249  BasicBlock::Ptr discoverBasicBlock(rose_addr_t startVa) const /*final*/;
1250  BasicBlock::Ptr discoverBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder) const /*final*/;
1267  BasicBlock::Successors basicBlockSuccessors(const BasicBlock::Ptr&,
1268  Precision::Level precision = Precision::HIGH) const /*final*/;
1269 
1278  std::vector<rose_addr_t> basicBlockConcreteSuccessors(const BasicBlock::Ptr&, bool *isComplete=NULL) const /*final*/;
1279 
1298  std::set<rose_addr_t> basicBlockGhostSuccessors(const BasicBlock::Ptr&) const /*final*/;
1299 
1309  bool basicBlockIsFunctionCall(const BasicBlock::Ptr&, Precision::Level precision = Precision::HIGH) const /*final*/;
1310 
1321  bool basicBlockIsFunctionReturn(const BasicBlock::Ptr&) const /*final*/;
1322 
1327  bool basicBlockPopsStack(const BasicBlock::Ptr&) const /*final*/;
1328 
1368  BaseSemantics::SValuePtr basicBlockStackDeltaIn(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1369  BaseSemantics::SValuePtr basicBlockStackDeltaOut(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1380  void forgetStackDeltas() const /*final*/;
1381  void forgetStackDeltas(const Function::Ptr&) const /*final*/;
1393  size_t stackDeltaInterproceduralLimit() const /*final*/ { return stackDeltaInterproceduralLimit_; }
1394  void stackDeltaInterproceduralLimit(size_t n) /*final*/ { stackDeltaInterproceduralLimit_ = std::max(size_t(1), n); }
1447  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const BasicBlock::Ptr&) const /*final*/;
1448 
1449  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
1458  void basicBlockMayReturnReset() const /*final*/;
1459 
1460 private:
1461  // Per-vertex data used during may-return analysis
1462  struct MayReturnVertexInfo {
1463  enum State {INIT, CALCULATING, FINISHED};
1464  State state; // current state of vertex
1465  bool processedCallees; // have we processed BBs this vertex calls?
1466  boost::logic::tribool anyCalleesReturn; // do any of those called BBs have a true may-return value?
1467  boost::logic::tribool result; // final result (eventually cached in BB)
1468  MayReturnVertexInfo(): state(INIT), processedCallees(false), anyCalleesReturn(false), result(boost::indeterminate) {}
1469  };
1470 
1471  // Is edge significant for analysis? See .C file for full documentation.
1472  bool mayReturnIsSignificantEdge(const ControlFlowGraph::ConstEdgeIterator &edge,
1473  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1474 
1475  // Determine (and cache in vertexInfo) whether any callees return.
1476  boost::logic::tribool mayReturnDoesCalleeReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1477  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1478 
1479  // Maximum may-return result from significant successors including phantom call-return edge.
1480  boost::logic::tribool mayReturnDoesSuccessorReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1481  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1482 
1483  // The guts of the may-return analysis
1484  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator &start,
1485  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1486 
1487 
1488 
1491  //
1492  // Partitioner data block operations
1493  //
1496 public:
1502  size_t nDataBlocks() const /*final*/;
1503 
1510  DataBlock::Ptr dataBlockExists(const DataBlock::Ptr&) const /*final*/;
1511 
1520  DataBlock::Ptr findBestDataBlock(const AddressInterval&) const /*final*/;
1521 
1533  DataBlock::Ptr attachDataBlock(const DataBlock::Ptr&) /*final*/;
1534 
1541  void detachDataBlock(const DataBlock::Ptr&) /*final*/;
1542 
1552  DataBlock::Ptr attachDataBlockToFunction(const DataBlock::Ptr&, const Function::Ptr&) /*final*/;
1553 
1566  DataBlock::Ptr attachDataBlockToBasicBlock(const DataBlock::Ptr&, const BasicBlock::Ptr&) /*final*/;
1567 
1577  std::vector<DataBlock::Ptr> dataBlocksOverlapping(const AddressInterval&) const /*final*/;
1578 
1588  std::vector<DataBlock::Ptr> dataBlocksSpanning(const AddressInterval&) const /*final*/;
1589 
1599  std::vector<DataBlock::Ptr> dataBlocksContainedIn(const AddressInterval&) const /*final*/;
1600 
1607  AddressInterval dataBlockExtent(const DataBlock::Ptr&) const /*final*/;
1608 
1614  std::vector<DataBlock::Ptr> dataBlocks() const /*final*/;
1615 
1616 
1617 
1620  //
1621  // Partitioner function operations
1622  //
1625 public:
1631  size_t nFunctions() const /*final*/ { return functions_.size(); }
1632 
1651  Function::Ptr functionExists(rose_addr_t entryVa) const /*final*/;
1652  Function::Ptr functionExists(const BasicBlock::Ptr &entryBlock) const /*final*/;
1653  Function::Ptr functionExists(const Function::Ptr &function) const /*final*/;
1661  std::vector<Function::Ptr> functions() const /*final*/;
1662 
1672  std::vector<Function::Ptr> functionsOverlapping(const AddressInterval&) const /*final*/;
1673 
1684  std::vector<Function::Ptr> functionsSpanning(const AddressInterval&) const /*final*/;
1685 
1695  std::vector<Function::Ptr> functionsContainedIn(const AddressInterval&) const /*final*/;
1696 
1713  AddressIntervalSet functionExtent(const Function::Ptr&) const /*final*/;
1714  void functionExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1715  AddressIntervalSet functionBasicBlockExtent(const Function::Ptr &function) const /*final*/;
1716  void functionBasicBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1717  AddressIntervalSet functionDataBlockExtent(const Function::Ptr &function) const /*final*/;
1718  void functionDataBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1740  size_t attachFunction(const Function::Ptr&) /*final*/;
1741  size_t attachFunctions(const Functions&) /*final*/;
1757  Function::Ptr attachOrMergeFunction(const Function::Ptr&) /*final*/;
1758 
1775  size_t attachFunctionBasicBlocks(const Functions&) /*final*/;
1776  size_t attachFunctionBasicBlocks(const Function::Ptr&) /*final*/;
1792  void detachFunction(const Function::Ptr&) /*final*/;
1793 
1828  std::vector<Function::Ptr>
1829  functionsOwningBasicBlock(const ControlFlowGraph::Vertex&, bool doSort = true) const /*final*/;
1830 
1831  std::vector<Function::Ptr>
1832  functionsOwningBasicBlock(const ControlFlowGraph::ConstVertexIterator&, bool doSort = true) const /*final*/;
1833 
1834  std::vector<Function::Ptr>
1835  functionsOwningBasicBlock(rose_addr_t bblockVa, bool doSort = true) const /*final*/;
1836 
1837  std::vector<Function::Ptr>
1838  functionsOwningBasicBlock(const BasicBlock::Ptr&, bool doSort = true) const /*final*/;
1839 
1840  template<class Container> // container can hold any type accepted by functionsOwningBasicBlock
1841  std::vector<Function::Ptr>
1842  functionsOwningBasicBlocks(const Container &bblocks) const /*final*/ {
1843  std::vector<Function::Ptr> retval;
1844  BOOST_FOREACH (const typename Container::value_type& bblock, bblocks) {
1845  BOOST_FOREACH (const Function::Ptr &function, functionsOwningBasicBlock(bblock, false))
1846  insertUnique(retval, function, sortFunctionsByAddress);
1847  }
1848  return retval;
1849  }
1861  std::vector<Function::Ptr> discoverCalledFunctions() const /*final*/;
1862 
1874  std::vector<Function::Ptr> discoverFunctionEntryVertices() const /*final*/;
1875 
1885  Sawyer::Optional<Thunk> functionIsThunk(const Function::Ptr&) const /*final*/;
1886 
1897  void discoverFunctionBasicBlocks(const Function::Ptr &function) const /*final*/;
1898 
1905  std::set<rose_addr_t> functionGhostSuccessors(const Function::Ptr&) const /*final*/;
1906 
1914  FunctionCallGraph functionCallGraph(AllowParallelEdges::Type allowParallelEdges) const /*final*/;
1915 
1935  BaseSemantics::SValuePtr functionStackDelta(const Function::Ptr &function) const /*final*/;
1936 
1940  void allFunctionStackDelta() const /*final*/;
1941 
1948  Sawyer::Optional<bool> functionOptionalMayReturn(const Function::Ptr &function) const /*final*/;
1949 
1953  void allFunctionMayReturn() const /*final*/;
1954 
1981  const CallingConvention::Analysis&
1982  functionCallingConvention(const Function::Ptr&,
1983  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1984  const /*final*/;
1985 
1998  void
1999  allFunctionCallingConvention(const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
2000  const /*final*/;
2001 
2027  CallingConvention::Dictionary
2028  functionCallingConventionDefinitions(const Function::Ptr&,
2029  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
2030  const /*final*/;
2031 
2044  void
2045  allFunctionCallingConventionDefinition(const CallingConvention::Definition::Ptr &dflt =
2046  CallingConvention::Definition::Ptr()) const /*final*/;
2047 
2056  void fixInterFunctionEdges() /*final*/;
2057  void fixInterFunctionEdge(const ControlFlowGraph::ConstEdgeIterator&) /*final*/;
2077  bool functionIsNoop(const Function::Ptr&) const /*final*/;
2078 
2084  void allFunctionIsNoop() const /*final*/;
2085 
2093  void forgetFunctionIsNoop() const /*final*/;
2094  void forgetFunctionIsNoop(const Function::Ptr&) const /*final*/;
2101  std::set<rose_addr_t> functionDataFlowConstants(const Function::Ptr&) const /*final*/;
2102 
2103 
2104 
2107  //
2108  // Callbacks
2109  //
2112 public:
2130  CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() /*final*/ { return cfgAdjustmentCallbacks_; }
2131  const CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() const /*final*/ { return cfgAdjustmentCallbacks_; }
2142  BasicBlockCallbacks& basicBlockCallbacks() /*final*/ { return basicBlockCallbacks_; }
2143  const BasicBlockCallbacks& basicBlockCallbacks() const /*final*/ { return basicBlockCallbacks_; }
2146 public:
2154  FunctionPrologueMatchers& functionPrologueMatchers() /*final*/ { return functionPrologueMatchers_; }
2155  const FunctionPrologueMatchers& functionPrologueMatchers() const /*final*/ { return functionPrologueMatchers_; }
2177  std::vector<Function::Ptr> nextFunctionPrologue(rose_addr_t startVa) /*final*/;
2178 
2179 public:
2185  FunctionPaddingMatchers& functionPaddingMatchers() /*final*/ { return functionPaddingMatchers_; }
2186  const FunctionPaddingMatchers& functionPaddingMatchers() const /*final*/ { return functionPaddingMatchers_; }
2196  DataBlock::Ptr matchFunctionPadding(const Function::Ptr&) /*final*/;
2197 
2198 
2199 
2202  //
2203  // Partitioner miscellaneous
2204  //
2207 public:
2219  void dumpCfg(std::ostream&, const std::string &prefix="", bool showBlocks=true,
2220  bool computeProperties=true) const /*final*/;
2221 
2235  void cfgGraphViz(std::ostream&, const AddressInterval &restrict = AddressInterval::whole(),
2236  bool showNeighbors=true) const /*final*/;
2237 
2243  static std::string vertexName(const ControlFlowGraph::Vertex&) /*final*/;
2244  std::string vertexName(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
2250  static std::string vertexNameEnd(const ControlFlowGraph::Vertex&) /*final*/;
2251 
2257  static std::string edgeNameSrc(const ControlFlowGraph::Edge&) /*final*/;
2258  std::string edgeNameSrc(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2266  static std::string edgeNameDst(const ControlFlowGraph::Edge&) /*final*/;
2267  std::string edgeNameDst(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2275  static std::string edgeName(const ControlFlowGraph::Edge&) /*final*/;
2276  std::string edgeName(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2282  static std::string basicBlockName(const BasicBlock::Ptr&) /*final*/;
2283 
2287  static std::string dataBlockName(const DataBlock::Ptr&) /*final*/;
2288 
2292  static std::string functionName(const Function::Ptr&) /*final*/;
2293 
2311  Progress::Ptr progress() const /*final*/;
2312  void progress(const Progress::Ptr&) /*final*/;
2319  void updateProgress(const std::string &phase, double completion) const;
2320 
2322  void showStatistics() const;
2323 
2324  // Checks consistency of internal data structures when debugging is enable (when NDEBUG is not defined).
2325  void checkConsistency() const;
2326 
2328  // Settings
2330 public:
2331 
2339  const BasePartitionerSettings& settings() const /*final*/ { return settings_; }
2340  void settings(const BasePartitionerSettings &s) /*final*/ { settings_ = s; }
2351  void enableSymbolicSemantics(bool b=true) /*final*/ { settings_.usingSemantics = b; }
2352  void disableSymbolicSemantics() /*final*/ { settings_.usingSemantics = false; }
2353  bool usingSymbolicSemantics() const /*final*/ { return settings_.usingSemantics; }
2377  void autoAddCallReturnEdges(bool b) /*final*/ { autoAddCallReturnEdges_ = b; }
2378  bool autoAddCallReturnEdges() const /*final*/ { return autoAddCallReturnEdges_; }
2395  void assumeFunctionsReturn(bool b) /*final*/ { assumeFunctionsReturn_ = b; }
2396  bool assumeFunctionsReturn() const /*final*/ { return assumeFunctionsReturn_; }
2408  void addressName(rose_addr_t, const std::string&) /*final*/;
2409  const std::string& addressName(rose_addr_t va) const /*final*/ { return addressNames_.getOrDefault(va); }
2410  const AddressNameMap& addressNames() const /*final*/ { return addressNames_; }
2420  bool checkingCallBranch() const /*final*/ { return settings_.checkingCallBranch; }
2421  void checkingCallBranch(bool b) /*final*/ { settings_.checkingCallBranch = b; }
2425  //
2428  // Instruction semantics
2429  //
2432 public:
2442  SemanticMemoryParadigm semanticMemoryParadigm() const { return semanticMemoryParadigm_; }
2443  void semanticMemoryParadigm(SemanticMemoryParadigm p) { semanticMemoryParadigm_ = p; }
2452  SmtSolverPtr smtSolver() const /*final*/ { return solver_; }
2453 
2464  BaseSemantics::RiscOperatorsPtr newOperators() const /*final*/;
2465  BaseSemantics::RiscOperatorsPtr newOperators(SemanticMemoryParadigm) const /*final*/;
2475  BaseSemantics::DispatcherPtr newDispatcher(const BaseSemantics::RiscOperatorsPtr&) const /*final*/;
2476 
2477 
2478 
2481  //
2482  // Python API support functions
2483  //
2486 #ifdef ROSE_ENABLE_PYTHON_API
2487  void pythonUnparse() const;
2488 #endif
2489 
2490 
2491 
2494  //
2495  // Partitioner internal utilities
2496  //
2499 private:
2500  void init(Disassembler*, const MemoryMap::Ptr&);
2501  void init(const Partitioner&);
2502  void updateCfgProgress();
2503 
2504 private:
2505  // Convert a CFG vertex iterator from one partitioner to another. This is called during copy construction when the source
2506  // and destination CFGs are identical.
2507  ControlFlowGraph::VertexIterator convertFrom(const Partitioner &other,
2508  ControlFlowGraph::ConstVertexIterator otherIter);
2509 
2510  // Adjusts edges for a placeholder vertex. This method erases all outgoing edges for the specified placeholder vertex and
2511  // then inserts a single edge from the placeholder to the special "undiscovered" vertex. */
2512  ControlFlowGraph::EdgeIterator adjustPlaceholderEdges(const ControlFlowGraph::VertexIterator &placeholder);
2513 
2514  // Adjusts edges for a non-existing basic block. This method erases all outgoing edges for the specified vertex and
2515  // then inserts a single edge from the vertex to the special "non-existing" vertex. */
2516  ControlFlowGraph::EdgeIterator adjustNonexistingEdges(const ControlFlowGraph::VertexIterator &vertex);
2517 
2518  // Implementation for the discoverBasicBlock methods. The startVa must not be the address of an existing placeholder.
2519  BasicBlock::Ptr discoverBasicBlockInternal(rose_addr_t startVa) const;
2520 
2521  // This method is called whenever a new placeholder is inserted into the CFG or a new basic block is attached to the
2522  // CFG/AUM. The call happens immediately after the CFG/AUM are updated.
2523  void bblockAttached(const ControlFlowGraph::VertexIterator &newVertex);
2524 
2525  // This method is called whenever a basic block is detached from the CFG/AUM or when a placeholder is erased from the CFG.
2526  // The call happens immediately after the CFG/AUM are updated.
2527  void bblockDetached(rose_addr_t startVa, const BasicBlock::Ptr &removedBlock);
2528 
2529  // Rebuild the vertexIndex_ and other cache-like data members from the control flow graph
2530  void rebuildVertexIndices();
2531 };
2532 
2533 } // namespace
2534 } // namespace
2535 } // namespace
2536 
2537 #endif
Represents information about a thunk.
Definition: Partitioner.h:328
size_t size() const
Number of addresses represented by the map.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Sawyer::Callbacks< BasicBlockCallback::Ptr > BasicBlockCallbacks
See basicBlockCallbacks.
Definition: Partitioner.h:323
const FunctionPrologueMatchers & functionPrologueMatchers() const
Ordered list of function prologue matchers.
Definition: Partitioner.h:2155
SmtSolverPtr smtSolver() const
SMT solver.
Definition: Partitioner.h:2452
BasicBlock::Ptr bblock
The one and only basic block for the thunk.
Definition: Partitioner.h:329
FunctionPrologueMatchers & functionPrologueMatchers()
Ordered list of function prologue matchers.
Definition: Partitioner.h:2154
const std::string & addressName(rose_addr_t va) const
Property: Name for address.
Definition: Partitioner.h:2409
void disableSymbolicSemantics()
Use or not use symbolic semantics.
Definition: Partitioner.h:2352
void checkingCallBranch(bool b)
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2421
ControlFlowGraph::ConstVertexIterator indeterminateVertex() const
Returns the special "indeterminate" vertex.
Definition: Partitioner.h:622
const Configuration & configuration() const
Configuration information.
Definition: Partitioner.h:495
Base class for machine instructions.
const FunctionPaddingMatchers & functionPaddingMatchers() const
Ordered list of function padding matchers.
Definition: Partitioner.h:2186
AddressUser instructionExists(SgAsmInstruction *insn) const
Determines whether an instruction is attached to the CFG/AUM.
Definition: Partitioner.h:783
const ControlFlowGraph & cfg() const
Returns the control flow graph.
Definition: Partitioner.h:650
Provides and caches instructions.
Settings that directly control a partitioner.
Definition: BasicTypes.h:277
STL namespace.
Holds a value or nothing.
Definition: Optional.h:49
ControlFlowGraph::VertexIterator indeterminateVertex()
Returns the special "indeterminate" vertex.
Definition: Partitioner.h:619
InstructionProvider & instructionProvider()
Returns the instruction provider.
Definition: Partitioner.h:503
boost::iterator_range< VertexIterator > vertices()
Iterators for all vertices.
Definition: Graph.h:1442
Main namespace for the ROSE library.
Sawyer::Container::Map< rose_addr_t, std::string > AddressNameMap
Map address to name.
Definition: Partitioner.h:335
BasicBlockCallbacks & basicBlockCallbacks()
Callbacks for adjusting basic block during discovery.
Definition: Partitioner.h:2142
ControlFlowGraph::VertexIterator undiscoveredVertex()
Returns the special "undiscovered" vertex.
Definition: Partitioner.h:601
SemanticMemoryParadigm semanticMemoryParadigm() const
Property: Whether to use map- or list-based memory states.
Definition: Partitioner.h:2442
Name space for the entire library.
Configuration & configuration()
Configuration information.
Definition: Partitioner.h:494
bool assumeFunctionsReturn() const
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2396
ControlFlowGraph::ConstVertexIterator findPlaceholder(rose_addr_t startVa) const
Find the CFG vertex for a basic block placeholder.
Definition: Partitioner.h:901
const AddressUsageMap & aum() const
Returns the address usage map.
Definition: Partitioner.h:658
const AddressNameMap & addressNames() const
Property: Name for address.
Definition: Partitioner.h:2410
Optional< Value > getOptional(const Key &key) const
Lookup and return a value or nothing.
Definition: HashMap.h:434
void settings(const BasePartitionerSettings &s)
Partitioner settings.
Definition: Partitioner.h:2340
void assumeFunctionsReturn(bool b)
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2395
rose_addr_t target
The one and only successor for the basic block.
Definition: Partitioner.h:330
ControlFlowGraph::VertexIterator nonexistingVertex()
Returns the special "non-existing" vertex.
Definition: Partitioner.h:636
bool isDefaultConstructed() const
Return true if this is a default constructed partitioner.
Definition: Partitioner.h:482
bool usingSymbolicSemantics() const
Use or not use symbolic semantics.
Definition: Partitioner.h:2353
SemanticMemoryParadigm
Organization of semantic memory.
Definition: BasicTypes.h:80
const BasicBlockCallbacks & basicBlockCallbacks() const
Callbacks for adjusting basic block during discovery.
Definition: Partitioner.h:2143
void basicBlockSemanticsAutoDrop(bool b)
Property: Automatically drop semantics for attached basic blocks.
Definition: Partitioner.h:967
const Value & getOrDefault(const Key &key) const
Lookup and return a value or a default.
Definition: Sawyer/Map.h:577
void stackDeltaInterproceduralLimit(size_t n)
Property: max depth for inter-procedural stack delta analysis.
Definition: Partitioner.h:1394
const InstructionProvider & instructionProvider() const
Returns the instruction provider.
Definition: Partitioner.h:504
bool checkingCallBranch
Check for situations where CALL is used as a branch.
Definition: BasicTypes.h:282
bool usingSemantics
Whether instruction semantics are used.
Definition: BasicTypes.h:278
A general, thread-safe way to report progress made on some task.
Definition: Progress.h:165
void autoAddCallReturnEdges(bool b)
Property: Insert (or not) function call return edges.
Definition: Partitioner.h:2377
AddressUser findInstruction(SgAsmInstruction *) const
Find an AddressUser record for the specified instruction, or equivalent.
bool basicBlockSemanticsAutoDrop() const
Property: Automatically drop semantics for attached basic blocks.
Definition: Partitioner.h:966
bool basicBlockSemanticsAutoDrop
Conserve memory by dropping semantics for attached basic blocks.
Definition: BasicTypes.h:283
ControlFlowGraph::ConstVertexIterator undiscoveredVertex() const
Returns the special "undiscovered" vertex.
Definition: Partitioner.h:604
ControlFlowGraph::ConstVertexIterator nonexistingVertex() const
Returns the special "non-existing" vertex.
Definition: Partitioner.h:639
std::vector< FunctionPrologueMatcher::Ptr > FunctionPrologueMatchers
See functionPrologueMatchers.
Definition: Partitioner.h:324
FunctionPaddingMatchers & functionPaddingMatchers()
Ordered list of function padding matchers.
Definition: Partitioner.h:2185
const CfgAdjustmentCallbacks & cfgAdjustmentCallbacks() const
List of all callbacks invoked when the CFG is adjusted.
Definition: Partitioner.h:2131
API and storage for attributes.
Definition: Attribute.h:208
MemoryMap::Ptr memoryMap() const
Returns the memory map.
Definition: Partitioner.h:516
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:316
bool checkingCallBranch() const
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2420
bool addressIsExecutable(rose_addr_t va) const
Returns true if address is executable.
Definition: Partitioner.h:522
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:48
size_t size() const
Number of nodes, keys, or values in this container.
Definition: Sawyer/Map.h:386
void enableSymbolicSemantics(bool b=true)
Use or not use symbolic semantics.
Definition: Partitioner.h:2351
bool autoAddCallReturnEdges() const
Property: Insert (or not) function call return edges.
Definition: Partitioner.h:2378
Sawyer::Callbacks< CfgAdjustmentCallback::Ptr > CfgAdjustmentCallbacks
See cfgAdjustmentCallbacks.
Definition: Partitioner.h:322
std::vector< FunctionPaddingMatcher::Ptr > FunctionPaddingMatchers
See functionPaddingMatchers.
Definition: Partitioner.h:325
void semanticMemoryParadigm(SemanticMemoryParadigm p)
Property: Whether to use map- or list-based memory states.
Definition: Partitioner.h:2443
Holds configuration information.
Definition: Config.h:279