[PATCH] D20461: [LibFuzzer] Fix sending SIGALRM to main thread under Mac OSX

Dan Liew via llvm-commits llvm-commits at lists.llvm.org
Thu May 19 21:52:01 PDT 2016


delcypher created this revision.
delcypher added reviewers: kcc, kubabrecka.
delcypher added subscribers: llvm-commits, kcc.

[LibFuzzer] Fix sending SIGALRM signal to main thread on Mac OSX.

This replaces the Linux only implementation with an implementation that
relies on `pthread_kill()` which is more portable. For this to work
the Fuzzer now records the thread it was created in (presumably the main
thread).

This isn't an ideal fix because now we have a pthreads dependency
but arguably if we wanted portability we shouldn't be using signals
either.

http://reviews.llvm.org/D20461

Files:
  lib/Fuzzer/FuzzerInternal.h
  lib/Fuzzer/FuzzerLoop.cpp
  lib/Fuzzer/FuzzerUtil.cpp

Index: lib/Fuzzer/FuzzerUtil.cpp
===================================================================
--- lib/Fuzzer/FuzzerUtil.cpp
+++ lib/Fuzzer/FuzzerUtil.cpp
@@ -248,12 +248,8 @@
 }
 
 int GetPid() { return getpid(); }
-int SignalToMainThread() {
-#ifdef __linux__
-  return syscall(SYS_tgkill, GetPid(), GetPid(), SIGALRM);
-#else
-  return 0;
-#endif
+int SignalToMainThread(pthread_t MainThread) {
+  return pthread_kill(MainThread, SIGALRM);
 }
 
 std::string Base64(const Unit &U) {
Index: lib/Fuzzer/FuzzerLoop.cpp
===================================================================
--- lib/Fuzzer/FuzzerLoop.cpp
+++ lib/Fuzzer/FuzzerLoop.cpp
@@ -147,7 +147,7 @@
 };
 
 Fuzzer::Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options)
-    : CB(CB), MD(MD), Options(Options) {
+    : CB(CB), MD(MD), Options(Options), MainThread(pthread_self()) {
   SetDeathCallback();
   InitializeTraceState();
   assert(!F);
@@ -258,9 +258,11 @@
 
 void Fuzzer::RssLimitCallback() {
   InOOMState = true;
-  SignalToMainThread();
+  if (SignalToMainThread(MainThread)) {
+    Printf("WARNING: Failed to signal main thread.\n");
+  }
   SleepSeconds(5);
-  Printf("Signal to main thread failed (non-linux?). Exiting.\n");
+  Printf("ERROR: Should have exited already. Forcing exit.\n");
   _Exit(Options.ErrorExitCode);
   return;
 }
Index: lib/Fuzzer/FuzzerInternal.h
===================================================================
--- lib/Fuzzer/FuzzerInternal.h
+++ lib/Fuzzer/FuzzerInternal.h
@@ -18,6 +18,7 @@
 #include <climits>
 #include <cstddef>
 #include <cstdlib>
+#include <pthread.h>
 #include <random>
 #include <string.h>
 #include <string>
@@ -125,7 +126,7 @@
 
 int NumberOfCpuCores();
 int GetPid();
-int SignalToMainThread();
+int SignalToMainThread(pthread_t MainThread);
 void SleepSeconds(int Seconds);
 
 class Random {
@@ -458,6 +459,10 @@
 
   // Maximum recorded coverage.
   Coverage MaxCoverage;
+
+  // Used to allow other threads to communicate with
+  // the thread the fuzzer was created in.
+  pthread_t MainThread;
 };
 
 }; // namespace fuzzer


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20461.57891.patch
Type: text/x-patch
Size: 2093 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160520/eda20b50/attachment.bin>


More information about the llvm-commits mailing list