xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerCrossOver.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- FuzzerCrossOver.cpp - Cross over two test inputs -------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric // Cross over test inputs.
9*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
10*0b57cec5SDimitry Andric 
11*0b57cec5SDimitry Andric #include "FuzzerDefs.h"
12*0b57cec5SDimitry Andric #include "FuzzerMutate.h"
13*0b57cec5SDimitry Andric #include "FuzzerRandom.h"
14*0b57cec5SDimitry Andric #include <cstring>
15*0b57cec5SDimitry Andric 
16*0b57cec5SDimitry Andric namespace fuzzer {
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric // Cross Data1 and Data2, store the result (up to MaxOutSize bytes) in Out.
CrossOver(const uint8_t * Data1,size_t Size1,const uint8_t * Data2,size_t Size2,uint8_t * Out,size_t MaxOutSize)19*0b57cec5SDimitry Andric size_t MutationDispatcher::CrossOver(const uint8_t *Data1, size_t Size1,
20*0b57cec5SDimitry Andric                                      const uint8_t *Data2, size_t Size2,
21*0b57cec5SDimitry Andric                                      uint8_t *Out, size_t MaxOutSize) {
22*0b57cec5SDimitry Andric   assert(Size1 || Size2);
23*0b57cec5SDimitry Andric   MaxOutSize = Rand(MaxOutSize) + 1;
24*0b57cec5SDimitry Andric   size_t OutPos = 0;
25*0b57cec5SDimitry Andric   size_t Pos1 = 0;
26*0b57cec5SDimitry Andric   size_t Pos2 = 0;
27*0b57cec5SDimitry Andric   size_t *InPos = &Pos1;
28*0b57cec5SDimitry Andric   size_t InSize = Size1;
29*0b57cec5SDimitry Andric   const uint8_t *Data = Data1;
30*0b57cec5SDimitry Andric   bool CurrentlyUsingFirstData = true;
31*0b57cec5SDimitry Andric   while (OutPos < MaxOutSize && (Pos1 < Size1 || Pos2 < Size2)) {
32*0b57cec5SDimitry Andric     // Merge a part of Data into Out.
33*0b57cec5SDimitry Andric     size_t OutSizeLeft = MaxOutSize - OutPos;
34*0b57cec5SDimitry Andric     if (*InPos < InSize) {
35*0b57cec5SDimitry Andric       size_t InSizeLeft = InSize - *InPos;
36*0b57cec5SDimitry Andric       size_t MaxExtraSize = std::min(OutSizeLeft, InSizeLeft);
37*0b57cec5SDimitry Andric       size_t ExtraSize = Rand(MaxExtraSize) + 1;
38*0b57cec5SDimitry Andric       memcpy(Out + OutPos, Data + *InPos, ExtraSize);
39*0b57cec5SDimitry Andric       OutPos += ExtraSize;
40*0b57cec5SDimitry Andric       (*InPos) += ExtraSize;
41*0b57cec5SDimitry Andric     }
42*0b57cec5SDimitry Andric     // Use the other input data on the next iteration.
43*0b57cec5SDimitry Andric     InPos  = CurrentlyUsingFirstData ? &Pos2 : &Pos1;
44*0b57cec5SDimitry Andric     InSize = CurrentlyUsingFirstData ? Size2 : Size1;
45*0b57cec5SDimitry Andric     Data   = CurrentlyUsingFirstData ? Data2 : Data1;
46*0b57cec5SDimitry Andric     CurrentlyUsingFirstData = !CurrentlyUsingFirstData;
47*0b57cec5SDimitry Andric   }
48*0b57cec5SDimitry Andric   return OutPos;
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
51*0b57cec5SDimitry Andric }  // namespace fuzzer
52