ROSE  0.9.10.103
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 
1321  BaseSemantics::SValuePtr basicBlockStackDeltaIn(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1322  BaseSemantics::SValuePtr basicBlockStackDeltaOut(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1333  void forgetStackDeltas() const /*final*/;
1334  void forgetStackDeltas(const Function::Ptr&) const /*final*/;
1346  size_t stackDeltaInterproceduralLimit() const /*final*/ { return stackDeltaInterproceduralLimit_; }
1347  void stackDeltaInterproceduralLimit(size_t n) /*final*/ { stackDeltaInterproceduralLimit_ = std::max(size_t(1), n); }
1400  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const BasicBlock::Ptr&) const /*final*/;
1401 
1402  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
1411  void basicBlockMayReturnReset() const /*final*/;
1412 
1413 private:
1414  // Per-vertex data used during may-return analysis
1415  struct MayReturnVertexInfo {
1416  enum State {INIT, CALCULATING, FINISHED};
1417  State state; // current state of vertex
1418  bool processedCallees; // have we processed BBs this vertex calls?
1419  boost::logic::tribool anyCalleesReturn; // do any of those called BBs have a true may-return value?
1420  boost::logic::tribool result; // final result (eventually cached in BB)
1421  MayReturnVertexInfo(): state(INIT), processedCallees(false), anyCalleesReturn(false), result(boost::indeterminate) {}
1422  };
1423 
1424  // Is edge significant for analysis? See .C file for full documentation.
1425  bool mayReturnIsSignificantEdge(const ControlFlowGraph::ConstEdgeIterator &edge,
1426  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1427 
1428  // Determine (and cache in vertexInfo) whether any callees return.
1429  boost::logic::tribool mayReturnDoesCalleeReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1430  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1431 
1432  // Maximum may-return result from significant successors including phantom call-return edge.
1433  boost::logic::tribool mayReturnDoesSuccessorReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1434  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1435 
1436  // The guts of the may-return analysis
1437  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator &start,
1438  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1439 
1440 
1441 
1444  //
1445  // Partitioner data block operations
1446  //
1449 public:
1455  size_t nDataBlocks() const /*final*/;
1456 
1462  bool dataBlockExists(const DataBlock::Ptr&) const /*final*/;
1463 
1472  DataBlock::Ptr findBestDataBlock(const AddressInterval&) const /*final*/;
1473 
1482  void attachDataBlock(const DataBlock::Ptr&) /*final*/;
1483 
1490  DataBlock::Ptr detachDataBlock(const DataBlock::Ptr&) /*final*/;
1491 
1501  std::vector<DataBlock::Ptr> dataBlocksOverlapping(const AddressInterval&) const /*final*/;
1502 
1512  std::vector<DataBlock::Ptr> dataBlocksSpanning(const AddressInterval&) const /*final*/;
1513 
1523  std::vector<DataBlock::Ptr> dataBlocksContainedIn(const AddressInterval&) const /*final*/;
1524 
1531  AddressInterval dataBlockExtent(const DataBlock::Ptr&) const /*final*/;
1532 
1538  std::vector<DataBlock::Ptr> dataBlocks() const /*final*/;
1539 
1540 
1541 
1544  //
1545  // Partitioner function operations
1546  //
1549 public:
1555  size_t nFunctions() const /*final*/ { return functions_.size(); }
1556 
1575  Function::Ptr functionExists(rose_addr_t entryVa) const /*final*/;
1576  Function::Ptr functionExists(const BasicBlock::Ptr &entryBlock) const /*final*/;
1577  Function::Ptr functionExists(const Function::Ptr &function) const /*final*/;
1585  std::vector<Function::Ptr> functions() const /*final*/;
1586 
1596  std::vector<Function::Ptr> functionsOverlapping(const AddressInterval&) const /*final*/;
1597 
1608  std::vector<Function::Ptr> functionsSpanning(const AddressInterval&) const /*final*/;
1609 
1619  std::vector<Function::Ptr> functionsContainedIn(const AddressInterval&) const /*final*/;
1620 
1637  AddressIntervalSet functionExtent(const Function::Ptr&) const /*final*/;
1638  void functionExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1639  AddressIntervalSet functionBasicBlockExtent(const Function::Ptr &function) const /*final*/;
1640  void functionBasicBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1641  AddressIntervalSet functionDataBlockExtent(const Function::Ptr &function) const /*final*/;
1642  void functionDataBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1664  size_t attachFunction(const Function::Ptr&) /*final*/;
1665  size_t attachFunctions(const Functions&) /*final*/;
1681  Function::Ptr attachOrMergeFunction(const Function::Ptr&) /*final*/;
1682 
1699  size_t attachFunctionBasicBlocks(const Functions&) /*final*/;
1700  size_t attachFunctionBasicBlocks(const Function::Ptr&) /*final*/;
1716  void detachFunction(const Function::Ptr&) /*final*/;
1717 
1728  DataBlock::Ptr attachFunctionDataBlock(const Function::Ptr&, rose_addr_t startVa, size_t nBytes) /*final*/;
1729 
1735  void attachFunctionDataBlock(const Function::Ptr&, const DataBlock::Ptr&) /*final*/;
1736 
1771  std::vector<Function::Ptr>
1772  functionsOwningBasicBlock(const ControlFlowGraph::Vertex&, bool doSort = true) const /*final*/;
1773 
1774  std::vector<Function::Ptr>
1775  functionsOwningBasicBlock(const ControlFlowGraph::ConstVertexIterator&, bool doSort = true) const /*final*/;
1776 
1777  std::vector<Function::Ptr>
1778  functionsOwningBasicBlock(rose_addr_t bblockVa, bool doSort = true) const /*final*/;
1779 
1780  std::vector<Function::Ptr>
1781  functionsOwningBasicBlock(const BasicBlock::Ptr&, bool doSort = true) const /*final*/;
1782 
1783  template<class Container> // container can hold any type accepted by functionsOwningBasicBlock
1784  std::vector<Function::Ptr>
1785  functionsOwningBasicBlocks(const Container &bblocks) const /*final*/ {
1786  std::vector<Function::Ptr> retval;
1787  BOOST_FOREACH (const typename Container::value_type& bblock, bblocks) {
1788  BOOST_FOREACH (const Function::Ptr &function, functionsOwningBasicBlock(bblock, false))
1789  insertUnique(retval, function, sortFunctionsByAddress);
1790  }
1791  return retval;
1792  }
1804  std::vector<Function::Ptr> discoverCalledFunctions() const /*final*/;
1805 
1817  std::vector<Function::Ptr> discoverFunctionEntryVertices() const /*final*/;
1818 
1828  Sawyer::Optional<Thunk> functionIsThunk(const Function::Ptr&) const /*final*/;
1829 
1840  void discoverFunctionBasicBlocks(const Function::Ptr &function) const /*final*/;
1841 
1848  std::set<rose_addr_t> functionGhostSuccessors(const Function::Ptr&) const /*final*/;
1849 
1857  FunctionCallGraph functionCallGraph(bool allowParallelEdges = true) const /*final*/;
1858 
1878  BaseSemantics::SValuePtr functionStackDelta(const Function::Ptr &function) const /*final*/;
1879 
1883  void allFunctionStackDelta() const /*final*/;
1884 
1891  Sawyer::Optional<bool> functionOptionalMayReturn(const Function::Ptr &function) const /*final*/;
1892 
1896  void allFunctionMayReturn() const /*final*/;
1897 
1924  const CallingConvention::Analysis&
1925  functionCallingConvention(const Function::Ptr&,
1926  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1927  const /*final*/;
1928 
1941  void
1942  allFunctionCallingConvention(const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1943  const /*final*/;
1944 
1970  CallingConvention::Dictionary
1971  functionCallingConventionDefinitions(const Function::Ptr&,
1972  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1973  const /*final*/;
1974 
1987  void
1988  allFunctionCallingConventionDefinition(const CallingConvention::Definition::Ptr &dflt =
1989  CallingConvention::Definition::Ptr()) const /*final*/;
1990 
1999  void fixInterFunctionEdges() /*final*/;
2000  void fixInterFunctionEdge(const ControlFlowGraph::ConstEdgeIterator&) /*final*/;
2020  bool functionIsNoop(const Function::Ptr&) const /*final*/;
2021 
2027  void allFunctionIsNoop() const /*final*/;
2028 
2036  void forgetFunctionIsNoop() const /*final*/;
2037  void forgetFunctionIsNoop(const Function::Ptr&) const /*final*/;
2042  //
2045  // Callbacks
2046  //
2049 public:
2067  CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() /*final*/ { return cfgAdjustmentCallbacks_; }
2068  const CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() const /*final*/ { return cfgAdjustmentCallbacks_; }
2079  BasicBlockCallbacks& basicBlockCallbacks() /*final*/ { return basicBlockCallbacks_; }
2080  const BasicBlockCallbacks& basicBlockCallbacks() const /*final*/ { return basicBlockCallbacks_; }
2083 public:
2091  FunctionPrologueMatchers& functionPrologueMatchers() /*final*/ { return functionPrologueMatchers_; }
2092  const FunctionPrologueMatchers& functionPrologueMatchers() const /*final*/ { return functionPrologueMatchers_; }
2114  std::vector<Function::Ptr> nextFunctionPrologue(rose_addr_t startVa) /*final*/;
2115 
2116 public:
2122  FunctionPaddingMatchers& functionPaddingMatchers() /*final*/ { return functionPaddingMatchers_; }
2123  const FunctionPaddingMatchers& functionPaddingMatchers() const /*final*/ { return functionPaddingMatchers_; }
2133  DataBlock::Ptr matchFunctionPadding(const Function::Ptr&) /*final*/;
2134 
2135 
2136 
2139  //
2140  // Partitioner miscellaneous
2141  //
2144 public:
2156  void dumpCfg(std::ostream&, const std::string &prefix="", bool showBlocks=true,
2157  bool computeProperties=true) const /*final*/;
2158 
2172  void cfgGraphViz(std::ostream&, const AddressInterval &restrict = AddressInterval::whole(),
2173  bool showNeighbors=true) const /*final*/;
2174 
2180  static std::string vertexName(const ControlFlowGraph::Vertex&) /*final*/;
2181  std::string vertexName(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
2187  static std::string vertexNameEnd(const ControlFlowGraph::Vertex&) /*final*/;
2188 
2194  static std::string edgeNameSrc(const ControlFlowGraph::Edge&) /*final*/;
2195  std::string edgeNameSrc(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2203  static std::string edgeNameDst(const ControlFlowGraph::Edge&) /*final*/;
2204  std::string edgeNameDst(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2212  static std::string edgeName(const ControlFlowGraph::Edge&) /*final*/;
2213  std::string edgeName(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2219  static std::string basicBlockName(const BasicBlock::Ptr&) /*final*/;
2220 
2224  static std::string dataBlockName(const DataBlock::Ptr&) /*final*/;
2225 
2229  static std::string functionName(const Function::Ptr&) /*final*/;
2230 
2248  Progress::Ptr progress() const /*final*/;
2249  void progress(const Progress::Ptr&) /*final*/;
2256  void updateProgress(const std::string &phase, double completion) const;
2257 
2259  void showStatistics() const;
2260 
2262  // Settings
2264 public:
2265 
2273  const BasePartitionerSettings& settings() const /*final*/ { return settings_; }
2274  void settings(const BasePartitionerSettings &s) /*final*/ { settings_ = s; }
2285  void enableSymbolicSemantics(bool b=true) /*final*/ { settings_.usingSemantics = b; }
2286  void disableSymbolicSemantics() /*final*/ { settings_.usingSemantics = false; }
2287  bool usingSymbolicSemantics() const /*final*/ { return settings_.usingSemantics; }
2311  void autoAddCallReturnEdges(bool b) /*final*/ { autoAddCallReturnEdges_ = b; }
2312  bool autoAddCallReturnEdges() const /*final*/ { return autoAddCallReturnEdges_; }
2329  void assumeFunctionsReturn(bool b) /*final*/ { assumeFunctionsReturn_ = b; }
2330  bool assumeFunctionsReturn() const /*final*/ { return assumeFunctionsReturn_; }
2342  void addressName(rose_addr_t, const std::string&) /*final*/;
2343  const std::string& addressName(rose_addr_t va) const /*final*/ { return addressNames_.getOrDefault(va); }
2344  const AddressNameMap& addressNames() const /*final*/ { return addressNames_; }
2354  bool checkingCallBranch() const /*final*/ { return settings_.checkingCallBranch; }
2355  void checkingCallBranch(bool b) /*final*/ { settings_.checkingCallBranch = b; }
2359  //
2362  // Instruction semantics
2363  //
2366 public:
2376  SemanticMemoryParadigm semanticMemoryParadigm() const { return semanticMemoryParadigm_; }
2377  void semanticMemoryParadigm(SemanticMemoryParadigm p) { semanticMemoryParadigm_ = p; }
2386  SmtSolverPtr smtSolver() const /*final*/ { return solver_; }
2387 
2398  BaseSemantics::RiscOperatorsPtr newOperators() const /*final*/;
2399  BaseSemantics::RiscOperatorsPtr newOperators(SemanticMemoryParadigm) const /*final*/;
2409  BaseSemantics::DispatcherPtr newDispatcher(const BaseSemantics::RiscOperatorsPtr&) const /*final*/;
2410 
2411 
2412 
2415  //
2416  // Python API support functions
2417  //
2420 #ifdef ROSE_ENABLE_PYTHON_API
2421  void pythonUnparse() const;
2422 #endif
2423 
2424 
2425 
2428  //
2429  // Partitioner internal utilities
2430  //
2433 private:
2434  void init(Disassembler*, const MemoryMap::Ptr&);
2435  void init(const Partitioner&);
2436  void updateCfgProgress();
2437 
2438 private:
2439  // Convert a CFG vertex iterator from one partitioner to another. This is called during copy construction when the source
2440  // and destination CFGs are identical.
2441  ControlFlowGraph::VertexIterator convertFrom(const Partitioner &other,
2442  ControlFlowGraph::ConstVertexIterator otherIter);
2443 
2444  // Adjusts edges for a placeholder vertex. This method erases all outgoing edges for the specified placeholder vertex and
2445  // then inserts a single edge from the placeholder to the special "undiscovered" vertex. */
2446  ControlFlowGraph::EdgeIterator adjustPlaceholderEdges(const ControlFlowGraph::VertexIterator &placeholder);
2447 
2448  // Adjusts edges for a non-existing basic block. This method erases all outgoing edges for the specified vertex and
2449  // then inserts a single edge from the vertex to the special "non-existing" vertex. */
2450  ControlFlowGraph::EdgeIterator adjustNonexistingEdges(const ControlFlowGraph::VertexIterator &vertex);
2451 
2452  // Implementation for the discoverBasicBlock methods. The startVa must not be the address of an existing placeholder.
2453  BasicBlock::Ptr discoverBasicBlockInternal(rose_addr_t startVa) const;
2454 
2455  // Checks consistency of internal data structures when debugging is enable (when NDEBUG is not defined).
2456  void checkConsistency() const;
2457 
2458  // This method is called whenever a new placeholder is inserted into the CFG or a new basic block is attached to the
2459  // CFG/AUM. The call happens immediately after the CFG/AUM are updated.
2460  void bblockAttached(const ControlFlowGraph::VertexIterator &newVertex);
2461 
2462  // This method is called whenever a basic block is detached from the CFG/AUM or when a placeholder is erased from the CFG.
2463  // The call happens immediately after the CFG/AUM are updated.
2464  void bblockDetached(rose_addr_t startVa, const BasicBlock::Ptr &removedBlock);
2465 
2466  // Rebuild the vertexIndex_ and other cache-like data members from the control flow graph
2467  void rebuildVertexIndices();
2468 };
2469 
2470 } // namespace
2471 } // namespace
2472 } // namespace
2473 
2474 #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:2092
SmtSolverPtr smtSolver() const
SMT solver.
Definition: Partitioner.h:2386
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:2091
const std::string & addressName(rose_addr_t va) const
Property: Name for address.
Definition: Partitioner.h:2343
void disableSymbolicSemantics()
Use or not use symbolic semantics.
Definition: Partitioner.h:2286
void checkingCallBranch(bool b)
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2355
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:2123
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:269
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:2079
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:2376
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:2330
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:2344
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:2274
void assumeFunctionsReturn(bool b)
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2329
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:2287
SemanticMemoryParadigm
Organization of semantic memory.
Definition: BasicTypes.h:72
const BasicBlockCallbacks & basicBlockCallbacks() const
Callbacks for adjusting basic block during discovery.
Definition: Partitioner.h:2080
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:1347
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:274
bool usingSemantics
Whether instruction semantics are used.
Definition: BasicTypes.h:270
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:2311
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:275
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:2122
const CfgAdjustmentCallbacks & cfgAdjustmentCallbacks() const
List of all callbacks invoked when the CFG is adjusted.
Definition: Partitioner.h:2068
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:2354
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:2285
bool autoAddCallReturnEdges() const
Property: Insert (or not) function call return edges.
Definition: Partitioner.h:2312
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:2377
Holds configuration information.
Definition: Config.h:210