blob: 0e8429bcb2720d3698a1ec7e4347e58ee6804eda [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +00001//===- FuzzerMutate.cpp - Mutate a test input -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// Mutate a test input.
10//===----------------------------------------------------------------------===//
11
Kostya Serebryanyf3424592015-05-22 22:35:31 +000012#include <cstring>
13
Aaron Ballmanef116982015-01-29 16:58:29 +000014#include "FuzzerInternal.h"
15
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000016#include <algorithm>
17
Aaron Ballmanef116982015-01-29 16:58:29 +000018namespace fuzzer {
19
Kostya Serebryany7d211662015-09-04 00:12:11 +000020typedef size_t (MutationDispatcher::*Mutator)(uint8_t *Data, size_t Size,
21 size_t Max);
22
23struct MutationDispatcher::Impl {
24 std::vector<Unit> Dictionary;
25 std::vector<Mutator> Mutators;
26 Impl() {
27 Mutators.push_back(&MutationDispatcher::Mutate_EraseByte);
28 Mutators.push_back(&MutationDispatcher::Mutate_InsertByte);
29 Mutators.push_back(&MutationDispatcher::Mutate_ChangeByte);
30 Mutators.push_back(&MutationDispatcher::Mutate_ChangeBit);
31 Mutators.push_back(&MutationDispatcher::Mutate_ShuffleBytes);
Kostya Serebryany25425ad2015-09-08 17:19:31 +000032 Mutators.push_back(&MutationDispatcher::Mutate_ChangeASCIIInteger);
Kostya Serebryany7d211662015-09-04 00:12:11 +000033 }
34 void AddWordToDictionary(const uint8_t *Word, size_t Size) {
35 if (Dictionary.empty()) {
36 Mutators.push_back(&MutationDispatcher::Mutate_AddWordFromDictionary);
37 }
38 Dictionary.push_back(Unit(Word, Word + Size));
39 }
40};
41
42
Kostya Serebryany404c69f2015-07-24 01:06:40 +000043static char FlipRandomBit(char X, FuzzerRandomBase &Rand) {
44 int Bit = Rand(8);
Aaron Ballmanef116982015-01-29 16:58:29 +000045 char Mask = 1 << Bit;
46 char R;
47 if (X & (1 << Bit))
48 R = X & ~Mask;
49 else
50 R = X | Mask;
51 assert(R != X);
52 return R;
53}
54
Kostya Serebryany404c69f2015-07-24 01:06:40 +000055static char RandCh(FuzzerRandomBase &Rand) {
56 if (Rand.RandBool()) return Rand(256);
Aaron Ballmanef116982015-01-29 16:58:29 +000057 const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000058 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000059}
60
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000061size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
62 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000063 assert(Size);
64 size_t ShuffleAmount = Rand(std::min(Size, 8UL)) + 1; // [1,8] and <= Size.
65 size_t ShuffleStart = Rand(Size - ShuffleAmount);
66 assert(ShuffleStart + ShuffleAmount <= Size);
67 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
68 Rand);
69 return Size;
70}
71
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000072size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size,
73 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +000074 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +000075 if (Size == 1) return 0;
Kostya Serebryany8ce74242015-08-01 01:42:51 +000076 size_t Idx = Rand(Size);
77 // Erase Data[Idx].
78 memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
79 return Size - 1;
80}
81
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000082size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
83 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +000084 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +000085 size_t Idx = Rand(Size + 1);
86 // Insert new value at Data[Idx].
87 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
88 Data[Idx] = RandCh(Rand);
89 return Size + 1;
90}
91
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000092size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
93 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +000094 size_t Idx = Rand(Size);
95 Data[Idx] = RandCh(Rand);
96 return Size;
97}
98
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000099size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
100 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000101 size_t Idx = Rand(Size);
102 Data[Idx] = FlipRandomBit(Data[Idx], Rand);
103 return Size;
104}
105
Kostya Serebryany7d211662015-09-04 00:12:11 +0000106size_t MutationDispatcher::Mutate_AddWordFromDictionary(uint8_t *Data,
107 size_t Size,
108 size_t MaxSize) {
109 auto &D = MDImpl->Dictionary;
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000110 assert(!D.empty());
111 if (D.empty()) return 0;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000112 const Unit &Word = D[Rand(D.size())];
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000113 if (Size + Word.size() > MaxSize) return 0;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000114 size_t Idx = Rand(Size + 1);
115 memmove(Data + Idx + Word.size(), Data + Idx, Size - Idx);
116 memcpy(Data + Idx, Word.data(), Word.size());
117 return Size + Word.size();
118}
119
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000120size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
121 size_t MaxSize) {
122 size_t B = Rand(Size);
123 while (B < Size && !isdigit(Data[B])) B++;
124 if (B == Size) return 0;
125 size_t E = B;
126 while (E < Size && isdigit(Data[E])) E++;
127 assert(B < E);
128 // now we have digits in [B, E).
129 // strtol and friends don't accept non-zero-teminated data, parse it manually.
130 uint64_t Val = Data[B] - '0';
131 for (size_t i = B + 1; i < E; i++)
132 Val = Val * 10 + Data[i] - '0';
133
134 // Mutate the integer value.
135 switch(Rand(5)) {
136 case 0: Val++; break;
137 case 1: Val--; break;
138 case 2: Val /= 2; break;
139 case 3: Val *= 2; break;
140 case 4: Val = Rand(Val * Val); break;
141 default: assert(0);
142 }
143 // Just replace the bytes with the new ones, don't bother moving bytes.
144 for (size_t i = B; i < E; i++) {
145 size_t Idx = E + B - i - 1;
146 assert(Idx >= B && Idx < E);
147 Data[Idx] = (Val % 10) + '0';
148 Val /= 10;
149 }
150 return Size;
151}
152
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000153// Mutates Data in place, returns new size.
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000154size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000155 assert(MaxSize > 0);
156 assert(Size <= MaxSize);
157 if (Size == 0) {
158 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000159 Data[i] = RandCh(Rand);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000160 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000161 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000162 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000163 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
164 // in which case they will return 0.
165 // Try several times before returning un-mutated data.
166 for (int Iter = 0; Iter < 10; Iter++) {
167 size_t MutatorIdx = Rand(MDImpl->Mutators.size());
168 size_t NewSize =
169 (this->*(MDImpl->Mutators[MutatorIdx]))(Data, Size, MaxSize);
170 if (NewSize) return NewSize;
171 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000172 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000173}
174
Kostya Serebryany7d211662015-09-04 00:12:11 +0000175void MutationDispatcher::AddWordToDictionary(const uint8_t *Word, size_t Size) {
176 MDImpl->AddWordToDictionary(Word, Size);
177}
178
179MutationDispatcher::MutationDispatcher(FuzzerRandomBase &Rand) : Rand(Rand) {
180 MDImpl = new Impl;
181}
182
183MutationDispatcher::~MutationDispatcher() { delete MDImpl; }
184
Aaron Ballmanef116982015-01-29 16:58:29 +0000185} // namespace fuzzer