[PATCH] D20402: Try to fix libFuzzer running on Mac OSX

Dan Liew via llvm-commits llvm-commits at lists.llvm.org
Wed May 18 16:55:13 PDT 2016


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

When trying to run on OSX the call to ``__sanitizer_malloc_hook`` (and probably ``__sanitizer_free_hook`` as well but this isn't confirmed) causes a crash at application start up (somewhere in the operating system's linker) when accessing ``AllocTracer``. I believe the reason is that thread local storage has not yet been initialized.

This patch attempts to address this removing the ``threadlocal`` storage specifier on ``AllocTracer`` and making the counters in ``MallocFreeTracer`` atomic. This changes the behavior because previously (AFAICT) we would only call ``MallocFreeTracer.Stop()`` from a main thread which means we would only be counting the malloc/frees in the main thread. Now we count the malloc/frees from all threads.

I'm not sure if this is desirable because the counts will get screwed up if the library under test creates some sort of persistent background thread that mallocs memory but does not free it at the end of the user call back  (i.e. LLVMFuzzerTestOneInput) but at a later date. This is due to the counters get reset for every call to the user call back.

If this is fix is inappropriate #ifdef'ing out the ``__sanitizer_malloc_hook``  ``__sanitizer_free_hook`` functions on OSX is a possible alternative.

http://reviews.llvm.org/D20402

Files:
  lib/Fuzzer/FuzzerLoop.cpp

Index: lib/Fuzzer/FuzzerLoop.cpp
===================================================================
--- lib/Fuzzer/FuzzerLoop.cpp
+++ lib/Fuzzer/FuzzerLoop.cpp
@@ -431,11 +431,11 @@
   }
   // Returns true if there were more mallocs than frees.
   bool Stop() { return Mallocs > Frees; }
-  size_t Mallocs;
-  size_t Frees;
+  std::atomic<size_t> Mallocs;
+  std::atomic<size_t> Frees;
 };
 
-static thread_local MallocFreeTracer AllocTracer;
+static MallocFreeTracer AllocTracer;
 
 extern "C" {
 void __sanitizer_malloc_hook(void *ptr, size_t size) { AllocTracer.Mallocs++; }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20402.57709.patch
Type: text/x-patch
Size: 579 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160518/a94ce2b0/attachment.bin>


More information about the llvm-commits mailing list