ROSE  0.9.10.164
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/serialization/access.hpp>
30 #include <boost/serialization/split_member.hpp>
31 
32 #include <ostream>
33 #include <set>
34 #include <string>
35 #include <vector>
36 
37 // Derived classes needed for serialization
38 #include <BinaryYicesSolver.h>
39 #include <BinaryZ3Solver.h>
40 #include <DispatcherM68k.h>
41 #include <DispatcherPowerpc.h>
42 #include <DispatcherX86.h>
43 
44 namespace Rose {
45 namespace BinaryAnalysis {
46 
78 namespace Partitioner2 {
79 
293 class ROSE_DLL_API Partitioner: public Sawyer::Attribute::Storage<> { // final
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  SemanticMemoryParadigm semanticMemoryParadigm_; // Slow and precise, or fast and imprecise?
325  Unparser::BasePtr unparser_; // For unparsing things to pseudo-assembly
326  Unparser::BasePtr insnUnparser_; // For unparsing single instructions in diagnostics
327 
328  // Callback lists
329  CfgAdjustmentCallbacks cfgAdjustmentCallbacks_;
330  BasicBlockCallbacks basicBlockCallbacks_;
331  FunctionPrologueMatchers functionPrologueMatchers_;
332  FunctionPaddingMatchers functionPaddingMatchers_;
333 
334  // Special CFG vertices.
335  ControlFlowGraph::VertexIterator undiscoveredVertex_;
336  ControlFlowGraph::VertexIterator indeterminateVertex_;
337  ControlFlowGraph::VertexIterator nonexistingVertex_;
338  static const size_t nSpecialVertices = 3;
339 
340  // Protects the following data members
341  mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
342  Progress::Ptr progress_; // Progress reporter to update, or null
343  mutable size_t cfgProgressTotal_; // Expected total for the CFG progress bar; initialized at first report
344 
345 
348  //
349  // Serialization
350  //
353 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
354 private:
355  friend class boost::serialization::access;
356 
357  template<class S>
358  void serializeCommon(S &s, const unsigned /*version*/) {
359  s.template register_type<InstructionSemantics2::SymbolicSemantics::SValue>();
360  s.template register_type<InstructionSemantics2::SymbolicSemantics::RiscOperators>();
361  s.template register_type<InstructionSemantics2::DispatcherX86>();
362  s.template register_type<InstructionSemantics2::DispatcherM68k>();
363  s.template register_type<InstructionSemantics2::DispatcherPowerpc>();
364  s.template register_type<SymbolicExpr::Interior>();
365  s.template register_type<SymbolicExpr::Leaf>();
366  s.template register_type<YicesSolver>();
367  s.template register_type<Z3Solver>();
368  s.template register_type<Semantics::SValue>();
369  s.template register_type<Semantics::MemoryListState>();
370  s.template register_type<Semantics::MemoryMapState>();
371  s.template register_type<Semantics::RegisterState>();
372  s.template register_type<Semantics::State>();
373  s.template register_type<Semantics::RiscOperators>();
374  s & BOOST_SERIALIZATION_NVP(settings_);
375  // s & config_; -- FIXME[Robb P Matzke 2016-11-08]
376  s & BOOST_SERIALIZATION_NVP(instructionProvider_);
377  s & BOOST_SERIALIZATION_NVP(memoryMap_);
378  s & BOOST_SERIALIZATION_NVP(cfg_);
379  // s & vertexIndex_; -- initialized by rebuildVertexIndices
380  s & BOOST_SERIALIZATION_NVP(aum_);
381  // s & BOOST_SERIALIZATION_NVP(solver_); -- not saved/restored in order to override from command-line
382  s & BOOST_SERIALIZATION_NVP(functions_);
383  s & BOOST_SERIALIZATION_NVP(autoAddCallReturnEdges_);
384  s & BOOST_SERIALIZATION_NVP(assumeFunctionsReturn_);
385  s & BOOST_SERIALIZATION_NVP(stackDeltaInterproceduralLimit_);
386  s & BOOST_SERIALIZATION_NVP(addressNames_);
387  s & BOOST_SERIALIZATION_NVP(semanticMemoryParadigm_);
388  // s & unparser_; -- not saved; restored from disassembler
389  // s & cfgAdjustmentCallbacks_; -- not saved/restored
390  // s & basicBlockCallbacks_; -- not saved/restored
391  // s & functionPrologueMatchers_; -- not saved/restored
392  // s & functionPaddingMatchers_; -- not saved/restored
393  // s & undiscoveredVertex_; -- initialized by rebuildVertexIndices
394  // s & indeterminateVertex_; -- initialized by rebuildVertexIndices
395  // s & nonexistingVertex_; -- initialized by rebuildVertexIndices
396  // s & progress_; -- not saved/restored
397  // s & cfgProgressTotal_; -- not saved/restored
398  }
399 
400  template<class S>
401  void save(S &s, const unsigned version) const {
402  const_cast<Partitioner*>(this)->serializeCommon(s, version);
403  }
404 
405  template<class S>
406  void load(S &s, const unsigned version) {
407  serializeCommon(s, version);
408  rebuildVertexIndices();
409  }
410 
411  BOOST_SERIALIZATION_SPLIT_MEMBER();
412 #endif
413 
414 
417  //
418  // Constructors
419  //
422 public:
427  Partitioner();
428 
433  Partitioner(Disassembler *disassembler, const MemoryMap::Ptr &map);
434 
435  // FIXME[Robb P. Matzke 2014-11-08]: This is not ready for use yet. The problem is that because of the shallow copy, both
436  // partitioners are pointing to the same basic blocks, data blocks, and functions. This is okay by itself since these
437  // things are reference counted, but the paradigm of locked/unlocked blocks and functions breaks down somewhat -- does
438  // unlocking a basic block from one partitioner make it modifiable even though it's still locked in the other partitioner?
439  // FIXME[Robb P. Matzke 2014-12-27]: Not the most efficient implementation, but saves on cut-n-paste which would surely rot
440  // after a while.
441  Partitioner(const Partitioner &other);
442  Partitioner& operator=(const Partitioner &other);
443 
444  ~Partitioner();
445 
452  bool isDefaultConstructed() const { return instructionProvider_ == NULL; }
453 
455  void clear() /*final*/;
456 
464  Configuration& configuration() { return config_; }
465  const Configuration& configuration() const { return config_; }
473  InstructionProvider& instructionProvider() /*final*/ { return *instructionProvider_; }
474  const InstructionProvider& instructionProvider() const /*final*/ { return *instructionProvider_; }
486  MemoryMap::Ptr memoryMap() const /*final*/ { return memoryMap_; }
492  bool addressIsExecutable(rose_addr_t va) const /*final*/ {
493  return memoryMap_!=NULL && memoryMap_->at(va).require(MemoryMap::EXECUTABLE).exists();
494  }
495 
498  //
499  // Unparsing
500  //
503 
513  Unparser::BasePtr unparser() const /*final*/;
514  void unparser(const Unparser::BasePtr&) /*final*/;
526  Unparser::BasePtr insnUnparser() const /*final*/;
527  void insnUnparser(const Unparser::BasePtr&) /*final*/;
538  std::string unparse(SgAsmInstruction*) const;
539  void unparse(std::ostream&, SgAsmInstruction*) const;
540  void unparse(std::ostream&, const BasicBlock::Ptr&) const;
541  void unparse(std::ostream&, const DataBlock::Ptr&) const;
542  void unparse(std::ostream&, const Function::Ptr&) const;
543  void unparse(std::ostream&) const;
546  //
549  // Partitioner CFG queries
550  //
553 public:
559  size_t nBytes() const /*final*/ { return aum_.size(); }
560 
568  ControlFlowGraph::VertexIterator undiscoveredVertex() /*final*/ {
569  return undiscoveredVertex_;
570  }
571  ControlFlowGraph::ConstVertexIterator undiscoveredVertex() const /*final*/ {
572  return undiscoveredVertex_;
573  }
586  ControlFlowGraph::VertexIterator indeterminateVertex() /*final*/ {
587  return indeterminateVertex_;
588  }
589  ControlFlowGraph::ConstVertexIterator indeterminateVertex() const /*final*/ {
590  return indeterminateVertex_;
591  }
603  ControlFlowGraph::VertexIterator nonexistingVertex() /*final*/ {
604  return nonexistingVertex_;
605  }
606  ControlFlowGraph::ConstVertexIterator nonexistingVertex() const /*final*/ {
607  return nonexistingVertex_;
608  }
617  const ControlFlowGraph& cfg() const /*final*/ { return cfg_; }
618 
625  const AddressUsageMap& aum() const /*final*/ { return aum_; }
626 
628  AddressUsageMap aum(const Function::Ptr&) const /*final*/;
629 
637  std::set<rose_addr_t> ghostSuccessors() const /*final*/;
638 
667  bool isEdgeIntraProcedural(ControlFlowGraph::ConstEdgeIterator edge,
668  const Function::Ptr &function = Function::Ptr()) const /*final*/;
669  bool isEdgeIntraProcedural(const ControlFlowGraph::Edge &edge,
670  const Function::Ptr &function = Function::Ptr()) const /*final*/;
707  bool isEdgeInterProcedural(ControlFlowGraph::ConstEdgeIterator edge,
708  const Function::Ptr &sourceFunction = Function::Ptr(),
709  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
710  bool isEdgeInterProcedural(const ControlFlowGraph::Edge &edge,
711  const Function::Ptr &sourceFunction = Function::Ptr(),
712  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
717  //
720  // Partitioner instruction operations
721  //
724 public:
730  size_t nInstructions() const /*final*/;
731 
741  Sawyer::Optional<AddressUser> instructionExists(rose_addr_t startVa) const /*final*/ {
742  return aum_.instructionExists(startVa);
743  }
745  return insn==NULL ? Sawyer::Nothing() : instructionExists(insn->get_address());
746  }
755  ControlFlowGraph::ConstVertexIterator instructionVertex(rose_addr_t insnVa) const;
756 
765  std::vector<SgAsmInstruction*> instructionsOverlapping(const AddressInterval&) const /*final*/;
766 
775  std::vector<SgAsmInstruction*> instructionsSpanning(const AddressInterval&) const /*final*/;
776 
786  std::vector<SgAsmInstruction*> instructionsContainedIn(const AddressInterval&) const /*final*/;
787 
795  AddressInterval instructionExtent(SgAsmInstruction*) const /*final*/;
796 
809  SgAsmInstruction* discoverInstruction(rose_addr_t startVa) const /*final*/;
810 
815  CrossReferences instructionCrossReferences(const AddressIntervalSet &restriction) const /*final*/;
816 
817 
820  //
821  // Partitioner basic block placeholder operations
822  //
825 public:
835  size_t nPlaceholders() const /*final*/;
836 
845  bool placeholderExists(rose_addr_t startVa) const /*final*/;
846 
857  ControlFlowGraph::VertexIterator findPlaceholder(rose_addr_t startVa) /*final*/ {
858  if (Sawyer::Optional<ControlFlowGraph::VertexIterator> found = vertexIndex_.getOptional(startVa))
859  return *found;
860  return cfg_.vertices().end();
861  }
862  ControlFlowGraph::ConstVertexIterator findPlaceholder(rose_addr_t startVa) const /*final*/ {
863  if (Sawyer::Optional<ControlFlowGraph::VertexIterator> found = vertexIndex_.getOptional(startVa))
864  return *found;
865  return cfg_.vertices().end();
866  }
885  ControlFlowGraph::VertexIterator insertPlaceholder(rose_addr_t startVa) /*final*/;
886 
900  BasicBlock::Ptr erasePlaceholder(const ControlFlowGraph::ConstVertexIterator &placeholder) /*final*/;
901  BasicBlock::Ptr erasePlaceholder(rose_addr_t startVa) /*final*/;
906  //
909  // Partitioner basic block operations
910  //
913 public:
927  bool basicBlockSemanticsAutoDrop() const /*final*/ { return settings_.basicBlockSemanticsAutoDrop; }
939  void basicBlockDropSemantics() const /*final*/;
940 
949  size_t nBasicBlocks() const /*final*/;
950 
958  std::vector<BasicBlock::Ptr> basicBlocks() const /*final*/;
959 
981  BasicBlock::Ptr basicBlockExists(rose_addr_t startVa) const /*final*/;
982  BasicBlock::Ptr basicBlockExists(const BasicBlock::Ptr&) const /*final*/;
994  std::vector<BasicBlock::Ptr> basicBlocksOverlapping(const AddressInterval&) const /*final*/;
995 
1006  std::vector<BasicBlock::Ptr> basicBlocksSpanning(const AddressInterval&) const /*final*/;
1007 
1017  std::vector<BasicBlock::Ptr> basicBlocksContainedIn(const AddressInterval&) const /*final*/;
1018 
1025  BasicBlock::Ptr basicBlockContainingInstruction(rose_addr_t insnVa) const /*final*/;
1026 
1036  AddressIntervalSet basicBlockInstructionExtent(const BasicBlock::Ptr&) const /*final*/;
1037 
1043  AddressIntervalSet basicBlockDataExtent(const BasicBlock::Ptr&) const /*final*/;
1044 
1069  BasicBlock::Ptr detachBasicBlock(rose_addr_t startVa) /*final*/;
1070  BasicBlock::Ptr detachBasicBlock(const BasicBlock::Ptr &basicBlock) /*final*/;
1071  BasicBlock::Ptr detachBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder) /*final*/;
1086  ControlFlowGraph::VertexIterator truncateBasicBlock(const ControlFlowGraph::ConstVertexIterator &basicBlock,
1087  SgAsmInstruction *insn) /*final*/;
1088 
1119  void attachBasicBlock(const BasicBlock::Ptr&) /*final*/;
1120  void attachBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder, const BasicBlock::Ptr&) /*final*/;
1208  BasicBlock::Ptr discoverBasicBlock(rose_addr_t startVa) const /*final*/;
1209  BasicBlock::Ptr discoverBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder) const /*final*/;
1226  BasicBlock::Successors basicBlockSuccessors(const BasicBlock::Ptr&,
1227  Precision::Level precision = Precision::HIGH) const /*final*/;
1228 
1237  std::vector<rose_addr_t> basicBlockConcreteSuccessors(const BasicBlock::Ptr&, bool *isComplete=NULL) const /*final*/;
1238 
1257  std::set<rose_addr_t> basicBlockGhostSuccessors(const BasicBlock::Ptr&) const /*final*/;
1258 
1268  bool basicBlockIsFunctionCall(const BasicBlock::Ptr&, Precision::Level precision = Precision::HIGH) const /*final*/;
1269 
1280  bool basicBlockIsFunctionReturn(const BasicBlock::Ptr&) const /*final*/;
1281 
1286  bool basicBlockPopsStack(const BasicBlock::Ptr&) const /*final*/;
1287 
1327  BaseSemantics::SValuePtr basicBlockStackDeltaIn(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1328  BaseSemantics::SValuePtr basicBlockStackDeltaOut(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1339  void forgetStackDeltas() const /*final*/;
1340  void forgetStackDeltas(const Function::Ptr&) const /*final*/;
1352  size_t stackDeltaInterproceduralLimit() const /*final*/ { return stackDeltaInterproceduralLimit_; }
1353  void stackDeltaInterproceduralLimit(size_t n) /*final*/ { stackDeltaInterproceduralLimit_ = std::max(size_t(1), n); }
1406  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const BasicBlock::Ptr&) const /*final*/;
1407 
1408  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
1417  void basicBlockMayReturnReset() const /*final*/;
1418 
1419 private:
1420  // Per-vertex data used during may-return analysis
1421  struct MayReturnVertexInfo {
1422  enum State {INIT, CALCULATING, FINISHED};
1423  State state; // current state of vertex
1424  bool processedCallees; // have we processed BBs this vertex calls?
1425  boost::logic::tribool anyCalleesReturn; // do any of those called BBs have a true may-return value?
1426  boost::logic::tribool result; // final result (eventually cached in BB)
1427  MayReturnVertexInfo(): state(INIT), processedCallees(false), anyCalleesReturn(false), result(boost::indeterminate) {}
1428  };
1429 
1430  // Is edge significant for analysis? See .C file for full documentation.
1431  bool mayReturnIsSignificantEdge(const ControlFlowGraph::ConstEdgeIterator &edge,
1432  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1433 
1434  // Determine (and cache in vertexInfo) whether any callees return.
1435  boost::logic::tribool mayReturnDoesCalleeReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1436  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1437 
1438  // Maximum may-return result from significant successors including phantom call-return edge.
1439  boost::logic::tribool mayReturnDoesSuccessorReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1440  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1441 
1442  // The guts of the may-return analysis
1443  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator &start,
1444  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1445 
1446 
1447 
1450  //
1451  // Partitioner data block operations
1452  //
1455 public:
1461  size_t nDataBlocks() const /*final*/;
1462 
1468  bool dataBlockExists(const DataBlock::Ptr&) const /*final*/;
1469 
1478  DataBlock::Ptr findBestDataBlock(const AddressInterval&) const /*final*/;
1479 
1488  void attachDataBlock(const DataBlock::Ptr&) /*final*/;
1489 
1496  DataBlock::Ptr detachDataBlock(const DataBlock::Ptr&) /*final*/;
1497 
1507  std::vector<DataBlock::Ptr> dataBlocksOverlapping(const AddressInterval&) const /*final*/;
1508 
1518  std::vector<DataBlock::Ptr> dataBlocksSpanning(const AddressInterval&) const /*final*/;
1519 
1529  std::vector<DataBlock::Ptr> dataBlocksContainedIn(const AddressInterval&) const /*final*/;
1530 
1537  AddressInterval dataBlockExtent(const DataBlock::Ptr&) const /*final*/;
1538 
1544  std::vector<DataBlock::Ptr> dataBlocks() const /*final*/;
1545 
1546 
1547 
1550  //
1551  // Partitioner function operations
1552  //
1555 public:
1561  size_t nFunctions() const /*final*/ { return functions_.size(); }
1562 
1581  Function::Ptr functionExists(rose_addr_t entryVa) const /*final*/;
1582  Function::Ptr functionExists(const BasicBlock::Ptr &entryBlock) const /*final*/;
1583  Function::Ptr functionExists(const Function::Ptr &function) const /*final*/;
1591  std::vector<Function::Ptr> functions() const /*final*/;
1592 
1602  std::vector<Function::Ptr> functionsOverlapping(const AddressInterval&) const /*final*/;
1603 
1614  std::vector<Function::Ptr> functionsSpanning(const AddressInterval&) const /*final*/;
1615 
1625  std::vector<Function::Ptr> functionsContainedIn(const AddressInterval&) const /*final*/;
1626 
1643  AddressIntervalSet functionExtent(const Function::Ptr&) const /*final*/;
1644  void functionExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1645  AddressIntervalSet functionBasicBlockExtent(const Function::Ptr &function) const /*final*/;
1646  void functionBasicBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1647  AddressIntervalSet functionDataBlockExtent(const Function::Ptr &function) const /*final*/;
1648  void functionDataBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1670  size_t attachFunction(const Function::Ptr&) /*final*/;
1671  size_t attachFunctions(const Functions&) /*final*/;
1687  Function::Ptr attachOrMergeFunction(const Function::Ptr&) /*final*/;
1688 
1705  size_t attachFunctionBasicBlocks(const Functions&) /*final*/;
1706  size_t attachFunctionBasicBlocks(const Function::Ptr&) /*final*/;
1722  void detachFunction(const Function::Ptr&) /*final*/;
1723 
1734  DataBlock::Ptr attachFunctionDataBlock(const Function::Ptr&, rose_addr_t startVa, size_t nBytes) /*final*/;
1735 
1741  void attachFunctionDataBlock(const Function::Ptr&, const DataBlock::Ptr&) /*final*/;
1742 
1777  std::vector<Function::Ptr>
1778  functionsOwningBasicBlock(const ControlFlowGraph::Vertex&, bool doSort = true) const /*final*/;
1779 
1780  std::vector<Function::Ptr>
1781  functionsOwningBasicBlock(const ControlFlowGraph::ConstVertexIterator&, bool doSort = true) const /*final*/;
1782 
1783  std::vector<Function::Ptr>
1784  functionsOwningBasicBlock(rose_addr_t bblockVa, bool doSort = true) const /*final*/;
1785 
1786  std::vector<Function::Ptr>
1787  functionsOwningBasicBlock(const BasicBlock::Ptr&, bool doSort = true) const /*final*/;
1788 
1789  template<class Container> // container can hold any type accepted by functionsOwningBasicBlock
1790  std::vector<Function::Ptr>
1791  functionsOwningBasicBlocks(const Container &bblocks) const /*final*/ {
1792  std::vector<Function::Ptr> retval;
1793  BOOST_FOREACH (const typename Container::value_type& bblock, bblocks) {
1794  BOOST_FOREACH (const Function::Ptr &function, functionsOwningBasicBlock(bblock, false))
1795  insertUnique(retval, function, sortFunctionsByAddress);
1796  }
1797  return retval;
1798  }
1810  std::vector<Function::Ptr> discoverCalledFunctions() const /*final*/;
1811 
1823  std::vector<Function::Ptr> discoverFunctionEntryVertices() const /*final*/;
1824 
1834  Sawyer::Optional<Thunk> functionIsThunk(const Function::Ptr&) const /*final*/;
1835 
1846  void discoverFunctionBasicBlocks(const Function::Ptr &function) const /*final*/;
1847 
1854  std::set<rose_addr_t> functionGhostSuccessors(const Function::Ptr&) const /*final*/;
1855 
1863  FunctionCallGraph functionCallGraph(AllowParallelEdges::Type allowParallelEdges) const /*final*/;
1864 
1884  BaseSemantics::SValuePtr functionStackDelta(const Function::Ptr &function) const /*final*/;
1885 
1889  void allFunctionStackDelta() const /*final*/;
1890 
1897  Sawyer::Optional<bool> functionOptionalMayReturn(const Function::Ptr &function) const /*final*/;
1898 
1902  void allFunctionMayReturn() const /*final*/;
1903 
1930  const CallingConvention::Analysis&
1931  functionCallingConvention(const Function::Ptr&,
1932  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1933  const /*final*/;
1934 
1947  void
1948  allFunctionCallingConvention(const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1949  const /*final*/;
1950 
1976  CallingConvention::Dictionary
1977  functionCallingConventionDefinitions(const Function::Ptr&,
1978  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1979  const /*final*/;
1980 
1993  void
1994  allFunctionCallingConventionDefinition(const CallingConvention::Definition::Ptr &dflt =
1995  CallingConvention::Definition::Ptr()) const /*final*/;
1996 
2005  void fixInterFunctionEdges() /*final*/;
2006  void fixInterFunctionEdge(const ControlFlowGraph::ConstEdgeIterator&) /*final*/;
2026  bool functionIsNoop(const Function::Ptr&) const /*final*/;
2027 
2033  void allFunctionIsNoop() const /*final*/;
2034 
2042  void forgetFunctionIsNoop() const /*final*/;
2043  void forgetFunctionIsNoop(const Function::Ptr&) const /*final*/;
2048  //
2051  // Callbacks
2052  //
2055 public:
2073  CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() /*final*/ { return cfgAdjustmentCallbacks_; }
2074  const CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() const /*final*/ { return cfgAdjustmentCallbacks_; }
2085  BasicBlockCallbacks& basicBlockCallbacks() /*final*/ { return basicBlockCallbacks_; }
2086  const BasicBlockCallbacks& basicBlockCallbacks() const /*final*/ { return basicBlockCallbacks_; }
2089 public:
2097  FunctionPrologueMatchers& functionPrologueMatchers() /*final*/ { return functionPrologueMatchers_; }
2098  const FunctionPrologueMatchers& functionPrologueMatchers() const /*final*/ { return functionPrologueMatchers_; }
2120  std::vector<Function::Ptr> nextFunctionPrologue(rose_addr_t startVa) /*final*/;
2121 
2122 public:
2128  FunctionPaddingMatchers& functionPaddingMatchers() /*final*/ { return functionPaddingMatchers_; }
2129  const FunctionPaddingMatchers& functionPaddingMatchers() const /*final*/ { return functionPaddingMatchers_; }
2139  DataBlock::Ptr matchFunctionPadding(const Function::Ptr&) /*final*/;
2140 
2141 
2142 
2145  //
2146  // Partitioner miscellaneous
2147  //
2150 public:
2162  void dumpCfg(std::ostream&, const std::string &prefix="", bool showBlocks=true,
2163  bool computeProperties=true) const /*final*/;
2164 
2178  void cfgGraphViz(std::ostream&, const AddressInterval &restrict = AddressInterval::whole(),
2179  bool showNeighbors=true) const /*final*/;
2180 
2186  static std::string vertexName(const ControlFlowGraph::Vertex&) /*final*/;
2187  std::string vertexName(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
2193  static std::string vertexNameEnd(const ControlFlowGraph::Vertex&) /*final*/;
2194 
2200  static std::string edgeNameSrc(const ControlFlowGraph::Edge&) /*final*/;
2201  std::string edgeNameSrc(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2209  static std::string edgeNameDst(const ControlFlowGraph::Edge&) /*final*/;
2210  std::string edgeNameDst(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2218  static std::string edgeName(const ControlFlowGraph::Edge&) /*final*/;
2219  std::string edgeName(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2225  static std::string basicBlockName(const BasicBlock::Ptr&) /*final*/;
2226 
2230  static std::string dataBlockName(const DataBlock::Ptr&) /*final*/;
2231 
2235  static std::string functionName(const Function::Ptr&) /*final*/;
2236 
2254  Progress::Ptr progress() const /*final*/;
2255  void progress(const Progress::Ptr&) /*final*/;
2262  void updateProgress(const std::string &phase, double completion) const;
2263 
2265  void showStatistics() const;
2266 
2268  // Settings
2270 public:
2271 
2279  const BasePartitionerSettings& settings() const /*final*/ { return settings_; }
2280  void settings(const BasePartitionerSettings &s) /*final*/ { settings_ = s; }
2291  void enableSymbolicSemantics(bool b=true) /*final*/ { settings_.usingSemantics = b; }
2292  void disableSymbolicSemantics() /*final*/ { settings_.usingSemantics = false; }
2293  bool usingSymbolicSemantics() const /*final*/ { return settings_.usingSemantics; }
2317  void autoAddCallReturnEdges(bool b) /*final*/ { autoAddCallReturnEdges_ = b; }
2318  bool autoAddCallReturnEdges() const /*final*/ { return autoAddCallReturnEdges_; }
2335  void assumeFunctionsReturn(bool b) /*final*/ { assumeFunctionsReturn_ = b; }
2336  bool assumeFunctionsReturn() const /*final*/ { return assumeFunctionsReturn_; }
2348  void addressName(rose_addr_t, const std::string&) /*final*/;
2349  const std::string& addressName(rose_addr_t va) const /*final*/ { return addressNames_.getOrDefault(va); }
2350  const AddressNameMap& addressNames() const /*final*/ { return addressNames_; }
2360  bool checkingCallBranch() const /*final*/ { return settings_.checkingCallBranch; }
2361  void checkingCallBranch(bool b) /*final*/ { settings_.checkingCallBranch = b; }
2365  //
2368  // Instruction semantics
2369  //
2372 public:
2382  SemanticMemoryParadigm semanticMemoryParadigm() const { return semanticMemoryParadigm_; }
2383  void semanticMemoryParadigm(SemanticMemoryParadigm p) { semanticMemoryParadigm_ = p; }
2392  SmtSolverPtr smtSolver() const /*final*/ { return solver_; }
2393 
2404  BaseSemantics::RiscOperatorsPtr newOperators() const /*final*/;
2405  BaseSemantics::RiscOperatorsPtr newOperators(SemanticMemoryParadigm) const /*final*/;
2415  BaseSemantics::DispatcherPtr newDispatcher(const BaseSemantics::RiscOperatorsPtr&) const /*final*/;
2416 
2417 
2418 
2421  //
2422  // Python API support functions
2423  //
2426 #ifdef ROSE_ENABLE_PYTHON_API
2427  void pythonUnparse() const;
2428 #endif
2429 
2430 
2431 
2434  //
2435  // Partitioner internal utilities
2436  //
2439 private:
2440  void init(Disassembler*, const MemoryMap::Ptr&);
2441  void init(const Partitioner&);
2442  void updateCfgProgress();
2443 
2444 private:
2445  // Convert a CFG vertex iterator from one partitioner to another. This is called during copy construction when the source
2446  // and destination CFGs are identical.
2447  ControlFlowGraph::VertexIterator convertFrom(const Partitioner &other,
2448  ControlFlowGraph::ConstVertexIterator otherIter);
2449 
2450  // Adjusts edges for a placeholder vertex. This method erases all outgoing edges for the specified placeholder vertex and
2451  // then inserts a single edge from the placeholder to the special "undiscovered" vertex. */
2452  ControlFlowGraph::EdgeIterator adjustPlaceholderEdges(const ControlFlowGraph::VertexIterator &placeholder);
2453 
2454  // Adjusts edges for a non-existing basic block. This method erases all outgoing edges for the specified vertex and
2455  // then inserts a single edge from the vertex to the special "non-existing" vertex. */
2456  ControlFlowGraph::EdgeIterator adjustNonexistingEdges(const ControlFlowGraph::VertexIterator &vertex);
2457 
2458  // Implementation for the discoverBasicBlock methods. The startVa must not be the address of an existing placeholder.
2459  BasicBlock::Ptr discoverBasicBlockInternal(rose_addr_t startVa) const;
2460 
2461  // Checks consistency of internal data structures when debugging is enable (when NDEBUG is not defined).
2462  void checkConsistency() const;
2463 
2464  // This method is called whenever a new placeholder is inserted into the CFG or a new basic block is attached to the
2465  // CFG/AUM. The call happens immediately after the CFG/AUM are updated.
2466  void bblockAttached(const ControlFlowGraph::VertexIterator &newVertex);
2467 
2468  // This method is called whenever a basic block is detached from the CFG/AUM or when a placeholder is erased from the CFG.
2469  // The call happens immediately after the CFG/AUM are updated.
2470  void bblockDetached(rose_addr_t startVa, const BasicBlock::Ptr &removedBlock);
2471 
2472  // Rebuild the vertexIndex_ and other cache-like data members from the control flow graph
2473  void rebuildVertexIndices();
2474 };
2475 
2476 } // namespace
2477 } // namespace
2478 } // namespace
2479 
2480 #endif
Represents information about a thunk.
Definition: Partitioner.h:301
size_t size() const
Number of addresses represented by the map.
Sawyer::Callbacks< BasicBlockCallback::Ptr > BasicBlockCallbacks
See basicBlockCallbacks.
Definition: Partitioner.h:296
const FunctionPrologueMatchers & functionPrologueMatchers() const
Ordered list of function prologue matchers.
Definition: Partitioner.h:2098
SmtSolverPtr smtSolver() const
SMT solver.
Definition: Partitioner.h:2392
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:2097
const std::string & addressName(rose_addr_t va) const
Property: Name for address.
Definition: Partitioner.h:2349
void disableSymbolicSemantics()
Use or not use symbolic semantics.
Definition: Partitioner.h:2292
void checkingCallBranch(bool b)
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2361
ControlFlowGraph::ConstVertexIterator indeterminateVertex() const
Returns the special "indeterminate" vertex.
Definition: Partitioner.h:589
const Configuration & configuration() const
Configuration information.
Definition: Partitioner.h:465
Base class for machine instructions.
Sawyer::Optional< AddressUser > instructionExists(SgAsmInstruction *insn) const
Determines whether an instruction is attached to the CFG/AUM.
Definition: Partitioner.h:744
const FunctionPaddingMatchers & functionPaddingMatchers() const
Ordered list of function padding matchers.
Definition: Partitioner.h:2129
const ControlFlowGraph & cfg() const
Returns the control flow graph.
Definition: Partitioner.h:617
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:586
InstructionProvider & instructionProvider()
Returns the instruction provider.
Definition: Partitioner.h:473
boost::iterator_range< VertexIterator > vertices()
Iterators for all vertices.
Definition: Graph.h:1462
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:2085
ControlFlowGraph::VertexIterator undiscoveredVertex()
Returns the special "undiscovered" vertex.
Definition: Partitioner.h:568
SemanticMemoryParadigm semanticMemoryParadigm() const
Property: Whether to use map- or list-based memory states.
Definition: Partitioner.h:2382
Name space for the entire library.
Definition: Access.h:13
Configuration & configuration()
Configuration information.
Definition: Partitioner.h:464
bool assumeFunctionsReturn() const
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2336
ControlFlowGraph::ConstVertexIterator findPlaceholder(rose_addr_t startVa) const
Find the CFG vertex for a basic block placeholder.
Definition: Partitioner.h:862
const AddressUsageMap & aum() const
Returns the address usage map.
Definition: Partitioner.h:625
const AddressNameMap & addressNames() const
Property: Name for address.
Definition: Partitioner.h:2350
Optional< Value > getOptional(const Key &key) const
Lookup and return a value or nothing.
Definition: HashMap.h:434
Base classes for instruction semantics.
void settings(const BasePartitionerSettings &s)
Partitioner settings.
Definition: Partitioner.h:2280
void assumeFunctionsReturn(bool b)
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2335
bool instructionExists(SgAsmInstruction *) const
Determines whether an instruction exists in the map.
rose_addr_t target
The one and only successor for the basic block.
Definition: Partitioner.h:303
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
ControlFlowGraph::VertexIterator nonexistingVertex()
Returns the special "non-existing" vertex.
Definition: Partitioner.h:603
bool isDefaultConstructed() const
Return true if this is a default constructed partitioner.
Definition: Partitioner.h:452
bool usingSymbolicSemantics() const
Use or not use symbolic semantics.
Definition: Partitioner.h:2293
SemanticMemoryParadigm
Organization of semantic memory.
Definition: BasicTypes.h:80
const BasicBlockCallbacks & basicBlockCallbacks() const
Callbacks for adjusting basic block during discovery.
Definition: Partitioner.h:2086
void basicBlockSemanticsAutoDrop(bool b)
Property: Automatically drop semantics for attached basic blocks.
Definition: Partitioner.h:928
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:1353
const InstructionProvider & instructionProvider() const
Returns the instruction provider.
Definition: Partitioner.h:474
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:164
void autoAddCallReturnEdges(bool b)
Property: Insert (or not) function call return edges.
Definition: Partitioner.h:2317
bool basicBlockSemanticsAutoDrop() const
Property: Automatically drop semantics for attached basic blocks.
Definition: Partitioner.h:927
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:571
ControlFlowGraph::ConstVertexIterator nonexistingVertex() const
Returns the special "non-existing" vertex.
Definition: Partitioner.h:606
std::vector< FunctionPrologueMatcher::Ptr > FunctionPrologueMatchers
See functionPrologueMatchers.
Definition: Partitioner.h:297
FunctionPaddingMatchers & functionPaddingMatchers()
Ordered list of function padding matchers.
Definition: Partitioner.h:2128
const CfgAdjustmentCallbacks & cfgAdjustmentCallbacks() const
List of all callbacks invoked when the CFG is adjusted.
Definition: Partitioner.h:2074
API and storage for attributes.
Definition: Attribute.h:208
Represents no value.
Definition: Optional.h:32
MemoryMap::Ptr memoryMap() const
Returns the memory map.
Definition: Partitioner.h:486
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:293
bool checkingCallBranch() const
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2360
bool addressIsExecutable(rose_addr_t va) const
Returns true if address is executable.
Definition: Partitioner.h:492
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:42
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:2291
bool autoAddCallReturnEdges() const
Property: Insert (or not) function call return edges.
Definition: Partitioner.h:2318
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:2383
Holds configuration information.
Definition: Config.h:210