FuzzerExtraCountersWindows.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //===- FuzzerExtraCountersWindows.cpp - Extra coverage counters for Win32 -===//
  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 for Windows.
  9. //===----------------------------------------------------------------------===//
  10. #include "FuzzerPlatform.h"
  11. #include <cstdint>
  12. #if LIBFUZZER_WINDOWS
  13. #include <windows.h>
  14. namespace fuzzer {
  15. //
  16. // The __start___libfuzzer_extra_counters variable is align 16, size 16 to
  17. // ensure the padding between it and the next variable in this section (either
  18. // __libfuzzer_extra_counters or __stop___libfuzzer_extra_counters) will be
  19. // located at (__start___libfuzzer_extra_counters +
  20. // sizeof(__start___libfuzzer_extra_counters)). Otherwise, the calculation of
  21. // (stop - (start + sizeof(start))) might be skewed.
  22. //
  23. // The section name, __libfuzzer_extra_countaaa ends with "aaa", so it sorts
  24. // before __libfuzzer_extra_counters alphabetically. We want the start symbol to
  25. // be placed in the section just before the user supplied counters (if present).
  26. //
  27. #pragma section(".data$__libfuzzer_extra_countaaa")
  28. ATTRIBUTE_ALIGNED(16)
  29. __declspec(allocate(".data$__libfuzzer_extra_countaaa")) uint8_t
  30. __start___libfuzzer_extra_counters[16] = {0};
  31. //
  32. // Example of what the user-supplied counters should look like. First, the
  33. // pragma to create the section name. It will fall alphabetically between
  34. // ".data$__libfuzzer_extra_countaaa" and ".data$__libfuzzer_extra_countzzz".
  35. // Next, the declspec to allocate the variable inside the specified section.
  36. // Finally, some array, struct, whatever that is used to track the counter data.
  37. // The size of this variable is computed at runtime by finding the difference of
  38. // __stop___libfuzzer_extra_counters and __start___libfuzzer_extra_counters +
  39. // sizeof(__start___libfuzzer_extra_counters).
  40. //
  41. //
  42. // #pragma section(".data$__libfuzzer_extra_counters")
  43. // __declspec(allocate(".data$__libfuzzer_extra_counters"))
  44. // uint8_t any_name_variable[64 * 1024];
  45. //
  46. //
  47. // Here, the section name, __libfuzzer_extra_countzzz ends with "zzz", so it
  48. // sorts after __libfuzzer_extra_counters alphabetically. We want the stop
  49. // symbol to be placed in the section just after the user supplied counters (if
  50. // present). Align to 1 so there isn't any padding placed between this and the
  51. // previous variable.
  52. //
  53. #pragma section(".data$__libfuzzer_extra_countzzz")
  54. ATTRIBUTE_ALIGNED(1)
  55. __declspec(allocate(".data$__libfuzzer_extra_countzzz")) uint8_t
  56. __stop___libfuzzer_extra_counters = 0;
  57. uint8_t *ExtraCountersBegin() {
  58. return __start___libfuzzer_extra_counters +
  59. sizeof(__start___libfuzzer_extra_counters);
  60. }
  61. uint8_t *ExtraCountersEnd() { return &__stop___libfuzzer_extra_counters; }
  62. ATTRIBUTE_NO_SANITIZE_ALL
  63. void ClearExtraCounters() {
  64. uint8_t *Beg = ExtraCountersBegin();
  65. SecureZeroMemory(Beg, ExtraCountersEnd() - Beg);
  66. }
  67. } // namespace fuzzer
  68. #endif