Skip to content

Commit e5c8755

Browse files
committed
further top level edits
1 parent 17f374f commit e5c8755

File tree

3 files changed

+122
-94
lines changed

3 files changed

+122
-94
lines changed

docs/cpp/ASAN/asan-jims-bone-yard.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
## STUFF TO KEEP -- remove later!!!!!
3+
4+
## Viewing Address Saniziter Errors ##
5+
6+
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+
void main()
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.
45+
46+
![Command line: alloca](media\CommandLine-alloca.png)
47+
48+
From top to bottom
49+
50+
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+
![IDE: povray](media\povray.png)
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.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Offline Address Sanitizer crash dumps
2+
3+
## Enable Cloud and Distributed testing
4+
5+
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.
16+
17+
## Example
18+
19+
## Command line build and run
20+
21+
## Viewing in Visual Studio
22+

docs/cpp/ASAN/asan-top-level.md

Lines changed: 24 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ The Address Sanitizer is a compiler and runtime runtime [introduced by Google](h
2424

2525
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.
2626

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:
2828

29-
- On-premises testing
29+
- On-premise single machine or distributed testing
3030
- Cloud based workflows for testing
3131

3232
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**]().
3737

3838
After installing you can build your executables with the `-fsanitize=address`compiler switch using any of the following:
3939

40-
- a command line
40+
- Command line
4141
- Visual Studio project system
4242
- Visual Studio Cmake make integration
4343

@@ -53,7 +53,6 @@ Microsoft recommends using the Address Sanitizer in these **three standard workf
5353
- Visual Studio - [Project system](#Using-the-Address-Sanitizer-from-Visual-Studio)
5454
- Visual Studio - [CMake]([CMake](#Using-the-Address-Sanitizer-from-Visual-Studio:-CMake))
5555

56-
5756
- **CI/CD** - continuous integration / continuous development
5857
- Error reporting - [New Address Sanitizer dump files]()
5958

@@ -67,9 +66,9 @@ This article will cover the information needed to enable the three workflows lis
6766
6867
## Using the Address Sanitizer from a Developer Command Prompt
6968

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`.
7170

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).
7372

7473
### Example - basic global buffer overflow:
7574

@@ -157,17 +156,15 @@ The following screen shot captures the error from the CMake build.
157156
158157
![cmake-F5-runt](.\MEDIA\asan-cmake-F5-error.PNG)
159158
160-
## Offline Address Sanitizer crash dumps
159+
## Address Sanitizer crash dumps - cloud and distributed workflows
161160
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:
163162
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"`
165164
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.
167166
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.
171168
172169
## Error types
173170
@@ -191,14 +188,23 @@ The following list of runtime errors can be exposed when you run your binaries c
191188
- [use-after-poison](.\examples-use-after-poison.md)
192189
- [alloc-dealloc-mismatch](.\examples-alloc-dealloc-mismatch.md)
193190
194-
## Differences with Clang
191+
## Differences with Clang 12.0
195192
196193
We differ in two functional areas:
197194
198195
- **stack-use-after-scope** - this is on by default and can't be turned off.
199196
- **stack-use-after-return** - this is not available by just setting ASAN_OPTIONS
200197
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)
203+
- [Intra Object Overflow](https://github.com/google/sanitizers/wiki/AddressSanitizerIntraObjectOverflow)
204+
- [Container Overflow](https://github.com/google/sanitizers/wiki/AddressSanitizerContainerOverflow)
205+
- [Pointer Subtraction/Comparison](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)
206+
207+
See [Building for the Address Sanitizer with MSVC](.\asan-building.md) for further details.
202208
203209
## Existing industry documentation
204210
@@ -210,90 +216,14 @@ Extensive documentation already exists for these language and platform dependent
210216
211217
This seminal paper on the [Address Sanitizer](https://www.usenix.org/system/files/conference/atc12/atc12-final39.pdf) describes the implementation.
212218
213-
!!! is this where we should mention that we don't implement the entire complete feature set? !!!
214-
215219
## See also
216220
217-
- [Building for the Address Sanitizer with MSVC](.\asan-building.md)
221+
- [Building for the Address Sanitizer with Visual Studio](.\asan-building.md)
218222
219223
- [Address Sanitizer runtime](.\address-sanitizer-runtime.md)
220224
221225
These structure all further details into the tools and the run times they target.
222226
223227
----
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.
268-
269-
![Command line: alloca](media\CommandLine-alloca.png)
270-
271-
From top to bottom
272-
273-
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-
![IDE: povray](media\povray.png)
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.
228+
----
229+
----

0 commit comments

Comments
 (0)