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 1 commit
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
4 changes: 4 additions & 0 deletions 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
40 changes: 35 additions & 5 deletions afl-fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@

#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__ */

Expand Down Expand Up @@ -405,14 +411,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 +490,26 @@ static void bind_to_free_cpu(void) {
}

closedir(d);
#else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be #elif __FreeBSD__?

struct kinfo_proc *procs;
size_t nprocs;
size_t proccount;
cpuset_t c;
int s_name[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"3" is not necessary here, better to remove

if (sysctl(s_name, 3, NULL, &nprocs, NULL, 0) < 0) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use magic numbers, please add a macro for calculating array length via sizeof(array) / sizeof(array[0]) and use it instead of literal 3

Copy link
Author

@devnexen devnexen Jul 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure but in fact I "mimicked" a similar sysctl call above (ie to get number of cpus) I usually try to fit the general style but not pb I can change.

proccount = nprocs / sizeof(*procs);
procs = ck_alloc(nprocs);
if (sysctl(s_name, 3, NULL, &nprocs, NULL, 0) < 0) goto procs_free;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


for (i = 0; i < proccount; i ++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove space between i and ++

if (procs[i].ki_oncpu < sizeof(cpu_used))
cpu_used[procs[i].ki_oncpu] = 1;
}

procs_free:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write this without a `goto please? Just branch on the line 502

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

ck_free(procs);

#endif

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

Expand All @@ -508,8 +533,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");
#else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would #elif __FreeBSD__ make sense here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well ... I think only NetBSD has this call too in case someone wants to port it too ... but I ll change it

if (pthread_setaffinity_np(pthread_self(), sizeof(c), &c))
PFATAL("pthread_setaffinity failed");
#endif

}

Expand Down