From 0efd96b96b3c086660cd0cef0b9d2375d6af40a1 Mon Sep 17 00:00:00 2001 From: 3manifold <22544721+3manifold@users.noreply.github.com> Date: Thu, 17 Jul 2025 13:58:51 +0200 Subject: [PATCH 1/6] Integrate Google AddressSanitizer (aka ASan) in tests --- .github/workflows/ci.yml | 16 ++++++++++++++-- CMakeLists.txt | 12 ++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 197117ef2..3c2057d6c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: strategy: matrix: os: [ubuntu-22.04] - backend: [mkl, dnnl] + backend: [mkl, dnnl, asan] steps: - uses: actions/checkout@v4 @@ -50,6 +50,15 @@ jobs: sudo apt-get install -y intel-oneapi-dnnl-devel=$DNNL_VERSION intel-oneapi-dnnl=$DNNL_VERSION cmake -DCMAKE_INSTALL_PREFIX=$PWD/install -DBUILD_TESTS=ON -DWITH_MKL=OFF -DOPENMP_RUNTIME=COMP -DWITH_DNNL=ON . + - name: Configure with ASAN + if: startsWith(matrix.os, 'ubuntu') && matrix.backend == 'asan' + env: + DNNL_VERSION: 2023.0.0-25399 + run: | + sudo apt-get install -y intel-oneapi-dnnl-devel=$DNNL_VERSION intel-oneapi-dnnl=$DNNL_VERSION + cmake -DCMAKE_INSTALL_PREFIX=$PWD/install -DBUILD_TESTS=ON -DWITH_MKL=OFF -DOPENMP_RUNTIME=COMP -DWITH_DNNL=ON \ + -DGOOGLE_ADDRESS_SANITIZER=ON -DCMAKE_BUILD_TYPE=Debug . + - name: Build run: | make install @@ -71,7 +80,10 @@ jobs: if: matrix.backend == 'dnnl' run: | tests/ctranslate2_test tests/data - + - name: Test ASAN + if: matrix.backend == 'asan' + run: | + ASAN_OPTIONS=detect_leaks=1:print_stats=1 tests/ctranslate2_test tests/data build-and-test-cpp-aarch64: runs-on: ubuntu-22.04 diff --git a/CMakeLists.txt b/CMakeLists.txt index 62b99d136..f5805da8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,8 @@ option(BUILD_TESTS "Compile the tests" OFF) option(BUILD_SHARED_LIBS "Build shared libraries" ON) option(WITH_TENSOR_PARALLEL "Compile with NCCL and MPI backend" OFF) option(WITH_FLASH_ATTN "Compile with Flash Attention 2" OFF) +option(GOOGLE_ADDRESS_SANITIZER "ASAN" OFF) + if(ENABLE_PROFILING) message(STATUS "Enable profiling support") @@ -444,6 +446,16 @@ if (WITH_RUY) list(APPEND LIBRARIES ruy) endif() +IF (GOOGLE_ADDRESS_SANITIZER AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "DEBUG")) + MESSAGE (STATUS "GOOGLE_ADDRESS_SANITIZER: ENABLED") + set(ASAN_FLAGS " -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-common") + string(APPEND CMAKE_C_FLAGS ${ASAN_FLAGS}) + string(APPEND CMAKE_CXX_FLAGS ${ASAN_FLAGS}) + add_link_options(-fsanitize=address) +ELSEIF (GOOGLE_ADDRESS_SANITIZER) + MESSAGE(FATAL_ERROR "SANITIZER requires Debug build type") # simplify CMake configuration for now +ENDIF () + if (WITH_CUDA) find_package(CUDA 11.0 REQUIRED) list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) From 842035ce909205a6c15465d2b3aeaefded8907fd Mon Sep 17 00:00:00 2001 From: 3manifold <22544721+3manifold@users.noreply.github.com> Date: Mon, 21 Jul 2025 15:16:20 +0200 Subject: [PATCH 2/6] Use Clang AddressSanitizer instead --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3c2057d6c..b4205b21f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,9 +55,9 @@ jobs: env: DNNL_VERSION: 2023.0.0-25399 run: | - sudo apt-get install -y intel-oneapi-dnnl-devel=$DNNL_VERSION intel-oneapi-dnnl=$DNNL_VERSION + sudo apt-get install -y llvm libomp-dev intel-oneapi-dnnl-devel=$DNNL_VERSION intel-oneapi-dnnl=$DNNL_VERSION cmake -DCMAKE_INSTALL_PREFIX=$PWD/install -DBUILD_TESTS=ON -DWITH_MKL=OFF -DOPENMP_RUNTIME=COMP -DWITH_DNNL=ON \ - -DGOOGLE_ADDRESS_SANITIZER=ON -DCMAKE_BUILD_TYPE=Debug . + -DGOOGLE_ADDRESS_SANITIZER=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++ . - name: Build run: | From bbc7ba8ca3439919f069798190981bf322d85975 Mon Sep 17 00:00:00 2001 From: 3manifold <22544721+3manifold@users.noreply.github.com> Date: Mon, 21 Jul 2025 15:21:08 +0200 Subject: [PATCH 3/6] [TMP] Test memory leak --- include/ctranslate2/ops/squeeze.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/ctranslate2/ops/squeeze.h b/include/ctranslate2/ops/squeeze.h index 3e667e339..8002341df 100644 --- a/include/ctranslate2/ops/squeeze.h +++ b/include/ctranslate2/ops/squeeze.h @@ -11,6 +11,7 @@ namespace ctranslate2 { public: Squeeze(const std::vector& axes) : _axes(axes) { + auto leak_ptr = new int(22); (void)leak_ptr; std::sort(_axes.begin(), _axes.end()); } From 9885b0713a5aead21391e4d232c5aecc23438716 Mon Sep 17 00:00:00 2001 From: 3manifold <22544721+3manifold@users.noreply.github.com> Date: Mon, 21 Jul 2025 16:09:43 +0200 Subject: [PATCH 4/6] Revert "[TMP] Test memory leak" This reverts commit bbc7ba8ca3439919f069798190981bf322d85975. --- include/ctranslate2/ops/squeeze.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/ctranslate2/ops/squeeze.h b/include/ctranslate2/ops/squeeze.h index 8002341df..3e667e339 100644 --- a/include/ctranslate2/ops/squeeze.h +++ b/include/ctranslate2/ops/squeeze.h @@ -11,7 +11,6 @@ namespace ctranslate2 { public: Squeeze(const std::vector& axes) : _axes(axes) { - auto leak_ptr = new int(22); (void)leak_ptr; std::sort(_axes.begin(), _axes.end()); } From c122cd44c8f0530e71ba1dbebe211e99a521c413 Mon Sep 17 00:00:00 2001 From: 3manifold <22544721+3manifold@users.noreply.github.com> Date: Tue, 22 Jul 2025 10:06:46 +0200 Subject: [PATCH 5/6] Remove redundant comment in CMakeLists.txt --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5805da8e..c7a95cd0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -453,7 +453,7 @@ IF (GOOGLE_ADDRESS_SANITIZER AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUI string(APPEND CMAKE_CXX_FLAGS ${ASAN_FLAGS}) add_link_options(-fsanitize=address) ELSEIF (GOOGLE_ADDRESS_SANITIZER) - MESSAGE(FATAL_ERROR "SANITIZER requires Debug build type") # simplify CMake configuration for now + MESSAGE(FATAL_ERROR "SANITIZER requires Debug build type") ENDIF () if (WITH_CUDA) From 691cc0aa11d77fdb607cc8e98514c45fab4d054f Mon Sep 17 00:00:00 2001 From: 3manifold <22544721+3manifold@users.noreply.github.com> Date: Thu, 6 Nov 2025 10:14:36 +0100 Subject: [PATCH 6/6] Add messages for compiler ID and version --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index c7a95cd0f..0ffce6efd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,8 @@ option(WITH_TENSOR_PARALLEL "Compile with NCCL and MPI backend" OFF) option(WITH_FLASH_ATTN "Compile with Flash Attention 2" OFF) option(GOOGLE_ADDRESS_SANITIZER "ASAN" OFF) +MESSAGE(STATUS "Compiler Id: ${CMAKE_CXX_COMPILER_ID}") +MESSAGE(STATUS "Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}") if(ENABLE_PROFILING) message(STATUS "Enable profiling support")