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)];
35 void Mutate(Unit *U, size_t MaxLen) {
37 assert(U->size() <= MaxLen);
39 for (size_t i = 0; i < MaxLen; i++)
40 U->push_back(RandCh());
47 U->erase(U->begin() + rand() % U->size());
50 [[clang::fallthrough]];
52 if (U->size() < MaxLen) {
53 U->insert(U->begin() + rand() % U->size(), RandCh());
54 } else { // At MaxLen.
55 uint8_t Ch = RandCh();
56 size_t Idx = rand() % U->size();
62 size_t Idx = rand() % U->size();
63 (*U)[Idx] = FlipRandomBit((*U)[Idx]);