ROSE  0.9.9.109
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/SharedPointer.h>
23 
24 #include <BinaryUnparser.h>
25 #include <Progress.h>
26 
27 #include <boost/serialization/access.hpp>
28 #include <boost/serialization/split_member.hpp>
29 
30 #include <ostream>
31 #include <set>
32 #include <string>
33 #include <vector>
34 
35 // Derived classes needed for serialization
36 #include <DispatcherM68k.h>
37 #include <DispatcherX86.h>
38 #include <YicesSolver.h>
39 
40 namespace Rose {
41 namespace BinaryAnalysis {
42 
74 namespace Partitioner2 {
75 
289 class ROSE_DLL_API Partitioner: public Sawyer::Attribute::Storage<> { // final
290 public:
293  typedef std::vector<FunctionPrologueMatcher::Ptr> FunctionPrologueMatchers;
294  typedef std::vector<FunctionPaddingMatcher::Ptr> FunctionPaddingMatchers;
297  struct Thunk {
299  rose_addr_t target;
300  Thunk(const BasicBlock::Ptr &bblock, rose_addr_t target): bblock(bblock), target(target) {}
301  };
302 
305 
306 private:
307  BasePartitionerSettings settings_; // settings adjustable from the command-line
308  Configuration config_; // configuration information about functions, blocks, etc.
309  InstructionProvider::Ptr instructionProvider_; // cache for all disassembled instructions
310  MemoryMap::Ptr memoryMap_; // description of memory, especially insns and non-writable
311  ControlFlowGraph cfg_; // basic blocks that will become part of the ROSE AST
312  CfgVertexIndex vertexIndex_; // Vertex-by-address index for the CFG
313  AddressUsageMap aum_; // How addresses are used for each address represented by the CFG
314  SMTSolver *solver_; // Satisfiable modulo theory solver used by semantic expressions
315  Functions functions_; // List of all attached functions by entry address
316  bool autoAddCallReturnEdges_; // Add E_CALL_RETURN edges when blocks are attached to CFG?
317  bool assumeFunctionsReturn_; // Assume that unproven functions return to caller?
318  size_t stackDeltaInterproceduralLimit_; // Max depth of call stack when computing stack deltas
319  AddressNameMap addressNames_; // Names for various addresses
320  SemanticMemoryParadigm semanticMemoryParadigm_; // Slow and precise, or fast and imprecise?
321  Unparser::BasePtr unparser_; // For unparsing things to pseudo-assembly
322  Unparser::BasePtr insnUnparser_; // For unparsing single instructions in diagnostics
323 
324  // Callback lists
325  CfgAdjustmentCallbacks cfgAdjustmentCallbacks_;
326  BasicBlockCallbacks basicBlockCallbacks_;
327  FunctionPrologueMatchers functionPrologueMatchers_;
328  FunctionPaddingMatchers functionPaddingMatchers_;
329 
330  // Special CFG vertices.
331  ControlFlowGraph::VertexIterator undiscoveredVertex_;
332  ControlFlowGraph::VertexIterator indeterminateVertex_;
333  ControlFlowGraph::VertexIterator nonexistingVertex_;
334  static const size_t nSpecialVertices = 3;
335 
336  // Protects the following data members
337  mutable SAWYER_THREAD_TRAITS::Mutex mutex_;
338  Progress::Ptr progress_; // Progress reporter to update, or null
339  mutable size_t cfgProgressTotal_; // Expected total for the CFG progress bar; initialized at first report
340 
341 
344  //
345  // Serialization
346  //
349 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
350 private:
351  friend class boost::serialization::access;
352 
353  template<class S>
354  void serializeCommon(S &s, const unsigned version) {
355  s.template register_type<InstructionSemantics2::SymbolicSemantics::SValue>();
356  s.template register_type<InstructionSemantics2::SymbolicSemantics::RiscOperators>();
357  s.template register_type<InstructionSemantics2::DispatcherX86>();
358  s.template register_type<InstructionSemantics2::DispatcherM68k>();
359  s.template register_type<SymbolicExpr::Interior>();
360  s.template register_type<SymbolicExpr::Leaf>();
361  s.template register_type<YicesSolver>();
362  s.template register_type<Semantics::SValue>();
363  s.template register_type<Semantics::MemoryListState>();
364  s.template register_type<Semantics::MemoryMapState>();
365  s.template register_type<Semantics::RegisterState>();
366  s.template register_type<Semantics::State>();
367  s.template register_type<Semantics::RiscOperators>();
368  s & BOOST_SERIALIZATION_NVP(settings_);
369  // s & config_; -- FIXME[Robb P Matzke 2016-11-08]
370  s & BOOST_SERIALIZATION_NVP(instructionProvider_);
371  s & BOOST_SERIALIZATION_NVP(memoryMap_);
372  s & BOOST_SERIALIZATION_NVP(cfg_);
373  // s & vertexIndex_; -- initialized by rebuildVertexIndices
374  s & BOOST_SERIALIZATION_NVP(aum_);
375  s & BOOST_SERIALIZATION_NVP(solver_);
376  s & BOOST_SERIALIZATION_NVP(functions_);
377  s & BOOST_SERIALIZATION_NVP(autoAddCallReturnEdges_);
378  s & BOOST_SERIALIZATION_NVP(assumeFunctionsReturn_);
379  s & BOOST_SERIALIZATION_NVP(stackDeltaInterproceduralLimit_);
380  s & BOOST_SERIALIZATION_NVP(addressNames_);
381  s & BOOST_SERIALIZATION_NVP(semanticMemoryParadigm_);
382  // s & unparser_; -- not saved; restored from disassembler
383  // s & cfgAdjustmentCallbacks_; -- not saved/restored
384  // s & basicBlockCallbacks_; -- not saved/restored
385  // s & functionPrologueMatchers_; -- not saved/restored
386  // s & functionPaddingMatchers_; -- not saved/restored
387  // s & undiscoveredVertex_; -- initialized by rebuildVertexIndices
388  // s & indeterminateVertex_; -- initialized by rebuildVertexIndices
389  // s & nonexistingVertex_; -- initialized by rebuildVertexIndices
390  // s & progress_; -- not saved/restored
391  // s & cfgProgressTotal_; -- not saved/restored
392  }
393 
394  template<class S>
395  void save(S &s, const unsigned version) const {
396  const_cast<Partitioner*>(this)->serializeCommon(s, version);
397  }
398 
399  template<class S>
400  void load(S &s, const unsigned version) {
401  serializeCommon(s, version);
402  rebuildVertexIndices();
403  }
404 
405  BOOST_SERIALIZATION_SPLIT_MEMBER();
406 #endif
407 
408 
411  //
412  // Constructors
413  //
416 public:
421  Partitioner();
422 
427  Partitioner(Disassembler *disassembler, const MemoryMap::Ptr &map);
428 
429  // FIXME[Robb P. Matzke 2014-11-08]: This is not ready for use yet. The problem is that because of the shallow copy, both
430  // partitioners are pointing to the same basic blocks, data blocks, and functions. This is okay by itself since these
431  // things are reference counted, but the paradigm of locked/unlocked blocks and functions breaks down somewhat -- does
432  // unlocking a basic block from one partitioner make it modifiable even though it's still locked in the other partitioner?
433  // FIXME[Robb P. Matzke 2014-12-27]: Not the most efficient implementation, but saves on cut-n-paste which would surely rot
434  // after a while.
435  Partitioner(const Partitioner &other);
436  Partitioner& operator=(const Partitioner &other);
437 
438  ~Partitioner();
439 
446  bool isDefaultConstructed() const { return instructionProvider_ == NULL; }
447 
449  void clear() /*final*/;
450 
458  Configuration& configuration() { return config_; }
459  const Configuration& configuration() const { return config_; }
467  InstructionProvider& instructionProvider() /*final*/ { return *instructionProvider_; }
468  const InstructionProvider& instructionProvider() const /*final*/ { return *instructionProvider_; }
480  MemoryMap::Ptr memoryMap() const /*final*/ { return memoryMap_; }
486  bool addressIsExecutable(rose_addr_t va) const /*final*/ {
487  return memoryMap_!=NULL && memoryMap_->at(va).require(MemoryMap::EXECUTABLE).exists();
488  }
489 
499  Unparser::BasePtr unparser() const /*final*/;
500  void unparser(const Unparser::BasePtr&) /*final*/;
512  Unparser::BasePtr insnUnparser() const /*final*/;
513  void insnUnparser(const Unparser::BasePtr&) /*final*/;
524  std::string unparse(SgAsmInstruction*) const;
525  void unparse(std::ostream&, SgAsmInstruction*) const;
526  void unparse(std::ostream&, const BasicBlock::Ptr&) const;
527  void unparse(std::ostream&, const DataBlock::Ptr&) const;
528  void unparse(std::ostream&, const Function::Ptr&) const;
529  void unparse(std::ostream&) const;
532  //
535  // Partitioner CFG queries
536  //
539 public:
545  size_t nBytes() const /*final*/ { return aum_.size(); }
546 
554  ControlFlowGraph::VertexIterator undiscoveredVertex() /*final*/ {
555  return undiscoveredVertex_;
556  }
557  ControlFlowGraph::ConstVertexIterator undiscoveredVertex() const /*final*/ {
558  return undiscoveredVertex_;
559  }
572  ControlFlowGraph::VertexIterator indeterminateVertex() /*final*/ {
573  return indeterminateVertex_;
574  }
575  ControlFlowGraph::ConstVertexIterator indeterminateVertex() const /*final*/ {
576  return indeterminateVertex_;
577  }
589  ControlFlowGraph::VertexIterator nonexistingVertex() /*final*/ {
590  return nonexistingVertex_;
591  }
592  ControlFlowGraph::ConstVertexIterator nonexistingVertex() const /*final*/ {
593  return nonexistingVertex_;
594  }
603  const ControlFlowGraph& cfg() const /*final*/ { return cfg_; }
604 
611  const AddressUsageMap& aum() const /*final*/ { return aum_; }
612 
614  AddressUsageMap aum(const Function::Ptr&) const /*final*/;
615 
623  std::set<rose_addr_t> ghostSuccessors() const /*final*/;
624 
653  bool isEdgeIntraProcedural(ControlFlowGraph::ConstEdgeIterator edge,
654  const Function::Ptr &function = Function::Ptr()) const /*final*/;
655  bool isEdgeIntraProcedural(const ControlFlowGraph::Edge &edge,
656  const Function::Ptr &function = Function::Ptr()) const /*final*/;
693  bool isEdgeInterProcedural(ControlFlowGraph::ConstEdgeIterator edge,
694  const Function::Ptr &sourceFunction = Function::Ptr(),
695  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
696  bool isEdgeInterProcedural(const ControlFlowGraph::Edge &edge,
697  const Function::Ptr &sourceFunction = Function::Ptr(),
698  const Function::Ptr &targetFunction = Function::Ptr()) const /*final*/;
703  //
706  // Partitioner instruction operations
707  //
710 public:
716  size_t nInstructions() const /*final*/;
717 
727  Sawyer::Optional<AddressUser> instructionExists(rose_addr_t startVa) const /*final*/ {
728  return aum_.instructionExists(startVa);
729  }
731  return insn==NULL ? Sawyer::Nothing() : instructionExists(insn->get_address());
732  }
741  ControlFlowGraph::ConstVertexIterator instructionVertex(rose_addr_t insnVa) const;
742 
751  std::vector<SgAsmInstruction*> instructionsOverlapping(const AddressInterval&) const /*final*/;
752 
761  std::vector<SgAsmInstruction*> instructionsSpanning(const AddressInterval&) const /*final*/;
762 
772  std::vector<SgAsmInstruction*> instructionsContainedIn(const AddressInterval&) const /*final*/;
773 
781  AddressInterval instructionExtent(SgAsmInstruction*) const /*final*/;
782 
795  SgAsmInstruction* discoverInstruction(rose_addr_t startVa) const /*final*/;
796 
801  CrossReferences instructionCrossReferences(const AddressIntervalSet &restriction) const /*final*/;
802 
803 
806  //
807  // Partitioner basic block placeholder operations
808  //
811 public:
821  size_t nPlaceholders() const /*final*/;
822 
831  bool placeholderExists(rose_addr_t startVa) const /*final*/;
832 
843  ControlFlowGraph::VertexIterator findPlaceholder(rose_addr_t startVa) /*final*/ {
844  if (Sawyer::Optional<ControlFlowGraph::VertexIterator> found = vertexIndex_.getOptional(startVa))
845  return *found;
846  return cfg_.vertices().end();
847  }
848  ControlFlowGraph::ConstVertexIterator findPlaceholder(rose_addr_t startVa) const /*final*/ {
849  if (Sawyer::Optional<ControlFlowGraph::VertexIterator> found = vertexIndex_.getOptional(startVa))
850  return *found;
851  return cfg_.vertices().end();
852  }
871  ControlFlowGraph::VertexIterator insertPlaceholder(rose_addr_t startVa) /*final*/;
872 
886  BasicBlock::Ptr erasePlaceholder(const ControlFlowGraph::ConstVertexIterator &placeholder) /*final*/;
887  BasicBlock::Ptr erasePlaceholder(rose_addr_t startVa) /*final*/;
892  //
895  // Partitioner basic block operations
896  //
899 public:
913  bool basicBlockSemanticsAutoDrop() const /*final*/ { return settings_.basicBlockSemanticsAutoDrop; }
925  void basicBlockDropSemantics() const /*final*/;
926 
935  size_t nBasicBlocks() const /*final*/;
936 
944  std::vector<BasicBlock::Ptr> basicBlocks() const /*final*/;
945 
967  BasicBlock::Ptr basicBlockExists(rose_addr_t startVa) const /*final*/;
968  BasicBlock::Ptr basicBlockExists(const BasicBlock::Ptr&) const /*final*/;
980  std::vector<BasicBlock::Ptr> basicBlocksOverlapping(const AddressInterval&) const /*final*/;
981 
992  std::vector<BasicBlock::Ptr> basicBlocksSpanning(const AddressInterval&) const /*final*/;
993 
1003  std::vector<BasicBlock::Ptr> basicBlocksContainedIn(const AddressInterval&) const /*final*/;
1004 
1011  BasicBlock::Ptr basicBlockContainingInstruction(rose_addr_t insnVa) const /*final*/;
1012 
1022  AddressIntervalSet basicBlockInstructionExtent(const BasicBlock::Ptr&) const /*final*/;
1023 
1029  AddressIntervalSet basicBlockDataExtent(const BasicBlock::Ptr&) const /*final*/;
1030 
1055  BasicBlock::Ptr detachBasicBlock(rose_addr_t startVa) /*final*/;
1056  BasicBlock::Ptr detachBasicBlock(const BasicBlock::Ptr &basicBlock) /*final*/;
1057  BasicBlock::Ptr detachBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder) /*final*/;
1072  ControlFlowGraph::VertexIterator truncateBasicBlock(const ControlFlowGraph::ConstVertexIterator &basicBlock,
1073  SgAsmInstruction *insn) /*final*/;
1074 
1105  void attachBasicBlock(const BasicBlock::Ptr&) /*final*/;
1106  void attachBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder, const BasicBlock::Ptr&) /*final*/;
1194  BasicBlock::Ptr discoverBasicBlock(rose_addr_t startVa) const /*final*/;
1195  BasicBlock::Ptr discoverBasicBlock(const ControlFlowGraph::ConstVertexIterator &placeholder) const /*final*/;
1208  BasicBlock::Successors basicBlockSuccessors(const BasicBlock::Ptr&) const /*final*/;
1209 
1218  std::vector<rose_addr_t> basicBlockConcreteSuccessors(const BasicBlock::Ptr&, bool *isComplete=NULL) const /*final*/;
1219 
1238  std::set<rose_addr_t> basicBlockGhostSuccessors(const BasicBlock::Ptr&) const /*final*/;
1239 
1247  bool basicBlockIsFunctionCall(const BasicBlock::Ptr&) const /*final*/;
1248 
1259  bool basicBlockIsFunctionReturn(const BasicBlock::Ptr&) const /*final*/;
1260 
1300  BaseSemantics::SValuePtr basicBlockStackDeltaIn(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1301  BaseSemantics::SValuePtr basicBlockStackDeltaOut(const BasicBlock::Ptr&, const Function::Ptr &function) const /*final*/;
1312  void forgetStackDeltas() const /*final*/;
1313  void forgetStackDeltas(const Function::Ptr&) const /*final*/;
1325  size_t stackDeltaInterproceduralLimit() const /*final*/ { return stackDeltaInterproceduralLimit_; }
1326  void stackDeltaInterproceduralLimit(size_t n) /*final*/ { stackDeltaInterproceduralLimit_ = std::max(size_t(1), n); }
1379  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const BasicBlock::Ptr&) const /*final*/;
1380 
1381  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
1390  void basicBlockMayReturnReset() const /*final*/;
1391 
1392 private:
1393  // Per-vertex data used during may-return analysis
1394  struct MayReturnVertexInfo {
1395  enum State {INIT, CALCULATING, FINISHED};
1396  State state; // current state of vertex
1397  bool processedCallees; // have we processed BBs this vertex calls?
1398  boost::logic::tribool anyCalleesReturn; // do any of those called BBs have a true may-return value?
1399  boost::logic::tribool result; // final result (eventually cached in BB)
1400  MayReturnVertexInfo(): state(INIT), processedCallees(false), anyCalleesReturn(false), result(boost::indeterminate) {}
1401  };
1402 
1403  // Is edge significant for analysis? See .C file for full documentation.
1404  bool mayReturnIsSignificantEdge(const ControlFlowGraph::ConstEdgeIterator &edge,
1405  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1406 
1407  // Determine (and cache in vertexInfo) whether any callees return.
1408  boost::logic::tribool mayReturnDoesCalleeReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1409  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1410 
1411  // Maximum may-return result from significant successors including phantom call-return edge.
1412  boost::logic::tribool mayReturnDoesSuccessorReturn(const ControlFlowGraph::ConstVertexIterator &vertex,
1413  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1414 
1415  // The guts of the may-return analysis
1416  Sawyer::Optional<bool> basicBlockOptionalMayReturn(const ControlFlowGraph::ConstVertexIterator &start,
1417  std::vector<MayReturnVertexInfo> &vertexInfo) const;
1418 
1419 
1420 
1423  //
1424  // Partitioner data block operations
1425  //
1428 public:
1434  size_t nDataBlocks() const /*final*/;
1435 
1441  bool dataBlockExists(const DataBlock::Ptr&) const /*final*/;
1442 
1451  DataBlock::Ptr findBestDataBlock(const AddressInterval&) const /*final*/;
1452 
1461  void attachDataBlock(const DataBlock::Ptr&) /*final*/;
1462 
1469  DataBlock::Ptr detachDataBlock(const DataBlock::Ptr&) /*final*/;
1470 
1480  std::vector<DataBlock::Ptr> dataBlocksOverlapping(const AddressInterval&) const /*final*/;
1481 
1491  std::vector<DataBlock::Ptr> dataBlocksSpanning(const AddressInterval&) const /*final*/;
1492 
1502  std::vector<DataBlock::Ptr> dataBlocksContainedIn(const AddressInterval&) const /*final*/;
1503 
1510  AddressInterval dataBlockExtent(const DataBlock::Ptr&) const /*final*/;
1511 
1517  std::vector<DataBlock::Ptr> dataBlocks() const /*final*/;
1518 
1519 
1520 
1523  //
1524  // Partitioner function operations
1525  //
1528 public:
1534  size_t nFunctions() const /*final*/ { return functions_.size(); }
1535 
1554  Function::Ptr functionExists(rose_addr_t entryVa) const /*final*/;
1555  Function::Ptr functionExists(const BasicBlock::Ptr &entryBlock) const /*final*/;
1556  Function::Ptr functionExists(const Function::Ptr &function) const /*final*/;
1564  std::vector<Function::Ptr> functions() const /*final*/;
1565 
1575  std::vector<Function::Ptr> functionsOverlapping(const AddressInterval&) const /*final*/;
1576 
1587  std::vector<Function::Ptr> functionsSpanning(const AddressInterval&) const /*final*/;
1588 
1598  std::vector<Function::Ptr> functionsContainedIn(const AddressInterval&) const /*final*/;
1599 
1616  AddressIntervalSet functionExtent(const Function::Ptr&) const /*final*/;
1617  void functionExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1618  AddressIntervalSet functionBasicBlockExtent(const Function::Ptr &function) const /*final*/;
1619  void functionBasicBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1620  AddressIntervalSet functionDataBlockExtent(const Function::Ptr &function) const /*final*/;
1621  void functionDataBlockExtent(const Function::Ptr &function, AddressIntervalSet &retval /*in,out*/) const /*final*/;
1643  size_t attachFunction(const Function::Ptr&) /*final*/;
1644  size_t attachFunctions(const Functions&) /*final*/;
1660  Function::Ptr attachOrMergeFunction(const Function::Ptr&) /*final*/;
1661 
1678  size_t attachFunctionBasicBlocks(const Functions&) /*final*/;
1679  size_t attachFunctionBasicBlocks(const Function::Ptr&) /*final*/;
1695  void detachFunction(const Function::Ptr&) /*final*/;
1696 
1707  DataBlock::Ptr attachFunctionDataBlock(const Function::Ptr&, rose_addr_t startVa, size_t nBytes) /*final*/;
1708 
1714  void attachFunctionDataBlock(const Function::Ptr&, const DataBlock::Ptr&) /*final*/;
1715 
1750  std::vector<Function::Ptr>
1751  functionsOwningBasicBlock(const ControlFlowGraph::Vertex&, bool doSort = true) const /*final*/;
1752 
1753  std::vector<Function::Ptr>
1754  functionsOwningBasicBlock(const ControlFlowGraph::ConstVertexIterator&, bool doSort = true) const /*final*/;
1755 
1756  std::vector<Function::Ptr>
1757  functionsOwningBasicBlock(rose_addr_t bblockVa, bool doSort = true) const /*final*/;
1758 
1759  std::vector<Function::Ptr>
1760  functionsOwningBasicBlock(const BasicBlock::Ptr&, bool doSort = true) const /*final*/;
1761 
1762  template<class Container> // container can hold any type accepted by functionsOwningBasicBlock
1763  std::vector<Function::Ptr>
1764  functionsOwningBasicBlocks(const Container &bblocks) const /*final*/ {
1765  std::vector<Function::Ptr> retval;
1766  BOOST_FOREACH (const typename Container::value_type& bblock, bblocks) {
1767  BOOST_FOREACH (const Function::Ptr &function, functionsOwningBasicBlock(bblock, false))
1768  insertUnique(retval, function, sortFunctionsByAddress);
1769  }
1770  return retval;
1771  }
1783  std::vector<Function::Ptr> discoverCalledFunctions() const /*final*/;
1784 
1796  std::vector<Function::Ptr> discoverFunctionEntryVertices() const /*final*/;
1797 
1807  Sawyer::Optional<Thunk> functionIsThunk(const Function::Ptr&) const /*final*/;
1808 
1819  void discoverFunctionBasicBlocks(const Function::Ptr &function) const /*final*/;
1820 
1827  std::set<rose_addr_t> functionGhostSuccessors(const Function::Ptr&) const /*final*/;
1828 
1836  FunctionCallGraph functionCallGraph(bool allowParallelEdges = true) const /*final*/;
1837 
1857  BaseSemantics::SValuePtr functionStackDelta(const Function::Ptr &function) const /*final*/;
1858 
1862  void allFunctionStackDelta() const /*final*/;
1863 
1870  Sawyer::Optional<bool> functionOptionalMayReturn(const Function::Ptr &function) const /*final*/;
1871 
1875  void allFunctionMayReturn() const /*final*/;
1876 
1903  const CallingConvention::Analysis&
1904  functionCallingConvention(const Function::Ptr&,
1905  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1906  const /*final*/;
1907 
1920  void
1921  allFunctionCallingConvention(const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1922  const /*final*/;
1923 
1949  CallingConvention::Dictionary
1950  functionCallingConventionDefinitions(const Function::Ptr&,
1951  const CallingConvention::Definition::Ptr &dflt = CallingConvention::Definition::Ptr())
1952  const /*final*/;
1953 
1966  void
1967  allFunctionCallingConventionDefinition(const CallingConvention::Definition::Ptr &dflt =
1968  CallingConvention::Definition::Ptr()) const /*final*/;
1969 
1978  void fixInterFunctionEdges() /*final*/;
1979  void fixInterFunctionEdge(const ControlFlowGraph::ConstEdgeIterator&) /*final*/;
1999  bool functionIsNoop(const Function::Ptr&) const /*final*/;
2000 
2006  void allFunctionIsNoop() const /*final*/;
2007 
2015  void forgetFunctionIsNoop() const /*final*/;
2016  void forgetFunctionIsNoop(const Function::Ptr&) const /*final*/;
2021  //
2024  // Callbacks
2025  //
2028 public:
2046  CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() /*final*/ { return cfgAdjustmentCallbacks_; }
2047  const CfgAdjustmentCallbacks& cfgAdjustmentCallbacks() const /*final*/ { return cfgAdjustmentCallbacks_; }
2058  BasicBlockCallbacks& basicBlockCallbacks() /*final*/ { return basicBlockCallbacks_; }
2059  const BasicBlockCallbacks& basicBlockCallbacks() const /*final*/ { return basicBlockCallbacks_; }
2062 public:
2070  FunctionPrologueMatchers& functionPrologueMatchers() /*final*/ { return functionPrologueMatchers_; }
2071  const FunctionPrologueMatchers& functionPrologueMatchers() const /*final*/ { return functionPrologueMatchers_; }
2093  std::vector<Function::Ptr> nextFunctionPrologue(rose_addr_t startVa) /*final*/;
2094 
2095 public:
2101  FunctionPaddingMatchers& functionPaddingMatchers() /*final*/ { return functionPaddingMatchers_; }
2102  const FunctionPaddingMatchers& functionPaddingMatchers() const /*final*/ { return functionPaddingMatchers_; }
2112  DataBlock::Ptr matchFunctionPadding(const Function::Ptr&) /*final*/;
2113 
2114 
2115 
2118  //
2119  // Partitioner miscellaneous
2120  //
2123 public:
2135  void dumpCfg(std::ostream&, const std::string &prefix="", bool showBlocks=true,
2136  bool computeProperties=true) const /*final*/;
2137 
2151  void cfgGraphViz(std::ostream&, const AddressInterval &restrict = AddressInterval::whole(),
2152  bool showNeighbors=true) const /*final*/;
2153 
2159  static std::string vertexName(const ControlFlowGraph::Vertex&) /*final*/;
2160  std::string vertexName(const ControlFlowGraph::ConstVertexIterator&) const /*final*/;
2166  static std::string vertexNameEnd(const ControlFlowGraph::Vertex&) /*final*/;
2167 
2173  static std::string edgeNameSrc(const ControlFlowGraph::Edge&) /*final*/;
2174  std::string edgeNameSrc(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2182  static std::string edgeNameDst(const ControlFlowGraph::Edge&) /*final*/;
2183  std::string edgeNameDst(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2191  static std::string edgeName(const ControlFlowGraph::Edge&) /*final*/;
2192  std::string edgeName(const ControlFlowGraph::ConstEdgeIterator&) const /*final*/;
2198  static std::string basicBlockName(const BasicBlock::Ptr&) /*final*/;
2199 
2203  static std::string dataBlockName(const DataBlock::Ptr&) /*final*/;
2204 
2208  static std::string functionName(const Function::Ptr&) /*final*/;
2209 
2217  const BasePartitionerSettings& settings() const /*final*/ { return settings_; }
2218  void settings(const BasePartitionerSettings &s) /*final*/ { settings_ = s; }
2238  Progress::Ptr progress() const /*final*/;
2239  void progress(const Progress::Ptr&) /*final*/;
2246  void updateProgress(const std::string &phase, double completion) const;
2247 
2256  void enableSymbolicSemantics(bool b=true) /*final*/ { settings_.usingSemantics = b; }
2257  void disableSymbolicSemantics() /*final*/ { settings_.usingSemantics = false; }
2258  bool usingSymbolicSemantics() const /*final*/ { return settings_.usingSemantics; }
2282  void autoAddCallReturnEdges(bool b) /*final*/ { autoAddCallReturnEdges_ = b; }
2283  bool autoAddCallReturnEdges() const /*final*/ { return autoAddCallReturnEdges_; }
2300  void assumeFunctionsReturn(bool b) /*final*/ { assumeFunctionsReturn_ = b; }
2301  bool assumeFunctionsReturn() const /*final*/ { return assumeFunctionsReturn_; }
2313  void addressName(rose_addr_t, const std::string&) /*final*/;
2314  const std::string& addressName(rose_addr_t va) const /*final*/ { return addressNames_.getOrDefault(va); }
2315  const AddressNameMap& addressNames() const /*final*/ { return addressNames_; }
2325  bool checkingCallBranch() const /*final*/ { return settings_.checkingCallBranch; }
2326  void checkingCallBranch(bool b) /*final*/ { settings_.checkingCallBranch = b; }
2330  //
2333  // Instruction semantics
2334  //
2337 public:
2347  SemanticMemoryParadigm semanticMemoryParadigm() const { return semanticMemoryParadigm_; }
2348  void semanticMemoryParadigm(SemanticMemoryParadigm p) { semanticMemoryParadigm_ = p; }
2357  SMTSolver *smtSolver() const /*final*/ { return solver_; }
2358 
2369  BaseSemantics::RiscOperatorsPtr newOperators() const /*final*/;
2370  BaseSemantics::RiscOperatorsPtr newOperators(SemanticMemoryParadigm) const /*final*/;
2380  BaseSemantics::DispatcherPtr newDispatcher(const BaseSemantics::RiscOperatorsPtr&) const /*final*/;
2381 
2382 
2383 
2384 
2387  //
2388  // Partitioner internal utilities
2389  //
2392 private:
2393  void init(Disassembler*, const MemoryMap::Ptr&);
2394  void init(const Partitioner&);
2395  void updateCfgProgress();
2396 
2397 private:
2398  // Convert a CFG vertex iterator from one partitioner to another. This is called during copy construction when the source
2399  // and destination CFGs are identical.
2400  ControlFlowGraph::VertexIterator convertFrom(const Partitioner &other,
2401  ControlFlowGraph::ConstVertexIterator otherIter);
2402 
2403  // Adjusts edges for a placeholder vertex. This method erases all outgoing edges for the specified placeholder vertex and
2404  // then inserts a single edge from the placeholder to the special "undiscovered" vertex. */
2405  ControlFlowGraph::EdgeIterator adjustPlaceholderEdges(const ControlFlowGraph::VertexIterator &placeholder);
2406 
2407  // Adjusts edges for a non-existing basic block. This method erases all outgoing edges for the specified vertex and
2408  // then inserts a single edge from the vertex to the special "non-existing" vertex. */
2409  ControlFlowGraph::EdgeIterator adjustNonexistingEdges(const ControlFlowGraph::VertexIterator &vertex);
2410 
2411  // Implementation for the discoverBasicBlock methods. The startVa must not be the address of an existing placeholder.
2412  BasicBlock::Ptr discoverBasicBlockInternal(rose_addr_t startVa) const;
2413 
2414  // Checks consistency of internal data structures when debugging is enable (when NDEBUG is not defined).
2415  void checkConsistency() const;
2416 
2417  // This method is called whenever a new placeholder is inserted into the CFG or a new basic block is attached to the
2418  // CFG/AUM. The call happens immediately after the CFG/AUM are updated.
2419  void bblockAttached(const ControlFlowGraph::VertexIterator &newVertex);
2420 
2421  // This method is called whenever a basic block is detached from the CFG/AUM or when a placeholder is erased from the CFG.
2422  // The call happens immediately after the CFG/AUM are updated.
2423  void bblockDetached(rose_addr_t startVa, const BasicBlock::Ptr &removedBlock);
2424 
2425  // Rebuild the vertexIndex_ and other cache-like data members from the control flow graph
2426  void rebuildVertexIndices();
2427 };
2428 
2429 } // namespace
2430 } // namespace
2431 } // namespace
2432 
2433 #endif
Represents information about a thunk.
Definition: Partitioner.h:297
size_t size() const
Number of addresses represented by the map.
Sawyer::Callbacks< BasicBlockCallback::Ptr > BasicBlockCallbacks
See basicBlockCallbacks.
Definition: Partitioner.h:292
const FunctionPrologueMatchers & functionPrologueMatchers() const
Ordered list of function prologue matchers.
Definition: Partitioner.h:2071
BasicBlock::Ptr bblock
The one and only basic block for the thunk.
Definition: Partitioner.h:298
Optional< Value > getOptional(const Key &key) const
Lookup and return a value or nothing.
Definition: Sawyer/Map.h:486
FunctionPrologueMatchers & functionPrologueMatchers()
Ordered list of function prologue matchers.
Definition: Partitioner.h:2070
const std::string & addressName(rose_addr_t va) const
Property: Name for address.
Definition: Partitioner.h:2314
void disableSymbolicSemantics()
Use or not use symbolic semantics.
Definition: Partitioner.h:2257
void checkingCallBranch(bool b)
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2326
ControlFlowGraph::ConstVertexIterator indeterminateVertex() const
Returns the special "indeterminate" vertex.
Definition: Partitioner.h:575
const Configuration & configuration() const
Configuration information.
Definition: Partitioner.h:459
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:730
const FunctionPaddingMatchers & functionPaddingMatchers() const
Ordered list of function padding matchers.
Definition: Partitioner.h:2102
const ControlFlowGraph & cfg() const
Returns the control flow graph.
Definition: Partitioner.h:603
Provides and caches instructions.
Settings that directly control a partitioner.
Definition: BasicTypes.h:239
STL namespace.
Holds a value or nothing.
Definition: Optional.h:49
ControlFlowGraph::VertexIterator indeterminateVertex()
Returns the special "indeterminate" vertex.
Definition: Partitioner.h:572
InstructionProvider & instructionProvider()
Returns the instruction provider.
Definition: Partitioner.h:467
boost::iterator_range< VertexIterator > vertices()
Iterators for all vertices.
Definition: Graph.h:1454
Main namespace for the ROSE library.
Bidirectional vertex node iterator.
Definition: Graph.h:1036
Sawyer::Container::Map< rose_addr_t, std::string > AddressNameMap
Map address to name.
Definition: Partitioner.h:304
BasicBlockCallbacks & basicBlockCallbacks()
Callbacks for adjusting basic block during discovery.
Definition: Partitioner.h:2058
ControlFlowGraph::VertexIterator undiscoveredVertex()
Returns the special "undiscovered" vertex.
Definition: Partitioner.h:554
SemanticMemoryParadigm semanticMemoryParadigm() const
Property: Whether to use map- or list-based memory states.
Definition: Partitioner.h:2347
Name space for the entire library.
Definition: Access.h:11
Configuration & configuration()
Configuration information.
Definition: Partitioner.h:458
bool assumeFunctionsReturn() const
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2301
ControlFlowGraph::ConstVertexIterator findPlaceholder(rose_addr_t startVa) const
Find the CFG vertex for a basic block placeholder.
Definition: Partitioner.h:848
const AddressUsageMap & aum() const
Returns the address usage map.
Definition: Partitioner.h:611
const AddressNameMap & addressNames() const
Property: Name for address.
Definition: Partitioner.h:2315
Base classes for instruction semantics.
void settings(const BasePartitionerSettings &s)
Partitioner settings.
Definition: Partitioner.h:2218
void assumeFunctionsReturn(bool b)
Property: Assume (or not) that function calls return.
Definition: Partitioner.h:2300
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:299
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:589
bool isDefaultConstructed() const
Return true if this is a default constructed partitioner.
Definition: Partitioner.h:446
bool usingSymbolicSemantics() const
Use or not use symbolic semantics.
Definition: Partitioner.h:2258
SemanticMemoryParadigm
Organization of semantic memory.
Definition: BasicTypes.h:60
An efficient mapping from an address space to stored data.
Definition: MemoryMap.h:96
const BasicBlockCallbacks & basicBlockCallbacks() const
Callbacks for adjusting basic block during discovery.
Definition: Partitioner.h:2059
void basicBlockSemanticsAutoDrop(bool b)
Property: Automatically drop semantics for attached basic blocks.
Definition: Partitioner.h:914
const Value & getOrDefault(const Key &key) const
Lookup and return a value or a default.
Definition: Sawyer/Map.h:513
void stackDeltaInterproceduralLimit(size_t n)
Property: max depth for inter-procedural stack delta analysis.
Definition: Partitioner.h:1326
const InstructionProvider & instructionProvider() const
Returns the instruction provider.
Definition: Partitioner.h:468
bool checkingCallBranch
Check for situations where CALL is used as a branch.
Definition: BasicTypes.h:244
bool usingSemantics
Whether instruction semantics are used.
Definition: BasicTypes.h:240
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:2282
bool basicBlockSemanticsAutoDrop() const
Property: Automatically drop semantics for attached basic blocks.
Definition: Partitioner.h:913
bool basicBlockSemanticsAutoDrop
Conserve memory by dropping semantics for attached basic blocks.
Definition: BasicTypes.h:245
ControlFlowGraph::ConstVertexIterator undiscoveredVertex() const
Returns the special "undiscovered" vertex.
Definition: Partitioner.h:557
ControlFlowGraph::ConstVertexIterator nonexistingVertex() const
Returns the special "non-existing" vertex.
Definition: Partitioner.h:592
std::vector< FunctionPrologueMatcher::Ptr > FunctionPrologueMatchers
See functionPrologueMatchers.
Definition: Partitioner.h:293
FunctionPaddingMatchers & functionPaddingMatchers()
Ordered list of function padding matchers.
Definition: Partitioner.h:2101
const CfgAdjustmentCallbacks & cfgAdjustmentCallbacks() const
List of all callbacks invoked when the CFG is adjusted.
Definition: Partitioner.h:2047
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:480
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:289
SMTSolver * smtSolver() const
SMT solver.
Definition: Partitioner.h:2357
bool checkingCallBranch() const
Property: Whether to look for function calls used as branches.
Definition: Partitioner.h:2325
bool addressIsExecutable(rose_addr_t va) const
Returns true if address is executable.
Definition: Partitioner.h:486
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:41
size_t size() const
Number of nodes, keys, or values in this container.
Definition: Sawyer/Map.h:322
bool autoAddCallReturnEdges() const
Property: Insert (or not) function call return edges.
Definition: Partitioner.h:2283
Sawyer::Callbacks< CfgAdjustmentCallback::Ptr > CfgAdjustmentCallbacks
See cfgAdjustmentCallbacks.
Definition: Partitioner.h:291
std::vector< FunctionPaddingMatcher::Ptr > FunctionPaddingMatchers
See functionPaddingMatchers.
Definition: Partitioner.h:294
void semanticMemoryParadigm(SemanticMemoryParadigm p)
Property: Whether to use map- or list-based memory states.
Definition: Partitioner.h:2348
Interface to Satisfiability Modulo Theory (SMT) solvers.
Definition: SMTSolver.h:19
Holds configuration information.
Definition: Config.h:210