{"id":24288,"date":"2019-05-08T17:58:06","date_gmt":"2019-05-08T17:58:06","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=24288"},"modified":"2019-09-22T22:16:53","modified_gmt":"2019-09-22T22:16:53","slug":"addresssanitizer-asan-for-the-linux-workload-in-visual-studio-2019","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/addresssanitizer-asan-for-the-linux-workload-in-visual-studio-2019\/","title":{"rendered":"AddressSanitizer (ASan) for the Linux Workload in Visual Studio 2019"},"content":{"rendered":"<p>In <a href=\"https:\/\/visualstudio.microsoft.com\/vs\/preview\/\">Visual Studio 2019 version 16.1 Preview 3<\/a> we have integrated <a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizer\">AddressSanitizer (ASan)<\/a> into Visual Studio for Linux projects. ASan is a runtime memory error detector for C\/C++ that catches the following errors:<\/p>\n<ul>\n<li>Use after free (dangling pointer reference)<\/li>\n<li>Heap buffer overflow<\/li>\n<li>Stack buffer overflow<\/li>\n<li>Use after return<\/li>\n<li>Use after scope<\/li>\n<li>Initialization order bugs<\/li>\n<\/ul>\n<p>You can enable ASan for <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/configure-a-linux-project?view=vs-2019\">MSBuild-based Linux projects<\/a> and <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/cmake-linux-project?view=vs-2019\">CMake projects<\/a> that target a <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/connect-to-your-remote-linux-computer?view=vs-2019\">remote Linux system<\/a> or <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/wsl\/about\">WSL (Windows Subsystem for Linux)<\/a>. If you are just getting started with cross-platform development, I recommend following this walk-through to <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/c-with-visual-studio-2019-and-windows-subsystem-for-linux-wsl\/\">get started with Visual Studio\u2019s native support for WSL<\/a>.<\/p>\n<p>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:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-24289\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-8.png\" alt=\"AddressSanitizer error\" width=\"724\" height=\"180\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-8.png 724w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-8-300x75.png 300w\" sizes=\"(max-width: 724px) 100vw, 724px\" \/><\/p>\n<p>You can also view the full ASan output (including where the corrupted memory was allocated\/deallocated) in the Debug pane of the output window.<\/p>\n<h4>Getting started with ASan in Visual Studio<\/h4>\n<p>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:<\/p>\n<table class=\" aligncenter\" style=\"height: 258px; width: 254px; border-style: none; border-color: #000000;\" width=\"254\">\n<tbody>\n<tr style=\"height: 43px;\">\n<td style=\"width: 128px; height: 43px; border-style: solid; border-color: #000000;\"><strong>ASan version<\/strong><\/td>\n<td style=\"width: 122px; height: 43px; border-style: solid; border-color: #000000;\"><strong>GCC version<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 43px;\">\n<td style=\"width: 128px; height: 43px; border-style: solid; border-color: #000000;\">libasan0<\/td>\n<td style=\"width: 122px; height: 43px; border-style: solid; border-color: #000000;\">gcc-4.8<\/td>\n<\/tr>\n<tr style=\"height: 43px;\">\n<td style=\"width: 128px; height: 43px; border-style: solid; border-color: #000000;\">libasan2<\/td>\n<td style=\"width: 122px; height: 43px; border-style: solid; border-color: #000000;\">gcc-5<\/td>\n<\/tr>\n<tr style=\"height: 43px;\">\n<td style=\"width: 128px; height: 43px; border-style: solid; border-color: #000000;\">libasan3<\/td>\n<td style=\"width: 122px; height: 43px; border-style: solid; border-color: #000000;\">gcc-6<\/td>\n<\/tr>\n<tr style=\"height: 43px;\">\n<td style=\"width: 128px; height: 43px; border-style: solid; border-color: #000000;\">libasan4<\/td>\n<td style=\"width: 122px; height: 43px; border-style: solid; border-color: #000000;\">gcc-7<\/td>\n<\/tr>\n<tr style=\"height: 43px;\">\n<td style=\"width: 128px; height: 43px; border-style: solid; border-color: #000000;\">libasan5<\/td>\n<td style=\"width: 122px; height: 43px; border-style: solid; border-color: #000000;\">gcc-8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>You can determine the version of GCC you have on your Linux machine or WSL installation with the following command:<\/p>\n<p style=\"text-align: center;\"><span class=\"font-size:14 lang:default highlight:0 decode:true crayon-inline\">gcc &#8211;version<\/span><\/p>\n<p>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:<\/p>\n<p style=\"text-align: center;\"><span class=\"font-size:14 lang:default highlight:0 decode:true crayon-inline\">Loaded &#8216;\/usr\/lib\/x86_64-linux-gnu\/libasan.so.4&#8217;. Symbols loaded.<\/span><\/p>\n<p>In this example, my Linux machine (Ubuntu 18.04) is using libasan4.<\/p>\n<p>You can install the ASan debug bits on Linux distros that use apt with the following command (this command installs version 4):<\/p>\n<p style=\"text-align: center;\"><span class=\"font-size:14 lang:default highlight:0 decode:true crayon-inline\">sudo apt-get install libasan4-dbg<\/span><\/p>\n<p>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.<\/p>\n<h4>Enable ASan for MSBuild-based Linux projects<\/h4>\n<p>You can enable ASan for <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/linux\/configure-a-linux-project?view=vs-2019\">MSBuild-based Linux projects<\/a> in the project\u2019s Property Pages. Right-click on the project in the Solution Explorer and select \u201cProperties\u201d to open the project\u2019s Property Pages, then navigate to Configuration Properties &gt; C\/C++ &gt; Sanitizers. ASan is enabled via compiler and linker flags and requires recompilation in order to work.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-24290\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-9.png\" alt=\"Enable ASan for MSBuild-based projects via the project's Property Pages\" width=\"828\" height=\"597\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-9.png 828w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-9-300x216.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-9-768x554.png 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/p>\n<p><strong>Note: Starting in Visual Studio 2019 version 16.4, AddressSanitizer for Linux projects is enabled via Configuration Properties &gt; C\/C++ &gt; Enable Address Sanitizer.\u00a0<\/strong><\/p>\n<p>You can also pass optional <a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizerFlags\">ASan runtime flags<\/a> by navigating to Configuration Properties &gt; Debugging &gt; AddressSanitizer Runtime Flags.<\/p>\n<h4>Enable ASan for Visual Studio CMake projects<\/h4>\n<p>You can enable ASan for CMake configurations targeting a remote Linux machine or WSL in the <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/introducing-the-new-cmake-project-settings-ui\/\">CMake Settings Editor<\/a>. In the \u201cGeneral\u201d section of the editor you will see the following two properties to enable ASan and pass optional runtime flags: <img decoding=\"async\" class=\"aligncenter wp-image-24291\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-10.png\" alt=\"Enable ASan for CMake projects via the CMake Settings Editor\" width=\"963\" height=\"131\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-10.png 963w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-10-300x41.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2019\/05\/word-image-10-768x104.png 768w\" sizes=\"(max-width: 963px) 100vw, 963px\" \/><\/p>\n<p>Again, ASan is enabled via compiler and linker flags and requires recompilation in order to work.<\/p>\n<h4>Give us your feedback!<\/h4>\n<p>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 (<a href=\"mailto:visualcpp@microsoft.com\">visualcpp@microsoft.com<\/a>). If you encounter other problems with Visual Studio or MSVC or have a\u00a0suggestion, you can use the\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/ide\/how-to-report-a-problem-with-visual-studio?view=vs-2019\">Report a Problem<\/a> tool in Visual Studio or head over to\u00a0<a href=\"https:\/\/developercommunity.visualstudio.com\/spaces\/62\/index.html\">Visual Studio Developer Community<\/a>. You can also find us on Twitter (<a href=\"https:\/\/twitter.com\/visualc\">@VisualC<\/a>) and (<a href=\"https:\/\/twitter.com\/erikasweet_\">@erikasweet_<\/a>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":2953,"featured_media":24289,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[270,266,239,279,230],"tags":[],"class_list":["post-24288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcement","category-cmake","category-diagnostics","category-linux","category-new-feature"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/24288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/2953"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=24288"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/24288\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/24289"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=24288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=24288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=24288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}