[PATCH] D21049: [LibFuzzer] Fix some unit test crashes on OSX.

Dan Liew via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 6 17:47:53 PDT 2016


delcypher created this revision.
delcypher added reviewers: kcc, aizatsky.
delcypher added subscribers: kcc, aizatsky, zaks.anna, dcoughlin, kubabrecka, llvm-commits.

[LibFuzzer] Fix some unit test crashes on OSX.

This fixes the following unit tests:

* FuzzerDictionary.ParseOneDictionaryEntry
* FuzzerDictionary.ParseDictionaryFile

The issue appears to be mixing non-ASan-ified code (LibFuzzer) and
ASan-ified code (the unittest) as the tests would pass fine if
everything was built with ASan.

In particular after the call to

```
  EXPECT_TRUE(
      ParseDictionaryFile("  #zzzz\naaa=\"aa\"\n\nabc=\"abc\"", &Units));
```

the stack seemed to be clobbered (e.g. the value of the implicit `this`
pointer has changed) and the application would crash soon afterwards.

I think the issue is the mixing of different implementations of standard
library containers between ASan-ified and non-ASan-ified code, hence
the use of `_LIBCPP_HAS_NO_ASAN` macro to make sure the same
implementations are used when compiling the unit test.


http://reviews.llvm.org/D21049

Files:
  lib/Fuzzer/test/CMakeLists.txt

Index: lib/Fuzzer/test/CMakeLists.txt
===================================================================
--- lib/Fuzzer/test/CMakeLists.txt
+++ lib/Fuzzer/test/CMakeLists.txt
@@ -116,6 +116,41 @@
   FuzzerFnAdapterUnittest.cpp
   )
 
+# Detect if host compiler is using libcxx.
+# FIXME: This probably belongs in LLVM's main configure code
+# so others can use this information.
+set(libcxx_detect_src "
+	#include <iostream>
+  #if defined(_LIBCPP_VERSION)
+  #error CMAKE_HAS_LIBCXX_TRUE
+  #else
+  #error CMAKE_HAS_LIBCXX_FALSE
+  #endif
+  ")
+
+  file(WRITE "${CMAKE_BINARY_DIR}/libcxx_detect.cpp" "${libcxx_detect_src}")
+  try_compile(compile_succeeded
+    "${CMAKE_BINARY_DIR}"
+    "${CMAKE_BINARY_DIR}/libcxx_detect.cpp"
+    OUTPUT_VARIABLE compiler_output
+  )
+if(compile_succeeded)
+	message(FATAL_ERROR "Compilation should fail")
+endif()
+string(REGEX MATCH "CMAKE_HAS_LIBCXX_(TRUE|FALSE)" filtered_ouput "${compiler_output}")
+if ("${filtered_ouput}" MATCHES "CMAKE_HAS_LIBCXX_TRUE")
+	set(HAS_LIBCXX TRUE)
+else()
+	set(HAS_LIBCXX FALSE)
+endif()
+
+if(HAS_LIBCXX)
+	# Avoid mixing different libc++ implementations of standard library
+  # containers caused by mixing ASan-ified (unit test) and non-ASan-ified code
+  # (LibFuzzer).
+  target_compile_definitions(LLVMFuzzer-Unittest PRIVATE _LIBCPP_HAS_NO_ASAN)
+endif()
+
 target_link_libraries(LLVMFuzzer-Unittest
   gtest
   gtest_main


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21049.59812.patch
Type: text/x-patch
Size: 1410 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160607/78796ff6/attachment.bin>


More information about the llvm-commits mailing list