Ghidra Decompiler Analysis Engine
emulateutil.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  */
19 #ifndef __CPUI_EMUTREE__
20 #define __CPUI_EMUTREE__
21 
22 #include "emulate.hh"
23 #include "op.hh"
24 
41 class EmulatePcodeOp : public Emulate {
42 protected:
46 
56  virtual uintb getLoadImageValue(AddrSpace *spc,uintb offset,int4 sz) const;
57  virtual void executeUnary(void);
58  virtual void executeBinary(void);
59  virtual void executeLoad(void);
60  virtual void executeStore(void);
61 // virtual void executeBranch(void)=0;
62  virtual bool executeCbranch(void);
63 // virtual void executeBranchind(void)=0;
64 // virtual void executeCall(void)=0;
65 // virtual void executeCallind(void)=0;
66 // virtual void executeCallother(void)=0;
67  virtual void executeMultiequal(void);
68  virtual void executeIndirect(void);
69  virtual void executeSegmentOp(void);
70  virtual void executeCpoolRef(void);
71  virtual void executeNew(void);
72 // virtual void fallthruOp(void)=0;
73 public:
75 
79  void setCurrentOp(PcodeOp *op) { currentOp = op; currentBehave = op->getOpcode()->getBehavior(); }
80  virtual Address getExecuteAddress(void) const { return currentOp->getAddr(); }
81 
88  virtual void setVarnodeValue(Varnode *vn,uintb val)=0;
89 
96  virtual uintb getVarnodeValue(Varnode *vn) const=0;
97 };
98 
111 class EmulateSnippet : public Emulate {
112  Architecture *glb;
113  vector<PcodeOpRaw *> opList;
114  vector<VarnodeData *> varList;
115  map<uintb,uintb> tempValues;
116  PcodeOpRaw *currentOp;
117  int4 pos;
118 
127  uintb getLoadImageValue(AddrSpace *spc,uintb offset,int4 sz) const;
128  virtual void executeUnary(void);
129  virtual void executeBinary(void);
130  virtual void executeLoad(void);
131  virtual void executeStore(void);
132  virtual void executeBranch(void);
133  virtual bool executeCbranch(void);
134  virtual void executeBranchind(void);
135  virtual void executeCall(void);
136  virtual void executeCallind(void);
137  virtual void executeCallother(void);
138  virtual void executeMultiequal(void);
139  virtual void executeIndirect(void);
140  virtual void executeSegmentOp(void);
141  virtual void executeCpoolRef(void);
142  virtual void executeNew(void);
143  virtual void fallthruOp(void);
144 public:
145  EmulateSnippet(Architecture *g) { glb = g; pos = 0; currentOp = (PcodeOpRaw *)0; }
146  virtual ~EmulateSnippet(void);
147  virtual void setExecuteAddress(const Address &addr) { setCurrentOp(0); }
148  virtual Address getExecuteAddress(void) const { return currentOp->getAddr(); }
149  Architecture *getArch(void) const { return glb; }
150 
154  void resetMemory(void) { tempValues.clear(); setCurrentOp(0); emu_halted = false; }
155 
156  PcodeEmit *buildEmitter(const vector<OpBehavior *> &inst,uintb uniqReserve);
157  bool checkForLegalCode(void) const;
158 
163  void setCurrentOp(int4 i) { pos = i; currentOp = opList[i]; currentBehave = currentOp->getBehavior(); }
164 
170  void setVarnodeValue(uintb offset,uintb val) { tempValues[offset] = val; }
171  uintb getVarnodeValue(VarnodeData *vn) const;
172  uintb getTempValue(uintb offset) const;
173 };
174 
175 #endif
EmulateSnippet::getTempValue
uintb getTempValue(uintb offset) const
Retrieve a temporary register value directly.
Definition: emulateutil.cc:386
EmulatePcodeOp::glb
Architecture * glb
The underlying Architecture for the program being emulated.
Definition: emulateutil.hh:43
PcodeEmit
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
EmulatePcodeOp::executeMultiequal
virtual void executeMultiequal(void)
Standard behavior for a MULTIEQUAL (phi-node)
Definition: emulateutil.cc:94
Emulate::emu_halted
bool emu_halted
Set to true if the emulator is halted.
Definition: emulate.hh:170
EmulatePcodeOp::executeNew
virtual void executeNew(void)
Standard behavior for (low-level) NEW op.
Definition: emulateutil.cc:142
EmulatePcodeOp::executeCpoolRef
virtual void executeCpoolRef(void)
Standard behavior for a CPOOLREF (constant pool reference) op.
Definition: emulateutil.cc:136
EmulatePcodeOp
Emulation based on (existing) PcodeOps and Varnodes.
Definition: emulateutil.hh:41
EmulatePcodeOp::executeIndirect
virtual void executeIndirect(void)
Standard behavior for an INDIRECT op.
Definition: emulateutil.cc:110
EmulateSnippet::resetMemory
void resetMemory(void)
Reset the emulation snippet.
Definition: emulateutil.hh:154
EmulatePcodeOp::currentOp
PcodeOp * currentOp
Current PcodeOp being executed.
Definition: emulateutil.hh:44
EmulateSnippet::setCurrentOp
void setCurrentOp(int4 i)
Set the current executing p-code op by index.
Definition: emulateutil.hh:163
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
EmulateSnippet::getVarnodeValue
uintb getVarnodeValue(VarnodeData *vn) const
Retrieve the value of a Varnode from the current machine state.
Definition: emulateutil.cc:363
Emulate
A pcode-based emulator interface.
Definition: emulate.hh:168
Varnode
A low-level variable or contiguous set of bytes described by an Address and a size.
Definition: varnode.hh:65
PcodeOpRaw::getAddr
const Address & getAddr(void) const
Get address of this operation.
Definition: pcoderaw.hh:166
EmulatePcodeOp::executeCbranch
virtual bool executeCbranch(void)
Check if the conditional of a CBRANCH is true.
Definition: emulateutil.cc:85
EmulateSnippet::checkForLegalCode
bool checkForLegalCode(void) const
Check for p-code that is deemed illegal for a snippet.
Definition: emulateutil.cc:325
EmulatePcodeOp::setCurrentOp
void setCurrentOp(PcodeOp *op)
Establish the current PcodeOp being emulated.
Definition: emulateutil.hh:79
EmulatePcodeOp::EmulatePcodeOp
EmulatePcodeOp(Architecture *g)
Constructor.
Definition: emulateutil.cc:20
EmulatePcodeOp::getExecuteAddress
virtual Address getExecuteAddress(void) const
Get the address of the current instruction being executed.
Definition: emulateutil.hh:80
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
EmulatePcodeOp::lastOp
PcodeOp * lastOp
Last PcodeOp that was executed.
Definition: emulateutil.hh:45
EmulateSnippet::setVarnodeValue
void setVarnodeValue(uintb offset, uintb val)
Set a temporary register value in the machine state.
Definition: emulateutil.hh:170
EmulatePcodeOp::getVarnodeValue
virtual uintb getVarnodeValue(Varnode *vn) const =0
Given a specific Varnode, retrieve the current value for it from the machine state.
EmulatePcodeOp::executeUnary
virtual void executeUnary(void)
Execute a unary arithmetic/logical operation.
Definition: emulateutil.cc:45
op.hh
The PcodeOp and PcodeOpBank classes.
emulate.hh
Classes for emulating p-code.
EmulateSnippet::setExecuteAddress
virtual void setExecuteAddress(const Address &addr)
Set the address of the next instruction to emulate.
Definition: emulateutil.hh:147
EmulatePcodeOp::getLoadImageValue
virtual uintb getLoadImageValue(AddrSpace *spc, uintb offset, int4 sz) const
Pull a value from the load-image given a specific address.
Definition: emulateutil.cc:28
Emulate::currentBehave
OpBehavior * currentBehave
Behavior of the next op to execute.
Definition: emulate.hh:171
PcodeOpRaw::getBehavior
OpBehavior * getBehavior(void) const
Retrieve the behavior for this op.
Definition: pcoderaw.hh:126
PcodeOpRaw
A low-level representation of a single pcode operation.
Definition: pcoderaw.hh:94
EmulateSnippet::buildEmitter
PcodeEmit * buildEmitter(const vector< OpBehavior * > &inst, uintb uniqReserve)
Provide the caller with an emitter for building the p-code snippet.
Definition: emulateutil.cc:310
EmulatePcodeOp::executeSegmentOp
virtual void executeSegmentOp(void)
Behavior for a SEGMENTOP.
Definition: emulateutil.cc:120
VarnodeData
Data defining a specific memory location.
Definition: pcoderaw.hh:33
EmulateSnippet::getExecuteAddress
virtual Address getExecuteAddress(void) const
Get the address of the current instruction being executed.
Definition: emulateutil.hh:148
EmulatePcodeOp::executeBinary
virtual void executeBinary(void)
Execute a binary arithmetic/logical operation.
Definition: emulateutil.cc:54
EmulatePcodeOp::executeStore
virtual void executeStore(void)
Standard behavior for a p-code STORE.
Definition: emulateutil.cc:76
EmulatePcodeOp::executeLoad
virtual void executeLoad(void)
Standard behavior for a p-code LOAD.
Definition: emulateutil.cc:64
EmulatePcodeOp::setVarnodeValue
virtual void setVarnodeValue(Varnode *vn, uintb val)=0
Given a specific Varnode, set the given value for it in the current machine state.
EmulateSnippet::~EmulateSnippet
virtual ~EmulateSnippet(void)
Destructor.
Definition: emulateutil.cc:293