ROSE 2.9.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/SValue.h>
7
8#include <Rose/BinaryAnalysis/RegisterDescriptor.h>
9#include <Combinatorics.h> // rose
10
11#include <boost/enable_shared_from_this.hpp>
12
13#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
14#include <boost/serialization/access.hpp>
15#include <boost/serialization/export.hpp>
16#include <boost/serialization/nvp.hpp>
17#include <boost/serialization/shared_ptr.hpp>
18#endif
19
20namespace Rose {
21namespace BinaryAnalysis {
22namespace InstructionSemantics {
23namespace BaseSemantics {
24
26// State
28
46class State: public boost::enable_shared_from_this<State> {
47public:
49 using Ptr = StatePtr;
50
51private:
52 std::vector<AddressSpacePtr> addressSpaces_; // ordered address spaces
53 SValuePtr protoval_; // Initial value used to create additional values as needed.
54 RegisterStatePtr registers_; // All machine register values for this semantic state.
55 MemoryStatePtr memory_; // All memory for this semantic state.
56 RegisterStatePtr interrupts_; // Whether interrupts occurred.
57 FrameStatePtr frame_; // Stores JVM local variables and operand stack for executing method.
58
60 // Serialization.
62#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
63private:
64 friend class boost::serialization::access;
65
66 template<class S>
67 void serialize(S &s, const unsigned version) {
68 ASSERT_require2(version >= 2, version);
69 s & BOOST_SERIALIZATION_NVP(protoval_);
70 s & BOOST_SERIALIZATION_NVP(addressSpaces_);
71 s & BOOST_SERIALIZATION_NVP(registers_);
72 s & BOOST_SERIALIZATION_NVP(memory_);
73 s & BOOST_SERIALIZATION_NVP(interrupts_);
74 s & BOOST_SERIALIZATION_NVP(frame_);
75 }
76#endif
77
78
80 // Real constructors.
82protected:
83 // needed for serialization
84 State();
85
86 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts, const FrameStatePtr &frame);
87 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
88 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
89
90 // deep-copy the registers and memory
91 State(const State &other);
92
93public:
94 virtual ~State();
95
97 // Static allocating constructors
99public:
101 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory,
102 const RegisterStatePtr &interrupts, const FrameStatePtr &frame);
103
105 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
106
108 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
109
114 static StatePtr instance(const StatePtr &other);
115
117 // Virtual constructors.
119public:
124 virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const;
125
130 virtual StatePtr clone() const;
131
133 // Dynamic pointer casts. No-op since this is the base class.
135public:
136 static StatePtr promote(const StatePtr&);
137
139 // Properties of a state.
141public:
152
154 // Address space declaration and searching.
156public:
165
167 const std::vector<AddressSpacePtr>& addressSpaces() const;
168
174
180
182 // Low-level operations on address spaces.
184public:
200 virtual SValuePtr read(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
201 RiscOperators &addrOps, RiscOperators &valOps);
202
206 virtual SValuePtr peek(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
207 RiscOperators &addrOps, RiscOperators &valOps);
208
220 virtual void write(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &value,
221 RiscOperators &addrOps, RiscOperators &valOps);
222
235 virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps);
236
238 // Operations on address spaces found by searching.
240public:
245 virtual void clear();
246
253
260
266
273
285
297
304 bool hasInterruptState() const;
305
314
320 bool hasFrameState() const;
321
322 virtual SValuePtr readLocal(size_t index);
323 virtual void writeLocal(size_t index, const SValuePtr &value);
324
328 virtual SValuePtr peekOperand(size_t depth = 0);
329
333 virtual void pushOperand(const SValuePtr &value);
334
339
344
349
353 virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
354
358 virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
359 RiscOperators *addrOps, RiscOperators *valOps);
360
364 virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps);
365
369 virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
370
379 virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
380
389 virtual SValuePtr peekInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
390
397 virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps);
398
403 SValuePtr raiseInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
404
410 SValuePtr clearInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
411
419 bool isInterruptDefinitelyRaised(unsigned major, unsigned minor, RiscOperators *valOps);
420
428 bool isInterruptDefinitelyClear(unsigned major, unsigned minor, RiscOperators *valOps);
429
434 virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const;
435
442 void printRegisters(std::ostream &stream, const std::string &prefix = "");
443 virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
451 void printMemory(std::ostream &stream, const std::string &prefix = "") const;
452 virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
461 void printInterrupts(std::ostream&, const std::string &prefix = "");
462 virtual void printInterrupts(std::ostream &stream, Formatter &fmt) const;
471 void print(std::ostream &stream, const std::string &prefix = "") const;
472 virtual void print(std::ostream&, Formatter&) const;
478 std::string toString() const;
479
482 StatePtr obj;
483 Formatter &fmt;
484 public:
485 WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
486 void print(std::ostream &stream) const { obj->print(stream, fmt); }
487 };
488
504 WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
506 WithFormatter operator+(const std::string &linePrefix);
514 virtual bool merge(const StatePtr &other, RiscOperators *ops);
515};
516
517std::ostream& operator<<(std::ostream&, const State&);
518std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
519
520} // namespace
521} // namespace
522} // namespace
523} // namespace
524
525#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
528#endif
529
530#endif
531#endif
Base class for most instruction semantics RISC operators.
Base class for semantics machine states.
Definition State.h:46
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:504
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.
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.
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:505
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.
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 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.