29 : Rand(Rand), Options(Options) {
30 DefaultMutators.insert(
31 DefaultMutators.begin(),
36 "InsertRepeatedBytes"},
50 DefaultMutators.push_back(
53 if (
EF->LLVMFuzzerCustomMutator)
56 Mutators = DefaultMutators;
58 if (
EF->LLVMFuzzerCustomCrossOver)
64 if (Rand.
RandBool())
return Rand(256);
65 const char *Special =
"!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
66 return Special[Rand(
sizeof(Special) - 1)];
71 return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.
Rand());
76 if (!Corpus || Corpus->
size() < 2 || Size == 0)
78 size_t Idx = Rand(Corpus->
size());
82 CustomCrossOverInPlaceHere.resize(MaxSize);
83 auto &U = CustomCrossOverInPlaceHere;
84 size_t NewSize =
EF->LLVMFuzzerCustomCrossOver(
85 Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.
Rand());
88 assert(NewSize <= MaxSize &&
"CustomCrossOver returned overisized unit");
89 memcpy(Data, U.data(), NewSize);
95 if (Size > MaxSize || Size == 0)
return 0;
96 size_t ShuffleAmount =
98 size_t ShuffleStart = Rand(Size - ShuffleAmount);
99 assert(ShuffleStart + ShuffleAmount <= Size);
100 std::shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount, Rand);
106 if (Size <= 1)
return 0;
107 size_t N = Rand(Size / 2) + 1;
109 size_t Idx = Rand(Size - N + 1);
111 memmove(Data + Idx, Data + Idx + N, Size - Idx - N);
118 if (Size >= MaxSize)
return 0;
119 size_t Idx = Rand(Size + 1);
121 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
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);
136 memmove(Data + Idx + N, Data + Idx, Size - Idx);
139 for (
size_t i = 0; i <
N; i++)
140 Data[Idx + i] = Byte;
146 if (Size > MaxSize)
return 0;
147 size_t Idx = Rand(Size);
154 if (Size > MaxSize)
return 0;
155 size_t Idx = Rand(Size);
156 Data[Idx] ^= 1 << Rand(8);
163 return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
166 size_t MutationDispatcher::ApplyDictionaryEntry(uint8_t *Data,
size_t Size,
174 if (Size + W.
size() > MaxSize)
return 0;
176 memmove(Data + Idx + W.
size(), Data + Idx, Size - Idx);
177 memcpy(Data + Idx, W.
data(), W.
size());
182 memcpy(Data + Idx, W.
data(), W.
size());
193 DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
194 const void *Arg1,
const void *Arg2,
195 const void *Arg1Mutation,
const void *Arg2Mutation,
196 size_t ArgSize,
const uint8_t *Data,
198 ScopedDoingMyOwnMemOrStr scoped_doing_my_own_mem_os_str;
200 const void *ExistingBytes, *DesiredBytes;
202 const uint8_t *
End = Data +
Size;
203 for (
int Arg = 0; Arg < 2; Arg++) {
204 ExistingBytes = HandleFirst ? Arg1 : Arg2;
205 DesiredBytes = HandleFirst ? Arg2Mutation : Arg1Mutation;
206 HandleFirst = !HandleFirst;
207 W.Set(reinterpret_cast<const uint8_t*>(DesiredBytes), ArgSize);
208 const size_t kMaxNumPositions = 8;
209 size_t Positions[kMaxNumPositions];
210 size_t NumPositions = 0;
211 for (
const uint8_t *Cur = Data;
212 Cur < End && NumPositions < kMaxNumPositions; Cur++) {
214 (
const uint8_t *)
SearchMemory(Cur, End - Cur, ExistingBytes, ArgSize);
216 Positions[NumPositions++] = Cur - Data;
218 if (!NumPositions)
continue;
219 return DictionaryEntry(W, Positions[Rand(NumPositions)]);
221 DictionaryEntry DE(W);
227 DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
228 T Arg1,
T Arg2,
const uint8_t *Data,
size_t Size) {
231 T Arg1Mutation = Arg1 + Rand(-1, 1);
232 T Arg2Mutation = Arg2 + Rand(-1, 1);
233 return MakeDictionaryEntryFromCMP(&Arg1, &Arg2, &Arg1Mutation, &Arg2Mutation,
234 sizeof(Arg1), Data, Size);
237 DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
238 const Word &Arg1,
const Word &Arg2,
const uint8_t *Data,
size_t Size) {
239 return MakeDictionaryEntryFromCMP(Arg1.data(), Arg2.data(), Arg1.data(),
240 Arg2.data(), Arg1.size(), Data,
Size);
244 uint8_t *Data,
size_t Size,
size_t MaxSize) {
250 DE = MakeDictionaryEntryFromCMP(
X.A,
X.B, Data, Size);
254 if ((
X.A >> 16) == 0 && (
X.B >> 16) == 0 && Rand.
RandBool())
255 DE = MakeDictionaryEntryFromCMP((uint16_t)
X.A, (uint16_t)
X.B, Data, Size);
257 DE = MakeDictionaryEntryFromCMP(
X.A,
X.B, Data, Size);
261 DE = MakeDictionaryEntryFromCMP(
X.A,
X.B, Data, Size);
271 Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);
274 CmpDictionaryEntriesDeque[CmpDictionaryEntriesDequeIdx++ %
275 kCmpDictionaryEntriesDequeSize];
277 CurrentDictionaryEntrySequence.push_back(&DERef);
282 uint8_t *Data,
size_t Size,
size_t MaxSize) {
283 return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
286 size_t MutationDispatcher::AddWordFromDictionary(
Dictionary &
D, uint8_t *Data,
287 size_t Size,
size_t MaxSize) {
288 if (Size > MaxSize)
return 0;
289 if (D.
empty())
return 0;
291 Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);
294 CurrentDictionaryEntrySequence.push_back(&DE);
300 size_t MutationDispatcher::CopyPartOf(
const uint8_t *From,
size_t FromSize,
301 uint8_t *To,
size_t ToSize) {
303 size_t ToBeg = Rand(ToSize);
304 size_t CopySize = Rand(ToSize - ToBeg) + 1;
305 assert(ToBeg + CopySize <= ToSize);
306 CopySize =
std::min(CopySize, FromSize);
307 size_t FromBeg = Rand(FromSize - CopySize + 1);
308 assert(FromBeg + CopySize <= FromSize);
309 memmove(To + ToBeg, From + FromBeg, CopySize);
315 size_t MutationDispatcher::InsertPartOf(
const uint8_t *From,
size_t FromSize,
316 uint8_t *To,
size_t ToSize,
318 if (ToSize >= MaxToSize)
return 0;
319 size_t AvailableSpace = MaxToSize - ToSize;
320 size_t MaxCopySize =
std::min(AvailableSpace, FromSize);
321 size_t CopySize = Rand(MaxCopySize) + 1;
322 size_t FromBeg = Rand(FromSize - CopySize + 1);
323 assert(FromBeg + CopySize <= FromSize);
324 size_t ToInsertPos = Rand(ToSize + 1);
325 assert(ToInsertPos + CopySize <= MaxToSize);
326 size_t TailSize = ToSize - ToInsertPos;
328 MutateInPlaceHere.resize(MaxToSize);
329 memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize);
330 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
331 memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize);
333 memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
334 memmove(To + ToInsertPos, From + FromBeg, CopySize);
336 return ToSize + CopySize;
341 if (Size > MaxSize || Size == 0)
return 0;
343 return CopyPartOf(Data, Size, Data, Size);
345 return InsertPartOf(Data, Size, Data, Size, MaxSize);
350 if (Size > MaxSize)
return 0;
351 size_t B = Rand(Size);
352 while (B < Size && !isdigit(Data[B])) B++;
353 if (B == Size)
return 0;
355 while (E < Size && isdigit(Data[E])) E++;
359 uint64_t Val = Data[
B] -
'0';
360 for (
size_t i = B + 1; i <
E; i++)
361 Val = Val * 10 + Data[i] -
'0';
365 case 0: Val++;
break;
366 case 1: Val--;
break;
367 case 2: Val /= 2;
break;
368 case 3: Val *= 2;
break;
369 case 4: Val = Rand(Val * Val);
break;
373 for (
size_t i = B; i <
E; i++) {
374 size_t Idx = E + B - i - 1;
375 assert(Idx >= B && Idx < E);
376 Data[Idx] = (Val % 10) +
'0';
384 if (Size <
sizeof(
T))
return 0;
385 size_t Off = Rand(Size -
sizeof(
T) + 1);
386 assert(Off +
sizeof(
T) <= Size);
388 if (Off < 64 && !Rand(4)) {
393 memcpy(&Val, Data + Off,
sizeof(Val));
403 memcpy(Data + Off, &Val,
sizeof(Val));
410 if (Size > MaxSize)
return 0;
412 case 3:
return ChangeBinaryInteger<uint64_t>(Data,
Size, Rand);
413 case 2:
return ChangeBinaryInteger<uint32_t>(Data,
Size, Rand);
414 case 1:
return ChangeBinaryInteger<uint16_t>(Data,
Size, Rand);
415 case 0:
return ChangeBinaryInteger<uint8_t>(Data,
Size, Rand);
423 if (Size > MaxSize)
return 0;
424 if (!Corpus || Corpus->
size() < 2 || Size == 0)
return 0;
425 size_t Idx = Rand(Corpus->
size());
426 const Unit &O = (*Corpus)[Idx];
427 if (O.empty())
return 0;
428 MutateInPlaceHere.resize(MaxSize);
429 auto &U = MutateInPlaceHere;
433 NewSize =
CrossOver(Data, Size, O.data(), O.size(), U.data(), U.size());
436 NewSize = InsertPartOf(O.data(), O.size(), U.data(), U.size(), MaxSize);
438 NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
441 NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
445 assert(NewSize > 0 &&
"CrossOver returned empty unit");
446 assert(NewSize <= MaxSize &&
"CrossOver returned overisized unit");
447 memcpy(Data, U.data(), NewSize);
452 CurrentMutatorSequence.clear();
453 CurrentDictionaryEntrySequence.clear();
458 for (
auto DE : CurrentDictionaryEntrySequence) {
469 std::vector<DictionaryEntry> V;
470 for (
auto &DE : PersistentAutoDictionary)
473 if (V.empty())
return;
474 Printf(
"###### Recommended dictionary. ######\n");
481 Printf(
"###### End of recommended dictionary. ######\n");
485 Printf(
"MS: %zd ", CurrentMutatorSequence.size());
486 for (
auto M : CurrentMutatorSequence)
488 if (!CurrentDictionaryEntrySequence.empty()) {
490 for (
auto DE : CurrentDictionaryEntrySequence) {
498 return MutateImpl(Data, Size, MaxSize, Mutators);
503 return MutateImpl(Data, Size, MaxSize, DefaultMutators);
507 size_t MutationDispatcher::MutateImpl(uint8_t *Data,
size_t Size,
509 const std::vector<Mutator> &Mutators) {
514 for (
int Iter = 0; Iter < 100; Iter++) {
515 auto M = Mutators[Rand(Mutators.size())];
516 size_t NewSize = (this->*(M.Fn))(Data,
Size, MaxSize);
517 if (NewSize && NewSize <= MaxSize) {
520 CurrentMutatorSequence.push_back(M);
static char RandCh(Random &Rand)
size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by inserting a byte.
size_t Mutate_Custom(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by invoking user-provided mutator.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
const Word & Get(size_t Idx)
void StartMutationSequence()
Indicate that we are about to start a new sequence of mutations.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
size_t Mutate_ChangeBinaryInteger(uint8_t *Data, size_t Size, size_t MaxSize)
Change a 1-, 2-, 4-, or 8-byte integer in interesting ways.
size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize)
CrossOver Data with some other element of the corpus.
size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize)
Applies one of the configured mutations.
size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2, size_t Size2, uint8_t *Out, size_t MaxOutSize)
Creates a cross-over of two pieces of Data, returns its size.
size_t DefaultMutate(uint8_t *Data, size_t Size, size_t MaxSize)
Applies one of the default mutations.
size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by adding a word from the manual dictionary.
void PrintRecommendedDictionary()
void PrintMutationSequence()
Print the current sequence of mutations.
void RecordSuccessfulMutationSequence()
Indicate that the current sequence of mutations was successfull.
MutationDispatcher(Random &Rand, const FuzzingOptions &Options)
size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by chanding one bit.
size_t Mutate_CustomCrossOver(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by invoking user-provided crossover.
const uint8_t * data() const
static PassOptionList PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"), cl::Hidden)
bool HasPositionHint() const
PrintASCII(Dict[i].data(), Dict[i].size(),"\"")
size_t Mutate_InsertRepeatedBytes(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by inserting several repeated bytes.
void AddWordToManualDictionary(const Word &W)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by adding a word from the persistent automatic dictionary.
static const unsigned End
void push_back(DictionaryEntry DE)
size_t Mutate_CopyPart(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by copying/inserting a part of data into a different place.
size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize)
Tries to find an ASCII integer in Data, changes it to another ASCII int.
TableOfRecentCompares< Word, 32 > TORCW
bool ToASCII(uint8_t *Data, size_t Size)
bool ContainsWord(const Word &W) const
const void * SearchMemory(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen)
size_t GetUseCount() const
ATTRIBUTE_NO_SANITIZE_ALL __attribute__((noinline)) void TracePC size_t N
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
size_t GetPositionHint() const
size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by chanding one byte.
static const size_t kMaxDictSize
std::vector< uint8_t > Unit
TableOfRecentCompares< uint64_t, 32 > TORC8
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand)
TableOfRecentCompares< uint32_t, 32 > TORC4
Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i])
const Word & GetW() const
size_t Mutate_AddWordFromTORC(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by adding a word from the TORC.
size_t Mutate_EraseBytes(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by erasing bytes.
size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by shuffling bytes.