2015-01-29 16:58:29 +00:00
|
|
|
//===- FuzzerMutate.cpp - Mutate a test input -----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Mutate a test input.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-21 02:05:39 +00:00
|
|
|
#include "FuzzerCorpus.h"
|
|
|
|
#include "FuzzerDefs.h"
|
|
|
|
#include "FuzzerExtFunctions.h"
|
2016-11-30 19:06:14 +00:00
|
|
|
#include "FuzzerIO.h"
|
2016-09-21 01:50:50 +00:00
|
|
|
#include "FuzzerMutate.h"
|
2016-09-21 02:05:39 +00:00
|
|
|
#include "FuzzerOptions.h"
|
2015-08-06 01:29:13 +00:00
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
namespace fuzzer {
|
|
|
|
|
2016-01-21 01:52:14 +00:00
|
|
|
const size_t Dictionary::kMaxDictSize;
|
|
|
|
|
2016-09-21 01:50:50 +00:00
|
|
|
static void PrintASCII(const Word &W, const char *PrintAfter) {
|
|
|
|
PrintASCII(W.data(), W.size(), PrintAfter);
|
|
|
|
}
|
|
|
|
|
2016-06-23 20:44:48 +00:00
|
|
|
MutationDispatcher::MutationDispatcher(Random &Rand,
|
|
|
|
const FuzzingOptions &Options)
|
|
|
|
: Rand(Rand), Options(Options) {
|
2016-06-03 21:34:29 +00:00
|
|
|
DefaultMutators.insert(
|
|
|
|
DefaultMutators.begin(),
|
|
|
|
{
|
2016-08-15 17:48:28 +00:00
|
|
|
{&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"},
|
2016-06-03 21:34:29 +00:00
|
|
|
{&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
|
2016-08-15 17:48:28 +00:00
|
|
|
{&MutationDispatcher::Mutate_InsertRepeatedBytes,
|
|
|
|
"InsertRepeatedBytes"},
|
2016-06-03 21:34:29 +00:00
|
|
|
{&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
|
|
|
|
{&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
|
|
|
|
{&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
|
|
|
|
{&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
|
2016-08-17 21:30:30 +00:00
|
|
|
{&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"},
|
2016-08-17 18:10:42 +00:00
|
|
|
{&MutationDispatcher::Mutate_CopyPart, "CopyPart"},
|
2016-06-03 21:34:29 +00:00
|
|
|
{&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
|
|
|
|
{&MutationDispatcher::Mutate_AddWordFromManualDictionary,
|
2016-10-14 20:20:33 +00:00
|
|
|
"ManualDict"},
|
2016-06-03 21:34:29 +00:00
|
|
|
{&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
|
2016-10-14 20:20:33 +00:00
|
|
|
"TempAutoDict"},
|
2016-06-03 21:34:29 +00:00
|
|
|
{&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
|
2016-10-14 20:20:33 +00:00
|
|
|
"PersAutoDict"},
|
2016-06-03 21:34:29 +00:00
|
|
|
});
|
2016-10-14 20:20:33 +00:00
|
|
|
if(Options.UseCmp)
|
|
|
|
DefaultMutators.push_back(
|
2016-10-25 02:04:43 +00:00
|
|
|
{&MutationDispatcher::Mutate_AddWordFromTORC, "CMP"});
|
2016-06-03 21:34:29 +00:00
|
|
|
|
2016-06-07 23:32:50 +00:00
|
|
|
if (EF->LLVMFuzzerCustomMutator)
|
2016-06-03 21:34:29 +00:00
|
|
|
Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
|
|
|
|
else
|
|
|
|
Mutators = DefaultMutators;
|
2016-06-07 20:22:15 +00:00
|
|
|
|
2016-06-07 23:32:50 +00:00
|
|
|
if (EF->LLVMFuzzerCustomCrossOver)
|
2016-06-07 20:22:15 +00:00
|
|
|
Mutators.push_back(
|
|
|
|
{&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
|
2016-06-03 21:34:29 +00:00
|
|
|
}
|
2016-02-13 03:46:26 +00:00
|
|
|
|
2016-02-13 03:00:53 +00:00
|
|
|
static char RandCh(Random &Rand) {
|
2015-07-24 01:06:40 +00:00
|
|
|
if (Rand.RandBool()) return Rand(256);
|
2016-08-17 18:10:42 +00:00
|
|
|
const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
|
2015-07-24 01:06:40 +00:00
|
|
|
return Special[Rand(sizeof(Special) - 1)];
|
2015-01-29 16:58:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 21:34:29 +00:00
|
|
|
size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-06-07 23:32:50 +00:00
|
|
|
return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
|
2016-06-03 21:34:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 20:22:15 +00:00
|
|
|
size_t MutationDispatcher::Mutate_CustomCrossOver(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
|
|
|
if (!Corpus || Corpus->size() < 2 || Size == 0)
|
|
|
|
return 0;
|
|
|
|
size_t Idx = Rand(Corpus->size());
|
|
|
|
const Unit &Other = (*Corpus)[Idx];
|
|
|
|
if (Other.empty())
|
|
|
|
return 0;
|
2017-03-07 20:37:38 +00:00
|
|
|
CustomCrossOverInPlaceHere.resize(MaxSize);
|
|
|
|
auto &U = CustomCrossOverInPlaceHere;
|
2016-06-07 23:32:50 +00:00
|
|
|
size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
|
2016-06-07 20:22:15 +00:00
|
|
|
Data, Size, Other.data(), Other.size(), U.data(), U.size(), Rand.Rand());
|
|
|
|
if (!NewSize)
|
|
|
|
return 0;
|
|
|
|
assert(NewSize <= MaxSize && "CustomCrossOver returned overisized unit");
|
|
|
|
memcpy(Data, U.data(), NewSize);
|
|
|
|
return NewSize;
|
|
|
|
}
|
|
|
|
|
2015-09-03 21:24:19 +00:00
|
|
|
size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2017-01-23 22:52:13 +00:00
|
|
|
if (Size > MaxSize || Size == 0) return 0;
|
2016-01-07 01:49:35 +00:00
|
|
|
size_t ShuffleAmount =
|
|
|
|
Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
|
2015-08-06 01:29:13 +00:00
|
|
|
size_t ShuffleStart = Rand(Size - ShuffleAmount);
|
|
|
|
assert(ShuffleStart + ShuffleAmount <= Size);
|
2017-02-07 22:37:34 +00:00
|
|
|
std::shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount, Rand);
|
2015-08-06 01:29:13 +00:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:48:28 +00:00
|
|
|
size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2017-01-23 22:52:13 +00:00
|
|
|
if (Size <= 1) return 0;
|
2016-08-15 17:48:28 +00:00
|
|
|
size_t N = Rand(Size / 2) + 1;
|
|
|
|
assert(N < Size);
|
|
|
|
size_t Idx = Rand(Size - N + 1);
|
|
|
|
// Erase Data[Idx:Idx+N].
|
|
|
|
memmove(Data + Idx, Data + Idx + N, Size - Idx - N);
|
|
|
|
// Printf("Erase: %zd %zd => %zd; Idx %zd\n", N, Size, Size - N, Idx);
|
|
|
|
return Size - N;
|
2015-08-01 01:42:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 21:24:19 +00:00
|
|
|
size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 01:34:58 +00:00
|
|
|
if (Size >= MaxSize) return 0;
|
2015-08-01 02:23:06 +00:00
|
|
|
size_t Idx = Rand(Size + 1);
|
|
|
|
// Insert new value at Data[Idx].
|
|
|
|
memmove(Data + Idx + 1, Data + Idx, Size - Idx);
|
|
|
|
Data[Idx] = RandCh(Rand);
|
|
|
|
return Size + 1;
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:48:28 +00:00
|
|
|
size_t MutationDispatcher::Mutate_InsertRepeatedBytes(uint8_t *Data,
|
|
|
|
size_t Size,
|
|
|
|
size_t MaxSize) {
|
|
|
|
const size_t kMinBytesToInsert = 3;
|
|
|
|
if (Size + kMinBytesToInsert >= MaxSize) return 0;
|
|
|
|
size_t MaxBytesToInsert = std::min(MaxSize - Size, (size_t)128);
|
|
|
|
size_t N = Rand(MaxBytesToInsert - kMinBytesToInsert + 1) + kMinBytesToInsert;
|
|
|
|
assert(Size + N <= MaxSize && N);
|
|
|
|
size_t Idx = Rand(Size + 1);
|
|
|
|
// Insert new values at Data[Idx].
|
|
|
|
memmove(Data + Idx + N, Data + Idx, Size - Idx);
|
2016-08-17 21:50:54 +00:00
|
|
|
// Give preference to 0x00 and 0xff.
|
|
|
|
uint8_t Byte = Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255);
|
2016-08-15 17:48:28 +00:00
|
|
|
for (size_t i = 0; i < N; i++)
|
|
|
|
Data[Idx + i] = Byte;
|
|
|
|
return Size + N;
|
|
|
|
}
|
|
|
|
|
2015-09-03 21:24:19 +00:00
|
|
|
size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 01:34:58 +00:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-08-01 02:23:06 +00:00
|
|
|
size_t Idx = Rand(Size);
|
|
|
|
Data[Idx] = RandCh(Rand);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2015-09-03 21:24:19 +00:00
|
|
|
size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 01:34:58 +00:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-08-01 02:23:06 +00:00
|
|
|
size_t Idx = Rand(Size);
|
2016-08-17 18:10:42 +00:00
|
|
|
Data[Idx] ^= 1 << Rand(8);
|
2015-08-01 02:23:06 +00:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2016-01-07 01:49:35 +00:00
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
|
|
|
|
size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-02-13 03:37:24 +00:00
|
|
|
return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
|
2016-01-07 01:49:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 02:36:44 +00:00
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
|
|
|
|
uint8_t *Data, size_t Size, size_t MaxSize) {
|
2016-02-13 03:37:24 +00:00
|
|
|
return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
|
2016-01-14 02:36:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 02:04:43 +00:00
|
|
|
size_t MutationDispatcher::ApplyDictionaryEntry(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize,
|
|
|
|
DictionaryEntry &DE) {
|
2016-01-22 23:55:14 +00:00
|
|
|
const Word &W = DE.GetW();
|
|
|
|
bool UsePositionHint = DE.HasPositionHint() &&
|
2016-10-25 02:04:43 +00:00
|
|
|
DE.GetPositionHint() + W.size() < Size &&
|
|
|
|
Rand.RandBool();
|
2016-01-16 03:53:32 +00:00
|
|
|
if (Rand.RandBool()) { // Insert W.
|
|
|
|
if (Size + W.size() > MaxSize) return 0;
|
2016-01-22 23:55:14 +00:00
|
|
|
size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
|
2016-01-16 03:53:32 +00:00
|
|
|
memmove(Data + Idx + W.size(), Data + Idx, Size - Idx);
|
|
|
|
memcpy(Data + Idx, W.data(), W.size());
|
|
|
|
Size += W.size();
|
|
|
|
} else { // Overwrite some bytes with W.
|
|
|
|
if (W.size() > Size) return 0;
|
2016-01-22 23:55:14 +00:00
|
|
|
size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
|
2016-01-16 03:53:32 +00:00
|
|
|
memcpy(Data + Idx, W.data(), W.size());
|
2016-01-06 02:13:04 +00:00
|
|
|
}
|
2016-10-25 02:04:43 +00:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Somewhere in the past we have observed a comparison instructions
|
|
|
|
// with arguments Arg1 Arg2. This function tries to guess a dictionary
|
|
|
|
// entry that will satisfy that comparison.
|
|
|
|
// It first tries to find one of the arguments (possibly swapped) in the
|
|
|
|
// input and if it succeeds it creates a DE with a position hint.
|
|
|
|
// Otherwise it creates a DE with one of the arguments w/o a position hint.
|
|
|
|
DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
|
2017-01-17 23:09:05 +00:00
|
|
|
const void *Arg1, const void *Arg2,
|
|
|
|
const void *Arg1Mutation, const void *Arg2Mutation,
|
|
|
|
size_t ArgSize, const uint8_t *Data,
|
|
|
|
size_t Size) {
|
2016-10-25 02:04:43 +00:00
|
|
|
ScopedDoingMyOwnMemmem scoped_doing_my_own_memmem;
|
|
|
|
bool HandleFirst = Rand.RandBool();
|
2017-01-17 23:09:05 +00:00
|
|
|
const void *ExistingBytes, *DesiredBytes;
|
2016-10-25 02:04:43 +00:00
|
|
|
Word W;
|
|
|
|
const uint8_t *End = Data + Size;
|
|
|
|
for (int Arg = 0; Arg < 2; Arg++) {
|
|
|
|
ExistingBytes = HandleFirst ? Arg1 : Arg2;
|
2017-01-17 23:09:05 +00:00
|
|
|
DesiredBytes = HandleFirst ? Arg2Mutation : Arg1Mutation;
|
2016-10-25 02:04:43 +00:00
|
|
|
HandleFirst = !HandleFirst;
|
2017-01-17 23:09:05 +00:00
|
|
|
W.Set(reinterpret_cast<const uint8_t*>(DesiredBytes), ArgSize);
|
2016-10-25 02:04:43 +00:00
|
|
|
const size_t kMaxNumPositions = 8;
|
|
|
|
size_t Positions[kMaxNumPositions];
|
|
|
|
size_t NumPositions = 0;
|
|
|
|
for (const uint8_t *Cur = Data;
|
|
|
|
Cur < End && NumPositions < kMaxNumPositions; Cur++) {
|
2017-01-17 23:09:05 +00:00
|
|
|
Cur = (uint8_t *)SearchMemory(Cur, End - Cur, ExistingBytes, ArgSize);
|
2016-10-25 02:04:43 +00:00
|
|
|
if (!Cur) break;
|
|
|
|
Positions[NumPositions++] = Cur - Data;
|
|
|
|
}
|
|
|
|
if (!NumPositions) break;
|
|
|
|
return DictionaryEntry(W, Positions[Rand(NumPositions)]);
|
|
|
|
}
|
|
|
|
DictionaryEntry DE(W);
|
|
|
|
return DE;
|
|
|
|
}
|
|
|
|
|
2017-01-17 23:09:05 +00:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
|
|
|
|
T Arg1, T Arg2, const uint8_t *Data, size_t Size) {
|
|
|
|
if (Rand.RandBool()) Arg1 = Bswap(Arg1);
|
|
|
|
if (Rand.RandBool()) Arg2 = Bswap(Arg2);
|
|
|
|
T Arg1Mutation = Arg1 + Rand(-1, 1);
|
|
|
|
T Arg2Mutation = Arg2 + Rand(-1, 1);
|
|
|
|
return MakeDictionaryEntryFromCMP(&Arg1, &Arg2, &Arg1Mutation, &Arg2Mutation,
|
|
|
|
sizeof(Arg1), Data, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
DictionaryEntry MutationDispatcher::MakeDictionaryEntryFromCMP(
|
|
|
|
const Word &Arg1, const Word &Arg2, const uint8_t *Data, size_t Size) {
|
|
|
|
return MakeDictionaryEntryFromCMP(Arg1.data(), Arg2.data(), Arg1.data(),
|
|
|
|
Arg2.data(), Arg1.size(), Data, Size);
|
|
|
|
}
|
|
|
|
|
2016-10-25 02:04:43 +00:00
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromTORC(
|
|
|
|
uint8_t *Data, size_t Size, size_t MaxSize) {
|
|
|
|
Word W;
|
|
|
|
DictionaryEntry DE;
|
2017-01-17 23:09:05 +00:00
|
|
|
switch (Rand(3)) {
|
|
|
|
case 0: {
|
2016-10-25 02:04:43 +00:00
|
|
|
auto X = TPC.TORC8.Get(Rand.Rand());
|
|
|
|
DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);
|
2017-01-17 23:09:05 +00:00
|
|
|
} break;
|
|
|
|
case 1: {
|
2016-10-25 02:04:43 +00:00
|
|
|
auto X = TPC.TORC4.Get(Rand.Rand());
|
|
|
|
if ((X.A >> 16) == 0 && (X.B >> 16) == 0 && Rand.RandBool())
|
2017-01-17 23:09:05 +00:00
|
|
|
DE = MakeDictionaryEntryFromCMP((uint16_t)X.A, (uint16_t)X.B, Data, Size);
|
2016-10-25 02:04:43 +00:00
|
|
|
else
|
|
|
|
DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);
|
2017-01-17 23:09:05 +00:00
|
|
|
} break;
|
|
|
|
case 2: {
|
|
|
|
auto X = TPC.TORCW.Get(Rand.Rand());
|
|
|
|
DE = MakeDictionaryEntryFromCMP(X.A, X.B, Data, Size);
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
2016-10-25 02:04:43 +00:00
|
|
|
}
|
2017-01-19 21:14:47 +00:00
|
|
|
if (!DE.GetW().size()) return 0;
|
2016-10-25 02:04:43 +00:00
|
|
|
Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);
|
|
|
|
if (!Size) return 0;
|
|
|
|
DictionaryEntry &DERef =
|
|
|
|
CmpDictionaryEntriesDeque[CmpDictionaryEntriesDequeIdx++ %
|
|
|
|
kCmpDictionaryEntriesDequeSize];
|
|
|
|
DERef = DE;
|
|
|
|
CurrentDictionaryEntrySequence.push_back(&DERef);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
|
|
|
|
uint8_t *Data, size_t Size, size_t MaxSize) {
|
|
|
|
return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
|
|
|
|
size_t Size, size_t MaxSize) {
|
|
|
|
if (Size > MaxSize) return 0;
|
|
|
|
if (D.empty()) return 0;
|
|
|
|
DictionaryEntry &DE = D[Rand(D.size())];
|
|
|
|
Size = ApplyDictionaryEntry(Data, Size, MaxSize, DE);
|
|
|
|
if (!Size) return 0;
|
2016-01-22 23:55:14 +00:00
|
|
|
DE.IncUseCount();
|
|
|
|
CurrentDictionaryEntrySequence.push_back(&DE);
|
2016-01-12 02:36:59 +00:00
|
|
|
return Size;
|
2015-09-04 00:12:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 18:10:42 +00:00
|
|
|
// Overwrites part of To[0,ToSize) with a part of From[0,FromSize).
|
|
|
|
// Returns ToSize.
|
|
|
|
size_t MutationDispatcher::CopyPartOf(const uint8_t *From, size_t FromSize,
|
|
|
|
uint8_t *To, size_t ToSize) {
|
|
|
|
// Copy From[FromBeg, FromBeg + CopySize) into To[ToBeg, ToBeg + CopySize).
|
|
|
|
size_t ToBeg = Rand(ToSize);
|
|
|
|
size_t CopySize = Rand(ToSize - ToBeg) + 1;
|
|
|
|
assert(ToBeg + CopySize <= ToSize);
|
|
|
|
CopySize = std::min(CopySize, FromSize);
|
|
|
|
size_t FromBeg = Rand(FromSize - CopySize + 1);
|
|
|
|
assert(FromBeg + CopySize <= FromSize);
|
|
|
|
memmove(To + ToBeg, From + FromBeg, CopySize);
|
|
|
|
return ToSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts part of From[0,ToSize) into To.
|
|
|
|
// Returns new size of To on success or 0 on failure.
|
|
|
|
size_t MutationDispatcher::InsertPartOf(const uint8_t *From, size_t FromSize,
|
|
|
|
uint8_t *To, size_t ToSize,
|
|
|
|
size_t MaxToSize) {
|
|
|
|
if (ToSize >= MaxToSize) return 0;
|
|
|
|
size_t AvailableSpace = MaxToSize - ToSize;
|
|
|
|
size_t MaxCopySize = std::min(AvailableSpace, FromSize);
|
|
|
|
size_t CopySize = Rand(MaxCopySize) + 1;
|
|
|
|
size_t FromBeg = Rand(FromSize - CopySize + 1);
|
|
|
|
assert(FromBeg + CopySize <= FromSize);
|
|
|
|
size_t ToInsertPos = Rand(ToSize + 1);
|
|
|
|
assert(ToInsertPos + CopySize <= MaxToSize);
|
|
|
|
size_t TailSize = ToSize - ToInsertPos;
|
|
|
|
if (To == From) {
|
|
|
|
MutateInPlaceHere.resize(MaxToSize);
|
|
|
|
memcpy(MutateInPlaceHere.data(), From + FromBeg, CopySize);
|
|
|
|
memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
|
|
|
|
memmove(To + ToInsertPos, MutateInPlaceHere.data(), CopySize);
|
|
|
|
} else {
|
|
|
|
memmove(To + ToInsertPos + CopySize, To + ToInsertPos, TailSize);
|
|
|
|
memmove(To + ToInsertPos, From + FromBeg, CopySize);
|
|
|
|
}
|
|
|
|
return ToSize + CopySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MutationDispatcher::Mutate_CopyPart(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2017-01-23 22:52:13 +00:00
|
|
|
if (Size > MaxSize || Size == 0) return 0;
|
2016-08-17 18:10:42 +00:00
|
|
|
if (Rand.RandBool())
|
|
|
|
return CopyPartOf(Data, Size, Data, Size);
|
|
|
|
else
|
|
|
|
return InsertPartOf(Data, Size, Data, Size, MaxSize);
|
|
|
|
}
|
|
|
|
|
2015-09-08 17:19:31 +00:00
|
|
|
size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 01:34:58 +00:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-09-08 17:19:31 +00:00
|
|
|
size_t B = Rand(Size);
|
|
|
|
while (B < Size && !isdigit(Data[B])) B++;
|
|
|
|
if (B == Size) return 0;
|
|
|
|
size_t E = B;
|
|
|
|
while (E < Size && isdigit(Data[E])) E++;
|
|
|
|
assert(B < E);
|
|
|
|
// now we have digits in [B, E).
|
|
|
|
// strtol and friends don't accept non-zero-teminated data, parse it manually.
|
|
|
|
uint64_t Val = Data[B] - '0';
|
|
|
|
for (size_t i = B + 1; i < E; i++)
|
|
|
|
Val = Val * 10 + Data[i] - '0';
|
|
|
|
|
|
|
|
// Mutate the integer value.
|
|
|
|
switch(Rand(5)) {
|
|
|
|
case 0: Val++; break;
|
|
|
|
case 1: Val--; break;
|
|
|
|
case 2: Val /= 2; break;
|
|
|
|
case 3: Val *= 2; break;
|
|
|
|
case 4: Val = Rand(Val * Val); break;
|
|
|
|
default: assert(0);
|
|
|
|
}
|
|
|
|
// Just replace the bytes with the new ones, don't bother moving bytes.
|
|
|
|
for (size_t i = B; i < E; i++) {
|
|
|
|
size_t Idx = E + B - i - 1;
|
|
|
|
assert(Idx >= B && Idx < E);
|
|
|
|
Data[Idx] = (Val % 10) + '0';
|
|
|
|
Val /= 10;
|
|
|
|
}
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2016-08-17 21:30:30 +00:00
|
|
|
template<class T>
|
|
|
|
size_t ChangeBinaryInteger(uint8_t *Data, size_t Size, Random &Rand) {
|
|
|
|
if (Size < sizeof(T)) return 0;
|
|
|
|
size_t Off = Rand(Size - sizeof(T) + 1);
|
|
|
|
assert(Off + sizeof(T) <= Size);
|
|
|
|
T Val;
|
2016-10-22 03:48:53 +00:00
|
|
|
if (Off < 64 && !Rand(4)) {
|
|
|
|
Val = Size;
|
|
|
|
if (Rand.RandBool())
|
|
|
|
Val = Bswap(Val);
|
|
|
|
} else {
|
|
|
|
memcpy(&Val, Data + Off, sizeof(Val));
|
|
|
|
T Add = Rand(21);
|
|
|
|
Add -= 10;
|
|
|
|
if (Rand.RandBool())
|
|
|
|
Val = Bswap(T(Bswap(Val) + Add)); // Add assuming different endiannes.
|
|
|
|
else
|
|
|
|
Val = Val + Add; // Add assuming current endiannes.
|
|
|
|
if (Add == 0 || Rand.RandBool()) // Maybe negate.
|
|
|
|
Val = -Val;
|
|
|
|
}
|
2016-08-17 21:30:30 +00:00
|
|
|
memcpy(Data + Off, &Val, sizeof(Val));
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MutationDispatcher::Mutate_ChangeBinaryInteger(uint8_t *Data,
|
|
|
|
size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 01:34:58 +00:00
|
|
|
if (Size > MaxSize) return 0;
|
2016-08-17 21:30:30 +00:00
|
|
|
switch (Rand(4)) {
|
|
|
|
case 3: return ChangeBinaryInteger<uint64_t>(Data, Size, Rand);
|
|
|
|
case 2: return ChangeBinaryInteger<uint32_t>(Data, Size, Rand);
|
|
|
|
case 1: return ChangeBinaryInteger<uint16_t>(Data, Size, Rand);
|
|
|
|
case 0: return ChangeBinaryInteger<uint8_t>(Data, Size, Rand);
|
|
|
|
default: assert(0);
|
|
|
|
}
|
2016-08-19 20:57:09 +00:00
|
|
|
return 0;
|
2016-08-17 21:30:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 02:49:09 +00:00
|
|
|
size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 01:34:58 +00:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-12-19 02:49:09 +00:00
|
|
|
if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
|
|
|
|
size_t Idx = Rand(Corpus->size());
|
2016-08-17 18:10:42 +00:00
|
|
|
const Unit &O = (*Corpus)[Idx];
|
|
|
|
if (O.empty()) return 0;
|
2016-02-13 17:56:51 +00:00
|
|
|
MutateInPlaceHere.resize(MaxSize);
|
|
|
|
auto &U = MutateInPlaceHere;
|
2016-08-19 20:57:09 +00:00
|
|
|
size_t NewSize = 0;
|
2016-08-17 18:10:42 +00:00
|
|
|
switch(Rand(3)) {
|
|
|
|
case 0:
|
|
|
|
NewSize = CrossOver(Data, Size, O.data(), O.size(), U.data(), U.size());
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
NewSize = InsertPartOf(O.data(), O.size(), U.data(), U.size(), MaxSize);
|
2017-02-06 21:21:37 +00:00
|
|
|
if (!NewSize)
|
|
|
|
NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
|
|
|
|
break;
|
2016-08-17 18:10:42 +00:00
|
|
|
case 2:
|
|
|
|
NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
|
|
|
|
break;
|
|
|
|
default: assert(0);
|
|
|
|
}
|
2015-12-19 02:49:09 +00:00
|
|
|
assert(NewSize > 0 && "CrossOver returned empty unit");
|
|
|
|
assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
|
|
|
|
memcpy(Data, U.data(), NewSize);
|
|
|
|
return NewSize;
|
|
|
|
}
|
|
|
|
|
2015-12-19 01:09:49 +00:00
|
|
|
void MutationDispatcher::StartMutationSequence() {
|
2016-02-13 03:37:24 +00:00
|
|
|
CurrentMutatorSequence.clear();
|
|
|
|
CurrentDictionaryEntrySequence.clear();
|
2015-12-19 01:09:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 02:36:44 +00:00
|
|
|
// Copy successful dictionary entries to PersistentAutoDictionary.
|
|
|
|
void MutationDispatcher::RecordSuccessfulMutationSequence() {
|
2016-02-13 03:37:24 +00:00
|
|
|
for (auto DE : CurrentDictionaryEntrySequence) {
|
|
|
|
// PersistentAutoDictionary.AddWithSuccessCountOne(DE);
|
2016-01-22 23:55:14 +00:00
|
|
|
DE->IncSuccessCount();
|
2017-01-19 21:14:47 +00:00
|
|
|
assert(DE->GetW().size());
|
2016-01-14 02:36:44 +00:00
|
|
|
// Linear search is fine here as this happens seldom.
|
2016-02-13 03:37:24 +00:00
|
|
|
if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
|
|
|
|
PersistentAutoDictionary.push_back({DE->GetW(), 1});
|
2016-01-22 23:55:14 +00:00
|
|
|
}
|
2016-01-14 02:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MutationDispatcher::PrintRecommendedDictionary() {
|
2016-01-22 23:55:14 +00:00
|
|
|
std::vector<DictionaryEntry> V;
|
2016-02-13 03:37:24 +00:00
|
|
|
for (auto &DE : PersistentAutoDictionary)
|
|
|
|
if (!ManualDictionary.ContainsWord(DE.GetW()))
|
2016-01-22 23:55:14 +00:00
|
|
|
V.push_back(DE);
|
2016-01-14 02:36:44 +00:00
|
|
|
if (V.empty()) return;
|
|
|
|
Printf("###### Recommended dictionary. ######\n");
|
2016-01-22 23:55:14 +00:00
|
|
|
for (auto &DE: V) {
|
2017-01-19 21:14:47 +00:00
|
|
|
assert(DE.GetW().size());
|
2016-01-14 02:36:44 +00:00
|
|
|
Printf("\"");
|
2016-01-22 23:55:14 +00:00
|
|
|
PrintASCII(DE.GetW(), "\"");
|
|
|
|
Printf(" # Uses: %zd\n", DE.GetUseCount());
|
2016-01-14 02:36:44 +00:00
|
|
|
}
|
|
|
|
Printf("###### End of recommended dictionary. ######\n");
|
|
|
|
}
|
|
|
|
|
2015-12-19 01:09:49 +00:00
|
|
|
void MutationDispatcher::PrintMutationSequence() {
|
2016-02-13 03:37:24 +00:00
|
|
|
Printf("MS: %zd ", CurrentMutatorSequence.size());
|
|
|
|
for (auto M : CurrentMutatorSequence)
|
2015-12-19 01:09:49 +00:00
|
|
|
Printf("%s-", M.Name);
|
2016-02-13 03:37:24 +00:00
|
|
|
if (!CurrentDictionaryEntrySequence.empty()) {
|
2016-01-12 02:36:59 +00:00
|
|
|
Printf(" DE: ");
|
2016-02-13 03:37:24 +00:00
|
|
|
for (auto DE : CurrentDictionaryEntrySequence) {
|
2016-01-12 02:36:59 +00:00
|
|
|
Printf("\"");
|
2016-01-22 23:55:14 +00:00
|
|
|
PrintASCII(DE->GetW(), "\"-");
|
2016-01-12 02:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-19 01:09:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 21:24:19 +00:00
|
|
|
size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
|
2016-06-03 21:34:29 +00:00
|
|
|
return MutateImpl(Data, Size, MaxSize, Mutators);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MutationDispatcher::DefaultMutate(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
|
|
|
return MutateImpl(Data, Size, MaxSize, DefaultMutators);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutates Data in place, returns new size.
|
|
|
|
size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize,
|
|
|
|
const std::vector<Mutator> &Mutators) {
|
2015-05-22 22:35:31 +00:00
|
|
|
assert(MaxSize > 0);
|
2015-09-04 00:40:29 +00:00
|
|
|
// Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize),
|
|
|
|
// in which case they will return 0.
|
|
|
|
// Try several times before returning un-mutated data.
|
2016-09-22 01:34:58 +00:00
|
|
|
for (int Iter = 0; Iter < 100; Iter++) {
|
2016-06-03 21:34:29 +00:00
|
|
|
auto M = Mutators[Rand(Mutators.size())];
|
2015-12-19 01:09:49 +00:00
|
|
|
size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
|
2016-09-22 01:34:58 +00:00
|
|
|
if (NewSize && NewSize <= MaxSize) {
|
2016-06-23 20:44:48 +00:00
|
|
|
if (Options.OnlyASCII)
|
|
|
|
ToASCII(Data, NewSize);
|
2016-02-13 03:37:24 +00:00
|
|
|
CurrentMutatorSequence.push_back(M);
|
2015-12-19 01:09:49 +00:00
|
|
|
return NewSize;
|
|
|
|
}
|
2015-09-04 00:40:29 +00:00
|
|
|
}
|
2017-01-23 22:52:13 +00:00
|
|
|
*Data = ' ';
|
|
|
|
return 1; // Fallback, should not happen frequently.
|
2015-01-29 16:58:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 03:53:32 +00:00
|
|
|
void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
|
2016-02-13 03:37:24 +00:00
|
|
|
ManualDictionary.push_back(
|
2016-01-16 03:53:32 +00:00
|
|
|
{W, std::numeric_limits<size_t>::max()});
|
2016-01-07 01:49:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 23:27:19 +00:00
|
|
|
void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) {
|
2016-01-09 03:08:58 +00:00
|
|
|
static const size_t kMaxAutoDictSize = 1 << 14;
|
2016-02-13 03:37:24 +00:00
|
|
|
if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
|
2016-07-15 23:27:19 +00:00
|
|
|
TempAutoDictionary.push_back(DE);
|
2015-09-04 00:12:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 03:08:58 +00:00
|
|
|
void MutationDispatcher::ClearAutoDictionary() {
|
2016-02-13 03:37:24 +00:00
|
|
|
TempAutoDictionary.clear();
|
2016-01-09 03:08:58 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
} // namespace fuzzer
|