Ghidra Decompiler Analysis Engine
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
MemoryBank::getPageSize
int4 getPageSize(void) const
Get the number of bytes in a page for this memory bank.
Definition: memstate.hh:74
MemoryState::setValue
void setValue(AddrSpace *spc, uintb off, int4 size, uintb cval)
Set a value on the memory state.
Definition: memstate.cc:650
MemoryBank::getPage
virtual void getPage(uintb addr, uint1 *res, int4 skip, int4 size) const
Retrieve data from a memory page.
Definition: memstate.cc:91
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
VarnodeData::space
AddrSpace * space
The address space.
Definition: pcoderaw.hh:34
MemoryBank
Memory storage/state for a single AddressSpace.
Definition: memstate.hh:36
LowlevelError
The lowest level error generated by the decompiler.
Definition: error.hh:44
MemoryState::getMemoryBank
MemoryBank * getMemoryBank(AddrSpace *spc) const
Get a memory bank associated with a particular space.
Definition: memstate.cc:634
loadimage.hh
Classes and API for accessing a binary load image.
MemoryPageOverlay::setPage
virtual void setPage(uintb addr, const uint1 *val, int4 skip, int4 size)
Overridden setPage.
Definition: memstate.cc:500
MemoryState::getChunk
void getChunk(uint1 *res, AddrSpace *spc, uintb off, int4 size) const
Get a chunk of data from memory state.
Definition: memstate.cc:710
MemoryState::MemoryState
MemoryState(Translate *t)
A constructor for MemoryState.
Definition: memstate.hh:171
MemoryState::setChunk
void setChunk(const uint1 *val, AddrSpace *spc, uintb off, int4 size)
Set a chunk of data from memory state.
Definition: memstate.cc:727
MemoryBank::setPage
virtual void setPage(uintb addr, const uint1 *val, int4 skip, int4 size)
Write data into a memory page.
Definition: memstate.cc:134
MemoryState::trans
Translate * trans
Architecture information about memory spaces.
Definition: memstate.hh:150
LoadImage
An interface into a particular binary executable image.
Definition: loadimage.hh:71
MemoryBank::insert
virtual void insert(uintb addr, uintb val)=0
Insert a word in memory bank at an aligned location.
MemoryBank::setChunk
void setChunk(uintb offset, int4 size, const uint1 *val)
Set values of an arbitrary sequence of bytes.
Definition: memstate.cc:300
MemoryState::getTranslate
Translate * getTranslate(void) const
Get the Translate object.
Definition: memstate.hh:179
VarnodeData::offset
uintb offset
The offset within the space.
Definition: pcoderaw.hh:35
VarnodeData::size
uint4 size
The number of bytes in the location.
Definition: pcoderaw.hh:36
MemoryBank::getSpace
AddrSpace * getSpace(void) const
Get the address space associated with this memory bank.
Definition: memstate.hh:82
MemoryState
All storage/state for a pcode machine.
Definition: memstate.hh:148
MemoryBank::setValue
void setValue(uintb offset, int4 size, uintb val)
Set the value of a (small) range of bytes.
Definition: memstate.cc:180
MemoryHashOverlay::find
virtual uintb find(uintb addr) const
Overridden aligned word find.
Definition: memstate.cc:573
MemoryImage
A kind of MemoryBank which retrieves its data from an underlying LoadImage.
Definition: memstate.hh:93
MemoryBank::getWordSize
int4 getWordSize(void) const
Get the number of bytes in a word for this memory bank.
Definition: memstate.hh:65
MemoryBank::MemoryBank
MemoryBank(AddrSpace *spc, int4 ws, int4 ps)
Generic constructor for a memory bank.
Definition: memstate.cc:73
MemoryState::memspace
vector< MemoryBank * > memspace
Memory banks associated with each address space.
Definition: memstate.hh:151
MemoryBank::find
virtual uintb find(uintb addr) const =0
Retrieve a word from memory bank at an aligned location.
MemoryHashOverlay::insert
virtual void insert(uintb addr, uintb val)
Overridden aligned word insert.
Definition: memstate.cc:549
MemoryPageOverlay::insert
virtual void insert(uintb addr, uintb val)
Overridden aligned word insert.
Definition: memstate.cc:417
Translate
The interface to a translation engine for a processor.
Definition: translate.hh:294
MemoryBank::constructValue
static uintb constructValue(const uint1 *ptr, int4 size, bool bigendian)
Decode bytes to value.
Definition: memstate.cc:25
MemoryHashOverlay
A memory bank that implements reads and writes using a hash table.
Definition: memstate.hh:128
MemoryPageOverlay::find
virtual uintb find(uintb addr) const
Overridden aligned word find.
Definition: memstate.cc:448
MemoryBank::getValue
uintb getValue(uintb offset, int4 size) const
Retrieve the value encoded in a (small) range of bytes.
Definition: memstate.cc:250
MemoryPageOverlay::getPage
virtual void getPage(uintb addr, uint1 *res, int4 skip, int4 size) const
Overridden getPage.
Definition: memstate.cc:474
MemoryPageOverlay
Memory bank that overlays some other memory bank, using a "copy on write" behavior.
Definition: memstate.hh:110
MemoryState::getValue
uintb getValue(AddrSpace *spc, uintb off, int4 size) const
Retrieve a memory value from the memory state.
Definition: memstate.cc:666
MemoryImage::getPage
virtual void getPage(uintb addr, uint1 *res, int4 skip, int4 size) const
Overridded getPage method.
Definition: memstate.cc:384
MemoryState::setMemoryBank
void setMemoryBank(MemoryBank *bank)
Map a memory bank into the state.
Definition: memstate.cc:618
MemoryImage::MemoryImage
MemoryImage(AddrSpace *spc, int4 ws, int4 ps, LoadImage *ld)
Constructor for a loadimage memorybank.
Definition: memstate.cc:405
VarnodeData
Data defining a specific memory location.
Definition: pcoderaw.hh:33
pcoderaw.hh
Raw descriptions of varnodes and p-code ops.
MemoryBank::getChunk
void getChunk(uintb offset, int4 size, uint1 *res) const
Retrieve an arbitrary sequence of bytes.
Definition: memstate.cc:333
MemoryImage::find
virtual uintb find(uintb addr) const
Overridden find method.
Definition: memstate.cc:363
MemoryBank::deconstructValue
static void deconstructValue(uint1 *ptr, uintb val, int4 size, bool bigendian)
Encode value to bytes.
Definition: memstate.cc:51