Skip to content

Commit

Permalink
[LibFuzzer] Fix NumberOfCpuCores() on Mac OSX.
Browse files Browse the repository at this point in the history
The ``nprocs`` command does not exist under Mac OSX so use
``sysctl`` instead on that platform.

Whilst I'm here

* Use ``pclose()`` instead of ``fclose()`` which the ``popen()``
  documentation says should be used.
* Check for errors that were previously unhandled.

Differential Revision: http://reviews.llvm.org/D20409

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270172 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
delcypher committed May 20, 2016
1 parent c776d19 commit adef878
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions lib/Fuzzer/FuzzerUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,36 @@ void SetSigIntHandler() { SetSigaction(SIGINT, InterruptHandler); }
void SetSigTermHandler() { SetSigaction(SIGTERM, InterruptHandler); }

int NumberOfCpuCores() {
FILE *F = popen("nproc", "r");
int N = 0;
if (fscanf(F, "%d", &N) != 1)
const char *CmdLine = nullptr;
if (LIBFUZZER_LINUX) {
CmdLine = "nproc";
} else if (LIBFUZZER_APPLE) {
CmdLine = "sysctl -n hw.ncpu";
} else {
assert(0 && "NumberOfCpuCores() is not implemented for your platform");
}

FILE *F = popen(CmdLine, "r");
int N = 1;
if (!F || fscanf(F, "%d", &N) != 1) {
Printf("WARNING: Failed to parse output of command \"%s\" in %s(). "
"Assuming CPU count of 1.\n",
CmdLine, __func__);
N = 1;
}

if (pclose(F)) {
Printf("WARNING: Executing command \"%s\" failed in %s(). "
"Assuming CPU count of 1.\n",
CmdLine, __func__);
N = 1;
}
if (N < 1) {
Printf("WARNING: Reported CPU count (%d) from command \"%s\" was invalid "
"in %s(). Assuming CPU count of 1.\n",
N, CmdLine, __func__);
N = 1;
fclose(F);
}
return N;
}

Expand Down

0 comments on commit adef878

Please sign in to comment.