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 
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 
20 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
21   if (Size != 24) return 0;
22   uint64_t x = 0;
23   int64_t  y = 0;
24   int32_t z = 0;
25   uint32_t a = 0;
26   memcpy(&x, Data, 8);  // 8
27   memcpy(&y, Data + 8, 8);  // 16
28   memcpy(&z, Data + 16, sizeof(z));  // 20
29   memcpy(&a, Data + 20, sizeof(a));  // 24
30   const bool k32bit = sizeof(void*) == 4;
31 
32   if ((k32bit || x > 1234567890) && PrintOnce(__LINE__) &&
33       (k32bit || x < 1234567895) && PrintOnce(__LINE__) &&
34       a == 0x4242 && PrintOnce(__LINE__) &&
35       (k32bit || y >= 987654321) && PrintOnce(__LINE__) &&
36       (k32bit || y <= 987654325) && PrintOnce(__LINE__) &&
37       z < -10000 && PrintOnce(__LINE__) &&
38       z >= -10005 && PrintOnce(__LINE__) &&
39       z != -10003 && PrintOnce(__LINE__) &&
40       true) {
41     fprintf(stderr, "BINGO; Found the target: size %zd (%zd, %zd, %d, %d), exiting.\n",
42             Size, x, y, z, a);
43     exit(1);
44   }
45   return 0;
46 }
47 
48 int AllLines[__LINE__ + 1];  // Must be the last line.
49