diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 197117ef2..b4205b21f 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 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 -DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++ . + - 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..c7a95cd0f 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") +ENDIF () + if (WITH_CUDA) find_package(CUDA 11.0 REQUIRED) list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)