Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
portability fixes (seen on DragonFlyBSD)
Browse files Browse the repository at this point in the history
  • Loading branch information
heiko authored and heiko committed Oct 6, 2020
1 parent 9275f2b commit 0c47c14
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
4 changes: 3 additions & 1 deletion examples/libaflfuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>

#include "aflpp.h"
Expand Down Expand Up @@ -309,7 +310,8 @@ static void handle_crash(int sig, siginfo_t *info, void *ucontext) {

static void setup_signal_handlers(void) {

struct sigaction sa = {{0},{0},0,0};
struct sigaction sa;
sa.sa_sigaction = NULL;

memset(&sa, 0, sizeof(sigaction));
sigemptyset(&sa.sa_mask);
Expand Down
52 changes: 36 additions & 16 deletions src/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@
#include "engine.h"
#include "xxh3.h"

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
defined(__NetBSD__) || defined(__DragonFly__)
#include <sys/sysctl.h>
#endif /* __APPLE__ || __FreeBSD__ || __OpenBSD__ */

#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__DragonFly__) || defined(__sun)
#define HAVE_AFFINITY 1
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/param.h>
#if defined(__FreeBSD__)
#include <sys/cpuset.h>
#endif
#include <sys/user.h>
#include <pthread.h>
#include <pthread_np.h>
#define cpu_set_t cpuset_t
#elif defined(__NetBSD__)
#include <pthread.h>
#elif defined(__sun)
#include <sys/types.h>
#include <kstat.h>
#include <sys/sysinfo.h>
#include <sys/pset.h>
#endif
#endif /* __linux__ */

// Process related functions

void _afl_process_init_internal(afl_os_t *afl_os) {
Expand Down Expand Up @@ -315,13 +342,13 @@ s32 get_core_count() {
#ifdef __APPLE__

if (sysctlbyname("hw.logicalcpu", &cpu_core_count, &s, NULL, 0) < 0)
return;
return 0;

#else

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

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

#endif /* ^__APPLE__ */

Expand Down Expand Up @@ -396,6 +423,8 @@ afl_ret_t bind_to_cpu() {

u8 cpu_used[4096];

s32 i;

#if defined(__linux__)

// Let's open up /proc and check if there are any CPU cores available
Expand Down Expand Up @@ -449,13 +478,12 @@ afl_ret_t bind_to_cpu() {

struct kinfo_proc *procs;
size_t nprocs;
size_t proccount;
s32 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) {

if (lockfile[0]) unlink(lockfile);
return AFL_RET_UNKNOWN_ERROR;

}
Expand All @@ -466,9 +494,8 @@ afl_ret_t bind_to_cpu() {
procs = ck_alloc(nprocs);
if (sysctl(s_name, s_name_l, procs, &nprocs, NULL, 0) != 0) {

if (lockfile[0]) unlink(lockfile);
ck_free(procs);
return;
return AFL_RET_UNKNOWN_ERROR;

}

Expand All @@ -488,7 +515,7 @@ afl_ret_t bind_to_cpu() {

#elif defined(__DragonFly__)

if (procs[i].kp_lwp.kl_cpuid < sizeof(cpu_used) &&
if (procs[i].kp_lwp.kl_cpuid < (s32)(sizeof(cpu_used)) &&
procs[i].kp_lwp.kl_pctcpu > 10)
cpu_used[procs[i].kp_lwp.kl_cpuid] = 1;

Expand All @@ -510,7 +537,6 @@ afl_ret_t bind_to_cpu() {

if (sysctl(s_name, s_name_l, NULL, &nprocs, NULL, 0) != 0) {

if (lockfile[0]) unlink(lockfile);
return AFL_RET_UNKNOWN_ERROR;

}
Expand All @@ -521,9 +547,8 @@ afl_ret_t bind_to_cpu() {

if (sysctl(s_name, s_name_l, procs, &nprocs, NULL, 0) != 0) {

if (lockfile[0]) unlink(lockfile);
ck_free(procs);
return;
return AFL_RET_UNKNOWN_ERROR;

}

Expand Down Expand Up @@ -552,15 +577,13 @@ afl_ret_t bind_to_cpu() {

if (!k) {

if (lockfile[0]) unlink(lockfile);
kstat_close(m);
return AFL_RET_UNKNOWN_ERROR;

}

if (kstat_read(m, k, NULL)) {

if (lockfile[0]) unlink(lockfile);
kstat_close(m);
return AFL_RET_UNKNOWN_ERROR;

Expand All @@ -576,7 +599,6 @@ afl_ret_t bind_to_cpu() {
k = kstat_lookup(m, "cpu_stat", i, NULL);
if (kstat_read(m, k, &cs)) {

if (lockfile[0]) unlink(lockfile);
kstat_close(m);
return AFL_RET_UNKNOWN_ERROR;

Expand All @@ -593,13 +615,11 @@ afl_ret_t bind_to_cpu() {

#else
#warning \
"For this platform we do not have free CPU binding code yet. If possible, please supply a PR to https://github.com/AFLplusplus/AFLplusplus"
"For this platform we do not have free CPU binding code yet. If possible, please supply a PR to https://github.com/AFLplusplus/libAFL"
#endif
size_t cpu_start = 0;
s32 cpu_core_count = get_core_count();

s32 i;

#if !defined(__ANDROID__)

for (i = cpu_start; i < cpu_core_count; i++) {
Expand Down

0 comments on commit 0c47c14

Please sign in to comment.