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