ROSE  0.11.109.0
NativeSemantics.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics_NativeSemantics_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics_NativeSemantics_H
3 #include <featureTests.h>
4 #if defined(ROSE_ENABLE_BINARY_ANALYSIS) && defined(ROSE_ENABLE_DEBUGGER_LINUX)
5 
6 #include <Rose/BinaryAnalysis/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/Debugger/Linux.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 
25 typedef ConcreteSemantics::SValue SValue;
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:
46  using Super = BaseSemantics::RegisterState;
47 
49  using Ptr = RegisterStatePtr;
50 
51 private:
52  Debugger::Linux::Ptr process_;
53 
54  //----------------------------------------
55  // Real constructors
56  //----------------------------------------
57 protected:
58  RegisterState();
59 
60  RegisterState(const BaseSemantics::SValuePtr &protoval, const Debugger::Linux::Ptr &process);
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::Linux::Ptr &process);
73 
74  //----------------------------------------
75  // Virtual constructors
76  //----------------------------------------
77 public:
78  virtual BaseSemantics::RegisterStatePtr create(const BaseSemantics::SValuePtr &protoval,
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::Linux::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*) override {
107  ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
108  }
109 
110  virtual BaseSemantics::SValuePtr readRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt,
111  BaseSemantics::RiscOperators *ops) override {
112  return peekRegister(reg, dflt, ops);
113  }
114 
115  virtual BaseSemantics::SValuePtr peekRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &dflt,
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:
139  using Super = BaseSemantics::MemoryState;
140 
142  using Ptr = MemoryStatePtr;
143 
144 private:
145  Debugger::Linux::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::Linux::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::Linux::Ptr &process);
168 
169  //----------------------------------------
170  // Virtual constructors
171  //----------------------------------------
172 public:
173  virtual BaseSemantics::MemoryStatePtr create(const BaseSemantics::SValuePtr &addrProtoval,
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::Linux::Ptr process() const;
190 
191  //----------------------------------------
192  // Virtual function implementations
193  //----------------------------------------
194 public:
195  virtual void clear() override {}
196 
197  virtual bool merge(const BaseSemantics::MemoryStatePtr &/*other*/, BaseSemantics::RiscOperators */*addrOps*/,
198  BaseSemantics::RiscOperators */*valOps*/) override {
199  ASSERT_not_implemented("not applicable for this class");
200  }
201 
202  virtual BaseSemantics::SValuePtr readMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt,
204  BaseSemantics::RiscOperators *valOps) override {
205  return peekMemory(address, dflt, addrOps, valOps);
206  }
207 
208  virtual BaseSemantics::SValuePtr peekMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt,
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 
217  virtual void hash(Combinatorics::Hasher&, BaseSemantics::RiscOperators */*addrOps*/,
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:
241  using Super = ConcreteSemantics::State;
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 
272 class RiscOperators: public ConcreteSemantics::RiscOperators {
273 public:
275  using Super = ConcreteSemantics::RiscOperators;
276 
278  using Ptr = RiscOperatorsPtr;
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 
297  static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const Debugger::Linux::Ptr &process);
298 
304  static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr &state);
305 
306  //----------------------------------------
307  // Virtual constructors
308  //----------------------------------------
309 public:
310  virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval,
311  const SmtSolverPtr &solver = SmtSolverPtr()) const override;
312 
313  //----------------------------------------
314  // Dynamic pointer casts
315  //----------------------------------------
316 public:
320  static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr&);
321 
322  //----------------------------------------
323  // Additional properties
324  //----------------------------------------
325 public:
329  Debugger::Linux::Ptr process() const {
330  return RegisterState::promote(currentState()->registerState())->process();
331  }
332 };
333 
335 // Dispatcher
337 
339 typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
340 
341 class Dispatcher: public BaseSemantics::Dispatcher {
342 public:
344  using Super = BaseSemantics::Dispatcher;
345 
347  using Ptr = DispatcherPtr;
348 
349 private:
350  Debugger::Linux::Ptr process_;
351 
352  //----------------------------------------
353  // Real constructors
354  //----------------------------------------
355 protected:
356  Dispatcher(const Debugger::Linux::Ptr &process, const BaseSemantics::SValuePtr &protoval);
357 
358  Dispatcher(const BaseSemantics::RiscOperatorsPtr&);
359 
360  //----------------------------------------
361  // Static allocating constructors
362  //----------------------------------------
363 public:
364  ~Dispatcher();
365 
367  static DispatcherPtr instance(const Debugger::Linux::Ptr &process, const BaseSemantics::SValuePtr &protoval = SValue::instance());
368 
370  static DispatcherPtr instance(const Debugger::Linux::Specimen&, const BaseSemantics::SValuePtr &protoval = SValue::instance());
371 
375  static DispatcherPtr instance(const BaseSemantics::RiscOperatorsPtr&);
376 
377  //----------------------------------------
378  // Virtual constructors
379  //----------------------------------------
380 public:
382  create(const BaseSemantics::RiscOperatorsPtr&, size_t addrWidth, const RegisterDictionaryPtr&) const override;
383 
384  //----------------------------------------
385  // Operations
386  //----------------------------------------
387 public:
396  virtual void processInstruction(SgAsmInstruction *insn) override;
397  void processInstruction(rose_addr_t va);
403  virtual SgAsmInstruction* currentInstruction() const override;
404 
405  virtual RegisterDescriptor instructionPointerRegister() const override;
406  virtual RegisterDescriptor stackPointerRegister() const override;
407  virtual RegisterDescriptor stackFrameRegister() const override;
408  virtual RegisterDescriptor callReturnRegister() const override;
409 
413  virtual void iprocReplace(SgAsmInstruction*, BaseSemantics::InsnProcessor*) override {
414  notApplicable("iprocReplace");
415  }
416  virtual void iprocSet(int /*key*/, BaseSemantics::InsnProcessor*) override {
417  notApplicable("iprocSet");
418  }
419  virtual int iprocKey(SgAsmInstruction*) const override {
420  notApplicable("iprocKey");
421  }
424 private:
425  [[noreturn]] void notApplicable(const std::string &what) const {
426  ASSERT_not_implemented(what + " is not applicable for this class");
427  }
428 };
429 
430 
431 } // namespace
432 } // namespace
433 } // namespace
434 } // namespace
435 
436 #endif
437 #endif
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
ROSE_DLL_API void clear()
Empties the memory pool of all nodes.
Base class for machine instructions.
Sawyer::SharedPointer< class SValue > SValuePtr
Smart-ownership pointer to a concrete semantic value.
boost::shared_ptr< class MemoryState > MemoryStatePtr
Shared-ownership pointer to a concrete memory state.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
Sawyer::SharedPointer< RegisterDictionary > RegisterDictionaryPtr
Reference counting pointer.
void print(const StackVariables &, const Partitioner2::Partitioner &, std::ostream &out, const std::string &prefix="")
Print info about multiple local variables.
Main namespace for the ROSE library.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
Dispatches instructions through the RISC layer.
Definition: Dispatcher.h:44
ROSE_DLL_API void merge(SgProject *project)
Performs sharing of AST nodes followed by linking accross translation units.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
Functor that knows how to dispatch a single kind of instruction.
Definition: Dispatcher.h:25
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to concrete RISC operations.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
BaseSemantics::Formatter Formatter
Formatter for symbolic values.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:49
Hash hash(const std::vector< Ptr > &)
Hash zero or more expressions.
State
Decoder state.
Definition: String.h:198