1 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2 // See https://llvm.org/LICENSE.txt for license information.
3 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 // Make sure the fuzzer eventually finds all possible values of a variable
14 const size_t N
= 1 << 12;
16 // Define an array of counters that will be understood by libFuzzer
17 // as extra coverage signal. The array must be:
19 // * in the section named __libfuzzer_extra_counters.
20 // The target code may declare more than one such array.
22 // Use either `Counters[Idx] = 1` or `Counters[Idx]++;`
23 // depending on whether multiple occurrences of the event 'Idx'
24 // is important to distinguish from one occurrence.
25 #if defined(__linux__) || defined(__FreeBSD__)
26 __attribute__((section("__libfuzzer_extra_counters")))
29 # pragma section(".data$__libfuzzer_extra_counters")
30 __declspec(allocate(".data$__libfuzzer_extra_counters"))
32 static uint8_t Counters
[N
];
34 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data
, size_t Size
) {
35 static std::set
<uint16_t> SeenIdx
;
36 if (Size
!= 4) return 0;
38 memcpy(&Idx
, Data
, 4);
40 assert(Counters
[Idx
] == 0); // libFuzzer should reset these between the runs.
41 // Or Counters[Idx]=1 if we don't care how many times this happened.
44 if (SeenIdx
.size() == N
) {
45 fprintf(stderr
, "BINGO: found all values\n");