ROSE 2.20.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
27using Fields = std::map<std::string, SValuePtr>;
28
30// State
32
50class State: public boost::enable_shared_from_this<State> {
51public:
53 using Ptr = StatePtr;
54
55private:
56 SValuePtr protoval_; // Initial value used to create additional values as needed.
57 std::vector<AddressSpacePtr> addressSpaces_; // All address spaces for this semantic state.
58 RegisterStatePtr registers_; // All machine register values for this semantic state.
59 MemoryStatePtr memory_; // All memory for this semantic state.
60 RegisterStatePtr interrupts_; // Whether interrupts occurred.
61 Fields fields_; // Storage for field values.
62 SValuePtr returnValue_; // Result returned by the root method, if any.
63 bool terminated_ = false; // No further instructions execute on this path.
64
66 // Serialization.
68#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
69private:
70 friend class boost::serialization::access;
71
72 template<class S>
73 void serialize(S &s, const unsigned version) {
74 ASSERT_require2(version >= 2, version);
75 s & BOOST_SERIALIZATION_NVP(protoval_);
76 s & BOOST_SERIALIZATION_NVP(addressSpaces_);
77 s & BOOST_SERIALIZATION_NVP(registers_);
78 s & BOOST_SERIALIZATION_NVP(memory_);
79 s & BOOST_SERIALIZATION_NVP(interrupts_);
80 s & BOOST_SERIALIZATION_NVP(returnValue_);
81 s & BOOST_SERIALIZATION_NVP(terminated_);
82 }
83#endif
84
85
87 // Real constructors.
89protected:
90 // needed for serialization
91 State();
92
93 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts, const FrameStatePtr &frame);
94 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
95 State(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
96
97 // deep-copy the registers and memory
98 State(const State &other);
99
100public:
101 virtual ~State();
102
104 // Static allocating constructors
106public:
108 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory,
109 const RegisterStatePtr &interrupts, const FrameStatePtr &frame);
110
112 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory, const RegisterStatePtr &interrupts);
113
115 static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory);
116
121 static StatePtr instance(const StatePtr &other);
122
124 // Virtual constructors.
126public:
131 virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const;
132
137 virtual StatePtr clone() const;
138
140 // Dynamic pointer casts. No-op since this is the base class.
142public:
143 static StatePtr promote(const StatePtr&);
144
146 // Properties of a state.
148public:
159
161 // Address space declaration and searching.
163public:
172
174 const std::vector<AddressSpacePtr>& addressSpaces() const;
175
181
187
189 // Low-level operations on address spaces.
191public:
207 virtual SValuePtr read(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
208 RiscOperators &addrOps, RiscOperators &valOps);
209
213 virtual SValuePtr peek(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &dflt,
214 RiscOperators &addrOps, RiscOperators &valOps);
215
227 virtual void write(const AddressSpacePtr&, const AddressSpaceAddress&, const SValuePtr &value,
228 RiscOperators &addrOps, RiscOperators &valOps);
229
242 virtual bool merge(const StatePtr &other, RiscOperators *addrOps, RiscOperators *valOps);
243
245 // Operations on address spaces found by searching.
247public:
252 virtual void clear();
253
260
267
273
280
292
304
311 bool hasInterruptState() const;
312
321
327 bool hasFrameState() const;
328
332 void terminate();
334
336 bool isTerminated() const;
337
342
343 virtual SValuePtr readLocal(size_t index);
344 virtual void writeLocal(size_t index, const SValuePtr &value);
345
349 virtual SValuePtr peekOperand(size_t depth = 0);
350
354 virtual void pushOperand(const SValuePtr &value);
355
360
362 virtual void pushFrame(const FrameStatePtr &frame);
363
366
369
374
379
383 virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
384
388 virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
389 RiscOperators *addrOps, RiscOperators *valOps);
390
394 virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps);
395
399 virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
400
402 void putField(const std::string &fieldId, const SValuePtr &value);
403
405 SValuePtr getField(const std::string &fieldId) const;
406
415 virtual SValuePtr readInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
416
425 virtual SValuePtr peekInterrupt(unsigned major, unsigned minor, const SValuePtr &dflt, RiscOperators *valOps);
426
433 virtual bool writeInterrupt(unsigned major, unsigned minor, const SValuePtr &value, RiscOperators *valOps);
434
439 SValuePtr raiseInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
440
446 SValuePtr clearInterrupt(unsigned major, unsigned minor, RiscOperators *valOps);
447
455 bool isInterruptDefinitelyRaised(unsigned major, unsigned minor, RiscOperators *valOps);
456
464 bool isInterruptDefinitelyClear(unsigned major, unsigned minor, RiscOperators *valOps);
465
470 virtual void hash(Combinatorics::Hasher&, RiscOperators *addrOps, RiscOperators *valOps) const;
471
478 void printRegisters(std::ostream &stream, const std::string &prefix = "");
479 virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
487 void printMemory(std::ostream &stream, const std::string &prefix = "") const;
488 virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
497 void printInterrupts(std::ostream&, const std::string &prefix = "");
498 virtual void printInterrupts(std::ostream &stream, Formatter &fmt) const;
507 void print(std::ostream &stream, const std::string &prefix = "") const;
508 virtual void print(std::ostream&, Formatter&) const;
514 std::string toString() const;
515
518 StatePtr obj;
519 Formatter &fmt;
520 public:
521 WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
522 void print(std::ostream &stream) const { obj->print(stream, fmt); }
523 };
524
540 WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
542 WithFormatter operator+(const std::string &linePrefix);
550 virtual bool merge(const StatePtr &other, RiscOperators *ops);
551};
552
553std::ostream& operator<<(std::ostream&, const State&);
554std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
555
556} // namespace
557} // namespace
558} // namespace
559} // namespace
560
561#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
564#endif
565
566#endif
567#endif
Base class for most instruction semantics RISC operators.
Base class for semantics machine states.
Definition State.h:50
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:540
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:541
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.
void putField(const std::string &fieldId, const SValuePtr &value)
Store the semantic value of a field.
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.
SValuePtr getField(const std::string &fieldId) const
Return the semantic value of a field, or null if it has not been stored.
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.
std::map< std::string, SValuePtr > Fields
Maps field identities to their current semantic values.
Definition State.h:27
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.