]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
build/cmake: provide asan, tsan, ubsan builds 12615/head
authorMatt Benjamin <mbenjamin@redhat.com>
Thu, 22 Dec 2016 04:36:37 +0000 (23:36 -0500)
committerMatt Benjamin <mbenjamin@redhat.com>
Fri, 30 Dec 2016 17:39:42 +0000 (12:39 -0500)
The options are taken from rocksdb, but updated to match the
instructions here:

https://gist.github.com/kwk/4171e37f4bcdf7705329

Added option to explicitly enable leak checking via
-fsanitize-leak, after review.  The behavior I've observed from
-fsanitize-address and libasan linkage included leak checking,
but perhaps this is not general or changing behavior.

Signed-off-by: Matt Benjamin <mbenjamin@redhat.com>
CMakeLists.txt

index 565547c2c5919bc7f4b366afc682dc7209c674f2..bab04c35e7f80995d2a1941e5511293d3e768f32 100644 (file)
@@ -266,6 +266,7 @@ if(ALLOCATOR)
   elseif(${ALLOCATOR} STREQUAL "jemalloc")
     find_package(JeMalloc REQUIRED)
     set(HAVE_LIBJEMALLOC ${JEMALLOC_FOUND})
+    set(HAVE_JEMALLOC 1)
   endif()
 else(ALLOCATOR)
   find_package(gperftools)
@@ -408,6 +409,47 @@ if(LINUX)
   add_definitions(-D__linux__)
 endif(LINUX)
 
+# ASAN and friends
+option(WITH_ASAN "build with ASAN" OFF)
+option(WITH_ASAN_LEAK "explicitly enable ASAN leak detection" OFF)
+
+if(WITH_ASAN)
+  set(ASAN_CFLAGS "-fsanitize=address -fno-omit-frame-pointer")
+  if(WITH_ASAN_LEAK)
+    set(ASAN_CFLAGS "${ASAN_CFLAGS} -fsanitize=leak")
+  endif()
+  set(ASAN_LFLAGS "${CMAKE_EXE_LINKER_FLAGS} ${ASAN_CFLAGS} -lasan")
+  set(CMAKE_EXE_LINKER_FLAGS "${ASAN_LFLAGS}")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ASAN_CFLAGS}")
+  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ASAN_CFLAGS}")
+  if(HAVE_JEMALLOC)
+    message(FATAL "ASAN does not work well with JeMalloc")
+  endif()
+endif()
+
+option(WITH_TSAN "build with TSAN" OFF)
+if(WITH_TSAN)
+  if (WITH_ASAN AND WITH_ASAN_LEAK)
+    message(FATAL_ERROR "Cannot combine -fsanitize-leak w/-fsanitize-thread")
+  endif()
+  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -pie -ltsan ")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -fPIC")
+  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -fno-omit-frame-pointer -fPIC")
+  if(HAVE_JEMALLOC)
+    message(FATAL "TSAN does not work well with JeMalloc")
+  endif()
+endif()
+
+option(WITH_UBSAN "build with UBSAN" OFF)
+if(WITH_UBSAN)
+  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
+  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
+  if(HAVE_JEMALLOC)
+    message(FATAL "UBSAN does not work well with JeMalloc")
+  endif()
+endif()
+
 # Boost
 option(WITH_SYSTEM_BOOST "require and build with system Boost" OFF)