12946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
22946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
32946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4f489e2bfSKostya Serebryany
5f489e2bfSKostya Serebryany // Find "FUZZME", the target has 3 different functions.
6*219b2b3aSKostya Serebryany //
7*219b2b3aSKostya Serebryany // This test, and the accompanying lit tests
8*219b2b3aSKostya Serebryany // (dataflow.test, only-some-bytes.test) assume that the compiler
9*219b2b3aSKostya Serebryany // instruments functions in their lexical order.
10*219b2b3aSKostya Serebryany // This assumption is not guaranteed, but it is likely to hold.
11*219b2b3aSKostya Serebryany // It allows to simplify the test quite a bit: in the lit tests
12*219b2b3aSKostya Serebryany // LLVMFuzzerTestOneInput is "F0", Func1 is "F1", Func2 is "F2".
13f489e2bfSKostya Serebryany #include <assert.h>
14f489e2bfSKostya Serebryany #include <cstddef>
15f489e2bfSKostya Serebryany #include <cstdint>
16f489e2bfSKostya Serebryany #include <cstdlib>
17f489e2bfSKostya Serebryany #include <cstdio>
18f489e2bfSKostya Serebryany
19*219b2b3aSKostya Serebryany extern "C" bool Func1(const uint8_t *Data, size_t Size);
20*219b2b3aSKostya Serebryany extern "C" bool Func2(const uint8_t *Data, size_t Size);
21f489e2bfSKostya Serebryany
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)22f489e2bfSKostya Serebryany extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
23f489e2bfSKostya Serebryany if (Size >= 5
24f489e2bfSKostya Serebryany && Data[0] == 'F'
25f489e2bfSKostya Serebryany && Data[1] == 'U'
26f489e2bfSKostya Serebryany && Data[2] == 'Z'
27f489e2bfSKostya Serebryany && Data[3] == 'Z'
28f489e2bfSKostya Serebryany && Func1(Data, Size)
29f489e2bfSKostya Serebryany && Func2(Data, Size)) {
30f489e2bfSKostya Serebryany fprintf(stderr, "BINGO\n");
31f489e2bfSKostya Serebryany abort();
32f489e2bfSKostya Serebryany }
33f489e2bfSKostya Serebryany return 0;
34f489e2bfSKostya Serebryany }
35*219b2b3aSKostya Serebryany
36*219b2b3aSKostya Serebryany extern "C"
37*219b2b3aSKostya Serebryany __attribute__((noinline))
Func1(const uint8_t * Data,size_t Size)38*219b2b3aSKostya Serebryany bool Func1(const uint8_t *Data, size_t Size) {
39*219b2b3aSKostya Serebryany // assumes Size >= 5, doesn't check it.
40*219b2b3aSKostya Serebryany return Data[4] == 'M';
41*219b2b3aSKostya Serebryany }
42*219b2b3aSKostya Serebryany
43*219b2b3aSKostya Serebryany extern "C"
44*219b2b3aSKostya Serebryany __attribute__((noinline))
Func2(const uint8_t * Data,size_t Size)45*219b2b3aSKostya Serebryany bool Func2(const uint8_t *Data, size_t Size) {
46*219b2b3aSKostya Serebryany return Size >= 6 && Data[5] == 'E';
47*219b2b3aSKostya Serebryany }
48*219b2b3aSKostya Serebryany
49*219b2b3aSKostya Serebryany
50