ROSE  0.11.2.0
InstructionProvider.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner2_InstructionProvider_H
2 #define ROSE_BinaryAnalysis_Partitioner2_InstructionProvider_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include "Disassembler.h"
8 #include "BaseSemantics2.h"
9 #include "AstSerialization.h"
10 
11 #include <boost/serialization/access.hpp>
12 #include <Sawyer/Assert.h>
13 #include <Sawyer/HashMap.h>
14 #include <Sawyer/SharedPointer.h>
15 
16 namespace Rose {
17 namespace BinaryAnalysis {
18 
32 public:
35 
38 
39 private:
40  Disassembler *disassembler_;
41  MemoryMap::Ptr memMap_;
42  mutable InsnMap insnMap_; // this is a cache
43  bool useDisassembler_;
44 
45 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
46 private:
47  friend class boost::serialization::access;
48 
49  template<class S>
50  void save(S &s, const unsigned /*version*/) const {
51  roseAstSerializationRegistration(s); // so we can save instructions through SgAsmInstruction base ptrs
52  bool hasDisassembler = disassembler_ != NULL;
53  s <<BOOST_SERIALIZATION_NVP(hasDisassembler);
54  s <<BOOST_SERIALIZATION_NVP(useDisassembler_);
55  s <<BOOST_SERIALIZATION_NVP(memMap_);
56  s <<BOOST_SERIALIZATION_NVP(insnMap_);
57  if (hasDisassembler) {
58  std::string disName = disassembler_->name();
59  s <<BOOST_SERIALIZATION_NVP(disName);
60  }
61  }
62 
63  template<class S>
64  void load(S &s, const unsigned /*version*/) {
65  roseAstSerializationRegistration(s);
66  bool hasDisassembler = false;
67  s >>BOOST_SERIALIZATION_NVP(hasDisassembler);
68  s >>BOOST_SERIALIZATION_NVP(useDisassembler_);
69  s >>BOOST_SERIALIZATION_NVP(memMap_);
70  s >>BOOST_SERIALIZATION_NVP(insnMap_);
71  if (hasDisassembler) {
72  std::string disName;
73  s >>BOOST_SERIALIZATION_NVP(disName);
74  disassembler_ = Disassembler::lookup(disName);
75  ASSERT_not_null2(disassembler_, "disassembler name=" + disName);
76  }
77  }
78 
79  BOOST_SERIALIZATION_SPLIT_MEMBER();
80 #endif
81 
82 protected:
83  InstructionProvider()
84  : disassembler_(NULL), useDisassembler_(false) {
85  // Start off with a large map to reduce early rehashing. There will probably be a lot of instructions.
86  insnMap_.rehash(1000000);
87  }
88 
89  InstructionProvider(Disassembler *disassembler, const MemoryMap::Ptr &map)
90  : disassembler_(disassembler), memMap_(map), useDisassembler_(true) {
91  ASSERT_not_null(disassembler);
92  // Start off with a large map to reduce early rehashing. There will probably be a lot of instructions.
93  insnMap_.rehash(1000000);
94  }
95 
96 public:
107  static Ptr instance(Disassembler *disassembler, const MemoryMap::Ptr &map) {
108  return Ptr(new InstructionProvider(disassembler, map));
109  }
110 
117  bool isDisassemblerEnabled() const {
118  return useDisassembler_;
119  }
120  void enableDisassembler(bool enable=true) {
121  ASSERT_require(!enable || disassembler_);
122  useDisassembler_ = enable;
123  }
125  useDisassembler_ = false;
126  }
135  SgAsmInstruction* operator[](rose_addr_t va) const;
136 
141  void insert(SgAsmInstruction*);
142 
147  Disassembler* disassembler() const { return disassembler_; }
148 
155  size_t nCached() const { return insnMap_.size(); }
156 
158  const RegisterDictionary* registerDictionary() const { return disassembler_->registerDictionary(); }
159 
161  const CallingConvention::Dictionary& callingConventions() const { return disassembler_->callingConventions(); }
162 
165 
167  RegisterDescriptor stackPointerRegister() const { return disassembler_->stackPointerRegister(); }
168 
172  RegisterDescriptor stackFrameRegister() const { return disassembler_->stackFrameRegister(); }
173 
178  RegisterDescriptor callReturnRegister() const { return disassembler_->callReturnRegister(); }
179 
183  RegisterDescriptor stackSegmentRegister() const { return disassembler_->stackSegmentRegister(); }
184 
186  ByteOrder::Endianness defaultByteOrder() const { return disassembler_->byteOrder(); }
187 
189  size_t wordSize() const { return 8 * disassembler_->wordSizeBytes(); }
190 
192  size_t instructionAlignment() const { return disassembler_->instructionAlignment(); }
193 
200 
202  void showStatistics() const;
203 };
204 
205 } // namespace
206 } // namespace
207 
208 #endif
209 #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:38
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.