My Project
globalcontext.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  */
16 #ifndef __GLOBALCONTEXT__
17 #define __GLOBALCONTEXT__
18 
21 
22 #include "pcoderaw.hh"
23 #include "partmap.hh"
24 
31  int4 word;
32  int4 startbit;
33  int4 endbit;
34  int4 shift;
35  uintm mask;
36 public:
37  ContextBitRange(void) { }
38  ContextBitRange(int4 sbit,int4 ebit);
39  int4 getShift(void) const { return shift; }
40  uintm getMask(void) const { return mask; }
41  int4 getWord(void) const { return word; }
42 
47  void setValue(uintm *vec,uintm val) const {
48  uintm newval = vec[word];
49  newval &= ~(mask<<shift);
50  newval |= ((val & mask)<<shift);
51  vec[word] = newval;
52  }
53 
58  uintm getValue(const uintm *vec) const {
59  return ((vec[word]>>shift)&mask);
60  }
61 };
62 
70  uintb val;
71  void restoreXml(const Element *el,const AddrSpaceManager *manage);
72  void saveXml(ostream &s) const;
73 };
74 typedef vector<TrackedContext> TrackedSet;
75 
109 protected:
110  static void saveTracked(ostream &s,const Address &addr,const TrackedSet &vec);
111  static void restoreTracked(const Element *el,const AddrSpaceManager *manage,TrackedSet &vec);
112 
118  virtual ContextBitRange &getVariable(const string &nm)=0;
119 
125  virtual const ContextBitRange &getVariable(const string &nm) const=0;
126 
139  virtual void getRegionForSet(vector<uintm *> &res,const Address &addr1,
140  const Address &addr2,int4 num,uintm mask)=0;
141 
151  virtual void getRegionToChangePoint(vector<uintm *> &res,const Address &addr,int4 num,uintm mask)=0;
152 
158  virtual uintm *getDefaultValue(void)=0;
159 
165  virtual const uintm *getDefaultValue(void) const=0;
166 public:
167  virtual ~ContextDatabase() {}
168 
172  virtual int4 getContextSize(void) const=0;
173 
183  virtual void registerVariable(const string &nm,int4 sbit,int4 ebit)=0;
184 
189  virtual const uintm *getContext(const Address &addr) const=0;
190 
199  virtual const uintm *getContext(const Address &addr,uintb &first,uintb &last) const=0;
200 
204  virtual TrackedSet &getTrackedDefault(void)=0;
205 
210  virtual const TrackedSet &getTrackedSet(const Address &addr) const=0;
211 
219  virtual TrackedSet &createSet(const Address &addr1,const Address &addr2)=0;
220 
224  virtual void saveXml(ostream &s) const=0;
225 
230  virtual void restoreXml(const Element *el,const AddrSpaceManager *manage)=0;
231 
238  virtual void restoreFromSpec(const Element *el,const AddrSpaceManager *manage)=0;
239 
240  void setVariableDefault(const string &nm,uintm val);
241  uintm getDefaultValue(const string &nm) const;
242  void setVariable(const string &nm,const Address &addr,uintm value);
243  uintm getVariable(const string &nm,const Address &addr) const;
244  void setContextChangePoint(const Address &addr,int4 num,uintm mask,uintm value);
245  void setContextRegion(const Address &addr1,const Address &addr2,int4 num,uintm mask,uintm value);
246  void setVariableRegion(const string &nm,const Address &begad,
247  const Address &endad,uintm value);
248  uintb getTrackedValue(const VarnodeData &mem,const Address &point) const;
249 };
250 
257 
263  struct FreeArray {
264  uintm *array;
265  uintm *mask;
266  int4 size;
267  FreeArray(void) { size=0; array = (uintm *)0; mask = (uintm *)0; }
268  ~FreeArray(void) { if (size!=0) { delete [] array; delete [] mask; } }
269  void reset(int4 sz);
270  FreeArray &operator=(const FreeArray &op2);
271  };
272 
273  int4 size;
274  map<string,ContextBitRange> variables;
275  partmap<Address,FreeArray> database;
276  partmap<Address,TrackedSet> trackbase;
277  void saveContext(ostream &s,const Address &addr,const uintm *vec) const;
278  void restoreContext(const Element *el,const Address &addr1,const Address &addr2);
279  virtual ContextBitRange &getVariable(const string &nm);
280  virtual const ContextBitRange &getVariable(const string &nm) const;
281  virtual void getRegionForSet(vector<uintm *> &res,const Address &addr1,
282  const Address &addr2,int4 num,uintm mask);
283  virtual void getRegionToChangePoint(vector<uintm *> &res,const Address &addr,int4 num,uintm mask);
284  virtual uintm *getDefaultValue(void) { return database.defaultValue().array; }
285  virtual const uintm *getDefaultValue(void) const { return database.defaultValue().array; }
286 public:
287  ContextInternal(void) { size = 0; }
288  virtual ~ContextInternal(void) {}
289  virtual int4 getContextSize(void) const { return size; }
290  virtual void registerVariable(const string &nm,int4 sbit,int4 ebit);
291 
292  virtual const uintm *getContext(const Address &addr) const { return database.getValue(addr).array; }
293  virtual const uintm *getContext(const Address &addr,uintb &first,uintb &last) const;
294 
295  virtual TrackedSet &getTrackedDefault(void) { return trackbase.defaultValue(); }
296  virtual const TrackedSet &getTrackedSet(const Address &addr) const { return trackbase.getValue(addr); }
297  virtual TrackedSet &createSet(const Address &addr1,const Address &addr2);
298 
299  virtual void saveXml(ostream &s) const;
300  virtual void restoreXml(const Element *el,const AddrSpaceManager *manage);
301  virtual void restoreFromSpec(const Element *el,const AddrSpaceManager *manage);
302 };
303 
310  ContextDatabase *database;
311  bool allowset;
312  mutable AddrSpace *curspace;
313  mutable uintb first;
314  mutable uintb last;
315  mutable const uintm *context;
316 public:
318  ContextDatabase *getDatabase(void) const { return database; }
319  void allowSet(bool val) { allowset = val; }
320  void getContext(const Address &addr,uintm *buf) const;
321  void setContext(const Address &addr,int4 num,uintm mask,uintm value);
322  void setContext(const Address &addr1,const Address &addr2,int4 num,uintm mask,uintm value);
323 };
324 
325 #endif
vector< TrackedContext > TrackedSet
A set of tracked registers and their values (at one code point)
Definition: globalcontext.hh:74
A region where processor data is stored.
Definition: space.hh:73
const _valuetype & defaultValue(void) const
Get the default value object.
Definition: partmap.hh:61
A manager for different address spaces.
Definition: translate.hh:218
void allowSet(bool val)
Toggle whether setContext() calls are ignored.
Definition: globalcontext.hh:319
Raw descriptions of varnodes and p-code ops.
VarnodeData loc
Storage details of the register being tracked.
Definition: globalcontext.hh:69
uintm getMask(void) const
Return the mask for this value.
Definition: globalcontext.hh:40
uintb val
The value of the register.
Definition: globalcontext.hh:70
ContextBitRange(void)
Constructor for use with restoreXml()
Definition: globalcontext.hh:37
ContextDatabase * getDatabase(void) const
Retrieve the encapsulated database object.
Definition: globalcontext.hh:318
_valuetype & getValue(const _linetype &pnt)
Get the value object at a point.
Definition: partmap.hh:81
An interface to a database of disassembly/decompiler context information.
Definition: globalcontext.hh:108
virtual const TrackedSet & getTrackedSet(const Address &addr) const
Get the set of tracked register values associated with the given address.
Definition: globalcontext.hh:296
virtual const uintm * getContext(const Address &addr) const
Get the context blob of values associated with a given address.
Definition: globalcontext.hh:292
virtual int4 getContextSize(void) const
Retrieve the number of words (uintm) in a context blob.
Definition: globalcontext.hh:289
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
void setValue(uintm *vec, uintm val) const
Set this value within a given context blob.
Definition: globalcontext.hh:47
uintm getValue(const uintm *vec) const
Retrieve this value from a given context blob.
Definition: globalcontext.hh:58
The partmap<> template mapping a linear space to value objects.
A tracked register (Varnode) and the value it contains.
Definition: globalcontext.hh:68
Description of a context variable within the disassembly context blob.
Definition: globalcontext.hh:30
An in-memory implementation of the ContextDatabase interface.
Definition: globalcontext.hh:256
An XML element. A node in the DOM tree.
Definition: xml.hh:150
virtual ~ContextDatabase()
Destructor.
Definition: globalcontext.hh:167
virtual TrackedSet & getTrackedDefault(void)
Get the set of default values for all tracked registers.
Definition: globalcontext.hh:295
A helper class for caching the active context blob to minimize database lookups.
Definition: globalcontext.hh:309
int4 getWord(void) const
Return the word index for this value.
Definition: globalcontext.hh:41
int4 getShift(void) const
Return the shift-amount for this value.
Definition: globalcontext.hh:39
Data defining a specific memory location.
Definition: pcoderaw.hh:33