2015-01-29 17:58:29 +01: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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-23 00:35:31 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
2016-09-21 04:05:39 +02:00
|
|
|
#include "FuzzerCorpus.h"
|
|
|
|
#include "FuzzerDefs.h"
|
|
|
|
#include "FuzzerExtFunctions.h"
|
2016-09-21 03:50:50 +02:00
|
|
|
#include "FuzzerMutate.h"
|
2016-09-21 04:05:39 +02:00
|
|
|
#include "FuzzerOptions.h"
|
2015-08-06 03:29:13 +02:00
|
|
|
|
2015-01-29 17:58:29 +01:00
|
|
|
namespace fuzzer {
|
|
|
|
|
2016-01-21 02:52:14 +01:00
|
|
|
const size_t Dictionary::kMaxDictSize;
|
|
|
|
|
2016-09-21 03:50:50 +02:00
|
|
|
static void PrintASCII(const Word &W, const char *PrintAfter) {
|
|
|
|
PrintASCII(W.data(), W.size(), PrintAfter);
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:44:48 +02:00
|
|
|
MutationDispatcher::MutationDispatcher(Random &Rand,
|
|
|
|
const FuzzingOptions &Options)
|
|
|
|
: Rand(Rand), Options(Options) {
|
2016-06-03 23:34:29 +02:00
|
|
|
DefaultMutators.insert(
|
|
|
|
DefaultMutators.begin(),
|
|
|
|
{
|
2016-08-15 19:48:28 +02:00
|
|
|
{&MutationDispatcher::Mutate_EraseBytes, "EraseBytes"},
|
2016-06-03 23:34:29 +02:00
|
|
|
{&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
|
2016-08-15 19:48:28 +02:00
|
|
|
{&MutationDispatcher::Mutate_InsertRepeatedBytes,
|
|
|
|
"InsertRepeatedBytes"},
|
2016-06-03 23:34:29 +02:00
|
|
|
{&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
|
|
|
|
{&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
|
|
|
|
{&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
|
|
|
|
{&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
|
2016-08-17 23:30:30 +02:00
|
|
|
{&MutationDispatcher::Mutate_ChangeBinaryInteger, "ChangeBinInt"},
|
2016-08-17 20:10:42 +02:00
|
|
|
{&MutationDispatcher::Mutate_CopyPart, "CopyPart"},
|
2016-06-03 23:34:29 +02:00
|
|
|
{&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
|
|
|
|
{&MutationDispatcher::Mutate_AddWordFromManualDictionary,
|
|
|
|
"AddFromManualDict"},
|
|
|
|
{&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
|
|
|
|
"AddFromTempAutoDict"},
|
|
|
|
{&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
|
|
|
|
"AddFromPersAutoDict"},
|
|
|
|
});
|
|
|
|
|
2016-06-08 01:32:50 +02:00
|
|
|
if (EF->LLVMFuzzerCustomMutator)
|
2016-06-03 23:34:29 +02:00
|
|
|
Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
|
|
|
|
else
|
|
|
|
Mutators = DefaultMutators;
|
2016-06-07 22:22:15 +02:00
|
|
|
|
2016-06-08 01:32:50 +02:00
|
|
|
if (EF->LLVMFuzzerCustomCrossOver)
|
2016-06-07 22:22:15 +02:00
|
|
|
Mutators.push_back(
|
|
|
|
{&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
|
2016-06-03 23:34:29 +02:00
|
|
|
}
|
2016-02-13 04:46:26 +01:00
|
|
|
|
2016-02-13 04:00:53 +01:00
|
|
|
static char RandCh(Random &Rand) {
|
2015-07-24 03:06:40 +02:00
|
|
|
if (Rand.RandBool()) return Rand(256);
|
2016-08-17 20:10:42 +02:00
|
|
|
const char *Special = "!*'();:@&=+$,/?%#[]012Az-`~.\xff\x00";
|
2015-07-24 03:06:40 +02:00
|
|
|
return Special[Rand(sizeof(Special) - 1)];
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
|
2016-06-03 23:34:29 +02:00
|
|
|
size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-06-08 01:32:50 +02:00
|
|
|
return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
|
2016-06-03 23:34:29 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:22:15 +02: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;
|
|
|
|
MutateInPlaceHere.resize(MaxSize);
|
|
|
|
auto &U = MutateInPlaceHere;
|
2016-06-08 01:32:50 +02:00
|
|
|
size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
|
2016-06-07 22:22:15 +02: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 23:24:19 +02:00
|
|
|
size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 03:34:58 +02:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-08-06 03:29:13 +02:00
|
|
|
assert(Size);
|
2016-01-07 02:49:35 +01:00
|
|
|
size_t ShuffleAmount =
|
|
|
|
Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
|
2015-08-06 03:29:13 +02:00
|
|
|
size_t ShuffleStart = Rand(Size - ShuffleAmount);
|
|
|
|
assert(ShuffleStart + ShuffleAmount <= Size);
|
|
|
|
std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
|
|
|
|
Rand);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:48:28 +02:00
|
|
|
size_t MutationDispatcher::Mutate_EraseBytes(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2015-08-01 03:42:51 +02:00
|
|
|
assert(Size);
|
2015-09-04 02:40:29 +02:00
|
|
|
if (Size == 1) return 0;
|
2016-08-15 19:48:28 +02: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 03:42:51 +02:00
|
|
|
}
|
|
|
|
|
2015-09-03 23:24:19 +02:00
|
|
|
size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 03:34:58 +02:00
|
|
|
if (Size >= MaxSize) return 0;
|
2015-08-01 04:23:06 +02: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 19:48:28 +02: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 23:50:54 +02:00
|
|
|
// Give preference to 0x00 and 0xff.
|
|
|
|
uint8_t Byte = Rand.RandBool() ? Rand(256) : (Rand.RandBool() ? 0 : 255);
|
2016-08-15 19:48:28 +02:00
|
|
|
for (size_t i = 0; i < N; i++)
|
|
|
|
Data[Idx + i] = Byte;
|
|
|
|
return Size + N;
|
|
|
|
}
|
|
|
|
|
2015-09-03 23:24:19 +02:00
|
|
|
size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 03:34:58 +02:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-08-01 04:23:06 +02:00
|
|
|
size_t Idx = Rand(Size);
|
|
|
|
Data[Idx] = RandCh(Rand);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2015-09-03 23:24:19 +02:00
|
|
|
size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 03:34:58 +02:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-08-01 04:23:06 +02:00
|
|
|
size_t Idx = Rand(Size);
|
2016-08-17 20:10:42 +02:00
|
|
|
Data[Idx] ^= 1 << Rand(8);
|
2015-08-01 04:23:06 +02:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2016-01-07 02:49:35 +01:00
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
|
|
|
|
size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-02-13 04:37:24 +01:00
|
|
|
return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
|
2016-01-07 02:49:35 +01:00
|
|
|
}
|
|
|
|
|
2016-01-14 03:36:44 +01:00
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
|
|
|
|
uint8_t *Data, size_t Size, size_t MaxSize) {
|
2016-02-13 04:37:24 +01:00
|
|
|
return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
|
2016-01-14 03:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
|
|
|
|
uint8_t *Data, size_t Size, size_t MaxSize) {
|
2016-02-13 04:37:24 +01:00
|
|
|
return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
|
2016-01-07 02:49:35 +01:00
|
|
|
}
|
|
|
|
|
2016-02-13 04:37:24 +01:00
|
|
|
size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
|
|
|
|
size_t Size, size_t MaxSize) {
|
2016-09-22 03:34:58 +02:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-09-04 02:40:29 +02:00
|
|
|
if (D.empty()) return 0;
|
2016-01-23 00:55:14 +01:00
|
|
|
DictionaryEntry &DE = D[Rand(D.size())];
|
|
|
|
const Word &W = DE.GetW();
|
|
|
|
bool UsePositionHint = DE.HasPositionHint() &&
|
|
|
|
DE.GetPositionHint() + W.size() < Size && Rand.RandBool();
|
2016-01-16 04:53:32 +01:00
|
|
|
if (Rand.RandBool()) { // Insert W.
|
|
|
|
if (Size + W.size() > MaxSize) return 0;
|
2016-01-23 00:55:14 +01:00
|
|
|
size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
|
2016-01-16 04:53:32 +01: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-23 00:55:14 +01:00
|
|
|
size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
|
2016-01-16 04:53:32 +01:00
|
|
|
memcpy(Data + Idx, W.data(), W.size());
|
2016-01-06 03:13:04 +01:00
|
|
|
}
|
2016-01-23 00:55:14 +01:00
|
|
|
DE.IncUseCount();
|
|
|
|
CurrentDictionaryEntrySequence.push_back(&DE);
|
2016-01-12 03:36:59 +01:00
|
|
|
return Size;
|
2015-09-04 02:12:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 20:10:42 +02: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) {
|
2016-09-22 03:34:58 +02:00
|
|
|
if (Size > MaxSize) return 0;
|
2016-08-17 20:10:42 +02:00
|
|
|
if (Rand.RandBool())
|
|
|
|
return CopyPartOf(Data, Size, Data, Size);
|
|
|
|
else
|
|
|
|
return InsertPartOf(Data, Size, Data, Size, MaxSize);
|
|
|
|
}
|
|
|
|
|
2015-09-08 19:19:31 +02:00
|
|
|
size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 03:34:58 +02:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-09-08 19:19:31 +02: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 23:30:30 +02:00
|
|
|
uint8_t Bswap(uint8_t x) { return x; }
|
|
|
|
uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
|
|
|
|
uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
|
|
|
|
uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
|
|
|
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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 03:34:58 +02:00
|
|
|
if (Size > MaxSize) return 0;
|
2016-08-17 23:30:30 +02: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 22:57:09 +02:00
|
|
|
return 0;
|
2016-08-17 23:30:30 +02:00
|
|
|
}
|
|
|
|
|
2015-12-19 03:49:09 +01:00
|
|
|
size_t MutationDispatcher::Mutate_CrossOver(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-09-22 03:34:58 +02:00
|
|
|
if (Size > MaxSize) return 0;
|
2015-12-19 03:49:09 +01:00
|
|
|
if (!Corpus || Corpus->size() < 2 || Size == 0) return 0;
|
|
|
|
size_t Idx = Rand(Corpus->size());
|
2016-08-17 20:10:42 +02:00
|
|
|
const Unit &O = (*Corpus)[Idx];
|
|
|
|
if (O.empty()) return 0;
|
2016-02-13 18:56:51 +01:00
|
|
|
MutateInPlaceHere.resize(MaxSize);
|
|
|
|
auto &U = MutateInPlaceHere;
|
2016-08-19 22:57:09 +02:00
|
|
|
size_t NewSize = 0;
|
2016-08-17 20:10:42 +02: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);
|
|
|
|
if (NewSize)
|
|
|
|
break;
|
2016-08-17 23:30:30 +02:00
|
|
|
// LLVM_FALLTHROUGH;
|
2016-08-17 20:10:42 +02:00
|
|
|
case 2:
|
|
|
|
NewSize = CopyPartOf(O.data(), O.size(), U.data(), U.size());
|
|
|
|
break;
|
|
|
|
default: assert(0);
|
|
|
|
}
|
2015-12-19 03:49:09 +01: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 02:09:49 +01:00
|
|
|
void MutationDispatcher::StartMutationSequence() {
|
2016-02-13 04:37:24 +01:00
|
|
|
CurrentMutatorSequence.clear();
|
|
|
|
CurrentDictionaryEntrySequence.clear();
|
2015-12-19 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2016-01-14 03:36:44 +01:00
|
|
|
// Copy successful dictionary entries to PersistentAutoDictionary.
|
|
|
|
void MutationDispatcher::RecordSuccessfulMutationSequence() {
|
2016-02-13 04:37:24 +01:00
|
|
|
for (auto DE : CurrentDictionaryEntrySequence) {
|
|
|
|
// PersistentAutoDictionary.AddWithSuccessCountOne(DE);
|
2016-01-23 00:55:14 +01:00
|
|
|
DE->IncSuccessCount();
|
2016-01-14 03:36:44 +01:00
|
|
|
// Linear search is fine here as this happens seldom.
|
2016-02-13 04:37:24 +01:00
|
|
|
if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
|
|
|
|
PersistentAutoDictionary.push_back({DE->GetW(), 1});
|
2016-01-23 00:55:14 +01:00
|
|
|
}
|
2016-01-14 03:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MutationDispatcher::PrintRecommendedDictionary() {
|
2016-01-23 00:55:14 +01:00
|
|
|
std::vector<DictionaryEntry> V;
|
2016-02-13 04:37:24 +01:00
|
|
|
for (auto &DE : PersistentAutoDictionary)
|
|
|
|
if (!ManualDictionary.ContainsWord(DE.GetW()))
|
2016-01-23 00:55:14 +01:00
|
|
|
V.push_back(DE);
|
2016-01-14 03:36:44 +01:00
|
|
|
if (V.empty()) return;
|
|
|
|
Printf("###### Recommended dictionary. ######\n");
|
2016-01-23 00:55:14 +01:00
|
|
|
for (auto &DE: V) {
|
2016-01-14 03:36:44 +01:00
|
|
|
Printf("\"");
|
2016-01-23 00:55:14 +01:00
|
|
|
PrintASCII(DE.GetW(), "\"");
|
|
|
|
Printf(" # Uses: %zd\n", DE.GetUseCount());
|
2016-01-14 03:36:44 +01:00
|
|
|
}
|
|
|
|
Printf("###### End of recommended dictionary. ######\n");
|
|
|
|
}
|
|
|
|
|
2015-12-19 02:09:49 +01:00
|
|
|
void MutationDispatcher::PrintMutationSequence() {
|
2016-02-13 04:37:24 +01:00
|
|
|
Printf("MS: %zd ", CurrentMutatorSequence.size());
|
|
|
|
for (auto M : CurrentMutatorSequence)
|
2015-12-19 02:09:49 +01:00
|
|
|
Printf("%s-", M.Name);
|
2016-02-13 04:37:24 +01:00
|
|
|
if (!CurrentDictionaryEntrySequence.empty()) {
|
2016-01-12 03:36:59 +01:00
|
|
|
Printf(" DE: ");
|
2016-02-13 04:37:24 +01:00
|
|
|
for (auto DE : CurrentDictionaryEntrySequence) {
|
2016-01-12 03:36:59 +01:00
|
|
|
Printf("\"");
|
2016-01-23 00:55:14 +01:00
|
|
|
PrintASCII(DE->GetW(), "\"-");
|
2016-01-12 03:36:59 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-19 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2015-09-03 23:24:19 +02:00
|
|
|
size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
|
2016-06-03 23:34:29 +02: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-23 00:35:31 +02:00
|
|
|
assert(MaxSize > 0);
|
|
|
|
if (Size == 0) {
|
|
|
|
for (size_t i = 0; i < MaxSize; i++)
|
2015-07-24 03:06:40 +02:00
|
|
|
Data[i] = RandCh(Rand);
|
2016-06-23 22:44:48 +02:00
|
|
|
if (Options.OnlyASCII)
|
|
|
|
ToASCII(Data, MaxSize);
|
2015-05-23 00:35:31 +02:00
|
|
|
return MaxSize;
|
2015-02-04 20:10:20 +01:00
|
|
|
}
|
2015-05-23 00:35:31 +02:00
|
|
|
assert(Size > 0);
|
2015-09-04 02:40:29 +02: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 03:34:58 +02:00
|
|
|
for (int Iter = 0; Iter < 100; Iter++) {
|
2016-06-03 23:34:29 +02:00
|
|
|
auto M = Mutators[Rand(Mutators.size())];
|
2015-12-19 02:09:49 +01:00
|
|
|
size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
|
2016-09-22 03:34:58 +02:00
|
|
|
if (NewSize && NewSize <= MaxSize) {
|
2016-06-23 22:44:48 +02:00
|
|
|
if (Options.OnlyASCII)
|
|
|
|
ToASCII(Data, NewSize);
|
2016-02-13 04:37:24 +01:00
|
|
|
CurrentMutatorSequence.push_back(M);
|
2015-12-19 02:09:49 +01:00
|
|
|
return NewSize;
|
|
|
|
}
|
2015-09-04 02:40:29 +02:00
|
|
|
}
|
2016-09-22 03:34:58 +02:00
|
|
|
return std::min(Size, MaxSize);
|
2015-01-29 17:58:29 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 04:53:32 +01:00
|
|
|
void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
|
2016-02-13 04:37:24 +01:00
|
|
|
ManualDictionary.push_back(
|
2016-01-16 04:53:32 +01:00
|
|
|
{W, std::numeric_limits<size_t>::max()});
|
2016-01-07 02:49:35 +01:00
|
|
|
}
|
|
|
|
|
2016-07-16 01:27:19 +02:00
|
|
|
void MutationDispatcher::AddWordToAutoDictionary(DictionaryEntry DE) {
|
2016-01-09 04:08:58 +01:00
|
|
|
static const size_t kMaxAutoDictSize = 1 << 14;
|
2016-02-13 04:37:24 +01:00
|
|
|
if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
|
2016-07-16 01:27:19 +02:00
|
|
|
TempAutoDictionary.push_back(DE);
|
2015-09-04 02:12:11 +02:00
|
|
|
}
|
|
|
|
|
2016-01-09 04:08:58 +01:00
|
|
|
void MutationDispatcher::ClearAutoDictionary() {
|
2016-02-13 04:37:24 +01:00
|
|
|
TempAutoDictionary.clear();
|
2016-01-09 04:08:58 +01:00
|
|
|
}
|
|
|
|
|
2015-01-29 17:58:29 +01:00
|
|
|
} // namespace fuzzer
|