blob: f69e922cf00420bdbfac08103e68117af3530550 [file] [log] [blame]
kcc1c0379f2017-08-22 01:28:32 +00001//===- FuzzerExtraCounters.cpp - Extra coverage counters ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// Coverage counters from Clang's SourceBasedCodeCoverage.
10//===----------------------------------------------------------------------===//
11
12// Support for SourceBasedCodeCoverage is experimental:
13// * Works only for the main binary, not DSOs yet.
14// * Works only on Linux.
15// * Does not implement print_pcs/print_coverage yet.
16// * Is not fully evaluated for performance and sensitivity.
17// We expect large performance drop due to 64-bit counters,
18// and *maybe* better sensitivity due to more fine-grained counters.
19// Preliminary comparison on a single benchmark (RE2) shows
20// a bit worse sensitivity though.
21
22#include "FuzzerDefs.h"
23
24#if LIBFUZZER_LINUX
25__attribute__((weak)) extern uint64_t __start___llvm_prf_cnts;
26__attribute__((weak)) extern uint64_t __stop___llvm_prf_cnts;
27namespace fuzzer {
28uint64_t *ClangCountersBegin() { return &__start___llvm_prf_cnts; }
29uint64_t *ClangCountersEnd() { return &__stop___llvm_prf_cnts; }
30} // namespace fuzzer
31#else
32// TODO: Implement on Mac (if the data shows it's worth it).
33//__attribute__((visibility("hidden")))
34//extern uint64_t CountersStart __asm("section$start$__DATA$__llvm_prf_cnts");
35//__attribute__((visibility("hidden")))
36//extern uint64_t CountersEnd __asm("section$end$__DATA$__llvm_prf_cnts");
37namespace fuzzer {
38uint64_t *ClangCountersBegin() { return nullptr; }
39uint64_t *ClangCountersEnd() { return nullptr; }
40} // namespace fuzzer
41#endif
42
43namespace fuzzer {
44ATTRIBUTE_NO_SANITIZE_ALL
45void ClearClangCounters() { // hand-written memset, don't asan-ify.
46 for (auto P = ClangCountersBegin(); P < ClangCountersEnd(); P++)
47 *P = 0;
48}
49}