Skip to content

Commit

Permalink
add address sanitizer ci, fix potential memory leak shouted by asan (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui authored Jul 20, 2022
1 parent 0ea7a67 commit 30ab31c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/linux-x64-cpu-gcc-san.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/layer/x86/cast_fp16.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit 30ab31c

Please sign in to comment.