aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Carlier <devnexen@gmail.com>2019-10-03 20:27:00 +0100
committerDavid Carlier <devnexen@gmail.com>2019-10-04 03:52:39 +0100
commit670316f997826f4846b6c596903ea330c7a3a5d5 (patch)
tree4f8311a6c4fab94ed7af48208ecd2a8208ffcaa0
parentd544a5a94790b85dd2326d0776d4aab6567dfd50 (diff)
downloadafl++-670316f997826f4846b6c596903ea330c7a3a5d5.tar.gz
Binding to the first free cpu, porting to FreeBSD
-rw-r--r--include/afl-fuzz.h9
-rw-r--r--src/afl-fuzz-init.c33
2 files changed, 39 insertions, 3 deletions
diff --git a/include/afl-fuzz.h b/include/afl-fuzz.h
index 4912b3f0..ff26c894 100644
--- a/include/afl-fuzz.h
+++ b/include/afl-fuzz.h
@@ -78,8 +78,15 @@
/* 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
+#if defined(__FreeBSD__)
+#include <sys/cpuset.h>
+#include <sys/user.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#define cpu_set_t cpuset_t
+#endif
#endif /* __linux__ */
#ifndef SIMPLE_FILES
diff --git a/src/afl-fuzz-init.c b/src/afl-fuzz-init.c
index de16f901..cb340107 100644
--- a/src/afl-fuzz-init.c
+++ b/src/afl-fuzz-init.c
@@ -32,8 +32,6 @@
void bind_to_free_cpu(void) {
- DIR* d;
- struct dirent* de;
cpu_set_t c;
u8 cpu_used[4096] = {0};
@@ -48,6 +46,9 @@ void bind_to_free_cpu(void) {
}
+#if defined(__linux__)
+ DIR* d;
+ struct dirent* de;
d = opendir("/proc");
if (!d) {
@@ -112,6 +113,30 @@ void bind_to_free_cpu(void) {
}
closedir(d);
+#elif defined(__FreeBSD__)
+ struct kinfo_proc *procs;
+ size_t nprocs;
+ size_t proccount;
+ int s_name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
+ size_t s_name_l = sizeof(s_name)/sizeof(s_name[0]);
+
+ if (sysctl(s_name, s_name_l, NULL, &nprocs, NULL, 0) != 0) return;
+ proccount = nprocs / sizeof(*procs);
+ nprocs = nprocs * 4/3;
+
+ procs = ck_alloc(nprocs);
+ if (sysctl(s_name, s_name_l, procs, &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;
+ }
+
+ ck_free(procs);
+#endif
for (i = 0; i < cpu_core_count; ++i)
if (!cpu_used[i]) break;
@@ -138,7 +163,11 @@ void bind_to_free_cpu(void) {
CPU_ZERO(&c);
CPU_SET(i, &c);
+#if defined(__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
}