From d75a40a9c1817ca047dec9cf0e1c0f1fec948e06 Mon Sep 17 00:00:00 2001 From: Midhunesh Date: Wed, 26 Mar 2025 04:59:35 +0530 Subject: [PATCH] Add cmake option to enable/disable searching PATH for symbolizer (#129012) Introduced a cmake option that is disabled by default that suppresses searching via the PATH variable for a symbolizer. The option will be enabled for downstream builds where the user will need to specify the symbolizer path more explicitly, e.g., by using ASAN_SYMBOLIZER_PATH. --- compiler-rt/CMakeLists.txt | 7 +++++ .../sanitizer_symbolizer_posix_libcdep.cpp | 11 ++++++-- compiler-rt/test/CMakeLists.txt | 2 ++ compiler-rt/test/lit.common.cfg.py | 28 +++++++++++++++++++ compiler-rt/test/lit.common.configured.in | 1 + .../disable_symbolizer_path_search.cpp | 19 +++++++++++++ .../gn/secondary/compiler-rt/test/BUILD.gn | 1 + 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 compiler-rt/test/sanitizer_common/TestCases/disable_symbolizer_path_search.cpp diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt index 2c52788de56a..f319113e5c16 100644 --- a/compiler-rt/CMakeLists.txt +++ b/compiler-rt/CMakeLists.txt @@ -830,6 +830,13 @@ pythonize_bool(COMPILER_RT_TEST_USE_LLD) option(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER "Build Compiler RT linked with in LLVM symbolizer" OFF) mark_as_advanced(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER) +option(SANITIZER_DISABLE_SYMBOLIZER_PATH_SEARCH "Disable searching for external symbolizer in $PATH" OFF) +mark_as_advanced(SANITIZER_DISABLE_SYMBOLIZER_PATH_SEARCH) + +if (SANITIZER_DISABLE_SYMBOLIZER_PATH_SEARCH) + add_compile_definitions(SANITIZER_DISABLE_SYMBOLIZER_PATH_SEARCH) +endif() + add_subdirectory(lib) if(COMPILER_RT_INCLUDE_TESTS) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cpp index 67af07d8bdd7..f8d821e125b7 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cpp @@ -448,13 +448,19 @@ static SymbolizerTool *ChooseExternalSymbolizer(LowLevelAllocator *allocator) { } // Otherwise symbolizer program is unknown, let's search $PATH +# ifdef SANITIZER_DISABLE_SYMBOLIZER_PATH_SEARCH + VReport(2, + "Symbolizer path search is disabled in the runtime " + "build configuration.\n"); + return nullptr; +# else CHECK(path == nullptr); -# if SANITIZER_APPLE +# if SANITIZER_APPLE if (const char *found_path = FindPathToBinary("atos")) { VReport(2, "Using atos found at: %s\n", found_path); return new (*allocator) AtosSymbolizer(found_path, allocator); } -# endif // SANITIZER_APPLE +# endif // SANITIZER_APPLE if (const char *found_path = FindPathToBinary("llvm-symbolizer")) { VReport(2, "Using llvm-symbolizer found at: %s\n", found_path); return new (*allocator) LLVMSymbolizer(found_path, allocator); @@ -466,6 +472,7 @@ static SymbolizerTool *ChooseExternalSymbolizer(LowLevelAllocator *allocator) { } } return nullptr; +# endif // SANITIZER_DISABLE_SYMBOLIZER_PATH_SEARCH } static void ChooseSymbolizerTools(IntrusiveList *list, diff --git a/compiler-rt/test/CMakeLists.txt b/compiler-rt/test/CMakeLists.txt index f9e23710d3e4..fad5b7e03925 100644 --- a/compiler-rt/test/CMakeLists.txt +++ b/compiler-rt/test/CMakeLists.txt @@ -10,6 +10,8 @@ pythonize_bool(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC) pythonize_bool(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER) +pythonize_bool(SANITIZER_DISABLE_SYMBOLIZER_PATH_SEARCH) + pythonize_bool(COMPILER_RT_HAS_AARCH64_SME) pythonize_bool(COMPILER_RT_HAS_NO_DEFAULT_CONFIG_FLAG) diff --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py index c6f27748ccb7..e15b8aefdd09 100644 --- a/compiler-rt/test/lit.common.cfg.py +++ b/compiler-rt/test/lit.common.cfg.py @@ -330,6 +330,10 @@ if config.have_zlib: if config.have_internal_symbolizer: config.available_features.add("internal_symbolizer") +if config.have_disable_symbolizer_path_search: + config.available_features.add("disable_symbolizer_path_search") + + # Use ugly construction to explicitly prohibit "clang", "clang++" etc. # in RUN lines. config.substitutions.append( @@ -372,6 +376,30 @@ def get_ios_commands_dir(): ) +# When cmake flag to disable path search is set, symbolizer is not allowed to search in $PATH, +# need to specify it via XXX_SYMBOLIZER_PATH +tool_symbolizer_path_list = [ + "ASAN_SYMBOLIZER_PATH", + "HWASAN_SYMBOLIZER_PATH", + "RTSAN_SYMBOLIZER_PATH", + "TSAN_SYMBOLIZER_PATH", + "MSAN_SYMBOLIZER_PATH", + "LSAN_SYMBOLIZER_PATH", + "UBSAN_SYMBOLIZER_PATH", +] + +if config.have_disable_symbolizer_path_search: + symbolizer_path = os.path.join(config.llvm_tools_dir, "llvm-symbolizer") + + for sanitizer in tool_symbolizer_path_list: + if sanitizer not in config.environment: + config.environment[sanitizer] = symbolizer_path + +env_unset_command = " ".join(f"-u {var}" for var in tool_symbolizer_path_list) +config.substitutions.append( + ("%env_unset_tool_symbolizer_path", f"env {env_unset_command}") +) + # Allow tests to be executed on a simulator or remotely. if emulator: config.substitutions.append(("%run", emulator)) diff --git a/compiler-rt/test/lit.common.configured.in b/compiler-rt/test/lit.common.configured.in index 050792b6b262..019c594e4fec 100644 --- a/compiler-rt/test/lit.common.configured.in +++ b/compiler-rt/test/lit.common.configured.in @@ -72,6 +72,7 @@ else: set_default("target_suffix", "-%s" % config.target_arch) set_default("have_internal_symbolizer", @COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER_PYBOOL@) +set_default("have_disable_symbolizer_path_search", @SANITIZER_DISABLE_SYMBOLIZER_PATH_SEARCH_PYBOOL@) set_default("have_zlib", @ZLIB_FOUND_PYBOOL@) set_default("zlib_include_dir", "@ZLIB_INCLUDE_DIR@") set_default("zlib_library", "@ZLIB_LIBRARY@") diff --git a/compiler-rt/test/sanitizer_common/TestCases/disable_symbolizer_path_search.cpp b/compiler-rt/test/sanitizer_common/TestCases/disable_symbolizer_path_search.cpp new file mode 100644 index 000000000000..72317f60e235 --- /dev/null +++ b/compiler-rt/test/sanitizer_common/TestCases/disable_symbolizer_path_search.cpp @@ -0,0 +1,19 @@ +// REQUIRES: disable_symbolizer_path_search + +// RUN: %clangxx %s -o %t +// RUN: %env_unset_tool_symbolizer_path \ +// RUN: %env_tool_opts=verbosity=3 %run %t 2>&1 | FileCheck %s + +// CHECK: Symbolizer path search is disabled in the runtime build configuration + +#include +#include + +static void Symbolize() { + char buffer[100]; + __sanitizer_symbolize_pc(__builtin_return_address(0), "%p %F %L", buffer, + sizeof(buffer)); + printf("%s\n", buffer); +} + +int main() { Symbolize(); } diff --git a/llvm/utils/gn/secondary/compiler-rt/test/BUILD.gn b/llvm/utils/gn/secondary/compiler-rt/test/BUILD.gn index 020f3e7d9acd..a7ea1cf309b9 100644 --- a/llvm/utils/gn/secondary/compiler-rt/test/BUILD.gn +++ b/llvm/utils/gn/secondary/compiler-rt/test/BUILD.gn @@ -53,6 +53,7 @@ write_cmake_config("lit_common_configured") { "COMPILER_RT_BUILD_STANDALONE_LIBATOMIC_PYBOOL=False", "COMPILER_RT_DEBUG_PYBOOL=False", "COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER_PYBOOL=False", + "SANITIZER_DISABLE_SYMBOLIZER_PATH_SEARCH_PYBOOL=False", "COMPILER_RT_HAS_NO_DEFAULT_CONFIG_FLAG_PYBOOL=True", "COMPILER_RT_INTERCEPT_LIBDISPATCH_PYBOOL=False", "COMPILER_RT_RESOLVED_EXEC_OUTPUT_DIR=" +