ROSE 0.11.145.192
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/As.h>
7#include <Rose/BinaryAnalysis/MemoryMap.h>
8#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
9#include <Rose/BinaryAnalysis/InstructionSemantics/SymbolicSemantics.h>
10#include <Rose/BinaryAnalysis/SymbolicExpression.h>
11
12#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
13#include <boost/serialization/access.hpp>
14#endif
15
16namespace Rose {
17namespace BinaryAnalysis {
18namespace Partitioner2 {
19
26namespace Semantics {
27
30
33
36
39
42
45
47// Memory State
49
56template<class Super = InstructionSemantics::SymbolicSemantics::MemoryListState> // or MemoryMapState
57class MemoryState: public Super {
58public:
60 using Ptr = boost::shared_ptr<MemoryState>;
61
62private:
63 MemoryMapPtr map_;
64 std::vector<SValuePtr> addressesRead_;
65 bool enabled_;
66
67#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
68private:
69 friend class boost::serialization::access;
70 template<class S> void serialize(S&, unsigned version);
71#endif
72
73protected:
74 MemoryState() // for serialization
75 : enabled_(true) {}
76
78 : Super(protocell), enabled_(true) {}
79
80 MemoryState(const InstructionSemantics::BaseSemantics::SValuePtr &addrProtoval,
82 : Super(addrProtoval, valProtoval), enabled_(true) {}
83
84public:
87 return Ptr(new MemoryState(protocell));
88 }
89
93 return Ptr(new MemoryState(addrProtoval, valProtoval));
94 }
95
97 static Ptr instance(const Ptr &other) {
98 return Ptr(new MemoryState(*other));
99 }
100
101public:
105 const InstructionSemantics::BaseSemantics::SValuePtr &valProtoval) const override {
106 return instance(addrProtoval, valProtoval);
107 }
108
112 return instance(protocell);
113 }
114
117 clone() const override {
118 return Ptr(new MemoryState(*this));
119 }
120
121public:
124 static Ptr
126 Ptr retval = as<MemoryState>(x);
127 assert(x!=NULL);
128 return retval;
129 }
130
131public:
138 bool enabled() const { return enabled_; }
139 void enabled(bool b) { enabled_ = b; }
149 MemoryMapPtr memoryMap() const { return map_; }
150 void memoryMap(const MemoryMapPtr &map) { map_ = map; }
156 const std::vector<SValuePtr>& addressesRead() const { return addressesRead_; }
157 std::vector<SValuePtr>& addressesRead() { return addressesRead_; }
160public:
166
167 virtual void
172
178
179private:
181 readOrPeekMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
185 bool withSideEffects);
186
187public:
188 void print(std::ostream&, InstructionSemantics::BaseSemantics::Formatter&) const override;
189};
190
193
196
198using MemoryListStatePtr = boost::shared_ptr<MemoryListState>;
199
201using MemoryMapStatePtr = boost::shared_ptr<MemoryMapState>;
202
204// RISC Operators
206
208using RiscOperatorsPtr = boost::shared_ptr<class RiscOperators>;
209
215public:
217 using Ptr = RiscOperatorsPtr;
218
219private:
220 static const size_t TRIM_THRESHOLD_DFLT = 100;
221
223 // Serialization
224#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
225private:
226 friend class boost::serialization::access;
227 template<class S> void serialize(S&, unsigned version);
228#endif
229
231 // Real constructors
232protected:
233 RiscOperators(); // for serialization
234
236
238
239public:
241
243 // Static allocating constructors
244public:
268 // Virtual constructors
269public:
272 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
273
276 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
277
279 // Dynamic pointer casts
280public:
284
286 // Override methods from base class.
287public:
288 virtual void startInstruction(SgAsmInstruction*) override;
289};
290
292// Memory State
294
295template<class Super>
301 return readOrPeekMemory(addr, dflt, addrOps, valOps, true/*with side effects*/);
302}
303
304template<class Super>
310 return readOrPeekMemory(addr, dflt, addrOps, valOps, false/*no side effects*/);
311}
312
313template<class Super>
319 bool withSideEffects) {
320 using namespace InstructionSemantics;
321
322 if (!enabled_)
323 return dflt->copy();
324
325 addressesRead_.push_back(SValue::promote(addr));
326 if (map_ && addr->toUnsigned()) {
327 ASSERT_require2(8==dflt->nBits(), "multi-byte reads should have been handled above this call");
328 rose_addr_t va = addr->toUnsigned().get();
329 bool isModifiable = map_->at(va).require(MemoryMap::WRITABLE).exists();
330 bool isInitialized = map_->at(va).require(MemoryMap::INITIALIZED).exists();
331 if (!isModifiable || isInitialized) {
332 uint8_t byte;
333 if (1 == map_->at(va).limit(1).read(&byte).size()) {
335 if (isModifiable) {
337 expr = SymbolicExpression::makeSet(expr, indet, valOps->solver());
338 }
339 SymbolicSemantics::SValuePtr val = SymbolicSemantics::SValue::promote(valOps->undefined_(8));
340 val->set_expression(expr);
341 return val;
342 }
343 }
344 }
345
346 if (withSideEffects) {
347 return Super::readMemory(addr, dflt, addrOps, valOps);
348 } else {
349 return Super::peekMemory(addr, dflt, addrOps, valOps);
350 }
351}
352
353template<class Super>
354void
359 if (!enabled_)
360 return;
361 Super::writeMemory(addr, value, addrOps, valOps);
362}
363
364template<class Super>
365void
367 if (map_) {
368 map_->dump(out, fmt.get_line_prefix());
369 } else {
370 out <<fmt.get_line_prefix() <<"no memory map\n";
371 }
372
373 Super::print(out, fmt);
374}
375
376} // namespace
377} // namespace
378} // namespace
379} // namespace
380
381#endif
382#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:42
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.
std::vector< SValuePtr > & addressesRead()
Property: concrete virtual addresses that were read.
Definition Semantics.h:157
const std::vector< SValuePtr > & addressesRead() const
Property: concrete virtual addresses that were read.
Definition Semantics.h:156
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:355
void print(std::ostream &, InstructionSemantics::BaseSemantics::Formatter &) const override
Print an address space.
Definition Semantics.h:366
boost::shared_ptr< MemoryState > Ptr
Shared-ownership pointer to a Semantics::MemoryState.
Definition Semantics.h:60
virtual InstructionSemantics::BaseSemantics::MemoryStatePtr create(const InstructionSemantics::BaseSemantics::SValuePtr &addrProtoval, const InstructionSemantics::BaseSemantics::SValuePtr &valProtoval) const override
Virtual constructor.
Definition Semantics.h:104
static Ptr instance(const InstructionSemantics::BaseSemantics::MemoryCellPtr &protocell)
Instantiates a new memory state having specified prototypical cells and value.
Definition Semantics.h:86
void memoryMap(const MemoryMapPtr &map)
The memory map for the specimen.
Definition Semantics.h:150
virtual InstructionSemantics::BaseSemantics::MemoryStatePtr create(const InstructionSemantics::BaseSemantics::MemoryCellPtr &protocell) const override
Virtual constructor.
Definition Semantics.h:111
MemoryMapPtr memoryMap() const
The memory map for the specimen.
Definition Semantics.h:149
virtual InstructionSemantics::BaseSemantics::AddressSpacePtr clone() const override
Virtual copy constructor.
Definition Semantics.h:117
static Ptr instance(const Ptr &other)
Instantiates a new deep copy of an existing state.
Definition Semantics.h:97
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:297
static Ptr promote(const InstructionSemantics::BaseSemantics::MemoryStatePtr &x)
Recasts a base pointer to a symbolic memory state.
Definition Semantics.h:125
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:91
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:306
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.
virtual InstructionSemantics::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
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.
boost::shared_ptr< AddressSpace > AddressSpacePtr
Shared-ownership pointer for AddressSpace objects.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer for symbolic semantic value.
InstructionSemantics::BaseSemantics::RegisterStateGenericPtr RegisterStatePtr
Reference counting pointer to register state.
Definition Semantics.h:38
InstructionSemantics::BaseSemantics::StatePtr StatePtr
Reference counting pointer to total state.
Definition Semantics.h:44
boost::shared_ptr< MemoryListState > MemoryListStatePtr
Shared-ownership pointer to a MemoryListState.
Definition Semantics.h:198
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to the RISC operators object.
Definition Semantics.h:208
boost::shared_ptr< MemoryMapState > MemoryMapStatePtr
Shared-ownership pointer to a MemoryMapState.
Definition Semantics.h:201
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.