Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [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 "FuzzerInternal.h" |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 13 | #include <algorithm> |
Kostya Serebryany | d50a3ee | 2016-01-13 23:02:30 +0000 | [diff] [blame] | 14 | #include <cstring> |
| 15 | #include <memory> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 16 | |
Kostya Serebryany | 2a48c24 | 2015-11-13 01:54:40 +0000 | [diff] [blame] | 17 | #if defined(__has_include) |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 18 | #if __has_include(<sanitizer / coverage_interface.h>) |
| 19 | #include <sanitizer/coverage_interface.h> |
| 20 | #endif |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 21 | #if __has_include(<sanitizer / lsan_interface.h>) |
| 22 | #include <sanitizer/lsan_interface.h> |
| 23 | #endif |
Kostya Serebryany | 2a48c24 | 2015-11-13 01:54:40 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
Benjamin Kramer | d96b0c1 | 2016-03-18 14:19:19 +0000 | [diff] [blame] | 26 | #define NO_SANITIZE_MEMORY |
| 27 | #if defined(__has_feature) |
| 28 | #if __has_feature(memory_sanitizer) |
| 29 | #undef NO_SANITIZE_MEMORY |
| 30 | #define NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory)) |
| 31 | #endif |
| 32 | #endif |
| 33 | |
Kostya Serebryany | d6edce9 | 2015-10-16 23:04:31 +0000 | [diff] [blame] | 34 | extern "C" { |
Kostya Serebryany | 5eab74e | 2015-11-09 23:17:45 +0000 | [diff] [blame] | 35 | // Re-declare some of the sanitizer functions as "weak" so that |
Kostya Serebryany | 2a48c24 | 2015-11-13 01:54:40 +0000 | [diff] [blame] | 36 | // libFuzzer can be linked w/o the sanitizers and sanitizer-coverage |
Kostya Serebryany | 5eab74e | 2015-11-09 23:17:45 +0000 | [diff] [blame] | 37 | // (in which case it will complain at start-up time). |
Kostya Serebryany | d6edce9 | 2015-10-16 23:04:31 +0000 | [diff] [blame] | 38 | __attribute__((weak)) void __sanitizer_print_stack_trace(); |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 39 | __attribute__((weak)) void __sanitizer_reset_coverage(); |
Kostya Serebryany | 94660b3 | 2015-10-23 18:37:58 +0000 | [diff] [blame] | 40 | __attribute__((weak)) size_t __sanitizer_get_total_unique_caller_callee_pairs(); |
Kostya Serebryany | 5eab74e | 2015-11-09 23:17:45 +0000 | [diff] [blame] | 41 | __attribute__((weak)) size_t __sanitizer_get_total_unique_coverage(); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 42 | __attribute__((weak)) void |
| 43 | __sanitizer_set_death_callback(void (*callback)(void)); |
Kostya Serebryany | 5eab74e | 2015-11-09 23:17:45 +0000 | [diff] [blame] | 44 | __attribute__((weak)) size_t __sanitizer_get_number_of_counters(); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 45 | __attribute__((weak)) uintptr_t |
| 46 | __sanitizer_update_counter_bitset_and_clear_counters(uint8_t *bitset); |
Mike Aizatsky | 8b11f87 | 2016-01-06 00:21:22 +0000 | [diff] [blame] | 47 | __attribute__((weak)) uintptr_t |
| 48 | __sanitizer_get_coverage_pc_buffer(uintptr_t **data); |
Kostya Serebryany | 22cc5e2 | 2016-02-13 02:29:38 +0000 | [diff] [blame] | 49 | |
| 50 | __attribute__((weak)) size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, |
| 51 | size_t MaxSize, |
| 52 | unsigned int Seed); |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 53 | __attribute__((weak)) void __sanitizer_malloc_hook(void *ptr, size_t size); |
| 54 | __attribute__((weak)) void __sanitizer_free_hook(void *ptr); |
| 55 | __attribute__((weak)) void __lsan_enable(); |
| 56 | __attribute__((weak)) void __lsan_disable(); |
| 57 | __attribute__((weak)) int __lsan_do_recoverable_leak_check(); |
Kostya Serebryany | d6edce9 | 2015-10-16 23:04:31 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 60 | namespace fuzzer { |
Kostya Serebryany | a9da9b4 | 2015-10-16 22:47:20 +0000 | [diff] [blame] | 61 | static const size_t kMaxUnitSizeToPrint = 256; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 62 | |
Kostya Serebryany | 5eab74e | 2015-11-09 23:17:45 +0000 | [diff] [blame] | 63 | static void MissingWeakApiFunction(const char *FnName) { |
| 64 | Printf("ERROR: %s is not defined. Exiting.\n" |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 65 | "Did you use -fsanitize-coverage=... to build your code?\n", |
| 66 | FnName); |
Kostya Serebryany | 5eab74e | 2015-11-09 23:17:45 +0000 | [diff] [blame] | 67 | exit(1); |
| 68 | } |
| 69 | |
| 70 | #define CHECK_WEAK_API_FUNCTION(fn) \ |
| 71 | do { \ |
| 72 | if (!fn) \ |
| 73 | MissingWeakApiFunction(#fn); \ |
| 74 | } while (false) |
| 75 | |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 76 | // Only one Fuzzer per process. |
| 77 | static Fuzzer *F; |
| 78 | |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 79 | struct CoverageController { |
| 80 | static void Reset() { |
| 81 | CHECK_WEAK_API_FUNCTION(__sanitizer_reset_coverage); |
| 82 | __sanitizer_reset_coverage(); |
| 83 | PcMapResetCurrent(); |
| 84 | } |
| 85 | |
| 86 | static void ResetCounters(const Fuzzer::FuzzingOptions &Options) { |
| 87 | if (Options.UseCounters) { |
| 88 | __sanitizer_update_counter_bitset_and_clear_counters(0); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static void Prepare(const Fuzzer::FuzzingOptions &Options, |
| 93 | Fuzzer::Coverage *C) { |
| 94 | if (Options.UseCounters) { |
| 95 | size_t NumCounters = __sanitizer_get_number_of_counters(); |
| 96 | C->CounterBitmap.resize(NumCounters); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Records data to a maximum coverage tracker. Returns true if additional |
| 101 | // coverage was discovered. |
| 102 | static bool RecordMax(const Fuzzer::FuzzingOptions &Options, |
| 103 | Fuzzer::Coverage *C) { |
| 104 | bool Res = false; |
| 105 | |
| 106 | uint64_t NewBlockCoverage = __sanitizer_get_total_unique_coverage(); |
| 107 | if (NewBlockCoverage > C->BlockCoverage) { |
| 108 | Res = true; |
| 109 | C->BlockCoverage = NewBlockCoverage; |
| 110 | } |
| 111 | |
| 112 | if (Options.UseIndirCalls && |
| 113 | __sanitizer_get_total_unique_caller_callee_pairs) { |
| 114 | uint64_t NewCallerCalleeCoverage = |
| 115 | __sanitizer_get_total_unique_caller_callee_pairs(); |
| 116 | if (NewCallerCalleeCoverage > C->CallerCalleeCoverage) { |
| 117 | Res = true; |
| 118 | C->CallerCalleeCoverage = NewCallerCalleeCoverage; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (Options.UseCounters) { |
| 123 | uint64_t CounterDelta = |
| 124 | __sanitizer_update_counter_bitset_and_clear_counters( |
| 125 | C->CounterBitmap.data()); |
| 126 | if (CounterDelta > 0) { |
| 127 | Res = true; |
| 128 | C->CounterBitmapBits += CounterDelta; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | uint64_t NewPcMapBits = PcMapMergeInto(&C->PCMap); |
| 133 | if (NewPcMapBits > C->PcMapBits) { |
| 134 | Res = true; |
| 135 | C->PcMapBits = NewPcMapBits; |
| 136 | } |
| 137 | |
| 138 | uintptr_t *CoverageBuf; |
| 139 | uint64_t NewPcBufferLen = __sanitizer_get_coverage_pc_buffer(&CoverageBuf); |
| 140 | if (NewPcBufferLen > C->PcBufferLen) { |
| 141 | Res = true; |
| 142 | C->PcBufferLen = NewPcBufferLen; |
| 143 | } |
| 144 | |
| 145 | return Res; |
| 146 | } |
| 147 | }; |
| 148 | |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 149 | Fuzzer::Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options) |
| 150 | : CB(CB), MD(MD), Options(Options) { |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 151 | SetDeathCallback(); |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 152 | InitializeTraceState(); |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 153 | assert(!F); |
| 154 | F = this; |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 155 | ResetCoverage(); |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 156 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 157 | |
| 158 | void Fuzzer::SetDeathCallback() { |
Kostya Serebryany | 5eab74e | 2015-11-09 23:17:45 +0000 | [diff] [blame] | 159 | CHECK_WEAK_API_FUNCTION(__sanitizer_set_death_callback); |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 160 | __sanitizer_set_death_callback(StaticDeathCallback); |
| 161 | } |
| 162 | |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 163 | void Fuzzer::StaticDeathCallback() { |
| 164 | assert(F); |
| 165 | F->DeathCallback(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 168 | void Fuzzer::DumpCurrentUnit(const char *Prefix) { |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 169 | if (CurrentUnitSize <= kMaxUnitSizeToPrint) { |
| 170 | PrintHexArray(CurrentUnitData, CurrentUnitSize, "\n"); |
| 171 | PrintASCII(CurrentUnitData, CurrentUnitSize, "\n"); |
Kostya Serebryany | e95022a | 2015-10-09 04:03:14 +0000 | [diff] [blame] | 172 | } |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 173 | WriteUnitToFileWithPrefix( |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 174 | {CurrentUnitData, CurrentUnitData + CurrentUnitSize}, Prefix); |
| 175 | } |
| 176 | |
Benjamin Kramer | d96b0c1 | 2016-03-18 14:19:19 +0000 | [diff] [blame] | 177 | NO_SANITIZE_MEMORY |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 178 | void Fuzzer::DeathCallback() { |
| 179 | if (!CurrentUnitSize) return; |
| 180 | Printf("DEATH:\n"); |
| 181 | DumpCurrentUnit("crash-"); |
Kostya Serebryany | 66ff075 | 2016-02-26 22:42:23 +0000 | [diff] [blame] | 182 | PrintFinalStats(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 185 | void Fuzzer::StaticAlarmCallback() { |
| 186 | assert(F); |
| 187 | F->AlarmCallback(); |
| 188 | } |
| 189 | |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 190 | void Fuzzer::StaticCrashSignalCallback() { |
| 191 | assert(F); |
| 192 | F->CrashCallback(); |
| 193 | } |
| 194 | |
| 195 | void Fuzzer::StaticInterruptCallback() { |
| 196 | assert(F); |
| 197 | F->InterruptCallback(); |
| 198 | } |
| 199 | |
| 200 | void Fuzzer::CrashCallback() { |
| 201 | Printf("==%d== ERROR: libFuzzer: deadly signal\n", GetPid()); |
| 202 | if (__sanitizer_print_stack_trace) |
| 203 | __sanitizer_print_stack_trace(); |
| 204 | Printf("NOTE: libFuzzer has rudimentary signal handlers.\n" |
| 205 | " Combine libFuzzer with AddressSanitizer or similar for better " |
| 206 | "crash reports.\n"); |
| 207 | Printf("SUMMARY: libFuzzer: deadly signal\n"); |
| 208 | DumpCurrentUnit("crash-"); |
| 209 | PrintFinalStats(); |
| 210 | exit(Options.ErrorExitCode); |
| 211 | } |
| 212 | |
| 213 | void Fuzzer::InterruptCallback() { |
| 214 | Printf("==%d== libFuzzer: run interrupted; exiting\n", GetPid()); |
| 215 | PrintFinalStats(); |
Kostya Serebryany | e483ed2 | 2016-03-03 22:36:37 +0000 | [diff] [blame] | 216 | _Exit(0); // Stop right now, don't perform any at-exit actions. |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Benjamin Kramer | d96b0c1 | 2016-03-18 14:19:19 +0000 | [diff] [blame] | 219 | NO_SANITIZE_MEMORY |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 220 | void Fuzzer::AlarmCallback() { |
Kostya Serebryany | 490bbd6 | 2015-05-19 22:12:57 +0000 | [diff] [blame] | 221 | assert(Options.UnitTimeoutSec > 0); |
Kostya Serebryany | 8b8f7a3 | 2016-05-06 23:38:07 +0000 | [diff] [blame] | 222 | if (InOOMState) { |
| 223 | Printf("==%d== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n", |
| 224 | GetPid(), GetPeakRSSMb(), Options.RssLimitMb); |
| 225 | Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n"); |
| 226 | if (CurrentUnitSize && CurrentUnitData) { |
| 227 | DumpCurrentUnit("oom-"); |
| 228 | if (__sanitizer_print_stack_trace) |
| 229 | __sanitizer_print_stack_trace(); |
| 230 | } |
| 231 | Printf("SUMMARY: libFuzzer: out-of-memory\n"); |
| 232 | PrintFinalStats(); |
| 233 | _Exit(Options.ErrorExitCode); // Stop right now. |
| 234 | } |
| 235 | |
Kostya Serebryany | cfbcf90 | 2016-02-17 19:42:34 +0000 | [diff] [blame] | 236 | if (!CurrentUnitSize) |
| 237 | return; // We have not started running units yet. |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 238 | size_t Seconds = |
| 239 | duration_cast<seconds>(system_clock::now() - UnitStartTime).count(); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 240 | if (Seconds == 0) |
| 241 | return; |
Kostya Serebryany | 490bbd6 | 2015-05-19 22:12:57 +0000 | [diff] [blame] | 242 | if (Options.Verbosity >= 2) |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 243 | Printf("AlarmCallback %zd\n", Seconds); |
Kostya Serebryany | 490bbd6 | 2015-05-19 22:12:57 +0000 | [diff] [blame] | 244 | if (Seconds >= (size_t)Options.UnitTimeoutSec) { |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 245 | Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds); |
Kostya Serebryany | 316b571 | 2015-05-26 20:57:47 +0000 | [diff] [blame] | 246 | Printf(" and the timeout value is %d (use -timeout=N to change)\n", |
| 247 | Options.UnitTimeoutSec); |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 248 | DumpCurrentUnit("timeout-"); |
Kostya Serebryany | d6edce9 | 2015-10-16 23:04:31 +0000 | [diff] [blame] | 249 | Printf("==%d== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(), |
| 250 | Seconds); |
| 251 | if (__sanitizer_print_stack_trace) |
| 252 | __sanitizer_print_stack_trace(); |
| 253 | Printf("SUMMARY: libFuzzer: timeout\n"); |
Kostya Serebryany | 66ff075 | 2016-02-26 22:42:23 +0000 | [diff] [blame] | 254 | PrintFinalStats(); |
Kostya Serebryany | 3151673 | 2016-03-24 01:32:08 +0000 | [diff] [blame] | 255 | _Exit(Options.TimeoutExitCode); // Stop right now. |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 256 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Kostya Serebryany | 8b8f7a3 | 2016-05-06 23:38:07 +0000 | [diff] [blame] | 259 | void Fuzzer::RssLimitCallback() { |
| 260 | InOOMState = true; |
| 261 | SignalToMainThread(); |
| 262 | SleepSeconds(5); |
| 263 | Printf("Signal to main thread failed (non-linux?). Exiting.\n"); |
| 264 | _Exit(Options.ErrorExitCode); |
| 265 | return; |
Kostya Serebryany | 52b394e | 2016-05-06 21:58:35 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Kostya Serebryany | 09d2a5f | 2015-10-22 22:56:45 +0000 | [diff] [blame] | 268 | void Fuzzer::PrintStats(const char *Where, const char *End) { |
Kostya Serebryany | 66ff075 | 2016-02-26 22:42:23 +0000 | [diff] [blame] | 269 | size_t ExecPerSec = execPerSec(); |
Mike Aizatsky | a9c2387 | 2015-11-12 04:38:40 +0000 | [diff] [blame] | 270 | if (Options.OutputCSV) { |
| 271 | static bool csvHeaderPrinted = false; |
| 272 | if (!csvHeaderPrinted) { |
| 273 | csvHeaderPrinted = true; |
| 274 | Printf("runs,block_cov,bits,cc_cov,corpus,execs_per_sec,tbms,reason\n"); |
| 275 | } |
Kostya Serebryany | 9ba1918 | 2016-04-25 19:41:45 +0000 | [diff] [blame] | 276 | Printf("%zd,%zd,%zd,%zd,%zd,%zd,%s\n", TotalNumberOfRuns, |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 277 | MaxCoverage.BlockCoverage, MaxCoverage.CounterBitmapBits, |
| 278 | MaxCoverage.CallerCalleeCoverage, Corpus.size(), ExecPerSec, Where); |
Mike Aizatsky | a9c2387 | 2015-11-12 04:38:40 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (!Options.Verbosity) |
| 282 | return; |
Kostya Serebryany | 007c9b2 | 2015-10-22 22:50:47 +0000 | [diff] [blame] | 283 | Printf("#%zd\t%s", TotalNumberOfRuns, Where); |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 284 | if (MaxCoverage.BlockCoverage) |
| 285 | Printf(" cov: %zd", MaxCoverage.BlockCoverage); |
| 286 | if (MaxCoverage.PcMapBits) |
| 287 | Printf(" path: %zd", MaxCoverage.PcMapBits); |
| 288 | if (auto TB = MaxCoverage.CounterBitmapBits) |
Kostya Serebryany | 007c9b2 | 2015-10-22 22:50:47 +0000 | [diff] [blame] | 289 | Printf(" bits: %zd", TB); |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 290 | if (MaxCoverage.CallerCalleeCoverage) |
| 291 | Printf(" indir: %zd", MaxCoverage.CallerCalleeCoverage); |
Kostya Serebryany | 007c9b2 | 2015-10-22 22:50:47 +0000 | [diff] [blame] | 292 | Printf(" units: %zd exec/s: %zd", Corpus.size(), ExecPerSec); |
Kostya Serebryany | 12c7837 | 2015-08-12 01:55:37 +0000 | [diff] [blame] | 293 | Printf("%s", End); |
Kostya Serebryany | 03db8b9 | 2015-03-30 22:44:03 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Kostya Serebryany | 66ff075 | 2016-02-26 22:42:23 +0000 | [diff] [blame] | 296 | void Fuzzer::PrintFinalStats() { |
| 297 | if (!Options.PrintFinalStats) return; |
| 298 | size_t ExecPerSec = execPerSec(); |
| 299 | Printf("stat::number_of_executed_units: %zd\n", TotalNumberOfRuns); |
| 300 | Printf("stat::average_exec_per_sec: %zd\n", ExecPerSec); |
| 301 | Printf("stat::new_units_added: %zd\n", NumberOfNewUnitsAdded); |
| 302 | Printf("stat::slowest_unit_time_sec: %zd\n", TimeOfLongestUnitInSeconds); |
| 303 | Printf("stat::peak_rss_mb: %zd\n", GetPeakRSSMb()); |
| 304 | } |
| 305 | |
Kostya Serebryany | 64d2457 | 2016-03-12 01:57:04 +0000 | [diff] [blame] | 306 | size_t Fuzzer::MaxUnitSizeInCorpus() const { |
| 307 | size_t Res = 0; |
| 308 | for (auto &X : Corpus) |
| 309 | Res = std::max(Res, X.size()); |
| 310 | return Res; |
| 311 | } |
| 312 | |
| 313 | void Fuzzer::SetMaxLen(size_t MaxLen) { |
| 314 | assert(Options.MaxLen == 0); // Can only reset MaxLen from 0 to non-0. |
| 315 | assert(MaxLen); |
| 316 | Options.MaxLen = MaxLen; |
| 317 | Printf("INFO: -max_len is not provided, using %zd\n", Options.MaxLen); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | void Fuzzer::RereadOutputCorpus(size_t MaxSize) { |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 322 | if (Options.OutputCorpus.empty()) |
| 323 | return; |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 324 | std::vector<Unit> AdditionalCorpus; |
| 325 | ReadDirToVectorOfUnits(Options.OutputCorpus.c_str(), &AdditionalCorpus, |
Kostya Serebryany | 64d2457 | 2016-03-12 01:57:04 +0000 | [diff] [blame] | 326 | &EpochOfLastReadOfOutputCorpus, MaxSize); |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 327 | if (Corpus.empty()) { |
| 328 | Corpus = AdditionalCorpus; |
| 329 | return; |
| 330 | } |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 331 | if (!Options.Reload) |
| 332 | return; |
Kostya Serebryany | 490bbd6 | 2015-05-19 22:12:57 +0000 | [diff] [blame] | 333 | if (Options.Verbosity >= 2) |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 334 | Printf("Reload: read %zd new units.\n", AdditionalCorpus.size()); |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 335 | for (auto &X : AdditionalCorpus) { |
Kostya Serebryany | 64d2457 | 2016-03-12 01:57:04 +0000 | [diff] [blame] | 336 | if (X.size() > MaxSize) |
| 337 | X.resize(MaxSize); |
Kostya Serebryany | cbb2334 | 2015-05-19 01:06:07 +0000 | [diff] [blame] | 338 | if (UnitHashesAddedToCorpus.insert(Hash(X)).second) { |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 339 | if (RunOne(X)) { |
Kostya Serebryany | cbb2334 | 2015-05-19 01:06:07 +0000 | [diff] [blame] | 340 | Corpus.push_back(X); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 341 | UpdateCorpusDistribution(); |
Mike Aizatsky | a9c2387 | 2015-11-12 04:38:40 +0000 | [diff] [blame] | 342 | PrintStats("RELOAD"); |
Kostya Serebryany | cbb2334 | 2015-05-19 01:06:07 +0000 | [diff] [blame] | 343 | } |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 348 | void Fuzzer::ShuffleCorpus(UnitVector *V) { |
| 349 | std::random_shuffle(V->begin(), V->end(), MD.GetRand()); |
| 350 | if (Options.PreferSmall) |
| 351 | std::stable_sort(V->begin(), V->end(), [](const Unit &A, const Unit &B) { |
| 352 | return A.size() < B.size(); |
| 353 | }); |
| 354 | } |
| 355 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 356 | void Fuzzer::ShuffleAndMinimize() { |
Kostya Serebryany | 09d2a5f | 2015-10-22 22:56:45 +0000 | [diff] [blame] | 357 | PrintStats("READ "); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 358 | std::vector<Unit> NewCorpus; |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 359 | if (Options.ShuffleAtStartUp) |
| 360 | ShuffleCorpus(&Corpus); |
| 361 | |
| 362 | for (const auto &U : Corpus) { |
| 363 | if (RunOne(U)) { |
| 364 | NewCorpus.push_back(U); |
| 365 | if (Options.Verbosity >= 2) |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 366 | Printf("NEW0: %zd L %zd\n", MaxCoverage.BlockCoverage, U.size()); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | Corpus = NewCorpus; |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 370 | UpdateCorpusDistribution(); |
Kostya Serebryany | cbb2334 | 2015-05-19 01:06:07 +0000 | [diff] [blame] | 371 | for (auto &X : Corpus) |
| 372 | UnitHashesAddedToCorpus.insert(Hash(X)); |
Kostya Serebryany | 09d2a5f | 2015-10-22 22:56:45 +0000 | [diff] [blame] | 373 | PrintStats("INITED"); |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 374 | CheckForMemoryLeaks(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 377 | bool Fuzzer::UpdateMaxCoverage() { |
| 378 | uintptr_t PrevBufferLen = MaxCoverage.PcBufferLen; |
| 379 | bool Res = CoverageController::RecordMax(Options, &MaxCoverage); |
| 380 | |
| 381 | if (Options.PrintNewCovPcs && PrevBufferLen != MaxCoverage.PcBufferLen) { |
| 382 | uintptr_t *CoverageBuf; |
| 383 | __sanitizer_get_coverage_pc_buffer(&CoverageBuf); |
| 384 | assert(CoverageBuf); |
| 385 | for (size_t I = PrevBufferLen; I < MaxCoverage.PcBufferLen; ++I) { |
| 386 | Printf("%p\n", CoverageBuf[I]); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return Res; |
| 391 | } |
| 392 | |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 393 | bool Fuzzer::RunOne(const uint8_t *Data, size_t Size) { |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 394 | TotalNumberOfRuns++; |
Kostya Serebryany | 007c9b2 | 2015-10-22 22:50:47 +0000 | [diff] [blame] | 395 | |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 396 | // TODO(aizatsky): this Reset call seems to be not needed. |
| 397 | CoverageController::ResetCounters(Options); |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 398 | ExecuteCallback(Data, Size); |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 399 | bool Res = UpdateMaxCoverage(); |
Kostya Serebryany | 007c9b2 | 2015-10-22 22:50:47 +0000 | [diff] [blame] | 400 | |
Kostya Serebryany | 16901a9 | 2015-03-30 23:04:35 +0000 | [diff] [blame] | 401 | auto UnitStopTime = system_clock::now(); |
| 402 | auto TimeOfUnit = |
| 403 | duration_cast<seconds>(UnitStopTime - UnitStartTime).count(); |
Mike Aizatsky | a9c2387 | 2015-11-12 04:38:40 +0000 | [diff] [blame] | 404 | if (!(TotalNumberOfRuns & (TotalNumberOfRuns - 1)) && |
| 405 | secondsSinceProcessStartUp() >= 2) |
Kostya Serebryany | 09d2a5f | 2015-10-22 22:56:45 +0000 | [diff] [blame] | 406 | PrintStats("pulse "); |
Kostya Serebryany | 70926ae | 2015-08-05 21:43:48 +0000 | [diff] [blame] | 407 | if (TimeOfUnit > TimeOfLongestUnitInSeconds && |
| 408 | TimeOfUnit >= Options.ReportSlowUnits) { |
Kostya Serebryany | 16901a9 | 2015-03-30 23:04:35 +0000 | [diff] [blame] | 409 | TimeOfLongestUnitInSeconds = TimeOfUnit; |
Kostya Serebryany | 70926ae | 2015-08-05 21:43:48 +0000 | [diff] [blame] | 410 | Printf("Slowest unit: %zd s:\n", TimeOfLongestUnitInSeconds); |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 411 | WriteUnitToFileWithPrefix({Data, Data + Size}, "slow-unit-"); |
Kostya Serebryany | 16901a9 | 2015-03-30 23:04:35 +0000 | [diff] [blame] | 412 | } |
| 413 | return Res; |
Kostya Serebryany | 2c1b33b | 2015-01-29 23:01:07 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 416 | void Fuzzer::RunOneAndUpdateCorpus(uint8_t *Data, size_t Size) { |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 417 | if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) |
| 418 | return; |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 419 | if (Options.OnlyASCII) |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 420 | ToASCII(Data, Size); |
| 421 | if (RunOne(Data, Size)) |
| 422 | ReportNewCoverage({Data, Data + Size}); |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 425 | // Leak detection is expensive, so we first check if there were more mallocs |
| 426 | // than frees (using the sanitizer malloc hooks) and only then try to call lsan. |
| 427 | struct MallocFreeTracer { |
| 428 | void Start() { |
| 429 | Mallocs = 0; |
| 430 | Frees = 0; |
| 431 | } |
| 432 | // Returns true if there were more mallocs than frees. |
| 433 | bool Stop() { return Mallocs > Frees; } |
| 434 | size_t Mallocs; |
| 435 | size_t Frees; |
| 436 | }; |
| 437 | |
| 438 | static thread_local MallocFreeTracer AllocTracer; |
| 439 | |
Dan Liew | 3868e46 | 2016-05-19 22:00:33 +0000 | [diff] [blame^] | 440 | // FIXME: The hooks only count on Linux because |
| 441 | // on Mac OSX calls to malloc are intercepted before |
| 442 | // thread local storage is initialised leading to |
| 443 | // crashes when accessing ``AllocTracer``. |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 444 | extern "C" { |
Dan Liew | 3868e46 | 2016-05-19 22:00:33 +0000 | [diff] [blame^] | 445 | void __sanitizer_malloc_hook(void *ptr, size_t size) { |
| 446 | if (!LIBFUZZER_APPLE) |
| 447 | AllocTracer.Mallocs++; |
| 448 | } |
| 449 | void __sanitizer_free_hook(void *ptr) { |
| 450 | if (!LIBFUZZER_APPLE) |
| 451 | AllocTracer.Frees++; |
| 452 | } |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 453 | } // extern "C" |
| 454 | |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 455 | void Fuzzer::ExecuteCallback(const uint8_t *Data, size_t Size) { |
Kostya Serebryany | ebb932d | 2016-04-18 22:50:39 +0000 | [diff] [blame] | 456 | UnitStartTime = system_clock::now(); |
Kostya Serebryany | d50a3ee | 2016-01-13 23:02:30 +0000 | [diff] [blame] | 457 | // We copy the contents of Unit into a separate heap buffer |
| 458 | // so that we reliably find buffer overflows in it. |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 459 | std::unique_ptr<uint8_t[]> DataCopy(new uint8_t[Size]); |
| 460 | memcpy(DataCopy.get(), Data, Size); |
| 461 | AssignTaintLabels(DataCopy.get(), Size); |
| 462 | CurrentUnitData = DataCopy.get(); |
| 463 | CurrentUnitSize = Size; |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 464 | AllocTracer.Start(); |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 465 | int Res = CB(DataCopy.get(), Size); |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 466 | (void)Res; |
| 467 | HasMoreMallocsThanFrees = AllocTracer.Stop(); |
Kostya Serebryany | ebb932d | 2016-04-18 22:50:39 +0000 | [diff] [blame] | 468 | CurrentUnitSize = 0; |
| 469 | CurrentUnitData = nullptr; |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 470 | assert(Res == 0); |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 473 | std::string Fuzzer::Coverage::DebugString() const { |
| 474 | std::string Result = |
| 475 | std::string("Coverage{") + "BlockCoverage=" + |
| 476 | std::to_string(BlockCoverage) + " CallerCalleeCoverage=" + |
| 477 | std::to_string(CallerCalleeCoverage) + " CounterBitmapBits=" + |
| 478 | std::to_string(CounterBitmapBits) + " PcMapBits=" + |
| 479 | std::to_string(PcMapBits) + "}"; |
| 480 | return Result; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void Fuzzer::WriteToOutputCorpus(const Unit &U) { |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 484 | if (Options.OutputCorpus.empty()) |
| 485 | return; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 486 | std::string Path = DirPlusFile(Options.OutputCorpus, Hash(U)); |
| 487 | WriteToFile(U, Path); |
| 488 | if (Options.Verbosity >= 2) |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 489 | Printf("Written to %s\n", Path.c_str()); |
Kostya Serebryany | a9346c2 | 2015-09-02 19:08:08 +0000 | [diff] [blame] | 490 | assert(!Options.OnlyASCII || IsASCII(U)); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Kostya Serebryany | 2b7d2e9 | 2015-07-23 18:37:22 +0000 | [diff] [blame] | 493 | void Fuzzer::WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix) { |
Kostya Serebryany | b91c62b | 2015-10-16 22:41:47 +0000 | [diff] [blame] | 494 | if (!Options.SaveArtifacts) |
| 495 | return; |
Kostya Serebryany | bd5d1cd | 2015-10-09 03:57:59 +0000 | [diff] [blame] | 496 | std::string Path = Options.ArtifactPrefix + Prefix + Hash(U); |
Kostya Serebryany | 2d0ef14 | 2015-11-25 21:40:46 +0000 | [diff] [blame] | 497 | if (!Options.ExactArtifactPath.empty()) |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 498 | Path = Options.ExactArtifactPath; // Overrides ArtifactPrefix. |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 499 | WriteToFile(U, Path); |
Kostya Serebryany | bd5d1cd | 2015-10-09 03:57:59 +0000 | [diff] [blame] | 500 | Printf("artifact_prefix='%s'; Test unit written to %s\n", |
| 501 | Options.ArtifactPrefix.c_str(), Path.c_str()); |
Kostya Serebryany | 9e48cda | 2015-12-04 22:29:39 +0000 | [diff] [blame] | 502 | if (U.size() <= kMaxUnitSizeToPrint) |
| 503 | Printf("Base64: %s\n", Base64(U).c_str()); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void Fuzzer::SaveCorpus() { |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 507 | if (Options.OutputCorpus.empty()) |
| 508 | return; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 509 | for (const auto &U : Corpus) |
| 510 | WriteToFile(U, DirPlusFile(Options.OutputCorpus, Hash(U))); |
| 511 | if (Options.Verbosity) |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 512 | Printf("Written corpus of %zd files to %s\n", Corpus.size(), |
| 513 | Options.OutputCorpus.c_str()); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 516 | void Fuzzer::PrintStatusForNewUnit(const Unit &U) { |
| 517 | if (!Options.PrintNEW) |
| 518 | return; |
Kostya Serebryany | 09d2a5f | 2015-10-22 22:56:45 +0000 | [diff] [blame] | 519 | PrintStats("NEW ", ""); |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 520 | if (Options.Verbosity) { |
Kostya Serebryany | 14c5028 | 2015-12-19 01:09:49 +0000 | [diff] [blame] | 521 | Printf(" L: %zd ", U.size()); |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 522 | MD.PrintMutationSequence(); |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 523 | Printf("\n"); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 524 | } |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | void Fuzzer::ReportNewCoverage(const Unit &U) { |
| 528 | Corpus.push_back(U); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 529 | UpdateCorpusDistribution(); |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 530 | UnitHashesAddedToCorpus.insert(Hash(U)); |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 531 | MD.RecordSuccessfulMutationSequence(); |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 532 | PrintStatusForNewUnit(U); |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 533 | WriteToOutputCorpus(U); |
Kostya Serebryany | 66ff075 | 2016-02-26 22:42:23 +0000 | [diff] [blame] | 534 | NumberOfNewUnitsAdded++; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 537 | // Finds minimal number of units in 'Extra' that add coverage to 'Initial'. |
| 538 | // We do it by actually executing the units, sometimes more than once, |
| 539 | // because we may be using different coverage-like signals and the only |
| 540 | // common thing between them is that we can say "this unit found new stuff". |
| 541 | UnitVector Fuzzer::FindExtraUnits(const UnitVector &Initial, |
| 542 | const UnitVector &Extra) { |
| 543 | UnitVector Res = Extra; |
| 544 | size_t OldSize = Res.size(); |
| 545 | for (int Iter = 0; Iter < 10; Iter++) { |
| 546 | ShuffleCorpus(&Res); |
| 547 | ResetCoverage(); |
| 548 | |
| 549 | for (auto &U : Initial) |
| 550 | RunOne(U); |
| 551 | |
| 552 | Corpus.clear(); |
| 553 | for (auto &U : Res) |
| 554 | if (RunOne(U)) |
| 555 | Corpus.push_back(U); |
| 556 | |
| 557 | char Stat[7] = "MIN "; |
| 558 | Stat[3] = '0' + Iter; |
| 559 | PrintStats(Stat); |
| 560 | |
| 561 | size_t NewSize = Corpus.size(); |
Kostya Serebryany | a0788e7 | 2016-05-13 22:11:23 +0000 | [diff] [blame] | 562 | assert(NewSize <= OldSize); |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 563 | Res.swap(Corpus); |
| 564 | |
Kostya Serebryany | a0788e7 | 2016-05-13 22:11:23 +0000 | [diff] [blame] | 565 | if (NewSize + 5 >= OldSize) |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 566 | break; |
| 567 | OldSize = NewSize; |
| 568 | } |
| 569 | return Res; |
| 570 | } |
| 571 | |
Kostya Serebryany | 9cc3b0d | 2015-10-24 01:16:40 +0000 | [diff] [blame] | 572 | void Fuzzer::Merge(const std::vector<std::string> &Corpora) { |
| 573 | if (Corpora.size() <= 1) { |
| 574 | Printf("Merge requires two or more corpus dirs\n"); |
| 575 | return; |
| 576 | } |
Kostya Serebryany | 9cc3b0d | 2015-10-24 01:16:40 +0000 | [diff] [blame] | 577 | std::vector<std::string> ExtraCorpora(Corpora.begin() + 1, Corpora.end()); |
| 578 | |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 579 | assert(Options.MaxLen > 0); |
| 580 | UnitVector Initial, Extra; |
| 581 | ReadDirToVectorOfUnits(Corpora[0].c_str(), &Initial, nullptr, Options.MaxLen); |
| 582 | for (auto &C : ExtraCorpora) |
| 583 | ReadDirToVectorOfUnits(C.c_str(), &Extra, nullptr, Options.MaxLen); |
| 584 | |
| 585 | if (!Initial.empty()) { |
| 586 | Printf("=== Minimizing the initial corpus of %zd units\n", Initial.size()); |
| 587 | Initial = FindExtraUnits({}, Initial); |
Kostya Serebryany | 9cc3b0d | 2015-10-24 01:16:40 +0000 | [diff] [blame] | 588 | } |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 589 | |
| 590 | Printf("=== Merging extra %zd units\n", Extra.size()); |
| 591 | auto Res = FindExtraUnits(Initial, Extra); |
| 592 | |
| 593 | for (auto &U: Res) |
| 594 | WriteToOutputCorpus(U); |
| 595 | |
| 596 | Printf("=== Merge: written %zd units\n", Res.size()); |
Kostya Serebryany | 9cc3b0d | 2015-10-24 01:16:40 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 599 | // Tries to call lsan, and if there are leaks exits. We call this right after |
| 600 | // the initial corpus was read because if there are leaky inputs in the corpus |
| 601 | // further fuzzing will likely hit OOMs. |
| 602 | void Fuzzer::CheckForMemoryLeaks() { |
| 603 | if (!Options.DetectLeaks) return; |
| 604 | if (!__lsan_do_recoverable_leak_check) |
| 605 | return; |
| 606 | if (__lsan_do_recoverable_leak_check()) { |
| 607 | Printf("==%d== ERROR: libFuzzer: initial corpus triggers memory leaks.\n" |
| 608 | "Exiting now. Use -detect_leaks=0 to disable leak detection here.\n" |
| 609 | "LeakSanitizer will still check for leaks at the process exit.\n", |
| 610 | GetPid()); |
| 611 | PrintFinalStats(); |
| 612 | _Exit(Options.ErrorExitCode); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // Tries detecting a memory leak on the particular input that we have just |
| 617 | // executed before calling this function. |
| 618 | void Fuzzer::TryDetectingAMemoryLeak(uint8_t *Data, size_t Size) { |
| 619 | if (!HasMoreMallocsThanFrees) return; // mallocs==frees, a leak is unlikely. |
| 620 | if (!Options.DetectLeaks) return; |
| 621 | if (!&__lsan_enable || !&__lsan_disable || !__lsan_do_recoverable_leak_check) |
| 622 | return; // No lsan. |
| 623 | // Run the target once again, but with lsan disabled so that if there is |
| 624 | // a real leak we do not report it twice. |
| 625 | __lsan_disable(); |
| 626 | RunOneAndUpdateCorpus(Data, Size); |
| 627 | __lsan_enable(); |
| 628 | if (!HasMoreMallocsThanFrees) return; // a leak is unlikely. |
Kostya Serebryany | 7018a1a | 2016-04-27 19:52:34 +0000 | [diff] [blame] | 629 | if (NumberOfLeakDetectionAttempts++ > 1000) { |
| 630 | Options.DetectLeaks = false; |
| 631 | Printf("INFO: libFuzzer disabled leak detection after every mutation.\n" |
| 632 | " Most likely the target function accumulates allocated\n" |
| 633 | " memory in a global state w/o actually leaking it.\n" |
| 634 | " If LeakSanitizer is enabled in this process it will still\n" |
| 635 | " run on the process shutdown.\n"); |
| 636 | return; |
| 637 | } |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 638 | // Now perform the actual lsan pass. This is expensive and we must ensure |
| 639 | // we don't call it too often. |
| 640 | if (__lsan_do_recoverable_leak_check()) { // Leak is found, report it. |
| 641 | CurrentUnitData = Data; |
| 642 | CurrentUnitSize = Size; |
| 643 | DumpCurrentUnit("leak-"); |
| 644 | PrintFinalStats(); |
| 645 | _Exit(Options.ErrorExitCode); // not exit() to disable lsan further on. |
| 646 | } |
| 647 | } |
| 648 | |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 649 | void Fuzzer::MutateAndTestOne() { |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 650 | MD.StartMutationSequence(); |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 651 | |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 652 | auto &U = ChooseUnitToMutate(); |
| 653 | MutateInPlaceHere.resize(Options.MaxLen); |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 654 | size_t Size = U.size(); |
Mike Aizatsky | 94e2966 | 2016-04-08 23:32:24 +0000 | [diff] [blame] | 655 | assert(Size <= Options.MaxLen && "Oversized Unit"); |
| 656 | memcpy(MutateInPlaceHere.data(), U.data(), Size); |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 657 | |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 658 | for (int i = 0; i < Options.MutateDepth; i++) { |
Kostya Serebryany | 22cc5e2 | 2016-02-13 02:29:38 +0000 | [diff] [blame] | 659 | size_t NewSize = 0; |
| 660 | if (LLVMFuzzerCustomMutator) |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 661 | NewSize = LLVMFuzzerCustomMutator(MutateInPlaceHere.data(), Size, |
| 662 | Options.MaxLen, MD.GetRand().Rand()); |
Kostya Serebryany | 22cc5e2 | 2016-02-13 02:29:38 +0000 | [diff] [blame] | 663 | else |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 664 | NewSize = MD.Mutate(MutateInPlaceHere.data(), Size, Options.MaxLen); |
Kostya Serebryany | 2ea204e | 2015-05-30 17:33:13 +0000 | [diff] [blame] | 665 | assert(NewSize > 0 && "Mutator returned empty unit"); |
Kostya Serebryany | 64d2457 | 2016-03-12 01:57:04 +0000 | [diff] [blame] | 666 | assert(NewSize <= Options.MaxLen && |
Kostya Serebryany | 2ea204e | 2015-05-30 17:33:13 +0000 | [diff] [blame] | 667 | "Mutator return overisized unit"); |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 668 | Size = NewSize; |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 669 | if (i == 0) |
| 670 | StartTraceRecording(); |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 671 | RunOneAndUpdateCorpus(MutateInPlaceHere.data(), Size); |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 672 | StopTraceRecording(); |
Kostya Serebryany | 1bfd583 | 2016-04-20 00:24:21 +0000 | [diff] [blame] | 673 | TryDetectingAMemoryLeak(MutateInPlaceHere.data(), Size); |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Kostya Serebryany | e692621 | 2015-11-04 23:22:25 +0000 | [diff] [blame] | 677 | // Returns an index of random unit from the corpus to mutate. |
| 678 | // Hypothesis: units added to the corpus last are more likely to be interesting. |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 679 | // This function gives more weight to the more recent units. |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 680 | size_t Fuzzer::ChooseUnitIdxToMutate() { |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 681 | size_t Idx = |
| 682 | static_cast<size_t>(CorpusDistribution(MD.GetRand().Get_mt19937())); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 683 | assert(Idx < Corpus.size()); |
| 684 | return Idx; |
Kostya Serebryany | e692621 | 2015-11-04 23:22:25 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 687 | void Fuzzer::ResetCoverage() { |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 688 | CoverageController::Reset(); |
| 689 | MaxCoverage.Reset(); |
| 690 | CoverageController::Prepare(Options, &MaxCoverage); |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 693 | // Experimental search heuristic: drilling. |
| 694 | // - Read, shuffle, execute and minimize the corpus. |
| 695 | // - Choose one random unit. |
| 696 | // - Reset the coverage. |
| 697 | // - Start fuzzing as if the chosen unit was the only element of the corpus. |
| 698 | // - When done, reset the coverage again. |
| 699 | // - Merge the newly created corpus into the original one. |
| 700 | void Fuzzer::Drill() { |
| 701 | // The corpus is already read, shuffled, and minimized. |
| 702 | assert(!Corpus.empty()); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 703 | Options.PrintNEW = false; // Don't print NEW status lines when drilling. |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 704 | |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 705 | Unit U = ChooseUnitToMutate(); |
| 706 | |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 707 | ResetCoverage(); |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 708 | |
| 709 | std::vector<Unit> SavedCorpus; |
| 710 | SavedCorpus.swap(Corpus); |
| 711 | Corpus.push_back(U); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 712 | UpdateCorpusDistribution(); |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 713 | assert(Corpus.size() == 1); |
| 714 | RunOne(U); |
| 715 | PrintStats("DRILL "); |
| 716 | std::string SavedOutputCorpusPath; // Don't write new units while drilling. |
| 717 | SavedOutputCorpusPath.swap(Options.OutputCorpus); |
| 718 | Loop(); |
| 719 | |
Kostya Serebryany | 945761b | 2016-03-18 00:23:29 +0000 | [diff] [blame] | 720 | ResetCoverage(); |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 721 | |
| 722 | PrintStats("REINIT"); |
| 723 | SavedOutputCorpusPath.swap(Options.OutputCorpus); |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 724 | for (auto &U : SavedCorpus) |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 725 | RunOne(U); |
| 726 | PrintStats("MERGE "); |
| 727 | Options.PrintNEW = true; |
| 728 | size_t NumMerged = 0; |
| 729 | for (auto &U : Corpus) { |
| 730 | if (RunOne(U)) { |
| 731 | PrintStatusForNewUnit(U); |
| 732 | NumMerged++; |
| 733 | WriteToOutputCorpus(U); |
| 734 | } |
| 735 | } |
| 736 | PrintStats("MERGED"); |
| 737 | if (NumMerged && Options.Verbosity) |
| 738 | Printf("Drilling discovered %zd new units\n", NumMerged); |
| 739 | } |
| 740 | |
| 741 | void Fuzzer::Loop() { |
Kostya Serebryany | 8617aaa | 2015-12-05 02:09:22 +0000 | [diff] [blame] | 742 | system_clock::time_point LastCorpusReload = system_clock::now(); |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 743 | if (Options.DoCrossOver) |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 744 | MD.SetCorpus(&Corpus); |
Kostya Serebryany | 468ed78 | 2015-09-08 17:30:35 +0000 | [diff] [blame] | 745 | while (true) { |
Kostya Serebryany | 8617aaa | 2015-12-05 02:09:22 +0000 | [diff] [blame] | 746 | auto Now = system_clock::now(); |
| 747 | if (duration_cast<seconds>(Now - LastCorpusReload).count()) { |
Kostya Serebryany | 64d2457 | 2016-03-12 01:57:04 +0000 | [diff] [blame] | 748 | RereadOutputCorpus(Options.MaxLen); |
Kostya Serebryany | 8617aaa | 2015-12-05 02:09:22 +0000 | [diff] [blame] | 749 | LastCorpusReload = Now; |
| 750 | } |
Kostya Serebryany | e692621 | 2015-11-04 23:22:25 +0000 | [diff] [blame] | 751 | if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) |
Mike Aizatsky | a9c2387 | 2015-11-12 04:38:40 +0000 | [diff] [blame] | 752 | break; |
Kostya Serebryany | e692621 | 2015-11-04 23:22:25 +0000 | [diff] [blame] | 753 | if (Options.MaxTotalTimeSec > 0 && |
| 754 | secondsSinceProcessStartUp() > |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 755 | static_cast<size_t>(Options.MaxTotalTimeSec)) |
Mike Aizatsky | a9c2387 | 2015-11-12 04:38:40 +0000 | [diff] [blame] | 756 | break; |
Kostya Serebryany | e692621 | 2015-11-04 23:22:25 +0000 | [diff] [blame] | 757 | // Perform several mutations and runs. |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 758 | MutateAndTestOne(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 759 | } |
Mike Aizatsky | a9c2387 | 2015-11-12 04:38:40 +0000 | [diff] [blame] | 760 | |
| 761 | PrintStats("DONE ", "\n"); |
Kostya Serebryany | 7ec0c56 | 2016-02-13 03:25:16 +0000 | [diff] [blame] | 762 | MD.PrintRecommendedDictionary(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 765 | void Fuzzer::UpdateCorpusDistribution() { |
| 766 | size_t N = Corpus.size(); |
| 767 | std::vector<double> Intervals(N + 1); |
| 768 | std::vector<double> Weights(N); |
| 769 | std::iota(Intervals.begin(), Intervals.end(), 0); |
| 770 | std::iota(Weights.begin(), Weights.end(), 1); |
| 771 | CorpusDistribution = std::piecewise_constant_distribution<double>( |
| 772 | Intervals.begin(), Intervals.end(), Weights.begin()); |
| 773 | } |
| 774 | |
| 775 | } // namespace fuzzer |
Kostya Serebryany | 8b0d90a | 2016-05-13 18:04:35 +0000 | [diff] [blame] | 776 | |
| 777 | extern "C" { |
| 778 | |
| 779 | size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) { |
| 780 | assert(fuzzer::F); |
| 781 | return fuzzer::F->GetMD().Mutate(Data, Size, MaxSize); |
| 782 | } |
| 783 | } // extern "C" |