Ghidra Decompiler Analysis Engine
condexe.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 __CONDEXE__
19 #define __CONDEXE__
20 
21 #include "funcdata.hh"
22 
34  PcodeOp *initop;
35  Varnode *basevn;
36  Varnode *boolvn;
37  Varnode *bool2vn;
38  Varnode *bool3vn;
39  PcodeOp *binaryop;
40 
41  bool matchflip;
42  int4 state;
43  PcodeOp *opstate[2];
44  bool flipstate[2];
45  int4 slotstate[2];
46  bool multion;
47  bool binon;
48  int4 multislot;
49 
50  void setupInitOp(PcodeOp *op);
51  Varnode *findMatch(PcodeOp *op);
52  bool sameOpComplement(PcodeOp *bin1op, PcodeOp *bin2op);
53  bool andOrComplement(PcodeOp *bin1op, PcodeOp *bin2op);
54  bool finalJudgement(Varnode *vn);
55 public:
56  ConditionMarker(void);
57  ~ConditionMarker(void);
58  bool verifyCondition(PcodeOp *op, PcodeOp *initop);
59  int4 getMultiSlot(void) const { return multislot; }
60  bool getFlip(void) const { return matchflip; }
61  static bool varnodeSame(Varnode *a,Varnode *b);
62  static bool varnodeComplement(Varnode *a,Varnode *b);
63 };
64 
132  Funcdata *fd;
133  PcodeOp *cbranch;
134  BlockBasic *initblock;
135  BlockBasic *iblock;
136  int4 prea_inslot;
137  bool init2a_true;
138  bool iblock2posta_true;
139  int4 camethruposta_slot;
140  int4 posta_outslot;
141  BlockBasic *posta_block;
142  BlockBasic *postb_block;
143  bool directsplit;
144  map<int4,Varnode *> replacement;
145  vector<PcodeOp *> returnop;
146  vector<bool> heritageyes;
147 
148  void buildHeritageArray(void);
149  bool testIBlock(void);
150  bool findInitPre(void);
151  bool verifySameCondition(void);
152  bool testOpRead(Varnode *vn,PcodeOp *op);
153  bool testMultiRead(Varnode *vn,PcodeOp *op);
154  bool testRemovability(PcodeOp *op);
155  void predefineDirectMulti(PcodeOp *op);
156  void adjustDirectMulti(void);
157  Varnode *getNewMulti(PcodeOp *op,BlockBasic *bl);
158  Varnode *getReplacementRead(PcodeOp *op,BlockBasic *bl);
159  void doReplacement(PcodeOp *op);
160  void fixReturnOp(void);
161  bool verify(void);
162 public:
164  bool trial(BlockBasic *ib);
165  void execute(void);
166 };
167 
172 class ActionConditionalExe : public Action {
173 public:
174  ActionConditionalExe(const string &g) : Action(0,"conditionalexe",g) {}
175  virtual Action *clone(const ActionGroupList &grouplist) const {
176  if (!grouplist.contains(getGroup())) return (Action *)0;
177  return new ActionConditionalExe(getGroup());
178  }
179  virtual int4 apply(Funcdata &data);
180 };
181 
211 class RuleOrPredicate : public Rule {
213  struct MultiPredicate {
214  PcodeOp *op;
215  int4 zeroSlot;
216  const FlowBlock *zeroBlock;
217  const FlowBlock *condBlock;
218  PcodeOp *cbranch;
219  Varnode *otherVn;
220  bool zeroPathIsTrue;
221  bool discoverZeroSlot(Varnode *vn);
222  bool discoverCbranch(void);
223  void discoverPathIsTrue(void);
224  bool discoverConditionalZero(Varnode *vn);
225  };
226  int4 checkSingle(Varnode *vn,MultiPredicate &branch,PcodeOp *op,Funcdata &data);
227 public:
228  RuleOrPredicate(const string &g) : Rule(g, 0, "orpredicate") {}
229  virtual Rule *clone(const ActionGroupList &grouplist) const {
230  if (!grouplist.contains(getGroup())) return (Rule *)0;
231  return new RuleOrPredicate(getGroup());
232  }
233  virtual void getOpList(vector<uint4> &oplist) const;
234  virtual int4 applyOp(PcodeOp *op,Funcdata &data);
235 };
236 
237 #endif
Action::Action
Action(uint4 f, const string &nm, const string &g)
Base constructor for an Action.
Definition: action.cc:25
FlowBlock
Description of a control-flow block containing PcodeOps.
Definition: block.hh:60
ActionConditionalExe
Search for and remove various forms of redundant CBRANCH operations.
Definition: condexe.hh:172
ConditionalExecution::execute
void execute(void)
Eliminate the unnecessary path join at iblock.
Definition: condexe.cc:832
Rule
Class for performing a single transformation on a PcodeOp or Varnode.
Definition: action.hh:194
ConditionMarker
A helper class for describing the similarity of the boolean condition between 2 CBRANCH operations.
Definition: condexe.hh:33
ConditionMarker::verifyCondition
bool verifyCondition(PcodeOp *op, PcodeOp *initop)
Perform the correlation test on two CBRANCH operations.
Definition: condexe.cc:341
ConditionalExecution
A class for simplifying a series of conditionally executed statements.
Definition: condexe.hh:131
ActionGroupList
The list of groups defining a root Action.
Definition: action.hh:29
ConditionMarker::ConditionMarker
ConditionMarker(void)
Constructor.
Definition: condexe.cc:18
ActionConditionalExe::clone
virtual Action * clone(const ActionGroupList &grouplist) const
Clone the Action.
Definition: condexe.hh:175
ConditionalExecution::ConditionalExecution
ConditionalExecution(Funcdata *f)
Constructor.
Definition: condexe.cc:764
Action
Large scale transformations applied to the varnode/op graph.
Definition: action.hh:50
BlockBasic
A basic block for p-code operations.
Definition: block.hh:365
PcodeOp
Lowest level operation of the p-code language.
Definition: op.hh:58
RuleOrPredicate::clone
virtual Rule * clone(const ActionGroupList &grouplist) const
Clone the Rule.
Definition: condexe.hh:229
Varnode
A low-level variable or contiguous set of bytes described by an Address and a size.
Definition: varnode.hh:65
funcdata.hh
Utilities for processing data structures associated with a single function.
ActionGroupList::contains
bool contains(const string &nm) const
Check if this ActionGroupList contains a given group.
Definition: action.hh:37
Funcdata
Container for data structures associated with a single function.
Definition: funcdata.hh:45
ConditionalExecution::trial
bool trial(BlockBasic *ib)
Test for a modifiable configuration around the given block.
Definition: condexe.cc:780
ConditionMarker::varnodeSame
static bool varnodeSame(Varnode *a, Varnode *b)
Do the given Varnodes hold the same value, possibly as constants.
Definition: condexe.cc:178
ActionConditionalExe::apply
virtual int4 apply(Funcdata &data)
Make a single attempt to apply this Action.
Definition: condexe.cc:864
RuleOrPredicate::getOpList
virtual void getOpList(vector< uint4 > &oplist) const
List of op codes this rule operates on.
Definition: condexe.cc:1003
RuleOrPredicate::applyOp
virtual int4 applyOp(PcodeOp *op, Funcdata &data)
Attempt to apply this Rule.
Definition: condexe.cc:1040
Rule::Rule
Rule(const string &g, uint4 fl, const string &nm)
Construct given group, properties name.
Definition: action.cc:620
ConditionMarker::varnodeComplement
static bool varnodeComplement(Varnode *a, Varnode *b)
Do the given boolean Varnodes always hold complementary values.
Definition: condexe.cc:193
RuleOrPredicate
Simplify predication constructions involving the INT_OR operator.
Definition: condexe.hh:211
ConditionMarker::~ConditionMarker
~ConditionMarker(void)
Destructor.
Definition: condexe.cc:30