ROSE 0.11.145.147
PartialSymbolicSemantics.h
1#ifndef ROSE_BinaryAnalysis_InstructionSemantics_PartialSymbolicSemantics_H
2#define ROSE_BinaryAnalysis_InstructionSemantics_PartialSymbolicSemantics_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/BasicTypes.h>
7#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/Formatter.h>
8#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/MemoryCellList.h>
9#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/State.h>
10#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/SValue.h>
11
12#ifdef ROSE_HAVE_LIBGCRYPT
13#include <gcrypt.h>
14#endif
15
16namespace Rose {
17namespace BinaryAnalysis {
18namespace InstructionSemantics {
19
33namespace PartialSymbolicSemantics {
34
36// Print formatter
38
42protected:
43 typedef std::map<uint64_t, uint64_t> Map;
44 Map renames;
45 size_t next_name;
46public:
47 Formatter(): next_name(1) {}
48 uint64_t rename(uint64_t orig_name);
49};
50
51
53// Semantic values
55
58
62public:
65
67 using Ptr = SValuePtr;
68
69public:
70 uint64_t name;
71 uint64_t offset;
72 bool negate;
75 // Real constructors
76protected:
77 explicit SValue(size_t nbits);
78 SValue(size_t nbits, uint64_t number);
79 SValue(size_t nbits, uint64_t name, uint64_t offset, bool negate);
80
81public:
87 static uint64_t nextName();
88
90 // Static allocating constructors
91public:
94
96 static SValuePtr instance(size_t nbits);
97
99 static SValuePtr instance(size_t nbits, uint64_t value);
100
102 static SValuePtr instance(size_t nbits, uint64_t name, uint64_t offset, bool negate);
103
105 // Virtual constructors
106public:
107 virtual BaseSemantics::SValuePtr bottom_(size_t nbits) const override;
108 virtual BaseSemantics::SValuePtr undefined_(size_t nbits) const override;
109 virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) const override;
110 virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) const override;
111 virtual BaseSemantics::SValuePtr copy(size_t new_width=0) const override;
112
115 const SmtSolverPtr&) const override;
116
118 virtual BaseSemantics::SValuePtr create(size_t nbits, uint64_t name, uint64_t offset, bool negate) const;
119
121 // Dynamic pointer casts
122public:
126
128 // Other stuff we inherited from the super class
129public:
130 virtual void hash(Combinatorics::Hasher&) const override;
131 virtual void print(std::ostream&, BaseSemantics::Formatter&) const override;
132
133 virtual bool isBottom() const override {
134 return false;
135 }
136
138 // Override legacy members. These have similar non-virtual camelCase names in the base class, and eventually we'll remoe
139 // these snake_case names and change the camelCase names to be the virtual functions. Therefore, be sure to use "override"
140 // in your own code so you know when we make this change.
141public:
142 // See nBits
143 virtual void set_width(size_t nbits) override;
144
145 // See mayEqual
146 virtual bool may_equal(const BaseSemantics::SValuePtr &other,
147 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
148
149 // See mustEqual
150 virtual bool must_equal(const BaseSemantics::SValuePtr &other,
151 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
152
153 // See isConcrete
154 virtual bool is_number() const override {
155 return 0==name;
156 }
157
158 // See toUnsigned and toSigned.
159 virtual uint64_t get_number() const override {
160 ASSERT_require(is_number());
161 return offset;
162 }
163};
164
165
167// Register state
169
170typedef BaseSemantics::RegisterStateGeneric RegisterState;
171typedef BaseSemantics::RegisterStateGenericPtr RegisterStatePtr;
172
173
175// Memory state
177
178// PartialSymbolicSemantics uses BaseSemantics::MemoryCellList (or subclass) as its memory state, and does not expect the
179// MemoryCellList to be byte-restricted (i.e., the cells can store multi-byte values).
180
181typedef BaseSemantics::MemoryCellList MemoryState;
182typedef BaseSemantics::MemoryCellListPtr MemoryStatePtr;
183
184
186// Complete state
188
190typedef boost::shared_ptr<class State> StatePtr;
191
195public:
198
200 using Ptr = StatePtr;
201
203 // Real constructors
204protected:
206 State(const State &other);
207
209 // Static allocating constructors
210public:
213
215 static StatePtr instance(const StatePtr &other);
216
218 // Virtual constructors
219public:
221 const BaseSemantics::MemoryStatePtr &memory) const override;
222 virtual BaseSemantics::StatePtr clone() const override;
223
225 // Dynamic pointer casts
226public:
227 static StatePtr promote(const BaseSemantics::StatePtr&);
228
230 // Methods first declared at this level of the class hierarchy
231public:
234 virtual void print_diff_registers(std::ostream&, const StatePtr &other_state, Formatter&) const;
235
237 virtual bool equal_registers(const StatePtr &other) const;
238
241 virtual void discard_popped_memory();
242};
243
244
246// RISC operators
248
250typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
251
254public:
257
260
261protected:
262 MemoryMapPtr map;
263
265 // Real constructors
266protected:
268
269 explicit RiscOperators(const BaseSemantics::StatePtr&, const SmtSolverPtr&);
270
272 // Static allocating constructors
273public:
275
279
282 const SmtSolverPtr &solver = SmtSolverPtr());
283
286
288 // Virtual constructors
289public:
291 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
292
294 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
295
297 // Dynamic pointer casts
298public:
302
304 // Methods first declared at this level of the class hierarchy
305public:
315 // Risc operators inherited
316public:
317 virtual void interrupt(int majr, int minr) override;
319 const BaseSemantics::SValuePtr &b_) override;
321 const BaseSemantics::SValuePtr &b_) override;
323 const BaseSemantics::SValuePtr &b_) override;
326 size_t begin_bit, size_t end_bit) override;
328 const BaseSemantics::SValuePtr &b_) override;
332 const BaseSemantics::SValuePtr &sa_) override;
334 const BaseSemantics::SValuePtr &sa_) override;
336 const BaseSemantics::SValuePtr &sa_) override;
338 const BaseSemantics::SValuePtr &sa_) override;
340 const BaseSemantics::SValuePtr &sa_) override;
343 const BaseSemantics::SValuePtr &a_,
344 const BaseSemantics::SValuePtr &b_,
345 IteStatus&) override;
346 virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override;
348 const BaseSemantics::SValuePtr &b_) override;
350 const BaseSemantics::SValuePtr &b_,
351 const BaseSemantics::SValuePtr &c_,
352 BaseSemantics::SValuePtr &carry_out/*out*/) override;
355 const BaseSemantics::SValuePtr &b_) override;
357 const BaseSemantics::SValuePtr &b_) override;
359 const BaseSemantics::SValuePtr &b_) override;
361 const BaseSemantics::SValuePtr &b_) override;
363 const BaseSemantics::SValuePtr &b_) override;
365 const BaseSemantics::SValuePtr &b_) override;
367 const BaseSemantics::SValuePtr &addr,
368 const BaseSemantics::SValuePtr &dflt,
369 const BaseSemantics::SValuePtr &cond) override;
371 const BaseSemantics::SValuePtr &addr,
372 const BaseSemantics::SValuePtr &dflt) override;
373 virtual void writeMemory(RegisterDescriptor segreg,
374 const BaseSemantics::SValuePtr &addr,
375 const BaseSemantics::SValuePtr &data,
376 const BaseSemantics::SValuePtr &cond) override;
377
378protected:
379 BaseSemantics::SValuePtr readOrPeekMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &address,
380 const BaseSemantics::SValuePtr &dflt_, bool allowSideEffects);
381};
382
383} // namespace
384} // namespace
385} // namespace
386} // namespace
387
388#endif
389#endif
Base class for most instruction semantics RISC operators.
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
Base class for semantics machine states.
Definition State.h:41
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::StatePtr &, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
static RiscOperatorsPtr instanceFromRegisters(const RegisterDictionaryPtr &)
Instantiates a new RiscOperators object and configures it to use semantic values and states that are ...
virtual BaseSemantics::SValuePtr signedModulo(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Calculates modulo with signed values.
static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators object with specified prototypical values.
virtual BaseSemantics::SValuePtr add(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Adds two integers of equal size.
virtual BaseSemantics::SValuePtr peekMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt) override
Read memory without side effects.
virtual BaseSemantics::SValuePtr shiftLeft(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted left.
static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &)
Run-time promotion of a base RiscOperators pointer to partial symbolic operators.
virtual void writeMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override
Writes a value to memory.
virtual BaseSemantics::SValuePtr concat(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Concatenates the bits of two values.
virtual BaseSemantics::SValuePtr negate(const BaseSemantics::SValuePtr &a_) override
Two's complement.
virtual BaseSemantics::SValuePtr xor_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise XOR of two values.
void set_memory_map(const MemoryMapPtr &)
A memory map can be used to provide default values for memory cells that are read before being writte...
virtual BaseSemantics::SValuePtr invert(const BaseSemantics::SValuePtr &a_) override
One's complement.
virtual BaseSemantics::SValuePtr signedDivide(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Divides two signed values.
virtual BaseSemantics::SValuePtr and_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise AND of two values.
virtual BaseSemantics::SValuePtr equalToZero(const BaseSemantics::SValuePtr &a_) override
Determines whether a value is equal to zero.
virtual BaseSemantics::SValuePtr shiftRightArithmetic(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted right arithmetically (with sign bit).
virtual BaseSemantics::SValuePtr unsignedDivide(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Divides two unsigned values.
virtual BaseSemantics::SValuePtr iteWithStatus(const BaseSemantics::SValuePtr &sel_, const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_, IteStatus &) override
If-then-else with status.
virtual BaseSemantics::SValuePtr readMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt, const BaseSemantics::SValuePtr &cond) override
Reads a value from memory.
virtual BaseSemantics::SValuePtr or_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise OR of two values.
virtual BaseSemantics::SValuePtr signedMultiply(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Multiplies two signed values.
virtual BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr &a_, size_t begin_bit, size_t end_bit) override
Extracts bits from a value.
virtual void interrupt(int majr, int minr) override
Unconditionally raise an interrupt.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr &, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators with specified state.
virtual BaseSemantics::SValuePtr rotateLeft(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Rotate bits to the left.
virtual BaseSemantics::SValuePtr addWithCarries(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_, const BaseSemantics::SValuePtr &c_, BaseSemantics::SValuePtr &carry_out) override
Add two values of equal size and a carry bit.
virtual BaseSemantics::SValuePtr leastSignificantSetBit(const BaseSemantics::SValuePtr &a_) override
Returns position of least significant set bit; zero when no bits are set.
virtual BaseSemantics::SValuePtr mostSignificantSetBit(const BaseSemantics::SValuePtr &a_) override
Returns position of most significant set bit; zero when no bits are set.
virtual BaseSemantics::SValuePtr unsignedMultiply(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Multiply two unsigned values.
virtual BaseSemantics::SValuePtr shiftRight(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted right logically (no sign bit).
virtual BaseSemantics::SValuePtr unsignedModulo(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Calculates modulo with unsigned values.
virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override
Sign extends a value.
virtual BaseSemantics::SValuePtr rotateRight(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Rotate bits to the right.
const MemoryMapPtr get_memory_map() const
A memory map can be used to provide default values for memory cells that are read before being writte...
Type of values manipulated by the PartialSymbolicSemantics domain.
virtual bool isBottom() const override
Determines whether a value is a data-flow bottom.
static SValuePtr instance(size_t nbits, uint64_t name, uint64_t offset, bool negate)
Insantiate a new value with all the necessary parts.
virtual Sawyer::Optional< BaseSemantics::SValuePtr > createOptionalMerge(const BaseSemantics::SValuePtr &other, const BaseSemantics::MergerPtr &, const SmtSolverPtr &) const override
Possibly create a new value by merging two existing values.
bool negate
Switch between name+offset and (-name)+offset; should be false for constants.
virtual bool may_equal(const BaseSemantics::SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual API.
static uint64_t nextName()
Returns the next available name.
virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) const override
Create a new unspecified semantic value.
static SValuePtr promote(const BaseSemantics::SValuePtr &)
Promote a base value to a PartialSymbolicSemantics value.
virtual BaseSemantics::SValuePtr bottom_(size_t nbits) const override
Data-flow bottom value.
static SValuePtr instance(size_t nbits, uint64_t value)
Instantiate a new concrete value.
virtual void print(std::ostream &, BaseSemantics::Formatter &) const override
Print a value to a stream using default format.
virtual void hash(Combinatorics::Hasher &) const override
Hash this semantic value.
virtual BaseSemantics::SValuePtr copy(size_t new_width=0) const override
Create a new value from an existing value, changing the width if new_width is non-zero.
virtual BaseSemantics::SValuePtr undefined_(size_t nbits) const override
Create a new undefined semantic value.
uint64_t name
Zero for constants; non-zero ID number for everything else.
static SValuePtr instance()
Instantiate a new prototypical value.
virtual BaseSemantics::SValuePtr create(size_t nbits, uint64_t name, uint64_t offset, bool negate) const
Virtual allocating constructor.
virtual bool must_equal(const BaseSemantics::SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual API.
virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) const override
Create a new concrete semantic value.
static SValuePtr instance(size_t nbits)
Instantiate a new undefined value of specified width.
virtual BaseSemantics::StatePtr clone() const override
Virtual copy constructor.
virtual BaseSemantics::StatePtr create(const BaseSemantics::RegisterStatePtr &registers, const BaseSemantics::MemoryStatePtr &memory) const override
Virtual constructor.
static StatePtr instance(const StatePtr &other)
Instantiates a new copy of an existing state.
virtual bool equal_registers(const StatePtr &other) const
Tests registers of two states for equality.
virtual void discard_popped_memory()
Removes from memory those values at addresses below the current stack pointer.
virtual void print_diff_registers(std::ostream &, const StatePtr &other_state, Formatter &) const
Print info about how registers differ.
static StatePtr instance(const BaseSemantics::RegisterStatePtr &registers, const BaseSemantics::MemoryStatePtr &memory)
Instantiates a new instance of memory state with specified register and memory states.
Describes (part of) a physical CPU register.
Holds a value or nothing.
Definition Optional.h:56
Reference-counting intrusive smart pointer.
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< class MemoryCellList > MemoryCellListPtr
Shared-ownership pointer to a list-based memory state.
boost::shared_ptr< class RegisterStateGeneric > RegisterStateGenericPtr
Shared-ownership pointer to generic register states.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to partial symbolic semantics RISC operations.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer to a partial-symbolic semantic value.
boost::shared_ptr< class State > StatePtr
Shared-ownership pointer to partial symbolic semantics state.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.