From 905f874c449cc114d74eaeb19639664779fd0b6e Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Wed, 14 Oct 2020 19:09:50 -0700 Subject: [PATCH] [cmake] Add LLVM_UBSAN_FLAGS, to allow overriding UBSan flags Allow overriding the default set of flags used to enable UBSan when building llvm. This can be used to test new checks or opt out of certain checks. Differential Revision: https://reviews.llvm.org/D89439 --- llvm/CMakeLists.txt | 4 ++++ llvm/cmake/modules/HandleLLVMOptions.cmake | 7 +++---- llvm/docs/CMake.rst | 5 +++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index ebc6733..21563e1 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -487,6 +487,10 @@ endif( LLVM_USE_PERF ) set(LLVM_USE_SANITIZER "" CACHE STRING "Define the sanitizer used to build binaries and tests.") option(LLVM_OPTIMIZE_SANITIZED_BUILDS "Pass -O1 on debug sanitizer builds" ON) +set(LLVM_UBSAN_FLAGS + "-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all" + CACHE STRING + "Compile flags set to enable UBSan. Only used if LLVM_USE_SANITIZER contains 'Undefined'.") set(LLVM_LIB_FUZZING_ENGINE "" CACHE PATH "Path to fuzzing library for linking with fuzz targets") diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake index ddf2b6e..78ed1c0 100644 --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -776,8 +776,7 @@ if(LLVM_USE_SANITIZER) endif() elseif (LLVM_USE_SANITIZER STREQUAL "Undefined") append_common_sanitizer_flags() - append("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all" - CMAKE_C_FLAGS CMAKE_CXX_FLAGS) + append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) elseif (LLVM_USE_SANITIZER STREQUAL "Thread") append_common_sanitizer_flags() append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) @@ -786,8 +785,8 @@ if(LLVM_USE_SANITIZER) elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR LLVM_USE_SANITIZER STREQUAL "Undefined;Address") append_common_sanitizer_flags() - append("-fsanitize=address,undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all" - CMAKE_C_FLAGS CMAKE_CXX_FLAGS) + append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) + append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) elseif (LLVM_USE_SANITIZER STREQUAL "Leaks") append_common_sanitizer_flags() append("-fsanitize=leak" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst index 5a73b7d..bb821b4 100644 --- a/llvm/docs/CMake.rst +++ b/llvm/docs/CMake.rst @@ -428,6 +428,11 @@ LLVM-specific variables are ``Address``, ``Memory``, ``MemoryWithOrigins``, ``Undefined``, ``Thread``, ``DataFlow``, and ``Address;Undefined``. Defaults to empty string. +**LLVM_UBSAN_FLAGS**:STRING + Defines the set of compile flags used to enable UBSan. Only used if + ``LLVM_USE_SANITIZER`` contains ``Undefined``. This can be used to override + the default set of UBSan flags. + **LLVM_ENABLE_LTO**:STRING Add ``-flto`` or ``-flto=`` flags to the compile and link command lines, enabling link-time optimization. Possible values are ``Off``, -- 2.7.4