[compiler-rt] 8c66d78 - [test] Fix asan dynamic unit tests with per-target runtime dirs

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 14 13:48:13 PDT 2021


Author: Collin Baker
Date: 2021-10-14T16:47:25-04:00
New Revision: 8c66d781721fd8779b7b57b675913789051aa946

URL: https://github.com/llvm/llvm-project/commit/8c66d781721fd8779b7b57b675913789051aa946
DIFF: https://github.com/llvm/llvm-project/commit/8c66d781721fd8779b7b57b675913789051aa946.diff

LOG: [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

Added: 
    

Modified: 
    compiler-rt/lib/asan/tests/CMakeLists.txt
    compiler-rt/test/asan/CMakeLists.txt
    compiler-rt/test/asan/Unit/lit.site.cfg.py.in
    compiler-rt/unittests/lit.common.unit.configured.in

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/asan/tests/CMakeLists.txt b/compiler-rt/lib/asan/tests/CMakeLists.txt
index d7caf4c861bb2..1a77d89469b97 100644
--- a/compiler-rt/lib/asan/tests/CMakeLists.txt
+++ b/compiler-rt/lib/asan/tests/CMakeLists.txt
@@ -163,6 +163,13 @@ set(ASAN_BENCHMARKS_SOURCES
 function(add_asan_tests arch test_runtime)
   cmake_parse_arguments(TEST "" "KIND" "CFLAGS" ${ARGN})
 
+  # The Lit files are configured once per architecture and static/dynamic
+  # selection. Each configuration expects the test binaries in a corresponding
+  # subdirectory. Generate subdirectory names based on the architecture name.
+  string(TOUPPER ${arch} ARCH_UPPER_CASE)
+  set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
+  set(CONFIG_NAME_DYNAMIC ${ARCH_UPPER_CASE}${OS_NAME}DynamicConfig)
+
   # Closure to keep the values.
   function(generate_asan_tests test_objects test_suite testname)
     generate_compiler_rt_tests(${test_objects} ${test_suite} ${testname} ${arch}
@@ -177,7 +184,7 @@ function(add_asan_tests arch test_runtime)
   set(ASAN_INST_TEST_OBJECTS)
   generate_asan_tests(ASAN_INST_TEST_OBJECTS AsanUnitTests
     "Asan-${arch}${TEST_KIND}-Test"
-    SUBDIR "default"
+    SUBDIR "${CONFIG_NAME}"
     LINK_FLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS}
     SOURCES ${ASAN_INST_TEST_SOURCES}
     CFLAGS ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} ${TEST_CFLAGS})
@@ -191,7 +198,7 @@ function(add_asan_tests arch test_runtime)
       set(ASAN_DYNAMIC_TEST_OBJECTS)
       generate_asan_tests(ASAN_DYNAMIC_TEST_OBJECTS
         AsanDynamicUnitTests "${dynamic_test_name}"
-        SUBDIR "dynamic"
+        SUBDIR "${CONFIG_NAME_DYNAMIC}"
         CFLAGS ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS} -D_MT -D_DLL
         SOURCES ${ASAN_INST_TEST_SOURCES}
         LINK_FLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS}
