1 //===- FuzzerMutate.cpp - Mutate a test input -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Mutate a test input.
10 //===----------------------------------------------------------------------===//
14 #include "FuzzerInternal.h"
18 static char FlipRandomBit(char X, FuzzerRandomBase &Rand) {
30 static char RandCh(FuzzerRandomBase &Rand) {
31 if (Rand.RandBool()) return Rand(256);
32 const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
33 return Special[Rand(sizeof(Special) - 1)];
36 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize,
37 FuzzerRandomBase &Rand) {
39 if (Size == 1) return Size;
40 size_t Idx = Rand(Size);
42 memmove(Data + Idx, Data + Idx + 1, Size - Idx - 1);
46 // Mutates Data in place, returns new size.
47 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
48 FuzzerRandomBase &Rand) {
50 assert(Size <= MaxSize);
52 for (size_t i = 0; i < MaxSize; i++)
53 Data[i] = RandCh(Rand);
57 size_t Idx = Rand(Size);
59 case 0: Size = Mutate_EraseByte(Data, Size, MaxSize, Rand); break;
62 // Insert new value at Data[Idx].
63 memmove(Data + Idx + 1, Data + Idx, Size - Idx);
64 Data[Idx] = RandCh(Rand);
66 Data[Idx] = RandCh(Rand);
69 Data[Idx] = FlipRandomBit(Data[Idx], Rand);