display.c revision 8ad8c01c319297294697b52cd715f720726c12b8
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 <string.h>
30#include <stdarg.h>
31#include <stdio.h>
32#include <unistd.h>
33#include <inttypes.h>
34
35#include "log.h"
36#include "util.h"
37
38#define ESC_CLEAR "\033[H\033[2J"
39#define ESC_NAV(x,y) "\033["#x";"#y"H"
40#define ESC_BOLD "\033[1m"
41#define ESC_RESET "\033[0m"
42
43static void display_put(const char *fmt, ...)
44{
45    va_list args;
46    va_start(args, fmt);
47    vdprintf(STDOUT_FILENO, fmt, args);
48    va_end(args);
49}
50
51static void display_displayLocked(honggfuzz_t * hfuzz)
52{
53    unsigned long elapsed_second = (unsigned long)(time(NULL) - hfuzz->timeStart);
54
55    unsigned int day, hour, min, second;
56    char time_elapsed_str[64];
57
58    if (elapsed_second < 24 * 3600) {
59        hour = elapsed_second / 3600;
60        min = (elapsed_second - 3600 * hour) / 60;
61        second = elapsed_second - hour * 3600 - min * 60;
62        snprintf(time_elapsed_str, sizeof(time_elapsed_str), "%u hrs %u min %u sec", hour, min,
63                 second);
64    } else {
65        day = elapsed_second / 24 / 3600;
66        elapsed_second = elapsed_second - day * 24 * 3600;
67        hour = elapsed_second / 3600;
68        min = (elapsed_second - 3600 * hour) / 60;
69        second = elapsed_second - hour * 3600 - min * 60;
70        snprintf(time_elapsed_str, sizeof(time_elapsed_str), "%u days %u hrs %u min %u sec", day,
71                 hour, min, second);
72    }
73
74    size_t curr_exec_cnt = ATOMIC_GET(hfuzz->mutationsCnt);
75    /*
76     * We increase the mutation counter unconditionally in threads, but if it's
77     * above hfuzz->mutationsMax we don't really execute the fuzzing loop.
78     * Therefore at the end of fuzzing, the mutation counter might be higher
79     * than hfuzz->mutationsMax
80     */
81    if (hfuzz->mutationsMax > 0 && curr_exec_cnt > hfuzz->mutationsMax) {
82        curr_exec_cnt = hfuzz->mutationsMax;
83    }
84    static size_t prev_exec_cnt = 0UL;
85    uintptr_t exec_per_sec = curr_exec_cnt - prev_exec_cnt;
86    prev_exec_cnt = curr_exec_cnt;
87
88    display_put("%s", ESC_CLEAR);
89    display_put("==================================== STAT ====================================\n");
90
91    display_put("Iterations: " ESC_BOLD "%zu" ESC_RESET, curr_exec_cnt);
92    if (hfuzz->mutationsMax) {
93        display_put(" (out of: " ESC_BOLD "%zu" ESC_RESET ")", hfuzz->mutationsMax);
94    }
95    display_put("\n");
96
97    char start_time_str[128];
98    util_getLocalTime("%F %T", start_time_str, sizeof(start_time_str), hfuzz->timeStart);
99    display_put("Start time: " ESC_BOLD "%s" ESC_RESET "\n", start_time_str);
100    display_put("Run time: " ESC_BOLD "%s" ESC_RESET "\n", time_elapsed_str);
101
102    display_put("Input file/dir: '" ESC_BOLD "%s" ESC_RESET "'\n", hfuzz->inputFile);
103    display_put("Fuzzed cmd: '" ESC_BOLD "%s" ESC_RESET "'\n", hfuzz->cmdline_txt);
104    if (hfuzz->linux.pid > 0) {
105        display_put("Remote cmd [" ESC_BOLD "%d" ESC_RESET "]: '" ESC_BOLD "%s" ESC_RESET "'\n",
106                    hfuzz->linux.pid, hfuzz->linux.pidCmd);
107    }
108
109    display_put("Fuzzing threads: " ESC_BOLD "%zu" ESC_RESET "\n", hfuzz->threadsMax);
110    display_put("Execs per second: " ESC_BOLD "%zu" ESC_RESET " (avg: " ESC_BOLD "%zu"
111                ESC_RESET ")\n", exec_per_sec,
112                elapsed_second ? (curr_exec_cnt / elapsed_second) : 0);
113
114    /* If dry run, print also the input file count */
115    if (hfuzz->origFlipRate == 0.0L && hfuzz->useVerifier) {
116        display_put("Input Files: '" ESC_BOLD "%zu" ESC_RESET "'\n", hfuzz->fileCnt);
117    }
118
119    display_put("Crashes: " ESC_BOLD "%zu" ESC_RESET " (unique: " ESC_BOLD "%zu" ESC_RESET
120                ", blacklist: " ESC_BOLD "%zu" ESC_RESET ", verified: " ESC_BOLD "%zu" ESC_RESET
121                ")\n", ATOMIC_GET(hfuzz->crashesCnt),
122                ATOMIC_GET(hfuzz->uniqueCrashesCnt),
123                ATOMIC_GET(hfuzz->blCrashesCnt), ATOMIC_GET(hfuzz->verifiedCrashesCnt));
124    display_put("Timeouts: " ESC_BOLD "%zu" ESC_RESET "\n", ATOMIC_GET(hfuzz->timeoutedCnt));
125
126    /* Feedback data sources are enabled. Start with common headers. */
127    if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE || hfuzz->useSanCov) {
128        display_put("File corpus size: " ESC_BOLD "%zu" ESC_RESET "\n", hfuzz->dynfileqCnt);
129        display_put("Coverage (max):\n");
130    }
131
132    /* HW perf specific counters */
133    if (hfuzz->dynFileMethod & _HF_DYNFILE_INSTR_COUNT) {
134        display_put("  - cpu instructions:      " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
135                    ATOMIC_GET(hfuzz->linux.hwCnts.cpuInstrCnt));
136    }
137    if (hfuzz->dynFileMethod & _HF_DYNFILE_BRANCH_COUNT) {
138        display_put("  - cpu branches:          " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
139                    ATOMIC_GET(hfuzz->linux.hwCnts.cpuBranchCnt));
140    }
141    if (hfuzz->dynFileMethod & _HF_DYNFILE_BTS_BLOCK) {
142        display_put("  - BTS unique blocks: " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
143                    ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
144    }
145    if (hfuzz->dynFileMethod & _HF_DYNFILE_BTS_EDGE) {
146        display_put("  - BTS unique edges:   " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
147                    ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
148    }
149    if (hfuzz->dynFileMethod & _HF_DYNFILE_IPT_BLOCK) {
150        display_put("  - PT unique blocks: " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
151                    ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
152    }
153    if (hfuzz->dynFileMethod & _HF_DYNFILE_CUSTOM) {
154        display_put("  - custom counter:        " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
155                    ATOMIC_GET(hfuzz->linux.hwCnts.customCnt));
156    }
157
158    /* Sanitizer coverage specific counters */
159    if (hfuzz->useSanCov) {
160        uint64_t hitBB = ATOMIC_GET(hfuzz->sanCovCnts.hitBBCnt);
161        uint64_t totalBB = ATOMIC_GET(hfuzz->sanCovCnts.totalBBCnt);
162        uint8_t covPer = totalBB ? ((hitBB * 100) / totalBB) : 0;
163        display_put("  - total hit #bb:  " ESC_BOLD "%" PRIu64 ESC_RESET " (coverage %d%%)\n",
164                    hitBB, covPer);
165        display_put("  - total #dso:     " ESC_BOLD "%" PRIu64 ESC_RESET " (instrumented only)\n",
166                    ATOMIC_GET(hfuzz->sanCovCnts.iDsoCnt));
167        display_put("  - discovered #bb: " ESC_BOLD "%" PRIu64 ESC_RESET " (new from input seed)\n",
168                    ATOMIC_GET(hfuzz->sanCovCnts.newBBCnt));
169        display_put("  - crashes:        " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
170                    ATOMIC_GET(hfuzz->sanCovCnts.crashesCnt));
171    }
172    display_put("==================================== LOGS ====================================\n");
173}
174
175extern void display_display(honggfuzz_t * hfuzz)
176{
177    display_displayLocked(hfuzz);
178}
179