Skip to content

Commit 665fdc8

Browse files
author
Colin Robertson
committed
Don't bury the lede
1 parent 2412103 commit 665fdc8

18 files changed

+71
-51
lines changed

docs/sanitizers/asan-error-examples.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ helpviewer_keywords: ["ASan error examples", "AddressSanitizer error examples",
66
---
77
# AddressSanitizer error examples
88

9-
We list a subset of the errors supported by AddressSanitizer in Microsoft C/C++ (MSVC) in this section. This list is not an exhaustive error list. It's meant to show several kinds of errors you'll see in AddressSanitizer. In each article, we've ported some examples that help you learn to use the AddressSanitizer features supported by MSVC in your code. These examples are based on sources in the [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases). All screenshots were generated by using **`devenv.exe /debugexe example.exe`**.
10-
11-
9+
We list a subset of the errors supported by AddressSanitizer in Microsoft C/C++ (MSVC) in this section. This list is not an exhaustive error list. It's meant to show several kinds of errors you'll see in AddressSanitizer. In each article, we've included example code with build instructions and screenshots of the debugger in action. They'll help you learn to use the AddressSanitizer features supported by MSVC in your code. All screenshots were generated by using **`devenv.exe /debugexe example.exe`**. Some of these examples are based on sample code in the [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
1210

1311
## Build the error examples
1412

docs/sanitizers/asan.md

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "AddressSanitizer"
33
description: "Top-level description of the AddressSanitizer feature for Microsoft C/C++."
4-
ms.date: 02/05/2021
4+
ms.date: 03/05/2021
55
f1_keywords: ["AddressSanitizer"]
66
helpviewer_keywords: ["ASan", "AddressSanitizer", "Address Sanitizer", "compiling for AddressSanitizer"]
77
---
@@ -10,31 +10,41 @@ helpviewer_keywords: ["ASan", "AddressSanitizer", "Address Sanitizer", "compilin
1010

1111
## Overview
1212

13-
The C & C++ languages are powerful, but can suffer from a class of bugs that affect **program correctness** and **program security**. Starting in Visual Studio 2019 version 16.9, the Microsoft C/C++ compiler (MSVC) and IDE supports the AddressSanitizer. This compiler and runtime technology exposes many [hard-to-find bugs](./asan-error-examples.md) with **zero false positives**.
13+
The C & C++ languages are powerful, but can suffer from a class of bugs that affect program correctness and program security. Starting in Visual Studio 2019 version 16.9, the Microsoft C/C++ compiler (MSVC) and IDE supports the *AddressSanitizer*. AddressSanitizer (ASan) is a compiler and runtime technology that exposes many hard-to-find bugs with **zero** false positives:
1414

15-
Use this feature to reduce your time spent on:
15+
- [Alloc/dealloc mismatches](error-alloc-dealloc-mismatch.md) and [`new`/`delete` type mismatches](error-new-delete-type-mismatch.md)
16+
- [Allocations too large for the heap](error-allocation-size-too-big.md)
17+
- [`calloc` overflow](error-calloc-overflow.md) and [`alloca` overflow](error-dynamic-stack-buffer-overflow.md)
18+
- [Double free](error-double-free.md) and [use after free](error-heap-use-after-free.md)
19+
- [Global variable overflow](error-global-buffer-overflow.md)
20+
- [Heap buffer overflow](error-heap-buffer-overflow.md)
21+
- [Invalid alignment of aligned values](error-invalid-allocation-alignment.md)
22+
- [`memcpy`](error-memcpy-param-overlap.md) and [`strncat` parameter overlap](error-strncat-param-overlap.md)
23+
- [Stack buffer overflow](error-stack-buffer-overflow.md) and [underflow](error-stack-buffer-underflow.md)
24+
- [Stack use after `return`](error-stack-use-after-return.md) and [use after scope](error-stack-use-after-scope.md)
25+
- [Memory use after it's poisoned](error-use-after-poison.md)
26+
27+
Use AddressSanitizer to reduce your time spent on:
1628

1729
- Basic correctness
1830
- Cross platform portability
1931
- Security
2032
- Stress testing
2133
- Integrating new code
2234

23-
The AddressSanitizer is a compiler and runtime [introduced by Google](https://www.usenix.org/conference/atc12/technical-sessions/presentation/serebryany). The [`/fsanitize=address`](../build/reference/fsanitize.md) compiler option is a powerful alternative to both [`/RTC`](../build/reference/rtc-run-time-error-checks.md) and [`/analyze`](../build/reference/analyze-code-analysis.md). It provides run-time bug-finding technologies that use your existing build systems and existing test assets directly.
24-
25-
Projects can enable the 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, but it's incompatible 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).
35+
AddressSanitizer, originally [introduced by Google](https://www.usenix.org/conference/atc12/technical-sessions/presentation/serebryany), is a powerful alternative to both [`/RTC` (Runtime error checks)](../build/reference/rtc-run-time-error-checks.md) and [`/analyze` (Static analysis)](../build/reference/analyze-code-analysis.md). It provides run-time bug-finding technologies that use your existing build systems and existing test assets directly.
2636

27-
The AddressSanitizer is integrated with the Visual Studio project system, the CMake build system, and the IDE.
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's incompatible 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).
2838

29-
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 the 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 gets created with extra metadata for efficient [post-mortem debugging](#crash-dumps) of precisely diagnosed bugs. These dump files make extended use of the AddressSanitizer easier for:
3040

3141
- Local machine testing,
3242
- On-premise distributed testing, and
3343
- Cloud-based workflows for testing.
3444

35-
### Installing the AddressSanitizer
45+
### Install the AddressSanitizer
3646

37-
The AddressSanitizer is installed by default with C++ workloads in the Visual Studio Installer. However, if you're upgrading from an older version of Visual Studio 2019, you'll need to use the Installer to enable ASan support after the upgrade:
47+
The AddressSanitizer IDE integration and libraries get installed by default with C++ workloads in the Visual Studio Installer. However, if you're upgrading from an older version of Visual Studio 2019, use the Installer to enable ASan support after the upgrade:
3848

3949
:::image type="content" source="media/asan-installer-option.png" alt-text="Visual Studio Installer screenshot highlighting the C++ AddressSanitizer component":::
4050

@@ -45,17 +55,17 @@ You can choose **Modify** on your existing Visual Studio installation from the V
4555
>
4656
> LNK1356: cannot find library 'clang_rt.asan_dynamic-i386.lib'
4757
48-
### <a name="using-asan"></a> Using the AddressSanitizer
58+
### <a name="using-asan"></a> Use the AddressSanitizer
4959

5060
Start building your executables with the **`/fsanitize=address`** compiler option using any of these common development methods:
5161

52-
- Command line
62+
- Command line builds
5363
- Visual Studio project system
5464
- Visual Studio CMake integration
5565

56-
Recompile, then run your program normally. This code generation exposes [many types of precisely diagnosed bugs](./asan-error-examples.md). These errors get reported in three ways: in the debugger IDE, on the command line, or stored in a [new type of dump file](#crash-dumps) for precise off-line processing.
66+
Recompile, then run your program normally. This code generation exposes [many types of precisely diagnosed bugs](#error-types). These errors get reported in three ways: in the debugger IDE, on the command line, or stored in a [new type of dump file](#crash-dumps) for precise off-line processing.
5767

58-
Microsoft recommends using the AddressSanitizer in these **three standard workflows**:
68+
Microsoft recommends using the AddressSanitizer in these three standard workflows:
5969

6070
- **Developer inner loop**
6171
- Visual Studio - [Command line](#command-prompt)
@@ -118,11 +128,11 @@ Consider the overlaid, red boxes that highlight seven key pieces of information:
118128
119129
## <a name="ide-msbuild"></a> Use the AddressSanitizer in Visual Studio
120130

121-
AddressSanitizer is integrated with the Visual Studio IDE. To turn on the AddressSanitizer for an MSBuild project, right-click on the project in Solution Explorer and choose Properties. In the Property Pages dialog, select **Configuration Properties** > **C/C++** > **General**, then modify the **Enable AddressSanitizer** property. Choose **OK** to save your changes.
131+
AddressSanitizer is integrated with the Visual Studio IDE. To turn on the AddressSanitizer for an MSBuild project, right-click on the project in Solution Explorer and choose **Properties**. In the **Property Pages** dialog, select **Configuration Properties** > **C/C++** > **General**, then modify the **Enable AddressSanitizer** property. Choose **OK** to save your changes.
122132

123133
:::image type="content" source="media/asan-project-system-dialog.png" alt-text="Screenshot of the Property Pages dialog showing the Enable AddressSanitizer property.":::
124134

125-
To build from the IDE, opt out of [these incompatible options](./asan-known-issues.md#incompatible-options). For an existing project compiled by using **`/Od`** (or Debug mode), you may need to turn off these options:
135+
To build from the IDE, opt out of any [incompatible options](./asan-known-issues.md#incompatible-options). For an existing project compiled by using **`/Od`** (or Debug mode), you may need to turn off these options:
126136

127137
- Turn off [edit and continue](/visualstudio/debugger/how-to-enable-and-disable-edit-and-continue)
128138
- Turn off [`/RTC1` (runtime checks)](../build/reference/rtc-run-time-error-checks.md)
@@ -140,9 +150,9 @@ To enable the AddressSanitizer for [a CMake project created to target Windows](.
140150

141151
:::image type="content" source="media/asan-cmake-configuration-dropdown.png" alt-text="Screenshot of the CMake configuration dropdown.":::
142152

143-
That selection opens the CMake Project Settings UI, which is saved in a CMakeSettings.json file.
153+
That selection opens the CMake Project Settings editor, which is saved in a CMakeSettings.json file.
144154

145-
1. Choose the **Edit JSON** link in the UI. This selection switches the view to raw JSON.
155+
1. Choose the **Edit JSON** link in the editor. This selection switches the view to raw JSON.
146156

147157
1. Add the property: **“addressSanitizerEnabled”: true**
148158

@@ -172,9 +182,29 @@ Starting with Visual Studio 16.9 you can display **a precisely diagnosed error**
172182

173183
[This new crash dump functionality](./asan-offline-crash-dumps.md) enables cloud-based workflows, or distributed testing. It can also be used to file a detailed, actionable bug in any scenario.
174184

175-
## <a name="error-types"></a> Error types
176-
177-
The MSVC AddressSanitizer implementation can detect many kinds of memory misuse errors. For a non-exhaustive list of errors and links to demonstration examples, see [AddressSanitizer error examples](./asan-error-examples.md).
185+
## <a name="error-types"></a> Example errors
186+
187+
AddressSanitizer can detect several kinds of memory misuse errors. Here are many of the runtime errors reported when you run your binaries compiled by using the AddressSanitizer (**`/fsanitize=address`**) compiler option:
188+
189+
- [`alloc-dealloc-mismatch`](error-alloc-dealloc-mismatch.md)
190+
- [`allocation-size-too-big`](error-allocation-size-too-big.md)
191+
- [`calloc-overflow`](error-calloc-overflow.md)
192+
- [`double-free`](error-double-free.md)
193+
- [`dynamic-stack-buffer-overflow`](error-dynamic-stack-buffer-overflow.md)
194+
- [`global-buffer-overflow`](error-global-buffer-overflow.md)
195+
- [`heap-buffer-overflow`](error-heap-buffer-overflow.md)
196+
- [`heap-use-after-free`](error-heap-use-after-free.md)
197+
- [`invalid-allocation-alignment`](error-invalid-allocation-alignment.md)
198+
- [`memcpy-param-overlap`](error-memcpy-param-overlap.md)
199+
- [`new-delete-type-mismatch`](error-new-delete-type-mismatch.md)
200+
- [`stack-buffer-overflow`](error-stack-buffer-overflow.md)
201+
- [`stack-buffer-underflow`](error-stack-buffer-underflow.md)
202+
- [`stack-use-after-return`](error-stack-use-after-return.md)
203+
- [`stack-use-after-scope`](error-stack-use-after-scope.md)
204+
- [`strncat-param-overlap`](error-strncat-param-overlap.md)
205+
- [`use-after-poison`](error-use-after-poison.md)
206+
207+
For more information about the examples, see [AddressSanitizer error examples](./asan-error-examples.md).
178208

179209
## <a name="differences"></a> Differences with Clang 12.0
180210

docs/sanitizers/error-alloc-dealloc-mismatch.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ helpviewer_keywords: ["alloc-dealloc-mismatch error", "AddressSanitizer error al
99

1010
> Address Sanitizer Error: Mismatch between allocation and deallocation APIs
1111
12-
The `alloc`/`dealloc` mismatch functionality in AddressSanitizer is off by default for Windows. To enable it, run `set ASAN_OPTIONS=alloc_dealloc_mismatch=1` before running the program. This environment variable is checked at runtime to report errors on `malloc`/`delete`, `new`/`free`, and `new`/`delete[]`. Example sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
12+
The `alloc`/`dealloc` mismatch functionality in AddressSanitizer is off by default for Windows. To enable it, run `set ASAN_OPTIONS=alloc_dealloc_mismatch=1` before running the program. This environment variable is checked at runtime to report errors on `malloc`/`delete`, `new`/`free`, and `new`/`delete[]`.
1313

1414
## Example
1515

docs/sanitizers/error-calloc-overflow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ helpviewer_keywords: ["calloc-overflow error", "AddressSanitizer error calloc-ov
99

1010
> Address Sanitizer Error: calloc-overflow
1111
12-
The CRT function [`calloc`](../c-runtime-library/reference/calloc.md) creates an array in memory with elements initialized to 0. The arguments can create an internal error that leads to a NULL pointer as the return value. Example sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
12+
The CRT function [`calloc`](../c-runtime-library/reference/calloc.md) creates an array in memory with elements initialized to 0. The arguments can create an internal error that leads to a NULL pointer as the return value.
1313

1414
## Example
1515

docs/sanitizers/error-double-free.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ helpviewer_keywords: ["double-free error", "AddressSanitizer error double-free"]
1010

1111
> Address Sanitizer Error: Deallocation of freed memory
1212
13-
In C, you can call `free` erroneously. In C++, you can call `delete` more than once. In these examples, we show errors with `delete`, `free`, and `HeapCreate`. Example sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
13+
In C, you can call `free` erroneously. In C++, you can call `delete` more than once. In these examples, we show errors with `delete`, `free`, and `HeapCreate`.
1414

1515
## Example C++ - double `operator delete`
1616

docs/sanitizers/error-dynamic-stack-buffer-overflow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ helpviewer_keywords: ["dynamic-stack-buffer-overflow error", "AddressSanitizer e
99

1010
> Address Sanitizer Error: dynamic-stack-buffer-overflow
1111
12-
This example shows the error that results from a buffer access outside the bounds of a stack-allocated object. Example sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
12+
This example shows the error that results from a buffer access outside the bounds of a stack-allocated object.
1313

1414
## Example - `alloca` overflow (right)
1515

docs/sanitizers/error-global-buffer-overflow.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ helpviewer_keywords: ["global-buffer-overflow error", "AddressSanitizer error gl
1111
1212
The compiler generates metadata for any variable in the `.data` or `.bss` sections. These variables have language scope of global or file static. They're allocated in memory before `main()` starts. Global variables in C are treated much differently than in C++. This difference is because of the complex rules for C++ linking.
1313

14-
In C, a global variable can be declared in several source files, and each definition can have different types. The compiler can't see all the possible definitions at once. However, the linker does see all the different definitions. For C, the linker defaults to selecting the largest-sized variable out of all the different declarations.
14+
In C, a global variable can be declared in several source files, and each definition can have different types. The compiler can't see all the possible definitions at once, but the linker can. For C, the linker defaults to selecting the largest-sized variable out of all the different declarations.
1515

1616
In C++, a global is allocated by the compiler. There can only be one definition, so the size of each definition is known at compile time.
1717

18-
Examples sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
19-
2018
## Example - globals in 'C' with multiple type definitions
2119

2220
```cpp

docs/sanitizers/error-heap-buffer-overflow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ helpviewer_keywords: ["heap-buffer-overflow error", "AddressSanitizer error heap
99

1010
> Address Sanitizer Error: Heap buffer overflow
1111
12-
This example demonstrates the error that results when a memory access occurs outside the bounds of a heap-allocated object. Example sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
12+
This example demonstrates the error that results when a memory access occurs outside the bounds of a heap-allocated object.
1313

1414
## Example - classic heap buffer overflow
1515

docs/sanitizers/error-heap-use-after-free.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ helpviewer_keywords: ["heap-use-after-free error", "AddressSanitizer error heap-
99

1010
> Address Sanitizer Error: Use of deallocated memory
1111
12-
We show three examples where storage in the heap can be allocated via `malloc`, `realloc` (C), and `new` (C++), along with a mistaken use of `volatile`. Examples sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
12+
We show three examples where storage in the heap can be allocated via `malloc`, `realloc` (C), and `new` (C++), along with a mistaken use of `volatile`.
1313

1414
## Example - `malloc`
1515

docs/sanitizers/error-invalid-allocation-alignment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ helpviewer_keywords: ["invalid-allocation-alignment error", "AddressSanitizer er
99

1010
> Address Sanitizer Error: invalid-allocation-alignment
1111
12-
The [`_aligned_malloc`](../c-runtime-library/reference/aligned-malloc.md) function requires a power of 2 for expressing the alignment. We simulate the "external" calculation of some alignment factor using an unoptimized global variable. Example sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
12+
The [`_aligned_malloc`](../c-runtime-library/reference/aligned-malloc.md) function requires a power of 2 for expressing the alignment. We simulate the "external" calculation of some alignment factor using an unoptimized global variable.
1313

1414
## Example
1515

docs/sanitizers/error-memcpy-param-overlap.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ The CRT function [`memcpy`](../c-runtime-library/reference/memcpy-wmemcpy.md) **
1313

1414
A common error is to treat `memmove` as being semantically equivalent to `memcpy`.
1515

16-
Example sourced from [LLVM compiler-rt test suite](https://github.com/llvm/llvm-project/tree/main/compiler-rt/test/asan/TestCases).
17-
1816
## Example
1917

2018
```cpp

0 commit comments

Comments
 (0)