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// Simple test for a cutom mutator.
5c5f01b2fSopenharmony_ci#include <assert.h>
6c5f01b2fSopenharmony_ci#include <cstddef>
7c5f01b2fSopenharmony_ci#include <cstdint>
8c5f01b2fSopenharmony_ci#include <cstdlib>
9c5f01b2fSopenharmony_ci#include <iostream>
10c5f01b2fSopenharmony_ci#include <random>
11c5f01b2fSopenharmony_ci#include <string.h>
12c5f01b2fSopenharmony_ci
13c5f01b2fSopenharmony_ci#include "FuzzerInterface.h"
14c5f01b2fSopenharmony_ci
15c5f01b2fSopenharmony_cistatic const char *Separator = "-_^_-";
16c5f01b2fSopenharmony_cistatic const char *Target = "012-_^_-abc";
17c5f01b2fSopenharmony_ci
18c5f01b2fSopenharmony_cistatic volatile int sink;
19c5f01b2fSopenharmony_ci
20c5f01b2fSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
21c5f01b2fSopenharmony_ci  assert(Data);
22c5f01b2fSopenharmony_ci  std::string Str(reinterpret_cast<const char *>(Data), Size);
23c5f01b2fSopenharmony_ci
24c5f01b2fSopenharmony_ci  // Ensure that two different elements exist in the corpus.
25c5f01b2fSopenharmony_ci  if (Size && Data[0] == '0') sink++;
26c5f01b2fSopenharmony_ci  if (Size && Data[0] == 'a') sink--;
27c5f01b2fSopenharmony_ci
28c5f01b2fSopenharmony_ci  if (Str.find(Target) != std::string::npos) {
29c5f01b2fSopenharmony_ci    std::cout << "BINGO; Found the target, exiting\n";
30c5f01b2fSopenharmony_ci    exit(1);
31c5f01b2fSopenharmony_ci  }
32c5f01b2fSopenharmony_ci  return 0;
33c5f01b2fSopenharmony_ci}
34c5f01b2fSopenharmony_ci
35c5f01b2fSopenharmony_ciextern "C" size_t LLVMFuzzerCustomCrossOver(const uint8_t *Data1, size_t Size1,
36c5f01b2fSopenharmony_ci                                            const uint8_t *Data2, size_t Size2,
37c5f01b2fSopenharmony_ci                                            uint8_t *Out, size_t MaxOutSize,
38c5f01b2fSopenharmony_ci                                            unsigned int Seed) {
39c5f01b2fSopenharmony_ci  static bool Printed;
40c5f01b2fSopenharmony_ci  static size_t SeparatorLen = strlen(Separator);
41c5f01b2fSopenharmony_ci
42c5f01b2fSopenharmony_ci  if (!Printed) {
43c5f01b2fSopenharmony_ci    std::cerr << "In LLVMFuzzerCustomCrossover\n";
44c5f01b2fSopenharmony_ci    Printed = true;
45c5f01b2fSopenharmony_ci  }
46c5f01b2fSopenharmony_ci
47c5f01b2fSopenharmony_ci  std::mt19937 R(Seed);
48c5f01b2fSopenharmony_ci
49c5f01b2fSopenharmony_ci  size_t Offset1 = 0;
50c5f01b2fSopenharmony_ci  size_t Len1 = R() % (Size1 - Offset1);
51c5f01b2fSopenharmony_ci  size_t Offset2 = 0;
52c5f01b2fSopenharmony_ci  size_t Len2 = R() % (Size2 - Offset2);
53c5f01b2fSopenharmony_ci  size_t Size = Len1 + Len2 + SeparatorLen;
54c5f01b2fSopenharmony_ci
55c5f01b2fSopenharmony_ci  if (Size > MaxOutSize)
56c5f01b2fSopenharmony_ci    return 0;
57c5f01b2fSopenharmony_ci
58c5f01b2fSopenharmony_ci  memcpy(Out, Data1 + Offset1, Len1);
59c5f01b2fSopenharmony_ci  memcpy(Out + Len1, Separator, SeparatorLen);
60c5f01b2fSopenharmony_ci  memcpy(Out + Len1 + SeparatorLen, Data2 + Offset2, Len2);
61c5f01b2fSopenharmony_ci
62c5f01b2fSopenharmony_ci  return Len1 + Len2 + SeparatorLen;
63c5f01b2fSopenharmony_ci}
64