Skip to content

Commit 002fcbd

Browse files
rorthtru
authored andcommitted
[asan][cmake][test] Fix finding dynamic asan runtime lib (#100083)
In a `runtimes` build on Solaris/amd64, there are two failues: ``` AddressSanitizer-Unit :: ./Asan-i386-calls-Dynamic-Test/failed_to_discover_tests_from_gtest AddressSanitizer-Unit :: ./Asan-i386-inline-Dynamic-Test/failed_to_discover_tests_from_gtest ``` This happens when `lit` enumerates the tests with `--gtest_list_tests --gtest_filter=-*DISABLED_*`. The error is twofold: - The `LD_LIBRARY_PATH*` variables point at the 64-bit directory (`lib/clang/19/lib/x86_64-pc-solaris2.11`) for a 32-bit test: ``` ld.so.1: Asan-i386-calls-Dynamic-Test: fatal: /var/llvm/local-amd64-release-stage2-A-flang-clang18-runtimes/tools/clang/stage2-bins/./lib/../lib/clang/19/lib/x86_64-pc-solaris2.11/libclang_rt.asan.so: wrong ELF class: ELFCLASS64 ``` - While the tests are linked with `-Wl,-rpath`, that path always is the 64-bit directory again. Accordingly, the fix consists of two parts: - The code in `compiler-rt/test/asan/Unit/lit.site.cfg.py.in` to adjust the `LD_LIBRARY_PATH*` variables is guarded by a `config.target_arch != config.host_arch` condition. This is wrong in two ways: - The adjustment is always needed independent of the host arch. This is what `compiler-rt/test/lit.common.cfg.py` already does. - Besides, `config.host_arch` is ultimately set from `CMAKE_HOST_SYSTEM_PROCESSOR`. On Linux/x86_64, this is `x86_64` (`uname -m`) while on Solaris/amd64 it's `i386` (`uname -p`), explaining why the transformation is skipped on Solaris, but not on Linux. - Besides, `RPATH` needs to be set to the correct subdirectory, so instead of using the default arch in `compiler-rt/CMakeLists.txt`, this patch moves the code to a function which takes the test's arch into account. Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`. (cherry picked from commit c34d673)
1 parent bf173ba commit 002fcbd

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

compiler-rt/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,6 @@ if (COMPILER_RT_TEST_STANDALONE_BUILD_LIBS)
604604
if ("${COMPILER_RT_TEST_COMPILER_ID}" MATCHES "Clang")
605605
list(APPEND COMPILER_RT_UNITTEST_LINK_FLAGS "-resource-dir=${COMPILER_RT_OUTPUT_DIR}")
606606
endif()
607-
get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} rtlib_dir)
608-
if (NOT WIN32)
609-
list(APPEND COMPILER_RT_UNITTEST_LINK_FLAGS "-Wl,-rpath,${rtlib_dir}")
610-
endif()
611607
endif()
612608

613609
if(COMPILER_RT_USE_LLVM_UNWINDER)

compiler-rt/cmake/config-ix.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,19 @@ function(get_target_link_flags_for_arch arch out_var)
267267
endif()
268268
endfunction()
269269

270+
# Returns a list of architecture specific dynamic ldflags in @out_var list.
271+
function(get_dynamic_link_flags_for_arch arch out_var)
272+
list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
273+
if(ARCH_INDEX EQUAL -1)
274+
message(FATAL_ERROR "Unsupported architecture: ${arch}")
275+
else()
276+
get_compiler_rt_output_dir(${arch} rtlib_dir)
277+
if (NOT WIN32)
278+
set(${out_var} "-Wl,-rpath,${rtlib_dir}" PARENT_SCOPE)
279+
endif()
280+
endif()
281+
endfunction()
282+
270283
# Returns a compiler and CFLAGS that should be used to run tests for the
271284
# specific architecture. When cross-compiling, this is controled via
272285
# COMPILER_RT_TEST_COMPILER and COMPILER_RT_TEST_COMPILER_CFLAGS.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,15 @@ function(add_asan_tests arch test_runtime)
206206
-Wl,-nodefaultlib:libcmt,-defaultlib:msvcrt,-defaultlib:oldnames
207207
)
208208
else()
209+
set(DYNAMIC_LINK_FLAGS)
210+
get_dynamic_link_flags_for_arch(${arch} DYNAMIC_LINK_FLAGS)
209211

210212
# Otherwise, reuse ASAN_INST_TEST_OBJECTS.
211213
add_compiler_rt_test(AsanDynamicUnitTests "${dynamic_test_name}" "${arch}"
212214
SUBDIR "${CONFIG_NAME_DYNAMIC}"
213215
OBJECTS ${ASAN_INST_TEST_OBJECTS}
214216
DEPS asan ${ASAN_INST_TEST_OBJECTS}
215-
LINK_FLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS} ${TARGET_LINK_FLAGS}
217+
LINK_FLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS} ${TARGET_LINK_FLAGS} ${DYNAMIC_LINK_FLAGS}
216218
)
217219
endif()
218220
endif()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ config.test_source_root = config.test_exec_root
5353
# host triple as the trailing path component. The value is incorrect for i386
5454
# tests on x86_64 hosts and vice versa. Adjust config.compiler_rt_libdir
5555
# accordingly.
56-
if config.enable_per_target_runtime_dir and config.target_arch != config.host_arch:
56+
if config.enable_per_target_runtime_dir:
5757
if config.target_arch == 'i386':
5858
config.compiler_rt_libdir = re.sub(r'/x86_64(?=-[^/]+$)', '/i386', config.compiler_rt_libdir)
5959
elif config.target_arch == 'x86_64':

0 commit comments

Comments
 (0)