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