Skip to content

Commit dec27b3

Browse files
committed
[LLVM][compiler-rt] Fix build with LLVM_USE_SANITIZER=Undefined
Fix #120003 The patch contains fixes to all the errors described in the issue attachement
1 parent 74fb992 commit dec27b3

File tree

5 files changed

+80
-63
lines changed

5 files changed

+80
-63
lines changed

compiler-rt/cmake/config-ix.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,8 @@ endif()
931931
# calling malloc on first use.
932932
# TODO(hctim): Enable this on Android again. Looks like it's causing a SIGSEGV
933933
# for Scudo and GWP-ASan, further testing needed.
934-
if (GWP_ASAN_SUPPORTED_ARCH AND
934+
if (COMPILER_RT_HAS_SANITIZER_COMMON AND
935+
GWP_ASAN_SUPPORTED_ARCH AND
935936
COMPILER_RT_BUILD_GWP_ASAN AND
936937
COMPILER_RT_BUILD_SANITIZERS AND
937938
"gwp_asan" IN_LIST COMPILER_RT_SANITIZERS_TO_BUILD AND

compiler-rt/lib/scudo/standalone/tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
include_directories(..)
22

3+
include(CompilerRTCompile)
4+
35
add_custom_target(ScudoUnitTests)
46
set_target_properties(ScudoUnitTests PROPERTIES
57
FOLDER "Compiler-RT Tests")

compiler-rt/test/CMakeLists.txt

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -46,75 +46,81 @@ if(NOT ANDROID)
4646
endif()
4747
endif()
4848

49-
umbrella_lit_testsuite_begin(check-compiler-rt)
50-
51-
function(compiler_rt_test_runtime runtime)
52-
string(TOUPPER ${runtime} runtime_uppercase)
53-
if(COMPILER_RT_HAS_${runtime_uppercase} AND COMPILER_RT_INCLUDE_TESTS)
54-
if (${runtime} STREQUAL cfi AND NOT COMPILER_RT_HAS_UBSAN)
55-
# CFI tests require diagnostic mode, which is implemented in UBSan.
56-
elseif (${runtime} STREQUAL scudo_standalone)
57-
add_subdirectory(scudo/standalone)
58-
else()
59-
add_subdirectory(${runtime})
49+
# XXX: Maybe more precise conditions to enable/disable tests
50+
# But now the below code is broken at least with LLVM_USE_SANITIZER=Undefined
51+
if(NOT LLVM_USE_SANITIZER)
52+
53+
umbrella_lit_testsuite_begin(check-compiler-rt)
54+
55+
function(compiler_rt_test_runtime runtime)
56+
string(TOUPPER ${runtime} runtime_uppercase)
57+
if(COMPILER_RT_HAS_${runtime_uppercase} AND COMPILER_RT_INCLUDE_TESTS)
58+
if (${runtime} STREQUAL cfi AND NOT COMPILER_RT_HAS_UBSAN)
59+
# CFI tests require diagnostic mode, which is implemented in UBSan.
60+
elseif (${runtime} STREQUAL scudo_standalone)
61+
add_subdirectory(scudo/standalone)
62+
else()
63+
add_subdirectory(${runtime})
64+
endif()
6065
endif()
61-
endif()
62-
endfunction()
66+
endfunction()
6367

64-
# Run sanitizer tests only if we're sure that clang would produce
65-
# working binaries.
66-
if(COMPILER_RT_CAN_EXECUTE_TESTS)
67-
if(COMPILER_RT_BUILD_BUILTINS)
68-
add_subdirectory(builtins)
69-
endif()
70-
if(COMPILER_RT_BUILD_SANITIZERS)
71-
compiler_rt_test_runtime(interception)
68+
# Run sanitizer tests only if we're sure that clang would produce
69+
# working binaries.
70+
if(COMPILER_RT_CAN_EXECUTE_TESTS)
71+
if(COMPILER_RT_BUILD_BUILTINS)
72+
add_subdirectory(builtins)
73+
endif()
74+
if(COMPILER_RT_BUILD_SANITIZERS)
75+
compiler_rt_test_runtime(interception)
7276

73-
compiler_rt_test_runtime(lsan)
74-
compiler_rt_test_runtime(ubsan)
75-
compiler_rt_test_runtime(sanitizer_common)
77+
compiler_rt_test_runtime(lsan)
78+
compiler_rt_test_runtime(ubsan)
79+
compiler_rt_test_runtime(sanitizer_common)
7680

77-
# OpenBSD not supporting asan, cannot run the tests
78-
if(COMPILER_RT_BUILD_LIBFUZZER AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD" AND NOT ANDROID)
79-
compiler_rt_test_runtime(fuzzer)
81+
# OpenBSD not supporting asan, cannot run the tests
82+
if(COMPILER_RT_BUILD_LIBFUZZER AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD" AND NOT ANDROID)
83+
compiler_rt_test_runtime(fuzzer)
8084

81-
# These tests don't need an additional runtime but use asan runtime.
82-
add_subdirectory(metadata)
83-
endif()
85+
# These tests don't need an additional runtime but use asan runtime.
86+
add_subdirectory(metadata)
87+
endif()
8488

85-
foreach(sanitizer ${COMPILER_RT_SANITIZERS_TO_BUILD})
86-
compiler_rt_test_runtime(${sanitizer})
87-
endforeach()
88-
endif()
89-
if(COMPILER_RT_BUILD_PROFILE AND COMPILER_RT_HAS_PROFILE)
90-
compiler_rt_test_runtime(profile)
91-
endif()
92-
if(COMPILER_RT_BUILD_CTX_PROFILE)
93-
compiler_rt_test_runtime(ctx_profile)
94-
endif()
95-
if(COMPILER_RT_BUILD_MEMPROF)
96-
compiler_rt_test_runtime(memprof)
97-
endif()
98-
if(COMPILER_RT_BUILD_XRAY)
99-
compiler_rt_test_runtime(xray)
89+
foreach(sanitizer ${COMPILER_RT_SANITIZERS_TO_BUILD})
90+
compiler_rt_test_runtime(${sanitizer})
91+
endforeach()
92+
endif()
93+
if(COMPILER_RT_BUILD_PROFILE AND COMPILER_RT_HAS_PROFILE)
94+
compiler_rt_test_runtime(profile)
95+
endif()
96+
if(COMPILER_RT_BUILD_CTX_PROFILE)
97+
compiler_rt_test_runtime(ctx_profile)
98+
endif()
99+
if(COMPILER_RT_BUILD_MEMPROF)
100+
compiler_rt_test_runtime(memprof)
101+
endif()
102+
if(COMPILER_RT_BUILD_XRAY)
103+
compiler_rt_test_runtime(xray)
104+
endif()
105+
if(COMPILER_RT_BUILD_ORC)
106+
compiler_rt_Test_runtime(orc)
107+
endif()
108+
# ShadowCallStack does not yet provide a runtime with compiler-rt, the tests
109+
# include their own minimal runtime
110+
add_subdirectory(shadowcallstack)
100111
endif()
101-
if(COMPILER_RT_BUILD_ORC)
102-
compiler_rt_Test_runtime(orc)
112+
113+
# Now that we've traversed all the directories and know all the lit testsuites,
114+
# introduce a rule to run to run all of them.
115+
get_property(LLVM_COMPILER_RT_LIT_DEPENDS GLOBAL PROPERTY LLVM_COMPILER_RT_LIT_DEPENDS)
116+
add_custom_target(compiler-rt-test-depends)
117+
set_target_properties(compiler-rt-test-depends PROPERTIES FOLDER "Compiler-RT/Tests")
118+
if(LLVM_COMPILER_RT_LIT_DEPENDS)
119+
add_dependencies(compiler-rt-test-depends ${LLVM_COMPILER_RT_LIT_DEPENDS})
103120
endif()
104-
# ShadowCallStack does not yet provide a runtime with compiler-rt, the tests
105-
# include their own minimal runtime
106-
add_subdirectory(shadowcallstack)
107-
endif()
121+
umbrella_lit_testsuite_end(check-compiler-rt)
108122

109-
# Now that we've traversed all the directories and know all the lit testsuites,
110-
# introduce a rule to run to run all of them.
111-
get_property(LLVM_COMPILER_RT_LIT_DEPENDS GLOBAL PROPERTY LLVM_COMPILER_RT_LIT_DEPENDS)
112-
add_custom_target(compiler-rt-test-depends)
113-
set_target_properties(compiler-rt-test-depends PROPERTIES FOLDER "Compiler-RT/Tests")
114-
if(LLVM_COMPILER_RT_LIT_DEPENDS)
115-
add_dependencies(compiler-rt-test-depends ${LLVM_COMPILER_RT_LIT_DEPENDS})
116-
endif()
117-
umbrella_lit_testsuite_end(check-compiler-rt)
123+
endif(LLVM_USE_SANITIZER)
118124

119125
if(COMPILER_RT_STANDALONE_BUILD)
120126
if(NOT TARGET check-all)

llvm/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,15 @@ endif( LLVM_USE_PERF )
707707
set(LLVM_USE_SANITIZER "" CACHE STRING
708708
"Define the sanitizer used to build binaries and tests.")
709709
option(LLVM_OPTIMIZE_SANITIZED_BUILDS "Pass -O1 on debug sanitizer builds" ON)
710+
711+
if( CMAKE_C_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
712+
set(LLVM_UBSAN_FLAGS_CLANG "-fno-sanitize=function")
713+
else()
714+
# gcc doesn't know about -fsanitize=function
715+
set(LLVM_UBSAN_FLAGS_CLANG)
716+
endif()
710717
set(LLVM_UBSAN_FLAGS
711-
"-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all"
718+
"-fsanitize=undefined -fno-sanitize=vptr ${LLVM_UBSAN_FLAGS_CLANG} -fno-sanitize-recover=all"
712719
CACHE STRING
713720
"Compile flags set to enable UBSan. Only used if LLVM_USE_SANITIZER contains 'Undefined'.")
714721
set(LLVM_LIB_FUZZING_ENGINE "" CACHE PATH

llvm/cmake/modules/HandleLLVMOptions.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,8 @@ if(LLVM_USE_SANITIZER)
10641064
if (LLVM_USE_SANITIZE_COVERAGE)
10651065
append("-fsanitize=fuzzer-no-link" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
10661066
endif()
1067-
if (LLVM_USE_SANITIZER MATCHES ".*Undefined.*")
1067+
if (LLVM_USE_SANITIZER MATCHES ".*Undefined.*" AND CMAKE_C_COMPILER_ID MATCHES "Clang" AND
1068+
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
10681069
set(IGNORELIST_FILE "${PROJECT_SOURCE_DIR}/utils/sanitizers/ubsan_ignorelist.txt")
10691070
if (EXISTS "${IGNORELIST_FILE}")
10701071
# Use this option name version since -fsanitize-ignorelist is only

0 commit comments

Comments
 (0)