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

Dan Liew via llvm-commits llvm-commits at lists.llvm.org
Thu May 19 18:36:48 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL270172: [LibFuzzer] Fix ``NumberOfCpuCores()`` on Mac OSX. (authored by delcypher).

Changed prior to commit:
  http://reviews.llvm.org/D20409?vs=57881&id=57884#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D20409

Files:
  llvm/trunk/lib/Fuzzer/FuzzerUtil.cpp

Index: llvm/trunk/lib/Fuzzer/FuzzerUtil.cpp
===================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerUtil.cpp
+++ llvm/trunk/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.57884.patch
Type: text/x-patch
Size: 1308 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160520/137113eb/attachment.bin>


More information about the llvm-commits mailing list