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

Add Undefined Behavior Sanitizer #180

Merged
merged 8 commits into from
Nov 5, 2023
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
11 changes: 8 additions & 3 deletions .github/workflows/core-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
BUILD_TYPE: [ Release, Debug ]
cfg:
- { BUILD_TYPE: Release }
- { BUILD_TYPE: Debug }
- { BUILD_TYPE: Debug, SANITIZER : ADDRESS }
- { BUILD_TYPE: Debug, SANITIZER : UB }
steps:
- uses: actions/checkout@v3
- name: Download libraries
Expand All @@ -26,10 +30,11 @@ jobs:
-DCMAKE_C_COMPILER=gcc-10
-DCMAKE_CXX_COMPILER=g++-10
-DBOOST_ROOT=${{github.workspace}}/lib/boost
-DCMAKE_BUILD_TYPE=${{matrix.BUILD_TYPE}}
-DCMAKE_BUILD_TYPE=${{matrix.cfg.BUILD_TYPE}}
-DSANITIZER=${{matrix.cfg.SANITIZER}}
-Dgtest_disable_pthreads=OFF
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{matrix.BUILD_TYPE}}
run: cmake --build ${{github.workspace}}/build --config ${{matrix.cfg.BUILD_TYPE}}
- name: Test
working-directory: ${{github.workspace}}/build/target
shell: bash
Expand Down
61 changes: 55 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ option(COMPILE_PYBIND "Build the python bindings" OFF)
option(COPY_PYTHON_EXAMPLES "Copy Python examples" OFF)
option(COMPILE_TESTS "Build tests" ON)
option(UNPACK_DATASETS "Unpack datasets" ON)
option(ASAN "Enable sanitizer" ON)
set(SANITIZER "" CACHE STRING "Build with sanitizer, possible values: ADDRESS, UB")

# By default select Debug build
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif(NOT CMAKE_BUILD_TYPE)

if (SANITIZER)
if (SANITIZER STREQUAL "ADDRESS")
set(ASAN ON)
elseif (SANITIZER STREQUAL "UB")
set(UBSAN ON)
else ()
message(FATAL_ERROR "Unknown sanitizer '${SANITIZER}', try cmake -LH")
endif ()
endif ()

if (ASAN AND COMPILE_PYBIND)
message(WARNING "Disabling ASAN with python bindings")
message(WARNING "Disabling ASAN with python bindings.")
set(ASAN OFF)
endif()

Expand All @@ -28,12 +38,51 @@ if (MSVC)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/target")
else()
# -DELPP_THREAD_SAFE -- for easylogging++ thread safety
add_compile_options("$<$<CONFIG:Debug>:-O0;-DELPP_THREAD_SAFE;-g;-Wall;-Wextra;-Werror;-fno-omit-frame-pointer>")
set(BUILD_OPTS "-DELPP_THREAD_SAFE")

# RELEASE build options
string(JOIN ";" RELEASE_BUILD_OPTS "${BUILD_OPTS}"
"-O3")

set(DEBUG_BUILD_OPTS "${BUILD_OPTS}")

# Set common DEBUG build options
string(JOIN ";" DEBUG_BUILD_OPTS "${DEBUG_BUILD_OPTS}"
"-g"
"-Wall"
"-Wextra"
"-Werror"
"-fno-omit-frame-pointer"
"-fno-optimize-sibling-calls")

if (ASAN)
add_compile_options("$<$<CONFIG:Debug>:-fsanitize=address>")
add_link_options("$<$<CONFIG:Debug>:-fsanitize=address>")
# Set DEBUG build options specific for build with ASAN
set(ASAN_OPTS "-fsanitize=address")
string(JOIN ";" DEBUG_BUILD_OPTS "${DEBUG_BUILD_OPTS}"
"-O1"
"${ASAN_OPTS}")
set(DEBUG_LINK_OPTS "${ASAN_OPTS}")
elseif (UBSAN)
# Set DEBUG build options specific for build with UBSAN
string(JOIN ";" UBSAN_OPTS "-fsanitize=undefined"
"-fsanitize=float-divide-by-zero"
"-fno-sanitize=signed-integer-overflow" # Remove this when CustomRandom gets fixed
"-fno-sanitize=shift" # Remove this when CustomRandom gets fixed
"-fno-sanitize-recover=all") # For tests to fail if UBSan finds an error
string(JOIN ";" DEBUG_BUILD_OPTS "${DEBUG_BUILD_OPTS}"
"-O1"
"${UBSAN_OPTS}")
set(DEBUG_LINK_OPTS "${UBSAN_OPTS}")
else ()
# No sanitizer, just debug build
string(JOIN ";" DEBUG_BUILD_OPTS "${DEBUG_BUILD_OPTS}"
"-O0")
endif()
add_compile_options("$<$<CONFIG:Release>:-O3;-DELPP_THREAD_SAFE>")

add_compile_options("$<$<CONFIG:Debug>:${DEBUG_BUILD_OPTS}>")
add_link_options("$<$<CONFIG:Debug>:${DEBUG_LINK_OPTS}>")

add_compile_options("$<$<CONFIG:Release>:${RELEASE_BUILD_OPTS}>")
endif()

# configuring boost
Expand Down
29 changes: 22 additions & 7 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@

function print_help() {
cat << EOF
Usage: ./build.sh [-h|--help] [-p|--pybind] [-n|--no-test]
[-u|--no-unpack] [-jN|--jobs=N] [-d|--debug]
Usage: ./build.sh [options]

Possible options:
-h, --help Display help
-p, --pybind Compile python bindings
-n, --no-tests Don't build tests
-u, --no-unpack Don't unpack datasets
-j[N], --jobs[=N] Allow N jobs at once (default [=1])
-d, --debug Set debug build type
-s[S], --sanitizer[=S] Build with sanitizer S (has effect only for debug build).
Possible values of S: ADDRESS, UB.
ADDRESS - Address Sanitizer
UB - Undefined Behavior Sanitizer

EOF
}

for i in "$@"
do
case $i in
-p|--pybind) # Enable python bindings compile
-p|--pybind) # Compile python bindings
PYBIND=true
;;
-n|--no-tests) # Disable tests build
-n|--no-tests) # Don't build tests
NO_TESTS=true
;;
-u|--no-unpack) # Don't unpack datasets
Expand All @@ -33,7 +37,15 @@ for i in "$@"
-d|--debug) # Set debug build type
DEBUG_MODE=true
;;
-h|--help|*) # Display help.
# It's a nightmare, we should use getopts for args parsing or even use something else
# instead of bash
--sanitizer=*) # Build with sanitizer S, long option
SANITIZER="${i#*=}"
;;
-s*) # Build with sanitizer S, short option
SANITIZER="${i#*s}"
;;
-h|--help|*) # Display help
print_help
exit 0
;;
Expand Down Expand Up @@ -73,9 +85,12 @@ if [[ $DEBUG_MODE != true ]]; then
PREFIX="$PREFIX -D CMAKE_BUILD_TYPE=Release"
fi

if [[ -v SANITIZER ]]; then
PREFIX="$PREFIX -D SANITIZER=${SANITIZER}"
fi

cd ..
mkdir -p build
cd build
rm CMakeCache.txt
cmake $PREFIX ..
make $JOBS_OPTION
cmake $PREFIX .. && make $JOBS_OPTION
2 changes: 2 additions & 0 deletions src/core/algorithms/fd/fd_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "fd_algorithm.h"

#include <map>
#include <thread>
#include <vector>

#include "config/equal_nulls/option.h"
#include "config/tabular_data/input_table/option.h"
Expand Down