Ghidra Decompiler Analysis Engine
subflow.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 #ifndef __SUBVARIABLE_FLOW__
19 #define __SUBVARIABLE_FLOW__
20 
21 #include "funcdata.hh"
22 
40  class ReplaceOp;
42  class ReplaceVarnode {
43  friend class SubvariableFlow;
44  Varnode *vn;
45  Varnode *replacement;
46  uintb mask;
47  uintb val;
48  ReplaceOp *def;
49  };
50 
52  class ReplaceOp {
53  friend class SubvariableFlow;
54  PcodeOp *op;
55  PcodeOp *replacement;
56  OpCode opc;
57  int4 numparams;
58  ReplaceVarnode *output;
59  vector<ReplaceVarnode *> input;
60  };
61 
63  class PatchRecord {
64  friend class SubvariableFlow;
66  enum patchtype {
67  copy_patch,
68  compare_patch,
69  parameter_patch,
70  extension_patch,
71  push_patch
72  };
73  patchtype type;
74  PcodeOp *patchOp;
75  ReplaceVarnode *in1;
76  ReplaceVarnode *in2;
77  int4 slot;
78  };
79 
80  int4 flowsize;
81  int4 bitsize;
82  bool returnsTraversed;
83  bool aggressive;
84  bool sextrestrictions;
85  Funcdata *fd;
86  map<Varnode *,ReplaceVarnode> varmap;
87  list<ReplaceVarnode> newvarlist;
88  list<ReplaceOp> oplist;
89  list<PatchRecord> patchlist;
90  vector<ReplaceVarnode *> worklist;
91  int4 pullcount;
92  static int4 doesOrSet(PcodeOp *orop,uintb mask);
93  static int4 doesAndClear(PcodeOp *andop,uintb mask);
94  Address getReplacementAddress(ReplaceVarnode *rvn) const;
95  ReplaceVarnode *setReplacement(Varnode *vn,uintb mask,bool &inworklist);
96  ReplaceOp *createOp(OpCode opc,int4 numparam,ReplaceVarnode *outrvn);
97  ReplaceOp *createOpDown(OpCode opc,int4 numparam,PcodeOp *op,ReplaceVarnode *inrvn,int4 slot);
98  bool tryCallPull(PcodeOp *op,ReplaceVarnode *rvn,int4 slot);
99  bool tryReturnPull(PcodeOp *op,ReplaceVarnode *rvn,int4 slot);
100  bool tryCallReturnPush(PcodeOp *op,ReplaceVarnode *rvn);
101  bool trySwitchPull(PcodeOp *op,ReplaceVarnode *rvn);
102  bool traceForward(ReplaceVarnode *rvn);
103  bool traceBackward(ReplaceVarnode *rvn);
104  bool traceForwardSext(ReplaceVarnode *rvn);
105  bool traceBackwardSext(ReplaceVarnode *rvn);
106  bool createLink(ReplaceOp *rop,uintb mask,int4 slot,Varnode *vn);
107  bool createCompareBridge(PcodeOp *op,ReplaceVarnode *inrvn,int4 slot,Varnode *othervn);
108  void addPush(PcodeOp *pushOp,ReplaceVarnode *rvn);
109  void addTerminalPatch(PcodeOp *pullop,ReplaceVarnode *rvn);
110  void addTerminalPatchSameOp(PcodeOp *pullop,ReplaceVarnode *rvn,int4 slot);
111  void addBooleanPatch(PcodeOp *pullop,ReplaceVarnode *rvn,int4 slot);
112  void addSuggestedPatch(ReplaceVarnode *rvn,PcodeOp *pushop,int4 sa);
113  void addComparePatch(ReplaceVarnode *in1,ReplaceVarnode *in2,PcodeOp *op);
114  ReplaceVarnode *addConstant(ReplaceOp *rop,uintb mask,uint4 slot,uintb val);
115  void createNewOut(ReplaceOp *rop,uintb mask);
116  void replaceInput(ReplaceVarnode *rvn);
117  bool useSameAddress(ReplaceVarnode *rvn);
118  Varnode *getReplaceVarnode(ReplaceVarnode *rvn);
119  bool processNextWork(void);
120 public:
121  SubvariableFlow(Funcdata *f,Varnode *root,uintb mask,bool aggr,bool sext,bool big);
122  bool doTrace(void);
123  void doReplacement(void);
124 };
125 
132 class SplitFlow : public TransformManager {
133  LaneDescription laneDescription;
134  vector<TransformVar *> worklist;
135  TransformVar *setReplacement(Varnode *vn);
136  bool addOp(PcodeOp *op,TransformVar *rvn,int4 slot);
137  bool traceForward(TransformVar *rvn);
138  bool traceBackward(TransformVar *rvn);
139  bool processNextWork(void);
140 public:
141  SplitFlow(Funcdata *f,Varnode *root,int4 lowSize);
142  bool doTrace(void);
143 };
144 
151  int4 precision;
152  int4 terminatorCount;
153  const FloatFormat *format;
154  vector<TransformVar *> worklist;
155  TransformVar *setReplacement(Varnode *vn);
156  bool traceForward(TransformVar *rvn);
157  bool traceBackward(TransformVar *rvn);
158  bool processNextWork(void);
159 public:
160  SubfloatFlow(Funcdata *f,Varnode *root,int4 prec);
161  virtual bool preserveAddress(Varnode *vn,int4 bitSize,int4 lsbOffset) const;
162  bool doTrace(void);
163 };
164 
171 class LaneDivide : public TransformManager {
173  class WorkNode {
174  friend class LaneDivide;
175  Varnode *vn;
176  TransformVar *lanes;
177  int4 numLanes;
178  int4 skipLanes;
179  };
180 
181  LaneDescription description;
182  vector<WorkNode> workList;
183  bool allowSubpieceTerminator;
184 
185  TransformVar *setReplacement(Varnode *vn,int4 numLanes,int4 skipLanes);
186  void buildUnaryOp(OpCode opc,PcodeOp *op,TransformVar *inVars,TransformVar *outVars,int4 numLanes);
187  void buildBinaryOp(OpCode opc,PcodeOp *op,TransformVar *in0Vars,TransformVar *in1Vars,TransformVar *outVars,int4 numLanes);
188  bool buildPiece(PcodeOp *op,TransformVar *outVars,int4 numLanes,int4 skipLanes);
189  bool buildMultiequal(PcodeOp *op,TransformVar *outVars,int4 numLanes,int4 skipLanes);
190  bool buildStore(PcodeOp *op,int4 numLanes,int4 skipLanes);
191  bool buildLoad(PcodeOp *op,TransformVar *outVars,int4 numLanes,int4 skipLanes);
192  bool buildRightShift(PcodeOp *op,TransformVar *outVars,int4 numLanes,int4 skipLanes);
193  bool traceForward(TransformVar *rvn,int4 numLanes,int4 skipLanes);
194  bool traceBackward(TransformVar *rvn,int4 numLanes,int4 skipLanes);
195  bool processNextWork(void);
196 public:
197  LaneDivide(Funcdata *f,Varnode *root,const LaneDescription &desc,bool allowDowncast);
198  bool doTrace(void);
199 };
200 
201 #endif
FloatFormat
Encoding information for a single floating-point format.
Definition: float.hh:30
LaneDescription
Description of logical lanes within a big Varnode.
Definition: transform.hh:126
SubvariableFlow::doReplacement
void doReplacement(void)
Perform the discovered transform, making logical values explicit.
Definition: subflow.cc:1327
TransformVar
Placeholder node for Varnode that will exist after a transform is applied to a function.
Definition: transform.hh:26
SubfloatFlow
Class for tracing changes of precision in floating point variables.
Definition: subflow.hh:150
SplitFlow::doTrace
bool doTrace(void)
Trace split through data-flow, constructing transform.
Definition: subflow.cc:1696
PcodeOp
Lowest level operation of the p-code language.
Definition: op.hh:58
SubvariableFlow::doTrace
bool doTrace(void)
Trace logical value through data-flow, constructing transform.
Definition: subflow.cc:1302
SubvariableFlow
Class for shrinking big Varnodes carrying smaller logical values.
Definition: subflow.hh:39
SplitFlow::SplitFlow
SplitFlow(Funcdata *f, Varnode *root, int4 lowSize)
Constructor.
Definition: subflow.cc:1686
Varnode
A low-level variable or contiguous set of bytes described by an Address and a size.
Definition: varnode.hh:65
TransformManager
Class for splitting larger registers holding smaller logical lanes.
Definition: transform.hh:150
funcdata.hh
Utilities for processing data structures associated with a single function.
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
Funcdata
Container for data structures associated with a single function.
Definition: funcdata.hh:45
LaneDivide
Class for splitting data-flow on laned registers.
Definition: subflow.hh:171
OpCode
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
SubvariableFlow::SubvariableFlow
SubvariableFlow(Funcdata *f, Varnode *root, uintb mask, bool aggr, bool sext, bool big)
Constructor.
Definition: subflow.cc:1264
LaneDivide::LaneDivide
LaneDivide(Funcdata *f, Varnode *root, const LaneDescription &desc, bool allowDowncast)
Constructor.
Definition: subflow.cc:2469
SubfloatFlow::preserveAddress
virtual bool preserveAddress(Varnode *vn, int4 bitSize, int4 lsbOffset) const
Should the address of the given Varnode be preserved when constructing a piece.
Definition: subflow.cc:1962
SubfloatFlow::doTrace
bool doTrace(void)
Trace logical value as far as possible.
Definition: subflow.cc:1973
SplitFlow
Class for splitting up Varnodes that hold 2 logical variables.
Definition: subflow.hh:132
LaneDivide::doTrace
bool doTrace(void)
Trace lanes as far as possible from the root Varnode.
Definition: subflow.cc:2479
SubfloatFlow::SubfloatFlow
SubfloatFlow(Funcdata *f, Varnode *root, int4 prec)
Definition: subflow.cc:1952