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

Search for other free cores when bind fails #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 48 additions & 15 deletions afl-fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,30 +488,63 @@ static void bind_to_free_cpu(void) {

closedir(d);

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

if (i == cpu_core_count) {
int bound = 0;
int tried_bind = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

rename to bind_attempts

int saved_errno = 0;

SAYF("\n" cLRD "[-] " cRST
"Uh-oh, looks like all %u CPU cores on your system are allocated to\n"
" other instances of afl-fuzz (or similar CPU-locked tasks). Starting\n"
" another fuzzer on this machine is probably a bad plan, but if you are\n"
" absolutely sure, you can set AFL_NO_AFFINITY and try again.\n",
cpu_core_count);
for (i = 0; i < cpu_core_count; i++) {

if (!cpu_used[i]) {
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 decrease the indentation by inverting the condition:

for (i = 0; i < cpu_core_count; i++) {
  if (cpu_used[i])
    continue;

  <the code trying to bind>
}


OKF("Found a free CPU core, attempting bind to #%u.", i);

CPU_ZERO(&c);
CPU_SET(i, &c);

if (sched_setaffinity(0, sizeof(c), &c)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

similar suggestion here:

  if (!sched_setaffinity(0, sizeof(c), &c)) {
    cpu_aff = i;
    bound = 1;
    break;
  }

  saved_errno = errno;
  tried_bind++;
  WARNF("Binding attempt failed; looking for another core...");


FATAL("No more free CPU cores");
saved_errno = errno;
tried_bind++;
WARNF("Binding attempt failed; looking for another core...");

} else {

cpu_aff = i;
bound = 1;
break;

}
}
}

OKF("Found a free CPU core, binding to #%u.", i);
if (!bound) {
Copy link
Contributor

Choose a reason for hiding this comment

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

  ...
  if (bound)
    return;

  <error reporting code here>
}


cpu_aff = i;
if (tried_bind == 0) {

CPU_ZERO(&c);
CPU_SET(i, &c);
SAYF("\n" cLRD "[-] " cRST
"Uh-oh, looks like all %u CPU cores on your system are allocated to\n"
" other instances of afl-fuzz (or similar CPU-locked tasks). Starting\n"
" another fuzzer on this machine is probably a bad plan, but if you are\n"
" absolutely sure, you can set AFL_NO_AFFINITY and try again.\n\n",
cpu_core_count);
FATAL("No more free CPU cores");

if (sched_setaffinity(0, sizeof(c), &c))
PFATAL("sched_setaffinity failed");
} else {

SAYF("\n" cLRD "[-] " cRST
"Uh-oh, afl-fuzz found %u apparently free CPU cores, but the system\n"
" wouldn't let us bind to any of them. This can happen if we do not\n"
" have CAP_SYS_NICE, or if we are running in a container that is\n"
" restricted to a certain set of CPUs that already have processes bound\n"
" to them. For a quick workaround, set AFL_NO_AFFINITY and try again.\n",
tried_bind);

errno = saved_errno;
PFATAL("sched_setaffinity failed");

}
}

}

Expand Down