[PATCH] D20409: [LibFuzzer] Fix NumberOfCpuCores() for Mac OSX

Dan Liew via llvm-commits llvm-commits at lists.llvm.org
Thu May 19 15:17:26 PDT 2016


delcypher updated the summary for this revision.
delcypher updated this revision to Diff 57865.
delcypher added a comment.

- Remove use of ifdefs
- Add some more error checking.


http://reviews.llvm.org/D20409

Files:
  lib/Fuzzer/FuzzerUtil.cpp

Index: lib/Fuzzer/FuzzerUtil.cpp
===================================================================
--- lib/Fuzzer/FuzzerUtil.cpp
+++ lib/Fuzzer/FuzzerUtil.cpp
@@ -113,11 +113,36 @@
 void SetSigTermHandler() { SetSigaction(SIGTERM, InterruptHandler); }
 
 int NumberOfCpuCores() {
-  FILE *F = popen("nproc", "r");
+  FILE *F = nullptr;
+  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");
+  }
+
+  F = popen(cmdLine, "r");
+  if (F == nullptr) {
+    Printf("libfuzzer: Failed to execute popen() in %s()\n", __func__);
+    exit(1);
+  }
   int N = 0;
-  if (fscanf(F, "%d", &N) != 1)
+  if (fscanf(F, "%d", &N) != 1) {
+    Printf("libfuzzer: Warning failed to parse output of command in %s(). "
+           "Assuming CPU count of 1.\n",
+           __func__);
     N = 1;
-  fclose(F);
+  }
+
+  int returnCode = pclose(F);
+  if (returnCode != 0) {
+    Printf("libfuzzer: Executing command \"%s\" failed in %s()\n", cmdLine,
+           __func__);
+    exit(1);
+  }
+  assert(N > 0 && "Number of cpu cores must be > 0");
   return N;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20409.57865.patch
Type: text/x-patch
Size: 1236 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160519/12874b93/attachment.bin>


More information about the llvm-commits mailing list