Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Leak Sanitizer and Undefined Behavior Sanitizer #45

Merged
merged 2 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/on_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
make -j2 VERBOSE=1

build_linux_gcc_asan:
name: Build on Linux with GCC+ASAN
name: Build on Linux with GCC+ASAN+UBSAN
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -45,7 +45,7 @@ jobs:
set -x
mkdir AddressSanitized
cd AddressSanitized
cmake -DBOOST_ROOT=$BOOST_ROOT_1_69_0 -DBoost_ARCHITECTURE=-x64 -DBINLOG_FORCE_TESTS=ON -DBINLOG_USE_ASAN=On ..
cmake -DBOOST_ROOT=$BOOST_ROOT_1_69_0 -DBoost_ARCHITECTURE=-x64 -DBINLOG_FORCE_TESTS=ON -DBINLOG_USE_ASAN=On -DBINLOG_USE_UBSAN=On ..
- name: Build
run: |
cd AddressSanitized
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include(Coverage)
include(MarkdownToHtml)
include(OptionalCompileOption)
include(ThreadSanitizer)
include(UndefinedSanitizer)

#---------------------------
# Depencencies
Expand Down Expand Up @@ -269,6 +270,7 @@ if(Boost_FOUND)
target_include_directories(UnitTest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bin)

add_test(NAME UnitTest COMMAND UnitTest --log_level=test_suite --color_output=true)
set_property(TEST UnitTest PROPERTY ENVIRONMENT ASAN_OPTIONS=detect_leaks=1)

else()
message(STATUS "Boost not found, will not build unit tests")
Expand Down Expand Up @@ -297,6 +299,7 @@ if(Boost_FOUND)

add_test(NAME IntegrationTest
COMMAND IntegrationTest --log_level=test_suite --color_output=true -- "$<TARGET_FILE:bread>" "$<TARGET_FILE_DIR:IntegrationTest>" "${PROJECT_SOURCE_DIR}")
set_property(TEST IntegrationTest PROPERTY ENVIRONMENT ASAN_OPTIONS=detect_leaks=1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#46


function(add_inttest name)
add_executable(${name} test/integration/${name}.cpp $<$<CXX_COMPILER_ID:MSVC>:bin/binaryio.cpp>)
Expand Down
4 changes: 3 additions & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ To be used when invoking `cmake`.

- `-DBINLOG_USE_CLANG_TIDY`: runs clang-tidy on built sources. Requires cmake 3.6 or greater.
- `-DBINLOG_SOURCE_BROWSER_URL`: if specified, links to code in documentation will use this prefix
- `-DBINLOG_USE_ASAN`: use [address sanitizer][] (with gcc or clang)
- `-DBINLOG_USE_ASAN`: use [address sanitizer][] (with gcc or clang) - also enables the Leak Sanitizer
- `-DBINLOG_USE_TSAN`: use [thread sanitizer][] (with gcc or clang)
- `-DBINLOG_USE_UBSAN`: use [undefined behavior sanitizer][] (with gcc or clang)
- `-DBINLOG_GEN_COVERAGE`: generate coverage data
- `-DBINLOG_FORCE_TESTS`: fail during configuration if tests will not be built
- `-DBOOST_ROOT`: specifies the path to an alternate boost installation (tests depend on boost)

[address sanitizer]: https://github.com/google/sanitizers/wiki/AddressSanitizer
[thread sanitizer]: https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual
[undefined behavior sanitizer]: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html

## Different Build Flavours

Expand Down
28 changes: 28 additions & 0 deletions cmake/UndefinedSanitizer.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
include(CheckCXXCompilerFlag)
include(CMakePushCheckState)

option(BINLOG_USE_UBSAN "Build with Undefined Behavior Sanitizer (UBSAN) enabled" OFF)

if (BINLOG_USE_UBSAN)

check_cxx_compiler_flag("-fsanitize=undefined" HAS_UBSAN)

CMAKE_PUSH_CHECK_STATE(RESET)
# Make check_cxx_compiler_flag pass required flags to linker as well:
set(CMAKE_REQUIRED_FLAGS "-fsanitize=undefined -static-libubsan")
check_cxx_compiler_flag("-fsanitize=undefined -static-libubsan" HAS_UBSAN_NEEDS_LIB)
CMAKE_POP_CHECK_STATE()

if (HAS_UBSAN_NEEDS_LIB)
add_compile_options("-fsanitize=undefined" "-static-libubsan")
add_link_options("-fsanitize=undefined" "-static-libubsan")
elseif (HAS_UBSAN)
add_compile_options("-fsanitize=undefined")
add_link_options("-fsanitize=undefined")
else ()
message(FATAL_ERROR "Undefined Behavior Sanitizer requested by BINLOG_USE_UBSAN, but appears to be not supported on this platform")
endif ()

message(STATUS "Use Undefined Behavior Sanitizer")

endif ()
7 changes: 5 additions & 2 deletions include/mserialize/detail/integer_to_hex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ constexpr std::size_t hex_string_size(Integer v)
#pragma warning(disable : 4146)
#endif

// static_cast below: writing v /= 16 triggers GCC ubsan error:
// error: conversion from 'int' to 'unsigned char' may change value [-Werror=conversion]

template <typename Integer>
constexpr char* write_integer_as_hex(Integer v, char* end)
{
Expand All @@ -45,15 +48,15 @@ constexpr char* write_integer_as_hex(Integer v, char* end)
while (v != 0)
{
*--end = digits[v % 16];
v /= 16;
v = static_cast<Integer>(v / 16);
}
}
else
{
while (v != 0)
{
*--end = digits[-(v % 16)];
v /= 16;
v = static_cast<Integer>(v / 16);
}
*--end = '-';
}
Expand Down