[PATCH] D15291: [libFuzzer] Port executing system commands to OS X

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 7 08:42:52 PST 2015


kubabrecka created this revision.
kubabrecka added reviewers: kcc, glider, samsonov.
kubabrecka added subscribers: llvm-commits, zaks.anna.

1) `nproc` doesn't exist on OS X, let's use `sysctl` instead.
2) running `system()` on OS X takes a lock, so we can't do that on multiple threads.

http://reviews.llvm.org/D15291

Files:
  lib/Fuzzer/FuzzerIO.cpp
  lib/Fuzzer/FuzzerUtil.cpp

Index: lib/Fuzzer/FuzzerUtil.cpp
===================================================================
--- lib/Fuzzer/FuzzerUtil.cpp
+++ lib/Fuzzer/FuzzerUtil.cpp
@@ -62,15 +62,35 @@
 }
 
 int NumberOfCpuCores() {
-  FILE *F = popen("nproc", "r");
+#ifdef __APPLE__
+  const char *command = "sysctl -n hw.ncpu";
+#else
+  const char *command = "nproc";
+#endif
+  FILE *F = popen(command, "r");
   int N = 0;
   fscanf(F, "%d", &N);
   fclose(F);
   return N;
 }
 
 int ExecuteCommand(const std::string &Command) {
+#ifdef __APPLE__
+  pid_t pid = fork();
+  int status = 0;
+  if (pid == -1)
+    _exit(EXIT_FAILURE);   // exec never returns
+  else if (pid > 0)
+    waitpid(pid, &status, 0);
+  else {
+    // we are the child
+    execl("/bin/bash", "/bin/bash", "-c", Command.c_str(), nullptr);
+    _exit(EXIT_FAILURE);   // exec never returns
+  }
+  return status;
+#else
   return system(Command.c_str());
+#endif
 }
 
 bool ToASCII(Unit &U) {
Index: lib/Fuzzer/FuzzerIO.cpp
===================================================================
--- lib/Fuzzer/FuzzerIO.cpp
+++ lib/Fuzzer/FuzzerIO.cpp
@@ -92,7 +92,12 @@
 }
 
 void PrintFileAsBase64(const std::string &Path) {
-  std::string Cmd = "base64 -w 0 < " + Path + "; echo";
+#ifdef __APPLE__
+  std::string Cmd = "base64";
+#else
+  std::string Cmd = "base64 -w 0";
+#endif
+  Cmd += " < " + Path + "; echo";
   ExecuteCommand(Cmd);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15291.42073.patch
Type: text/x-patch
Size: 1402 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151207/8b559cd8/attachment.bin>


More information about the llvm-commits mailing list