blob: 219837f4a0fa031e60db1d9efa36f46c4f0b1663 [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 Serebryany27ab2d72015-12-19 02:49:09 +000035 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000036 FuzzerRandomBase &Rand;
Kostya Serebryany14c50282015-12-19 01:09:49 +000037
38 void Add(Mutator M) { Mutators.push_back(M); }
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000039 Impl(FuzzerRandomBase &Rand) : Rand(Rand) {
Kostya Serebryany14c50282015-12-19 01:09:49 +000040 Add({&MutationDispatcher::Mutate_EraseByte, "EraseByte"});
41 Add({&MutationDispatcher::Mutate_InsertByte, "InsertByte"});
42 Add({&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"});
43 Add({&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"});
44 Add({&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"});
45 Add({&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"});
Kostya Serebryany27ab2d72015-12-19 02:49:09 +000046 Add({&MutationDispatcher::Mutate_CrossOver, "CrossOver"});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000047 Add({&MutationDispatcher::Mutate_AddWordFromManualDictionary,
48 "AddFromManualDict"});
49 Add({&MutationDispatcher::Mutate_AddWordFromAutoDictionary,
50 "AddFromAutoDict"});
Kostya Serebryany7d211662015-09-04 00:12:11 +000051 }
Kostya Serebryany27ab2d72015-12-19 02:49:09 +000052 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000053 size_t AddWordFromDictionary(const std::vector<DictionaryEntry> &D,
54 uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany7d211662015-09-04 00:12:11 +000055};
56
Kostya Serebryany404c69f2015-07-24 01:06:40 +000057static char FlipRandomBit(char X, FuzzerRandomBase &Rand) {
58 int Bit = Rand(8);
Aaron Ballmanef116982015-01-29 16:58:29 +000059 char Mask = 1 << Bit;
60 char R;
61 if (X & (1 << Bit))
62 R = X & ~Mask;
63 else
64 R = X | Mask;
65 assert(R != X);
66 return R;
67}
68
Kostya Serebryany404c69f2015-07-24 01:06:40 +000069static char RandCh(FuzzerRandomBase &Rand) {
70 if (Rand.RandBool()) return Rand(256);
Aaron Ballmanef116982015-01-29 16:58:29 +000071 const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000072 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000073}
74
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000075size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
76 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000077 assert(Size);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000078 size_t ShuffleAmount =
79 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000080 size_t ShuffleStart = Rand(Size - ShuffleAmount);
81 assert(ShuffleStart + ShuffleAmount <= Size);
82 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
83 Rand);
84 return Size;
85}
86
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000087size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size,
88 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +000089 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +000090 if (Size == 1) return 0;
Kostya Serebryany8ce74242015-08-01 01:42:51 +000091 size_t Idx = Rand(Size);
92 // Erase Data[Idx].
93 memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
94 return Size - 1;
95}
96
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000097size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
98 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +000099 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000100 size_t Idx = Rand(Size + 1);
101 // Insert new value at Data[Idx].
102 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
103 Data[Idx] = RandCh(Rand);
104 return Size + 1;
105}
106
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000107size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
108 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000109 size_t Idx = Rand(Size);
110 Data[Idx] = RandCh(Rand);
111 return Size;
112}
113
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000114size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
115 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000116 size_t Idx = Rand(Size);
117 Data[Idx] = FlipRandomBit(Data[Idx], Rand);
118 return Size;
119}
120
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000121size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
122 size_t Size,
123 size_t MaxSize) {
124 return MDImpl->AddWordFromDictionary(MDImpl->ManualDictionary, Data, Size,
125 MaxSize);
126}
127
128size_t MutationDispatcher::Mutate_AddWordFromAutoDictionary(uint8_t *Data,
129 size_t Size,
130 size_t MaxSize) {
131 return MDImpl->AddWordFromDictionary(MDImpl->AutoDictionary, Data, Size,
132 MaxSize);
133}
134
135size_t MutationDispatcher::Impl::AddWordFromDictionary(
136 const std::vector<DictionaryEntry> &D, uint8_t *Data, size_t Size,
137 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000138 if (D.empty()) return 0;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000139 const DictionaryEntry &DE = D[Rand(D.size())];
140 const Unit &Word = DE.Word;
141 size_t PositionHint = DE.PositionHint;
142 bool UsePositionHint = PositionHint != std::numeric_limits<size_t>::max() &&
143 PositionHint + Word.size() < Size && Rand.RandBool();
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000144 if (Rand.RandBool()) { // Insert Word.
145 if (Size + Word.size() > MaxSize) return 0;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000146 size_t Idx = UsePositionHint ? PositionHint : Rand(Size + 1);
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000147 memmove(Data + Idx + Word.size(), Data + Idx, Size - Idx);
148 memcpy(Data + Idx, Word.data(), Word.size());
149 return Size + Word.size();
150 } else { // Overwrite some bytes with Word.
151 if (Word.size() > Size) return 0;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000152 size_t Idx = UsePositionHint ? PositionHint : Rand(Size - Word.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000153 memcpy(Data + Idx, Word.data(), Word.size());
154 return Size;
155 }
Kostya Serebryany7d211662015-09-04 00:12:11 +0000156}
157
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000158size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
159 size_t MaxSize) {
160 size_t B = Rand(Size);
161 while (B < Size && !isdigit(Data[B])) B++;
162 if (B == Size) return 0;
163 size_t E = B;
164 while (E < Size && isdigit(Data[E])) E++;
165 assert(B < E);
166 // now we have digits in [B, E).
167 // strtol and friends don't accept non-zero-teminated data, parse it manually.
168 uint64_t Val = Data[B] - '0';
169 for (size_t i = B + 1; i < E; i++)
170 Val = Val * 10 + Data[i] - '0';
171
172 // Mutate the integer value.
173 switch(Rand(5)) {
174 case 0: Val++; break;
175 case 1: Val--; break;
176 case 2: Val /= 2; break;
177 case 3: Val *= 2; break;
178 case 4: Val = Rand(Val * Val); break;
179 default: assert(0);
180 }
181 // Just replace the bytes with the new ones, don't bother moving bytes.
182 for (size_t i = B; i < E; i++) {
183 size_t Idx = E + B - i - 1;
184 assert(Idx >= B && Idx < E);
185 Data[Idx] = (Val % 10) + '0';
186 Val /= 10;
187 }
188 return Size;
189}
190
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000191size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
192 size_t MaxSize) {
193 auto Corpus = MDImpl->Corpus;
194 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
195 size_t Idx = Rand(Corpus->size());
196 const Unit &Other = (*Corpus)[Idx];
197 if (Other.empty()) return 0;
198 Unit U(MaxSize);
199 size_t NewSize =
200 CrossOver(Data, Size, Other.data(), Other.size(), U.data(), U.size());
201 assert(NewSize > 0 && "CrossOver returned empty unit");
202 assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
203 memcpy(Data, U.data(), NewSize);
204 return NewSize;
205}
206
Kostya Serebryany14c50282015-12-19 01:09:49 +0000207void MutationDispatcher::StartMutationSequence() {
208 MDImpl->CurrentMutatorSequence.clear();
209}
210
211void MutationDispatcher::PrintMutationSequence() {
212 Printf("MS: %zd ", MDImpl->CurrentMutatorSequence.size());
213 for (auto M : MDImpl->CurrentMutatorSequence)
214 Printf("%s-", M.Name);
215}
216
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000217// Mutates Data in place, returns new size.
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000218size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000219 assert(MaxSize > 0);
220 assert(Size <= MaxSize);
221 if (Size == 0) {
222 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000223 Data[i] = RandCh(Rand);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000224 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000225 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000226 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000227 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
228 // in which case they will return 0.
229 // Try several times before returning un-mutated data.
230 for (int Iter = 0; Iter < 10; Iter++) {
231 size_t MutatorIdx = Rand(MDImpl->Mutators.size());
Kostya Serebryany14c50282015-12-19 01:09:49 +0000232 auto M = MDImpl->Mutators[MutatorIdx];
233 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
234 if (NewSize) {
235 MDImpl->CurrentMutatorSequence.push_back(M);
236 return NewSize;
237 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000238 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000239 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000240}
241
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000242void MutationDispatcher::SetCorpus(const std::vector<Unit> *Corpus) {
243 MDImpl->SetCorpus(Corpus);
244}
245
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000246void MutationDispatcher::AddWordToManualDictionary(const Unit &Word) {
247 MDImpl->ManualDictionary.push_back(
248 {Word, std::numeric_limits<size_t>::max()});
249}
250
251void MutationDispatcher::AddWordToAutoDictionary(const Unit &Word,
252 size_t PositionHint) {
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000253 static const size_t kMaxAutoDictSize = 1 << 14;
254 if (MDImpl->AutoDictionary.size() >= kMaxAutoDictSize) return;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000255 MDImpl->AutoDictionary.push_back({Word, PositionHint});
Kostya Serebryany7d211662015-09-04 00:12:11 +0000256}
257
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000258void MutationDispatcher::ClearAutoDictionary() {
259 MDImpl->AutoDictionary.clear();
260}
261
Kostya Serebryany7d211662015-09-04 00:12:11 +0000262MutationDispatcher::MutationDispatcher(FuzzerRandomBase &Rand) : Rand(Rand) {
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000263 MDImpl = new Impl(Rand);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000264}
265
266MutationDispatcher::~MutationDispatcher() { delete MDImpl; }
267
Aaron Ballmanef116982015-01-29 16:58:29 +0000268} // namespace fuzzer