-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
walker.h
283 lines (237 loc) · 7.62 KB
/
walker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//*****************************************************************************
// File: walker.h
//
//
// Debugger code stream analysis routines
//
//*****************************************************************************
#ifndef WALKER_H_
#define WALKER_H_
/* ========================================================================= */
/* ------------------------------------------------------------------------- *
* Constants
* ------------------------------------------------------------------------- */
enum WALK_TYPE
{
WALK_NEXT,
WALK_BRANCH,
WALK_COND_BRANCH,
WALK_CALL,
WALK_RETURN,
WALK_BREAK,
WALK_THROW,
WALK_META,
WALK_UNKNOWN
};
// struct holding information for the instruction being skipped over
struct InstructionAttribute
{
bool m_fIsCall; // is this a call instruction?
bool m_fIsCond; // is this a conditional jump?
bool m_fIsAbsBranch; // is this an absolute branch (either a call or a jump)?
bool m_fIsRelBranch; // is this a relative branch (either a call or a jump)?
bool m_fIsWrite; // does the instruction write to an address?
DWORD m_cbInstr; // the size of the instruction
DWORD m_cbDisp; // the size of the displacement
DWORD m_dwOffsetToDisp; // the offset from the beginning of the instruction
// to the beginning of the displacement
BYTE m_cOperandSize; // the size of the operand
void Reset()
{
m_fIsCall = false;
m_fIsCond = false;
m_fIsAbsBranch = false;
m_fIsRelBranch = false;
m_fIsWrite = false;
m_cbInstr = 0;
m_cbDisp = 0;
m_dwOffsetToDisp = 0;
m_cOperandSize = 0;
}
};
/* ------------------------------------------------------------------------- *
* Classes
* ------------------------------------------------------------------------- */
class Walker
{
protected:
Walker()
: m_type(WALK_UNKNOWN), m_registers(NULL), m_ip(0), m_skipIP(0), m_nextIP(0), m_isAbsoluteBranch(false)
{LIMITED_METHOD_CONTRACT; }
public:
virtual void Init(const BYTE *ip, REGDISPLAY *pregisters)
{
PREFIX_ASSUME(pregisters != NULL);
_ASSERTE(GetControlPC(pregisters) == (PCODE)ip);
m_registers = pregisters;
SetIP(ip);
}
const BYTE *GetIP()
{ return m_ip; }
WALK_TYPE GetOpcodeWalkType()
{ return m_type; }
const BYTE *GetSkipIP()
{ return m_skipIP; }
bool IsAbsoluteBranch()
{ return m_isAbsoluteBranch; }
const BYTE *GetNextIP()
{ return m_nextIP; }
// We don't currently keep the registers up to date
// <TODO> Check if it really works on IA64. </TODO>
virtual void Next() { m_registers = NULL; SetIP(m_nextIP); }
virtual void Skip() { m_registers = NULL; LOG((LF_CORDB, LL_INFO10000, "skipping over to %p \n", m_skipIP)); SetIP(m_skipIP); }
// Decode the instruction
virtual void Decode() = 0;
private:
void SetIP(const BYTE *ip)
{ m_ip = ip; Decode(); }
protected:
WALK_TYPE m_type; // Type of instructions
REGDISPLAY *m_registers; // Registers
const BYTE *m_ip; // Current IP
const BYTE *m_skipIP; // IP if we skip the instruction
const BYTE *m_nextIP; // IP if the instruction is taken
bool m_isAbsoluteBranch; // Is it an obsolute branch or not
};
#ifdef TARGET_X86
class NativeWalker : public Walker
{
public:
void Init(const BYTE *ip, REGDISPLAY *pregisters)
{
m_opcode = 0;
Walker::Init(ip, pregisters);
}
DWORD GetOpcode()
{ return m_opcode; }
/*
void SetRegDisplay(REGDISPLAY *registers)
{ m_registers = registers; }
*/
REGDISPLAY *GetRegDisplay()
{ return m_registers; }
void Decode();
void DecodeModRM(BYTE mod, BYTE reg, BYTE rm, const BYTE *ip);
static void DecodeInstructionForPatchSkip(const BYTE *address, InstructionAttribute * pInstrAttrib);
private:
DWORD GetRegisterValue(int registerNumber);
DWORD m_opcode; // Current instruction or opcode
};
#elif defined (TARGET_ARM)
class NativeWalker : public Walker
{
public:
void Init(const BYTE *ip, REGDISPLAY *pregisters)
{
Walker::Init(ip, pregisters);
}
void Decode();
private:
bool ConditionHolds(DWORD cond);
DWORD GetReg(DWORD reg);
};
#elif defined(TARGET_AMD64)
class NativeWalker : public Walker
{
public:
void Init(const BYTE *ip, REGDISPLAY *pregisters)
{
m_opcode = 0;
Walker::Init(ip, pregisters);
}
DWORD GetOpcode()
{ return m_opcode; }
/*
void SetRegDisplay(REGDISPLAY *registers)
{ m_registers = registers; }
*/
REGDISPLAY *GetRegDisplay()
{ return m_registers; }
void Decode();
void DecodeModRM(BYTE mod, BYTE reg, BYTE rm, const BYTE *ip);
static void DecodeInstructionForPatchSkip(const BYTE *address, InstructionAttribute * pInstrAttrib);
private:
UINT64 GetRegisterValue(int registerNumber);
DWORD m_opcode; // Current instruction or opcode
};
#elif defined (TARGET_ARM64)
#include "controller.h"
class NativeWalker : public Walker
{
public:
void Init(const BYTE *ip, REGDISPLAY *pregisters)
{
Walker::Init(ip, pregisters);
}
void Decode();
static void DecodeInstructionForPatchSkip(const BYTE *address, InstructionAttribute * pInstrAttrib)
{
pInstrAttrib->Reset();
}
static BOOL DecodePCRelativeBranchInst(PT_CONTEXT context,const PRD_TYPE& opcode, PCODE& offset, WALK_TYPE& walk);
static BOOL DecodeCallInst(const PRD_TYPE& opcode, int& RegNum, WALK_TYPE& walk);
static BYTE* SetupOrSimulateInstructionForPatchSkip(T_CONTEXT * context, SharedPatchBypassBuffer * m_pSharedPatchBypassBuffer, const BYTE *address, PRD_TYPE opcode);
};
#elif defined (TARGET_LOONGARCH64)
#include "controller.h"
class NativeWalker : public Walker
{
public:
void Init(const BYTE *ip, REGDISPLAY *pregisters)
{
Walker::Init(ip, pregisters);
}
void Decode();
static void DecodeInstructionForPatchSkip(const BYTE *address, InstructionAttribute * pInstrAttrib)
{
pInstrAttrib->Reset();
}
static BOOL DecodePCRelativeBranchInst(PT_CONTEXT context,const PRD_TYPE opcode, PCODE& offset, WALK_TYPE& walk);
static BOOL DecodeJumpInst(const PRD_TYPE opcode, int& RegNum, PCODE& offset, WALK_TYPE& walk);
};
#elif defined (TARGET_RISCV64)
#include "controller.h"
class NativeWalker : public Walker
{
public:
void Init(const BYTE *ip, REGDISPLAY *pregisters)
{
Walker::Init(ip, pregisters);
}
void Decode();
uint64_t GetReg(uint64_t reg);
};
#else
PORTABILITY_WARNING("NativeWalker not implemented on this platform");
class NativeWalker : public Walker
{
public:
void Init(const BYTE *ip, REGDISPLAY *pregisters)
{
m_opcode = 0;
Walker::Init(ip, pregisters);
}
DWORD GetOpcode()
{ return m_opcode; }
void Next()
{ Walker::Next(); }
void Skip()
{ Walker::Skip(); }
void Decode()
{
PORTABILITY_ASSERT("NativeWalker not implemented on this platform");
m_type = WALK_UNKNOWN;
m_skipIP = m_ip++;
m_nextIP = m_ip++;
}
static void DecodeInstructionForPatchSkip(const BYTE *address, InstructionAttribute * pInstrAttrib)
{
PORTABILITY_ASSERT("NativeWalker not implemented on this platform");
}
private:
DWORD m_opcode; // Current instruction or opcode
};
#endif
#endif // WALKER_H_