1*c239eda8SKostya Serebryany // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2*c239eda8SKostya Serebryany // See https://llvm.org/LICENSE.txt for license information.
3*c239eda8SKostya Serebryany // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4*c239eda8SKostya Serebryany
5*c239eda8SKostya Serebryany // Simple test for a fuzzer.
6*c239eda8SKostya Serebryany // This is a sample fuzz target for a custom serialization format that uses
7*c239eda8SKostya Serebryany // a magic separator to split the input into several independent buffers.
8*c239eda8SKostya Serebryany // The fuzzer must find the input consisting of 2 subinputs: "Fuzz" and "me".
9*c239eda8SKostya Serebryany #include <cassert>
10*c239eda8SKostya Serebryany #include <cstdint>
11*c239eda8SKostya Serebryany #include <cstdio>
12*c239eda8SKostya Serebryany #include <cstdlib>
13*c239eda8SKostya Serebryany #include <cstring>
14*c239eda8SKostya Serebryany
15*c239eda8SKostya Serebryany #include <algorithm>
16*c239eda8SKostya Serebryany #include <vector>
17*c239eda8SKostya Serebryany
18*c239eda8SKostya Serebryany // Splits [data,data+size) into a vector of strings using a "magic" Separator.
SplitInput(const uint8_t * Data,size_t Size,const uint8_t * Separator,size_t SeparatorSize)19*c239eda8SKostya Serebryany std::vector<std::vector<uint8_t>> SplitInput(const uint8_t *Data, size_t Size,
20*c239eda8SKostya Serebryany const uint8_t *Separator,
21*c239eda8SKostya Serebryany size_t SeparatorSize) {
22*c239eda8SKostya Serebryany std::vector<std::vector<uint8_t>> Res;
23*c239eda8SKostya Serebryany assert(SeparatorSize > 0);
24*c239eda8SKostya Serebryany auto Beg = Data;
25*c239eda8SKostya Serebryany auto End = Data + Size;
26*c239eda8SKostya Serebryany // Using memmem here. std::search may be harder for libFuzzer today.
27*c239eda8SKostya Serebryany while (const uint8_t *Pos = (const uint8_t *)memmem(Beg, End - Beg,
28*c239eda8SKostya Serebryany Separator, SeparatorSize)) {
29*c239eda8SKostya Serebryany Res.push_back({Beg, Pos});
30*c239eda8SKostya Serebryany Beg = Pos + SeparatorSize;
31*c239eda8SKostya Serebryany }
32*c239eda8SKostya Serebryany if (Beg < End)
33*c239eda8SKostya Serebryany Res.push_back({Beg, End});
34*c239eda8SKostya Serebryany return Res;
35*c239eda8SKostya Serebryany }
36*c239eda8SKostya Serebryany
37*c239eda8SKostya Serebryany static volatile int *Nil = nullptr;
38*c239eda8SKostya Serebryany
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)39*c239eda8SKostya Serebryany extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
40*c239eda8SKostya Serebryany if (Size > 10) return 0; // To make the test quick.
41*c239eda8SKostya Serebryany const uint8_t Separator[] = {0xDE, 0xAD, 0xBE, 0xEF};
42*c239eda8SKostya Serebryany auto Inputs = SplitInput(Data, Size, Separator, sizeof(Separator));
43*c239eda8SKostya Serebryany std::vector<uint8_t> Fuzz({'F', 'u', 'z', 'z'});
44*c239eda8SKostya Serebryany std::vector<uint8_t> Me({'m', 'e'});
45*c239eda8SKostya Serebryany if (Inputs.size() == 2 && Inputs[0] == Fuzz && Inputs[1] == Me)
46*c239eda8SKostya Serebryany *Nil = 42; // crash.
47*c239eda8SKostya Serebryany return 0;
48*c239eda8SKostya Serebryany }
49*c239eda8SKostya Serebryany
50