Skip to content

Commit f5c3146

Browse files
committed
cmake: support sanitizers natively
... so that users can easily test sanitized binaries themselves.
1 parent de264ac commit f5c3146

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,12 @@ jobs:
3737
sudo apt install -y help2man libboost-dev libboost-filesystem-dev \
3838
libboost-program-options-dev libboost-python-dev libboost-regex-dev
3939
40-
- name: '[Jammy] Add sanitizers to CXXFLAGS'
41-
if: matrix.version >= 22.04
42-
run: |
43-
# Use ASAN and UBSAN
44-
CXXFLAGS="$CXXFLAGS -fsanitize=address,undefined"
45-
# Recommended for better error traces
46-
CXXFLAGS="$CXXFLAGS -fno-omit-frame-pointer"
47-
# Make UBSAN reports fatal
48-
CXXFLAGS="$CXXFLAGS -fno-sanitize-recover=all"
49-
50-
# Make UBSAN print whole stack traces
51-
UBSAN_OPTIONS="print_stacktrace=1"
52-
53-
# Store the env variables
54-
echo "CXXFLAGS=$CXXFLAGS" >> "$GITHUB_ENV"
55-
echo "UBSAN_OPTIONS=$UBSAN_OPTIONS" >> "$GITHUB_ENV"
56-
5740
- name: Build and check
58-
run: make distcheck
41+
run: |
42+
if [[ "${{ matrix.version }}" == 22.04 ]]; then
43+
# Make UBSAN print whole stack traces
44+
export UBSAN_OPTIONS="print_stacktrace=1"
45+
make distcheck-sanitizers
46+
else
47+
make distcheck
48+
fi

Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2011 - 2020 Red Hat, Inc.
1+
# Copyright (C) 2011 - 2022 Red Hat, Inc.
22
#
33
# This file is part of csdiff.
44
#
@@ -20,13 +20,18 @@ NUM_CPU ?= $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
2020
CMAKE ?= cmake
2121
CTEST ?= ctest -j$(NUM_CPU) --progress
2222

23+
SANITIZERS ?= OFF
24+
2325
CMAKE_BUILD_TYPE ?= RelWithDebInfo
2426

25-
.PHONY: all check clean distclean distcheck fast install version.cc src/version.cc
27+
.PHONY: all check clean sanitizers distclean distcheck distcheck-sanitizers \
28+
fast install version.cc src/version.cc
2629

2730
all: version.cc
2831
mkdir -p csdiff_build
29-
cd csdiff_build && $(CMAKE) -D 'CMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE)' ..
32+
cd csdiff_build && $(CMAKE) \
33+
-D 'CMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE)' \
34+
-D SANITIZERS='$(SANITIZERS)' ..
3035
$(MAKE) -sC csdiff_build -j$(NUM_CPU)
3136

3237
fast: version.cc
@@ -38,13 +43,19 @@ check: all
3843
clean:
3944
if test -e csdiff_build/Makefile; then $(MAKE) clean -C csdiff_build; fi
4045

46+
sanitizers:
47+
$(MAKE) -s all SANITIZERS=ON
48+
4149
distclean:
4250
if test -e .git; then rm -f src/version.cc; fi
4351
rm -rf csdiff_build
4452

4553
distcheck: distclean
4654
$(MAKE) -s check
4755

56+
distcheck-sanitizers:
57+
$(MAKE) -s distcheck SANITIZERS=ON
58+
4859
install: all
4960
$(MAKE) -C csdiff_build install
5061

src/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ set(CMAKE_CXX_EXTENSIONS OFF)
2323
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
2424
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
2525

26+
# enable sanitizers
27+
option(SANITIZERS "Compile with ASan and UBSan" OFF)
28+
if(SANITIZERS)
29+
# enable ASan and UBSan
30+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,undefined")
31+
# recommended for better error traces
32+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer")
33+
# make UBSan reports fatal
34+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=all")
35+
endif()
36+
2637
# find Boost
2738
find_package(Boost REQUIRED
2839
COMPONENTS filesystem

0 commit comments

Comments
 (0)