目录

ASan: Detecting Memory Errors

To achieve optimal performance of C/C++, the compiler and OS (Windows, Linux, and macOS) runtime framework do not perform security detection on memory operations. This is where Address Sanitizer (ASan) comes into the picture. ASan is integrated in DevEco Studio to detect C/C++ out-of-bounds addresses, and display error stack details and problematic code lines in FaultLog. For details about the ASan detection principles, see ASan Detection Principles.

Constraints

Enabling ASan

You can enable ASan in either of the following ways.

[h2]Method 1

  1. Choose Run > Edit Configurations > Diagnostics and select Address Sanitizer.

  2. If a local library is referenced, set arguments to -DOHOS_ENABLE_ASAN=ON in the build-profile.json5 file of the library module. This allows the .so file to be compiled in ASan mode.

[h2]Method 2

  1. In the AppScope/app.json5 file of the project, add the ASan configuration.

     "asanEnabled": true

  2. Add build parameters to enable ASan instrumentation for the module.

    In the module where ASan needs to be enabled, add build parameters to enable ASan instrumentation. Specifically, in the build-profile.json5 file of the module, add the following:

    "arguments": "-DOHOS_ENABLE_ASAN=ON"

    Leaving this parameter unspecified will not cause errors. However, in this case, aside from a few memory errors that include malloc and free functions, ASan will not be able to detect other memory errors that require instrumentation for detection.

(Optional) Configuring Parameters

Configure ASAN_OPTIONS to define behavior of ASan at runtime, including setting the detection level, output format, detail level of memory error reports, and more. ASAN_OPTIONS can be configured in the app.json5 file or under Run/Debug Configurations. The configuration in app.json5 takes precedence. If both methods are used, the settings in app.json5 prevail. For details about how to configure ASAN_OPTIONS and common parameters, see Configuring Parameters.

Using ASan

  1. Run or debug the app.
  2. When a memory error occurs, the ASan log information will be displayed. You can click the link in the information to go to the code that causes the memory error. For details about the description of each field in the log, see ASan Log Specifications; for details about detectable error types, see Detectable Error Types.