ROSE 0.11.145.192
StaticSemantics.h
1// Turn instruction semantics into part of the AST
2#ifndef ROSE_BinaryAnalysis_InstructionSemantics_StaticSemantics_H
3#define ROSE_BinaryAnalysis_InstructionSemantics_StaticSemantics_H
4#include <featureTests.h>
5#ifdef ROSE_ENABLE_BINARY_ANALYSIS
6
7#include <Rose/BinaryAnalysis/BasicTypes.h>
8
9#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics/SValue.h>
10#include <Rose/BinaryAnalysis/InstructionSemantics/NullSemantics.h>
11
12#include <SgAsmRiscOperation.h>
13
14namespace Rose {
15namespace BinaryAnalysis {
16namespace InstructionSemantics {
17
41namespace StaticSemantics {
42
44// Free functions
46
63// Value type
65
68
77public:
80
82 using Ptr = SValuePtr;
83
84protected:
85 SgAsmExpression *ast_;
86
87protected:
89 SValue(size_t nbits, uint64_t number);
90 SValue(const SValue &other);
91
92public:
97
99 static SValuePtr instance_bottom(size_t nbits);
100
106 static SValuePtr instance_undefined(size_t nbits);
107
113 static SValuePtr instance_unspecified(size_t nbits);
114
116 static SValuePtr instance_integer(size_t nbits, uint64_t value);
117
118public:
119 virtual BaseSemantics::SValuePtr bottom_(size_t nbits) const override;
120 virtual BaseSemantics::SValuePtr undefined_(size_t nbits) const override;
121 virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) const override;
122 virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) const override;
123 virtual BaseSemantics::SValuePtr boolean_(bool value) const override;
124 virtual BaseSemantics::SValuePtr copy(size_t new_width=0) const override;
125
128
129public:
132
133public:
134 // These are not needed since this domain never tries to compare semantic values.
135 virtual bool may_equal(const BaseSemantics::SValuePtr &/*other*/, const SmtSolverPtr& = SmtSolverPtr()) const override;
136 virtual bool must_equal(const BaseSemantics::SValuePtr &/*other*/, const SmtSolverPtr& = SmtSolverPtr()) const override;
137 virtual void set_width(size_t /*nbits*/) override;
138 virtual bool isBottom() const override;
139 virtual bool is_number() const override;
140 virtual uint64_t get_number() const override;
141 virtual void hash(Combinatorics::Hasher&) const override;
142 virtual void print(std::ostream&, BaseSemantics::Formatter&) const override;
143
144public:
149 virtual void ast(SgAsmExpression*);
151};
152
154// State
156
157// No state necessary for this domain. All instruction side effects are immediately attached to the SgAsmInstruction node
158// rather than being stored in some state.
159
161typedef NullSemantics::RegisterStatePtr RegisterStatePtr;
162
164typedef NullSemantics::MemoryStatePtr MemoryStatePtr;
165
167typedef NullSemantics::StatePtr StatePtr;
168
169
171// RiscOperators
173
175typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
176
188public:
191
194
195protected:
197
199
200public:
202
206
211 const SmtSolverPtr &solver = SmtSolverPtr());
212
216
218 // Virtual constructors
219public:
221 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
222
224 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
225
227 // Dynamic pointer casts
228public:
232
234 // Supporting functions
235protected:
246 const BaseSemantics::SValuePtr &a);
249 const BaseSemantics::SValuePtr &b);
253 const BaseSemantics::SValuePtr &c);
258 const BaseSemantics::SValuePtr &d);
268
270 // Override all operator methods from base class. These are the RISC operators that are invoked by a Dispatcher.
271public:
272 virtual void startInstruction(SgAsmInstruction*) override;
276 virtual void hlt() override;
277 virtual void cpuid() override;
278 virtual BaseSemantics::SValuePtr rdtsc() override;
280 const BaseSemantics::SValuePtr &b_) override;
282 const BaseSemantics::SValuePtr &b_) override;
284 const BaseSemantics::SValuePtr &b_) override;
287 size_t begin_bit, size_t end_bit) override;
289 const BaseSemantics::SValuePtr &b_) override;
293 const BaseSemantics::SValuePtr &sa_) override;
295 const BaseSemantics::SValuePtr &sa_) override;
297 const BaseSemantics::SValuePtr &sa_) override;
299 const BaseSemantics::SValuePtr &sa_) override;
301 const BaseSemantics::SValuePtr &sa_) override;
304 const BaseSemantics::SValuePtr &a_,
305 const BaseSemantics::SValuePtr &b_,
306 IteStatus&) override;
310 const BaseSemantics::SValuePtr&) override;
312 const BaseSemantics::SValuePtr&) override;
314 const BaseSemantics::SValuePtr&) override;
316 const BaseSemantics::SValuePtr&) override;
318 const BaseSemantics::SValuePtr&) override;
320 const BaseSemantics::SValuePtr&) override;
322 const BaseSemantics::SValuePtr&) override;
324 const BaseSemantics::SValuePtr&) override;
325 virtual BaseSemantics::SValuePtr unsignedExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override;
326 virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override;
328 const BaseSemantics::SValuePtr &b_) override;
330 const BaseSemantics::SValuePtr &b_) override;
332 const BaseSemantics::SValuePtr &b_,
333 const BaseSemantics::SValuePtr &c_,
334 BaseSemantics::SValuePtr &carry_out/*out*/) override;
337 const BaseSemantics::SValuePtr &b_) override;
339 const BaseSemantics::SValuePtr &b_) override;
341 const BaseSemantics::SValuePtr &b_) override;
343 const BaseSemantics::SValuePtr &b_) override;
345 const BaseSemantics::SValuePtr &b_) override;
347 const BaseSemantics::SValuePtr &b_) override;
348 virtual void interrupt(int majr, int minr) override;
350 const BaseSemantics::SValuePtr &dflt) override;
352 const BaseSemantics::SValuePtr &dflt) override;
353 virtual void writeRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &a) override;
355 const BaseSemantics::SValuePtr &addr,
356 const BaseSemantics::SValuePtr &dflt,
357 const BaseSemantics::SValuePtr &cond) override;
359 const BaseSemantics::SValuePtr &addr,
360 const BaseSemantics::SValuePtr &dflt) override;
361 virtual void writeMemory(RegisterDescriptor segreg,
362 const BaseSemantics::SValuePtr &addr,
363 const BaseSemantics::SValuePtr &data,
364 const BaseSemantics::SValuePtr &cond) override;
365};
366
367
368} // namespace
369} // namespace
370} // namespace
371} // namespace
372
373#endif
374#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:42
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
virtual BaseSemantics::SValuePtr iteWithStatus(const BaseSemantics::SValuePtr &sel_, const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_, IteStatus &) override
If-then-else with status.
static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators object with specified prototypical values.
virtual BaseSemantics::SValuePtr unsignedExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override
Extend (or shrink) operand a so it is nbits wide by adding or removing high-order bits.
BaseSemantics::SValuePtr makeSValue(size_t nbits, SgAsmRiscOperation::RiscOperator, const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b)
Create a new SValue.
virtual void hlt() override
Invoked for the x86 HLT instruction.
virtual BaseSemantics::SValuePtr negate(const BaseSemantics::SValuePtr &a_) override
Two's complement.
static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &)
Run-time promotion of a base RiscOperators pointer to static 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 void writeRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &a) override
Writes a value to a register.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
virtual BaseSemantics::SValuePtr isSignedLessThan(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Comparison for signed values.
virtual BaseSemantics::SValuePtr unsignedMultiply(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Multiply two unsigned values.
virtual BaseSemantics::SValuePtr add(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Adds two integers of equal size.
virtual BaseSemantics::SValuePtr isUnsignedLessThan(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Comparison for unsigned values.
virtual BaseSemantics::SValuePtr invert(const BaseSemantics::SValuePtr &a_) override
One's complement.
static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr &, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators object with specified state.
virtual BaseSemantics::SValuePtr unsignedDivide(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Divides two unsigned values.
virtual BaseSemantics::SValuePtr subtract(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Subtract one value from another.
virtual BaseSemantics::SValuePtr equalToZero(const BaseSemantics::SValuePtr &a_) override
Determines whether a value is equal to zero.
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 filterCallTarget(const BaseSemantics::SValuePtr &a) override
Invoked to filter call targets.
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 isSignedGreaterThan(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Comparison for signed values.
BaseSemantics::SValuePtr makeSValue(size_t nbits, SgAsmRiscOperation::RiscOperator, const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b, const BaseSemantics::SValuePtr &c, const BaseSemantics::SValuePtr &d)
Create a new SValue.
BaseSemantics::SValuePtr makeSValue(size_t nbits, SgAsmRiscOperation::RiscOperator, const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b, const BaseSemantics::SValuePtr &c)
Create a new SValue.
void saveSemanticEffect(const BaseSemantics::SValuePtr &)
Save instruction side effect.
virtual BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr &a_, size_t begin_bit, size_t end_bit) override
Extracts bits from a value.
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 xor_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise XOR of two values.
BaseSemantics::SValuePtr makeSValue(size_t nbits, SgAsmRiscOperation::RiscOperator, const BaseSemantics::SValuePtr &a)
Create a new SValue.
virtual BaseSemantics::SValuePtr and_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise AND of two values.
virtual BaseSemantics::SValuePtr rotateRight(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Rotate bits to the right.
virtual BaseSemantics::SValuePtr isUnsignedGreaterThanOrEqual(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Comparison for unsigned values.
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 isSignedGreaterThanOrEqual(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Comparison for signed values.
virtual BaseSemantics::SValuePtr signedModulo(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Calculates modulo with signed values.
virtual BaseSemantics::SValuePtr rotateLeft(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Rotate bits to the left.
virtual BaseSemantics::SValuePtr signedDivide(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Divides two signed values.
virtual BaseSemantics::SValuePtr unsignedModulo(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Calculates modulo with unsigned values.
virtual BaseSemantics::SValuePtr signedMultiply(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Multiplies two signed values.
static RiscOperatorsPtr instanceFromRegisters(const RegisterDictionaryPtr &, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators object and configures it to use semantic values and states that are ...
virtual BaseSemantics::SValuePtr isUnsignedGreaterThan(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Comparison for unsigned values.
virtual BaseSemantics::SValuePtr shiftRightArithmetic(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted right arithmetically (with sign bit).
virtual BaseSemantics::SValuePtr isEqual(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Equality comparison.
virtual BaseSemantics::SValuePtr isSignedLessThanOrEqual(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Comparison for signed values.
virtual BaseSemantics::SValuePtr isUnsignedLessThanOrEqual(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Comparison for unsigned values.
virtual BaseSemantics::SValuePtr concat(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Concatenates the bits of two values.
BaseSemantics::SValuePtr makeSValue(size_t nbits, SgAsmRiscOperation::RiscOperator)
Create a new SValue.
virtual BaseSemantics::SValuePtr peekMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt) override
Read memory without side effects.
virtual BaseSemantics::SValuePtr shiftRight(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted right logically (no sign bit).
BaseSemantics::SValuePtr makeSValue(size_t nbits, SgAsmExpression *)
Create a new SValue.
virtual BaseSemantics::SValuePtr or_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise OR of two values.
virtual void interrupt(int majr, int minr) override
Unconditionally raise an interrupt.
virtual BaseSemantics::SValuePtr isNotEqual(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Equality comparison.
virtual BaseSemantics::SValuePtr readRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt) override
Reads a value from a register.
virtual void startInstruction(SgAsmInstruction *) override
Called at the beginning of every instruction.
virtual void cpuid() override
Invoked for the x86 CPUID instruction.
virtual BaseSemantics::SValuePtr rdtsc() override
Invoked for the x86 RDTSC instruction.
virtual BaseSemantics::SValuePtr filterReturnTarget(const BaseSemantics::SValuePtr &a) override
Invoked to filter return targets.
virtual BaseSemantics::SValuePtr peekRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt) override
Obtain a register value without side effects.
virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override
Sign extends a value.
virtual BaseSemantics::SValuePtr filterIndirectJumpTarget(const BaseSemantics::SValuePtr &a) override
Invoked to filter indirect jumps.
virtual BaseSemantics::SValuePtr shiftLeft(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted left.
Semantic values for generating static semantic ASTs.
virtual SgAsmExpression * ast()
Property: Abstract syntax tree.
virtual void print(std::ostream &, BaseSemantics::Formatter &) const override
Print a value to a stream using default format.
virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) const override
Create a new concrete semantic value.
static SValuePtr instance_integer(size_t nbits, uint64_t value)
Instantiate an integer constant.
virtual bool isBottom() const override
Determines whether a value is a data-flow bottom.
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.
static SValuePtr instance_undefined(size_t nbits)
Instantiate an undefined value.
virtual BaseSemantics::SValuePtr undefined_(size_t nbits) const override
Create a new undefined semantic value.
virtual bool must_equal(const BaseSemantics::SValuePtr &, const SmtSolverPtr &=SmtSolverPtr()) const override
Virtual API.
static SValuePtr promote(const BaseSemantics::SValuePtr &)
Promote a base value to a static semantics value.
static SValuePtr instance()
Instantiate a prototypical SValue.
virtual bool may_equal(const BaseSemantics::SValuePtr &, const SmtSolverPtr &=SmtSolverPtr()) const override
Virtual API.
virtual BaseSemantics::SValuePtr boolean_(bool value) const override
Create a new, Boolean value.
virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) const override
Create a new unspecified semantic value.
virtual uint64_t get_number() const override
Virtual API.
virtual void ast(SgAsmExpression *)
Property: Abstract syntax tree.
virtual void hash(Combinatorics::Hasher &) const override
Hash this semantic value.
virtual BaseSemantics::SValuePtr bottom_(size_t nbits) const override
Data-flow bottom value.
static SValuePtr instance_bottom(size_t nbits)
Instantiate a data-flow bottom value.
virtual Sawyer::Optional< BaseSemantics::SValuePtr > createOptionalMerge(const BaseSemantics::SValuePtr &, const BaseSemantics::MergerPtr &, const SmtSolverPtr &) const override
Possibly create a new value by merging two existing values.
static SValuePtr instance_unspecified(size_t nbits)
Instantiate an unspecified value.
Describes (part of) a physical CPU register.
Holds a value or nothing.
Definition Optional.h:56
Reference-counting intrusive smart pointer.
Base class for expressions.
Base class for machine instructions.
RiscOperator
One enum per RISC operator.
This class represents the base class for all IR nodes within Sage III.
std::shared_ptr< const Base > BaseConstPtr
Reference counted pointer for Architecture::Base.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
boost::shared_ptr< class RegisterState > RegisterStatePtr
Shared-ownership pointer.
boost::shared_ptr< class MemoryState > MemoryStatePtr
Shared-ownership pointer.
void attachInstructionSemantics(SgNode *ast, const Architecture::BaseConstPtr &)
Build and attach static semantics to all instructions.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer for a static-semantics value.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer for basic semantic operations.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.