Home | History | Annotate | Download | only in test
      1 // This file is distributed under the University of Illinois Open Source
      2 // License. See LICENSE.TXT for details.
      3 
      4 // Simple test for a cutom mutator.
      5 #include <assert.h>
      6 #include <cstddef>
      7 #include <cstdint>
      8 #include <cstdlib>
      9 #include <iostream>
     10 #include <random>
     11 #include <string.h>
     12 
     13 #include "FuzzerInterface.h"
     14 
     15 static const char *Separator = "-_^_-";
     16 static const char *Target = "012-_^_-abc";
     17 
     18 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
     19   assert(Data);
     20   std::string Str(reinterpret_cast<const char *>(Data), Size);
     21 
     22   if (Str.find(Target) != std::string::npos) {
     23     std::cout << "BINGO; Found the target, exiting\n";
     24     exit(1);
     25   }
     26   return 0;
     27 }
     28 
     29 extern "C" size_t LLVMFuzzerCustomCrossOver(const uint8_t *Data1, size_t Size1,
     30                                             const uint8_t *Data2, size_t Size2,
     31                                             uint8_t *Out, size_t MaxOutSize,
     32                                             unsigned int Seed) {
     33   static bool Printed;
     34   static size_t SeparatorLen = strlen(Separator);
     35 
     36   if (!Printed) {
     37     std::cerr << "In LLVMFuzzerCustomCrossover\n";
     38     Printed = true;
     39   }
     40 
     41   std::mt19937 R(Seed);
     42 
     43   size_t Offset1 = 0;
     44   size_t Len1 = R() % (Size1 - Offset1);
     45   size_t Offset2 = 0;
     46   size_t Len2 = R() % (Size2 - Offset2);
     47   size_t Size = Len1 + Len2 + SeparatorLen;
     48 
     49   if (Size > MaxOutSize)
     50     return 0;
     51 
     52   memcpy(Out, Data1 + Offset1, Len1);
     53   memcpy(Out + Len1, Separator, SeparatorLen);
     54   memcpy(Out + Len1 + SeparatorLen, Data2 + Offset2, Len2);
     55 
     56   return Len1 + Len2 + SeparatorLen;
     57 }
     58