diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ff63a23..b49490b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,9 +6,9 @@ jobs: build: runs-on: ubuntu-20.04 - + steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 with: submodules: recursive - name: install diff --git a/.github/workflows/sanitizers.yml b/.github/workflows/sanitizers.yml new file mode 100644 index 0000000..5898593 --- /dev/null +++ b/.github/workflows/sanitizers.yml @@ -0,0 +1,32 @@ +name: sanitizers + +on: [push] + +jobs: + build: + runs-on: ubuntu-20.04 + env: + CC: clang + CXX: clang++ + strategy: + matrix: + sanitizer: + - ADDRESS_SANITIZER + - MEMORY_SANITIZER + - THREAD_SANITIZER + - UNDEFINED_BEHAVIOR_SANITIZER + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + + - name: install + run: sudo apt-get update && sudo apt-get --no-install-recommends -y install cmake clang + + - name: build + run: > + mkdir cmake ; + cd cmake ; + cmake .. -DENABLE_TESTS=true -D${{matrix.sanitizer}}=true; + make -j$(nproc); + run-parts ./tests/ || true; diff --git a/CMakeLists.txt b/CMakeLists.txt index a348959..81f2fdd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,10 @@ project(peer) option(ENABLE_TESTS "Enable tests" OFF) option(BUILD_SHARED_LIBS "Build shared libraries" ON) +option(ADDRESS_SANITIZER "Build with AddressSanitizer." OFF) +option(MEMORY_SANITIZER "Build with MemorySanitizer." OFF) +option(THREAD_SANITIZER "Build with ThreadSanitizer." OFF) +option(UNDEFINED_BEHAVIOR_SANITIZER "Build with UndefinedBehaviorSanitizer." OFF) include(ExternalProject) @@ -30,6 +34,29 @@ set(DEP_LIBS "srtp2" "usrsctp" "mbedtls" "mbedcrypto" "mbedx509" "cjson") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g3") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3") +function(enableSanitizer SANITIZER) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -g -fsanitize=${SANITIZER} -fno-omit-frame-pointer -fno-optimize-sibling-calls" PARENT_SCOPE) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${SANITIZER}" PARENT_SCOPE) +endfunction() + +if(ADDRESS_SANITIZER) + enableSanitizer("address") +endif() + +if(MEMORY_SANITIZER) + enableSanitizer("memory") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-memory-track-origins") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize-memory-track-origins") +endif() + +if(THREAD_SANITIZER) + enableSanitizer("thread") +endif() + +if(UNDEFINED_BEHAVIOR_SANITIZER) + enableSanitizer("undefined") +endif() + add_definitions("-Wunused-variable -Werror=sequence-point -Werror=pointer-sign -Werror=return-type -Werror=sizeof-pointer-memaccess -Wincompatible-pointer-types -DHTTP_DO_NOT_USE_CUSTOM_CONFIG -DMQTT_DO_NOT_USE_CUSTOM_CONFIG") add_subdirectory(src)