Skip to content

Commit 0aa40b5

Browse files
committed
cpu-exec: Use read_self_maps() for AFL_QEMU_INST_RANGES
Simplify parsing of AFL_QEMU_INST_RANGES to use the already existing read_self_maps() function.
1 parent e36a30e commit 0aa40b5

File tree

1 file changed

+28
-44
lines changed

1 file changed

+28
-44
lines changed

accel/tcg/cpu-exec.c

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "exec/tb-lookup.h"
3636
#include "exec/log.h"
3737
#include "qemu/main-loop.h"
38+
#include "qemu/selfmap.h"
3839
#if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
3940
#include "hw/i386/apic.h"
4041
#endif
@@ -408,57 +409,40 @@ void afl_setup(void) {
408409
}
409410

410411
if (have_names) {
411-
412-
FILE *fp;
413-
char *line = NULL;
414-
size_t len = 0;
415-
ssize_t read;
416-
417-
fp = fopen("/proc/self/maps", "r");
418-
if (fp == NULL) {
419-
fprintf(stderr, "[AFL] ERROR: cannot open /proc/self/maps\n");
420-
exit(1);
421-
}
422-
423-
while ((read = getline(&line, &len, fp)) != -1) {
424-
425-
int fields, dev_maj, dev_min, inode;
426-
uint64_t min, max, offset;
427-
char flag_r, flag_w, flag_x, flag_p;
428-
char path[512] = "";
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;
429415

430-
fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
431-
" %512s", &min, &max, &flag_r, &flag_w, &flag_x,
432-
&flag_p, &offset, &dev_maj, &dev_min, &inode, path);
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));
433420

434-
if ((fields < 10) || (fields > 11) || !flag_x || !h2g_valid(min))
435-
continue;
436-
437-
int flags = page_get_flags(h2g(min));
438-
439-
max = h2g_valid(max - 1) ? max : (uintptr_t)AFL_G2H(GUEST_ADDR_MAX) + 1;
440-
if (page_check_range(h2g(min), max - min, flags) == -1)
421+
max = h2g_valid(max - 1) ? max : (uintptr_t) AFL_G2H(GUEST_ADDR_MAX) + 1;
422+
423+
if (page_check_range(h2g(min), max - min, flags) == -1) {
441424
continue;
442-
443-
target_ulong gmin = h2g(min);
444-
target_ulong gmax = h2g(max);
445-
446-
struct vmrange* n = afl_instr_code;
447-
while (n) {
448-
if (n->name && strstr(path, n->name)) {
449-
if (gmin < n->start) n->start = gmin;
450-
if (gmax > n->end) n->end = gmax;
451-
break;
452425
}
453-
n = n->next;
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;
438+
}
439+
n = n->next;
440+
}
454441
}
455-
456442
}
457-
458-
fclose(fp);
459-
443+
free_self_maps(map_info);
460444
}
461-
445+
462446
if (getenv("AFL_DEBUG") && afl_instr_code) {
463447
struct vmrange* n = afl_instr_code;
464448
while (n) {

0 commit comments

Comments
 (0)