ROSE 0.11.145.147
Semantics.h
1#ifndef ROSE_BinaryAnalysis_Partitioner2_Semantics_H
2#define ROSE_BinaryAnalysis_Partitioner2_Semantics_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/MemoryMap.h>
7#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
8#include <Rose/BinaryAnalysis/InstructionSemantics/SymbolicSemantics.h>
9#include <Rose/BinaryAnalysis/SymbolicExpression.h>
10
11#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
12#include <boost/serialization/access.hpp>
13#endif
14
15namespace Rose {
16namespace BinaryAnalysis {
17namespace Partitioner2 {
18
25namespace Semantics {
26
29
32
35
38
41
44
46// Memory State
48
55template<class Super = InstructionSemantics::SymbolicSemantics::MemoryListState> // or MemoryMapState
56class MemoryState: public Super {
57public:
59 typedef boost::shared_ptr<MemoryState> Ptr;
60
61private:
62 MemoryMapPtr map_;
63 std::vector<SValuePtr> addressesRead_;
64 bool enabled_;
65
66#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
67private:
68 friend class boost::serialization::access;
69 template<class S> void serialize(S&, unsigned version);
70#endif
71
72protected:
73 MemoryState() // for serialization
74 : enabled_(true) {}
75
77 : Super(protocell), enabled_(true) {}
78
79 MemoryState(const InstructionSemantics::BaseSemantics::SValuePtr &addrProtoval,
81 : Super(addrProtoval, valProtoval), enabled_(true) {}
82
83public:
86 return Ptr(new MemoryState(protocell));
87 }
88
92 return Ptr(new MemoryState(addrProtoval, valProtoval));
93 }
94
96 static Ptr instance(const Ptr &other) {
97 return Ptr(new MemoryState(*other));
98 }
99
100public:
104 const InstructionSemantics::BaseSemantics::SValuePtr &valProtoval) const override {
105 return instance(addrProtoval, valProtoval);
106 }
107
111 return instance(protocell);
112 }
113
116 clone() const override {
117 return Ptr(new MemoryState(*this));
118 }
119
120public:
123 static Ptr
125 Ptr retval = boost::dynamic_pointer_cast<MemoryState>(x);
126 assert(x!=NULL);
127 return retval;
128 }
129
130public:
137 bool enabled() const { return enabled_; }
138 void enabled(bool b) { enabled_ = b; }
148 MemoryMapPtr memoryMap() const { return map_; }
149 void memoryMap(const MemoryMapPtr &map) { map_ = map; }
155 const std::vector<SValuePtr>& addressesRead() const { return addressesRead_; }
156 std::vector<SValuePtr>& addressesRead() { return addressesRead_; }
159public:
165
166 virtual void
171
177
178private:
180 readOrPeekMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
184 bool withSideEffects);
185
186public:
187 void print(std::ostream&, InstructionSemantics::BaseSemantics::Formatter&) const override;
188};
189
192
195
197typedef boost::shared_ptr<MemoryListState> MemoryListStatePtr;
198
200typedef boost::shared_ptr<MemoryMapState> MemoryMapStatePtr;
201
203// RISC Operators
205
207typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
208
214public:
216 using Ptr = RiscOperatorsPtr;
217
218private:
219 static const size_t TRIM_THRESHOLD_DFLT = 100;
220
222 // Serialization
223#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
224private:
225 friend class boost::serialization::access;
226 template<class S> void serialize(S&, unsigned version);
227#endif
228
230 // Real constructors
231protected:
232 RiscOperators(); // for serialization
233
235
237
238public:
240
242 // Static allocating constructors
243public:
267 // Virtual constructors
268public:
274
277 const SmtSolverPtr &solver = SmtSolverPtr()) const override {
278 return instance(state, solver);
279 }
280
282 // Dynamic pointer casts
283public:
287
289 // Override methods from base class.
290public:
291 virtual void startInstruction(SgAsmInstruction*) override;
292};
293
295// Memory State
297
298template<class Super>
304 return readOrPeekMemory(addr, dflt, addrOps, valOps, true/*with side effects*/);
305}
306
307template<class Super>
313 return readOrPeekMemory(addr, dflt, addrOps, valOps, false/*no side effects*/);
314}
315
316template<class Super>
322 bool withSideEffects) {
323 using namespace InstructionSemantics;
324
325 if (!enabled_)
326 return dflt->copy();
327
328 addressesRead_.push_back(SValue::promote(addr));
329 if (map_ && addr->toUnsigned()) {
330 ASSERT_require2(8==dflt->nBits(), "multi-byte reads should have been handled above this call");
331 rose_addr_t va = addr->toUnsigned().get();
332 bool isModifiable = map_->at(va).require(MemoryMap::WRITABLE).exists();
333 bool isInitialized = map_->at(va).require(MemoryMap::INITIALIZED).exists();
334 if (!isModifiable || isInitialized) {
335 uint8_t byte;
336 if (1 == map_->at(va).limit(1).read(&byte).size()) {
338 if (isModifiable) {
340 expr = SymbolicExpression::makeSet(expr, indet, valOps->solver());
341 }
342 SymbolicSemantics::SValuePtr val = SymbolicSemantics::SValue::promote(valOps->undefined_(8));
343 val->set_expression(expr);
344 return val;
345 }
346 }
347 }
348
349 if (withSideEffects) {
350 return Super::readMemory(addr, dflt, addrOps, valOps);
351 } else {
352 return Super::peekMemory(addr, dflt, addrOps, valOps);
353 }
354}
355
356template<class Super>
357void
362 if (!enabled_)
363 return;
364 Super::writeMemory(addr, value, addrOps, valOps);
365}
366
367template<class Super>
368void
370 if (map_) {
371 map_->dump(out, fmt.get_line_prefix());
372 } else {
373 out <<fmt.get_line_prefix() <<"no memory map\n";
374 }
375
376 Super::print(out, fmt);
377}
378
379} // namespace
380} // namespace
381} // namespace
382} // namespace
383
384#endif
385#endif
std::string get_line_prefix() const
The string to print at the start of each line.
Definition Formatter.h:60
Base class for most instruction semantics RISC operators.
virtual SValuePtr undefined_(size_t nbits)
Returns a new undefined value.
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
Sawyer::Optional< uint64_t > toUnsigned() const
Converts a concrete value to a native unsigned integer.
Base class for semantics machine states.
Definition State.h:41
Defines RISC operators for the SymbolicSemantics domain.
Type of values manipulated by the SymbolicSemantics domain.
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 promote(const BaseSemantics::SValuePtr &)
Promote a base value to a SymbolicSemantics value.
boost::shared_ptr< MemoryState > Ptr
Shared-ownership pointer to a Semantics::MemoryState.
Definition Semantics.h:59
std::vector< SValuePtr > & addressesRead()
Property: concrete virtual addresses that were read.
Definition Semantics.h:156
const std::vector< SValuePtr > & addressesRead() const
Property: concrete virtual addresses that were read.
Definition Semantics.h:155
virtual void writeMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr, const InstructionSemantics::BaseSemantics::SValuePtr &value, InstructionSemantics::BaseSemantics::RiscOperators *addrOps, InstructionSemantics::BaseSemantics::RiscOperators *valOps) override
Write a byte to memory.
Definition Semantics.h:358
void print(std::ostream &, InstructionSemantics::BaseSemantics::Formatter &) const override
Print a memory state to more than one line of output.
Definition Semantics.h:369
virtual InstructionSemantics::BaseSemantics::MemoryStatePtr create(const InstructionSemantics::BaseSemantics::SValuePtr &addrProtoval, const InstructionSemantics::BaseSemantics::SValuePtr &valProtoval) const override
Virtual constructor.
Definition Semantics.h:103
static Ptr instance(const InstructionSemantics::BaseSemantics::MemoryCellPtr &protocell)
Instantiates a new memory state having specified prototypical cells and value.
Definition Semantics.h:85
void memoryMap(const MemoryMapPtr &map)
The memory map for the specimen.
Definition Semantics.h:149
virtual InstructionSemantics::BaseSemantics::MemoryStatePtr create(const InstructionSemantics::BaseSemantics::MemoryCellPtr &protocell) const override
Virtual constructor.
Definition Semantics.h:110
MemoryMapPtr memoryMap() const
The memory map for the specimen.
Definition Semantics.h:148
virtual InstructionSemantics::BaseSemantics::MemoryStatePtr clone() const override
Virtual copy constructor.
Definition Semantics.h:116
static Ptr instance(const Ptr &other)
Instantiates a new deep copy of an existing state.
Definition Semantics.h:96
virtual InstructionSemantics::BaseSemantics::SValuePtr readMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr, const InstructionSemantics::BaseSemantics::SValuePtr &dflt, InstructionSemantics::BaseSemantics::RiscOperators *addrOps, InstructionSemantics::BaseSemantics::RiscOperators *valOps) override
Read a byte from memory.
Definition Semantics.h:300
static Ptr promote(const InstructionSemantics::BaseSemantics::MemoryStatePtr &x)
Recasts a base pointer to a symbolic memory state.
Definition Semantics.h:124
static Ptr instance(const InstructionSemantics::BaseSemantics::SValuePtr &addrProtoval, const InstructionSemantics::BaseSemantics::SValuePtr &valProtoval)
Instantiates a new memory state having specified prototypical value.
Definition Semantics.h:90
virtual InstructionSemantics::BaseSemantics::SValuePtr peekMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr, const InstructionSemantics::BaseSemantics::SValuePtr &dflt, InstructionSemantics::BaseSemantics::RiscOperators *addrOps, InstructionSemantics::BaseSemantics::RiscOperators *valOps) override
Read a byte from memory with no side effects.
Definition Semantics.h:309
static RiscOperatorsPtr promote(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &)
Run-time promotion of a base RiscOperators pointer to our operators.
static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::StatePtr &, const SmtSolverPtr &)
Instantiate a new RiscOperators with specified state.
static RiscOperatorsPtr instance(const RegisterDictionaryPtr &, const SmtSolverPtr &, SemanticMemoryParadigm memoryParadigm=LIST_BASED_MEMORY)
Instantiate a new RiscOperators object and configure it using default values.
virtual void startInstruction(SgAsmInstruction *) override
Called at the beginning of every instruction.
static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &)
Instantiate a new RiscOperators object with specified prototypical values.
static RiscOperatorsPtr instance(const RegisterDictionaryPtr &)
Instantiate a new RiscOperators object and configure it using default values.
static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::StatePtr &)
Instantiate a new RiscOperators with specified state.
virtual InstructionSemantics::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics::BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
Definition Semantics.h:276
virtual InstructionSemantics::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
Definition Semantics.h:270
static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::SValuePtr &protoval)
Instantiate a new RiscOperators object with specified prototypical values.
const Value & get() const
Dereference to obtain value.
Definition Optional.h:229
Reference-counting intrusive smart pointer.
Base class for machine instructions.
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< MemoryCell > MemoryCellPtr
Shared-ownership pointer to a memory cell.
boost::shared_ptr< class RegisterStateGeneric > RegisterStateGenericPtr
Shared-ownership pointer to generic register states.
InstructionSemantics::SymbolicSemantics::SValue SValue
Semantic value in the partitioner.
Definition Semantics.h:28
MemoryState< InstructionSemantics::SymbolicSemantics::MemoryListState > MemoryListState
Memory state using a chronological list of cells.
Definition Semantics.h:191
MemoryState< InstructionSemantics::SymbolicSemantics::MemoryMapState > MemoryMapState
Memory state indexed by hash of address expressions.
Definition Semantics.h:194
InstructionSemantics::BaseSemantics::RegisterStateGenericPtr RegisterStatePtr
Reference counting pointer to register state.
Definition Semantics.h:37
InstructionSemantics::BaseSemantics::State State
Total state (registers and memory) for the partitioner.
Definition Semantics.h:40
boost::shared_ptr< MemoryListState > MemoryListStatePtr
Shared-ownership pointer to a MemoryListState.
Definition Semantics.h:197
InstructionSemantics::BaseSemantics::RegisterStateGeneric RegisterState
Register state for the partitioner.
Definition Semantics.h:34
boost::shared_ptr< MemoryMapState > MemoryMapStatePtr
Shared-ownership pointer to a MemoryMapState.
Definition Semantics.h:200
InstructionSemantics::BaseSemantics::StatePtr StatePtr
Reference counting pointer to total state.
Definition Semantics.h:43
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to the RISC operators object.
Definition Semantics.h:207
InstructionSemantics::SymbolicSemantics::SValuePtr SValuePtr
Reference counting pointer to semantic value.
Definition Semantics.h:31
LeafPtr makeIntegerConstant(size_t nBits, uint64_t value, const std::string &comment="", unsigned flags=0)
Leaf constructor.
LeafPtr makeIntegerVariable(size_t nBits, const std::string &comment="", unsigned flags=0)
Leaf constructor.
Ptr makeSet(const Ptr &a, const Ptr &b, const SmtSolverPtr &solver=SmtSolverPtr(), const std::string &comment="", unsigned flags=0)
Interior node constructor.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.
ROSE_DLL_API bool isInitialized()
Checks whether the library has been initialized.