-
Notifications
You must be signed in to change notification settings - Fork 1
/
sourcecov.cc
300 lines (257 loc) · 8.89 KB
/
sourcecov.cc
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
#include "bochs.h"
#include "cpu/cpu.h"
#include "fuzz.h"
#include <stdio.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <regex>
static int coverage_dump_precision = 300;
static uint64_t last_coverage_dump;
// The format of the header:
/* See: https://github.com/llvm/llvm-project/blob/36daf3532d91bb3e61d631edceea77ebb8417801/compiler-rt/include/profile/InstrProfData.inc#L126
INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
INSTR_PROF_RAW_HEADER(uint64_t, DataSize, DataSize)
INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesBeforeCounters, PaddingBytesBeforeCounters)
INSTR_PROF_RAW_HEADER(uint64_t, CountersSize, CountersSize)
INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesAfterCounters, PaddingBytesAfterCounters)
INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta,
(uintptr_t)CountersBegin - (uintptr_t)DataBegin)
INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
Eg:
00000000: 8172 666f 7270 6cff 0800 0000 0000 0000 .rforpl.........
00000010: 2000 0000 0000 0000 0fb0 0000 0000 0000 ...............
00000020: 0000 0000 0000 0000 bb55 0300 0000 0000 .........U......
00000030: 0000 0000 0000 0000 ce76 6d00 0000 0000 .........vm.....
00000040: 2852 e5ff ffff ffff 8dd1 3959 5555 0000 (R........9YUU..
00000050: 0100 0000 0000 0000 1400 0000 0000 0000 ................
*/
static uint64_t base;
static uint64_t get_addr_of_symbol(const char* symbolname)
{
FILE *fp;
char addr[100];
char cmd[500];
snprintf(cmd, 500, "nm --defined-only -n %s | grep %s | cut -f1 -d ' '", getenv("LINK_OBJ_PATH"), symbolname);
fp = popen(cmd, "r");
if (fp == NULL) {
printf("Failed to run command %s\n", cmd);
exit(1);
}
/* Read the output a line at a time - output it. */
while (fgets(addr, sizeof(addr), fp) != NULL) {
printf("%s: %s", symbolname, addr);
return strtoll(addr, NULL, 16);
}
return NULL;
}
static uint64_t pdstart, pdstop, pdsize;
static uint64_t pcstart, pcstop, pcsize;
static uint64_t pnstart, pnstop, pnsize;
static uint8_t *pd, *pc, *pn;
void write_source_cov() {
// Write Header
static uint64_t header[11] = {};
size_t len;
size_t offset = 0;
if(!header[0]) {
uint64_t value;
// Magic
memcpy(&header[0], "\x81\x72\x66\x6f\x72\x70\x6c\xff", sizeof(uint64_t));
// Version
value = 0x8;
memcpy(&header[1], &value, sizeof(uint64_t));
// BinaryIdsSize (just copied from default.profraw)
value = 0x20;
value = 0;
memcpy(&header[2], &value, sizeof(uint64_t));
// DataSize
// __start___llvm_prf_data
// __stop___llvm_prf_data
// Divide by 8
value = pdsize/(8*6);
memcpy(&header[3], &value, sizeof(uint64_t));
// PaddingBytesBeforeCounters
value = 0;
memcpy(&header[4], &value, sizeof(uint64_t));
// CountersSize
// __start___llvm_prf_cnts
// __stop___llvm_prf_cnts
// Divide by 8
value = pcsize/8;
memcpy(&header[5], &value, sizeof(uint64_t));
// PaddingBytesAfterCounters
value = 0;
memcpy(&header[6], &value, sizeof(uint64_t));
// NamesSize
// __start___llvm_prf_names
// __stop___llvm_prf_names
value = pnsize;
memcpy(&header[7], &value, sizeof(uint64_t));
// CountersDelta
// Address of __start___llvm_prf_cnts
// Address of __start___llvm_prf_data
value = pcstart-pdstart;
memcpy(&header[8], &value, sizeof(uint64_t));
// CountersDelta
// Address of __start___llvm_prf_names
value = pnstart;
memcpy(&header[9], &value, sizeof(uint64_t));
// ValueKindLast
value = 1;
memcpy(&header[10], &value, sizeof(uint64_t));
len = pdsize;
offset = 0;
while (len) {
/* printf("Reading pd %lx\n", pd+offset); */
if(len> 0x1000) {
BX_CPU(0)->access_read_linear(pdstart+offset, 0x1000, 0, BX_READ, 0x0, pd + offset);
len -= 0x1000;
offset += 0x1000;
} else {
BX_CPU(0)->access_read_linear(pdstart+offset, len, 0, BX_READ, 0x0, pd + offset);
len = 0;
}
}
len = pnsize;
offset = 0;
while (len) {
/* printf("Reading pn %lx\n", pn+offset); */
if(len> 0x1000) {
BX_CPU(0)->access_read_linear(pnstart+offset, 0x1000, 0, BX_READ, 0x0, pn + offset);
len -= 0x1000;
offset += 0x1000;
} else {
BX_CPU(0)->access_read_linear(pnstart+offset, len, 0, BX_READ, 0x0, pn + offset);
len = 0;
}
}
}
len = pcsize;
offset = 0;
while (len) {
/* printf("Reading pc %lx\n", pc+offset); */
if(len> 0x1000) {
BX_CPU(0)->access_read_linear(pcstart+offset, 0x1000, 0, BX_READ, 0x0, pc + offset);
len -= 0x1000;
offset += 0x1000;
} else {
BX_CPU(0)->access_read_linear(pcstart+offset, len, 0, BX_READ, 0x0, pc + offset);
len = 0;
}
}
uint8_t padding[10] = {};
struct iovec iov[] = {
{header, sizeof(header)},
{pd, pdsize},
{pc, pcsize},
{pn, pnsize},
{padding, 8-((sizeof(header)+pdsize+pcsize+pnsize)%8)}
};
char filename[100];
sprintf(filename, "%d-%ld.profraw", getpid(), time(NULL));
int fd = open(filename, O_CREAT|O_RDWR, 0666);
writev(fd, iov, sizeof(iov)/sizeof(struct iovec));
close(fd);
}
void TERMhandler(int sig){
write_source_cov();
_exit(0);
}
void check_write_coverage(){
if(!master_fuzzer || !last_coverage_dump)
return;
uint64_t t = time(NULL);
if(t - last_coverage_dump < coverage_dump_precision)
return;
last_coverage_dump=t;
// following dump
write_source_cov();
}
static void sig_handler(int signum) {
uint64_t t = time(NULL);
switch (signum) {
case SIGALRM:
if(t - last_coverage_dump < coverage_dump_precision)
return;
last_coverage_dump=t;
// following dump
write_source_cov();
alarm(coverage_dump_precision);
break;
}
}
void init_sourcecov(size_t baseaddr) {
base = baseaddr;
pdstart = get_addr_of_symbol("__start___llvm_prf_data") + base;
pdstop = get_addr_of_symbol("__stop___llvm_prf_data") + base;
pcstart = get_addr_of_symbol("__start___llvm_prf_cnts") + base;
pcstop = get_addr_of_symbol("__stop___llvm_prf_cnts") + base;
pnstart = get_addr_of_symbol("__start___llvm_prf_names") + base;
pnstop = get_addr_of_symbol("__stop___llvm_prf_names") + base;
pdsize = pdstop-pdstart;
pcsize = pcstop-pcstart;
pnsize = pnstop-pnstart;
pd = (uint8_t*)malloc(pdsize);
pc = (uint8_t*)malloc(pcsize);
pn = (uint8_t*)malloc(pnsize);
memset(pc, 0, pcsize);
for(size_t page = (pcstart >> 12) << 12; page < pcstop; page += 0x1000){
size_t start, len;
if(pcstart - page < 0x1000)
start = pcstart;
else
start = page;
len = 0x1000 - (start & 0xFFF);
if(pcstop - page < 0x1000)
len = pcstop - page;
Bit32u lpf_mask = 0xfff; // 4K pages
Bit32u pkey = 0;
bx_phy_address phystart =
BX_CPU(0)->translate_linear_long_mode(start, lpf_mask, pkey, 0, BX_READ);
BX_CPU(0)->access_write_linear(start, len, 0, BX_WRITE, 0x0, pc);
phystart = (phystart & ~((Bit64u) lpf_mask)) | (start & lpf_mask);
add_persistent_memory_range(phystart, len);
}
/* std::atexit(write_source_cov); */
/* signal(SIGTERM, TERMhandler); */
}
extern uint64_t icount_limit_floor;
extern uint64_t icount_limit;
void setup_periodic_coverage(){
char linkpath[100];
readlink("/proc/self/fd/1", linkpath, 100);
linkpath[99] = 0;
if(strstr(linkpath, "fuzz-0.log")){
if(!getenv("NOCOV")) {
last_coverage_dump=time(NULL);
write_source_cov();
}
master_fuzzer = true;
} else if(strstr(linkpath, "fuzz-") && getenv("PROGRESSIVE_TIMEOUT")){
std::stringstream ss;
std::regex log_regex("fuzz-(.*).log");
std::smatch match;
std::string s = linkpath;
std::regex_search(s, match, log_regex);
assert(match.size() > 1);
ss << std::dec << match[1].str();
int val;
ss >> val;
unsigned long max = strtol(getenv("NSLOTS"), NULL, 10);
assert(val < max);
if(max != LONG_MIN){
icount_limit = icount_limit_floor + ((icount_limit-icount_limit_floor)/(max))*(val-1);
printf("SET ICOUNT LIMIT: %d\n", icount_limit);
}
}
}