@@ -201,7 +208,7 @@ function(add_asan_tests arch test_runtime)
 
       # Otherwise, reuse ASAN_INST_TEST_OBJECTS.
       add_compiler_rt_test(AsanDynamicUnitTests "${dynamic_test_name}" "${arch}"
-        SUBDIR "dynamic"
+        SUBDIR "${CONFIG_NAME_DYNAMIC}"
         OBJECTS ${ASAN_INST_TEST_OBJECTS}
         DEPS asan ${ASAN_INST_TEST_OBJECTS}
         LINK_FLAGS ${ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS}
@@ -213,7 +220,7 @@ function(add_asan_tests arch test_runtime)
   set(ASAN_NOINST_TEST_OBJECTS)
   generate_asan_tests(ASAN_NOINST_TEST_OBJECTS
     AsanUnitTests "Asan-${arch}${TEST_KIND}-Noinst-Test"
-    SUBDIR "default"
+    SUBDIR "${CONFIG_NAME}"
     CFLAGS ${ASAN_UNITTEST_COMMON_CFLAGS}
     LINK_FLAGS ${ASAN_UNITTEST_NOINST_LINK_FLAGS}
     SOURCES ${ASAN_NOINST_TEST_SOURCES}
@@ -222,7 +229,7 @@ function(add_asan_tests arch test_runtime)
   set(ASAN_BENCHMARK_OBJECTS)
   generate_asan_tests(ASAN_BENCHMARK_OBJECTS
     AsanBenchmarks "Asan-${arch}${TEST_KIND}-Benchmark"
-    SUBDIR "default"
+    SUBDIR "${CONFIG_NAME}"
     CFLAGS ${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
     SOURCES ${ASAN_BENCHMARKS_SOURCES}
     LINK_FLAGS ${ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS})

diff  --git a/compiler-rt/test/asan/CMakeLists.txt b/compiler-rt/test/asan/CMakeLists.txt
index 80d0a56003307..d02ee4b5bd680 100644
--- a/compiler-rt/test/asan/CMakeLists.txt
+++ b/compiler-rt/test/asan/CMakeLists.txt
@@ -127,25 +127,31 @@ endif()
 
 # Add unit tests.
 if(COMPILER_RT_INCLUDE_TESTS)
-  set(ASAN_TEST_DYNAMIC False)
-  configure_lit_site_cfg(
-    ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
-    ${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py)
-  if(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME)
-    set(ASAN_TEST_DYNAMIC True)
+  foreach(arch ${ASAN_TEST_ARCH})
+    string(TOUPPER ${arch} ARCH_UPPER_CASE)
+    set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
+    set(CONFIG_NAME_DYNAMIC ${ARCH_UPPER_CASE}${OS_NAME}DynamicConfig)
+
+    set(ASAN_TEST_DYNAMIC False)
     configure_lit_site_cfg(
       ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
-      ${CMAKE_CURRENT_BINARY_DIR}/Unit/dynamic/lit.site.cfg.py)
-  endif()
-  # FIXME: support unit test in the android test runner
-  if (NOT ANDROID)
-    list(APPEND ASAN_TEST_DEPS AsanUnitTests)
-    list(APPEND ASAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit)
+      ${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME}/lit.site.cfg.py)
     if(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME)
-      list(APPEND ASAN_DYNAMIC_TEST_DEPS AsanDynamicUnitTests)
-      list(APPEND ASAN_DYNAMIC_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/dynamic)
+      set(ASAN_TEST_DYNAMIC True)
+      configure_lit_site_cfg(
+        ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
+        ${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME_DYNAMIC}/lit.site.cfg.py)
     endif()
-  endif()
+    # FIXME: support unit test in the android test runner
+    if (NOT ANDROID)
+      list(APPEND ASAN_TEST_DEPS AsanUnitTests)
+      list(APPEND ASAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME})
+      if(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME)
+        list(APPEND ASAN_DYNAMIC_TEST_DEPS AsanDynamicUnitTests)
+        list(APPEND ASAN_DYNAMIC_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME_DYNAMIC})
+      endif()
+    endif()
+  endforeach()
 endif()
 
 if (SHADOW_MAPPING_UNRELIABLE)

diff  --git a/compiler-rt/test/asan/Unit/lit.site.cfg.py.in b/compiler-rt/test/asan/Unit/lit.site.cfg.py.in
index aae5078affadb..28f654a560e96 100644
--- a/compiler-rt/test/asan/Unit/lit.site.cfg.py.in
+++ b/compiler-rt/test/asan/Unit/lit.site.cfg.py.in
@@ -2,6 +2,8 @@
 
 import os
 import platform
+import re
+import shlex
 
 # Load common config for all compiler-rt unit tests.
 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):
 # Setup config name.
 config.name = 'AddressSanitizer-Unit'
 
+# Load target architecture information. Note config.target_triple can be
+# incorrect since it is populated with the default target. This unit test suite
+# may run for multiple targets. The dynamic suite needs the correct target for
+# library path selection.
+config.target_arch = "@arch@"
+
 # Setup test source and exec root. For unit tests, we define
 # it as build directory with ASan unit tests.
 # FIXME: De-hardcode this path.
 if @ASAN_TEST_DYNAMIC@:
-  test_dir = "dynamic"
+  test_dir = "@CONFIG_NAME_DYNAMIC@"
 else:
-  test_dir = "default"
+  test_dir = "@CONFIG_NAME@"
 config.test_exec_root = os.path.join("@COMPILER_RT_BINARY_DIR@",
                                      "lib", "asan", "tests", test_dir)
 
 config.test_source_root = config.test_exec_root
 
+# When LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on, the initial value of
+# config.compiler_rt_libdir (COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR) has the
+# host triple as the trailing path component. The value is incorrect for i386
+# tests on x86_64 hosts and vice versa. Adjust config.compiler_rt_libdir
+# accordingly.
+if config.enable_per_target_runtime_dir and config.target_arch != config.host_arch:
+    if config.target_arch == 'i386':
+        config.compiler_rt_libdir = re.sub(r'/x86_64(?=-[^/]+$)', '/i386', config.compiler_rt_libdir)
+    elif config.target_arch == 'x86_64':
+        config.compiler_rt_libdir = re.sub(r'/i386(?=-[^/]+$)', '/x86_64', config.compiler_rt_libdir)
+
 # Set LD_LIBRARY_PATH to pick dynamic runtime up properly.
 push_ld_library_path(config, config.compiler_rt_libdir)
 

diff  --git a/compiler-rt/unittests/lit.common.unit.configured.in b/compiler-rt/unittests/lit.common.unit.configured.in
index 29e1615ff28d2..6a3e404fa932b 100644
--- a/compiler-rt/unittests/lit.common.unit.configured.in
+++ b/compiler-rt/unittests/lit.common.unit.configured.in
@@ -7,6 +7,7 @@ config.llvm_obj_root = "@LLVM_BINARY_DIR@"
 config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
 config.compiler_rt_src_root = "@COMPILER_RT_SOURCE_DIR@"
 config.compiler_rt_libdir = "@COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR@"
+config.enable_per_target_runtime_dir = @LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_PYBOOL@
 config.llvm_build_mode = "@LLVM_BUILD_MODE@"
 config.host_arch = "@HOST_ARCH@"
 config.host_os = "@HOST_OS@"


        


More information about the llvm-commits mailing list