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
4 
5 // Simple test for a fuzzer. The fuzzer must find several narrow ranges.
6 #include <cstdint>
7 #include <cstdio>
8 #include <cstdlib>
9 #include <cstring>
10 
11 extern int AllLines[];
12 
PrintOnce(int Line)13 bool PrintOnce(int Line) {
14   if (!AllLines[Line])
15     fprintf(stderr, "Seen line %d\n", Line);
16   AllLines[Line] = 1;
17   return true;
18 }
19 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)20 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
21   if (Size != 21)
22     return 0;
23   uint64_t x = 0;
24   int64_t  y = 0;
25   int32_t z = 0;
26   uint8_t a = 0;
27   memcpy(&x, Data, 8);  // 8
28   memcpy(&y, Data + 8, 8);  // 16
29   memcpy(&z, Data + 16, sizeof(z));  // 20
30   memcpy(&a, Data + 20, sizeof(a));  // 21
31   const bool k32bit = sizeof(void*) == 4;
32 
33   if ((k32bit || x > 1234567890) && PrintOnce(__LINE__) &&
34       (k32bit || x < 1234567895) && PrintOnce(__LINE__) &&
35       a == 0x42 && PrintOnce(__LINE__) &&
36       (k32bit || y >= 987654321) && PrintOnce(__LINE__) &&
37       (k32bit || y <= 987654325) && PrintOnce(__LINE__) &&
38       z < -10000 && PrintOnce(__LINE__) &&
39       z >= -10005 && PrintOnce(__LINE__) &&
40       z != -10003 && PrintOnce(__LINE__) &&
41       true) {
42     fprintf(stderr, "BINGO; Found the target: size %zd (%zd, %zd, %d, %d), exiting.\n",
43             Size, x, y, z, a);
44     exit(1);
45   }
46   return 0;
47 }
48 
49 int AllLines[__LINE__ + 1];  // Must be the last line.
50