ROSE  0.11.145.0
InstructionProvider.h
1 #ifndef ROSE_BinaryAnalysis_InstructionProvider_H
2 #define ROSE_BinaryAnalysis_InstructionProvider_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/CallingConvention.h>
8 #include <Rose/BinaryAnalysis/Disassembler/BasicTypes.h>
9 #include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics.h>
10 #include "AstSerialization.h"
11 
12 #include <boost/serialization/access.hpp>
13 #include <Sawyer/Assert.h>
14 #include <Sawyer/HashMap.h>
15 #include <Sawyer/SharedPointer.h>
16 
17 namespace Rose {
18 namespace BinaryAnalysis {
19 
33 public:
36 
39 
40 private:
41  Disassembler::BasePtr disassembler_;
42  MemoryMap::Ptr memMap_;
43  mutable InsnMap insnMap_; // this is a cache
44  bool useDisassembler_;
45 
46 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
47 private:
48  friend class boost::serialization::access;
49 
50  template<class S>
51  void save(S &s, const unsigned /*version*/) const {
52  roseAstSerializationRegistration(s); // so we can save instructions through SgAsmInstruction base ptrs
53  bool hasDisassembler = disassembler_ != NULL;
54  s <<BOOST_SERIALIZATION_NVP(hasDisassembler);
55  s <<BOOST_SERIALIZATION_NVP(useDisassembler_);
56  s <<BOOST_SERIALIZATION_NVP(memMap_);
57 
58  const size_t mapSize = insnMap_.size();
59  s <<BOOST_SERIALIZATION_NVP(mapSize);
60  for (const auto &insn: insnMap_.values())
61  saveAst(s, insn);
62 
63  if (hasDisassembler) {
64  std::string disName = Disassembler::name(disassembler_);
65  s <<BOOST_SERIALIZATION_NVP(disName);
66  }
67  }
68 
69  template<class S>
70  void load(S &s, const unsigned /*version*/) {
71  roseAstSerializationRegistration(s);
72  bool hasDisassembler = false;
73  s >>BOOST_SERIALIZATION_NVP(hasDisassembler);
74  s >>BOOST_SERIALIZATION_NVP(useDisassembler_);
75  s >>BOOST_SERIALIZATION_NVP(memMap_);
76 
77  size_t mapSize;
78  s >>BOOST_SERIALIZATION_NVP(mapSize);
79  for (size_t i = 0; i < mapSize; ++i) {
80  SgNode *node = restoreAst(s);
81  auto insn = isSgAsmInstruction(node);
82  ASSERT_require(!node || insn);
83  insnMap_.insert(insn->get_address(), insn);
84  }
85 
86  if (hasDisassembler) {
87  std::string disName;
88  s >>BOOST_SERIALIZATION_NVP(disName);
89  disassembler_ = Disassembler::lookup(disName);
90  ASSERT_not_null2(disassembler_, "disassembler name=" + disName);
91  }
92  }
93 
94  BOOST_SERIALIZATION_SPLIT_MEMBER();
95 #endif
96 
97 protected:
98  InstructionProvider();
99 
100  InstructionProvider(const Disassembler::BasePtr &disassembler, const MemoryMap::Ptr &map);
101 
102 public:
103  ~InstructionProvider();
104 
116  return Ptr(new InstructionProvider(disassembler, map));
117  }
118 
125  bool isDisassemblerEnabled() const {
126  return useDisassembler_;
127  }
128  void enableDisassembler(bool enable=true);
130  useDisassembler_ = false;
131  }
140  SgAsmInstruction* operator[](rose_addr_t va) const;
141 
146  void insert(SgAsmInstruction*);
147 
153 
160  size_t nCached() const { return insnMap_.size(); }
161 
164 
167 
170 
173 
178 
184 
189 
191  ByteOrder::Endianness defaultByteOrder() const;
192 
194  size_t wordSize() const;
195 
197  size_t instructionAlignment() const;
198 
205 
207  void showStatistics() const;
208 };
209 
210 } // namespace
211 } // namespace
212 
213 #endif
214 #endif
RegisterDescriptor instructionPointerRegister() const
Register used as the instruction pointer.
InstructionSemantics::BaseSemantics::DispatcherPtr dispatcher() const
Instruction dispatcher.
Sawyer::Container::HashMap< rose_addr_t, SgAsmInstruction * > InsnMap
Mapping from address to instruction.
boost::iterator_range< ValueIterator > values()
Iterators for container values.
Definition: HashMap.h:292
const CallingConvention::Dictionary & callingConventions() const
Returns the calling convention dictionary.
void disableDisassembler()
Enable or disable the disassembler.
RegisterDescriptor stackSegmentRegister() const
Register used as a segment to access stack memory.
size_t wordSize() const
Word size in bits.
void showStatistics() const
Print some partitioner performance statistics.
Base class for machine instructions.
Provides and caches instructions.
RegisterDescriptor stackPointerRegister() const
Register used as a user-mode stack pointer.
Disassembler::BasePtr disassembler() const
Returns the disassembler.
Main namespace for the ROSE library.
RegisterDescriptor stackFrameRegister() const
Register used for function call frames.
ByteOrder::Endianness defaultByteOrder() const
Default memory byte order.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
size_t size() const
Number of nodes, keys, or values in this container.
Definition: HashMap.h:316
RegisterDescriptor callReturnRegister() const
Register holding a function call's return address.
MemoryMapPtr Ptr
Reference counting pointer.
Definition: MemoryMap.h:115
std::vector< Definition::Ptr > Dictionary
A ordered collection of calling convention definitions.
HashMap & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Definition: HashMap.h:485
Describes (part of) a physical CPU register.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9846
static Ptr instance(const Disassembler::BasePtr &disassembler, const MemoryMap::Ptr &map)
Static allocating Constructor.
bool isDisassemblerEnabled() const
Enable or disable the disassembler.
RegisterDictionaryPtr registerDictionary() const
Returns the register dictionary.
Sawyer::SharedPointer< InstructionProvider > Ptr
Shared-ownership pointer.
void insert(SgAsmInstruction *)
Insert an instruction into the cache.
const std::string & name(const BasePtr &)
Disassembler name free function.
Base class for reference counted objects.
Definition: SharedObject.h:64
BasePtr lookup(SgAsmGenericHeader *)
Finds a suitable disassembler for a file header.
void enableDisassembler(bool enable=true)
Enable or disable the disassembler.
Sawyer::SharedPointer< Base > BasePtr
Reference counted pointer for disassemblers.
SgAsmInstruction * operator[](rose_addr_t va) const
Returns the instruction at the specified virtual address, or null.
size_t instructionAlignment() const
Alignment requirement for instructions.
size_t nCached() const
Returns number of cached starting addresses.