ROSE  0.11.109.0
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/Partitioner2/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/InstructionSemantics/SymbolicSemantics.h>
8 #include <Rose/BinaryAnalysis/SymbolicExpression.h>
9 
10 #include <boost/serialization/access.hpp>
11 #include <boost/serialization/base_object.hpp>
12 #include <boost/serialization/export.hpp>
13 #include <boost/serialization/vector.hpp>
14 
15 namespace Rose {
16 namespace BinaryAnalysis {
17 namespace Partitioner2 {
18 
25 namespace Semantics {
26 
29 
32 
35 
38 
41 
44 
46 // Memory State
48 
55 template<class Super = InstructionSemantics::SymbolicSemantics::MemoryListState> // or MemoryMapState
56 class MemoryState: public Super {
57 public:
59  typedef boost::shared_ptr<MemoryState> Ptr;
60 
61 private:
62  MemoryMap::Ptr map_;
63  std::vector<SValuePtr> addressesRead_;
64  bool enabled_;
65 
66 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
67 private:
68  friend class boost::serialization::access;
69 
70  template<class S>
71  void serialize(S &s, const unsigned /*version*/) {
72  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
73  s & BOOST_SERIALIZATION_NVP(map_);
74  s & BOOST_SERIALIZATION_NVP(addressesRead_);
75  s & BOOST_SERIALIZATION_NVP(enabled_);
76  }
77 #endif
78 
79 protected:
80  MemoryState() // for serialization
81  : enabled_(true) {}
82 
83  explicit MemoryState(const InstructionSemantics::BaseSemantics::MemoryCellPtr &protocell)
84  : Super(protocell), enabled_(true) {}
85 
86  MemoryState(const InstructionSemantics::BaseSemantics::SValuePtr &addrProtoval,
88  : Super(addrProtoval, valProtoval), enabled_(true) {}
89 
90 public:
93  return Ptr(new MemoryState(protocell));
94  }
95 
99  return Ptr(new MemoryState(addrProtoval, valProtoval));
100  }
101 
103  static Ptr instance(const Ptr &other) {
104  return Ptr(new MemoryState(*other));
105  }
106 
107 public:
111  const InstructionSemantics::BaseSemantics::SValuePtr &valProtoval) const override {
112  return instance(addrProtoval, valProtoval);
113  }
114 
118  return instance(protocell);
119  }
120 
123  clone() const override {
124  return Ptr(new MemoryState(*this));
125  }
126 
127 public:
130  static Ptr
132  Ptr retval = boost::dynamic_pointer_cast<MemoryState>(x);
133  assert(x!=NULL);
134  return retval;
135  }
136 
137 public:
144  bool enabled() const { return enabled_; }
145  void enabled(bool b) { enabled_ = b; }
155  MemoryMap::Ptr memoryMap() const { return map_; }
156  void memoryMap(const MemoryMap::Ptr &map) { map_ = map; }
162  const std::vector<SValuePtr>& addressesRead() const { return addressesRead_; }
163  std::vector<SValuePtr>& addressesRead() { return addressesRead_; }
166 public:
168  readMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
172 
173  virtual void
174  writeMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
178 
180  peekMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
184 
185 private:
187  readOrPeekMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
191  bool withSideEffects);
192 
193 public:
194  void print(std::ostream&, InstructionSemantics::BaseSemantics::Formatter&) const override;
195 };
196 
199 
202 
204 typedef boost::shared_ptr<MemoryListState> MemoryListStatePtr;
205 
207 typedef boost::shared_ptr<MemoryMapState> MemoryMapStatePtr;
208 
210 // RISC Operators
212 
214 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
215 
221 public:
223  using Ptr = RiscOperatorsPtr;
224 
225 private:
226  static const size_t TRIM_THRESHOLD_DFLT = 100;
227 
229  // Serialization
230 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
231 private:
232  friend class boost::serialization::access;
233 
234  template<class S>
235  void serialize(S &s, const unsigned /*version*/) {
236  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
237  }
238 #endif
239 
241  // Real constructors
242 protected:
243  RiscOperators(); // for serialization
244 
246  const SmtSolverPtr &solver = SmtSolverPtr());
247 
249  const SmtSolverPtr &solver = SmtSolverPtr());
250 
251 public:
252  ~RiscOperators();
253 
255  // Static allocating constructors
256 public:
258  static RiscOperatorsPtr instance(const RegisterDictionaryPtr &regdict, const SmtSolverPtr &solver = SmtSolverPtr(),
259  SemanticMemoryParadigm memoryParadigm = LIST_BASED_MEMORY) {
263  switch (memoryParadigm) {
264  case LIST_BASED_MEMORY:
265  memory = MemoryListState::instance(protoval, protoval);
266  break;
267  case MAP_BASED_MEMORY:
268  memory = MemoryMapState::instance(protoval, protoval);
269  break;
270  }
272  return RiscOperatorsPtr(new RiscOperators(state, solver));
273  }
274 
276  static RiscOperatorsPtr
278  return RiscOperatorsPtr(new RiscOperators(protoval, solver));
279  }
280 
282  static RiscOperatorsPtr
284  return RiscOperatorsPtr(new RiscOperators(state, solver));
285  }
286 
288  // Virtual constructors
289 public:
292  const SmtSolverPtr &solver = SmtSolverPtr()) const override {
293  return instance(protoval, solver);
294  }
295 
298  const SmtSolverPtr &solver = SmtSolverPtr()) const override {
299  return instance(state, solver);
300  }
301 
303  // Dynamic pointer casts
304 public:
307  static RiscOperatorsPtr
309  RiscOperatorsPtr retval = boost::dynamic_pointer_cast<RiscOperators>(x);
310  assert(retval!=NULL);
311  return retval;
312  }
313 
315  // Override methods from base class.
316 public:
317  virtual void startInstruction(SgAsmInstruction*) override;
318 };
319 
321 // Memory State
323 
324 template<class Super>
326 MemoryState<Super>::readMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
330  return readOrPeekMemory(addr, dflt, addrOps, valOps, true/*with side effects*/);
331 }
332 
333 template<class Super>
335 MemoryState<Super>::peekMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
339  return readOrPeekMemory(addr, dflt, addrOps, valOps, false/*no side effects*/);
340 }
341 
342 template<class Super>
344 MemoryState<Super>::readOrPeekMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
346  InstructionSemantics::BaseSemantics::RiscOperators *addrOps,
347  InstructionSemantics::BaseSemantics::RiscOperators *valOps,
348  bool withSideEffects) {
349  using namespace InstructionSemantics;
350 
351  if (!enabled_)
352  return dflt->copy();
353 
354  addressesRead_.push_back(SValue::promote(addr));
355  if (map_ && addr->toUnsigned()) {
356  ASSERT_require2(8==dflt->nBits(), "multi-byte reads should have been handled above this call");
357  rose_addr_t va = addr->toUnsigned().get();
358  bool isModifiable = map_->at(va).require(MemoryMap::WRITABLE).exists();
359  bool isInitialized = map_->at(va).require(MemoryMap::INITIALIZED).exists();
360  if (!isModifiable || isInitialized) {
361  uint8_t byte;
362  if (1 == map_->at(va).limit(1).read(&byte).size()) {
364  if (isModifiable) {
366  expr = SymbolicExpression::makeSet(expr, indet, valOps->solver());
367  }
368  SymbolicSemantics::SValuePtr val = SymbolicSemantics::SValue::promote(valOps->undefined_(8));
369  val->set_expression(expr);
370  return val;
371  }
372  }
373  }
374 
375  if (withSideEffects) {
376  return Super::readMemory(addr, dflt, addrOps, valOps);
377  } else {
378  return Super::peekMemory(addr, dflt, addrOps, valOps);
379  }
380 }
381 
382 template<class Super>
383 void
384 MemoryState<Super>::writeMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
386  InstructionSemantics::BaseSemantics::RiscOperators *addrOps,
387  InstructionSemantics::BaseSemantics::RiscOperators *valOps) {
388  if (!enabled_)
389  return;
390  Super::writeMemory(addr, value, addrOps, valOps);
391 }
392 
393 template<class Super>
394 void
395 MemoryState<Super>::print(std::ostream &out, InstructionSemantics::BaseSemantics::Formatter &fmt) const {
396  if (map_) {
397  map_->dump(out, fmt.get_line_prefix());
398  } else {
399  out <<fmt.get_line_prefix() <<"no memory map\n";
400  }
401 
402  Super::print(out, fmt);
403 }
404 
405 } // namespace
406 } // namespace
407 } // namespace
408 } // namespace
409 
410 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
414 #endif
415 
416 #endif
417 #endif
InstructionSemantics::BaseSemantics::State State
Total state (registers and memory) for the partitioner.
Definition: Semantics.h:40
InstructionSemantics::BaseSemantics::StatePtr StatePtr
Reference counting pointer to total state.
Definition: Semantics.h:43
virtual InstructionSemantics::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics::BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
Definition: Semantics.h:297
void memoryMap(const MemoryMap::Ptr &map)
The memory map for the specimen.
Definition: Semantics.h:156
virtual InstructionSemantics::BaseSemantics::MemoryStatePtr clone() const override
Virtual copy constructor.
Definition: Semantics.h:123
static RiscOperatorsPtr instance(const RegisterDictionaryPtr &regdict, const SmtSolverPtr &solver=SmtSolverPtr(), SemanticMemoryParadigm memoryParadigm=LIST_BASED_MEMORY)
Instantiate a new RiscOperators object and configure it using default values.
Definition: Semantics.h:258
static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiate a new RiscOperators with specified state.
Definition: Semantics.h:283
std::vector< SValuePtr > & addressesRead()
Property: concrete virtual addresses that were read.
Definition: Semantics.h:163
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Base class for machine instructions.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to the RISC operators object.
Definition: Semantics.h:214
static SValuePtr promote(const BaseSemantics::SValuePtr &v)
Promote a base value to a SymbolicSemantics value.
virtual InstructionSemantics::BaseSemantics::MemoryStatePtr create(const InstructionSemantics::BaseSemantics::MemoryCellPtr &protocell) const override
Virtual constructor.
Definition: Semantics.h:117
boost::shared_ptr< MemoryMapState > MemoryMapStatePtr
Shared-ownership pointer to a MemoryMapState.
Definition: Semantics.h:207
static Ptr instance(const InstructionSemantics::BaseSemantics::MemoryCellPtr &protocell)
Instantiates a new memory state having specified prototypical cells and value.
Definition: Semantics.h:92
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
const std::vector< SValuePtr > & addressesRead() const
Property: concrete virtual addresses that were read.
Definition: Semantics.h:162
Main namespace for the ROSE library.
boost::shared_ptr< MemoryCell > MemoryCellPtr
Shared-ownership pointer to a memory cell.
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
Definition: State.h:81
boost::shared_ptr< class RegisterStateGeneric > RegisterStateGenericPtr
Shared-ownership pointer to generic register states.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
MemoryMap::Ptr memoryMap() const
The memory map for the specimen.
Definition: Semantics.h:155
InstructionSemantics::SymbolicSemantics::SValuePtr SValuePtr
Reference counting pointer to semantic value.
Definition: Semantics.h:31
InstructionSemantics::BaseSemantics::RegisterStateGenericPtr RegisterStatePtr
Reference counting pointer to register state.
Definition: Semantics.h:37
static Ptr instance(const Ptr &other)
Instantiates a new deep copy of an existing state.
Definition: Semantics.h:103
boost::shared_ptr< MemoryState > Ptr
Shared-ownership pointer to a MemoryState.
Definition: Semantics.h:59
LeafPtr makeIntegerVariable(size_t nBits, const std::string &comment="", unsigned flags=0)
Leaf constructor.
Type of values manipulated by the SymbolicSemantics domain.
static SValuePtr instance()
Instantiate a new prototypical value.
virtual InstructionSemantics::BaseSemantics::MemoryStatePtr create(const InstructionSemantics::BaseSemantics::SValuePtr &addrProtoval, const InstructionSemantics::BaseSemantics::SValuePtr &valProtoval) const override
Virtual constructor.
Definition: Semantics.h:110
Ptr makeSet(const Ptr &a, const Ptr &b, const SmtSolverPtr &solver=SmtSolverPtr(), const std::string &comment="", unsigned flags=0)
Interior node constructor.
ROSE_DLL_API bool isInitialized()
Checks whether the library has been initialized.
static RegisterStateGenericPtr instance(const SValuePtr &protoval, const RegisterDictionaryPtr &regdict)
Instantiate a new register state.
Defines RISC operators for the SymbolicSemantics domain.
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:97
static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiate a new RiscOperators object with specified prototypical values.
Definition: Semantics.h:277
static Ptr promote(const InstructionSemantics::BaseSemantics::MemoryStatePtr &x)
Recasts a base pointer to a symbolic memory state.
Definition: Semantics.h:131
LeafPtr makeIntegerConstant(size_t nBits, uint64_t value, const std::string &comment="", unsigned flags=0)
Leaf constructor.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
InstructionSemantics::BaseSemantics::RegisterStateGeneric RegisterState
Register state for the partitioner.
Definition: Semantics.h:34
virtual void startInstruction(SgAsmInstruction *) override
Called at the beginning of every instruction.
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
Sawyer::SharedPointer< Node > Ptr
Reference counting pointer.
virtual InstructionSemantics::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
Definition: Semantics.h:291
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
static RiscOperatorsPtr promote(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &x)
Run-time promotion of a base RiscOperators pointer to our operators.
Definition: Semantics.h:308
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:49
MemoryState< InstructionSemantics::SymbolicSemantics::MemoryMapState > MemoryMapState
Memory state indexed by hash of address expressions.
Definition: Semantics.h:201
MemoryState< InstructionSemantics::SymbolicSemantics::MemoryListState > MemoryListState
Memory state using a chronological list of cells.
Definition: Semantics.h:198
InstructionSemantics::SymbolicSemantics::SValue SValue
Semantic value in the partitioner.
Definition: Semantics.h:28
Base class for semantics machine states.
Definition: State.h:39
boost::shared_ptr< MemoryListState > MemoryListStatePtr
Shared-ownership pointer to a MemoryListState.
Definition: Semantics.h:204