display.c revision 0edf8249daf89b4ed446eaf505445dc40a2dcf2f
1/* 2 * 3 * honggfuzz - display statistics 4 * ----------------------------------------- 5 * 6 * Author: Robert Swiecki <swiecki@google.com> 7 * 8 * Copyright 2010-2015 by Google Inc. All Rights Reserved. 9 * 10 * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 * not use this file except in compliance with the License. You may obtain 12 * a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 19 * implied. See the License for the specific language governing 20 * permissions and limitations under the License. 21 * 22 */ 23 24#define _WITH_DPRINTF 25 26#include "common.h" 27#include "display.h" 28 29#include <inttypes.h> 30#include <math.h> 31#include <string.h> 32#include <stdarg.h> 33#include <stdio.h> 34#include <unistd.h> 35 36#include "log.h" 37#include "util.h" 38 39#define ESC_CLEAR "\033[H\033[2J" 40#define ESC_NAV(x,y) "\033["#x";"#y"H" 41#define ESC_BOLD "\033[1m" 42#define ESC_RED "\033[31m" 43#define ESC_RESET "\033[0m" 44 45#if defined(_HF_ARCH_LINUX) 46#define _HF_MONETARY_MOD "'" 47#else 48#define _HF_MONETARY_MOD "" 49#endif 50 51static void display_put(const char *fmt, ...) 52{ 53 va_list args; 54 va_start(args, fmt); 55 vfprintf(stdout, fmt, args); 56 va_end(args); 57 fflush(stdout); 58} 59 60static void display_printKMG(uint64_t val) 61{ 62 if (val >= 1000000000UL) { 63 display_put(" [%.2lfG]", (double)val / 1000000000.0); 64 } else if (val >= 1000000UL) { 65 display_put(" [%.2lfM]", (double)val / 1000000.0); 66 } else if (val >= 1000UL) { 67 display_put(" [%.2lfk]", (double)val / 1000.0); 68 } 69} 70 71static unsigned getCpuUse(long num_cpu) 72{ 73 static uint64_t prevIdleT = 0UL; 74 75 FILE *f = fopen("/proc/stat", "re"); 76 if (f == NULL) { 77 return 0; 78 } 79 defer { 80 fclose(f); 81 }; 82 uint64_t userT, niceT, systemT, idleT; 83 if (fscanf 84 (f, "cpu %" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64, &userT, &niceT, &systemT, 85 &idleT) != 4) { 86 LOG_W("fscanf('/proc/stat') != 4"); 87 return 0; 88 } 89 90 if (prevIdleT == 0UL) { 91 prevIdleT = idleT; 92 return 0; 93 } 94 95 uint64_t cpuUse = (num_cpu * sysconf(_SC_CLK_TCK)) - (idleT - prevIdleT); 96 prevIdleT = idleT; 97 return cpuUse * 100 / sysconf(_SC_CLK_TCK); 98} 99 100static void display_displayLocked(honggfuzz_t * hfuzz) 101{ 102 unsigned long elapsed_second = (unsigned long)(time(NULL) - hfuzz->timeStart); 103 unsigned int day, hour, min, second; 104 char time_elapsed_str[64]; 105 if (elapsed_second < 24 * 3600) { 106 hour = elapsed_second / 3600; 107 min = (elapsed_second - 3600 * hour) / 60; 108 second = elapsed_second - hour * 3600 - min * 60; 109 snprintf(time_elapsed_str, sizeof(time_elapsed_str), "%u hrs %u min %u sec", hour, 110 min, second); 111 } else { 112 day = elapsed_second / 24 / 3600; 113 elapsed_second = elapsed_second - day * 24 * 3600; 114 hour = elapsed_second / 3600; 115 min = (elapsed_second - 3600 * hour) / 60; 116 second = elapsed_second - hour * 3600 - min * 60; 117 snprintf(time_elapsed_str, sizeof(time_elapsed_str), 118 "%u days %u hrs %u min %u sec", day, hour, min, second); 119 } 120 121 size_t curr_exec_cnt = ATOMIC_GET(hfuzz->mutationsCnt); 122 /* 123 * We increase the mutation counter unconditionally in threads, but if it's 124 * above hfuzz->mutationsMax we don't really execute the fuzzing loop. 125 * Therefore at the end of fuzzing, the mutation counter might be higher 126 * than hfuzz->mutationsMax 127 */ 128 if (hfuzz->mutationsMax > 0 && curr_exec_cnt > hfuzz->mutationsMax) { 129 curr_exec_cnt = hfuzz->mutationsMax; 130 } 131 float exeProgress = 0.0f; 132 if (hfuzz->mutationsMax > 0) { 133 exeProgress = ((float)curr_exec_cnt * 100 / hfuzz->mutationsMax); 134 } 135 136 static size_t prev_exec_cnt = 0UL; 137 uintptr_t exec_per_sec = curr_exec_cnt - prev_exec_cnt; 138 prev_exec_cnt = curr_exec_cnt; 139 140 /* The lock should be acquired before any output is printed on the screen */ 141 MX_SCOPED_LOCK(logMutexGet()); 142 143 display_put("%s", ESC_CLEAR); 144 display_put("----------------------------[ " ESC_BOLD "%s v%s" ESC_RESET 145 " ]---------------------------\n", PROG_NAME, PROG_VERSION); 146 display_put(" Iterations : " ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET, curr_exec_cnt); 147 display_printKMG(curr_exec_cnt); 148 if (hfuzz->mutationsMax) { 149 display_put(" (out of: " ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET " [" ESC_BOLD "%.2f" 150 ESC_RESET "%%])", hfuzz->mutationsMax, exeProgress); 151 } 152 switch (ATOMIC_GET(hfuzz->state)) { 153 case _HF_STATE_STATIC: 154 display_put("\n Phase : " ESC_BOLD "Main" ESC_RESET); 155 break; 156 case _HF_STATE_DYNAMIC_PRE: 157 display_put("\n Phase : " ESC_BOLD "Dry Run" ESC_RESET); 158 break; 159 case _HF_STATE_DYNAMIC_MAIN: 160 display_put("\n Phase : " ESC_BOLD "Dynamic Main" ESC_RESET); 161 break; 162 default: 163 display_put("\n Phase : " ESC_BOLD "Unknown" ESC_RESET); 164 break; 165 } 166 167 char start_time_str[128]; 168 util_getLocalTime("%F %T", start_time_str, sizeof(start_time_str), hfuzz->timeStart); 169 display_put("\n Run Time : " ESC_BOLD "%s" ESC_RESET " (since: " ESC_BOLD "%s" ESC_RESET 170 ")\n", time_elapsed_str, start_time_str); 171 display_put(" Input Dir : '" ESC_BOLD "%s" ESC_RESET "'\n", 172 hfuzz->inputDir != NULL ? hfuzz->inputDir : "[NONE]"); 173 display_put(" Fuzzed Cmd : '" ESC_BOLD "%s" ESC_RESET "'\n", hfuzz->cmdline_txt); 174 if (hfuzz->linux.pid > 0) { 175 display_put("Remote cmd [" ESC_BOLD "%d" ESC_RESET "]: '" ESC_BOLD "%s" ESC_RESET 176 "'\n", hfuzz->linux.pid, hfuzz->linux.pidCmd); 177 } 178 179 static long num_cpu = 0; 180 if (num_cpu == 0) { 181 num_cpu = sysconf(_SC_NPROCESSORS_ONLN); 182 } 183 unsigned cpuUse = getCpuUse(num_cpu); 184 display_put(" Threads : " ESC_BOLD "%zu" ESC_RESET ", CPUs: " ESC_BOLD "%ld" ESC_RESET 185 ", CPU: " ESC_BOLD "%u" ESC_RESET "%% (" ESC_BOLD "%u" ESC_RESET "%%/CPU)\n", 186 hfuzz->threadsMax, num_cpu, cpuUse, cpuUse / num_cpu); 187 188 display_put(" Speed : " ESC_BOLD "% " _HF_MONETARY_MOD "zu" ESC_RESET "/sec" 189 " (avg: " ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET ")\n", exec_per_sec, 190 elapsed_second ? (curr_exec_cnt / elapsed_second) : 0); 191 /* If dry run, print also the input file count */ 192 if (hfuzz->origFlipRate == 0.0L && hfuzz->useVerifier) { 193 display_put(" Input Files : '" ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET "'\n", 194 hfuzz->fileCnt); 195 } 196 197 uint64_t crashesCnt = ATOMIC_GET(hfuzz->crashesCnt); 198 /* colored the crash count as red when exist crash */ 199 display_put(" Crashes : " ESC_BOLD "%s" "%zu" ESC_RESET " (unique: %s" ESC_BOLD "%zu" 200 ESC_RESET ", blacklist: " ESC_BOLD "%zu" ESC_RESET ", verified: " 201 ESC_BOLD "%zu" ESC_RESET ")\n", crashesCnt > 0 ? ESC_RED : "", 202 hfuzz->crashesCnt, crashesCnt > 0 ? ESC_RED : "", 203 ATOMIC_GET(hfuzz->uniqueCrashesCnt), ATOMIC_GET(hfuzz->blCrashesCnt), 204 ATOMIC_GET(hfuzz->verifiedCrashesCnt)); 205 display_put(" Timeouts : " ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET " [%" 206 _HF_MONETARY_MOD "zu sec.]\n", ATOMIC_GET(hfuzz->timeoutedCnt), hfuzz->tmOut); 207 /* Feedback data sources are enabled. Start with common headers. */ 208 if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE || hfuzz->useSanCov) { 209 display_put(" Corpus Size : " ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET 210 ", max size (bytes): " ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET "\n", 211 hfuzz->dynfileqCnt, hfuzz->maxFileSz); 212 display_put(" Coverage :\n"); 213 } 214 215 /* HW perf specific counters */ 216 if (hfuzz->dynFileMethod & _HF_DYNFILE_INSTR_COUNT) { 217 display_put(" *** instructions: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 218 "\n", ATOMIC_GET(hfuzz->linux.hwCnts.cpuInstrCnt)); 219 } 220 if (hfuzz->dynFileMethod & _HF_DYNFILE_BRANCH_COUNT) { 221 display_put(" *** branches: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 222 "\n", ATOMIC_GET(hfuzz->linux.hwCnts.cpuBranchCnt)); 223 } 224 if (hfuzz->dynFileMethod & _HF_DYNFILE_BTS_BLOCK) { 225 display_put(" *** BTS blocks: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 226 "\n", ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt)); 227 } 228 if (hfuzz->dynFileMethod & _HF_DYNFILE_BTS_EDGE) { 229 display_put(" *** BTS edges: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 230 "\n", ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt)); 231 } 232 if (hfuzz->dynFileMethod & _HF_DYNFILE_IPT_BLOCK) { 233 display_put(" *** PT blocks: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 234 "\n", ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt)); 235 } 236 if (hfuzz->dynFileMethod & _HF_DYNFILE_SOFT) { 237 uint64_t softCntPc = ATOMIC_GET(hfuzz->linux.hwCnts.softCntPc); 238 uint64_t softCntCmp = ATOMIC_GET(hfuzz->linux.hwCnts.softCntCmp); 239 display_put(" *** blocks seen: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 240 ", comparison map: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET "\n", 241 softCntPc, softCntCmp); 242 } 243 244 /* Sanitizer coverage specific counters */ 245 if (hfuzz->useSanCov) { 246 uint64_t hitBB = ATOMIC_GET(hfuzz->sanCovCnts.hitBBCnt); 247 uint64_t totalBB = ATOMIC_GET(hfuzz->sanCovCnts.totalBBCnt); 248 float covPer = totalBB ? (((float)hitBB * 100) / totalBB) : 0.0; 249 display_put(" *** total hit #bb: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 250 " (coverage " ESC_BOLD "%.2f" ESC_RESET "%%)\n", hitBB, covPer); 251 display_put(" *** total #dso: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 252 " (instrumented only)\n", ATOMIC_GET(hfuzz->sanCovCnts.iDsoCnt)); 253 display_put(" *** discovered #bb: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 254 " (new from input seed)\n", ATOMIC_GET(hfuzz->sanCovCnts.newBBCnt)); 255 display_put(" *** crashes: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET 256 "\n", ATOMIC_GET(hfuzz->sanCovCnts.crashesCnt)); 257 } 258 display_put("-----------------------------------[ " ESC_BOLD "LOGS" ESC_RESET 259 " ]-----------------------------------\n"); 260} 261 262extern void display_display(honggfuzz_t * hfuzz) 263{ 264 display_displayLocked(hfuzz); 265} 266