AddressSanitizer (ASan) is a compiler and runtime technology built into MSVC that exposes hard-to-find memory safety bugs with zero false positives. Starting in Visual Studio 2019 version 16.9, ASan is fully integrated into the Visual Studio IDE, the MSBuild project system, and CMake workflows. You compile your code with a single extra flag, run your program normally, and ASan reports precisely diagnosed errors directly in the debugger.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt
Use this file to discover all available pages before exploring further.
How ASan Works
ASan instruments memory references — loads, stores, allocations, scopes, and CRT functions — during compilation. At runtime, a shadow memory region tracks the state of every byte of addressable memory. When your program accesses memory it shouldn’t, ASan intercepts the operation, prints a detailed report showing the exact source file and line, and terminates the process (or triggers a Visual Studio exception).ASan is not recommended for production builds. Use it during development, testing, and CI pipelines. It is compatible with all optimization levels (
/Od, /O1, /O2, /O2 /GL) and CRT variants (/MD, /MDd, /MT, /MTd), but is incompatible with /ZI (Edit and Continue), /RTC, and /INCREMENTAL linking.Enabling ASan
- Command Line
- MSBuild / Visual Studio
- CMake
- Linux / Clang
Compile with The linker option
/fsanitize=address from a Developer Command Prompt. Include /Zi for full call stack symbolization:/INFERASANLIBS (enabled by default) automatically links the correct clang_rt.asan* runtime libraries. No extra linker flags are required for most projects.Heap Buffer Overflow Example
The following example demonstrates a classic heap buffer overflow and the ASan output you will see:Types of Errors Detected
Heap Errors
heap-buffer-overflow— write/read past allocated heap memoryheap-use-after-free— access memory afterfree()double-free— callingfree()twice on the same pointeralloc-dealloc-mismatch— mismatched allocator/deallocator (e.g.,malloc/delete)allocation-size-too-big— allocation request exceeds addressable memory
Stack Errors
stack-buffer-overflow/stack-buffer-underflow— write past a stack arraystack-use-after-return— use a pointer to a local after the function returns (requires/fsanitize-address-use-after-return)stack-use-after-scope— access a local variable outside its enclosing blockdynamic-stack-buffer-overflow— overflow of a VLA orallocabuffer
Global Errors
global-buffer-overflow— write past a global array
Other Errors
calloc-overflow— integer overflow incallocsize computationnew-delete-type-mismatch—new Tfollowed bydelete[]memcpy-param-overlap/strncat-param-overlap— overlapping buffers in CRT functionsuse-after-poison— access to manually poisoned memory regions
ASan Runtime Options (ASAN_OPTIONS)
Control ASan behavior at runtime through the ASAN_OPTIONS environment variable. Set it before launching your program:
Key ASAN_OPTIONS flags
Key ASAN_OPTIONS flags
| Flag | Default | Description |
|---|---|---|
detect_stack_use_after_return | false | Detect use-after-return bugs. Requires /fsanitize-address-use-after-return at compile time. |
malloc_context_size | 1 | Number of stack frames to record per allocation. Increase for deeper call stacks. |
quarantine_size_mb | -1 | Size (MB) of quarantine buffer for use-after-free detection. |
abort_on_error | false | Call abort() instead of _exit() after reporting an error. |
continue_on_error | 0 | 1 = report errors and continue (output to stdout); 2 = report errors and continue (output to stderr). |
verbosity | 0 | Increase to 1, 2, or 3 for more diagnostic output. |
alloc_dealloc_mismatch | false | Enable detection of mismatched alloc/dealloc pairs. |
windows_fast_fail_on_error | false | Terminate via __fastfail(71) after reporting an error. |
__asan_default_options() function in your code:
Debugger Integration in Visual Studio
When your ASan-instrumented binary is launched under the Visual Studio debugger (F5), ASan exceptions are surfaced as first-chance exceptions in the IDE:Exception dialog appears
Visual Studio shows an Exception Thrown window identifying the ASan error type (e.g.,
AddressSanitizer Error: Heap buffer overflow).Source highlighting
The editor navigates to the exact source line that caused the memory error, with the call stack displayed in the Call Stack window.
Inspect variables
Use the Locals, Watch, and Memory windows to inspect the state of your program at the point of the error.
Selectively Disabling ASan
Use__declspec(no_sanitize_address) to exempt specific functions or variables from instrumentation:
Known Limitations
Linker Reference
The/INFERASANLIBS linker option (on by default) automatically selects and links the correct ASan runtime library based on your CRT choice:
| CRT Option | Linked Libraries |
|---|---|
/MT or /MTd | clang_rt.asan_dynamic-{arch}.lib + /wholearchive:clang_rt.asan_static_runtime_thunk-{arch}.lib |
/MD or /MDd | clang_rt.asan_dynamic-{arch}.lib + /wholearchive:clang_rt.asan_dynamic_runtime_thunk-{arch}.lib |
/INFERASANLIBS:NO if you need to provide ASan library paths manually in your build scripts.