Skip to content

Undefined Behavior Sanitizer

lbuild module: modm:platform:ubsan

Instruments all code compiled by modm with the compiler's Undefined Behavior Sanitizer to find bugs like invalid shifts, divisions by zero, out-of-bounds array accesses and invalid bool or enum values at runtime.

<module>modm:platform:ubsan</module>
<option name="modm:platform:ubsan:mode">report</option>

The arm-none-eabi toolchain does not ship the libubsan runtime, so there are two modes that work without it:

  • trap: All checks except null and alignment are enabled, since address zero is valid memory on some devices and Thumb function pointers are odd. The first failure executes a faulting instruction, which ends up in the HardFault handler and by default resets the device. Use a debugger or the modm:platform:fault module to find the location.
  • report: A reduced set of checks calls a small runtime, which logs each failing source location once and then continues:
UBSAN: src/main.cpp:42:17 shift_out_of_bounds

This is a debugging tool

The instrumentation increases the code size considerably and changes the timing. The report mode additionally keeps its check descriptions in RAM, and needs MODM_LOG_ERROR to be connected to a logger.

Precompiled libraries, like the C library, are not instrumented. Since modm compiles with -fwrapv, signed integer overflow is defined and is not reported.

This module is only available for nrf, rp, sam, stm32.

Options

mode

Reaction to undefined behavior

  • trap: Enables all checks except null and alignment and executes a faulting instruction on the first failure. Needs no runtime and no additional RAM.
  • report: Enables a reduced set of checks, logs every failing source location once to MODM_LOG_ERROR and continues. Needs a lot more flash and RAM.

Default: trap
Inputs: [report, trap]
Input Dependency: report -> modm:debug

Dependencies

modm:platform:ubsan modm_platform_ubsan modm: platform: ubsan modm_debug modm: debug modm_platform_ubsan->modm_debug

Limited availability: Check with 'lbuild discover' if this module is available for your target!