1881faf41SMatt Morehouse //===- FuzzerExtraCountersWindows.cpp - Extra coverage counters for Win32 -===//
2881faf41SMatt Morehouse //
3881faf41SMatt Morehouse // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4881faf41SMatt Morehouse // See https://llvm.org/LICENSE.txt for license information.
5881faf41SMatt Morehouse // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6881faf41SMatt Morehouse //
7881faf41SMatt Morehouse //===----------------------------------------------------------------------===//
8881faf41SMatt Morehouse // Extra coverage counters defined by user code for Windows.
9881faf41SMatt Morehouse //===----------------------------------------------------------------------===//
10881faf41SMatt Morehouse 
11881faf41SMatt Morehouse #include "FuzzerPlatform.h"
12881faf41SMatt Morehouse #include <cstdint>
13881faf41SMatt Morehouse 
14881faf41SMatt Morehouse #if LIBFUZZER_WINDOWS
15*ab737d53SMartin Storsjö #include <windows.h>
16881faf41SMatt Morehouse 
17881faf41SMatt Morehouse namespace fuzzer {
18881faf41SMatt Morehouse 
19881faf41SMatt Morehouse //
20881faf41SMatt Morehouse // The __start___libfuzzer_extra_counters variable is align 16, size 16 to
21881faf41SMatt Morehouse // ensure the padding between it and the next variable in this section (either
22881faf41SMatt Morehouse // __libfuzzer_extra_counters or __stop___libfuzzer_extra_counters) will be
23881faf41SMatt Morehouse // located at (__start___libfuzzer_extra_counters +
24881faf41SMatt Morehouse // sizeof(__start___libfuzzer_extra_counters)). Otherwise, the calculation of
25881faf41SMatt Morehouse // (stop - (start + sizeof(start))) might be skewed.
26881faf41SMatt Morehouse //
27881faf41SMatt Morehouse // The section name, __libfuzzer_extra_countaaa ends with "aaa", so it sorts
28881faf41SMatt Morehouse // before __libfuzzer_extra_counters alphabetically. We want the start symbol to
29881faf41SMatt Morehouse // be placed in the section just before the user supplied counters (if present).
30881faf41SMatt Morehouse //
31881faf41SMatt Morehouse #pragma section(".data$__libfuzzer_extra_countaaa")
32881faf41SMatt Morehouse ATTRIBUTE_ALIGNED(16)
33881faf41SMatt Morehouse __declspec(allocate(".data$__libfuzzer_extra_countaaa")) uint8_t
34881faf41SMatt Morehouse     __start___libfuzzer_extra_counters[16] = {0};
35881faf41SMatt Morehouse 
36881faf41SMatt Morehouse //
37881faf41SMatt Morehouse // Example of what the user-supplied counters should look like. First, the
38881faf41SMatt Morehouse // pragma to create the section name. It will fall alphabetically between
39881faf41SMatt Morehouse // ".data$__libfuzzer_extra_countaaa" and ".data$__libfuzzer_extra_countzzz".
40881faf41SMatt Morehouse // Next, the declspec to allocate the variable inside the specified section.
41881faf41SMatt Morehouse // Finally, some array, struct, whatever that is used to track the counter data.
42881faf41SMatt Morehouse // The size of this variable is computed at runtime by finding the difference of
43881faf41SMatt Morehouse // __stop___libfuzzer_extra_counters and __start___libfuzzer_extra_counters +
44881faf41SMatt Morehouse // sizeof(__start___libfuzzer_extra_counters).
45881faf41SMatt Morehouse //
46881faf41SMatt Morehouse 
47881faf41SMatt Morehouse //
48881faf41SMatt Morehouse //     #pragma section(".data$__libfuzzer_extra_counters")
49881faf41SMatt Morehouse //     __declspec(allocate(".data$__libfuzzer_extra_counters"))
50881faf41SMatt Morehouse //         uint8_t any_name_variable[64 * 1024];
51881faf41SMatt Morehouse //
52881faf41SMatt Morehouse 
53881faf41SMatt Morehouse //
54881faf41SMatt Morehouse // Here, the section name, __libfuzzer_extra_countzzz ends with "zzz", so it
55881faf41SMatt Morehouse // sorts after __libfuzzer_extra_counters alphabetically. We want the stop
56881faf41SMatt Morehouse // symbol to be placed in the section just after the user supplied counters (if
57881faf41SMatt Morehouse // present). Align to 1 so there isn't any padding placed between this and the
58881faf41SMatt Morehouse // previous variable.
59881faf41SMatt Morehouse //
60881faf41SMatt Morehouse #pragma section(".data$__libfuzzer_extra_countzzz")
61881faf41SMatt Morehouse ATTRIBUTE_ALIGNED(1)
62881faf41SMatt Morehouse __declspec(allocate(".data$__libfuzzer_extra_countzzz")) uint8_t
63881faf41SMatt Morehouse     __stop___libfuzzer_extra_counters = 0;
64881faf41SMatt Morehouse 
ExtraCountersBegin()65881faf41SMatt Morehouse uint8_t *ExtraCountersBegin() {
66881faf41SMatt Morehouse   return __start___libfuzzer_extra_counters +
67881faf41SMatt Morehouse          sizeof(__start___libfuzzer_extra_counters);
68881faf41SMatt Morehouse }
69881faf41SMatt Morehouse 
ExtraCountersEnd()70881faf41SMatt Morehouse uint8_t *ExtraCountersEnd() { return &__stop___libfuzzer_extra_counters; }
71881faf41SMatt Morehouse 
72881faf41SMatt Morehouse ATTRIBUTE_NO_SANITIZE_ALL
ClearExtraCounters()73881faf41SMatt Morehouse void ClearExtraCounters() {
74881faf41SMatt Morehouse   uint8_t *Beg = ExtraCountersBegin();
75881faf41SMatt Morehouse   SecureZeroMemory(Beg, ExtraCountersEnd() - Beg);
76881faf41SMatt Morehouse }
77881faf41SMatt Morehouse 
78881faf41SMatt Morehouse } // namespace fuzzer
79881faf41SMatt Morehouse 
80881faf41SMatt Morehouse #endif
81