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
11extern int AllLines[];
12
13bool PrintOnce(int Line) {
14 if (!AllLines[Line])
15 fprintf(stderr, format: "Seen line %d\n", Line);
16 AllLines[Line] = 1;
17 return true;
18}
19
20extern "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(dest: &x, src: Data, n: 8); // 8
28 memcpy(dest: &y, src: Data + 8, n: 8); // 16
29 memcpy(dest: &z, src: Data + 16, n: sizeof(z)); // 20
30 memcpy(dest: &a, src: Data + 20, n: 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, format: "BINGO; Found the target: size %zd (%zd, %zd, %d, %d), exiting.\n",
43 Size, x, y, z, a);
44 exit(status: 1);
45 }
46 return 0;
47}
48
49int AllLines[__LINE__ + 1]; // Must be the last line.
50

source code of compiler-rt/test/fuzzer/SimpleCmpTest.cpp