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

Erika Sweet

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

You can enable ASan for MSBuild-based Linux projects and CMake projects that target a remote Linux system or WSL (Windows Subsystem for Linux). If you are just getting started with cross-platform development, I recommend following this walk-through to get started with Visual Studio’s native support for WSL.

ASan detects errors that are encountered during program execution and stops execution on the first detected error. When you run a program that has ASan enabled under the debugger, you will see the following error message (detailing the type of error and location) at the line where the error occurred:

AddressSanitizer error

You can also view the full ASan output (including where the corrupted memory was allocated/deallocated) in the Debug pane of the output window.

Getting started with ASan in Visual Studio

In order to use ASan in Visual Studio, you need to install the debug symbols for ASan (libasan-dbg) on your remote Linux machine or WSL installation. The version of libasan-dbg that you load depends on the version of GCC you have installed on your Linux machine:

ASan version GCC version
libasan0 gcc-4.8
libasan2 gcc-5
libasan3 gcc-6
libasan4 gcc-7
libasan5 gcc-8

 

You can determine the version of GCC you have on your Linux machine or WSL installation with the following command:

gcc –version

You can also view the version of libasan-dbg you will need by looking at the Debug pane of the output window. The version of ASan that is loaded corresponds to the version of libasan-dbg you will need on your Linux machine. You can search for the following line (ctrl + F) in the Debug pane of the output window:

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

In this example, my Linux machine (Ubuntu 18.04) is using libasan4.

You can install the ASan debug bits on Linux distros that use apt with the following command (this command installs version 4):

sudo apt-get install libasan4-dbg

If you have enabled ASan in Visual Studio, then we will prompt you to install the debug symbols for ASan at the top of the Debug pane of the output window.

Enable ASan for MSBuild-based Linux projects

You can enable ASan for MSBuild-based Linux projects in the project’s Property Pages. Right-click on the project in the Solution Explorer and select “Properties” to open the project’s Property Pages, then navigate to Configuration Properties > C/C++ > Sanitizers. ASan is enabled via compiler and linker flags and requires recompilation in order to work.

Enable ASan for MSBuild-based projects via the project's Property Pages

Note: Starting in Visual Studio 2019 version 16.4, AddressSanitizer for Linux projects is enabled via Configuration Properties > C/C++ > Enable Address Sanitizer. 

You can also pass optional ASan runtime flags by navigating to Configuration Properties > Debugging > AddressSanitizer Runtime Flags.

Enable ASan for Visual Studio CMake projects

You can enable ASan for CMake configurations targeting a remote Linux machine or WSL in the CMake Settings Editor. In the “General” section of the editor you will see the following two properties to enable ASan and pass optional runtime flags: Enable ASan for CMake projects via the CMake Settings Editor

Again, ASan is enabled via compiler and linker flags and requires recompilation in order to work.

Give us your feedback!

If you have feedback on ASan for the Linux Workload or anything regarding our Linux support in Visual Studio, we would love to hear from you. We can be reached via the comments below or via email (visualcpp@microsoft.com). If you encounter other problems with Visual Studio or MSVC or have a suggestion, you can use the Report a Problem tool in Visual Studio or head over to Visual Studio Developer Community. You can also find us on Twitter (@VisualC) and (@erikasweet_).

7 comments

