File tree Expand file tree Collapse file tree 6 files changed +50
-5
lines changed Expand file tree Collapse file tree 6 files changed +50
-5
lines changed Original file line number Diff line number Diff 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>>)
345345endif ()
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
348360zephyr_compile_options($<TARGET_PROPERTY:compiler,no_common>)
349361
Original file line number Diff line number Diff line change @@ -328,6 +328,7 @@ config PRIVILEGED_STACK_SIZE
328328
329329config 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
Original file line number Diff line number Diff line change @@ -126,6 +126,7 @@ config PRIVILEGED_STACK_SIZE
126126 default 4096
127127
128128config KOBJECT_TEXT_AREA
129+ default 1024 if UBSAN
129130 default 512 if TEST
130131
131132config WAIT_AT_RESET_VECTOR
Original file line number Diff line number Diff line change @@ -197,6 +197,10 @@ if(NOT CONFIG_NO_OPTIMIZATIONS)
197197 set_compiler_property(PROPERTY security_fortify_run_time _FORTIFY_SOURCE=2)
198198endif ()
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
201205check_set_compiler_property(APPEND PROPERTY hosted -fno-freestanding)
202206
Original file line number Diff line number Diff line change @@ -24,6 +24,10 @@ check_set_linker_property(TARGET linker PROPERTY orphan_error
2424
2525check_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
Original file line number Diff line number Diff line change @@ -67,12 +67,35 @@ config ASAN_NOP_DLCLOSE
6767
6868config 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
77100config MSAN
78101 bool "Build with memory sanitizer"
You can’t perform that action at this time.
0 commit comments