解决MSVC上的清理程序功能

Fra*_*ter 10 c++ qt visual-studio address-sanitizer

来自Linux/gcc/clang我发现自己在Windows/Visual Studio上的工作越来越多.

我真正缺少的是地址清理器(边界检查,泄漏,免费后使用,......).我做了一些研究,并尝试了一些事情,但没有找到完整(功能明智)和可靠的替代品.我以Dr.Memory为例,但了解到它对基于Qt的程序不起作用(至少在Windows 10上不行).

那么如何在Windows/MSVC上获得类似地址清理程序的功能呢?

Fou*_*Two 14

来自clang的至少ASan和Ubsan应该在Windows上运行,但有一些限制.这些可以与msvc工具链一起使用,使用clang-cl作为cl.exe的替代品 - 谷歌似乎正在研究这个,mozilla 也是如此.

我所知道的问题(并且保证我自己使用它直到现在):

  • 与所需库链接不是自动的.它们有两个版本,具体取决于CRT在应用程序中的链接方式(/ MT表示静态CRT,/ MD表示动态CRT,后者通常用于Qt).要查找所需的链接器参数,请打开Visual Studio命令提示符,将clang bin文件夹添加到路径中,然后使用带有clang-cl的详细选项编译一个简单的main.cpp(空主函数),如下所示: clang-cl -v /MD -fsanitize=address main.cpp 所需的link.exe命令是在详细输出的末尾,从那里提取所需的libs以进行链接.

  • Windows上仅支持发布版本

  • 不支持Windows上的异常(请参阅问题)

  • 在Windows端口似乎没有太多进一步的工作,wiki例如非常过时(2015年的最后一次更改),所以我怀疑很多人正在高效地使用它.因此,在线获取其他用户的帮助可能会非常困难......

在Windows上谈论其他替代方案,有:

Linux上的清理程序和Valgrind IMO比这些工具更先进和/或具有更好的性能,因此在Linux上保持应用程序构建似乎是最好的想法,至少在使用像Qt这样的跨平台工具包时(正如你提到的那样) ).

  • 这个答案的好东西。还应注意,MSVC的运行时库内置了相当不错的堆内存跟踪功能,但您必须将它们打开。https://docs.microsoft.com/zh-cn/visualstudio/debugger/crt-debug-heap-details (2认同)

phu*_*clv 6

Microsoft has integrated the Address Sanitizer into Visual Studio 2019 version 16.1 Preview 3 and up. Unfortunately currently only the Linux build is supported. But at least you can still use your favorite IDE and debug apps in WSL

Update:

Address Sanitizer for Windows projects is also available since Visual Studio 2019 version 16.4

We are pleased to announce AddressSanitizer (ASan) support for the MSVC toolset. ASan is a fast memory error detector that can find runtime memory issues such as use-after-free and perform out of bounds checks. Support for sanitizers has been one of our more popular suggestions on Developer Community, and we can now say that we have an experience for ASan on Windows, in addition to our existing support for Linux projects.

AddressSanitizer (ASan) for Windows with MSVC


In Visual Studio 2019 version 16.1 Preview 3 we have integrated AddressSanitizer (ASan) into Visual Studio for Linux projects. 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
  • Initialization order bugs

ASAN错误示例

ASAN选件

AddressSanitizer (ASan) for the Linux Workload in Visual Studio 2019


Note that MSVC itself already have various tools for debugging memory issues like CRT Debug Heap as mentioned above by Adrian McCarthy or Control Flow Guard

There are many tools that try to make your code secure from outside the box: Valgrind and address/thread sanitizers are popular examples. And there are many of these tools on Windows as well, both from Microsoft and other companies. But MSVC features powerful technologies inside the compiler that integrate security with your code. For example, Control Flow Guard, is highly-optimized security feature that combats many memory corruption vulnerabilities. We can’t talk openly our current security research but we’re always working to make your code (and ours!) safe from increasingly sophisticated attackers.

https://devblogs.microsoft.com/cppblog/msvc-the-best-choice-for-windows/

See also

  • @MarekR 这在这里无关紧要。问题是关于 MSVC。无论如何,自 2017 年以来的 MSVC 是一个很棒的 IDE 与许多其他人相比 (7认同)
  • @MikeMB 从 16.7.7 开始,地址清理程序适用于 x64 和 x86。 (2认同)