Path: blob/main/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerMutate.cpp
35262 views
//===- FuzzerMutate.cpp - Mutate a test input -----------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// Mutate a test input.8//===----------------------------------------------------------------------===//910#include "FuzzerDefs.h"11#include "FuzzerExtFunctions.h"12#include "FuzzerIO.h"13#include "FuzzerMutate.h"14#include "FuzzerOptions.h"15#include "FuzzerTracePC.h"1617namespace fuzzer {1819const size_t Dictionary::kMaxDictSize;20static const size_t kMaxMutationsToPrint = 10;2122static void PrintASCII(const Word &W, const char *PrintAfter) {23PrintASCII(W.data(), W.size(), PrintAfter);24}2526MutationDispatcher::MutationDispatcher(Random &Rand,27const FuzzingOptions &Options)28: Rand(Rand), Options(Options) {29DefaultMutators.insert(30DefaultMutators.begin(),31{32{&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"},33{&MutationDispatcher::Mutate_InsertByte, "InsertByte"},34{&MutationDispatcher::Mutate_InsertRepeatedBytes,35"InsertRepeatedBytes"},36{&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},37{&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},38{&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},39{&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},40{&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"},41{&MutationDispatcher::Mutate_CopyPart, "CopyPart"},42{&MutationDispatcher::Mutate_CrossOver, "CrossOver"},43{&MutationDispatcher::Mutate_AddWordFromManualDictionary,44"ManualDict"},45{&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,46"PersAutoDict"},47});48if(Options.UseCmp)49DefaultMutators.push_back(50{&MutationDispatcher::Mutate_AddWordFromTORC, "CMP"});5152if (EF->LLVMFuzzerCustomMutator)53Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});54else55Mutators = DefaultMutators;5657if (EF->LLVMFuzzerCustomCrossOver)58Mutators.push_back(59{&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});60}6162static char RandCh(Random &Rand) {63if (Rand.RandBool())64return static_cast<char>(Rand(256));65const char Special[] = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";66return Special[Rand(sizeof(Special) - 1)];67}6869size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,70size_t MaxSize) {71if (EF->__msan_unpoison)72EF->__msan_unpoison(Data, Size);73if (EF->__msan_unpoison_param)74EF->__msan_unpoison_param(4);75return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize,76Rand.Rand<unsigned int>());77}7879size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size,80size_t MaxSize) {81if (Size == 0)82return 0;83if (!CrossOverWith) return 0;84const Unit &Other = *CrossOverWith;85if (Other.empty())86return 0;87CustomCrossOverInPlaceHere.resize(MaxSize);88auto &U = CustomCrossOverInPlaceHere;8990if (EF->__msan_unpoison) {91EF->__msan_unpoison(Data, Size);92EF->__msan_unpoison(Other.data(), Other.size());93EF->__msan_unpoison(U.data(), U.size());94}95if (EF->__msan_unpoison_param)96EF->__msan_unpoison_param(7);97size_t NewSize = EF->LLVMFuzzerCustomCrossOver(98Data, Size, Other.data(), Other.size(), U.data(), U.size(),99Rand.Rand<unsigned int>());100101if (!NewSize)102return 0;103assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit");104memcpy(Data, U.data(), NewSize);105return NewSize;106}107108size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,109size_t MaxSize) {110if (Size > MaxSize || Size == 0) return 0;111size_t ShuffleAmount =112Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.113size_t ShuffleStart = Rand(Size - ShuffleAmount);114assert(ShuffleStart + ShuffleAmount <= Size);115std::shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount, Rand);116return Size;117}118119size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,120size_t MaxSize) {121if (Size <= 1) return 0;122size_t N = Rand(Size / 2) + 1;123assert(N < Size);124size_t Idx = Rand(Size - N + 1);125// Erase Data[Idx:Idx+N].126memmove(Data + Idx, Data + Idx + N, Size - Idx - N);127// Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx);128return Size - N;129}130131size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,132size_t MaxSize) {133if (Size >= MaxSize) return 0;134size_t Idx = Rand(Size + 1);135// Insert new value at Data[Idx].136memmove(Data + Idx + 1, Data + Idx, Size - Idx);137Data[Idx] = RandCh(Rand);138return Size + 1;139}140141size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data,142size_t Size,143size_t MaxSize) {144const size_t kMinBytesToInsert = 3;145if (Size + kMinBytesToInsert >= MaxSize) return 0;146size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128);147size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert;148assert(Size + N <= MaxSize && N);149size_t Idx = Rand(Size + 1);150// Insert new values at Data[Idx].151memmove(Data + Idx + N, Data + Idx, Size - Idx);152// Give preference to 0x00 and 0xff.153uint8_t Byte = static_cast<uint8_t>(154Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255));155for (size_t i = 0; i < N; i++)156Data[Idx + i] = Byte;157return Size + N;158}159160size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,161size_t MaxSize) {162if (Size > MaxSize) return 0;163size_t Idx = Rand(Size);164Data[Idx] = RandCh(Rand);165return Size;166}167168size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,169size_t MaxSize) {170if (Size > MaxSize) return 0;171size_t Idx = Rand(Size);172Data[Idx] ^= 1 << Rand(8);173return Size;174}175176size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,177size_t Size,178size_t MaxSize) {179return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);180}181182size_t MutationDispatcher::ApplyDictionaryEntry(uint8_t *Data, size_t Size,183size_t MaxSize,184DictionaryEntry &DE) {185const Word &W = DE.GetW();186bool UsePositionHint = DE.HasPositionHint() &&187DE.GetPositionHint() + W.size() < Size &&188Rand.RandBool();189if (Rand.RandBool()) { // Insert W.190if (Size + W.size() > MaxSize) return 0;191size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);192memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);193memcpy(Data + Idx, W.data(), W.size());194Size += W.size();195} else { // Overwrite some bytes with W.196if (W.size() > Size) return 0;197size_t Idx =198UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1 - W.size());199memcpy(Data + Idx, W.data(), W.size());200}201return Size;202}203204// Somewhere in the past we have observed a comparison instructions205// with arguments Arg1 Arg2. This function tries to guess a dictionary206// entry that will satisfy that comparison.207// It first tries to find one of the arguments (possibly swapped) in the208// input and if it succeeds it creates a DE with a position hint.209// Otherwise it creates a DE with one of the arguments w/o a position hint.210DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(211const void *Arg1, const void *Arg2,212const void *Arg1Mutation, const void *Arg2Mutation,213size_t ArgSize, const uint8_t *Data,214size_t Size) {215bool HandleFirst = Rand.RandBool();216const void *ExistingBytes, *DesiredBytes;217Word W;218const uint8_t *End = Data + Size;219for (int Arg = 0; Arg < 2; Arg++) {220ExistingBytes = HandleFirst ? Arg1 : Arg2;221DesiredBytes = HandleFirst ? Arg2Mutation : Arg1Mutation;222HandleFirst = !HandleFirst;223W.Set(reinterpret_cast<const uint8_t*>(DesiredBytes), ArgSize);224const size_t kMaxNumPositions = 8;225size_t Positions[kMaxNumPositions];226size_t NumPositions = 0;227for (const uint8_t *Cur = Data;228Cur < End && NumPositions < kMaxNumPositions; Cur++) {229Cur =230(const uint8_t *)SearchMemory(Cur, End - Cur, ExistingBytes, ArgSize);231if (!Cur) break;232Positions[NumPositions++] = Cur - Data;233}234if (!NumPositions) continue;235return DictionaryEntry(W, Positions[Rand(NumPositions)]);236}237DictionaryEntry DE(W);238return DE;239}240241242template <class T>243DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(244T Arg1, T Arg2, const uint8_t *Data, size_t Size) {245if (Rand.RandBool()) Arg1 = Bswap(Arg1);246if (Rand.RandBool()) Arg2 = Bswap(Arg2);247T Arg1Mutation = static_cast<T>(Arg1 + Rand(-1, 1));248T Arg2Mutation = static_cast<T>(Arg2 + Rand(-1, 1));249return MakeDictionaryEntryFromCMP(&Arg1, &Arg2, &Arg1Mutation, &Arg2Mutation,250sizeof(Arg1), Data, Size);251}252253DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(254const Word &Arg1, const Word &Arg2, const uint8_t *Data, size_t Size) {255return MakeDictionaryEntryFromCMP(Arg1.data(), Arg2.data(), Arg1.data(),256Arg2.data(), Arg1.size(), Data, Size);257}258259size_t MutationDispatcher::Mutate_AddWordFromTORC(260uint8_t *Data, size_t Size, size_t MaxSize) {261Word W;262DictionaryEntry DE;263switch (Rand(4)) {264case 0: {265auto X = TPC.TORC8.Get(Rand.Rand<size_t>());266DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);267} break;268case 1: {269auto X = TPC.TORC4.Get(Rand.Rand<size_t>());270if ((X.A >> 16) == 0 && (X.B >> 16) == 0 && Rand.RandBool())271DE = MakeDictionaryEntryFromCMP((uint16_t)X.A, (uint16_t)X.B, Data, Size);272else273DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);274} break;275case 2: {276auto X = TPC.TORCW.Get(Rand.Rand<size_t>());277DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);278} break;279case 3: if (Options.UseMemmem) {280auto X = TPC.MMT.Get(Rand.Rand<size_t>());281DE = DictionaryEntry(X);282} break;283default:284assert(0);285}286if (!DE.GetW().size()) return 0;287Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);288if (!Size) return 0;289DictionaryEntry &DERef =290CmpDictionaryEntriesDeque[CmpDictionaryEntriesDequeIdx++ %291kCmpDictionaryEntriesDequeSize];292DERef = DE;293CurrentDictionaryEntrySequence.push_back(&DERef);294return Size;295}296297size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(298uint8_t *Data, size_t Size, size_t MaxSize) {299return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);300}301302size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,303size_t Size, size_t MaxSize) {304if (Size > MaxSize) return 0;305if (D.empty()) return 0;306DictionaryEntry &DE = D[Rand(D.size())];307Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);308if (!Size) return 0;309DE.IncUseCount();310CurrentDictionaryEntrySequence.push_back(&DE);311return Size;312}313314// Overwrites part of To[0,ToSize) with a part of From[0,FromSize).315// Returns ToSize.316size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize,317uint8_t *To, size_t ToSize) {318// Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize).319size_t ToBeg = Rand(ToSize);320size_t CopySize = Rand(ToSize - ToBeg) + 1;321assert(ToBeg + CopySize <= ToSize);322CopySize = std::min(CopySize, FromSize);323size_t FromBeg = Rand(FromSize - CopySize + 1);324assert(FromBeg + CopySize <= FromSize);325memmove(To + ToBeg, From + FromBeg, CopySize);326return ToSize;327}328329// Inserts part of From[0,ToSize) into To.330// Returns new size of To on success or 0 on failure.331size_t MutationDispatcher::InsertPartOf(const uint8_t *From, size_t FromSize,332uint8_t *To, size_t ToSize,333size_t MaxToSize) {334if (ToSize >= MaxToSize) return 0;335size_t AvailableSpace = MaxToSize - ToSize;336size_t MaxCopySize = std::min(AvailableSpace, FromSize);337size_t CopySize = Rand(MaxCopySize) + 1;338size_t FromBeg = Rand(FromSize - CopySize + 1);339assert(FromBeg + CopySize <= FromSize);340size_t ToInsertPos = Rand(ToSize + 1);341assert(ToInsertPos + CopySize <= MaxToSize);342size_t TailSize = ToSize - ToInsertPos;343if (To == From) {344MutateInPlaceHere.resize(MaxToSize);345memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize);346memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);347memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize);348} else {349memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);350memmove(To + ToInsertPos, From + FromBeg, CopySize);351}352return ToSize + CopySize;353}354355size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size,356size_t MaxSize) {357if (Size > MaxSize || Size == 0) return 0;358// If Size == MaxSize, `InsertPartOf(...)` will359// fail so there's no point using it in this case.360if (Size == MaxSize || Rand.RandBool())361return CopyPartOf(Data, Size, Data, Size);362else363return InsertPartOf(Data, Size, Data, Size, MaxSize);364}365366size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,367size_t MaxSize) {368if (Size > MaxSize) return 0;369size_t B = Rand(Size);370while (B < Size && !isdigit(Data[B])) B++;371if (B == Size) return 0;372size_t E = B;373while (E < Size && isdigit(Data[E])) E++;374assert(B < E);375// now we have digits in [B, E).376// strtol and friends don't accept non-zero-teminated data, parse it manually.377uint64_t Val = Data[B] - '0';378for (size_t i = B + 1; i < E; i++)379Val = Val * 10 + Data[i] - '0';380381// Mutate the integer value.382switch(Rand(5)) {383case 0: Val++; break;384case 1: Val--; break;385case 2: Val /= 2; break;386case 3: Val *= 2; break;387case 4: Val = Rand(Val * Val); break;388default: assert(0);389}390// Just replace the bytes with the new ones, don't bother moving bytes.391for (size_t i = B; i < E; i++) {392size_t Idx = E + B - i - 1;393assert(Idx >= B && Idx < E);394Data[Idx] = (Val % 10) + '0';395Val /= 10;396}397return Size;398}399400template<class T>401size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand) {402if (Size < sizeof(T)) return 0;403size_t Off = Rand(Size - sizeof(T) + 1);404assert(Off + sizeof(T) <= Size);405T Val;406if (Off < 64 && !Rand(4)) {407Val = static_cast<T>(Size);408if (Rand.RandBool())409Val = Bswap(Val);410} else {411memcpy(&Val, Data + Off, sizeof(Val));412T Add = static_cast<T>(Rand(21));413Add -= 10;414if (Rand.RandBool())415Val = Bswap(T(Bswap(Val) + Add)); // Add assuming different endiannes.416else417Val = Val + Add; // Add assuming current endiannes.418if (Add == 0 || Rand.RandBool()) // Maybe negate.419Val = -Val;420}421memcpy(Data + Off, &Val, sizeof(Val));422return Size;423}424425size_t MutationDispatcher::Mutate_ChangeBinaryInteger(uint8_t *Data,426size_t Size,427size_t MaxSize) {428if (Size > MaxSize) return 0;429switch (Rand(4)) {430case 3: return ChangeBinaryInteger<uint64_t>(Data, Size, Rand);431case 2: return ChangeBinaryInteger<uint32_t>(Data, Size, Rand);432case 1: return ChangeBinaryInteger<uint16_t>(Data, Size, Rand);433case 0: return ChangeBinaryInteger<uint8_t>(Data, Size, Rand);434default: assert(0);435}436return 0;437}438439size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,440size_t MaxSize) {441if (Size > MaxSize) return 0;442if (Size == 0) return 0;443if (!CrossOverWith) return 0;444const Unit &O = *CrossOverWith;445if (O.empty()) return 0;446size_t NewSize = 0;447switch(Rand(3)) {448case 0:449MutateInPlaceHere.resize(MaxSize);450NewSize = CrossOver(Data, Size, O.data(), O.size(),451MutateInPlaceHere.data(), MaxSize);452memcpy(Data, MutateInPlaceHere.data(), NewSize);453break;454case 1:455NewSize = InsertPartOf(O.data(), O.size(), Data, Size, MaxSize);456if (!NewSize)457NewSize = CopyPartOf(O.data(), O.size(), Data, Size);458break;459case 2:460NewSize = CopyPartOf(O.data(), O.size(), Data, Size);461break;462default: assert(0);463}464assert(NewSize > 0 && "CrossOver returned empty unit");465assert(NewSize <= MaxSize && "CrossOver returned overisized unit");466return NewSize;467}468469void MutationDispatcher::StartMutationSequence() {470CurrentMutatorSequence.clear();471CurrentDictionaryEntrySequence.clear();472}473474// Copy successful dictionary entries to PersistentAutoDictionary.475void MutationDispatcher::RecordSuccessfulMutationSequence() {476for (auto DE : CurrentDictionaryEntrySequence) {477// PersistentAutoDictionary.AddWithSuccessCountOne(DE);478DE->IncSuccessCount();479assert(DE->GetW().size());480// Linear search is fine here as this happens seldom.481if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))482PersistentAutoDictionary.push_back(*DE);483}484}485486void MutationDispatcher::PrintRecommendedDictionary() {487std::vector<DictionaryEntry> V;488for (auto &DE : PersistentAutoDictionary)489if (!ManualDictionary.ContainsWord(DE.GetW()))490V.push_back(DE);491if (V.empty()) return;492Printf("###### Recommended dictionary. ######\n");493for (auto &DE: V) {494assert(DE.GetW().size());495Printf("\"");496PrintASCII(DE.GetW(), "\"");497Printf(" # Uses: %zd\n", DE.GetUseCount());498}499Printf("###### End of recommended dictionary. ######\n");500}501502void MutationDispatcher::PrintMutationSequence(bool Verbose) {503Printf("MS: %zd ", CurrentMutatorSequence.size());504size_t EntriesToPrint =505Verbose ? CurrentMutatorSequence.size()506: std::min(kMaxMutationsToPrint, CurrentMutatorSequence.size());507for (size_t i = 0; i < EntriesToPrint; i++)508Printf("%s-", CurrentMutatorSequence[i].Name);509if (!CurrentDictionaryEntrySequence.empty()) {510Printf(" DE: ");511EntriesToPrint = Verbose ? CurrentDictionaryEntrySequence.size()512: std::min(kMaxMutationsToPrint,513CurrentDictionaryEntrySequence.size());514for (size_t i = 0; i < EntriesToPrint; i++) {515Printf("\"");516PrintASCII(CurrentDictionaryEntrySequence[i]->GetW(), "\"-");517}518}519}520521std::string MutationDispatcher::MutationSequence() {522std::string MS;523for (const auto &M : CurrentMutatorSequence) {524MS += M.Name;525MS += "-";526}527return MS;528}529530size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {531return MutateImpl(Data, Size, MaxSize, Mutators);532}533534size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size,535size_t MaxSize) {536return MutateImpl(Data, Size, MaxSize, DefaultMutators);537}538539// Mutates Data in place, returns new size.540size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,541size_t MaxSize,542std::vector<Mutator> &Mutators) {543assert(MaxSize > 0);544// Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),545// in which case they will return 0.546// Try several times before returning un-mutated data.547for (int Iter = 0; Iter < 100; Iter++) {548auto M = Mutators[Rand(Mutators.size())];549size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);550if (NewSize && NewSize <= MaxSize) {551if (Options.OnlyASCII)552ToASCII(Data, NewSize);553CurrentMutatorSequence.push_back(M);554return NewSize;555}556}557*Data = ' ';558return 1; // Fallback, should not happen frequently.559}560561// Mask represents the set of Data bytes that are worth mutating.562size_t MutationDispatcher::MutateWithMask(uint8_t *Data, size_t Size,563size_t MaxSize,564const std::vector<uint8_t> &Mask) {565size_t MaskedSize = std::min(Size, Mask.size());566// * Copy the worthy bytes into a temporary array T567// * Mutate T568// * Copy T back.569// This is totally unoptimized.570auto &T = MutateWithMaskTemp;571if (T.size() < Size)572T.resize(Size);573size_t OneBits = 0;574for (size_t I = 0; I < MaskedSize; I++)575if (Mask[I])576T[OneBits++] = Data[I];577578if (!OneBits) return 0;579assert(!T.empty());580size_t NewSize = Mutate(T.data(), OneBits, OneBits);581assert(NewSize <= OneBits);582(void)NewSize;583// Even if NewSize < OneBits we still use all OneBits bytes.584for (size_t I = 0, J = 0; I < MaskedSize; I++)585if (Mask[I])586Data[I] = T[J++];587return Size;588}589590void MutationDispatcher::AddWordToManualDictionary(const Word &W) {591ManualDictionary.push_back(592{W, std::numeric_limits<size_t>::max()});593}594595} // namespace fuzzer596597598