ROSE  0.11.59.0
Semantics.h
1 #ifndef ROSE_BinaryAnalysis_Partitioner_Semantics_H
2 #define ROSE_BinaryAnalysis_Partitioner_Semantics_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/InstructionSemantics2/SymbolicSemantics.h>
8 
9 #include <boost/serialization/access.hpp>
10 #include <boost/serialization/base_object.hpp>
11 #include <boost/serialization/export.hpp>
12 #include <boost/serialization/vector.hpp>
13 
14 namespace Rose {
15 namespace BinaryAnalysis {
16 namespace Partitioner2 {
17 
24 namespace Semantics {
25 
28 
31 
34 
37 
40 
43 
45 // Memory State
47 
54 template<class Super = InstructionSemantics2::SymbolicSemantics::MemoryListState> // or MemoryMapState
55 class MemoryState: public Super {
56 public:
58  typedef boost::shared_ptr<MemoryState> Ptr;
59 
60 private:
61  MemoryMap::Ptr map_;
62  std::vector<SValuePtr> addressesRead_;
63  bool enabled_;
64 
65 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
66 private:
67  friend class boost::serialization::access;
68 
69  template<class S>
70  void serialize(S &s, const unsigned /*version*/) {
71  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
72  s & BOOST_SERIALIZATION_NVP(map_);
73  s & BOOST_SERIALIZATION_NVP(addressesRead_);
74  s & BOOST_SERIALIZATION_NVP(enabled_);
75  }
76 #endif
77 
78 protected:
79  MemoryState() // for serialization
80  : enabled_(true) {}
81 
82  explicit MemoryState(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell)
83  : Super(protocell), enabled_(true) {}
84 
85  MemoryState(const InstructionSemantics2::BaseSemantics::SValuePtr &addrProtoval,
87  : Super(addrProtoval, valProtoval), enabled_(true) {}
88 
89 public:
92  return Ptr(new MemoryState(protocell));
93  }
94 
98  return Ptr(new MemoryState(addrProtoval, valProtoval));
99  }
100 
102  static Ptr instance(const Ptr &other) {
103  return Ptr(new MemoryState(*other));
104  }
105 
106 public:
110  const InstructionSemantics2::BaseSemantics::SValuePtr &valProtoval) const override {
111  return instance(addrProtoval, valProtoval);
112  }
113 
117  return instance(protocell);
118  }
119 
122  clone() const override {
123  return Ptr(new MemoryState(*this));
124  }
125 
126 public:
129  static Ptr
131  Ptr retval = boost::dynamic_pointer_cast<MemoryState>(x);
132  assert(x!=NULL);
133  return retval;
134  }
135 
136 public:
143  bool enabled() const { return enabled_; }
144  void enabled(bool b) { enabled_ = b; }
154  MemoryMap::Ptr memoryMap() const { return map_; }
155  void memoryMap(const MemoryMap::Ptr &map) { map_ = map; }
161  const std::vector<SValuePtr>& addressesRead() const { return addressesRead_; }
162  std::vector<SValuePtr>& addressesRead() { return addressesRead_; }
165 public:
171 
172  virtual void
173  writeMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
177 
183 
184 private:
186  readOrPeekMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
190  bool withSideEffects);
191 
192 public:
193  void print(std::ostream&, InstructionSemantics2::BaseSemantics::Formatter&) const override;
194 };
195 
198 
201 
203 typedef boost::shared_ptr<MemoryListState> MemoryListStatePtr;
204 
206 typedef boost::shared_ptr<MemoryMapState> MemoryMapStatePtr;
207 
209 // RISC Operators
211 
213 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
214 
220 public:
222 
223 private:
224  static const size_t TRIM_THRESHOLD_DFLT = 100;
225 
227  // Serialization
228 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
229 private:
230  friend class boost::serialization::access;
231 
232  template<class S>
233  void serialize(S &s, const unsigned /*version*/) {
234  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
235  }
236 #endif
237 
239  // Real constructors
240 protected:
241  RiscOperators() {} // for serialization
242 
244  const SmtSolverPtr &solver = SmtSolverPtr())
246  name("PartitionerSemantics");
247  (void)SValue::promote(protoval); // make sure its dynamic type is appropriate
248  trimThreshold(TRIM_THRESHOLD_DFLT);
249  }
250 
252  const SmtSolverPtr &solver = SmtSolverPtr())
254  name("PartitionerSemantics");
255  (void)SValue::promote(state->protoval());
256  trimThreshold(TRIM_THRESHOLD_DFLT);
257  }
258 
260  // Static allocating constructors
261 public:
263  static RiscOperatorsPtr instance(const RegisterDictionary *regdict, const SmtSolverPtr &solver = SmtSolverPtr(),
264  SemanticMemoryParadigm memoryParadigm = LIST_BASED_MEMORY) {
268  switch (memoryParadigm) {
269  case LIST_BASED_MEMORY:
270  memory = MemoryListState::instance(protoval, protoval);
271  break;
272  case MAP_BASED_MEMORY:
273  memory = MemoryMapState::instance(protoval, protoval);
274  break;
275  }
277  return RiscOperatorsPtr(new RiscOperators(state, solver));
278  }
279 
281  static RiscOperatorsPtr
283  return RiscOperatorsPtr(new RiscOperators(protoval, solver));
284  }
285 
287  static RiscOperatorsPtr
289  return RiscOperatorsPtr(new RiscOperators(state, solver));
290  }
291 
293  // Virtual constructors
294 public:
297  const SmtSolverPtr &solver = SmtSolverPtr()) const override {
298  return instance(protoval, solver);
299  }
300 
303  const SmtSolverPtr &solver = SmtSolverPtr()) const override {
304  return instance(state, solver);
305  }
306 
308  // Dynamic pointer casts
309 public:
312  static RiscOperatorsPtr
314  RiscOperatorsPtr retval = boost::dynamic_pointer_cast<RiscOperators>(x);
315  assert(retval!=NULL);
316  return retval;
317  }
318 
320  // Override methods from base class.
321 public:
322  virtual void startInstruction(SgAsmInstruction*) override;
323 };
324 
326 // Memory State
328 
329 template<class Super>
331 MemoryState<Super>::readMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
335  return readOrPeekMemory(addr, dflt, addrOps, valOps, true/*with side effects*/);
336 }
337 
338 template<class Super>
340 MemoryState<Super>::peekMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
344  return readOrPeekMemory(addr, dflt, addrOps, valOps, false/*no side effects*/);
345 }
346 
347 template<class Super>
349 MemoryState<Super>::readOrPeekMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
351  InstructionSemantics2::BaseSemantics::RiscOperators *addrOps,
352  InstructionSemantics2::BaseSemantics::RiscOperators *valOps,
353  bool withSideEffects) {
354  using namespace InstructionSemantics2;
355 
356  if (!enabled_)
357  return dflt->copy();
358 
359  addressesRead_.push_back(SValue::promote(addr));
360  if (map_ && addr->toUnsigned()) {
361  ASSERT_require2(8==dflt->nBits(), "multi-byte reads should have been handled above this call");
362  rose_addr_t va = addr->toUnsigned().get();
363  bool isModifiable = map_->at(va).require(MemoryMap::WRITABLE).exists();
364  bool isInitialized = map_->at(va).require(MemoryMap::INITIALIZED).exists();
365  if (!isModifiable || isInitialized) {
366  uint8_t byte;
367  if (1 == map_->at(va).limit(1).read(&byte).size()) {
369  if (isModifiable) {
371  expr = SymbolicExpr::makeSet(expr, indet, valOps->solver());
372  }
373  SymbolicSemantics::SValuePtr val = SymbolicSemantics::SValue::promote(valOps->undefined_(8));
374  val->set_expression(expr);
375  return val;
376  }
377  }
378  }
379 
380  if (withSideEffects) {
381  return Super::readMemory(addr, dflt, addrOps, valOps);
382  } else {
383  return Super::peekMemory(addr, dflt, addrOps, valOps);
384  }
385 }
386 
387 template<class Super>
388 void
389 MemoryState<Super>::writeMemory(const InstructionSemantics2::BaseSemantics::SValuePtr &addr,
391  InstructionSemantics2::BaseSemantics::RiscOperators *addrOps,
392  InstructionSemantics2::BaseSemantics::RiscOperators *valOps) {
393  if (!enabled_)
394  return;
395  Super::writeMemory(addr, value, addrOps, valOps);
396 }
397 
398 template<class Super>
399 void
400 MemoryState<Super>::print(std::ostream &out, InstructionSemantics2::BaseSemantics::Formatter &fmt) const {
401  if (map_) {
402  map_->dump(out, fmt.get_line_prefix());
403  } else {
404  out <<fmt.get_line_prefix() <<"no memory map\n";
405  }
406 
407  Super::print(out, fmt);
408 }
409 
410 } // namespace
411 } // namespace
412 } // namespace
413 } // namespace
414 
415 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
419 #endif
420 
421 #endif
422 #endif
static RegisterStateGenericPtr instance(const SValuePtr &protoval, const RegisterDictionary *regdict)
Instantiate a new register state.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Sawyer::SharedPointer< Node > Ptr
Shared-ownership pointer to an expression Node.
Definition: SymbolicExpr.h:166
MemoryState< InstructionSemantics2::SymbolicSemantics::MemoryMapState > MemoryMapState
Memory state indexed by hash of address expressions.
Definition: Semantics.h:200
void memoryMap(const MemoryMap::Ptr &map)
The memory map for the specimen.
Definition: Semantics.h:155
static RiscOperatorsPtr promote(const InstructionSemantics2::BaseSemantics::RiscOperatorsPtr &x)
Run-time promotion of a base RiscOperators pointer to our operators.
Definition: Semantics.h:313
Defines RISC operators for the SymbolicSemantics domain.
boost::shared_ptr< class RegisterStateGeneric > RegisterStateGenericPtr
Shared-ownership pointer to generic register states.
virtual InstructionSemantics2::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics2::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
Definition: Semantics.h:296
std::vector< SValuePtr > & addressesRead()
Property: concrete virtual addresses that were read.
Definition: Semantics.h:162
static Ptr promote(const InstructionSemantics2::BaseSemantics::MemoryStatePtr &x)
Recasts a base pointer to a symbolic memory state.
Definition: Semantics.h:130
Base class for machine instructions.
InstructionSemantics2::BaseSemantics::RegisterStateGeneric RegisterState
Register state for the partitioner.
Definition: Semantics.h:33
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to the RISC operators object.
Definition: Semantics.h:213
LeafPtr makeIntegerConstant(size_t nBits, uint64_t value, const std::string &comment="", unsigned flags=0)
Leaf constructor.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
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:206
LeafPtr makeIntegerVariable(size_t nBits, const std::string &comment="", unsigned flags=0)
Leaf constructor.
const std::vector< SValuePtr > & addressesRead() const
Property: concrete virtual addresses that were read.
Definition: Semantics.h:161
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
static Ptr instance(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell)
Instantiates a new memory state having specified prototypical cells and value.
Definition: Semantics.h:91
Main namespace for the ROSE library.
InstructionSemantics2::SymbolicSemantics::SValuePtr SValuePtr
Reference counting pointer to semantic value.
Definition: Semantics.h:30
virtual InstructionSemantics2::BaseSemantics::RiscOperatorsPtr create(const InstructionSemantics2::BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
Definition: Semantics.h:302
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
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:263
MemoryMap::Ptr memoryMap() const
The memory map for the specimen.
Definition: Semantics.h:154
InstructionSemantics2::BaseSemantics::State State
Total state (registers and memory) for the partitioner.
Definition: Semantics.h:39
static Ptr instance(const Ptr &other)
Instantiates a new deep copy of an existing state.
Definition: Semantics.h:102
virtual InstructionSemantics2::BaseSemantics::MemoryStatePtr create(const InstructionSemantics2::BaseSemantics::SValuePtr &addrProtoval, const InstructionSemantics2::BaseSemantics::SValuePtr &valProtoval) const override
Virtual constructor.
Definition: Semantics.h:109
static SValuePtr instance()
Instantiate a new prototypical value.
Base class for semantics machine states.
Definition: State.h:39
boost::shared_ptr< MemoryState > Ptr
Shared-ownership pointer to a MemoryState.
Definition: Semantics.h:58
SemanticMemoryParadigm
Organization of semantic memory.
uint64_t trimThreshold() const
Property: Maximum size of expressions.
virtual InstructionSemantics2::BaseSemantics::MemoryStatePtr create(const InstructionSemantics2::BaseSemantics::MemoryCellPtr &protocell) const override
Virtual constructor.
Definition: Semantics.h:116
MemoryState< InstructionSemantics2::SymbolicSemantics::MemoryListState > MemoryListState
Memory state using a chronological list of cells.
Definition: Semantics.h:197
ROSE_DLL_API bool isInitialized()
Checks whether the library has been initialized.
Base class for most instruction semantics RISC operators.
Definition: RiscOperators.h:48
boost::shared_ptr< MemoryCell > MemoryCellPtr
Shared-ownership pointer to a memory cell.
static RiscOperatorsPtr instance(const InstructionSemantics2::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiate a new RiscOperators object with specified prototypical values.
Definition: Semantics.h:282
static RiscOperatorsPtr instance(const InstructionSemantics2::BaseSemantics::StatePtr &state, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiate a new RiscOperators with specified state.
Definition: Semantics.h:288
InstructionSemantics2::BaseSemantics::StatePtr StatePtr
Reference counting pointer to total state.
Definition: Semantics.h:42
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
Definition: State.h:81
Type of values manipulated by the SymbolicSemantics domain.
virtual void startInstruction(SgAsmInstruction *) override
Called at the beginning of every instruction.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
virtual InstructionSemantics2::BaseSemantics::MemoryStatePtr clone() const override
Virtual copy constructor.
Definition: Semantics.h:122
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:96
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:37
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
virtual const std::string & name() const
Property: Name used for debugging.
InstructionSemantics2::BaseSemantics::RegisterStateGenericPtr RegisterStatePtr
Reference counting pointer to register state.
Definition: Semantics.h:36
InstructionSemantics2::SymbolicSemantics::SValue SValue
Semantic value in the partitioner.
Definition: Semantics.h:27
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
std::shared_ptr< class SmtSolver > SmtSolverPtr
Reference-counting pointer for SMT solvers.
Definition: SmtSolver.h:25
boost::shared_ptr< MemoryListState > MemoryListStatePtr
Shared-ownership pointer to a MemoryListState.
Definition: Semantics.h:203