hanchenye-llvm-project/compiler-rt/test/ubsan/CMakeLists.txt

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

136 lines
5.0 KiB
CMake
Raw Normal View History

set(UBSAN_LIT_TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(UBSAN_TESTSUITES)
set(UBSAN_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS})
macro(add_ubsan_testsuite test_mode sanitizer arch lld thinlto)
set(UBSAN_LIT_TEST_MODE "${test_mode}")
set(CONFIG_NAME ${UBSAN_LIT_TEST_MODE})
if (${lld})
set(CONFIG_NAME ${CONFIG_NAME}-lld)
list(APPEND UBSAN_TEST_DEPS lld)
endif()
if (${thinlto})
set(CONFIG_NAME ${CONFIG_NAME}-thinlto)
list(APPEND UBSAN_TEST_DEPS LTO)
endif()
set(UBSAN_TEST_USE_LLD ${lld})
set(UBSAN_TEST_USE_THINLTO ${thinlto})
set(CONFIG_NAME ${CONFIG_NAME}-${arch})
configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py)
list(APPEND UBSAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME})
if(NOT COMPILER_RT_STANDALONE_BUILD)
list(APPEND UBSAN_TEST_DEPS ${sanitizer})
endif()
endmacro()
macro(add_ubsan_testsuites test_mode sanitizer arch)
add_ubsan_testsuite(${test_mode} ${sanitizer} ${arch} False False)
if(COMPILER_RT_HAS_LLD AND arch STREQUAL "x86_64" AND NOT (APPLE OR WIN32))
add_ubsan_testsuite(${test_mode} ${sanitizer} ${arch} True False)
endif()
endmacro()
set(UBSAN_TEST_ARCH ${UBSAN_SUPPORTED_ARCH})
if(APPLE)
darwin_filter_host_archs(UBSAN_SUPPORTED_ARCH UBSAN_TEST_ARCH)
endif()
foreach(arch ${UBSAN_TEST_ARCH})
set(UBSAN_TEST_TARGET_ARCH ${arch})
get_test_cc_for_arch(${arch} UBSAN_TEST_TARGET_CC UBSAN_TEST_TARGET_CFLAGS)
add_ubsan_testsuites("Standalone" ubsan ${arch})
if(COMPILER_RT_HAS_ASAN AND ";${ASAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
# TODO(wwchrome): Re-enable ubsan for asan win 64-bit when ready.
# Disable ubsan with AddressSanitizer tests for Windows 64-bit,
# 64-bit Solaris/x86, and SPARC.
if((NOT (OS_NAME MATCHES "Windows" AND CMAKE_SIZEOF_VOID_P EQUAL 8)) AND
(NOT (OS_NAME MATCHES "SunOS" AND ${arch} MATCHES x86_64)) AND
(NOT ${arch} MATCHES sparc))
add_ubsan_testsuites("AddressSanitizer" asan ${arch})
endif()
endif()
if(COMPILER_RT_HAS_MSAN AND ";${MSAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
add_ubsan_testsuites("MemorySanitizer" msan ${arch})
endif()
if(COMPILER_RT_HAS_TSAN AND ";${TSAN_SUPPORTED_ARCH};" MATCHES ";${arch};" AND NOT ANDROID)
add_ubsan_testsuites("ThreadSanitizer" tsan ${arch})
endif()
endforeach()
macro(add_ubsan_device_testsuite test_mode sanitizer platform arch)
# Note we expect the caller to have already set UBSAN_TEST_TARGET_CFLAGS
set(UBSAN_LIT_TEST_MODE "${test_mode}")
set(CONFIG_NAME ${UBSAN_LIT_TEST_MODE}-${platform}-${arch})
set(UBSAN_TEST_TARGET_ARCH ${arch})
set(UBSAN_TEST_USE_LLD "False")
set(UBSAN_TEST_USE_THINLTO "False")
if (APPLE)
set(UBSAN_TEST_APPLE_PLATFORM "${platform}")
else()
unset(UBSAN_TEST_APPLE_PLATFORM)
endif()
configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py)
#list(APPEND UBSAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME})
if(NOT COMPILER_RT_STANDALONE_BUILD)
list(APPEND UBSAN_TEST_DEPS ${sanitizer})
endif()
add_lit_testsuite(check-ubsan-${test_mode}-${platform}-${arch}
"UBSan ${CONFIG_NAME} tests"
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/
DEPENDS ${UBSAN_TEST_DEPS})
endmacro()
if(APPLE)
foreach(arch ${UBSAN_TEST_ARCH})
set(UBSAN_TEST_TARGET_ARCH ${arch})
get_test_cc_for_arch(${arch} UBSAN_TEST_TARGET_CC UBSAN_TEST_TARGET_CFLAGS)
set(UBSAN_TEST_TARGET_CFLAGS "${UBSAN_TEST_TARGET_CFLAGS} -lc++abi")
add_ubsan_testsuites("StandaloneStatic" ubsan ${arch})
endforeach()
# Device and simulator test suites.
# These are not added into "check-all", in order to run these tests, use
# "check-asan-iossim-x86_64" and similar. They also require that an extra env
# variable to select which iOS device or simulator to use, e.g.:
# SANITIZER_IOSSIM_TEST_DEVICE_IDENTIFIER="iPhone 6"
set(EXCLUDE_FROM_ALL ON)
[CMake] Fix the value of `config.target_cflags` for non-macOS Apple platforms. Attempt #3. The main problem here is that `-*-version_min=` was not being passed to the compiler when building test cases. This can cause problems when testing on devices running older OSs because Clang would previously assume the minimum deployment target is the the latest OS in the SDK which could be much newer than what the device is running. Previously the generated value looked like this: `-arch arm64 -isysroot <path_to_xcode>/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk` With this change it now looks like: `-arch arm64 -stdlib=libc++ -miphoneos-version-min=8.0 -isysroot <path_to_xcode>/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk` This mirrors the setting of config.target_cflags on macOS. This change is made for ASan, LibFuzzer, TSan, and UBSan. To implement this a new `get_test_cflags_for_apple_platform()` function has been added that when given an Apple platform name and architecture returns a string containing the C compiler flags to use when building tests. This also calls a new helper function `is_valid_apple_platform()` that validates Apple platform names. This is the third attempt at landing the patch. The first attempt (r359305) had to be reverted (r359327) due to a buildbot failure. The problem was that calling `get_test_cflags_for_apple_platform()` can trigger a CMake error if the provided architecture is not supported by the current CMake configuration. Previously, this could be triggered by passing `-DCOMPILER_RT_ENABLE_IOS=OFF` to CMake. The root cause is that we were generating test configurations for a list of architectures without checking if the relevant Sanitizer actually supported that architecture. We now intersect the list of architectures for an Apple platform with `<SANITIZER>_SUPPORTED_ARCH` (where `<SANITIZER>` is a Sanitizer name) to iterate through the correct list of architectures. The second attempt (r363633) had to be reverted (r363779) due to a build failure. The failed build was using a modified Apple toolchain where the iOS simulator SDK was missing. This exposed a bug in the existing UBSan test generation code where it was assumed that `COMPILER_RT_ENABLE_IOS` implied that the toolchain supported both iOS and the iOS simulator. This is not true. This has been fixed by using the list `SANITIZER_COMMON_SUPPORTED_OS` for the list of supported Apple platforms for UBSan. For consistency with the other Sanitizers we also now intersect the list of architectures with UBSAN_SUPPORTED_ARCH. rdar://problem/50124489 Differential Revision: https://reviews.llvm.org/D61242 llvm-svn: 373405
2019-10-02 07:08:18 +08:00
set(UBSAN_APPLE_PLATFORMS ${SANITIZER_COMMON_SUPPORTED_OS})
foreach(platform ${UBSAN_APPLE_PLATFORMS})
[CMake] Fix the value of `config.target_cflags` for non-macOS Apple platforms. Attempt #3. The main problem here is that `-*-version_min=` was not being passed to the compiler when building test cases. This can cause problems when testing on devices running older OSs because Clang would previously assume the minimum deployment target is the the latest OS in the SDK which could be much newer than what the device is running. Previously the generated value looked like this: `-arch arm64 -isysroot <path_to_xcode>/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk` With this change it now looks like: `-arch arm64 -stdlib=libc++ -miphoneos-version-min=8.0 -isysroot <path_to_xcode>/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk` This mirrors the setting of config.target_cflags on macOS. This change is made for ASan, LibFuzzer, TSan, and UBSan. To implement this a new `get_test_cflags_for_apple_platform()` function has been added that when given an Apple platform name and architecture returns a string containing the C compiler flags to use when building tests. This also calls a new helper function `is_valid_apple_platform()` that validates Apple platform names. This is the third attempt at landing the patch. The first attempt (r359305) had to be reverted (r359327) due to a buildbot failure. The problem was that calling `get_test_cflags_for_apple_platform()` can trigger a CMake error if the provided architecture is not supported by the current CMake configuration. Previously, this could be triggered by passing `-DCOMPILER_RT_ENABLE_IOS=OFF` to CMake. The root cause is that we were generating test configurations for a list of architectures without checking if the relevant Sanitizer actually supported that architecture. We now intersect the list of architectures for an Apple platform with `<SANITIZER>_SUPPORTED_ARCH` (where `<SANITIZER>` is a Sanitizer name) to iterate through the correct list of architectures. The second attempt (r363633) had to be reverted (r363779) due to a build failure. The failed build was using a modified Apple toolchain where the iOS simulator SDK was missing. This exposed a bug in the existing UBSan test generation code where it was assumed that `COMPILER_RT_ENABLE_IOS` implied that the toolchain supported both iOS and the iOS simulator. This is not true. This has been fixed by using the list `SANITIZER_COMMON_SUPPORTED_OS` for the list of supported Apple platforms for UBSan. For consistency with the other Sanitizers we also now intersect the list of architectures with UBSAN_SUPPORTED_ARCH. rdar://problem/50124489 Differential Revision: https://reviews.llvm.org/D61242 llvm-svn: 373405
2019-10-02 07:08:18 +08:00
list_intersect(
UBSAN_TEST_${platform}_ARCHS
UBSAN_SUPPORTED_ARCH
DARWIN_${platform}_ARCHS
)
foreach(arch ${UBSAN_TEST_${platform}_ARCHS})
get_test_cflags_for_apple_platform(
"${platform}"
"${arch}"
UBSAN_TEST_TARGET_CFLAGS
)
add_ubsan_device_testsuite("Standalone" ubsan ${platform} ${arch})
if(COMPILER_RT_HAS_ASAN AND ";${ASAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
add_ubsan_device_testsuite("AddressSanitizer" asan ${platform} ${arch})
endif()
if(COMPILER_RT_HAS_TSAN AND ";${TSAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
add_ubsan_device_testsuite("ThreadSanitizer" tsan ${platform} ${arch})
endif()
endforeach()
endforeach()
set(EXCLUDE_FROM_ALL OFF)
endif()
add_lit_testsuite(check-ubsan "Running UndefinedBehaviorSanitizer tests"
${UBSAN_TESTSUITES}
DEPENDS ${UBSAN_TEST_DEPS})
set_target_properties(check-ubsan PROPERTIES FOLDER "Compiler-RT Misc")