ROSE 0.11.145.147
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
13namespace Rose {
14namespace BinaryAnalysis {
15namespace InstructionSemantics {
16
18namespace NativeSemantics {
19
21// Value type
23
25typedef ConcreteSemantics::SValue SValue;
26
28typedef ConcreteSemantics::SValuePtr SValuePtr;
29
31typedef ConcreteSemantics::Formatter Formatter;
32
34// Register state
36
38typedef boost::shared_ptr<class RegisterState> RegisterStatePtr;
39
43class RegisterState: public BaseSemantics::RegisterState, boost::noncopyable {
44public:
46 using Super = BaseSemantics::RegisterState;
47
49 using Ptr = RegisterStatePtr;
50
51private:
52 Debugger::Linux::Ptr process_;
53
54 //----------------------------------------
55 // Real constructors
56 //----------------------------------------
57protected:
58 RegisterState();
59
60 RegisterState(const BaseSemantics::SValuePtr &protoval, const Debugger::Linux::Ptr &process);
61
62 //----------------------------------------
63 // Static allocating constructors
64 //----------------------------------------
65public:
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 //----------------------------------------
77public:
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 //----------------------------------------
86public:
87 static RegisterStatePtr promote(const BaseSemantics::RegisterStatePtr&);
88
89 //----------------------------------------
90 // Additional properties
91 //----------------------------------------
92public:
94 Debugger::Linux::Ptr process() const;
95
96 //----------------------------------------
97 // Virtual function implementations
98 //----------------------------------------
99public:
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,
117
118 virtual void writeRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &value,
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
134typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
135
139class MemoryState: public BaseSemantics::MemoryState, boost::noncopyable {
140public:
142 using Super = BaseSemantics::MemoryState;
143
145 using Ptr = MemoryStatePtr;
146
147private:
148 Debugger::Linux::Ptr process_;
149
150 //----------------------------------------
151 // Real constructors
152 //----------------------------------------
153protected:
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 //----------------------------------------
162public:
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 //----------------------------------------
175public:
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 //----------------------------------------
184public:
185 static MemoryStatePtr promote(const BaseSemantics::MemoryStatePtr&);
186
187 //----------------------------------------
188 // Additional properties
189 //----------------------------------------
190public:
192 Debugger::Linux::Ptr process() const;
193
194 //----------------------------------------
195 // Virtual function implementations
196 //----------------------------------------
197public:
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
235typedef boost::shared_ptr<class State> StatePtr;
236
241class State: public ConcreteSemantics::State, boost::noncopyable {
242public:
244 using Super = ConcreteSemantics::State;
245
247 using Ptr = StatePtr;
248
249 //----------------------------------------
250 // Real constructors
251 //----------------------------------------
252protected:
254 : ConcreteSemantics::State(registers, memory) {
255 (void) RegisterState::promote(registers);
256 (void) MemoryState::promote(memory);
257 }
258
259 //----------------------------------------
260 // Static allocating constructors
261 //----------------------------------------
262public:
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
273typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
274
275class RiscOperators: public ConcreteSemantics::RiscOperators {
276public:
278 using Super = ConcreteSemantics::RiscOperators;
279
281 using Ptr = RiscOperatorsPtr;
282
283 //----------------------------------------
284 // Real constructors
285 //----------------------------------------
286protected:
287 explicit RiscOperators(const BaseSemantics::StatePtr&);
288
289 //----------------------------------------
290 // Static allocating constructors
291 //----------------------------------------
292public:
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 //----------------------------------------
312public:
313 virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval,
314 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
315
316 //----------------------------------------
317 // Dynamic pointer casts
318 //----------------------------------------
319public:
324
325 //----------------------------------------
326 // Additional properties
327 //----------------------------------------
328public:
332 Debugger::Linux::Ptr process() const {
333 return RegisterState::promote(currentState()->registerState())->process();
334 }
335};
336
338// Dispatcher
340
342typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
343
344class Dispatcher: public BaseSemantics::Dispatcher {
345public:
347 using Super = BaseSemantics::Dispatcher;
348
350 using Ptr = DispatcherPtr;
351
352private:
353 Debugger::Linux::Ptr process_;
354
355 //----------------------------------------
356 // Real constructors
357 //----------------------------------------
358protected:
359 Dispatcher(const Architecture::BaseConstPtr&, const Debugger::Linux::Ptr &process, const BaseSemantics::SValuePtr &protoval);
360
361 Dispatcher(const Architecture::BaseConstPtr&, const BaseSemantics::RiscOperatorsPtr&);
362
363 //----------------------------------------
364 // Static allocating constructors
365 //----------------------------------------
366public:
367 ~Dispatcher();
368
370 static DispatcherPtr instance(const Architecture::BaseConstPtr&, const Debugger::Linux::Ptr &process,
371 const BaseSemantics::SValuePtr &protoval = SValue::instance());
372
374 static DispatcherPtr instance(const Architecture::BaseConstPtr&, const Debugger::Linux::Specimen&,
375 const BaseSemantics::SValuePtr &protoval = SValue::instance());
376
380 static DispatcherPtr instance(const Architecture::BaseConstPtr&, const BaseSemantics::RiscOperatorsPtr&);
381
382 //----------------------------------------
383 // Virtual constructors
384 //----------------------------------------
385public:
387 create(const BaseSemantics::RiscOperatorsPtr&) const override;
388
389 //----------------------------------------
390 // Operations
391 //----------------------------------------
392public:
401 virtual void processInstruction(SgAsmInstruction *insn) override;
402 void processInstruction(rose_addr_t va);
408 virtual SgAsmInstruction* currentInstruction() const override;
409
410 virtual RegisterDescriptor instructionPointerRegister() const override;
411 virtual RegisterDescriptor stackPointerRegister() const override;
412 virtual RegisterDescriptor stackFrameRegister() const override;
413 virtual RegisterDescriptor callReturnRegister() const override;
414
418 virtual void iprocReplace(SgAsmInstruction*, BaseSemantics::InsnProcessor*) override {
419 notApplicable("iprocReplace");
420 }
421 virtual void iprocSet(int /*key*/, BaseSemantics::InsnProcessor*) override {
422 notApplicable("iprocSet");
423 }
424 virtual int iprocKey(SgAsmInstruction*) const override {
425 notApplicable("iprocKey");
426 }
429private:
430 [[noreturn]] void notApplicable(const std::string &what) const {
431 ASSERT_not_implemented(what + " is not applicable for this class");
432 }
433};
434
435
436} // namespace
437} // namespace
438} // namespace
439} // namespace
440
441#endif
442#endif
Dispatches instructions through the RISC layer.
Definition Dispatcher.h:43
Functor that knows how to dispatch a single kind of instruction.
Definition Dispatcher.h:24
Base class for most instruction semantics RISC operators.
Base class for machine instructions.
ROSE_DLL_API void clear()
Empties the memory pool of all nodes.
ROSE_DLL_API void merge(SgProject *project)
Performs sharing of AST nodes followed by linking accross translation units.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
boost::shared_ptr< class MemoryState > MemoryStatePtr
Shared-ownership pointer to a concrete memory state.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to concrete RISC operations.
BaseSemantics::Formatter Formatter
Formatter for symbolic values.
Sawyer::SharedPointer< class SValue > SValuePtr
Smart-ownership pointer to a concrete semantic value.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
Hash hash(const std::vector< Ptr > &)
Hash zero or more expressions.
void print(const StackVariables &, const Partitioner2::PartitionerConstPtr &, std::ostream &out, const std::string &prefix="")
Print info about multiple local variables.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.