1 //===- FuzzerMutate.cpp - Mutate a test input -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Mutate a test input.
10 //===----------------------------------------------------------------------===//
14 #include "FuzzerInternal.h"
21 size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
25 struct DictionaryEntry {
30 struct MutationDispatcher::Impl {
31 std::vector<DictionaryEntry> ManualDictionary;
32 std::vector<DictionaryEntry> AutoDictionary;
33 std::vector<Mutator> Mutators;
34 std::vector<Mutator> CurrentMutatorSequence;
35 const std::vector<Unit> *Corpus = nullptr;
36 FuzzerRandomBase &Rand;
38 void Add(Mutator M) { Mutators.push_back(M); }
39 Impl(FuzzerRandomBase &Rand) : Rand(Rand) {
40 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"});
46 Add({&MutationDispatcher::Mutate_CrossOver, "CrossOver"});
47 Add({&MutationDispatcher::Mutate_AddWordFromManualDictionary,
48 "AddFromManualDict"});
49 Add({&MutationDispatcher::Mutate_AddWordFromAutoDictionary,
52 void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
53 size_t AddWordFromDictionary(const std::vector<DictionaryEntry> &D,
54 uint8_t *Data, size_t Size, size_t MaxSize);
57 static char FlipRandomBit(char X, FuzzerRandomBase &Rand) {
69 static char RandCh(FuzzerRandomBase &Rand) {
70 if (Rand.RandBool()) return Rand(256);
71 const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
72 return Special[Rand(sizeof(Special) - 1)];
75 size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
78 size_t ShuffleAmount =
79 Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
80 size_t ShuffleStart = Rand(Size - ShuffleAmount);
81 assert(ShuffleStart + ShuffleAmount <= Size);
82 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
87 size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size,
90 if (Size == 1) return 0;
91 size_t Idx = Rand(Size);
93 memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
97 size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
99 if (Size == MaxSize) return 0;
100 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);
107 size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
109 size_t Idx = Rand(Size);
110 Data[Idx] = RandCh(Rand);
114 size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
116 size_t Idx = Rand(Size);
117 Data[Idx] = FlipRandomBit(Data[Idx], Rand);
121 size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
124 return MDImpl->AddWordFromDictionary(MDImpl->ManualDictionary, Data, Size,
128 size_t MutationDispatcher::Mutate_AddWordFromAutoDictionary(uint8_t *Data,
131 return MDImpl->AddWordFromDictionary(MDImpl->AutoDictionary, Data, Size,
135 size_t MutationDispatcher::Impl::AddWordFromDictionary(
136 const std::vector<DictionaryEntry> &D, uint8_t *Data, size_t Size,
138 if (D.empty()) return 0;
139 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();
144 if (Rand.RandBool()) { // Insert Word.
145 if (Size + Word.size() > MaxSize) return 0;
146 size_t Idx = UsePositionHint ? PositionHint : Rand(Size + 1);
147 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;
152 size_t Idx = UsePositionHint ? PositionHint : Rand(Size - Word.size());
153 memcpy(Data + Idx, Word.data(), Word.size());
158 size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
160 size_t B = Rand(Size);
161 while (B < Size && !isdigit(Data[B])) B++;
162 if (B == Size) return 0;
164 while (E < Size && isdigit(Data[E])) 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';
172 // Mutate the integer value.
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;
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';
191 size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
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;
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);
207 void MutationDispatcher::StartMutationSequence() {
208 MDImpl->CurrentMutatorSequence.clear();
211 void MutationDispatcher::PrintMutationSequence() {
212 Printf("MS: %zd ", MDImpl->CurrentMutatorSequence.size());
213 for (auto M : MDImpl->CurrentMutatorSequence)
214 Printf("%s-", M.Name);
217 // Mutates Data in place, returns new size.
218 size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
220 assert(Size <= MaxSize);
222 for (size_t i = 0; i < MaxSize; i++)
223 Data[i] = RandCh(Rand);
227 // 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());
232 auto M = MDImpl->Mutators[MutatorIdx];
233 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
235 MDImpl->CurrentMutatorSequence.push_back(M);
242 void MutationDispatcher::SetCorpus(const std::vector<Unit> *Corpus) {
243 MDImpl->SetCorpus(Corpus);
246 void MutationDispatcher::AddWordToManualDictionary(const Unit &Word) {
247 MDImpl->ManualDictionary.push_back(
248 {Word, std::numeric_limits<size_t>::max()});
251 void MutationDispatcher::AddWordToAutoDictionary(const Unit &Word,
252 size_t PositionHint) {
253 static const size_t kMaxAutoDictSize = 1 << 14;
254 if (MDImpl->AutoDictionary.size() >= kMaxAutoDictSize) return;
255 MDImpl->AutoDictionary.push_back({Word, PositionHint});
258 void MutationDispatcher::ClearAutoDictionary() {
259 MDImpl->AutoDictionary.clear();
262 MutationDispatcher::MutationDispatcher(FuzzerRandomBase &Rand) : Rand(Rand) {
263 MDImpl = new Impl(Rand);
266 MutationDispatcher::~MutationDispatcher() { delete MDImpl; }
268 } // namespace fuzzer