Path: blob/main/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerExtraCountersWindows.cpp
35262 views
//===- FuzzerExtraCountersWindows.cpp - Extra coverage counters for Win32 -===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// Extra coverage counters defined by user code for Windows.8//===----------------------------------------------------------------------===//910#include "FuzzerPlatform.h"11#include <cstdint>1213#if LIBFUZZER_WINDOWS14#include <windows.h>1516namespace fuzzer {1718//19// The __start___libfuzzer_extra_counters variable is align 16, size 16 to20// ensure the padding between it and the next variable in this section (either21// __libfuzzer_extra_counters or __stop___libfuzzer_extra_counters) will be22// located at (__start___libfuzzer_extra_counters +23// sizeof(__start___libfuzzer_extra_counters)). Otherwise, the calculation of24// (stop - (start + sizeof(start))) might be skewed.25//26// The section name, __libfuzzer_extra_countaaa ends with "aaa", so it sorts27// before __libfuzzer_extra_counters alphabetically. We want the start symbol to28// be placed in the section just before the user supplied counters (if present).29//30#pragma section(".data$__libfuzzer_extra_countaaa")31ATTRIBUTE_ALIGNED(16)32__declspec(allocate(".data$__libfuzzer_extra_countaaa")) uint8_t33__start___libfuzzer_extra_counters[16] = {0};3435//36// Example of what the user-supplied counters should look like. First, the37// pragma to create the section name. It will fall alphabetically between38// ".data$__libfuzzer_extra_countaaa" and ".data$__libfuzzer_extra_countzzz".39// Next, the declspec to allocate the variable inside the specified section.40// Finally, some array, struct, whatever that is used to track the counter data.41// The size of this variable is computed at runtime by finding the difference of42// __stop___libfuzzer_extra_counters and __start___libfuzzer_extra_counters +43// sizeof(__start___libfuzzer_extra_counters).44//4546//47// #pragma section(".data$__libfuzzer_extra_counters")48// __declspec(allocate(".data$__libfuzzer_extra_counters"))49// uint8_t any_name_variable[64 * 1024];50//5152//53// Here, the section name, __libfuzzer_extra_countzzz ends with "zzz", so it54// sorts after __libfuzzer_extra_counters alphabetically. We want the stop55// symbol to be placed in the section just after the user supplied counters (if56// present). Align to 1 so there isn't any padding placed between this and the57// previous variable.58//59#pragma section(".data$__libfuzzer_extra_countzzz")60ATTRIBUTE_ALIGNED(1)61__declspec(allocate(".data$__libfuzzer_extra_countzzz")) uint8_t62__stop___libfuzzer_extra_counters = 0;6364uint8_t *ExtraCountersBegin() {65return __start___libfuzzer_extra_counters +66sizeof(__start___libfuzzer_extra_counters);67}6869uint8_t *ExtraCountersEnd() { return &__stop___libfuzzer_extra_counters; }7071ATTRIBUTE_NO_SANITIZE_ALL72void ClearExtraCounters() {73uint8_t *Beg = ExtraCountersBegin();74SecureZeroMemory(Beg, ExtraCountersEnd() - Beg);75}7677} // namespace fuzzer7879#endif808182