From 30ab31cc4194f57866ba48753aeceae40e823d81 Mon Sep 17 00:00:00 2001 From: nihui Date: Wed, 20 Jul 2022 18:16:28 +0800 Subject: [PATCH] add address sanitizer ci, fix potential memory leak shouted by asan (#4058) --- .github/workflows/linux-x64-cpu-gcc-san.yml | 42 +++++++++++++++++++++ CMakeLists.txt | 1 + src/CMakeLists.txt | 5 +++ src/layer/x86/cast_fp16.h | 8 ++-- 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/linux-x64-cpu-gcc-san.yml diff --git a/.github/workflows/linux-x64-cpu-gcc-san.yml b/.github/workflows/linux-x64-cpu-gcc-san.yml new file mode 100644 index 00000000000..ae57b37bc54 --- /dev/null +++ b/.github/workflows/linux-x64-cpu-gcc-san.yml @@ -0,0 +1,42 @@ +name: linux-x64-cpu-gcc-san +on: + push: + branches: [master] + paths: + - '.github/workflows/linux-x64-cpu-gcc-san.yml' + - 'CMakeLists.txt' + - 'cmake/**' + - 'src/*' + - 'src/layer/*' + - 'src/layer/x86/**' + - 'tests/**' + pull_request: + branches: [master] + paths: + - '.github/workflows/linux-x64-cpu-gcc-san.yml' + - 'CMakeLists.txt' + - 'cmake/**' + - 'src/*' + - 'src/layer/*' + - 'src/layer/x86/**' + - 'tests/**' +concurrency: + group: linux-x64-cpu-gcc-san-${{ github.ref }} + cancel-in-progress: true +permissions: + contents: read + +jobs: + linux-gcc-san: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - name: build + run: | + mkdir build && cd build + cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_ASAN=ON -DNCNN_BUILD_TESTS=ON -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. + cmake --build . -j 2 + - name: test + run: | + cd build + ctest --output-on-failure -j 2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 187d3b7ba7e..85b11ed616a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ option(NCNN_RUNTIME_CPU "runtime dispatch cpu routines" ON) option(NCNN_DISABLE_PIC "disable position-independent code" OFF) option(NCNN_BUILD_TESTS "build tests" OFF) option(NCNN_COVERAGE "build for coverage" OFF) +option(NCNN_ASAN "build for address sanitizer" OFF) option(NCNN_BUILD_BENCHMARK "build benchmark" ON) option(NCNN_PYTHON "build python api" OFF) option(NCNN_INT8 "int8 inference" ON) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a5582fc8b96..279f24be7b3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -471,6 +471,11 @@ if(NCNN_COVERAGE) target_link_libraries(ncnn PUBLIC -coverage -lgcov) endif() +if(NCNN_ASAN) + target_compile_options(ncnn PUBLIC -fsanitize=address) + target_link_libraries(ncnn PUBLIC -fsanitize=address) +endif() + add_dependencies(ncnn ncnn-generate-spirv) if(NCNN_INSTALL_SDK) diff --git a/src/layer/x86/cast_fp16.h b/src/layer/x86/cast_fp16.h index fb07a504d6f..8fba9748c3b 100644 --- a/src/layer/x86/cast_fp16.h +++ b/src/layer/x86/cast_fp16.h @@ -78,7 +78,7 @@ static void cast_fp32_to_fp16_sse(const Mat& bottom_blob, Mat& top_blob, const O { __m128 _v_fp32 = _mm_loadu_ps(ptr); __m128h _v_fp16 = _mm_cvtxps_ph(_v_fp32); - _mm_storeu_si128((__m128i*)outptr, (__m128i)_v_fp16); + _mm_storel_epi64((__m128i*)outptr, (__m128i)_v_fp16); ptr += 4; outptr += 4; @@ -97,7 +97,7 @@ static void cast_fp32_to_fp16_sse(const Mat& bottom_blob, Mat& top_blob, const O { __m128 _v_fp32 = _mm_loadu_ps(ptr); __m128i _v_fp16 = _mm_cvtps_ph(_v_fp32, _MM_FROUND_TRUNC); - _mm_storeu_si128((__m128i*)outptr, _v_fp16); + _mm_storel_epi64((__m128i*)outptr, _v_fp16); ptr += 4; outptr += 4; @@ -164,7 +164,7 @@ static void cast_fp16_to_fp32_sse(const Mat& bottom_blob, Mat& top_blob, const O } for (; i + 3 < size; i += 4) { - __m128h _v_fp16 = (__m128h)_mm_loadu_si128((const __m128i*)ptr); + __m128h _v_fp16 = (__m128h)_mm_loadl_epi64((const __m128i*)ptr); __m128 _v_fp32 = _mm_cvtxph_ps(_v_fp16); _mm_storeu_ps(outptr, _v_fp32); @@ -183,7 +183,7 @@ static void cast_fp16_to_fp32_sse(const Mat& bottom_blob, Mat& top_blob, const O } for (; i + 3 < size; i += 4) { - __m128i _v_fp16 = _mm_loadu_si128((const __m128i*)ptr); + __m128i _v_fp16 = _mm_loadl_epi64((const __m128i*)ptr); __m128 _v_fp32 = _mm_cvtph_ps(_v_fp16); _mm_storeu_ps(outptr, _v_fp32);