George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 1 | //===- FuzzerLoop.cpp - Fuzzer's main loop --------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 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 |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // Fuzzer's main loop. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "FuzzerCorpus.h" |
| 12 | #include "FuzzerIO.h" |
| 13 | #include "FuzzerInternal.h" |
| 14 | #include "FuzzerMutate.h" |
Dokyung Song | 750369e | 2020-07-14 22:25:51 | [diff] [blame] | 15 | #include "FuzzerPlatform.h" |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 16 | #include "FuzzerRandom.h" |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 17 | #include "FuzzerTracePC.h" |
| 18 | #include <algorithm> |
| 19 | #include <cstring> |
| 20 | #include <memory> |
Vitaly Buka | 7dbc1d8 | 2017-11-01 03:02:59 | [diff] [blame] | 21 | #include <mutex> |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 22 | #include <set> |
| 23 | |
| 24 | #if defined(__has_include) |
| 25 | #if __has_include(<sanitizer / lsan_interface.h>) |
| 26 | #include <sanitizer/lsan_interface.h> |
| 27 | #endif |
| 28 | #endif |
| 29 | |
| 30 | #define NO_SANITIZE_MEMORY |
| 31 | #if defined(__has_feature) |
| 32 | #if __has_feature(memory_sanitizer) |
| 33 | #undef NO_SANITIZE_MEMORY |
| 34 | #define NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory)) |
| 35 | #endif |
| 36 | #endif |
| 37 | |
| 38 | namespace fuzzer { |
| 39 | static const size_t kMaxUnitSizeToPrint = 256; |
| 40 | |
| 41 | thread_local bool Fuzzer::IsMyThread; |
| 42 | |
Matt Morehouse | 43a2296 | 2018-07-17 16:12:00 | [diff] [blame] | 43 | bool RunningUserCallback = false; |
| 44 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 45 | // Only one Fuzzer per process. |
| 46 | static Fuzzer *F; |
| 47 | |
| 48 | // Leak detection is expensive, so we first check if there were more mallocs |
| 49 | // than frees (using the sanitizer malloc hooks) and only then try to call lsan. |
| 50 | struct MallocFreeTracer { |
| 51 | void Start(int TraceLevel) { |
| 52 | this->TraceLevel = TraceLevel; |
| 53 | if (TraceLevel) |
| 54 | Printf("MallocFreeTracer: START\n"); |
| 55 | Mallocs = 0; |
| 56 | Frees = 0; |
| 57 | } |
| 58 | // Returns true if there were more mallocs than frees. |
| 59 | bool Stop() { |
| 60 | if (TraceLevel) |
| 61 | Printf("MallocFreeTracer: STOP %zd %zd (%s)\n", Mallocs.load(), |
| 62 | Frees.load(), Mallocs == Frees ? "same" : "DIFFERENT"); |
| 63 | bool Result = Mallocs > Frees; |
| 64 | Mallocs = 0; |
| 65 | Frees = 0; |
| 66 | TraceLevel = 0; |
| 67 | return Result; |
| 68 | } |
| 69 | std::atomic<size_t> Mallocs; |
| 70 | std::atomic<size_t> Frees; |
| 71 | int TraceLevel = 0; |
Vitaly Buka | 7d22324 | 2017-11-02 04:12:10 | [diff] [blame] | 72 | |
| 73 | std::recursive_mutex TraceMutex; |
| 74 | bool TraceDisabled = false; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static MallocFreeTracer AllocTracer; |
| 78 | |
Vitaly Buka | 7d22324 | 2017-11-02 04:12:10 | [diff] [blame] | 79 | // Locks printing and avoids nested hooks triggered from mallocs/frees in |
| 80 | // sanitizer. |
| 81 | class TraceLock { |
| 82 | public: |
| 83 | TraceLock() : Lock(AllocTracer.TraceMutex) { |
| 84 | AllocTracer.TraceDisabled = !AllocTracer.TraceDisabled; |
| 85 | } |
| 86 | ~TraceLock() { AllocTracer.TraceDisabled = !AllocTracer.TraceDisabled; } |
| 87 | |
| 88 | bool IsDisabled() const { |
| 89 | // This is already inverted value. |
| 90 | return !AllocTracer.TraceDisabled; |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | std::lock_guard<std::recursive_mutex> Lock; |
| 95 | }; |
Vitaly Buka | 7dbc1d8 | 2017-11-01 03:02:59 | [diff] [blame] | 96 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 97 | ATTRIBUTE_NO_SANITIZE_MEMORY |
| 98 | void MallocHook(const volatile void *ptr, size_t size) { |
| 99 | size_t N = AllocTracer.Mallocs++; |
| 100 | F->HandleMalloc(size); |
| 101 | if (int TraceLevel = AllocTracer.TraceLevel) { |
Vitaly Buka | 7d22324 | 2017-11-02 04:12:10 | [diff] [blame] | 102 | TraceLock Lock; |
| 103 | if (Lock.IsDisabled()) |
| 104 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 105 | Printf("MALLOC[%zd] %p %zd\n", N, ptr, size); |
| 106 | if (TraceLevel >= 2 && EF) |
Matt Morehouse | 14cf71a | 2018-05-08 23:45:05 | [diff] [blame] | 107 | PrintStackTrace(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | ATTRIBUTE_NO_SANITIZE_MEMORY |
| 112 | void FreeHook(const volatile void *ptr) { |
| 113 | size_t N = AllocTracer.Frees++; |
| 114 | if (int TraceLevel = AllocTracer.TraceLevel) { |
Vitaly Buka | 7d22324 | 2017-11-02 04:12:10 | [diff] [blame] | 115 | TraceLock Lock; |
| 116 | if (Lock.IsDisabled()) |
| 117 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 118 | Printf("FREE[%zd] %p\n", N, ptr); |
| 119 | if (TraceLevel >= 2 && EF) |
Matt Morehouse | 14cf71a | 2018-05-08 23:45:05 | [diff] [blame] | 120 | PrintStackTrace(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | // Crash on a single malloc that exceeds the rss limit. |
| 125 | void Fuzzer::HandleMalloc(size_t Size) { |
Kostya Serebryany | de9bafb | 2017-12-01 22:12:04 | [diff] [blame] | 126 | if (!Options.MallocLimitMb || (Size >> 20) < (size_t)Options.MallocLimitMb) |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 127 | return; |
| 128 | Printf("==%d== ERROR: libFuzzer: out-of-memory (malloc(%zd))\n", GetPid(), |
| 129 | Size); |
| 130 | Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n"); |
Matt Morehouse | 14cf71a | 2018-05-08 23:45:05 | [diff] [blame] | 131 | PrintStackTrace(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 132 | DumpCurrentUnit("oom-"); |
| 133 | Printf("SUMMARY: libFuzzer: out-of-memory\n"); |
| 134 | PrintFinalStats(); |
Kostya Serebryany | 0fda9dc | 2019-02-09 00:16:21 | [diff] [blame] | 135 | _Exit(Options.OOMExitCode); // Stop right now. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | Fuzzer::Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD, |
| 139 | FuzzingOptions Options) |
| 140 | : CB(CB), Corpus(Corpus), MD(MD), Options(Options) { |
| 141 | if (EF->__sanitizer_set_death_callback) |
| 142 | EF->__sanitizer_set_death_callback(StaticDeathCallback); |
| 143 | assert(!F); |
| 144 | F = this; |
| 145 | TPC.ResetMaps(); |
| 146 | IsMyThread = true; |
| 147 | if (Options.DetectLeaks && EF->__sanitizer_install_malloc_and_free_hooks) |
| 148 | EF->__sanitizer_install_malloc_and_free_hooks(MallocHook, FreeHook); |
| 149 | TPC.SetUseCounters(Options.UseCounters); |
Kostya Serebryany | 51ddb88 | 2018-07-03 22:33:09 | [diff] [blame] | 150 | TPC.SetUseValueProfileMask(Options.UseValueProfile); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 151 | |
| 152 | if (Options.Verbosity) |
| 153 | TPC.PrintModuleInfo(); |
| 154 | if (!Options.OutputCorpus.empty() && Options.ReloadIntervalSec) |
| 155 | EpochOfLastReadOfOutputCorpus = GetEpoch(Options.OutputCorpus); |
| 156 | MaxInputLen = MaxMutationLen = Options.MaxLen; |
Kostya Serebryany | b6ca1e7 | 2019-02-16 01:23:41 | [diff] [blame] | 157 | TmpMaxMutationLen = 0; // Will be set once we load the corpus. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 158 | AllocateCurrentUnitData(); |
| 159 | CurrentUnitSize = 0; |
| 160 | memset(BaseSha1, 0, sizeof(BaseSha1)); |
| 161 | } |
| 162 | |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 163 | Fuzzer::~Fuzzer() {} |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 164 | |
| 165 | void Fuzzer::AllocateCurrentUnitData() { |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 166 | if (CurrentUnitData || MaxInputLen == 0) |
| 167 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 168 | CurrentUnitData = new uint8_t[MaxInputLen]; |
| 169 | } |
| 170 | |
| 171 | void Fuzzer::StaticDeathCallback() { |
| 172 | assert(F); |
| 173 | F->DeathCallback(); |
| 174 | } |
| 175 | |
| 176 | void Fuzzer::DumpCurrentUnit(const char *Prefix) { |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 177 | if (!CurrentUnitData) |
| 178 | return; // Happens when running individual inputs. |
Matt Morehouse | a34c65e | 2018-07-09 23:51:08 | [diff] [blame] | 179 | ScopedDisableMsanInterceptorChecks S; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 180 | MD.PrintMutationSequence(); |
| 181 | Printf("; base unit: %s\n", Sha1ToString(BaseSha1).c_str()); |
| 182 | size_t UnitSize = CurrentUnitSize; |
| 183 | if (UnitSize <= kMaxUnitSizeToPrint) { |
| 184 | PrintHexArray(CurrentUnitData, UnitSize, "\n"); |
| 185 | PrintASCII(CurrentUnitData, UnitSize, "\n"); |
| 186 | } |
| 187 | WriteUnitToFileWithPrefix({CurrentUnitData, CurrentUnitData + UnitSize}, |
| 188 | Prefix); |
| 189 | } |
| 190 | |
| 191 | NO_SANITIZE_MEMORY |
| 192 | void Fuzzer::DeathCallback() { |
| 193 | DumpCurrentUnit("crash-"); |
| 194 | PrintFinalStats(); |
| 195 | } |
| 196 | |
| 197 | void Fuzzer::StaticAlarmCallback() { |
| 198 | assert(F); |
| 199 | F->AlarmCallback(); |
| 200 | } |
| 201 | |
| 202 | void Fuzzer::StaticCrashSignalCallback() { |
| 203 | assert(F); |
| 204 | F->CrashCallback(); |
| 205 | } |
| 206 | |
| 207 | void Fuzzer::StaticExitCallback() { |
| 208 | assert(F); |
| 209 | F->ExitCallback(); |
| 210 | } |
| 211 | |
| 212 | void Fuzzer::StaticInterruptCallback() { |
| 213 | assert(F); |
| 214 | F->InterruptCallback(); |
| 215 | } |
| 216 | |
Kostya Serebryany | a2ca2dc | 2017-11-09 20:30:19 | [diff] [blame] | 217 | void Fuzzer::StaticGracefulExitCallback() { |
| 218 | assert(F); |
| 219 | F->GracefulExitRequested = true; |
| 220 | Printf("INFO: signal received, trying to exit gracefully\n"); |
| 221 | } |
| 222 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 223 | void Fuzzer::StaticFileSizeExceedCallback() { |
| 224 | Printf("==%lu== ERROR: libFuzzer: file size exceeded\n", GetPid()); |
| 225 | exit(1); |
| 226 | } |
| 227 | |
| 228 | void Fuzzer::CrashCallback() { |
Julian Lettner | 15df273 | 2019-01-31 01:24:01 | [diff] [blame] | 229 | if (EF->__sanitizer_acquire_crash_state && |
| 230 | !EF->__sanitizer_acquire_crash_state()) |
| 231 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 232 | Printf("==%lu== ERROR: libFuzzer: deadly signal\n", GetPid()); |
Matt Morehouse | 14cf71a | 2018-05-08 23:45:05 | [diff] [blame] | 233 | PrintStackTrace(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 234 | Printf("NOTE: libFuzzer has rudimentary signal handlers.\n" |
| 235 | " Combine libFuzzer with AddressSanitizer or similar for better " |
| 236 | "crash reports.\n"); |
| 237 | Printf("SUMMARY: libFuzzer: deadly signal\n"); |
| 238 | DumpCurrentUnit("crash-"); |
| 239 | PrintFinalStats(); |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 240 | _Exit(Options.ErrorExitCode); // Stop right now. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void Fuzzer::ExitCallback() { |
Matt Morehouse | 43a2296 | 2018-07-17 16:12:00 | [diff] [blame] | 244 | if (!RunningUserCallback) |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 245 | return; // This exit did not come from the user callback |
Matt Morehouse | 52fd169 | 2018-05-01 21:01:53 | [diff] [blame] | 246 | if (EF->__sanitizer_acquire_crash_state && |
| 247 | !EF->__sanitizer_acquire_crash_state()) |
| 248 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 249 | Printf("==%lu== ERROR: libFuzzer: fuzz target exited\n", GetPid()); |
Matt Morehouse | 14cf71a | 2018-05-08 23:45:05 | [diff] [blame] | 250 | PrintStackTrace(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 251 | Printf("SUMMARY: libFuzzer: fuzz target exited\n"); |
| 252 | DumpCurrentUnit("crash-"); |
| 253 | PrintFinalStats(); |
| 254 | _Exit(Options.ErrorExitCode); |
| 255 | } |
| 256 | |
Kostya Serebryany | a2ca2dc | 2017-11-09 20:30:19 | [diff] [blame] | 257 | void Fuzzer::MaybeExitGracefully() { |
Kostya Serebryany | f762a11 | 2019-02-08 21:27:23 | [diff] [blame] | 258 | if (!F->GracefulExitRequested) return; |
Kostya Serebryany | a2ca2dc | 2017-11-09 20:30:19 | [diff] [blame] | 259 | Printf("==%lu== INFO: libFuzzer: exiting as requested\n", GetPid()); |
Yuanfang Chen | 4f3c3bb | 2020-02-11 02:22:09 | [diff] [blame] | 260 | RmDirRecursive(TempPath("FuzzWithFork", ".dir")); |
Kostya Serebryany | f762a11 | 2019-02-08 21:27:23 | [diff] [blame] | 261 | F->PrintFinalStats(); |
Kostya Serebryany | a2ca2dc | 2017-11-09 20:30:19 | [diff] [blame] | 262 | _Exit(0); |
| 263 | } |
| 264 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 265 | void Fuzzer::InterruptCallback() { |
| 266 | Printf("==%lu== libFuzzer: run interrupted; exiting\n", GetPid()); |
| 267 | PrintFinalStats(); |
Matt Morehouse | 1b76063 | 2019-04-26 00:17:41 | [diff] [blame] | 268 | ScopedDisableMsanInterceptorChecks S; // RmDirRecursive may call opendir(). |
Yuanfang Chen | 4f3c3bb | 2020-02-11 02:22:09 | [diff] [blame] | 269 | RmDirRecursive(TempPath("FuzzWithFork", ".dir")); |
Kostya Serebryany | 0fda9dc | 2019-02-09 00:16:21 | [diff] [blame] | 270 | // Stop right now, don't perform any at-exit actions. |
| 271 | _Exit(Options.InterruptExitCode); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | NO_SANITIZE_MEMORY |
| 275 | void Fuzzer::AlarmCallback() { |
| 276 | assert(Options.UnitTimeoutSec > 0); |
Jake Ehrlich | e7bfce7 | 2019-10-09 21:01:50 | [diff] [blame] | 277 | // In Windows and Fuchsia, Alarm callback is executed by a different thread. |
Kamil Rytarowski | 2e61186 | 2018-11-06 01:28:01 | [diff] [blame] | 278 | // NetBSD's current behavior needs this change too. |
Jake Ehrlich | e7bfce7 | 2019-10-09 21:01:50 | [diff] [blame] | 279 | #if !LIBFUZZER_WINDOWS && !LIBFUZZER_NETBSD && !LIBFUZZER_FUCHSIA |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 280 | if (!InFuzzingThread()) |
| 281 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 282 | #endif |
Matt Morehouse | 43a2296 | 2018-07-17 16:12:00 | [diff] [blame] | 283 | if (!RunningUserCallback) |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 284 | return; // We have not started running units yet. |
| 285 | size_t Seconds = |
| 286 | duration_cast<seconds>(system_clock::now() - UnitStartTime).count(); |
| 287 | if (Seconds == 0) |
| 288 | return; |
| 289 | if (Options.Verbosity >= 2) |
| 290 | Printf("AlarmCallback %zd\n", Seconds); |
| 291 | if (Seconds >= (size_t)Options.UnitTimeoutSec) { |
Matt Morehouse | 52fd169 | 2018-05-01 21:01:53 | [diff] [blame] | 292 | if (EF->__sanitizer_acquire_crash_state && |
| 293 | !EF->__sanitizer_acquire_crash_state()) |
| 294 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 295 | Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds); |
| 296 | Printf(" and the timeout value is %d (use -timeout=N to change)\n", |
| 297 | Options.UnitTimeoutSec); |
| 298 | DumpCurrentUnit("timeout-"); |
| 299 | Printf("==%lu== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(), |
| 300 | Seconds); |
Matt Morehouse | 14cf71a | 2018-05-08 23:45:05 | [diff] [blame] | 301 | PrintStackTrace(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 302 | Printf("SUMMARY: libFuzzer: timeout\n"); |
| 303 | PrintFinalStats(); |
| 304 | _Exit(Options.TimeoutExitCode); // Stop right now. |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void Fuzzer::RssLimitCallback() { |
Matt Morehouse | 52fd169 | 2018-05-01 21:01:53 | [diff] [blame] | 309 | if (EF->__sanitizer_acquire_crash_state && |
| 310 | !EF->__sanitizer_acquire_crash_state()) |
| 311 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 312 | Printf( |
| 313 | "==%lu== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n", |
| 314 | GetPid(), GetPeakRSSMb(), Options.RssLimitMb); |
| 315 | Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n"); |
Matt Morehouse | 14cf71a | 2018-05-08 23:45:05 | [diff] [blame] | 316 | PrintMemoryProfile(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 317 | DumpCurrentUnit("oom-"); |
| 318 | Printf("SUMMARY: libFuzzer: out-of-memory\n"); |
| 319 | PrintFinalStats(); |
Kostya Serebryany | 0fda9dc | 2019-02-09 00:16:21 | [diff] [blame] | 320 | _Exit(Options.OOMExitCode); // Stop right now. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 321 | } |
| 322 | |
Max Moroz | 74cec61 | 2019-08-12 20:21:27 | [diff] [blame] | 323 | void Fuzzer::PrintStats(const char *Where, const char *End, size_t Units, |
| 324 | size_t Features) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 325 | size_t ExecPerSec = execPerSec(); |
| 326 | if (!Options.Verbosity) |
| 327 | return; |
| 328 | Printf("#%zd\t%s", TotalNumberOfRuns, Where); |
| 329 | if (size_t N = TPC.GetTotalPCCoverage()) |
| 330 | Printf(" cov: %zd", N); |
Max Moroz | 74cec61 | 2019-08-12 20:21:27 | [diff] [blame] | 331 | if (size_t N = Features ? Features : Corpus.NumFeatures()) |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 332 | Printf(" ft: %zd", N); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 333 | if (!Corpus.empty()) { |
| 334 | Printf(" corp: %zd", Corpus.NumActiveUnits()); |
| 335 | if (size_t N = Corpus.SizeInBytes()) { |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 336 | if (N < (1 << 14)) |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 337 | Printf("/%zdb", N); |
| 338 | else if (N < (1 << 24)) |
| 339 | Printf("/%zdKb", N >> 10); |
| 340 | else |
| 341 | Printf("/%zdMb", N >> 20); |
| 342 | } |
Kostya Serebryany | e9c6f06 | 2018-05-16 23:26:37 | [diff] [blame] | 343 | if (size_t FF = Corpus.NumInputsThatTouchFocusFunction()) |
| 344 | Printf(" focus: %zd", FF); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 345 | } |
Matt Morehouse | ddf352b | 2018-02-22 19:00:17 | [diff] [blame] | 346 | if (TmpMaxMutationLen) |
| 347 | Printf(" lim: %zd", TmpMaxMutationLen); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 348 | if (Units) |
| 349 | Printf(" units: %zd", Units); |
| 350 | |
| 351 | Printf(" exec/s: %zd", ExecPerSec); |
| 352 | Printf(" rss: %zdMb", GetPeakRSSMb()); |
| 353 | Printf("%s", End); |
| 354 | } |
| 355 | |
| 356 | void Fuzzer::PrintFinalStats() { |
Max Moroz | dc62d5e | 2020-10-23 18:07:30 | [diff] [blame] | 357 | if (Options.PrintFullCoverage) |
| 358 | TPC.PrintCoverage(/*PrintAllCounters=*/true); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 359 | if (Options.PrintCoverage) |
Max Moroz | dc62d5e | 2020-10-23 18:07:30 | [diff] [blame] | 360 | TPC.PrintCoverage(/*PrintAllCounters=*/false); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 361 | if (Options.PrintCorpusStats) |
| 362 | Corpus.PrintStats(); |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 363 | if (!Options.PrintFinalStats) |
| 364 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 365 | size_t ExecPerSec = execPerSec(); |
| 366 | Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns); |
| 367 | Printf("stat::average_exec_per_sec: %zd\n", ExecPerSec); |
| 368 | Printf("stat::new_units_added: %zd\n", NumberOfNewUnitsAdded); |
| 369 | Printf("stat::slowest_unit_time_sec: %zd\n", TimeOfLongestUnitInSeconds); |
| 370 | Printf("stat::peak_rss_mb: %zd\n", GetPeakRSSMb()); |
| 371 | } |
| 372 | |
| 373 | void Fuzzer::SetMaxInputLen(size_t MaxInputLen) { |
| 374 | assert(this->MaxInputLen == 0); // Can only reset MaxInputLen from 0 to non-0. |
| 375 | assert(MaxInputLen); |
| 376 | this->MaxInputLen = MaxInputLen; |
| 377 | this->MaxMutationLen = MaxInputLen; |
| 378 | AllocateCurrentUnitData(); |
| 379 | Printf("INFO: -max_len is not provided; " |
| 380 | "libFuzzer will not generate inputs larger than %zd bytes\n", |
| 381 | MaxInputLen); |
| 382 | } |
| 383 | |
| 384 | void Fuzzer::SetMaxMutationLen(size_t MaxMutationLen) { |
| 385 | assert(MaxMutationLen && MaxMutationLen <= MaxInputLen); |
| 386 | this->MaxMutationLen = MaxMutationLen; |
| 387 | } |
| 388 | |
| 389 | void Fuzzer::CheckExitOnSrcPosOrItem() { |
| 390 | if (!Options.ExitOnSrcPos.empty()) { |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 391 | static auto *PCsSet = new Set<uintptr_t>; |
Kostya Serebryany | 96f81bc | 2019-02-14 23:12:33 | [diff] [blame] | 392 | auto HandlePC = [&](const TracePC::PCTableEntry *TE) { |
| 393 | if (!PCsSet->insert(TE->PC).second) |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 394 | return; |
Kostya Serebryany | 96f81bc | 2019-02-14 23:12:33 | [diff] [blame] | 395 | std::string Descr = DescribePC("%F %L", TE->PC + 1); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 396 | if (Descr.find(Options.ExitOnSrcPos) != std::string::npos) { |
| 397 | Printf("INFO: found line matching '%s', exiting.\n", |
| 398 | Options.ExitOnSrcPos.c_str()); |
| 399 | _Exit(0); |
| 400 | } |
| 401 | }; |
| 402 | TPC.ForEachObservedPC(HandlePC); |
| 403 | } |
| 404 | if (!Options.ExitOnItem.empty()) { |
| 405 | if (Corpus.HasUnit(Options.ExitOnItem)) { |
| 406 | Printf("INFO: found item with checksum '%s', exiting.\n", |
| 407 | Options.ExitOnItem.c_str()); |
| 408 | _Exit(0); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | void Fuzzer::RereadOutputCorpus(size_t MaxSize) { |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 414 | if (Options.OutputCorpus.empty() || !Options.ReloadIntervalSec) |
| 415 | return; |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 416 | Vector<Unit> AdditionalCorpus; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 417 | ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus, |
| 418 | &EpochOfLastReadOfOutputCorpus, MaxSize, |
| 419 | /*ExitOnError*/ false); |
| 420 | if (Options.Verbosity >= 2) |
| 421 | Printf("Reload: read %zd new units.\n", AdditionalCorpus.size()); |
| 422 | bool Reloaded = false; |
| 423 | for (auto &U : AdditionalCorpus) { |
| 424 | if (U.size() > MaxSize) |
| 425 | U.resize(MaxSize); |
| 426 | if (!Corpus.HasUnit(U)) { |
| 427 | if (RunOne(U.data(), U.size())) { |
| 428 | CheckExitOnSrcPosOrItem(); |
| 429 | Reloaded = true; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | if (Reloaded) |
| 434 | PrintStats("RELOAD"); |
| 435 | } |
| 436 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 437 | void Fuzzer::PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size) { |
| 438 | auto TimeOfUnit = |
| 439 | duration_cast<seconds>(UnitStopTime - UnitStartTime).count(); |
| 440 | if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && |
| 441 | secondsSinceProcessStartUp() >= 2) |
| 442 | PrintStats("pulse "); |
Aaron Green | 6708186 | 2021-03-12 00:00:53 | [diff] [blame] | 443 | auto Threshhold = |
| 444 | static_cast<long>(static_cast<double>(TimeOfLongestUnitInSeconds) * 1.1); |
| 445 | if (TimeOfUnit > Threshhold && TimeOfUnit >= Options.ReportSlowUnits) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 446 | TimeOfLongestUnitInSeconds = TimeOfUnit; |
| 447 | Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds); |
| 448 | WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-"); |
| 449 | } |
| 450 | } |
| 451 | |
Kostya Serebryany | 4614cc3 | 2019-04-13 00:20:31 | [diff] [blame] | 452 | static void WriteFeatureSetToFile(const std::string &FeaturesDir, |
Kostya Serebryany | 5e67abd | 2019-04-13 01:57:33 | [diff] [blame] | 453 | const std::string &FileName, |
Kostya Serebryany | 4614cc3 | 2019-04-13 00:20:31 | [diff] [blame] | 454 | const Vector<uint32_t> &FeatureSet) { |
| 455 | if (FeaturesDir.empty() || FeatureSet.empty()) return; |
| 456 | WriteToFile(reinterpret_cast<const uint8_t *>(FeatureSet.data()), |
| 457 | FeatureSet.size() * sizeof(FeatureSet[0]), |
Kostya Serebryany | 5e67abd | 2019-04-13 01:57:33 | [diff] [blame] | 458 | DirPlusFile(FeaturesDir, FileName)); |
Kostya Serebryany | 4614cc3 | 2019-04-13 00:20:31 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static void RenameFeatureSetFile(const std::string &FeaturesDir, |
| 462 | const std::string &OldFile, |
| 463 | const std::string &NewFile) { |
| 464 | if (FeaturesDir.empty()) return; |
| 465 | RenameFile(DirPlusFile(FeaturesDir, OldFile), |
| 466 | DirPlusFile(FeaturesDir, NewFile)); |
| 467 | } |
| 468 | |
Dokyung Song | 1bb1eac | 2020-07-08 19:30:53 | [diff] [blame] | 469 | static void WriteEdgeToMutationGraphFile(const std::string &MutationGraphFile, |
| 470 | const InputInfo *II, |
| 471 | const InputInfo *BaseII, |
| 472 | const std::string &MS) { |
| 473 | if (MutationGraphFile.empty()) |
| 474 | return; |
| 475 | |
| 476 | std::string Sha1 = Sha1ToString(II->Sha1); |
| 477 | |
| 478 | std::string OutputString; |
| 479 | |
| 480 | // Add a new vertex. |
| 481 | OutputString.append("\""); |
| 482 | OutputString.append(Sha1); |
| 483 | OutputString.append("\"\n"); |
| 484 | |
| 485 | // Add a new edge if there is base input. |
| 486 | if (BaseII) { |
| 487 | std::string BaseSha1 = Sha1ToString(BaseII->Sha1); |
| 488 | OutputString.append("\""); |
| 489 | OutputString.append(BaseSha1); |
| 490 | OutputString.append("\" -> \""); |
| 491 | OutputString.append(Sha1); |
| 492 | OutputString.append("\" [label=\""); |
| 493 | OutputString.append(MS); |
| 494 | OutputString.append("\"];\n"); |
| 495 | } |
| 496 | |
| 497 | AppendToFile(OutputString, MutationGraphFile); |
| 498 | } |
| 499 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 500 | bool Fuzzer::RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile, |
Dokyung Song | 62673c4 | 2020-07-31 00:07:20 | [diff] [blame] | 501 | InputInfo *II, bool ForceAddToCorpus, |
| 502 | bool *FoundUniqFeatures) { |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 503 | if (!Size) |
| 504 | return false; |
Aaron Green | 6708186 | 2021-03-12 00:00:53 | [diff] [blame] | 505 | // Largest input length should be INT_MAX. |
| 506 | assert(Size < std::numeric_limits<uint32_t>::max()); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 507 | |
| 508 | ExecuteCallback(Data, Size); |
Dokyung Song | 5cda4dc | 2020-08-17 16:59:59 | [diff] [blame] | 509 | auto TimeOfUnit = duration_cast<microseconds>(UnitStopTime - UnitStartTime); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 510 | |
| 511 | UniqFeatureSetTmp.clear(); |
| 512 | size_t FoundUniqFeaturesOfII = 0; |
| 513 | size_t NumUpdatesBefore = Corpus.NumFeatureUpdates(); |
Aaron Green | 6708186 | 2021-03-12 00:00:53 | [diff] [blame] | 514 | TPC.CollectFeatures([&](uint32_t Feature) { |
| 515 | if (Corpus.AddFeature(Feature, static_cast<uint32_t>(Size), Options.Shrink)) |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 516 | UniqFeatureSetTmp.push_back(Feature); |
Matt Morehouse | e2e38fc | 2020-05-19 17:28:18 | [diff] [blame] | 517 | if (Options.Entropic) |
| 518 | Corpus.UpdateFeatureFrequency(II, Feature); |
Dokyung Song | 62673c4 | 2020-07-31 00:07:20 | [diff] [blame] | 519 | if (Options.ReduceInputs && II && !II->NeverReduce) |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 520 | if (std::binary_search(II->UniqFeatureSet.begin(), |
| 521 | II->UniqFeatureSet.end(), Feature)) |
| 522 | FoundUniqFeaturesOfII++; |
| 523 | }); |
Kostya Serebryany | ad05ee0 | 2017-12-01 19:18:38 | [diff] [blame] | 524 | if (FoundUniqFeatures) |
| 525 | *FoundUniqFeatures = FoundUniqFeaturesOfII; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 526 | PrintPulseAndReportSlowInput(Data, Size); |
| 527 | size_t NumNewFeatures = Corpus.NumFeatureUpdates() - NumUpdatesBefore; |
Dokyung Song | 62673c4 | 2020-07-31 00:07:20 | [diff] [blame] | 528 | if (NumNewFeatures || ForceAddToCorpus) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 529 | TPC.UpdateObservedPCs(); |
Dokyung Song | 62673c4 | 2020-07-31 00:07:20 | [diff] [blame] | 530 | auto NewII = |
| 531 | Corpus.AddToCorpus({Data, Data + Size}, NumNewFeatures, MayDeleteFile, |
| 532 | TPC.ObservedFocusFunction(), ForceAddToCorpus, |
Dokyung Song | 5cda4dc | 2020-08-17 16:59:59 | [diff] [blame] | 533 | TimeOfUnit, UniqFeatureSetTmp, DFT, II); |
Kostya Serebryany | 5e67abd | 2019-04-13 01:57:33 | [diff] [blame] | 534 | WriteFeatureSetToFile(Options.FeaturesDir, Sha1ToString(NewII->Sha1), |
Kostya Serebryany | 4614cc3 | 2019-04-13 00:20:31 | [diff] [blame] | 535 | NewII->UniqFeatureSet); |
Dokyung Song | 1bb1eac | 2020-07-08 19:30:53 | [diff] [blame] | 536 | WriteEdgeToMutationGraphFile(Options.MutationGraphFile, NewII, II, |
| 537 | MD.MutationSequence()); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 538 | return true; |
| 539 | } |
| 540 | if (II && FoundUniqFeaturesOfII && |
Kostya Serebryany | 67af992 | 2018-06-07 01:40:20 | [diff] [blame] | 541 | II->DataFlowTraceForFocusFunction.empty() && |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 542 | FoundUniqFeaturesOfII == II->UniqFeatureSet.size() && |
| 543 | II->U.size() > Size) { |
Kostya Serebryany | 4614cc3 | 2019-04-13 00:20:31 | [diff] [blame] | 544 | auto OldFeaturesFile = Sha1ToString(II->Sha1); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 545 | Corpus.Replace(II, {Data, Data + Size}); |
Kostya Serebryany | 4614cc3 | 2019-04-13 00:20:31 | [diff] [blame] | 546 | RenameFeatureSetFile(Options.FeaturesDir, OldFeaturesFile, |
| 547 | Sha1ToString(II->Sha1)); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 548 | return true; |
| 549 | } |
| 550 | return false; |
| 551 | } |
| 552 | |
Max Moroz | dc62d5e | 2020-10-23 18:07:30 | [diff] [blame] | 553 | void Fuzzer::TPCUpdateObservedPCs() { TPC.UpdateObservedPCs(); } |
| 554 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 555 | size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const { |
| 556 | assert(InFuzzingThread()); |
| 557 | *Data = CurrentUnitData; |
| 558 | return CurrentUnitSize; |
| 559 | } |
| 560 | |
| 561 | void Fuzzer::CrashOnOverwrittenData() { |
Mitch Phillips | da3cf61 | 2019-09-26 00:54:30 | [diff] [blame] | 562 | Printf("==%d== ERROR: libFuzzer: fuzz target overwrites its const input\n", |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 563 | GetPid()); |
Mitch Phillips | d1e222e | 2019-09-27 22:04:36 | [diff] [blame] | 564 | PrintStackTrace(); |
| 565 | Printf("SUMMARY: libFuzzer: overwrites-const-input\n"); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 566 | DumpCurrentUnit("crash-"); |
Mitch Phillips | d1e222e | 2019-09-27 22:04:36 | [diff] [blame] | 567 | PrintFinalStats(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 568 | _Exit(Options.ErrorExitCode); // Stop right now. |
| 569 | } |
| 570 | |
| 571 | // Compare two arrays, but not all bytes if the arrays are large. |
| 572 | static bool LooseMemeq(const uint8_t *A, const uint8_t *B, size_t Size) { |
| 573 | const size_t Limit = 64; |
| 574 | if (Size <= 64) |
| 575 | return !memcmp(A, B, Size); |
| 576 | // Compare first and last Limit/2 bytes. |
| 577 | return !memcmp(A, B, Limit / 2) && |
| 578 | !memcmp(A + Size - Limit / 2, B + Size - Limit / 2, Limit / 2); |
| 579 | } |
| 580 | |
Jonas Paulsson | 5908c7c | 2021-03-04 20:55:41 | [diff] [blame] | 581 | // This method is not inlined because it would cause a test to fail where it |
| 582 | // is part of the stack unwinding. See D97975 for details. |
Matt Morehouse | 4b82f61 | 2021-03-12 22:36:57 | [diff] [blame^] | 583 | ATTRIBUTE_NOINLINE void Fuzzer::ExecuteCallback(const uint8_t *Data, |
| 584 | size_t Size) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 585 | TPC.RecordInitialStack(); |
| 586 | TotalNumberOfRuns++; |
| 587 | assert(InFuzzingThread()); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 588 | // We copy the contents of Unit into a separate heap buffer |
| 589 | // so that we reliably find buffer overflows in it. |
| 590 | uint8_t *DataCopy = new uint8_t[Size]; |
| 591 | memcpy(DataCopy, Data, Size); |
Matt Morehouse | a34c65e | 2018-07-09 23:51:08 | [diff] [blame] | 592 | if (EF->__msan_unpoison) |
| 593 | EF->__msan_unpoison(DataCopy, Size); |
Matt Morehouse | 3478494 | 2019-05-09 22:48:46 | [diff] [blame] | 594 | if (EF->__msan_unpoison_param) |
| 595 | EF->__msan_unpoison_param(2); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 596 | if (CurrentUnitData && CurrentUnitData != Data) |
| 597 | memcpy(CurrentUnitData, Data, Size); |
| 598 | CurrentUnitSize = Size; |
Matt Morehouse | a34c65e | 2018-07-09 23:51:08 | [diff] [blame] | 599 | { |
| 600 | ScopedEnableMsanInterceptorChecks S; |
| 601 | AllocTracer.Start(Options.TraceMalloc); |
| 602 | UnitStartTime = system_clock::now(); |
| 603 | TPC.ResetMaps(); |
Matt Morehouse | 43a2296 | 2018-07-17 16:12:00 | [diff] [blame] | 604 | RunningUserCallback = true; |
Matt Morehouse | a34c65e | 2018-07-09 23:51:08 | [diff] [blame] | 605 | int Res = CB(DataCopy, Size); |
Matt Morehouse | 43a2296 | 2018-07-17 16:12:00 | [diff] [blame] | 606 | RunningUserCallback = false; |
Matt Morehouse | a34c65e | 2018-07-09 23:51:08 | [diff] [blame] | 607 | UnitStopTime = system_clock::now(); |
| 608 | (void)Res; |
| 609 | assert(Res == 0); |
| 610 | HasMoreMallocsThanFrees = AllocTracer.Stop(); |
| 611 | } |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 612 | if (!LooseMemeq(DataCopy, Data, Size)) |
| 613 | CrashOnOverwrittenData(); |
| 614 | CurrentUnitSize = 0; |
| 615 | delete[] DataCopy; |
| 616 | } |
| 617 | |
Kostya Serebryany | 63f4871 | 2019-02-12 00:12:33 | [diff] [blame] | 618 | std::string Fuzzer::WriteToOutputCorpus(const Unit &U) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 619 | if (Options.OnlyASCII) |
| 620 | assert(IsASCII(U)); |
| 621 | if (Options.OutputCorpus.empty()) |
Kostya Serebryany | 63f4871 | 2019-02-12 00:12:33 | [diff] [blame] | 622 | return ""; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 623 | std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U)); |
| 624 | WriteToFile(U, Path); |
| 625 | if (Options.Verbosity >= 2) |
| 626 | Printf("Written %zd bytes to %s\n", U.size(), Path.c_str()); |
Kostya Serebryany | 63f4871 | 2019-02-12 00:12:33 | [diff] [blame] | 627 | return Path; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) { |
| 631 | if (!Options.SaveArtifacts) |
| 632 | return; |
| 633 | std::string Path = Options.ArtifactPrefix + Prefix + Hash(U); |
| 634 | if (!Options.ExactArtifactPath.empty()) |
| 635 | Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix. |
| 636 | WriteToFile(U, Path); |
| 637 | Printf("artifact_prefix='%s'; Test unit written to %s\n", |
| 638 | Options.ArtifactPrefix.c_str(), Path.c_str()); |
| 639 | if (U.size() <= kMaxUnitSizeToPrint) |
| 640 | Printf("Base64: %s\n", Base64(U).c_str()); |
| 641 | } |
| 642 | |
| 643 | void Fuzzer::PrintStatusForNewUnit(const Unit &U, const char *Text) { |
| 644 | if (!Options.PrintNEW) |
| 645 | return; |
| 646 | PrintStats(Text, ""); |
| 647 | if (Options.Verbosity) { |
| 648 | Printf(" L: %zd/%zd ", U.size(), Corpus.MaxInputSize()); |
mhl | 66df989 | 2020-09-16 15:02:34 | [diff] [blame] | 649 | MD.PrintMutationSequence(Options.Verbosity >= 2); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 650 | Printf("\n"); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | void Fuzzer::ReportNewCoverage(InputInfo *II, const Unit &U) { |
| 655 | II->NumSuccessfullMutations++; |
| 656 | MD.RecordSuccessfulMutationSequence(); |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 657 | PrintStatusForNewUnit(U, II->Reduced ? "REDUCE" : "NEW "); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 658 | WriteToOutputCorpus(U); |
| 659 | NumberOfNewUnitsAdded++; |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 660 | CheckExitOnSrcPosOrItem(); // Check only after the unit is saved to corpus. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 661 | LastCorpusUpdateRun = TotalNumberOfRuns; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | // Tries detecting a memory leak on the particular input that we have just |
| 665 | // executed before calling this function. |
| 666 | void Fuzzer::TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size, |
| 667 | bool DuringInitialCorpusExecution) { |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 668 | if (!HasMoreMallocsThanFrees) |
| 669 | return; // mallocs==frees, a leak is unlikely. |
| 670 | if (!Options.DetectLeaks) |
| 671 | return; |
Max Moroz | 3f26dac | 2017-09-12 02:01:54 | [diff] [blame] | 672 | if (!DuringInitialCorpusExecution && |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 673 | TotalNumberOfRuns >= Options.MaxNumberOfRuns) |
| 674 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 675 | if (!&(EF->__lsan_enable) || !&(EF->__lsan_disable) || |
| 676 | !(EF->__lsan_do_recoverable_leak_check)) |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 677 | return; // No lsan. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 678 | // Run the target once again, but with lsan disabled so that if there is |
| 679 | // a real leak we do not report it twice. |
| 680 | EF->__lsan_disable(); |
| 681 | ExecuteCallback(Data, Size); |
| 682 | EF->__lsan_enable(); |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 683 | if (!HasMoreMallocsThanFrees) |
| 684 | return; // a leak is unlikely. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 685 | if (NumberOfLeakDetectionAttempts++ > 1000) { |
| 686 | Options.DetectLeaks = false; |
| 687 | Printf("INFO: libFuzzer disabled leak detection after every mutation.\n" |
| 688 | " Most likely the target function accumulates allocated\n" |
| 689 | " memory in a global state w/o actually leaking it.\n" |
| 690 | " You may try running this binary with -trace_malloc=[12]" |
| 691 | " to get a trace of mallocs and frees.\n" |
| 692 | " If LeakSanitizer is enabled in this process it will still\n" |
| 693 | " run on the process shutdown.\n"); |
| 694 | return; |
| 695 | } |
| 696 | // Now perform the actual lsan pass. This is expensive and we must ensure |
| 697 | // we don't call it too often. |
| 698 | if (EF->__lsan_do_recoverable_leak_check()) { // Leak is found, report it. |
| 699 | if (DuringInitialCorpusExecution) |
| 700 | Printf("\nINFO: a leak has been found in the initial corpus.\n\n"); |
| 701 | Printf("INFO: to ignore leaks on libFuzzer side use -detect_leaks=0.\n\n"); |
| 702 | CurrentUnitSize = Size; |
| 703 | DumpCurrentUnit("leak-"); |
| 704 | PrintFinalStats(); |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 705 | _Exit(Options.ErrorExitCode); // not exit() to disable lsan further on. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
| 709 | void Fuzzer::MutateAndTestOne() { |
| 710 | MD.StartMutationSequence(); |
| 711 | |
| 712 | auto &II = Corpus.ChooseUnitToMutate(MD.GetRand()); |
Dokyung Song | b53243e | 2020-09-01 16:22:59 | [diff] [blame] | 713 | if (Options.DoCrossOver) { |
| 714 | auto &CrossOverII = Corpus.ChooseUnitToCrossOverWith( |
| 715 | MD.GetRand(), Options.CrossOverUniformDist); |
| 716 | MD.SetCrossOverWith(&CrossOverII.U); |
| 717 | } |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 718 | const auto &U = II.U; |
| 719 | memcpy(BaseSha1, II.Sha1, sizeof(BaseSha1)); |
| 720 | assert(CurrentUnitData); |
| 721 | size_t Size = U.size(); |
| 722 | assert(Size <= MaxInputLen && "Oversized Unit"); |
| 723 | memcpy(CurrentUnitData, U.data(), Size); |
| 724 | |
| 725 | assert(MaxMutationLen > 0); |
| 726 | |
| 727 | size_t CurrentMaxMutationLen = |
| 728 | Min(MaxMutationLen, Max(U.size(), TmpMaxMutationLen)); |
| 729 | assert(CurrentMaxMutationLen > 0); |
| 730 | |
| 731 | for (int i = 0; i < Options.MutateDepth; i++) { |
| 732 | if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) |
| 733 | break; |
Kostya Serebryany | a2ca2dc | 2017-11-09 20:30:19 | [diff] [blame] | 734 | MaybeExitGracefully(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 735 | size_t NewSize = 0; |
Kostya Serebryany | 6b87e0c | 2018-07-19 01:23:32 | [diff] [blame] | 736 | if (II.HasFocusFunction && !II.DataFlowTraceForFocusFunction.empty() && |
| 737 | Size <= CurrentMaxMutationLen) |
| 738 | NewSize = MD.MutateWithMask(CurrentUnitData, Size, Size, |
| 739 | II.DataFlowTraceForFocusFunction); |
Matt Morehouse | 1b76063 | 2019-04-26 00:17:41 | [diff] [blame] | 740 | |
Max Moroz | 9d5e7ee | 2019-04-11 16:24:53 | [diff] [blame] | 741 | // If MutateWithMask either failed or wasn't called, call default Mutate. |
| 742 | if (!NewSize) |
Kostya Serebryany | 6b87e0c | 2018-07-19 01:23:32 | [diff] [blame] | 743 | NewSize = MD.Mutate(CurrentUnitData, Size, CurrentMaxMutationLen); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 744 | assert(NewSize > 0 && "Mutator returned empty unit"); |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 745 | assert(NewSize <= CurrentMaxMutationLen && "Mutator return oversized unit"); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 746 | Size = NewSize; |
| 747 | II.NumExecutedMutations++; |
Matt Morehouse | e2e38fc | 2020-05-19 17:28:18 | [diff] [blame] | 748 | Corpus.IncrementNumExecutedMutations(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 749 | |
Kostya Serebryany | ad05ee0 | 2017-12-01 19:18:38 | [diff] [blame] | 750 | bool FoundUniqFeatures = false; |
| 751 | bool NewCov = RunOne(CurrentUnitData, Size, /*MayDeleteFile=*/true, &II, |
Dokyung Song | 62673c4 | 2020-07-31 00:07:20 | [diff] [blame] | 752 | /*ForceAddToCorpus*/ false, &FoundUniqFeatures); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 753 | TryDetectingAMemoryLeak(CurrentUnitData, Size, |
| 754 | /*DuringInitialCorpusExecution*/ false); |
Kostya Serebryany | ad05ee0 | 2017-12-01 19:18:38 | [diff] [blame] | 755 | if (NewCov) { |
Matt Morehouse | 947838c | 2017-11-09 20:44:08 | [diff] [blame] | 756 | ReportNewCoverage(&II, {CurrentUnitData, CurrentUnitData + Size}); |
Kostya Serebryany | ad05ee0 | 2017-12-01 19:18:38 | [diff] [blame] | 757 | break; // We will mutate this input more in the next rounds. |
| 758 | } |
| 759 | if (Options.ReduceDepth && !FoundUniqFeatures) |
Matt Morehouse | 3478494 | 2019-05-09 22:48:46 | [diff] [blame] | 760 | break; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 761 | } |
Matt Morehouse | e2e38fc | 2020-05-19 17:28:18 | [diff] [blame] | 762 | |
| 763 | II.NeedsEnergyUpdate = true; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 764 | } |
| 765 | |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 766 | void Fuzzer::PurgeAllocator() { |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 767 | if (Options.PurgeAllocatorIntervalSec < 0 || !EF->__sanitizer_purge_allocator) |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 768 | return; |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 769 | if (duration_cast<seconds>(system_clock::now() - |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 770 | LastAllocatorPurgeAttemptTime) |
| 771 | .count() < Options.PurgeAllocatorIntervalSec) |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 772 | return; |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 773 | |
| 774 | if (Options.RssLimitMb <= 0 || |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 775 | GetPeakRSSMb() > static_cast<size_t>(Options.RssLimitMb) / 2) |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 776 | EF->__sanitizer_purge_allocator(); |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 777 | |
| 778 | LastAllocatorPurgeAttemptTime = system_clock::now(); |
| 779 | } |
| 780 | |
Kostya Serebryany | 4c7353c | 2019-05-10 01:34:26 | [diff] [blame] | 781 | void Fuzzer::ReadAndExecuteSeedCorpora(Vector<SizedFile> &CorporaFiles) { |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 782 | const size_t kMaxSaneLen = 1 << 20; |
| 783 | const size_t kMinDefaultLen = 4096; |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 784 | size_t MaxSize = 0; |
| 785 | size_t MinSize = -1; |
| 786 | size_t TotalSize = 0; |
Kostya Serebryany | 4c7353c | 2019-05-10 01:34:26 | [diff] [blame] | 787 | for (auto &File : CorporaFiles) { |
Kostya Serebryany | 93679be | 2017-09-12 21:58:07 | [diff] [blame] | 788 | MaxSize = Max(File.Size, MaxSize); |
| 789 | MinSize = Min(File.Size, MinSize); |
| 790 | TotalSize += File.Size; |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 791 | } |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 792 | if (Options.MaxLen == 0) |
| 793 | SetMaxInputLen(std::min(std::max(kMinDefaultLen, MaxSize), kMaxSaneLen)); |
| 794 | assert(MaxInputLen > 0); |
| 795 | |
Kostya Serebryany | 51823d3 | 2017-10-13 01:12:23 | [diff] [blame] | 796 | // Test the callback with empty input and never try it again. |
| 797 | uint8_t dummy = 0; |
| 798 | ExecuteCallback(&dummy, 0); |
| 799 | |
Kostya Serebryany | 4c7353c | 2019-05-10 01:34:26 | [diff] [blame] | 800 | if (CorporaFiles.empty()) { |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 801 | Printf("INFO: A corpus is not provided, starting from an empty corpus\n"); |
| 802 | Unit U({'\n'}); // Valid ASCII input. |
| 803 | RunOne(U.data(), U.size()); |
| 804 | } else { |
| 805 | Printf("INFO: seed corpus: files: %zd min: %zdb max: %zdb total: %zdb" |
| 806 | " rss: %zdMb\n", |
Kostya Serebryany | 4c7353c | 2019-05-10 01:34:26 | [diff] [blame] | 807 | CorporaFiles.size(), MinSize, MaxSize, TotalSize, GetPeakRSSMb()); |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 808 | if (Options.ShuffleAtStartUp) |
Kostya Serebryany | 4c7353c | 2019-05-10 01:34:26 | [diff] [blame] | 809 | std::shuffle(CorporaFiles.begin(), CorporaFiles.end(), MD.GetRand()); |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 810 | |
Kostya Serebryany | 93679be | 2017-09-12 21:58:07 | [diff] [blame] | 811 | if (Options.PreferSmall) { |
Kostya Serebryany | 4c7353c | 2019-05-10 01:34:26 | [diff] [blame] | 812 | std::stable_sort(CorporaFiles.begin(), CorporaFiles.end()); |
| 813 | assert(CorporaFiles.front().Size <= CorporaFiles.back().Size); |
Kostya Serebryany | 93679be | 2017-09-12 21:58:07 | [diff] [blame] | 814 | } |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 815 | |
| 816 | // Load and execute inputs one by one. |
Kostya Serebryany | 4c7353c | 2019-05-10 01:34:26 | [diff] [blame] | 817 | for (auto &SF : CorporaFiles) { |
Kostya Serebryany | 082e9a7 | 2017-08-31 19:17:15 | [diff] [blame] | 818 | auto U = FileToVector(SF.File, MaxInputLen, /*ExitOnError=*/false); |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 819 | assert(U.size() <= MaxInputLen); |
Dokyung Song | 62673c4 | 2020-07-31 00:07:20 | [diff] [blame] | 820 | RunOne(U.data(), U.size(), /*MayDeleteFile*/ false, /*II*/ nullptr, |
| 821 | /*ForceAddToCorpus*/ Options.KeepSeed, |
| 822 | /*FoundUniqFeatures*/ nullptr); |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 823 | CheckExitOnSrcPosOrItem(); |
| 824 | TryDetectingAMemoryLeak(U.data(), U.size(), |
| 825 | /*DuringInitialCorpusExecution*/ true); |
| 826 | } |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 827 | } |
| 828 | |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 829 | PrintStats("INITED"); |
Max Moroz | 0764757 | 2020-02-18 17:57:16 | [diff] [blame] | 830 | if (!Options.FocusFunction.empty()) { |
Kostya Serebryany | e9c6f06 | 2018-05-16 23:26:37 | [diff] [blame] | 831 | Printf("INFO: %zd/%zd inputs touch the focus function\n", |
| 832 | Corpus.NumInputsThatTouchFocusFunction(), Corpus.size()); |
Max Moroz | 0764757 | 2020-02-18 17:57:16 | [diff] [blame] | 833 | if (!Options.DataFlowTrace.empty()) |
| 834 | Printf("INFO: %zd/%zd inputs have the Data Flow Trace\n", |
| 835 | Corpus.NumInputsWithDataFlowTrace(), |
| 836 | Corpus.NumInputsThatTouchFocusFunction()); |
| 837 | } |
Kostya Serebryany | e9c6f06 | 2018-05-16 23:26:37 | [diff] [blame] | 838 | |
Max Moroz | fe97441 | 2018-05-23 19:42:30 | [diff] [blame] | 839 | if (Corpus.empty() && Options.MaxNumberOfRuns) { |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 840 | Printf("ERROR: no interesting inputs were found. " |
| 841 | "Is the code instrumented for coverage? Exiting.\n"); |
| 842 | exit(1); |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 843 | } |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 844 | } |
| 845 | |
Kostya Serebryany | 4c7353c | 2019-05-10 01:34:26 | [diff] [blame] | 846 | void Fuzzer::Loop(Vector<SizedFile> &CorporaFiles) { |
Kostya Serebryany | 060f4b4 | 2019-05-24 00:43:52 | [diff] [blame] | 847 | auto FocusFunctionOrAuto = Options.FocusFunction; |
| 848 | DFT.Init(Options.DataFlowTrace, &FocusFunctionOrAuto, CorporaFiles, |
| 849 | MD.GetRand()); |
| 850 | TPC.SetFocusFunction(FocusFunctionOrAuto); |
Kostya Serebryany | 4c7353c | 2019-05-10 01:34:26 | [diff] [blame] | 851 | ReadAndExecuteSeedCorpora(CorporaFiles); |
Kostya Serebryany | 67af992 | 2018-06-07 01:40:20 | [diff] [blame] | 852 | DFT.Clear(); // No need for DFT any more. |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 853 | TPC.SetPrintNewPCs(Options.PrintNewCovPcs); |
Kostya Serebryany | 2eef816 | 2017-08-25 20:09:25 | [diff] [blame] | 854 | TPC.SetPrintNewFuncs(Options.PrintNewCovFuncs); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 855 | system_clock::time_point LastCorpusReload = system_clock::now(); |
Kostya Serebryany | b6ca1e7 | 2019-02-16 01:23:41 | [diff] [blame] | 856 | |
| 857 | TmpMaxMutationLen = |
| 858 | Min(MaxMutationLen, Max(size_t(4), Corpus.MaxInputSize())); |
| 859 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 860 | while (true) { |
| 861 | auto Now = system_clock::now(); |
Kostya Serebryany | db88fc5 | 2019-06-14 22:56:50 | [diff] [blame] | 862 | if (!Options.StopFile.empty() && |
| 863 | !FileToVector(Options.StopFile, 1, false).empty()) |
| 864 | break; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 865 | if (duration_cast<seconds>(Now - LastCorpusReload).count() >= |
| 866 | Options.ReloadIntervalSec) { |
| 867 | RereadOutputCorpus(MaxInputLen); |
| 868 | LastCorpusReload = system_clock::now(); |
| 869 | } |
| 870 | if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) |
| 871 | break; |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 872 | if (TimedOut()) |
| 873 | break; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 874 | |
| 875 | // Update TmpMaxMutationLen |
Matt Morehouse | 36c89b3 | 2018-02-13 20:52:15 | [diff] [blame] | 876 | if (Options.LenControl) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 877 | if (TmpMaxMutationLen < MaxMutationLen && |
Kostya Serebryany | e9ed232 | 2017-12-12 23:11:28 | [diff] [blame] | 878 | TotalNumberOfRuns - LastCorpusUpdateRun > |
Matt Morehouse | 36c89b3 | 2018-02-13 20:52:15 | [diff] [blame] | 879 | Options.LenControl * Log(TmpMaxMutationLen)) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 880 | TmpMaxMutationLen = |
Kostya Serebryany | e9ed232 | 2017-12-12 23:11:28 | [diff] [blame] | 881 | Min(MaxMutationLen, TmpMaxMutationLen + Log(TmpMaxMutationLen)); |
Kostya Serebryany | e9ed232 | 2017-12-12 23:11:28 | [diff] [blame] | 882 | LastCorpusUpdateRun = TotalNumberOfRuns; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 883 | } |
| 884 | } else { |
| 885 | TmpMaxMutationLen = MaxMutationLen; |
| 886 | } |
| 887 | |
| 888 | // Perform several mutations and runs. |
| 889 | MutateAndTestOne(); |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame] | 890 | |
| 891 | PurgeAllocator(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | PrintStats("DONE ", "\n"); |
| 895 | MD.PrintRecommendedDictionary(); |
| 896 | } |
| 897 | |
| 898 | void Fuzzer::MinimizeCrashLoop(const Unit &U) { |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 899 | if (U.size() <= 1) |
| 900 | return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 901 | while (!TimedOut() && TotalNumberOfRuns < Options.MaxNumberOfRuns) { |
| 902 | MD.StartMutationSequence(); |
| 903 | memcpy(CurrentUnitData, U.data(), U.size()); |
| 904 | for (int i = 0; i < Options.MutateDepth; i++) { |
| 905 | size_t NewSize = MD.Mutate(CurrentUnitData, U.size(), MaxMutationLen); |
| 906 | assert(NewSize > 0 && NewSize <= MaxMutationLen); |
| 907 | ExecuteCallback(CurrentUnitData, NewSize); |
| 908 | PrintPulseAndReportSlowInput(CurrentUnitData, NewSize); |
| 909 | TryDetectingAMemoryLeak(CurrentUnitData, NewSize, |
| 910 | /*DuringInitialCorpusExecution*/ false); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 915 | } // namespace fuzzer |
| 916 | |
| 917 | extern "C" { |
| 918 | |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame] | 919 | ATTRIBUTE_INTERFACE size_t |
Petr Hosek | eac2b47 | 2018-01-17 20:39:14 | [diff] [blame] | 920 | LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) { |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 921 | assert(fuzzer::F); |
| 922 | return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize); |
| 923 | } |
| 924 | |
Alex Shlyapnikov | 5ded070 | 2017-10-23 23:24:33 | [diff] [blame] | 925 | } // extern "C" |