Skip to content

Commit a33c43b

Browse files
committed
build: fix sanitizers
The recommended way to enable sanitizers nowadays is to add `-fsanitize=*` as a compiler and linking flags. Also fix some warnings and remove some targets from "all" to speed up builds. Bumping macosx version to 11 since that seems to be necessary for asan to work. Add CI check to verify that address santizier is turned on for unit tests
1 parent 818835a commit a33c43b

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

.github/workflows/ci-common.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ jobs:
9797
- name: Build unit-tests
9898
run: make -j$(($(nproc) + 1)) unit-test
9999

100+
- name: Check that address sanitizer is enabled
101+
run: nm build-build/bin/test_cleanup | grep __asan_version_mismatch_check_v8
102+
100103
- name: run unit-tests
101104
run: make run-unit-tests
102105

CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ endif()
4646

4747
# This is ignored on platforms other than darwin. By default rust compiles for
4848
# 10.7 which doesn't link for us.
49-
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version")
49+
set(CMAKE_OSX_DEPLOYMENT_TARGET "11" CACHE STRING "Minimum OS X deployment version")
5050

5151
project(bitbox02 C)
5252

@@ -65,6 +65,7 @@ option(COVERAGE "Compile with test coverage flags." OFF)
6565
option(SANITIZE_ADDRESS "Compile with asan." OFF)
6666
option(SANITIZE_UNDEFINED "Compile with ubsan." OFF)
6767
option(CMAKE_VERBOSE_MAKEFILE "Verbose build." OFF)
68+
6869
# Generate compile_command.json (for tidy and other tools)
6970
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
7071

@@ -343,6 +344,12 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=600")
343344
string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE)
344345
add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}")
345346

347+
if(SANITIZE_ADDRESS)
348+
string(APPEND CMAKE_C_FLAGS " -fsanitize=address")
349+
endif()
350+
if(SANITIZE_UNDEFINED)
351+
string(APPEND CMAKE_C_FLAGS " -fsanitize=undefined")
352+
endif()
346353

347354
# protoc is used to generate API messages
348355
find_program(PROTOC protoc)

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717
UNAME_S := $(shell uname -s)
1818

1919
.DEFAULT_GOAL := firmware
20-
# asan/ubsan is not supported on darwin, default to off
21-
ifeq ($(UNAME_S),Darwin)
22-
SANITIZE ?= OFF
23-
else
24-
SANITIZE ?= ON
25-
endif
20+
SANITIZE ?= ON
2621

2722
bootstrap:
2823
git submodule update --init --recursive

external/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ aux_source_directory(optiga-trust-m/src/comms/ifx_i2c SRC_COMMS_IFX_I2C_FILES)
238238
aux_source_directory(optiga-trust-m/src/crypt SRC_CRYPT_FILES)
239239
aux_source_directory(optiga-trust-m/src/util SRC_UTIL_FILES)
240240
set(PAL_FILES optiga-trust-m/extras/pal/pal_crypt_mbedtls.c)
241-
add_library(optiga
241+
add_library(optiga EXCLUDE_FROM_ALL
242242
${SRC_CMD_FILES}
243243
${SRC_COMMON_FILES}
244244
${SRC_COMMS_IFX_I2C_FILES}

test/simulator/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ add_executable(simulator EXCLUDE_FROM_ALL simulator.c)
1616

1717
# asan must be first library in linking order
1818
target_link_libraries(simulator PRIVATE
19-
$<$<BOOL:${SANITIZE_ADDRESS}>:asan>
19+
$<$<BOOL:${SANITIZE_ADDRESS}>:-fsanitize=address>
2020
$<$<BOOL:${SANITIZE_UNDEFINED}>:-fsanitize=undefined>
2121
$<$<NOT:$<PLATFORM_ID:Darwin>>:-Wl,--start-group>
2222
c-unit-tests_rust_c

test/unit-test/CMakeLists.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ endif()
3131

3232
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter -Wno-missing-prototypes -Wno-missing-declarations -Wno-implicit-function-declaration -Wno-bad-function-cast")
3333

34-
add_library(mocks STATIC
34+
add_library(mocks STATIC EXCLUDE_FROM_ALL
3535
framework/src/mock_gestures.c
3636
framework/src/mock_screen_stack.c
3737
framework/src/mock_memory.c
@@ -89,7 +89,7 @@ else()
8989
add_executable(${EXE} test_${TEST_NAME}.c)
9090
# asan must be first library in linking order
9191
target_link_libraries(${EXE} PRIVATE
92-
$<$<BOOL:${SANITIZE_ADDRESS}>:asan>
92+
$<$<BOOL:${SANITIZE_ADDRESS}>:-fsanitize=address>
9393
$<$<BOOL:${SANITIZE_UNDEFINED}>:-fsanitize=undefined>
9494
-Wl,--start-group
9595
c-unit-tests_rust_c
@@ -114,11 +114,13 @@ add_library(u2f-util
114114
u2f/u2f_util_t.c
115115
)
116116
target_include_directories(u2f-util
117-
PUBLIC
118-
${CMAKE_CURRENT_SOURCE_DIR}
119117
SYSTEM PUBLIC
120118
${HIDAPI_INCLUDE_DIRS}
121119
)
120+
target_include_directories(u2f-util
121+
PUBLIC
122+
${CMAKE_CURRENT_SOURCE_DIR}
123+
)
122124

123125
# NOTE: we only depend on include directories and definitions from c-unit-tests_rust_c
124126
target_link_libraries(u2f-util PUBLIC ${HIDAPI_LDFLAGS} c-unit-tests_rust_c)
@@ -136,7 +138,7 @@ foreach(TEST_NAME ${U2F_TESTS})
136138
# This tests link to our code
137139
add_executable(${EXE} test_${TEST_NAME}.c framework/src/mock_hidapi.c)
138140
target_link_libraries(${EXE} PRIVATE
139-
$<$<BOOL:${SANITIZE_ADDRESS}>:asan>
141+
$<$<BOOL:${SANITIZE_ADDRESS}>:-fsanitize=address>
140142
$<$<BOOL:${SANITIZE_UNDEFINED}>:-fsanitize=undefined>
141143
$<$<NOT:$<PLATFORM_ID:Darwin>>:-Wl,--start-group>
142144
c-unit-tests_rust_c
@@ -152,7 +154,7 @@ foreach(TEST_NAME ${U2F_TESTS})
152154
add_executable(${EXE} test_${TEST_NAME}.c)
153155
# asan must be first library in linking order
154156
target_link_libraries(${EXE} PRIVATE
155-
$<$<BOOL:${SANITIZE_ADDRESS}>:asan>
157+
$<$<BOOL:${SANITIZE_ADDRESS}>:-fsanitize=address>
156158
$<$<BOOL:${SANITIZE_UNDEFINED}>:-fsanitize=undefined>
157159
u2f-util
158160
)

0 commit comments

Comments
 (0)