-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.c
362 lines (287 loc) · 6.97 KB
/
common.c
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#ifndef __KERNEL__
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include "eflags.h"
#else /* ifndef __KERNEL__ */
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/gfp.h>
#endif /* else ifndef __KERNEL__ */
#include "common.h"
#ifdef __KERNEL__
#define strdup(x) kstrdup(x, GFP_KERNEL)
#endif
int
branch_op_check_condition(struct branch_op *branch,
unsigned long eflags,
unsigned long rcx)
{
#define FLAG(x) (eflags & X86_EFLAGS_ ## x)
switch (branch->opcode) {
case 0x77: /* ja or jnbe */
return !FLAG(CF) && !FLAG(ZF);
case 0x73: /* jae or jnc or jnb */
return !FLAG(CF);
case 0x72: /* jb or jc or jnae */
return FLAG(CF);
case 0x76: /* jbe or jna */
return FLAG(CF) || FLAG(ZF);
case 0x74: /* je or jz */
return FLAG(ZF);
case 0x7f: /* jg or jnle */
return !FLAG(ZF) && FLAG(SF) == FLAG(OF);
case 0x7d: /* jge or jnl */
return FLAG(SF) == FLAG(OF);
case 0x7c: /* jl or jnge */
return FLAG(SF) != FLAG(OF);
case 0x7e: /* jle or jng */
return FLAG(ZF) || FLAG(SF) != FLAG(OF);
case 0x75: /* jne or jnz */
return !FLAG(ZF);
case 0x71: /* jno */
return !FLAG(OF);
case 0xe3: /* jcxz/jecxz/jrcxz */
return !!rcx;
case 0x7b: /* jnp or jpo */
return !FLAG(PF);
case 0x79: /* jns */
return !FLAG(SF);
case 0x7a: /* jp or jpe */
return FLAG(PF);
case 0x78: /* js */
return FLAG(SF);
case 0x70: /* jo */
return FLAG(OF);
case 0xe9:
case 0xeb:
case JUMP_OP_OPCODE_DYNAMIC:
/* unconditional jumps */
return 1;
default:
return -EINVAL;
}
}
long
branch_op_resolve_to(struct branch_op *branch,
long (*read_reg)(int reg, void *arg),
long (*read_mem)(long mem, void *arg),
void *arg)
{
int is_ref = branch->dynamic_reg & JUMP_OP_DYNAMIC_REG_REF;
int is_sib = branch->dynamic_sib_mult;
int reg = branch->dynamic_reg & ~JUMP_OP_DYNAMIC_REG_REF;
if (branch->opcode != JUMP_OP_OPCODE_DYNAMIC) {
return branch->to;
}
if (!is_ref)
return read_reg(reg, arg);
if (!is_sib) {
long off = 0;
if (reg != REG_NONE)
off = read_reg(reg, arg);
off += (long)branch->dynamic_disp32;
return read_mem(off, arg);
} else {
long base = 0, index = 0, off = (long)branch->dynamic_disp32;
if (reg != REG_NONE)
base = read_reg(reg, arg);
if (branch->dynamic_sib_reg != REG_NONE)
index = read_reg(branch->dynamic_sib_reg, arg);
off += base + index * branch->dynamic_sib_mult;
return read_mem(off, arg);
}
#ifndef __KERNEL__
errno = -EINVAL;
#endif
return -1;
}
/* returns -1 on error, 1 when branch op is found, 0 otherwise.
* updates pbuf accordingly, may be used to got through functions */
int branch_op_decode(struct branch_op *branch, const char **pbuf, size_t size)
{
struct insn insn;
unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
modrm_reg = 0, sib = 0;
/* Parsed already! */
if (branch->opcode != 0)
return 1;
insn_init(&insn, *pbuf, size, /* always x86_64 */1);
insn_get_length(&insn);
if (!insn_complete(&insn))
return -1;
(*pbuf) += insn.length;
op1 = insn.opcode.bytes[0];
op2 = insn.opcode.bytes[1];
if (insn.rex_prefix.nbytes) {
rex = insn.rex_prefix.bytes[0];
rex_w = X86_REX_W(rex) >> 3;
rex_r = X86_REX_R(rex) >> 2;
rex_x = X86_REX_X(rex) >> 1;
rex_b = X86_REX_B(rex);
}
if (insn.modrm.nbytes) {
modrm = insn.modrm.bytes[0];
modrm_mod = X86_MODRM_MOD(modrm);
modrm_reg = X86_MODRM_REG(modrm);
modrm_rm = X86_MODRM_RM(modrm);
}
if (insn.sib.nbytes)
sib = insn.sib.bytes[0];
/* defaults */
branch->to = branch->from + insn.length + insn.immediate.value;
branch->opcode = op1;
branch->len = insn.length;
switch (op1) {
case 0xe8: /* call */
branch->type = INSN_CALL;
break;
case 0xe9: /* dumb jumps */
case 0xeb:
branch->type = INSN_JUMP_UNCONDITIONAL;
break;
case 0xe3: /* ecx jumps */
case 0x70 ... 0x7f: /* cond jumps */
branch->type = INSN_JUMP_CONDITIONAL;
break;
case 0x0f:
if (op2 >= 0x80 && op2 <= 0x8f) {
branch->type = INSN_JUMP_CONDITIONAL;
branch->opcode = op2 - 0x10;
break;
}
return 0;
case 0xff:
if (modrm_reg == 2 || modrm_reg == 3) {
branch->type = INSN_CALL_DYNAMIC;
goto dynamic_regs;
} else if (modrm_reg == 4) {
branch->type = INSN_JUMP_DYNAMIC;
goto dynamic_regs;
}
/* fallthrough */
default:
branch->opcode = 0;
return 0;
}
return 1;
dynamic_regs:
branch->to = 0;
branch->dynamic_reg = modrm_rm + (rex_b << 3);
/* SDM Vol. 2, Table 2-2 */
if (modrm_mod != 0x3) {
/* RIP-relative addressing, Table 2-7 */
if (modrm_mod == 0 && modrm_rm == 0x5) {
branch->dynamic_reg = REG_RIP;
}
/* SIB byte, Table 2-3 */
else if (modrm_rm == 0x4) {
branch->dynamic_reg = X86_SIB_BASE(sib) + (rex_b << 3);
if (branch->dynamic_reg == 0x5 && modrm_mod == 0)
branch->dynamic_reg = REG_NONE;
branch->dynamic_sib_reg = X86_SIB_INDEX(sib) + (rex_x << 3);
if (branch->dynamic_sib_reg == 0x4)
branch->dynamic_sib_reg = REG_NONE;
branch->dynamic_sib_mult = 1 << X86_SIB_SCALE(sib);
}
branch->dynamic_reg |= JUMP_OP_DYNAMIC_REG_REF;
}
branch->dynamic_disp32 = insn.displacement.value;
return 1;
}
#ifndef __KERNEL__
/* TODO(pboldin) these are not so common, move them */
static int branch_op_parse(struct branch_op *branch,
const char *in)
{
int ret;
memset(branch, 0, sizeof(*branch));
ret = sscanf(in, "0x%lx+0x%x", &branch->from, &branch->len);
if (ret != 2)
return -1;
return 0;
}
int parse_trace_point_line(const char *buf,
struct pmb_tracepoint *point)
{
char *p;
const char objname_str[] = "objname=";
const size_t objname_str_len = sizeof(objname_str) - 1;
static char *objname;
int ret;
p = strstr(buf, objname_str);
if (p) {
p += objname_str_len;
objname = strdup(p);
}
p = strchr(buf, '#');
if (p == buf)
return 0;
if (p)
*p = '\0';
ret = branch_op_parse(&point->branch, buf);
if (ret < 0) {
return -1;
}
point->objname = objname;
return 1;
}
int branch_op_read_input_file(const char *filename,
struct pmb_tracepoint **points,
size_t *npoints)
{
FILE *fh;
char buf[1024], *p;
struct pmb_tracepoint *t = NULL, tmp;
size_t n = 0, nalloc = 0;
int ret = -1;
tmp.objname = NULL;
*points = NULL;
*npoints = 0;
if (!strcmp(filename, "-"))
fh = stdin;
else
fh = fopen(filename, "r");
if (fh == NULL) {
perror("fopen");
return -1;
}
while (!feof(fh)) {
if (fgets(buf, sizeof(buf), fh) == NULL) {
if (errno == 0)
break;
goto out_err;
}
buf[strlen(buf) - 1] = '\0';
ret = parse_trace_point_line(buf, &tmp);
if (ret < 0)
goto out_err;
if (ret == 0)
continue;
if (n + 1 > nalloc) {
struct pmb_tracepoint *newt;
nalloc += 1024;
newt = realloc(t, sizeof(*t) * nalloc);
if (newt == NULL) {
ret = -1;
goto out_err;
}
t = newt;
}
t[n] = tmp;
n++;
}
*points = t;
*npoints = n;
ret = 0;
out_err:
if (ret < 0)
free(t);
if (fh != stdin)
fclose(fh);
return ret;
}
#endif /* ifndef __KERNEL__ */