xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerExtraCounters.cpp (revision 9e5787d2284e187abb5b654d924394a65772e004)
1 //===- FuzzerExtraCounters.cpp - Extra coverage counters ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // Extra coverage counters defined by user code.
9 //===----------------------------------------------------------------------===//
10 
11 #include "FuzzerPlatform.h"
12 #include <cstdint>
13 
14 #if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD ||                \
15     LIBFUZZER_OPENBSD || LIBFUZZER_FUCHSIA || LIBFUZZER_EMSCRIPTEN
16 __attribute__((weak)) extern uint8_t __start___libfuzzer_extra_counters;
17 __attribute__((weak)) extern uint8_t __stop___libfuzzer_extra_counters;
18 
19 namespace fuzzer {
20 uint8_t *ExtraCountersBegin() { return &__start___libfuzzer_extra_counters; }
21 uint8_t *ExtraCountersEnd() { return &__stop___libfuzzer_extra_counters; }
22 ATTRIBUTE_NO_SANITIZE_ALL
23 void ClearExtraCounters() {  // hand-written memset, don't asan-ify.
24   uintptr_t *Beg = reinterpret_cast<uintptr_t*>(ExtraCountersBegin());
25   uintptr_t *End = reinterpret_cast<uintptr_t*>(ExtraCountersEnd());
26   for (; Beg < End; Beg++) {
27     *Beg = 0;
28     __asm__ __volatile__("" : : : "memory");
29   }
30 }
31 
32 }  // namespace fuzzer
33 
34 #else
35 // TODO: implement for other platforms.
36 namespace fuzzer {
37 uint8_t *ExtraCountersBegin() { return nullptr; }
38 uint8_t *ExtraCountersEnd() { return nullptr; }
39 void ClearExtraCounters() {}
40 }  // namespace fuzzer
41 
42 #endif
43