From 551e01c124a04f3a5f4b1f4679cdcad96dd5f321 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Tue, 23 Jul 2024 12:18:28 +0200 Subject: [PATCH] CMake: Allow opting out of Intel CET hardening Some Qt internal targets might not work correctly when built with Intel CET hardening. Add a per-target opt out to skip adding the Intel CET flags. Task-number: QTBUG-127464 Change-Id: I1a28b228a82b3505d987649eec6db08281c15482 Reviewed-by: Allan Sandfeld Jensen (cherry picked from commit c5e42a9d2faef611e3a6d124d5b093670346fb87) Reviewed-by: Qt Cherry-pick Bot --- cmake/QtFlagHandlingHelpers.cmake | 27 +++++++++++++++++++++++++++ cmake/QtInternalTargets.cmake | 9 ++------- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/cmake/QtFlagHandlingHelpers.cmake b/cmake/QtFlagHandlingHelpers.cmake index 251a356df0e..3cfab2abf61 100644 --- a/cmake/QtFlagHandlingHelpers.cmake +++ b/cmake/QtFlagHandlingHelpers.cmake @@ -235,6 +235,7 @@ function(qt_internal_apply_gc_binaries target visibility) endif() endfunction() +# Only applied to Bootstrap and BundledPCRE2. function(qt_internal_apply_intel_cet target visibility) if(NOT QT_FEATURE_intelcet) return() @@ -254,10 +255,36 @@ function(qt_internal_apply_intel_cet target visibility) ">:-mshstk>") endif() if(flags) + set(opt_out_condition "$>>") + set(flags "$<${opt_out_condition}:${flags}>") target_compile_options("${target}" ${visibility} "${flags}") endif() endfunction() +# Meant to be applied to PlatformCommonInternal. +function(qt_internal_apply_intel_cet_harderning target) + if(NOT QT_FEATURE_intelcet) + return() + endif() + + set(opt_out_condition "$>>") + + if(MSVC) + set(intel_cet_flag "-CETCOMPAT") + set(condition "$<${opt_out_condition}:${intel_cet_flag}>") + qt_internal_platform_link_options("${target}" INTERFACE "${condition}") + else() + set(intel_cet_flag "-fcf-protection=full") + set(condition "$<${opt_out_condition}:${intel_cet_flag}>") + target_compile_options("${target}" INTERFACE "${condition}") + endif() +endfunction() + +# Allow opting out of the Intel CET hardening on a per-target basis. +function(qt_internal_skip_intel_cet_hardening target) + set_target_properties("${target}" PROPERTIES _qt_no_intel_cet_harderning TRUE) +endfunction() + # Sets the exceptions flags for the given target according to exceptions_on function(qt_internal_set_exceptions_flags target exceptions_on) set(_defs "") diff --git a/cmake/QtInternalTargets.cmake b/cmake/QtInternalTargets.cmake index f7b32d18f23..79f7fd9c57f 100644 --- a/cmake/QtInternalTargets.cmake +++ b/cmake/QtInternalTargets.cmake @@ -311,13 +311,8 @@ if (GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "9.2") endif() # Hardening options -if(QT_FEATURE_intelcet) - if(MSVC) - qt_internal_platform_link_options(PlatformCommonInternal INTERFACE -CETCOMPAT) - else() - target_compile_options(PlatformCommonInternal INTERFACE -fcf-protection=full) - endif() -endif() + +qt_internal_apply_intel_cet_harderning(PlatformCommonInternal) if(QT_FEATURE_glibc_fortify_source) set(is_optimized_build "$>,$>")