Skip to content

Commit e4630c5

Browse files
keith-packardbjarki-andreasen
authored andcommitted
[nrf fromtree] cmake: Enable undefined behavior sanitizer on all targets
GCC and Clang support the undefined behavior sanitizer in any configuration, the only restriction is that if you want to get nice messages printed, then you need the ubsan library routines which are only present for posix architecture or when using picolibc. This patch adds three new compiler properties: * sanitizer_undefined. Enables the undefined behavior sanitizer. * sanitizer_undefined_library. Calls ubsan library routines on fault. * sanitizer_undefined_trap. Invokes __builtin_trap() on fault. Overhead for using the trapping sanitizer is fairly low and should be considered for use in CI once all of the undefined behavior faults in Zephyr are fixed. Signed-off-by: Keith Packard <[email protected]> (cherry picked from commit 2d64237)
1 parent 924f770 commit e4630c5

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,18 @@ if (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_IO_FLOAT)
344344
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_printf_return_value>>)
345345
endif()
346346

347+
if(CONFIG_UBSAN)
348+
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,sanitizer_undefined>>)
349+
zephyr_link_libraries($<TARGET_PROPERTY:linker,sanitizer_undefined>)
350+
if(CONFIG_UBSAN_LIBRARY)
351+
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,sanitizer_undefined_library>>)
352+
zephyr_link_libraries($<TARGET_PROPERTY:linker,sanitizer_undefined_library>)
353+
elseif(CONFIG_UBSAN_TRAP)
354+
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,sanitizer_undefined_trap>>)
355+
zephyr_link_libraries($<TARGET_PROPERTY:linker,sanitizer_undefined_trap>)
356+
endif()
357+
endif()
358+
347359
# @Intent: Set compiler specific flag for tentative definitions, no-common
348360
zephyr_compile_options($<TARGET_PROPERTY:compiler,no_common>)
349361

arch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ config PRIVILEGED_STACK_SIZE
328328

329329
config KOBJECT_TEXT_AREA
330330
int "Size of kobject text area"
331+
default 1024 if UBSAN
331332
default 512 if COVERAGE_GCOV
332333
default 512 if NO_OPTIMIZATIONS
333334
default 512 if STACK_CANARIES && RISCV

arch/arm64/core/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ config PRIVILEGED_STACK_SIZE
126126
default 4096
127127

128128
config KOBJECT_TEXT_AREA
129+
default 1024 if UBSAN
129130
default 512 if TEST
130131

131132
config WAIT_AT_RESET_VECTOR

cmake/compiler/gcc/compiler_flags.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ if(NOT CONFIG_NO_OPTIMIZATIONS)
197197
set_compiler_property(PROPERTY security_fortify_run_time _FORTIFY_SOURCE=2)
198198
endif()
199199

200+
check_set_compiler_property(PROPERTY sanitizer_undefined -fsanitize=undefined)
201+
check_set_compiler_property(PROPERTY sanitizer_undefined_trap -fsanitize-undefined-trap-on-error)
202+
check_set_compiler_property(PROPERTY sanitizer_undefined_library)
203+
200204
# gcc flag for a hosted (no-freestanding) application
201205
check_set_compiler_property(APPEND PROPERTY hosted -fno-freestanding)
202206

cmake/linker/ld/linker_flags.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ check_set_linker_property(TARGET linker PROPERTY orphan_error
2424

2525
check_set_linker_property(TARGET linker PROPERTY memusage "${LINKERFLAGPREFIX},--print-memory-usage")
2626

27+
check_set_linker_property(TARGET linker PROPERTY sanitizer_undefined -fsanitize=undefined)
28+
check_set_linker_property(TARGET linker PROPERTY sanitizer_undefined_trap -fsanitize-undefined-trap-on-error)
29+
check_set_linker_property(TARGET linker PROPERTY sanitizer_undefined_library)
30+
2731
# -no-pie is not supported until binutils 2.37.
2832
# If -no-pie is passed to old binutils <= 2.36, it is parsed
2933
# as separate arguments -n and -o, which results in output file

subsys/debug/Kconfig

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,35 @@ config ASAN_NOP_DLCLOSE
6767

6868
config UBSAN
6969
bool "Build with undefined behavior sanitizer"
70-
depends on ARCH_POSIX
7170
help
72-
Builds Zephyr with Undefined Behavior Sanitizer enabled.
73-
This is currently only supported by boards based on the posix
74-
architecture, and requires a recent-ish compiler with the
75-
``-fsanitize=undefined`` command line option.
71+
Builds Zephyr with Undefined Behavior Sanitizer enabled. This
72+
requires a recent-ish compiler with the ``-fsanitize=undefined``
73+
command line option.
74+
75+
choice UBSAN_MODE
76+
prompt "Undefined behavior sanitizer mode"
77+
depends on UBSAN
78+
default UBSAN_LIBRARY
79+
80+
config UBSAN_LIBRARY
81+
bool "Call ubsan routines"
82+
depends on ARCH_POSIX || PICOLIBC
83+
help
84+
Call ubsan library routines when undefined behavior is detected
85+
at runtime. This provides information about the faulting
86+
condition along with the source filename, line number, types and
87+
values involved. This is currently only supported by boards
88+
based on the posix architecture or when building with picolibc.
89+
90+
config UBSAN_TRAP
91+
bool "Call __builtin_trap"
92+
help
93+
When undefined behavior is detected, invoke __builtin_trap to
94+
cause an exception to be raised. This can be used on any target,
95+
but the lack of information makes figuring out the triggering
96+
code difficult.
97+
98+
endchoice
7699

77100
config MSAN
78101
bool "Build with memory sanitizer"

0 commit comments

Comments
 (0)