-
Notifications
You must be signed in to change notification settings - Fork 119
/
litecov.cpp
430 lines (339 loc) · 13.8 KB
/
litecov.cpp
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
/*
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https ://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define _CRT_SECURE_NO_WARNINGS
#include "litecov.h"
#include "common.h"
ModuleCovData::ModuleCovData() { ClearInstrumentationData(); }
// does not clear collected coverage and ignore coverage
void ModuleCovData::ClearInstrumentationData() {
coverage_buffer_remote = NULL;
coverage_buffer_size = 0;
coverage_buffer_next = 0;
has_remote_coverage = false;
buf_to_coverage.clear();
coverage_to_inst.clear();
ClearCmpCoverageData();
}
void LiteCov::Init(int argc, char **argv) {
TinyInst::Init(argc, argv);
coverage_type = COVTYPE_BB;
char *option = GetOption("-covtype", argc, argv);
if (option) {
if (strcmp(option, "bb") == 0)
coverage_type = COVTYPE_BB;
else if (strcmp(option, "edge") == 0)
coverage_type = COVTYPE_EDGE;
else
FATAL("Unknown coverage type");
}
compare_coverage = GetBinaryOption("-cmp_coverage", argc, argv, false);
for (ModuleInfo *module : instrumented_modules) {
module->client_data = new ModuleCovData();
}
}
void LiteCov::OnModuleInstrumented(ModuleInfo *module) {
TinyInst::OnModuleInstrumented(module);
if(!module->client_data) {
module->client_data = new ModuleCovData();
}
ModuleCovData *data = (ModuleCovData *)module->client_data;
data->ClearInstrumentationData();
data->coverage_buffer_size = COVERAGE_SIZE;
if (!data->coverage_buffer_size) {
data->coverage_buffer_size = module->code_size;
}
// map as readonly initially
// this causes an exception the first time coverage is written to the buffer
// this enables us to quickly determine if we had new coverage or not
data->coverage_buffer_remote =
#ifdef ARM64
(unsigned char *)RemoteAllocate(data->coverage_buffer_size, READONLY, true);
#else
(unsigned char *)RemoteAllocateNear(
(uint64_t)module->instrumented_code_remote,
(uint64_t)module->instrumented_code_remote +
module->instrumented_code_size,
data->coverage_buffer_size, READONLY, true);
#endif
if (!data->coverage_buffer_remote) {
FATAL("Could not allocate coverage buffer");
}
}
void LiteCov::OnModuleUninstrumented(ModuleInfo *module) {
TinyInst::OnModuleUninstrumented(module);
ModuleCovData *data = (ModuleCovData *)module->client_data;
CollectCoverage(data);
if (data->coverage_buffer_remote && IsTargetAlive()) {
RemoteFree(data->coverage_buffer_remote, data->coverage_buffer_size);
data->coverage_buffer_remote = NULL;
}
data->ClearInstrumentationData();
}
// just replaces the inserted instructions with NOPs
void LiteCov::ClearCoverageInstrumentation(ModuleInfo *module,
uint64_t coverage_code) {
ModuleCovData *data = (ModuleCovData *)module->client_data;
auto iter = data->coverage_to_inst.find(coverage_code);
if (iter == data->coverage_to_inst.end()) {
if (compare_coverage) {
ClearCmpCoverageInstrumentation(module, coverage_code);
}
return;
}
size_t code_offset = iter->second;
NopCovInstructions(module, code_offset);
data->coverage_to_inst.erase(iter);
}
uint64_t LiteCov::GetCmpCode(size_t bb_offset, size_t cmp_offset,
int bits_match) {
// cmp code consists of bb offset in the highest 32 bits
// cmp instruction offset (from bb start) in the next 24 bits
// and bits to match in the lowest 8 bits
uint64_t code = (((uint64_t)bb_offset << 32) +
((cmp_offset << 8) & 0xFFFFFFFFUL) + bits_match);
// it also has the highest bit set
code |= 0x8000000000000000ULL;
return code;
}
bool LiteCov::IsCmpCoverageCode(uint64_t code) {
return (code & 0x8000000000000000ULL);
}
void LiteCov::EmitCoverageInstrumentation(ModuleInfo *module,
uint64_t coverage_code) {
ModuleCovData *data = (ModuleCovData *)module->client_data;
// don't instrument if we are ignoring this bit of coverage
if (data->ignore_coverage.find(coverage_code) != data->ignore_coverage.end())
return;
if (data->coverage_buffer_next == data->coverage_buffer_size) {
WARN("Coverage buffer full\n");
return;
}
if (data->coverage_to_inst.find(coverage_code) !=
data->coverage_to_inst.end()) {
WARN("Edge %llx already exists", coverage_code);
return;
}
data->buf_to_coverage[data->coverage_buffer_next] = coverage_code;
data->coverage_to_inst[coverage_code] = module->instrumented_code_allocated;
size_t bit_address =
(size_t)data->coverage_buffer_remote + data->coverage_buffer_next;
size_t mov_address = GetCurrentInstrumentedAddress(module);
data->coverage_buffer_next++;
EmitCoverageInstrumentation(module, bit_address, mov_address);
}
void LiteCov::InstrumentBasicBlock(ModuleInfo *module, size_t bb_address) {
if (coverage_type != COVTYPE_BB) return;
uint64_t coverage_code = GetBBCode(module, bb_address);
EmitCoverageInstrumentation(module, coverage_code);
}
void LiteCov::InstrumentEdge(ModuleInfo *previous_module,
ModuleInfo *next_module, size_t previous_address,
size_t next_address) {
if (coverage_type != COVTYPE_EDGE) return;
// don't do anything on cross-module edges
if (previous_module != next_module) return;
uint64_t coverage_code =
GetEdgeCode(previous_module, previous_address, next_address);
EmitCoverageInstrumentation(previous_module, coverage_code);
}
// basic block code is just offset from the start of the module
uint64_t LiteCov::GetBBCode(ModuleInfo *module, size_t bb_address) {
return ((uint64_t)bb_address - (uint64_t)module->min_address);
}
// edge code has previous offset in higher 32 bits
// and next offset in the lower 32 bits
// note that source address can be 0 if we don't know it
// (module entries, indirect jumps using global jumptable)
uint64_t LiteCov::GetEdgeCode(ModuleInfo *module, size_t edge_address1,
size_t edge_address2) {
uint64_t offset1 = 0;
if (edge_address1)
offset1 = ((uint64_t)edge_address1 - (uint64_t)module->min_address);
uint64_t offset2 = 0;
if (edge_address2)
offset2 = ((uint64_t)edge_address2 - (uint64_t)module->min_address);
return ((offset1 << 32) + (offset2 & 0xFFFFFFFF));
}
void LiteCov::OnModuleEntered(ModuleInfo *module, size_t entry_address) {
if (coverage_type == COVTYPE_BB) return;
// if we are in edge coverage mode, record module entries as edges
// we don't know the source address (it's in another module)
// so treat it as zero
ModuleCovData *data = (ModuleCovData *)module->client_data;
uint64_t coverage_code = GetEdgeCode(module, 0, entry_address);
if (data->ignore_coverage.count(coverage_code) != 0) return;
data->collected_coverage.insert(coverage_code);
}
// checks if address is in any of our remote coverage buffers
ModuleCovData *LiteCov::GetDataByRemoteAddress(size_t address) {
for (ModuleInfo *module : instrumented_modules) {
ModuleCovData *data = (ModuleCovData *)module->client_data;
if (!data->coverage_buffer_remote) continue;
if ((address >= (size_t)data->coverage_buffer_remote) &&
(address <
((size_t)data->coverage_buffer_remote + data->coverage_buffer_size))) {
return data;
}
}
return NULL;
}
// catches writing to the coverage buffer for the first time
void LiteCov::HandleBufferWriteException(ModuleCovData *data) {
RemoteProtect(data->coverage_buffer_remote, data->coverage_buffer_size,
READWRITE);
data->has_remote_coverage = true;
}
bool LiteCov::OnException(Exception *exception_record) {
if ((exception_record->type == ACCESS_VIOLATION) &&
(exception_record->maybe_write_violation)) {
ModuleCovData *data =
GetDataByRemoteAddress((size_t)exception_record->access_address);
if (data) {
HandleBufferWriteException(data);
return true;
}
}
return TinyInst::OnException(exception_record);
}
void LiteCov::ClearRemoteBuffer(ModuleCovData *data) {
if (!IsTargetAlive()) return;
if (!data->coverage_buffer_remote) return;
if (!data->has_remote_coverage) return;
unsigned char *buf = (unsigned char *)malloc(data->coverage_buffer_next);
memset(buf, 0, data->coverage_buffer_next);
RemoteWrite(data->coverage_buffer_remote, buf, data->coverage_buffer_next);
RemoteProtect(data->coverage_buffer_remote, data->coverage_buffer_size,
READONLY);
data->has_remote_coverage = false;
free(buf);
}
void LiteCov::ClearCoverage(ModuleCovData *data) {
data->collected_coverage.clear();
ClearRemoteBuffer(data);
}
void LiteCov::ClearCoverage() {
for (ModuleInfo *module : instrumented_modules) {
ModuleCovData *data = (ModuleCovData *)module->client_data;
ClearCoverage(data);
}
}
// fetches and decodes coverage from the remote buffer
void LiteCov::CollectCoverage(ModuleCovData *data) {
if (!data->has_remote_coverage) return;
unsigned char *buf = (unsigned char *)malloc(data->coverage_buffer_next);
RemoteRead(data->coverage_buffer_remote, buf, data->coverage_buffer_next);
for (size_t i = 0; i < data->coverage_buffer_next; i++) {
if (buf[i]) {
auto iter = data->buf_to_coverage.find(i);
if (iter == data->buf_to_coverage.end()) {
if (compare_coverage) {
CollectCmpCoverage(data, i, buf[i]);
}
} else {
uint64_t coverage_code = iter->second;
data->collected_coverage.insert(coverage_code);
}
}
}
free(buf);
ClearRemoteBuffer(data);
data->has_remote_coverage = false;
}
void LiteCov::CollectCoverage() {
for (ModuleInfo *module : instrumented_modules) {
ModuleCovData *data = (ModuleCovData *)module->client_data;
CollectCoverage(data);
}
}
void LiteCov::GetCoverage(Coverage &coverage, bool clear_coverage) {
CollectCoverage();
for (ModuleInfo *module : instrumented_modules) {
ModuleCovData *data = (ModuleCovData *)module->client_data;
if (data->collected_coverage.empty()) continue;
// check if that module is already in the coverage list
// (if the client calls with non-empty initial coverage)
ModuleCoverage *module_coverage =
GetModuleCoverage(coverage, module->module_name);
if (module_coverage) {
module_coverage->offsets.insert(data->collected_coverage.begin(),
data->collected_coverage.end());
} else {
coverage.push_back({module->module_name, data->collected_coverage});
}
}
if (clear_coverage) ClearCoverage();
}
// sets (new) coverage to ignore
void LiteCov::IgnoreCoverage(Coverage &coverage) {
for (const ModuleCoverage &mod_cov : coverage) {
ModuleInfo *module = GetModuleByName(mod_cov.module_name.c_str());
if (!module) continue;
ModuleCovData *data = (ModuleCovData *)module->client_data;
// remember the offsets so they don't get instrumented later
data->ignore_coverage.insert(mod_cov.offsets.begin(),
mod_cov.offsets.end());
if (!module->instrumented) continue;
// if we already have instrumentation in place for some of the offsets
// remove it here
for (const uint64_t code_off : mod_cov.offsets) {
ClearCoverageInstrumentation(module, code_off);
}
}
}
// quickly checks if we have new coverage
bool LiteCov::HasNewCoverage() {
for (ModuleInfo *module : instrumented_modules) {
ModuleCovData *data = (ModuleCovData *)module->client_data;
if (!data->collected_coverage.empty()) return true;
if (data->has_remote_coverage) return true;
}
return false;
}
void LiteCov::OnProcessExit() {
CollectCoverage();
TinyInst::OnProcessExit();
}
void ModuleCovData::ClearCmpCoverageData() {
for (auto &[buffer_offset, cmp_record] : buf_to_cmp) {
delete cmp_record;
}
buf_to_cmp.clear();
coverage_to_cmp.clear();
}
void LiteCov::ClearCmpCoverageInstrumentation(ModuleInfo *module,
uint64_t coverage_code) {
if (!IsCmpCoverageCode(coverage_code)) return;
ModuleCovData *data = (ModuleCovData *)module->client_data;
int matched_width = coverage_code & 0xFF;
coverage_code -= matched_width;
auto iter = data->coverage_to_cmp.find(coverage_code);
if (iter == data->coverage_to_cmp.end()) return;
CmpCoverageRecord *cmp_record = iter->second;
if (cmp_record->ignored) return;
NopCmpCovInstructions(module, *cmp_record, matched_width);
}
void LiteCov::CollectCmpCoverage(ModuleCovData *data, size_t buffer_offset,
char buffer_value) {
auto iter = data->buf_to_cmp.find(buffer_offset);
if (iter == data->buf_to_cmp.end()) return;
CmpCoverageRecord *cmp_record = iter->second;
if (cmp_record->ignored) return;
if (buffer_value < cmp_record->match_width) return;
for (int match = cmp_record->match_width; match <= buffer_value; match += 8) {
// full match, no need to record it
if (match == cmp_record->width) break;
uint64_t coverage_code =
GetCmpCode(cmp_record->bb_offset, cmp_record->cmp_offset, match);
data->collected_coverage.insert(coverage_code);
}
}