C++内存泄漏、数组越界……检测工具:Sanitizers

编程入门 行业动态 更新时间:2024-10-08 22:16:31

C++内存泄漏、<a href=https://www.elefans.com/category/jswz/34/1771288.html style=数组越界……检测工具:Sanitizers"/>

C++内存泄漏、数组越界……检测工具:Sanitizers

g++ gcc自带的sanitizers可用于检测内存泄漏、数组越界、线程竞态条件、未初始化等问题,支持Linux(win暂时只支持Aaddress sanitizers)。

用法:

CMAKE中的用法:

Google sanitizers

Sanitizers are open-source tools for dynamic code analysis designed by Google. CLion integrates with the following Sanitizers:

  • AddressSanitizer (ASan)
  • LeakSanitizer (LSan)
  • ThreadSanitizer (TSan)
  • UndefinedBehaviorSanitizer (UBSsan)
  • MemorySanitizer (MSan)

Useful links: Official documentation, Sanitizers in Clang, Sanitizers among Program Instrumentation Options in GCC.

Sanitizers are implemented in Clang starting 3.1 and GCC starting 4.8. All the sanitizers are available on Linux x86_64 machines and also on Windows 10 with clang-cl. For macOS, only AddressSanitizer and UndefinedBehaviorSanitizer are supported.

As Sanitizers are based on compiler instrumentation, you need to rebuild your project in order to start using these tools.

Configure Sanitizers

Specify compiler flags

Adjust the following template line and add it to your CMakeLists.txt:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=[sanitizer_name] [additional_options] [-g] [-OX]")

note
Use CMAKE_C_FLAGS instead of CMAKE_CXX_FLAGS for C projects.

For [sanitizer_name] use one of the following:

  • address for AddressSanitizer
  • leak for LeakSanitizer
  • thread for ThreadSanitizer
  • undefined for UndefinedBehaviorSanitizer (other options are also available, see the UBSan section)
  • memory for MemorySanitizer

[Additional_flags] are other compilation flags, such as -fno-omit-frame-pointer, fsanitize-recover/fno-sanitize-recover, -fsanitize-blacklist, etc.

Use [-g] to have file names and line numbers included in warning messages.

Add optimization level [-OX] to get reasonable performance (see recommendations in the particular Sanitizer documentation).

详细:.html

更多推荐

C++内存泄漏、数组越界……检测工具:Sanitizers

本文发布于:2024-02-13 20:24:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   内存   检测工具   Sanitizers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!