blob: 3e8102a12f6755221a199969de68eaaba241726e [file] [log] [blame]
Sergey Matveev33e32242015-04-23 21:29:37 +00001=================
Sergey Matveev07e2d282015-04-23 20:40:04 +00002SanitizerCoverage
Sergey Matveev33e32242015-04-23 21:29:37 +00003=================
Sergey Matveev07e2d282015-04-23 20:40:04 +00004
5.. contents::
6 :local:
7
8Introduction
9============
10
11Sanitizer tools have a very simple code coverage tool built in. It allows to
12get function-level, basic-block-level, and edge-level coverage at a very low
13cost.
14
15How to build and run
16====================
17
18SanitizerCoverage can be used with :doc:`AddressSanitizer`,
Evgeniy Stepanov5b49eb42016-06-14 21:33:40 +000019:doc:`LeakSanitizer`, :doc:`MemorySanitizer`,
20UndefinedBehaviorSanitizer, or without any sanitizer. Pass one of the
21following compile-time flags:
Sergey Matveev07e2d282015-04-23 20:40:04 +000022
Alexey Samsonov8fffba12015-05-07 23:04:19 +000023* ``-fsanitize-coverage=func`` for function-level coverage (very fast).
24* ``-fsanitize-coverage=bb`` for basic-block-level coverage (may add up to 30%
Sergey Matveev07e2d282015-04-23 20:40:04 +000025 **extra** slowdown).
Alexey Samsonov8fffba12015-05-07 23:04:19 +000026* ``-fsanitize-coverage=edge`` for edge-level coverage (up to 40% slowdown).
Sergey Matveev07e2d282015-04-23 20:40:04 +000027
Alexey Samsonov8fffba12015-05-07 23:04:19 +000028You may also specify ``-fsanitize-coverage=indirect-calls`` for
29additional `caller-callee coverage`_.
Sergey Matveev07e2d282015-04-23 20:40:04 +000030
Evgeniy Stepanov5b49eb42016-06-14 21:33:40 +000031At run time, pass ``coverage=1`` in ``ASAN_OPTIONS``,
32``LSAN_OPTIONS``, ``MSAN_OPTIONS`` or ``UBSAN_OPTIONS``, as
33appropriate. For the standalone coverage mode, use ``UBSAN_OPTIONS``.
Alexey Samsonov8fffba12015-05-07 23:04:19 +000034
35To get `Coverage counters`_, add ``-fsanitize-coverage=8bit-counters``
Sergey Matveev07e2d282015-04-23 20:40:04 +000036to one of the above compile-time flags. At runtime, use
37``*SAN_OPTIONS=coverage=1:coverage_counters=1``.
38
39Example:
40
41.. code-block:: console
42
43 % cat -n cov.cc
44 1 #include <stdio.h>
45 2 __attribute__((noinline))
46 3 void foo() { printf("foo\n"); }
47 4
48 5 int main(int argc, char **argv) {
49 6 if (argc == 2)
50 7 foo();
51 8 printf("main\n");
52 9 }
Alexey Samsonov8fffba12015-05-07 23:04:19 +000053 % clang++ -g cov.cc -fsanitize=address -fsanitize-coverage=func
Sergey Matveev07e2d282015-04-23 20:40:04 +000054 % ASAN_OPTIONS=coverage=1 ./a.out; ls -l *sancov
55 main
56 -rw-r----- 1 kcc eng 4 Nov 27 12:21 a.out.22673.sancov
57 % ASAN_OPTIONS=coverage=1 ./a.out foo ; ls -l *sancov
58 foo
59 main
60 -rw-r----- 1 kcc eng 4 Nov 27 12:21 a.out.22673.sancov
61 -rw-r----- 1 kcc eng 8 Nov 27 12:21 a.out.22679.sancov
62
63Every time you run an executable instrumented with SanitizerCoverage
64one ``*.sancov`` file is created during the process shutdown.
65If the executable is dynamically linked against instrumented DSOs,
66one ``*.sancov`` file will be also created for every DSO.
67
68Postprocessing
69==============
70
71The format of ``*.sancov`` files is very simple: the first 8 bytes is the magic,
72one of ``0xC0BFFFFFFFFFFF64`` and ``0xC0BFFFFFFFFFFF32``. The last byte of the
73magic defines the size of the following offsets. The rest of the data is the
74offsets in the corresponding binary/DSO that were executed during the run.
75
76A simple script
77``$LLVM/projects/compiler-rt/lib/sanitizer_common/scripts/sancov.py`` is
78provided to dump these offsets.
79
80.. code-block:: console
81
82 % sancov.py print a.out.22679.sancov a.out.22673.sancov
83 sancov.py: read 2 PCs from a.out.22679.sancov
84 sancov.py: read 1 PCs from a.out.22673.sancov
85 sancov.py: 2 files merged; 2 PCs total
86 0x465250
87 0x4652a0
88
89You can then filter the output of ``sancov.py`` through ``addr2line --exe
90ObjectFile`` or ``llvm-symbolizer --obj ObjectFile`` to get file names and line
91numbers:
92
93.. code-block:: console
94
95 % sancov.py print a.out.22679.sancov a.out.22673.sancov 2> /dev/null | llvm-symbolizer --obj a.out
96 cov.cc:3
97 cov.cc:5
98
Mike Aizatsky3828cbb2016-01-27 23:56:12 +000099Sancov Tool
100===========
101
102A new experimental ``sancov`` tool is developed to process coverage files.
103The tool is part of LLVM project and is currently supported only on Linux.
Mike Aizatskya731ee32016-02-12 00:29:45 +0000104It can handle symbolization tasks autonomously without any extra support
105from the environment. You need to pass .sancov files (named
106``<module_name>.<pid>.sancov`` and paths to all corresponding binary elf files.
107Sancov matches these files using module names and binaries file names.
Mike Aizatsky3828cbb2016-01-27 23:56:12 +0000108
109.. code-block:: console
110
Mike Aizatskya731ee32016-02-12 00:29:45 +0000111 USAGE: sancov [options] <action> (<binary file>|<.sancov file>)...
Mike Aizatsky3828cbb2016-01-27 23:56:12 +0000112
113 Action (required)
114 -print - Print coverage addresses
Sylvestre Ledrube8f3962016-02-14 20:20:58 +0000115 -covered-functions - Print all covered functions.
116 -not-covered-functions - Print all not covered functions.
Mike Aizatskya675e0e2016-09-30 21:02:56 +0000117 -symbolize - Symbolizes the report.
Mike Aizatsky3828cbb2016-01-27 23:56:12 +0000118
119 Options
120 -blacklist=<string> - Blacklist file (sanitizer blacklist format).
121 -demangle - Print demangled function name.
Mike Aizatsky3828cbb2016-01-27 23:56:12 +0000122 -strip_path_prefix=<string> - Strip this prefix from file paths in reports
123
124
Mike Aizatskya675e0e2016-09-30 21:02:56 +0000125Coverage Reports (Experimental)
Mike Aizatsky3828cbb2016-01-27 23:56:12 +0000126================================
127
Mike Aizatskya675e0e2016-09-30 21:02:56 +0000128``.sancov`` files do not contain enough information to generate a source-level
129coverage report. The missing information is contained
130in debug info of the binary. Thus the ``.sancov`` has to be symbolized
131to produce a ``.symcov`` file first:
132
133.. code-block:: console
Kostya Serebryanyf74169c2016-09-30 21:57:10 +0000134
Mike Aizatskya675e0e2016-09-30 21:02:56 +0000135 sancov -symbolize my_program.123.sancov my_program > my_program.123.symcov
136
137The ``.symcov`` file can be browsed overlayed over the source code by
Mike Aizatskya271d1a2016-10-04 19:19:16 +0000138running ``tools/sancov/coverage-report-server.py`` script that will start
Mike Aizatskya675e0e2016-09-30 21:02:56 +0000139an HTTP server.
Mike Aizatsky3828cbb2016-01-27 23:56:12 +0000140
141
Sergey Matveev07e2d282015-04-23 20:40:04 +0000142How good is the coverage?
143=========================
144
Sergey Matveevea558e02015-05-06 21:09:00 +0000145It is possible to find out which PCs are not covered, by subtracting the covered
146set from the set of all instrumented PCs. The latter can be obtained by listing
147all callsites of ``__sanitizer_cov()`` in the binary. On Linux, ``sancov.py``
148can do this for you. Just supply the path to binary and a list of covered PCs:
Sergey Matveev07e2d282015-04-23 20:40:04 +0000149
150.. code-block:: console
151
Sergey Matveevea558e02015-05-06 21:09:00 +0000152 % sancov.py print a.out.12345.sancov > covered.txt
153 sancov.py: read 2 64-bit PCs from a.out.12345.sancov
154 sancov.py: 1 file merged; 2 PCs total
155 % sancov.py missing a.out < covered.txt
156 sancov.py: found 3 instrumented PCs in a.out
157 sancov.py: read 2 PCs from stdin
158 sancov.py: 1 PCs missing from coverage
159 0x4cc61c
Sergey Matveev07e2d282015-04-23 20:40:04 +0000160
161Edge coverage
162=============
163
164Consider this code:
165
166.. code-block:: c++
167
168 void foo(int *a) {
169 if (a)
170 *a = 0;
171 }
172
173It contains 3 basic blocks, let's name them A, B, C:
174
175.. code-block:: none
176
177 A
178 |\
179 | \
180 | B
181 | /
182 |/
183 C
184
185If blocks A, B, and C are all covered we know for certain that the edges A=>B
186and B=>C were executed, but we still don't know if the edge A=>C was executed.
187Such edges of control flow graph are called
188`critical <http://en.wikipedia.org/wiki/Control_flow_graph#Special_edges>`_. The
Alexey Samsonov8fffba12015-05-07 23:04:19 +0000189edge-level coverage (``-fsanitize-coverage=edge``) simply splits all critical
190edges by introducing new dummy blocks and then instruments those blocks:
Sergey Matveev07e2d282015-04-23 20:40:04 +0000191
192.. code-block:: none
193
194 A
195 |\
196 | \
197 D B
198 | /
199 |/
200 C
201
202Bitset
203======
204
205When ``coverage_bitset=1`` run-time flag is given, the coverage will also be
206dumped as a bitset (text file with 1 for blocks that have been executed and 0
207for blocks that were not).
208
209.. code-block:: console
210
Alexey Samsonov8fffba12015-05-07 23:04:19 +0000211 % clang++ -fsanitize=address -fsanitize-coverage=edge cov.cc
Sergey Matveev07e2d282015-04-23 20:40:04 +0000212 % ASAN_OPTIONS="coverage=1:coverage_bitset=1" ./a.out
213 main
214 % ASAN_OPTIONS="coverage=1:coverage_bitset=1" ./a.out 1
215 foo
216 main
217 % head *bitset*
218 ==> a.out.38214.bitset-sancov <==
219 01101
220 ==> a.out.6128.bitset-sancov <==
221 11011%
222
223For a given executable the length of the bitset is always the same (well,
224unless dlopen/dlclose come into play), so the bitset coverage can be
225easily used for bitset-based corpus distillation.
226
227Caller-callee coverage
228======================
229
230(Experimental!)
231Every indirect function call is instrumented with a run-time function call that
232captures caller and callee. At the shutdown time the process dumps a separate
233file called ``caller-callee.PID.sancov`` which contains caller/callee pairs as
234pairs of lines (odd lines are callers, even lines are callees)
235
236.. code-block:: console
237
238 a.out 0x4a2e0c
239 a.out 0x4a6510
240 a.out 0x4a2e0c
241 a.out 0x4a87f0
242
243Current limitations:
244
245* Only the first 14 callees for every caller are recorded, the rest are silently
246 ignored.
247* The output format is not very compact since caller and callee may reside in
248 different modules and we need to spell out the module names.
249* The routine that dumps the output is not optimized for speed
250* Only Linux x86_64 is tested so far.
251* Sandboxes are not supported.
252
253Coverage counters
254=================
255
256This experimental feature is inspired by
Aaron Ballman0f6f82a32016-02-22 13:09:36 +0000257`AFL <http://lcamtuf.coredump.cx/afl/technical_details.txt>`__'s coverage
Sergey Matveev07e2d282015-04-23 20:40:04 +0000258instrumentation. With additional compile-time and run-time flags you can get
259more sensitive coverage information. In addition to boolean values assigned to
260every basic block (edge) the instrumentation will collect imprecise counters.
261On exit, every counter will be mapped to a 8-bit bitset representing counter
262ranges: ``1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+`` and those 8-bit bitsets will
263be dumped to disk.
264
265.. code-block:: console
266
Alexey Samsonov8fffba12015-05-07 23:04:19 +0000267 % clang++ -g cov.cc -fsanitize=address -fsanitize-coverage=edge,8bit-counters
Sergey Matveev07e2d282015-04-23 20:40:04 +0000268 % ASAN_OPTIONS="coverage=1:coverage_counters=1" ./a.out
269 % ls -l *counters-sancov
270 ... a.out.17110.counters-sancov
271 % xxd *counters-sancov
272 0000000: 0001 0100 01
273
274These counters may also be used for in-process coverage-guided fuzzers. See
275``include/sanitizer/coverage_interface.h``:
276
277.. code-block:: c++
278
279 // The coverage instrumentation may optionally provide imprecise counters.
280 // Rather than exposing the counter values to the user we instead map
281 // the counters to a bitset.
282 // Every counter is associated with 8 bits in the bitset.
283 // We define 8 value ranges: 1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+
284 // The i-th bit is set to 1 if the counter value is in the i-th range.
285 // This counter-based coverage implementation is *not* thread-safe.
286
287 // Returns the number of registered coverage counters.
288 uintptr_t __sanitizer_get_number_of_counters();
289 // Updates the counter 'bitset', clears the counters and returns the number of
290 // new bits in 'bitset'.
291 // If 'bitset' is nullptr, only clears the counters.
292 // Otherwise 'bitset' should be at least
293 // __sanitizer_get_number_of_counters bytes long and 8-aligned.
294 uintptr_t
295 __sanitizer_update_counter_bitset_and_clear_counters(uint8_t *bitset);
296
Kostya Serebryany5ce81792015-12-02 02:08:26 +0000297Tracing basic blocks
298====================
Kostya Serebryany64537862016-04-18 21:28:37 +0000299Experimental support for basic block (or edge) tracing.
Kostya Serebryany5ce81792015-12-02 02:08:26 +0000300With ``-fsanitize-coverage=trace-bb`` the compiler will insert
301``__sanitizer_cov_trace_basic_block(s32 *id)`` before every function, basic block, or edge
302(depending on the value of ``-fsanitize-coverage=[func,bb,edge]``).
Kostya Serebryany64537862016-04-18 21:28:37 +0000303Example:
304
305.. code-block:: console
306
307 % clang -g -fsanitize=address -fsanitize-coverage=edge,trace-bb foo.cc
308 % ASAN_OPTIONS=coverage=1 ./a.out
309
310This will produce two files after the process exit:
311`trace-points.PID.sancov` and `trace-events.PID.sancov`.
312The first file will contain a textual description of all the instrumented points in the program
313in the form that you can feed into llvm-symbolizer (e.g. `a.out 0x4dca89`), one per line.
314The second file will contain the actual execution trace as a sequence of 4-byte integers
315-- these integers are the indices into the array of instrumented points (the first file).
316
317Basic block tracing is currently supported only for single-threaded applications.
318
Kostya Serebryany5ce81792015-12-02 02:08:26 +0000319
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000320Tracing PCs
321===========
322*Experimental* feature similar to tracing basic blocks, but with a different API.
Kostya Serebryany52e86492016-02-18 00:49:23 +0000323With ``-fsanitize-coverage=trace-pc`` the compiler will insert
324``__sanitizer_cov_trace_pc()`` on every edge.
325With an additional ``...=trace-pc,indirect-calls`` flag
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000326``__sanitizer_cov_trace_pc_indirect(void *callee)`` will be inserted on every indirect call.
327These callbacks are not implemented in the Sanitizer run-time and should be defined
Kostya Serebryany52e86492016-02-18 00:49:23 +0000328by the user. So, these flags do not require the other sanitizer to be used.
329This mechanism is used for fuzzing the Linux kernel (https://github.com/google/syzkaller)
Aaron Ballman0f6f82a32016-02-22 13:09:36 +0000330and can be used with `AFL <http://lcamtuf.coredump.cx/afl>`__.
Kostya Serebryanyd4590c72016-02-17 21:34:43 +0000331
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000332Tracing PCs with guards
333=======================
Kostya Serebryany66a9c172016-09-15 22:11:08 +0000334Another *experimental* feature that tries to combine the functionality of `trace-pc`,
335`8bit-counters` and boolean coverage.
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000336
337With ``-fsanitize-coverage=trace-pc-guard`` the compiler will insert the following code
338on every edge:
339
340.. code-block:: none
341
Kostya Serebryany8e781a82016-09-18 04:52:23 +0000342 if (guard_variable)
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000343 __sanitizer_cov_trace_pc_guard(&guard_variable)
344
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000345Every edge will have its own `guard_variable` (uint32_t).
Kostya Serebryany66a9c172016-09-15 22:11:08 +0000346
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000347The compler will also insert a module constructor that will call
348
349.. code-block:: c++
350
Kostya Serebryany8ad41552016-09-17 05:03:05 +0000351 // The guards are [start, stop).
352 // This function may be called multiple times with the same values of start/stop.
Kostya Serebryany6bb54982016-09-29 18:34:40 +0000353 __sanitizer_cov_trace_pc_guard_init(uint32_t *start, uint32_t *stop);
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000354
Kostya Serebryany8ad41552016-09-17 05:03:05 +0000355Similarly to `trace-pc,indirect-calls`, with `trace-pc-guards,indirect-calls`
356``__sanitizer_cov_trace_pc_indirect(void *callee)`` will be inserted on every indirect call.
357
358The functions `__sanitizer_cov_trace_pc_*` should be defined by the user.
Kostya Serebryany60cdd612016-09-14 01:39:49 +0000359
Kostya Serebryanyd6ae22a2016-09-29 18:58:17 +0000360Example:
361
362.. code-block:: c++
363
364 // trace-pc-guard-cb.cc
365 #include <stdint.h>
366 #include <stdio.h>
367 #include <sanitizer/coverage_interface.h>
368
369 // This callback is inserted by the compiler as a module constructor
370 // into every compilation unit. 'start' and 'stop' correspond to the
371 // beginning and end of the section with the guards for the entire
372 // binary (executable or DSO) and so it will be called multiple times
373 // with the same parameters.
374 extern "C" void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
375 uint32_t *stop) {
376 static uint64_t N; // Counter for the guards.
377 if (start == stop || *start) return; // Initialize only once.
378 printf("INIT: %p %p\n", start, stop);
379 for (uint32_t *x = start; x < stop; x++)
380 *x = ++N; // Guards should start from 1.
381 }
382
383 // This callback is inserted by the compiler on every edge in the
384 // control flow (some optimizations apply).
385 // Typically, the compiler will emit the code like this:
386 // if(*guard)
387 // __sanitizer_cov_trace_pc_guard(guard);
388 // But for large functions it will emit a simple call:
389 // __sanitizer_cov_trace_pc_guard(guard);
390 extern "C" void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
391 if (!*guard) return; // Duplicate the guard check.
392 // If you set *guard to 0 this code will not be called again for this edge.
393 // Now you can get the PC and do whatever you want:
394 // store it somewhere or symbolize it and print right away.
395 // The values of `*guard` are as you set them in
Kostya Serebryany851cb982016-09-29 19:06:09 +0000396 // __sanitizer_cov_trace_pc_guard_init and so you can make them consecutive
Kostya Serebryanyd6ae22a2016-09-29 18:58:17 +0000397 // and use them to dereference an array or a bit vector.
398 void *PC = __builtin_return_address(0);
399 char PcDescr[1024];
400 // This function is a part of the sanitizer run-time.
401 // To use it, link with AddressSanitizer or other sanitizer.
402 __sanitizer_symbolize_pc(PC, "%p %F %L", PcDescr, sizeof(PcDescr));
403 printf("guard: %p %x PC %s\n", guard, *guard, PcDescr);
404 }
405
406.. code-block:: c++
407
408 // trace-pc-guard-example.cc
409 void foo() { }
410 int main(int argc, char **argv) {
411 if (argc > 1) foo();
412 }
413
414.. code-block:: console
415
416 clang++ -g -fsanitize-coverage=trace-pc-guard trace-pc-guard-example.cc -c
417 clang++ trace-pc-guard-cb.cc trace-pc-guard-example.o -fsanitize=address
418 ASAN_OPTIONS=strip_path_prefix=`pwd`/ ./a.out
419
420.. code-block:: console
421
422 INIT: 0x71bcd0 0x71bce0
423 guard: 0x71bcd4 2 PC 0x4ecd5b in main trace-pc-guard-example.cc:2
424 guard: 0x71bcd8 3 PC 0x4ecd9e in main trace-pc-guard-example.cc:3:7
425
Kostya Serebryany851cb982016-09-29 19:06:09 +0000426.. code-block:: console
427
428 ASAN_OPTIONS=strip_path_prefix=`pwd`/ ./a.out with-foo
429
430
431.. code-block:: console
432
433 INIT: 0x71bcd0 0x71bce0
434 guard: 0x71bcd4 2 PC 0x4ecd5b in main trace-pc-guard-example.cc:3
435 guard: 0x71bcdc 4 PC 0x4ecdc7 in main trace-pc-guard-example.cc:4:17
436 guard: 0x71bcd0 1 PC 0x4ecd20 in foo() trace-pc-guard-example.cc:2:14
437
Kostya Serebryanyd6ae22a2016-09-29 18:58:17 +0000438
Kostya Serebryanyb17e2982015-07-31 21:48:10 +0000439Tracing data flow
440=================
441
Kostya Serebryany3b419712016-08-30 01:27:03 +0000442Support for data-flow-guided fuzzing.
Kostya Serebryanyb17e2982015-07-31 21:48:10 +0000443With ``-fsanitize-coverage=trace-cmp`` the compiler will insert extra instrumentation
444around comparison instructions and switch statements.
Kostya Serebryany3b419712016-08-30 01:27:03 +0000445Similarly, with ``-fsanitize-coverage=trace-div`` the compiler will instrument
446integer division instructions (to capture the right argument of division)
447and with ``-fsanitize-coverage=trace-gep`` --
448the `LLVM GEP instructions <http://llvm.org/docs/GetElementPtr.html>`_
449(to capture array indices).
Kostya Serebryanyb17e2982015-07-31 21:48:10 +0000450
451.. code-block:: c++
452
453 // Called before a comparison instruction.
Kostya Serebryanyb17e2982015-07-31 21:48:10 +0000454 // Arg1 and Arg2 are arguments of the comparison.
Kostya Serebryany070bcb02016-08-18 01:26:36 +0000455 void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2);
456 void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2);
457 void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2);
458 void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2);
Kostya Serebryanyb17e2982015-07-31 21:48:10 +0000459
460 // Called before a switch statement.
461 // Val is the switch operand.
462 // Cases[0] is the number of case constants.
463 // Cases[1] is the size of Val in bits.
464 // Cases[2:] are the case constants.
465 void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases);
466
Kostya Serebryany3b419712016-08-30 01:27:03 +0000467 // Called before a division statement.
468 // Val is the second argument of division.
469 void __sanitizer_cov_trace_div4(uint32_t Val);
470 void __sanitizer_cov_trace_div8(uint64_t Val);
471
472 // Called before a GetElemementPtr (GEP) instruction
473 // for every non-constant array index.
474 void __sanitizer_cov_trace_gep(uintptr_t Idx);
475
476
Kostya Serebryanyb17e2982015-07-31 21:48:10 +0000477This interface is a subject to change.
Kostya Serebryanya94e6e72015-11-30 22:17:19 +0000478The current implementation is not thread-safe and thus can be safely used only for single-threaded targets.
Kostya Serebryanyb17e2982015-07-31 21:48:10 +0000479
Sergey Matveev07e2d282015-04-23 20:40:04 +0000480Output directory
481================
482
483By default, .sancov files are created in the current working directory.
484This can be changed with ``ASAN_OPTIONS=coverage_dir=/path``:
485
486.. code-block:: console
487
488 % ASAN_OPTIONS="coverage=1:coverage_dir=/tmp/cov" ./a.out foo
489 % ls -l /tmp/cov/*sancov
490 -rw-r----- 1 kcc eng 4 Nov 27 12:21 a.out.22673.sancov
491 -rw-r----- 1 kcc eng 8 Nov 27 12:21 a.out.22679.sancov
492
493Sudden death
494============
495
496Normally, coverage data is collected in memory and saved to disk when the
497program exits (with an ``atexit()`` handler), when a SIGSEGV is caught, or when
498``__sanitizer_cov_dump()`` is called.
499
500If the program ends with a signal that ASan does not handle (or can not handle
501at all, like SIGKILL), coverage data will be lost. This is a big problem on
502Android, where SIGKILL is a normal way of evicting applications from memory.
503
504With ``ASAN_OPTIONS=coverage=1:coverage_direct=1`` coverage data is written to a
505memory-mapped file as soon as it collected.
506
507.. code-block:: console
508
509 % ASAN_OPTIONS="coverage=1:coverage_direct=1" ./a.out
510 main
511 % ls
512 7036.sancov.map 7036.sancov.raw a.out
513 % sancov.py rawunpack 7036.sancov.raw
514 sancov.py: reading map 7036.sancov.map
515 sancov.py: unpacking 7036.sancov.raw
516 writing 1 PCs to a.out.7036.sancov
517 % sancov.py print a.out.7036.sancov
518 sancov.py: read 1 PCs from a.out.7036.sancov
519 sancov.py: 1 files merged; 1 PCs total
520 0x4b2bae
521
522Note that on 64-bit platforms, this method writes 2x more data than the default,
523because it stores full PC values instead of 32-bit offsets.
524
525In-process fuzzing
526==================
527
528Coverage data could be useful for fuzzers and sometimes it is preferable to run
529a fuzzer in the same process as the code being fuzzed (in-process fuzzer).
530
531You can use ``__sanitizer_get_total_unique_coverage()`` from
532``<sanitizer/coverage_interface.h>`` which returns the number of currently
533covered entities in the program. This will tell the fuzzer if the coverage has
534increased after testing every new input.
535
536If a fuzzer finds a bug in the ASan run, you will need to save the reproducer
537before exiting the process. Use ``__asan_set_death_callback`` from
538``<sanitizer/asan_interface.h>`` to do that.
539
540An example of such fuzzer can be found in `the LLVM tree
541<http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/README.txt?view=markup>`_.
542
543Performance
544===========
545
546This coverage implementation is **fast**. With function-level coverage
Alexey Samsonov8fffba12015-05-07 23:04:19 +0000547(``-fsanitize-coverage=func``) the overhead is not measurable. With
548basic-block-level coverage (``-fsanitize-coverage=bb``) the overhead varies
Sergey Matveev07e2d282015-04-23 20:40:04 +0000549between 0 and 25%.
550
551============== ========= ========= ========= ========= ========= =========
552 benchmark cov0 cov1 diff 0-1 cov2 diff 0-2 diff 1-2
553============== ========= ========= ========= ========= ========= =========
554 400.perlbench 1296.00 1307.00 1.01 1465.00 1.13 1.12
555 401.bzip2 858.00 854.00 1.00 1010.00 1.18 1.18
556 403.gcc 613.00 617.00 1.01 683.00 1.11 1.11
557 429.mcf 605.00 582.00 0.96 610.00 1.01 1.05
558 445.gobmk 896.00 880.00 0.98 1050.00 1.17 1.19
559 456.hmmer 892.00 892.00 1.00 918.00 1.03 1.03
560 458.sjeng 995.00 1009.00 1.01 1217.00 1.22 1.21
561462.libquantum 497.00 492.00 0.99 534.00 1.07 1.09
562 464.h264ref 1461.00 1467.00 1.00 1543.00 1.06 1.05
563 471.omnetpp 575.00 590.00 1.03 660.00 1.15 1.12
564 473.astar 658.00 652.00 0.99 715.00 1.09 1.10
565 483.xalancbmk 471.00 491.00 1.04 582.00 1.24 1.19
566 433.milc 616.00 627.00 1.02 627.00 1.02 1.00
567 444.namd 602.00 601.00 1.00 654.00 1.09 1.09
568 447.dealII 630.00 634.00 1.01 653.00 1.04 1.03
569 450.soplex 365.00 368.00 1.01 395.00 1.08 1.07
570 453.povray 427.00 434.00 1.02 495.00 1.16 1.14
571 470.lbm 357.00 375.00 1.05 370.00 1.04 0.99
572 482.sphinx3 927.00 928.00 1.00 1000.00 1.08 1.08
573============== ========= ========= ========= ========= ========= =========
574
575Why another coverage?
576=====================
577
578Why did we implement yet another code coverage?
579 * We needed something that is lightning fast, plays well with
580 AddressSanitizer, and does not significantly increase the binary size.
581 * Traditional coverage implementations based in global counters
582 `suffer from contention on counters
583 <https://groups.google.com/forum/#!topic/llvm-dev/cDqYgnxNEhY>`_.