blob: aad3f28f71257ced440a7670d8bd66e70c549344 [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 Serebryany160dcba2016-01-22 23:55:14 +000025class DictionaryEntry {
26 public:
27 DictionaryEntry() {}
28 DictionaryEntry(Word W) : W(W) {}
29 DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
30 const Word &GetW() const { return W; }
31
32 bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
33 size_t GetPositionHint() const {
34 assert(HasPositionHint());
35 return PositionHint;
36 }
37 void IncUseCount() { UseCount++; }
38 void IncSuccessCount() { SuccessCount++; }
39 size_t GetUseCount() const { return UseCount; }
40 size_t GetSuccessCount() const {return SuccessCount; }
41
42private:
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000043 Word W;
Kostya Serebryany160dcba2016-01-22 23:55:14 +000044 size_t PositionHint = std::numeric_limits<size_t>::max();
45 size_t UseCount = 0;
46 size_t SuccessCount = 0;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000047};
48
Kostya Serebryany2f13f222016-01-21 01:52:14 +000049class Dictionary {
50 public:
51 static const size_t kMaxDictSize = 1 << 14;
52
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000053 bool ContainsWord(const Word &W) const {
Kostya Serebryany160dcba2016-01-22 23:55:14 +000054 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
55 return DE.GetW() == W;
56 });
Kostya Serebryany4b358742016-01-14 02:36:44 +000057 }
Kostya Serebryany2f13f222016-01-21 01:52:14 +000058 const DictionaryEntry *begin() const { return &DE[0]; }
59 const DictionaryEntry *end() const { return begin() + Size; }
Kostya Serebryany160dcba2016-01-22 23:55:14 +000060 DictionaryEntry & operator[] (size_t Idx) {
Kostya Serebryany2f13f222016-01-21 01:52:14 +000061 assert(Idx < Size);
62 return DE[Idx];
63 }
64 void push_back(DictionaryEntry DE) {
65 if (Size < kMaxDictSize)
66 this->DE[Size++] = DE;
67 }
68 void clear() { Size = 0; }
69 bool empty() const { return Size == 0; }
70 size_t size() const { return Size; }
71
72private:
73 DictionaryEntry DE[kMaxDictSize];
74 size_t Size = 0;
Kostya Serebryany4b358742016-01-14 02:36:44 +000075};
76
Kostya Serebryany2f13f222016-01-21 01:52:14 +000077const size_t Dictionary::kMaxDictSize;
78
Kostya Serebryany7d211662015-09-04 00:12:11 +000079struct MutationDispatcher::Impl {
Kostya Serebryany4b358742016-01-14 02:36:44 +000080 // Dictionary provided by the user via -dict=DICT_FILE.
81 Dictionary ManualDictionary;
82 // Temporary dictionary modified by the fuzzer itself,
83 // recreated periodically.
84 Dictionary TempAutoDictionary;
85 // Persistent dictionary modified by the fuzzer, consists of
86 // entries that led to successfull discoveries in the past mutations.
87 Dictionary PersistentAutoDictionary;
88
Kostya Serebryany7d211662015-09-04 00:12:11 +000089 std::vector<Mutator> Mutators;
Kostya Serebryany14c50282015-12-19 01:09:49 +000090 std::vector<Mutator> CurrentMutatorSequence;
Kostya Serebryany160dcba2016-01-22 23:55:14 +000091 std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
Kostya Serebryany27ab2d72015-12-19 02:49:09 +000092 const std::vector<Unit> *Corpus = nullptr;
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000093 FuzzerRandomBase &Rand;
Kostya Serebryany14c50282015-12-19 01:09:49 +000094
95 void Add(Mutator M) { Mutators.push_back(M); }
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000096 Impl(FuzzerRandomBase &Rand) : Rand(Rand) {
Kostya Serebryany14c50282015-12-19 01:09:49 +000097 Add({&MutationDispatcher::Mutate_EraseByte, "EraseByte"});
98 Add({&MutationDispatcher::Mutate_InsertByte, "InsertByte"});
99 Add({&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"});
100 Add({&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"});
101 Add({&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"});
102 Add({&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"});
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000103 Add({&MutationDispatcher::Mutate_CrossOver, "CrossOver"});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000104 Add({&MutationDispatcher::Mutate_AddWordFromManualDictionary,
105 "AddFromManualDict"});
Kostya Serebryany4b358742016-01-14 02:36:44 +0000106 Add({&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
107 "AddFromTempAutoDict"});
108 Add({&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
109 "AddFromPersAutoDict"});
Kostya Serebryany7d211662015-09-04 00:12:11 +0000110 }
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000111 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000112 size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
Kostya Serebryany2f13f222016-01-21 01:52:14 +0000113 size_t MaxSize);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000114};
115
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000116static char FlipRandomBit(char X, FuzzerRandomBase &Rand) {
117 int Bit = Rand(8);
Aaron Ballmanef116982015-01-29 16:58:29 +0000118 char Mask = 1 << Bit;
119 char R;
120 if (X & (1 << Bit))
121 R = X & ~Mask;
122 else
123 R = X | Mask;
124 assert(R != X);
125 return R;
126}
127
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000128static char RandCh(FuzzerRandomBase &Rand) {
129 if (Rand.RandBool()) return Rand(256);
Aaron Ballmanef116982015-01-29 16:58:29 +0000130 const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000131 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +0000132}
133
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000134size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
135 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +0000136 assert(Size);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000137 size_t ShuffleAmount =
138 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +0000139 size_t ShuffleStart = Rand(Size - ShuffleAmount);
140 assert(ShuffleStart + ShuffleAmount <= Size);
141 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
142 Rand);
143 return Size;
144}
145
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000146size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size,
147 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000148 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000149 if (Size == 1) return 0;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000150 size_t Idx = Rand(Size);
151 // Erase Data[Idx].
152 memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
153 return Size - 1;
154}
155
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000156size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
157 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000158 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000159 size_t Idx = Rand(Size + 1);
160 // Insert new value at Data[Idx].
161 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
162 Data[Idx] = RandCh(Rand);
163 return Size + 1;
164}
165
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000166size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
167 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000168 size_t Idx = Rand(Size);
169 Data[Idx] = RandCh(Rand);
170 return Size;
171}
172
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000173size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
174 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000175 size_t Idx = Rand(Size);
176 Data[Idx] = FlipRandomBit(Data[Idx], Rand);
177 return Size;
178}
179
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000180size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
181 size_t Size,
182 size_t MaxSize) {
183 return MDImpl->AddWordFromDictionary(MDImpl->ManualDictionary, Data, Size,
184 MaxSize);
185}
186
Kostya Serebryany4b358742016-01-14 02:36:44 +0000187size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
188 uint8_t *Data, size_t Size, size_t MaxSize) {
189 return MDImpl->AddWordFromDictionary(MDImpl->TempAutoDictionary, Data, Size,
190 MaxSize);
191}
192
193size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
194 uint8_t *Data, size_t Size, size_t MaxSize) {
195 return MDImpl->AddWordFromDictionary(MDImpl->PersistentAutoDictionary, Data, Size,
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000196 MaxSize);
197}
198
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000199size_t MutationDispatcher::Impl::AddWordFromDictionary(Dictionary &D,
Kostya Serebryany2f13f222016-01-21 01:52:14 +0000200 uint8_t *Data,
201 size_t Size,
202 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000203 if (D.empty()) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000204 DictionaryEntry &DE = D[Rand(D.size())];
205 const Word &W = DE.GetW();
206 bool UsePositionHint = DE.HasPositionHint() &&
207 DE.GetPositionHint() + W.size() < Size && Rand.RandBool();
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000208 if (Rand.RandBool()) { // Insert W.
209 if (Size + W.size() > MaxSize) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000210 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000211 memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);
212 memcpy(Data + Idx, W.data(), W.size());
213 Size += W.size();
214 } else { // Overwrite some bytes with W.
215 if (W.size() > Size) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000216 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000217 memcpy(Data + Idx, W.data(), W.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000218 }
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000219 DE.IncUseCount();
220 CurrentDictionaryEntrySequence.push_back(&DE);
Kostya Serebryany41740052016-01-12 02:36:59 +0000221 return Size;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000222}
223
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000224size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
225 size_t MaxSize) {
226 size_t B = Rand(Size);
227 while (B < Size && !isdigit(Data[B])) B++;
228 if (B == Size) return 0;
229 size_t E = B;
230 while (E < Size && isdigit(Data[E])) E++;
231 assert(B < E);
232 // now we have digits in [B, E).
233 // strtol and friends don't accept non-zero-teminated data, parse it manually.
234 uint64_t Val = Data[B] - '0';
235 for (size_t i = B + 1; i < E; i++)
236 Val = Val * 10 + Data[i] - '0';
237
238 // Mutate the integer value.
239 switch(Rand(5)) {
240 case 0: Val++; break;
241 case 1: Val--; break;
242 case 2: Val /= 2; break;
243 case 3: Val *= 2; break;
244 case 4: Val = Rand(Val * Val); break;
245 default: assert(0);
246 }
247 // Just replace the bytes with the new ones, don't bother moving bytes.
248 for (size_t i = B; i < E; i++) {
249 size_t Idx = E + B - i - 1;
250 assert(Idx >= B && Idx < E);
251 Data[Idx] = (Val % 10) + '0';
252 Val /= 10;
253 }
254 return Size;
255}
256
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000257size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
258 size_t MaxSize) {
259 auto Corpus = MDImpl->Corpus;
260 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
261 size_t Idx = Rand(Corpus->size());
262 const Unit &Other = (*Corpus)[Idx];
263 if (Other.empty()) return 0;
264 Unit U(MaxSize);
265 size_t NewSize =
266 CrossOver(Data, Size, Other.data(), Other.size(), U.data(), U.size());
267 assert(NewSize > 0 && "CrossOver returned empty unit");
268 assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
269 memcpy(Data, U.data(), NewSize);
270 return NewSize;
271}
272
Kostya Serebryany14c50282015-12-19 01:09:49 +0000273void MutationDispatcher::StartMutationSequence() {
274 MDImpl->CurrentMutatorSequence.clear();
Kostya Serebryany41740052016-01-12 02:36:59 +0000275 MDImpl->CurrentDictionaryEntrySequence.clear();
Kostya Serebryany14c50282015-12-19 01:09:49 +0000276}
277
Kostya Serebryany4b358742016-01-14 02:36:44 +0000278// Copy successful dictionary entries to PersistentAutoDictionary.
279void MutationDispatcher::RecordSuccessfulMutationSequence() {
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000280 for (auto DE : MDImpl->CurrentDictionaryEntrySequence) {
281 // MDImpl->PersistentAutoDictionary.AddWithSuccessCountOne(DE);
282 DE->IncSuccessCount();
Kostya Serebryany4b358742016-01-14 02:36:44 +0000283 // Linear search is fine here as this happens seldom.
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000284 if (!MDImpl->PersistentAutoDictionary.ContainsWord(DE->GetW()))
285 MDImpl->PersistentAutoDictionary.push_back({DE->GetW(), 1});
286 }
Kostya Serebryany4b358742016-01-14 02:36:44 +0000287}
288
289void MutationDispatcher::PrintRecommendedDictionary() {
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000290 std::vector<DictionaryEntry> V;
Kostya Serebryany4b358742016-01-14 02:36:44 +0000291 for (auto &DE : MDImpl->PersistentAutoDictionary)
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000292 if (!MDImpl->ManualDictionary.ContainsWord(DE.GetW()))
293 V.push_back(DE);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000294 if (V.empty()) return;
295 Printf("###### Recommended dictionary. ######\n");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000296 for (auto &DE: V) {
Kostya Serebryany4b358742016-01-14 02:36:44 +0000297 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000298 PrintASCII(DE.GetW(), "\"");
299 Printf(" # Uses: %zd\n", DE.GetUseCount());
Kostya Serebryany4b358742016-01-14 02:36:44 +0000300 }
301 Printf("###### End of recommended dictionary. ######\n");
302}
303
Kostya Serebryany14c50282015-12-19 01:09:49 +0000304void MutationDispatcher::PrintMutationSequence() {
305 Printf("MS: %zd ", MDImpl->CurrentMutatorSequence.size());
306 for (auto M : MDImpl->CurrentMutatorSequence)
307 Printf("%s-", M.Name);
Kostya Serebryany41740052016-01-12 02:36:59 +0000308 if (!MDImpl->CurrentDictionaryEntrySequence.empty()) {
309 Printf(" DE: ");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000310 for (auto DE : MDImpl->CurrentDictionaryEntrySequence) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000311 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000312 PrintASCII(DE->GetW(), "\"-");
Kostya Serebryany41740052016-01-12 02:36:59 +0000313 }
314 }
Kostya Serebryany14c50282015-12-19 01:09:49 +0000315}
316
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000317// Mutates Data in place, returns new size.
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000318size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000319 assert(MaxSize > 0);
320 assert(Size <= MaxSize);
321 if (Size == 0) {
322 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000323 Data[i] = RandCh(Rand);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000324 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000325 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000326 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000327 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
328 // in which case they will return 0.
329 // Try several times before returning un-mutated data.
330 for (int Iter = 0; Iter < 10; Iter++) {
331 size_t MutatorIdx = Rand(MDImpl->Mutators.size());
Kostya Serebryany14c50282015-12-19 01:09:49 +0000332 auto M = MDImpl->Mutators[MutatorIdx];
333 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
334 if (NewSize) {
335 MDImpl->CurrentMutatorSequence.push_back(M);
336 return NewSize;
337 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000338 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000339 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000340}
341
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000342void MutationDispatcher::SetCorpus(const std::vector<Unit> *Corpus) {
343 MDImpl->SetCorpus(Corpus);
344}
345
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000346void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000347 MDImpl->ManualDictionary.push_back(
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000348 {W, std::numeric_limits<size_t>::max()});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000349}
350
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000351void MutationDispatcher::AddWordToAutoDictionary(const Word &W,
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000352 size_t PositionHint) {
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000353 static const size_t kMaxAutoDictSize = 1 << 14;
Kostya Serebryany4b358742016-01-14 02:36:44 +0000354 if (MDImpl->TempAutoDictionary.size() >= kMaxAutoDictSize) return;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000355 MDImpl->TempAutoDictionary.push_back({W, PositionHint});
Kostya Serebryany7d211662015-09-04 00:12:11 +0000356}
357
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000358void MutationDispatcher::ClearAutoDictionary() {
Kostya Serebryany4b358742016-01-14 02:36:44 +0000359 MDImpl->TempAutoDictionary.clear();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000360}
361
Kostya Serebryany7d211662015-09-04 00:12:11 +0000362MutationDispatcher::MutationDispatcher(FuzzerRandomBase &Rand) : Rand(Rand) {
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000363 MDImpl = new Impl(Rand);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000364}
365
366MutationDispatcher::~MutationDispatcher() { delete MDImpl; }
367
Aaron Ballmanef116982015-01-29 16:58:29 +0000368} // namespace fuzzer