ROSE 0.11.145.202
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::AddressSpacePtr clone() const override;
82
83 //----------------------------------------
84 // Dynamic pointer casts
85 //----------------------------------------
86public:
87 static RegisterStatePtr promote(const BaseSemantics::AddressSpacePtr&);
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
108 ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
109 }
110
111 virtual BaseSemantics::SValuePtr readRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt,
112 BaseSemantics::RiscOperators *ops) override {
113 return peekRegister(reg, dflt, ops);
114 }
115
116 virtual BaseSemantics::SValuePtr peekRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &dflt,
118
119 virtual void writeRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &value,
121
122 virtual void updateReadProperties(RegisterDescriptor) override {}
123 virtual void updateWriteProperties(RegisterDescriptor, BaseSemantics::InputOutputProperty) override {}
124
125 virtual void hash(Combinatorics::Hasher&, BaseSemantics::RiscOperators*, BaseSemantics::RiscOperators*) const override;
126
127 virtual void print(std::ostream&, Formatter&) const override;
128};
129
131// Memory state
133
135typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
136
140class MemoryState: public BaseSemantics::MemoryState, boost::noncopyable {
141public:
143 using Super = BaseSemantics::MemoryState;
144
146 using Ptr = MemoryStatePtr;
147
148private:
149 Debugger::Linux::Ptr process_;
150
151 //----------------------------------------
152 // Real constructors
153 //----------------------------------------
154protected:
155 MemoryState();
156
157 MemoryState(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval,
158 const Debugger::Linux::Ptr &process);
159
160 //----------------------------------------
161 // Static allocating constructors
162 //----------------------------------------
163public:
164 ~MemoryState();
165
167 static MemoryStatePtr instance();
168
170 static MemoryStatePtr instance(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval,
171 const Debugger::Linux::Ptr &process);
172
173 //----------------------------------------
174 // Virtual constructors
175 //----------------------------------------
176public:
177 virtual BaseSemantics::MemoryStatePtr create(const BaseSemantics::SValuePtr &addrProtoval,
178 const BaseSemantics::SValuePtr &valProtoval) const override;
179
180 virtual BaseSemantics::AddressSpacePtr clone() const override;
181
182 //----------------------------------------
183 // Dynamic pointer casts
184 //----------------------------------------
185public:
186 static MemoryStatePtr promote(const BaseSemantics::AddressSpacePtr&);
187
188 //----------------------------------------
189 // Additional properties
190 //----------------------------------------
191public:
193 Debugger::Linux::Ptr process() const;
194
195 //----------------------------------------
196 // Virtual function implementations
197 //----------------------------------------
198public:
199 virtual void clear() override {}
200
201 virtual bool merge(const BaseSemantics::AddressSpacePtr &/*other*/, BaseSemantics::RiscOperators */*addrOps*/,
202 BaseSemantics::RiscOperators */*valOps*/) override {
203 ASSERT_not_implemented("not applicable for this class");
204 }
205
206 virtual BaseSemantics::SValuePtr readMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt,
208 BaseSemantics::RiscOperators *valOps) override {
209 return peekMemory(address, dflt, addrOps, valOps);
210 }
211
212 virtual BaseSemantics::SValuePtr peekMemory(const BaseSemantics::SValuePtr &address, const BaseSemantics::SValuePtr &dflt,
214 BaseSemantics::RiscOperators *valOps) override;
215
216 virtual void writeMemory(const BaseSemantics::SValuePtr &/*addr*/, const BaseSemantics::SValuePtr &/*value*/,
217 BaseSemantics::RiscOperators */*addrOps*/, BaseSemantics::RiscOperators */*valOps*/) override {
218 ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
219 }
220
221 virtual void hash(Combinatorics::Hasher&, BaseSemantics::RiscOperators */*addrOps*/,
222 BaseSemantics::RiscOperators */*valOps*/) const override {
223 ASSERT_not_implemented("[Robb Matzke 2021-03-26]");
224 }
225
226 virtual void print(std::ostream&, BaseSemantics::Formatter&) const override {
227 ASSERT_not_implemented("[Robb Matzke 2019-09-05]");
228 }
229};
230
232// Complete semantic state
234
236typedef boost::shared_ptr<class State> StatePtr;
237
242class State: public ConcreteSemantics::State, boost::noncopyable {
243public:
245 using Super = ConcreteSemantics::State;
246
248 using Ptr = StatePtr;
249
250 //----------------------------------------
251 // Real constructors
252 //----------------------------------------
253protected:
255 : ConcreteSemantics::State(registers, memory) {
256 (void) RegisterState::promote(registers);
257 (void) MemoryState::promote(memory);
258 }
259
260 //----------------------------------------
261 // Static allocating constructors
262 //----------------------------------------
263public:
264 static StatePtr instance(const BaseSemantics::RegisterStatePtr &registers, const BaseSemantics::MemoryStatePtr &memory) {
265 return StatePtr(new State(registers, memory));
266 }
267};
268
270// RISC operators
272
274typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
275
276class RiscOperators: public ConcreteSemantics::RiscOperators {
277public:
279 using Super = ConcreteSemantics::RiscOperators;
280
282 using Ptr = RiscOperatorsPtr;
283
284 //----------------------------------------
285 // Real constructors
286 //----------------------------------------
287protected:
288 explicit RiscOperators(const BaseSemantics::StatePtr&);
289
290 //----------------------------------------
291 // Static allocating constructors
292 //----------------------------------------
293public:
294 ~RiscOperators();
295
301 static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const Debugger::Linux::Ptr &process);
302
308 static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr &state);
309
310 //----------------------------------------
311 // Virtual constructors
312 //----------------------------------------
313public:
314 virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval,
315 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
316
317 //----------------------------------------
318 // Dynamic pointer casts
319 //----------------------------------------
320public:
325
326 //----------------------------------------
327 // Additional properties
328 //----------------------------------------
329public:
333 Debugger::Linux::Ptr process() const {
334 return RegisterState::promote(currentState()->registerState())->process();
335 }
336};
337
339// Dispatcher
341
343typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
344
345class Dispatcher: public BaseSemantics::Dispatcher {
346public:
348 using Super = BaseSemantics::Dispatcher;
349
351 using Ptr = DispatcherPtr;
352
353private:
354 Debugger::Linux::Ptr process_;
355
356 //----------------------------------------
357 // Real constructors
358 //----------------------------------------
359protected:
360 Dispatcher(const Architecture::BaseConstPtr&, const Debugger::Linux::Ptr &process, const BaseSemantics::SValuePtr &protoval);
361
362 Dispatcher(const Architecture::BaseConstPtr&, const BaseSemantics::RiscOperatorsPtr&);
363
364 //----------------------------------------
365 // Static allocating constructors
366 //----------------------------------------
367public:
368 ~Dispatcher();
369
371 static DispatcherPtr instance(const Architecture::BaseConstPtr&, const Debugger::Linux::Ptr &process,
372 const BaseSemantics::SValuePtr &protoval = SValue::instance());
373
375 static DispatcherPtr instance(const Architecture::BaseConstPtr&, const Debugger::Linux::Specimen&,
376 const BaseSemantics::SValuePtr &protoval = SValue::instance());
377
381 static DispatcherPtr instance(const Architecture::BaseConstPtr&, const BaseSemantics::RiscOperatorsPtr&);
382
383 //----------------------------------------
384 // Virtual constructors
385 //----------------------------------------
386public:
388 create(const BaseSemantics::RiscOperatorsPtr&) const override;
389
390 //----------------------------------------
391 // Operations
392 //----------------------------------------
393public:
402 virtual void processInstruction(SgAsmInstruction *insn) override;
403 void processInstruction(rose_addr_t va);
409 virtual SgAsmInstruction* currentInstruction() const override;
410
411 virtual RegisterDescriptor instructionPointerRegister() const override;
412 virtual RegisterDescriptor stackPointerRegister() const override;
413 virtual RegisterDescriptor stackFrameRegister() const override;
414 virtual RegisterDescriptor callReturnRegister() const override;
415
419 virtual void iprocReplace(SgAsmInstruction*, BaseSemantics::InsnProcessor*) override {
420 notApplicable("iprocReplace");
421 }
422 virtual void iprocSet(int /*key*/, BaseSemantics::InsnProcessor*) override {
423 notApplicable("iprocSet");
424 }
425 virtual int iprocKey(SgAsmInstruction*) const override {
426 notApplicable("iprocKey");
427 }
430private:
431 [[noreturn]] void notApplicable(const std::string &what) const {
432 ASSERT_not_implemented(what + " is not applicable for this class");
433 }
434};
435
436
437} // namespace
438} // namespace
439} // namespace
440} // namespace
441
442#endif
443#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< AddressSpace > AddressSpacePtr
Shared-ownership pointer for AddressSpace objects.
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 GlobalVariables &, const Partitioner2::PartitionerConstPtr &, std::ostream &out, const std::string &prefix="")
Print info about multiple global variables.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.