ROSE  0.10.5.0
Partitioner.h
1 #ifndef ROSE_Partitioner2_Partitioner_H
2 #define ROSE_Partitioner2_Partitioner_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include <Partitioner2/AddressUsageMap.h>
8 #include <Partitioner2/BasicBlock.h>
9 #include <Partitioner2/BasicTypes.h>
10 #include <Partitioner2/Config.h>
11 #include <Partitioner2/ControlFlowGraph.h>
12 #include <Partitioner2/DataBlock.h>
13 #include <Partitioner2/Function.h>
14 #include <Partitioner2/FunctionCallGraph.h>
15 #include <Partitioner2/InstructionProvider.h>
16 #include <Partitioner2/Modules.h>
17 #include <Partitioner2/Reference.h>
18 
19 #include <Sawyer/Attribute.h>
20 #include <Sawyer/Callbacks.h>
21 #include <Sawyer/IntervalSet.h>
22 #include <Sawyer/Map.h>
23 #include <Sawyer/Message.h>
24 #include <Sawyer/Optional.h>
25 #include <Sawyer/ProgressBar.h>
26 #include <Sawyer/SharedPointer.h>
27 
28 #include <BinarySourceLocations.h>
29 #include <BinaryUnparser.h>
30 #include <Progress.h>
31 
32 #include <boost/filesystem.hpp>
33 #include <boost/move/utility_core.hpp>
34 #include <boost/serialization/access.hpp>
35 #include <boost/serialization/split_member.hpp>
36 #include <boost/serialization/version.hpp>
37 
38 #include <ostream>
39 #include <set>
40 #include <string>
41 #include <vector>
42 
43 // Derived classes needed for serialization
44 #include <BinaryYicesSolver.h>
45 #include <BinaryZ3Solver.h>
46 #include <DispatcherM68k.h>
47 #include <DispatcherPowerpc.h>
48 #include <DispatcherX86.h>
49 
50 // Define ROSE_PARTITIONER_MOVE if boost::move works. Mainly this is to work around a GCC bug that reports this error:
51 //
52 // prototype for
53 // 'Rose::BinaryAnalysis::Partitioner2::Partitioner::Partitioner(boost::rv<Rose::BinaryAnalysis::Partitioner2::Partitioner>&)'
54 // does not match any in class 'Rose::BinaryAnalysis::Partitioner2::Partitioner'
55 //
56 // followed by saying that the exact same signature is one of the candidates:
57 //
58 // candidates are:
59 // Rose::BinaryAnalysis::Partitioner2::Partitioner::Partitioner(boost::rv<Rose::BinaryAnalysis::Partitioner2::Partitioner>&)
60 //
61 // This is apparently GCC issue 49377 [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49377] fixed in GCC-6.1.0.
62 #if __cplusplus >= 201103L
63  #define ROSE_PARTITIONER_MOVE
64 #elif defined(__GNUC__)
65  #if __GNUC__ > 5
66  #define ROSE_PARTITIONER_MOVE
67  #elif BOOST_VERSION >= 106900 // 1.68.0 might be okay too, but ROSE blacklists it for other reasons
68  #define ROSE_PARTITIONER_MOVE
69  #endif
70 #endif
71 
72 namespace Rose {
73 namespace BinaryAnalysis {
74 
106 namespace Partitioner2 {
107 
321 class ROSE_DLL_API Partitioner: public Sawyer::Attribute::Storage<> { // final
322 #ifdef ROSE_PARTITIONER_MOVE
323  BOOST_MOVABLE_BUT_NOT_COPYABLE(Partitioner)
324 #endif
325 
326 public:
329  typedef std::vector<FunctionPrologueMatcher::Ptr> FunctionPrologueMatchers;
330  typedef std::vector<FunctionPaddingMatcher::Ptr> FunctionPaddingMatchers;
333  struct Thunk {
335  rose_addr_t target;
336  Thunk(const BasicBlock::Ptr &bblock, rose_addr_t target): bblock(bblock), target(target) {}
337  };
338 
341 
342 private:
343  BasePartitionerSettings settings_; // settings adjustable from the command-line
344  Configuration config_; // configuration information about functions, blocks, etc.
345  InstructionProvider::Ptr instructionProvider_; // cache for all disassembled instructions
346  MemoryMap::Ptr memoryMap_; // description of memory, especially insns and non-writable
347  ControlFlowGraph cfg_; // basic blocks that will become part of the ROSE AST
348  CfgVertexIndex vertexIndex_; // Vertex-by-address index for the CFG
349  AddressUsageMap aum_; // How addresses are used for each address represented by the CFG
350  SmtSolverPtr solver_; // Satisfiable modulo theory solver used by semantic expressions
351  Functions functions_; // List of all attached functions by entry address
352  bool autoAddCallReturnEdges_; // Add E_CALL_RETURN edges when blocks are attached to CFG?
353  bool assumeFunctionsReturn_; // Assume that unproven functions return to caller?
354  size_t stackDeltaInterproceduralLimit_; // Max depth of call stack when computing stack deltas
355  AddressNameMap addressNames_; // Names for various addresses
356  SourceLocations sourceLocations_; // Mapping between source locations and addresses
357  SemanticMemoryParadigm semanticMemoryParadigm_; // Slow and precise, or fast and imprecise?
358  Unparser::BasePtr unparser_; // For unparsing things to pseudo-assembly
359  Unparser::BasePtr insnUnparser_; // For unparsing single instructions in diagnostics
360 
361  // Callback lists
362  CfgAdjustmentCallbacks cfgAdjustmentCallbacks_;
363  BasicBlockCallbacks basicBlockCallbacks_;
364  FunctionPrologueMatchers functionPrologueMatchers_;
365  FunctionPaddingMatchers functionPaddingMatchers_;
366 
367  // Special CFG vertices.
368  ControlFlowGraph::VertexIterator undiscoveredVertex_;
369  ControlFlowGraph::VertexIterator indeterminateVertex_;
370  ControlFlowGraph::VertexIterator nonexistingVertex_;
371  static const size_t nSpecialVertices = 3;
372 
373  // Protects the following data members
374  mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
375  Progress::Ptr progress_; // Progress reporter to update, or null
376  mutable size_t cfgProgressTotal_; // Expected total for the CFG progress bar; initialized at first report
377 
378 
381  //
382  // Serialization
383  //
386 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
387 private:
388  friend class boost::serialization::access;
389 
390  template<class S>
391  void serializeCommon(S &s, const unsigned version) {
392  s.template register_type<InstructionSemantics2::SymbolicSemantics::SValue>();
393  s.template register_type<InstructionSemantics2::SymbolicSemantics::RiscOperators>();
394  s.template register_type<InstructionSemantics2::DispatcherX86>();
395  s.template register_type<InstructionSemantics2::DispatcherM68k>();
396  s.template register_type<InstructionSemantics2::DispatcherPowerpc>();
397  s.template register_type<SymbolicExpr::Interior>();
398  s.template register_type<SymbolicExpr::Leaf>();
399  s.template register_type<YicesSolver>();
400  s.template register_type<Z3Solver>();
401  s.template register_type<Semantics::SValue>();
402  s.template register_type<Semantics::MemoryListState>();
403  s.template register_type<Semantics::MemoryMapState>();
404  s.template register_type<Semantics::RegisterState>();
405  s.template register_type<Semantics::State>();
406  s.template register_type<Semantics::RiscOperators>();
407  s & BOOST_SERIALIZATION_NVP(settings_);
408  // s & config_; -- FIXME[Robb P Matzke 2016-11-08]
409  s & BOOST_SERIALIZATION_NVP(instructionProvider_);
410  s & BOOST_SERIALIZATION_NVP(memoryMap_);
411  s & BOOST_SERIALIZATION_NVP(cfg_);
412  // s & vertexIndex_; -- initialized by rebuildVertexIndices
413  s & BOOST_SERIALIZATION_NVP(aum_);
414  // s & BOOST_SERIALIZATION_NVP(solver_); -- not saved/restored in order to override from command-line
415  s & BOOST_SERIALIZATION_NVP(functions_);
416  s & BOOST_SERIALIZATION_NVP(autoAddCallReturnEdges_);
417  s & BOOST_SERIALIZATION_NVP(assumeFunctionsReturn_);
418  s & BOOST_SERIALIZATION_NVP(stackDeltaInterproceduralLimit_);
419  s & BOOST_SERIALIZATION_NVP(addressNames_);
420  if (version >= 1)
421  s & BOOST_SERIALIZATION_NVP(sourceLocations_);
422  s & BOOST_SERIALIZATION_NVP(semanticMemoryParadigm_);
423  // s & unparser_; -- not saved; restored from disassembler
424  // s & cfgAdjustmentCallbacks_; -- not saved/restored
425  // s & basicBlockCallbacks_; -- not saved/restored
426  // s & functionPrologueMatchers_; -- not saved/restored
427  // s & functionPaddingMatchers_; -- not saved/restored
428  // s & undiscoveredVertex_; -- initialized by rebuildVertexIndices
429  // s & indeterminateVertex_; -- initialized by rebuildVertexIndices
430  // s & nonexistingVertex_; -- initialized by rebuildVertexIndices
431  // s & progress_; -- not saved/restored
432  // s & cfgProgressTotal_; -- not saved/restored
433  }
434 
435  template<class S>
436  void save(S &s, const unsigned version) const {
437  const_cast<Partitioner*>(this)->serializeCommon(s, version);
438  }
439 
440  template<class S>
441  void load(S &s, const unsigned version) {
442  serializeCommon(s, version);
443  rebuildVertexIndices();
444  }
445 
446  BOOST_SERIALIZATION_SPLIT_MEMBER();
447 #endif
448 
449 
452  //
453  // Constructors
454  //
457 public:
462  Partitioner();
463 
468  Partitioner(Disassembler *disassembler, const MemoryMap::Ptr &map);
469 
470 #ifdef ROSE_PARTITIONER_MOVE
471 
472  Partitioner(BOOST_RV_REF(Partitioner));
473 
475  Partitioner& operator=(BOOST_RV_REF(Partitioner));
476 #else
477  // These are unsafe
478  Partitioner(const Partitioner&);
479  Partitioner& operator=(const Partitioner&);
480 #endif
481 
482  ~Partitioner();
483 
490  bool isDefaultConstructed() const { return instructionProvider_ == NULL; }
491 
493  void clear() /*final*/;
494 
502  Configuration& configuration() { return config_; }
503  const Configuration& configuration() const { return config_; }
511  InstructionProvider& instructionProvider() /*final*/ { return *instructionProvider_; }
512  const InstructionProvider& instructionProvider() const /*final*/ { return *instructionProvider_; }
524  MemoryMap::Ptr memoryMap() const /*final*/ { return memoryMap_; }
530  bool addressIsExecutable(rose_addr_t va) const /*final*/ {
531  return memoryMap_!=NULL && memoryMap_->at(va).require(MemoryMap::EXECUTABLE).exists();
532  }
533 
536  //
537  // Unparsing
538  //
541 
551  Unparser::BasePtr unparser() const /*final*/;
552  void unparser(const Unparser::BasePtr&) /*final*/;
564  Unparser::BasePtr insnUnparser() const /*final*/;
565  void insnUnparser(const Unparser::BasePtr&) /*final*/;
569  void configureInsnUnparser(const Unparser::BasePtr&) const /*final*/;
570 
579  std::string unparse(SgAsmInstruction*) const;
580  void unparse(std::ostream&, SgAsmInstruction*) const;
581  void unparse(std::ostream&, const BasicBlock::Ptr&) const;
582  void unparse(std::ostream&, const DataBlock::Ptr&) const;
583  void unparse(std::ostream&, const Function::Ptr&) const;
584  void unparse(std::ostream&) const;
587  //
590  // Partitioner CFG queries
591  //
594 public:
600  size_t nBytes() const /*final*/ { return aum_.size(); }
601 
609  ControlFlowGraph::VertexIterator undiscoveredVertex() /*final*/ {
610  return undiscoveredVertex_;
611  }
612  ControlFlowGraph::ConstVertexIterator undiscoveredVertex() const /*final*/ {
613  return undiscoveredVertex_;
614  }
627  ControlFlowGraph::VertexIterator indeterminateVertex() /*final*/ {
628  return indeterminateVertex_;
629  }
630  ControlFlowGraph::ConstVertexIterator indeterminateVertex() const /*final*/ {
631  return indeterminateVertex_;
632  }
644  ControlFlowGraph::VertexIterator nonexistingVertex() /*final*/ {
645  return nonexistingVertex_;
646  }
647  ControlFlowGraph::ConstVertexIterator nonexistingVertex() const /*final*/ {
648  return nonexistingVertex_;
649  }
658  const ControlFlowGraph& cfg() const /*final*/ { return cfg_; }
659 
666  const AddressUsageMap& aum() const /*final*/ { return aum_; }
667 
669  AddressUsageMap aum(const Function::Ptr&) const /*final*/;
670 
675  std::vector<AddressUser> users(rose_addr_t) const /*final*/;
676 
684  std::set<rose_addr_t> ghostSuccessors() const /*final*/;
685 
714  bool isEdgeIntraProcedural(ControlFlowGraph::ConstEdgeIterator edge,
715  const Function::Ptr &function = Function::Ptr()) const /*final*/;
716  bool isEdgeIntraProcedural(const ControlFlowGraph::Edge &edge,
717  const Function::Ptr &function = Function::Ptr()) const /*final*/;
754  bool isEdgeInterProcedural(ControlFlowGraph::ConstEdgeIterator edge,
755  const Function::Ptr &sourceFunction = Function::Ptr(),
756  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
757  bool isEdgeInterProcedural(const ControlFlowGraph::Edge &edge,
758  const Function::Ptr &sourceFunction = Function::Ptr(),
759  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
764  //
767  // Partitioner instruction operations
768  //
771 public:
777  size_t nInstructions() const /*final*/;
778 
788  AddressUser instructionExists(rose_addr_t startVa) const /*final*/ {
789  return aum_.findInstruction(startVa);
790  }
792  return aum_.findInstruction(insn);
793  }
802  ControlFlowGraph::ConstVertexIterator instructionVertex(rose_addr_t insnVa) const;
803 
812  std::vector<SgAsmInstruction*> instructionsOverlapping(const AddressInterval&) const /*final*/;
813 
822  std::vector<SgAsmInstruction*> instructionsSpanning(const AddressInterval&) const /*final*/;
823 
833  std::vector<SgAsmInstruction*> instructionsContainedIn(const AddressInterval&) const /*final*/;
834 
842  AddressInterval instructionExtent(SgAsmInstruction*) const /*final*/;
843 
856  SgAsmInstruction* discoverInstruction(rose_addr_t startVa) const /*final*/;
857 
862  CrossReferences instructionCrossReferences(const AddressIntervalSet &restriction) const /*final*/;
863 
864 
867  //
868  // Partitioner basic block placeholder operations
869  //
872 public:
882  size_t nPlaceholders() const /*final*/;
883 
892  bool placeholderExists(rose_addr_t startVa) const /*final*/;
893 
904  ControlFlowGraph::VertexIterator findPlaceholder(rose_addr_t startVa) /*final*/ {
905  if (Sawyer::Optional<ControlFlowGraph::VertexIterator> found = vertexIndex_.getOptional(startVa))
906  return *found;
907  return cfg_.vertices().end();
908  }
909  ControlFlowGraph::ConstVertexIterator findPlaceholder(rose_addr_t startVa) const /*final*/ {
910  if (Sawyer::Optional<ControlFlowGraph::VertexIterator> found = vertexIndex_.getOptional(startVa))
911  return *found;
912  return cfg_.vertices().end();
913  }
932  ControlFlowGraph::VertexIterator insertPlaceholder(rose_addr_t startVa) /*final*/;
933 
947  BasicBlock::Ptr erasePlaceholder(const ControlFlowGraph::ConstVertexIterator &placeholder) /*final*/;
948  BasicBlock::Ptr erasePlaceholder(rose_addr_t startVa) /*final*/;
953  //
956  // Partitioner basic block operations
957  //
960 public:
974  bool basicBlockSemanticsAutoDrop() const /*final*/ { return settings_.basicBlockSemanticsAutoDrop; }
986  void basicBlockDropSemantics() const /*final*/;
987 
996  size_t nBasicBlocks() const /*final*/;
997 
1005  std::vector<BasicBlock::Ptr> basicBlocks() const /*final*/;
1006 
1028  BasicBlock::Ptr basicBlockExists(rose_addr_t startVa) const /*final*/;
1029  BasicBlock::Ptr basicBlockExists(const BasicBlock::Ptr&) const /*final*/;
1041  std::vector<BasicBlock::Ptr> basicBlocksOverlapping(const AddressInterval&) const /*final*/;
1042 
1053  std::vector<BasicBlock::Ptr> basicBlocksSpanning(const AddressInterval&) const /*final*/;
1054 
1064  std::vector<BasicBlock::Ptr> basicBlocksContainedIn(const AddressInterval&) const /*final*/;
1065 
1072  BasicBlock::Ptr basicBlockContainingInstruction(rose_addr_t insnVa) const /*final*/;
1073 
1083  AddressIntervalSet basicBlockInstructionExtent(const BasicBlock::Ptr&) const /*final*/;
1084 
1090  AddressIntervalSet basicBlockDataExtent(const BasicBlock::Ptr&) const /*final*/;
1091 
1116  BasicBlock::Ptr detachBasicBlock(rose_addr_t startVa) /*final*/;
1117  BasicBlock::Ptr detachBasicBlock(const BasicBlock::Ptr &basicBlock) /*final*/;
1118  BasicBlock::Ptr detachBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder) /*final*/;
1133  ControlFlowGraph::VertexIterator truncateBasicBlock(const ControlFlowGraph::ConstVertexIterator &basicBlock,
1134  SgAsmInstruction *insn) /*final*/;
1135 
1168  void attachBasicBlock(const BasicBlock::Ptr&) /*final*/;
1169  void attachBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder, const BasicBlock::Ptr&) /*final*/;
1257  BasicBlock::Ptr discoverBasicBlock(rose_addr_t startVa) const /*final*/;
1258  BasicBlock::Ptr discoverBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder) const /*final*/;
1275  BasicBlock::Successors basicBlockSuccessors(const BasicBlock::Ptr&,
1276  Precision::Level precision = Precision::HIGH) const /*final*/;
1277 
1286  std::vector<rose_addr_t> basicBlockConcreteSuccessors(const BasicBlock::Ptr&, bool *isComplete=NULL) const /*final*/;
1287 
1306  std::set<rose_addr_t> basicBlockGhostSuccessors(const BasicBlock::Ptr&) const /*final*/;
1307 
1317  bool basicBlockIsFunctionCall(const BasicBlock::Ptr&, Precision::Level precision = Precision::HIGH) const /*final*/;
1318 
1329  bool basicBlockIsFunctionReturn(const BasicBlock::Ptr&) const /*final*/;
1330 
1335  bool basicBlockPopsStack(const BasicBlock::Ptr&) const /*final*/;
1336 
1376  BaseSemantics::SValuePtr basicBlockStackDeltaIn(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1377  BaseSemantics::SValuePtr basicBlockStackDeltaOut(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1388  void forgetStackDeltas() const /*final*/;
1389  void forgetStackDeltas(const Function::Ptr&) const /*final*/;
1401  size_t stackDeltaInterproceduralLimit() const /*final*/ { return stackDeltaInterproceduralLimit_; }
1402  void stackDeltaInterproceduralLimit(size_t n) /*final*/ { stackDeltaInterproceduralLimit_ = std::max(size_t(1), n); }
1455  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const BasicBlock::Ptr&) const /*final*/;
1456 
1457  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
1466  void basicBlockMayReturnReset() const /*final*/;
1467 
1468 private:
1469  // Per-vertex data used during may-return analysis
1470  struct MayReturnVertexInfo {
1471  enum State {INIT, CALCULATING, FINISHED};
1472  State state; // current state of vertex
1473  bool processedCallees; // have we processed BBs this vertex calls?
1474  boost::logic::tribool anyCalleesReturn; // do any of those called BBs have a true may-return value?
1475  boost::logic::tribool result; // final result (eventually cached in BB)
1476  MayReturnVertexInfo(): state(INIT), processedCallees(false), anyCalleesReturn(false), result(boost::indeterminate) {}
1477  };
1478 
1479  // Is edge significant for analysis? See .C file for full documentation.
1480  bool mayReturnIsSignificantEdge(const ControlFlowGraph::ConstEdgeIterator &edge,
1481  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1482 
1483  // Determine (and cache in vertexInfo) whether any callees return.
1484  boost::logic::tribool mayReturnDoesCalleeReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1485  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1486 
1487  // Maximum may-return result from significant successors including phantom call-return edge.
1488  boost::logic::tribool mayReturnDoesSuccessorReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1489  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1490 
1491  // The guts of the may-return analysis
1492  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator &start,
1493  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1494 
1495 
1496 
1499  //
1500  // Partitioner data block operations
1501  //
1504 public:
1510  size_t nDataBlocks() const /*final*/;
1511 
1518  DataBlock::Ptr dataBlockExists(const DataBlock::Ptr&) const /*final*/;
1519 
1528  DataBlock::Ptr findBestDataBlock(const AddressInterval&) const /*final*/;
1529 
1541  DataBlock::Ptr attachDataBlock(const DataBlock::Ptr&) /*final*/;
1542 
1549  void detachDataBlock(const DataBlock::Ptr&) /*final*/;
1550 
1560  DataBlock::Ptr attachDataBlockToFunction(const DataBlock::Ptr&, const Function::Ptr&) /*final*/;
1561 
1574  DataBlock::Ptr attachDataBlockToBasicBlock(const DataBlock::Ptr&, const BasicBlock::Ptr&) /*final*/;
1575 
1585  std::vector<DataBlock::Ptr> dataBlocksOverlapping(const AddressInterval&) const /*final*/;
1586 
1596  std::vector<DataBlock::Ptr> dataBlocksSpanning(const AddressInterval&) const /*final*/;
1597 
1607  std::vector<DataBlock::Ptr> dataBlocksContainedIn(const AddressInterval&) const /*final*/;
1608 
1615  AddressInterval dataBlockExtent(const DataBlock::Ptr&) const /*final*/;
1616 
1622  std::vector<DataBlock::Ptr> dataBlocks() const /*final*/;
1623 
1624 
1625 
1628  //
1629  // Partitioner function operations
1630  //
1633 public:
1639  size_t nFunctions() const /*final*/ { return functions_.size(); }
1640 
1659  Function::Ptr functionExists(rose_addr_t entryVa) const /*final*/;
1660  Function::Ptr functionExists(const BasicBlock::Ptr &entryBlock) const /*final*/;
1661  Function::Ptr functionExists(const Function::Ptr &function) const /*final*/;
1669  std::vector<Function::Ptr> functions() const /*final*/;
1670 
1680  std::vector<Function::Ptr> functionsOverlapping(const AddressInterval&) const /*final*/;
1681 
1692  std::vector<Function::Ptr> functionsSpanning(const AddressInterval&) const /*final*/;
1693 
1703  std::vector<Function::Ptr> functionsContainedIn(const AddressInterval&) const /*final*/;
1704 
1721  AddressIntervalSet functionExtent(const Function::Ptr&) const /*final*/;
1722  void functionExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1723  AddressIntervalSet functionBasicBlockExtent(const Function::Ptr &function) const /*final*/;
1724  void functionBasicBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1725  AddressIntervalSet functionDataBlockExtent(const Function::Ptr &function) const /*final*/;
1726  void functionDataBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1748  size_t attachFunction(const Function::Ptr&) /*final*/;
1749  size_t attachFunctions(const Functions&) /*final*/;
1765  Function::Ptr attachOrMergeFunction(const Function::Ptr&) /*final*/;
1766 
1783  size_t attachFunctionBasicBlocks(const Functions&) /*final*/;
1784  size_t attachFunctionBasicBlocks(const Function::Ptr&) /*final*/;
1800  void detachFunction(const Function::Ptr&) /*final*/;
1801 
1836  std::vector<Function::Ptr>
1837  functionsOwningBasicBlock(const ControlFlowGraph::Vertex&, bool doSort = true) const /*final*/;
1838 
1839  std::vector<Function::Ptr>
1840  functionsOwningBasicBlock(const ControlFlowGraph::ConstVertexIterator&, bool doSort = true) const /*final*/;
1841 
1842  std::vector<Function::Ptr>
1843  functionsOwningBasicBlock(rose_addr_t bblockVa, bool doSort = true) const /*final*/;
1844 
1845  std::vector<Function::Ptr>
1846  functionsOwningBasicBlock(const BasicBlock::Ptr&, bool doSort = true) const /*final*/;
1847 
1848  template<class Container> // container can hold any type accepted by functionsOwningBasicBlock
1849  std::vector<Function::Ptr>
1850  functionsOwningBasicBlocks(const Container &bblocks) const /*final*/ {
1851  std::vector<Function::Ptr> retval;
1852  BOOST_FOREACH (const typename Container::value_type& bblock, bblocks) {
1853  BOOST_FOREACH (const Function::Ptr &function, functionsOwningBasicBlock(bblock, false))
1854  insertUnique(retval, function, sortFunctionsByAddress);
1855  }
1856  return retval;
1857  }
1869  std::vector<Function::Ptr> discoverCalledFunctions() const /*final*/;
1870 
1882  std::vector<Function::Ptr> discoverFunctionEntryVertices() const /*final*/;
1883 
1893  Sawyer::Optional<Thunk> functionIsThunk(const Function::Ptr&) const /*final*/;
1894 
1905  void discoverFunctionBasicBlocks(const Function::Ptr &function) const /*final*/;
1906 
1913  std::set<rose_addr_t> functionGhostSuccessors(const Function::Ptr&) const /*final*/;
1914 
1922  FunctionCallGraph functionCallGraph(AllowParallelEdges::Type allowParallelEdges) const /*final*/;
1923 
1943  BaseSemantics::SValuePtr functionStackDelta(const Function::Ptr &function) const /*final*/;
1944 
1948  void allFunctionStackDelta() const /*final*/;
1949 
1956  Sawyer::Optional<bool> functionOptionalMayReturn(const Function::Ptr &function) const /*final*/;
1957 
1961  void allFunctionMayReturn() const /*final*/;
1962 
1989  const CallingConvention::Analysis&
1990  functionCallingConvention(const Function::Ptr&,
1991  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1992  const /*final*/;
1993 
2006  void
2007  allFunctionCallingConvention(const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
2008  const /*final*/;
2009 
2035  CallingConvention::Dictionary
2036  functionCallingConventionDefinitions(const Function::Ptr&,
2037  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
2038  const /*final*/;
2039 
2052  void
2053  allFunctionCallingConventionDefinition(const CallingConvention::Definition::Ptr &dflt =
2054  CallingConvention::Definition::Ptr()) const /*final*/;
2055 
2064  void fixInterFunctionEdges() /*final*/;
2065  void fixInterFunctionEdge(const ControlFlowGraph::ConstEdgeIterator&) /*final*/;
2085  bool functionIsNoop(const Function::Ptr&) const /*final*/;
2086 
2092  void allFunctionIsNoop() const /*final*/;
2093 
2101  void forgetFunctionIsNoop() const /*final*/;
2102  void forgetFunctionIsNoop(const Function::Ptr&) const /*final*/;
2109  std::set<rose_addr_t> functionDataFlowConstants(const Function::Ptr&) const /*final*/;
2110 
2111 
2112 
2115  //
2116  // Callbacks
2117  //
2120 public:
2138  CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() /*final*/ { return cfgAdjustmentCallbacks_; }
2139  const CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() const /*final*/ { return cfgAdjustmentCallbacks_; }
2150  BasicBlockCallbacks& basicBlockCallbacks() /*final*/ { return basicBlockCallbacks_; }
2151  const BasicBlockCallbacks& basicBlockCallbacks() const /*final*/ { return basicBlockCallbacks_; }
2154 public:
2162  FunctionPrologueMatchers& functionPrologueMatchers() /*final*/ { return functionPrologueMatchers_; }
2163  const FunctionPrologueMatchers& functionPrologueMatchers() const /*final*/ { return functionPrologueMatchers_; }
2185  std::vector<Function::Ptr> nextFunctionPrologue(rose_addr_t startVa) /*final*/;
2186 
2187 public:
2193  FunctionPaddingMatchers& functionPaddingMatchers() /*final*/ { return functionPaddingMatchers_; }
2194  const FunctionPaddingMatchers& functionPaddingMatchers() const /*final*/ { return functionPaddingMatchers_; }
2204  DataBlock::Ptr matchFunctionPadding(const Function::Ptr&) /*final*/;
2205 
2206 
2207 
2210  //
2211  // Partitioner miscellaneous
2212  //
2215 public:
2227  void dumpCfg(std::ostream&, const std::string &prefix="", bool showBlocks=true,
2228  bool computeProperties=true) const /*final*/;
2229 
2243  void cfgGraphViz(std::ostream&, const AddressInterval &restrict = AddressInterval::whole(),
2244  bool showNeighbors=true) const /*final*/;
2245 
2251  static std::string vertexName(const ControlFlowGraph::Vertex&) /*final*/;
2252  std::string vertexName(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
2258  static std::string vertexNameEnd(const ControlFlowGraph::Vertex&) /*final*/;
2259 
2265  static std::string edgeNameSrc(const ControlFlowGraph::Edge&) /*final*/;
2266  std::string edgeNameSrc(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2274  static std::string edgeNameDst(const ControlFlowGraph::Edge&) /*final*/;
2275  std::string edgeNameDst(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2283  static std::string edgeName(const ControlFlowGraph::Edge&) /*final*/;
2284  std::string edgeName(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2290  static std::string basicBlockName(const BasicBlock::Ptr&) /*final*/;
2291 
2295  static std::string dataBlockName(const DataBlock::Ptr&) /*final*/;
2296 
2300  static std::string functionName(const Function::Ptr&) /*final*/;
2301 
2306  void expandIndeterminateCalls();
2307 
2325  Progress::Ptr progress() const /*final*/;
2326  void progress(const Progress::Ptr&) /*final*/;
2333  void updateProgress(const std::string &phase, double completion) const;
2334 
2336  void showStatistics() const;
2337 
2338  // Checks consistency of internal data structures when debugging is enable (when NDEBUG is not defined).
2339  void checkConsistency() const;
2340 
2342  // Settings
2344 public:
2345 
2353  const BasePartitionerSettings& settings() const /*final*/ { return settings_; }
2354  void settings(const BasePartitionerSettings &s) /*final*/ { settings_ = s; }
2365  void enableSymbolicSemantics(bool b=true) /*final*/ { settings_.usingSemantics = b; }
2366  void disableSymbolicSemantics() /*final*/ { settings_.usingSemantics = false; }
2367  bool usingSymbolicSemantics() const /*final*/ { return settings_.usingSemantics; }
2391  void autoAddCallReturnEdges(bool b) /*final*/ { autoAddCallReturnEdges_ = b; }
2392  bool autoAddCallReturnEdges() const /*final*/ { return autoAddCallReturnEdges_; }
2409  void assumeFunctionsReturn(bool b) /*final*/ { assumeFunctionsReturn_ = b; }
2410  bool assumeFunctionsReturn() const /*final*/ { return assumeFunctionsReturn_; }
2422  void addressName(rose_addr_t, const std::string&) /*final*/;
2423  const std::string& addressName(rose_addr_t va) const /*final*/ { return addressNames_.getOrDefault(va); }
2424  const AddressNameMap& addressNames() const /*final*/ { return addressNames_; }
2432  const SourceLocations& sourceLocations() const /*final*/ { return sourceLocations_; }
2433  SourceLocations& sourceLocations() /*final*/ { return sourceLocations_; }
2434  void sourceLocations(const SourceLocations &locs) { sourceLocations_ = locs; }
2444  bool checkingCallBranch() const /*final*/ { return settings_.checkingCallBranch; }
2445  void checkingCallBranch(bool b) /*final*/ { settings_.checkingCallBranch = b; }
2449  //
2452  // Instruction semantics
2453  //
2456 public:
2466  SemanticMemoryParadigm semanticMemoryParadigm() const { return semanticMemoryParadigm_; }
2467  void semanticMemoryParadigm(SemanticMemoryParadigm p) { semanticMemoryParadigm_ = p; }
2476  SmtSolverPtr smtSolver() const /*final*/ { return solver_; }
2477 
2488  BaseSemantics::RiscOperatorsPtr newOperators() const /*final*/;
2489  BaseSemantics::RiscOperatorsPtr newOperators(SemanticMemoryParadigm) const /*final*/;
2499  BaseSemantics::DispatcherPtr newDispatcher(const BaseSemantics::RiscOperatorsPtr&) const /*final*/;
2500 
2501 
2502 
2505  //
2506  // Python API support functions
2507  //
2510 #ifdef ROSE_ENABLE_PYTHON_API
2511  void pythonUnparse() const;
2512 #endif
2513 
2514 
2515 
2518  //
2519  // Partitioner internal utilities
2520  //
2523 private:
2524  void init(Disassembler*, const MemoryMap::Ptr&);
2525  void init(const Partitioner&);
2526  void updateCfgProgress();
2527 
2528 private:
2529  // Convert a CFG vertex iterator from one partitioner to another. This is called during copy construction when the source
2530  // and destination CFGs are identical.
2531  ControlFlowGraph::VertexIterator convertFrom(const Partitioner &other,
2532  ControlFlowGraph::ConstVertexIterator otherIter);
2533 
2534  // Adjusts edges for a placeholder vertex. This method erases all outgoing edges for the specified placeholder vertex and
2535  // then inserts a single edge from the placeholder to the special "undiscovered" vertex. */
2536  ControlFlowGraph::EdgeIterator adjustPlaceholderEdges(const ControlFlowGraph::VertexIterator &placeholder);
2537 
2538  // Adjusts edges for a non-existing basic block. This method erases all outgoing edges for the specified vertex and
2539  // then inserts a single edge from the vertex to the special "non-existing" vertex. */
2540  ControlFlowGraph::EdgeIterator adjustNonexistingEdges(const ControlFlowGraph::VertexIterator &vertex);
2541 
2542  // Implementation for the discoverBasicBlock methods. The startVa must not be the address of an existing placeholder.
2543  BasicBlock::Ptr discoverBasicBlockInternal(rose_addr_t startVa) const;
2544 
2545  // This method is called whenever a new placeholder is inserted into the CFG or a new basic block is attached to the
2546  // CFG/AUM. The call happens immediately after the CFG/AUM are updated.
2547  void bblockAttached(const ControlFlowGraph::VertexIterator &newVertex);
2548 
2549  // This method is called whenever a basic block is detached from the CFG/AUM or when a placeholder is erased from the CFG.
2550  // The call happens immediately after the CFG/AUM are updated.
2551  void bblockDetached(rose_addr_t startVa, const BasicBlock::Ptr &removedBlock);
2552 
2553  // Rebuild the vertexIndex_ and other cache-like data members from the control flow graph
2554  void rebuildVertexIndices();
2555 };
2556 
2557 } // namespace
2558 } // namespace
2559 } // namespace
2560 
2561 // Class versions must be at global scope
2562 BOOST_CLASS_VERSION(Rose::BinaryAnalysis::Partitioner2::Partitioner, 1);
2563 
2564 #endif
2565 #endif
Represents information about a thunk.
Definition: Partitioner.h:333
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:328
const FunctionPrologueMatchers & functionPrologueMatchers() const
Ordered list of function prologue matchers.
Definition: Partitioner.h:2163
SmtSolverPtr smtSolver() const
SMT solver.
Definition: Partitioner.h:2476
BasicBlock::Ptr bblock
The one and only basic block for the thunk.
Definition: Partitioner.h:334
FunctionPrologueMatchers & functionPrologueMatchers()
Ordered list of function prologue matchers.
Definition: Partitioner.h:2162
const std::string & addressName(rose_addr_t va) const
Property: Name for address.
Definition: Partitioner.h:2423
void disableSymbolicSemantics()
Use or not use symbolic semantics.
Definition: Partitioner.h:2366
void checkingCallBranch(bool b)
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2445
ControlFlowGraph::ConstVertexIterator indeterminateVertex() const
Returns the special "indeterminate" vertex.
Definition: Partitioner.h:630
const SourceLocations & sourceLocations() const
Property: Source locations.
Definition: Partitioner.h:2432
const Configuration & configuration() const
Configuration information.
Definition: Partitioner.h:503
Base class for machine instructions.
Bidirectional mapping between addresses and source locations.
const FunctionPaddingMatchers & functionPaddingMatchers() const
Ordered list of function padding matchers.
Definition: Partitioner.h:2194
AddressUser instructionExists(SgAsmInstruction *insn) const
Determines whether an instruction is attached to the CFG/AUM.
Definition: Partitioner.h:791
const ControlFlowGraph & cfg() const
Returns the control flow graph.
Definition: Partitioner.h:658
Provides and caches instructions.
Settings that directly control a partitioner.
Definition: BasicTypes.h:305
STL namespace.
Holds a value or nothing.
Definition: Optional.h:49
ControlFlowGraph::VertexIterator indeterminateVertex()
Returns the special "indeterminate" vertex.
Definition: Partitioner.h:627
InstructionProvider & instructionProvider()
Returns the instruction provider.
Definition: Partitioner.h:511
void sourceLocations(const SourceLocations &locs)
Property: Source locations.
Definition: Partitioner.h:2434
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:340
BasicBlockCallbacks & basicBlockCallbacks()
Callbacks for adjusting basic block during discovery.
Definition: Partitioner.h:2150
ControlFlowGraph::VertexIterator undiscoveredVertex()
Returns the special "undiscovered" vertex.
Definition: Partitioner.h:609
SemanticMemoryParadigm semanticMemoryParadigm() const
Property: Whether to use map- or list-based memory states.
Definition: Partitioner.h:2466
Name space for the entire library.
Configuration & configuration()
Configuration information.
Definition: Partitioner.h:502
bool assumeFunctionsReturn() const
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2410
ControlFlowGraph::ConstVertexIterator findPlaceholder(rose_addr_t startVa) const
Find the CFG vertex for a basic block placeholder.
Definition: Partitioner.h:909
const AddressUsageMap & aum() const
Returns the address usage map.
Definition: Partitioner.h:666
const AddressNameMap & addressNames() const
Property: Name for address.
Definition: Partitioner.h:2424
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:2354
void assumeFunctionsReturn(bool b)
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2409
rose_addr_t target
The one and only successor for the basic block.
Definition: Partitioner.h:335
ControlFlowGraph::VertexIterator nonexistingVertex()
Returns the special "non-existing" vertex.
Definition: Partitioner.h:644
bool isDefaultConstructed() const
Return true if this is a default constructed partitioner.
Definition: Partitioner.h:490
bool usingSymbolicSemantics() const
Use or not use symbolic semantics.
Definition: Partitioner.h:2367
SemanticMemoryParadigm
Organization of semantic memory.
Definition: BasicTypes.h:85
const BasicBlockCallbacks & basicBlockCallbacks() const
Callbacks for adjusting basic block during discovery.
Definition: Partitioner.h:2151
void basicBlockSemanticsAutoDrop(bool b)
Property: Automatically drop semantics for attached basic blocks.
Definition: Partitioner.h:975
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:1402
const InstructionProvider & instructionProvider() const
Returns the instruction provider.
Definition: Partitioner.h:512
bool checkingCallBranch
Check for situations where CALL is used as a branch.
Definition: BasicTypes.h:310
bool usingSemantics
Whether instruction semantics are used.
Definition: BasicTypes.h:306
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:2391
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:974
bool basicBlockSemanticsAutoDrop
Conserve memory by dropping semantics for attached basic blocks.
Definition: BasicTypes.h:311
ControlFlowGraph::ConstVertexIterator undiscoveredVertex() const
Returns the special "undiscovered" vertex.
Definition: Partitioner.h:612
ControlFlowGraph::ConstVertexIterator nonexistingVertex() const
Returns the special "non-existing" vertex.
Definition: Partitioner.h:647
std::vector< FunctionPrologueMatcher::Ptr > FunctionPrologueMatchers
See functionPrologueMatchers.
Definition: Partitioner.h:329
FunctionPaddingMatchers & functionPaddingMatchers()
Ordered list of function padding matchers.
Definition: Partitioner.h:2193
const CfgAdjustmentCallbacks & cfgAdjustmentCallbacks() const
List of all callbacks invoked when the CFG is adjusted.
Definition: Partitioner.h:2139
API and storage for attributes.
Definition: Attribute.h:208
MemoryMap::Ptr memoryMap() const
Returns the memory map.
Definition: Partitioner.h:524
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:321
bool checkingCallBranch() const
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2444
bool addressIsExecutable(rose_addr_t va) const
Returns true if address is executable.
Definition: Partitioner.h:530
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:50
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:2365
bool autoAddCallReturnEdges() const
Property: Insert (or not) function call return edges.
Definition: Partitioner.h:2392
Sawyer::Callbacks< CfgAdjustmentCallback::Ptr > CfgAdjustmentCallbacks
See cfgAdjustmentCallbacks.
Definition: Partitioner.h:327
std::vector< FunctionPaddingMatcher::Ptr > FunctionPaddingMatchers
See functionPaddingMatchers.
Definition: Partitioner.h:330
void semanticMemoryParadigm(SemanticMemoryParadigm p)
Property: Whether to use map- or list-based memory states.
Definition: Partitioner.h:2467
Holds configuration information.
Definition: Config.h:282
SourceLocations & sourceLocations()
Property: Source locations.
Definition: Partitioner.h:2433