Skip to content

Commit 0b3425f

Browse files
Steve WishnouskyColin Robertsonttorble
authored
Add container-overflow ASAN error page (#4226)
* Update known issues section on standard library support to mention existing vector annotation support. * Write up container-overflow page and put error/support information there instead. Known issues only mentions that the support is partial but still useful now without annotations. * Add example image * Use standard "for more info" form * Edits and Acrolinx fixes * Update docs/sanitizers/error-container-overflow.md Co-authored-by: Colin Robertson <[email protected]> Co-authored-by: Tracey Torble <[email protected]>
1 parent 829e952 commit 0b3425f

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

docs/sanitizers/asan-error-examples.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Each error example provides source code and compilation instructions for a comma
2020

2121
- [Error: `calloc-overflow`](./error-calloc-overflow.md)
2222

23+
- [Error: `container-overflow`](./error-container-overflow.md)
24+
2325
- [Error: `double-free`](./error-double-free.md)
2426

2527
- [Error: `dynamic-stack-buffer-overflow`](./error-dynamic-stack-buffer-overflow.md)

docs/sanitizers/asan-known-issues.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "AddressSanitizer known issues"
33
description: "Technical description of the AddressSanitizer for Microsoft C/C++ known issues."
4-
ms.date: 03/02/2021
4+
ms.date: 04/15/2022
55
helpviewer_keywords: ["AddressSanitizer known issues"]
66
---
77

@@ -26,9 +26,11 @@ These options and functionality are incompatible with [`/fsanitize=address`](../
2626

2727
## Standard library support
2828

29-
The MSVC standard library (STL) isn't enlightened to understand the AddressSanitizer. AddressSanitizer exceptions raised in STL code do identify true bugs. However, they aren't as precise as they could be.
29+
The MSVC standard library (STL) is partially enlightened to understand the AddressSanitizer and provide additional checks. For more information, see [container-overflow error](./error-container-overflow.md).
3030

31-
This example demonstrates the lack of precision:
31+
When annotations are disabled or in versions without support, AddressSanitizer exceptions raised in STL code do still identify true bugs. However, they aren't as precise as they could be.
32+
33+
This example demonstrates the lack of precision and the benefits of enabling annotations:
3234

3335
```cpp
3436
// Compile with: cl /fsanitize=address /Zi
@@ -39,12 +41,14 @@ int main() {
3941
std::vector<int> v(10);
4042
v.reserve(20);
4143

42-
// Currently, MSVC ASan does NOT raise an exception here.
44+
// In versions prior to 17.2, MSVC ASan does NOT raise an exception here.
4345
// While this is an out-of-bounds write to 'v', MSVC ASan
4446
// ensures the write is within the heap allocation size (20).
47+
// With 17.2 and later, MSVC ASan will raise a 'container-overflow' exception:
48+
// ==18364==ERROR: AddressSanitizer: container-overflow on address 0x1263cb8a0048 at pc 0x7ff6466411ab bp 0x005cf81ef7b0 sp 0x005cf81ef7b8
4549
v[10] = 1;
4650

47-
// MSVC ASan DOES raise an exception here, as this write
51+
// Regardless of version, MSVC ASan DOES raise an exception here, as this write
4852
// is out of bounds from the heap allocation.
4953
v[20] = 1;
5054
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: "Error: container-overflow"
3+
description: "Source examples and live debug screenshots for container overflow errors."
4+
ms.date: 04/15/2022
5+
f1_keywords: ["container-overflow", "mismatch detected for 'annotate_vector'", "_DISABLE_VECTOR_ANNOTATION"]
6+
helpviewer_keywords: ["container-overflow error", "AddressSanitizer error container-overflow", "mismatch detected for 'annotate_vector'", "_DISABLE_VECTOR_ANNOTATION"]
7+
---
8+
9+
# Error: `container-overflow`
10+
11+
> Address Sanitizer Error: Container overflow
12+
13+
In Visual Studio 2022 version 17.2 and later, the MSVC standard library (STL) is partially enlightened to understand the AddressSanitizer. The following container types have inserted extra annotations to detect memory access issues:
14+
15+
| Standard container type | Disable annotations macro | Supported in version |
16+
|--|--|--|
17+
| `std::vector` | `_DISABLE_VECTOR_ANNOTATION` | Visual Studio 2022 17.2 |
18+
19+
When a standard type has annotations enabled, to avoid one-definition-rule (ODR) violations, each static library and object used to link the binary must also enable those annotations. Effectively, you must build those static libraries and objects with AddressSanitizer enabled. Mixing code with different annotation settings causes an error:
20+
21+
```Output
22+
my_static.lib(my_code.obj) : error LNK2038: mismatch detected for 'annotate_vector': value '0' doesn't match value '1' in main.obj
23+
```
24+
25+
To resolve this error, either disable annotations in all projects that use the corresponding macro, or build each project by using **`/fsanitize=address`** and annotations enabled. (Annotations are enabled by default.)
26+
27+
## Example: Access reserved memory in a `std::vector`
28+
29+
```cpp
30+
// Compile with: cl /EHsc /fsanitize=address /Zi
31+
#include <vector>
32+
33+
int main() {
34+
// Create a vector of size 10, but with a capacity of 20.
35+
std::vector<int> v(10);
36+
v.reserve(20);
37+
38+
// In versions prior to 17.2, MSVC ASan does NOT raise an exception here.
39+
// While this is an out-of-bounds write to 'v', MSVC ASan
40+
// ensures the write is within the heap allocation size (20).
41+
// With 17.2 and later, MSVC ASan will raise a 'container-overflow' exception:
42+
// ==18364==ERROR: AddressSanitizer: container-overflow on address 0x1263cb8a0048 at pc 0x7ff6466411ab bp 0x005cf81ef7b0 sp 0x005cf81ef7b8
43+
v[10] = 1;
44+
45+
// Regardless of version, MSVC ASan DOES raise an exception here, as this write
46+
// is out of bounds from the heap allocation.
47+
v[20] = 1;
48+
}
49+
```
50+
51+
To build and test this example, run the following commands in a Visual Studio 2022 version 17.2 or later [Developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts) window:
52+
53+
```cmd
54+
cl /EHsc example1.cpp /fsanitize=address /Zi
55+
devenv /debugexe example1.exe
56+
```
57+
58+
### Error result of reserved memory access in a `std::vector`
59+
60+
:::image type="content" source="media/container-overflow-example-1.png" alt-text="Screenshot of debugger displaying container-overflow error in example 1." lightbox="media/container-overflow-example-1.png":::
61+
62+
## See also
63+
64+
[AddressSanitizer overview](./asan.md)\
65+
[AddressSanitizer known issues](./asan-known-issues.md)\
66+
[AddressSanitizer build and language reference](./asan-building.md)\
67+
[AddressSanitizer runtime reference](./asan-runtime.md)\
68+
[AddressSanitizer shadow bytes](./asan-shadow-bytes.md)\
69+
[AddressSanitizer cloud or distributed testing](./asan-offline-crash-dumps.md)\
70+
[AddressSanitizer debugger integration](./asan-debugger-integration.md)\
71+
[AddressSanitizer error examples](./asan-error-examples.md)
Loading

docs/sanitizers/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ items:
2727
href: ../sanitizers/error-allocation-size-too-big.md
2828
- name: "calloc-overflow error"
2929
href: ../sanitizers/error-calloc-overflow.md
30+
- name: "container-overflow error"
31+
href: ../sanitizers/error-container-overflow.md
3032
- name: "double-free error"
3133
href: ../sanitizers/error-double-free.md
3234
- name: "dynamic-stack-buffer-overflow error"

0 commit comments

Comments
 (0)