Skip to content

Commit

Permalink
cmake: support sanitizers natively
Browse files Browse the repository at this point in the history
... so that users can easily test sanitized binaries themselves.
  • Loading branch information
lzaoral committed Jul 21, 2022
1 parent f3008e4 commit 6de1f7c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
29 changes: 6 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,10 @@ jobs:
sudo apt update
sudo apt install -y clang
- name: '[Jammy] Add sanitizers to CFLAGS'
if: matrix.version >= 22.04
run: |
# Use ASAN and UBSAN
CFLAGS="$CFLAGS -fsanitize=address,undefined"
# Recommended for better error traces
CFLAGS="$CFLAGS -fno-omit-frame-pointer"
# Make UBSAN reports fatal
CFLAGS="$CFLAGS -fno-sanitize-recover=all"
# FIXME: release also the memory allocated by asprintf() and
# read_custom_opts()
ASAN_OPTIONS="detect_leaks=0"
# Make UBSAN print whole stack traces
UBSAN_OPTIONS="print_stacktrace=1"
# Store the env variables
echo "CFLAGS=$CFLAGS" >> "$GITHUB_ENV"
echo "ASAN_OPTIONS=$ASAN_OPTIONS" >> "$GITHUB_ENV"
echo "UBSAN_OPTIONS=$UBSAN_OPTIONS" >> "$GITHUB_ENV"
- name: Build and check
run: make distcheck
run: |
if [[ "${{ matrix.version }}" == 22.04 ]]; then
make distcheck-sanitizers
else
make distcheck
fi
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ CMAKE ?= cmake
CTEST ?= ctest -j$(NUM_CPU)

STATIC ?= OFF
SANITIZERS ?= OFF

CMAKE_BUILD_TYPE ?= RelWithDebInfo

.PHONY: all check clean static distclean distcheck distcheck-static install
.PHONY: all check clean static sanitizers distclean distcheck \
distcheck-static distcheck-sanitizers install

all:
mkdir -p cscppc_build
cd cscppc_build && $(CMAKE) \
-DCMAKE_BUILD_TYPE="$(CMAKE_BUILD_TYPE)" \
-DSTATIC_LINKING="$(STATIC)" ..
-DSTATIC_LINKING="$(STATIC)" \
-DSANITIZERS="$(SANITIZERS)" ..
$(MAKE) -sC cscppc_build -j$(NUM_CPU)

static:
$(MAKE) -s all STATIC=ON

sanitizers:
$(MAKE) -s all SANITIZERS=ON

check: all
cd cscppc_build && $(CTEST) --output-on-failure

Expand All @@ -51,5 +57,8 @@ distcheck: distclean
distcheck-static:
$(MAKE) -s distcheck STATIC=ON

distcheck-sanitizers:
$(MAKE) -s distcheck SANITIZERS=ON

install: all
$(MAKE) -C cscppc_build install
11 changes: 11 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic")

# enable sanitizers
option(SANITIZERS "Compile with ASan and UBSan" OFF)
if (SANITIZERS)
# enable ASan and UBSan
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,undefined")
# recommended for better error traces
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer")
# make UBSan reports fatal
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=all")
endif()

# link to libc statically
option(STATIC_LINKING "Link to libc statically" OFF)
if(STATIC_LINKING)
Expand Down
2 changes: 1 addition & 1 deletion tests/0003-translation-of-args/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ln -fs "$PATH_TO_WRAP/csclng++" csclng++/g++ || exit $?

# run the wrappers through valgrind if available
EXEC_PREFIX=
if valgrind --version; then
if [[ "$HAS_SANITIZERS" -eq 1 ]] && valgrind --version; then
EXEC_PREFIX="valgrind -q --undef-value-errors=no --error-exitcode=7"
printf "%s " "$EXEC_PREFIX" >> tools/g++
printf "%s " "$EXEC_PREFIX" >> tools/gcc
Expand Down
14 changes: 14 additions & 0 deletions tests/testlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ TEST_DST_DIR="$2"
# path to binaries of the wrappers
PATH_TO_WRAP="$3"

# sanitizer build is used
grep -q "SANITIZERS:BOOL=ON" "$PATH_TO_WRAP/../CMakeCache.txt"
HAS_SANITIZERS="$?"

if [[ "$HAS_SANITIZERS" -eq 0 ]]; then
# make UBSan print whole stack traces
export UBSAN_OPTIONS="print_stacktrace=1"

# disable LSan because the fork&exec machinery in 0003-translation-of-args
# test produces incomplete stack traces making the leak suppression of
# these deliberate leaks useless on some distributions (Arch, Ubuntu, ...)
export ASAN_OPTIONS="detect_leaks=0"
fi

# create $TEST_DST_DIR (if it does not exist already)
mkdir -p "$TEST_DST_DIR" || exit $?

Expand Down

0 comments on commit 6de1f7c

Please sign in to comment.