Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2/2] support build sanitizer lit test case with libc++ #72470

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PikachuHyA
Copy link

@PikachuHyA PikachuHyA commented Nov 16, 2023

fix #72200

this patch provide a solution for build sanitizer lit test case with libc++

we add an option SANITIZER_LIT_USE_LIBCXX to indicate whether to use libc++ to build lit test cases.

the libc++ is from the same LLVM repo, which means -DLLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi" is required.

the option SANITIZER_LIT_USE_LIBCXX works for all sanitizers (asan, hwasan, lsan, msan, ubsan) except for tsan.

the tsan has separate logic for handling libc++. It only uses the header files but does not link against the libc++ library.

https://github.com/llvm/llvm-project/blob/main/compiler-rt/test/tsan/lit.cfg.py#L62C1-L79C1

  • enable SANITIZER_LIT_USE_LIBCXX
+ ldd runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxConfig/TestCases/Output/contiguous_container.cpp.tmp
	libc++.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++.so.1 (0x00007f559edea000)
	libc++abi.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++abi.so.1 (0x00007f559eda3000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/msan/X86_64/Linux/Output/b64.cpp.tmp
	libc++.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++.so.1 (0x00007fd1929d3000)
	libc++abi.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++abi.so.1 (0x00007fd19298c000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/lsan/X86_64AsanConfig/TestCases/Linux/Output/leak_check_segv.cpp.tmp
	libc++.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++.so.1 (0x00007f1e622a7000)
	libc++abi.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++abi.so.1 (0x00007f1e62260000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/hwasan/X86_64/TestCases/Output/allocator_returns_null.cpp.tmp
	libc++.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++.so.1 (0x00007f7ad0e0b000)
	libc++abi.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++abi.so.1 (0x00007f7ad0dc4000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/tsan/X86_64Config/Linux/Output/epoll_norace.cpp.tmp
+ ldd runtimes/runtimes-bins/compiler-rt/test/ubsan/Standalone-x86_64/TestCases/Float/Output/cast-overflow.cpp.tmp
	libc++.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++.so.1 (0x00007f7ed997d000)
	libc++abi.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++abi.so.1 (0x00007f7ed9936000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/ubsan_minimal/x86_64/TestCases/Output/uadd-overflow.cpp.tmp
	libc++.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++.so.1 (0x00007fa98fd98000)
	libc++abi.so.1 => /llvm/build_compiler_rt_test_with_libcxx/lib64/libc++abi.so.1 (0x00007fa98fd51000)
  • disable SANITIZER_LIT_USE_LIBCXX
+ ldd runtimes/runtimes-bins/compiler-rt/test/asan/X86_64LinuxConfig/TestCases/Output/contiguous_container.cpp.tmp
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f7de83ae000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/msan/X86_64/Linux/Output/b64.cpp.tmp
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fd758bc1000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/lsan/X86_64AsanConfig/TestCases/Linux/Output/leak_check_segv.cpp.tmp
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f8b9e688000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/hwasan/X86_64/TestCases/Output/allocator_returns_null.cpp.tmp
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007efc12746000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/tsan/X86_64Config/Linux/Output/epoll_norace.cpp.tmp
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fc4646e8000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/ubsan/Standalone-x86_64/TestCases/Float/Output/cast-overflow.cpp.tmp
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f6907c20000)
+ ldd runtimes/runtimes-bins/compiler-rt/test/ubsan_minimal/x86_64/TestCases/Output/uadd-overflow.cpp.tmp
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fa30c740000)

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 16, 2023

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: PikachuHy (PikachuHyA)

Changes

we add an option SANITIZER_LIT_USE_LIBCXX to indicate whether to use libc++ to build lit test cases.

the libc++ is from the same LLVM repo, which means -DLLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi" is required.

the option SANITIZER_LIT_USE_LIBCXX works for all sanitizers (asan, hwasan, lsan, msan, ubsan) except for tsan.

the tsan has separate logic for handling libc++. It only uses the header files but does not link against the libc++ library.

https://github.com/llvm/llvm-project/blob/main/compiler-rt/test/tsan/lit.cfg.py#L62C1-L79C1


Full diff: https://github.com/llvm/llvm-project/pull/72470.diff

9 Files Affected:

  • (modified) compiler-rt/CMakeLists.txt (+4)
  • (modified) compiler-rt/test/CMakeLists.txt (+3-1)
  • (modified) compiler-rt/test/asan/lit.cfg.py (+8-3)
  • (modified) compiler-rt/test/hwasan/lit.cfg.py (+7-2)
  • (modified) compiler-rt/test/lit.common.configured.in (+3-1)
  • (modified) compiler-rt/test/lsan/lit.common.cfg.py (+7-1)
  • (modified) compiler-rt/test/msan/lit.cfg.py (+7-1)
  • (modified) compiler-rt/test/ubsan/lit.common.cfg.py (+6-1)
  • (modified) compiler-rt/test/ubsan_minimal/lit.common.cfg.py (+6-1)
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index 1a46f5b33480694..4d0d7fb35fd5ee9 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -182,6 +182,8 @@ else()
   set(cxxabi_supported ON)
 endif()
 
+option(SANITIZER_LIT_USE_LIBCXX "Build sanitizer lit test case with libc++" OFF)
+
 option(SANITIZER_ALLOW_CXXABI "Allow use of C++ ABI details in ubsan" ON)
 
 set(SANITIZER_CAN_USE_CXXABI OFF)
@@ -633,8 +635,10 @@ if (SANITIZER_TEST_CXX_LIBNAME STREQUAL "libc++")
     list(APPEND SANITIZER_TEST_CXX_CFLAGS "$<$<TARGET_EXISTS:cxx-headers>:$<IF:$<BOOL:${MSVC}>,/imsvc,-isystem>$<JOIN:$<TARGET_PROPERTY:cxx-headers,INTERFACE_INCLUDE_DIRECTORIES>,$<SEMICOLON>$<IF:$<BOOL:${MSVC}>,/imsvc,-isystem>>>")
     if (SANITIZER_USE_STATIC_TEST_CXX)
       list(APPEND SANITIZER_TEST_CXX_LIBRARIES "$<TARGET_LINKER_FILE:cxx_static>")
+      list(APPEND SANITIZER_TEST_CXX_LIBRARIES "$<TARGET_LINKER_FILE:cxxabi_static>")
     else()
       list(APPEND SANITIZER_TEST_CXX_LIBRARIES "$<TARGET_LINKER_FILE:$<IF:$<TARGET_EXISTS:cxx_shared>,cxx_shared,cxx_static>>")
+      list(APPEND SANITIZER_TEST_CXX_LIBRARIES "-L $<TARGET_LINKER_FILE_DIR:cxxabi_shared>")
     endif()
     # We are using the in tree libc++ so avoid including the default one.
     append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ COMPILER_RT_UNITTEST_CFLAGS)
diff --git a/compiler-rt/test/CMakeLists.txt b/compiler-rt/test/CMakeLists.txt
index f9b01b15b0e62c6..7b4c934fbc69e8d 100644
--- a/compiler-rt/test/CMakeLists.txt
+++ b/compiler-rt/test/CMakeLists.txt
@@ -17,7 +17,9 @@ pythonize_bool(ZLIB_FOUND)
 pythonize_bool(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC)
 
 pythonize_bool(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER)
-
+if (SANITIZER_LIT_USE_LIBCXX)
+  set (LLVM_LIBCXX_USED 1)
+endif ()
 configure_compiler_rt_lit_site_cfg(
   ${CMAKE_CURRENT_SOURCE_DIR}/lit.common.configured.in
   ${CMAKE_CURRENT_BINARY_DIR}/lit.common.configured)
diff --git a/compiler-rt/test/asan/lit.cfg.py b/compiler-rt/test/asan/lit.cfg.py
index d93034660212509..86b88f566a8a300 100644
--- a/compiler-rt/test/asan/lit.cfg.py
+++ b/compiler-rt/test/asan/lit.cfg.py
@@ -52,10 +52,15 @@ def get_required_attr(config, attr_name):
 else:
     extra_link_flags = []
 
+if config.libcxx_used == "1":
+    extra_libcxx_flags = config.libcxx_flags
+else:
+    extra_libcxx_flags = []
+
 # Setup default compiler flags used with -fsanitize=address option.
 # FIXME: Review the set of required flags and check if it can be reduced.
 target_cflags = [get_required_attr(config, "target_cflags")] + extra_link_flags
-target_cxxflags = config.cxx_mode_flags + target_cflags
+target_cxxflags = config.cxx_mode_flags + extra_libcxx_flags + target_cflags
 clang_asan_static_cflags = (
     [
         "-fsanitize=address",
@@ -68,7 +73,7 @@ def get_required_attr(config, attr_name):
 )
 if config.target_arch == "s390x":
     clang_asan_static_cflags.append("-mbackchain")
-clang_asan_static_cxxflags = config.cxx_mode_flags + clang_asan_static_cflags
+clang_asan_static_cxxflags = config.cxx_mode_flags + extra_libcxx_flags + clang_asan_static_cflags
 
 target_is_msvc = bool(re.match(r".*-windows-msvc$", config.target_triple))
 
@@ -82,7 +87,7 @@ def get_required_attr(config, attr_name):
             "-D_DLL",
             "-Wl,-nodefaultlib:libcmt,-defaultlib:msvcrt,-defaultlib:oldnames",
         ]
-    elif platform.system() == "FreeBSD":
+    elif platform.system() == "FreeBSD" or config.libcxx_used:
         # On FreeBSD, we need to add -pthread to ensure pthread functions are available.
         asan_dynamic_flags += ["-pthread"]
     config.available_features.add("asan-dynamic-runtime")
diff --git a/compiler-rt/test/hwasan/lit.cfg.py b/compiler-rt/test/hwasan/lit.cfg.py
index 594f3294a84ac17..eec8ab54e430abe 100644
--- a/compiler-rt/test/hwasan/lit.cfg.py
+++ b/compiler-rt/test/hwasan/lit.cfg.py
@@ -42,8 +42,13 @@
     "-hwasan-instrument-personality-functions=0",
 ]
 
-clang_hwasan_cxxflags = config.cxx_mode_flags + clang_hwasan_cflags
-clang_hwasan_oldrt_cxxflags = config.cxx_mode_flags + clang_hwasan_oldrt_cflags
+if config.libcxx_used == "1":
+    extra_libcxx_flags = config.libcxx_flags
+else:
+    extra_libcxx_flags = []
+
+clang_hwasan_cxxflags = config.cxx_mode_flags + extra_libcxx_flags + clang_hwasan_cflags
+clang_hwasan_oldrt_cxxflags = config.cxx_mode_flags + extra_libcxx_flags + clang_hwasan_oldrt_cflags
 
 
 def build_invocation(compile_flags):
diff --git a/compiler-rt/test/lit.common.configured.in b/compiler-rt/test/lit.common.configured.in
index 7c2d53520099a19..9ca52d496776d1e 100644
--- a/compiler-rt/test/lit.common.configured.in
+++ b/compiler-rt/test/lit.common.configured.in
@@ -69,7 +69,9 @@ else:
 set_default("have_internal_symbolizer", @COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER_PYBOOL@)
 set_default("have_zlib", @ZLIB_FOUND_PYBOOL@)
 set_default("libcxx_used", "@LLVM_LIBCXX_USED@")
-
+set_default("libcxx_flags", ["-nostdinc++ -nostdlib++ " + \
+                            "-isystem @LLVM_BINARY_DIR@/include/c++/v1 -L @LLVM_BINARY_DIR@/lib@LLVM_LIBDIR_SUFFIX@ " + \
+                            "-Wl,-rpath,@LLVM_BINARY_DIR@/lib@LLVM_LIBDIR_SUFFIX@ -lc++"])
 # LLVM tools dir can be passed in lit parameters, so try to
 # apply substitution.
 config.llvm_tools_dir = lit_config.substitute(config.llvm_tools_dir)
diff --git a/compiler-rt/test/lsan/lit.common.cfg.py b/compiler-rt/test/lsan/lit.common.cfg.py
index e9b974955730d32..cb6e81677bdf6ed 100644
--- a/compiler-rt/test/lsan/lit.common.cfg.py
+++ b/compiler-rt/test/lsan/lit.common.cfg.py
@@ -71,7 +71,13 @@ def get_required_attr(config, attr_name):
 clang_cflags = ["-O0", config.target_cflags] + config.debug_info_flags
 if config.android:
     clang_cflags = clang_cflags + ["-fno-emulated-tls"]
-clang_cxxflags = config.cxx_mode_flags + clang_cflags
+
+if config.libcxx_used == "1":
+    extra_libcxx_flags = config.libcxx_flags
+else:
+    extra_libcxx_flags = []
+
+clang_cxxflags = config.cxx_mode_flags + extra_libcxx_flags + clang_cflags
 lsan_incdir = config.test_source_root + "/../"
 clang_lsan_cflags = clang_cflags + lsan_cflags + ["-I%s" % lsan_incdir]
 clang_lsan_cxxflags = clang_cxxflags + lsan_cflags + ["-I%s" % lsan_incdir]
diff --git a/compiler-rt/test/msan/lit.cfg.py b/compiler-rt/test/msan/lit.cfg.py
index 361be79e2557ed4..9883d788890fa19 100644
--- a/compiler-rt/test/msan/lit.cfg.py
+++ b/compiler-rt/test/msan/lit.cfg.py
@@ -25,7 +25,13 @@
 # On SystemZ we need -mbackchain to make the fast unwinder work.
 if config.target_arch == "s390x":
     clang_msan_cflags.append("-mbackchain")
-clang_msan_cxxflags = config.cxx_mode_flags + clang_msan_cflags
+
+if config.libcxx_used == "1":
+    extra_libcxx_flags = config.libcxx_flags
+else:
+    extra_libcxx_flags = []
+
+clang_msan_cxxflags = config.cxx_mode_flags + extra_libcxx_flags + clang_msan_cflags
 
 # Flags for KMSAN invocation. This is C-only, we're not interested in C++.
 clang_kmsan_cflags = (
diff --git a/compiler-rt/test/ubsan/lit.common.cfg.py b/compiler-rt/test/ubsan/lit.common.cfg.py
index 61f6818cce064ff..ab7bd6d984d9135 100644
--- a/compiler-rt/test/ubsan/lit.common.cfg.py
+++ b/compiler-rt/test/ubsan/lit.common.cfg.py
@@ -61,9 +61,14 @@ def build_invocation(compile_flags):
     return " " + " ".join([config.clang] + compile_flags) + " "
 
 
+if config.libcxx_used == "1":
+    extra_libcxx_flags = config.libcxx_flags
+else:
+    extra_libcxx_flags = []
+
 target_cflags = [get_required_attr(config, "target_cflags")]
 clang_ubsan_cflags += target_cflags
-clang_ubsan_cxxflags = config.cxx_mode_flags + clang_ubsan_cflags
+clang_ubsan_cxxflags = config.cxx_mode_flags + extra_libcxx_flags + clang_ubsan_cflags
 
 # Define %clang and %clangxx substitutions to use in test RUN lines.
 config.substitutions.append(("%clang ", build_invocation(clang_ubsan_cflags)))
diff --git a/compiler-rt/test/ubsan_minimal/lit.common.cfg.py b/compiler-rt/test/ubsan_minimal/lit.common.cfg.py
index d398d3a0a81626e..74e901281215ecb 100644
--- a/compiler-rt/test/ubsan_minimal/lit.common.cfg.py
+++ b/compiler-rt/test/ubsan_minimal/lit.common.cfg.py
@@ -23,9 +23,14 @@ def build_invocation(compile_flags):
     return " " + " ".join([config.clang] + compile_flags) + " "
 
 
+if config.libcxx_used == "1":
+    extra_libcxx_flags = config.libcxx_flags
+else:
+    extra_libcxx_flags = []
+
 target_cflags = [get_required_attr(config, "target_cflags")]
 clang_ubsan_cflags = ["-fsanitize-minimal-runtime"] + target_cflags
-clang_ubsan_cxxflags = config.cxx_mode_flags + clang_ubsan_cflags
+clang_ubsan_cxxflags = config.cxx_mode_flags + extra_libcxx_flags + clang_ubsan_cflags
 
 # Define %clang and %clangxx substitutions to use in test RUN lines.
 config.substitutions.append(("%clang ", build_invocation(clang_ubsan_cflags)))

@PikachuHyA PikachuHyA changed the title support build sanitizer lit test case with libc++ [2/2] support build sanitizer lit test case with libc++ Nov 16, 2023
Copy link

github-actions bot commented Nov 16, 2023

✅ With the latest revision this PR passed the Python code formatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[compiler-rt] support build compiler-rt asan lit test case with libc++
2 participants