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