Skip to content

Commit 866d199

Browse files
authored
Merge pull request #4985 from branh/branh-asan-docs-update
Update ASAN documentation
2 parents 43c5bba + c521b56 commit 866d199

6 files changed

+19
-10
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ int main(int argc, char **argv) {
126126
case 'g': return global[one * 11]; //Boom! simple global
127127
case 'c': return C::array[one * 11]; //Boom! class static
128128
case 'f':
129-
static int array[10];
130-
memset(array, 0, 10);
129+
{
130+
static int array[10] = {};
131131
return array[one * 11]; //Boom! function static
132+
}
132133
case 'l':
133134
// literal global ptr created by compiler
134135
const char *str = "0123456789";

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ int main(int argc, char **argv) {
3535
To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts):
3636
3737
```cmd
38-
cl example1.cpp /fsanitize=address /Zi
38+
cl example1.cpp /fsanitize=address /Zi /Oi
3939
devenv /debugexe example1.exe
4040
```
4141

42+
The [/Oi flag](../build/reference/oi-generate-intrinsic-functions.md) tells the compiler to treat `memcpy` and `memmove` as intrinsic functions. This is necessary because some versions of the standard library implement `memcpy` and `memmove` in the same way. Because ASAN is a dynamic analysis tool, it only detects errors with an observable runtime effect.
43+
4244
### Resulting error
4345

4446
:::image type="content" source="media/memcpy-param-overlap-example-1.png" alt-text="Screenshot of debugger displaying memcpy-param-overlap error in example 1.":::

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public:
8686

8787
class Child : public Parent {
8888
public:
89-
int extra_field;
89+
volatile int extra_field;
9090
};
9191

9292
int main(void) {
@@ -95,7 +95,7 @@ int main(void) {
9595
Child *c = (Child*)&p;
9696
c->extra_field = 42; // Boom !
9797

98-
return 0;
98+
return (c->extra_field == 42);
9999
}
100100
```
101101

docs/sanitizers/error-stack-buffer-underflow.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ int main() {
3131
To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts):
3232

3333
```cmd
34-
cl example1.cpp /fsanitize=address /Zi
34+
cl example1.cpp /fsanitize=address /Zi /Od
3535
devenv /debugexe example1.exe
3636
```
3737

38+
ASAN is a form of dynamic analysis, which means it can only detect bad code that is actually executed. An optimizer will remove the assignment to `buffer[subscript]` because `buffer[subscript]` is never read from. As a result, this example requires the `/Od` flag.
39+
3840
### Resulting error
3941

4042
:::image type="content" source="media/stack-buffer-underflow-example-1.png" alt-text="Screenshot of debugger displaying stack-buffer-underflow error in example 1.":::

docs/sanitizers/error-stack-use-after-return.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This check can slow your application down substantially. Consider the [Clang sum
2121
```cpp
2222
// example1.cpp
2323
// stack-use-after-return error
24-
char* x;
24+
volatile char* x;
2525

2626
void foo() {
2727
char stack_buffer[42];
@@ -33,7 +33,7 @@ int main() {
3333
foo();
3434
*x = 42; // Boom!
3535

36-
return 0;
36+
return (*x == 42);
3737
}
3838
```
3939

@@ -96,11 +96,13 @@ int main(int argc, char* argv[]) {
9696
To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts):
9797
9898
```cmd
99-
cl example2.cpp /fsanitize=address /fsanitize-address-use-after-return /Zi
99+
cl example2.cpp /fsanitize=address /fsanitize-address-use-after-return /Zi /Od
100100
set ASAN_OPTIONS=detect_stack_use_after_return=1
101101
devenv /debugexe example2.exe 1
102102
```
103103

104+
ASAN is a form of dynamic analysis, which means it can only detect bad code that is actually executed. An optimizer may determine that the value of `t[100 + Idx]` or `sink` is never used and elide the assignment. As a result, this example requires the `/Od` flag.
105+
104106
### Resulting error - C++ and templates
105107

106108
:::image type="content" source="media/stack-use-after-return-example-2.png" alt-text="Screenshot of debugger displaying stack-use-after-return error in example 2.":::

docs/sanitizers/error-stack-use-after-scope.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,12 @@ void main() {
147147
To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later [developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts):
148148
149149
```cmd
150-
cl example4.cpp /EHsc /fsanitize=address /Zi
150+
cl example4.cpp /EHsc /fsanitize=address /Zi /Od
151151
devenv /debugexe example4.exe
152152
```
153153

154+
ASAN is a form of dynamic analysis, which means it can only detect bad code that is actually executed. An optimizer may propagate the value of `v` in these cases instead of reading from the address stored in `p`. As a result, this example requires the `/Od` flag.
155+
154156
### Resulting error - temporaries
155157

156158
:::image type="content" source="media/stack-use-after-scope-example-4.png" alt-text="Screenshot of debugger displaying stack-use-after-scope error in example 4.":::

0 commit comments

Comments
 (0)