Ghidra Decompiler Analysis Engine
variable.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_TYPEVAR__
20 #define __CPUI_TYPEVAR__
21 
22 #include "varnode.hh"
23 
24 class Symbol;
25 
38 class HighVariable {
39 public:
45  enum {
46  flagsdirty = 1,
48  typedirty = 4,
49  coverdirty = 8,
50  symboldirty = 0x10,
51  copy_in1 = 0x20,
52  copy_in2 = 0x40,
53  type_finalized = 0x80,
54  unmerged = 0x100
55  };
56 private:
57  friend class Varnode;
58  friend class Merge;
59  vector<Varnode *> inst;
60  int4 numMergeClasses;
61  mutable uint4 highflags;
62  mutable uint4 flags;
63  mutable Datatype *type;
64  mutable Varnode *nameRepresentative;
65  mutable Cover wholecover;
66  mutable Symbol *symbol;
67  mutable int4 symboloffset;
68  int4 instanceIndex(const Varnode *vn) const;
69  void updateFlags(void) const;
70  void updateCover(void) const;
71  void updateType(void) const;
72  void updateSymbol(void) const;
73  void setCopyIn1(void) const { highflags |= copy_in1; }
74  void setCopyIn2(void) const { highflags |= copy_in2; }
75  void clearCopyIns(void) const { highflags &= ~(copy_in1 | copy_in2); }
76  bool hasCopyIn1(void) const { return ((highflags&copy_in1)!=0); }
77  bool hasCopyIn2(void) const { return ((highflags&copy_in2)!=0); }
78  void remove(Varnode *vn);
79  void merge(HighVariable *tv2,bool isspeculative);
80  void setSymbol(Varnode *vn) const;
81  void setSymbolReference(Symbol *sym,int4 off);
82  void flagsDirty(void) const { highflags |= flagsdirty | namerepdirty; }
83  void coverDirty(void) const { highflags |= coverdirty; }
84  void typeDirty(void) const { highflags |= typedirty; }
85  void setUnmerged(void) const { highflags |= unmerged; }
86 public:
87  HighVariable(Varnode *vn);
88  Datatype *getType(void) const { updateType(); return type; }
89  Symbol *getSymbol(void) const { updateSymbol(); return symbol; }
90  SymbolEntry *getSymbolEntry(void) const;
91  int4 getSymbolOffset(void) const { return symboloffset; }
92  int4 numInstances(void) const { return inst.size(); }
93  Varnode *getInstance(int4 i) const { return inst[i]; }
94  void finalizeDatatype(Datatype *tp);
95 
99  void printCover(ostream &s) const { if ((highflags&HighVariable::coverdirty)==0) wholecover.print(s); else s << "Cover dirty"; }
100 
101  void printInfo(ostream &s) const;
102  bool hasName(void) const;
103  Varnode *getTiedVarnode(void) const;
104  Varnode *getInputVarnode(void) const;
105  Varnode *getTypeRepresentative(void) const;
106  Varnode *getNameRepresentative(void) const;
107  int4 getNumMergeClasses(void) const { return numMergeClasses; }
108  bool isMapped(void) const { updateFlags(); return ((flags&Varnode::mapped)!=0); }
109  bool isPersist(void) const { updateFlags(); return ((flags&Varnode::persist)!=0); }
110  bool isAddrTied(void) const { updateFlags(); return ((flags&Varnode::addrtied)!=0); }
111  bool isInput(void) const { updateFlags(); return ((flags&Varnode::input)!=0); }
112  bool isImplied(void) const { updateFlags(); return ((flags&Varnode::implied)!=0); }
113  bool isSpacebase(void) const { updateFlags(); return ((flags&Varnode::spacebase)!=0); }
114  bool isConstant(void) const { updateFlags(); return ((flags&Varnode::constant)!=0); }
115  bool isUnaffected(void) const { updateFlags(); return ((flags&Varnode::unaffected)!=0); }
116  bool isExtraOut(void) const { updateFlags(); return ((flags&(Varnode::indirect_creation|Varnode::addrtied))==Varnode::indirect_creation); }
117  void setMark(void) const { flags |= Varnode::mark; }
118  void clearMark(void) const { flags &= ~Varnode::mark; }
119  bool isMark(void) const { return ((flags&Varnode::mark)!=0); }
120  bool isUnmerged(void) const { return ((highflags&unmerged)!=0); }
121 
126  bool hasCover(void) const {
127  updateFlags();
129 
130  bool isUnattached(void) const { return inst.empty(); }
131  bool isTypeLock(void) const { updateType(); return ((flags & Varnode::typelock)!=0); }
132  bool isNameLock(void) const { updateFlags(); return ((flags & Varnode::namelock)!=0); }
133  void saveXml(ostream &s) const;
134 #ifdef MERGEMULTI_DEBUG
135  void verifyCover(void) const;
136 #endif
137  // Varnode *findGlobalRep(void) const;
138  static bool compareName(Varnode *vn1,Varnode *vn2);
139  static bool compareJustLoc(const Varnode *a,const Varnode *b);
140  static int4 markExpression(Varnode *vn,vector<HighVariable *> &highList);
141 };
142 
143 #endif
HighVariable::printCover
void printCover(ostream &s) const
Print details of the cover for this (for debug purposes)
Definition: variable.hh:99
Varnode::namelock
@ namelock
The Name of the Varnode is locked.
Definition: varnode.hh:83
HighVariable::compareJustLoc
static bool compareJustLoc(const Varnode *a, const Varnode *b)
Compare based on storage location.
Definition: variable.cc:184
Varnode::persist
@ persist
Persists after (and before) function.
Definition: varnode.hh:89
HighVariable::coverdirty
@ coverdirty
The cover for the HighVariable is dirty.
Definition: variable.hh:49
Varnode::insert
@ insert
Definition: varnode.hh:76
HighVariable::printInfo
void printInfo(ostream &s) const
Print information about this HighVariable to stream.
Definition: variable.cc:410
Varnode::mark
@ mark
Prevents infinite loops.
Definition: varnode.hh:71
HighVariable::copy_in1
@ copy_in1
There exists at least 1 COPY into this HighVariable from other HighVariables.
Definition: variable.hh:51
HighVariable::getSymbolEntry
SymbolEntry * getSymbolEntry(void) const
Definition: variable.cc:278
Varnode::implied
@ implied
This varnode is a temporary variable.
Definition: varnode.hh:79
Merge
Class for merging low-level Varnodes into high-level HighVariables.
Definition: merge.hh:80
HighVariable::saveXml
void saveXml(ostream &s) const
Save the variable to stream as an XML <high> tag.
Definition: variable.cc:452
HighVariable::copy_in2
@ copy_in2
There exists at least 2 COPYs into this HighVariable from other HighVariables.
Definition: variable.hh:52
Varnode::typelock
@ typelock
The Dataype of the Varnode is locked.
Definition: varnode.hh:82
Varnode::unaffected
@ unaffected
Input which is unaffected by the function.
Definition: varnode.hh:91
SymbolEntry
A storage location for a particular Symbol.
Definition: database.hh:51
Cover::print
void print(ostream &s) const
Dump a description of this cover to stream.
Definition: cover.cc:554
Varnode::spacebase
@ spacebase
This is a base register for an address space.
Definition: varnode.hh:92
HighVariable::hasCover
bool hasCover(void) const
Determine if this HighVariable has an associated cover.
Definition: variable.hh:126
HighVariable::flagsdirty
@ flagsdirty
Boolean properties for the HighVariable are dirty.
Definition: variable.hh:46
HighVariable::hasName
bool hasName(void) const
Check if this HighVariable can be named.
Definition: variable.cc:350
HighVariable::unmerged
@ unmerged
Set if part of a multi-entry Symbol but did not get merged with other SymbolEntrys.
Definition: variable.hh:54
Varnode
A low-level variable or contiguous set of bytes described by an Address and a size.
Definition: varnode.hh:65
Varnode::mapped
@ mapped
Varnode has a database entry associated with it.
Definition: varnode.hh:97
HighVariable::namerepdirty
@ namerepdirty
The name representative for the HighVariable is dirty.
Definition: variable.hh:47
Varnode::annotation
@ annotation
This varnode is an annotation and has no dataflow.
Definition: varnode.hh:73
Cover
A description of the topological scope of a single variable object.
Definition: cover.hh:68
Datatype
The base datatype class for the decompiler.
Definition: type.hh:62
Varnode::input
@ input
This varnode has no ancestor.
Definition: varnode.hh:74
HighVariable::getTiedVarnode
Varnode * getTiedVarnode(void) const
Find the first address tied member Varnode.
Definition: variable.cc:384
HighVariable::type_finalized
@ type_finalized
Set if a final data-type is locked in and dirtying is disabled.
Definition: variable.hh:53
HighVariable::getInputVarnode
Varnode * getInputVarnode(void) const
Find (the) input member Varnode.
Definition: variable.cc:399
Varnode::constant
@ constant
The varnode is constant.
Definition: varnode.hh:72
HighVariable::typedirty
@ typedirty
The data-type for the HighVariable is dirty.
Definition: variable.hh:48
HighVariable
A high-level variable modeled as a list of low-level variables, each written once.
Definition: variable.hh:38
HighVariable::finalizeDatatype
void finalizeDatatype(Datatype *tp)
Set a final datatype for this variable.
Definition: variable.cc:292
HighVariable::HighVariable
HighVariable(Varnode *vn)
Construct a HighVariable with a single member Varnode.
Definition: variable.cc:22
HighVariable::symboldirty
@ symboldirty
The symbol attachment is dirty.
Definition: variable.hh:50
HighVariable::compareName
static bool compareName(Varnode *vn1, Varnode *vn2)
Determine which given Varnode is most nameable.
Definition: variable.cc:201
HighVariable::getTypeRepresentative
Varnode * getTypeRepresentative(void) const
Get a member Varnode with the strongest data-type.
Definition: variable.cc:122
HighVariable::markExpression
static int4 markExpression(Varnode *vn, vector< HighVariable * > &highList)
Mark and collect variables in expression.
Definition: variable.cc:504
Symbol
The base class for a symbol in a symbol table or scope.
Definition: database.hh:152
varnode.hh
The Varnode and VarnodeBank classes.
Varnode::indirect_creation
@ indirect_creation
The value in this Varnode is created indirectly.
Definition: varnode.hh:98
Varnode::addrtied
@ addrtied
High-level variable is tied to address.
Definition: varnode.hh:90
HighVariable::getNameRepresentative
Varnode * getNameRepresentative(void) const
Get a member Varnode that dictates the naming of this HighVariable.
Definition: variable.cc:235