diff options
author | Dan Liew <dan@su-root.co.uk> | 2016-05-20 01:30:36 +0000 |
---|---|---|
committer | Dan Liew <dan@su-root.co.uk> | 2016-05-20 01:30:36 +0000 |
commit | e6ac1fd0897faa87821cd5ba2daec4c5ea3ac30f (patch) | |
tree | 140d748fc4980045c466a622e317b0bc91200f3c /llvm/lib/Fuzzer | |
parent | 7ec6f560403303aa7b462bcb543df346972e4a36 (diff) | |
download | llvm-e6ac1fd0897faa87821cd5ba2daec4c5ea3ac30f.zip llvm-e6ac1fd0897faa87821cd5ba2daec4c5ea3ac30f.tar.gz llvm-e6ac1fd0897faa87821cd5ba2daec4c5ea3ac30f.tar.bz2 |
[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
Diffstat (limited to 'llvm/lib/Fuzzer')
-rw-r--r-- | llvm/lib/Fuzzer/FuzzerUtil.cpp | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/llvm/lib/Fuzzer/FuzzerUtil.cpp b/llvm/lib/Fuzzer/FuzzerUtil.cpp index 88b18d7..16d6e7b 100644 --- a/llvm/lib/Fuzzer/FuzzerUtil.cpp +++ b/llvm/lib/Fuzzer/FuzzerUtil.cpp @@ -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; } |