Skip to content

Commit 8c66d78

Browse files
chbaker0nico
authored andcommitted
[test] Fix asan dynamic unit tests with per-target runtime dirs
When LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on Asan-i386-calls-Dynamic-Test and Asan-i386-inline-Dynamic-Test fail to run on a x86_64 host. This is because asan's unit test lit files are configured once, rather than per target arch as with the non-unit tests. LD_LIBRARY_PATH ends up incorrect, and the tests try linking against the x86_64 runtime which fails. This changes the unit test CMake machinery to configure the default and dynamic unit tests once per target arch, similar to the other asan tests. Then the fix from https://reviews.llvm.org/D108859 is adapted to the unit test Lit files with some modifications. Fixes PR52158. Differential Revision: https://reviews.llvm.org/D111756
1 parent d0a5f61 commit 8c66d78

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

compiler-rt/lib/asan/tests/CMakeLists.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ set(ASAN_BENCHMARKS_SOURCES
163163
function(add_asan_tests arch test_runtime)
164164
cmake_parse_arguments(TEST "" "KIND" "CFLAGS" ${ARGN})
165165

166+
# The Lit files are configured once per architecture and static/dynamic
167+
# selection. Each configuration expects the test binaries in a corresponding
168+
# subdirectory. Generate subdirectory names based on the architecture name.
169+
string(TOUPPER ${arch} ARCH_UPPER_CASE)
170+
set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
171+
set(CONFIG_NAME_DYNAMIC ${ARCH_UPPER_CASE}${OS_NAME}DynamicConfig)
172+
166173
# Closure to keep the values.
167174
function(generate_asan_tests test_objects test_suite testname)
168175
generate_compiler_rt_tests(${test_objects} ${test_suite} ${testname} ${arch}
@@ -177,7 +184,7 @@ function(add_asan_tests arch test_runtime)
177184
set(ASAN_INST_TEST_OBJECTS)
178185
generate_asan_tests(ASAN_INST_TEST_OBJECTS AsanUnitTests
179186
"Asan-${arch}${TEST_KIND}-Test"
180-
SUBDIR "default"
187+
SUBDIR "${CONFIG_NAME}"
181188
LINK_FLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS}
182189
SOURCES ${ASAN_INST_TEST_SOURCES}
183190
CFLAGS ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} ${TEST_CFLAGS})
@@ -191,7 +198,7 @@ function(add_asan_tests arch test_runtime)
191198
set(ASAN_DYNAMIC_TEST_OBJECTS)
192199
generate_asan_tests(ASAN_DYNAMIC_TEST_OBJECTS
193200
AsanDynamicUnitTests "${dynamic_test_name}"
194-
SUBDIR "dynamic"
201+
SUBDIR "${CONFIG_NAME_DYNAMIC}"
195202
CFLAGS ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} -D_MT -D_DLL
196203
SOURCES ${ASAN_INST_TEST_SOURCES}
197204
LINK_FLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS}
@@ -201,7 +208,7 @@ function(add_asan_tests arch test_runtime)
201208

202209
# Otherwise, reuse ASAN_INST_TEST_OBJECTS.
203210
add_compiler_rt_test(AsanDynamicUnitTests "${dynamic_test_name}" "${arch}"
204-
SUBDIR "dynamic"
211+
SUBDIR "${CONFIG_NAME_DYNAMIC}"
205212
OBJECTS ${ASAN_INST_TEST_OBJECTS}
206213
DEPS asan ${ASAN_INST_TEST_OBJECTS}
207214
LINK_FLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS}
@@ -213,7 +220,7 @@ function(add_asan_tests arch test_runtime)
213220
set(ASAN_NOINST_TEST_OBJECTS)
214221
generate_asan_tests(ASAN_NOINST_TEST_OBJECTS
215222
AsanUnitTests "Asan-${arch}${TEST_KIND}-Noinst-Test"
216-
SUBDIR "default"
223+
SUBDIR "${CONFIG_NAME}"
217224
CFLAGS ${ASAN_UNITTEST_COMMON_CFLAGS}
218225
LINK_FLAGS ${ASAN_UNITTEST_NOINST_LINK_FLAGS}
219226
SOURCES ${ASAN_NOINST_TEST_SOURCES}
@@ -222,7 +229,7 @@ function(add_asan_tests arch test_runtime)
222229
set(ASAN_BENCHMARK_OBJECTS)
223230
generate_asan_tests(ASAN_BENCHMARK_OBJECTS
224231
AsanBenchmarks "Asan-${arch}${TEST_KIND}-Benchmark"
225-
SUBDIR "default"
232+
SUBDIR "${CONFIG_NAME}"
226233
CFLAGS ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
227234
SOURCES ${ASAN_BENCHMARKS_SOURCES}
228235
LINK_FLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS})

compiler-rt/test/asan/CMakeLists.txt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,31 @@ endif()
127127

