blob: 65e1650bfeab629ecb824921678710f426363dc0 [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
Aaron Ballmanef116982015-01-29 16:58:29 +000017namespace fuzzer {
18
Kostya Serebryany2f13f222016-01-21 01:52:14 +000019const size_t Dictionary::kMaxDictSize;
20
Mike Aizatskyf0b3e852016-06-23 20:44:48 +000021MutationDispatcher::MutationDispatcher(Random &Rand,
22 const FuzzingOptions &Options)
23 : Rand(Rand), Options(Options) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000024 DefaultMutators.insert(
25 DefaultMutators.begin(),
26 {
27 {&MutationDispatcher::Mutate_EraseByte, "EraseByte"},
28 {&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
29 {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
30 {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
31 {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
32 {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
33 {&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
34 {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
35 "AddFromManualDict"},
36 {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
37 "AddFromTempAutoDict"},
38 {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
39 "AddFromPersAutoDict"},
40 });
41
Dan Liew1873a492016-06-07 23:32:50 +000042 if (EF->LLVMFuzzerCustomMutator)
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000043 Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
44 else
45 Mutators = DefaultMutators;
Mike Aizatsky41d66832016-06-07 20:22:15 +000046
Dan Liew1873a492016-06-07 23:32:50 +000047 if (EF->LLVMFuzzerCustomCrossOver)
Mike Aizatsky41d66832016-06-07 20:22:15 +000048 Mutators.push_back(
49 {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000050}
Kostya Serebryany23194962016-02-13 03:46:26 +000051
Kostya Serebryanya3992212016-02-13 03:00:53 +000052static char FlipRandomBit(char X, Random &Rand) {
Kostya Serebryany404c69f2015-07-24 01:06:40 +000053 int Bit = Rand(8);
Aaron Ballmanef116982015-01-29 16:58:29 +000054 char Mask = 1 << Bit;
55 char R;
56 if (X & (1 << Bit))
57 R = X & ~Mask;
58 else
59 R = X | Mask;
60 assert(R != X);
61 return R;
62}
63
Kostya Serebryanya3992212016-02-13 03:00:53 +000064static char RandCh(Random &Rand) {
Kostya Serebryany404c69f2015-07-24 01:06:40 +000065 if (Rand.RandBool()) return Rand(256);
Aaron Ballmanef116982015-01-29 16:58:29 +000066 const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000067 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000068}
69
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000070size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
71 size_t MaxSize) {
Dan Liew1873a492016-06-07 23:32:50 +000072 return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
Mike Aizatsky70fd3e42016-06-03 21:34:29 +000073}
74
Mike Aizatsky41d66832016-06-07 20:22:15 +000075size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size,
76 size_t MaxSize) {
77 if (!Corpus || Corpus->size() < 2 || Size == 0)
78 return 0;
79 size_t Idx = Rand(Corpus->size());
80 const Unit &Other = (*Corpus)[Idx];
81 if (Other.empty())
82 return 0;
83 MutateInPlaceHere.resize(MaxSize);
84 auto &U = MutateInPlaceHere;
Dan Liew1873a492016-06-07 23:32:50 +000085 size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
Mike Aizatsky41d66832016-06-07 20:22:15 +000086 Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.Rand());
87 if (!NewSize)
88 return 0;
89 assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit");
90 memcpy(Data, U.data(), NewSize);
91 return NewSize;
92}
93
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000094size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
95 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000096 assert(Size);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000097 size_t ShuffleAmount =
98 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000099 size_t ShuffleStart = Rand(Size - ShuffleAmount);
100 assert(ShuffleStart + ShuffleAmount <= Size);
101 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
102 Rand);
103 return Size;
104}
105
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000106size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size,
107 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000108 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000109 if (Size == 1) return 0;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000110 size_t Idx = Rand(Size);
111 // Erase Data[Idx].
112 memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
113 return Size - 1;
114}
115
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000116size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
117 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000118 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000119 size_t Idx = Rand(Size + 1);
120 // Insert new value at Data[Idx].
121 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
122 Data[Idx] = RandCh(Rand);
123 return Size + 1;
124}
125
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000126size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
127 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000128 size_t Idx = Rand(Size);
129 Data[Idx] = RandCh(Rand);
130 return Size;
131}
132
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000133size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
134 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000135 size_t Idx = Rand(Size);
136 Data[Idx] = FlipRandomBit(Data[Idx], Rand);
137 return Size;
138}
139
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000140size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
141 size_t Size,
142 size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000143 return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000144}
145
Kostya Serebryany4b358742016-01-14 02:36:44 +0000146size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
147 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000148 return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000149}
150
151size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
152 uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000153 return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000154}
155
Kostya Serebryany292cf032016-02-13 03:37:24 +0000156size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
157 size_t Size, size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000158 if (D.empty()) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000159 DictionaryEntry &DE = D[Rand(D.size())];
160 const Word &W = DE.GetW();
161 bool UsePositionHint = DE.HasPositionHint() &&
162 DE.GetPositionHint() + W.size() < Size && Rand.RandBool();
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000163 if (Rand.RandBool()) { // Insert W.
164 if (Size + W.size() > MaxSize) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000165 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000166 memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);
167 memcpy(Data + Idx, W.data(), W.size());
168 Size += W.size();
169 } else { // Overwrite some bytes with W.
170 if (W.size() > Size) return 0;
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000171 size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000172 memcpy(Data + Idx, W.data(), W.size());
Kostya Serebryany80eb76a2016-01-06 02:13:04 +0000173 }
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000174 DE.IncUseCount();
175 CurrentDictionaryEntrySequence.push_back(&DE);
Kostya Serebryany41740052016-01-12 02:36:59 +0000176 return Size;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000177}
178
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000179size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
180 size_t MaxSize) {
181 size_t B = Rand(Size);
182 while (B < Size && !isdigit(Data[B])) B++;
183 if (B == Size) return 0;
184 size_t E = B;
185 while (E < Size && isdigit(Data[E])) E++;
186 assert(B < E);
187 // now we have digits in [B, E).
188 // strtol and friends don't accept non-zero-teminated data, parse it manually.
189 uint64_t Val = Data[B] - '0';
190 for (size_t i = B + 1; i < E; i++)
191 Val = Val * 10 + Data[i] - '0';
192
193 // Mutate the integer value.
194 switch(Rand(5)) {
195 case 0: Val++; break;
196 case 1: Val--; break;
197 case 2: Val /= 2; break;
198 case 3: Val *= 2; break;
199 case 4: Val = Rand(Val * Val); break;
200 default: assert(0);
201 }
202 // Just replace the bytes with the new ones, don't bother moving bytes.
203 for (size_t i = B; i < E; i++) {
204 size_t Idx = E + B - i - 1;
205 assert(Idx >= B && Idx < E);
206 Data[Idx] = (Val % 10) + '0';
207 Val /= 10;
208 }
209 return Size;
210}
211
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000212size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
213 size_t MaxSize) {
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000214 if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
215 size_t Idx = Rand(Corpus->size());
216 const Unit &Other = (*Corpus)[Idx];
217 if (Other.empty()) return 0;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000218 MutateInPlaceHere.resize(MaxSize);
219 auto &U = MutateInPlaceHere;
Kostya Serebryany27ab2d72015-12-19 02:49:09 +0000220 size_t NewSize =
221 CrossOver(Data, Size, Other.data(), Other.size(), U.data(), U.size());
222 assert(NewSize > 0 && "CrossOver returned empty unit");
223 assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
224 memcpy(Data, U.data(), NewSize);
225 return NewSize;
226}
227
Kostya Serebryany14c50282015-12-19 01:09:49 +0000228void MutationDispatcher::StartMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000229 CurrentMutatorSequence.clear();
230 CurrentDictionaryEntrySequence.clear();
Kostya Serebryany14c50282015-12-19 01:09:49 +0000231}
232
Kostya Serebryany4b358742016-01-14 02:36:44 +0000233// Copy successful dictionary entries to PersistentAutoDictionary.
234void MutationDispatcher::RecordSuccessfulMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000235 for (auto DE : CurrentDictionaryEntrySequence) {
236 // PersistentAutoDictionary.AddWithSuccessCountOne(DE);
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000237 DE->IncSuccessCount();
Kostya Serebryany4b358742016-01-14 02:36:44 +0000238 // Linear search is fine here as this happens seldom.
Kostya Serebryany292cf032016-02-13 03:37:24 +0000239 if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
240 PersistentAutoDictionary.push_back({DE->GetW(), 1});
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000241 }
Kostya Serebryany4b358742016-01-14 02:36:44 +0000242}
243
244void MutationDispatcher::PrintRecommendedDictionary() {
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000245 std::vector<DictionaryEntry> V;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000246 for (auto &DE : PersistentAutoDictionary)
247 if (!ManualDictionary.ContainsWord(DE.GetW()))
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000248 V.push_back(DE);
Kostya Serebryany4b358742016-01-14 02:36:44 +0000249 if (V.empty()) return;
250 Printf("###### Recommended dictionary. ######\n");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000251 for (auto &DE: V) {
Kostya Serebryany4b358742016-01-14 02:36:44 +0000252 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000253 PrintASCII(DE.GetW(), "\"");
254 Printf(" # Uses: %zd\n", DE.GetUseCount());
Kostya Serebryany4b358742016-01-14 02:36:44 +0000255 }
256 Printf("###### End of recommended dictionary. ######\n");
257}
258
Kostya Serebryany14c50282015-12-19 01:09:49 +0000259void MutationDispatcher::PrintMutationSequence() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000260 Printf("MS: %zd ", CurrentMutatorSequence.size());
261 for (auto M : CurrentMutatorSequence)
Kostya Serebryany14c50282015-12-19 01:09:49 +0000262 Printf("%s-", M.Name);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000263 if (!CurrentDictionaryEntrySequence.empty()) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000264 Printf(" DE: ");
Kostya Serebryany292cf032016-02-13 03:37:24 +0000265 for (auto DE : CurrentDictionaryEntrySequence) {
Kostya Serebryany41740052016-01-12 02:36:59 +0000266 Printf("\"");
Kostya Serebryany160dcba2016-01-22 23:55:14 +0000267 PrintASCII(DE->GetW(), "\"-");
Kostya Serebryany41740052016-01-12 02:36:59 +0000268 }
269 }
Kostya Serebryany14c50282015-12-19 01:09:49 +0000270}
271
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000272size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000273 return MutateImpl(Data, Size, MaxSize, Mutators);
274}
275
276size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size,
277 size_t MaxSize) {
278 return MutateImpl(Data, Size, MaxSize, DefaultMutators);
279}
280
281// Mutates Data in place, returns new size.
282size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
283 size_t MaxSize,
284 const std::vector<Mutator> &Mutators) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000285 assert(MaxSize > 0);
286 assert(Size <= MaxSize);
287 if (Size == 0) {
288 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000289 Data[i] = RandCh(Rand);
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000290 if (Options.OnlyASCII)
291 ToASCII(Data, MaxSize);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000292 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000293 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000294 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000295 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
296 // in which case they will return 0.
297 // Try several times before returning un-mutated data.
298 for (int Iter = 0; Iter < 10; Iter++) {
Mike Aizatsky70fd3e42016-06-03 21:34:29 +0000299 auto M = Mutators[Rand(Mutators.size())];
Kostya Serebryany14c50282015-12-19 01:09:49 +0000300 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
301 if (NewSize) {
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000302 if (Options.OnlyASCII)
303 ToASCII(Data, NewSize);
Kostya Serebryany292cf032016-02-13 03:37:24 +0000304 CurrentMutatorSequence.push_back(M);
Kostya Serebryany14c50282015-12-19 01:09:49 +0000305 return NewSize;
306 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000307 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000308 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000309}
310
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000311void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000312 ManualDictionary.push_back(
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000313 {W, std::numeric_limits<size_t>::max()});
Kostya Serebryany152ac7a2016-01-07 01:49:35 +0000314}
315
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000316void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) {
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000317 static const size_t kMaxAutoDictSize = 1 << 14;
Kostya Serebryany292cf032016-02-13 03:37:24 +0000318 if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000319 TempAutoDictionary.push_back(DE);
Kostya Serebryany7d211662015-09-04 00:12:11 +0000320}
321
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000322void MutationDispatcher::ClearAutoDictionary() {
Kostya Serebryany292cf032016-02-13 03:37:24 +0000323 TempAutoDictionary.clear();
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000324}
325
Aaron Ballmanef116982015-01-29 16:58:29 +0000326} // namespace fuzzer