ROSE 2.9.0
Loading...
Searching...
No Matches
TraceSemantics.h
1#ifndef ROSE_BinaryAnalysis_InstructionSemantics_TraceSemantics_H
2#define ROSE_BinaryAnalysis_InstructionSemantics_TraceSemantics_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics.h>
7#include <Rose/Diagnostics.h>
8
9namespace Rose {
10namespace BinaryAnalysis {
11namespace InstructionSemantics {
12
52namespace TraceSemantics {
53
54
56// Semantic values
58
59using SValue = void;
60
62using SValuePtr = boost::shared_ptr<void>;
63
64
66// Register state
68
69using RegisterState = void;
70
72using RegisterStatePtr = boost::shared_ptr<void>;
73
74
76// Memory state
78
79using MemoryState = void;
80
82using MemoryStatePtr = boost::shared_ptr<void>;
83
84
86// RISC operators
88
90using RiscOperatorsPtr = boost::shared_ptr<class RiscOperators>;
91
94public:
97
100
101private:
102 BaseSemantics::RiscOperatorsPtr subdomain_; // Domain to which all our RISC operators chain
103 Sawyer::Message::Stream stream_; // stream to which output is emitted
104 std::string indentation_; // string to print at start of each line
105 bool showingSubdomain_ = true; // show subdomain name and address on each line of output?
106 bool showingInstructionVa_ = true; // show instruction VA on each line of output?
107 bool onlyInstructions_ = true; // trace only operations associated with an instruction.
108
110 // Real constructors.
111protected:
112 // use the version that takes a subdomain instead of this c'tor
114
115 // use the version that takes a subdomain instead of this c'tor.
116 explicit RiscOperators(const BaseSemantics::StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr());
117
119
120public:
121 virtual ~RiscOperators();
122
124 // Static allocating constructors.
125public:
130
135
139
141 // Virtual constructors
142public:
144 const SmtSolverPtr& = SmtSolverPtr()) const override;
145
147 const SmtSolverPtr& = SmtSolverPtr()) const override;
148
151
153 // Dynamic pointer casts
154public:
158
160 // Methods first defined at this level of the class hierarchy
161public:
170 void checkSubdomain() const;
171
185 const std::string& indentation() const;
186 void indentation(const std::string&);
194 bool showingSubdomain() const;
215 bool onlyInstructions() const;
219protected:
220 void linePrefix();
221 std::string toString(const BaseSemantics::SValuePtr&);
222 std::string toString(SgAsmFloatType*);
223 void check_equal_widths(const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&);
224 const BaseSemantics::SValuePtr &check_width(const BaseSemantics::SValuePtr &a, size_t nbits,
225 const std::string &what="result");
226 std::string register_name(RegisterDescriptor);
227
228 bool shouldPrint();
229 bool shouldPrint(SgAsmInstruction*);
230
231 void before(const std::string&);
232 void before(const std::string&, RegisterDescriptor);
233 void before(const std::string&, RegisterDescriptor, const BaseSemantics::SValuePtr&);
234 void before(const std::string&, RegisterDescriptor, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&);
235 void before(const std::string&, RegisterDescriptor, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&,
236 size_t);
237 void before(const std::string&, RegisterDescriptor, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&,
239 void before(const std::string&, SgAsmInstruction*, bool showAddress);
240 void before(const std::string&, size_t);
241 void before(const std::string&, size_t, uint64_t);
242 void before(const std::string&, const BaseSemantics::SValuePtr&);
243
244 void before(const std::string&, const BaseSemantics::SValuePtr&, size_t);
245 void before(const std::string&, const BaseSemantics::SValuePtr&, size_t, size_t);
246 void before(const std::string&, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&);
247 void before(const std::string&, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&,
249 void before(const std::string&, const BaseSemantics::SValuePtr&, SgAsmFloatType*);
250 void before(const std::string&, const BaseSemantics::SValuePtr&, SgAsmFloatType*, const BaseSemantics::SValuePtr&);
251 void before(const std::string&, const BaseSemantics::SValuePtr&, SgAsmFloatType*, SgAsmFloatType*);
252 void before(const std::string&, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&, SgAsmFloatType*);
253
254 void after();
255 void after(SgAsmInstruction*);
259 void after(const BaseSemantics::Exception&);
260 void after(const BaseSemantics::Exception&, SgAsmInstruction*);
261 void after_exception();
262 void after_exception(SgAsmInstruction*);
263
265 // Methods we override from our super class
266public:
268 void solver(const SmtSolverPtr&) override;
269 SmtSolverPtr solver() const override;
272 void print(std::ostream&, BaseSemantics::Formatter&) const override;
273 size_t nInsns() const override;
274 void nInsns(size_t n) override;
277 bool isNoopRead() const override;
278 void isNoopRead(bool) override;
281 void comment(const std::string&) override;
282
283 BaseSemantics::SValuePtr undefined_(size_t nbits) override;
285 BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) override;
286 BaseSemantics::SValuePtr boolean_(bool value) override;
287 BaseSemantics::SValuePtr bottom_(size_t nbits) override;
288
292 void hlt() override;
293 void cpuid() override;
295
296 // The actual RISC operators. These are pure virtual in the base class
301 BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr&, size_t begin_bit, size_t end_bit) override;
310 const BaseSemantics::SValuePtr&) override;
313 const BaseSemantics::SValuePtr&, IteStatus&) override;
319 BaseSemantics::SValuePtr&/*out*/) override;
324 const BaseSemantics::SValuePtr&) override;
326 const BaseSemantics::SValuePtr&) override;
328 const BaseSemantics::SValuePtr&) override;
330 const BaseSemantics::SValuePtr&) override;
331
332 void interrupt(int majr, int minr) override;
333
336 const BaseSemantics::SValuePtr&) override;
345 SgAsmFloatType*) override;
347 SgAsmFloatType*) override;
349 SgAsmFloatType*) override;
351 SgAsmFloatType*) override;
354
356 const BaseSemantics::SValuePtr &dflt) override;
358 const BaseSemantics::SValuePtr &dflt) override;
361 const BaseSemantics::SValuePtr &dflt,
362 const BaseSemantics::SValuePtr &cond) override;
364 const BaseSemantics::SValuePtr &dflt) override;
366 const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override;
367
368 BaseSemantics::SValuePtr peekOperand(size_t depth = 0) override;
371};
372
373} // namespace
374} // namespace
375} // namespace
376} // namespace
377
378#endif
379#endif
Base class for most instruction semantics RISC operators.
BaseSemantics::SValuePtr fpEffectiveExponent(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Exponent of floating-point value.
BaseSemantics::SValuePtr negate(const BaseSemantics::SValuePtr &) override
Two's complement.
BaseSemantics::SValuePtr unsignedExtend(const BaseSemantics::SValuePtr &, size_t nbits) override
Extend (or shrink) operand a so it is nbits wide by adding or removing high-order bits.
void interrupt(int majr, int minr) override
Unconditionally raise an interrupt.
BaseSemantics::SValuePtr filterCallTarget(const BaseSemantics::SValuePtr &) override
Invoked to filter call targets.
BaseSemantics::SValuePtr fpSquareRoot(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Square root.
BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr &, size_t begin_bit, size_t end_bit) override
Extracts bits from a value.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::RiscOperatorsPtr &subdomain)
Wraps a subdomain's RISC operators to add tracing.
void subdomain(const BaseSemantics::RiscOperatorsPtr &subdomain)
Property: Subdomain to which operations are forwarded.
BaseSemantics::SValuePtr readRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &dflt) override
Reads a value from a register.
BaseSemantics::SValuePtr fpIsZero(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is equal to zero.
BaseSemantics::SValuePtr shiftRightArithmetic(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Returns arg shifted right arithmetically (with sign bit).
void writeMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override
Writes a value to memory.
BaseSemantics::SValuePtr xor_(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Computes bit-wise XOR of two values.
void currentInstruction(SgAsmInstruction *) override
Property: Current instruction.
BaseSemantics::SValuePtr addWithCarries(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, BaseSemantics::SValuePtr &) override
Add two values of equal size and a carry bit.
BaseSemantics::SValuePtr fpIsInfinity(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is infinity.
void finishInstruction(SgAsmInstruction *) override
Called at the end of every instruction.
bool showingInstructionVa() const
Property: Show instruction in output.
static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &)
Run-time promotion of a base RiscOperators pointer to trace operators.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::StatePtr &state, const SmtSolverPtr &=SmtSolverPtr()) const override
Virtual allocating constructor.
BaseSemantics::SValuePtr iteWithStatus(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, IteStatus &) override
If-then-else with status.
BaseSemantics::SValuePtr signedModulo(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Calculates modulo with signed values.
BaseSemantics::SValuePtr unsignedModulo(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Calculates modulo with unsigned values.
Sawyer::Message::Stream & stream()
Property: output stream to which tracing is emitted.
BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &, size_t nbits) override
Sign extends a value.
BaseSemantics::SValuePtr fpToInteger(const BaseSemantics::SValuePtr &, SgAsmFloatType *, const BaseSemantics::SValuePtr &) override
Construct an integer value from a floating-point value.
const std::string & indentation() const
Property: Line prefix string.
void showingSubdomain(bool)
Property: Show subdomain name in output.
BaseSemantics::SValuePtr fpRoundTowardZero(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Round toward zero.
BaseSemantics::SValuePtr bottom_(size_t nbits) override
Returns a data-flow bottom value.
BaseSemantics::SValuePtr fpSubtract(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Subtract one floating-point value from another.
void currentState(const BaseSemantics::StatePtr &) override
Property: Current semantic state.
BaseSemantics::SValuePtr signedDivide(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Divides two signed values.
static RiscOperatorsPtr instance(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &=SmtSolverPtr())
Instantiates a new RiscOperators object.
static RiscOperatorsPtr instance(const BaseSemantics::StatePtr &, const SmtSolverPtr &=SmtSolverPtr())
Instantiates a new RiscOperators object.
BaseSemantics::SValuePtr fpMultiply(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Multiply two floating-point values.
BaseSemantics::SValuePtr unsignedMultiply(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Multiply two unsigned values.
BaseSemantics::SValuePtr fpSign(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Sign of floating-point value.
BaseSemantics::SValuePtr filterReturnTarget(const BaseSemantics::SValuePtr &) override
Invoked to filter return targets.
BaseSemantics::SValuePtr fpAdd(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Add two floating-point values.
bool showingSubdomain() const
Property: Show subdomain name in output.
BaseSemantics::SValuePtr unsignedDivide(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Divides two unsigned values.
BaseSemantics::SValuePtr or_(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Computes bit-wise OR of two values.
void startInstruction(SgAsmInstruction *) override
Called at the beginning of every instruction.
BaseSemantics::SValuePtr unspecified_(size_t nbits) override
Returns a new undefined value.
BaseSemantics::SValuePtr fpIsNan(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is a special not-a-number bit pattern.
BaseSemantics::SValuePtr undefined_(size_t nbits) override
Returns a new undefined value.
bool onlyInstructions() const
Property: Show only operations for instructions.
BaseSemantics::SValuePtr readMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt, const BaseSemantics::SValuePtr &cond) override
Reads a value from memory.
BaseSemantics::SValuePtr rotateRight(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Rotate bits to the right.
BaseSemantics::SValuePtr fpDivide(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Divide one floating-point value by another.
BaseSemantics::SValuePtr add(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Adds two integers of equal size.
BaseSemantics::SValuePtr fpFromInteger(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Construct a floating-point value from an integer value.
BaseSemantics::SValuePtr protoval() const override
Property: Prototypical semantic value.
BaseSemantics::SValuePtr rotateLeft(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Rotate bits to the left.
void print(std::ostream &, BaseSemantics::Formatter &) const override
Print multi-line output for this object.
SgAsmInstruction * currentInstruction() const override
Property: Current instruction.
static RiscOperatorsPtr instance(const BaseSemantics::RiscOperatorsPtr &subdomain)
Instantiate a new RiscOperators object.
BaseSemantics::SValuePtr boolean_(bool value) override
Returns a Boolean value.
void solver(const SmtSolverPtr &) override
Property: Satisfiability module theory (SMT) solver.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &=SmtSolverPtr()) const override
Virtual allocating constructor.
BaseSemantics::SValuePtr filterIndirectJumpTarget(const BaseSemantics::SValuePtr &) override
Invoked to filter indirect jumps.
BaseSemantics::SValuePtr peekOperand(size_t depth=0) override
Peeks at a value from the Frame Operand Stack.
const BaseSemantics::RiscOperatorsPtr & subdomain() const
Property: Subdomain to which operations are forwarded.
void pushOperand(const BaseSemantics::SValue::Ptr &a) override
Pushes a value to the Frame Operand Stack.
BaseSemantics::SValuePtr mostSignificantSetBit(const BaseSemantics::SValuePtr &) override
Returns position of most significant set bit; zero when no bits are set.
void checkSubdomain() const
Check that we have a valid subdomain.
BaseSemantics::SValuePtr equalToZero(const BaseSemantics::SValuePtr &) override
Determines whether a value is equal to zero.
void writeRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &) override
Writes a value to a register.
BaseSemantics::SValuePtr shiftLeft(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Returns arg shifted left.
void nInsns(size_t n) override
Property: Number of instructions processed.
BaseSemantics::SValuePtr peekMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt) override
Read memory without side effects.
void hlt() override
Invoked for the x86 HLT instruction.
size_t nInsns() const override
Property: Number of instructions processed.
BaseSemantics::SValuePtr leastSignificantSetBit(const BaseSemantics::SValuePtr &) override
Returns position of least significant set bit; zero when no bits are set.
BaseSemantics::SValuePtr fpIsDenormalized(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is denormalized.
SmtSolverPtr solver() const override
Property: Satisfiability module theory (SMT) solver.
BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) override
Returns a number of the specified bit width.
BaseSemantics::StatePtr currentState() const override
Property: Current semantic state.
BaseSemantics::SValuePtr signedMultiply(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Multiplies two signed values.
BaseSemantics::SValuePtr popOperand() override
Pops a value from the Frame Operand Stack.
BaseSemantics::SValuePtr rdtsc() override
Invoked for the x86 RDTSC instruction.
void onlyInstructions(bool)
Property: Show only operations for instructions.
void indentation(const std::string &)
Property: Line prefix string.
BaseSemantics::SValuePtr peekRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &dflt) override
Obtain a register value without side effects.
void comment(const std::string &) override
Inject a line comment into debugging streams.
BaseSemantics::SValuePtr shiftRight(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Returns arg shifted right logically (no sign bit).
void showingInstructionVa(bool)
Property: Show instruction in output.
void stream(Sawyer::Message::Stream &)
Property: output stream to which tracing is emitted.
BaseSemantics::SValuePtr and_(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Computes bit-wise AND of two values.
void cpuid() override
Invoked for the x86 CPUID instruction.
BaseSemantics::SValuePtr concat(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Concatenates the bits of two values.
BaseSemantics::SValuePtr invert(const BaseSemantics::SValuePtr &) override
One's complement.
Describes (part of) a physical CPU register.
Converts text to messages.
Definition Message.h:1396
Floating point types.
Base class for machine instructions.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
boost::shared_ptr< void > MemoryStatePtr
Shared-ownership pointer to trace-semantics memory state.
boost::shared_ptr< void > SValuePtr
Shared-ownership pointer to trace-semantics values.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to trace-semantics RISC operations.
boost::shared_ptr< void > RegisterStatePtr
Shared-ownership pointer to trace-semantics register state.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.