1c5f01b2fSopenharmony_ci// This file is distributed under the University of Illinois Open Source 2c5f01b2fSopenharmony_ci// License. See LICENSE.TXT for details. 3c5f01b2fSopenharmony_ci 4c5f01b2fSopenharmony_ci// This test computes a checksum of the data (all but the last 4 bytes), 5c5f01b2fSopenharmony_ci// and then compares the last 4 bytes with the computed value. 6c5f01b2fSopenharmony_ci// A fuzzer with cmp traces is expected to defeat this check. 7c5f01b2fSopenharmony_ci#include <cstdint> 8c5f01b2fSopenharmony_ci#include <cstdlib> 9c5f01b2fSopenharmony_ci#include <cstring> 10c5f01b2fSopenharmony_ci#include <cstdio> 11c5f01b2fSopenharmony_ci 12c5f01b2fSopenharmony_ci// A modified jenkins_one_at_a_time_hash initialized by non-zero, 13c5f01b2fSopenharmony_ci// so that simple_hash(0) != 0. See also 14c5f01b2fSopenharmony_ci// https://en.wikipedia.org/wiki/Jenkins_hash_function 15c5f01b2fSopenharmony_cistatic uint32_t simple_hash(const uint8_t *Data, size_t Size) { 16c5f01b2fSopenharmony_ci uint32_t Hash = 0x12039854; 17c5f01b2fSopenharmony_ci for (uint32_t i = 0; i < Size; i++) { 18c5f01b2fSopenharmony_ci Hash += Data[i]; 19c5f01b2fSopenharmony_ci Hash += (Hash << 10); 20c5f01b2fSopenharmony_ci Hash ^= (Hash >> 6); 21c5f01b2fSopenharmony_ci } 22c5f01b2fSopenharmony_ci Hash += (Hash << 3); 23c5f01b2fSopenharmony_ci Hash ^= (Hash >> 11); 24c5f01b2fSopenharmony_ci Hash += (Hash << 15); 25c5f01b2fSopenharmony_ci return Hash; 26c5f01b2fSopenharmony_ci} 27c5f01b2fSopenharmony_ci 28c5f01b2fSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { 29c5f01b2fSopenharmony_ci if (Size < 14) 30c5f01b2fSopenharmony_ci return 0; 31c5f01b2fSopenharmony_ci 32c5f01b2fSopenharmony_ci uint32_t Hash = simple_hash(&Data[0], Size - 4); 33c5f01b2fSopenharmony_ci uint32_t Want = reinterpret_cast<const uint32_t *>(&Data[Size - 4])[0]; 34c5f01b2fSopenharmony_ci if (Hash != Want) 35c5f01b2fSopenharmony_ci return 0; 36c5f01b2fSopenharmony_ci fprintf(stderr, "BINGO; simple_hash defeated: %x == %x\n", (unsigned int)Hash, 37c5f01b2fSopenharmony_ci (unsigned int)Want); 38c5f01b2fSopenharmony_ci exit(1); 39c5f01b2fSopenharmony_ci return 0; 40c5f01b2fSopenharmony_ci} 41