128128
# Add unit tests.
129129
if(COMPILER_RT_INCLUDE_TESTS)
130-
set(ASAN_TEST_DYNAMIC False)
131-
configure_lit_site_cfg(
132-
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
133-
${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py)
134-
if(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME)
135-
set(ASAN_TEST_DYNAMIC True)
130+
foreach(arch ${ASAN_TEST_ARCH})
131+
string(TOUPPER ${arch} ARCH_UPPER_CASE)
132+
set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
133+
set(CONFIG_NAME_DYNAMIC ${ARCH_UPPER_CASE}${OS_NAME}DynamicConfig)
134+
135+
set(ASAN_TEST_DYNAMIC False)
136136
configure_lit_site_cfg(
137137
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
138-
${CMAKE_CURRENT_BINARY_DIR}/Unit/dynamic/lit.site.cfg.py)
139-
endif()
140-
# FIXME: support unit test in the android test runner
141-
if (NOT ANDROID)
142-
list(APPEND ASAN_TEST_DEPS AsanUnitTests)
143-
list(APPEND ASAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit)
138+
${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME}/lit.site.cfg.py)
144139
if(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME)
145-
list(APPEND ASAN_DYNAMIC_TEST_DEPS AsanDynamicUnitTests)
146-
list(APPEND ASAN_DYNAMIC_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/dynamic)
140+
set(ASAN_TEST_DYNAMIC True)
141+
configure_lit_site_cfg(
142+
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
143+
${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME_DYNAMIC}/lit.site.cfg.py)
147144
endif()
148-
endif()
145+
# FIXME: support unit test in the android test runner
146+
if (NOT ANDROID)
147+
list(APPEND ASAN_TEST_DEPS AsanUnitTests)
148+
list(APPEND ASAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME})
149+
if(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME)
150+
list(APPEND ASAN_DYNAMIC_TEST_DEPS AsanDynamicUnitTests)
151+
list(APPEND ASAN_DYNAMIC_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME_DYNAMIC})
152+
endif()
153+
endif()
154+
endforeach()
149155
endif()
150156

151157
if (SHADOW_MAPPING_UNRELIABLE)

compiler-rt/test/asan/Unit/lit.site.cfg.py.in

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import os
44
import platform
5+
import re
6+
import shlex
57

68
# Load common config for all compiler-rt unit tests.
79
lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/unittests/lit.common.unit.configured")
@@ -28,18 +30,35 @@ def push_ld_library_path(config, new_path):
2830
# Setup config name.
2931
config.name = 'AddressSanitizer-Unit'
3032

33+
# Load target architecture information. Note config.target_triple can be
34+
# incorrect since it is populated with the default target. This unit test suite
35+
# may run for multiple targets. The dynamic suite needs the correct target for
36+
# library path selection.
37+
config.target_arch = "@arch@"
38+
3139
# Setup test source and exec root. For unit tests, we define
3240
# it as build directory with ASan unit tests.
3341
# FIXME: De-hardcode this path.
3442
if @ASAN_TEST_DYNAMIC@:
35-
test_dir = "dynamic"
43+
test_dir = "@CONFIG_NAME_DYNAMIC@"
3644
else:
37-
test_dir = "default"
45+
test_dir = "@CONFIG_NAME@"
3846
config.test_exec_root = os.path.join("@COMPILER_RT_BINARY_DIR@",
3947
"lib", "asan", "tests", test_dir)
4048

4149
config.test_source_root = config.test_exec_root
4250

51+
# When LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on, the initial value of
52+
# config.compiler_rt_libdir (COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR) has the
53+
# host triple as the trailing path component. The value is incorrect for i386
54+
# tests on x86_64 hosts and vice versa. Adjust config.compiler_rt_libdir
55+
# accordingly.
56+
if config.enable_per_target_runtime_dir and config.target_arch != config.host_arch:
57+
if config.target_arch == 'i386':
58+
config.compiler_rt_libdir = re.sub(r'/x86_64(?=-[^/]+$)', '/i386', config.compiler_rt_libdir)
59+
elif config.target_arch == 'x86_64':
60+
config.compiler_rt_libdir = re.sub(r'/i386(?=-[^/]+$)', '/x86_64', config.compiler_rt_libdir)
61+
4362
# Set LD_LIBRARY_PATH to pick dynamic runtime up properly.
4463
push_ld_library_path(config, config.compiler_rt_libdir)
4564

compiler-rt/unittests/lit.common.unit.configured.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ config.llvm_obj_root = "@LLVM_BINARY_DIR@"
77
config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
88
config.compiler_rt_src_root = "@COMPILER_RT_SOURCE_DIR@"
99
config.compiler_rt_libdir = "@COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR@"
10+
config.enable_per_target_runtime_dir = @LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_PYBOOL@
1011
config.llvm_build_mode = "@LLVM_BUILD_MODE@"
1112
config.host_arch = "@HOST_ARCH@"
1213
config.host_os = "@HOST_OS@"

0 commit comments

Comments
 (0)