xref: /llvm-project/compiler-rt/test/fuzzer/CustomCrossOverTest.cpp (revision d0386fac269b2583a774b4dc8d88ede75fb96946)
1 // This file is distributed under the University of Illinois Open Source
2 // License. See LICENSE.TXT for details.
3 
4 // Simple test for a cutom crossover.
5 #include <assert.h>
6 #include <cstddef>
7 #include <cstdint>
8 #include <cstdlib>
9 #include <iostream>
10 #include <ostream>
11 #include <random>
12 #include <string.h>
13 #include <functional>
14 
15 static const char *Separator = "-########-";
16 static const char *Target = "A-########-B";
17 
18 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
19   assert(Data);
20   std::string Str(reinterpret_cast<const char *>(Data), Size);
21   static const size_t TargetHash = std::hash<std::string>{}(std::string(Target));
22   size_t StrHash = std::hash<std::string>{}(Str);
23 
24   if (TargetHash == StrHash) {
25     std::cout << "BINGO; Found the target, exiting\n" << std::flush;
26     exit(1);
27   }
28   return 0;
29 }
30 
31 extern "C" size_t LLVMFuzzerCustomCrossOver(const uint8_t *Data1, size_t Size1,
32                                             const uint8_t *Data2, size_t Size2,
33                                             uint8_t *Out, size_t MaxOutSize,
34                                             unsigned int Seed) {
35   static size_t Printed;
36   static size_t SeparatorLen = strlen(Separator);
37 
38   if (Printed++ < 32)
39     std::cerr << "In LLVMFuzzerCustomCrossover " << Size1 << " " << Size2 << "\n";
40 
41   size_t Size = Size1 + Size2 + SeparatorLen;
42 
43   if (Size > MaxOutSize)
44     return 0;
45 
46   memcpy(Out, Data1, Size1);
47   memcpy(Out + Size1, Separator, SeparatorLen);
48   memcpy(Out + Size1 + SeparatorLen, Data2, Size2);
49 
50   return Size;
51 }
52