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 // Test for a fuzzer. The fuzzer must find the string
6 // ABCDEFGHIJ
7 // We use it as a test for CrossOver functionality
8 // by passing two inputs to it:
9 // ABCDE00000
10 // ZZZZZFGHIJ
11 //
12 #include <assert.h>
13 #include <cstddef>
14 #include <cstdint>
15 #include <cstdlib>
16 #include <iostream>
17 #include <ostream>
18 
19 static volatile int Sink;
20 static volatile int *NullPtr;
21 
22 // A modified jenkins_one_at_a_time_hash initialized by non-zero,
23 // so that simple_hash(0) != 0. See also
24 // https://en.wikipedia.org/wiki/Jenkins_hash_function
25 static uint32_t simple_hash(const uint8_t *Data, size_t Size) {
26   uint32_t Hash = 0x12039854;
27   for (uint32_t i = 0; i < Size; i++) {
28     Hash += Data[i];
29     Hash += (Hash << 10);
30     Hash ^= (Hash >> 6);
31   }
32   Hash += (Hash << 3);
33   Hash ^= (Hash >> 11);
34   Hash += (Hash << 15);
35   return Hash;
36 }
37 
38 // Don't leave the string in the binary, so that fuzzer don't cheat;
39 // const char *ABC = "ABCDEFGHIJ";
40 // static uint32_t ExpectedHash = simple_hash((const uint8_t *)ABC, 10);
41 static const uint32_t ExpectedHash = 0xe1677acb;
42 
43 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
44   // fprintf(stderr, "ExpectedHash: %x\n", ExpectedHash);
45   if (Size != 10) return 0;
46   if (*Data == 'A')
47     Sink++;
48   if (*Data == 'Z')
49     Sink--;
50   if (ExpectedHash == simple_hash(Data, Size))
51     *NullPtr = 0;
52   return 0;
53 }
54 
55