linux配置器项目,配置 Linux 项目以使用地址擦除器

配置 Linux 项目以使用地址擦除器Configure Linux projects to use Address Sanitizer

10/7/2020

本文内容

在 Visual Studio 2019 版本 16.1 中,AddressSanitizer (ASan) 支持已集成到 Linux 项目。In Visual Studio 2019 version 16.1, AddressSanitizer (ASan) support is integrated into Linux projects. 可以为基于 MSBuild 的 Linux 项目和 CMake 项目启用 ASan。You can enable ASan for both MSBuild-based Linux projects and CMake projects. 它可在远程 Linux 系统和适用于 Linux 的 Windows 子系统 (WSL) 上运行。It works on remote Linux systems and on Windows Subsystem for Linux (WSL).

关于 ASanAbout ASan

ASan 是 C/C++ 的运行时内存错误检测器,可捕获以下错误:ASan is a runtime memory error detector for C/C++ that catches the following errors:

释放后使用(无关联的指针引用)Use after free (dangling pointer reference)

堆缓冲区溢出Heap buffer overflow

堆栈缓冲区溢出Stack buffer overflow

返回后使用Use after return

审视后使用Use after scope

初始化顺序 bugInitialization order bugs

当 ASan 检测到错误时,它会立即停止执行。When ASan detects an error, it stops execution immediately. 如果在调试程序中运行启用了 ASan 的程序,系统会显示一条消息,其中描述了错误类型、内存地址以及源文件中发生错误的位置:If you run an ASan-enabled program in the debugger, you see a message that describes the type of error, the memory address, and the location in the source file where the error occurred:

b4c92edb56eed8c58bcfdc2e2457dc42.png

还可以在输出窗口的“调试”窗格中查看完整的 ASan 输出(包括分配/取消分配损坏内存的位置)。You can also view the full ASan output (including where the corrupted memory was allocated/deallocated) in the Debug pane of the output window.

为基于 MSBuild 的 Linux 项目启用 ASanEnable ASan for MSBuild-based Linux projects

备注

从 Visual Studio 2019 版本 16.4 开始,可通过“项目属性” > “配置属性” > “C/C++” > “启用地址擦除”来启用适用于 Linux 项目的 AddressSanitizer。Starting in Visual Studio 2019 version 16.4, AddressSanitizer for Linux projects is enabled via Project properties > Configuration Properties > C/C++ > Enable Address Sanitizer.

要为基于 MSBuild 的 Linux 项目启用 ASan,请右键单击“解决方案资源管理器”中的项目,然后选择“属性” 。To enable ASan for MSBuild-based Linux projects, right-click on the project in Solution Explorer and select Properties. 接下来,导航到“配置属性” > “C/C++” > “擦除器” 。Next, navigate to Configuration Properties > C/C++ > Sanitizers. ASan 通过编译器和链接器标志启用,并且需要重新编译项目才能正常运行。ASan is enabled via compiler and linker flags, and requires your project to be recompiled to work.

8d93418f00b13bc9d608fd6afd87c145.png

可通过导航到“配置属性” > “调试” > “AddressSanitizer 运行时标志”,传递可选 ASan 运行时标志 。You can pass optional ASan runtime flags by navigating to Configuration Properties > Debugging > AddressSanitizer Runtime Flags. 单击向下箭头,添加或删除标志。Click the down-arrow to add or remove flags.

cac2ced2caa652fbb3b9a66b35641e22.png

为 Visual Studio CMake 项目启用 ASanEnable ASan for Visual Studio CMake projects

备注

若要使用 CMake 预设进行生成,请先在 CMakeLists.txt 文件中启用 ASan。To build with CMake Presets, first enable ASan in your CMakeLists.txt file.

要为 CMake 启用 ASan,请右键单击“解决方案资源管理器”中的 CMakeLists.txt 文件,然后选择“项目的 CMake 设置” 。To enable ASan for CMake, right-click on the CMakeLists.txt file in Solution Explorer and choose CMake Settings for Project.

确保在对话框的左侧窗格中选中了 Linux 配置(如“Linux 调试” ):Make sure you have a Linux configuration (for example, Linux-Debug) selected in the left pane of the dialog:

ee9ca604eaa8197b49164b8e7c323a03.png

ASan 选项位于“常规”下 。The ASan options are under General. 以“标志=值”格式输入 ASan 运行时标志,并以空格分隔。Enter the ASan runtime flags in the format "flag=value", separated by spaces. UI 错误地建议使用分号。The UI incorrectly suggests using semi-colons. 使用空格或冒号分隔标志。Use spaces or colons to separate flags.

4e2a35438caca81e1d78f5738c4a1520.png

安装 ASan 调试符号Install the ASan debug symbols

要启用 ASan 诊断,必须在远程 Linux 计算机或 WSL 安装上安装其调试符号 (libasan-dbg)。To enable the ASan diagnostics, you must install its debug symbols (libasan-dbg) on your remote Linux machine or WSL installation. 加载的 libasan-dbg 版本取决于 Linux 计算机上安装的 GCC 版本:The version of libasan-dbg that you load depends on the version of GCC installed on your Linux machine:

ASan 版本ASan version

GCC 版本GCC version

libasan0libasan0

gcc-4.8gcc-4.8

libasan2libasan2

gcc-5gcc-5

libasan3libasan3

gcc-6gcc-6

libasan4libasan4

gcc-7gcc-7

libasan5libasan5

gcc-8gcc-8

可使用以下命令确定拥有的 GCC 版本:You can determine which version of GCC you have by using this command:

gcc --version

要查看所需的 libasan-dbg 版本,请运行你的程序,然后查看“输出”窗口的“调试”窗格 。To view the version of libasan-dbg you need, run your program, and then look at the Debug pane of the Output window. 加载的 ASan 版本对应于 Linux 计算机上所需的 libasan-dbg 版本。The version of ASan that's loaded corresponds to the version of libasan-dbg needed on your Linux machine. 可以使用 Ctrl + F 在窗口中搜索“libasan” 。You can use Ctrl + F to search for "libasan" in the window. 例如,如果具有 libasan4,则会看到如下所示的行:If you have libasan4, for example, you see a line like this:

Loaded '/usr/lib/x86_64-linux-gnu/libasan.so.4'. Symbols loaded.

可使用以下命令,在使用 apt 的 Linux 发行版上安装 ASan 调试位。You can install the ASan debug bits on Linux distros that use apt with the following command. 此命令安装版本 4:This command installs version 4:

sudo apt-get install libasan4-dbg

有关在 Ubuntu 上安装调试符号包的完整说明,请参阅调试符号包。Full instructions for installing debug symbol packages on Ubuntu can be found at Debug symbol packages.

如果启用了 ASan,Visual Studio 会在“输出”窗口的“调试”窗格顶部提示安装 ASan 调试符号 。If ASan is enabled, Visual Studio prompts you at the top of the Debug pane of the Output window to install the ASan debug symbols.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值