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

Dan Liew via llvm-commits llvm-commits at lists.llvm.org
Thu May 19 16:51:23 PDT 2016


delcypher updated this revision to Diff 57881.
delcypher added a comment.

- Replace assert with ``if ()``
- Remove stray comma in Printf() which lead to a crash


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");
-  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;
 }
 


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


More information about the llvm-commits mailing list