My Project
pcodeinject.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 __PCODEINJECT__
20 #define __PCODEINJECT__
21 
22 #include "emulateutil.hh"
23 
24 class Architecture;
25 
34  friend class InjectPayload;
35  string name;
36  int4 index;
37  uint4 size;
38 public:
39  InjectParameter(const string &nm,uint4 sz) :name(nm) { index = 0; size = sz; }
40  const string &getName(void) const { return name; }
41  int4 getIndex(void) const { return index; }
42  uint4 getSize(void) const { return size; }
43 };
44 
57 public:
62  vector<VarnodeData> inputlist;
63  vector<VarnodeData> output;
64  virtual ~InjectContext(void) {}
65  virtual void clear(void) { inputlist.clear(); output.clear(); }
66 
70  virtual void saveXml(ostream &s) const=0;
71 };
72 
79 public:
80  enum {
81  CALLFIXUP_TYPE = 1,
82  CALLOTHERFIXUP_TYPE = 2,
83  CALLMECHANISM_TYPE = 3,
84  EXECUTABLEPCODE_TYPE = 4
85  };
86 protected:
87  string name;
88  int4 type;
89  bool dynamic;
91  int4 paramshift;
92  vector<InjectParameter> inputlist;
93  vector<InjectParameter> output;
94  static void readParameter(const Element *el,string &name,uint4 &size);
95  void orderParameters(void);
96 public:
97  InjectPayload(const string &nm,int4 tp) { name=nm; type=tp; paramshift=0; dynamic = false; incidentalCopy = false; }
98  int4 getParamShift(void) const { return paramshift; }
99  bool isDynamic(void) const { return dynamic; }
100  bool isIncidentalCopy(void) const { return incidentalCopy; }
101  int4 sizeInput(void) const { return inputlist.size(); }
102  int4 sizeOutput(void) const { return output.size(); }
103  InjectParameter &getInput(int4 i) { return inputlist[i]; }
104  InjectParameter &getOutput(int4 i) { return output[i]; }
105  virtual ~InjectPayload(void) {}
106 
116  virtual void inject(InjectContext &context,PcodeEmit &emit) const=0;
117 
118  virtual void restoreXml(const Element *el);
119  virtual void printTemplate(ostream &s) const=0;
120  string getName(void) const { return name; }
121  int4 getType(void) const { return type; }
122  virtual string getSource(void) const=0;
123 };
124 
135  Architecture *glb;
136  string source;
137  bool built;
138  EmulateSnippet emulator;
139  vector<uintb> inputList;
140  vector<uintb> outputList;
141  PcodeEmit *emitter;
142  void build(void);
143 public:
144  ExecutablePcode(Architecture *g,const string &src,const string &nm);
145  virtual ~ExecutablePcode(void) { if (emitter != (PcodeEmit *)0) delete emitter; }
146  virtual string getSource(void) const { return source; }
147  uintb evaluate(const vector<uintb> &input);
148 };
149 
163 protected:
165  uintb tempbase;
166  vector<InjectPayload *> injection;
167  map<string,int4> callFixupMap;
168  map<string,int4> callOtherFixupMap;
169  map<string,int4> callMechFixupMap;
170  map<string,int4> scriptMap;
171  vector<string> callFixupNames;
172  vector<string> callOtherTarget;
173  vector<string> callMechTarget;
174  vector<string> scriptNames;
175  void registerCallFixup(const string &fixupName,int4 injectid/* , vector<string> targets */);
176  void registerCallOtherFixup(const string &fixupName,int4 injectid);
177  void registerCallMechanism(const string &fixupName,int4 injectid);
178  void registerExeScript(const string &scriptName,int4 injectid);
179 
188  virtual int4 allocateInject(const string &sourceName,const string &name,int4 type)=0;
189 
196  virtual void registerInject(int4 injectid)=0;
197 public:
198  PcodeInjectLibrary(Architecture *g,uintb tmpbase) { glb = g; tempbase = tmpbase; }
199  virtual ~PcodeInjectLibrary(void);
200  uintb getUniqueBase(void) const { return tempbase; }
201  int4 getPayloadId(int4 type,const string &nm) const;
202  InjectPayload *getPayload(int4 id) const { return injection[id]; }
203  string getCallFixupName(int4 injectid) const;
204  string getCallOtherTarget(int4 injectid) const;
205  string getCallMechanismName(int4 injectid) const;
206  int4 restoreXmlInject(const string &src,const string &nm,int4 tp,const Element *el);
207 
214  virtual void restoreDebug(const Element *el) {}
215 
222  virtual int4 manualCallFixup(const string &name,const string &snippetstring)=0;
223 
233  virtual int4 manualCallOtherFixup(const string &name,const string &outname,const vector<string> &inname,
234  const string &snippet)=0;
235 
241  virtual InjectContext &getCachedContext(void)=0;
242 
248  virtual const vector<OpBehavior *> &getBehaviors(void)=0;
249 };
250 
251 #endif
Context needed to emit a p-code injection as a full set of p-code operations.
Definition: pcodeinject.hh:56
vector< InjectPayload * > injection
Registered injections.
Definition: pcodeinject.hh:166
Address baseaddr
Address of instruction causing inject.
Definition: pcodeinject.hh:59
A collection of p-code injection payloads.
Definition: pcodeinject.hh:162
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
A snippet of p-code that can be executed outside of normal analysis.
Definition: pcodeinject.hh:134
int4 getType(void) const
Return the type of injection (CALLFIXUP_TYPE, CALLOTHERFIXUP_TYPE, etc.)
Definition: pcodeinject.hh:121
int4 sizeOutput(void) const
Return the number of output parameters.
Definition: pcodeinject.hh:102
An input or output parameter to a p-code injection payload.
Definition: pcodeinject.hh:33
vector< InjectParameter > inputlist
List of input parameters to this payload.
Definition: pcodeinject.hh:92
vector< string > callFixupNames
Map from injectid to call-fixup name.
Definition: pcodeinject.hh:171
virtual void restoreDebug(const Element *el)
A method for reading in p-code generated externally for use in debugging.
Definition: pcodeinject.hh:214
uint4 getSize(void) const
Get the size of the parameter in bytes.
Definition: pcodeinject.hh:42
vector< string > callOtherTarget
Map from injectid to callother-fixup target-op name.
Definition: pcodeinject.hh:172
vector< VarnodeData > inputlist
Storage location for input parameters.
Definition: pcodeinject.hh:62
bool incidentalCopy
True if injected COPYs are considered incidental.
Definition: pcodeinject.hh:90
InjectParameter & getInput(int4 i)
Get the i-th input parameter.
Definition: pcodeinject.hh:103
An active container for a set of p-code operations that can be injected into data-flow.
Definition: pcodeinject.hh:78
int4 getIndex(void) const
Get the assigned index.
Definition: pcodeinject.hh:41
uintb getUniqueBase(void) const
Get the (current) offset for building temporary registers.
Definition: pcodeinject.hh:200
int4 paramshift
Number of parameters shifted in the original call.
Definition: pcodeinject.hh:91
map< string, int4 > callOtherFixupMap
Map of registered callother-fixup names to injection id.
Definition: pcodeinject.hh:168
string name
Formal name of the payload.
Definition: pcodeinject.hh:87
map< string, int4 > scriptMap
Map of registered script names to ExecutablePcode id.
Definition: pcodeinject.hh:170
int4 getParamShift(void) const
Get the number of parameters shifted.
Definition: pcodeinject.hh:98
InjectParameter(const string &nm, uint4 sz)
Constructor.
Definition: pcodeinject.hh:39
Architecture * glb
The Architecture to which the injection payloads apply.
Definition: pcodeinject.hh:164
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
int4 type
Type of this payload: CALLFIXUP_TYPE, CALLOTHERFIXUP_TYPE, etc.
Definition: pcodeinject.hh:88
Manager for all the major decompiler subsystems.
Definition: architecture.hh:117
bool dynamic
True if the injection is generated dynamically.
Definition: pcodeinject.hh:89
bool isIncidentalCopy(void) const
Return true if any injected COPY is considered incidental.
Definition: pcodeinject.hh:100
virtual void clear(void)
Release resources (from last injection)
Definition: pcodeinject.hh:65
vector< string > scriptNames
Map from injectid to script name.
Definition: pcodeinject.hh:174
map< string, int4 > callMechFixupMap
Map of registered mechanism names to injection id.
Definition: pcodeinject.hh:169
An XML element. A node in the DOM tree.
Definition: xml.hh:150
uintb tempbase
Offset within unique space for allocating temporaries within a payload.
Definition: pcodeinject.hh:165
map< string, int4 > callFixupMap
Map of registered call-fixup names to injection id.
Definition: pcodeinject.hh:167
PcodeInjectLibrary(Architecture *g, uintb tmpbase)
Constructor.
Definition: pcodeinject.hh:198
Architecture * glb
Architecture associated with the injection.
Definition: pcodeinject.hh:58
vector< InjectParameter > output
List of output parameters.
Definition: pcodeinject.hh:93
const string & getName(void) const
Get the parameter name.
Definition: pcodeinject.hh:40
vector< string > callMechTarget
Map from injectid to call-mech name.
Definition: pcodeinject.hh:173
string getName(void) const
Return the name of the injection.
Definition: pcodeinject.hh:120
InjectPayload(const string &nm, int4 tp)
Construct for use with restoreXml.
Definition: pcodeinject.hh:97
InjectPayload * getPayload(int4 id) const
Get the InjectPayload by id.
Definition: pcodeinject.hh:202
vector< VarnodeData > output
Storage location for output.
Definition: pcodeinject.hh:63
virtual ~InjectPayload(void)
Destructor.
Definition: pcodeinject.hh:105
int4 sizeInput(void) const
Return the number of input parameters.
Definition: pcodeinject.hh:101
Emulate a snippet of PcodeOps out of a functional context.
Definition: emulateutil.hh:111
bool isDynamic(void) const
Return true if p-code in the injection is generated dynamically.
Definition: pcodeinject.hh:99
(Lightweight) emulation interface for executing PcodeOp objects within a syntax tree or for executing...
InjectParameter & getOutput(int4 i)
Get the i-th output parameter.
Definition: pcodeinject.hh:104
Address nextaddr
Address of following instruction.
Definition: pcodeinject.hh:60
virtual ~InjectContext(void)
Destructor.
Definition: pcodeinject.hh:64
Address calladdr
If the instruction being injected is a call, this is the address being called.
Definition: pcodeinject.hh:61
virtual string getSource(void) const
Return a string describing the source of the injection (.cspec, prototype model, etc.)
Definition: pcodeinject.hh:146