Skip to content

Commit bbde056

Browse files
committed
[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
1 parent e4ee28d commit bbde056

File tree

5 files changed

+105
-50
lines changed

5 files changed

+105
-50
lines changed

Diff for: compiler-rt/cmake/config-ix.cmake

+26
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,32 @@ macro(get_test_cc_for_arch arch cc_out cflags_out)
208208
endif()
209209
endmacro()
210210

211+
# Returns CFLAGS that should be used to run tests for the
212+
# specific apple platform and architecture.
213+
function(get_test_cflags_for_apple_platform platform arch cflags_out)
214+
is_valid_apple_platform("${platform}" is_valid_platform)
215+
if (NOT is_valid_platform)
216+
message(FATAL_ERROR "\"${platform}\" is not a valid apple platform")
217+
endif()
218+
set(test_cflags "")
219+
get_target_flags_for_arch(${arch} test_cflags)
220+
list(APPEND test_cflags ${DARWIN_${platform}_CFLAGS})
221+
string(REPLACE ";" " " test_cflags_str "${test_cflags}")
222+
string(APPEND test_cflags_str "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
223+
set(${cflags_out} "${test_cflags_str}" PARENT_SCOPE)
224+
endfunction()
225+
226+
function(is_valid_apple_platform platform is_valid_out)
227+
set(is_valid FALSE)
228+
if ("${platform}" STREQUAL "")
229+
message(FATAL_ERROR "platform cannot be empty")
230+
endif()
231+
if ("${platform}" MATCHES "^(osx|((ios|watchos|tvos)(sim)?))$")
232+
set(is_valid TRUE)
233+
endif()
234+
set(${is_valid_out} ${is_valid} PARENT_SCOPE)
235+
endfunction()
236+
211237
set(ARM64 aarch64)
212238
set(ARM32 arm armhf)
213239
set(HEXAGON hexagon)

Diff for: compiler-rt/test/asan/CMakeLists.txt

+15-4
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,22 @@ endforeach()
8282
# variable to select which iOS device or simulator to use, e.g.:
8383
# SANITIZER_IOSSIM_TEST_DEVICE_IDENTIFIER="iPhone 6"
8484
if(APPLE)
85+
# FIXME(dliew): This logic should be refactored to the way UBSan Darwin
86+
# testing is done.
8587
set(EXCLUDE_FROM_ALL ON)
8688

8789
set(ASAN_TEST_TARGET_CC ${COMPILER_RT_TEST_COMPILER})
8890
set(ASAN_TEST_DYNAMIC True)
8991

90-
foreach(arch ${DARWIN_iossim_ARCHS})
92+
list_intersect(ASAN_TEST_IOSSIM_ARCHS ASAN_SUPPORTED_ARCH DARWIN_iossim_ARCHS)
93+
foreach(arch ${ASAN_TEST_IOSSIM_ARCHS})
9194
set(ASAN_TEST_APPLE_PLATFORM "iossim")
9295
set(ASAN_TEST_TARGET_ARCH ${arch})
93-
set(ASAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_iossim_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
96+
get_test_cflags_for_apple_platform(
97+
"${ASAN_TEST_APPLE_PLATFORM}"
98+
"${ASAN_TEST_TARGET_ARCH}"
99+
ASAN_TEST_TARGET_CFLAGS
100+
)
94101
set(ASAN_TEST_CONFIG_SUFFIX "-${arch}-${ASAN_TEST_APPLE_PLATFORM}")
95102
get_bits_for_arch(${arch} ASAN_TEST_BITS)
96103
string(TOUPPER ${arch} ARCH_UPPER_CASE)
@@ -104,10 +111,14 @@ if(APPLE)
104111
DEPENDS ${ASAN_TEST_DEPS})
105112
endforeach()
106113

107-
foreach (arch ${DARWIN_ios_ARCHS})
114+
list_intersect(ASAN_TEST_IOS_ARCHS ASAN_SUPPORTED_ARCH DARWIN_ios_ARCHS)
115+
foreach (arch ${ASAN_TEST_IOS_ARCHS})
108116
set(ASAN_TEST_APPLE_PLATFORM "ios")
109117
set(ASAN_TEST_TARGET_ARCH ${arch})
110-
set(ASAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_ios_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
118+
get_test_cflags_for_apple_platform(
119+
"${ASAN_TEST_APPLE_PLATFORM}"
120+
"${arch}"
121+
ASAN_TEST_TARGET_CFLAGS)
111122
set(ASAN_TEST_CONFIG_SUFFIX "-${arch}-${ASAN_TEST_APPLE_PLATFORM}")
112123
get_bits_for_arch(${arch} ASAN_TEST_BITS)
113124
string(TOUPPER ${arch} ARCH_UPPER_CASE)

Diff for: compiler-rt/test/fuzzer/CMakeLists.txt

+9-2
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,19 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
8989
endif()
9090

9191
if (APPLE)
92+
# FIXME(dliew): This logic should be refactored to the way UBSan Darwin
93+
# testing is done.
9294
set(EXCLUDE_FROM_ALL ON)
9395

94-
foreach(arch ${DARWIN_ios_ARCHS})
96+
list_intersect(FUZZER_TEST_IOS_ARCHS FUZZER_SUPPORTED_ARCH DARWIN_ios_ARCHS)
97+
foreach(arch ${FUZZER_TEST_IOS_ARCHS})
9598
set(LIBFUZZER_TEST_APPLE_PLATFORM "ios")
9699
set(LIBFUZZER_TEST_TARGET_ARCH ${arch})
97-
set(LIBFUZZER_TEST_FLAGS "-arch ${arch} -isysroot ${DARWIN_ios_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
100+
get_test_cflags_for_apple_platform(
101+
"${LIBFUZZER_TEST_APPLE_PLATFORM}"
102+
"${LIBFUZZER_TEST_TARGET_ARCH}"
103+
LIBFUZZER_TEST_FLAGS
104+
)
98105
set(LIBFUZZER_TEST_CONFIG_SUFFIX "-${arch}-${LIBFUZZER_TEST_APPLE_PLATFORM}")
99106
string(TOUPPER ${arch} ARCH_UPPER_CASE)
100107
set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config")

Diff for: compiler-rt/test/tsan/CMakeLists.txt

+42-29
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,52 @@ endforeach()
4949
# variable to select which iOS device or simulator to use, e.g.:
5050
# SANITIZER_IOSSIM_TEST_DEVICE_IDENTIFIER="iPhone 6"
5151
if(APPLE)
52+
# FIXME(dliew): This logic should be refactored to the way UBSan Darwin
53+
# testing is done.
5254
set(EXCLUDE_FROM_ALL ON)
53-
5455
set(TSAN_TEST_TARGET_CC ${COMPILER_RT_TEST_COMPILER})
5556

56-
set(TSAN_TEST_APPLE_PLATFORM "iossim")
57-
set(arch "x86_64")
58-
set(TSAN_TEST_TARGET_ARCH ${arch})
59-
set(TSAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_iossim_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
60-
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
61-
string(TOUPPER ${arch} ARCH_UPPER_CASE)
62-
set(CONFIG_NAME "IOSSim${ARCH_UPPER_CASE}Config")
63-
configure_lit_site_cfg(
64-
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
65-
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py
66-
)
67-
add_lit_testsuite(check-tsan-iossim-${arch} "ThreadSanitizer iOS Simulator ${arch} tests"
68-
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/
69-
DEPENDS ${TSAN_TEST_DEPS})
57+
list_intersect(TSAN_TEST_IOSSIM_ARCHS TSAN_SUPPORTED_ARCH DARWIN_iossim_ARCHS)
58+
foreach(arch ${TSAN_TEST_IOSSIM_ARCHS})
59+
set(TSAN_TEST_APPLE_PLATFORM "iossim")
60+
set(TSAN_TEST_TARGET_ARCH ${arch})
61+
get_test_cflags_for_apple_platform(
62+
"${TSAN_TEST_APPLE_PLATFORM}"
63+
"${TSAN_TEST_TARGET_ARCH}"
64+
TSAN_TEST_TARGET_CFLAGS
65+
)
66+
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
67+
string(TOUPPER ${arch} ARCH_UPPER_CASE)
68+
set(CONFIG_NAME "IOSSim${ARCH_UPPER_CASE}Config")
69+
configure_lit_site_cfg(
70+
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
71+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py
72+
)
73+
add_lit_testsuite(check-tsan-iossim-${arch} "ThreadSanitizer iOS Simulator ${arch} tests"
74+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/
75+
DEPENDS ${TSAN_TEST_DEPS})
76+
endforeach()
7077

71-
set(TSAN_TEST_APPLE_PLATFORM "ios")
72-
set(arch "arm64")
73-
set(TSAN_TEST_TARGET_ARCH ${arch})
74-
set(TSAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_ios_SYSROOT} ${COMPILER_RT_TEST_COMPILER_CFLAGS}")
75-
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
76-
string(TOUPPER ${arch} ARCH_UPPER_CASE)
77-
set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config")
78-
configure_lit_site_cfg(
79-
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
80-
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py
81-
)
82-
add_lit_testsuite(check-tsan-ios-${arch} "ThreadSanitizer iOS Simulator ${arch} tests"
83-
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/
84-
DEPENDS ${TSAN_TEST_DEPS})
78+
list_intersect(TSAN_TEST_IOS_ARCHS TSAN_SUPPORTED_ARCH DARWIN_ios_ARCHS)
79+
foreach(arch ${TSAN_TEST_IOS_ARCHS})
80+
set(TSAN_TEST_APPLE_PLATFORM "ios")
81+
set(TSAN_TEST_TARGET_ARCH ${arch})
82+
get_test_cflags_for_apple_platform(
83+
"${TSAN_TEST_APPLE_PLATFORM}"
84+
"${TSAN_TEST_TARGET_ARCH}"
85+
TSAN_TEST_TARGET_CFLAGS
86+
)
87+
set(TSAN_TEST_CONFIG_SUFFIX "-${arch}-${TSAN_TEST_APPLE_PLATFORM}")
88+
string(TOUPPER ${arch} ARCH_UPPER_CASE)
89+
set(CONFIG_NAME "IOS${ARCH_UPPER_CASE}Config")
90+
configure_lit_site_cfg(
91+
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
92+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py
93+
)
94+
add_lit_testsuite(check-tsan-ios-${arch} "ThreadSanitizer iOS ${arch} tests"
95+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/
96+
DEPENDS ${TSAN_TEST_DEPS})
97+
endforeach()
8598

8699
set(EXCLUDE_FROM_ALL OFF)
87100
endif()

Diff for: compiler-rt/test/ubsan/CMakeLists.txt

+13-15
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,20 @@ if(APPLE)
101101
# variable to select which iOS device or simulator to use, e.g.:
102102
# SANITIZER_IOSSIM_TEST_DEVICE_IDENTIFIER="iPhone 6"
103103
set(EXCLUDE_FROM_ALL ON)
104-
set(UBSAN_APPLE_PLATFORMS "")
105-
if (COMPILER_RT_ENABLE_IOS)
106-
list(APPEND UBSAN_APPLE_PLATFORMS ios iossim)
107-
endif()
108-
if (COMPILER_RT_ENABLE_WATCHOS)
109-
list(APPEND UBSAN_APPLE_PLATFORMS watchos watchossim)
110-
endif()
111-
if (COMPILER_RT_ENABLE_TVOS)
112-
list(APPEND UBSAN_APPLE_PLATFORMS tvos tvossim)
113-
endif()
104+
set(UBSAN_APPLE_PLATFORMS ${SANITIZER_COMMON_SUPPORTED_OS})
114105
foreach(platform ${UBSAN_APPLE_PLATFORMS})
115-
foreach(arch ${DARWIN_${platform}_ARCHS})
116-
set(UBSAN_TEST_TARGET_CFLAGS "-arch ${arch} -isysroot ${DARWIN_${platform}_SYSROOT}")
117-
if (";${UBSAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
118-
add_ubsan_device_testsuite("Standalone" ubsan ${platform} ${arch})
119-
endif()
106+
list_intersect(
107+
UBSAN_TEST_${platform}_ARCHS
108+
UBSAN_SUPPORTED_ARCH
109+
DARWIN_${platform}_ARCHS
110+
)
111+
foreach(arch ${UBSAN_TEST_${platform}_ARCHS})
112+
get_test_cflags_for_apple_platform(
113+
"${platform}"
114+
"${arch}"
115+
UBSAN_TEST_TARGET_CFLAGS
116+
)
117+
add_ubsan_device_testsuite("Standalone" ubsan ${platform} ${arch})
120118

121119
if(COMPILER_RT_HAS_ASAN AND ";${ASAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
122120
add_ubsan_device_testsuite("AddressSanitizer" asan ${platform} ${arch})

0 commit comments

Comments
 (0)