Skip to content

Commit 1640db5

Browse files
authored
Add asan support to cmake/nix (#4194)
1 parent 3758f4b commit 1640db5

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

CMakeLists.txt

+14-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ option(S2N_INTEG_TESTS "Enable the integrationv2 tests" OFF)
3939
option(S2N_FAST_INTEG_TESTS "Enable the integrationv2 with more parallelism, only has effect if S2N_INTEG_TESTS=ON" OFF)
4040
option(S2N_INSTALL_S2NC_S2ND "Install the binaries s2nc and s2nd" OFF)
4141
option(TSAN "Enable ThreadSanitizer to test thread safety" OFF)
42+
option(ASAN "Enable AddressSanitizer to test memory safety" OFF)
4243

4344
# Turn BUILD_TESTING=ON by default
4445
include(CTest)
@@ -218,6 +219,16 @@ if(TSAN)
218219
target_link_options(${PROJECT_NAME} PUBLIC -fsanitize=thread)
219220
endif()
220221

222+
if(ASAN)
223+
target_compile_options(${PROJECT_NAME} PUBLIC -fsanitize=address -DS2N_ADDRESS_SANITIZER=1)
224+
target_link_options(${PROJECT_NAME} PUBLIC -fsanitize=address)
225+
endif()
226+
227+
if(TSAN OR ASAN)
228+
# no-omit-frame-pointer and no-optimize-sibling-calls provide better stack traces
229+
target_compile_options(${PROJECT_NAME} PUBLIC -fno-omit-frame-pointer -fno-optimize-sibling-calls)
230+
endif()
231+
221232
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
222233

223234
if (NOT $ENV{S2N_LIBCRYPTO} MATCHES "awslc")
@@ -503,12 +514,14 @@ if (BUILD_TESTING)
503514
add_library(allocator_overrides SHARED ${TEST_LD_PRELOAD})
504515

505516
set(UNIT_TEST_ENVS S2N_DONT_MLOCK=1)
517+
if (TSAN OR ASAN)
518+
set(UNIT_TEST_ENVS ${UNIT_TEST_ENVS} S2N_ADDRESS_SANITIZER=1)
519+
endif()
506520
if(TSAN)
507521
set(TSAN_SUPPRESSIONS_FILE ${CMAKE_SOURCE_DIR}/tests/.tsan_suppressions)
508522
if(NOT EXISTS ${TSAN_SUPPRESSIONS_FILE})
509523
message(FATAL_ERROR "TSAN suppression file ${TSAN_SUPPRESSIONS_FILE} missing")
510524
endif()
511-
set(UNIT_TEST_ENVS ${UNIT_TEST_ENVS} S2N_ADDRESS_SANITIZER=1)
512525
set(TSAN_OPTIONS suppressions=${TSAN_SUPPRESSIONS_FILE})
513526
if(DEFINED ENV{TSAN_OPTIONS})
514527
set(TSAN_OPTIONS "${TSAN_OPTIONS} $ENV{TSAN_OPTIONS}")

nix/shell.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ banner()
1111

1212

1313
function clean {
14-
banner "Cleanup up ./build"
14+
banner "Cleanup ./build"
1515
rm -rf ./build
1616
}
1717

@@ -23,6 +23,7 @@ function configure {
2323
-DS2N_INSTALL_S2NC_S2ND=ON \
2424
-DS2N_INTEG_NIX=ON \
2525
-DBUILD_SHARED_LIBS=ON \
26+
$S2N_CMAKE_OPTIONS \
2627
-DCMAKE_BUILD_TYPE=RelWithDebInfo
2728
}
2829

0 commit comments

Comments
 (0)