ROSE  0.11.145.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 updateReadProperties(RegisterDescriptor) override {}
122  virtual void updateWriteProperties(RegisterDescriptor, BaseSemantics::InputOutputProperty) override {}
123 
124  virtual void hash(Combinatorics::Hasher&, BaseSemantics::RiscOperators*) const override;
125 
126  virtual void print(std::ostream&, Formatter&) const override;
127 };
128 
130 // Memory state
132 
134 typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
135 
139 class MemoryState: public BaseSemantics::MemoryState, boost::noncopyable {
140 public:
142  using Super = BaseSemantics::MemoryState;
143 
145  using Ptr = MemoryStatePtr;
146 
147 private:
148  Debugger::Linux::Ptr process_;
149 
150  //----------------------------------------
151  // Real constructors
152  //----------------------------------------
153 protected:
154  MemoryState();
155 
156  MemoryState(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval,
157  const Debugger::Linux::Ptr &process);
158 
159  //----------------------------------------
160  // Static allocating constructors
161  //----------------------------------------
162 public:
163  ~MemoryState();
164 
166  static MemoryStatePtr instance();
167 
169  static MemoryStatePtr instance(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval,
170  const Debugger::Linux::Ptr &process);
171 
172  //----------------------------------------
173  // Virtual constructors
174  //----------------------------------------
175 public:
176  virtual BaseSemantics::MemoryStatePtr create(const BaseSemantics::SValuePtr &addrProtoval,
177  const BaseSemantics::SValuePtr &valProtoval) const override;
178 
179  virtual BaseSemantics::MemoryStatePtr clone() const override;
180 
181  //----------------------------------------
182  // Dynamic pointer casts
183  //----------------------------------------
184 public:
185  static MemoryStatePtr promote(const BaseSemantics::MemoryStatePtr&);
186 
187  //----------------------------------------
188  // Additional properties
189  //----------------------------------------
190 public:
192  Debugger::Linux::Ptr process() const;
193 
194  //----------------------------------------
195  // Virtual function implementations
196  //----------------------------------------
197 public:
198  virtual void clear() override {}
199 
200  virtual bool merge(const BaseSemantics::MemoryStatePtr &/*other*/, BaseSemantics::RiscOperators */*addrOps*/,
201  BaseSemantics::RiscOperators */*valOps*/) override {
202  ASSERT_not_implemented("not applicable for this class");
203  }
204 
205  virtual BaseSemantics::SValuePtr readMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt,
207  BaseSemantics::RiscOperators *valOps) override {
208  return peekMemory(address, dflt, addrOps, valOps);
209  }
210 
211  virtual BaseSemantics::SValuePtr peekMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt,
213  BaseSemantics::RiscOperators *valOps) override;
214 
215  virtual void writeMemory(const BaseSemantics::SValuePtr &/*addr*/, const BaseSemantics::SValuePtr &/*value*/,
216  BaseSemantics::RiscOperators */*addrOps*/, BaseSemantics::RiscOperators */*valOps*/) override {
217  ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
218  }
219 
220  virtual void hash(Combinatorics::Hasher&, BaseSemantics::RiscOperators */*addrOps*/,
221  BaseSemantics::RiscOperators */*valOps*/) const override {
222  ASSERT_not_implemented("[Robb Matzke 2021-03-26]");
223  }
224 
225  virtual void print(std::ostream&, BaseSemantics::Formatter&) const override {
226  ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
227  }
228 };
229 
231 // Complete semantic state
233 
235 typedef boost::shared_ptr<class State> StatePtr;
236 
241 class State: public ConcreteSemantics::State, boost::noncopyable {
242 public:
244  using Super = ConcreteSemantics::State;
245 
247  using Ptr = StatePtr;
248 
249  //----------------------------------------
250  // Real constructors
251  //----------------------------------------
252 protected:
254  : ConcreteSemantics::State(registers, memory) {
255  (void) RegisterState::promote(registers);
256  (void) MemoryState::promote(memory);
257  }
258 
259  //----------------------------------------
260  // Static allocating constructors
261  //----------------------------------------
262 public:
263  static StatePtr instance(const BaseSemantics::RegisterStatePtr &registers, const BaseSemantics::MemoryStatePtr &memory) {
264  return StatePtr(new State(registers, memory));
265  }
266 };
267 
269 // RISC operators
271 
273 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
274 
275 class RiscOperators: public ConcreteSemantics::RiscOperators {
276 public:
278  using Super = ConcreteSemantics::RiscOperators;
279 
281  using Ptr = RiscOperatorsPtr;
282 
283  //----------------------------------------
284  // Real constructors
285  //----------------------------------------
286 protected:
287  explicit RiscOperators(const BaseSemantics::StatePtr&);
288 
289  //----------------------------------------
290  // Static allocating constructors
291  //----------------------------------------
292 public:
293  ~RiscOperators();
294 
300  static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const Debugger::Linux::Ptr &process);
301 
307  static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr &state);
308 
309  //----------------------------------------
310  // Virtual constructors
311  //----------------------------------------
312 public:
313  virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval,
314  const SmtSolverPtr &solver = SmtSolverPtr()) const override;
315 
316  //----------------------------------------
317  // Dynamic pointer casts
318  //----------------------------------------
319 public:
323  static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr&);
324 
325  //----------------------------------------
326  // Additional properties
327  //----------------------------------------
328 public:
332  Debugger::Linux::Ptr process() const {
333  return RegisterState::promote(currentState()->registerState())->process();
334  }
335 };
336 
338 // Dispatcher
340 
342 typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
343 
344 class Dispatcher: public BaseSemantics::Dispatcher {
345 public:
347  using Super = BaseSemantics::Dispatcher;
348 
350  using Ptr = DispatcherPtr;
351 
352 private:
353  Debugger::Linux::Ptr process_;
354 
355  //----------------------------------------
356  // Real constructors
357  //----------------------------------------
358 protected:
359  Dispatcher(const Debugger::Linux::Ptr &process, const BaseSemantics::SValuePtr &protoval);
360 
361  Dispatcher(const BaseSemantics::RiscOperatorsPtr&);
362 
363  //----------------------------------------
364  // Static allocating constructors
365  //----------------------------------------
366 public:
367  ~Dispatcher();
368 
370  static DispatcherPtr instance(const Debugger::Linux::Ptr &process, const BaseSemantics::SValuePtr &protoval = SValue::instance());
371 
373  static DispatcherPtr instance(const Debugger::Linux::Specimen&, const BaseSemantics::SValuePtr &protoval = SValue::instance());
374 
378  static DispatcherPtr instance(const BaseSemantics::RiscOperatorsPtr&);
379 
380  //----------------------------------------
381  // Virtual constructors
382  //----------------------------------------
383 public:
385  create(const BaseSemantics::RiscOperatorsPtr&, size_t addrWidth, const RegisterDictionaryPtr&) const override;
386 
387  //----------------------------------------
388  // Operations
389  //----------------------------------------
390 public:
399  virtual void processInstruction(SgAsmInstruction *insn) override;
400  void processInstruction(rose_addr_t va);
406  virtual SgAsmInstruction* currentInstruction() const override;
407 
408  virtual RegisterDescriptor instructionPointerRegister() const override;
409  virtual RegisterDescriptor stackPointerRegister() const override;
410  virtual RegisterDescriptor stackFrameRegister() const override;
411  virtual RegisterDescriptor callReturnRegister() const override;
412 
416  virtual void iprocReplace(SgAsmInstruction*, BaseSemantics::InsnProcessor*) override {
417  notApplicable("iprocReplace");
418  }
419  virtual void iprocSet(int /*key*/, BaseSemantics::InsnProcessor*) override {
420  notApplicable("iprocSet");
421  }
422  virtual int iprocKey(SgAsmInstruction*) const override {
423  notApplicable("iprocKey");
424  }
427 private:
428  [[noreturn]] void notApplicable(const std::string &what) const {
429  ASSERT_not_implemented(what + " is not applicable for this class");
430  }
431 };
432 
433 
434 } // namespace
435 } // namespace
436 } // namespace
437 } // namespace
438 
439 #endif
440 #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.
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:43
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.
void print(const StackVariables &, const Partitioner2::PartitionerConstPtr &, std::ostream &out, const std::string &prefix="")
Print info about multiple local variables.
Functor that knows how to dispatch a single kind of instruction.
Definition: Dispatcher.h:24
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