ROSE 0.11.145.147
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/BinaryAnalysis/InstructionSemantics/Utility.h>
8#include <Rose/Diagnostics.h>
9
10namespace Rose {
11namespace BinaryAnalysis {
12namespace InstructionSemantics {
13
53namespace TraceSemantics {
54
55
57// Semantic values
59
60typedef void SValue;
61
63typedef boost::shared_ptr<void> SValuePtr;
64
65
67// Register state
69
70typedef void RegisterState;
71
73typedef boost::shared_ptr<void> RegisterStatePtr;
74
75
77// Memory state
79
80typedef void MemoryState;
81
83typedef boost::shared_ptr<void> MemoryStatePtr;
84
85
87// RISC operators
89
91typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
92
95public:
98
101
102private:
103 BaseSemantics::RiscOperatorsPtr subdomain_; // Domain to which all our RISC operators chain
104 Sawyer::Message::Stream stream_; // stream to which output is emitted
105 std::string indentation_; // string to print at start of each line
106 bool showingSubdomain_ = true; // show subdomain name and address on each line of output?
107 bool showingInstructionVa_ = true; // show instruction VA on each line of output?
108 bool onlyInstructions_ = true; // trace only operations associated with an instruction.
109
111 // Real constructors.
112protected:
113 // use the version that takes a subdomain instead of this c'tor
115 : BaseSemantics::RiscOperators(protoval, solver), stream_(mlog[Diagnostics::INFO]) {
116 name("Trace");
117 }
118
119 // use the version that takes a subdomain instead of this c'tor.
120 explicit RiscOperators(const BaseSemantics::StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr())
121 : BaseSemantics::RiscOperators(state, solver), stream_(mlog[Diagnostics::INFO]) {
122 name("Trace");
123 }
124
125 explicit RiscOperators(const BaseSemantics::RiscOperatorsPtr &subdomain)
126 : BaseSemantics::RiscOperators(subdomain->currentState(), subdomain->solver()),
127 subdomain_(subdomain), stream_(mlog[Diagnostics::INFO]) {
128 name("Trace");
129 }
130
131public:
132 virtual ~RiscOperators() {
133 linePrefix();
134 stream_ <<"operators destroyed\n";
135 }
136
138 // Static allocating constructors.
139public:
146
153
157 ASSERT_not_null(subdomain);
158 RiscOperatorsPtr self = subdomain->currentState()!=NULL ?
159 RiscOperatorsPtr(new RiscOperators(subdomain->currentState(), subdomain->solver())) :
160 RiscOperatorsPtr(new RiscOperators(subdomain->protoval(), subdomain->solver()));
161 self->subdomain_ = subdomain;
162 return self;
163 }
164
166 // Virtual constructors
167public:
172
174 const SmtSolverPtr &solver = SmtSolverPtr()) const override {
175 return instance(state, solver);
176 }
177
182
184 // Dynamic pointer casts
185public:
189 RiscOperatorsPtr retval = boost::dynamic_pointer_cast<RiscOperators>(x);
190 ASSERT_not_null(retval);
191 return retval;
192 }
193
195 // Methods first defined at this level of the class hierarchy
196public:
200 const BaseSemantics::RiscOperatorsPtr& subdomain() const { return subdomain_; }
207 void checkSubdomain() const {
208 if (subdomain_==NULL)
209 throw BaseSemantics::Exception("subdomain is not set; nothing to trace", NULL);
210 }
217 Sawyer::Message::Stream& stream() { return stream_; }
218 void stream(Sawyer::Message::Stream &s) { stream_ = s; }
226 const std::string& indentation() const { return indentation_; }
227 void indentation(const std::string &s) { indentation_ = s; }
235 bool showingSubdomain() const { return showingSubdomain_; }
236 void showingSubdomain(bool b) { showingSubdomain_ = b; }
244 bool showingInstructionVa() const { return showingInstructionVa_; }
245 void showingInstructionVa(bool b) { showingInstructionVa_ = b; }
256 bool onlyInstructions() const { return onlyInstructions_; }
257 void onlyInstructions(bool b) { onlyInstructions_ = b; }
260protected:
261 void linePrefix();
262 std::string toString(const BaseSemantics::SValuePtr&);
263 std::string toString(SgAsmFloatType*);
264 void check_equal_widths(const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&);
265 const BaseSemantics::SValuePtr &check_width(const BaseSemantics::SValuePtr &a, size_t nbits,
266 const std::string &what="result");
267 std::string register_name(RegisterDescriptor);
268
269 bool shouldPrint() const;
270 bool shouldPrint(SgAsmInstruction*) const;
271
272 void before(const std::string&);
273 void before(const std::string&, RegisterDescriptor);
274 void before(const std::string&, RegisterDescriptor, const BaseSemantics::SValuePtr&);
275 void before(const std::string&, RegisterDescriptor, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&);
276 void before(const std::string&, RegisterDescriptor, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&,
277 size_t);
278 void before(const std::string&, RegisterDescriptor, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&,
280 void before(const std::string&, SgAsmInstruction*, bool showAddress);
281 void before(const std::string&, size_t);
282 void before(const std::string&, size_t, uint64_t);
283 void before(const std::string&, const BaseSemantics::SValuePtr&);
284 void before(const std::string&, const BaseSemantics::SValuePtr&, size_t);
285 void before(const std::string&, const BaseSemantics::SValuePtr&, size_t, size_t);
286 void before(const std::string&, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&);
287 void before(const std::string&, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&,
289 void before(const std::string&, const BaseSemantics::SValuePtr&, SgAsmFloatType*);
290 void before(const std::string&, const BaseSemantics::SValuePtr&, SgAsmFloatType*, const BaseSemantics::SValuePtr&);
291 void before(const std::string&, const BaseSemantics::SValuePtr&, SgAsmFloatType*, SgAsmFloatType*);
292 void before(const std::string&, const BaseSemantics::SValuePtr&, const BaseSemantics::SValuePtr&, SgAsmFloatType*);
293
294 void after();
295 void after(SgAsmInstruction*);
299 void after(const BaseSemantics::Exception&);
300 void after(const BaseSemantics::Exception&, SgAsmInstruction*);
301 void after_exception();
302 void after_exception(SgAsmInstruction*);
303
305 // Methods we override from our super class
306public:
307 virtual BaseSemantics::SValuePtr protoval() const override;
308 virtual void solver(const SmtSolverPtr&) override;
309 virtual SmtSolverPtr solver() const override;
310 virtual BaseSemantics::StatePtr currentState() const override;
311 virtual void currentState(const BaseSemantics::StatePtr&) override;
312 virtual void print(std::ostream&, BaseSemantics::Formatter&) const override;
313 virtual size_t nInsns() const override;
314 virtual void nInsns(size_t n) override;
315 virtual SgAsmInstruction* currentInstruction() const override;
316 virtual bool isNoopRead() const override;
317 virtual void isNoopRead(bool) override;
318 virtual void startInstruction(SgAsmInstruction*) override;
319 virtual void finishInstruction(SgAsmInstruction*) override;
320 virtual void comment(const std::string&) override;
321
322 virtual BaseSemantics::SValuePtr undefined_(size_t nbits) override;
323 virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) override;
324 virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) override;
325 virtual BaseSemantics::SValuePtr boolean_(bool value) override;
326 virtual BaseSemantics::SValuePtr bottom_(size_t nbits) override;
327
331 virtual void hlt() override;
332 virtual void cpuid() override;
333 virtual BaseSemantics::SValuePtr rdtsc() override;
334
335 // The actual RISC operators. These are pure virtual in the base class
340 virtual BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr&, size_t begin_bit, size_t end_bit) override;
349 const BaseSemantics::SValuePtr&) override;
352 const BaseSemantics::SValuePtr&, IteStatus&) override;
354 virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr&, size_t nbits) override;
358 BaseSemantics::SValuePtr&/*out*/) override;
363 const BaseSemantics::SValuePtr&) override;
365 const BaseSemantics::SValuePtr&) override;
367 const BaseSemantics::SValuePtr&) override;
369 const BaseSemantics::SValuePtr&) override;
370
371 virtual void interrupt(int majr, int minr) override;
372
375 const BaseSemantics::SValuePtr&) override;
384 SgAsmFloatType*) override;
386 SgAsmFloatType*) override;
388 SgAsmFloatType*) override;
390 SgAsmFloatType*) override;
393
395 const BaseSemantics::SValuePtr &dflt) override;
397 const BaseSemantics::SValuePtr &dflt) override;
400 const BaseSemantics::SValuePtr &dflt,
401 const BaseSemantics::SValuePtr &cond) override;
403 const BaseSemantics::SValuePtr &dflt) override;
405 const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override;
406};
407
408} // namespace
409} // namespace
410} // namespace
411} // namespace
412
413#endif
414#endif
Base class for most instruction semantics RISC operators.
virtual const std::string & name() const
Property: Name used for debugging.
virtual BaseSemantics::SValuePtr add(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Adds two integers of equal size.
virtual BaseSemantics::SValuePtr fpIsInfinity(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is infinity.
virtual SgAsmInstruction * currentInstruction() const override
Property: Current instruction.
virtual BaseSemantics::SValuePtr fpAdd(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Add two floating-point values.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
virtual BaseSemantics::SValuePtr protoval() const override
Property: Prototypical semantic value.
void onlyInstructions(bool b)
Property: Show only operations for instructions.
virtual BaseSemantics::SValuePtr unsignedModulo(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Calculates modulo with unsigned values.
virtual BaseSemantics::SValuePtr xor_(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Computes bit-wise XOR of two values.
virtual BaseSemantics::SValuePtr fpSubtract(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Subtract one floating-point value from another.
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.
virtual BaseSemantics::SValuePtr mostSignificantSetBit(const BaseSemantics::SValuePtr &) override
Returns position of most significant set bit; zero when no bits are set.
virtual 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.
virtual BaseSemantics::SValuePtr rdtsc() override
Invoked for the x86 RDTSC instruction.
bool showingInstructionVa() const
Property: Show instruction in output.
virtual BaseSemantics::SValuePtr filterReturnTarget(const BaseSemantics::SValuePtr &) override
Invoked to filter return targets.
virtual void startInstruction(SgAsmInstruction *) override
Called at the beginning of every instruction.
virtual BaseSemantics::SValuePtr filterCallTarget(const BaseSemantics::SValuePtr &) override
Invoked to filter call targets.
virtual BaseSemantics::SValuePtr fpSign(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Sign of floating-point value.
virtual BaseSemantics::SValuePtr undefined_(size_t nbits) override
Returns a new undefined value.
Sawyer::Message::Stream & stream()
Property: output stream to which tracing is emitted.
virtual BaseSemantics::SValuePtr shiftLeft(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Returns arg shifted left.
const std::string & indentation() const
Property: Line prefix string.
virtual BaseSemantics::SValuePtr rotateLeft(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Rotate bits to the left.
virtual BaseSemantics::SValuePtr leastSignificantSetBit(const BaseSemantics::SValuePtr &) override
Returns position of least significant set bit; zero when no bits are set.
virtual BaseSemantics::SValuePtr signedMultiply(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Multiplies two signed values.
virtual BaseSemantics::SValuePtr fpIsZero(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is equal to zero.
virtual BaseSemantics::SValuePtr unsignedDivide(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Divides two unsigned values.
virtual BaseSemantics::SValuePtr shiftRight(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Returns arg shifted right logically (no sign bit).
virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) override
Returns a new undefined value.
virtual BaseSemantics::SValuePtr signedModulo(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Calculates modulo with signed values.
virtual SmtSolverPtr solver() const override
Property: Satisfiability module theory (SMT) solver.
virtual BaseSemantics::SValuePtr peekMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt) override
Read memory without side effects.
virtual void print(std::ostream &, BaseSemantics::Formatter &) const override
Print multi-line output for this object.
virtual BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr &, size_t begin_bit, size_t end_bit) override
Extracts bits from a value.
virtual void cpuid() override
Invoked for the x86 CPUID instruction.
void indentation(const std::string &s)
Property: Line prefix string.
virtual BaseSemantics::SValuePtr fpFromInteger(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Construct a floating-point value from an integer value.
bool showingSubdomain() const
Property: Show subdomain name in output.
virtual size_t nInsns() const override
Property: Number of instructions processed.
virtual BaseSemantics::SValuePtr equalToZero(const BaseSemantics::SValuePtr &) override
Determines whether a value is equal to zero.
bool onlyInstructions() const
Property: Show only operations for instructions.
virtual BaseSemantics::SValuePtr or_(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Computes bit-wise OR of two values.
static RiscOperatorsPtr instance(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators object.
virtual BaseSemantics::SValuePtr boolean_(bool value) override
Returns a Boolean value.
virtual BaseSemantics::SValuePtr fpConvert(const BaseSemantics::SValuePtr &, SgAsmFloatType *, SgAsmFloatType *) override
Convert from one floating-point type to another.
virtual BaseSemantics::SValuePtr concat(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Concatenates the bits of two values.
virtual BaseSemantics::SValuePtr peekRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &dflt) override
Obtain a register value without side effects.
virtual void writeMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override
Writes a value to memory.
static RiscOperatorsPtr instance(const BaseSemantics::RiscOperatorsPtr &subdomain)
Instantiate a new RiscOperators object.
virtual BaseSemantics::SValuePtr bottom_(size_t nbits) override
Returns a data-flow bottom value.
void showingSubdomain(bool b)
Property: Show subdomain name in output.
virtual BaseSemantics::SValuePtr unsignedMultiply(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Multiply two unsigned values.
virtual BaseSemantics::SValuePtr iteWithStatus(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, IteStatus &) override
If-then-else with status.
virtual void solver(const SmtSolverPtr &) override
Property: Satisfiability module theory (SMT) solver.
const BaseSemantics::RiscOperatorsPtr & subdomain() const
Property: Subdomain to which operations are forwarded.
void checkSubdomain() const
Check that we have a valid subdomain.
void stream(Sawyer::Message::Stream &s)
Property: output stream to which tracing is emitted.
virtual bool isNoopRead() const override
Property: No-op read.
virtual BaseSemantics::SValuePtr signedDivide(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Divides two signed values.
virtual BaseSemantics::SValuePtr filterIndirectJumpTarget(const BaseSemantics::SValuePtr &) override
Invoked to filter indirect jumps.
virtual void isNoopRead(bool) override
Property: No-op read.
virtual BaseSemantics::StatePtr currentState() const override
Property: Current semantic state.
virtual BaseSemantics::SValuePtr invert(const BaseSemantics::SValuePtr &) override
One's complement.
virtual BaseSemantics::SValuePtr readRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &dflt) override
Reads a value from a register.
virtual void finishInstruction(SgAsmInstruction *) override
Called at the end of every instruction.
virtual void hlt() override
Invoked for the x86 HLT instruction.
virtual BaseSemantics::SValuePtr fpEffectiveExponent(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Exponent of floating-point value.
virtual BaseSemantics::SValuePtr fpRoundTowardZero(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Round toward zero.
virtual void comment(const std::string &) override
Inject a line comment into debugging streams.
virtual BaseSemantics::SValuePtr and_(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Computes bit-wise AND of two values.
virtual void writeRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &) override
Writes a value to a register.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
static RiscOperatorsPtr instance(const BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators object.
virtual BaseSemantics::SValuePtr shiftRightArithmetic(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Returns arg shifted right arithmetically (with sign bit).
virtual BaseSemantics::SValuePtr fpToInteger(const BaseSemantics::SValuePtr &, SgAsmFloatType *, const BaseSemantics::SValuePtr &) override
Construct an integer value from a floating-point value.
virtual BaseSemantics::SValuePtr negate(const BaseSemantics::SValuePtr &) override
Two's complement.
virtual void nInsns(size_t n) override
Property: Number of instructions processed.
virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) override
Returns a number of the specified bit width.
void showingInstructionVa(bool b)
Property: Show instruction in output.
virtual void currentState(const BaseSemantics::StatePtr &) override
Property: Current semantic state.
virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &, size_t nbits) override
Sign extends a value.
static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &x)
Run-time promotion of a base RiscOperators pointer to trace operators.
virtual BaseSemantics::SValuePtr fpMultiply(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Multiply two floating-point values.
virtual BaseSemantics::SValuePtr fpSquareRoot(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Square root.
virtual BaseSemantics::SValuePtr fpIsNan(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is a special not-a-number bit pattern.
virtual 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.
virtual BaseSemantics::SValuePtr rotateRight(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &) override
Rotate bits to the right.
virtual BaseSemantics::SValuePtr fpIsDenormalized(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is denormalized.
virtual BaseSemantics::SValuePtr fpDivide(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Divide one floating-point value by another.
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 void interrupt(int majr, int minr) override
Unconditionally raise an interrupt.
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< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to trace-semantics RISC operations.
boost::shared_ptr< void > SValuePtr
Shared-ownership pointer to trace-semantics values.
boost::shared_ptr< void > RegisterStatePtr
Shared-ownership pointer to trace-semantics register state.
boost::shared_ptr< void > MemoryStatePtr
Shared-ownership pointer to trace-semantics memory state.
Sawyer::Message::Facility mlog
Diagnostics logging facility for instruction semantics.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.