Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Address Sanitizer in Visual C++

4.88/5 (6 votes)
6 Aug 2020CPOL2 min read 20K   101  
Trying out Address Sanitizer in Visual C++
Use Address Sanitizer in Visual C++ to detect memory address problems

In this blog, I'll demonstrate how to utilize Address Sanitizer (ASan) in Visual C++ to check for memory problems. MSVC team ported the Clang ASan to the Windows platform in 2019 and since it is still at an experimental stage, be sure to expect kinks to be ironed out.

Before using Address Sanitizer in Visual C++, it has to be installed by the Visual Studio Installer. Check the C++ AddressSanitizer (Experimental) checkbox and then click the Modify button.

Image 1

After installing ASan, be sure to add this path to your PATH environment variable so that your executable can find the clang_rt.asan_dynamic-i386.dll.

<small>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\Hostx64\x86</small>

Image 2

Add the environment variable by clicking the New button and paste the path in the new row and click OK.

Image 3

Enable the AddressSanitizer in your C++ project properties.

Image 4

At this time of writing, ASan supports only Release and 32-bit build. x64 support is in the works and is coming soon. Debug build and x64 support are supported on Visual C++ Update 16.7.

Image 5

Image 6

Console Application

Add the two lines of code below to the main function to trigger the ASan detection of memory access violation and console application would terminate to show the line number of the source code that causes this crash. As the console output is verbose, it shall not be shown here.

C++
int* arr = new int[10];
arr[10] = 1;

MFC Application

Attempt to build MFC application with ASan causes these multiple defined symbols linker errors because new and delete operators are defined in MFC and Clang library at the same time. I have no good way to resolve this linkage error. I have filed a bug report, please help upvote it else Microsoft will unilaterally close it without resolving it.

2>uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" 
(??2@YAPAXI@Z) already defined in clang_rt.asan_cxx-i386.lib(asan_new_delete.cc.obj)
2>uafxcw.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" 
(??3@YAXPAX@Z) already defined in clang_rt.asan_cxx-i386.lib(asan_new_delete.cc.obj)
2>uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" 
(??_U@YAPAXI@Z) already defined in clang_rt.asan_cxx-i386.lib(asan_new_delete.cc.obj)
2>uafxcw.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" 
(??_V@YAXPAX@Z) already defined in clang_rt.asan_cxx-i386.lib(asan_new_delete.cc.obj)
2>D:\TextPerfect\Source\Release\SDIScratchPad2.exe : fatal error LNK1169: 
one or more multiply defined symbols found

Win32 OpenGL Application

Lastly, I tried ASan on a Win32 OpenGL application. To make sure ASan works as intended, I added the two offending lines. After I made sure ASan detection works, I remove those two lines.

C++
int* arr = new int[10];
arr[10] = 1;

To see the ASan output in a GUI application which does not have a console, you have to use Visual Studio to debug your Release build application. The ASan output will be shown in the output pane of Visual Studio. But you will notice no line number of the offending line is revealed. To fix that, let's add debug information to your Release build.

Image 7

This step is automatically done for the earlier console application. As long as the OpenGL application runs to completion without crashing, it means ASan is not triggered on memory access violation.

Reference

History

  • 7th Aug 2020: x64 ASAN support is available on Visual Studio 2019 Update 16.7. Sample code is uploaded.
  • 25th May 2020: First release on x86 ASAN

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)