Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

FreeBSD, implementing binding to free cpu. #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ ifneq "$(filter Linux GNU%,$(shell uname))" ""
LDFLAGS += -ldl
endif

ifneq "$(filter FreeBSD GNU%,$(shell uname))" ""
LDFLAGS += -pthread
endif

ifeq "$(findstring clang, $(shell $(CC) --version 2>/dev/null))" ""
TEST_CC = afl-gcc
else
Expand Down Expand Up @@ -86,11 +90,14 @@ afl-gotcpu: afl-gotcpu.c $(COMM_HDR) | test_x86

ifndef AFL_NO_X86

test_build: afl-gcc afl-as afl-showmap
test_build: afl-gcc afl-as afl-showmap afl-fuzz
@echo "[*] Testing the CC wrapper and instrumentation output..."
unset AFL_USE_ASAN AFL_USE_MSAN; AFL_QUIET=1 AFL_INST_RATIO=100 AFL_PATH=. ./$(TEST_CC) $(CFLAGS) test-instr.c -o test-instr $(LDFLAGS)
echo 0 | ./afl-showmap -m none -q -o .test-instr0 ./test-instr
echo 1 | ./afl-showmap -m none -q -o .test-instr1 ./test-instr
mkdir -p .out
./afl-fuzz -i testcases/others/elf -o .out -- /dev/null | grep 'Found a free CPU core'
@rm -rf .out
@rm -f test-instr
@cmp -s .test-instr0 .test-instr1; DR="$$?"; rm -f .test-instr0 .test-instr1; if [ "$$DR" = "0" ]; then echo; echo "Oops, the instrumentation does not seem to be behaving correctly!"; echo; echo "Please ping <[email protected]> to troubleshoot the issue."; echo; exit 1; fi
@echo "[+] All right, the instrumentation seems to be working!"
Expand Down
46 changes: 39 additions & 7 deletions afl-fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,23 @@

#if defined(__APPLE__) || defined(__FreeBSD__) || defined (__OpenBSD__)
# include <sys/sysctl.h>
# ifdef __FreeBSD__
# include <sys/user.h>
# include <sys/cpuset.h>
# include <pthread.h>
# include <pthread_np.h>
# endif
#endif /* __APPLE__ || __FreeBSD__ || __OpenBSD__ */

/* For systems that have sched_setaffinity; right now just Linux, but one
can hope... */

#ifdef __linux__
#if defined(__linux__) || defined(__FreeBSD__)
# define HAVE_AFFINITY 1
#endif /* __linux__ */

#define __arraysize(arr) (sizeof(arr)/sizeof(arr[0]))

/* A toggle to export some variables when building as a library. Not very
useful for the general public. */

Expand Down Expand Up @@ -405,14 +413,13 @@ static void shuffle_ptrs(void** ptrs, u32 cnt) {
can be found. Assumes an upper bound of 4k CPUs. */

static void bind_to_free_cpu(void) {

u8 cpu_used[4096] = { 0 };
u32 i;
#ifdef __linux__
DIR* d;
struct dirent* de;
cpu_set_t c;

u8 cpu_used[4096] = { 0 };
u32 i;

if (cpu_core_count < 2) return;

if (getenv("AFL_NO_AFFINITY")) {
Expand Down Expand Up @@ -485,6 +492,26 @@ static void bind_to_free_cpu(void) {
}

closedir(d);
#elif defined(__FreeBSD__)
struct kinfo_proc *procs;
size_t nprocs;
size_t proccount;
cpuset_t c;
int s_name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
if (sysctl(s_name, __arraysize(s_name), NULL, &nprocs, NULL, 0) < 0) return;
proccount = nprocs / sizeof(*procs);
procs = ck_alloc(nprocs);
if (sysctl(s_name, __arraysize(s_name), NULL, &nprocs, NULL, 0) < 0) {
ck_free(procs);
return;
}

for (i = 0; i < proccount; i++) {
if (procs[i].ki_oncpu < sizeof(cpu_used))
cpu_used[procs[i].ki_oncpu] = 1;
}

#endif

for (i = 0; i < cpu_core_count; i++) if (!cpu_used[i]) break;

Expand All @@ -508,8 +535,13 @@ static void bind_to_free_cpu(void) {
CPU_ZERO(&c);
CPU_SET(i, &c);

#ifdef __linux__
if (sched_setaffinity(0, sizeof(c), &c))
PFATAL("sched_setaffinity failed");
#elif defined(__FreeBSD__)
if (pthread_setaffinity_np(pthread_self(), sizeof(c), &c))
PFATAL("pthread_setaffinity failed");
#endif

}

Expand Down Expand Up @@ -7368,9 +7400,9 @@ static void get_core_count(void) {

#else

int s_name[2] = { CTL_HW, HW_NCPU };
int s_name[] = { CTL_HW, HW_NCPU };

if (sysctl(s_name, 2, &cpu_core_count, &s, NULL, 0) < 0) return;
if (sysctl(s_name, __arraysize(s_name), &cpu_core_count, &s, NULL, 0) < 0) return;

#endif /* ^__APPLE__ */

Expand Down