Skip to content
Snippets Groups Projects
  • Alexander Potapenko's avatar
    1956004b
    [sanitizer-coverage] Change cmp instrumentation to distinguish const operands · 1956004b
    Alexander Potapenko authored
    This implementation of SanitizerCoverage instrumentation inserts different
    callbacks depending on constantness of operands:
    
      1. If both operands are non-const, then a usual
         __sanitizer_cov_trace_cmp[1248] call is inserted.
      2. If exactly one operand is const, then a
         __sanitizer_cov_trace_const_cmp[1248] call is inserted. The first
         argument of the call is always the constant one.
      3. If both operands are const, then no callback is inserted.
    
    This separation comes useful in fuzzing when tasks like "find one operand
    of the comparison in input arguments and replace it with the other one"
    have to be done. The new instrumentation allows us to not waste time on
    searching the constant operands in the input.
    
    Patch by Victor Chibotaru.
    
    
    git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310600 91177308-0d34-0410-b5e6-96231b3b80d8
    1956004b
    History
    [sanitizer-coverage] Change cmp instrumentation to distinguish const operands
    Alexander Potapenko authored
    This implementation of SanitizerCoverage instrumentation inserts different
    callbacks depending on constantness of operands:
    
      1. If both operands are non-const, then a usual
         __sanitizer_cov_trace_cmp[1248] call is inserted.
      2. If exactly one operand is const, then a
         __sanitizer_cov_trace_const_cmp[1248] call is inserted. The first
         argument of the call is always the constant one.
      3. If both operands are const, then no callback is inserted.
    
    This separation comes useful in fuzzing when tasks like "find one operand
    of the comparison in input arguments and replace it with the other one"
    have to be done. The new instrumentation allows us to not waste time on
    searching the constant operands in the input.
    
    Patch by Victor Chibotaru.
    
    
    git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310600 91177308-0d34-0410-b5e6-96231b3b80d8
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
SanitizerCoverage.rst 11.69 KiB

SanitizerCoverage

Introduction

LLVM has a simple code coverage instrumentation built in (SanitizerCoverage). It inserts calls to user-defined functions on function-, basic-block-, and edge- levels. Default implementations of those callbacks are provided and implement simple coverage reporting and visualization, however if you need just coverage visualization you may want to use :doc:`SourceBasedCodeCoverage <SourceBasedCodeCoverage>` instead.

Tracing PCs with guards

With -fsanitize-coverage=trace-pc-guard the compiler will insert the following code on every edge:

__sanitizer_cov_trace_pc_guard(&guard_variable)

Every edge will have its own guard_variable (uint32_t).

The compler will also insert calls to a module constructor:

// The guards are [start, stop).
// This function will be called at least once per DSO and may be called
// more than once with the same values of start/stop.
__sanitizer_cov_trace_pc_guard_init(uint32_t *start, uint32_t *stop);

With an additional ...=trace-pc,indirect-calls flag __sanitizer_cov_trace_pc_indirect(void *callee) will be inserted on every indirect call.

The functions __sanitizer_cov_trace_pc_* should be defined by the user.

Example:

// trace-pc-guard-cb.cc
#include <stdint.h>
#include <stdio.h>
#include <sanitizer/coverage_interface.h>

// This callback is inserted by the compiler as a module constructor
// into every DSO. 'start' and 'stop' correspond to the
// beginning and end of the section with the guards for the entire
// binary (executable or DSO). The callback will be called at least
// once per DSO and may be called multiple times with the same parameters.
extern "C" void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
                                                    uint32_t *stop) {
  static uint64_t N;  // Counter for the guards.
  if (start == stop || *start) return;  // Initialize only once.
  printf("INIT: %p %p\n", start, stop);
  for (uint32_t *x = start; x < stop; x++)
    *x = ++N;  // Guards should start from 1.
}

// This callback is inserted by the compiler on every edge in the
// control flow (some optimizations apply).
// Typically, the compiler will emit the code like this:
//    if(*guard)
//      __sanitizer_cov_trace_pc_guard(guard);
// But for large functions it will emit a simple call:
//    __sanitizer_cov_trace_pc_guard(guard);
extern "C" void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
  if (!*guard) return;  // Duplicate the guard check.
  // If you set *guard to 0 this code will not be called again for this edge.
  // Now you can get the PC and do whatever you want:
  //   store it somewhere or symbolize it and print right away.
  // The values of `*guard` are as you set them in
  // __sanitizer_cov_trace_pc_guard_init and so you can make them consecutive
  // and use them to dereference an array or a bit vector.
  void *PC = __builtin_return_address(0);
  char PcDescr[1024];
  // This function is a part of the sanitizer run-time.
  // To use it, link with AddressSanitizer or other sanitizer.
  __sanitizer_symbolize_pc(PC, "%p %F %L", PcDescr, sizeof(PcDescr));
  printf("guard: %p %x PC %s\n", guard, *guard, PcDescr);
}
// trace-pc-guard-example.cc
void foo() { }
int main(int argc, char **argv) {
  if (argc > 1) foo();
}
clang++ -g  -fsanitize-coverage=trace-pc-guard trace-pc-guard-example.cc -c
clang++ trace-pc-guard-cb.cc trace-pc-guard-example.o -fsanitize=address
ASAN_OPTIONS=strip_path_prefix=`pwd`/ ./a.out
INIT: 0x71bcd0 0x71bce0
guard: 0x71bcd4 2 PC 0x4ecd5b in main trace-pc-guard-example.cc:2
guard: 0x71bcd8 3 PC 0x4ecd9e in main trace-pc-guard-example.cc:3:7
ASAN_OPTIONS=strip_path_prefix=`pwd`/ ./a.out with-foo
INIT: 0x71bcd0 0x71bce0
guard: 0x71bcd4 2 PC 0x4ecd5b in main trace-pc-guard-example.cc:3
guard: 0x71bcdc 4 PC 0x4ecdc7 in main trace-pc-guard-example.cc:4:17
guard: 0x71bcd0 1 PC 0x4ecd20 in foo() trace-pc-guard-example.cc:2:14

Tracing PCs

With -fsanitize-coverage=trace-pc the compiler will insert __sanitizer_cov_trace_pc() on every edge. With an additional ...=trace-pc,indirect-calls flag __sanitizer_cov_trace_pc_indirect(void *callee) will be inserted on every indirect call. These callbacks are not implemented in the Sanitizer run-time and should be defined by the user. This mechanism is used for fuzzing the Linux kernel (https://github.com/google/syzkaller).

Instrumentation points

Sanitizer Coverage offers different levels of instrumentation.

  • edge (default): edges are instrumented (see below).
  • bb: basic blocks are instrumented.
  • func: only the entry block of every function will be instrumented.

Use these flags together with trace-pc-guard or trace-pc, like this: -fsanitize-coverage=func,trace-pc-guard.

When edge or bb is used, some of the edges/blocks may still be left uninstrumented (pruned) if such instrumentation is considered redundant. Use no-prune (e.g. -fsanitize-coverage=bb,no-prune,trace-pc-guard) to disable pruning. This could be useful for better coverage visualization.