Click here to Skip to main content
15,886,798 members
Articles / Programming Languages / C++

Address Sanitizer in Visual C++

Rate me:
Please Sign up or sign in to vote.
4.88/5 (6 votes)
6 Aug 2020CPOL2 min read 18.6K   93   5   11
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)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Jan Heckman10-Aug-20 8:20
professionalJan Heckman10-Aug-20 8:20 
GeneralRe: My vote of 4 Pin
Shao Voon Wong10-Aug-20 15:40
mvaShao Voon Wong10-Aug-20 15:40 
GeneralRe: My vote of 4 Pin
Shao Voon Wong11-Aug-20 23:45
mvaShao Voon Wong11-Aug-20 23:45 
QuestionVerifying the option Pin
Gisle Vanem1-Jun-20 2:58
Gisle Vanem1-Jun-20 2:58 
AnswerRe: Verifying the option Pin
Shao Voon Wong25-Jul-20 16:21
mvaShao Voon Wong25-Jul-20 16:21 
GeneralRe: Verifying the option Pin
Gisle Vanem25-Jul-20 23:20
Gisle Vanem25-Jul-20 23:20 
SuggestionScreen captures Pin
Razmar31-May-20 22:13
Razmar31-May-20 22:13 
Why not putting some screen captures with the detection itself?
GeneralRe: Screen captures Pin
Shao Voon Wong25-Jul-20 16:27
mvaShao Voon Wong25-Jul-20 16:27 
GeneralRe: Screen captures Pin
Shao Voon Wong25-Jul-20 17:29
mvaShao Voon Wong25-Jul-20 17:29 
QuestionMFC application Pin
ferdo28-May-20 9:19
ferdo28-May-20 9:19 
AnswerRe: MFC application Pin
Shao Voon Wong25-Jul-20 16:25
mvaShao Voon Wong25-Jul-20 16:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.