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