brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.7 KiB · eb8a8c4 Raw
57 lines · cpp
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-exception4 5// Test for a fuzzer. The fuzzer must find the string6// ABCDEFGHIJ7// We use it as a test for each of CrossOver functionalities8// by passing the following sets of two inputs to it:9// {ABCDE00000, ZZZZZFGHIJ}10// {ABCDEHIJ, ZFG} to specifically test InsertPartOf11// {ABCDE00HIJ, ZFG} to specifically test CopyPartOf12//13#include <assert.h>14#include <cstddef>15#include <cstdint>16#include <cstdlib>17#include <iostream>18#include <ostream>19 20static volatile int Sink;21static 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 also25// https://en.wikipedia.org/wiki/Jenkins_hash_function26static 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);42static const uint32_t ExpectedHash = 0xe1677acb;43 44extern "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  // It's UB to read *Data when Size == 049  if (Size == 0)50    return 0;51  if (*Data == 'A')52    Sink++;53  if (*Data == 'Z')54    Sink--;55  return 0;56}57