You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sanitizers/asan.md
+85-31Lines changed: 85 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "AddressSanitizer"
3
3
description: "Top-level description of the AddressSanitizer feature for Microsoft C/C++."
4
-
ms.date: 03/05/2021
4
+
ms.date: 06/13/2023
5
5
f1_keywords: ["AddressSanitizer"]
6
6
helpviewer_keywords: ["ASan", "AddressSanitizer", "Address Sanitizer", "compiling for AddressSanitizer"]
7
7
---
@@ -32,23 +32,21 @@ Use AddressSanitizer to reduce your time spent on:
32
32
- Stress testing
33
33
- Integrating new code
34
34
35
-
AddressSanitizer, originally [introduced by Google](https://www.usenix.org/conference/atc12/technical-sessions/presentation/serebryany), provides run-time bug-finding technologies that use your existing build systems and existing test assets directly.
35
+
AddressSanitizer, originally [introduced by Google](https://www.usenix.org/conference/atc12/technical-sessions/presentation/serebryany), provides runtime bug-finding technologies that use your existing build systems and existing test assets directly.
36
36
37
37
AddressSanitizer is integrated with the Visual Studio project system, the CMake build system, and the IDE. Projects can enable AddressSanitizer by setting a project property, or by using one extra compiler option: **`/fsanitize=address`**. The new option is compatible with all levels of optimization and configurations of x86 and x64. However, it isn't compatible with [edit-and-continue](/visualstudio/debugger/edit-and-continue-visual-cpp), [incremental linking](../build/reference/incremental-link-incrementally.md), and [`/RTC`](../build/reference/rtc-run-time-error-checks.md).
38
38
39
-
Starting in Visual Studio 2019 version 16.9, Microsoft's AddressSanitizer technology enables integration with the Visual Studio IDE. The functionality can optionally create a crash dump file when the sanitizer finds a bug at runtime. If you set the `ASAN_SAVE_DUMPS=MyFileName.dmp` environment variable before you run your program, a crash dump file gets created with extra metadata for efficient [post-mortem debugging](#crash-dumps) of precisely diagnosed bugs. These dump files make extended use of AddressSanitizer easier for:
39
+
Starting in Visual Studio 2019 version 16.9, Microsoft's AddressSanitizer technology enables integration with the Visual Studio IDE. The functionality can optionally create a crash dump file when the sanitizer finds a bug at runtime. If you set the `ASAN_SAVE_DUMPS=MyFileName.dmp` environment variable before you run your program, a crash dump file is created with extra metadata for efficient [post-mortem debugging](#crash-dumps) of precisely diagnosed bugs. These dump files make extended use of AddressSanitizer easier for:
40
40
41
-
- Local machine testing,
42
-
- On-premise distributed testing, and
43
-
- Cloud-based workflows for testing.
41
+
- Local machine testing
42
+
- On-premises distributed testing
43
+
- Cloud-based workflows for testing
44
44
45
45
### Install AddressSanitizer
46
46
47
-
C++ workloads in the Visual Studio Installer install the AddressSanitizer libraries and IDE integration by default. However, if you're upgrading from an older version of Visual Studio 2019, use the Installer to enable ASan support after the upgrade:
47
+
C++ workloads in the Visual Studio Installer install the AddressSanitizer libraries and IDE integration by default. However, if you're upgrading from an older version of Visual Studio 2019, use the Installer to enable ASan support after the upgrade. You can open the installer from the Visual Studio main menu via **Tools** > **Get Tools and Features...** Choose **Modify** on your existing Visual Studio installation from the Visual Studio Installer to get to the following screen.
48
48
49
-
:::image type="content" source="media/asan-installer-option.png" alt-text="Visual Studio Installer screenshot highlighting the C++ AddressSanitizer component":::
50
-
51
-
You can choose **Modify** on your existing Visual Studio installation from the Visual Studio Installer to get to the screen above.
49
+
:::image type="content" source="media/asan-installer-option.png" alt-text="Screenshot of the Visual Studio Installer. The C++ AddressSanitizer component, under the Optional section, is highlighted.":::
52
50
53
51
> [!NOTE]
54
52
> If you run Visual Studio on the new update but haven't installed ASan, you'll get an error when you run your code:
@@ -79,14 +77,14 @@ Microsoft recommends you use AddressSanitizer in these three standard workflows:
This article covers the information you require to enable the three workflows listed above. The information is specific to the **platform-dependent** Windows 10 implementation of AddressSanitizer. This documentation supplements the excellent documentation from [Google, Apple, and GCC](#external-docs) already published.
80
+
This article covers the information you require to enable the three workflows listed previously. The information is specific to the **platform-dependent** Windows 10 implementation of AddressSanitizer. This documentation supplements the excellent documentation from [Google, Apple, and GCC](#external-docs) already published.
83
81
84
82
> [!NOTE]
85
83
> Current support is limited to x86 and x64 on Windows 10. [Send us feedback](https://aka.ms/vsfeedback/browsecpp) on what you'd like to see in future releases. Your feedback helps us prioritize other sanitizers for the future, such as **`/fsanitize=thread`**, **`/fsanitize=leak`**, **`/fsanitize=memory`**, **`/fsanitize=undefined`**, or **`/fsanitize=hwaddress`**. You can [report bugs here](https://aka.ms/feedback/report?space=62) if you run into issues.
86
84
87
85
## <aname="command-prompt"></a> Use AddressSanitizer from a developer command prompt
88
86
89
-
Use the **`/fsanitize=address`** compiler option in a [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts) to enable compiling for the AddressSanitizer runtime. The **`/fsanitize=address`** option is compatible with all existing C++ or C optimization levels (for example, `/Od`, `/O1`, `/O2`, `/O2 /GL`, and `PGO`). The option works with static and dynamic CRTs (for example, `/MD`, `/MDd`, `/MT`, and `/MTd`). It works whether you create an EXE or a DLL. Debug information is required for optimal formatting of call stacks. In the example below, `cl /fsanitize=address /Zi` is passed on the command line.
87
+
Use the **`/fsanitize=address`** compiler option in a [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts) to enable compiling for the AddressSanitizer runtime. The **`/fsanitize=address`** option is compatible with all existing C++ or C optimization levels (for example, `/Od`, `/O1`, `/O2`, `/O2 /GL`, and `PGO`). The option works with static and dynamic CRTs (for example, `/MD`, `/MDd`, `/MT`, and `/MTd`). It works whether you create an EXE or a DLL. Debug information is required for optimal formatting of call stacks. In the following example, `cl /fsanitize=address /Zi` is passed on the command line.
90
88
91
89
The AddressSanitizer libraries (.lib files) are linked for you automatically. For more information, see [AddressSanitizer language, build, and debugging reference](asan-building.md).
92
90
@@ -105,13 +103,15 @@ int main() {
105
103
106
104
Using a developer command prompt for Visual Studio 2019, compile *`main.cpp`* using `/fsanitize=address /Zi`
107
105
108
-
:::image type="content" source="media/asan-command-basic-global-overflow.png" alt-text="Screenshot of a command prompt showing the command to compile with AddressSanitizer options.":::
106
+
:::image type="content" source="media/asan-command-basic-global-overflow.png" alt-text="Screenshot of a command prompt showing the command to compile with AddressSanitizer options. The command is: `cl main.cpp -faanitize-address /Zi`.":::
109
107
110
-
When you run the resulting *`main.exe`* at the command line, it creates the formatted error report seen below.
108
+
When you run the resulting *`main.exe`* at the command line, it creates the formatted error report that follows.
111
109
112
110
Consider the overlaid, red boxes that highlight seven key pieces of information:
113
111
114
-
:::image type="content" source="media/asan-basic-global-overflow.png" alt-text="Screenshot of the debugger showing a basic global overflow error.":::
112
+
:::image type="complex" source="media/asan-basic-global-overflow.png" alt-text="Screenshot of the debugger showing a basic global overflow error.":::
113
+
There are seven red highlights identifying key pieces of information in the error report. They map to the numbered list that follows this screenshot. The numbered boxes highlight the following text: 1) global-buffer-overflow 2) WRITE of size 4 3) basic-global-overflow.cpp 7 4) to the right of global variable 'x' defined in 'basic-global-overflow.cpp:3:8' 5) of size 400 6) 00 00[f9]f9 f9 7) Box is in the shadow byte legend area and contains Global redzone: f9
114
+
:::image-end:::
115
115
116
116
### Red highlights, from top to bottom
117
117
@@ -138,34 +138,88 @@ To build from the IDE, opt out of any [incompatible options](./asan-known-issues
138
138
- Turn off [`/RTC1` (runtime checks)](../build/reference/rtc-run-time-error-checks.md)
139
139
- Turn off [`/INCREMENTAL` (incremental linking)](../build/reference/incremental-link-incrementally.md)
140
140
141
-
To build and run the debugger, enter**F5**. You'll see this window in Visual Studio:
141
+
To build and run the debugger, press**F5**. An **Exception Thrown** window appears in Visual Studio:
142
142
143
143
:::image type="content" source="media/asan-global-buffer-overflow-F5.png" alt-text="Screenshot of the debugger showing a global buffer overflow error.":::
144
144
145
145
## <aname="ide-cmake"></a> Use AddressSanitizer from Visual Studio: CMake
146
146
147
-
To enable AddressSanitizer for [a CMake project created to target Windows](../build/cmake-projects-in-visual-studio.md), take these steps:
147
+
To enable AddressSanitizer for a [CMake project created to target Windows](../build/cmake-projects-in-visual-studio.md), follow these steps:
148
148
149
149
1. Open the **Configurations** dropdown in the toolbar at the top of the IDE and select **Manage Configurations**.
150
150
151
-
:::image type="content" source="media/asan-cmake-configuration-dropdown.png" alt-text="Screenshot of the CMake configuration dropdown.":::
151
+
:::image type="content" source="media/asan-cmake-configuration-drop-down.png" alt-text="Screenshot of the CMake configuration dropdown. It displays options like x64 Debug, x64 Release, and so on. At the bottom of the list, Manage Configurations... is highlighted.":::
152
152
153
-
That selection opens the CMake Project Settings editor, which is saved in a CMakeSettings.json file.
153
+
That opens the CMake Project Settings editor, which reflects the contents of your project's `CMakeSettings.json` file.
154
154
155
155
1. Choose the **Edit JSON** link in the editor. This selection switches the view to raw JSON.
156
156
157
-
1. Add the property: **"addressSanitizerEnabled": true**
158
-
159
-
This image is of CMakeSettings.json after that change:
160
-
161
-
:::image type="content" source="media/asan-cmake-json.png" alt-text="Screenshot of the text editor view of CMakeSettings.json.":::
162
-
163
-
1. Enter **Ctrl+S** to save this JSON file, then enter **F5** to recompile and run under the debugger.
164
-
165
-
This screenshot captures the error from the CMake build.
166
-
167
-
:::image type="content" source="media/asan-cmake-error-f5.png" alt-text="Screenshot of the CMake build error message.":::
168
-
157
+
1. Add the following snippet to the `"windows-base"` preset, inside `"configurePresets":` to turn on Address Sanitizer:
158
+
159
+
```json
160
+
"environment": {
161
+
"CFLAGS": "/fsanitize=address",
162
+
"CXXFLAGS": "/fsanitize=address"
163
+
}
164
+
```
165
+
166
+
`"configurePresets"` looks something like this, afterwards:
1. Address sanitizer doesn't work if edit-and-continue is specified (`/ZI`), which is enabled by default for new CMake projects. In `CMakeLists.txt`, comment out (prefix with `#`) the line that starts with `set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT"`. That line looks something like this, afterwards:
1. Clear your CMake cache directory and reconfigure by choosing from the Visual Studio menu: **Project** > **Delete cache and Reconfigure**. Choose **Yes** when the prompt appears to clear your cache directory and reconfigure.
200
+
1. Replace the contents of the source file (for example, `CMakeProject1.cpp`) with the following:
201
+
202
+
```cpp
203
+
// CMakeProject1.cpp : Defines the entry point for the application
204
+
205
+
#include <stdio.h>
206
+
207
+
int x[100];
208
+
209
+
int main()
210
+
{
211
+
printf("Hello!\n");
212
+
x[100] = 5; // Boom!
213
+
return 0;
214
+
}
215
+
```
216
+
217
+
1. Choose **F5** to recompile and run under the debugger.
218
+
219
+
This screenshot captures the error from the CMake build.
220
+
221
+
:::image type="content" source="media/asan-cmake-error-f5.png" alt-text="Screenshot of an exception that says: Address Sanitizer Error: Global buffer overflow. In the background, address sanitizer output is visible in command window.":::
We introduced new functionality in AddressSanitizer for use with cloud and distributed workflows. This functionality allows offline viewing of an AddressSanitizer error in the IDE. The error gets overlaid on top of your source, just as you would experience in a live debug session.
0 commit comments