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") @@ -69,6 +70,17 @@ -D${COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION}) endif() +if (COMPILER_RT_ENABLE_CET) + if (MSVC) + message(FATAL_ERROR "compiler-rt CET support is not available for MSVC!") + endif() + + check_c_compiler_flag(-fcf-protection=full COMPILER_RT_HAS_FCF_PROTECTION_FLAG) + if(NOT COMPILER_RT_HAS_FCF_PROTECTION_FLAG) + message(FATAL_ERROR "Compiler used to build compiler-rt doesn't support CET!") + endif() +endif() + if(FUCHSIA) set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT OFF) else() 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,13 @@ #ifndef COMPILERRT_ASSEMBLY_H #define COMPILERRT_ASSEMBLY_H +#if defined(__linux__) && defined(__CET__) +#include +#define _COMPILER_RT_ENDBR _CET_ENDBR +#else +#define _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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. + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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. + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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 @@ -23,7 +23,7 @@ .text .balign 4 DEFINE_COMPILERRT_FUNCTION(__umoddi3) - + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_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) + _COMPILER_RT_ENDBR movq %rdi, -8(%rsp) fildq -8(%rsp) test %rdi, %rdi Index: compiler-rt/test/builtins/CMakeLists.txt =================================================================== --- compiler-rt/test/builtins/CMakeLists.txt +++ compiler-rt/test/builtins/CMakeLists.txt @@ -49,6 +49,11 @@ string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}") endif() + if (${arch} MATCHES "i386|x86_64" AND COMPILER_RT_ENABLE_CET) + list(APPEND BUILTINS_TEST_TARGET_CFLAGS -fcf-protection=full) + string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}") + endif() + # Compute builtins available in library and add them as lit features. if(APPLE) # TODO: Support other Apple platforms.