Ghidra Decompiler Analysis Engine
action.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 __CPUI_ACTION__
19 #define __CPUI_ACTION__
20 
21 #include "block.hh"
22 
30  friend class ActionDatabase;
31  set<string> list;
32 public:
37  bool contains(const string &nm) const { return (list.find(nm)!=list.end()); }
38 };
39 
40 class Rule;
41 
50 class Action {
51 public:
53  enum ruleflags {
57  rule_debug = 32,
60  };
62  enum statusflags {
69  };
71  enum breakflags {
75  tmpbreak_action = 8
76  };
77 protected:
78  int4 lcount;
79  int4 count;
80  uint4 status;
81  uint4 breakpoint;
82  uint4 flags;
83  uint4 count_tests;
84  uint4 count_apply;
85  string name;
86  string basegroup;
87  void issueWarning(Architecture *glb);
88  bool checkStartBreak(void);
89  bool checkActionBreak(void);
90  void turnOnWarnings(void) { flags |= rule_warnings_on; }
91  void turnOffWarnings(void) { flags &= ~rule_warnings_on; }
92 public:
93  Action(uint4 f,const string &nm,const string &g);
94  virtual ~Action(void) {}
95 #ifdef OPACTION_DEBUG
96  virtual bool turnOnDebug(const string &nm);
97  virtual bool turnOffDebug(const string &nm);
98 #endif
99  virtual void printStatistics(ostream &s) const;
100  int4 perform(Funcdata &data);
101  bool setBreakPoint(uint4 tp,const string &specify);
102  virtual void clearBreakPoints(void);
103  bool setWarning(bool val,const string &specify);
104  bool disableRule(const string &specify);
105  bool enableRule(const string &specify);
106  const string &getName(void) const { return name; }
107  const string &getGroup(void) const { return basegroup; }
108  uint4 getStatus(void) const { return status; }
109  uint4 getNumTests(void) { return count_tests; }
110  uint4 getNumApply(void) { return count_apply; }
111  virtual Action *clone(const ActionGroupList &grouplist) const=0;
118  virtual void reset(Funcdata &data);
119  virtual void resetStats(void);
120  virtual int4 apply(Funcdata &data)=0;
129  virtual int4 print(ostream &s,int4 num,int4 depth) const;
130  virtual void printState(ostream &s) const;
131  virtual void saveXml(ostream &s) const {}
132  virtual void restoreXml(const Element *el,Funcdata *fd) {}
133  virtual Action *getSubAction(const string &specify);
134  virtual Rule *getSubRule(const string &specify);
135 };
136 
143 class ActionGroup : public Action {
144 protected:
145  vector<Action *> list;
146  vector<Action *>::iterator state;
147 public:
148  ActionGroup(uint4 f,const string &nm) : Action(f,nm,"") {}
149  virtual ~ActionGroup(void);
150  void addAction(Action *ac);
151  virtual void clearBreakPoints(void);
152  virtual Action *clone(const ActionGroupList &grouplist) const;
153  virtual void reset(Funcdata &data);
154  virtual void resetStats(void);
155  virtual int4 apply(Funcdata &data);
156  virtual int4 print(ostream &s,int4 num,int4 depth) const;
157  virtual void printState(ostream &s) const;
158  virtual Action *getSubAction(const string &specify);
159  virtual Rule *getSubRule(const string &specify);
160 #ifdef OPACTION_DEBUG
161  virtual bool turnOnDebug(const string &nm);
162  virtual bool turnOffDebug(const string &nm);
163 #endif
164  virtual void printStatistics(ostream &s) const;
165 };
166 
174  int4 maxrestarts;
175  int4 curstart;
176 public:
177  ActionRestartGroup(uint4 f,const string &nm,int4 max) :
178  ActionGroup(f,nm) { maxrestarts = max; }
179  virtual Action *clone(const ActionGroupList &grouplist) const;
180  virtual void reset(Funcdata &data);
181  virtual int4 apply(Funcdata &data);
182 };
183 
194 class Rule {
195 public:
197  enum typeflags {
202  };
203 private:
204  friend class ActionPool;
205  uint4 flags;
206  uint4 breakpoint;
207  string name;
208  string basegroup;
209  uint4 count_tests;
210  uint4 count_apply;
211  void issueWarning(Architecture *glb);
212 public:
213  Rule(const string &g,uint4 fl,const string &nm);
214  virtual ~Rule(void) {}
215  const string &getName(void) const { return name; }
216  const string &getGroup(void) const { return basegroup; }
217  uint4 getNumTests(void) { return count_tests; }
218  uint4 getNumApply(void) { return count_apply; }
219  void setBreak(uint4 tp) { breakpoint |= tp; }
220  void clearBreak(uint4 tp) { breakpoint &= ~tp; }
221  void clearBreakPoints(void) { breakpoint = 0; }
222  void turnOnWarnings(void) { flags |= warnings_on; }
223  void turnOffWarnings(void) { flags &= ~warnings_on; }
224  bool isDisabled(void) const { return ((flags & type_disable)!=0); }
225  void setDisable(void) { flags |= type_disable; }
226  void clearDisable(void) { flags &= ~type_disable; }
227  bool checkActionBreak(void);
228  uint4 getBreakPoint(void) const { return breakpoint; }
229 
236  virtual Rule *clone(const ActionGroupList &grouplist) const=0;
237  virtual void getOpList(vector<uint4> &oplist) const;
238 
246  virtual int4 applyOp(PcodeOp *op,Funcdata &data) { return 0; }
247  virtual void reset(Funcdata &data);
248  virtual void resetStats(void);
249  virtual void printStatistics(ostream &s) const;
250 #ifdef OPACTION_DEBUG
251  virtual bool turnOnDebug(const string &nm);
252  virtual bool turnOffDebug(const string &nm);
253 #endif
254 };
255 
262 class ActionPool : public Action {
263  vector<Rule *> allrules;
264  vector<Rule *> perop[CPUI_MAX];
265  PcodeOpTree::const_iterator op_state;
266  int4 rule_index;
267  int4 processOp(PcodeOp *op,Funcdata &data);
268 public:
269  ActionPool(uint4 f,const string &nm) : Action(f,nm,"") {}
270  virtual ~ActionPool(void);
271  void addRule(Rule *rl);
272  virtual void clearBreakPoints(void);
273  virtual Action *clone(const ActionGroupList &grouplist) const;
274  virtual void reset(Funcdata &data);
275  virtual void resetStats(void);
276  virtual int4 apply(Funcdata &data);
277  virtual int4 print(ostream &s,int4 num,int4 depth) const;
278  virtual void printState(ostream &s) const;
279  virtual Rule *getSubRule(const string &specify);
280  virtual void printStatistics(ostream &s) const;
281 #ifdef OPACTION_DEBUG
282  virtual bool turnOnDebug(const string &nm);
283  virtual bool turnOffDebug(const string &nm);
284 #endif
285 };
286 
299  Action *currentact;
300  string currentactname;
301  map<string,ActionGroupList> groupmap;
302  map<string,Action *> actionmap;
303  bool isDefaultGroups;
304  static const char universalname[];
305  void registerAction(const string &nm,Action *act);
306  void buildDefaultGroups(void);
307  Action *getAction(const string &nm) const;
308  Action *deriveAction(const string &baseaction,const string &grp);
309 public:
310  ActionDatabase(void) { currentact = (Action *)0; isDefaultGroups = false; }
311  ~ActionDatabase(void);
312  void resetDefaults(void);
313  Action *getCurrent(void) const { return currentact; }
314  const string &getCurrentName(void) const { return currentactname; }
315  const ActionGroupList &getGroup(const string &grp) const;
316  Action *setCurrent(const string &actname);
317  Action *toggleAction(const string &grp,const string &basegrp,bool val);
318 
319  void setGroup(const string &grp,const char **argv);
320  void cloneGroup(const string &oldname,const string &newname);
321  bool addToGroup(const string &grp,const string &basegroup);
322  bool removeFromGroup(const string &grp,const string &basegroup);
323  void universalAction(Architecture *glb);
324 };
325 
326 #endif
Rule::applyOp
virtual int4 applyOp(PcodeOp *op, Funcdata &data)
Attempt to apply this Rule.
Definition: action.hh:246
Action::rule_oneactperfunc
@ rule_oneactperfunc
Makes a change only once per function.
Definition: action.hh:56
Action::Action
Action(uint4 f, const string &nm, const string &g)
Base constructor for an Action.
Definition: action.cc:25
Action::lcount
int4 lcount
Changes not including last call to apply()
Definition: action.hh:78
ActionGroup::apply
virtual int4 apply(Funcdata &data)
Make a single attempt to apply this Action.
Definition: action.cc:504
Action::count_apply
uint4 count_apply
Number of times apply() made changes.
Definition: action.hh:84
Action::printStatistics
virtual void printStatistics(ostream &s) const
Dump statistics to stream.
Definition: action.cc:91
ActionDatabase::~ActionDatabase
~ActionDatabase(void)
Destructor.
Definition: action.cc:974
Action::break_action
@ break_action
Break if a change has been made.
Definition: action.hh:74
ActionRestartGroup
Action which checks if restart (sub)actions have been generated and restarts itself.
Definition: action.hh:173
ActionPool::apply
virtual int4 apply(Funcdata &data)
Make a single attempt to apply this Action.
Definition: action.cc:875
Action::status_end
@ status_end
getFuncdata has completed once (for onceperfunc)
Definition: action.hh:67
Action::rule_debug
@ rule_debug
Print debug messages specifically for this action.
Definition: action.hh:57
ActionDatabase::resetDefaults
void resetDefaults(void)
(Re)set the default configuration
Definition: action.cc:984
Action::clearBreakPoints
virtual void clearBreakPoints(void)
Clear all breakpoints set on this Action.
Definition: action.cc:185
block.hh
Classes related to basic blocks and control-flow structuring.
ActionPool::clearBreakPoints
virtual void clearBreakPoints(void)
Clear all breakpoints set on this Action.
Definition: action.cc:888
Rule
Class for performing a single transformation on a PcodeOp or Varnode.
Definition: action.hh:194
ActionPool::addRule
void addRule(Rule *rl)
Add a Rule to the pool.
Definition: action.cc:738
Action::perform
int4 perform(Funcdata &data)
Perform this action (if necessary)
Definition: action.cc:296
ActionPool
A pool of Rules that apply simultaneously.
Definition: action.hh:262
Action::rule_warnings_given
@ rule_warnings_given
A warning has been issued for this action.
Definition: action.hh:59
ActionGroup::state
vector< Action * >::iterator state
Current action being applied.
Definition: action.hh:146
Action::ruleflags
ruleflags
Boolean behavior properties governing this particular Action.
Definition: action.hh:53
ActionPool::getSubRule
virtual Rule * getSubRule(const string &specify)
Retrieve a specific sub-rule by name.
Definition: action.cc:787
ActionDatabase::setGroup
void setGroup(const string &grp, const char **argv)
Establish a new root Action.
Definition: action.cc:1057
Action::break_start
@ break_start
Break at beginning of action.
Definition: action.hh:72
Action::breakpoint
uint4 breakpoint
Breakpoint properties.
Definition: action.hh:81
ActionPool::printStatistics
virtual void printStatistics(ostream &s) const
Dump statistics to stream.
Definition: action.cc:962
Action::status_mid
@ status_mid
In middle of action (use subclass status)
Definition: action.hh:66
ActionDatabase::setCurrent
Action * setCurrent(const string &actname)
Set the current root Action.
Definition: action.cc:1019
ActionDatabase::toggleAction
Action * toggleAction(const string &grp, const string &basegrp, bool val)
Toggle a group of Actions with a root Action.
Definition: action.cc:1034
ActionGroupList
The list of groups defining a root Action.
Definition: action.hh:29
Action::rule_repeatapply
@ rule_repeatapply
Apply rule repeatedly until no change.
Definition: action.hh:54
Rule::clone
virtual Rule * clone(const ActionGroupList &grouplist) const =0
Clone the Rule.
ActionGroup::getSubRule
virtual Rule * getSubRule(const string &specify)
Retrieve a specific sub-rule by name.
Definition: action.cc:479
ActionGroup
A group of actions (generally) applied in sequence.
Definition: action.hh:143
ActionPool::~ActionPool
virtual ~ActionPool(void)
Destructor.
Definition: action.cc:726
Action::reset
virtual void reset(Funcdata &data)
Reset the Action for a new function.
Definition: action.cc:98
ActionGroup::reset
virtual void reset(Funcdata &data)
Reset the Action for a new function.
Definition: action.cc:406
Action::statusflags
statusflags
Boolean properties describing the status of an action.
Definition: action.hh:62
Action::getSubAction
virtual Action * getSubAction(const string &specify)
Retrieve a specific sub-action by name.
Definition: action.cc:273
Action
Large scale transformations applied to the varnode/op graph.
Definition: action.hh:50
Action::flags
uint4 flags
Behavior properties.
Definition: action.hh:82
ActionDatabase
Database of root Action objects that can be used to transform a function.
Definition: action.hh:298
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
ActionDatabase::getGroup
const ActionGroupList & getGroup(const string &grp) const
Get a specific grouplist by name.
Definition: action.cc:1004
Action::breakflags
breakflags
Break points associated with an Action.
Definition: action.hh:71
Action::status_actionbreak
@ status_actionbreak
Completed full action last time but indicated action break.
Definition: action.hh:68
PcodeOp
Lowest level operation of the p-code language.
Definition: op.hh:58
Architecture
Manager for all the major decompiler subsystems.
Definition: architecture.hh:119
ActionGroup::list
vector< Action * > list
List of actions to perform in the group.
Definition: action.hh:145
Rule::reset
virtual void reset(Funcdata &data)
Reset this Rule.
Definition: action.cc:648
Rule::warnings_on
@ warnings_on
A warning is issued if this rule is applied.
Definition: action.hh:200
Action::count_tests
uint4 count_tests
Number of times apply() has been called.
Definition: action.hh:83
Action::count
int4 count
Number of changes made by this action so far.
Definition: action.hh:79
ActionRestartGroup::apply
virtual int4 apply(Funcdata &data)
Make a single attempt to apply this Action.
Definition: action.cc:551
Action::setBreakPoint
bool setBreakPoint(uint4 tp, const string &specify)
Set a breakpoint on this action.
Definition: action.cc:169
ActionPool::reset
virtual void reset(Funcdata &data)
Reset the Action for a new function.
Definition: action.cc:914
ActionDatabase::removeFromGroup
bool removeFromGroup(const string &grp, const string &basegroup)
Remove a group from a root Action.
Definition: action.cc:1101
ActionPool::resetStats
virtual void resetStats(void)
Reset all the counts to zero.
Definition: action.cc:924
ActionPool::print
virtual int4 print(ostream &s, int4 num, int4 depth) const
Print a description of this Action to stream.
Definition: action.cc:751
CPUI_MAX
@ CPUI_MAX
Value indicating the end of the op-code values.
Definition: opcodes.hh:127
Action::status_repeat
@ status_repeat
Repeating the same action.
Definition: action.hh:65
Action::getSubRule
virtual Rule * getSubRule(const string &specify)
Retrieve a specific sub-rule by name.
Definition: action.cc:283
Action::resetStats
virtual void resetStats(void)
Reset all the counts to zero.
Definition: action.cc:106
Action::apply
virtual int4 apply(Funcdata &data)=0
Make a single attempt to apply this Action.
Action::status
uint4 status
Current status.
Definition: action.hh:80
Action::tmpbreak_start
@ tmpbreak_start
Temporary break at start of action.
Definition: action.hh:73
Action::basegroup
string basegroup
Base group this action belongs to.
Definition: action.hh:86
Rule::checkActionBreak
bool checkActionBreak(void)
Check if an action breakpoint is turned on.
Definition: action.cc:716
Action::disableRule
bool disableRule(const string &specify)
Disable a specific Rule within this.
Definition: action.cc:224
ActionDatabase::addToGroup
bool addToGroup(const string &grp, const string &basegroup)
Add a group to a root Action.
Definition: action.cc:1088
ActionGroupList::contains
bool contains(const string &nm) const
Check if this ActionGroupList contains a given group.
Definition: action.hh:37
Rule::type_disable
@ type_disable
Is this rule disabled.
Definition: action.hh:198
Rule::warnings_given
@ warnings_given
Set if a warning for this rule has been given before.
Definition: action.hh:201
Funcdata
Container for data structures associated with a single function.
Definition: funcdata.hh:45
Action::status_start
@ status_start
At start of action.
Definition: action.hh:63
Action::name
string name
Name of the action.
Definition: action.hh:85
Rule::getOpList
virtual void getOpList(vector< uint4 > &oplist) const
List of op codes this rule operates on.
Definition: action.cc:704
Action::checkStartBreak
bool checkStartBreak(void)
Check start breakpoint.
Definition: action.cc:50
ActionGroup::clearBreakPoints
virtual void clearBreakPoints(void)
Clear all breakpoints set on this Action.
Definition: action.cc:380
ActionDatabase::cloneGroup
void cloneGroup(const string &oldname, const string &newname)
Clone a root Action.
Definition: action.cc:1075
ActionRestartGroup::clone
virtual Action * clone(const ActionGroupList &grouplist) const
Clone the Action.
Definition: action.cc:527
ActionGroup::printState
virtual void printState(ostream &s) const
Print status to stream.
Definition: action.cc:442
ActionGroup::print
virtual int4 print(ostream &s, int4 num, int4 depth) const
Print a description of this Action to stream.
Definition: action.cc:426
Action::print
virtual int4 print(ostream &s, int4 num, int4 depth) const
Print a description of this Action to stream.
Definition: action.cc:130
Action::checkActionBreak
bool checkActionBreak(void)
Check action breakpoint.
Definition: action.cc:115
Rule::printStatistics
virtual void printStatistics(ostream &s) const
Print statistics for this Rule.
Definition: action.cc:695
Action::status_breakstarthit
@ status_breakstarthit
At start after breakpoint.
Definition: action.hh:64
Action::issueWarning
void issueWarning(Architecture *glb)
Warn that this Action has applied.
Definition: action.cc:39
ActionPool::printState
virtual void printState(ostream &s) const
Print status to stream.
Definition: action.cc:775
Action::enableRule
bool enableRule(const string &specify)
Enable a specific Rule within this.
Definition: action.cc:240
ActionGroup::getSubAction
virtual Action * getSubAction(const string &specify)
Retrieve a specific sub-action by name.
Definition: action.cc:454
ActionGroup::resetStats
virtual void resetStats(void)
Reset all the counts to zero.
Definition: action.cc:416
ActionRestartGroup::reset
virtual void reset(Funcdata &data)
Reset the Action for a new function.
Definition: action.cc:544
Rule::Rule
Rule(const string &g, uint4 fl, const string &nm)
Construct given group, properties name.
Definition: action.cc:620
ActionGroup::printStatistics
virtual void printStatistics(ostream &s) const
Dump statistics to stream.
Definition: action.cc:608
Rule::rule_debug
@ rule_debug
Print debug info specific for this rule.
Definition: action.hh:199
Action::rule_onceperfunc
@ rule_onceperfunc
Apply rule once per function.
Definition: action.hh:55
ActionDatabase::universalAction
void universalAction(Architecture *glb)
Build the universal action.
Definition: coreaction.cc:4878
Action::clone
virtual Action * clone(const ActionGroupList &grouplist) const =0
Clone the Action.
Action::printState
virtual void printState(ostream &s) const
Print status to stream.
Definition: action.cc:146
ActionGroup::clone
virtual Action * clone(const ActionGroupList &grouplist) const
Clone the Action.
Definition: action.cc:389
ActionGroup::~ActionGroup
virtual ~ActionGroup(void)
Destructor.
Definition: action.cc:362
ActionGroup::addAction
void addAction(Action *ac)
Add an Action to the group.
Definition: action.cc:374
ActionPool::clone
virtual Action * clone(const ActionGroupList &grouplist) const
Clone the Action.
Definition: action.cc:897
Action::rule_warnings_on
@ rule_warnings_on
If this action makes a change, issue a warning.
Definition: action.hh:58
Rule::typeflags
typeflags
Properties associated with a Rule.
Definition: action.hh:197
Action::setWarning
bool setWarning(bool val, const string &specify)
Set a warning on this action.
Definition: action.cc:197
Rule::resetStats
virtual void resetStats(void)
Reset Rule statistics.
Definition: action.cc:656