Skip to content

Commit

Permalink
make sanitizer configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
huangminghuang committed Feb 12, 2025
1 parent 5eca99c commit 4fefdef
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
fail-fast: false
matrix:
os: [ubuntu-24.04]
compiler: [gcc-12, gcc-13, gcc-14, clang-16, clang-17, clang-18]
compiler: [gcc-12, gcc-14, clang-16, clang-18]
build_type: [Release, Debug]
protoc: [find]
include:
- sanitize: OFF
- build_type: Debug
sanitize: ON
sanitize: "address,undefined"
- build_type: Coverage
compiler: clang-18
protoc: compile
Expand Down Expand Up @@ -76,7 +76,7 @@
run: |
cmake -GNinja -S . -B build -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
-DHPP_PROTO_PROTOC=${{ matrix.protoc }} \
-DHPP_PROTO_ENABLE_SANITIZER=${{ matrix.sanitize }} \
-DHPP_PROTO_ENABLE_SANITIZERS=${{ matrix.sanitize }} \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
Expand Down
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ option(HPP_PROTO_PROTOC_PLUGIN "Enable protoc plugin" ON)
option(HPP_PROTO_TESTS "Enable HPP_PROTO tests" ${PROJECT_IS_TOP_LEVEL})
option(HPP_PROTO_BENCHMARKS "Enable building benchmarks" OFF)

option(HPP_PROTO_ENABLE_SANITIZER "Enable address and undefined behavior sanitizer" OFF)
option(HPP_PROTO_ENABLE_SANITIZERS "The sanitizer to enable" OFF)
option(HPP_PROTO_BUILD_FUZZ "Build fuzz target" OFF)
option(HPP_PROTO_TEST_USE_PROTOBUF "Use libprotobuf to generate data for tests" OFF)

Expand Down Expand Up @@ -63,13 +63,13 @@ endif()

include(third-parties.cmake)

if(HPP_PROTO_ENABLE_SANITIZER)
if(HPP_PROTO_ENABLE_SANITIZERS)
if(MSVC)
# add_compile_options("/fsanitize=address")
# add_link_options("/fsanitize=address")
else()
add_compile_options("-fsanitize=address,undefined" "-fno-omit-frame-pointer")
add_link_options("-fsanitize=address,undefined")
add_compile_options("-fsanitize=${HPP_PROTO_ENABLE_SANITIZERS}" "-fno-omit-frame-pointer")
add_link_options("-fsanitize=${HPP_PROTO_ENABLE_SANITIZERS}")
endif()
endif()

Expand Down
14 changes: 12 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"HPP_PROTO_PROTOC_PLUGIN": false,
"HPP_PROTO_ENABLE_SANITIZER": true
"HPP_PROTO_ENABLE_SANITIZERS": "address,undefined"
}
},
{
Expand All @@ -38,10 +38,20 @@
"binaryDir": "${sourceDir}/build/debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"HPP_PROTO_ENABLE_SANITIZER": true,
"HPP_PROTO_ENABLE_SANITIZERS": "address,undefined",
"HPP_PROTO_TEST_USE_PROTOBUF": true
}
},
{
"name": "dev-msan",
"inherits": "default",
"displayName": "Memory Sanitizer Mode",
"binaryDir": "${sourceDir}/build/msan",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"HPP_PROTO_ENABLE_SANITIZERS": "memory"
}
},
{
"name": "release",
"inherits": "default",
Expand Down
6 changes: 3 additions & 3 deletions fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
if(DEFINED ENV{LIB_FUZZING_ENGINE})
set(LIB_FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE})
else()
set(FUZZ_COMPILE_FLAGS -fsanitize=fuzzer,address,undefined -fno-sanitize-recover=all)
set(LIB_FUZZING_ENGINE -fsanitize=fuzzer,address,undefined)
set(FUZZ_COMPILE_FLAGS "-fsanitize=fuzzer,${HPP_PROTO_ENABLE_SANITIZERS}" -fno-sanitize-recover=all)
set(LIB_FUZZING_ENGINE "-fsanitize=fuzzer,${HPP_PROTO_ENABLE_SANITIZERS}")
endif()

function(add_fuzz_target target source)
Expand All @@ -14,7 +14,7 @@ function(add_fuzz_target target source)

if(DEFINED FUZZ_COMPILE_FLAGS)
add_executable(${target}_debug_case ${source} fuzz_case_main.cpp)
target_compile_options(${target}_debug_case PRIVATE -fsanitize=address,undefined)
target_compile_options(${target}_debug_case PRIVATE -fsanitize=${HPP_PROTO_ENABLE_SANITIZERS})
target_link_libraries(${target}_debug_case PRIVATE hpp_proto::libhpp_proto unittest_proto_lib)
endif()
endfunction()
Expand Down
2 changes: 1 addition & 1 deletion tutorial/grpc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
find_package(gRPC CONFIG 1.51)

if(gRPC_FOUND AND NOT HPP_PROTO_ENABLE_SANITIZER)
if(gRPC_FOUND AND NOT HPP_PROTO_ENABLE_SANITIZERS)
## when sanitizer is enable, we can't use gRPC unless the gRPC library is built with sanitizer.
add_library(helloworld INTERFACE)

Expand Down

0 comments on commit 4fefdef

Please sign in to comment.