blob: 30e5b43c0839005f0392e99ed1088a5dba53ffef [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 Serebryany14c50282015-12-19 01:09:49 +000020struct Mutator {
21 size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
22 const char *Name;
23};
Kostya Serebryany7d211662015-09-04 00:12:11 +000024
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000025struct DictionaryEntry {
26 Unit Word;
27 size_t PositionHint;
28};
29
Kostya Serebryany7d211662015-09-04 00:12:11 +000030struct MutationDispatcher::Impl {
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000031 std::vector<DictionaryEntry> ManualDictionary;
32 std::vector<DictionaryEntry> AutoDictionary;
Kostya Serebryany7d211662015-09-04 00:12:11 +000033 std::vector<Mutator> Mutators;
Kostya Serebryany14c50282015-12-19 01:09:49 +000034 std::vector<Mutator> CurrentMutatorSequence;
Kostya Serebryany41740052016-01-12 02:36:59 +000035 std::vector<DictionaryEntry> CurrentDictionaryEntrySequence;
Kostya Serebryany27ab2d72015-12-19 02:49:09 +000036 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000037 FuzzerRandomBase &Rand;
Kostya Serebryany14c50282015-12-19 01:09:49 +000038
39 void Add(Mutator M) { Mutators.push_back(M); }
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000040 Impl(FuzzerRandomBase &Rand) : Rand(Rand) {
Kostya Serebryany14c50282015-12-19 01:09:49 +000041 Add({&MutationDispatcher::Mutate_EraseByte, "EraseByte"});
42 Add({&MutationDispatcher::Mutate_InsertByte, "InsertByte"});
43 Add({&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"});
44 Add({&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"});
45 Add({&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"});
46 Add({&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"});
Kostya Serebryany27ab2d72015-12-19 02:49:09 +000047 Add({&MutationDispatcher::Mutate_CrossOver, "CrossOver"});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000048 Add({&MutationDispatcher::Mutate_AddWordFromManualDictionary,
49 "AddFromManualDict"});
50 Add({&MutationDispatcher::Mutate_AddWordFromAutoDictionary,
51 "AddFromAutoDict"});
Kostya Serebryany7d211662015-09-04 00:12:11 +000052 }
Kostya Serebryany27ab2d72015-12-19 02:49:09 +000053 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000054 size_t AddWordFromDictionary(const std::vector<DictionaryEntry> &D,
55 uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany7d211662015-09-04 00:12:11 +000056};
57
Kostya Serebryany404c69f2015-07-24 01:06:40 +000058static char FlipRandomBit(char X, FuzzerRandomBase &Rand) {
59 int Bit = Rand(8);
Aaron Ballmanef116982015-01-29 16:58:29 +000060 char Mask = 1 << Bit;
61 char R;
62 if (X & (1 << Bit))
63 R = X & ~Mask;
64 else
65 R = X | Mask;
66 assert(R != X);
67 return R;
68}
69
Kostya Serebryany404c69f2015-07-24 01:06:40 +000070static char RandCh(FuzzerRandomBase &Rand) {
71 if (Rand.RandBool()) return Rand(256);
Aaron Ballmanef116982015-01-29 16:58:29 +000072 const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000073 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000074}
75
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000076size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
77 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000078 assert(Size);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000079 size_t ShuffleAmount =
80 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000081 size_t ShuffleStart = Rand(Size - ShuffleAmount);
82 assert(ShuffleStart + ShuffleAmount <= Size);
83 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
84 Rand);
85 return Size;
86}
87
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000088size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size,
89 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +000090 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +000091 if (Size == 1) return 0;
Kostya Serebryany8ce74242015-08-01 01:42:51 +000092 size_t Idx = Rand(Size);
93 // Erase Data[Idx].
94 memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
95 return Size - 1;
96}
97
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000098size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
99 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000100 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000101 size_t Idx = Rand(Size + 1);
102 // Insert new value at Data[Idx].
103 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
104 Data[Idx] = RandCh(Rand);
105 return Size + 1;
106}
107
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000108size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
109 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000110 size_t Idx = Rand(Size);
111 Data[Idx] = RandCh(Rand);
112 return Size;
113}
114
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000115size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
116 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000117 size_t Idx = Rand(Size);
118 Data[Idx] = FlipRandomBit(Data[Idx], Rand);
119 return Size;
120}
121
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000122size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
123 size_t Size,
124 size_t MaxSize) {
125 return MDImpl->AddWordFromDictionary(MDImpl->ManualDictionary, Data, Size,
126 MaxSize);
127}
128
129size_t MutationDispatcher::Mutate_AddWordFromAutoDictionary(uint8_t *Data,
130 size_t Size,
131 size_t MaxSize) {
132 return MDImpl->AddWordFromDictionary(MDImpl->AutoDictionary, Data, Size,
133 MaxSize);
134}
135
136size_t MutationDispatcher::Impl::AddWordFromDictionary(
137 const std::vector<DictionaryEntry> &D, uint8_t *Data, size_t Size,
138 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000139 if (D.empty()) return 0;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000140 const DictionaryEntry &DE = D[Rand(D.size())];
141 const Unit &Word = DE.Word;
142 size_t PositionHint = DE.PositionHint;
143 bool UsePositionHint = PositionHint != std::numeric_limits<size_t>::max() &&
144 PositionHint + Word.size() < Size && Rand.RandBool();
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000145 if (Rand.RandBool()) { // Insert Word.
146 if (Size + Word.size() > MaxSize) return 0;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000147 size_t Idx = UsePositionHint ? PositionHint : Rand(Size + 1);
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000148 memmove(Data + Idx + Word.size(), Data + Idx, Size - Idx);
149 memcpy(Data + Idx, Word.data(), Word.size());
Kostya Serebryany41740052016-01-12 02:36:59 +0000150 Size += Word.size();
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000151 } else { // Overwrite some bytes with Word.
152 if (Word.size() > Size) return 0;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000153 size_t Idx = UsePositionHint ? PositionHint : Rand(Size - Word.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000154 memcpy(Data + Idx, Word.data(), Word.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000155 }
Kostya Serebryany41740052016-01-12 02:36:59 +0000156 CurrentDictionaryEntrySequence.push_back(DE);
157 return Size;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000158}
159
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000160size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
161 size_t MaxSize) {
162 size_t B = Rand(Size);
163 while (B < Size && !isdigit(Data[B])) B++;
164 if (B == Size) return 0;
165 size_t E = B;
166 while (E < Size && isdigit(Data[E])) E++;
167 assert(B < E);
168 // now we have digits in [B, E).
169 // strtol and friends don't accept non-zero-teminated data, parse it manually.
170 uint64_t Val = Data[B] - '0';
171 for (size_t i = B + 1; i < E; i++)
172 Val = Val * 10 + Data[i] - '0';
173
174 // Mutate the integer value.
175 switch(Rand(5)) {
176 case 0: Val++; break;
177 case 1: Val--; break;
178 case 2: Val /= 2; break;
179 case 3: Val *= 2; break;
180 case 4: Val = Rand(Val * Val); break;
181 default: assert(0);
182 }
183 // Just replace the bytes with the new ones, don't bother moving bytes.
184 for (size_t i = B; i < E; i++) {
185 size_t Idx = E + B - i - 1;
186 assert(Idx >= B && Idx < E);
187 Data[Idx] = (Val % 10) + '0';
188 Val /= 10;
189 }
190 return Size;
191}
192
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000193size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
194 size_t MaxSize) {
195 auto Corpus = MDImpl->Corpus;
196 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
197 size_t Idx = Rand(Corpus->size());
198 const Unit &Other = (*Corpus)[Idx];
199 if (Other.empty()) return 0;
200 Unit U(MaxSize);
201 size_t NewSize =
202 CrossOver(Data, Size, Other.data(), Other.size(), U.data(), U.size());
203 assert(NewSize > 0 && "CrossOver returned empty unit");
204 assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
205 memcpy(Data, U.data(), NewSize);
206 return NewSize;
207}
208
Kostya Serebryany14c50282015-12-19 01:09:49 +0000209void MutationDispatcher::StartMutationSequence() {
210 MDImpl->CurrentMutatorSequence.clear();
Kostya Serebryany41740052016-01-12 02:36:59 +0000211 MDImpl->CurrentDictionaryEntrySequence.clear();
Kostya Serebryany14c50282015-12-19 01:09:49 +0000212}
213
214void MutationDispatcher::PrintMutationSequence() {
215 Printf("MS: %zd ", MDImpl->CurrentMutatorSequence.size());
216 for (auto M : MDImpl->CurrentMutatorSequence)
217 Printf("%s-", M.Name);
Kostya Serebryany41740052016-01-12 02:36:59 +0000218 if (!MDImpl->CurrentDictionaryEntrySequence.empty()) {
219 Printf(" DE: ");
220 for (auto DE : MDImpl->CurrentDictionaryEntrySequence) {
221 Printf("\"");
222 PrintASCII(DE.Word, "\"-");
223 }
224 }
Kostya Serebryany14c50282015-12-19 01:09:49 +0000225}
226
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000227// Mutates Data in place, returns new size.
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000228size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000229 assert(MaxSize > 0);
230 assert(Size <= MaxSize);
231 if (Size == 0) {
232 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000233 Data[i] = RandCh(Rand);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000234 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000235 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000236 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000237 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
238 // in which case they will return 0.
239 // Try several times before returning un-mutated data.
240 for (int Iter = 0; Iter < 10; Iter++) {
241 size_t MutatorIdx = Rand(MDImpl->Mutators.size());
Kostya Serebryany14c50282015-12-19 01:09:49 +0000242 auto M = MDImpl->Mutators[MutatorIdx];
243 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
244 if (NewSize) {
245 MDImpl->CurrentMutatorSequence.push_back(M);
246 return NewSize;
247 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000248 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000249 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000250}
251
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000252void MutationDispatcher::SetCorpus(const std::vector<Unit> *Corpus) {
253 MDImpl->SetCorpus(Corpus);
254}
255
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000256void MutationDispatcher::AddWordToManualDictionary(const Unit &Word) {
257 MDImpl->ManualDictionary.push_back(
258 {Word, std::numeric_limits<size_t>::max()});
259}
260
261void MutationDispatcher::AddWordToAutoDictionary(const Unit &Word,
262 size_t PositionHint) {
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000263 static const size_t kMaxAutoDictSize = 1 << 14;
264 if (MDImpl->AutoDictionary.size() >= kMaxAutoDictSize) return;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000265 MDImpl->AutoDictionary.push_back({Word, PositionHint});
Kostya Serebryany7d211662015-09-04 00:12:11 +0000266}
267
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000268void MutationDispatcher::ClearAutoDictionary() {
269 MDImpl->AutoDictionary.clear();
270}
271
Kostya Serebryany7d211662015-09-04 00:12:11 +0000272MutationDispatcher::MutationDispatcher(FuzzerRandomBase &Rand) : Rand(Rand) {
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000273 MDImpl = new Impl(Rand);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000274}
275
276MutationDispatcher::~MutationDispatcher() { delete MDImpl; }
277
Aaron Ballmanef116982015-01-29 16:58:29 +0000278} // namespace fuzzer