forked from GJDuck/e9patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe9action.h
More file actions
474 lines (433 loc) · 9.64 KB
/
e9action.h
File metadata and controls
474 lines (433 loc) · 9.64 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* Copyright (C) 2021 National University of Singapore
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __E9ACTION_H
#define __E9ACTION_H
#include <cassert>
#include <cstdint>
#include <map>
#include <regex>
#include <set>
#include <vector>
#include "e9tool.h"
#include "e9plugin.h"
/*
* Plugins.
*/
struct Plugin
{
const char *filename;
std::vector<char *> argv;
void *handle;
void *context;
intptr_t result;
PluginInit initFunc;
PluginEvent eventFunc;
PluginMatch matchFunc;
PluginCode codeFunc;
PluginData dataFunc;
PluginPatch patchFunc;
PluginFini finiFunc;
};
/*
* Match regex.
*/
struct MatchRegex
{
const std::regex regex;
const std::string str;
MatchRegex(const char *str) : str(str), regex(str)
{
;
}
bool match(const char *str) const
{
std::cmatch cmatch;
return std::regex_match(str, cmatch, regex);
}
};
/*
* Match types.
*/
typedef uint16_t MatchType;
#define MATCH_TYPE_UNDEFINED 0x0000
#define MATCH_TYPE_NIL 0x0001
#define MATCH_TYPE_INTEGER 0x0002
#define MATCH_TYPE_OPERAND 0x0004
#define MATCH_TYPE_ACCESS 0x0008
#define MATCH_TYPE_REGISTER 0x0010
#define MATCH_TYPE_MEMORY 0x0020
#define MATCH_TYPE_STRING 0x0040
#define MATCH_TYPE_REGEX 0x0080
#define MATCH_TYPE_SET 0x0100
/*
* Match value.
*/
struct MatchVal
{
union
{
intptr_t i;
const char *str;
e9tool::OpType op;
e9tool::Access access;
e9tool::Register reg;
e9tool::MemOp mem;
const MatchRegex *regex;
const MatchVal *vals;
};
MatchType type;
MatchVal() : type(MATCH_TYPE_UNDEFINED), i(0)
{
;
}
MatchVal(std::nullptr_t) : type(MATCH_TYPE_NIL), i(0)
{
;
}
MatchVal(intptr_t i) : type(MATCH_TYPE_INTEGER), i(i)
{
;
}
MatchVal(const char *str) : type(MATCH_TYPE_STRING), str(str)
{
;
}
MatchVal(e9tool::OpType op) : type(MATCH_TYPE_OPERAND), op(op)
{
;
}
MatchVal(e9tool::Access access) : type(MATCH_TYPE_ACCESS), access(access)
{
;
}
MatchVal(e9tool::Register reg) : type(MATCH_TYPE_REGISTER), reg(reg)
{
;
}
MatchVal(const e9tool::MemOp &mem) : type(MATCH_TYPE_MEMORY), mem(mem)
{
;
}
MatchVal(const MatchRegex *regex) : type(MATCH_TYPE_REGEX), regex(regex)
{
;
}
MatchVal(const MatchVal *vals) : type(MATCH_TYPE_SET), vals(vals)
{
;
}
int compare(const MatchVal &val) const;
bool operator==(const MatchVal &val) const;
bool operator!=(const MatchVal &val) const
{
return !(MatchVal::operator==(val));
}
bool operator<(const MatchVal &val) const
{
return (compare(val) < 0);
}
bool operator<=(const MatchVal &val) const
{
return (compare(val) <= 0);
}
bool operator>(const MatchVal &val) const
{
return (compare(val) > 0);
}
bool operator>=(const MatchVal &val) const
{
return (compare(val) >= 0);
}
};
/*
* Match kinds.
*/
enum MatchKind
{
MATCH_INVALID,
MATCH_TRUE,
MATCH_FALSE,
MATCH_PLUGIN,
MATCH_ASSEMBLY,
MATCH_ADDRESS,
MATCH_BYTES,
MATCH_CALL,
MATCH_CONDJUMP,
MATCH_DISP32,
MATCH_DISP8,
MATCH_IMM32,
MATCH_IMM8,
MATCH_JUMP,
MATCH_MMX,
MATCH_MNEMONIC,
MATCH_MODRM,
MATCH_OFFSET,
MATCH_RANDOM,
MATCH_RETURN,
MATCH_REX,
MATCH_SECTION,
MATCH_SIB,
MATCH_SIZE,
MATCH_TARGET,
MATCH_X87,
MATCH_SSE,
MATCH_AVX,
MATCH_AVX2,
MATCH_AVX512,
MATCH_CSV,
MATCH_BB_BEST,
MATCH_BB_ENTRY,
MATCH_BB_EXIT,
MATCH_BB_ADDR,
MATCH_BB_OFFSET,
MATCH_BB_SIZE,
MATCH_BB_LEN,
MATCH_F_BEST,
MATCH_F_ENTRY,
MATCH_F_ADDR,
MATCH_F_OFFSET,
MATCH_F_SIZE,
MATCH_F_LEN,
MATCH_F_NAME,
MATCH_OP,
MATCH_SRC,
MATCH_DST,
MATCH_IMM,
MATCH_REG,
MATCH_MEM,
MATCH_REGS,
MATCH_READS,
MATCH_WRITES,
};
/*
* Match sets.
*/
enum MatchSet
{
MATCH_Is, // Instructions
MATCH_BBs, // Basic blocks
MATCH_Fs, // Functions
};
/*
* Match fields.
*/
enum MatchField
{
MATCH_FIELD_NONE,
MATCH_FIELD_TYPE,
MATCH_FIELD_ACCESS,
MATCH_FIELD_SIZE,
MATCH_FIELD_SEG,
MATCH_FIELD_DISPL,
MATCH_FIELD_BASE,
MATCH_FIELD_INDEX,
MATCH_FIELD_SCALE,
MATCH_FIELD_ADDR,
};
/*
* Match variable.
*/
struct MatchVar
{
const MatchSet set; // Instruction set
const int i; // Instruction set index
const MatchKind match; // Variable name
const int j; // Variable index
const MatchField field; // Variable field
const char * const basename; // Basename (if applicable)
Plugin * const plugin; // Plugin (if applicable)
MatchVar(MatchSet set, int i, MatchKind match, int j, MatchField field,
const char *basename, Plugin *plugin) :
set(set), i(i), match(match), j(j), field(field), basename(basename),
plugin(plugin)
{
;
}
};
/*
* Match instantiation.
*/
enum MatchInst : uint8_t
{
MATCH_INST_VAL,
MATCH_INST_VAR,
MATCH_INST_EMPTY
};
/*
* A match arg.
*/
struct MatchArg
{
union
{
const MatchVar * const var;
const MatchVal * const val;
};
const MatchInst inst;
MatchArg(const MatchVal *val) : inst(MATCH_INST_VAL), val(val)
{
;
}
MatchArg(const MatchVar *var) : inst(MATCH_INST_VAR), var(var)
{
;
}
MatchArg() : inst(MATCH_INST_EMPTY), val(nullptr)
{
;
}
};
/*
* Match operations.
*/
enum MatchOp
{
MATCH_OP_ARG,
MATCH_OP_DEFINED,
MATCH_OP_NOT,
MATCH_OP_AND,
MATCH_OP_OR,
MATCH_OP_EQ,
MATCH_OP_NEQ,
MATCH_OP_LT,
MATCH_OP_LEQ,
MATCH_OP_GT,
MATCH_OP_GEQ,
MATCH_OP_IN,
MATCH_OP_NEG,
MATCH_OP_ADD,
MATCH_OP_SUB,
MATCH_OP_MUL,
MATCH_OP_DIV,
MATCH_OP_MOD,
MATCH_OP_BIT_NOT,
MATCH_OP_BIT_AND,
MATCH_OP_BIT_OR,
MATCH_OP_BIT_XOR,
MATCH_OP_LSHIFT,
MATCH_OP_RSHIFT,
};
/*
* A match expression.
*/
struct MatchExpr
{
const MatchOp op;
union
{
const MatchExpr *lhs;
const MatchArg arg;
};
const MatchExpr *rhs;
MatchExpr(MatchOp op, const MatchExpr *expr) :
op(op), lhs(expr), rhs(nullptr)
{
;
}
MatchExpr(MatchOp op, const MatchExpr *lhs, const MatchExpr *rhs) :
op(op), lhs(lhs), rhs(rhs)
{
;
}
MatchExpr(MatchOp op, const MatchArg arg) : op(op), arg(arg), rhs(nullptr)
{
;
}
};
/*
* Patch kind.
*/
enum PatchKind
{
PATCH_EMPTY,
PATCH_BREAK,
PATCH_TRAP,
PATCH_PRINT,
PATCH_EXIT,
PATCH_SIGNAL,
PATCH_CALL,
PATCH_PLUGIN,
};
/*
* Patch.
*/
struct Patch
{
const char * const name;
const PatchKind kind;
const e9tool::PatchPos pos;
union
{
int status = 0;
int signal;
};
const char * const filename = nullptr;
const char * const entry = nullptr;
const e9tool::CallABI abi = e9tool::ABI_CLEAN;
const e9tool::CallJump jmp = e9tool::JUMP_NONE;
const std::vector<e9tool::Argument> args;
mutable const e9tool::Call *call = nullptr;
Plugin * const plugin = nullptr;
Patch(const char *name, PatchKind kind, e9tool::PatchPos pos) :
name(name), kind(kind), pos(pos)
{
assert(kind == PATCH_EMPTY || kind == PATCH_BREAK ||
kind == PATCH_PRINT || kind == PATCH_TRAP);
};
Patch(const char *name, PatchKind kind, e9tool::PatchPos pos, int status) :
name(name), kind(kind), pos(pos), status(status)
{
assert(kind == PATCH_EXIT || kind == PATCH_SIGNAL);
}
Patch(const char *name, PatchKind kind, e9tool::PatchPos pos,
const char *filename, const char *entry,
e9tool::CallABI abi, e9tool::CallJump jmp,
const std::vector<e9tool::Argument> &args) :
name(name), kind(kind), pos(pos),
filename(filename), entry(entry), abi(abi), jmp(jmp), args(args)
{
assert(kind == PATCH_CALL);
}
Patch(const char *name, PatchKind kind, e9tool::PatchPos pos,
Plugin *plugin) :
name(name), kind(kind), pos(pos), plugin(plugin)
{
assert(kind == PATCH_PLUGIN);
}
};
/*
* An "action" is a match/patch pair.
*/
struct Action
{
const MatchExpr * const match;
const std::vector<const Patch *> patch;
Action(const MatchExpr * match, std::vector<const Patch *> &&patch) :
match(match), patch(patch)
{
;
}
};
/*
* Prototypes.
*/
extern MatchExpr *parseMatch(const e9tool::ELF &elf, const char *str);
extern const Patch *parsePatch(const e9tool::ELF &elf, const char *str);
extern bool matchEval(const MatchExpr *expr, const e9tool::ELF &elf,
const std::vector<e9tool::Instr> &Is, size_t idx,
const e9tool::InstrInfo *I);
#endif