FuzzerMutate.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. //===- FuzzerMutate.cpp - Mutate a test input -----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Mutate a test input.
  9. //===----------------------------------------------------------------------===//
  10. #include "FuzzerDefs.h"
  11. #include "FuzzerExtFunctions.h"
  12. #include "FuzzerIO.h"
  13. #include "FuzzerMutate.h"
  14. #include "FuzzerOptions.h"
  15. #include "FuzzerTracePC.h"
  16. namespace fuzzer {
  17. const size_t Dictionary::kMaxDictSize;
  18. static const size_t kMaxMutationsToPrint = 10;
  19. static void PrintASCII(const Word &W, const char *PrintAfter) {
  20. PrintASCII(W.data(), W.size(), PrintAfter);
  21. }
  22. MutationDispatcher::MutationDispatcher(Random &Rand,
  23. const FuzzingOptions &Options)
  24. : Rand(Rand), Options(Options) {
  25. DefaultMutators.insert(
  26. DefaultMutators.begin(),
  27. {
  28. {&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"},
  29. {&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
  30. {&MutationDispatcher::Mutate_InsertRepeatedBytes,
  31. "InsertRepeatedBytes"},
  32. {&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
  33. {&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
  34. {&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
  35. {&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
  36. {&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"},
  37. {&MutationDispatcher::Mutate_CopyPart, "CopyPart"},
  38. {&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
  39. {&MutationDispatcher::Mutate_AddWordFromManualDictionary,
  40. "ManualDict"},
  41. {&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
  42. "PersAutoDict"},
  43. });
  44. if(Options.UseCmp)
  45. DefaultMutators.push_back(
  46. {&MutationDispatcher::Mutate_AddWordFromTORC, "CMP"});
  47. if (EF->LLVMFuzzerCustomMutator)
  48. Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
  49. else
  50. Mutators = DefaultMutators;
  51. if (EF->LLVMFuzzerCustomCrossOver)
  52. Mutators.push_back(
  53. {&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
  54. }
  55. static char RandCh(Random &Rand) {
  56. if (Rand.RandBool())
  57. return static_cast<char>(Rand(256));
  58. const char Special[] = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
  59. return Special[Rand(sizeof(Special) - 1)];
  60. }
  61. size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
  62. size_t MaxSize) {
  63. if (EF->__msan_unpoison)
  64. EF->__msan_unpoison(Data, Size);
  65. if (EF->__msan_unpoison_param)
  66. EF->__msan_unpoison_param(4);
  67. return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize,
  68. Rand.Rand<unsigned int>());
  69. }
  70. size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size,
  71. size_t MaxSize) {
  72. if (Size == 0)
  73. return 0;
  74. if (!CrossOverWith) return 0;
  75. const Unit &Other = *CrossOverWith;
  76. if (Other.empty())
  77. return 0;
  78. CustomCrossOverInPlaceHere.resize(MaxSize);
  79. auto &U = CustomCrossOverInPlaceHere;
  80. if (EF->__msan_unpoison) {
  81. EF->__msan_unpoison(Data, Size);
  82. EF->__msan_unpoison(Other.data(), Other.size());
  83. EF->__msan_unpoison(U.data(), U.size());
  84. }
  85. if (EF->__msan_unpoison_param)
  86. EF->__msan_unpoison_param(7);
  87. size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
  88. Data, Size, Other.data(), Other.size(), U.data(), U.size(),
  89. Rand.Rand<unsigned int>());
  90. if (!NewSize)
  91. return 0;
  92. assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit");
  93. memcpy(Data, U.data(), NewSize);
  94. return NewSize;
  95. }
  96. size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
  97. size_t MaxSize) {
  98. if (Size > MaxSize || Size == 0) return 0;
  99. size_t ShuffleAmount =
  100. Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
  101. size_t ShuffleStart = Rand(Size - ShuffleAmount);
  102. assert(ShuffleStart + ShuffleAmount <= Size);
  103. std::shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount, Rand);
  104. return Size;
  105. }
  106. size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,
  107. size_t MaxSize) {
  108. if (Size <= 1) return 0;
  109. size_t N = Rand(Size / 2) + 1;
  110. assert(N < Size);
  111. size_t Idx = Rand(Size - N + 1);
  112. // Erase Data[Idx:Idx+N].
  113. memmove(Data + Idx, Data + Idx + N, Size - Idx - N);
  114. // Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx);
  115. return Size - N;
  116. }
  117. size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
  118. size_t MaxSize) {
  119. if (Size >= MaxSize) return 0;
  120. size_t Idx = Rand(Size + 1);
  121. // Insert new value at Data[Idx].
  122. memmove(Data + Idx + 1, Data + Idx, Size - Idx);
  123. Data[Idx] = RandCh(Rand);
  124. return Size + 1;
  125. }
  126. size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data,
  127. size_t Size,
  128. size_t MaxSize) {
  129. const size_t kMinBytesToInsert = 3;
  130. if (Size + kMinBytesToInsert >= MaxSize) return 0;
  131. size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128);
  132. size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert;
  133. assert(Size + N <= MaxSize && N);
  134. size_t Idx = Rand(Size + 1);
  135. // Insert new values at Data[Idx].
  136. memmove(Data + Idx + N, Data + Idx, Size - Idx);
  137. // Give preference to 0x00 and 0xff.
  138. uint8_t Byte = static_cast<uint8_t>(
  139. Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255));
  140. for (size_t i = 0; i < N; i++)
  141. Data[Idx + i] = Byte;
  142. return Size + N;
  143. }
  144. size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
  145. size_t MaxSize) {
  146. if (Size > MaxSize) return 0;
  147. size_t Idx = Rand(Size);
  148. Data[Idx] = RandCh(Rand);
  149. return Size;
  150. }
  151. size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
  152. size_t MaxSize) {
  153. if (Size > MaxSize) return 0;
  154. size_t Idx = Rand(Size);
  155. Data[Idx] ^= 1 << Rand(8);
  156. return Size;
  157. }
  158. size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
  159. size_t Size,
  160. size_t MaxSize) {
  161. return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
  162. }
  163. size_t MutationDispatcher::ApplyDictionaryEntry(uint8_t *Data, size_t Size,
  164. size_t MaxSize,
  165. DictionaryEntry &DE) {
  166. const Word &W = DE.GetW();
  167. bool UsePositionHint = DE.HasPositionHint() &&
  168. DE.GetPositionHint() + W.size() < Size &&
  169. Rand.RandBool();
  170. if (Rand.RandBool()) { // Insert W.
  171. if (Size + W.size() > MaxSize) return 0;
  172. size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
  173. memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);
  174. memcpy(Data + Idx, W.data(), W.size());
  175. Size += W.size();
  176. } else { // Overwrite some bytes with W.
  177. if (W.size() > Size) return 0;
  178. size_t Idx =
  179. UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1 - W.size());
  180. memcpy(Data + Idx, W.data(), W.size());
  181. }
  182. return Size;
  183. }
  184. // Somewhere in the past we have observed a comparison instructions
  185. // with arguments Arg1 Arg2. This function tries to guess a dictionary
  186. // entry that will satisfy that comparison.
  187. // It first tries to find one of the arguments (possibly swapped) in the
  188. // input and if it succeeds it creates a DE with a position hint.
  189. // Otherwise it creates a DE with one of the arguments w/o a position hint.
  190. DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
  191. const void *Arg1, const void *Arg2,
  192. const void *Arg1Mutation, const void *Arg2Mutation,
  193. size_t ArgSize, const uint8_t *Data,
  194. size_t Size) {
  195. bool HandleFirst = Rand.RandBool();
  196. const void *ExistingBytes, *DesiredBytes;
  197. Word W;
  198. const uint8_t *End = Data + Size;
  199. for (int Arg = 0; Arg < 2; Arg++) {
  200. ExistingBytes = HandleFirst ? Arg1 : Arg2;
  201. DesiredBytes = HandleFirst ? Arg2Mutation : Arg1Mutation;
  202. HandleFirst = !HandleFirst;
  203. W.Set(reinterpret_cast<const uint8_t*>(DesiredBytes), ArgSize);
  204. const size_t kMaxNumPositions = 8;
  205. size_t Positions[kMaxNumPositions];
  206. size_t NumPositions = 0;
  207. for (const uint8_t *Cur = Data;
  208. Cur < End && NumPositions < kMaxNumPositions; Cur++) {
  209. Cur =
  210. (const uint8_t *)SearchMemory(Cur, End - Cur, ExistingBytes, ArgSize);
  211. if (!Cur) break;
  212. Positions[NumPositions++] = Cur - Data;
  213. }
  214. if (!NumPositions) continue;
  215. return DictionaryEntry(W, Positions[Rand(NumPositions)]);
  216. }
  217. DictionaryEntry DE(W);
  218. return DE;
  219. }
  220. template <class T>
  221. DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
  222. T Arg1, T Arg2, const uint8_t *Data, size_t Size) {
  223. if (Rand.RandBool()) Arg1 = Bswap(Arg1);
  224. if (Rand.RandBool()) Arg2 = Bswap(Arg2);
  225. T Arg1Mutation = static_cast<T>(Arg1 + Rand(-1, 1));
  226. T Arg2Mutation = static_cast<T>(Arg2 + Rand(-1, 1));
  227. return MakeDictionaryEntryFromCMP(&Arg1, &Arg2, &Arg1Mutation, &Arg2Mutation,
  228. sizeof(Arg1), Data, Size);
  229. }
  230. DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
  231. const Word &Arg1, const Word &Arg2, const uint8_t *Data, size_t Size) {
  232. return MakeDictionaryEntryFromCMP(Arg1.data(), Arg2.data(), Arg1.data(),
  233. Arg2.data(), Arg1.size(), Data, Size);
  234. }
  235. size_t MutationDispatcher::Mutate_AddWordFromTORC(
  236. uint8_t *Data, size_t Size, size_t MaxSize) {
  237. Word W;
  238. DictionaryEntry DE;
  239. switch (Rand(4)) {
  240. case 0: {
  241. auto X = TPC.TORC8.Get(Rand.Rand<size_t>());
  242. DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);
  243. } break;
  244. case 1: {
  245. auto X = TPC.TORC4.Get(Rand.Rand<size_t>());
  246. if ((X.A >> 16) == 0 && (X.B >> 16) == 0 && Rand.RandBool())
  247. DE = MakeDictionaryEntryFromCMP((uint16_t)X.A, (uint16_t)X.B, Data, Size);
  248. else
  249. DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);
  250. } break;
  251. case 2: {
  252. auto X = TPC.TORCW.Get(Rand.Rand<size_t>());
  253. DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);
  254. } break;
  255. case 3: if (Options.UseMemmem) {
  256. auto X = TPC.MMT.Get(Rand.Rand<size_t>());
  257. DE = DictionaryEntry(X);
  258. } break;
  259. default:
  260. assert(0);
  261. }
  262. if (!DE.GetW().size()) return 0;
  263. Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);
  264. if (!Size) return 0;
  265. DictionaryEntry &DERef =
  266. CmpDictionaryEntriesDeque[CmpDictionaryEntriesDequeIdx++ %
  267. kCmpDictionaryEntriesDequeSize];
  268. DERef = DE;
  269. CurrentDictionaryEntrySequence.push_back(&DERef);
  270. return Size;
  271. }
  272. size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
  273. uint8_t *Data, size_t Size, size_t MaxSize) {
  274. return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
  275. }
  276. size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
  277. size_t Size, size_t MaxSize) {
  278. if (Size > MaxSize) return 0;
  279. if (D.empty()) return 0;
  280. DictionaryEntry &DE = D[Rand(D.size())];
  281. Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);
  282. if (!Size) return 0;
  283. DE.IncUseCount();
  284. CurrentDictionaryEntrySequence.push_back(&DE);
  285. return Size;
  286. }
  287. // Overwrites part of To[0,ToSize) with a part of From[0,FromSize).
  288. // Returns ToSize.
  289. size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize,
  290. uint8_t *To, size_t ToSize) {
  291. // Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize).
  292. size_t ToBeg = Rand(ToSize);
  293. size_t CopySize = Rand(ToSize - ToBeg) + 1;
  294. assert(ToBeg + CopySize <= ToSize);
  295. CopySize = std::min(CopySize, FromSize);
  296. size_t FromBeg = Rand(FromSize - CopySize + 1);
  297. assert(FromBeg + CopySize <= FromSize);
  298. memmove(To + ToBeg, From + FromBeg, CopySize);
  299. return ToSize;
  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. size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size,
  327. size_t MaxSize) {
  328. if (Size > MaxSize || Size == 0) return 0;
  329. // If Size == MaxSize, `InsertPartOf(...)` will
  330. // fail so there's no point using it in this case.
  331. if (Size == MaxSize || Rand.RandBool())
  332. return CopyPartOf(Data, Size, Data, Size);
  333. else
  334. return InsertPartOf(Data, Size, Data, Size, MaxSize);
  335. }
  336. size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
  337. size_t MaxSize) {
  338. if (Size > MaxSize) return 0;
  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. // Mutate the integer value.
  351. switch(Rand(5)) {
  352. case 0: Val++; break;
  353. case 1: Val--; break;
  354. case 2: Val /= 2; break;
  355. case 3: Val *= 2; break;
  356. case 4: Val = Rand(Val * Val); break;
  357. default: assert(0);
  358. }
  359. // Just replace the bytes with the new ones, don't bother moving bytes.
  360. for (size_t i = B; i < E; i++) {
  361. size_t Idx = E + B - i - 1;
  362. assert(Idx >= B && Idx < E);
  363. Data[Idx] = (Val % 10) + '0';
  364. Val /= 10;
  365. }
  366. return Size;
  367. }
  368. template<class T>
  369. size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand) {
  370. if (Size < sizeof(T)) return 0;
  371. size_t Off = Rand(Size - sizeof(T) + 1);
  372. assert(Off + sizeof(T) <= Size);
  373. T Val;
  374. if (Off < 64 && !Rand(4)) {
  375. Val = static_cast<T>(Size);
  376. if (Rand.RandBool())
  377. Val = Bswap(Val);
  378. } else {
  379. memcpy(&Val, Data + Off, sizeof(Val));
  380. T Add = static_cast<T>(Rand(21));
  381. Add -= 10;
  382. if (Rand.RandBool())
  383. Val = Bswap(T(Bswap(Val) + Add)); // Add assuming different endiannes.
  384. else
  385. Val = Val + Add; // Add assuming current endiannes.
  386. if (Add == 0 || Rand.RandBool()) // Maybe negate.
  387. Val = -Val;
  388. }
  389. memcpy(Data + Off, &Val, sizeof(Val));
  390. return Size;
  391. }
  392. size_t MutationDispatcher::Mutate_ChangeBinaryInteger(uint8_t *Data,
  393. size_t Size,
  394. size_t MaxSize) {
  395. if (Size > MaxSize) return 0;
  396. switch (Rand(4)) {
  397. case 3: return ChangeBinaryInteger<uint64_t>(Data, Size, Rand);
  398. case 2: return ChangeBinaryInteger<uint32_t>(Data, Size, Rand);
  399. case 1: return ChangeBinaryInteger<uint16_t>(Data, Size, Rand);
  400. case 0: return ChangeBinaryInteger<uint8_t>(Data, Size, Rand);
  401. default: assert(0);
  402. }
  403. return 0;
  404. }
  405. size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
  406. size_t MaxSize) {
  407. if (Size > MaxSize) return 0;
  408. if (Size == 0) return 0;
  409. if (!CrossOverWith) return 0;
  410. const Unit &O = *CrossOverWith;
  411. if (O.empty()) return 0;
  412. size_t NewSize = 0;
  413. switch(Rand(3)) {
  414. case 0:
  415. MutateInPlaceHere.resize(MaxSize);
  416. NewSize = CrossOver(Data, Size, O.data(), O.size(),
  417. MutateInPlaceHere.data(), MaxSize);
  418. memcpy(Data, MutateInPlaceHere.data(), NewSize);
  419. break;
  420. case 1:
  421. NewSize = InsertPartOf(O.data(), O.size(), Data, Size, MaxSize);
  422. if (!NewSize)
  423. NewSize = CopyPartOf(O.data(), O.size(), Data, Size);
  424. break;
  425. case 2:
  426. NewSize = CopyPartOf(O.data(), O.size(), Data, Size);
  427. break;
  428. default: assert(0);
  429. }
  430. assert(NewSize > 0 && "CrossOver returned empty unit");
  431. assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
  432. return NewSize;
  433. }
  434. void MutationDispatcher::StartMutationSequence() {
  435. CurrentMutatorSequence.clear();
  436. CurrentDictionaryEntrySequence.clear();
  437. }
  438. // Copy successful dictionary entries to PersistentAutoDictionary.
  439. void MutationDispatcher::RecordSuccessfulMutationSequence() {
  440. for (auto DE : CurrentDictionaryEntrySequence) {
  441. // PersistentAutoDictionary.AddWithSuccessCountOne(DE);
  442. DE->IncSuccessCount();
  443. assert(DE->GetW().size());
  444. // Linear search is fine here as this happens seldom.
  445. if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
  446. PersistentAutoDictionary.push_back(*DE);
  447. }
  448. }
  449. void MutationDispatcher::PrintRecommendedDictionary() {
  450. std::vector<DictionaryEntry> V;
  451. for (auto &DE : PersistentAutoDictionary)
  452. if (!ManualDictionary.ContainsWord(DE.GetW()))
  453. V.push_back(DE);
  454. if (V.empty()) return;
  455. Printf("###### Recommended dictionary. ######\n");
  456. for (auto &DE: V) {
  457. assert(DE.GetW().size());
  458. Printf("\"");
  459. PrintASCII(DE.GetW(), "\"");
  460. Printf(" # Uses: %zd\n", DE.GetUseCount());
  461. }
  462. Printf("###### End of recommended dictionary. ######\n");
  463. }
  464. void MutationDispatcher::PrintMutationSequence(bool Verbose) {
  465. Printf("MS: %zd ", CurrentMutatorSequence.size());
  466. size_t EntriesToPrint =
  467. Verbose ? CurrentMutatorSequence.size()
  468. : std::min(kMaxMutationsToPrint, CurrentMutatorSequence.size());
  469. for (size_t i = 0; i < EntriesToPrint; i++)
  470. Printf("%s-", CurrentMutatorSequence[i].Name);
  471. if (!CurrentDictionaryEntrySequence.empty()) {
  472. Printf(" DE: ");
  473. EntriesToPrint = Verbose ? CurrentDictionaryEntrySequence.size()
  474. : std::min(kMaxMutationsToPrint,
  475. CurrentDictionaryEntrySequence.size());
  476. for (size_t i = 0; i < EntriesToPrint; i++) {
  477. Printf("\"");
  478. PrintASCII(CurrentDictionaryEntrySequence[i]->GetW(), "\"-");
  479. }
  480. }
  481. }
  482. std::string MutationDispatcher::MutationSequence() {
  483. std::string MS;
  484. for (auto M : CurrentMutatorSequence) {
  485. MS += M.Name;
  486. MS += "-";
  487. }
  488. return MS;
  489. }
  490. size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
  491. return MutateImpl(Data, Size, MaxSize, Mutators);
  492. }
  493. size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size,
  494. size_t MaxSize) {
  495. return MutateImpl(Data, Size, MaxSize, DefaultMutators);
  496. }
  497. // Mutates Data in place, returns new size.
  498. size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
  499. size_t MaxSize,
  500. std::vector<Mutator> &Mutators) {
  501. assert(MaxSize > 0);
  502. // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
  503. // in which case they will return 0.
  504. // Try several times before returning un-mutated data.
  505. for (int Iter = 0; Iter < 100; Iter++) {
  506. auto M = Mutators[Rand(Mutators.size())];
  507. size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
  508. if (NewSize && NewSize <= MaxSize) {
  509. if (Options.OnlyASCII)
  510. ToASCII(Data, NewSize);
  511. CurrentMutatorSequence.push_back(M);
  512. return NewSize;
  513. }
  514. }
  515. *Data = ' ';
  516. return 1; // Fallback, should not happen frequently.
  517. }
  518. // Mask represents the set of Data bytes that are worth mutating.
  519. size_t MutationDispatcher::MutateWithMask(uint8_t *Data, size_t Size,
  520. size_t MaxSize,
  521. const std::vector<uint8_t> &Mask) {
  522. size_t MaskedSize = std::min(Size, Mask.size());
  523. // * Copy the worthy bytes into a temporary array T
  524. // * Mutate T
  525. // * Copy T back.
  526. // This is totally unoptimized.
  527. auto &T = MutateWithMaskTemp;
  528. if (T.size() < Size)
  529. T.resize(Size);
  530. size_t OneBits = 0;
  531. for (size_t I = 0; I < MaskedSize; I++)
  532. if (Mask[I])
  533. T[OneBits++] = Data[I];
  534. if (!OneBits) return 0;
  535. assert(!T.empty());
  536. size_t NewSize = Mutate(T.data(), OneBits, OneBits);
  537. assert(NewSize <= OneBits);
  538. (void)NewSize;
  539. // Even if NewSize < OneBits we still use all OneBits bytes.
  540. for (size_t I = 0, J = 0; I < MaskedSize; I++)
  541. if (Mask[I])
  542. Data[I] = T[J++];
  543. return Size;
  544. }
  545. void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
  546. ManualDictionary.push_back(
  547. {W, std::numeric_limits<size_t>::max()});
  548. }
  549. } // namespace fuzzer