Ghidra Decompiler Analysis Engine
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 {
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
InjectPayload::output
vector< InjectParameter > output
List of output parameters.
Definition: pcodeinject.hh:93
PcodeEmit
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
InjectContext::glb
Architecture * glb
Architecture associated with the injection.
Definition: pcodeinject.hh:58
PcodeInjectLibrary::manualCallFixup
virtual int4 manualCallFixup(const string &name, const string &snippetstring)=0
Manually add a call-fixup payload given a compilable snippet of p-code source.
PcodeInjectLibrary::getBehaviors
virtual const vector< OpBehavior * > & getBehaviors(void)=0
Get the array of op-code behaviors for initializing and emulator.
InjectPayload::paramshift
int4 paramshift
Number of parameters shifted in the original call.
Definition: pcodeinject.hh:91
PcodeInjectLibrary::getPayloadId
int4 getPayloadId(int4 type, const string &nm) const
Map name and type to the payload id.
Definition: pcodeinject.cc:252
InjectPayload::orderParameters
void orderParameters(void)
Assign an index to parameters.
Definition: pcodeinject.cc:44
PcodeInjectLibrary::callFixupMap
map< string, int4 > callFixupMap
Map of registered call-fixup names to injection id.
Definition: pcodeinject.hh:167
InjectPayload::CALLFIXUP_TYPE
@ CALLFIXUP_TYPE
Injection that replaces a CALL.
Definition: pcodeinject.hh:81
InjectPayload::inject
virtual void inject(InjectContext &context, PcodeEmit &emit) const =0
PcodeInjectLibrary::getCallMechanismName
string getCallMechanismName(int4 injectid) const
Get the call mechanism name associated with an id.
Definition: pcodeinject.cc:301
InjectPayload::type
int4 type
Type of this payload: CALLFIXUP_TYPE, CALLOTHERFIXUP_TYPE, etc.
Definition: pcodeinject.hh:88
InjectContext::output
vector< VarnodeData > output
Storage location for output.
Definition: pcodeinject.hh:63
PcodeInjectLibrary::callFixupNames
vector< string > callFixupNames
Map from injectid to call-fixup name.
Definition: pcodeinject.hh:171
InjectPayload::printTemplate
virtual void printTemplate(ostream &s) const =0
Print the p-code ops of the injection to a stream (for debugging)
ExecutablePcode::ExecutablePcode
ExecutablePcode(Architecture *g, const string &src, const string &nm)
Constructor.
Definition: pcodeinject.cc:103
InjectPayload::getSource
virtual string getSource(void) const =0
Return a string describing the source of the injection (.cspec, prototype model, etc....
PcodeInjectLibrary
A collection of p-code injection payloads.
Definition: pcodeinject.hh:162
InjectParameter
An input or output parameter to a p-code injection payload.
Definition: pcodeinject.hh:33
PcodeInjectLibrary::getCachedContext
virtual InjectContext & getCachedContext(void)=0
Retrieve a reusable context object for this library.
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
InjectContext::baseaddr
Address baseaddr
Address of instruction causing inject.
Definition: pcodeinject.hh:59
Architecture
Manager for all the major decompiler subsystems.
Definition: architecture.hh:119
PcodeInjectLibrary::glb
Architecture * glb
The Architecture to which the injection payloads apply.
Definition: pcodeinject.hh:164
ExecutablePcode::getSource
virtual string getSource(void) const
Return a string describing the source of the injection (.cspec, prototype model, etc....
Definition: pcodeinject.hh:146
PcodeInjectLibrary::callMechFixupMap
map< string, int4 > callMechFixupMap
Map of registered mechanism names to injection id.
Definition: pcodeinject.hh:169
PcodeInjectLibrary::registerExeScript
void registerExeScript(const string &scriptName, int4 injectid)
Map a p-code script name to a payload id.
Definition: pcodeinject.cc:235
InjectContext
Context needed to emit a p-code injection as a full set of p-code operations.
Definition: pcodeinject.hh:56
InjectContext::inputlist
vector< VarnodeData > inputlist
Storage location for input parameters.
Definition: pcodeinject.hh:62
InjectPayload
An active container for a set of p-code operations that can be injected into data-flow.
Definition: pcodeinject.hh:78
PcodeInjectLibrary::callOtherTarget
vector< string > callOtherTarget
Map from injectid to callother-fixup target-op name.
Definition: pcodeinject.hh:172
PcodeInjectLibrary::registerCallOtherFixup
void registerCallOtherFixup(const string &fixupName, int4 injectid)
Map a callother-fixup name to a payload id.
Definition: pcodeinject.cc:203
InjectContext::calladdr
Address calladdr
If the instruction being injected is a call, this is the address being called.
Definition: pcodeinject.hh:61
PcodeInjectLibrary::restoreXmlInject
int4 restoreXmlInject(const string &src, const string &nm, int4 tp, const Element *el)
Read in and register an injection payload from an XML stream.
Definition: pcodeinject.cc:319
InjectPayload::CALLMECHANISM_TYPE
@ CALLMECHANISM_TYPE
Injection to patch up data-flow around the caller/callee boundary.
Definition: pcodeinject.hh:83
PcodeInjectLibrary::callOtherFixupMap
map< string, int4 > callOtherFixupMap
Map of registered callother-fixup names to injection id.
Definition: pcodeinject.hh:168
InjectPayload::restoreXml
virtual void restoreXml(const Element *el)
Restore this payload from an XML stream.
Definition: pcodeinject.cc:62
emulateutil.hh
(Lightweight) emulation interface for executing PcodeOp objects within a syntax tree or for executing...
PcodeInjectLibrary::scriptMap
map< string, int4 > scriptMap
Map of registered script names to ExecutablePcode id.
Definition: pcodeinject.hh:170
InjectContext::saveXml
virtual void saveXml(ostream &s) const =0
Save this context to an XML stream as a <context> tag.
PcodeInjectLibrary::manualCallOtherFixup
virtual int4 manualCallOtherFixup(const string &name, const string &outname, const vector< string > &inname, const string &snippet)=0
Manually add a callother-fixup payload given a compilable snippet of p-code source.
EmulateSnippet
Emulate a snippet of PcodeOps out of a functional context.
Definition: emulateutil.hh:111
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
PcodeInjectLibrary::registerCallMechanism
void registerCallMechanism(const string &fixupName, int4 injectid)
Map a call mechanism name to a payload id.
Definition: pcodeinject.cc:219
InjectPayload::CALLOTHERFIXUP_TYPE
@ CALLOTHERFIXUP_TYPE
Injection that replaces a user-defined p-code op, CALLOTHER.
Definition: pcodeinject.hh:82
PcodeInjectLibrary::injection
vector< InjectPayload * > injection
Registered injections.
Definition: pcodeinject.hh:166
PcodeInjectLibrary::tempbase
uintb tempbase
Offset within unique space for allocating temporaries within a payload.
Definition: pcodeinject.hh:165
InjectPayload::dynamic
bool dynamic
True if the injection is generated dynamically.
Definition: pcodeinject.hh:89
InjectPayload::EXECUTABLEPCODE_TYPE
@ EXECUTABLEPCODE_TYPE
Injection running as a stand-alone p-code script.
Definition: pcodeinject.hh:84
InjectContext::nextaddr
Address nextaddr
Address of following instruction.
Definition: pcodeinject.hh:60
InjectPayload::inputlist
vector< InjectParameter > inputlist
List of input parameters to this payload.
Definition: pcodeinject.hh:92
PcodeInjectLibrary::getCallFixupName
string getCallFixupName(int4 injectid) const
Get the call-fixup name associated with an id.
Definition: pcodeinject.cc:281
PcodeInjectLibrary::allocateInject
virtual int4 allocateInject(const string &sourceName, const string &name, int4 type)=0
Allocate a new InjectPayload object.
PcodeInjectLibrary::scriptNames
vector< string > scriptNames
Map from injectid to script name.
Definition: pcodeinject.hh:174
PcodeInjectLibrary::callMechTarget
vector< string > callMechTarget
Map from injectid to call-mech name.
Definition: pcodeinject.hh:173
InjectPayload::readParameter
static void readParameter(const Element *el, string &name, uint4 &size)
Read in an <input> or <output> XML tag describing an injection parameter.
Definition: pcodeinject.cc:24
PcodeInjectLibrary::restoreDebug
virtual void restoreDebug(const Element *el)
A method for reading in p-code generated externally for use in debugging.
Definition: pcodeinject.hh:214
PcodeInjectLibrary::getCallOtherTarget
string getCallOtherTarget(int4 injectid) const
Get the callother-fixup name associated with an id.
Definition: pcodeinject.cc:291
PcodeInjectLibrary::~PcodeInjectLibrary
virtual ~PcodeInjectLibrary(void)
Destructor.
Definition: pcodeinject.cc:175
PcodeInjectLibrary::registerCallFixup
void registerCallFixup(const string &fixupName, int4 injectid)
Map a call-fixup name to a payload id.
Definition: pcodeinject.cc:187
ExecutablePcode::evaluate
uintb evaluate(const vector< uintb > &input)
Evaluate the snippet on the given inputs.
Definition: pcodeinject.cc:159
InjectPayload::name
string name
Formal name of the payload.
Definition: pcodeinject.hh:87
ExecutablePcode
A snippet of p-code that can be executed outside of normal analysis.
Definition: pcodeinject.hh:134
InjectPayload::incidentalCopy
bool incidentalCopy
True if injected COPYs are considered incidental.
Definition: pcodeinject.hh:90
PcodeInjectLibrary::registerInject
virtual void registerInject(int4 injectid)=0
Finalize a payload within the library, once the payload is initialized.