[CMake] Support more clang and gcc sanitizers
authorchris.reid@sony.com <chris.reid@sony.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 19 Mar 2019 23:34:03 +0000 (23:34 +0000)
committerchris.reid@sony.com <chris.reid@sony.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 19 Mar 2019 23:34:03 +0000 (23:34 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195956

Reviewed by Michael Catanzaro.

.:

Add support to compile with undefined, thread, memory, and leak sanitizers in cmake.
Also add ubsan and asan support when compiling with clang-cl on WinCairo.
This changes ENABLE_ADDRESS_SANITIZER flag to a generic ENABLE_SANITIZERS
which takes a semicolon separated list of sanitizers to enable.
e.g. `-DENABLE_SANITIZERS=address;undefined`

Building WinCairo with sanitizers enabled also needs CLANG_LIB_PATH set to the clang compiler runtime library folder.
e.g. pass `--cmakeargs="-DCLANG_LIB_PATH=\"C:\Program Files\LLVM\lib\clang\7.0.1\lib\windows\""` to build-webkit

* Source/cmake/WebKitCompilerFlags.cmake:

Tools:

* Scripts/webkitdirs.pm: Updating ENABLE_ADDRESS_SANITIZER to ENABLE_SANITIZERS=address

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@243179 268f45cc-cd09-0410-ab3c-d52691b4dbfc

ChangeLog
Source/cmake/WebKitCompilerFlags.cmake
Tools/ChangeLog
Tools/Scripts/webkitdirs.pm

index 05072f8..7365013 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2019-03-19  Christopher Reid  <chris.reid@sony.com>
+
+        [CMake] Support more clang and gcc sanitizers
+        https://bugs.webkit.org/show_bug.cgi?id=195956
+
+        Reviewed by Michael Catanzaro.
+
+        Add support to compile with undefined, thread, memory, and leak sanitizers in cmake.
+        Also add ubsan and asan support when compiling with clang-cl on WinCairo.
+        This changes ENABLE_ADDRESS_SANITIZER flag to a generic ENABLE_SANITIZERS
+        which takes a semicolon separated list of sanitizers to enable.
+        e.g. `-DENABLE_SANITIZERS=address;undefined`
+
+        Building WinCairo with sanitizers enabled also needs CLANG_LIB_PATH set to the clang compiler runtime library folder.
+        e.g. pass `--cmakeargs="-DCLANG_LIB_PATH=\"C:\Program Files\LLVM\lib\clang\7.0.1\lib\windows\""` to build-webkit
+
+        * Source/cmake/WebKitCompilerFlags.cmake:
+
 2019-03-18  Adrian Perez de Castro  <aperez@igalia.com>
 
         [WPE] Bump public API to wpe-1.0
index 1b98a6f..b4300dc 100644 (file)
@@ -169,17 +169,51 @@ endif ()
 if (COMPILER_IS_GCC_OR_CLANG)
     # Careful: this needs to be above where ENABLED_COMPILER_SANITIZERS is set.
     # Also, it's not possible to use the normal prepend/append macros for
-    # -fsanitize=address, because check_cxx_compiler_flag will report it's
+    # -fsanitize=* flags, because check_cxx_compiler_flag will report it's
     # unsupported, because it causes the build to fail if not used when linking.
-    option(ENABLE_ADDRESS_SANITIZER "Enable address sanitizer" OFF)
-    if (ENABLE_ADDRESS_SANITIZER)
-        WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS(-fno-omit-frame-pointer
-                                             -fno-optimize-sibling-calls)
-        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
-        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
-        set(CMAKE_EXE_LINKER_FLAGS "-lpthread ${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
-        set(CMAKE_SHARED_LINKER_FLAGS "-lpthread ${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
-        set(CMAKE_MODULE_LINKER_FLAGS "-lpthread ${CMAKE_MODULE_LINKER_FLAGS} -fsanitize=address")
+    if (ENABLE_SANITIZERS)
+        if (MSVC AND WTF_CPU_X86_64)
+            find_library(CLANG_ASAN_LIBRARY clang_rt.asan_dynamic_runtime_thunk-x86_64 ${CLANG_LIB_PATH})
+            find_library(CLANG_ASAN_RT_LIBRARY clang_rt.asan_dynamic-x86_64 PATHS ${CLANG_LIB_PATH})
+            set(SANITIZER_LINK_FLAGS "\"${CLANG_ASAN_LIBRARY}\" \"${CLANG_ASAN_RT_LIBRARY}\"")
+        else ()
+            set(SANITIZER_LINK_FLAGS "-lpthread")
+        endif ()
+
+        foreach (SANITIZER ${ENABLE_SANITIZERS})
+            if (${SANITIZER} MATCHES "address")
+                WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS("-fno-omit-frame-pointer -fno-optimize-sibling-calls")
+                set(SANITIZER_COMPILER_FLAGS "-fsanitize=address ${SANITIZER_COMPILER_FLAGS}")
+                set(SANITIZER_LINK_FLAGS "-fsanitize=address ${SANITIZER_LINK_FLAGS}")
+
+            elseif (${SANITIZER} MATCHES "undefined")
+                WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS("-fno-omit-frame-pointer -fno-optimize-sibling-calls")
+                # -fsanitize=vptr is incompatible with -fno-rtti
+                set(SANITIZER_COMPILER_FLAGS "-fsanitize=undefined -frtti ${SANITIZER_COMPILER_FLAGS}")
+                set(SANITIZER_LINK_FLAGS "-fsanitize=undefined ${SANITIZER_LINK_FLAGS}")
+
+            elseif (${SANITIZER} MATCHES "thread" AND NOT MSVC)
+                set(SANITIZER_COMPILER_FLAGS "-fsanitize=thread ${SANITIZER_COMPILER_FLAGS}")
+                set(SANITIZER_LINK_FLAGS "-fsanitize=thread ${SANITIZER_LINK_FLAGS}")
+
+            elseif (${SANITIZER} MATCHES "memory" AND COMPILER_IS_CLANG AND NOT MSVC)
+                set(SANITIZER_COMPILER_FLAGS "-fsanitize=memory ${SANITIZER_COMPILER_FLAGS}")
+                set(SANITIZER_LINK_FLAGS "-fsanitize=memory ${SANITIZER_LINK_FLAGS}")
+
+            elseif (${SANITIZER} MATCHES "leak" AND NOT MSVC)
+                set(SANITIZER_COMPILER_FLAGS "-fsanitize=leak ${SANITIZER_COMPILER_FLAGS}")
+                set(SANITIZER_LINK_FLAGS "-fsanitize=leak ${SANITIZER_LINK_FLAGS}")
+
+            else ()
+                message(FATAL_ERROR "Unsupported sanitizer: ${SANITIZER}")
+            endif ()
+        endforeach ()
+
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_COMPILER_FLAGS}")
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_COMPILER_FLAGS}")
+        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_LINK_FLAGS}")
+        set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${SANITIZER_LINK_FLAGS}")
+        set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${SANITIZER_LINK_FLAGS}")
     endif ()
 endif ()
 
index 7d30976..a3077e0 100644 (file)
@@ -1,3 +1,12 @@
+2019-03-19  Christopher Reid  <chris.reid@sony.com>
+
+        [CMake] Support more clang and gcc sanitizers
+        https://bugs.webkit.org/show_bug.cgi?id=195956
+
+        Reviewed by Michael Catanzaro.
+
+        * Scripts/webkitdirs.pm: Updating ENABLE_ADDRESS_SANITIZER to ENABLE_SANITIZERS=address
+
 2019-03-19  Aakash Jain  <aakash_jain@apple.com>
 
         [ews-build] Improve summary for PrintConfiguration step
index 1ce255d..d52b351 100755 (executable)
@@ -2237,7 +2237,7 @@ sub generateBuildSystemFromCMakeProject
         push @args, "-DCMAKE_BUILD_TYPE=Debug";
     }
 
-    push @args, "-DENABLE_ADDRESS_SANITIZER=ON" if asanIsEnabled();
+    push @args, "-DENABLE_SANITIZERS=address" if asanIsEnabled();
 
     push @args, '-DCMAKE_TOOLCHAIN_FILE=Platform/PlayStation' if isPlayStation();