Repository navigation
Expand file tree
/
Copy pathafl-dr.cpp
More file actions
168 lines (142 loc) · 5.81 KB
/
Copy pathafl-dr.cpp
File metadata and controls
168 lines (142 loc) · 5.81 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
#include <dr_api.h>
#include <droption.h>
#include <stdint.h>
#include <sys/shm.h>
#include "afl-annotations.h"
#include "afl-dr.h"
static droption_t<bool> opt_stack_spill(DROPTION_SCOPE_CLIENT, "stack-spill", false,
"Spill registers on stack",
"Spill registers on stack instead of DR's SPILL_SLOTs");
static void parse_options(int argc, const char *argv[]) {
std::string parse_err;
if (!droption_parser_t::parse_argv(DROPTION_SCOPE_CLIENT, argc, argv, &parse_err, NULL)) {
dr_fprintf(STDERR, "Incorrect client options: %s\n", parse_err.c_str());
dr_exit_process(1);
}
}
static const int MAP_SIZE = 1 << 16;
static uint8_t *shmem;
static bool init_shmem(bool allow_dummy) {
const char *id_str = getenv("__AFL_SHM_ID");
if (!id_str) {
dr_fprintf(STDERR, "Cannot get SHM id from the environment.\n");
if (allow_dummy) {
dr_fprintf(STDERR, "Creating dummy map.\n");
shmem = (uint8_t *)dr_global_alloc(MAP_SIZE);
return true;
}
return false;
}
shmem = (uint8_t *)shmat(atoi(id_str), NULL, 0);
EXIT_IF_FAILED(shmem != (void*)-1, "Cannot acquire SHM.\n", 1);
dr_fprintf(STDERR, "SHM = %p\n", shmem);
return true;
}
typedef struct {
uint64_t scratch;
uint8_t map[MAP_SIZE];
} thread_data;
static void *lock;
static void event_thread_init(void *drcontext) {
void *data = dr_thread_alloc(drcontext, sizeof(thread_data));
memset(data, 0, sizeof(thread_data));
dr_set_tls_field(drcontext, data);
}
static void event_thread_exit(void *drcontext) {
thread_data *data = (thread_data *) dr_get_tls_field(drcontext);
dr_mutex_lock(lock);
for (int i = 0; i < MAP_SIZE; ++i) {
shmem[i] += data->map[i];
}
dr_mutex_unlock(lock);
dr_thread_free(drcontext, data, sizeof(thread_data));
}
static void event_exit() {
if (lock) {
dr_mutex_destroy(lock);
lock = NULL;
}
}
static dr_emit_flags_t event_basic_block(void *drcontext, void *tag, instrlist_t *bb,
bool for_trace, bool translating) {
app_pc pc = dr_fragment_app_pc(tag);
if (!translating) {
trace_bb_instrumentation(pc, for_trace);
}
if (!need_instrument_pc(pc)) {
return DR_EMIT_DEFAULT;
}
uint32_t cur_location = (((uint32_t)(uintptr_t)pc) * (uint32_t)33533) & 0xFFFF;
instr_t *where = instrlist_first(bb);
reg_id_t tls_reg = DR_REG_XDI, offset_reg = DR_REG_XDX, arith_reg = DR_REG_XAX;
if (opt_stack_spill.get_value()) {
instrlist_meta_preinsert(bb, where,
INSTR_CREATE_push(drcontext,
opnd_create_reg(tls_reg)));
instrlist_meta_preinsert(bb, where,
INSTR_CREATE_push(drcontext,
opnd_create_reg(offset_reg)));
instrlist_meta_preinsert(bb, where,
INSTR_CREATE_push(drcontext,
opnd_create_reg(arith_reg)));
} else {
dr_save_reg(drcontext, bb, where, arith_reg, SPILL_SLOT_1);
dr_save_reg(drcontext, bb, where, tls_reg, SPILL_SLOT_2);
dr_save_reg(drcontext, bb, where, offset_reg, SPILL_SLOT_3);
}
dr_save_arith_flags_to_xax(drcontext, bb, where);
if (dr_using_all_private_caches()) {
instrlist_meta_preinsert(bb, where,
INSTR_CREATE_mov_imm(drcontext,
opnd_create_reg(tls_reg),
OPND_CREATE_INTPTR(dr_get_tls_field(drcontext))));
} else {
dr_insert_read_tls_field(drcontext, bb, where, tls_reg);
}
instrlist_meta_preinsert(bb, where,
XINST_CREATE_load(drcontext,
opnd_create_reg(offset_reg),
OPND_CREATE_MEM64(tls_reg, offsetof(thread_data, scratch))));
instrlist_meta_preinsert(bb, where,
INSTR_CREATE_xor(drcontext,
opnd_create_reg(offset_reg),
OPND_CREATE_INT32(cur_location)));
instrlist_meta_preinsert(bb, where,
XINST_CREATE_store(drcontext,
OPND_CREATE_MEM32(tls_reg, offsetof(thread_data, scratch)),
OPND_CREATE_INT32(cur_location >> 1)));
instrlist_meta_preinsert(bb, where,
INSTR_CREATE_inc(drcontext,
opnd_create_base_disp(tls_reg, offset_reg, 1, offsetof(thread_data, map), OPSZ_1)));
dr_restore_arith_flags_from_xax(drcontext, bb, where);
if (opt_stack_spill.get_value()) {
instrlist_meta_preinsert(bb, where,
INSTR_CREATE_pop(drcontext,
opnd_create_reg(arith_reg)));
instrlist_meta_preinsert(bb, where,
INSTR_CREATE_pop(drcontext,
opnd_create_reg(offset_reg)));
instrlist_meta_preinsert(bb, where,
INSTR_CREATE_pop(drcontext,
opnd_create_reg(tls_reg)));
} else {
dr_restore_reg(drcontext, bb, where, offset_reg, SPILL_SLOT_3);
dr_restore_reg(drcontext, bb, where, tls_reg, SPILL_SLOT_2);
dr_restore_reg(drcontext, bb, where, arith_reg, SPILL_SLOT_1);
}
return DR_EMIT_DEFAULT;
}
DR_EXPORT void dr_client_main(client_id_t id, int argc, const char *argv[]) {
parse_options(argc, argv);
init_module_filter();
if (init_shmem(true)) {
lock = dr_mutex_create();
dr_register_thread_init_event(event_thread_init);
dr_register_thread_exit_event(event_thread_exit);
dr_register_bb_event(event_basic_block);
dr_register_exit_event(event_exit);
}
EXIT_IF_FAILED(
dr_annotation_register_call("run_forkserver", (void *)start_forkserver, false, 0, DR_ANNOTATION_CALL_TYPE_FASTCALL),
"Cannot register forkserver annotation.\n", 1);
}