Ghidra Decompiler Analysis Engine
emulate.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 __CPUI_EMULATE__
20 #define __CPUI_EMULATE__
21 
22 #include "memstate.hh"
23 #include "translate.hh"
24 
25 class Emulate; // Forward declaration
26 
37 class BreakTable {
38 public:
39  virtual ~BreakTable(void) {};
40 
46  virtual void setEmulate(Emulate *emu)=0;
47 
55  virtual bool doPcodeOpBreak(PcodeOpRaw *curop)=0;
56 
64  virtual bool doAddressBreak(const Address &addr)=0;
65 };
66 
78 protected:
80 public:
81  BreakCallBack(void);
82  virtual ~BreakCallBack(void) {}
83  virtual bool pcodeCallback(PcodeOpRaw *op);
84  virtual bool addressCallback(const Address &addr);
85  void setEmulate(Emulate *emu);
86 };
87 
91 
92 {
93  emulate = (Emulate *)0;
94 }
95 
103 
104 {
105  return true;
106 }
107 
115 inline bool BreakCallBack::addressCallback(const Address &addr)
116 
117 {
118  return true;
119 }
120 
124 
125 {
126  emulate = emu;
127 }
128 
138  Emulate *emulate;
139  Translate *trans;
140  map<Address,BreakCallBack *> addresscallback;
141  map<uintb,BreakCallBack *> pcodecallback;
142 public:
144  void registerPcodeCallback(const string &nm,BreakCallBack *func);
145  void registerAddressCallback(const Address &addr,BreakCallBack *func);
146  virtual void setEmulate(Emulate *emu);
147  virtual bool doPcodeOpBreak(PcodeOpRaw *curop);
148  virtual bool doAddressBreak(const Address &addr);
149 };
150 
155 
156 {
157  emulate = (Emulate *)0;
158  trans = t;
159 }
160 
168 class Emulate {
169 protected:
170  bool emu_halted;
172  virtual void executeUnary(void)=0;
173  virtual void executeBinary(void)=0;
174  virtual void executeLoad(void)=0;
175  virtual void executeStore(void)=0;
176 
182  virtual void executeBranch(void)=0;
183 
189  virtual bool executeCbranch(void)=0;
190  virtual void executeBranchind(void)=0;
191  virtual void executeCall(void)=0;
192  virtual void executeCallind(void)=0;
193  virtual void executeCallother(void)=0;
194  virtual void executeMultiequal(void)=0;
195  virtual void executeIndirect(void)=0;
196  virtual void executeSegmentOp(void)=0;
197  virtual void executeCpoolRef(void)=0;
198  virtual void executeNew(void)=0;
199  virtual void fallthruOp(void)=0;
200 public:
201  Emulate(void) { emu_halted = true; currentBehave = (OpBehavior *)0; }
202  virtual ~Emulate(void) {}
203  void setHalt(bool val);
204  bool getHalt(void) const;
205  virtual void setExecuteAddress(const Address &addr)=0;
206  virtual Address getExecuteAddress(void) const=0;
207  void executeCurrentOp(void);
208 };
209 
214 inline void Emulate::setHalt(bool val)
215 
216 {
217  emu_halted = val;
218 }
219 
224 inline bool Emulate::getHalt(void) const
225 
226 {
227  return emu_halted;
228 }
229 
242 
243 class EmulateMemory : public Emulate {
244 protected:
247  virtual void executeUnary(void);
248  virtual void executeBinary(void);
249  virtual void executeLoad(void);
250  virtual void executeStore(void);
251  virtual void executeBranch(void);
252  virtual bool executeCbranch(void);
253  virtual void executeBranchind(void);
254  virtual void executeCall(void);
255  virtual void executeCallind(void);
256  virtual void executeCallother(void);
257  virtual void executeMultiequal(void);
258  virtual void executeIndirect(void);
259  virtual void executeSegmentOp(void);
260  virtual void executeCpoolRef(void);
261  virtual void executeNew(void);
262 public:
265  MemoryState *getMemoryState(void) const;
266 };
267 
270 
271 {
272  return memstate;
273 }
274 
278 class PcodeEmitCache : public PcodeEmit {
279  vector<PcodeOpRaw *> &opcache;
280  vector<VarnodeData *> &varcache;
281  const vector<OpBehavior *> &inst;
282  uintm uniq;
283  VarnodeData *createVarnode(const VarnodeData *var);
284 public:
285  PcodeEmitCache(vector<PcodeOpRaw *> &ocache,vector<VarnodeData *> &vcache,
286  const vector<OpBehavior *> &in,uintb uniqReserve);
287  virtual void dump(const Address &addr,OpCode opc,VarnodeData *outvar,VarnodeData *vars,int4 isize);
288 };
289 
297  Translate *trans;
298  vector<PcodeOpRaw *> opcache;
299  vector<VarnodeData *> varcache;
300  vector<OpBehavior *> inst;
301  BreakTable *breaktable;
302  Address current_address;
303  bool instruction_start;
304  int4 current_op;
305  int4 instruction_length;
306  void clearCache(void);
307  void createInstruction(const Address &addr);
308  void establishOp(void);
309 protected:
310  virtual void fallthruOp(void);
311  virtual void executeBranch(void);
312  virtual void executeCallother(void);
313 public:
315  ~EmulatePcodeCache(void);
316  bool isInstructionStart(void) const;
317  int4 numCurrentOps(void) const;
318  int4 getCurrentOpIndex(void) const;
319  PcodeOpRaw *getOpByIndex(int4 i) const;
320  virtual void setExecuteAddress(const Address &addr);
321  virtual Address getExecuteAddress(void) const;
322  void executeInstruction(void);
323 };
324 
331 
332 {
333  return instruction_start;
334 }
335 
338 inline int4 EmulatePcodeCache::numCurrentOps(void) const
339 
340 {
341  return opcache.size();
342 }
343 
348 
349 {
350  return current_op;
351 }
352 
358 
359 {
360  return opcache[i];
361 }
362 
365 
366 {
367  return current_address;
368 }
369 
548 #endif
Emulate::executeBinary
virtual void executeBinary(void)=0
Execute a binary arithmetic/logical operation.
EmulateMemory::executeMultiequal
virtual void executeMultiequal(void)
Standard behavior for a MULTIEQUAL (phi-node)
Definition: emulate.cc:296
EmulatePcodeCache::EmulatePcodeCache
EmulatePcodeCache(Translate *t, MemoryState *s, BreakTable *b)
Pcode cache emulator constructor.
Definition: emulate.cc:329
BreakTableCallBack::setEmulate
virtual void setEmulate(Emulate *emu)
Associate an emulator with all breakpoints in the table.
Definition: emulate.cc:52
PcodeEmit
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
EmulatePcodeCache::setExecuteAddress
virtual void setExecuteAddress(const Address &addr)
Set current execution address.
Definition: emulate.cc:432
Emulate::executeMultiequal
virtual void executeMultiequal(void)=0
Standard behavior for a MULTIEQUAL (phi-node)
Emulate::emu_halted
bool emu_halted
Set to true if the emulator is halted.
Definition: emulate.hh:170
Emulate::executeCallother
virtual void executeCallother(void)=0
Standard behavior for a user-defined p-code op.
EmulatePcodeCache::numCurrentOps
int4 numCurrentOps(void) const
Return number of pcode ops in translation of current instruction.
Definition: emulate.hh:338
EmulatePcodeCache::executeCallother
virtual void executeCallother(void)
Execute breakpoint for this user-defined op.
Definition: emulate.cc:421
translate.hh
Classes for disassembly and pcode generation.
BreakTableCallBack::doAddressBreak
virtual bool doAddressBreak(const Address &addr)
Invoke any breakpoints for the given address.
Definition: emulate.cc:87
BreakCallBack::addressCallback
virtual bool addressCallback(const Address &addr)
Call back method for address based breakpoints.
Definition: emulate.hh:115
EmulateMemory::executeCallother
virtual void executeCallother(void)
Standard behavior for a user-defined p-code op.
Definition: emulate.cc:290
BreakTableCallBack::registerPcodeCallback
void registerPcodeCallback(const string &nm, BreakCallBack *func)
Register a pcode based breakpoint.
Definition: emulate.cc:23
BreakTable::doAddressBreak
virtual bool doAddressBreak(const Address &addr)=0
Invoke any breakpoints associated with this machine address.
EmulateMemory::executeBinary
virtual void executeBinary(void)
Execute a binary arithmetic/logical operation.
Definition: emulate.cc:225
PcodeEmitCache::dump
virtual void dump(const Address &addr, OpCode opc, VarnodeData *outvar, VarnodeData *vars, int4 isize)
The main pcode emit method.
Definition: emulate.cc:121
Emulate::executeLoad
virtual void executeLoad(void)=0
Standard behavior for a p-code LOAD.
EmulatePcodeCache::getCurrentOpIndex
int4 getCurrentOpIndex(void) const
Get the index of current pcode op within current instruction.
Definition: emulate.hh:347
EmulateMemory::executeIndirect
virtual void executeIndirect(void)
Standard behavior for an INDIRECT op.
Definition: emulate.cc:302
EmulatePcodeCache::getOpByIndex
PcodeOpRaw * getOpByIndex(int4 i) const
Get pcode op in current instruction translation by index.
Definition: emulate.hh:357
EmulateMemory
An abstract Emulate class using a MemoryState object as the backing machine state.
Definition: emulate.hh:243
EmulateMemory::executeStore
virtual void executeStore(void)
Standard behavior for a p-code STORE.
Definition: emulate.cc:246
Emulate::executeBranchind
virtual void executeBranchind(void)=0
Standard behavior for a BRANCHIND.
memstate.hh
Classes for keeping track of memory state during emulation.
OpBehavior
Class encapsulating the action/behavior of specific pcode opcodes.
Definition: opbehavior.hh:42
BreakTable::setEmulate
virtual void setEmulate(Emulate *emu)=0
Associate a particular emulator with breakpoints in this table.
EmulatePcodeCache::fallthruOp
virtual void fallthruOp(void)
Execute fallthru semantics for the pcode cache.
Definition: emulate.cc:389
BreakTableCallBack::doPcodeOpBreak
virtual bool doPcodeOpBreak(PcodeOpRaw *curop)
Invoke any breakpoints for the given pcode op.
Definition: emulate.cc:72
BreakTableCallBack::BreakTableCallBack
BreakTableCallBack(Translate *t)
Basic breaktable constructor.
Definition: emulate.hh:154
EmulatePcodeCache
A SLEIGH based implementation of the Emulate interface.
Definition: emulate.hh:296
EmulateMemory::currentOp
PcodeOpRaw * currentOp
Current op to execute.
Definition: emulate.hh:246
Emulate::executeUnary
virtual void executeUnary(void)=0
Execute a unary arithmetic/logical operation.
BreakCallBack::setEmulate
void setEmulate(Emulate *emu)
Associate a particular emulator with this breakpoint.
Definition: emulate.hh:123
Emulate::getHalt
bool getHalt(void) const
Get the halt state of the emulator.
Definition: emulate.hh:224
Emulate::setHalt
void setHalt(bool val)
Set the halt state of the emulator.
Definition: emulate.hh:214
Emulate
A pcode-based emulator interface.
Definition: emulate.hh:168
PcodeEmitCache
P-code emitter that dumps its raw Varnodes and PcodeOps to an in memory cache.
Definition: emulate.hh:278
BreakTableCallBack::registerAddressCallback
void registerAddressCallback(const Address &addr, BreakCallBack *func)
Register an address based breakpoint.
Definition: emulate.cc:43
MemoryState
All storage/state for a pcode machine.
Definition: memstate.hh:148
Emulate::executeCallind
virtual void executeCallind(void)=0
Standard behavior for a CALLIND.
Emulate::executeNew
virtual void executeNew(void)=0
Standard behavior for (low-level) NEW op.
BreakCallBack::pcodeCallback
virtual bool pcodeCallback(PcodeOpRaw *op)
Call back method for pcode based breakpoints.
Definition: emulate.hh:102
EmulatePcodeCache::executeInstruction
void executeInstruction(void)
Execute (the rest of) a single machine instruction.
Definition: emulate.cc:446
BreakTable::doPcodeOpBreak
virtual bool doPcodeOpBreak(PcodeOpRaw *curop)=0
Invoke any breakpoints associated with this particular pcodeop.
Address
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
EmulateMemory::executeBranchind
virtual void executeBranchind(void)
Standard behavior for a BRANCHIND.
Definition: emulate.cc:270
EmulatePcodeCache::executeBranch
virtual void executeBranch(void)
Execute branch (including relative branches)
Definition: emulate.cc:402
Translate
The interface to a translation engine for a processor.
Definition: translate.hh:294
EmulateMemory::executeSegmentOp
virtual void executeSegmentOp(void)
Behavior for a SEGMENTOP.
Definition: emulate.cc:308
Emulate::fallthruOp
virtual void fallthruOp(void)=0
Standard p-code fall-thru semantics.
BreakCallBack::emulate
Emulate * emulate
The emulator currently associated with this breakpoint.
Definition: emulate.hh:79
BreakCallBack::BreakCallBack
BreakCallBack(void)
Generic breakpoint constructor.
Definition: emulate.hh:90
EmulateMemory::executeNew
virtual void executeNew(void)
Standard behavior for (low-level) NEW op.
Definition: emulate.cc:320
EmulateMemory::executeUnary
virtual void executeUnary(void)
Execute a unary arithmetic/logical operation.
Definition: emulate.cc:216
OpCode
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
Emulate::executeBranch
virtual void executeBranch(void)=0
Standard behavior for a BRANCH.
BreakCallBack
A breakpoint object.
Definition: emulate.hh:77
Emulate::currentBehave
OpBehavior * currentBehave
Behavior of the next op to execute.
Definition: emulate.hh:171
Emulate::executeCbranch
virtual bool executeCbranch(void)=0
Check if the conditional of a CBRANCH is true.
EmulatePcodeCache::getExecuteAddress
virtual Address getExecuteAddress(void) const
Get current execution address.
Definition: emulate.hh:364
EmulateMemory::memstate
MemoryState * memstate
The memory state of the emulator.
Definition: emulate.hh:245
PcodeOpRaw
A low-level representation of a single pcode operation.
Definition: pcoderaw.hh:94
PcodeEmitCache::PcodeEmitCache
PcodeEmitCache(vector< PcodeOpRaw * > &ocache, vector< VarnodeData * > &vcache, const vector< OpBehavior * > &in, uintb uniqReserve)
Constructor.
Definition: emulate.cc:102
Emulate::getExecuteAddress
virtual Address getExecuteAddress(void) const =0
Get the address of the current instruction being executed.
VarnodeData
Data defining a specific memory location.
Definition: pcoderaw.hh:33
Emulate::setExecuteAddress
virtual void setExecuteAddress(const Address &addr)=0
Set the address of the next instruction to emulate.
Emulate::executeCpoolRef
virtual void executeCpoolRef(void)=0
Standard behavior for a CPOOLREF (constant pool reference) op.
EmulateMemory::executeCall
virtual void executeCall(void)
Standard behavior for a p-code CALL.
Definition: emulate.cc:277
Emulate::executeCurrentOp
void executeCurrentOp(void)
Do a single pcode op step.
Definition: emulate.cc:141
EmulateMemory::executeCbranch
virtual bool executeCbranch(void)
Check if the conditional of a CBRANCH is true.
Definition: emulate.cc:263
EmulateMemory::executeBranch
virtual void executeBranch(void)
Standard behavior for a BRANCH.
Definition: emulate.cc:257
BreakTableCallBack
A basic instantiation of a breakpoint table.
Definition: emulate.hh:137
Emulate::executeIndirect
virtual void executeIndirect(void)=0
Standard behavior for an INDIRECT op.
Emulate::executeStore
virtual void executeStore(void)=0
Standard behavior for a p-code STORE.
EmulateMemory::executeLoad
virtual void executeLoad(void)
Standard behavior for a p-code LOAD.
Definition: emulate.cc:235
EmulatePcodeCache::isInstructionStart
bool isInstructionStart(void) const
Return true if we are at an instruction start.
Definition: emulate.hh:330
EmulateMemory::executeCallind
virtual void executeCallind(void)
Standard behavior for a CALLIND.
Definition: emulate.cc:283
Emulate::executeCall
virtual void executeCall(void)=0
Standard behavior for a p-code CALL.
EmulateMemory::getMemoryState
MemoryState * getMemoryState(void) const
Get the emulator's memory state.
Definition: emulate.hh:269
EmulateMemory::executeCpoolRef
virtual void executeCpoolRef(void)
Standard behavior for a CPOOLREF (constant pool reference) op.
Definition: emulate.cc:314
Emulate::executeSegmentOp
virtual void executeSegmentOp(void)=0
Behavior for a SEGMENTOP.
EmulateMemory::EmulateMemory
EmulateMemory(MemoryState *mem)
Construct given a memory state.
Definition: emulate.hh:264
BreakTable
A collection of breakpoints for the emulator.
Definition: emulate.hh:37