Skip to content

Commit e6ac1fd

Browse files
committedMay 20, 2016
[LibFuzzer] Fix NumberOfCpuCores() on Mac OSX.
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 llvm-svn: 270172
1 parent 7ec6f56 commit e6ac1fd

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed
 

‎llvm/lib/Fuzzer/FuzzerUtil.cpp

+29-4
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,36 @@ void SetSigIntHandler() { SetSigaction(SIGINT, InterruptHandler); }
113113
void SetSigTermHandler() { SetSigaction(SIGTERM, InterruptHandler); }
114114

115115
int NumberOfCpuCores() {
116-
FILE *F = popen("nproc", "r");
117-
int N = 0;
118-
if (fscanf(F, "%d", &N) != 1)
116+
const char *CmdLine = nullptr;
117+
if (LIBFUZZER_LINUX) {
118+
CmdLine = "nproc";
119+
} else if (LIBFUZZER_APPLE) {
120+
CmdLine = "sysctl -n hw.ncpu";
121+
} else {
122+
assert(0 && "NumberOfCpuCores() is not implemented for your platform");
123+
}
124+
125+
FILE *F = popen(CmdLine, "r");
126+
int N = 1;
127+
if (!F || fscanf(F, "%d", &N) != 1) {
128+
Printf("WARNING: Failed to parse output of command \"%s\" in %s(). "
129+
"Assuming CPU count of 1.\n",
130+
CmdLine, __func__);
131+
N = 1;
132+
}
133+
134+
if (pclose(F)) {
135+
Printf("WARNING: Executing command \"%s\" failed in %s(). "
136+
"Assuming CPU count of 1.\n",
137+
CmdLine, __func__);
138+
N = 1;
139+
}
140+
if (N < 1) {
141+
Printf("WARNING: Reported CPU count (%d) from command \"%s\" was invalid "
142+
"in %s(). Assuming CPU count of 1.\n",
143+
N, CmdLine, __func__);
119144
N = 1;
120-
fclose(F);
145+
}
121146
return N;
122147
}
123148

0 commit comments

Comments
 (0)
Please sign in to comment.