FuzzerExtraCounters.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  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. #include "FuzzerPlatform.h"
  11. #include <cstdint>
  12. #if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD || \
  13. LIBFUZZER_FUCHSIA || LIBFUZZER_EMSCRIPTEN
  14. __attribute__((weak)) extern uint8_t __start___libfuzzer_extra_counters;
  15. __attribute__((weak)) extern uint8_t __stop___libfuzzer_extra_counters;
  16. namespace fuzzer {
  17. uint8_t *ExtraCountersBegin() { return &__start___libfuzzer_extra_counters; }
  18. uint8_t *ExtraCountersEnd() { return &__stop___libfuzzer_extra_counters; }
  19. ATTRIBUTE_NO_SANITIZE_ALL
  20. void ClearExtraCounters() { // hand-written memset, don't asan-ify.
  21. uintptr_t *Beg = reinterpret_cast<uintptr_t*>(ExtraCountersBegin());
  22. uintptr_t *End = reinterpret_cast<uintptr_t*>(ExtraCountersEnd());
  23. for (; Beg < End; Beg++) {
  24. *Beg = 0;
  25. __asm__ __volatile__("" : : : "memory");
  26. }
  27. }
  28. } // namespace fuzzer
  29. #endif