ROSE  0.10.12.0
MemoryCellList.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_MemoryCellList_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_MemoryCellList_H
3 #include <rosePublicConfig.h>
4 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
5 
6 #include <BaseSemantics2.h>
7 #include <MemoryCellState.h>
8 
9 #include <boost/serialization/access.hpp>
10 #include <boost/serialization/base_object.hpp>
11 #include <boost/serialization/export.hpp>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 namespace InstructionSemantics2 {
16 namespace BaseSemantics {
17 
19 typedef boost::shared_ptr<class MemoryCellList> MemoryCellListPtr;
20 
40 public:
41  typedef std::list<MemoryCellPtr> CellList;
43 protected:
44  CellList cells; // list of cells in reverse chronological order
45  bool occlusionsErased_; // prune away old cells that are occluded by newer ones.
46 
48  // Serialization
49 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
50 private:
51  friend class boost::serialization::access;
52 
53  template<class S>
54  void serialize(S &s, const unsigned /*version*/) {
55  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(MemoryCellState);
56  s & BOOST_SERIALIZATION_NVP(cells);
57  s & BOOST_SERIALIZATION_NVP(occlusionsErased_);
58  }
59 #endif
60 
62  // Real constructors
63 protected:
64  MemoryCellList() // for serialization
65  : occlusionsErased_(false) {}
66 
67  explicit MemoryCellList(const MemoryCellPtr &protocell)
68  : MemoryCellState(protocell), occlusionsErased_(false) {}
69 
70  MemoryCellList(const SValuePtr &addrProtoval, const SValuePtr &valProtoval)
71  : MemoryCellState(addrProtoval, valProtoval), occlusionsErased_(false) {}
72 
73  // deep-copy cell list so that modifying this new state does not modify the existing state
74  MemoryCellList(const MemoryCellList &other)
75  : MemoryCellState(other), occlusionsErased_(other.occlusionsErased_) {
76  for (CellList::const_iterator ci=other.cells.begin(); ci!=other.cells.end(); ++ci)
77  cells.push_back((*ci)->clone());
78  }
79 
81  // Static allocating constructors
82 public:
86  static MemoryCellListPtr instance(const SValuePtr &addrProtoval, const SValuePtr &valProtoval) {
87  return MemoryCellListPtr(new MemoryCellList(addrProtoval, valProtoval));
88  }
89 
91  static MemoryCellListPtr instance(const MemoryCellPtr &protocell) {
92  return MemoryCellListPtr(new MemoryCellList(protocell));
93  }
94 
96  static MemoryCellListPtr instance(const MemoryCellListPtr &other) {
97  return MemoryCellListPtr(new MemoryCellList(*other));
98  }
99 
100 
102  // Virtual constructors
103 public:
104  virtual MemoryStatePtr create(const SValuePtr &addrProtoval, const SValuePtr &valProtoval) const ROSE_OVERRIDE {
105  return instance(addrProtoval, valProtoval);
106  }
107 
109  virtual MemoryStatePtr create(const MemoryCellPtr &protocell) const {
110  return instance(protocell);
111  }
112 
113  virtual MemoryStatePtr clone() const ROSE_OVERRIDE {
114  return MemoryStatePtr(new MemoryCellList(*this));
115  }
116 
118  // Dynamic pointer casts
119 public:
122  static MemoryCellListPtr promote(const BaseSemantics::MemoryStatePtr &m) {
123  MemoryCellListPtr retval = boost::dynamic_pointer_cast<MemoryCellList>(m);
124  ASSERT_not_null(retval);
125  return retval;
126  }
127 
129  // Methods we inherited
130 public:
131  virtual void clear() ROSE_OVERRIDE;
132  virtual bool merge(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps) ROSE_OVERRIDE;
133  virtual std::vector<MemoryCellPtr> matchingCells(const MemoryCell::Predicate&) const ROSE_OVERRIDE;
134  virtual std::vector<MemoryCellPtr> leadingCells(const MemoryCell::Predicate&) const ROSE_OVERRIDE;
135  virtual void eraseMatchingCells(const MemoryCell::Predicate&) ROSE_OVERRIDE;
136  virtual void eraseLeadingCells(const MemoryCell::Predicate&) ROSE_OVERRIDE;
137  virtual void traverse(MemoryCell::Visitor&) ROSE_OVERRIDE;
138 
149  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
150  RiscOperators *addrOps, RiscOperators *valOps) ROSE_OVERRIDE;
151 
152  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
153  RiscOperators *addrOps, RiscOperators *valOps) ROSE_OVERRIDE;
154 
161  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value,
162  RiscOperators *addrOps, RiscOperators *valOps) ROSE_OVERRIDE;
163 
164  virtual void print(std::ostream&, Formatter&) const ROSE_OVERRIDE;
165 
167  // Methods first declared at this level of the class hierarchy
168 public:
173  bool mergeNoAliasing(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps);
174 
179  bool mergeWithAliasing(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps);
180 
185  virtual bool isAllPresent(const SValuePtr &address, size_t nBytes, RiscOperators *addrOps, RiscOperators *valOps) const;
186 
193  bool occlusionsErased() const { return occlusionsErased_; }
194  void occlusionsErased(bool b) { occlusionsErased_ = b; }
212  template<class Iterator>
213  CellList scan(Iterator &cursor /*in,out*/, const SValuePtr &addr, size_t nBits,
214  RiscOperators *addrOps, RiscOperators *valOps) const {
215  ASSERT_not_null(addr);
216  CellList retval;
217  MemoryCellPtr tempCell = protocell->create(addr, valOps->undefined_(nBits));
218  for (/*void*/; cursor!=cells.end(); ++cursor) {
219  if (tempCell->may_alias(*cursor, addrOps)) {
220  retval.push_back(*cursor);
221  if (tempCell->must_alias(*cursor, addrOps))
222  break;
223  }
224  }
225  return retval;
226  }
227 
230  virtual const CellList& get_cells() const { return cells; }
231  virtual CellList& get_cells() { return cells; }
234  virtual MemoryCell::AddressSet getWritersUnion(const SValuePtr &addr, size_t nBits, RiscOperators *addrOps,
235  RiscOperators *valOps) ROSE_OVERRIDE;
236 
237  virtual MemoryCell::AddressSet getWritersIntersection(const SValuePtr &addr, size_t nBits, RiscOperators *addrOps,
238  RiscOperators *valOps) ROSE_OVERRIDE;
239 
240 protected:
241  // Compute a new value by merging the specified cells. If the cell list is empty return the specified default.
242  virtual SValuePtr mergeCellValues(const CellList &cells, const SValuePtr &dflt, RiscOperators *addrOps,
243  RiscOperators *valOps);
244 
245  // Returns the union of all writers from the specified cells.
246  virtual AddressSet mergeCellWriters(const CellList &cells);
247 
248  // Returns the union of all properties from the specified cells.
249  virtual InputOutputPropertySet mergeCellProperties(const CellList &cells);
250 
251  // Adjust I/O properties in the specified cells to make it look like they were just read. This adds the READ property and
252  // may also add READ_AFTER_WRITE, READ_BEFORE_WRITE, and/or READ_UNINITIALIZED.
253  virtual void updateReadProperties(CellList &cells);
254 
255  // Insert a new cell at the head of the list. It's writers set is empty and its I/O properties will be READ,
256  // READ_BEFORE_WRITE, and READ_UNINITIALIZED.
257  virtual MemoryCellPtr insertReadCell(const SValuePtr &addr, const SValuePtr &value);
258 
259  // Insert a new cell at the head of the list. The specified writers and I/O properties are used.
260  virtual MemoryCellPtr insertReadCell(const SValuePtr &addr, const SValuePtr &value,
261  const AddressSet &writers, const InputOutputPropertySet &props);
262 };
263 
264 } // namespace
265 } // namespace
266 } // namespace
267 } // namespace
268 
269 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
271 #endif
272 
273 #endif
274 #endif
boost::shared_ptr< class MemoryCell > MemoryCellPtr
Shared-ownership pointer to a semantic memory cell.
Definition: MemoryCell.h:17
virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps) ROSE_OVERRIDE
Read a value from memory.
virtual const CellList & get_cells() const
Returns the list of all memory cells.
virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps) ROSE_OVERRIDE
Write a value to memory.
bool mergeWithAliasing(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps)
Merge two states with aliasing.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
STL namespace.
Main namespace for the ROSE library.
static MemoryCellListPtr instance(const MemoryCellPtr &protocell)
Instantiate a new memory state with prototypical memory cell.
virtual MemoryCell::AddressSet getWritersUnion(const SValuePtr &addr, size_t nBits, RiscOperators *addrOps, RiscOperators *valOps) ROSE_OVERRIDE
Writers for an address.
virtual MemoryStatePtr create(const MemoryCellPtr &protocell) const
Virtual allocating constructor.
boost::shared_ptr< class MemoryCellList > MemoryCellListPtr
Shared-ownership pointer to a list-based memory state.
Sawyer::Container::Set< rose_addr_t > AddressSet
Set of concrete virtual addresses.
static MemoryCellListPtr instance(const SValuePtr &addrProtoval, const SValuePtr &valProtoval)
Instantiate a new prototypical memory state.
Base class for most instruction semantics RISC operators.
virtual void traverse(MemoryCell::Visitor &) ROSE_OVERRIDE
Traverse and modify cells.
CellList scan(Iterator &cursor, const SValuePtr &addr, size_t nBits, RiscOperators *addrOps, RiscOperators *valOps) const
Scan cell list to find matching cells.
virtual MemoryCell::AddressSet getWritersIntersection(const SValuePtr &addr, size_t nBits, RiscOperators *addrOps, RiscOperators *valOps) ROSE_OVERRIDE
Writers for an address.
std::list< MemoryCellPtr > CellList
List of memory cells.
virtual CellList & get_cells()
Returns the list of all memory cells.
virtual std::vector< MemoryCellPtr > leadingCells(const MemoryCell::Predicate &) const ROSE_OVERRIDE
Find leading matching cells.
virtual void eraseMatchingCells(const MemoryCell::Predicate &) ROSE_OVERRIDE
Remove all matching cells.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
bool mergeNoAliasing(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps)
Merge two states without aliasing.
virtual void eraseLeadingCells(const MemoryCell::Predicate &) ROSE_OVERRIDE
Remove leading matching cells.
static MemoryCellListPtr promote(const BaseSemantics::MemoryStatePtr &m)
Promote a base memory state pointer to a BaseSemantics::MemoryCellList pointer.
static MemoryCellListPtr instance(const MemoryCellListPtr &other)
Instantiate a new copy of an existing memory state.
virtual std::vector< MemoryCellPtr > matchingCells(const MemoryCell::Predicate &) const ROSE_OVERRIDE
Find all matching cells.
virtual bool isAllPresent(const SValuePtr &address, size_t nBytes, RiscOperators *addrOps, RiscOperators *valOps) const
Predicate to determine whether all bytes are present.