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 //===----------------------------------------------------------------------===//
12 #include "FuzzerInternal.h"
16 static char FlipRandomBit(char X) {
28 static char RandCh() {
29 if (rand() % 2) return rand();
30 const char *Special = "!*'();:@&=+$,/?%#[]123ABCxyz-`~.";
31 return Special[rand() % (sizeof(Special) - 1)];
34 void Mutate(Unit *U, size_t MaxLen) {
36 assert(U->size() <= MaxLen);
40 U->erase(U->begin() + rand() % U->size());
44 U->push_back(RandCh());
45 } else if (U->size() < MaxLen) {
46 U->insert(U->begin() + rand() % U->size(), RandCh());
47 } else { // At MaxLen.
48 uint8_t Ch = RandCh();
49 size_t Idx = rand() % U->size();
55 size_t idx = rand() % U->size();
56 (*U)[idx] = FlipRandomBit((*U)[idx]);