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