ROSE  0.11.57.0
InstructionProvider.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner2_InstructionProvider_H
2 #define ROSE_BinaryAnalysis_Partitioner2_InstructionProvider_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/Disassembler.h>
7 #include <Rose/BinaryAnalysis/InstructionSemantics2/BaseSemantics.h>
8 #include "AstSerialization.h"
9 
10 #include <boost/serialization/access.hpp>
11 #include <Sawyer/Assert.h>
12 #include <Sawyer/HashMap.h>
13 #include <Sawyer/SharedPointer.h>
14 
15 namespace Rose {
16 namespace BinaryAnalysis {
17 
31 public:
34 
37 
38 private:
39  Disassembler *disassembler_;
40  MemoryMap::Ptr memMap_;
41  mutable InsnMap insnMap_; // this is a cache
42  bool useDisassembler_;
43 
44 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
45 private:
46  friend class boost::serialization::access;
47 
48  template<class S>
49  void save(S &s, const unsigned /*version*/) const {
50  roseAstSerializationRegistration(s); // so we can save instructions through SgAsmInstruction base ptrs
51  bool hasDisassembler = disassembler_ != NULL;
52  s <<BOOST_SERIALIZATION_NVP(hasDisassembler);
53  s <<BOOST_SERIALIZATION_NVP(useDisassembler_);
54  s <<BOOST_SERIALIZATION_NVP(memMap_);
55  s <<BOOST_SERIALIZATION_NVP(insnMap_);
56  if (hasDisassembler) {
57  std::string disName = disassembler_->name();
58  s <<BOOST_SERIALIZATION_NVP(disName);
59  }
60  }
61 
62  template<class S>
63  void load(S &s, const unsigned /*version*/) {
64  roseAstSerializationRegistration(s);
65  bool hasDisassembler = false;
66  s >>BOOST_SERIALIZATION_NVP(hasDisassembler);
67  s >>BOOST_SERIALIZATION_NVP(useDisassembler_);
68  s >>BOOST_SERIALIZATION_NVP(memMap_);
69  s >>BOOST_SERIALIZATION_NVP(insnMap_);
70  if (hasDisassembler) {
71  std::string disName;
72  s >>BOOST_SERIALIZATION_NVP(disName);
73  disassembler_ = Disassembler::lookup(disName);
74  ASSERT_not_null2(disassembler_, "disassembler name=" + disName);
75  }
76  }
77 
78  BOOST_SERIALIZATION_SPLIT_MEMBER();
79 #endif
80 
81 protected:
82  InstructionProvider()
83  : disassembler_(NULL), useDisassembler_(false) {
84  // Start off with a large map to reduce early rehashing. There will probably be a lot of instructions.
85  insnMap_.rehash(1000000);
86  }
87 
88  InstructionProvider(Disassembler *disassembler, const MemoryMap::Ptr &map)
89  : disassembler_(disassembler), memMap_(map), useDisassembler_(true) {
90  ASSERT_not_null(disassembler);
91  // Start off with a large map to reduce early rehashing. There will probably be a lot of instructions.
92  insnMap_.rehash(1000000);
93  }
94 
95 public:
106  static Ptr instance(Disassembler *disassembler, const MemoryMap::Ptr &map) {
107  return Ptr(new InstructionProvider(disassembler, map));
108  }
109 
116  bool isDisassemblerEnabled() const {
117  return useDisassembler_;
118  }
119  void enableDisassembler(bool enable=true) {
120  ASSERT_require(!enable || disassembler_);
121  useDisassembler_ = enable;
122  }
124  useDisassembler_ = false;
125  }
134  SgAsmInstruction* operator[](rose_addr_t va) const;
135 
140  void insert(SgAsmInstruction*);
141 
146  Disassembler* disassembler() const { return disassembler_; }
147 
154  size_t nCached() const { return insnMap_.size(); }
155 
157  const RegisterDictionary* registerDictionary() const { return disassembler_->registerDictionary(); }
158 
160  const CallingConvention::Dictionary& callingConventions() const { return disassembler_->callingConventions(); }
161 
164 
166  RegisterDescriptor stackPointerRegister() const { return disassembler_->stackPointerRegister(); }
167 
171  RegisterDescriptor stackFrameRegister() const { return disassembler_->stackFrameRegister(); }
172 
177  RegisterDescriptor callReturnRegister() const { return disassembler_->callReturnRegister(); }
178 
182  RegisterDescriptor stackSegmentRegister() const { return disassembler_->stackSegmentRegister(); }
183 
185  ByteOrder::Endianness defaultByteOrder() const { return disassembler_->byteOrder(); }
186 
188  size_t wordSize() const { return 8 * disassembler_->wordSizeBytes(); }
189 
191  size_t instructionAlignment() const { return disassembler_->instructionAlignment(); }
192 
199 
201  void showStatistics() const;
202 };
203 
204 } // namespace
205 } // namespace
206 
207 #endif
208 #endif
void registerDictionary(const RegisterDictionary *rdict)
Properties: Register dictionary.
Definition: Disassembler.h:250
RegisterDescriptor instructionPointerRegister() const
Register used as the instruction pointer.
Sawyer::Container::HashMap< rose_addr_t, SgAsmInstruction * > InsnMap
Mapping from address to instruction.
size_t wordSizeBytes() const
Property: Basic word size in bytes.
Definition: Disassembler.h:230
const CallingConvention::Dictionary & callingConventions() const
Returns the calling convention dictionary.
void disableDisassembler()
Enable or disable the disassembler.
Disassembler * disassembler() const
Returns 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.
const std::string & name() const
Property: Name by which disassembler is registered.
Definition: Disassembler.h:176
InstructionSemantics2::BaseSemantics::DispatcherPtr dispatcher() const
Instruction dispatcher.
virtual RegisterDescriptor stackFrameRegister() const
Returns the register that ponts to the stack frame.
Definition: Disassembler.h:280
Main namespace for the ROSE library.
RegisterDescriptor stackFrameRegister() const
Register used for function call frames.
virtual RegisterDescriptor stackSegmentRegister() const
Returns the segment register for accessing the stack.
Definition: Disassembler.h:286
ByteOrder::Endianness defaultByteOrder() const
Default memory byte order.
ByteOrder::Endianness byteOrder() const
Property: Byte order of instructions in memory.
Definition: Disassembler.h:223
size_t size() const
Number of nodes, keys, or values in this container.
Definition: HashMap.h:308
RegisterDescriptor callReturnRegister() const
Register holding a function call's return address.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
size_t instructionAlignment() const
Property: Instruction alignment requirement.
virtual RegisterDescriptor instructionPointerRegister() const
Returns the register that points to instructions.
Definition: Disassembler.h:268
virtual RegisterDescriptor callReturnRegister() const
Returns the register that holds the return address for a function.
Definition: Disassembler.h:293
std::vector< Definition::Ptr > Dictionary
A ordered collection of calling convention definitions.
Describes (part of) a physical CPU register.
bool isDisassemblerEnabled() const
Enable or disable the disassembler.
static Disassembler * lookup(SgAsmGenericHeader *)
Finds a suitable disassembler.
Sawyer::SharedPointer< InstructionProvider > Ptr
Shared-ownership pointer to an InstructionProvider.
void insert(SgAsmInstruction *)
Insert an instruction into the cache.
const RegisterDictionary * registerDictionary() const
Returns the register dictionary.
void rehash(size_t nBuckets)
Change number of buckets.
Definition: HashMap.h:334
const CallingConvention::Dictionary & callingConventions() const
Property: Calling convention dictionary.
Definition: Disassembler.h:262
Base class for reference counted objects.
Definition: SharedObject.h:64
const Rose::BinaryAnalysis::InstructionSemantics2::BaseSemantics::DispatcherPtr & dispatcher() const
Return an instruction semantics dispatcher if possible.
Definition: Disassembler.h:302
static Ptr instance(Disassembler *disassembler, const MemoryMap::Ptr &map)
Static allocating Constructor.
void enableDisassembler(bool enable=true)
Enable or disable the disassembler.
Defines registers available for a particular architecture.
Definition: Registers.h:37
virtual RegisterDescriptor stackPointerRegister() const
Returns the register that points to the stack.
Definition: Disassembler.h:274
SgAsmInstruction * operator[](rose_addr_t va) const
Returns the instruction at the specified virtual address, or null.
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:50
size_t instructionAlignment() const
Alignment requirement for instructions.
size_t nCached() const
Returns number of cached starting addresses.