ROSE  0.11.2.0
NativeSemantics.h
1 #ifndef Rose_BinaryAnalysis_NativeSemantics_H
2 #define Rose_BinaryAnalysis_NativeSemantics_H
3 #include <rosePublicConfig.h>
4 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
5 
6 #include <BinaryDebugger.h>
7 #include <ConcreteSemantics2.h>
8 
9 #include <boost/noncopyable.hpp>
10 #include <boost/filesystem.hpp>
11 
12 namespace Rose {
13 namespace BinaryAnalysis {
14 namespace InstructionSemantics2 {
15 
17 namespace NativeSemantics {
18 
20 // Value type
22 
25 
28 
31 
33 // Register state
35 
37 typedef boost::shared_ptr<class RegisterState> RegisterStatePtr;
38 
42 class RegisterState: public BaseSemantics::RegisterState, boost::noncopyable {
43  Debugger::Ptr process_;
44 
45  //----------------------------------------
46  // Real constructors
47  //----------------------------------------
48 protected:
49  RegisterState() {}
50 
52  : BaseSemantics::RegisterState(protoval, process->registerDictionary()), process_(process) {
53  ASSERT_not_null(process);
54  }
55 
56  //----------------------------------------
57  // Static allocating constructors
58  //----------------------------------------
59 public:
61  static RegisterStatePtr instance() {
62  return RegisterStatePtr(new RegisterState);
63  }
64 
66  static RegisterStatePtr instance(const BaseSemantics::SValuePtr &protoval, const Debugger::Ptr &process) {
67  ASSERT_not_null(protoval);
68  (void) SValue::promote(protoval);
69  return RegisterStatePtr(new RegisterState(protoval, process));
70  }
71 
72  //----------------------------------------
73  // Virtual constructors
74  //----------------------------------------
75 public:
76  virtual BaseSemantics::RegisterStatePtr create(const BaseSemantics::SValuePtr &protoval,
77  const RegisterDictionary *regdict) const ROSE_OVERRIDE {
78  ASSERT_not_implemented("not applicable for this class");
79  }
80 
81  virtual BaseSemantics::RegisterStatePtr clone() const ROSE_OVERRIDE {
82  ASSERT_not_implemented("not applicable for this class");
83  }
84 
85  //----------------------------------------
86  // Dynamic pointer casts
87  //----------------------------------------
88 public:
89  static RegisterStatePtr promote(const BaseSemantics::RegisterStatePtr &x) {
90  RegisterStatePtr retval = boost::dynamic_pointer_cast<RegisterState>(x);
91  ASSERT_not_null(retval);
92  return retval;
93  }
94 
95  //----------------------------------------
96  // Additional properties
97  //----------------------------------------
98 public:
101  return process_;
102  }
103 
104  //----------------------------------------
105  // Virtual function implementations
106  //----------------------------------------
107 public:
108  virtual void clear() ROSE_OVERRIDE {}
109 
110  virtual void zero() ROSE_OVERRIDE {
111  TODO("[Robb Matzke 2019-09-05]"); // set all registers to zero
112  }
113 
114  virtual bool merge(const BaseSemantics::RegisterStatePtr &other, BaseSemantics::RiscOperators *ops) ROSE_OVERRIDE {
115  ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
116  }
117 
118  virtual BaseSemantics::SValuePtr readRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt,
119  BaseSemantics::RiscOperators *ops) ROSE_OVERRIDE {
120  return peekRegister(reg, dflt, ops);
121  }
122 
123  virtual BaseSemantics::SValuePtr peekRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &dflt,
124  BaseSemantics::RiscOperators*) ROSE_OVERRIDE;
125 
126  virtual void writeRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &value,
127  BaseSemantics::RiscOperators*) ROSE_OVERRIDE;
128 
129  virtual void print(std::ostream&, Formatter&) const ROSE_OVERRIDE;
130 };
131 
133 // Memory state
135 
137 typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
138 
142 class MemoryState: public BaseSemantics::MemoryState, boost::noncopyable {
143  Debugger::Ptr process_;
144 
145  //----------------------------------------
146  // Real constructors
147  //----------------------------------------
148 protected:
149  MemoryState() {}
150 
151  MemoryState(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval,
152  const Debugger::Ptr &process)
153  : BaseSemantics::MemoryState(addrProtoval, valProtoval), process_(process) {
154  ASSERT_not_null(process);
155  }
156 
157  //----------------------------------------
158  // Static allocating constructors
159  //----------------------------------------
160 public:
162  static MemoryStatePtr instance() {
163  return MemoryStatePtr(new MemoryState);
164  }
165 
167  static MemoryStatePtr instance(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval,
168  const Debugger::Ptr &process) {
169  return MemoryStatePtr(new MemoryState(addrProtoval, valProtoval, process));
170  }
171 
172  //----------------------------------------
173  // Virtual constructors
174  //----------------------------------------
175 public:
176  virtual BaseSemantics::MemoryStatePtr create(const BaseSemantics::SValuePtr &addrProtoval,
177  const BaseSemantics::SValuePtr &valProtoval) const ROSE_OVERRIDE {
178  ASSERT_not_implemented("not applicable for this class");
179  }
180 
181  virtual BaseSemantics::MemoryStatePtr clone() const ROSE_OVERRIDE {
182  ASSERT_not_implemented("not applicable for this class");
183  }
184 
185  //----------------------------------------
186  // Dynamic pointer casts
187  //----------------------------------------
188 public:
189  static MemoryStatePtr promote(const BaseSemantics::MemoryStatePtr &x) {
190  MemoryStatePtr retval = boost::dynamic_pointer_cast<MemoryState>(x);
191  ASSERT_not_null(retval);
192  return retval;
193  }
194 
195  //----------------------------------------
196  // Additional properties
197  //----------------------------------------
198 public:
201  return process_;
202  }
203 
204  //----------------------------------------
205  // Virtual function implementations
206  //----------------------------------------
207 public:
208  virtual void clear() ROSE_OVERRIDE {}
209 
210  virtual bool merge(const BaseSemantics::MemoryStatePtr &other, BaseSemantics::RiscOperators *addrOps,
211  BaseSemantics::RiscOperators *valOps) ROSE_OVERRIDE {
212  ASSERT_not_implemented("not applicable for this class");
213  }
214 
215  virtual BaseSemantics::SValuePtr readMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt,
217  BaseSemantics::RiscOperators *valOps) ROSE_OVERRIDE {
218  return peekMemory(address, dflt, addrOps, valOps);
219  }
220 
221  virtual BaseSemantics::SValuePtr peekMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt,
223  BaseSemantics::RiscOperators *valOps) ROSE_OVERRIDE;
224 
225  virtual void writeMemory(const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &value,
226  BaseSemantics::RiscOperators *addrOps, BaseSemantics::RiscOperators *valOps) ROSE_OVERRIDE {
227  ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
228  }
229 
230  virtual void print(std::ostream&, BaseSemantics::Formatter&) const ROSE_OVERRIDE {
231  ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
232  }
233 };
234 
236 // Complete semantic state
238 
240 typedef boost::shared_ptr<class State> StatePtr;
241 
246 class State: public ConcreteSemantics::State, boost::noncopyable {
247  //----------------------------------------
248  // Real constructors
249  //----------------------------------------
250 protected:
252  : ConcreteSemantics::State(registers, memory) {
253  (void) RegisterState::promote(registers);
254  (void) MemoryState::promote(memory);
255  }
256 
257  //----------------------------------------
258  // Static allocating constructors
259  //----------------------------------------
260 public:
261  static StatePtr instance(const BaseSemantics::RegisterStatePtr &registers, const BaseSemantics::MemoryStatePtr &memory) {
262  return StatePtr(new State(registers, memory));
263  }
264 };
265 
267 // RISC operators
269 
271 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
272 
274  //----------------------------------------
275  // Real constructors
276  //----------------------------------------
277 protected:
278  explicit RiscOperators(const BaseSemantics::StatePtr &state)
280  name("Native");
281  }
282 
283  //----------------------------------------
284  // Static allocating constructors
285  //----------------------------------------
286 public:
291  static RiscOperatorsPtr instance(const BaseSemantics::SValuePtr &protoval, const Debugger::Ptr &process) {
292  RegisterStatePtr registers = RegisterState::instance(protoval, process);
293  MemoryStatePtr memory = MemoryState::instance(protoval, protoval, process);
294  StatePtr state = State::instance(registers, memory);
295  return RiscOperatorsPtr(new RiscOperators(state));
296  }
297 
303  static RiscOperatorsPtr instance(const BaseSemantics::StatePtr &state) {
304  (void) State::promote(state); // check that it's the correct type
305  return RiscOperatorsPtr(new RiscOperators(state));
306  }
307 
308  //----------------------------------------
309  // Virtual constructors
310  //----------------------------------------
311 public:
313  const SmtSolverPtr &solver = SmtSolverPtr()) const ROSE_OVERRIDE {
314  TODO("[Robb Matzke 2019-09-05]");
315  }
316 
317  //----------------------------------------
318  // Dynamic pointer casts
319  //----------------------------------------
320 public:
323  static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &x) {
324  RiscOperatorsPtr retval = boost::dynamic_pointer_cast<RiscOperators>(x);
325  ASSERT_not_null(retval);
326  return retval;
327  }
328 
329  //----------------------------------------
330  // Additional properties
331  //----------------------------------------
332 public:
337  return RegisterState::promote(currentState()->registerState())->process();
338  }
339 };
340 
342 // Dispatcher
344 
346 typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
347 
349  Debugger::Ptr process_;
350 
351  //----------------------------------------
352  // Real constructors
353  //----------------------------------------
354 protected:
355  Dispatcher(const Debugger::Ptr &process, const BaseSemantics::SValuePtr &protoval)
356  : process_(process) {
357  set_register_dictionary(process_->registerDictionary());
358  addressWidth(process_->kernelWordSize());
359  operators = RiscOperators::instance(protoval, process_);
360  }
361 
363  : process_(RiscOperators::promote(ops)->process()) {
364  set_register_dictionary(process_->registerDictionary());
365  addressWidth(process_->kernelWordSize());
366  operators = ops;
367  }
368 
369  //----------------------------------------
370  // Static allocating constructors
371  //----------------------------------------
372 public:
374  static DispatcherPtr instance(const Debugger::Ptr &process,
375  const BaseSemantics::SValuePtr &protoval = SValue::instance()) {
376  return DispatcherPtr(new Dispatcher(process, protoval));
377  }
378 
380  static DispatcherPtr instance(const Debugger::Specimen &specimen,
381  const BaseSemantics::SValuePtr &protoval = SValue::instance()) {
382  Debugger::Ptr process = Debugger::instance(specimen);
383  return DispatcherPtr(new Dispatcher(process, protoval));
384  }
385 
389  static DispatcherPtr instance(const BaseSemantics::RiscOperatorsPtr &ops) {
390  (void) RiscOperators::promote(ops); // check type
391  return DispatcherPtr(new Dispatcher(ops));
392  }
393 
394  //----------------------------------------
395  // Virtual constructors
396  //----------------------------------------
397 public:
399  create(const BaseSemantics::RiscOperatorsPtr &ops, size_t addrWidth=0,
400  const RegisterDictionary *regs=NULL) const ROSE_OVERRIDE {
401  notApplicable("create");
402  }
403 
404  //----------------------------------------
405  // Operations
406  //----------------------------------------
407 public:
416  virtual void processInstruction(SgAsmInstruction *insn) ROSE_OVERRIDE;
417  void processInstruction(rose_addr_t va);
423  virtual SgAsmInstruction* currentInstruction() const ROSE_OVERRIDE;
424 
425  virtual RegisterDescriptor instructionPointerRegister() const ROSE_OVERRIDE;
426  virtual RegisterDescriptor stackPointerRegister() const ROSE_OVERRIDE;
427  virtual RegisterDescriptor callReturnRegister() const ROSE_OVERRIDE;
428 
432  virtual void iproc_replace(SgAsmInstruction*, BaseSemantics::InsnProcessor*) ROSE_OVERRIDE {
433  notApplicable("iproc_replace");
434  }
435  virtual void iproc_set(int key, BaseSemantics::InsnProcessor*) ROSE_OVERRIDE {
436  notApplicable("iproc_set");
437  }
438  virtual int iproc_key(SgAsmInstruction*) const ROSE_OVERRIDE {
439  notApplicable("iproc_key");
440  }
443 private:
444  void notApplicable(const std::string &what) const SAWYER_ATTR_NORETURN {
445  ASSERT_not_implemented(what + " is not applicable for this class");
446  }
447 };
448 
449 
450 } // namespace
451 } // namespace
452 } // namespace
453 } // namespace
454 
455 #endif
456 #endif
Debugger::Ptr process() const
Property: Subordinate process storing the registers.
static RegisterStatePtr instance()
Construct a state not attached to any subordinate process.
Defines RISC operators for the ConcreteSemantics domain.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
virtual int iproc_key(SgAsmInstruction *) const ROSE_OVERRIDE
Disabled in this class.
Debugger::Ptr process() const
Property: Subordinate process storing the registers.
ConcreteSemantics::Formatter Formatter
Formatter for printing values.
static SValuePtr instance()
Instantiate a new prototypical value.
Base class for machine instructions.
boost::shared_ptr< class RegisterState > RegisterStatePtr
Shared-ownership pointer to RegisterState.
Type of values manipulated by the concrete domain.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
STL namespace.
boost::shared_ptr< class Dispatcher > DispatcherPtr
Shared-ownership pointer to Dispatcher.
static Ptr instance()
Create a debugger object that isn't attached to any subordinate process.
static MemoryStatePtr instance(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval, const Debugger::Ptr &process)
Construct a state attached to the specified process.
Main namespace for the ROSE library.
static DispatcherPtr instance(const BaseSemantics::RiscOperatorsPtr &ops)
Create a new dispatcher using the specified operators.
static RiscOperatorsPtr instance(const BaseSemantics::StatePtr &state)
Instantiate a new RiscOperators object.
virtual void iproc_set(int key, BaseSemantics::InsnProcessor *) ROSE_OVERRIDE
Disabled in this class.
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
boost::shared_ptr< class State > StatePtr
Shared-ownership pointer to State.
Sawyer::SharedPointer< class SmtSolver > SmtSolverPtr
Reference-counting pointer for SMT solvers.
Debugger::Ptr process() const
Property: Process storing the state.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
static RegisterStatePtr instance(const BaseSemantics::SValuePtr &protoval, const Debugger::Ptr &process)
Construct a state attached to the specified process.
boost::shared_ptr< class MemoryState > MemoryStatePtr
Shared-ownership pointer to MemoryState.
Describes (part of) a physical CPU register.
ConcreteSemantics::SValuePtr SValuePtr
Shared-ownership pointer to SValue.
static RiscOperatorsPtr instance(const BaseSemantics::SValuePtr &protoval, const Debugger::Ptr &process)
Instantiate a new RiscOperators object.
Describes the specimen to be debugged.
Functor that knows how to dispatch a single kind of instruction.
virtual BaseSemantics::DispatcherPtr create(const BaseSemantics::RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const ROSE_OVERRIDE
Virtual constructor.
static SValuePtr promote(const BaseSemantics::SValuePtr &v)
Promote a base value to a SymbolicSemantics value.
Base class for most instruction semantics RISC operators.
ConcreteSemantics::SValue SValue
Concrete values from the specimen.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const ROSE_OVERRIDE
Virtual allocating constructor.
Defines registers available for a particular architecture.
Definition: Registers.h:38
static DispatcherPtr instance(const Debugger::Ptr &process, const BaseSemantics::SValuePtr &protoval=SValue::instance())
Create a new dispatcher using the specified process.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to RiscOperators.
static DispatcherPtr instance(const Debugger::Specimen &specimen, const BaseSemantics::SValuePtr &protoval=SValue::instance())
Create a new dispatcher using the specified executable specimen.
static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &x)
Run-time promotion of a base object to a NativeSemantics RiscOperators.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
static MemoryStatePtr instance()
Construct a state not attached to any subordinate process.