Ghidra Decompiler Analysis Engine
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
partmap::getValue
_valuetype & getValue(const _linetype &pnt)
Get the value object at a point.
Definition: partmap.hh:81
ContextDatabase::saveTracked
static void saveTracked(ostream &s, const Address &addr, const TrackedSet &vec)
Save all tracked register values for a specific address to an XML stream.
Definition: globalcontext.cc:69
ContextDatabase::getRegionToChangePoint
virtual void getRegionToChangePoint(vector< uintm * > &res, const Address &addr, int4 num, uintm mask)=0
Grab the context blob(s) starting at the given address up to the first point of change.
TrackedContext::restoreXml
void restoreXml(const Element *el, const AddrSpaceManager *manage)
Restore this from an XML stream.
Definition: globalcontext.cc:47
ContextInternal
An in-memory implementation of the ContextDatabase interface.
Definition: globalcontext.hh:256
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
partmap.hh
The partmap<> template mapping a linear space to value objects.
ContextDatabase::getContext
virtual const uintm * getContext(const Address &addr) const =0
Get the context blob of values associated with a given address.
ContextInternal::createSet
virtual TrackedSet & createSet(const Address &addr1, const Address &addr2)
Create a tracked register set that is valid over the given range.
Definition: globalcontext.cc:482
ContextInternal::restoreXml
virtual void restoreXml(const Element *el, const AddrSpaceManager *manage)
Restore the state of this database object from a serialized XML stream.
Definition: globalcontext.cc:512
ContextDatabase::setContextRegion
void setContextRegion(const Address &addr1, const Address &addr2, int4 num, uintm mask, uintm value)
Set a context variable value over a given range of addresses.
Definition: globalcontext.cc:190
ContextDatabase::getVariable
virtual ContextBitRange & getVariable(const string &nm)=0
Retrieve the context variable description object by name.
ContextBitRange
Description of a context variable within the disassembly context blob.
Definition: globalcontext.hh:30
ContextDatabase::saveXml
virtual void saveXml(ostream &s) const =0
Serialize the entire database to an XML stream.
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
ContextDatabase::setVariableDefault
void setVariableDefault(const string &nm, uintm val)
Provide a default value for a context variable.
Definition: globalcontext.cc:109
ContextCache::setContext
void setContext(const Address &addr, int4 num, uintm mask, uintm value)
Change the value of a context variable at the given address with no bound.
Definition: globalcontext.cc:601
ContextDatabase::getTrackedValue
uintb getTrackedValue(const VarnodeData &mem, const Address &point) const
Get the value of a tracked register at a specific address.
Definition: globalcontext.cc:229
TrackedContext::loc
VarnodeData loc
Storage details of the register being tracked.
Definition: globalcontext.hh:69
TrackedSet
vector< TrackedContext > TrackedSet
A set of tracked registers and their values (at one code point)
Definition: globalcontext.hh:74
ContextDatabase::getContextSize
virtual int4 getContextSize(void) const =0
Retrieve the number of words (uintm) in a context blob.
ContextDatabase::restoreTracked
static void restoreTracked(const Element *el, const AddrSpaceManager *manage, TrackedSet &vec)
Restore a sequence of tracked register values from an XML stream.
Definition: globalcontext.cc:90
ContextDatabase::setVariableRegion
void setVariableRegion(const string &nm, const Address &begad, const Address &endad, uintm value)
Set a context variable by name over a given range of addresses.
Definition: globalcontext.cc:207
ContextDatabase::restoreXml
virtual void restoreXml(const Element *el, const AddrSpaceManager *manage)=0
Restore the state of this database object from a serialized XML stream.
ContextBitRange::getValue
uintm getValue(const uintm *vec) const
Retrieve this value from a given context blob.
Definition: globalcontext.hh:58
ContextInternal::getContext
virtual const uintm * getContext(const Address &addr) const
Get the context blob of values associated with a given address.
Definition: globalcontext.hh:292
ContextInternal::getTrackedDefault
virtual TrackedSet & getTrackedDefault(void)
Get the set of default values for all tracked registers.
Definition: globalcontext.hh:295
ContextDatabase::createSet
virtual TrackedSet & createSet(const Address &addr1, const Address &addr2)=0
Create a tracked register set that is valid over the given range.
ContextInternal::registerVariable
virtual void registerVariable(const string &nm, int4 sbit, int4 ebit)
Register a new named context variable (as a bit range) with the database.
Definition: globalcontext.cc:380
ContextCache
A helper class for caching the active context blob to minimize database lookups.
Definition: globalcontext.hh:309
ContextDatabase::getDefaultValue
virtual uintm * getDefaultValue(void)=0
Retrieve the memory region holding all default context values.
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
ContextBitRange::setValue
void setValue(uintm *vec, uintm val) const
Set this value within a given context blob.
Definition: globalcontext.hh:47
ContextDatabase
An interface to a database of disassembly/decompiler context information.
Definition: globalcontext.hh:108
TrackedContext
A tracked register (Varnode) and the value it contains.
Definition: globalcontext.hh:68
ContextCache::getContext
void getContext(const Address &addr, uintm *buf) const
Retrieve the context blob for the given address.
Definition: globalcontext.cc:582
ContextInternal::restoreFromSpec
virtual void restoreFromSpec(const Element *el, const AddrSpaceManager *manage)
Add initial context state from XML tags in compiler/processor specifications.
Definition: globalcontext.cc:539
ContextDatabase::getRegionForSet
virtual void getRegionForSet(vector< uintm * > &res, const Address &addr1, const Address &addr2, int4 num, uintm mask)=0
Grab the context blob(s) for the given address range, marking bits that will be set.
ContextDatabase::setContextChangePoint
void setContextChangePoint(const Address &addr, int4 num, uintm mask, uintm value)
Set a specific context value starting at the given address.
Definition: globalcontext.cc:167
TrackedContext::val
uintb val
The value of the register.
Definition: globalcontext.hh:70
ContextCache::ContextCache
ContextCache(ContextDatabase *db)
Construct given a context database.
Definition: globalcontext.cc:570
ContextDatabase::setVariable
void setVariable(const string &nm, const Address &addr, uintm value)
Set a context value at the given address.
Definition: globalcontext.cc:131
ContextInternal::getContextSize
virtual int4 getContextSize(void) const
Retrieve the number of words (uintm) in a context blob.
Definition: globalcontext.hh:289
TrackedContext::saveXml
void saveXml(ostream &s) const
Save this to an XML stream.
Definition: globalcontext.cc:35
ContextDatabase::restoreFromSpec
virtual void restoreFromSpec(const Element *el, const AddrSpaceManager *manage)=0
Add initial context state from XML tags in compiler/processor specifications.
ContextBitRange::ContextBitRange
ContextBitRange(int4 sbit, int4 ebit)
Construct a context value given an absolute bit range.
Definition: globalcontext.cc:23
ContextDatabase::registerVariable
virtual void registerVariable(const string &nm, int4 sbit, int4 ebit)=0
Register a new named context variable (as a bit range) with the database.
ContextDatabase::getTrackedDefault
virtual TrackedSet & getTrackedDefault(void)=0
Get the set of default values for all tracked registers.
VarnodeData
Data defining a specific memory location.
Definition: pcoderaw.hh:33
pcoderaw.hh
Raw descriptions of varnodes and p-code ops.
ContextDatabase::getTrackedSet
virtual const TrackedSet & getTrackedSet(const Address &addr) const =0
Get the set of tracked register values associated with the given address.
partmap< Address, FreeArray >
AddrSpaceManager
A manager for different address spaces.
Definition: translate.hh:218
ContextInternal::saveXml
virtual void saveXml(ostream &s) const
Serialize the entire database to an XML stream.
Definition: globalcontext.cc:490
ContextInternal::getTrackedSet
virtual const TrackedSet & getTrackedSet(const Address &addr) const
Get the set of tracked register values associated with the given address.
Definition: globalcontext.hh:296