Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion afl-fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

#if defined(__APPLE__) || defined(__FreeBSD__) || defined (__OpenBSD__)
# include <sys/sysctl.h>
# define HAVE_ARC4RANDOM 1
#endif /* __APPLE__ || __FreeBSD__ || __OpenBSD__ */

/* For systems that have sched_setaffinity; right now just Linux, but one
Expand Down Expand Up @@ -135,7 +136,9 @@ EXP_ST u8 skip_deterministic, /* Skip deterministic stages? */
fast_cal; /* Try to calibrate faster? */

static s32 out_fd, /* Persistent fd for out_file */
#ifndef HAVE_ARC4RANDOM
dev_urandom_fd = -1, /* Persistent fd for /dev/urandom */
#endif
dev_null_fd = -1, /* Persistent fd for /dev/null */
fsrv_ctl_fd, /* Fork server control pipe (write) */
fsrv_st_fd; /* Fork server status pipe (read) */
Expand Down Expand Up @@ -215,7 +218,9 @@ static u8 stage_val_type; /* Value type (STAGE_VAL_*) */
static u64 stage_finds[32], /* Patterns found per fuzz stage */
stage_cycles[32]; /* Execs per fuzz stage */

#ifndef HAVE_ARC4RANDOM
static u32 rand_cnt; /* Random number counter */
#endif

static u64 total_cal_us, /* Total calibration time (us) */
total_cal_cycles; /* Total calibration cycles */
Expand Down Expand Up @@ -364,7 +369,7 @@ static u64 get_cur_time_us(void) {
have slight bias. */

static inline u32 UR(u32 limit) {

#ifndef HAVE_ARC4RANDOM
if (unlikely(!rand_cnt--)) {

u32 seed[2];
Expand All @@ -377,6 +382,11 @@ static inline u32 UR(u32 limit) {
}

return random() % limit;
#else
/* The boundary not being necessarily a power of 2,
we need to ensure the result uniformity. */
return arc4random_uniform(limit);
#endif

}

Expand Down Expand Up @@ -2071,7 +2081,9 @@ EXP_ST void init_forkserver(char** argv) {

close(out_dir_fd);
close(dev_null_fd);
#ifndef HAVE_ARC4RANDOM
close(dev_urandom_fd);
#endif
close(fileno(plot_file));

/* This should improve performance a bit, since it stops the linker from
Expand Down Expand Up @@ -2343,7 +2355,9 @@ static u8 run_target(char** argv, u32 timeout) {

close(dev_null_fd);
close(out_dir_fd);
#ifndef HAVE_ARC4RANDOM
close(dev_urandom_fd);
#endif
close(fileno(plot_file));

/* Set sane defaults for ASAN if nothing else specified. */
Expand Down Expand Up @@ -7189,8 +7203,10 @@ EXP_ST void setup_dirs_fds(void) {
dev_null_fd = open("/dev/null", O_RDWR);
if (dev_null_fd < 0) PFATAL("Unable to open /dev/null");

#ifndef HAVE_ARC4RANDOM
dev_urandom_fd = open("/dev/urandom", O_RDONLY);
if (dev_urandom_fd < 0) PFATAL("Unable to open /dev/urandom");
#endif

/* Gnuplot output file. */

Expand Down