2015-01-30 00:58:29 +08: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 06:35:31 +08:00
|
|
|
#include <cstring>
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
#include "FuzzerInternal.h"
|
|
|
|
|
2015-08-06 09:29:13 +08:00
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
namespace fuzzer {
|
|
|
|
|
2016-01-21 09:52:14 +08:00
|
|
|
const size_t Dictionary::kMaxDictSize;
|
|
|
|
|
2016-06-04 05:34:29 +08:00
|
|
|
MutationDispatcher::MutationDispatcher(Random &Rand) : Rand(Rand) {
|
|
|
|
DefaultMutators.insert(
|
|
|
|
DefaultMutators.begin(),
|
|
|
|
{
|
|
|
|
{&MutationDispatcher::Mutate_EraseByte, "EraseByte"},
|
|
|
|
{&MutationDispatcher::Mutate_InsertByte, "InsertByte"},
|
|
|
|
{&MutationDispatcher::Mutate_ChangeByte, "ChangeByte"},
|
|
|
|
{&MutationDispatcher::Mutate_ChangeBit, "ChangeBit"},
|
|
|
|
{&MutationDispatcher::Mutate_ShuffleBytes, "ShuffleBytes"},
|
|
|
|
{&MutationDispatcher::Mutate_ChangeASCIIInteger, "ChangeASCIIInt"},
|
|
|
|
{&MutationDispatcher::Mutate_CrossOver, "CrossOver"},
|
|
|
|
{&MutationDispatcher::Mutate_AddWordFromManualDictionary,
|
|
|
|
"AddFromManualDict"},
|
|
|
|
{&MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary,
|
|
|
|
"AddFromTempAutoDict"},
|
|
|
|
{&MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary,
|
|
|
|
"AddFromPersAutoDict"},
|
|
|
|
});
|
|
|
|
|
2016-06-08 07:32:50 +08:00
|
|
|
if (EF->LLVMFuzzerCustomMutator)
|
2016-06-04 05:34:29 +08:00
|
|
|
Mutators.push_back({&MutationDispatcher::Mutate_Custom, "Custom"});
|
|
|
|
else
|
|
|
|
Mutators = DefaultMutators;
|
2016-06-08 04:22:15 +08:00
|
|
|
|
2016-06-08 07:32:50 +08:00
|
|
|
if (EF->LLVMFuzzerCustomCrossOver)
|
2016-06-08 04:22:15 +08:00
|
|
|
Mutators.push_back(
|
|
|
|
{&MutationDispatcher::Mutate_CustomCrossOver, "CustomCrossOver"});
|
2016-06-04 05:34:29 +08:00
|
|
|
}
|
2016-02-13 11:46:26 +08:00
|
|
|
|
2016-02-13 11:00:53 +08:00
|
|
|
static char FlipRandomBit(char X, Random &Rand) {
|
2015-07-24 09:06:40 +08:00
|
|
|
int Bit = Rand(8);
|
2015-01-30 00:58:29 +08:00
|
|
|
char Mask = 1 << Bit;
|
|
|
|
char R;
|
|
|
|
if (X & (1 << Bit))
|
|
|
|
R = X & ~Mask;
|
|
|
|
else
|
|
|
|
R = X | Mask;
|
|
|
|
assert(R != X);
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2016-02-13 11:00:53 +08:00
|
|
|
static char RandCh(Random &Rand) {
|
2015-07-24 09:06:40 +08:00
|
|
|
if (Rand.RandBool()) return Rand(256);
|
2015-01-30 00:58:29 +08:00
|
|
|
const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
|
2015-07-24 09:06:40 +08:00
|
|
|
return Special[Rand(sizeof(Special) - 1)];
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2016-06-04 05:34:29 +08:00
|
|
|
size_t MutationDispatcher::Mutate_Custom(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-06-08 07:32:50 +08:00
|
|
|
return EF->LLVMFuzzerCustomMutator(Data, Size, MaxSize, Rand.Rand());
|
2016-06-04 05:34:29 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 04:22:15 +08: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 07:32:50 +08:00
|
|
|
size_t NewSize = EF->LLVMFuzzerCustomCrossOver(
|
2016-06-08 04:22:15 +08: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-04 05:24:19 +08:00
|
|
|
size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2015-08-06 09:29:13 +08:00
|
|
|
assert(Size);
|
2016-01-07 09:49:35 +08:00
|
|
|
size_t ShuffleAmount =
|
|
|
|
Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
|
2015-08-06 09:29:13 +08:00
|
|
|
size_t ShuffleStart = Rand(Size - ShuffleAmount);
|
|
|
|
assert(ShuffleStart + ShuffleAmount <= Size);
|
|
|
|
std::random_shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount,
|
|
|
|
Rand);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2015-09-04 05:24:19 +08:00
|
|
|
size_t MutationDispatcher::Mutate_EraseByte(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2015-08-01 09:42:51 +08:00
|
|
|
assert(Size);
|
2015-09-04 08:40:29 +08:00
|
|
|
if (Size == 1) return 0;
|
2015-08-01 09:42:51 +08:00
|
|
|
size_t Idx = Rand(Size);
|
|
|
|
// Erase Data[Idx].
|
|
|
|
memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
|
|
|
|
return Size - 1;
|
|
|
|
}
|
|
|
|
|
2015-09-04 05:24:19 +08:00
|
|
|
size_t MutationDispatcher::Mutate_InsertByte(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2015-09-04 08:40:29 +08:00
|
|
|
if (Size == MaxSize) return 0;
|
2015-08-01 10:23:06 +08: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;
|
|
|
|
}
|
|
|
|
|
2015-09-04 05:24:19 +08:00
|
|
|
size_t MutationDispatcher::Mutate_ChangeByte(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2015-08-01 10:23:06 +08:00
|
|
|
size_t Idx = Rand(Size);
|
|
|
|
Data[Idx] = RandCh(Rand);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2015-09-04 05:24:19 +08:00
|
|
|
size_t MutationDispatcher::Mutate_ChangeBit(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
2015-08-01 10:23:06 +08:00
|
|
|
size_t Idx = Rand(Size);
|
|
|
|
Data[Idx] = FlipRandomBit(Data[Idx], Rand);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2016-01-07 09:49:35 +08:00
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromManualDictionary(uint8_t *Data,
|
|
|
|
size_t Size,
|
|
|
|
size_t MaxSize) {
|
2016-02-13 11:37:24 +08:00
|
|
|
return AddWordFromDictionary(ManualDictionary, Data, Size, MaxSize);
|
2016-01-07 09:49:35 +08:00
|
|
|
}
|
|
|
|
|
2016-01-14 10:36:44 +08:00
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary(
|
|
|
|
uint8_t *Data, size_t Size, size_t MaxSize) {
|
2016-02-13 11:37:24 +08:00
|
|
|
return AddWordFromDictionary(TempAutoDictionary, Data, Size, MaxSize);
|
2016-01-14 10:36:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t MutationDispatcher::Mutate_AddWordFromPersistentAutoDictionary(
|
|
|
|
uint8_t *Data, size_t Size, size_t MaxSize) {
|
2016-02-13 11:37:24 +08:00
|
|
|
return AddWordFromDictionary(PersistentAutoDictionary, Data, Size, MaxSize);
|
2016-01-07 09:49:35 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 11:37:24 +08:00
|
|
|
size_t MutationDispatcher::AddWordFromDictionary(Dictionary &D, uint8_t *Data,
|
|
|
|
size_t Size, size_t MaxSize) {
|
2015-09-04 08:40:29 +08:00
|
|
|
if (D.empty()) return 0;
|
2016-01-23 07:55:14 +08: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 11:53:32 +08:00
|
|
|
if (Rand.RandBool()) { // Insert W.
|
|
|
|
if (Size + W.size() > MaxSize) return 0;
|
2016-01-23 07:55:14 +08:00
|
|
|
size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size + 1);
|
2016-01-16 11:53:32 +08: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 07:55:14 +08:00
|
|
|
size_t Idx = UsePositionHint ? DE.GetPositionHint() : Rand(Size - W.size());
|
2016-01-16 11:53:32 +08:00
|
|
|
memcpy(Data + Idx, W.data(), W.size());
|
2016-01-06 10:13:04 +08:00
|
|
|
}
|
2016-01-23 07:55:14 +08:00
|
|
|
DE.IncUseCount();
|
|
|
|
CurrentDictionaryEntrySequence.push_back(&DE);
|
2016-01-12 10:36:59 +08:00
|
|
|
return Size;
|
2015-09-04 08:12:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-09 01:19:31 +08:00
|
|
|
size_t MutationDispatcher::Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-19 10:49:09 +08:00
|
|
|
size_t MutationDispatcher::Mutate_CrossOver(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;
|
2016-02-14 01:56:51 +08:00
|
|
|
MutateInPlaceHere.resize(MaxSize);
|
|
|
|
auto &U = MutateInPlaceHere;
|
2015-12-19 10:49:09 +08:00
|
|
|
size_t NewSize =
|
|
|
|
CrossOver(Data, Size, Other.data(), Other.size(), U.data(), U.size());
|
|
|
|
assert(NewSize > 0 && "CrossOver returned empty unit");
|
|
|
|
assert(NewSize <= MaxSize && "CrossOver returned overisized unit");
|
|
|
|
memcpy(Data, U.data(), NewSize);
|
|
|
|
return NewSize;
|
|
|
|
}
|
|
|
|
|
2015-12-19 09:09:49 +08:00
|
|
|
void MutationDispatcher::StartMutationSequence() {
|
2016-02-13 11:37:24 +08:00
|
|
|
CurrentMutatorSequence.clear();
|
|
|
|
CurrentDictionaryEntrySequence.clear();
|
2015-12-19 09:09:49 +08:00
|
|
|
}
|
|
|
|
|
2016-01-14 10:36:44 +08:00
|
|
|
// Copy successful dictionary entries to PersistentAutoDictionary.
|
|
|
|
void MutationDispatcher::RecordSuccessfulMutationSequence() {
|
2016-02-13 11:37:24 +08:00
|
|
|
for (auto DE : CurrentDictionaryEntrySequence) {
|
|
|
|
// PersistentAutoDictionary.AddWithSuccessCountOne(DE);
|
2016-01-23 07:55:14 +08:00
|
|
|
DE->IncSuccessCount();
|
2016-01-14 10:36:44 +08:00
|
|
|
// Linear search is fine here as this happens seldom.
|
2016-02-13 11:37:24 +08:00
|
|
|
if (!PersistentAutoDictionary.ContainsWord(DE->GetW()))
|
|
|
|
PersistentAutoDictionary.push_back({DE->GetW(), 1});
|
2016-01-23 07:55:14 +08:00
|
|
|
}
|
2016-01-14 10:36:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MutationDispatcher::PrintRecommendedDictionary() {
|
2016-01-23 07:55:14 +08:00
|
|
|
std::vector<DictionaryEntry> V;
|
2016-02-13 11:37:24 +08:00
|
|
|
for (auto &DE : PersistentAutoDictionary)
|
|
|
|
if (!ManualDictionary.ContainsWord(DE.GetW()))
|
2016-01-23 07:55:14 +08:00
|
|
|
V.push_back(DE);
|
2016-01-14 10:36:44 +08:00
|
|
|
if (V.empty()) return;
|
|
|
|
Printf("###### Recommended dictionary. ######\n");
|
2016-01-23 07:55:14 +08:00
|
|
|
for (auto &DE: V) {
|
2016-01-14 10:36:44 +08:00
|
|
|
Printf("\"");
|
2016-01-23 07:55:14 +08:00
|
|
|
PrintASCII(DE.GetW(), "\"");
|
|
|
|
Printf(" # Uses: %zd\n", DE.GetUseCount());
|
2016-01-14 10:36:44 +08:00
|
|
|
}
|
|
|
|
Printf("###### End of recommended dictionary. ######\n");
|
|
|
|
}
|
|
|
|
|
2015-12-19 09:09:49 +08:00
|
|
|
void MutationDispatcher::PrintMutationSequence() {
|
2016-02-13 11:37:24 +08:00
|
|
|
Printf("MS: %zd ", CurrentMutatorSequence.size());
|
|
|
|
for (auto M : CurrentMutatorSequence)
|
2015-12-19 09:09:49 +08:00
|
|
|
Printf("%s-", M.Name);
|
2016-02-13 11:37:24 +08:00
|
|
|
if (!CurrentDictionaryEntrySequence.empty()) {
|
2016-01-12 10:36:59 +08:00
|
|
|
Printf(" DE: ");
|
2016-02-13 11:37:24 +08:00
|
|
|
for (auto DE : CurrentDictionaryEntrySequence) {
|
2016-01-12 10:36:59 +08:00
|
|
|
Printf("\"");
|
2016-01-23 07:55:14 +08:00
|
|
|
PrintASCII(DE->GetW(), "\"-");
|
2016-01-12 10:36:59 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-19 09:09:49 +08:00
|
|
|
}
|
|
|
|
|
2015-09-04 05:24:19 +08:00
|
|
|
size_t MutationDispatcher::Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
|
2016-06-04 05:34:29 +08: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 06:35:31 +08:00
|
|
|
assert(MaxSize > 0);
|
|
|
|
assert(Size <= MaxSize);
|
|
|
|
if (Size == 0) {
|
|
|
|
for (size_t i = 0; i < MaxSize; i++)
|
2015-07-24 09:06:40 +08:00
|
|
|
Data[i] = RandCh(Rand);
|
2015-05-23 06:35:31 +08:00
|
|
|
return MaxSize;
|
2015-02-05 03:10:20 +08:00
|
|
|
}
|
2015-05-23 06:35:31 +08:00
|
|
|
assert(Size > 0);
|
2015-09-04 08:40:29 +08: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.
|
|
|
|
for (int Iter = 0; Iter < 10; Iter++) {
|
2016-06-04 05:34:29 +08:00
|
|
|
auto M = Mutators[Rand(Mutators.size())];
|
2015-12-19 09:09:49 +08:00
|
|
|
size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize);
|
|
|
|
if (NewSize) {
|
2016-02-13 11:37:24 +08:00
|
|
|
CurrentMutatorSequence.push_back(M);
|
2015-12-19 09:09:49 +08:00
|
|
|
return NewSize;
|
|
|
|
}
|
2015-09-04 08:40:29 +08:00
|
|
|
}
|
2015-05-23 06:35:31 +08:00
|
|
|
return Size;
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
2016-01-16 11:53:32 +08:00
|
|
|
void MutationDispatcher::AddWordToManualDictionary(const Word &W) {
|
2016-02-13 11:37:24 +08:00
|
|
|
ManualDictionary.push_back(
|
2016-01-16 11:53:32 +08:00
|
|
|
{W, std::numeric_limits<size_t>::max()});
|
2016-01-07 09:49:35 +08:00
|
|
|
}
|
|
|
|
|
2016-01-16 11:53:32 +08:00
|
|
|
void MutationDispatcher::AddWordToAutoDictionary(const Word &W,
|
2016-01-07 09:49:35 +08:00
|
|
|
size_t PositionHint) {
|
2016-01-09 11:08:58 +08:00
|
|
|
static const size_t kMaxAutoDictSize = 1 << 14;
|
2016-02-13 11:37:24 +08:00
|
|
|
if (TempAutoDictionary.size() >= kMaxAutoDictSize) return;
|
|
|
|
TempAutoDictionary.push_back({W, PositionHint});
|
2015-09-04 08:12:11 +08:00
|
|
|
}
|
|
|
|
|
2016-01-09 11:08:58 +08:00
|
|
|
void MutationDispatcher::ClearAutoDictionary() {
|
2016-02-13 11:37:24 +08:00
|
|
|
TempAutoDictionary.clear();
|
2016-01-09 11:08:58 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
} // namespace fuzzer
|