Sergey Matveev | f4633ac | 2013-05-14 15:48:54 +0000 | [diff] [blame] | 1 | ================ |
| 2 | LeakSanitizer |
| 3 | ================ |
| 4 | |
| 5 | .. contents:: |
| 6 | :local: |
| 7 | |
| 8 | Introduction |
| 9 | ============ |
| 10 | |
Sergey Matveev | 072db59 | 2013-12-10 20:10:30 +0000 | [diff] [blame] | 11 | LeakSanitizer is a run-time memory leak detector. It can be combined with |
Alexey Samsonov | 710d9f8 | 2016-01-22 01:35:45 +0000 | [diff] [blame] | 12 | :doc:`AddressSanitizer` to get both memory error and leak detection, or |
| 13 | used in a stand-alone mode. LSan adds almost no performance overhead |
| 14 | until the very end of the process, at which point there is an extra leak |
| 15 | detection phase. |
Sergey Matveev | f4633ac | 2013-05-14 15:48:54 +0000 | [diff] [blame] | 16 | |
Alexey Samsonov | 710d9f8 | 2016-01-22 01:35:45 +0000 | [diff] [blame] | 17 | Usage |
| 18 | ===== |
Sergey Matveev | f4633ac | 2013-05-14 15:48:54 +0000 | [diff] [blame] | 19 | |
Alexey Samsonov | 710d9f8 | 2016-01-22 01:35:45 +0000 | [diff] [blame] | 20 | LeakSanitizer is only supported on x86\_64 Linux. In order to use it, |
| 21 | simply build your program with :doc:`AddressSanitizer`: |
Sergey Matveev | 072db59 | 2013-12-10 20:10:30 +0000 | [diff] [blame] | 22 | |
Alexey Samsonov | 710d9f8 | 2016-01-22 01:35:45 +0000 | [diff] [blame] | 23 | .. code-block:: console |
Sergey Matveev | 072db59 | 2013-12-10 20:10:30 +0000 | [diff] [blame] | 24 | |
Alexey Samsonov | 710d9f8 | 2016-01-22 01:35:45 +0000 | [diff] [blame] | 25 | $ cat memory-leak.c |
| 26 | #include <stdlib.h> |
| 27 | void *p; |
| 28 | int main() { |
| 29 | p = malloc(7); |
| 30 | p = 0; // The memory is leaked here. |
| 31 | return 0; |
| 32 | } |
| 33 | % clang -fsanitize=address -g memory-leak.c ; ./a.out |
| 34 | ==23646==ERROR: LeakSanitizer: detected memory leaks |
| 35 | Direct leak of 7 byte(s) in 1 object(s) allocated from: |
| 36 | #0 0x4af01b in __interceptor_malloc /projects/compiler-rt/lib/asan/asan_malloc_linux.cc:52:3 |
| 37 | #1 0x4da26a in main memory-leak.c:4:7 |
| 38 | #2 0x7f076fd9cec4 in __libc_start_main libc-start.c:287 |
| 39 | SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s). |
| 40 | |
| 41 | To use LeakSanitizer in stand-alone mode, link your program with |
| 42 | ``-fsanitize=leak`` flag. Make sure to use ``clang`` (not ``ld``) for the |
| 43 | link step, so that it would link in proper LeakSanitizer run-time library |
| 44 | into the final executable. |
Sergey Matveev | f4633ac | 2013-05-14 15:48:54 +0000 | [diff] [blame] | 45 | |
| 46 | More Information |
| 47 | ================ |
| 48 | |
Alexey Samsonov | 2e2469d | 2015-12-04 00:38:13 +0000 | [diff] [blame] | 49 | `<https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer>`_ |