目录

UBSan: Detecting Undefined Behaviors

Undefined behavior in code might seem harmless at first, but it can lead to program crashes or errors as code complexity grows, making the root cause difficult to pinpoint. Undefined Behavior Sanitizer (UBSan) is a tool designed to identify such behavior and assist in resolving runtime errors stemming from undefined behavior.

Below are common instances of undefined behavior:

This feature is supported since DevEco Studio 5.1.0 Release.

Constraints

ASan, TSan, UBSan, and HWASan cannot be enabled simultaneously; only one of these sanitizers can be enabled at a time.

Enabling UBSan

You can enable UBSan in either of the following ways.

[h2]Method 1

Choose Run > Edit Configurations > Diagnostics and select Undefined Behavior Sanitizer.

[h2]Method 2

In the module where UBSan needs to be enabled, add build parameters to enable UBSan instrumentation. Specifically, in the build-profile.json5 file of the module, add the following:
"arguments": "-DOHOS_ENABLE_UBSAN=ON"

Using UBSan

  1. Run or debug the app.
  2. When UBSan detects undefined behavior, it displays log information. You can click the provided link in this information to directly navigate to the code causing the undefined behavior. For details about the error types detectable by UBSan, see Detectable Error Types.

    This direct link to the source code is available regardless of whether the build mode is debug or release.