ROSE  0.9.9.109
Semantics.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner_Semantics_H
2 #define ROSE_BinaryAnalysis_Partitioner_Semantics_H
3 
4 #include <Partitioner2/BasicTypes.h>
5 #include "SymbolicSemantics2.h"
6 
7 #include <boost/serialization/access.hpp>
8 #include <boost/serialization/base_object.hpp>
9 #include <boost/serialization/export.hpp>
10 #include <boost/serialization/vector.hpp>
11 
12 namespace Rose {
13 namespace BinaryAnalysis {
14 namespace Partitioner2 {
15 namespace Semantics {
16 
17 typedef InstructionSemantics2::SymbolicSemantics::SValue SValue;
19 
20 typedef InstructionSemantics2::BaseSemantics::RegisterStateGeneric RegisterState;
22 
23 typedef InstructionSemantics2::BaseSemantics::State State;
25 
27 // Memory State
29 
36 template<class Super = InstructionSemantics2::SymbolicSemantics::MemoryListState> // or MemoryMapState
37 class MemoryState: public Super {
38 public:
40  typedef boost::shared_ptr<MemoryState> Ptr;
41 
42 private:
43  MemoryMap::Ptr map_;
44  std::vector<SValuePtr> addressesRead_;
45  bool enabled_;
46 
47 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
48 private:
49  friend class boost::serialization::access;
50 
51  template<class S>
52  void serialize(S &s, const unsigned version) {
53  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
54  s & BOOST_SERIALIZATION_NVP(map_);
55  s & BOOST_SERIALIZATION_NVP(addressesRead_);
56  s & BOOST_SERIALIZATION_NVP(enabled_);
57  }
58 #endif
59 
60 protected:
61  MemoryState() // for serialization
62  : enabled_(true) {}
63 
64  explicit MemoryState(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell)
65  : Super(protocell), enabled_(true) {}
66 
67  MemoryState(const InstructionSemantics2::BaseSemantics::SValuePtr &addrProtoval,
69  : Super(addrProtoval, valProtoval), enabled_(true) {}
70 
71 public:
74  return Ptr(new MemoryState(protocell));
75  }
76 
80  return Ptr(new MemoryState(addrProtoval, valProtoval));
81  }
82 
84  static Ptr instance(const Ptr &other) {
85  return Ptr(new MemoryState(*other));
86  }
87 
88 public:
92  const InstructionSemantics2::BaseSemantics::SValuePtr &valProtoval) const ROSE_OVERRIDE {
93  return instance(addrProtoval, valProtoval);
94  }
95 
98  create(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell) const ROSE_OVERRIDE {
99  return instance(protocell);
100  }
101 
104  clone() const ROSE_OVERRIDE {
105  return Ptr(new MemoryState(*this));
106  }
107 
108 public:
111  static Ptr
113  Ptr retval = boost::dynamic_pointer_cast<MemoryState>(x);
114  assert(x!=NULL);
115  return retval;
116  }
117 
118 public:
125  bool enabled() const { return enabled_; }
126  void enabled(bool b) { enabled_ = b; }
136  MemoryMap::Ptr memoryMap() const { return map_; }
137  void memoryMap(const MemoryMap::Ptr &map) { map_ = map; }
143  const std::vector<SValuePtr>& addressesRead() const { return addressesRead_; }
144  std::vector<SValuePtr>& addressesRead() { return addressesRead_; }
147 public:
153 
154  virtual void
155  writeMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
159 };
160 
161 typedef MemoryState<InstructionSemantics2::SymbolicSemantics::MemoryListState> MemoryListState;
162 typedef MemoryState<InstructionSemantics2::SymbolicSemantics::MemoryMapState> MemoryMapState;
163 
165 typedef boost::shared_ptr<MemoryListState> MemoryListStatePtr;
166 
168 typedef boost::shared_ptr<MemoryMapState> MemoryMapStatePtr;
169 
171 // RISC Operators
173 
175 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
176 
182 public:
184 
185 private:
186  static const size_t TRIM_THRESHOLD_DFLT = 100;
187 
189  // Serialization
190 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
191 private:
192  friend class boost::serialization::access;
193 
194  template<class S>
195  void serialize(S &s, const unsigned version) {
196  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
197  }
198 #endif
199 
201  // Real constructors
202 protected:
203  RiscOperators() {} // for serialization
204 
207  name("PartitionerSemantics");
208  (void)SValue::promote(protoval); // make sure its dynamic type is appropriate
209  trimThreshold(TRIM_THRESHOLD_DFLT);
210  }
211 
214  name("PartitionerSemantics");
215  (void)SValue::promote(state->protoval());
216  trimThreshold(TRIM_THRESHOLD_DFLT);
217  }
218 
220  // Static allocating constructors
221 public:
223  static RiscOperatorsPtr instance(const RegisterDictionary *regdict, SMTSolver *solver=NULL,
224  SemanticMemoryParadigm memoryParadigm = LIST_BASED_MEMORY) {
228  switch (memoryParadigm) {
229  case LIST_BASED_MEMORY:
230  memory = MemoryListState::instance(protoval, protoval);
231  break;
232  case MAP_BASED_MEMORY:
233  memory = MemoryMapState::instance(protoval, protoval);
234  break;
235  }
237  return RiscOperatorsPtr(new RiscOperators(state, solver));
238  }
239 
241  static RiscOperatorsPtr
243  return RiscOperatorsPtr(new RiscOperators(protoval, solver));
244  }
245 
247  static RiscOperatorsPtr
249  return RiscOperatorsPtr(new RiscOperators(state, solver));
250  }
251 
253  // Virtual constructors
254 public:
257  SMTSolver *solver=NULL) const ROSE_OVERRIDE {
258  return instance(protoval, solver);
259  }
260 
263  SMTSolver *solver=NULL) const ROSE_OVERRIDE {
264  return instance(state, solver);
265  }
266 
268  // Dynamic pointer casts
269 public:
272  static RiscOperatorsPtr
274  RiscOperatorsPtr retval = boost::dynamic_pointer_cast<RiscOperators>(x);
275  assert(retval!=NULL);
276  return retval;
277  }
278 
280  // Override methods from base class.
281 public:
282  virtual void startInstruction(SgAsmInstruction*) ROSE_OVERRIDE;
283 };
284 
286 // Memory State
288 
289 template<class Super>
290 InstructionSemantics2::BaseSemantics::SValuePtr
291 MemoryState<Super>::readMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
292  const InstructionSemantics2::BaseSemantics::SValuePtr &dflt,
293  InstructionSemantics2::BaseSemantics::RiscOperators *addrOps,
294  InstructionSemantics2::BaseSemantics::RiscOperators *valOps) {
295  using namespace InstructionSemantics2;
296 
297  if (!enabled_)
298  return dflt->copy();
299 
300  addressesRead_.push_back(SValue::promote(addr));
301  if (map_ && addr->is_number()) {
302  ASSERT_require2(8==dflt->get_width(), "multi-byte reads should have been handled above this call");
303  rose_addr_t va = addr->get_number();
304  bool isModifiable = map_->at(va).require(MemoryMap::WRITABLE).exists();
305  bool isInitialized = map_->at(va).require(MemoryMap::INITIALIZED).exists();
306  if (!isModifiable || isInitialized) {
307  uint8_t byte;
308  if (1 == map_->at(va).limit(1).read(&byte).size()) {
310  if (isModifiable) {
312  expr = SymbolicExpr::makeSet(expr, indet);
313  }
314  SymbolicSemantics::SValuePtr val = SymbolicSemantics::SValue::promote(valOps->undefined_(8));
315  val->set_expression(expr);
316  return val;
317  }
318  }
319  }
320  return Super::readMemory(addr, dflt, addrOps, valOps);
321 }
322 
323 template<class Super>
324 void
325 MemoryState<Super>::writeMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
327  InstructionSemantics2::BaseSemantics::RiscOperators *addrOps,
328  InstructionSemantics2::BaseSemantics::RiscOperators *valOps) {
329  if (!enabled_)
330  return;
331  Super::writeMemory(addr, value, addrOps, valOps);
332 }
333 
334 } // namespace
335 } // namespace
336 } // namespace
337 } // namespace
338 
339 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
343 #endif
344 
345 #endif
virtual void writeMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) ROSE_OVERRIDE
Writes a value to memory.
static RegisterStateGenericPtr instance(const SValuePtr &protoval, const RegisterDictionary *regdict)
Instantiate a new register state.
boost::shared_ptr< class MemoryCell > MemoryCellPtr
Shared-ownership pointer to a semantic memory cell.
Definition: MemoryCell.h:15
void memoryMap(const MemoryMap::Ptr &map)
The memory map for the specimen.
Definition: Semantics.h:137
static RiscOperatorsPtr promote(const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &x)
Run-time promotion of a base RiscOperators pointer to our operators.
Definition: Semantics.h:273
Ptr makeInteger(size_t nbits, uint64_t n, const std::string &comment="", unsigned flags=0)
Leaf constructor.
Defines RISC operators for the SymbolicSemantics domain.
boost::shared_ptr< class RegisterStateGeneric > RegisterStateGenericPtr
Shared-ownership pointer to generic register states.
std::vector< SValuePtr > & addressesRead()
Property: concrete virtual addresses that were read.
Definition: Semantics.h:144
static Ptr promote(const InstructionSemantics2::BaseSemantics::MemoryStatePtr &x)
Recasts a base pointer to a symbolic memory state.
Definition: Semantics.h:112
Base class for machine instructions.
Ptr makeVariable(size_t nbits, const std::string &comment="", unsigned flags=0)
Leaf constructor.
static SValuePtr promote(const BaseSemantics::SValuePtr &v)
Promote a base value to a SymbolicSemantics value.
virtual InstructionSemantics2::BaseSemantics::MemoryStatePtr create(const InstructionSemantics2::BaseSemantics::SValuePtr &addrProtoval, const InstructionSemantics2::BaseSemantics::SValuePtr &valProtoval) const ROSE_OVERRIDE
Virtual constructor.
Definition: Semantics.h:91
const std::vector< SValuePtr > & addressesRead() const
Property: concrete virtual addresses that were read.
Definition: Semantics.h:143
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
static RiscOperatorsPtr instance(const RegisterDictionary *regdict, SMTSolver *solver=NULL, SemanticMemoryParadigm memoryParadigm=LIST_BASED_MEMORY)
Instantiate a new RiscOperators object and configure it using default values.
Definition: Semantics.h:223
static Ptr instance(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell)
Instantiates a new memory state having specified prototypical cells and value.
Definition: Semantics.h:73
Main namespace for the ROSE library.
Ptr makeSet(const Ptr &a, const Ptr &b, const std::string &comment="", unsigned flags=0)
Interior node constructor.
virtual InstructionSemantics2::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics2::BaseSemantics::SValuePtr &protoval, SMTSolver *solver=NULL) const ROSE_OVERRIDE
Virtual allocating constructor.
Definition: Semantics.h:256
boost::shared_ptr< class RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
boost::shared_ptr< class State > StatePtr
Shared-ownership pointer to a semantic state.
MemoryMap::Ptr memoryMap() const
The memory map for the specimen.
Definition: Semantics.h:136
virtual void startInstruction(SgAsmInstruction *) ROSE_OVERRIDE
Called at the beginning of every instruction.
Base classes for instruction semantics.
static Ptr instance(const Ptr &other)
Instantiates a new deep copy of an existing state.
Definition: Semantics.h:84
virtual InstructionSemantics2::BaseSemantics::MemoryStatePtr clone() const ROSE_OVERRIDE
Virtual copy constructor.
Definition: Semantics.h:104
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
static SValuePtr instance()
Instantiate a new prototypical value.
boost::shared_ptr< MemoryState > Ptr
Shared-ownership pointer to a MemoryState.
Definition: Semantics.h:40
SemanticMemoryParadigm
Organization of semantic memory.
Definition: BasicTypes.h:60
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer for symbolic semantic value.
Defines registers available for a particular architecture.
Definition: Registers.h:32
ROSE_DLL_API bool isInitialized()
Checks whether the library has been initialized.
static RiscOperatorsPtr instance(const InstructionSemantics2::BaseSemantics::SValuePtr &protoval, SMTSolver *solver=NULL)
Instantiate a new RiscOperators object with specified prototypical values.
Definition: Semantics.h:242
Base class for most instruction semantics RISC operators.
static RiscOperatorsPtr instance(const InstructionSemantics2::BaseSemantics::StatePtr &state, SMTSolver *solver=NULL)
Instantiate a new RiscOperators with specified state.
Definition: Semantics.h:248
virtual InstructionSemantics2::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics2::BaseSemantics::StatePtr &state, SMTSolver *solver=NULL) const ROSE_OVERRIDE
Virtual allocating constructor.
Definition: Semantics.h:262
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
size_t trimThreshold() const
Property: Maximum size of expressions.
virtual InstructionSemantics2::BaseSemantics::MemoryStatePtr create(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell) const ROSE_OVERRIDE
Virtual constructor.
Definition: Semantics.h:98
boost::shared_ptr< class MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
static Ptr instance(const InstructionSemantics2::BaseSemantics::SValuePtr &addrProtoval, const InstructionSemantics2::BaseSemantics::SValuePtr &valProtoval)
Instantiates a new memory state having specified prototypical value.
Definition: Semantics.h:78
virtual SMTSolver * solver() const
Property: Satisfiability module theory (SMT) solver.
virtual const std::string & name() const
Property: Name used for debugging.
virtual BaseSemantics::SValuePtr readMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt, const BaseSemantics::SValuePtr &cond) ROSE_OVERRIDE
Reads a value from memory.
Interface to Satisfiability Modulo Theory (SMT) solvers.
Definition: SMTSolver.h:19