ROSE  0.9.11.10
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 
22 namespace Semantics {
23 
26 
29 
32 
35 
38 
41 
43 // Memory State
45 
52 template<class Super = InstructionSemantics2::SymbolicSemantics::MemoryListState> // or MemoryMapState
53 class MemoryState: public Super {
54 public:
56  typedef boost::shared_ptr<MemoryState> Ptr;
57 
58 private:
59  MemoryMap::Ptr map_;
60  std::vector<SValuePtr> addressesRead_;
61  bool enabled_;
62 
63 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
64 private:
65  friend class boost::serialization::access;
66 
67  template<class S>
68  void serialize(S &s, const unsigned /*version*/) {
69  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
70  s & BOOST_SERIALIZATION_NVP(map_);
71  s & BOOST_SERIALIZATION_NVP(addressesRead_);
72  s & BOOST_SERIALIZATION_NVP(enabled_);
73  }
74 #endif
75 
76 protected:
77  MemoryState() // for serialization
78  : enabled_(true) {}
79 
80  explicit MemoryState(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell)
81  : Super(protocell), enabled_(true) {}
82 
83  MemoryState(const InstructionSemantics2::BaseSemantics::SValuePtr &addrProtoval,
85  : Super(addrProtoval, valProtoval), enabled_(true) {}
86 
87 public:
90  return Ptr(new MemoryState(protocell));
91  }
92 
96  return Ptr(new MemoryState(addrProtoval, valProtoval));
97  }
98 
100  static Ptr instance(const Ptr &other) {
101  return Ptr(new MemoryState(*other));
102  }
103 
104 public:
108  const InstructionSemantics2::BaseSemantics::SValuePtr &valProtoval) const ROSE_OVERRIDE {
109  return instance(addrProtoval, valProtoval);
110  }
111 
114  create(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell) const ROSE_OVERRIDE {
115  return instance(protocell);
116  }
117 
120  clone() const ROSE_OVERRIDE {
121  return Ptr(new MemoryState(*this));
122  }
123 
124 public:
127  static Ptr
129  Ptr retval = boost::dynamic_pointer_cast<MemoryState>(x);
130  assert(x!=NULL);
131  return retval;
132  }
133 
134 public:
141  bool enabled() const { return enabled_; }
142  void enabled(bool b) { enabled_ = b; }
152  MemoryMap::Ptr memoryMap() const { return map_; }
153  void memoryMap(const MemoryMap::Ptr &map) { map_ = map; }
159  const std::vector<SValuePtr>& addressesRead() const { return addressesRead_; }
160  std::vector<SValuePtr>& addressesRead() { return addressesRead_; }
163 public:
169 
170  virtual void
171  writeMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
175 
181 
182 private:
184  readOrPeekMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
188  bool withSideEffects);
189 
190 public:
191  void print(std::ostream&, InstructionSemantics2::BaseSemantics::Formatter&) const ROSE_OVERRIDE;
192 };
193 
195 typedef MemoryState<InstructionSemantics2::SymbolicSemantics::MemoryListState> MemoryListState;
196 
198 typedef MemoryState<InstructionSemantics2::SymbolicSemantics::MemoryMapState> MemoryMapState;
199 
201 typedef boost::shared_ptr<MemoryListState> MemoryListStatePtr;
202 
204 typedef boost::shared_ptr<MemoryMapState> MemoryMapStatePtr;
205 
207 // RISC Operators
209 
211 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
212 
217 class RiscOperators: public InstructionSemantics2::SymbolicSemantics::RiscOperators {
218 public:
220 
221 private:
222  static const size_t TRIM_THRESHOLD_DFLT = 100;
223 
225  // Serialization
226 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
227 private:
228  friend class boost::serialization::access;
229 
230  template<class S>
231  void serialize(S &s, const unsigned /*version*/) {
232  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
233  }
234 #endif
235 
237  // Real constructors
238 protected:
239  RiscOperators() {} // for serialization
240 
241  explicit RiscOperators(const InstructionSemantics2::BaseSemantics::SValuePtr &protoval,
242  const SmtSolverPtr &solver = SmtSolverPtr())
244  name("PartitionerSemantics");
245  (void)SValue::promote(protoval); // make sure its dynamic type is appropriate
246  trimThreshold(TRIM_THRESHOLD_DFLT);
247  }
248 
249  explicit RiscOperators(const InstructionSemantics2::BaseSemantics::StatePtr &state,
250  const SmtSolverPtr &solver = SmtSolverPtr())
252  name("PartitionerSemantics");
253  (void)SValue::promote(state->protoval());
254  trimThreshold(TRIM_THRESHOLD_DFLT);
255  }
256 
258  // Static allocating constructors
259 public:
261  static RiscOperatorsPtr instance(const RegisterDictionary *regdict, const SmtSolverPtr &solver = SmtSolverPtr(),
262  SemanticMemoryParadigm memoryParadigm = LIST_BASED_MEMORY) {
266  switch (memoryParadigm) {
267  case LIST_BASED_MEMORY:
268  memory = MemoryListState::instance(protoval, protoval);
269  break;
270  case MAP_BASED_MEMORY:
271  memory = MemoryMapState::instance(protoval, protoval);
272  break;
273  }
275  return RiscOperatorsPtr(new RiscOperators(state, solver));
276  }
277 
279  static RiscOperatorsPtr
281  return RiscOperatorsPtr(new RiscOperators(protoval, solver));
282  }
283 
285  static RiscOperatorsPtr
287  return RiscOperatorsPtr(new RiscOperators(state, solver));
288  }
289 
291  // Virtual constructors
292 public:
295  const SmtSolverPtr &solver = SmtSolverPtr()) const ROSE_OVERRIDE {
296  return instance(protoval, solver);
297  }
298 
301  const SmtSolverPtr &solver = SmtSolverPtr()) const ROSE_OVERRIDE {
302  return instance(state, solver);
303  }
304 
306  // Dynamic pointer casts
307 public:
310  static RiscOperatorsPtr
312  RiscOperatorsPtr retval = boost::dynamic_pointer_cast<RiscOperators>(x);
313  assert(retval!=NULL);
314  return retval;
315  }
316 
318  // Override methods from base class.
319 public:
320  virtual void startInstruction(SgAsmInstruction*) ROSE_OVERRIDE;
321 };
322 
324 // Memory State
326 
327 template<class Super>
328 InstructionSemantics2::BaseSemantics::SValuePtr
329 MemoryState<Super>::readMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
330  const InstructionSemantics2::BaseSemantics::SValuePtr &dflt,
331  InstructionSemantics2::BaseSemantics::RiscOperators *addrOps,
332  InstructionSemantics2::BaseSemantics::RiscOperators *valOps) {
333  return readOrPeekMemory(addr, dflt, addrOps, valOps, true/*with side effects*/);
334 }
335 
336 template<class Super>
338 MemoryState<Super>::peekMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
342  return readOrPeekMemory(addr, dflt, addrOps, valOps, false/*no side effects*/);
343 }
344 
345 template<class Super>
347 MemoryState<Super>::readOrPeekMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
351  bool withSideEffects) {
352  using namespace InstructionSemantics2;
353 
354  if (!enabled_)
355  return dflt->copy();
356 
357  addressesRead_.push_back(SValue::promote(addr));
358  if (map_ && addr->is_number()) {
359  ASSERT_require2(8==dflt->get_width(), "multi-byte reads should have been handled above this call");
360  rose_addr_t va = addr->get_number();
361  bool isModifiable = map_->at(va).require(MemoryMap::WRITABLE).exists();
362  bool isInitialized = map_->at(va).require(MemoryMap::INITIALIZED).exists();
363  if (!isModifiable || isInitialized) {
364  uint8_t byte;
365  if (1 == map_->at(va).limit(1).read(&byte).size()) {
367  if (isModifiable) {
369  expr = SymbolicExpr::makeSet(expr, indet, valOps->solver());
370  }
371  SymbolicSemantics::SValuePtr val = SymbolicSemantics::SValue::promote(valOps->undefined_(8));
372  val->set_expression(expr);
373  return val;
374  }
375  }
376  }
377 
378  if (withSideEffects) {
379  return Super::readMemory(addr, dflt, addrOps, valOps);
380  } else {
381  return Super::peekMemory(addr, dflt, addrOps, valOps);
382  }
383 }
384 
385 template<class Super>
386 void
387 MemoryState<Super>::writeMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
389  InstructionSemantics2::BaseSemantics::RiscOperators *addrOps,
390  InstructionSemantics2::BaseSemantics::RiscOperators *valOps) {
391  if (!enabled_)
392  return;
393  Super::writeMemory(addr, value, addrOps, valOps);
394 }
395 
396 template<class Super>
397 void
398 MemoryState<Super>::print(std::ostream &out, InstructionSemantics2::BaseSemantics::Formatter &fmt) const {
399  if (map_) {
400  map_->dump(out, fmt.get_line_prefix());
401  } else {
402  out <<fmt.get_line_prefix() <<"no memory map\n";
403  }
404 
405  Super::print(out, fmt);
406 }
407 
408 } // namespace
409 } // namespace
410 } // namespace
411 } // namespace
412 
413 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
417 #endif
418 
419 #endif
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:153
static RiscOperatorsPtr promote(const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &x)
Run-time promotion of a base RiscOperators pointer to our operators.
Definition: Semantics.h:311
Ptr makeInteger(size_t nbits, uint64_t n, const std::string &comment="", unsigned flags=0)
Leaf constructor.
Defines RISC operators for the SymbolicSemantics domain.
virtual SValuePtr undefined_(size_t nbits)
Returns a new undefined value.
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:160
static Ptr promote(const InstructionSemantics2::BaseSemantics::MemoryStatePtr &x)
Recasts a base pointer to a symbolic memory state.
Definition: Semantics.h:128
Base class for machine instructions.
Ptr makeVariable(size_t nbits, const std::string &comment="", unsigned flags=0)
Leaf constructor.
InstructionSemantics2::BaseSemantics::RegisterStateGeneric RegisterState
Register state for the partitioner.
Definition: Semantics.h:31
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to the RISC operators object.
Definition: Semantics.h:211
static SValuePtr promote(const BaseSemantics::SValuePtr &v)
Promote a base value to a SymbolicSemantics value.
boost::shared_ptr< MemoryMapState > MemoryMapStatePtr
Shared-ownership pointer to a MemoryMapState.
Definition: Semantics.h:204
virtual InstructionSemantics2::BaseSemantics::MemoryStatePtr create(const InstructionSemantics2::BaseSemantics::SValuePtr &addrProtoval, const InstructionSemantics2::BaseSemantics::SValuePtr &valProtoval) const ROSE_OVERRIDE
Virtual constructor.
Definition: Semantics.h:107
const std::vector< SValuePtr > & addressesRead() const
Property: concrete virtual addresses that were read.
Definition: Semantics.h:159
virtual InstructionSemantics2::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics2::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const ROSE_OVERRIDE
Virtual allocating constructor.
Definition: Semantics.h:294
static Ptr instance(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell)
Instantiates a new memory state having specified prototypical cells and value.
Definition: Semantics.h:89
Main namespace for the ROSE library.
InstructionSemantics2::SymbolicSemantics::SValuePtr SValuePtr
Reference counting pointer to semantic value.
Definition: Semantics.h:28
Sawyer::SharedPointer< class SmtSolver > SmtSolverPtr
Reference-counting pointer for SMT solvers.
static RiscOperatorsPtr instance(const RegisterDictionary *regdict, const SmtSolverPtr &solver=SmtSolverPtr(), SemanticMemoryParadigm memoryParadigm=LIST_BASED_MEMORY)
Instantiate a new RiscOperators object and configure it using default values.
Definition: Semantics.h:261
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:152
Base classes for instruction semantics.
InstructionSemantics2::BaseSemantics::State State
Total state (registers and memory) for the partitioner.
Definition: Semantics.h:37
static Ptr instance(const Ptr &other)
Instantiates a new deep copy of an existing state.
Definition: Semantics.h:100
virtual InstructionSemantics2::BaseSemantics::MemoryStatePtr clone() const ROSE_OVERRIDE
Virtual copy constructor.
Definition: Semantics.h:120
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:56
SemanticMemoryParadigm
Organization of semantic memory.
Definition: BasicTypes.h:80
virtual InstructionSemantics2::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics2::BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr()) const ROSE_OVERRIDE
Virtual allocating constructor.
Definition: Semantics.h:300
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
ROSE_DLL_API bool isInitialized()
Checks whether the library has been initialized.
Base class for most instruction semantics RISC operators.
static RiscOperatorsPtr instance(const InstructionSemantics2::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiate a new RiscOperators object with specified prototypical values.
Definition: Semantics.h:280
static RiscOperatorsPtr instance(const InstructionSemantics2::BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiate a new RiscOperators with specified state.
Definition: Semantics.h:286
InstructionSemantics2::BaseSemantics::StatePtr StatePtr
Reference counting pointer to total state.
Definition: Semantics.h:40
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
virtual InstructionSemantics2::BaseSemantics::MemoryStatePtr create(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell) const ROSE_OVERRIDE
Virtual constructor.
Definition: Semantics.h:114
Type of values manipulated by the SymbolicSemantics domain.
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:94
Ptr makeSet(const Ptr &a, const Ptr &b, const SmtSolverPtr &solver=SmtSolverPtr(), const std::string &comment="", unsigned flags=0)
Interior node constructor.
Defines registers available for a particular architecture.
Definition: Registers.h:35
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
InstructionSemantics2::BaseSemantics::RegisterStateGenericPtr RegisterStatePtr
Reference counting pointer to register state.
Definition: Semantics.h:34
InstructionSemantics2::SymbolicSemantics::SValue SValue
Semantic value in the partitioner.
Definition: Semantics.h:25
boost::shared_ptr< MemoryListState > MemoryListStatePtr
Shared-ownership pointer to a MemoryListState.
Definition: Semantics.h:201