This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-proton.git

commit 341176b76e019f3a4a7681d8e5785abf76dbb317
Author: Andrew Stitcher <[email protected]>
AuthorDate: Fri Nov 6 15:28:04 2026 -0500

    PROTON-2992: Put the clang sanitizer runtime in the RPATH
    
    clang builds the sanitizers with -shared-libasan, so that they can be
    LD_PRELOADed, but it keeps the runtime in a directory that the loader does
    not search. Nothing built that way can start:
    
      error while loading shared libraries: libclang_rt.asan.so:
      cannot open shared object file: No such file or directory
    
    For an asan or tsan build, ask the compiler for that directory with
    -print-runtime-dir and add it to the RPATH of everything we link. The
    compiler is selected with the ..._${CMAKE_C_COMPILER_ID} suffix, so gcc,
    whose sanitizer libraries are already on the default search path, gets an
    empty value and no special treatment. The existing -shared-libasan
    conditional becomes the same form.
    
    An RPATH rather than LD_LIBRARY_PATH in the test environment keeps the
    binaries runnable on their own, outside of ctest. It goes on the linker
    flags alone: in CMAKE_<LANG>_FLAGS it would reach the compile line too,
    where clang reports it as an unused argument.
    
    Assisted-By: Claude Opus 5 <[email protected]>
---
 tests/RuntimeCheck.cmake | 55 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 47 insertions(+), 8 deletions(-)

diff --git a/tests/RuntimeCheck.cmake b/tests/RuntimeCheck.cmake
index 68e494885..f5b098285 100644
--- a/tests/RuntimeCheck.cmake
+++ b/tests/RuntimeCheck.cmake
@@ -47,6 +47,41 @@ if((CMAKE_C_COMPILER_ID MATCHES "GNU"
   set(HAS_SANITIZERS TRUE)
 endif()
 
+# Per-compiler sanitizer settings, selected by the ..._${CMAKE_C_COMPILER_ID}
+# suffix below. A compiler with no entry gets an empty value and no special
+# treatment.
+
+# clang defaults to static sanitizer libs (which is preferred), but then we
+# cannot LD_PRELOAD.
+set(SHARED_LIBASAN_Clang "-shared-libasan")
+
+# Having linked the sanitizer runtime dynamically, clang puts it in a
+# directory that the loader does not search by default, so anything built
+# here fails to start. Ask the compiler where that directory is, to put it in
+# the RPATH. gcc's sanitizer libraries are already on the default search path.
+set(PRINT_RUNTIME_DIR_Clang "-print-runtime-dir")
+
+# Set <result> to a link flag that finds the compiler's sanitizer runtime at
+# run time, or to empty for a compiler that does not need one. Using RPATH
+# rather than LD_LIBRARY_PATH keeps the binaries runnable on their own,
+# outside of ctest.
+function(sanitizer_runtime_rpath result)
+  set(${result} "" PARENT_SCOPE)
+  set(print_runtime_dir ${PRINT_RUNTIME_DIR_${CMAKE_C_COMPILER_ID}})
+  if(NOT print_runtime_dir)
+    return()
+  endif()
+  execute_process(
+    COMMAND ${CMAKE_C_COMPILER} ${print_runtime_dir}
+    OUTPUT_VARIABLE runtime_dir
+    OUTPUT_STRIP_TRAILING_WHITESPACE
+    ERROR_QUIET)
+  if(runtime_dir)
+    file(REAL_PATH "${runtime_dir}" runtime_dir)
+    set(${result} "-Wl,-rpath,${runtime_dir}" PARENT_SCOPE)
+  endif()
+endfunction()
+
 # Valid values for RUNTIME_CHECK
 set(runtime_checks OFF asan tsan memcheck helgrind)
 
@@ -100,15 +135,11 @@ elseif(RUNTIME_CHECK STREQUAL "helgrind")
 elseif(RUNTIME_CHECK STREQUAL "asan")
   assert_has_sanitizers()
   message(STATUS "Runtime memory checker: gcc/clang memory sanitizers")
-  # clang defaults to static sanitizer libs (which is preferred), but then we 
cannot LD_PRELOAD
-  if (CMAKE_C_COMPILER_ID MATCHES "Clang")
-      set(CLANG_ASAN_FLAG "-shared-libasan")
-  endif()
-
-  set(SANITIZE_FLAGS "-g -fno-omit-frame-pointer ${CLANG_ASAN_FLAG} 
-fsanitize=address,undefined -fsanitize-recover=vptr")
+  set(SANITIZE_FLAGS "-g -fno-omit-frame-pointer 
${SHARED_LIBASAN_${CMAKE_C_COMPILER_ID}} -fsanitize=address,undefined 
-fsanitize-recover=vptr")
   set(TEST_WRAP_PREFIX "${PROJECT_SOURCE_DIR}/tests/preload_asan.sh 
$<TARGET_FILE:qpid-proton-core>")
   list(APPEND TEST_ENV 
"UBSAN_OPTIONS=suppressions=${PROJECT_SOURCE_DIR}/tests/ubsan.supp")
   list(APPEND TEST_ENV 
"LSAN_OPTIONS=suppressions=${PROJECT_SOURCE_DIR}/tests/lsan.supp")
+  sanitizer_runtime_rpath(SANITIZER_RPATH)
 
 elseif(RUNTIME_CHECK STREQUAL "tsan")
   assert_has_sanitizers()
@@ -116,6 +147,7 @@ elseif(RUNTIME_CHECK STREQUAL "tsan")
   set(SANITIZE_FLAGS "-g -fno-omit-frame-pointer -fsanitize=thread")
   set(TEST_WRAP_PREFIX "${PROJECT_SOURCE_DIR}/tests/preload_tsan.sh 
$<TARGET_FILE:qpid-proton-core>")
   list(APPEND TEST_ENV "TSAN_OPTIONS=second_deadlock_stack=1 
suppressions=${PROJECT_SOURCE_DIR}/tests/tsan.supp")
+  sanitizer_runtime_rpath(SANITIZER_RPATH)
 
 elseif(RUNTIME_CHECK)
   message(FATAL_ERROR "'RUNTIME_CHECK=${RUNTIME_CHECK}' is invalid, valid 
values: ${runtime_checks}")
@@ -123,8 +155,15 @@ endif()
 
 if(SANITIZE_FLAGS)
   set(ENABLE_UNDEFINED_ERROR OFF CACHE BOOL "Disabled for sanitizers" FORCE)
-  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZE_FLAGS}")
-  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZE_FLAGS}")
+  string(APPEND CMAKE_C_FLAGS " ${SANITIZE_FLAGS}")
+  string(APPEND CMAKE_CXX_FLAGS " ${SANITIZE_FLAGS}")
+  # A link flag only: in CMAKE_<LANG>_FLAGS it would also reach the compile
+  # line, where clang reports it as an unused argument. SANITIZER_RPATH is
+  # empty unless we are building with a compiler that needs it, and these
+  # appends then add nothing but a space.
+  string(APPEND CMAKE_EXE_LINKER_FLAGS " ${SANITIZER_RPATH}")
+  string(APPEND CMAKE_SHARED_LINKER_FLAGS " ${SANITIZER_RPATH}")
+  string(APPEND CMAKE_MODULE_LINKER_FLAGS " ${SANITIZER_RPATH}")
 endif()
 
 if(TEST_EXE_PREFIX)


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to