ROSE  0.11.145.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 
248 
249 public:
250  ~RiscOperators();
251 
253  // Static allocating constructors
254 public:
258  static RiscOperatorsPtr instance(const RegisterDictionaryPtr&);
259  static RiscOperatorsPtr instance(const RegisterDictionaryPtr&, const SmtSolverPtr&,
260  SemanticMemoryParadigm memoryParadigm = LIST_BASED_MEMORY);
266  static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::SValuePtr &protoval);
267  static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::SValuePtr &protoval, const SmtSolverPtr&);
273  static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::StatePtr&);
274  static RiscOperatorsPtr instance(const InstructionSemantics::BaseSemantics::StatePtr&, const SmtSolverPtr&);
277  // Virtual constructors
279 public:
282  const SmtSolverPtr &solver = SmtSolverPtr()) const override {
283  return instance(protoval, solver);
284  }
285 
288  const SmtSolverPtr &solver = SmtSolverPtr()) const override {
289  return instance(state, solver);
290  }
291 
293  // Dynamic pointer casts
294 public:
297  static RiscOperatorsPtr promote(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr&);
298 
300  // Override methods from base class.
301 public:
302  virtual void startInstruction(SgAsmInstruction*) override;
303 };
304 
306 // Memory State
308 
309 template<class Super>
311 MemoryState<Super>::readMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
315  return readOrPeekMemory(addr, dflt, addrOps, valOps, true/*with side effects*/);
316 }
317 
318 template<class Super>
320 MemoryState<Super>::peekMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
324  return readOrPeekMemory(addr, dflt, addrOps, valOps, false/*no side effects*/);
325 }
326 
327 template<class Super>
329 MemoryState<Super>::readOrPeekMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
331  InstructionSemantics::BaseSemantics::RiscOperators *addrOps,
332  InstructionSemantics::BaseSemantics::RiscOperators *valOps,
333  bool withSideEffects) {
334  using namespace InstructionSemantics;
335 
336  if (!enabled_)
337  return dflt->copy();
338 
339  addressesRead_.push_back(SValue::promote(addr));
340  if (map_ && addr->toUnsigned()) {
341  ASSERT_require2(8==dflt->nBits(), "multi-byte reads should have been handled above this call");
342  rose_addr_t va = addr->toUnsigned().get();
343  bool isModifiable = map_->at(va).require(MemoryMap::WRITABLE).exists();
344  bool isInitialized = map_->at(va).require(MemoryMap::INITIALIZED).exists();
345  if (!isModifiable || isInitialized) {
346  uint8_t byte;
347  if (1 == map_->at(va).limit(1).read(&byte).size()) {
349  if (isModifiable) {
351  expr = SymbolicExpression::makeSet(expr, indet, valOps->solver());
352  }
353  SymbolicSemantics::SValuePtr val = SymbolicSemantics::SValue::promote(valOps->undefined_(8));
354  val->set_expression(expr);
355  return val;
356  }
357  }
358  }
359 
360  if (withSideEffects) {
361  return Super::readMemory(addr, dflt, addrOps, valOps);
362  } else {
363  return Super::peekMemory(addr, dflt, addrOps, valOps);
364  }
365 }
366 
367 template<class Super>
368 void
369 MemoryState<Super>::writeMemory(const InstructionSemantics::BaseSemantics::SValuePtr &addr,
371  InstructionSemantics::BaseSemantics::RiscOperators *addrOps,
372  InstructionSemantics::BaseSemantics::RiscOperators *valOps) {
373  if (!enabled_)
374  return;
375  Super::writeMemory(addr, value, addrOps, valOps);
376 }
377 
378 template<class Super>
379 void
380 MemoryState<Super>::print(std::ostream &out, InstructionSemantics::BaseSemantics::Formatter &fmt) const {
381  if (map_) {
382  map_->dump(out, fmt.get_line_prefix());
383  } else {
384  out <<fmt.get_line_prefix() <<"no memory map\n";
385  }
386 
387  Super::print(out, fmt);
388 }
389 
390 } // namespace
391 } // namespace
392 } // namespace
393 } // namespace
394 
395 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
399 #endif
400 
401 #endif
402 #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:287
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
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
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.
static RiscOperatorsPtr promote(const InstructionSemantics::BaseSemantics::RiscOperatorsPtr &)
Run-time promotion of a base RiscOperators pointer to our operators.
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
boost::shared_ptr< class RegisterStateGeneric > RegisterStateGenericPtr
Shared-ownership pointer to generic register states.
static RiscOperatorsPtr instance(const RegisterDictionaryPtr &)
Instantiate a new RiscOperators object and configure it using default values.
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.
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.
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 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:281
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
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