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
There are three ways your code can generate error reports:
7
+
8
+
-[Command line](#Command-line)
9
+
-[IDE](#IDE)
10
+
-[Snapshot files](#Snapshot-file)
11
+
12
+
These types of reports can be generated for many types of [errors found at runtime](#Error-types).
13
+
14
+
### Example
15
+
16
+
!!! Jim, I think we need a simpler example here. !!!
17
+
The following source code is **safe by coincidence**. It will **not** fail at runtime. The Windows 10, 16.9 version of the C runtime, will pad the 13 bytes requested, in order to facilitate alignment for subsequent calls to alloca.
18
+
19
+
```cpp
20
+
int x = 13;
21
+
22
+
voidmain()
23
+
{
24
+
int *cc;
25
+
int i;
26
+
int k = 17;
27
+
28
+
__try{
29
+
30
+
cc = (int*)_alloca(x); // Cast pointer to 1-byte to pointer to 4-bytes
31
+
if (((int)cc) & 0x3)
32
+
fail = 1;
33
+
34
+
cc[0] = 1;
35
+
cc[1] = 1;
36
+
cc[2] = 2;
37
+
cc[3] = 3; //Boom! Only caught with -fsanitize=address
38
+
```
39
+
40
+
Padding and alignment is platform dependent. It's a function of the CRT used on a particular operating system and whether the target is 32-bits, or 64-bits. The above example, or a slightly modified version, could produce different runtime results on Linux macOS or Android.
41
+
42
+
### Command line ###
43
+
44
+
If you run from the command line then your binary will emit formatted text reports to the screen. Consider the over layed, red boxes which high light four (4) key pieces of information from the error in the _alloca example.
1.) A write of 4 bytes (32-bits) went beyond the stack allocation the program explicitly requested.
51
+
52
+
2.) That store was produced from line 47 in the function "main"
53
+
54
+
3.) The type of error was a dynamic stack buffer overflow for the 13 bytes explicitly requested.
55
+
56
+
4.) The [**shadow bytes**](.\asan-shadowbytes.md) that correspond to the address used in the overflowing store, indicate 13 bytes (8 + 5) were explicitly allocated for the alloca.
57
+
58
+
**Note:** The call stack is converted to function names through the [LLVM symbolizer](https://llvm.org/docs/CommandGuide/llvm-symbolizer.html). The Address Sanitizer creates the rest of of the report based on its context, the shadow bytes, and meta-data the compiler produces.
59
+
60
+
### IDE
61
+
62
+
By simply recompiling with -fsanitze=address and invoking Visual Studio from the command line, the IDE will automatically map a pop up to the line and column causing the error.
63
+
64
+
devenv /debugexe MyApp.exe arg1 arg2 ... argn
65
+
66
+
Consider the following error found in our cached version of spec2k6\povray where the program allocates 24-bytes but only frees 8-bytes. The details for where the allocation and free took place are in the **output pane** of the Visual Studio screen shot immediately below:
67
+
68
+

69
+
70
+
71
+
### VCASan library
72
+
73
+
The flag -fsanitize=address automatically links a new static library to your .EXE or .DLL. This static library will automatically produce:
74
+
75
+
- In memory meta-data for directly interfacing with the VS IDE, [while debugging](#Error-types).
76
+
- An optional [snap shot file](#Snapshot-files) with the same IDE meta-data.
We've made it very simple to store an Address Sanitizer runtime error in a new type of crash dump file.This functionality is simply enabled with an environment variable.
6
+
7
+
`set ASAN_SAVE_DUMPS="MyFileName.dmpx"`
8
+
9
+
## How it works
10
+
11
+
Address Sanitizer exceptions are thrown from inside the runtime, due to many [error types](#errors) detected at runtime. When run from the command line the Address Sanitizer runtime produces text output with error information. When run from the Visual Studio IDE the debugger displays the error information.
12
+
13
+
Upon error, your application will produce MyFileName.dmpx which is a [dump file](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/proc_snap/export-a-process-snapshot-to-a-file) that can be opened and debugged using Visual Studio.
14
+
15
+
**Note** that like all other dump files, [debugging symbols](https://docs.microsoft.com/en-us/windows/win32/dxtecharts/debugging-with-symbols) must be available and must match the version of the source compiled.
Copy file name to clipboardExpand all lines: docs/cpp/ASAN/asan-top-level.md
+24-94Lines changed: 24 additions & 94 deletions
Original file line number
Diff line number
Diff line change
@@ -24,9 +24,9 @@ The Address Sanitizer is a compiler and runtime runtime [introduced by Google](h
24
24
25
25
Compiling with `-fsanitize=address` is a powerful alternative to both [/RTC](https://docs.microsoft.com/en-us/cpp/build/reference/rtc-run-time-error-checks?view=msvc-160), and [/analyze](https://docs.microsoft.com/en-us/cpp/code-quality/code-analysis-for-c-cpp-overview?view=msvc-160). It provides run-time bug-finding technologies which leverage your existing build systems and existing test assets.
26
26
27
-
We've also extended the basic Windows executable file. By setting a new environment variable via **`set ASAN_SAVE_DUMPS=”MyFileName.dmpx”`** your program will create a new type of crash dump file that will contain extra meta-data for post-mortem debugging. These dump files can be displayed off-line, with Visual Studio's new debugger IDE. These dump files can be an enabler for work flows requiring:
27
+
We also link a new library to your executable. By setting a new environment variable via **`set ASAN_SAVE_DUMPS=”MyFileName.dmpx”`** your program will create a new type of crash dump file that will contain extra meta-data for post-mortem debugging. These dump files can be displayed off-line, with Visual Studio's new debugger IDE. These dump files are an enabler for:
28
28
29
-
- On-premises testing
29
+
- On-premise single machine or distributed testing
30
30
- Cloud based workflows for testing
31
31
32
32
These test systems can store the dump files, with **precisely diagnosed bugs**, for later viewing super imposed on your source code in the IDE.
@@ -37,7 +37,7 @@ Simply [**install the Address Sanitizer functionality**]().
37
37
38
38
After installing you can build your executables with the `-fsanitize=address`compiler switch using any of the following:
39
39
40
-
-a command line
40
+
-Command line
41
41
- Visual Studio project system
42
42
- Visual Studio Cmake make integration
43
43
@@ -53,7 +53,6 @@ Microsoft recommends using the Address Sanitizer in these **three standard workf
53
53
- Visual Studio - [Project system](#Using-the-Address-Sanitizer-from-Visual-Studio)
54
54
- Visual Studio - [CMake]([CMake](#Using-the-Address-Sanitizer-from-Visual-Studio:-CMake))
55
55
56
-
57
56
-**CI/CD** - continuous integration / continuous development
@@ -67,9 +66,9 @@ This article will cover the information needed to enable the three workflows lis
67
66
68
67
## Using the Address Sanitizer from a Developer Command Prompt
69
68
70
-
Compile with `-fsanitize=address` to enable Address Sanitizer. The compiler flag `-fsanitize=address` is compatible with all existing C++ or C optimization levels (e.g., /Od, /O1, /O2, /O2 /GL and PGO), works with static and dynamic CRTs (e.g. /MD, /MDd, /MT, /MTd) and can be used to create a .EXE or .DLL. Debug information is required for the Address Sanitizer runtime to print correct call stacks on errors; In this example we pass `-/Zi`.
69
+
Compile with `-fsanitize=address` to enable Address Sanitizer. The compiler flag `-fsanitize=address` is compatible with all existing C++ or C optimization levels (e.g., /Od, /O1, /O2, /O2 /GL and PGO), works with static and dynamic CRTs (e.g. /MD, /MDd, /MT, /MTd) and can be used to create an .EXE or .DLL. Debug information is required for optimal formatting of call stacks. In this example we explicitly pass `-/Zi`.
71
70
72
-
The Address Sanitizer libraries (.lib files) will be linked in for you. For more detail, and for guidelines on partitioned build systems, see [building to target the Address Sanitizer runtime.](.\ASan-building.md).
71
+
The Address Sanitizer libraries (.lib files) will be linked for you. For more detail, and for guidelines on partitioned build systems, see [building to target the Address Sanitizer runtime.](.\ASan-building.md).
73
72
74
73
### Example - basic global buffer overflow:
75
74
@@ -157,17 +156,15 @@ The following screen shot captures the error from the CMake build.
157
156
158
157

159
158
160
-
## Offline Address Sanitizer crash dumps
159
+
## Address Sanitizer crash dumps - cloud and distributed workflows
161
160
162
-
Address Sanitizer exceptions are triggered one of the many [errors](#errors) are detected at runtime. When run from the command line the Address Sanitizer runtime produces text output with error information. When run from the Visual Studio IDE the debugger displays the error information.
161
+
To produce a new type of dump file which can be viewed in Visual Studio on another machine at a later date:
163
162
164
-
To produce a dump file of the error which can be debugged offline, set an environment variable which is consumed by the Address Sanitizer runtime.
163
+
`set ASAN_SAVE_DUMPS="MyFileName.dmpx"`
165
164
166
-
`set ASAN_SAVE_DUMPS="MyFileName.dmpx"`
165
+
As of Visual Studio 16.9 will be able to display the **precisely diagnosed error**, stored in MyFileName.dmpx, with your source code.
167
166
168
-
Upon error, your application will produce MyFileName.dmpx which is a [dump file](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/proc_snap/export-a-process-snapshot-to-a-file) that can be opened and debugged using Visual Studio.
169
-
170
-
**Note** that like all other dump files, [debugging symbols](https://docs.microsoft.com/en-us/windows/win32/dxtecharts/debugging-with-symbols) must be available and must match the version of the source compiled.
167
+
[This new crash dump functionality](.\asan-offline-address-sanitizer-crash-dumps.md) is an enabler for cloud based workflows and distributed testing.
171
168
172
169
## Error types
173
170
@@ -191,14 +188,23 @@ The following list of runtime errors can be exposed when you run your binaries c
- **stack-use-after-scope** - this is on by default and can't be turned off.
199
196
- **stack-use-after-return** - this is not available by just setting ASAN_OPTIONS
200
197
201
-
See [Building for the Address Sanitizer with MSVC](.\asan-building.md).These decisions were made to reduce the test matrix used to ship this first version.
198
+
These decisions were made to reduce the test matrix used to ship this first version.
199
+
200
+
We did NOT ship features that could lead to false positives in this first release. That discipline enforced an effective testing integrity necessary when considering inter-op with years of exiting code. The following functionalities will be shipped later:
201
+
202
+
- [Initialization Order Fiasco](https://github.com/google/sanitizers/wiki/AddressSanitizerInitializationOrderFiasco)
These structure all further details into the tools and the run times they target.
222
226
223
227
----
224
-
225
-
## STUFF TO KEEP -- remove later!!!!!
226
-
227
-
## Viewing Address Saniziter Errors ##
228
-
229
-
There are three ways your code can generate error reports:
230
-
231
-
- [Command line](#Command-line)
232
-
- [IDE](#IDE)
233
-
- [Snapshot files](#Snapshot-file)
234
-
235
-
These types of reports can be generated for many types of [errors found at runtime](#Error-types).
236
-
237
-
### Example
238
-
239
-
!!! Jim, I think we need a simpler example here. !!!
240
-
The following source code is **safe by coincidence**. It will **not** fail at runtime. The Windows 10, 16.9 version of the C runtime, will pad the 13 bytes requested, in order to facilitate alignment for subsequent calls to alloca.
241
-
242
-
```cpp
243
-
int x = 13;
244
-
245
-
void main()
246
-
{
247
-
int *cc;
248
-
int i;
249
-
int k = 17;
250
-
251
-
__try{
252
-
253
-
cc = (int*)_alloca(x); // Cast pointer to 1-byte to pointer to 4-bytes
254
-
if (((int)cc) & 0x3)
255
-
fail = 1;
256
-
257
-
cc[0] = 1;
258
-
cc[1] = 1;
259
-
cc[2] = 2;
260
-
cc[3] = 3; //Boom! Only caught with -fsanitize=address
261
-
```
262
-
263
-
Padding and alignment is platform dependent. It's a function of the CRT used on a particular operating system and whether the target is 32-bits, or 64-bits. The above example, or a slightly modified version, could produce different runtime results on Linux macOS or Android.
264
-
265
-
### Command line ###
266
-
267
-
If you run from the command line then your binary will emit formatted text reports to the screen. Consider the over layed, red boxes which high light four (4) key pieces of information from the error in the _alloca example.
1.) A write of 4 bytes (32-bits) went beyond the stack allocation the program explicitly requested.
274
-
275
-
2.) That store was produced from line 47 in the function "main"
276
-
277
-
3.) The type of error was a dynamic stack buffer overflow for the 13 bytes explicitly requested.
278
-
279
-
4.) The [**shadow bytes**](.\asan-shadowbytes.md) that correspond to the address used in the overflowing store, indicate 13 bytes (8 + 5) were explicitly allocated for the alloca.
280
-
281
-
**Note:** The call stack is converted to function names through the [LLVM symbolizer](https://llvm.org/docs/CommandGuide/llvm-symbolizer.html). The Address Sanitizer creates the rest of of the report based on its context, the shadow bytes, and meta-data the compiler produces.
282
-
283
-
### IDE
284
-
285
-
By simply recompiling with -fsanitze=address and invoking Visual Studio from the command line, the IDE will automatically map a pop up to the line and column causing the error.
286
-
287
-
devenv /debugexe MyApp.exe arg1 arg2 ... argn
288
-
289
-
Consider the following error found in our cached version of spec2k6\povray where the program allocates 24-bytes but only frees 8-bytes. The details for where the allocation and free took place are in the **output pane** of the Visual Studio screen shot immediately below:
290
-
291
-

292
-
293
-
294
-
### VCASan library
295
-
296
-
The flag -fsanitize=address automatically links a new static library to your .EXE or .DLL. This static library will automatically produce:
297
-
298
-
- In memory meta-data for directly interfacing with the VS IDE, [while debugging](#Error-types).
299
-
- An optional [snap shot file](#Snapshot-files) with the same IDE meta-data.
0 commit comments