My Project
memstate.hh
Go to the documentation of this file.
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
18 
19 #ifndef __CPUI_MEMSTATE__
20 #define __CPUI_MEMSTATE__
21 
22 #include "pcoderaw.hh"
23 #include "loadimage.hh"
24 
35 
36 class MemoryBank {
37  friend class MemoryPageOverlay;
38  friend class MemoryHashOverlay;
39  int4 wordsize;
40  int4 pagesize;
41  AddrSpace *space;
42 protected:
43  virtual void insert(uintb addr,uintb val)=0;
44  virtual uintb find(uintb addr) const=0;
45  virtual void getPage(uintb addr,uint1 *res,int4 skip,int4 size) const;
46  virtual void setPage(uintb addr,const uint1 *val,int4 skip,int4 size);
47 public:
48  MemoryBank(AddrSpace *spc,int4 ws,int4 ps);
49  virtual ~MemoryBank(void) {}
50  int4 getWordSize(void) const;
51  int4 getPageSize(void) const;
52  AddrSpace *getSpace(void) const;
53 
54  void setValue(uintb offset,int4 size,uintb val);
55  uintb getValue(uintb offset,int4 size) const;
56  void setChunk(uintb offset,int4 size,const uint1 *val);
57  void getChunk(uintb offset,int4 size,uint1 *res) const;
58  static uintb constructValue(const uint1 *ptr,int4 size,bool bigendian);
59  static void deconstructValue(uint1 *ptr,uintb val,int4 size,bool bigendian);
60 };
61 
65 inline int4 MemoryBank::getWordSize(void) const
66 
67 {
68  return wordsize;
69 }
70 
74 inline int4 MemoryBank::getPageSize(void) const
75 
76 {
77  return pagesize;
78 }
79 
82 inline AddrSpace *MemoryBank::getSpace(void) const
83 
84 {
85  return space;
86 }
87 
93 class MemoryImage : public MemoryBank {
94  LoadImage *loader;
95 protected:
96  virtual void insert(uintb addr,uintb val) {
97  throw LowlevelError("Writing to read-only MemoryBank"); }
98  virtual uintb find(uintb addr) const;
99  virtual void getPage(uintb addr,uint1 *res,int4 skip,int4 size) const;
100 public:
101  MemoryImage(AddrSpace *spc,int4 ws,int4 ps,LoadImage *ld);
102 };
103 
111  MemoryBank *underlie;
112  map<uintb,uint1 *> page;
113 protected:
114  virtual void insert(uintb addr,uintb val);
115  virtual uintb find(uintb addr) const;
116  virtual void getPage(uintb addr,uint1 *res,int4 skip,int4 size) const;
117  virtual void setPage(uintb addr,const uint1 *val,int4 skip,int4 size);
118 public:
119  MemoryPageOverlay(AddrSpace *spc,int4 ws,int4 ps,MemoryBank *ul);
120  virtual ~MemoryPageOverlay(void);
121 };
122 
129  MemoryBank *underlie;
130  int4 alignshift;
131  uintb collideskip;
132  vector<uintb> address;
133  vector<uintb> value;
134 protected:
135  virtual void insert(uintb addr,uintb val);
136  virtual uintb find(uintb addr) const;
137 public:
138  MemoryHashOverlay(AddrSpace *spc,int4 ws,int4 ps,int4 hashsize,MemoryBank *ul);
139 };
140 
141 class Translate; // Forward declaration
142 
148 class MemoryState {
149 protected:
151  vector<MemoryBank *> memspace;
152 public:
153  MemoryState(Translate *t);
154  ~MemoryState(void) {}
155  Translate *getTranslate(void) const;
156  void setMemoryBank(MemoryBank *bank);
157  MemoryBank *getMemoryBank(AddrSpace *spc) const;
158  void setValue(AddrSpace *spc,uintb off,int4 size,uintb cval);
159  uintb getValue(AddrSpace *spc,uintb off,int4 size) const;
160  void setValue(const string &nm,uintb cval);
161  uintb getValue(const string &nm) const;
162  void setValue(const VarnodeData *vn,uintb cval);
163  uintb getValue(const VarnodeData *vn) const;
164  void getChunk(uint1 *res,AddrSpace *spc,uintb off,int4 size) const;
165  void setChunk(const uint1 *val,AddrSpace *spc,uintb off,int4 size);
166 };
167 
172 
173 {
174  trans = t;
175 }
176 
180 
181 {
182  return trans;
183 }
184 
189 inline void MemoryState::setValue(const VarnodeData *vn,uintb cval)
190 
191 {
192  setValue(vn->space,vn->offset,vn->size,cval);
193 }
194 
199 inline uintb MemoryState::getValue(const VarnodeData *vn) const
200 
201 {
202  return getValue(vn->space,vn->offset,vn->size);
203 }
204 
205  #endif
A region where processor data is stored.
Definition: space.hh:73
virtual void setPage(uintb addr, const uint1 *val, int4 skip, int4 size)
Write data into a memory page.
Definition: memstate.cc:134
MemoryBank(AddrSpace *spc, int4 ws, int4 ps)
Generic constructor for a memory bank.
Definition: memstate.cc:73
MemoryState(Translate *t)
A constructor for MemoryState.
Definition: memstate.hh:171
An interface into a particular binary executable image.
Definition: loadimage.hh:71
Raw descriptions of varnodes and p-code ops.
Translate * getTranslate(void) const
Get the Translate object.
Definition: memstate.hh:179
Translate * trans
Architecture information about memory spaces.
Definition: memstate.hh:150
Memory storage/state for a single AddressSpace.
Definition: memstate.hh:36
void setValue(uintb offset, int4 size, uintb val)
Set the value of a (small) range of bytes.
Definition: memstate.cc:180
void setValue(AddrSpace *spc, uintb off, int4 size, uintb cval)
Set a value on the memory state.
Definition: memstate.cc:650
vector< MemoryBank * > memspace
Memory banks associated with each address space.
Definition: memstate.hh:151
AddrSpace * space
The address space.
Definition: pcoderaw.hh:34
virtual void getPage(uintb addr, uint1 *res, int4 skip, int4 size) const
Retrieve data from a memory page.
Definition: memstate.cc:91
A kind of MemoryBank which retrieves its data from an underlying LoadImage.
Definition: memstate.hh:93
virtual void insert(uintb addr, uintb val)=0
Insert a word in memory bank at an aligned location.
void setChunk(uintb offset, int4 size, const uint1 *val)
Set values of an arbitrary sequence of bytes.
Definition: memstate.cc:300
int4 getWordSize(void) const
Get the number of bytes in a word for this memory bank.
Definition: memstate.hh:65
uintb offset
The offset within the space.
Definition: pcoderaw.hh:35
The interface to a translation engine for a processor.
Definition: translate.hh:293
The lowest level error generated by the decompiler.
Definition: error.hh:44
A memory bank that implements reads and writes using a hash table.
Definition: memstate.hh:128
uintb getValue(AddrSpace *spc, uintb off, int4 size) const
Retrieve a memory value from the memory state.
Definition: memstate.cc:666
void getChunk(uintb offset, int4 size, uint1 *res) const
Retrieve an arbitrary sequence of bytes.
Definition: memstate.cc:333
uint4 size
The number of bytes in the location.
Definition: pcoderaw.hh:36
uintb getValue(uintb offset, int4 size) const
Retrieve the value encoded in a (small) range of bytes.
Definition: memstate.cc:250
AddrSpace * getSpace(void) const
Get the address space associated with this memory bank.
Definition: memstate.hh:82
static void deconstructValue(uint1 *ptr, uintb val, int4 size, bool bigendian)
Encode value to bytes.
Definition: memstate.cc:51
All storage/state for a pcode machine.
Definition: memstate.hh:148
virtual void insert(uintb addr, uintb val)
Exception is thrown for write attempts.
Definition: memstate.hh:96
Memory bank that overlays some other memory bank, using a "copy on write" behavior.
Definition: memstate.hh:110
virtual uintb find(uintb addr) const =0
Retrieve a word from memory bank at an aligned location.
static uintb constructValue(const uint1 *ptr, int4 size, bool bigendian)
Decode bytes to value.
Definition: memstate.cc:25
int4 getPageSize(void) const
Get the number of bytes in a page for this memory bank.
Definition: memstate.hh:74
Data defining a specific memory location.
Definition: pcoderaw.hh:33
Classes and API for accessing a binary load image.