blob: 471ae6ce197102bf4ebe5f6bb8981c6e14c1598d [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
25struct MutationDispatcher::Impl {
26 std::vector<Unit> Dictionary;
27 std::vector<Mutator> Mutators;
Kostya Serebryany14c50282015-12-19 01:09:49 +000028 std::vector<Mutator> CurrentMutatorSequence;
29
30 void Add(Mutator M) { Mutators.push_back(M); }
Kostya Serebryany7d211662015-09-04 00:12:11 +000031 Impl() {
Kostya Serebryany14c50282015-12-19 01:09:49 +000032 Add({&MutationDispatcher::Mutate_EraseByte, "EraseByte"});
33 Add({&MutationDispatcher::Mutate_InsertByte, "InsertByte"});
34 Add({&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"});
35 Add({&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"});
36 Add({&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"});
37 Add({&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"});
Kostya Serebryany7d211662015-09-04 00:12:11 +000038 }
39 void AddWordToDictionary(const uint8_t *Word, size_t Size) {
40 if (Dictionary.empty()) {
Kostya Serebryany14c50282015-12-19 01:09:49 +000041 Add({&MutationDispatcher::Mutate_AddWordFromDictionary, "AddFromDict"});
Kostya Serebryany7d211662015-09-04 00:12:11 +000042 }
43 Dictionary.push_back(Unit(Word, Word + Size));
44 }
45};
46
Kostya Serebryany404c69f2015-07-24 01:06:40 +000047static char FlipRandomBit(char X, FuzzerRandomBase &Rand) {
48 int Bit = Rand(8);
Aaron Ballmanef116982015-01-29 16:58:29 +000049 char Mask = 1 << Bit;
50 char R;
51 if (X & (1 << Bit))
52 R = X & ~Mask;
53 else
54 R = X | Mask;
55 assert(R != X);
56 return R;
57}
58
Kostya Serebryany404c69f2015-07-24 01:06:40 +000059static char RandCh(FuzzerRandomBase &Rand) {
60 if (Rand.RandBool()) return Rand(256);
Aaron Ballmanef116982015-01-29 16:58:29 +000061 const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
Kostya Serebryany404c69f2015-07-24 01:06:40 +000062 return Special[Rand(sizeof(Special) - 1)];
Aaron Ballmanef116982015-01-29 16:58:29 +000063}
64
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000065size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
66 size_t MaxSize) {
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000067 assert(Size);
Kostya Serebryany3b804872015-10-08 00:59:25 +000068 size_t ShuffleAmount = Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000069 size_t ShuffleStart = Rand(Size - ShuffleAmount);
70 assert(ShuffleStart + ShuffleAmount <= Size);
71 std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
72 Rand);
73 return Size;
74}
75
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000076size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size,
77 size_t MaxSize) {
Kostya Serebryany8ce74242015-08-01 01:42:51 +000078 assert(Size);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +000079 if (Size == 1) return 0;
Kostya Serebryany8ce74242015-08-01 01:42:51 +000080 size_t Idx = Rand(Size);
81 // Erase Data[Idx].
82 memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
83 return Size - 1;
84}
85
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000086size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
87 size_t MaxSize) {
Kostya Serebryanyb2e98972015-09-04 00:40:29 +000088 if (Size == MaxSize) return 0;
Kostya Serebryany86a5fba2015-08-01 02:23:06 +000089 size_t Idx = Rand(Size + 1);
90 // Insert new value at Data[Idx].
91 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
92 Data[Idx] = RandCh(Rand);
93 return Size + 1;
94}
95
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000096size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
97 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +000098 size_t Idx = Rand(Size);
99 Data[Idx] = RandCh(Rand);
100 return Size;
101}
102
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000103size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
104 size_t MaxSize) {
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000105 size_t Idx = Rand(Size);
106 Data[Idx] = FlipRandomBit(Data[Idx], Rand);
107 return Size;
108}
109
Kostya Serebryany7d211662015-09-04 00:12:11 +0000110size_t MutationDispatcher::Mutate_AddWordFromDictionary(uint8_t *Data,
111 size_t Size,
112 size_t MaxSize) {
113 auto &D = MDImpl->Dictionary;
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000114 assert(!D.empty());
115 if (D.empty()) return 0;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000116 const Unit &Word = D[Rand(D.size())];
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000117 if (Size + Word.size() > MaxSize) return 0;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000118 size_t Idx = Rand(Size + 1);
119 memmove(Data + Idx + Word.size(), Data + Idx, Size - Idx);
120 memcpy(Data + Idx, Word.data(), Word.size());
121 return Size + Word.size();
122}
123
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000124size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
125 size_t MaxSize) {
126 size_t B = Rand(Size);
127 while (B < Size && !isdigit(Data[B])) B++;
128 if (B == Size) return 0;
129 size_t E = B;
130 while (E < Size && isdigit(Data[E])) E++;
131 assert(B < E);
132 // now we have digits in [B, E).
133 // strtol and friends don't accept non-zero-teminated data, parse it manually.
134 uint64_t Val = Data[B] - '0';
135 for (size_t i = B + 1; i < E; i++)
136 Val = Val * 10 + Data[i] - '0';
137
138 // Mutate the integer value.
139 switch(Rand(5)) {
140 case 0: Val++; break;
141 case 1: Val--; break;
142 case 2: Val /= 2; break;
143 case 3: Val *= 2; break;
144 case 4: Val = Rand(Val * Val); break;
145 default: assert(0);
146 }
147 // Just replace the bytes with the new ones, don't bother moving bytes.
148 for (size_t i = B; i < E; i++) {
149 size_t Idx = E + B - i - 1;
150 assert(Idx >= B && Idx < E);
151 Data[Idx] = (Val % 10) + '0';
152 Val /= 10;
153 }
154 return Size;
155}
156
Kostya Serebryany14c50282015-12-19 01:09:49 +0000157void MutationDispatcher::StartMutationSequence() {
158 MDImpl->CurrentMutatorSequence.clear();
159}
160
161void MutationDispatcher::PrintMutationSequence() {
162 Printf("MS: %zd ", MDImpl->CurrentMutatorSequence.size());
163 for (auto M : MDImpl->CurrentMutatorSequence)
164 Printf("%s-", M.Name);
165}
166
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000167// Mutates Data in place, returns new size.
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000168size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000169 assert(MaxSize > 0);
170 assert(Size <= MaxSize);
171 if (Size == 0) {
172 for (size_t i = 0; i < MaxSize; i++)
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000173 Data[i] = RandCh(Rand);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000174 return MaxSize;
Kostya Serebryany5b266a82015-02-04 19:10:20 +0000175 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000176 assert(Size > 0);
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000177 // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
178 // in which case they will return 0.
179 // Try several times before returning un-mutated data.
180 for (int Iter = 0; Iter < 10; Iter++) {
181 size_t MutatorIdx = Rand(MDImpl->Mutators.size());
Kostya Serebryany14c50282015-12-19 01:09:49 +0000182 auto M = MDImpl->Mutators[MutatorIdx];
183 size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
184 if (NewSize) {
185 MDImpl->CurrentMutatorSequence.push_back(M);
186 return NewSize;
187 }
Kostya Serebryanyb2e98972015-09-04 00:40:29 +0000188 }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000189 return Size;
Aaron Ballmanef116982015-01-29 16:58:29 +0000190}
191
Kostya Serebryany7d211662015-09-04 00:12:11 +0000192void MutationDispatcher::AddWordToDictionary(const uint8_t *Word, size_t Size) {
193 MDImpl->AddWordToDictionary(Word, Size);
194}
195
196MutationDispatcher::MutationDispatcher(FuzzerRandomBase &Rand) : Rand(Rand) {
197 MDImpl = new Impl;
198}
199
200MutationDispatcher::~MutationDispatcher() { delete MDImpl; }
201
Aaron Ballmanef116982015-01-29 16:58:29 +0000202} // namespace fuzzer