Skip to content

Commit

Permalink
Add address sanitizer and undefined behavior sanitizer (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored May 31, 2024
1 parent a388818 commit f1cff83
Show file tree
Hide file tree
Showing 56 changed files with 354 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ jobs:
git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs main
- name: Release android libs
if: github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa' && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
uses: svenstaro/upload-release-action@v2
with:
file_glob: true
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/flutter-linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,11 @@ jobs:
with:
name: flutter-sherpa-onnx-linux-x64
path: ./*.tar.bz2

# - name: Release android libs
# if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
# uses: svenstaro/upload-release-action@v2
# with:
# file_glob: true
# overwrite: true
# file: flutter*.tar.bz2
8 changes: 8 additions & 0 deletions .github/workflows/flutter-macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,11 @@ jobs:
with:
name: flutter-sherpa-onnx-app-macos-${{ matrix.arch }}
path: ./*.tar.bz2

- name: Release android libs
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
uses: svenstaro/upload-release-action@v2
with:
file_glob: true
overwrite: true
file: flutter*.tar.bz2
8 changes: 8 additions & 0 deletions .github/workflows/flutter-windows-x64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,11 @@ jobs:
with:
name: flutter-sherpa-onnx-windows-x64
path: ./*.tar.bz2

- name: Release android libs
if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
uses: svenstaro/upload-release-action@v2
with:
file_glob: true
overwrite: true
file: flutter*.tar.bz2
186 changes: 186 additions & 0 deletions .github/workflows/sanitizer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
name: sanitizer

on:
workflow_dispatch:

schedule:
# minute (0-59)
# hour (0-23)
# day of the month (1-31)
# month (1-12)
# day of the week (0-6)
# nightly build at 22:50 UTC time every day
- cron: "50 22 * * *"

concurrency:
group: sanitizer-${{ github.ref }}
cancel-in-progress: true

jobs:
sanitizer:
runs-on: ${{ matrix.os }}
name: sanitizer
strategy:
fail-fast: false
matrix:
os: [macos-latest]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: ccache
uses: hendrikmuhs/ccache-action@v1.2
with:
key: ${{ matrix.os }}-sanitizer

- name: Configure CMake
shell: bash
run: |
export CMAKE_CXX_COMPILER_LAUNCHER=ccache
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
cmake --version
mkdir build
cd build
cmake \
-DSHERPA_ONNX_ENABLE_PYTHON=ON \
-DSHERPA_ONNX_ENABLE_TESTS=ON \
-DSHERPA_ONNX_ENABLE_JNI=ON \
-DSHERPA_ONNX_ENABLE_SANITIZER=ON \
-D BUILD_SHARED_LIBS=ON \
-D CMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=./install \
..
- name: Build sherpa-onnx
shell: bash
run: |
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
cd build
make -j2
make install
ls -lh lib
ls -lh bin
file ./bin/sherpa-onnx
- name: Display dependencies of sherpa-onnx for macos
shell: bash
run: |
file bin/sherpa-onnx
otool -L build/bin/sherpa-onnx
otool -l build/bin/sherpa-onnx
- name: Test offline transducer
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline
.github/scripts/test-offline-transducer.sh
- name: Test online CTC
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx
.github/scripts/test-online-ctc.sh
- name: Test offline punctuation
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline-punctuation
.github/scripts/test-offline-punctuation.sh
- name: Test C API
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export SLID_EXE=spoken-language-identification-c-api
export SID_EXE=speaker-identification-c-api
export AT_EXE=audio-tagging-c-api
export PUNCT_EXE=add-punctuation-c-api
.github/scripts/test-c-api.sh
- name: Test Audio tagging
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline-audio-tagging
.github/scripts/test-audio-tagging.sh
- name: Test spoken language identification (C++ API)
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline-language-identification
.github/scripts/test-spoken-language-identification.sh
- name: Test transducer kws
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-keyword-spotter
.github/scripts/test-kws.sh
- name: Test offline TTS
if: matrix.with_tts == 'ON'
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline-tts
.github/scripts/test-offline-tts.sh
- name: Test online paraformer
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx
.github/scripts/test-online-paraformer.sh
- name: Test offline Whisper
if: matrix.build_type != 'Debug'
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline
.github/scripts/test-offline-whisper.sh
- name: Test offline CTC
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx-offline
.github/scripts/test-offline-ctc.sh
- name: Test online transducer
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=sherpa-onnx
.github/scripts/test-online-transducer.sh
- name: Test online transducer (C API)
shell: bash
run: |
export PATH=$PWD/build/bin:$PATH
export EXE=decode-file-c-api
.github/scripts/test-online-transducer.sh
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.13 FATAL_ERROR)

set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14" CACHE STRING "Minimum OS X deployment version. Used only for macOS")



project(sherpa-onnx)

set(SHERPA_ONNX_VERSION "1.9.26")
Expand Down Expand Up @@ -32,6 +34,7 @@ option(SHERPA_ONNX_ENABLE_BINARY "Whether to build binaries" ON)
option(SHERPA_ONNX_ENABLE_TTS "Whether to build TTS related code" ON)
option(SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY "True to link libstdc++ statically. Used only when BUILD_SHARED_LIBS is OFF on Linux" ON)
option(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE "True to use pre-installed onnxruntime if available" ON)
option(SHERPA_ONNX_ENABLE_SANITIZER "Whether to enable ubsan and asan" OFF)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
Expand Down Expand Up @@ -121,6 +124,7 @@ message(STATUS "SHERPA_ONNX_ENABLE_BINARY ${SHERPA_ONNX_ENABLE_BINARY}")
message(STATUS "SHERPA_ONNX_ENABLE_TTS ${SHERPA_ONNX_ENABLE_TTS}")
message(STATUS "SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY ${SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY}")
message(STATUS "SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE ${SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE}")
message(STATUS "SHERPA_ONNX_ENABLE_SANITIZER: ${SHERPA_ONNX_ENABLE_SANITIZER}")

if(SHERPA_ONNX_ENABLE_TTS)
message(STATUS "TTS is enabled")
Expand Down Expand Up @@ -267,6 +271,33 @@ if(SHERPA_ONNX_ENABLE_TTS)
include(cppjieba) # For Chinese TTS. It is a header-only C++ library
endif()

# if(NOT MSVC AND CMAKE_BUILD_TYPE STREQUAL Debug AND (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"))
if(SHERPA_ONNX_ENABLE_SANITIZER)
message(WARNING "enable ubsan and asan")
set(CMAKE_REQUIRED_LIBRARIES -lubsan -lasan)
include(CheckCCompilerFlag)

set(flags -fsanitize=undefined )
string(APPEND flags " -fno-sanitize-recover=undefined ")
string(APPEND flags " -fsanitize=integer ")
string(APPEND flags " -fsanitize=nullability ")
string(APPEND flags " -fsanitize=implicit-conversion ")
string(APPEND flags " -fsanitize=bounds ")
string(APPEND flags " -fsanitize=address ")

if(OFF)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flags} -Wall -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flags} -Wall -Wextra")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flags}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flags}")
endif()

set(CMAKE_EXECUTBLE_LINKER_FLAGS "${CMAKE_EXECUTBLE_LINKER_FLAGS} ${flags}")

add_compile_options(-fno-omit-frame-pointer)
endif()

add_subdirectory(sherpa-onnx)

if(SHERPA_ONNX_ENABLE_C_API AND SHERPA_ONNX_ENABLE_BINARY)
Expand Down
4 changes: 2 additions & 2 deletions c-api-examples/add-punctuation-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ int32_t main() {
const char *texts[] = {
"这是一个测试你好吗How are you我很好thank you are you ok谢谢你",
"我们都是木头人不会说话不会动",
"The African blogosphere is rapidly expanding bringing more voices "
"online in the form of commentaries opinions analyses rants and poetry",
("The African blogosphere is rapidly expanding bringing more voices "
"online in the form of commentaries opinions analyses rants and poetry"),
};

int32_t n = sizeof(texts) / sizeof(const char *);
Expand Down
2 changes: 0 additions & 2 deletions c-api-examples/decode-file-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ int32_t main(int32_t argc, char *argv[]) {

#define N 3200 // 0.2 s. Sample rate is fixed to 16 kHz

int16_t buffer[N];
float samples[N];
fprintf(stderr, "sample rate: %d, num samples: %d, duration: %.2f s\n",
wave->sample_rate, wave->num_samples,
(float)wave->num_samples / wave->sample_rate);
Expand Down
2 changes: 0 additions & 2 deletions c-api-examples/streaming-hlg-decode-file-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ int32_t main() {
// simulate streaming. You can choose an arbitrary N
#define N 3200

int16_t buffer[N];
float samples[N];
fprintf(stderr, "sample rate: %d, num samples: %d, duration: %.2f s\n",
wave->sample_rate, wave->num_samples,
(float)wave->num_samples / wave->sample_rate);
Expand Down
5 changes: 1 addition & 4 deletions cmake/espeak-ng-for-piper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ function(download_espeak_ng_for_piper)
set(USE_SPEECHPLAYER OFF CACHE BOOL "" FORCE)
set(EXTRA_cmn ON CACHE BOOL "" FORCE)
set(EXTRA_ru ON CACHE BOOL "" FORCE)

if(SHERPA_ONNX_ENABLE_WASM)
set(BUILD_ESPEAK_NG_EXE OFF CACHE BOOL "" FORCE)
endif()
set(BUILD_ESPEAK_NG_EXE OFF CACHE BOOL "" FORCE)

# If you don't have access to the Internet,
# please pre-download kaldi-decoder
Expand Down
6 changes: 3 additions & 3 deletions sherpa-onnx/c-api/c-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult(
r->text = pText;

// copy json
const auto &json = result.AsJsonString();
std::string json = result.AsJsonString();
char *pJson = new char[json.size() + 1];
std::copy(json.begin(), json.end(), pJson);
pJson[json.size()] = 0;
Expand Down Expand Up @@ -445,7 +445,7 @@ const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult(
r->text = pText;

// copy json
const auto &json = result.AsJsonString();
std::string json = result.AsJsonString();
char *pJson = new char[json.size() + 1];
std::copy(json.begin(), json.end(), pJson);
pJson[json.size()] = 0;
Expand Down Expand Up @@ -643,7 +643,7 @@ const SherpaOnnxKeywordResult *GetKeywordResult(
r->keyword = pKeyword;

// copy json
const auto &json = result.AsJsonString();
std::string json = result.AsJsonString();
char *pJson = new char[json.size() + 1];
std::copy(json.begin(), json.end(), pJson);
pJson[json.size()] = 0;
Expand Down
2 changes: 1 addition & 1 deletion sherpa-onnx/csrc/audio-tagging-ced-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class AudioTaggingCEDImpl : public AudioTaggingImpl {
std::vector<float> f = s->GetFrames();

int32_t num_frames = f.size() / feat_dim;
assert(feat_dim * num_frames == f.size());
assert(feat_dim * num_frames == static_cast<int32_t>(f.size()));

std::array<int64_t, 3> shape = {1, num_frames, feat_dim};

Expand Down
2 changes: 1 addition & 1 deletion sherpa-onnx/csrc/audio-tagging-label-file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void AudioTaggingLabels::Init(std::istream &is) {
exit(-1);
}

if (i != names_.size()) {
if (i != static_cast<int32_t>(names_.size())) {
SHERPA_ONNX_LOGE(
"Index should be sorted and contiguous. Expected index: %d, given: "
"%d.",
Expand Down
2 changes: 1 addition & 1 deletion sherpa-onnx/csrc/audio-tagging-zipformer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AudioTaggingZipformerImpl : public AudioTaggingImpl {

int32_t num_frames = f.size() / feat_dim;

assert(feat_dim * num_frames == f.size());
assert(feat_dim * num_frames == static_cast<int32_t>(f.size()));

std::array<int64_t, 3> shape = {1, num_frames, feat_dim};

Expand Down
2 changes: 1 addition & 1 deletion sherpa-onnx/csrc/cat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Ort::Value Cat(OrtAllocator *allocator,
}
}

return std::move(ans);
return ans;
}

template Ort::Value Cat<float>(OrtAllocator *allocator,
Expand Down
Loading

0 comments on commit f1cff83

Please sign in to comment.