Skip to content

Commit

Permalink
CMake: Improve sanitizer detection
Browse files Browse the repository at this point in the history
The change fixes the labels to show up correctly in the configure
summary.
It also allows enabling the sanitizer via the feature flags, e.g.
-DFEATURE_sanitize_address=ON.
Finally the qtbase sanitizer option is saved in QtBuildInternalsExtra
so that repos built after qtbase have the same sanitizer options
enabled.

Change-Id: Ic9d9e3ce3c7ebbc244ced2e6d163d1ac8ee06b12
Fixes: QTBUG-84721
Reviewed-by: Fabian Kosmale <[email protected]>
Reviewed-by: Joerg Bornemann <[email protected]>
  • Loading branch information
alcroito committed Jun 15, 2020
1 parent 29ad4f8 commit 7e03bc3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
32 changes: 32 additions & 0 deletions cmake/QtBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5406,6 +5406,38 @@ function(qt_apply_rpaths)
endif()
endfunction()

function(qt_internal_set_up_sanitizer_features)
set(ECM_ENABLE_SANITIZERS "" CACHE STRING "Enable sanitizers")
set_property(CACHE ECM_ENABLE_SANITIZERS PROPERTY STRINGS "address;memory;thread;undefined")

# If FEATURE_sanitize_foo is set on the command line, make sure to set the appropriate
# ECM_ENABLE_SANITIZERS value. Also the other way around. This basically allows setting either
# the feature or ECM_ENABLE_SANITIZERS directly.
#
# TODO: Decide which one of these should be the source of truth, because reconfiguration with
# different options might not work as expected when ECM_ENABLE_SANITIZERS is provided instead of
# the features.
set(enabled_sanitizer_features "")
foreach(sanitizer_type address memory thread undefined)
if(FEATURE_sanitize_${sanitizer_type})
list(APPEND enabled_sanitizer_features "${sanitizer_type}")
endif()
endforeach()
if(enabled_sanitizer_features)
set(ECM_ENABLE_SANITIZERS
"${enabled_sanitizer_features}" CACHE STRING "Enable sanitizers" FORCE)
endif()

if(ECM_ENABLE_SANITIZERS)
foreach(sanitizer_type ${ECM_ENABLE_SANITIZERS})
message(STATUS "Enabling sanitizer: ${sanitizer_type}")
set(feature_name "FEATURE_sanitize_${sanitizer_type}")
set(${feature_name} "ON" CACHE BOOL "Enable ${sanitizer_type} sanitizer" FORCE)
set(QT_${feature_name} "ON" CACHE BOOL "Enable ${sanitizer_type} sanitizer" FORCE)
endforeach()
endif()
endfunction()

# Compatibility macros that should be removed once all their usages are removed.
function(extend_target)
qt_extend_target(${ARGV})
Expand Down
5 changes: 5 additions & 0 deletions cmake/QtPostProcess.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ endif()\n")
"set(QT_BUILD_TOOLS_WHEN_CROSSCOMPILING \"TRUE\" CACHE BOOL \"\" FORCE)\n")
endif()

if(ECM_ENABLE_SANITIZERS)
string(APPEND QT_EXTRA_BUILD_INTERNALS_VARS
"set(ECM_ENABLE_SANITIZERS \"${ECM_ENABLE_SANITIZERS}\" CACHE BOOL \"\" FORCE)\n")
endif()

# Rpath related things that need to be re-used when building other repos.
string(APPEND QT_EXTRA_BUILD_INTERNALS_VARS
"set(CMAKE_INSTALL_RPATH \"${CMAKE_INSTALL_RPATH}\" CACHE STRING \"\")\n")
Expand Down
1 change: 1 addition & 0 deletions cmake/QtSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ if (CMAKE_CROSSCOMPILING AND NOT IS_DIRECTORY ${QT_HOST_PATH})
endif()

## Enable support for sanitizers:
qt_internal_set_up_sanitizer_features()
include(${CMAKE_CURRENT_LIST_DIR}/3rdparty/extra-cmake-modules/modules/ECMEnableSanitizers.cmake)

option(QT_USE_CCACHE "Enable the use of ccache")
Expand Down
25 changes: 25 additions & 0 deletions configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,37 @@ qt_feature("testcocoon"
AUTODETECT OFF
)
qt_feature_config("testcocoon" QMAKE_PUBLIC_CONFIG)
qt_feature("sanitize_address"
LABEL "Addresses"
AUTODETECT OFF
)
qt_feature_config("sanitize_address" QMAKE_PUBLIC_CONFIG)
qt_feature("sanitize_thread"
LABEL "Threads"
AUTODETECT OFF
)
qt_feature_config("sanitize_thread" QMAKE_PUBLIC_CONFIG)
qt_feature("sanitize_memory"
LABEL "Memory"
AUTODETECT OFF
)
qt_feature_config("sanitize_memory" QMAKE_PUBLIC_CONFIG)
qt_feature("sanitize_fuzzer_no_link"
LABEL "Fuzzer (instrumentation only)"
PURPOSE "Adds instrumentation for fuzzing to the binaries but links to the usual main function instead of a fuzzer's."
AUTODETECT OFF
)
qt_feature_config("sanitize_fuzzer_no_link" QMAKE_PUBLIC_CONFIG)
qt_feature("sanitize_undefined"
LABEL "Undefined"
AUTODETECT OFF
)
qt_feature_config("sanitize_undefined" QMAKE_PUBLIC_CONFIG)
qt_feature("sanitizer"
LABEL "Sanitizers"
CONDITION QT_FEATURE_sanitize_address OR QT_FEATURE_sanitize_thread OR QT_FEATURE_sanitize_memory OR QT_FEATURE_sanitize_fuzzer_no_link OR QT_FEATURE_sanitize_undefined
)
qt_feature_config("sanitizer" QMAKE_PUBLIC_CONFIG)
qt_feature("coverage_trace_pc_guard"
LABEL "trace-pc-guard"
AUTODETECT OFF
Expand Down
6 changes: 1 addition & 5 deletions util/cmake/configurejson2cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,11 +903,7 @@ def get_feature_mapping():
"autoDetect": "1",
"condition": "BUILD_SHARED_LIBS AND UNIX AND NOT WIN32 AND NOT ANDROID",
},
"sanitize_address": None, # sanitizer
"sanitize_memory": None,
"sanitizer": None,
"sanitize_thread": None,
"sanitize_undefined": None,

"shared": {
"condition": "BUILD_SHARED_LIBS",
"output": [
Expand Down

0 comments on commit 7e03bc3

Please sign in to comment.