ROSE 2.15.0
Loading...
Searching...
No Matches
State.h
1#ifndef ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_State_H
2#define ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_State_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/BasicTypes.h>
6#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/State.h>
7#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/SValue.h>
8
9#include <Rose/BinaryAnalysis/RegisterDescriptor.h>
10#include <Combinatorics.h> // rose
11
12#include <boost/enable_shared_from_this.hpp>
13
14#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
15#include <boost/serialization/access.hpp>
16#include <boost/serialization/export.hpp>
17#include <boost/serialization/nvp.hpp>
18#include <boost/serialization/shared_ptr.hpp>
19#endif
20
21namespace Rose {
22namespace BinaryAnalysis {
23namespace InstructionSemantics {
24namespace BaseSemantics {
25
27// State
29
47class State: public boost::enable_shared_from_this<State> {
48public:
50 using Ptr = StatePtr;
51
52private:
53 SValuePtr protoval_; // Initial value used to create additional values as needed.
54 std::vector<AddressSpacePtr> addressSpaces_; // All address spaces for this semantic state.
55 RegisterStatePtr registers_; // All machine register values for this semantic state.
56 MemoryStatePtr memory_; // All memory for this semantic state.
57 RegisterStatePtr interrupts_; // Whether interrupts occurred.
58 SValuePtr returnValue_; // Result returned by the root method, if any.
59 bool terminated_ = false; // No further instructions execute on this path.
60
62 // Serialization.
64#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
65private:
66 friend class boost::serialization::access;
67
68 template<class S>
69 void serialize(S &s, const unsigned version) {
70 ASSERT_require2(version >= 2, version);
71 s & BOOST_SERIALIZATION_NVP(protoval_);
72 s & BOOST_SERIALIZATION_NVP(addressSpaces_);
73 s & BOOST_SERIALIZATION_NVP(registers_);
74 s & BOOST_SERIALIZATION_NVP(memory_);
75 s & BOOST_SERIALIZATION_NVP(interrupts_);
76 s & BOOST_SERIALIZATION_NVP(returnValue_);
77 s & BOOST_SERIALIZATION_NVP(terminated_);
78 }
79#endif
80
81
83 // Real constructors.
85protected:
86 // needed for serialization
87 State();
88
89 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts, const FrameStatePtr &frame);
90 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
91 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
92
93 // deep-copy the registers and memory
94 State(const State &other);
95
96public:
97 virtual ~State();
98
100 // Static allocating constructors
102public:
104 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory,
105 const RegisterStatePtr &interrupts, const FrameStatePtr &frame);
106
108 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
109
111 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
112
117 static StatePtr instance(const StatePtr &other);
118
120 // Virtual constructors.
122public:
127 virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const;
128
133 virtual StatePtr clone() const;
134
136 // Dynamic pointer casts. No-op since this is the base class.
138public:
139 static StatePtr promote(const StatePtr&);
140
142 // Properties of a state.
144public:
155
157 // Address space declaration and searching.
159public:
168
170 const std::vector<AddressSpacePtr>& addressSpaces() const;
171
177
183
185 // Low-level operations on address spaces.
187public:
203 virtual SValuePtr read(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
204 RiscOperators &addrOps, RiscOperators &valOps);
205
209 virtual SValuePtr peek(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
210 RiscOperators &addrOps, RiscOperators &valOps);
211
223 virtual void write(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &value,
224 RiscOperators &addrOps, RiscOperators &valOps);
225
238 virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps);
239
241 // Operations on address spaces found by searching.
243public:
248 virtual void clear();
249
256
263
269
276
288
300
307 bool hasInterruptState() const;
308
317
323 bool hasFrameState() const;
324
328 void terminate();
330
332 bool isTerminated() const;
333
338
339 virtual SValuePtr readLocal(size_t index);
340 virtual void writeLocal(size_t index, const SValuePtr &value);
341
345 virtual SValuePtr peekOperand(size_t depth = 0);
346
350 virtual void pushOperand(const SValuePtr &value);
351
356
358 virtual void pushFrame(const FrameStatePtr &frame);
359
362
365
370
375
379 virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
380
384 virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
385 RiscOperators *addrOps, RiscOperators *valOps);
386
390 virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps);
391
395 virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
396
405 virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
406
415 virtual SValuePtr peekInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
416
423 virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps);
424
429 SValuePtr raiseInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
430
436 SValuePtr clearInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
437
445 bool isInterruptDefinitelyRaised(unsigned major, unsigned minor, RiscOperators *valOps);
446
454 bool isInterruptDefinitelyClear(unsigned major, unsigned minor, RiscOperators *valOps);
455
460 virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const;
461
468 void printRegisters(std::ostream &stream, const std::string &prefix = "");
469 virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
477 void printMemory(std::ostream &stream, const std::string &prefix = "") const;
478 virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
487 void printInterrupts(std::ostream&, const std::string &prefix = "");
488 virtual void printInterrupts(std::ostream &stream, Formatter &fmt) const;
497 void print(std::ostream &stream, const std::string &prefix = "") const;
498 virtual void print(std::ostream&, Formatter&) const;
504 std::string toString() const;
505
508 StatePtr obj;
509 Formatter &fmt;
510 public:
511 WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
512 void print(std::ostream &stream) const { obj->print(stream, fmt); }
513 };
514
530 WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
532 WithFormatter operator+(const std::string &linePrefix);
540 virtual bool merge(const StatePtr &other, RiscOperators *ops);
541};
542
543std::ostream& operator<<(std::ostream&, const State&);
544std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
545
546} // namespace
547} // namespace
548} // namespace
549} // namespace
550
551#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
554#endif
555
556#endif
557#endif
Base class for most instruction semantics RISC operators.
Base class for semantics machine states.
Definition State.h:47
RegisterStatePtr interruptState() const
Property: Interrupt state.
virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops)
Write a value to a register.
MemoryStatePtr memoryState() const
Property: Memory state.
WithFormatter with_format(Formatter &fmt)
Used for printing states with formatting.
Definition State.h:530
virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps)
Merge operation for data flow analysis.
virtual void pushOperand(const SValuePtr &value)
Push an operand value.
virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps)
Write an interrupt state.
virtual void pushFrame(const FrameStatePtr &frame)
Pushes a method invocation frame.
SValuePtr protoval() const
Property: Prototypical value.
void printMemory(std::ostream &stream, const std::string &prefix="") const
Print memory contents.
std::string toString() const
Convert the state to a string for debugging.
virtual void printInterrupts(std::ostream &stream, Formatter &fmt) const
Print interrupt states.
static StatePtr instance(const StatePtr &other)
Instantiate a new copy of an existing state.
virtual SValuePtr peekOperand(size_t depth=0)
Peek at an operand value.
AddressSpacePtr findFirstAddressSpace(AddressSpacePurpose, const std::string &name) const
Find the first address space with the specified purpose and name.
virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps)
Read an interrupt state.
bool isTerminated() const
Tests whether a state is terminated.
const std::vector< AddressSpacePtr > & addressSpaces() const
The list of all address spaces.
virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const
Virtual constructor.
bool isInterruptDefinitelyClear(unsigned major, unsigned minor, RiscOperators *valOps)
Test an interrupt.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts)
Instantiate a new state object with specified register, memory, and interrupt address spaces.
virtual bool merge(const StatePtr &other, RiscOperators *ops)
Merge operation for data flow analysis.
bool hasInterruptState() const
Tests whether an interrupt state is present.
SValuePtr raiseInterrupt(unsigned major, unsigned minor, RiscOperators *valOps)
Raise an interrupt.
WithFormatter operator+(Formatter &fmt)
Used for printing states with formatting.
Definition State.h:531
virtual SValuePtr popOperand()
Pop an operand value.
virtual SValuePtr read(const AddressSpacePtr &, const AddressSpaceAddress &, const SValuePtr &dflt, RiscOperators &addrOps, RiscOperators &valOps)
Read a value from an address space.
virtual void printRegisters(std::ostream &stream, Formatter &fmt) const
Print the register contents.
virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read register without side effects.
virtual void hash(Combinatorics::Hasher &, RiscOperators *addrOps, RiscOperators *valOps) const
Compute a hash of the state.
virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read a value from memory.
virtual SValuePtr peekInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps)
Read an interrupt state without side effects.
virtual void print(std::ostream &, Formatter &) const
Print the state.
SValuePtr clearInterrupt(unsigned major, unsigned minor, RiscOperators *valOps)
Clear an interrupt.
virtual FrameStatePtr popFrame()
Pops and returns the current method invocation frame.
bool isInterruptDefinitelyRaised(unsigned major, unsigned minor, RiscOperators *valOps)
Test an interrupt.
void print(std::ostream &stream, const std::string &prefix="") const
Print the state.
virtual StatePtr clone() const
Virtual copy constructor.
void interruptState(const RegisterStatePtr &)
Property: Interrupt state.
WithFormatter operator+(const std::string &linePrefix)
Used for printing states with formatting.
virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps)
Write a value to memory.
virtual void printMemory(std::ostream &stream, Formatter &fmt) const
Print memory contents.
void printRegisters(std::ostream &stream, const std::string &prefix="")
Print the register contents.
void insertAddressSpace(const AddressSpacePtr &)
Insert an address space into this state.
RegisterStatePtr registerState() const
Property: Register state.
virtual SValuePtr peek(const AddressSpacePtr &, const AddressSpaceAddress &, const SValuePtr &dflt, RiscOperators &addrOps, RiscOperators &valOps)
Peek at a value in an address space.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts, const FrameStatePtr &frame)
Instantiate a new state object with specified register, memory, interrupt, and frame address spaces.
virtual FrameStatePtr currentFrame() const
Returns the current method invocation frame.
virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read a value from a register.
FrameStatePtr frameState() const
Property: Frame state.
AddressSpacePtr findFirstAddressSpace(AddressSpacePurpose) const
Find the first address space with the specified purpose.
virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read from memory without side effects.
bool hasFrameState() const
Tests whether an frame state is present.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory address spaces.
void zeroRegisters()
Initialize all registers to zero.
void printInterrupts(std::ostream &, const std::string &prefix="")
Print interrupt states.
virtual void write(const AddressSpacePtr &, const AddressSpaceAddress &, const SValuePtr &value, RiscOperators &addrOps, RiscOperators &valOps)
Write a value to an address space.
Describes (part of) a physical CPU register.
Base classes for instruction semantics.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
boost::shared_ptr< AddressSpace > AddressSpacePtr
Shared-ownership pointer for AddressSpace objects.
boost::shared_ptr< FrameState > FrameStatePtr
Shared-ownership pointer to a frame state.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
The ROSE library.