1*71ab4acbSKostya Serebryany // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2*71ab4acbSKostya Serebryany // See https://llvm.org/LICENSE.txt for license information.
3*71ab4acbSKostya Serebryany // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4*71ab4acbSKostya Serebryany
5*71ab4acbSKostya Serebryany // A fuzz target that consumes a Zlib-compressed input.
6*71ab4acbSKostya Serebryany // This test verifies that we can find this bug with a custom mutator.
7*71ab4acbSKostya Serebryany #include <cstddef>
8*71ab4acbSKostya Serebryany #include <cstdint>
9*71ab4acbSKostya Serebryany #include <cstdio>
10*71ab4acbSKostya Serebryany #include <cstdlib>
11*71ab4acbSKostya Serebryany #include <zlib.h>
12*71ab4acbSKostya Serebryany
13*71ab4acbSKostya Serebryany // The fuzz target.
14*71ab4acbSKostya Serebryany // Uncompress the data, crash on input starting with "FU".
15*71ab4acbSKostya Serebryany // Good luck finding this w/o a custom mutator. :)
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)16*71ab4acbSKostya Serebryany extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
17*71ab4acbSKostya Serebryany uint8_t Uncompressed[100];
18*71ab4acbSKostya Serebryany size_t UncompressedLen = sizeof(Uncompressed);
19*71ab4acbSKostya Serebryany if (Z_OK != uncompress(Uncompressed, &UncompressedLen, Data, Size))
20*71ab4acbSKostya Serebryany return 0;
21*71ab4acbSKostya Serebryany if (UncompressedLen < 2) return 0;
22*71ab4acbSKostya Serebryany if (Uncompressed[0] == 'F' && Uncompressed[1] == 'U')
23*71ab4acbSKostya Serebryany abort(); // Boom
24*71ab4acbSKostya Serebryany return 0;
25*71ab4acbSKostya Serebryany }
26*71ab4acbSKostya Serebryany
27*71ab4acbSKostya Serebryany #ifdef CUSTOM_MUTATOR
28*71ab4acbSKostya Serebryany
29*71ab4acbSKostya Serebryany // Forward-declare the libFuzzer's mutator callback.
30*71ab4acbSKostya Serebryany extern "C" size_t
31*71ab4acbSKostya Serebryany LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize);
32*71ab4acbSKostya Serebryany
33*71ab4acbSKostya Serebryany // The custom mutator:
34*71ab4acbSKostya Serebryany // * deserialize the data (in this case, uncompress).
35*71ab4acbSKostya Serebryany // * If the data doesn't deserialize, create a properly serialized dummy.
36*71ab4acbSKostya Serebryany // * Mutate the deserialized data (in this case, just call LLVMFuzzerMutate).
37*71ab4acbSKostya Serebryany // * Serialize the mutated data (in this case, compress).
LLVMFuzzerCustomMutator(uint8_t * Data,size_t Size,size_t MaxSize,unsigned int Seed)38*71ab4acbSKostya Serebryany extern "C" size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size,
39*71ab4acbSKostya Serebryany size_t MaxSize, unsigned int Seed) {
40*71ab4acbSKostya Serebryany uint8_t Uncompressed[100];
41*71ab4acbSKostya Serebryany size_t UncompressedLen = sizeof(Uncompressed);
42*71ab4acbSKostya Serebryany size_t CompressedLen = MaxSize;
43*71ab4acbSKostya Serebryany if (Z_OK != uncompress(Uncompressed, &UncompressedLen, Data, Size)) {
44*71ab4acbSKostya Serebryany // The data didn't uncompress.
45*71ab4acbSKostya Serebryany // So, it's either a broken input and we want to ignore it,
46*71ab4acbSKostya Serebryany // or we've started fuzzing from an empty corpus and we need to supply
47*71ab4acbSKostya Serebryany // out first properly compressed input.
48*71ab4acbSKostya Serebryany uint8_t Dummy[] = {'H', 'i'};
49*71ab4acbSKostya Serebryany if (Z_OK != compress(Data, &CompressedLen, Dummy, sizeof(Dummy)))
50*71ab4acbSKostya Serebryany return 0;
51*71ab4acbSKostya Serebryany // fprintf(stderr, "Dummy: max %zd res %zd\n", MaxSize, CompressedLen);
52*71ab4acbSKostya Serebryany return CompressedLen;
53*71ab4acbSKostya Serebryany }
54*71ab4acbSKostya Serebryany UncompressedLen =
55*71ab4acbSKostya Serebryany LLVMFuzzerMutate(Uncompressed, UncompressedLen, sizeof(Uncompressed));
56*71ab4acbSKostya Serebryany if (Z_OK != compress(Data, &CompressedLen, Uncompressed, UncompressedLen))
57*71ab4acbSKostya Serebryany return 0;
58*71ab4acbSKostya Serebryany return CompressedLen;
59*71ab4acbSKostya Serebryany }
60*71ab4acbSKostya Serebryany
61*71ab4acbSKostya Serebryany #endif // CUSTOM_MUTATOR
62