ROSE  0.11.83.2
Partitioner.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner2_Partitioner_H
2 #define ROSE_BinaryAnalysis_Partitioner2_Partitioner_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/Partitioner2/AddressUsageMap.h>
7 #include <Rose/BinaryAnalysis/Partitioner2/BasicBlock.h>
8 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
9 #include <Rose/BinaryAnalysis/Partitioner2/Config.h>
10 #include <Rose/BinaryAnalysis/Partitioner2/ControlFlowGraph.h>
11 #include <Rose/BinaryAnalysis/Partitioner2/DataBlock.h>
12 #include <Rose/BinaryAnalysis/Partitioner2/Function.h>
13 #include <Rose/BinaryAnalysis/Partitioner2/FunctionCallGraph.h>
14 #include <Rose/BinaryAnalysis/Partitioner2/InstructionProvider.h>
15 #include <Rose/BinaryAnalysis/Partitioner2/Modules.h>
16 #include <Rose/BinaryAnalysis/Partitioner2/Reference.h>
17 
18 #include <Sawyer/Attribute.h>
19 #include <Sawyer/Callbacks.h>
20 #include <Sawyer/IntervalSet.h>
21 #include <Sawyer/Map.h>
22 #include <Sawyer/Message.h>
23 #include <Sawyer/Optional.h>
24 #include <Sawyer/ProgressBar.h>
25 #include <Sawyer/SharedPointer.h>
26 
27 #include <Rose/BinaryAnalysis/SourceLocations.h>
28 #include <Rose/BinaryAnalysis/Unparser/Settings.h>
29 #include <Rose/Progress.h>
30 
31 #include <boost/filesystem.hpp>
32 #include <boost/move/utility_core.hpp>
33 #include <boost/serialization/access.hpp>
34 #include <boost/serialization/split_member.hpp>
35 #include <boost/serialization/version.hpp>
36 
37 #include <ostream>
38 #include <set>
39 #include <string>
40 #include <vector>
41 
42 // Derived classes needed for serialization
43 #include <Rose/BinaryAnalysis/Z3Solver.h>
44 #include <Rose/BinaryAnalysis/InstructionSemantics2/DispatcherAarch32.h>
45 #include <Rose/BinaryAnalysis/InstructionSemantics2/DispatcherAarch64.h>
46 #include <Rose/BinaryAnalysis/InstructionSemantics2/DispatcherM68k.h>
47 #include <Rose/BinaryAnalysis/InstructionSemantics2/DispatcherPowerpc.h>
48 #include <Rose/BinaryAnalysis/InstructionSemantics2/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 namespace Partitioner2 {
75 
289 class ROSE_DLL_API Partitioner: public Sawyer::Attribute::Storage<> { // final
290 #ifdef ROSE_PARTITIONER_MOVE
291  BOOST_MOVABLE_BUT_NOT_COPYABLE(Partitioner)
292 #endif
293 
294 public:
297  typedef std::vector<FunctionPrologueMatcher::Ptr> FunctionPrologueMatchers;
298  typedef std::vector<FunctionPaddingMatcher::Ptr> FunctionPaddingMatchers;
301  struct Thunk {
303  rose_addr_t target;
304  Thunk(const BasicBlock::Ptr &bblock, rose_addr_t target): bblock(bblock), target(target) {}
305  };
306 
309 
310 private:
311  BasePartitionerSettings settings_; // settings adjustable from the command-line
312  Configuration config_; // configuration information about functions, blocks, etc.
313  InstructionProvider::Ptr instructionProvider_; // cache for all disassembled instructions
314  MemoryMap::Ptr memoryMap_; // description of memory, especially insns and non-writable
315  ControlFlowGraph cfg_; // basic blocks that will become part of the ROSE AST
316  CfgVertexIndex vertexIndex_; // Vertex-by-address index for the CFG
317  AddressUsageMap aum_; // How addresses are used for each address represented by the CFG
318  SmtSolverPtr solver_; // Satisfiable modulo theory solver used by semantic expressions
319  Functions functions_; // List of all attached functions by entry address
320  bool autoAddCallReturnEdges_; // Add E_CALL_RETURN edges when blocks are attached to CFG?
321  bool assumeFunctionsReturn_; // Assume that unproven functions return to caller?
322  size_t stackDeltaInterproceduralLimit_; // Max depth of call stack when computing stack deltas
323  AddressNameMap addressNames_; // Names for various addresses
324  SourceLocations sourceLocations_; // Mapping between source locations and addresses
325  SemanticMemoryParadigm semanticMemoryParadigm_; // Slow and precise, or fast and imprecise?
326  Unparser::BasePtr unparser_; // For unparsing things to pseudo-assembly
327  Unparser::BasePtr insnUnparser_; // For unparsing single instructions in diagnostics
328  Unparser::BasePtr insnPlainUnparser_; // For unparsing just instruction mnemonic and operands
329 
330  // Callback lists
331  CfgAdjustmentCallbacks cfgAdjustmentCallbacks_;
332  BasicBlockCallbacks basicBlockCallbacks_;
333  FunctionPrologueMatchers functionPrologueMatchers_;
334  FunctionPaddingMatchers functionPaddingMatchers_;
335 
336  // Special CFG vertices.
337  ControlFlowGraph::VertexIterator undiscoveredVertex_;
338  ControlFlowGraph::VertexIterator indeterminateVertex_;
339  ControlFlowGraph::VertexIterator nonexistingVertex_;
340  static const size_t nSpecialVertices = 3;
341 
342  // Optional cached information
343  Sawyer::Optional<rose_addr_t> elfGotVa_; // address of ELF GOT, set by findElfGotVa
344 
345  // Protects the following data members
346  mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
347  Progress::Ptr progress_; // Progress reporter to update, or null
348  mutable size_t cfgProgressTotal_; // Expected total for the CFG progress bar; initialized at first report
349 
350 
351 
354  //
355  // Serialization
356  //
359 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
360 private:
361  friend class boost::serialization::access;
362 
363  template<class S>
364  void serializeCommon(S &s, const unsigned version) {
365  s.template register_type<InstructionSemantics2::SymbolicSemantics::SValue>();
366  s.template register_type<InstructionSemantics2::SymbolicSemantics::RiscOperators>();
367 #ifdef ROSE_ENABLE_ASM_AARCH64
368  s.template register_type<InstructionSemantics2::DispatcherAarch64>();
369 #endif
370 #ifdef ROSE_ENABLE_ASM_AARCH32
371  s.template register_type<InstructionSemantics2::DispatcherAarch32>();
372 #endif
373  s.template register_type<InstructionSemantics2::DispatcherX86>();
374  s.template register_type<InstructionSemantics2::DispatcherM68k>();
375  s.template register_type<InstructionSemantics2::DispatcherPowerpc>();
376  s.template register_type<SymbolicExpr::Interior>();
377  s.template register_type<SymbolicExpr::Leaf>();
378  s.template register_type<Z3Solver>();
379  s.template register_type<Semantics::SValue>();
380  s.template register_type<Semantics::MemoryListState>();
381  s.template register_type<Semantics::MemoryMapState>();
382  s.template register_type<Semantics::RegisterState>();
383  s.template register_type<Semantics::State>();
384  s.template register_type<Semantics::RiscOperators>();
385  s & BOOST_SERIALIZATION_NVP(settings_);
386  // s & config_; -- FIXME[Robb P Matzke 2016-11-08]
387  s & BOOST_SERIALIZATION_NVP(instructionProvider_);
388  s & BOOST_SERIALIZATION_NVP(memoryMap_);
389  s & BOOST_SERIALIZATION_NVP(cfg_);
390  // s & vertexIndex_; -- initialized by rebuildVertexIndices
391  s & BOOST_SERIALIZATION_NVP(aum_);
392  // s & BOOST_SERIALIZATION_NVP(solver_); -- not saved/restored in order to override from command-line
393  s & BOOST_SERIALIZATION_NVP(functions_);
394  s & BOOST_SERIALIZATION_NVP(autoAddCallReturnEdges_);
395  s & BOOST_SERIALIZATION_NVP(assumeFunctionsReturn_);
396  s & BOOST_SERIALIZATION_NVP(stackDeltaInterproceduralLimit_);
397  s & BOOST_SERIALIZATION_NVP(addressNames_);
398  if (version >= 1)
399  s & BOOST_SERIALIZATION_NVP(sourceLocations_);
400  s & BOOST_SERIALIZATION_NVP(semanticMemoryParadigm_);
401  // s & unparser_; -- not saved; restored from disassembler
402  // s & cfgAdjustmentCallbacks_; -- not saved/restored
403  // s & basicBlockCallbacks_; -- not saved/restored
404  // s & functionPrologueMatchers_; -- not saved/restored
405  // s & functionPaddingMatchers_; -- not saved/restored
406  // s & undiscoveredVertex_; -- initialized by rebuildVertexIndices
407  // s & indeterminateVertex_; -- initialized by rebuildVertexIndices
408  // s & nonexistingVertex_; -- initialized by rebuildVertexIndices
409  if (version >= 2)
410  s & BOOST_SERIALIZATION_NVP(elfGotVa_);
411  // s & progress_; -- not saved/restored
412  // s & cfgProgressTotal_; -- not saved/restored
413  }
414 
415  template<class S>
416  void save(S &s, const unsigned version) const {
417  const_cast<Partitioner*>(this)->serializeCommon(s, version);
418  }
419 
420  template<class S>
421  void load(S &s, const unsigned version) {
422  serializeCommon(s, version);
423  rebuildVertexIndices();
424  }
425 
426  BOOST_SERIALIZATION_SPLIT_MEMBER();
427 #endif
428 
429 
430 
433  //
434  // Constructors
435  //
438 public:
443  Partitioner();
444 
449  Partitioner(Disassembler *disassembler, const MemoryMap::Ptr &map);
450 
451 #ifdef ROSE_PARTITIONER_MOVE
452 
453  Partitioner(BOOST_RV_REF(Partitioner));
454 
456  Partitioner& operator=(BOOST_RV_REF(Partitioner));
457 #else
458  // These are unsafe
459  Partitioner(const Partitioner&);
460  Partitioner& operator=(const Partitioner&);
461 #endif
462 
463  ~Partitioner();
464 
471  bool isDefaultConstructed() const { return instructionProvider_ == NULL; }
472 
474  void clear() /*final*/;
475 
483  Configuration& configuration() { return config_; }
484  const Configuration& configuration() const { return config_; }
492  InstructionProvider& instructionProvider() /*final*/ { return *instructionProvider_; }
493  const InstructionProvider& instructionProvider() const /*final*/ { return *instructionProvider_; }
505  MemoryMap::Ptr memoryMap() const /*final*/ { return memoryMap_; }
511  bool addressIsExecutable(rose_addr_t va) const /*final*/ {
512  return memoryMap_!=NULL && memoryMap_->at(va).require(MemoryMap::EXECUTABLE).exists();
513  }
514 
517  //
518  // Unparsing
519  //
522 
532  Unparser::BasePtr unparser() const /*final*/;
533  void unparser(const Unparser::BasePtr&) /*final*/;
545  Unparser::BasePtr insnUnparser() const /*final*/;
546  void insnUnparser(const Unparser::BasePtr&) /*final*/;
550  void configureInsnUnparser(const Unparser::BasePtr&) const /*final*/;
551 
555  void configureInsnPlainUnparser(const Unparser::BasePtr&) const /*final*/;
556 
565  std::string unparse(SgAsmInstruction*) const;
566  void unparse(std::ostream&, SgAsmInstruction*) const;
567  void unparse(std::ostream&, const BasicBlock::Ptr&) const;
568  void unparse(std::ostream&, const DataBlock::Ptr&) const;
569  void unparse(std::ostream&, const Function::Ptr&) const;
570  void unparse(std::ostream&) const;
576  std::string unparsePlain(SgAsmInstruction*) const;
577 
580  //
581  // Partitioner CFG queries
582  //
585 public:
591  size_t nBytes() const /*final*/ { return aum_.size(); }
592 
600  ControlFlowGraph::VertexIterator undiscoveredVertex() /*final*/ {
601  return undiscoveredVertex_;
602  }
603  ControlFlowGraph::ConstVertexIterator undiscoveredVertex() const /*final*/ {
604  return undiscoveredVertex_;
605  }
618  ControlFlowGraph::VertexIterator indeterminateVertex() /*final*/ {
619  return indeterminateVertex_;
620  }
621  ControlFlowGraph::ConstVertexIterator indeterminateVertex() const /*final*/ {
622  return indeterminateVertex_;
623  }
635  ControlFlowGraph::VertexIterator nonexistingVertex() /*final*/ {
636  return nonexistingVertex_;
637  }
638  ControlFlowGraph::ConstVertexIterator nonexistingVertex() const /*final*/ {
639  return nonexistingVertex_;
640  }
649  const ControlFlowGraph& cfg() const /*final*/ { return cfg_; }
650 
657  const AddressUsageMap& aum() const /*final*/ { return aum_; }
658 
660  AddressUsageMap aum(const Function::Ptr&) const /*final*/;
661 
666  std::vector<AddressUser> users(rose_addr_t) const /*final*/;
667 
675  std::set<rose_addr_t> ghostSuccessors() const /*final*/;
676 
705  bool isEdgeIntraProcedural(ControlFlowGraph::ConstEdgeIterator edge,
706  const Function::Ptr &function = Function::Ptr()) const /*final*/;
707  bool isEdgeIntraProcedural(const ControlFlowGraph::Edge &edge,
708  const Function::Ptr &function = Function::Ptr()) const /*final*/;
745  bool isEdgeInterProcedural(ControlFlowGraph::ConstEdgeIterator edge,
746  const Function::Ptr &sourceFunction = Function::Ptr(),
747  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
748  bool isEdgeInterProcedural(const ControlFlowGraph::Edge &edge,
749  const Function::Ptr &sourceFunction = Function::Ptr(),
750  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
755  //
758  // Partitioner instruction operations
759  //
762 public:
768  size_t nInstructions() const /*final*/;
769 
779  AddressUser instructionExists(rose_addr_t startVa) const /*final*/ {
780  return aum_.findInstruction(startVa);
781  }
783  return aum_.findInstruction(insn);
784  }
793  ControlFlowGraph::ConstVertexIterator instructionVertex(rose_addr_t insnVa) const;
794 
803  std::vector<SgAsmInstruction*> instructionsOverlapping(const AddressInterval&) const /*final*/;
804 
813  std::vector<SgAsmInstruction*> instructionsSpanning(const AddressInterval&) const /*final*/;
814 
824  std::vector<SgAsmInstruction*> instructionsContainedIn(const AddressInterval&) const /*final*/;
825 
833  AddressInterval instructionExtent(SgAsmInstruction*) const /*final*/;
834 
847  SgAsmInstruction* discoverInstruction(rose_addr_t startVa) const /*final*/;
848 
853  CrossReferences instructionCrossReferences(const AddressIntervalSet &restriction) const /*final*/;
854 
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*/;
1124  ControlFlowGraph::VertexIterator truncateBasicBlock(const ControlFlowGraph::ConstVertexIterator &basicBlock,
1125  SgAsmInstruction *insn) /*final*/;
1126 
1159  void attachBasicBlock(const BasicBlock::Ptr&) /*final*/;
1160  void attachBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder, const BasicBlock::Ptr&) /*final*/;
1248  BasicBlock::Ptr discoverBasicBlock(rose_addr_t startVa) const /*final*/;
1249  BasicBlock::Ptr discoverBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder) const /*final*/;
1266  BasicBlock::Successors basicBlockSuccessors(const BasicBlock::Ptr&,
1267  Precision::Level precision = Precision::HIGH) const /*final*/;
1268 
1277  std::vector<rose_addr_t> basicBlockConcreteSuccessors(const BasicBlock::Ptr&, bool *isComplete=NULL) const /*final*/;
1278 
1297  std::set<rose_addr_t> basicBlockGhostSuccessors(const BasicBlock::Ptr&) const /*final*/;
1298 
1308  bool basicBlockIsFunctionCall(const BasicBlock::Ptr&, Precision::Level precision = Precision::HIGH) const /*final*/;
1309 
1320  bool basicBlockIsFunctionReturn(const BasicBlock::Ptr&) const /*final*/;
1321 
1326  bool basicBlockPopsStack(const BasicBlock::Ptr&) const /*final*/;
1327 
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  for (const typename Container::value_type& bblock: bblocks) {
1845  for (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_; }
2182  std::vector<Function::Ptr> nextFunctionPrologue(rose_addr_t startVa) /*final*/;
2183  std::vector<Function::Ptr> nextFunctionPrologue(rose_addr_t startVa, rose_addr_t &lastSearchedVa /*out*/) /*final*/;
2186 public:
2192  FunctionPaddingMatchers& functionPaddingMatchers() /*final*/ { return functionPaddingMatchers_; }
2193  const FunctionPaddingMatchers& functionPaddingMatchers() const /*final*/ { return functionPaddingMatchers_; }
2203  DataBlock::Ptr matchFunctionPadding(const Function::Ptr&) /*final*/;
2204 
2205 
2206 
2209  //
2210  // Partitioner miscellaneous
2211  //
2214 public:
2226  void dumpCfg(std::ostream&, const std::string &prefix="", bool showBlocks=true,
2227  bool computeProperties=true) const /*final*/;
2228 
2242  void cfgGraphViz(std::ostream&, const AddressInterval &restrict = AddressInterval::whole(),
2243  bool showNeighbors=true) const /*final*/;
2244 
2250  static std::string vertexName(const ControlFlowGraph::Vertex&) /*final*/;
2251  std::string vertexName(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
2257  static std::string vertexNameEnd(const ControlFlowGraph::Vertex&) /*final*/;
2258 
2264  static std::string edgeNameSrc(const ControlFlowGraph::Edge&) /*final*/;
2265  std::string edgeNameSrc(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2273  static std::string edgeNameDst(const ControlFlowGraph::Edge&) /*final*/;
2274  std::string edgeNameDst(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2282  static std::string edgeName(const ControlFlowGraph::Edge&) /*final*/;
2283  std::string edgeName(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2289  static std::string basicBlockName(const BasicBlock::Ptr&) /*final*/;
2290 
2294  static std::string dataBlockName(const DataBlock::Ptr&) /*final*/;
2295 
2299  static std::string functionName(const Function::Ptr&) /*final*/;
2300 
2305  void expandIndeterminateCalls();
2306 
2324  Progress::Ptr progress() const /*final*/;
2325  void progress(const Progress::Ptr&) /*final*/;
2332  void updateProgress(const std::string &phase, double completion) const;
2333 
2335  void showStatistics() const;
2336 
2337  // Checks consistency of internal data structures when debugging is enable (when NDEBUG is not defined).
2338  void checkConsistency() const;
2339 
2346 
2350  Sawyer::Optional<rose_addr_t> elfGotVa() const;
2351 
2352 
2353 
2356  //
2357  // Settings
2358  //
2361 public:
2362 
2370  const BasePartitionerSettings& settings() const /*final*/ { return settings_; }
2371  void settings(const BasePartitionerSettings &s) /*final*/ { settings_ = s; }
2382  void enableSymbolicSemantics(bool b=true) /*final*/ { settings_.usingSemantics = b; }
2383  void disableSymbolicSemantics() /*final*/ { settings_.usingSemantics = false; }
2384  bool usingSymbolicSemantics() const /*final*/ { return settings_.usingSemantics; }
2408  void autoAddCallReturnEdges(bool b) /*final*/ { autoAddCallReturnEdges_ = b; }
2409  bool autoAddCallReturnEdges() const /*final*/ { return autoAddCallReturnEdges_; }
2426  void assumeFunctionsReturn(bool b) /*final*/ { assumeFunctionsReturn_ = b; }
2427  bool assumeFunctionsReturn() const /*final*/ { return assumeFunctionsReturn_; }
2439  void addressName(rose_addr_t, const std::string&) /*final*/;
2440  const std::string& addressName(rose_addr_t va) const /*final*/ { return addressNames_.getOrDefault(va); }
2441  const AddressNameMap& addressNames() const /*final*/ { return addressNames_; }
2449  const SourceLocations& sourceLocations() const /*final*/ { return sourceLocations_; }
2450  SourceLocations& sourceLocations() /*final*/ { return sourceLocations_; }
2451  void sourceLocations(const SourceLocations &locs) { sourceLocations_ = locs; }
2461  bool checkingCallBranch() const /*final*/ { return settings_.checkingCallBranch; }
2462  void checkingCallBranch(bool b) /*final*/ { settings_.checkingCallBranch = b; }
2467  //
2470  // Instruction semantics
2471  //
2474 public:
2484  SemanticMemoryParadigm semanticMemoryParadigm() const { return semanticMemoryParadigm_; }
2485  void semanticMemoryParadigm(SemanticMemoryParadigm p) { semanticMemoryParadigm_ = p; }
2494  SmtSolverPtr smtSolver() const /*final*/ { return solver_; }
2495 
2506  BaseSemantics::RiscOperatorsPtr newOperators() const /*final*/;
2507  BaseSemantics::RiscOperatorsPtr newOperators(SemanticMemoryParadigm) const /*final*/;
2517  BaseSemantics::DispatcherPtr newDispatcher(const BaseSemantics::RiscOperatorsPtr&) const /*final*/;
2518 
2519 
2520 
2523  //
2524  // Python API support functions
2525  //
2528 #ifdef ROSE_ENABLE_PYTHON_API
2529  void pythonUnparse() const;
2530 #endif
2531 
2532 
2533 
2536  //
2537  // Partitioner internal utilities
2538  //
2541 private:
2542  void init(Disassembler*, const MemoryMap::Ptr&);
2543  void init(const Partitioner&);
2544  void updateCfgProgress();
2545 
2546 private:
2547  // Convert a CFG vertex iterator from one partitioner to another. This is called during copy construction when the source
2548  // and destination CFGs are identical.
2549  ControlFlowGraph::VertexIterator convertFrom(const Partitioner &other,
2550  ControlFlowGraph::ConstVertexIterator otherIter);
2551 
2552  // Adjusts edges for a placeholder vertex. This method erases all outgoing edges for the specified placeholder vertex and
2553  // then inserts a single edge from the placeholder to the special "undiscovered" vertex. */
2554  ControlFlowGraph::EdgeIterator adjustPlaceholderEdges(const ControlFlowGraph::VertexIterator &placeholder);
2555 
2556  // Adjusts edges for a non-existing basic block. This method erases all outgoing edges for the specified vertex and
2557  // then inserts a single edge from the vertex to the special "non-existing" vertex. */
2558  ControlFlowGraph::EdgeIterator adjustNonexistingEdges(const ControlFlowGraph::VertexIterator &vertex);
2559 
2560  // Implementation for the discoverBasicBlock methods. The startVa must not be the address of an existing placeholder.
2561  BasicBlock::Ptr discoverBasicBlockInternal(rose_addr_t startVa) const;
2562 
2563  // This method is called whenever a new placeholder is inserted into the CFG or a new basic block is attached to the
2564  // CFG/AUM. The call happens immediately after the CFG/AUM are updated.
2565  void bblockAttached(const ControlFlowGraph::VertexIterator &newVertex);
2566 
2567  // This method is called whenever a basic block is detached from the CFG/AUM or when a placeholder is erased from the CFG.
2568  // The call happens immediately after the CFG/AUM are updated.
2569  void bblockDetached(rose_addr_t startVa, const BasicBlock::Ptr &removedBlock);
2570 
2571  // Rebuild the vertexIndex_ and other cache-like data members from the control flow graph
2572  void rebuildVertexIndices();
2573 };
2574 
2575 } // namespace
2576 } // namespace
2577 } // namespace
2578 
2579 // Class versions must be at global scope
2580 BOOST_CLASS_VERSION(Rose::BinaryAnalysis::Partitioner2::Partitioner, 2);
2581 
2582 #endif
2583 #endif
Represents information about a thunk.
Definition: Partitioner.h:301
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:296
const FunctionPrologueMatchers & functionPrologueMatchers() const
Ordered list of function prologue matchers.
Definition: Partitioner.h:2155
Contiguous region of a file.
SmtSolverPtr smtSolver() const
SMT solver.
Definition: Partitioner.h:2494
BasicBlock::Ptr bblock
The one and only basic block for the thunk.
Definition: Partitioner.h:302
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:2440
void disableSymbolicSemantics()
Use or not use symbolic semantics.
Definition: Partitioner.h:2383
void checkingCallBranch(bool b)
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2462
Represents the file header of an ELF binary container.
ControlFlowGraph::ConstVertexIterator indeterminateVertex() const
Returns the special "indeterminate" vertex.
Definition: Partitioner.h:621
const SourceLocations & sourceLocations() const
Property: Source locations.
Definition: Partitioner.h:2449
const Configuration & configuration() const
Configuration information.
Definition: Partitioner.h:484
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:2193
AddressUser instructionExists(SgAsmInstruction *insn) const
Determines whether an instruction is attached to the CFG/AUM.
Definition: Partitioner.h:782
const ControlFlowGraph & cfg() const
Returns the control flow graph.
Definition: Partitioner.h:649
Provides and caches instructions.
STL namespace.
ControlFlowGraph::VertexIterator indeterminateVertex()
Returns the special "indeterminate" vertex.
Definition: Partitioner.h:618
InstructionProvider & instructionProvider()
Returns the instruction provider.
Definition: Partitioner.h:492
void sourceLocations(const SourceLocations &locs)
Property: Source locations.
Definition: Partitioner.h:2451
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:308
BasicBlockCallbacks & basicBlockCallbacks()
Callbacks for adjusting basic block during discovery.
Definition: Partitioner.h:2142
ControlFlowGraph::VertexIterator undiscoveredVertex()
Returns the special "undiscovered" vertex.
Definition: Partitioner.h:600
SemanticMemoryParadigm semanticMemoryParadigm() const
Property: Whether to use map- or list-based memory states.
Definition: Partitioner.h:2484
Name space for the entire library.
Definition: FeasiblePath.h:785
Configuration & configuration()
Configuration information.
Definition: Partitioner.h:483
bool assumeFunctionsReturn() const
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2427
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:657
const AddressNameMap & addressNames() const
Property: Name for address.
Definition: Partitioner.h:2441
Optional< Value > getOptional(const Key &key) const
Lookup and return a value or nothing.
Definition: HashMap.h:434
Base classes for instruction semantics.
Definition: Dispatcher.h:18
void settings(const BasePartitionerSettings &s)
Partitioner settings.
Definition: Partitioner.h:2371
void assumeFunctionsReturn(bool b)
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2426
rose_addr_t target
The one and only successor for the basic block.
Definition: Partitioner.h:303
ControlFlowGraph::VertexIterator nonexistingVertex()
Returns the special "non-existing" vertex.
Definition: Partitioner.h:635
bool isDefaultConstructed() const
Return true if this is a default constructed partitioner.
Definition: Partitioner.h:471
bool usingSymbolicSemantics() const
Use or not use symbolic semantics.
Definition: Partitioner.h:2384
SemanticMemoryParadigm
Organization of semantic memory.
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:493
bool checkingCallBranch
Check for situations where CALL is used as a branch.
bool usingSemantics
Whether instruction semantics are used.
A general, thread-safe way to report progress made on some task.
void autoAddCallReturnEdges(bool b)
Property: Insert (or not) function call return edges.
Definition: Partitioner.h:2408
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.
ControlFlowGraph::ConstVertexIterator undiscoveredVertex() const
Returns the special "undiscovered" vertex.
Definition: Partitioner.h:603
ControlFlowGraph::ConstVertexIterator nonexistingVertex() const
Returns the special "non-existing" vertex.
Definition: Partitioner.h:638
std::vector< FunctionPrologueMatcher::Ptr > FunctionPrologueMatchers
See functionPrologueMatchers.
Definition: Partitioner.h:297
FunctionPaddingMatchers & functionPaddingMatchers()
Ordered list of function padding matchers.
Definition: Partitioner.h:2192
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:505
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:289
bool checkingCallBranch() const
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2461
bool addressIsExecutable(rose_addr_t va) const
Returns true if address is executable.
Definition: Partitioner.h:511
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:2382
bool autoAddCallReturnEdges() const
Property: Insert (or not) function call return edges.
Definition: Partitioner.h:2409
Sawyer::Callbacks< CfgAdjustmentCallback::Ptr > CfgAdjustmentCallbacks
See cfgAdjustmentCallbacks.
Definition: Partitioner.h:295
std::vector< FunctionPaddingMatcher::Ptr > FunctionPaddingMatchers
See functionPaddingMatchers.
Definition: Partitioner.h:298
void semanticMemoryParadigm(SemanticMemoryParadigm p)
Property: Whether to use map- or list-based memory states.
Definition: Partitioner.h:2485
std::shared_ptr< class SmtSolver > SmtSolverPtr
Reference-counting pointer for SMT solvers.
Definition: SmtSolver.h:26
Holds configuration information.
Definition: Config.h:281
SourceLocations & sourceLocations()
Property: Source locations.
Definition: Partitioner.h:2450