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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-22 22:35:31 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
#include "FuzzerInternal.h"
|
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
static char FlipRandomBit(char X) {
|
|
|
|
int Bit = rand() % 8;
|
|
|
|
char Mask = 1 << Bit;
|
|
|
|
char R;
|
|
|
|
if (X & (1 << Bit))
|
|
|
|
R = X & ~Mask;
|
|
|
|
else
|
|
|
|
R = X | Mask;
|
|
|
|
assert(R != X);
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char RandCh() {
|
|
|
|
if (rand() % 2) return rand();
|
|
|
|
const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
|
|
|
|
return Special[rand() % (sizeof(Special) - 1)];
|
|
|
|
}
|
|
|
|
|
2015-05-22 22:35:31 +00:00
|
|
|
// Mutates Data in place, returns new size.
|
|
|
|
size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
|
|
|
|
assert(MaxSize > 0);
|
|
|
|
assert(Size <= MaxSize);
|
|
|
|
if (Size == 0) {
|
|
|
|
for (size_t i = 0; i < MaxSize; i++)
|
|
|
|
Data[i] = RandCh();
|
|
|
|
return MaxSize;
|
2015-02-04 19:10:20 +00:00
|
|
|
}
|
2015-05-22 22:35:31 +00:00
|
|
|
assert(Size > 0);
|
|
|
|
size_t Idx = rand() % Size;
|
2015-01-29 16:58:29 +00:00
|
|
|
switch (rand() % 3) {
|
|
|
|
case 0:
|
2015-05-22 22:35:31 +00:00
|
|
|
if (Size > 1) {
|
|
|
|
// Erase Data[Idx].
|
|
|
|
memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
|
|
|
|
Size = Size - 1;
|
2015-02-04 19:10:20 +00:00
|
|
|
}
|
2015-02-19 18:21:12 +00:00
|
|
|
[[clang::fallthrough]];
|
2015-01-29 16:58:29 +00:00
|
|
|
case 1:
|
2015-05-22 22:35:31 +00:00
|
|
|
if (Size < MaxSize) {
|
|
|
|
// Insert new value at Data[Idx].
|
|
|
|
memmove(Data + Idx + 1, Data + Idx, Size - Idx);
|
|
|
|
Data[Idx] = RandCh();
|
2015-01-29 16:58:29 +00:00
|
|
|
}
|
2015-05-22 22:35:31 +00:00
|
|
|
Data[Idx] = RandCh();
|
2015-01-29 16:58:29 +00:00
|
|
|
break;
|
2015-05-22 22:35:31 +00:00
|
|
|
case 2:
|
|
|
|
Data[Idx] = FlipRandomBit(Data[Idx]);
|
2015-01-29 16:58:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-05-22 22:35:31 +00:00
|
|
|
assert(Size > 0);
|
|
|
|
return Size;
|
2015-01-29 16:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzzer
|