ROSE  0.11.101.0
NativeSemantics.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics_NativeSemantics_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics_NativeSemantics_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/Debugger.h>
8 #include <Rose/BinaryAnalysis/InstructionSemantics/ConcreteSemantics.h>
9 
10 #include <boost/noncopyable.hpp>
11 #include <boost/filesystem.hpp>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 namespace InstructionSemantics {
16 
18 namespace NativeSemantics {
19 
21 // Value type
23 
26 
29 
32 
34 // Register state
36 
38 typedef boost::shared_ptr<class RegisterState> RegisterStatePtr;
39 
43 class RegisterState: public BaseSemantics::RegisterState, boost::noncopyable {
44 public:
47 
50 
51 private:
52  Debugger::Ptr process_;
53 
54  //----------------------------------------
55  // Real constructors
56  //----------------------------------------
57 protected:
58  RegisterState();
59 
61 
62  //----------------------------------------
63  // Static allocating constructors
64  //----------------------------------------
65 public:
66  ~RegisterState();
67 
69  static RegisterStatePtr instance();
70 
72  static RegisterStatePtr instance(const BaseSemantics::SValuePtr &protoval, const Debugger::Ptr &process);
73 
74  //----------------------------------------
75  // Virtual constructors
76  //----------------------------------------
77 public:
79  const RegisterDictionaryPtr&) const override;
80 
81  virtual BaseSemantics::RegisterStatePtr clone() const override;
82 
83  //----------------------------------------
84  // Dynamic pointer casts
85  //----------------------------------------
86 public:
87  static RegisterStatePtr promote(const BaseSemantics::RegisterStatePtr&);
88 
89  //----------------------------------------
90  // Additional properties
91  //----------------------------------------
92 public:
94  Debugger::Ptr process() const;
95 
96  //----------------------------------------
97  // Virtual function implementations
98  //----------------------------------------
99 public:
100  virtual void clear() override {}
101 
102  virtual void zero() override {
103  TODO("[Robb Matzke 2019-09-05]"); // set all registers to zero
104  }
105 
106  virtual bool merge(const BaseSemantics::RegisterStatePtr &other, BaseSemantics::RiscOperators *ops) override {
107  ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
108  }
109 
111  BaseSemantics::RiscOperators *ops) override {
112  return peekRegister(reg, dflt, ops);
113  }
114 
116  BaseSemantics::RiscOperators*) override;
117 
118  virtual void writeRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &value,
119  BaseSemantics::RiscOperators*) override;
120 
121  virtual void hash(Combinatorics::Hasher&, BaseSemantics::RiscOperators*) const override;
122 
123  virtual void print(std::ostream&, Formatter&) const override;
124 };
125 
127 // Memory state
129 
131 typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
132 
136 class MemoryState: public BaseSemantics::MemoryState, boost::noncopyable {
137 public:
140 
143 
144 private:
145  Debugger::Ptr process_;
146 
147  //----------------------------------------
148  // Real constructors
149  //----------------------------------------
150 protected:
151  MemoryState();
152 
153  MemoryState(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval,
154  const Debugger::Ptr &process);
155 
156  //----------------------------------------
157  // Static allocating constructors
158  //----------------------------------------
159 public:
160  ~MemoryState();
161 
163  static MemoryStatePtr instance();
164 
166  static MemoryStatePtr instance(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval,
167  const Debugger::Ptr &process);
168 
169  //----------------------------------------
170  // Virtual constructors
171  //----------------------------------------
172 public:
174  const BaseSemantics::SValuePtr &valProtoval) const override;
175 
176  virtual BaseSemantics::MemoryStatePtr clone() const override;
177 
178  //----------------------------------------
179  // Dynamic pointer casts
180  //----------------------------------------
181 public:
182  static MemoryStatePtr promote(const BaseSemantics::MemoryStatePtr&);
183 
184  //----------------------------------------
185  // Additional properties
186  //----------------------------------------
187 public:
189  Debugger::Ptr process() const;
190 
191  //----------------------------------------
192  // Virtual function implementations
193  //----------------------------------------
194 public:
195  virtual void clear() override {}
196 
198  BaseSemantics::RiscOperators *valOps) override {
199  ASSERT_not_implemented("not applicable for this class");
200  }
201 
204  BaseSemantics::RiscOperators *valOps) override {
205  return peekMemory(address, dflt, addrOps, valOps);
206  }
207 
210  BaseSemantics::RiscOperators *valOps) override;
211 
212  virtual void writeMemory(const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &value,
213  BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override {
214  ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
215  }
216 
218  BaseSemantics::RiscOperators *valOps) const override {
219  ASSERT_not_implemented("[Robb Matzke 2021-03-26]");
220  }
221 
222  virtual void print(std::ostream&, BaseSemantics::Formatter&) const override {
223  ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
224  }
225 };
226 
228 // Complete semantic state
230 
232 typedef boost::shared_ptr<class State> StatePtr;
233 
238 class State: public ConcreteSemantics::State, boost::noncopyable {
239 public:
242 
244  using Ptr = StatePtr;
245 
246  //----------------------------------------
247  // Real constructors
248  //----------------------------------------
249 protected:
251  : ConcreteSemantics::State(registers, memory) {
252  (void) RegisterState::promote(registers);
253  (void) MemoryState::promote(memory);
254  }
255 
256  //----------------------------------------
257  // Static allocating constructors
258  //----------------------------------------
259 public:
260  static StatePtr instance(const BaseSemantics::RegisterStatePtr &registers, const BaseSemantics::MemoryStatePtr &memory) {
261  return StatePtr(new State(registers, memory));
262  }
263 };
264 
266 // RISC operators
268 
270 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
271 
273 public:
276 
279 
280  //----------------------------------------
281  // Real constructors
282  //----------------------------------------
283 protected:
284  explicit RiscOperators(const BaseSemantics::StatePtr&);
285 
286  //----------------------------------------
287  // Static allocating constructors
288  //----------------------------------------
289 public:
290  ~RiscOperators();
291 
296  static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const Debugger::Ptr &process);
297 
303  static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr&);
304 
305  //----------------------------------------
306  // Virtual constructors
307  //----------------------------------------
308 public:
310  const SmtSolverPtr &solver = SmtSolverPtr()) const override;
311 
312  //----------------------------------------
313  // Dynamic pointer casts
314  //----------------------------------------
315 public:
318  static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr&);
319 
320  //----------------------------------------
321  // Additional properties
322  //----------------------------------------
323 public:
328  return RegisterState::promote(currentState()->registerState())->process();
329  }
330 };
331 
333 // Dispatcher
335 
337 typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
338 
340 public:
343 
346 
347 private:
348  Debugger::Ptr process_;
349 
350  //----------------------------------------
351  // Real constructors
352  //----------------------------------------
353 protected:
355 
357 
358  //----------------------------------------
359  // Static allocating constructors
360  //----------------------------------------
361 public:
362  ~Dispatcher();
363 
365  static DispatcherPtr instance(const Debugger::Ptr &process, const BaseSemantics::SValuePtr &protoval = SValue::instance());
366 
368  static DispatcherPtr instance(const Debugger::Specimen&, const BaseSemantics::SValuePtr &protoval = SValue::instance());
369 
373  static DispatcherPtr instance(const BaseSemantics::RiscOperatorsPtr&);
374 
375  //----------------------------------------
376  // Virtual constructors
377  //----------------------------------------
378 public:
380  create(const BaseSemantics::RiscOperatorsPtr&, size_t addrWidth, const RegisterDictionaryPtr&) const override;
381 
382  //----------------------------------------
383  // Operations
384  //----------------------------------------
385 public:
394  virtual void processInstruction(SgAsmInstruction *insn) override;
395  void processInstruction(rose_addr_t va);
401  virtual SgAsmInstruction* currentInstruction() const override;
402 
403  virtual RegisterDescriptor instructionPointerRegister() const override;
404  virtual RegisterDescriptor stackPointerRegister() const override;
405  virtual RegisterDescriptor stackFrameRegister() const override;
406  virtual RegisterDescriptor callReturnRegister() const override;
407 
412  notApplicable("iprocReplace");
413  }
414  virtual void iprocSet(int key, BaseSemantics::InsnProcessor*) override {
415  notApplicable("iprocSet");
416  }
417  virtual int iprocKey(SgAsmInstruction*) const override {
418  notApplicable("iprocKey");
419  }
422 private:
423  [[noreturn]] void notApplicable(const std::string &what) const {
424  ASSERT_not_implemented(what + " is not applicable for this class");
425  }
426 };
427 
428 
429 } // namespace
430 } // namespace
431 } // namespace
432 } // namespace
433 
434 #endif
435 #endif
ConcreteSemantics::SValuePtr SValuePtr
Shared-ownership pointer to SValue.
virtual BaseSemantics::MemoryStatePtr create(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval) const override
Virtual allocating constructor.
virtual RegisterDescriptor callReturnRegister() const override
Returns the function call return address register.
virtual BaseSemantics::RegisterStatePtr clone() const override
Make a copy of this register state.
virtual bool merge(const BaseSemantics::MemoryStatePtr &other, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override
Merge memory states for data flow analysis.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
virtual BaseSemantics::SValuePtr peekMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override
Read a value from memory without side effects.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Base class for machine instructions.
virtual BaseSemantics::SValuePtr peekRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *) override
Read a register without side effects.
Debugger::Ptr process() const
Property: Subordinate process storing the registers.
static SValuePtr instance()
Instantiate a new prototypical value.
virtual int iprocKey(SgAsmInstruction *) const override
Disabled in this class.
virtual void writeRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &value, BaseSemantics::RiscOperators *) override
Write a value to a register.
static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const Debugger::Ptr &process)
Instantiate a new RiscOperators object.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
virtual void processInstruction(SgAsmInstruction *insn) override
Process an instruction.
Main namespace for the ROSE library.
boost::shared_ptr< class Dispatcher > DispatcherPtr
Shared-ownership pointer to Dispatcher.
static RegisterStatePtr instance()
Construct a state not attached to any subordinate process.
virtual BaseSemantics::DispatcherPtr create(const BaseSemantics::RiscOperatorsPtr &, size_t addrWidth, const RegisterDictionaryPtr &) const override
Virtual constructor.
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
Debugger::Ptr process() const
Property: Subordinate process storing the registers.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
ConcreteSemantics::SValue SValue
Concrete values from the specimen.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
virtual BaseSemantics::RegisterStatePtr create(const BaseSemantics::SValuePtr &protoval, const RegisterDictionaryPtr &) const override
Virtual constructor.
Dispatches instructions through the RISC layer.
Definition: Dispatcher.h:44
virtual BaseSemantics::SValuePtr readMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override
Read a value from memory.
virtual void clear() override
Removes stored values from the register state.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
ConcreteSemantics::Formatter Formatter
Formatter for printing values.
static DispatcherPtr instance(const Debugger::Ptr &process, const BaseSemantics::SValuePtr &protoval=SValue::instance())
Create a new dispatcher using the specified process.
Functor that knows how to dispatch a single kind of instruction.
Definition: Dispatcher.h:25
Describes (part of) a physical CPU register.
virtual SgAsmInstruction * currentInstruction() const override
Decode the current instruction.
Describes the specimen to be debugged.
Definition: Debugger.h:52
virtual BaseSemantics::SValuePtr readRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *ops) override
Read a value from a register.
virtual RegisterDescriptor stackFrameRegister() const override
Returns the stack call frame register.
virtual void iprocSet(int key, BaseSemantics::InsnProcessor *) override
Disabled in this class.
boost::shared_ptr< class MemoryState > MemoryStatePtr
Shared-ownership pointer to MemoryState.
virtual BaseSemantics::MemoryStatePtr clone() const override
Virtual allocating copy constructor.
Type of values manipulated by the concrete domain.
Binary analysis.
virtual void print(std::ostream &, BaseSemantics::Formatter &) const override
Print a memory state to more than one line of output.
virtual void hash(Combinatorics::Hasher &, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) const override
Calculate a hash for this memory state.
Debugger::Ptr process() const
Property: Process storing the state.
virtual SValuePtr protoval() const
Return the prototypical value.
boost::shared_ptr< class RegisterState > RegisterStatePtr
Shared-ownership pointer to RegisterState.
Defines RISC operators for the ConcreteSemantics domain.
virtual StatePtr currentState() const
Property: Current semantic state.
virtual void zero() override
Set all registers to the zero.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to RiscOperators.
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
boost::shared_ptr< class State > StatePtr
Shared-ownership pointer to State.
virtual RegisterDescriptor stackPointerRegister() const override
Returns the stack pointer register.
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:48
virtual bool merge(const BaseSemantics::RegisterStatePtr &other, BaseSemantics::RiscOperators *ops) override
Merge register states for data flow analysis.
virtual void iprocReplace(SgAsmInstruction *, BaseSemantics::InsnProcessor *) override
Disabled in this class.
virtual RegisterDescriptor instructionPointerRegister() const override
Returns the instruction pointer register.
Base class for semantics machine states.
Definition: State.h:39
static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr &)
Instantiate a new RiscOperators object.
virtual void print(std::ostream &, Formatter &) const override
Print the register contents.
static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &)
Run-time promotion of a base object to a NativeSemantics RiscOperators.
virtual void writeMemory(const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &value, BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) override
Write a value to memory.
static MemoryStatePtr instance()
Construct a state not attached to any subordinate process.
virtual void hash(Combinatorics::Hasher &, BaseSemantics::RiscOperators *) const override
Hash the register state.