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