Index: compiler-rt/CMakeLists.txt =================================================================== --- compiler-rt/CMakeLists.txt +++ compiler-rt/CMakeLists.txt @@ -49,7 +49,8 @@ mark_as_advanced(COMPILER_RT_BUILD_ORC) option(COMPILER_RT_BUILD_GWP_ASAN "Build GWP-ASan, and link it into SCUDO" ON) mark_as_advanced(COMPILER_RT_BUILD_GWP_ASAN) - +option(COMPILER_RT_ENABLE_CET "Build Compiler RT with CET enabled" OFF) +mark_as_advanced(COMPILER_RT_ENABLE_CET) set(COMPILER_RT_ASAN_SHADOW_SCALE "" CACHE STRING "Override the shadow scale to be used in ASan runtime") @@ -257,6 +258,13 @@ #================================ # Setup Compiler Flags #================================ +# fcf-protection is a gcc/clang option for CET support on Linux platforms. +# We need to handle MSVC CET option on Windows platforms. +if (NOT MSVC) + if (COMPILER_RT_ENABLE_CET AND NOT COMPILER_RT_HAS_FCF_PROTECTION_FLAG) + message(FATAL_ERROR "Compiler used to build compiler-rt doesn't support CET!") + endif() +endif() if(MSVC) # Override any existing /W flags with /W4. This is what LLVM does. Failing to Index: compiler-rt/cmake/config-ix.cmake =================================================================== --- compiler-rt/cmake/config-ix.cmake +++ compiler-rt/cmake/config-ix.cmake @@ -63,6 +63,7 @@ check_c_compiler_flag(-ffreestanding COMPILER_RT_HAS_FFREESTANDING_FLAG) check_c_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG) check_c_compiler_flag(-std=c11 COMPILER_RT_HAS_STD_C11_FLAG) +check_c_compiler_flag(-fcf-protection=full COMPILER_RT_HAS_FCF_PROTECTION_FLAG) check_cxx_compiler_flag(-fPIC COMPILER_RT_HAS_FPIC_FLAG) check_cxx_compiler_flag(-fPIE COMPILER_RT_HAS_FPIE_FLAG) check_cxx_compiler_flag(-fno-builtin COMPILER_RT_HAS_FNO_BUILTIN_FLAG) Index: compiler-rt/lib/builtins/CMakeLists.txt =================================================================== --- compiler-rt/lib/builtins/CMakeLists.txt +++ compiler-rt/lib/builtins/CMakeLists.txt @@ -674,6 +674,10 @@ else () set(BUILTIN_CFLAGS "") + if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG) + append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full BUILTIN_CFLAGS) + endif() + append_list_if(COMPILER_RT_HAS_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS) append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 BUILTIN_CFLAGS) Index: compiler-rt/lib/builtins/assembly.h =================================================================== --- compiler-rt/lib/builtins/assembly.h +++ compiler-rt/lib/builtins/assembly.h @@ -14,6 +14,15 @@ #ifndef COMPILERRT_ASSEMBLY_H #define COMPILERRT_ASSEMBLY_H +#if defined(__linux__) && defined(__CET__) +#if !defined(__has_include) +#define __has_include(inc) 0 +#endif +#if __has_include() +#include +#endif +#endif + #if defined(__APPLE__) && defined(__aarch64__) #define SEPARATOR %% #else Index: compiler-rt/lib/crt/CMakeLists.txt =================================================================== --- compiler-rt/lib/crt/CMakeLists.txt +++ compiler-rt/lib/crt/CMakeLists.txt @@ -97,7 +97,9 @@ append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS) append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS) append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS) - +if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG) + append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS) +endif() foreach(arch ${CRT_SUPPORTED_ARCH}) add_compiler_rt_runtime(clang_rt.crtbegin OBJECT Index: compiler-rt/test/builtins/CMakeLists.txt =================================================================== --- compiler-rt/test/builtins/CMakeLists.txt +++ compiler-rt/test/builtins/CMakeLists.txt @@ -49,6 +49,16 @@ string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}") endif() + if(COMPILER_RT_ENABLE_CET) + if(NOT arch MATCHES "i?86|x86_64|AMD64") + message(SEND_ERROR "${arch} does not support CET") + endif() + if(COMPILER_RT_HAS_FCF_PROTECTION_FLAG) + list(APPEND BUILTINS_TEST_TARGET_CFLAGS -fcf-protection=full) + string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}") + endif() + endif() + # Compute builtins available in library and add them as lit features. if(APPLE) # TODO: Support other Apple platforms. Index: compiler-rt/test/crt/CMakeLists.txt =================================================================== --- compiler-rt/test/crt/CMakeLists.txt +++ compiler-rt/test/crt/CMakeLists.txt @@ -20,7 +20,14 @@ get_test_cc_for_arch(${arch} CRT_TEST_TARGET_CC CRT_TEST_TARGET_CFLAGS) string(TOUPPER ${arch} ARCH_UPPER_CASE) set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config) - + if (COMPILER_RT_ENABLE_CET) + if (${arch} MATCHES "i386|x86_64") + list(APPEND CRT_TEST_TARGET_CFLAGS -fcf-protection=full) + string(REPLACE ";" " " CRT_TEST_TARGET_CFLAGS "${CRT_TEST_TARGET_CFLAGS}") + else() + message(FATAL_ERROR "The target arch ${arch} doesn't support CET") + endif() + endif() configure_lit_site_cfg( ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py)