Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / fuzzer / CustomCrossOverTest.cpp
blobf6013b93efb8b22caf96a9af1991ea18ed728139
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
5 // Simple test for a custom crossover.
6 #include <assert.h>
7 #include <cstddef>
8 #include <cstdint>
9 #include <cstdlib>
10 #include <iostream>
11 #include <ostream>
12 #include <random>
13 #include <string.h>
14 #include <functional>
16 static const char *Separator = "-########-";
17 static const char *Target = "A-########-B";
19 static volatile int sink;
21 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
22 assert(Data);
23 std::string Str(reinterpret_cast<const char *>(Data), Size);
24 static const size_t TargetHash = std::hash<std::string>{}(std::string(Target));
25 size_t StrHash = std::hash<std::string>{}(Str);
27 // Ensure we have 'A' and 'B' in the corpus.
28 if (Size == 1 && *Data == 'A')
29 sink++;
30 if (Size == 1 && *Data == 'B')
31 sink--;
33 if (TargetHash == StrHash) {
34 std::cout << "BINGO; Found the target, exiting\n" << std::flush;
35 exit(1);
37 return 0;
40 extern "C" size_t LLVMFuzzerCustomCrossOver(const uint8_t *Data1, size_t Size1,
41 const uint8_t *Data2, size_t Size2,
42 uint8_t *Out, size_t MaxOutSize,
43 unsigned int Seed) {
44 static size_t Printed;
45 static size_t SeparatorLen = strlen(Separator);
47 if (Printed++ < 32)
48 std::cerr << "In LLVMFuzzerCustomCrossover " << Size1 << " " << Size2 << "\n";
50 size_t Size = Size1 + Size2 + SeparatorLen;
52 if (Size > MaxOutSize)
53 return 0;
55 memcpy(Out, Data1, Size1);
56 memcpy(Out + Size1, Separator, SeparatorLen);
57 memcpy(Out + Size1 + SeparatorLen, Data2, Size2);
59 return Size;