From 5ef6f7f38990961c0337e3982fb62da4d94947c1 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 6 Jun 2026 15:39:38 +0800 Subject: [PATCH] [fix](build) Strip sanitizer and platform-specific flags for OpenBLAS/FAISS ### What problem does this PR solve? Issue Number: close #64170 Problem Summary: On aarch64, building Doris BE with LSAN/ASAN/ASAN_UT/UBSAN sanitizer modes fails during CMake configuration or compilation: 1. LSAN/ASAN/ASAN_UT: The OpenBLAS getarch build-time tool inherits sanitizer flags from the parent CMake context, causing LeakSanitizer to detect memory leaks in OpenBLAS's detect() function (strdup without free on aarch64). This makes getarch exit with non-zero code and CMake configuration fails. 2. UBSAN: -mcmodel=medium is x86_64-specific and unsupported on aarch64. It causes compilation failure in both OpenBLAS getarch and Doris BE targets (e.g. ann_index.cpp). The cmake-protect layer strips it for OpenBLAS/FAISS, but the global CXX_FLAGS_UBSAN still contains it, breaking all BE targets. Also fix __lsan_ignore_object declaration: previously only enabled under ADDRESS_SANITIZER, but LEAK_SANITIZER also provides this symbol, so the empty stub was incorrectly used under LSAN mode causing link errors. The fix: - Strip sanitizer flags (-fsanitize=*, -fno-sanitize=*, -fsanitize-ignorelist=*) and -D*SANITIZER defines from CMAKE_C_FLAGS/CMAKE_CXX_FLAGS in cmake-protect CMakeLists.txt before building OpenBLAS and FAISS - Strip -mcmodel= in cmake-protect for OpenBLAS/FAISS - Move -mcmodel=medium in CXX_FLAGS_UBSAN to only apply on ARCH_AMD64 ### Release note None ### Check List (For Author) - Test: Manual test - Verified LSAN/ASAN/ASAN_UT BE UT can build and run successfully on aarch64 - Behavior changed: No - Does this need documentation: No --- be/CMakeLists.txt | 5 ++++- be/src/common/phdr_cache.cpp | 2 +- .../storage/index/ann/cmake-protect/CMakeLists.txt | 13 +++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/be/CMakeLists.txt b/be/CMakeLists.txt index 74c0e998226491..cfa5a0552a3edc 100644 --- a/be/CMakeLists.txt +++ b/be/CMakeLists.txt @@ -470,7 +470,10 @@ set(CXX_FLAGS_ASAN_UT "-O0 -fsanitize=address -DADDRESS_SANITIZER") # Set the flags to the undefined behavior sanitizer, also known as "ubsan" # Turn on sanitizer and debug symbols to get stack traces: -set(CXX_FLAGS_UBSAN "-O0 -fno-wrapv -mcmodel=medium -fsanitize=undefined -DUNDEFINED_BEHAVIOR_SANITIZER") +set(CXX_FLAGS_UBSAN "-O0 -fno-wrapv -fsanitize=undefined -DUNDEFINED_BEHAVIOR_SANITIZER") +if (ARCH_AMD64) + set(CXX_FLAGS_UBSAN "${CXX_FLAGS_UBSAN} -mcmodel=medium") +endif() # Set the flags to the thread sanitizer, also known as "tsan" # Turn on sanitizer and debug symbols to get stack traces: diff --git a/be/src/common/phdr_cache.cpp b/be/src/common/phdr_cache.cpp index e0f5ff43fed6a5..deb543bc3716a6 100644 --- a/be/src/common/phdr_cache.cpp +++ b/be/src/common/phdr_cache.cpp @@ -107,7 +107,7 @@ extern "C" */ extern "C" { -#ifdef ADDRESS_SANITIZER +#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) void __lsan_ignore_object(const void*); #else void __lsan_ignore_object(const void*) {} // NOLINT diff --git a/be/src/storage/index/ann/cmake-protect/CMakeLists.txt b/be/src/storage/index/ann/cmake-protect/CMakeLists.txt index 0c65e4ae170f7e..c5c35c1c32a077 100644 --- a/be/src/storage/index/ann/cmake-protect/CMakeLists.txt +++ b/be/src/storage/index/ann/cmake-protect/CMakeLists.txt @@ -16,6 +16,19 @@ # under the License. # Make sure compile check in doris will not break compilation of faiss and openblas +# Strip sanitizer flags to prevent build-time tools (e.g., openblas getarch) from +# triggering LeakSanitizer/AddressSanitizer errors during CMake configure phase +string(REGEX REPLACE "-fsanitize=[a-z,=_./-]+" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +string(REGEX REPLACE "-fsanitize=[a-z,=_./-]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +string(REGEX REPLACE "-fno-sanitize=[a-z,=_./-]+" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +string(REGEX REPLACE "-fno-sanitize=[a-z,=_./-]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +string(REGEX REPLACE "-fsanitize-ignorelist=[a-z,=_./-]+" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +string(REGEX REPLACE "-fsanitize-ignorelist=[a-z,=_./-]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +string(REGEX REPLACE "-D[A-Z_]+SANITIZER" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +string(REGEX REPLACE "-D[A-Z_]+SANITIZER" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +# Strip -mcmodel= which is x86_64-specific and unsupported on aarch64 +string(REGEX REPLACE "-mcmodel=[a-z]+" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +string(REGEX REPLACE "-mcmodel=[a-z]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")