Ghidra Decompiler Analysis Engine
varmap.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_VARMAP__
20 #define __CPUI_VARMAP__
21 
22 #include "database.hh"
23 
30  Address addr;
31  Address useaddr;
32  int4 size;
33  string name;
34  uint8 symbolId;
35 public:
36  NameRecommend(const Address &ad,const Address &use,int4 sz,const string &nm,uint8 id) :
37  addr(ad), useaddr(use), size(sz), name(nm), symbolId(id) {}
38  const Address &getAddr(void) const { return addr; }
39  const Address &getUseAddr(void) const { return useaddr; }
40  int4 getSize(void) const { return size; }
41  string getName(void) const { return name; }
42  uint8 getSymbolId(void) const { return symbolId; }
43 };
44 
50  Address usePoint;
51  uint8 hash;
52  string name;
53  uint8 symbolId;
54 public:
55  DynamicRecommend(const Address &addr,uint8 h,const string &nm,uint8 id) :
56  usePoint(addr), hash(h), name(nm), symbolId(id) {}
57  const Address &getAddress(void) const { return usePoint; }
58  uint8 getHash(void) const { return hash; }
59  string getName(void) const { return name; }
60  uint8 getSymbolId(void) const { return symbolId; }
61 };
62 
68  Address addr;
69  Datatype *dataType;
70 public:
71  TypeRecommend(const Address &ad,Datatype *dt) :
72  addr(ad), dataType(dt) {}
73  const Address &getAddress(void) const { return addr; }
74  Datatype *getType(void) const { return dataType; }
75 };
76 
83 class RangeHint {
84  friend class MapState;
85  friend class ScopeLocal;
86 public:
88  enum RangeType {
89  fixed = 0,
90  open = 1,
91  endpoint = 2
92  };
93 private:
94  uintb start;
95  int4 size;
96  intb sstart;
97  Datatype *type;
98  uint4 flags;
99  RangeType rangeType;
100  int4 highind;
101 public:
102  RangeHint(void) {}
103  RangeHint(uintb st,int4 sz,intb sst,Datatype *ct,uint4 fl,RangeType rt,int4 hi) {
104  start=st; size=sz; sstart=sst; type=ct; flags=fl; rangeType = rt; highind=hi; }
105  bool reconcile(const RangeHint *b) const;
106  bool contain(const RangeHint *b) const;
107  bool preferred(const RangeHint *b,bool reconcile) const;
108  bool absorb(RangeHint *b);
109  bool merge(RangeHint *b,AddrSpace *space,TypeFactory *typeFactory);
110  int4 compare(const RangeHint &op2) const;
111  static bool compareRanges(const RangeHint *a,const RangeHint *b) { return (a->compare(*b) < 0); }
112 };
113 
114 class ProtoModel;
115 class LoadGuard;
116 
123 public:
125  struct AddBase {
128  AddBase(Varnode *b,Varnode *i) { base=b; index=i; }
129  };
130 private:
131  const Funcdata *fd;
132  AddrSpace *space;
133  mutable vector<AddBase> addBase;
134  mutable vector<uintb> alias;
135  mutable bool calculated;
136  uintb localExtreme;
137  uintb localBoundary;
138  mutable uintb aliasBoundary;
139  int4 direction;
140  void deriveBoundaries(const FuncProto &proto);
141  void gatherInternal(void) const;
142 public:
143  AliasChecker() { fd = (const Funcdata *)0; space = (AddrSpace *)0; calculated=false; }
144  void gather(const Funcdata *f,AddrSpace *spc,bool defer);
145  bool hasLocalAlias(Varnode *vn) const;
146  void sortAlias(void) const;
147  const vector<AddBase> &getAddBase(void) const { return addBase; }
148  const vector<uintb> &getAlias(void) const { return alias; }
149  static void gatherAdditiveBase(Varnode *startvn,vector<AddBase> &addbase);
150  static uintb gatherOffset(Varnode *vn);
151 };
152 
159 class MapState {
160  AddrSpace *spaceid;
161  RangeList range;
162  vector<RangeHint *> maplist;
163  vector<RangeHint *>::iterator iter;
164  Datatype *defaultType;
165  AliasChecker checker;
166  void addGuard(const LoadGuard &guard,OpCode opc,TypeFactory *typeFactory);
167  void addRange(uintb st,Datatype *ct,uint4 fl,RangeHint::RangeType rt,int4 hi);
168  void reconcileDatatypes(void);
169 public:
170 #ifdef OPACTION_DEBUG
171  mutable bool debugon;
172  mutable Architecture *glb;
173  void turnOnDebug(Architecture *g) const { debugon = true; glb=g; }
174  void turnOffDebug(void) const { debugon = false; }
175 #endif
176  MapState(AddrSpace *spc,const RangeList &rn,const RangeList &pm,Datatype *dt);
177  ~MapState(void);
178  bool initialize(void);
179  void sortAlias(void) { checker.sortAlias(); }
180  const vector<uintb> &getAlias(void) { return checker.getAlias(); }
181  void gatherSymbols(const EntryMap *rangemap);
182  void gatherVarnodes(const Funcdata &fd);
183  void gatherHighs(const Funcdata &fd);
184  void gatherOpen(const Funcdata &fd);
185  RangeHint *next(void) { return *iter; }
186  bool getNext(void) { ++iter; if (iter==maplist.end()) return false; return true; }
187 };
188 
196 class ScopeLocal : public ScopeInternal {
197  AddrSpace *space;
198  RangeList localRange;
199  list<NameRecommend> nameRecommend;
200  list<DynamicRecommend> dynRecommend;
201  list<TypeRecommend> typeRecommend;
202  bool stackGrowsNegative;
203  bool rangeLocked;
204  bool adjustFit(RangeHint &a) const;
205  void createEntry(const RangeHint &a);
206  bool restructure(MapState &state);
207  void markUnaliased(const vector<uintb> &alias);
208  void fakeInputSymbols(void);
209  void addRecommendName(Symbol *sym);
210  void collectNameRecs(void);
211 public:
212  ScopeLocal(uint8 id,AddrSpace *spc,Funcdata *fd,Architecture *g);
213  virtual ~ScopeLocal(void) {}
214 
215  AddrSpace *getSpaceId(void) const { return space; }
216 
221  bool isUnaffectedStorage(Varnode *vn) const { return (vn->getSpace() == space); }
222 
223  void markNotMapped(AddrSpace *spc,uintb first,int4 sz,bool param);
224 
225  // Routines that are specific to one address space
226  virtual void saveXml(ostream &s) const;
227  virtual void restoreXml(const Element *el);
228  virtual string buildVariableName(const Address &addr,
229  const Address &pc,
230  Datatype *ct,
231  int4 &index,uint4 flags) const;
232  void resetLocalWindow(void);
233  void restructureVarnode(bool aliasyes);
234  void restructureHigh(void);
235  SymbolEntry *remapSymbol(Symbol *sym,const Address &addr,const Address &usepoint);
236  SymbolEntry *remapSymbolDynamic(Symbol *sym,uint8 hash,const Address &usepoint);
238  void applyTypeRecommendations(void);
239 };
240 
241 #endif
AliasChecker::hasLocalAlias
bool hasLocalAlias(Varnode *vn) const
Return true if it looks like the given Varnode is aliased by a pointer.
Definition: varmap.cc:565
RangeHint::fixed
@ fixed
A data-type with a fixed size.
Definition: varmap.hh:89
TypeRecommend
Data-type for a storage location when there is no Symbol (yet)
Definition: varmap.hh:67
AliasChecker::gatherAdditiveBase
static void gatherAdditiveBase(Varnode *startvn, vector< AddBase > &addbase)
Gather result Varnodes for all sums that the given starting Varnode is involved in.
Definition: varmap.cc:595
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
AliasChecker::sortAlias
void sortAlias(void) const
Sort the alias starting offsets.
Definition: varmap.cc:580
RangeHint::compare
int4 compare(const RangeHint &op2) const
Order this with another RangeHint.
Definition: varmap.cc:247
ScopeLocal
A Symbol scope for local variables of a particular function.
Definition: varmap.hh:196
ScopeInternal
An in-memory implementation of the Scope interface.
Definition: database.hh:726
ScopeLocal::restructureVarnode
void restructureVarnode(bool aliasyes)
Layout mapped symbols based on Varnode information.
Definition: varmap.cc:997
rangemap
An interval map container.
Definition: rangemap.hh:64
RangeHint::reconcile
bool reconcile(const RangeHint *b) const
Can the given intersecting RangeHint coexist with this at their given offsets.
Definition: varmap.cc:26
MapState::gatherVarnodes
void gatherVarnodes(const Funcdata &fd)
Add stack Varnodes as hints to the collection.
Definition: varmap.cc:901
Scope::fd
Funcdata * fd
(If non-null) the function which this is the local Scope for
Definition: database.hh:415
RangeHint::endpoint
@ endpoint
An (artificial) boundary to the range of bytes getting analyzed.
Definition: varmap.hh:91
ScopeLocal::ScopeLocal
ScopeLocal(uint8 id, AddrSpace *spc, Funcdata *fd, Architecture *g)
Constructor.
Definition: varmap.cc:269
ScopeLocal::markNotMapped
void markNotMapped(AddrSpace *spc, uintb first, int4 sz, bool param)
Mark a specific address range is not mapped.
Definition: varmap.cc:366
MapState::gatherOpen
void gatherOpen(const Funcdata &fd)
Add pointer references as hints to the collection.
Definition: varmap.cc:952
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
Architecture
Manager for all the major decompiler subsystems.
Definition: architecture.hh:119
SymbolEntry
A storage location for a particular Symbol.
Definition: database.hh:51
LoadGuard
Description of a LOAD operation that needs to be guarded.
Definition: heritage.hh:105
ProtoModel
A prototype model: a model for passing parameters between functions.
Definition: fspec.hh:622
MapState
A container for hints about the data-type layout of an address space.
Definition: varmap.hh:159
ScopeLocal::buildVariableName
virtual string buildVariableName(const Address &addr, const Address &pc, Datatype *ct, int4 &index, uint4 flags) const
Given an address and data-type, build a suitable generic symbol name.
Definition: varmap.cc:408
RangeHint::absorb
bool absorb(RangeHint *b)
Try to absorb the other RangeHint into this.
Definition: varmap.cc:107
MapState::MapState
MapState(AddrSpace *spc, const RangeList &rn, const RangeList &pm, Datatype *dt)
Constructor.
Definition: varmap.cc:715
Varnode
A low-level variable or contiguous set of bytes described by an Address and a size.
Definition: varnode.hh:65
RangeList
A disjoint set of Ranges, possibly across multiple address spaces.
Definition: address.hh:203
AliasChecker::AddBase
A helper class holding a Varnode pointer reference and a possible index added to it.
Definition: varmap.hh:125
AliasChecker::gatherOffset
static uintb gatherOffset(Varnode *vn)
If the given Varnode is a sum result, return the constant portion of this sum.
Definition: varmap.cc:671
RangeHint::RangeType
RangeType
The basic categorization of the range.
Definition: varmap.hh:88
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
ScopeLocal::remapSymbolDynamic
SymbolEntry * remapSymbolDynamic(Symbol *sym, uint8 hash, const Address &usepoint)
Make the primary mapping for the given Symbol, dynamic.
Definition: varmap.cc:1234
Funcdata
Container for data structures associated with a single function.
Definition: funcdata.hh:45
Datatype
The base datatype class for the decompiler.
Definition: type.hh:62
MapState::gatherHighs
void gatherHighs(const Funcdata &fd)
Add HighVariables as hints to the collection.
Definition: varmap.cc:923
TypeFactory
Container class for all Datatype objects in an Architecture.
Definition: type.hh:396
ScopeLocal::isUnaffectedStorage
bool isUnaffectedStorage(Varnode *vn) const
Is this a storage location for unaffected registers.
Definition: varmap.hh:221
NameRecommend
A symbol name recommendation with its associated storage location.
Definition: varmap.hh:29
database.hh
Symbol and Scope objects for the decompiler.
DynamicRecommend
A name recommendation for a particular dynamic storage location.
Definition: varmap.hh:49
ScopeLocal::applyTypeRecommendations
void applyTypeRecommendations(void)
Try to apply recommended data-type information.
Definition: varmap.cc:1323
OpCode
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
ScopeLocal::remapSymbol
SymbolEntry * remapSymbol(Symbol *sym, const Address &addr, const Address &usepoint)
Change the primary mapping for the given Symbol to be a specific storage address and use point.
Definition: varmap.cc:1206
ScopeLocal::restructureHigh
void restructureHigh(void)
Layout mapped symbols based on HighVariable information.
Definition: varmap.cc:1028
ScopeLocal::restoreXml
virtual void restoreXml(const Element *el)
Restore this Scope from a <scope> XML tag.
Definition: varmap.cc:348
ScopeLocal::saveXml
virtual void saveXml(ostream &s) const
Write out this as a <scope> XML tag.
Definition: varmap.cc:337
MapState::~MapState
~MapState(void)
Destructor.
Definition: varmap.cc:732
RangeHint
Partial data-type information mapped to a specific range of bytes.
Definition: varmap.hh:83
RangeHint::merge
bool merge(RangeHint *b, AddrSpace *space, TypeFactory *typeFactory)
Try to form the union of this with another RangeHint.
Definition: varmap.cc:160
RangeHint::open
@ open
An array with a (possibly unknown) number of elements.
Definition: varmap.hh:90
ScopeLocal::recoverNameRecommendationsForSymbols
void recoverNameRecommendationsForSymbols(void)
Run through name recommendations, checking if any match unnamed symbols.
Definition: varmap.cc:1256
RangeHint::contain
bool contain(const RangeHint *b) const
Return true if this or the given range contains the other.
Definition: varmap.cc:56
MapState::initialize
bool initialize(void)
Initialize the hint collection for iteration.
Definition: varmap.cc:876
MapState::gatherSymbols
void gatherSymbols(const EntryMap *rangemap)
Add Symbol information as hints to the collection.
Definition: varmap.cc:858
ScopeLocal::resetLocalWindow
void resetLocalWindow(void)
Reset the set of addresses that are considered mapped by the scope to the default.
Definition: varmap.cc:310
AliasChecker::gather
void gather(const Funcdata *f, AddrSpace *spc, bool defer)
Gather Varnodes that point on the stack.
Definition: varmap.cc:546
AliasChecker::AddBase::base
Varnode * base
The Varnode holding the base pointer.
Definition: varmap.hh:126
RangeHint::preferred
bool preferred(const RangeHint *b, bool reconcile) const
Return true if the this range's data-type is preferred over the other given range.
Definition: varmap.cc:73
FuncProto
A function prototype.
Definition: fspec.hh:1164
AliasChecker
A light-weight class for analyzing pointers and aliasing on the stack.
Definition: varmap.hh:122
AliasChecker::AddBase::index
Varnode * index
The index value or NULL.
Definition: varmap.hh:127
Symbol
The base class for a symbol in a symbol table or scope.
Definition: database.hh:152