Skip to content

Commit fe6a8da

Browse files
committed
cpu-exec: Split parsing of AFL_QEMU_INST_RANGES
Split parsing of AFL_QEMU_INST_RANGES into a section that parses the environment variable and a section which extracts the address ranges from library names.
1 parent 0aa40b5 commit fe6a8da

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

accel/tcg/cpu-exec.c

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,11 @@ void afl_setup(void) {
374374
if (getenv("AFL_CODE_END"))
375375
afl_end_code = strtoll(getenv("AFL_CODE_END"), NULL, 16);
376376

377+
int have_names = 0;
377378
if (getenv("AFL_QEMU_INST_RANGES")) {
378-
379379
char *str = getenv("AFL_QEMU_INST_RANGES");
380380
char *saveptr1, *saveptr2 = NULL;
381381
char *pt1, *pt2, *pt3 = NULL;
382-
int have_names = 0;
383382

384383
while (1) {
385384

@@ -390,7 +389,7 @@ void afl_setup(void) {
390389
pt2 = strtok_r(pt1, "-", &saveptr2);
391390
pt3 = strtok_r(NULL, "-", &saveptr2);
392391

393-
struct vmrange* n = malloc(sizeof(struct vmrange));
392+
struct vmrange* n = calloc(1, sizeof(struct vmrange));
394393
n->next = afl_instr_code;
395394

396395
if (pt3 == NULL) { // filename
@@ -407,52 +406,51 @@ void afl_setup(void) {
407406
afl_instr_code = n;
408407

409408
}
410-
411-
if (have_names) {
412-
GSList *map_info = read_self_maps();
413-
for (GSList *s = map_info; s; s = g_slist_next(s)) {
414-
MapInfo *e = (MapInfo *) s->data;
409+
}
415410

416-
if (h2g_valid(e->start)) {
417-
unsigned long min = e->start;
418-
unsigned long max = e->end;
419-
int flags = page_get_flags(h2g(min));
411+
if (have_names) {
412+
GSList *map_info = read_self_maps();
413+
for (GSList *s = map_info; s; s = g_slist_next(s)) {
414+
MapInfo *e = (MapInfo *) s->data;
420415

421-
max = h2g_valid(max - 1) ? max : (uintptr_t) AFL_G2H(GUEST_ADDR_MAX) + 1;
416+
if (h2g_valid(e->start)) {
417+
unsigned long min = e->start;
418+
unsigned long max = e->end;
419+
int flags = page_get_flags(h2g(min));
422420

423-
if (page_check_range(h2g(min), max - min, flags) == -1) {
424-
continue;
425-
}
421+
max = h2g_valid(max - 1) ? max : (uintptr_t) AFL_G2H(GUEST_ADDR_MAX) + 1;
426422

427-
// Now that we have a valid guest address region, compare its
428-
// name against the names we care about:
429-
target_ulong gmin = h2g(min);
430-
target_ulong gmax = h2g(max);
431-
432-
struct vmrange* n = afl_instr_code;
433-
while (n) {
434-
if (n->name && strstr(e->path, n->name)) {
435-
if (gmin < n->start) n->start = gmin;
436-
if (gmax > n->end) n->end = gmax;
437-
break;
438-
}
439-
n = n->next;
423+
if (page_check_range(h2g(min), max - min, flags) == -1) {
424+
continue;
425+
}
426+
427+
// Now that we have a valid guest address region, compare its
428+
// name against the names we care about:
429+
target_ulong gmin = h2g(min);
430+
target_ulong gmax = h2g(max);
431+
432+
struct vmrange* n = afl_instr_code;
433+
while (n) {
434+
if (n->name && strstr(e->path, n->name)) {
435+
if (gmin < n->start) n->start = gmin;
436+
if (gmax > n->end) n->end = gmax;
437+
break;
440438
}
439+
n = n->next;
441440
}
442441
}
443-
free_self_maps(map_info);
444442
}
443+
free_self_maps(map_info);
444+
}
445445

446-
if (getenv("AFL_DEBUG") && afl_instr_code) {
447-
struct vmrange* n = afl_instr_code;
448-
while (n) {
449-
fprintf(stderr, "Instrument range: 0x%lx-0x%lx (%s)\n",
450-
(unsigned long)n->start, (unsigned long)n->end,
451-
n->name ? n->name : "<noname>");
452-
n = n->next;
453-
}
446+
if (getenv("AFL_DEBUG") && afl_instr_code) {
447+
struct vmrange* n = afl_instr_code;
448+
while (n) {
449+
fprintf(stderr, "Instrument range: 0x%lx-0x%lx (%s)\n",
450+
(unsigned long)n->start, (unsigned long)n->end,
451+
n->name ? n->name : "<noname>");
452+
n = n->next;
454453
}
455-
456454
}
457455

458456
/* Maintain for compatibility */

0 commit comments

Comments
 (0)