1124271Sgreen//===- FuzzerExtraCountersWindows.cpp - Extra coverage counters for Win32 -===// 2124271Sgreen// 3124271Sgreen// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4124271Sgreen// See https://llvm.org/LICENSE.txt for license information. 5124271Sgreen// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6124271Sgreen// 7124271Sgreen//===----------------------------------------------------------------------===// 8124271Sgreen// Extra coverage counters defined by user code for Windows. 9124271Sgreen//===----------------------------------------------------------------------===// 10124271Sgreen 11124271Sgreen#include "FuzzerPlatform.h" 12124271Sgreen#include <cstdint> 13124271Sgreen 14124271Sgreen#if LIBFUZZER_WINDOWS 15124271Sgreen#include <windows.h> 16124271Sgreen 17124271Sgreennamespace fuzzer { 18124271Sgreen 19124271Sgreen// 20124271Sgreen// The __start___libfuzzer_extra_counters variable is align 16, size 16 to 21124271Sgreen// ensure the padding between it and the next variable in this section (either 22124271Sgreen// __libfuzzer_extra_counters or __stop___libfuzzer_extra_counters) will be 23124271Sgreen// located at (__start___libfuzzer_extra_counters + 24124271Sgreen// sizeof(__start___libfuzzer_extra_counters)). Otherwise, the calculation of 25124271Sgreen// (stop - (start + sizeof(start))) might be skewed. 26124271Sgreen// 27124271Sgreen// The section name, __libfuzzer_extra_countaaa ends with "aaa", so it sorts 28124271Sgreen// before __libfuzzer_extra_counters alphabetically. We want the start symbol to 29124271Sgreen// be placed in the section just before the user supplied counters (if present). 30124271Sgreen// 31124271Sgreen#pragma section(".data$__libfuzzer_extra_countaaa") 32124271SgreenATTRIBUTE_ALIGNED(16) 33124271Sgreen__declspec(allocate(".data$__libfuzzer_extra_countaaa")) uint8_t 34124271Sgreen __start___libfuzzer_extra_counters[16] = {0}; 35124271Sgreen 36124271Sgreen// 37124271Sgreen// Example of what the user-supplied counters should look like. First, the 38124271Sgreen// pragma to create the section name. It will fall alphabetically between 39124271Sgreen// ".data$__libfuzzer_extra_countaaa" and ".data$__libfuzzer_extra_countzzz". 40124271Sgreen// Next, the declspec to allocate the variable inside the specified section. 41158882Sglebius// Finally, some array, struct, whatever that is used to track the counter data. 42124271Sgreen// The size of this variable is computed at runtime by finding the difference of 43158882Sglebius// __stop___libfuzzer_extra_counters and __start___libfuzzer_extra_counters + 44158882Sglebius// sizeof(__start___libfuzzer_extra_counters). 45158882Sglebius// 46158882Sglebius 47124271Sgreen// 48124271Sgreen// #pragma section(".data$__libfuzzer_extra_counters") 49124271Sgreen// __declspec(allocate(".data$__libfuzzer_extra_counters")) 50124271Sgreen// uint8_t any_name_variable[64 * 1024]; 51124271Sgreen// 52124271Sgreen 53124271Sgreen// 54124271Sgreen// Here, the section name, __libfuzzer_extra_countzzz ends with "zzz", so it 55124271Sgreen// sorts after __libfuzzer_extra_counters alphabetically. We want the stop 56124271Sgreen// symbol to be placed in the section just after the user supplied counters (if 57124271Sgreen// present). Align to 1 so there isn't any padding placed between this and the 58124271Sgreen// previous variable. 59124271Sgreen// 60124271Sgreen#pragma section(".data$__libfuzzer_extra_countzzz") 61124271SgreenATTRIBUTE_ALIGNED(1) 62124271Sgreen__declspec(allocate(".data$__libfuzzer_extra_countzzz")) uint8_t 63124271Sgreen __stop___libfuzzer_extra_counters = 0; 64124271Sgreen 65125115Sruuint8_t *ExtraCountersBegin() { 66125115Sru return __start___libfuzzer_extra_counters + 67124271Sgreen sizeof(__start___libfuzzer_extra_counters); 68125011Sru} 69125011Sru 70124271Sgreenuint8_t *ExtraCountersEnd() { return &__stop___libfuzzer_extra_counters; } 71124271Sgreen 72124271SgreenATTRIBUTE_NO_SANITIZE_ALL 73166529Skevlovoid ClearExtraCounters() { 74124271Sgreen uint8_t *Beg = ExtraCountersBegin(); 75124271Sgreen SecureZeroMemory(Beg, ExtraCountersEnd() - Beg); 76124271Sgreen} 77124271Sgreen 78124271Sgreen} // namespace fuzzer 79124271Sgreen 80124271Sgreen#endif 81124271Sgreen