Discussion is closed. Login to edit/delete existing comments.

  • Robin KuzminMicrosoft employee 0

    Erika,For me the ASan is ignored. At least I don’t see any effect.I have a WSL (Ubuntu, presumably 18.04, default installed yesterday). I have MS VS Enterprise 2019 Preview, 16.3.0, Preview 2.0 (installed yesterday).I successfully built the “Hello CMake” example from “C++ with Visual Studio 2019 and Windows Subsystem for Linux (WSL)” and the example works fine both in WSL console and on native Ubuntu Linux 19.04.
    # I have the following `libasan` packages installed in WSL:$ dpkg -l *asan*Desired=Unknown/Install/Remove/Purge/Hold| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)||/ Name                     Version           Architecture      Description+++-========================-=================-=================-=====================================================ii  libasan4:amd64           7.4.0-1ubuntu1~18 amd64             AddressSanitizer — a fast memory error detectorii  libasan4-dbg:amd64       7.4.0-1ubuntu1~18 amd64             AddressSanitizer — a fast memory error detector (deb# And the following files in the packages:$ dpkg -L libasan4/./usr/usr/lib/usr/lib/x86_64-linux-gnu/usr/lib/x86_64-linux-gnu/libasan.so.4.0.0/usr/share/usr/share/doc/usr/lib/x86_64-linux-gnu/libasan.so.4/usr/share/doc/libasan4In VS I have a project targetted at WSL-Debug with the following .CPP source:#include “CMakeProject1_linux.h”using namespace std;int main(int argc, char** argv){ //cout << “Hello CMake.” << endl;  volatile int* array = new int[100]; delete[] array; int i = array[argc];  return array[argc];}In VS I have ASan enabled (checkmarked), flags: detect_leaks=0; leak_check_at_exit=true; detect_deadlocks=true(I also tried with no flags).I rebuild all successfully: cd /mnt/c/ed/dev/src/test/msvs/CMakeProject1_linux/out/build/WSL-Debug;export CXXFLAGS=-fsanitize\=address;export CFLAGS=-fsanitize\=address;export LDFLAGS=-fsanitize\=address;/usr/bin/cmake –build “/mnt/c/ed/dev/src/test/msvs/CMakeProject1_linux/out/build/WSL-Debug” –clean-first  ;[ 50%] Building CXX object CMakeFiles/CMakeProject1_linux.dir/CMakeProject1_linux.cpp.o[100%] Linking CXX executable CMakeProject1_linux[100%] Built target CMakeProject1_linuxRebuild All succeeded.I press <F5> (Debug) and break in the debugger in the beginning of the `main()`. I see:Address Sanitizer debugging is enabled. In order to break when errors occur the symbols for libasan.so must be installed on the Linux target, for example by running “sudo apt-get install libasan4-dbg” if libasan version is libasan.so.4.=thread-group-added,id=”i1″GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-gitCopyright (C) 2018 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html&gt;This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.  Type “show copying”and “show warranty” for details.This GDB was configured as “x86_64-linux-gnu”.Type “show configuration” for configuration details.For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/&gt;.Find the GDB manual and other documentation resources online at:<http://www.gnu.org/software/gdb/documentation/&gt;.For help, type “help”.Type “apropos word” to search for commands related to “word”.Warning: Debuggee TargetArchitecture not detected, assuming x86_64.=cmd-param-changed,param=”pagination”,value=”off”Breakpoint 1, main (argc=1, argv=0x7ffffffee858) at /mnt/c/ed/dev/src/test/msvs/CMakeProject1_linux/CMakeProject1_linux.cpp:1212  volatile int* array = new int[100];Loaded ‘/lib64/ld-linux-x86-64.so.2’. Symbols loaded.Loaded ‘/usr/lib/x86_64-linux-gnu/libstdc++.so.6’. Symbols loaded.Loaded ‘/lib/x86_64-linux-gnu/libc.so.6’. Symbols loaded.Loaded ‘/lib/x86_64-linux-gnu/libm.so.6’. Symbols loaded.Loaded ‘/lib/x86_64-linux-gnu/libgcc_s.so.1’. Symbols loaded.In the debugger output only the first-most line mentions the Address Sanitizer. But I don’t see the lineLoaded ‘/usr/lib/x86_64-linux-gnu/libasan.so.4’. Symbols loaded.I press <F5> (Continue) and I don’t see any reaction from ASan untill the very termination :-\.If I double-check which dynamic libraries the compiled executable is about to load:# In WSL console:$ ldd CMakeProject1_linux        linux-vdso.so.1 (0x00007fffe9657000)        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f68eda20000)        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f68ed620000)        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f68ed280000)        /lib64/ld-linux-x86-64.so.2 (0x00007f68ee000000)        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f68ed060000)then I don’t see that it expects `libasan.so.4` :-\—What do I do wrong? What do I miss?

  • Valentin Yanushkovsky 0

    Hi, Your article really helped me, but i have 1 problem.
    When i add “detect_leaks=1” in “AddressSanitizer Runtime Flags”
    I get libsan.so.pdb not loadedAlthough it is installed on my WSL.
    How can i fix it?

    • Erika SweetMicrosoft employee 0

      Hi Valentin, sorry for the late reply. Unfortunately LeakSanitizer doesn’t work under ptrace (strace, gdb, etc.). Visual Studio’s ASan integration for Linux projects is integrated with the debugger (which leverages gdb) so LeakSanitizer is not integrated with Visual Studio as this time.

  • Graham Morris 0

    I am trying to enable Address Sanitizer with WSL. First I understand that x64 is not supported. So I select x86 and I prompted to Connect to Linux. Why? Is this because WSL only works in x64? If I do need to provide a connection what is it for the WSL? If I disable the Sanitizer and enable X64 all works. Summary: Is Address Sanitizer, x64, WSL supported? Aside: In general WSL/VS2019 is good but just far too many different configurations and not all are possible which makes it difficult to use.

    • Erika SweetMicrosoft employee 0

      Hey Graham, thanks for the feedback. Are you using a CMake project or MSBuild-based project?
      x64 + AddressSanitizer + WSL should be a supported scenario. The x64/x86 distinction you are referring to is specific to AddressSanitizer for Windows with MSVC. Can you please open a bug on Developer Community so we can investigate the issue?
      And thanks for the feedback on the configurations. This is helpful as we work to improve VS2019.

  • Nabarun Dev 0

    Hi,

    Does VS code currently have this functionality?

  • Aviv Zisso 0

    Is this still supposed to work?
    Just setup a Linux Console Application project using these instructions, and allowed “Enable Address Sanitizer” following this article. I’m trying to build a simple program against an Ubuntu 18 WSL machine, and get the error:
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(444,5): error MSB8058: -fsanitize=address (Enable Address Sanitizer) not supported by platform ‘x64’.

    I’m using the latest VS2019 Preview 2

Feedback usabilla icon