Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 1 | //===- 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 Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 12 | #include <cstring> |
| 13 | |
Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 14 | #include "FuzzerCorpus.h" |
| 15 | #include "FuzzerDefs.h" |
| 16 | #include "FuzzerExtFunctions.h" |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 17 | #include "FuzzerMutate.h" |
Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 18 | #include "FuzzerOptions.h" |
Kostya Serebryany | bf29ff2 | 2015-08-06 01:29:13 +0000 | [diff] [blame] | 19 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 20 | namespace fuzzer { |
| 21 | |
Kostya Serebryany | 2f13f22 | 2016-01-21 01:52:14 +0000 | [diff] [blame] | 22 | const size_t Dictionary::kMaxDictSize; |
| 23 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 24 | static void PrintASCII(const Word &W, const char *PrintAfter) { |
| 25 | PrintASCII(W.data(), W.size(), PrintAfter); |
| 26 | } |
| 27 | |
Mike Aizatsky | f0b3e85 | 2016-06-23 20:44:48 +0000 | [diff] [blame] | 28 | MutationDispatcher::MutationDispatcher(Random &Rand, |
| 29 | const FuzzingOptions &Options) |
| 30 | : Rand(Rand), Options(Options) { |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 31 | DefaultMutators.insert( |
| 32 | DefaultMutators.begin(), |
| 33 | { |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 34 | {&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"}, |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 35 | {&MutationDispatcher::Mutate_InsertByte, "InsertByte"}, |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 36 | {&MutationDispatcher::Mutate_InsertRepeatedBytes, |
| 37 | "InsertRepeatedBytes"}, |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 38 | {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"}, |
| 39 | {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"}, |
| 40 | {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"}, |
| 41 | {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"}, |
Kostya Serebryany | 0c537b1 | 2016-08-17 21:30:30 +0000 | [diff] [blame] | 42 | {&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"}, |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame] | 43 | {&MutationDispatcher::Mutate_CopyPart, "CopyPart"}, |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 44 | {&MutationDispatcher::Mutate_CrossOver, "CrossOver"}, |
| 45 | {&MutationDispatcher::Mutate_AddWordFromManualDictionary, |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 46 | "ManualDict"}, |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 47 | {&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary, |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 48 | "TempAutoDict"}, |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 49 | {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary, |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 50 | "PersAutoDict"}, |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 51 | }); |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 52 | if(Options.UseCmp) |
| 53 | DefaultMutators.push_back( |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame^] | 54 | {&MutationDispatcher::Mutate_AddWordFromTORC, "CMP"}); |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 55 | |
Dan Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 56 | if (EF->LLVMFuzzerCustomMutator) |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 57 | Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"}); |
| 58 | else |
| 59 | Mutators = DefaultMutators; |
Mike Aizatsky | 41d6683 | 2016-06-07 20:22:15 +0000 | [diff] [blame] | 60 | |
Dan Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 61 | if (EF->LLVMFuzzerCustomCrossOver) |
Mike Aizatsky | 41d6683 | 2016-06-07 20:22:15 +0000 | [diff] [blame] | 62 | Mutators.push_back( |
| 63 | {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"}); |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 64 | } |
Kostya Serebryany | 2319496 | 2016-02-13 03:46:26 +0000 | [diff] [blame] | 65 | |
Kostya Serebryany | a399221 | 2016-02-13 03:00:53 +0000 | [diff] [blame] | 66 | static char RandCh(Random &Rand) { |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 67 | if (Rand.RandBool()) return Rand(256); |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame] | 68 | const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00"; |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 69 | return Special[Rand(sizeof(Special) - 1)]; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 72 | size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size, |
| 73 | size_t MaxSize) { |
Dan Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 74 | return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand()); |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Mike Aizatsky | 41d6683 | 2016-06-07 20:22:15 +0000 | [diff] [blame] | 77 | size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size, |
| 78 | size_t MaxSize) { |
| 79 | if (!Corpus || Corpus->size() < 2 || Size == 0) |
| 80 | return 0; |
| 81 | size_t Idx = Rand(Corpus->size()); |
| 82 | const Unit &Other = (*Corpus)[Idx]; |
| 83 | if (Other.empty()) |
| 84 | return 0; |
| 85 | MutateInPlaceHere.resize(MaxSize); |
| 86 | auto &U = MutateInPlaceHere; |
Dan Liew | 1873a49 | 2016-06-07 23:32:50 +0000 | [diff] [blame] | 87 | size_t NewSize = EF->LLVMFuzzerCustomCrossOver( |
Mike Aizatsky | 41d6683 | 2016-06-07 20:22:15 +0000 | [diff] [blame] | 88 | Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.Rand()); |
| 89 | if (!NewSize) |
| 90 | return 0; |
| 91 | assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit"); |
| 92 | memcpy(Data, U.data(), NewSize); |
| 93 | return NewSize; |
| 94 | } |
| 95 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 96 | size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size, |
| 97 | size_t MaxSize) { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 98 | if (Size > MaxSize) return 0; |
Kostya Serebryany | bf29ff2 | 2015-08-06 01:29:13 +0000 | [diff] [blame] | 99 | assert(Size); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 100 | size_t ShuffleAmount = |
| 101 | Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size. |
Kostya Serebryany | bf29ff2 | 2015-08-06 01:29:13 +0000 | [diff] [blame] | 102 | size_t ShuffleStart = Rand(Size - ShuffleAmount); |
| 103 | assert(ShuffleStart + ShuffleAmount <= Size); |
| 104 | std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount, |
| 105 | Rand); |
| 106 | return Size; |
| 107 | } |
| 108 | |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 109 | size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size, |
| 110 | size_t MaxSize) { |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 111 | assert(Size); |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 112 | if (Size == 1) return 0; |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 113 | size_t N = Rand(Size / 2) + 1; |
| 114 | assert(N < Size); |
| 115 | size_t Idx = Rand(Size - N + 1); |
| 116 | // Erase Data[Idx:Idx+N]. |
| 117 | memmove(Data + Idx, Data + Idx + N, Size - Idx - N); |
| 118 | // Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx); |
| 119 | return Size - N; |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 122 | size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size, |
| 123 | size_t MaxSize) { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 124 | if (Size >= MaxSize) return 0; |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 125 | size_t Idx = Rand(Size + 1); |
| 126 | // Insert new value at Data[Idx]. |
| 127 | memmove(Data + Idx + 1, Data + Idx, Size - Idx); |
| 128 | Data[Idx] = RandCh(Rand); |
| 129 | return Size + 1; |
| 130 | } |
| 131 | |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 132 | size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data, |
| 133 | size_t Size, |
| 134 | size_t MaxSize) { |
| 135 | const size_t kMinBytesToInsert = 3; |
| 136 | if (Size + kMinBytesToInsert >= MaxSize) return 0; |
| 137 | size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128); |
| 138 | size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert; |
| 139 | assert(Size + N <= MaxSize && N); |
| 140 | size_t Idx = Rand(Size + 1); |
| 141 | // Insert new values at Data[Idx]. |
| 142 | memmove(Data + Idx + N, Data + Idx, Size - Idx); |
Kostya Serebryany | e72774d | 2016-08-17 21:50:54 +0000 | [diff] [blame] | 143 | // Give preference to 0x00 and 0xff. |
| 144 | uint8_t Byte = Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255); |
Kostya Serebryany | dfbe59b | 2016-08-15 17:48:28 +0000 | [diff] [blame] | 145 | for (size_t i = 0; i < N; i++) |
| 146 | Data[Idx + i] = Byte; |
| 147 | return Size + N; |
| 148 | } |
| 149 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 150 | size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size, |
| 151 | size_t MaxSize) { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 152 | if (Size > MaxSize) return 0; |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 153 | size_t Idx = Rand(Size); |
| 154 | Data[Idx] = RandCh(Rand); |
| 155 | return Size; |
| 156 | } |
| 157 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 158 | size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size, |
| 159 | size_t MaxSize) { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 160 | if (Size > MaxSize) return 0; |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 161 | size_t Idx = Rand(Size); |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame] | 162 | Data[Idx] ^= 1 << Rand(8); |
Kostya Serebryany | 86a5fba | 2015-08-01 02:23:06 +0000 | [diff] [blame] | 163 | return Size; |
| 164 | } |
| 165 | |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 166 | size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data, |
| 167 | size_t Size, |
| 168 | size_t MaxSize) { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 169 | return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 172 | size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary( |
| 173 | uint8_t *Data, size_t Size, size_t MaxSize) { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 174 | return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize); |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame^] | 177 | size_t MutationDispatcher::ApplyDictionaryEntry(uint8_t *Data, size_t Size, |
| 178 | size_t MaxSize, |
| 179 | DictionaryEntry &DE) { |
| 180 | const Word &W = DE.GetW(); |
| 181 | bool UsePositionHint = DE.HasPositionHint() && |
| 182 | DE.GetPositionHint() + W.size() < Size && |
| 183 | Rand.RandBool(); |
| 184 | if (Rand.RandBool()) { // Insert W. |
| 185 | if (Size + W.size() > MaxSize) return 0; |
| 186 | size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1); |
| 187 | memmove(Data + Idx + W.size(), Data + Idx, Size - Idx); |
| 188 | memcpy(Data + Idx, W.data(), W.size()); |
| 189 | Size += W.size(); |
| 190 | } else { // Overwrite some bytes with W. |
| 191 | if (W.size() > Size) return 0; |
| 192 | size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size()); |
| 193 | memcpy(Data + Idx, W.data(), W.size()); |
| 194 | } |
| 195 | return Size; |
| 196 | } |
| 197 | |
| 198 | // Somewhere in the past we have observed a comparison instructions |
| 199 | // with arguments Arg1 Arg2. This function tries to guess a dictionary |
| 200 | // entry that will satisfy that comparison. |
| 201 | // It first tries to find one of the arguments (possibly swapped) in the |
| 202 | // input and if it succeeds it creates a DE with a position hint. |
| 203 | // Otherwise it creates a DE with one of the arguments w/o a position hint. |
| 204 | template <class T> |
| 205 | DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP( |
| 206 | T Arg1, T Arg2, const uint8_t *Data, size_t Size) { |
| 207 | ScopedDoingMyOwnMemmem scoped_doing_my_own_memmem; |
| 208 | if (Rand.RandBool()) Arg1 = Bswap(Arg1); |
| 209 | if (Rand.RandBool()) Arg2 = Bswap(Arg2); |
| 210 | bool HandleFirst = Rand.RandBool(); |
| 211 | T ExistingBytes, DesiredBytes; |
| 212 | Word W; |
| 213 | const uint8_t *End = Data + Size; |
| 214 | for (int Arg = 0; Arg < 2; Arg++) { |
| 215 | ExistingBytes = HandleFirst ? Arg1 : Arg2; |
| 216 | DesiredBytes = HandleFirst ? Arg2 : Arg1; |
| 217 | HandleFirst = !HandleFirst; |
| 218 | W.Set(reinterpret_cast<uint8_t*>(&DesiredBytes), sizeof(T)); |
| 219 | const size_t kMaxNumPositions = 8; |
| 220 | size_t Positions[kMaxNumPositions]; |
| 221 | size_t NumPositions = 0; |
| 222 | for (const uint8_t *Cur = Data; |
| 223 | Cur < End && NumPositions < kMaxNumPositions; Cur++) { |
| 224 | Cur = (uint8_t *)memmem(Cur, End - Cur, &ExistingBytes, sizeof(T)); |
| 225 | if (!Cur) break; |
| 226 | Positions[NumPositions++] = Cur - Data; |
| 227 | } |
| 228 | if (!NumPositions) break; |
| 229 | return DictionaryEntry(W, Positions[Rand(NumPositions)]); |
| 230 | } |
| 231 | DictionaryEntry DE(W); |
| 232 | return DE; |
| 233 | } |
| 234 | |
| 235 | size_t MutationDispatcher::Mutate_AddWordFromTORC( |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 236 | uint8_t *Data, size_t Size, size_t MaxSize) { |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame^] | 237 | Word W; |
| 238 | DictionaryEntry DE; |
| 239 | bool Debug = false; |
| 240 | if (Rand.RandBool()) { |
| 241 | auto X = TPC.TORC8.Get(Rand.Rand()); |
| 242 | DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size); |
| 243 | if (X.A > 10000 &&X.B > 10000) Debug = false; |
| 244 | if (Debug) { |
| 245 | Printf("ZZZ %zx %zx\n", X.A, X.B); |
| 246 | DE.Print(); |
| 247 | } |
| 248 | } else { |
| 249 | auto X = TPC.TORC4.Get(Rand.Rand()); |
| 250 | if ((X.A >> 16) == 0 && (X.B >> 16) == 0 && Rand.RandBool()) |
| 251 | DE = MakeDictionaryEntryFromCMP((uint16_t)X.A, (uint16_t)X.B, Data, |
| 252 | Size); |
| 253 | else |
| 254 | DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size); |
| 255 | } |
| 256 | Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE); |
| 257 | if (!Size) return 0; |
| 258 | if (Debug) { |
| 259 | Printf("DONE\n"); |
| 260 | } |
| 261 | DictionaryEntry &DERef = |
| 262 | CmpDictionaryEntriesDeque[CmpDictionaryEntriesDequeIdx++ % |
| 263 | kCmpDictionaryEntriesDequeSize]; |
| 264 | DERef = DE; |
| 265 | CurrentDictionaryEntrySequence.push_back(&DERef); |
| 266 | return Size; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 269 | size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary( |
| 270 | uint8_t *Data, size_t Size, size_t MaxSize) { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 271 | return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 274 | size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data, |
| 275 | size_t Size, size_t MaxSize) { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 276 | if (Size > MaxSize) return 0; |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 277 | if (D.empty()) return 0; |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 278 | DictionaryEntry &DE = D[Rand(D.size())]; |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame^] | 279 | Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE); |
| 280 | if (!Size) return 0; |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 281 | DE.IncUseCount(); |
| 282 | CurrentDictionaryEntrySequence.push_back(&DE); |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 283 | return Size; |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame] | 286 | // Overwrites part of To[0,ToSize) with a part of From[0,FromSize). |
| 287 | // Returns ToSize. |
| 288 | size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize, |
| 289 | uint8_t *To, size_t ToSize) { |
| 290 | // Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize). |
| 291 | size_t ToBeg = Rand(ToSize); |
| 292 | size_t CopySize = Rand(ToSize - ToBeg) + 1; |
| 293 | assert(ToBeg + CopySize <= ToSize); |
| 294 | CopySize = std::min(CopySize, FromSize); |
| 295 | size_t FromBeg = Rand(FromSize - CopySize + 1); |
| 296 | assert(FromBeg + CopySize <= FromSize); |
| 297 | memmove(To + ToBeg, From + FromBeg, CopySize); |
| 298 | return ToSize; |
| 299 | } |
| 300 | |
| 301 | // Inserts part of From[0,ToSize) into To. |
| 302 | // Returns new size of To on success or 0 on failure. |
| 303 | size_t MutationDispatcher::InsertPartOf(const uint8_t *From, size_t FromSize, |
| 304 | uint8_t *To, size_t ToSize, |
| 305 | size_t MaxToSize) { |
| 306 | if (ToSize >= MaxToSize) return 0; |
| 307 | size_t AvailableSpace = MaxToSize - ToSize; |
| 308 | size_t MaxCopySize = std::min(AvailableSpace, FromSize); |
| 309 | size_t CopySize = Rand(MaxCopySize) + 1; |
| 310 | size_t FromBeg = Rand(FromSize - CopySize + 1); |
| 311 | assert(FromBeg + CopySize <= FromSize); |
| 312 | size_t ToInsertPos = Rand(ToSize + 1); |
| 313 | assert(ToInsertPos + CopySize <= MaxToSize); |
| 314 | size_t TailSize = ToSize - ToInsertPos; |
| 315 | if (To == From) { |
| 316 | MutateInPlaceHere.resize(MaxToSize); |
| 317 | memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize); |
| 318 | memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize); |
| 319 | memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize); |
| 320 | } else { |
| 321 | memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize); |
| 322 | memmove(To + ToInsertPos, From + FromBeg, CopySize); |
| 323 | } |
| 324 | return ToSize + CopySize; |
| 325 | } |
| 326 | |
| 327 | size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size, |
| 328 | size_t MaxSize) { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 329 | if (Size > MaxSize) return 0; |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame] | 330 | if (Rand.RandBool()) |
| 331 | return CopyPartOf(Data, Size, Data, Size); |
| 332 | else |
| 333 | return InsertPartOf(Data, Size, Data, Size, MaxSize); |
| 334 | } |
| 335 | |
Kostya Serebryany | 25425ad | 2015-09-08 17:19:31 +0000 | [diff] [blame] | 336 | size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, |
| 337 | size_t MaxSize) { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 338 | if (Size > MaxSize) return 0; |
Kostya Serebryany | 25425ad | 2015-09-08 17:19:31 +0000 | [diff] [blame] | 339 | size_t B = Rand(Size); |
| 340 | while (B < Size && !isdigit(Data[B])) B++; |
| 341 | if (B == Size) return 0; |
| 342 | size_t E = B; |
| 343 | while (E < Size && isdigit(Data[E])) E++; |
| 344 | assert(B < E); |
| 345 | // now we have digits in [B, E). |
| 346 | // strtol and friends don't accept non-zero-teminated data, parse it manually. |
| 347 | uint64_t Val = Data[B] - '0'; |
| 348 | for (size_t i = B + 1; i < E; i++) |
| 349 | Val = Val * 10 + Data[i] - '0'; |
| 350 | |
| 351 | // Mutate the integer value. |
| 352 | switch(Rand(5)) { |
| 353 | case 0: Val++; break; |
| 354 | case 1: Val--; break; |
| 355 | case 2: Val /= 2; break; |
| 356 | case 3: Val *= 2; break; |
| 357 | case 4: Val = Rand(Val * Val); break; |
| 358 | default: assert(0); |
| 359 | } |
| 360 | // Just replace the bytes with the new ones, don't bother moving bytes. |
| 361 | for (size_t i = B; i < E; i++) { |
| 362 | size_t Idx = E + B - i - 1; |
| 363 | assert(Idx >= B && Idx < E); |
| 364 | Data[Idx] = (Val % 10) + '0'; |
| 365 | Val /= 10; |
| 366 | } |
| 367 | return Size; |
| 368 | } |
| 369 | |
Kostya Serebryany | 0c537b1 | 2016-08-17 21:30:30 +0000 | [diff] [blame] | 370 | template<class T> |
| 371 | size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand) { |
| 372 | if (Size < sizeof(T)) return 0; |
| 373 | size_t Off = Rand(Size - sizeof(T) + 1); |
| 374 | assert(Off + sizeof(T) <= Size); |
| 375 | T Val; |
Kostya Serebryany | 65f102d | 2016-10-22 03:48:53 +0000 | [diff] [blame] | 376 | if (Off < 64 && !Rand(4)) { |
| 377 | Val = Size; |
| 378 | if (Rand.RandBool()) |
| 379 | Val = Bswap(Val); |
| 380 | } else { |
| 381 | memcpy(&Val, Data + Off, sizeof(Val)); |
| 382 | T Add = Rand(21); |
| 383 | Add -= 10; |
| 384 | if (Rand.RandBool()) |
| 385 | Val = Bswap(T(Bswap(Val) + Add)); // Add assuming different endiannes. |
| 386 | else |
| 387 | Val = Val + Add; // Add assuming current endiannes. |
| 388 | if (Add == 0 || Rand.RandBool()) // Maybe negate. |
| 389 | Val = -Val; |
| 390 | } |
Kostya Serebryany | 0c537b1 | 2016-08-17 21:30:30 +0000 | [diff] [blame] | 391 | memcpy(Data + Off, &Val, sizeof(Val)); |
| 392 | return Size; |
| 393 | } |
| 394 | |
| 395 | size_t MutationDispatcher::Mutate_ChangeBinaryInteger(uint8_t *Data, |
| 396 | size_t Size, |
| 397 | size_t MaxSize) { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 398 | if (Size > MaxSize) return 0; |
Kostya Serebryany | 0c537b1 | 2016-08-17 21:30:30 +0000 | [diff] [blame] | 399 | switch (Rand(4)) { |
| 400 | case 3: return ChangeBinaryInteger<uint64_t>(Data, Size, Rand); |
| 401 | case 2: return ChangeBinaryInteger<uint32_t>(Data, Size, Rand); |
| 402 | case 1: return ChangeBinaryInteger<uint16_t>(Data, Size, Rand); |
| 403 | case 0: return ChangeBinaryInteger<uint8_t>(Data, Size, Rand); |
| 404 | default: assert(0); |
| 405 | } |
Kostya Serebryany | a533e51 | 2016-08-19 20:57:09 +0000 | [diff] [blame] | 406 | return 0; |
Kostya Serebryany | 0c537b1 | 2016-08-17 21:30:30 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 409 | size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size, |
| 410 | size_t MaxSize) { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 411 | if (Size > MaxSize) return 0; |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 412 | if (!Corpus || Corpus->size() < 2 || Size == 0) return 0; |
| 413 | size_t Idx = Rand(Corpus->size()); |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame] | 414 | const Unit &O = (*Corpus)[Idx]; |
| 415 | if (O.empty()) return 0; |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 416 | MutateInPlaceHere.resize(MaxSize); |
| 417 | auto &U = MutateInPlaceHere; |
Kostya Serebryany | a533e51 | 2016-08-19 20:57:09 +0000 | [diff] [blame] | 418 | size_t NewSize = 0; |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame] | 419 | switch(Rand(3)) { |
| 420 | case 0: |
| 421 | NewSize = CrossOver(Data, Size, O.data(), O.size(), U.data(), U.size()); |
| 422 | break; |
| 423 | case 1: |
| 424 | NewSize = InsertPartOf(O.data(), O.size(), U.data(), U.size(), MaxSize); |
| 425 | if (NewSize) |
| 426 | break; |
Kostya Serebryany | 0c537b1 | 2016-08-17 21:30:30 +0000 | [diff] [blame] | 427 | // LLVM_FALLTHROUGH; |
Kostya Serebryany | a7398ba | 2016-08-17 18:10:42 +0000 | [diff] [blame] | 428 | case 2: |
| 429 | NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size()); |
| 430 | break; |
| 431 | default: assert(0); |
| 432 | } |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 433 | assert(NewSize > 0 && "CrossOver returned empty unit"); |
| 434 | assert(NewSize <= MaxSize && "CrossOver returned overisized unit"); |
| 435 | memcpy(Data, U.data(), NewSize); |
| 436 | return NewSize; |
| 437 | } |
| 438 | |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 439 | void MutationDispatcher::StartMutationSequence() { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 440 | CurrentMutatorSequence.clear(); |
| 441 | CurrentDictionaryEntrySequence.clear(); |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 444 | // Copy successful dictionary entries to PersistentAutoDictionary. |
| 445 | void MutationDispatcher::RecordSuccessfulMutationSequence() { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 446 | for (auto DE : CurrentDictionaryEntrySequence) { |
| 447 | // PersistentAutoDictionary.AddWithSuccessCountOne(DE); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 448 | DE->IncSuccessCount(); |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 449 | // Linear search is fine here as this happens seldom. |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 450 | if (!PersistentAutoDictionary.ContainsWord(DE->GetW())) |
| 451 | PersistentAutoDictionary.push_back({DE->GetW(), 1}); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 452 | } |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void MutationDispatcher::PrintRecommendedDictionary() { |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 456 | std::vector<DictionaryEntry> V; |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 457 | for (auto &DE : PersistentAutoDictionary) |
| 458 | if (!ManualDictionary.ContainsWord(DE.GetW())) |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 459 | V.push_back(DE); |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 460 | if (V.empty()) return; |
| 461 | Printf("###### Recommended dictionary. ######\n"); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 462 | for (auto &DE: V) { |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 463 | Printf("\""); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 464 | PrintASCII(DE.GetW(), "\""); |
| 465 | Printf(" # Uses: %zd\n", DE.GetUseCount()); |
Kostya Serebryany | 4b35874 | 2016-01-14 02:36:44 +0000 | [diff] [blame] | 466 | } |
| 467 | Printf("###### End of recommended dictionary. ######\n"); |
| 468 | } |
| 469 | |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 470 | void MutationDispatcher::PrintMutationSequence() { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 471 | Printf("MS: %zd ", CurrentMutatorSequence.size()); |
| 472 | for (auto M : CurrentMutatorSequence) |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 473 | Printf("%s-", M.Name); |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 474 | if (!CurrentDictionaryEntrySequence.empty()) { |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 475 | Printf(" DE: "); |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 476 | for (auto DE : CurrentDictionaryEntrySequence) { |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 477 | Printf("\""); |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 478 | PrintASCII(DE->GetW(), "\"-"); |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Kostya Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 483 | size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) { |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 484 | return MutateImpl(Data, Size, MaxSize, Mutators); |
| 485 | } |
| 486 | |
| 487 | size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size, |
| 488 | size_t MaxSize) { |
| 489 | return MutateImpl(Data, Size, MaxSize, DefaultMutators); |
| 490 | } |
| 491 | |
| 492 | // Mutates Data in place, returns new size. |
| 493 | size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size, |
| 494 | size_t MaxSize, |
| 495 | const std::vector<Mutator> &Mutators) { |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 496 | assert(MaxSize > 0); |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 497 | if (Size == 0) { |
| 498 | for (size_t i = 0; i < MaxSize; i++) |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 499 | Data[i] = RandCh(Rand); |
Mike Aizatsky | f0b3e85 | 2016-06-23 20:44:48 +0000 | [diff] [blame] | 500 | if (Options.OnlyASCII) |
| 501 | ToASCII(Data, MaxSize); |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 502 | return MaxSize; |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 503 | } |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 504 | assert(Size > 0); |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 505 | // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize), |
| 506 | // in which case they will return 0. |
| 507 | // Try several times before returning un-mutated data. |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 508 | for (int Iter = 0; Iter < 100; Iter++) { |
Mike Aizatsky | 70fd3e4 | 2016-06-03 21:34:29 +0000 | [diff] [blame] | 509 | auto M = Mutators[Rand(Mutators.size())]; |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 510 | size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize); |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 511 | if (NewSize && NewSize <= MaxSize) { |
Mike Aizatsky | f0b3e85 | 2016-06-23 20:44:48 +0000 | [diff] [blame] | 512 | if (Options.OnlyASCII) |
| 513 | ToASCII(Data, NewSize); |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 514 | CurrentMutatorSequence.push_back(M); |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 515 | return NewSize; |
| 516 | } |
Kostya Serebryany | b2e9897 | 2015-09-04 00:40:29 +0000 | [diff] [blame] | 517 | } |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 518 | return std::min(Size, MaxSize); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 521 | void MutationDispatcher::AddWordToManualDictionary(const Word &W) { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 522 | ManualDictionary.push_back( |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 523 | {W, std::numeric_limits<size_t>::max()}); |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 526 | void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) { |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 527 | static const size_t kMaxAutoDictSize = 1 << 14; |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 528 | if (TempAutoDictionary.size() >= kMaxAutoDictSize) return; |
Kostya Serebryany | c135b55 | 2016-07-15 23:27:19 +0000 | [diff] [blame] | 529 | TempAutoDictionary.push_back(DE); |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 532 | void MutationDispatcher::ClearAutoDictionary() { |
Kostya Serebryany | 292cf03 | 2016-02-13 03:37:24 +0000 | [diff] [blame] | 533 | TempAutoDictionary.clear(); |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 536 | } // namespace fuzzer |