xref: /llvm-project/compiler-rt/lib/fuzzer/FuzzerMutate.cpp (revision c4a41cd77c15c2905ac74beeec09f8343a65a549)
1 //===- FuzzerMutate.cpp - Mutation utilities -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // Mutate utilities.
9 //===----------------------------------------------------------------------===//
10 
11 #include "FuzzerMutate.h"
12 #include "FuzzerExtFunctions.h"
13 #include "FuzzerIO.h"
14 #include "FuzzerTracePC.h"
15 #include "FuzzerUtil.h"
16 
17 namespace fuzzer {
18 namespace {
19 
20 void FromTORC4(size_t Idx, uint32_t *A, uint32_t *B) {
21   const auto &X = TPC.TORC4.Get(Idx);
22   *A = X.A;
23   *B = X.B;
24 }
25 
26 void FromTORC8(size_t Idx, uint64_t *A, uint64_t *B) {
27   const auto &X = TPC.TORC8.Get(Idx);
28   *A = X.A;
29   *B = X.B;
30 }
31 
32 void FromTORCW(size_t Idx, const uint8_t **DataA, size_t *SizeA,
33                const uint8_t **DataB, size_t *SizeB) {
34   const auto &X = TPC.TORCW.Get(Idx);
35   *DataA = X.A.data();
36   *SizeA = X.A.size();
37   *DataB = X.B.data();
38   *SizeB = X.B.size();
39 }
40 
41 void FromMMT(size_t Idx, const uint8_t **Data, size_t *Size) {
42   auto W = TPC.MMT.Get(Idx);
43   *Data = W.data();
44   *Size = W.size();
45 }
46 
47 void PrintASCII(const Word &W, const char *PrintAfter) {
48   fuzzer::PrintASCII(W.data(), W.size(), PrintAfter);
49 }
50 
51 } // namespace
52 
53 void ConfigureMutagen(unsigned int Seed, const FuzzingOptions &Options,
54                       LLVMMutagenConfiguration *OutConfig) {
55   OutConfig->Seed = Seed;
56   OutConfig->UseCmp = Options.UseCmp;
57   OutConfig->FromTORC4 = FromTORC4;
58   OutConfig->FromTORC8 = FromTORC8;
59   OutConfig->FromTORCW = FromTORCW;
60   OutConfig->UseMemmem = Options.UseMemmem;
61   OutConfig->FromMMT = FromMMT;
62   OutConfig->CustomMutator = EF->LLVMFuzzerCustomMutator;
63   OutConfig->CustomCrossOver = EF->LLVMFuzzerCustomCrossOver;
64   OutConfig->MSanUnpoison = EF->__msan_unpoison;
65   OutConfig->MSanUnpoisonParam = EF->__msan_unpoison_param;
66 }
67 
68 void PrintRecommendedDictionary(MutationDispatcher &MD) {
69   auto RecommendedDictionary = MD.RecommendDictionary();
70   if (RecommendedDictionary.empty())
71     return;
72   Printf("###### Recommended dictionary. ######\n");
73   for (auto &DE : RecommendedDictionary) {
74     assert(DE.GetW().size());
75     Printf("\"");
76     PrintASCII(DE.GetW(), "\"");
77     Printf(" # Uses: %zd\n", DE.GetUseCount());
78   }
79   Printf("###### End of recommended dictionary. ######\n");
80 }
81 
82 void PrintMutationSequence(MutationDispatcher &MD, bool Verbose) {
83   const auto &MS = MD.MutationSequence();
84   const auto &DS = MD.DictionaryEntrySequence();
85   Printf("MS: %zd %s", MS.size(), MS.GetString(Verbose).c_str());
86   if (!DS.empty())
87     Printf(" DE: %s", DS.GetString(Verbose).c_str());
88 }
89 
90 }  // namespace fuzzer
91