about summary refs log tree commit diff
path: root/src/afl-fuzz-init.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/afl-fuzz-init.c')
-rw-r--r--src/afl-fuzz-init.c52
1 files changed, 46 insertions, 6 deletions
diff --git a/src/afl-fuzz-init.c b/src/afl-fuzz-init.c
index de16f901..12dcafae 100644
--- a/src/afl-fuzz-init.c
+++ b/src/afl-fuzz-init.c
@@ -32,9 +32,7 @@
 
 void bind_to_free_cpu(void) {
 
-  DIR*           d;
-  struct dirent* de;
-  cpu_set_t      c;
+  cpu_set_t c;
 
   u8  cpu_used[4096] = {0};
   u32 i;
@@ -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,36 @@ 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);
+#else
+#warning \
+    "For this platform we do not have free CPU binding code yet. If poxxible, please supply a PR to https://github.com/vanhauser-thc/AFLplusplus"
+#endif
 
   for (i = 0; i < cpu_core_count; ++i)
     if (!cpu_used[i]) break;
@@ -138,7 +169,14 @@ 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");
+#else
+  // this will need something for other platforms
+#endif
 
 }
 
@@ -786,7 +824,8 @@ double get_runnable_processes(void) {
 
   static double res;
 
-#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
+    defined(__NetBSD__)
 
   /* I don't see any portable sysctl or so that would quickly give us the
      number of runnable processes; the 1-minute load average can be a
@@ -827,7 +866,7 @@ double get_runnable_processes(void) {
 
   }
 
-#endif                        /* ^(__APPLE__ || __FreeBSD__ || __OpenBSD__ || __NetBSD__) */
+#endif          /* ^(__APPLE__ || __FreeBSD__ || __OpenBSD__ || __NetBSD__) */
 
   return res;
 
@@ -1481,7 +1520,7 @@ void check_cpu_governor(void) {
   FATAL("Suboptimal CPU scaling governor");
 
 #elif defined __APPLE__
-  u64   min = 0, max = 0;
+  u64 min = 0, max = 0;
   size_t mlen = sizeof(min);
   if (getenv("AFL_SKIP_CPUFREQ")) return;
 
@@ -1513,6 +1552,7 @@ void check_cpu_governor(void) {
        min / 1024, max / 1024);
   FATAL("Suboptimal CPU scaling governor");
 #endif
+
 }
 
 /* Count the number of logical CPU cores. */