Debian Codegraph @

tools/fuzzing/libfuzzer/patches/11-callback-rv.patch

raw
1 # HG changeset patch
2 # User Christian Holler <[email protected]>
3 # Date 1596126448 -7200
4 # Thu Jul 30 18:27:28 2020 +0200
5 # Node ID ea198a0331a6db043cb5978512226977514104db
6 # Parent 8a2a26b33d516c43c366b2f24d731d27d9843349
7 [libFuzzer] Change libFuzzer callback contract to allow positive return values
8
9 diff --git a/FuzzerInternal.h b/FuzzerInternal.h
10 --- a/FuzzerInternal.h
11 +++ b/FuzzerInternal.h
12 @@ -60,17 +60,17 @@ public:
13
14 static void StaticAlarmCallback();
15 static void StaticCrashSignalCallback();
16 static void StaticExitCallback();
17 static void StaticInterruptCallback();
18 static void StaticFileSizeExceedCallback();
19 static void StaticGracefulExitCallback();
20
21 - void ExecuteCallback(const uint8_t *Data, size_t Size);
22 + int ExecuteCallback(const uint8_t *Data, size_t Size);
23 bool RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile = false,
24 InputInfo *II = nullptr, bool *FoundUniqFeatures = nullptr);
25
26 // Merge Corpora[1:] into Corpora[0].
27 void Merge(const Vector<std::string> &Corpora);
28 void CrashResistantMergeInternalStep(const std::string &ControlFilePath);
29 MutationDispatcher &GetMD() { return MD; }
30 void PrintFinalStats();
31 diff --git a/FuzzerLoop.cpp b/FuzzerLoop.cpp
32 --- a/FuzzerLoop.cpp
33 +++ b/FuzzerLoop.cpp
34 @@ -463,17 +463,19 @@ static void RenameFeatureSetFile(const s
35 DirPlusFile(FeaturesDir, NewFile));
36 }
37
38 bool Fuzzer::RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile,
39 InputInfo *II, bool *FoundUniqFeatures) {
40 if (!Size)
41 return false;
42
43 - ExecuteCallback(Data, Size);
44 + if (ExecuteCallback(Data, Size)) {
45 + return false;
46 + }
47
48 UniqFeatureSetTmp.clear();
49 size_t FoundUniqFeaturesOfII = 0;
50 size_t NumUpdatesBefore = Corpus.NumFeatureUpdates();
51 TPC.CollectFeatures([&](size_t Feature) {
52 if (Corpus.AddFeature(Feature, Size, Options.Shrink))
53 UniqFeatureSetTmp.push_back(Feature);
54 if (Options.Entropic)
55 @@ -530,48 +532,49 @@ static bool LooseMemeq(const uint8_t *A,
56 const size_t Limit = 64;
57 if (Size <= 64)
58 return !memcmp(A, B, Size);
59 // Compare first and last Limit/2 bytes.
60 return !memcmp(A, B, Limit / 2) &&
61 !memcmp(A + Size - Limit / 2, B + Size - Limit / 2, Limit / 2);
62 }
63
64 -void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {
65 +int Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) {
66 TPC.RecordInitialStack();
67 TotalNumberOfRuns++;
68 assert(InFuzzingThread());
69 // We copy the contents of Unit into a separate heap buffer
70 // so that we reliably find buffer overflows in it.
71 uint8_t *DataCopy = new uint8_t[Size];
72 memcpy(DataCopy, Data, Size);
73 if (EF->__msan_unpoison)
74 EF->__msan_unpoison(DataCopy, Size);
75 if (EF->__msan_unpoison_param)
76 EF->__msan_unpoison_param(2);
77 if (CurrentUnitData && CurrentUnitData != Data)
78 memcpy(CurrentUnitData, Data, Size);
79 CurrentUnitSize = Size;
80 + int Res = 0;
81 {
82 ScopedEnableMsanInterceptorChecks S;
83 AllocTracer.Start(Options.TraceMalloc);
84 UnitStartTime = system_clock::now();
85 TPC.ResetMaps();
86 RunningUserCallback = true;
87 - int Res = CB(DataCopy, Size);
88 + Res = CB(DataCopy, Size);
89 RunningUserCallback = false;
90 UnitStopTime = system_clock::now();
91 - (void)Res;
92 - assert(Res == 0);
93 HasMoreMallocsThanFrees = AllocTracer.Stop();
94 }
95 if (!LooseMemeq(DataCopy, Data, Size))
96 CrashOnOverwrittenData();
97 CurrentUnitSize = 0;
98 delete[] DataCopy;
99 + return Res;
100 }
101
102 std::string Fuzzer::WriteToOutputCorpus(const Unit &U) {
103 if (Options.OnlyASCII)
104 assert(IsASCII(U));
105 if (Options.OutputCorpus.empty())
106 return "";
107 std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U));
108 diff --git a/FuzzerMerge.cpp b/FuzzerMerge.cpp
109 --- a/FuzzerMerge.cpp
110 +++ b/FuzzerMerge.cpp
111 @@ -223,17 +223,19 @@ void Fuzzer::CrashResistantMergeInternal
112 U.shrink_to_fit();
113 }
114
115 // Write the pre-run marker.
116 OF << "STARTED " << i << " " << U.size() << "\n";
117 OF.flush(); // Flush is important since Command::Execute may crash.
118 // Run.
119 TPC.ResetMaps();
120 - ExecuteCallback(U.data(), U.size());
121 + if (ExecuteCallback(U.data(), U.size())) {
122 + continue;
123 + }
124 // Collect coverage. We are iterating over the files in this order:
125 // * First, files in the initial corpus ordered by size, smallest first.
126 // * Then, all other files, smallest first.
127 // So it makes no sense to record all features for all files, instead we
128 // only record features that were not seen before.
129 Set<size_t> UniqFeatures;
130 TPC.CollectFeatures([&](size_t Feature) {
131 if (AllFeatures.insert(Feature).second)