Skip to content

Commit e63621a

Browse files
committed
initial add of Custom Allocator section, no inline code
1 parent 9361031 commit e63621a

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎docs/cpp/address-sanitizer.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,31 @@ helpviewer_keywords: ["ASan","sanitizers","AddressSanitizer","clang_rt.asan"]
1010

1111
AddressSanitizer (ASan) is an algorithm that can help developers identify memory safety issues in their C and C++ programs. Developers can use the /fsanitize=address flag to add ASan instrumentation to their native programs. At runtime, the ASan runtime replaces common allocation and memory manipulation functions with instrumented versions. Together, the instrumentation and ASan runtime enable developers to gather useful information about any memory safety issues that are encountered during execution.
1212

13+
14+
## Custom allocators and ASan
15+
16+
ASan provides interceptors for common allocator interfaces, malloc/free, new/delete, HeapAlloc/HeapFree (via RtlAllocateHeap/RtlFreeHeap). Many programs make use of custom allocators for one reason or another. An example would be any program using dlmalloc or a solution using the std::allocator interface and VirtualAlloc. ASan instrumentation does not automatically enlighten these allocators, so it falls to the programmer to 'enlighten' them when using ASan.
17+
18+
### Manual ASan poisoning interface
19+
20+
The interface for enlightening is simple but it imposes alignment restrictions on the user. The interface function prototypes:
21+
`void __asan_poison_memory_region(void const volatile *addr, size_t size);`
22+
`void __asan_unpoison_memory_region(void const volatile *addr, size_t size);`
23+
24+
These functions are not needed when ASan is disabled, the interface header provides wrapper macros that will check whether ASan is enabled during compilation and omit the functions when ASan instrumentation is not enabled. These macros should be used rather than using the above functions directly:
25+
`#define ASAN_POISON_MEMORY_REGION(addr, size)`
26+
`#define ASAN_UNPOISON_MEMORY_REGION(addr, size)`
27+
28+
### Manual ASan poisoning and alignment
29+
30+
ASan poisoning has alignment requirements: the user must add padding such that the padding ends on a byte boundary in the shadow memory. Since each bit in the ASan shadow memory encodes the state of a byte in real memory, this means that **the size + padding for each allocation must align on a 8 byte boundary.** If this requirement is not satisfied it can lead to incorrect bug reporting, including missed and false-positive reports.
31+
32+
See the included examples for a little background. One is a small program to show what can go wrong with manual shadow memory poisoning. The second is an example implementation of manual poising using the `std::allocator` interface.
33+
34+
To build and run the allocator example:
35+
36+
`nmake allocator && allocator.exe`
37+
38+
To build the 'bad unpoisoning' example:
39+
40+
`nmake unpoison && unpoison.exe`

0 commit comments

Comments
 (0)