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") @@ -258,6 +259,10 @@ # Setup Compiler Flags #================================ +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() + if(MSVC) # Override any existing /W flags with /W4. This is what LLVM does. Failing to # remove other /W[0-4] flags will result in a warning about overriding a 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,8 @@ else () set(BUILTIN_CFLAGS "") + append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full BUILTIN_CFLAGS) + 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,12 @@ #ifndef COMPILERRT_ASSEMBLY_H #define COMPILERRT_ASSEMBLY_H +#if defined(__linux__) && defined(__CET__) +#include +#elif defined(__i386__) || defined(__x86_64__) +#define _CET_ENDBR +#endif + #if defined(__APPLE__) && defined(__aarch64__) #define SEPARATOR %% #else Index: compiler-rt/lib/builtins/i386/ashldi3.S =================================================================== --- compiler-rt/lib/builtins/i386/ashldi3.S +++ compiler-rt/lib/builtins/i386/ashldi3.S @@ -19,6 +19,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__ashldi3) + _CET_ENDBR movd 12(%esp), %xmm2 // Load count #ifndef TRUST_CALLERS_USE_64_BIT_STORES movd 4(%esp), %xmm0 @@ -39,6 +40,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__ashldi3) + _CET_ENDBR movl 12(%esp), %ecx // Load count movl 8(%esp), %edx // Load high movl 4(%esp), %eax // Load low Index: compiler-rt/lib/builtins/i386/ashrdi3.S =================================================================== --- compiler-rt/lib/builtins/i386/ashrdi3.S +++ compiler-rt/lib/builtins/i386/ashrdi3.S @@ -12,6 +12,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__ashrdi3) + _CET_ENDBR movd 12(%esp), %xmm2 // Load count movl 8(%esp), %eax #ifndef TRUST_CALLERS_USE_64_BIT_STORES @@ -49,6 +50,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__ashrdi3) + _CET_ENDBR movl 12(%esp), %ecx // Load count movl 8(%esp), %edx // Load high movl 4(%esp), %eax // Load low Index: compiler-rt/lib/builtins/i386/divdi3.S =================================================================== --- compiler-rt/lib/builtins/i386/divdi3.S +++ compiler-rt/lib/builtins/i386/divdi3.S @@ -27,6 +27,7 @@ // value, then restoring the correct sign at the end of the computation. This could // certainly be improved upon. + _CET_ENDBR pushl %esi movl 20(%esp), %edx // high word of b movl 16(%esp), %eax // low word of b Index: compiler-rt/lib/builtins/i386/floatdidf.S =================================================================== --- compiler-rt/lib/builtins/i386/floatdidf.S +++ compiler-rt/lib/builtins/i386/floatdidf.S @@ -23,6 +23,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__floatdidf) + _CET_ENDBR cvtsi2sd 8(%esp), %xmm1 movss 4(%esp), %xmm0 // low 32 bits of a calll 0f Index: compiler-rt/lib/builtins/i386/floatdisf.S =================================================================== --- compiler-rt/lib/builtins/i386/floatdisf.S +++ compiler-rt/lib/builtins/i386/floatdisf.S @@ -18,6 +18,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__floatdisf) + _CET_ENDBR #ifndef TRUST_CALLERS_USE_64_BIT_STORES movd 4(%esp), %xmm0 movd 8(%esp), %xmm1 Index: compiler-rt/lib/builtins/i386/floatdixf.S =================================================================== --- compiler-rt/lib/builtins/i386/floatdixf.S +++ compiler-rt/lib/builtins/i386/floatdixf.S @@ -18,6 +18,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__floatdixf) + _CET_ENDBR #ifndef TRUST_CALLERS_USE_64_BIT_STORES movd 4(%esp), %xmm0 movd 8(%esp), %xmm1 Index: compiler-rt/lib/builtins/i386/floatundidf.S =================================================================== --- compiler-rt/lib/builtins/i386/floatundidf.S +++ compiler-rt/lib/builtins/i386/floatundidf.S @@ -35,6 +35,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__floatundidf) + _CET_ENDBR movss 8(%esp), %xmm1 // high 32 bits of a movss 4(%esp), %xmm0 // low 32 bits of a calll 0f Index: compiler-rt/lib/builtins/i386/floatundisf.S =================================================================== --- compiler-rt/lib/builtins/i386/floatundisf.S +++ compiler-rt/lib/builtins/i386/floatundisf.S @@ -75,6 +75,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__floatundisf) + _CET_ENDBR movl 8(%esp), %eax movd 8(%esp), %xmm1 movd 4(%esp), %xmm0 Index: compiler-rt/lib/builtins/i386/floatundixf.S =================================================================== --- compiler-rt/lib/builtins/i386/floatundixf.S +++ compiler-rt/lib/builtins/i386/floatundixf.S @@ -27,6 +27,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__floatundixf) + _CET_ENDBR calll 0f 0: popl %eax movss 8(%esp), %xmm0 // hi 32 bits of input Index: compiler-rt/lib/builtins/i386/lshrdi3.S =================================================================== --- compiler-rt/lib/builtins/i386/lshrdi3.S +++ compiler-rt/lib/builtins/i386/lshrdi3.S @@ -19,6 +19,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__lshrdi3) + _CET_ENDBR movd 12(%esp), %xmm2 // Load count #ifndef TRUST_CALLERS_USE_64_BIT_STORES movd 4(%esp), %xmm0 @@ -39,6 +40,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__lshrdi3) + _CET_ENDBR movl 12(%esp), %ecx // Load count movl 8(%esp), %edx // Load high movl 4(%esp), %eax // Load low Index: compiler-rt/lib/builtins/i386/moddi3.S =================================================================== --- compiler-rt/lib/builtins/i386/moddi3.S +++ compiler-rt/lib/builtins/i386/moddi3.S @@ -27,6 +27,7 @@ // This is currently implemented by wrapping the unsigned modulus up in an absolute // value. This could certainly be improved upon. + _CET_ENDBR pushl %esi movl 20(%esp), %edx // high word of b movl 16(%esp), %eax // low word of b Index: compiler-rt/lib/builtins/i386/muldi3.S =================================================================== --- compiler-rt/lib/builtins/i386/muldi3.S +++ compiler-rt/lib/builtins/i386/muldi3.S @@ -11,6 +11,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__muldi3) + _CET_ENDBR pushl %ebx movl 16(%esp), %eax // b.lo movl 12(%esp), %ecx // a.hi Index: compiler-rt/lib/builtins/i386/udivdi3.S =================================================================== --- compiler-rt/lib/builtins/i386/udivdi3.S +++ compiler-rt/lib/builtins/i386/udivdi3.S @@ -23,6 +23,7 @@ .balign 4 DEFINE_COMPILERRT_FUNCTION(__udivdi3) + _CET_ENDBR pushl %ebx movl 20(%esp), %ebx // Find the index i of the leading bit in b. bsrl %ebx, %ecx // If the high word of b is zero, jump to Index: compiler-rt/lib/builtins/i386/umoddi3.S =================================================================== --- compiler-rt/lib/builtins/i386/umoddi3.S +++ compiler-rt/lib/builtins/i386/umoddi3.S @@ -24,6 +24,7 @@ .balign 4 DEFINE_COMPILERRT_FUNCTION(__umoddi3) + _CET_ENDBR pushl %ebx movl 20(%esp), %ebx // Find the index i of the leading bit in b. bsrl %ebx, %ecx // If the high word of b is zero, jump to Index: compiler-rt/lib/builtins/x86_64/floatundidf.S =================================================================== --- compiler-rt/lib/builtins/x86_64/floatundidf.S +++ compiler-rt/lib/builtins/x86_64/floatundidf.S @@ -35,6 +35,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__floatundidf) + _CET_ENDBR movd %edi, %xmm0 // low 32 bits of a shrq $32, %rdi // high 32 bits of a orq REL_ADDR(twop84), %rdi // 0x1p84 + a_hi (no rounding occurs) Index: compiler-rt/lib/builtins/x86_64/floatundisf.S =================================================================== --- compiler-rt/lib/builtins/x86_64/floatundisf.S +++ compiler-rt/lib/builtins/x86_64/floatundisf.S @@ -19,6 +19,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__floatundisf) + _CET_ENDBR movq $1, %rsi testq %rdi, %rdi js 1f Index: compiler-rt/lib/builtins/x86_64/floatundixf.S =================================================================== --- compiler-rt/lib/builtins/x86_64/floatundixf.S +++ compiler-rt/lib/builtins/x86_64/floatundixf.S @@ -20,6 +20,7 @@ .balign 4 DEFINE_COMPILERRT_FUNCTION(__floatundixf) + _CET_ENDBR movq %rdi, -8(%rsp) fildq -8(%rsp) test %rdi, %rdi Index: compiler-rt/lib/crt/CMakeLists.txt =================================================================== --- compiler-rt/lib/crt/CMakeLists.txt +++ compiler-rt/lib/crt/CMakeLists.txt @@ -97,6 +97,7 @@ 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) +append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS) foreach(arch ${CRT_SUPPORTED_ARCH}) add_compiler_rt_runtime(clang_rt.crtbegin Index: compiler-rt/test/builtins/CMakeLists.txt =================================================================== --- compiler-rt/test/builtins/CMakeLists.txt +++ compiler-rt/test/builtins/CMakeLists.txt @@ -49,6 +49,15 @@ string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}") endif() + if (COMPILER_RT_ENABLE_CET) + if (${arch} MATCHES "i386|x86_64") + list(APPEND BUILTINS_TEST_TARGET_CFLAGS -fcf-protection=full) + string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}") + else() + message(FATAL_ERROR "The target arch ${arch} doesn't support CET") + 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)