George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 1 | //===- FuzzerLoop.cpp - Fuzzer's main loop --------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // Fuzzer's main loop. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include "FuzzerCorpus.h" |
| 13 | #include "FuzzerIO.h" |
| 14 | #include "FuzzerInternal.h" |
| 15 | #include "FuzzerMutate.h" |
| 16 | #include "FuzzerRandom.h" |
| 17 | #include "FuzzerShmem.h" |
| 18 | #include "FuzzerTracePC.h" |
| 19 | #include <algorithm> |
| 20 | #include <cstring> |
| 21 | #include <memory> |
| 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 | |
| 43 | SharedMemoryRegion SMR; |
| 44 | |
| 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; |
| 72 | }; |
| 73 | |
| 74 | static MallocFreeTracer AllocTracer; |
| 75 | |
| 76 | ATTRIBUTE_NO_SANITIZE_MEMORY |
| 77 | void MallocHook(const volatile void *ptr, size_t size) { |
| 78 | size_t N = AllocTracer.Mallocs++; |
| 79 | F->HandleMalloc(size); |
| 80 | if (int TraceLevel = AllocTracer.TraceLevel) { |
| 81 | Printf("MALLOC[%zd] %p %zd\n", N, ptr, size); |
| 82 | if (TraceLevel >= 2 && EF) |
| 83 | EF->__sanitizer_print_stack_trace(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | ATTRIBUTE_NO_SANITIZE_MEMORY |
| 88 | void FreeHook(const volatile void *ptr) { |
| 89 | size_t N = AllocTracer.Frees++; |
| 90 | if (int TraceLevel = AllocTracer.TraceLevel) { |
| 91 | Printf("FREE[%zd] %p\n", N, ptr); |
| 92 | if (TraceLevel >= 2 && EF) |
| 93 | EF->__sanitizer_print_stack_trace(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Crash on a single malloc that exceeds the rss limit. |
| 98 | void Fuzzer::HandleMalloc(size_t Size) { |
| 99 | if (!Options.RssLimitMb || (Size >> 20) < (size_t)Options.RssLimitMb) |
| 100 | return; |
| 101 | Printf("==%d== ERROR: libFuzzer: out-of-memory (malloc(%zd))\n", GetPid(), |
| 102 | Size); |
| 103 | Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n"); |
| 104 | if (EF->__sanitizer_print_stack_trace) |
| 105 | EF->__sanitizer_print_stack_trace(); |
| 106 | DumpCurrentUnit("oom-"); |
| 107 | Printf("SUMMARY: libFuzzer: out-of-memory\n"); |
| 108 | PrintFinalStats(); |
| 109 | _Exit(Options.ErrorExitCode); // Stop right now. |
| 110 | } |
| 111 | |
| 112 | Fuzzer::Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD, |
| 113 | FuzzingOptions Options) |
| 114 | : CB(CB), Corpus(Corpus), MD(MD), Options(Options) { |
| 115 | if (EF->__sanitizer_set_death_callback) |
| 116 | EF->__sanitizer_set_death_callback(StaticDeathCallback); |
| 117 | assert(!F); |
| 118 | F = this; |
| 119 | TPC.ResetMaps(); |
| 120 | IsMyThread = true; |
| 121 | if (Options.DetectLeaks && EF->__sanitizer_install_malloc_and_free_hooks) |
| 122 | EF->__sanitizer_install_malloc_and_free_hooks(MallocHook, FreeHook); |
| 123 | TPC.SetUseCounters(Options.UseCounters); |
| 124 | TPC.SetUseValueProfile(Options.UseValueProfile); |
Max Moroz | 330496c | 2017-10-05 22:41:03 | [diff] [blame] | 125 | TPC.SetUseClangCoverage(Options.UseClangCoverage); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 126 | |
| 127 | if (Options.Verbosity) |
| 128 | TPC.PrintModuleInfo(); |
| 129 | if (!Options.OutputCorpus.empty() && Options.ReloadIntervalSec) |
| 130 | EpochOfLastReadOfOutputCorpus = GetEpoch(Options.OutputCorpus); |
| 131 | MaxInputLen = MaxMutationLen = Options.MaxLen; |
| 132 | TmpMaxMutationLen = Max(size_t(4), Corpus.MaxInputSize()); |
| 133 | AllocateCurrentUnitData(); |
| 134 | CurrentUnitSize = 0; |
| 135 | memset(BaseSha1, 0, sizeof(BaseSha1)); |
| 136 | } |
| 137 | |
| 138 | Fuzzer::~Fuzzer() { } |
| 139 | |
| 140 | void Fuzzer::AllocateCurrentUnitData() { |
| 141 | if (CurrentUnitData || MaxInputLen == 0) return; |
| 142 | CurrentUnitData = new uint8_t[MaxInputLen]; |
| 143 | } |
| 144 | |
| 145 | void Fuzzer::StaticDeathCallback() { |
| 146 | assert(F); |
| 147 | F->DeathCallback(); |
| 148 | } |
| 149 | |
| 150 | void Fuzzer::DumpCurrentUnit(const char *Prefix) { |
| 151 | if (!CurrentUnitData) return; // Happens when running individual inputs. |
| 152 | MD.PrintMutationSequence(); |
| 153 | Printf("; base unit: %s\n", Sha1ToString(BaseSha1).c_str()); |
| 154 | size_t UnitSize = CurrentUnitSize; |
| 155 | if (UnitSize <= kMaxUnitSizeToPrint) { |
| 156 | PrintHexArray(CurrentUnitData, UnitSize, "\n"); |
| 157 | PrintASCII(CurrentUnitData, UnitSize, "\n"); |
| 158 | } |
| 159 | WriteUnitToFileWithPrefix({CurrentUnitData, CurrentUnitData + UnitSize}, |
| 160 | Prefix); |
| 161 | } |
| 162 | |
| 163 | NO_SANITIZE_MEMORY |
| 164 | void Fuzzer::DeathCallback() { |
| 165 | DumpCurrentUnit("crash-"); |
| 166 | PrintFinalStats(); |
| 167 | } |
| 168 | |
| 169 | void Fuzzer::StaticAlarmCallback() { |
| 170 | assert(F); |
| 171 | F->AlarmCallback(); |
| 172 | } |
| 173 | |
| 174 | void Fuzzer::StaticCrashSignalCallback() { |
| 175 | assert(F); |
| 176 | F->CrashCallback(); |
| 177 | } |
| 178 | |
| 179 | void Fuzzer::StaticExitCallback() { |
| 180 | assert(F); |
| 181 | F->ExitCallback(); |
| 182 | } |
| 183 | |
| 184 | void Fuzzer::StaticInterruptCallback() { |
| 185 | assert(F); |
| 186 | F->InterruptCallback(); |
| 187 | } |
| 188 | |
| 189 | void Fuzzer::StaticFileSizeExceedCallback() { |
| 190 | Printf("==%lu== ERROR: libFuzzer: file size exceeded\n", GetPid()); |
| 191 | exit(1); |
| 192 | } |
| 193 | |
| 194 | void Fuzzer::CrashCallback() { |
| 195 | Printf("==%lu== ERROR: libFuzzer: deadly signal\n", GetPid()); |
| 196 | if (EF->__sanitizer_print_stack_trace) |
| 197 | EF->__sanitizer_print_stack_trace(); |
| 198 | Printf("NOTE: libFuzzer has rudimentary signal handlers.\n" |
| 199 | " Combine libFuzzer with AddressSanitizer or similar for better " |
| 200 | "crash reports.\n"); |
| 201 | Printf("SUMMARY: libFuzzer: deadly signal\n"); |
| 202 | DumpCurrentUnit("crash-"); |
| 203 | PrintFinalStats(); |
| 204 | _Exit(Options.ErrorExitCode); // Stop right now. |
| 205 | } |
| 206 | |
| 207 | void Fuzzer::ExitCallback() { |
| 208 | if (!RunningCB) |
| 209 | return; // This exit did not come from the user callback |
| 210 | Printf("==%lu== ERROR: libFuzzer: fuzz target exited\n", GetPid()); |
| 211 | if (EF->__sanitizer_print_stack_trace) |
| 212 | EF->__sanitizer_print_stack_trace(); |
| 213 | Printf("SUMMARY: libFuzzer: fuzz target exited\n"); |
| 214 | DumpCurrentUnit("crash-"); |
| 215 | PrintFinalStats(); |
| 216 | _Exit(Options.ErrorExitCode); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | void Fuzzer::InterruptCallback() { |
| 221 | Printf("==%lu== libFuzzer: run interrupted; exiting\n", GetPid()); |
| 222 | PrintFinalStats(); |
| 223 | _Exit(0); // Stop right now, don't perform any at-exit actions. |
| 224 | } |
| 225 | |
| 226 | NO_SANITIZE_MEMORY |
| 227 | void Fuzzer::AlarmCallback() { |
| 228 | assert(Options.UnitTimeoutSec > 0); |
| 229 | // In Windows Alarm callback is executed by a different thread. |
| 230 | #if !LIBFUZZER_WINDOWS |
| 231 | if (!InFuzzingThread()) return; |
| 232 | #endif |
| 233 | if (!RunningCB) |
| 234 | return; // We have not started running units yet. |
| 235 | size_t Seconds = |
| 236 | duration_cast<seconds>(system_clock::now() - UnitStartTime).count(); |
| 237 | if (Seconds == 0) |
| 238 | return; |
| 239 | if (Options.Verbosity >= 2) |
| 240 | Printf("AlarmCallback %zd\n", Seconds); |
| 241 | if (Seconds >= (size_t)Options.UnitTimeoutSec) { |
| 242 | Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds); |
| 243 | Printf(" and the timeout value is %d (use -timeout=N to change)\n", |
| 244 | Options.UnitTimeoutSec); |
| 245 | DumpCurrentUnit("timeout-"); |
| 246 | Printf("==%lu== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(), |
| 247 | Seconds); |
| 248 | if (EF->__sanitizer_print_stack_trace) |
| 249 | EF->__sanitizer_print_stack_trace(); |
| 250 | Printf("SUMMARY: libFuzzer: timeout\n"); |
| 251 | PrintFinalStats(); |
| 252 | _Exit(Options.TimeoutExitCode); // Stop right now. |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void Fuzzer::RssLimitCallback() { |
| 257 | Printf( |
| 258 | "==%lu== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n", |
| 259 | GetPid(), GetPeakRSSMb(), Options.RssLimitMb); |
| 260 | Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n\n"); |
| 261 | if (EF->__sanitizer_print_memory_profile) |
| 262 | EF->__sanitizer_print_memory_profile(95, 8); |
| 263 | DumpCurrentUnit("oom-"); |
| 264 | Printf("SUMMARY: libFuzzer: out-of-memory\n"); |
| 265 | PrintFinalStats(); |
| 266 | _Exit(Options.ErrorExitCode); // Stop right now. |
| 267 | } |
| 268 | |
| 269 | void Fuzzer::PrintStats(const char *Where, const char *End, size_t Units) { |
| 270 | size_t ExecPerSec = execPerSec(); |
| 271 | if (!Options.Verbosity) |
| 272 | return; |
| 273 | Printf("#%zd\t%s", TotalNumberOfRuns, Where); |
| 274 | if (size_t N = TPC.GetTotalPCCoverage()) |
| 275 | Printf(" cov: %zd", N); |
| 276 | if (size_t N = Corpus.NumFeatures()) |
| 277 | Printf( " ft: %zd", N); |
| 278 | if (!Corpus.empty()) { |
| 279 | Printf(" corp: %zd", Corpus.NumActiveUnits()); |
| 280 | if (size_t N = Corpus.SizeInBytes()) { |
| 281 | if (N < (1<<14)) |
| 282 | Printf("/%zdb", N); |
| 283 | else if (N < (1 << 24)) |
| 284 | Printf("/%zdKb", N >> 10); |
| 285 | else |
| 286 | Printf("/%zdMb", N >> 20); |
| 287 | } |
| 288 | } |
| 289 | if (Units) |
| 290 | Printf(" units: %zd", Units); |
| 291 | |
| 292 | Printf(" exec/s: %zd", ExecPerSec); |
| 293 | Printf(" rss: %zdMb", GetPeakRSSMb()); |
| 294 | Printf("%s", End); |
| 295 | } |
| 296 | |
| 297 | void Fuzzer::PrintFinalStats() { |
| 298 | if (Options.PrintCoverage) |
| 299 | TPC.PrintCoverage(); |
| 300 | if (Options.DumpCoverage) |
| 301 | TPC.DumpCoverage(); |
| 302 | if (Options.PrintCorpusStats) |
| 303 | Corpus.PrintStats(); |
| 304 | if (!Options.PrintFinalStats) return; |
| 305 | size_t ExecPerSec = execPerSec(); |
| 306 | Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns); |
| 307 | Printf("stat::average_exec_per_sec: %zd\n", ExecPerSec); |
| 308 | Printf("stat::new_units_added: %zd\n", NumberOfNewUnitsAdded); |
| 309 | Printf("stat::slowest_unit_time_sec: %zd\n", TimeOfLongestUnitInSeconds); |
| 310 | Printf("stat::peak_rss_mb: %zd\n", GetPeakRSSMb()); |
| 311 | } |
| 312 | |
| 313 | void Fuzzer::SetMaxInputLen(size_t MaxInputLen) { |
| 314 | assert(this->MaxInputLen == 0); // Can only reset MaxInputLen from 0 to non-0. |
| 315 | assert(MaxInputLen); |
| 316 | this->MaxInputLen = MaxInputLen; |
| 317 | this->MaxMutationLen = MaxInputLen; |
| 318 | AllocateCurrentUnitData(); |
| 319 | Printf("INFO: -max_len is not provided; " |
| 320 | "libFuzzer will not generate inputs larger than %zd bytes\n", |
| 321 | MaxInputLen); |
| 322 | } |
| 323 | |
| 324 | void Fuzzer::SetMaxMutationLen(size_t MaxMutationLen) { |
| 325 | assert(MaxMutationLen && MaxMutationLen <= MaxInputLen); |
| 326 | this->MaxMutationLen = MaxMutationLen; |
| 327 | } |
| 328 | |
| 329 | void Fuzzer::CheckExitOnSrcPosOrItem() { |
| 330 | if (!Options.ExitOnSrcPos.empty()) { |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 331 | static auto *PCsSet = new Set<uintptr_t>; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 332 | auto HandlePC = [&](uintptr_t PC) { |
| 333 | if (!PCsSet->insert(PC).second) return; |
| 334 | std::string Descr = DescribePC("%F %L", PC + 1); |
| 335 | if (Descr.find(Options.ExitOnSrcPos) != std::string::npos) { |
| 336 | Printf("INFO: found line matching '%s', exiting.\n", |
| 337 | Options.ExitOnSrcPos.c_str()); |
| 338 | _Exit(0); |
| 339 | } |
| 340 | }; |
| 341 | TPC.ForEachObservedPC(HandlePC); |
| 342 | } |
| 343 | if (!Options.ExitOnItem.empty()) { |
| 344 | if (Corpus.HasUnit(Options.ExitOnItem)) { |
| 345 | Printf("INFO: found item with checksum '%s', exiting.\n", |
| 346 | Options.ExitOnItem.c_str()); |
| 347 | _Exit(0); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void Fuzzer::RereadOutputCorpus(size_t MaxSize) { |
| 353 | if (Options.OutputCorpus.empty() || !Options.ReloadIntervalSec) return; |
George Karpenkov | bebcbfb | 2017-08-27 23:20:09 | [diff] [blame] | 354 | Vector<Unit> AdditionalCorpus; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 355 | ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus, |
| 356 | &EpochOfLastReadOfOutputCorpus, MaxSize, |
| 357 | /*ExitOnError*/ false); |
| 358 | if (Options.Verbosity >= 2) |
| 359 | Printf("Reload: read %zd new units.\n", AdditionalCorpus.size()); |
| 360 | bool Reloaded = false; |
| 361 | for (auto &U : AdditionalCorpus) { |
| 362 | if (U.size() > MaxSize) |
| 363 | U.resize(MaxSize); |
| 364 | if (!Corpus.HasUnit(U)) { |
| 365 | if (RunOne(U.data(), U.size())) { |
| 366 | CheckExitOnSrcPosOrItem(); |
| 367 | Reloaded = true; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | if (Reloaded) |
| 372 | PrintStats("RELOAD"); |
| 373 | } |
| 374 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 375 | void Fuzzer::PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size) { |
| 376 | auto TimeOfUnit = |
| 377 | duration_cast<seconds>(UnitStopTime - UnitStartTime).count(); |
| 378 | if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && |
| 379 | secondsSinceProcessStartUp() >= 2) |
| 380 | PrintStats("pulse "); |
| 381 | if (TimeOfUnit > TimeOfLongestUnitInSeconds * 1.1 && |
| 382 | TimeOfUnit >= Options.ReportSlowUnits) { |
| 383 | TimeOfLongestUnitInSeconds = TimeOfUnit; |
| 384 | Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds); |
| 385 | WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-"); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | bool Fuzzer::RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile, |
| 390 | InputInfo *II) { |
| 391 | if (!Size) return false; |
| 392 | |
| 393 | ExecuteCallback(Data, Size); |
| 394 | |
| 395 | UniqFeatureSetTmp.clear(); |
| 396 | size_t FoundUniqFeaturesOfII = 0; |
| 397 | size_t NumUpdatesBefore = Corpus.NumFeatureUpdates(); |
| 398 | TPC.CollectFeatures([&](size_t Feature) { |
Kostya Serebryany | 4083d54 | 2017-10-11 01:44:26 | [diff] [blame] | 399 | Corpus.UpdateFeatureFrequency(Feature); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 400 | if (Corpus.AddFeature(Feature, Size, Options.Shrink)) |
| 401 | UniqFeatureSetTmp.push_back(Feature); |
| 402 | if (Options.ReduceInputs && II) |
| 403 | if (std::binary_search(II->UniqFeatureSet.begin(), |
| 404 | II->UniqFeatureSet.end(), Feature)) |
| 405 | FoundUniqFeaturesOfII++; |
| 406 | }); |
| 407 | PrintPulseAndReportSlowInput(Data, Size); |
| 408 | size_t NumNewFeatures = Corpus.NumFeatureUpdates() - NumUpdatesBefore; |
| 409 | if (NumNewFeatures) { |
| 410 | TPC.UpdateObservedPCs(); |
| 411 | Corpus.AddToCorpus({Data, Data + Size}, NumNewFeatures, MayDeleteFile, |
| 412 | UniqFeatureSetTmp); |
| 413 | return true; |
| 414 | } |
| 415 | if (II && FoundUniqFeaturesOfII && |
| 416 | FoundUniqFeaturesOfII == II->UniqFeatureSet.size() && |
| 417 | II->U.size() > Size) { |
| 418 | Corpus.Replace(II, {Data, Data + Size}); |
| 419 | return true; |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | size_t Fuzzer::GetCurrentUnitInFuzzingThead(const uint8_t **Data) const { |
| 425 | assert(InFuzzingThread()); |
| 426 | *Data = CurrentUnitData; |
| 427 | return CurrentUnitSize; |
| 428 | } |
| 429 | |
| 430 | void Fuzzer::CrashOnOverwrittenData() { |
| 431 | Printf("==%d== ERROR: libFuzzer: fuzz target overwrites it's const input\n", |
| 432 | GetPid()); |
| 433 | DumpCurrentUnit("crash-"); |
| 434 | Printf("SUMMARY: libFuzzer: out-of-memory\n"); |
| 435 | _Exit(Options.ErrorExitCode); // Stop right now. |
| 436 | } |
| 437 | |
| 438 | // Compare two arrays, but not all bytes if the arrays are large. |
| 439 | static bool LooseMemeq(const uint8_t *A, const uint8_t *B, size_t Size) { |
| 440 | const size_t Limit = 64; |
| 441 | if (Size <= 64) |
| 442 | return !memcmp(A, B, Size); |
| 443 | // Compare first and last Limit/2 bytes. |
| 444 | return !memcmp(A, B, Limit / 2) && |
| 445 | !memcmp(A + Size - Limit / 2, B + Size - Limit / 2, Limit / 2); |
| 446 | } |
| 447 | |
| 448 | void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) { |
| 449 | TPC.RecordInitialStack(); |
| 450 | TotalNumberOfRuns++; |
| 451 | assert(InFuzzingThread()); |
| 452 | if (SMR.IsClient()) |
| 453 | SMR.WriteByteArray(Data, Size); |
| 454 | // We copy the contents of Unit into a separate heap buffer |
| 455 | // so that we reliably find buffer overflows in it. |
| 456 | uint8_t *DataCopy = new uint8_t[Size]; |
| 457 | memcpy(DataCopy, Data, Size); |
| 458 | if (CurrentUnitData && CurrentUnitData != Data) |
| 459 | memcpy(CurrentUnitData, Data, Size); |
| 460 | CurrentUnitSize = Size; |
| 461 | AllocTracer.Start(Options.TraceMalloc); |
| 462 | UnitStartTime = system_clock::now(); |
| 463 | TPC.ResetMaps(); |
| 464 | RunningCB = true; |
| 465 | int Res = CB(DataCopy, Size); |
| 466 | RunningCB = false; |
| 467 | UnitStopTime = system_clock::now(); |
| 468 | (void)Res; |
| 469 | assert(Res == 0); |
| 470 | HasMoreMallocsThanFrees = AllocTracer.Stop(); |
| 471 | if (!LooseMemeq(DataCopy, Data, Size)) |
| 472 | CrashOnOverwrittenData(); |
| 473 | CurrentUnitSize = 0; |
| 474 | delete[] DataCopy; |
| 475 | } |
| 476 | |
| 477 | void Fuzzer::WriteToOutputCorpus(const Unit &U) { |
| 478 | if (Options.OnlyASCII) |
| 479 | assert(IsASCII(U)); |
| 480 | if (Options.OutputCorpus.empty()) |
| 481 | return; |
| 482 | std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U)); |
| 483 | WriteToFile(U, Path); |
| 484 | if (Options.Verbosity >= 2) |
| 485 | Printf("Written %zd bytes to %s\n", U.size(), Path.c_str()); |
| 486 | } |
| 487 | |
| 488 | void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) { |
| 489 | if (!Options.SaveArtifacts) |
| 490 | return; |
| 491 | std::string Path = Options.ArtifactPrefix + Prefix + Hash(U); |
| 492 | if (!Options.ExactArtifactPath.empty()) |
| 493 | Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix. |
| 494 | WriteToFile(U, Path); |
| 495 | Printf("artifact_prefix='%s'; Test unit written to %s\n", |
| 496 | Options.ArtifactPrefix.c_str(), Path.c_str()); |
| 497 | if (U.size() <= kMaxUnitSizeToPrint) |
| 498 | Printf("Base64: %s\n", Base64(U).c_str()); |
| 499 | } |
| 500 | |
| 501 | void Fuzzer::PrintStatusForNewUnit(const Unit &U, const char *Text) { |
| 502 | if (!Options.PrintNEW) |
| 503 | return; |
| 504 | PrintStats(Text, ""); |
| 505 | if (Options.Verbosity) { |
| 506 | Printf(" L: %zd/%zd ", U.size(), Corpus.MaxInputSize()); |
| 507 | MD.PrintMutationSequence(); |
| 508 | Printf("\n"); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | void Fuzzer::ReportNewCoverage(InputInfo *II, const Unit &U) { |
| 513 | II->NumSuccessfullMutations++; |
| 514 | MD.RecordSuccessfulMutationSequence(); |
| 515 | PrintStatusForNewUnit(U, II->Reduced ? "REDUCE" : |
| 516 | "NEW "); |
| 517 | WriteToOutputCorpus(U); |
| 518 | NumberOfNewUnitsAdded++; |
| 519 | CheckExitOnSrcPosOrItem(); // Check only after the unit is saved to corpus. |
| 520 | LastCorpusUpdateRun = TotalNumberOfRuns; |
| 521 | LastCorpusUpdateTime = system_clock::now(); |
| 522 | } |
| 523 | |
| 524 | // Tries detecting a memory leak on the particular input that we have just |
| 525 | // executed before calling this function. |
| 526 | void Fuzzer::TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size, |
| 527 | bool DuringInitialCorpusExecution) { |
| 528 | if (!HasMoreMallocsThanFrees) return; // mallocs==frees, a leak is unlikely. |
| 529 | if (!Options.DetectLeaks) return; |
Max Moroz | 3f26dac | 2017-09-12 02:01:54 | [diff] [blame] | 530 | if (!DuringInitialCorpusExecution && |
| 531 | TotalNumberOfRuns >= Options.MaxNumberOfRuns) return; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 532 | if (!&(EF->__lsan_enable) || !&(EF->__lsan_disable) || |
| 533 | !(EF->__lsan_do_recoverable_leak_check)) |
| 534 | return; // No lsan. |
| 535 | // Run the target once again, but with lsan disabled so that if there is |
| 536 | // a real leak we do not report it twice. |
| 537 | EF->__lsan_disable(); |
| 538 | ExecuteCallback(Data, Size); |
| 539 | EF->__lsan_enable(); |
| 540 | if (!HasMoreMallocsThanFrees) return; // a leak is unlikely. |
| 541 | if (NumberOfLeakDetectionAttempts++ > 1000) { |
| 542 | Options.DetectLeaks = false; |
| 543 | Printf("INFO: libFuzzer disabled leak detection after every mutation.\n" |
| 544 | " Most likely the target function accumulates allocated\n" |
| 545 | " memory in a global state w/o actually leaking it.\n" |
| 546 | " You may try running this binary with -trace_malloc=[12]" |
| 547 | " to get a trace of mallocs and frees.\n" |
| 548 | " If LeakSanitizer is enabled in this process it will still\n" |
| 549 | " run on the process shutdown.\n"); |
| 550 | return; |
| 551 | } |
| 552 | // Now perform the actual lsan pass. This is expensive and we must ensure |
| 553 | // we don't call it too often. |
| 554 | if (EF->__lsan_do_recoverable_leak_check()) { // Leak is found, report it. |
| 555 | if (DuringInitialCorpusExecution) |
| 556 | Printf("\nINFO: a leak has been found in the initial corpus.\n\n"); |
| 557 | Printf("INFO: to ignore leaks on libFuzzer side use -detect_leaks=0.\n\n"); |
| 558 | CurrentUnitSize = Size; |
| 559 | DumpCurrentUnit("leak-"); |
| 560 | PrintFinalStats(); |
| 561 | _Exit(Options.ErrorExitCode); // not exit() to disable lsan further on. |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | void Fuzzer::MutateAndTestOne() { |
| 566 | MD.StartMutationSequence(); |
| 567 | |
| 568 | auto &II = Corpus.ChooseUnitToMutate(MD.GetRand()); |
Kostya Serebryany | 4083d54 | 2017-10-11 01:44:26 | [diff] [blame] | 569 | if (Options.UseFeatureFrequency) |
| 570 | Corpus.UpdateFeatureFrequencyScore(&II); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 571 | const auto &U = II.U; |
| 572 | memcpy(BaseSha1, II.Sha1, sizeof(BaseSha1)); |
| 573 | assert(CurrentUnitData); |
| 574 | size_t Size = U.size(); |
| 575 | assert(Size <= MaxInputLen && "Oversized Unit"); |
| 576 | memcpy(CurrentUnitData, U.data(), Size); |
| 577 | |
| 578 | assert(MaxMutationLen > 0); |
| 579 | |
| 580 | size_t CurrentMaxMutationLen = |
| 581 | Min(MaxMutationLen, Max(U.size(), TmpMaxMutationLen)); |
| 582 | assert(CurrentMaxMutationLen > 0); |
| 583 | |
| 584 | for (int i = 0; i < Options.MutateDepth; i++) { |
| 585 | if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) |
| 586 | break; |
| 587 | size_t NewSize = 0; |
| 588 | NewSize = MD.Mutate(CurrentUnitData, Size, CurrentMaxMutationLen); |
| 589 | assert(NewSize > 0 && "Mutator returned empty unit"); |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame^] | 590 | assert(NewSize <= CurrentMaxMutationLen && "Mutator return oversized unit"); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 591 | Size = NewSize; |
| 592 | II.NumExecutedMutations++; |
| 593 | if (RunOne(CurrentUnitData, Size, /*MayDeleteFile=*/true, &II)) |
| 594 | ReportNewCoverage(&II, {CurrentUnitData, CurrentUnitData + Size}); |
| 595 | |
| 596 | TryDetectingAMemoryLeak(CurrentUnitData, Size, |
| 597 | /*DuringInitialCorpusExecution*/ false); |
| 598 | } |
| 599 | } |
| 600 | |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame^] | 601 | void Fuzzer::PurgeAllocator() { |
| 602 | if (Options.PurgeAllocatorIntervalSec < 0 || |
| 603 | !EF->__sanitizer_purge_allocator) { |
| 604 | return; |
| 605 | } |
| 606 | if (duration_cast<seconds>(system_clock::now() - |
| 607 | LastAllocatorPurgeAttemptTime).count() < |
| 608 | Options.PurgeAllocatorIntervalSec) { |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | if (Options.RssLimitMb <= 0 || |
| 613 | GetPeakRSSMb() > static_cast<size_t>(Options.RssLimitMb) / 2) { |
| 614 | EF->__sanitizer_purge_allocator(); |
| 615 | } |
| 616 | |
| 617 | LastAllocatorPurgeAttemptTime = system_clock::now(); |
| 618 | } |
| 619 | |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 620 | void Fuzzer::ReadAndExecuteSeedCorpora(const Vector<std::string> &CorpusDirs) { |
| 621 | const size_t kMaxSaneLen = 1 << 20; |
| 622 | const size_t kMinDefaultLen = 4096; |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 623 | Vector<SizedFile> SizedFiles; |
| 624 | size_t MaxSize = 0; |
| 625 | size_t MinSize = -1; |
| 626 | size_t TotalSize = 0; |
Kostya Serebryany | 93679be | 2017-09-12 21:58:07 | [diff] [blame] | 627 | size_t LastNumFiles = 0; |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 628 | for (auto &Dir : CorpusDirs) { |
Kostya Serebryany | 93679be | 2017-09-12 21:58:07 | [diff] [blame] | 629 | GetSizedFilesFromDir(Dir, &SizedFiles); |
| 630 | Printf("INFO: % 8zd files found in %s\n", SizedFiles.size() - LastNumFiles, |
| 631 | Dir.c_str()); |
| 632 | LastNumFiles = SizedFiles.size(); |
| 633 | } |
| 634 | for (auto &File : SizedFiles) { |
| 635 | MaxSize = Max(File.Size, MaxSize); |
| 636 | MinSize = Min(File.Size, MinSize); |
| 637 | TotalSize += File.Size; |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 638 | } |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 639 | if (Options.MaxLen == 0) |
| 640 | SetMaxInputLen(std::min(std::max(kMinDefaultLen, MaxSize), kMaxSaneLen)); |
| 641 | assert(MaxInputLen > 0); |
| 642 | |
Kostya Serebryany | 51823d3 | 2017-10-13 01:12:23 | [diff] [blame] | 643 | // Test the callback with empty input and never try it again. |
| 644 | uint8_t dummy = 0; |
| 645 | ExecuteCallback(&dummy, 0); |
| 646 | |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 647 | if (SizedFiles.empty()) { |
| 648 | Printf("INFO: A corpus is not provided, starting from an empty corpus\n"); |
| 649 | Unit U({'\n'}); // Valid ASCII input. |
| 650 | RunOne(U.data(), U.size()); |
| 651 | } else { |
| 652 | Printf("INFO: seed corpus: files: %zd min: %zdb max: %zdb total: %zdb" |
| 653 | " rss: %zdMb\n", |
| 654 | SizedFiles.size(), MinSize, MaxSize, TotalSize, GetPeakRSSMb()); |
| 655 | if (Options.ShuffleAtStartUp) |
| 656 | std::shuffle(SizedFiles.begin(), SizedFiles.end(), MD.GetRand()); |
| 657 | |
Kostya Serebryany | 93679be | 2017-09-12 21:58:07 | [diff] [blame] | 658 | if (Options.PreferSmall) { |
| 659 | std::stable_sort(SizedFiles.begin(), SizedFiles.end()); |
| 660 | assert(SizedFiles.front().Size <= SizedFiles.back().Size); |
| 661 | } |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 662 | |
| 663 | // Load and execute inputs one by one. |
| 664 | for (auto &SF : SizedFiles) { |
Kostya Serebryany | 082e9a7 | 2017-08-31 19:17:15 | [diff] [blame] | 665 | auto U = FileToVector(SF.File, MaxInputLen, /*ExitOnError=*/false); |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 666 | assert(U.size() <= MaxInputLen); |
| 667 | RunOne(U.data(), U.size()); |
| 668 | CheckExitOnSrcPosOrItem(); |
| 669 | TryDetectingAMemoryLeak(U.data(), U.size(), |
| 670 | /*DuringInitialCorpusExecution*/ true); |
| 671 | } |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 672 | } |
| 673 | |
Kostya Serebryany | 4faeb87 | 2017-08-29 20:51:24 | [diff] [blame] | 674 | |
| 675 | PrintStats("INITED"); |
| 676 | if (Corpus.empty()) { |
| 677 | Printf("ERROR: no interesting inputs were found. " |
| 678 | "Is the code instrumented for coverage? Exiting.\n"); |
| 679 | exit(1); |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 680 | } |
Kostya Serebryany | 3a8e3c8 | 2017-08-29 02:05:01 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void Fuzzer::Loop(const Vector<std::string> &CorpusDirs) { |
| 684 | ReadAndExecuteSeedCorpora(CorpusDirs); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 685 | TPC.SetPrintNewPCs(Options.PrintNewCovPcs); |
Kostya Serebryany | 2eef816 | 2017-08-25 20:09:25 | [diff] [blame] | 686 | TPC.SetPrintNewFuncs(Options.PrintNewCovFuncs); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 687 | system_clock::time_point LastCorpusReload = system_clock::now(); |
| 688 | if (Options.DoCrossOver) |
| 689 | MD.SetCorpus(&Corpus); |
| 690 | while (true) { |
| 691 | auto Now = system_clock::now(); |
| 692 | if (duration_cast<seconds>(Now - LastCorpusReload).count() >= |
| 693 | Options.ReloadIntervalSec) { |
| 694 | RereadOutputCorpus(MaxInputLen); |
| 695 | LastCorpusReload = system_clock::now(); |
| 696 | } |
| 697 | if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) |
| 698 | break; |
| 699 | if (TimedOut()) break; |
| 700 | |
| 701 | // Update TmpMaxMutationLen |
| 702 | if (Options.ExperimentalLenControl) { |
| 703 | if (TmpMaxMutationLen < MaxMutationLen && |
| 704 | (TotalNumberOfRuns - LastCorpusUpdateRun > 1000 && |
| 705 | duration_cast<seconds>(Now - LastCorpusUpdateTime).count() >= 1)) { |
| 706 | LastCorpusUpdateRun = TotalNumberOfRuns; |
| 707 | LastCorpusUpdateTime = Now; |
| 708 | TmpMaxMutationLen = |
| 709 | Min(MaxMutationLen, |
| 710 | TmpMaxMutationLen + Max(size_t(4), TmpMaxMutationLen / 8)); |
| 711 | if (TmpMaxMutationLen <= MaxMutationLen) |
| 712 | Printf("#%zd\tTEMP_MAX_LEN: %zd\n", TotalNumberOfRuns, |
| 713 | TmpMaxMutationLen); |
| 714 | } |
| 715 | } else { |
| 716 | TmpMaxMutationLen = MaxMutationLen; |
| 717 | } |
| 718 | |
| 719 | // Perform several mutations and runs. |
| 720 | MutateAndTestOne(); |
Alex Shlyapnikov | 6f1c26f | 2017-10-23 22:04:30 | [diff] [blame^] | 721 | |
| 722 | PurgeAllocator(); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | PrintStats("DONE ", "\n"); |
| 726 | MD.PrintRecommendedDictionary(); |
| 727 | } |
| 728 | |
| 729 | void Fuzzer::MinimizeCrashLoop(const Unit &U) { |
| 730 | if (U.size() <= 1) return; |
| 731 | while (!TimedOut() && TotalNumberOfRuns < Options.MaxNumberOfRuns) { |
| 732 | MD.StartMutationSequence(); |
| 733 | memcpy(CurrentUnitData, U.data(), U.size()); |
| 734 | for (int i = 0; i < Options.MutateDepth; i++) { |
| 735 | size_t NewSize = MD.Mutate(CurrentUnitData, U.size(), MaxMutationLen); |
| 736 | assert(NewSize > 0 && NewSize <= MaxMutationLen); |
| 737 | ExecuteCallback(CurrentUnitData, NewSize); |
| 738 | PrintPulseAndReportSlowInput(CurrentUnitData, NewSize); |
| 739 | TryDetectingAMemoryLeak(CurrentUnitData, NewSize, |
| 740 | /*DuringInitialCorpusExecution*/ false); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | void Fuzzer::AnnounceOutput(const uint8_t *Data, size_t Size) { |
| 746 | if (SMR.IsServer()) { |
| 747 | SMR.WriteByteArray(Data, Size); |
| 748 | } else if (SMR.IsClient()) { |
| 749 | SMR.PostClient(); |
| 750 | SMR.WaitServer(); |
| 751 | size_t OtherSize = SMR.ReadByteArraySize(); |
| 752 | uint8_t *OtherData = SMR.GetByteArray(); |
| 753 | if (Size != OtherSize || memcmp(Data, OtherData, Size) != 0) { |
| 754 | size_t i = 0; |
| 755 | for (i = 0; i < Min(Size, OtherSize); i++) |
| 756 | if (Data[i] != OtherData[i]) |
| 757 | break; |
| 758 | Printf("==%lu== ERROR: libFuzzer: equivalence-mismatch. Sizes: %zd %zd; " |
| 759 | "offset %zd\n", GetPid(), Size, OtherSize, i); |
| 760 | DumpCurrentUnit("mismatch-"); |
| 761 | Printf("SUMMARY: libFuzzer: equivalence-mismatch\n"); |
| 762 | PrintFinalStats(); |
| 763 | _Exit(Options.ErrorExitCode); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | } // namespace fuzzer |
| 769 | |
| 770 | extern "C" { |
| 771 | |
| 772 | size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) { |
| 773 | assert(fuzzer::F); |
| 774 | return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize); |
| 775 | } |
| 776 | |
| 777 | // Experimental |
| 778 | void LLVMFuzzerAnnounceOutput(const uint8_t *Data, size_t Size) { |
| 779 | assert(fuzzer::F); |
| 780 | fuzzer::F->AnnounceOutput(Data, Size); |
| 781 | } |
| 782 | } // extern "C" |