display.c revision bfcba12c8897c02b4396bbd98ad78dc49957ec50
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 = (unsigned long)(time(NULL) - hfuzz->timeStart);
54
55    size_t curr_exec_cnt = ATOMIC_GET(hfuzz->mutationsCnt);
56    /*
57     * We increase the mutation counter unconditionally in threads, but if it's
58     * above hfuzz->mutationsMax we don't really execute the fuzzing loop.
59     * Therefore at the end of fuzzing, the mutation counter might be higher
60     * than hfuzz->mutationsMax
61     */
62    if (hfuzz->mutationsMax > 0 && curr_exec_cnt > hfuzz->mutationsMax) {
63        curr_exec_cnt = hfuzz->mutationsMax;
64    }
65    static size_t prev_exec_cnt = 0UL;
66    uintptr_t exec_per_sec = curr_exec_cnt - prev_exec_cnt;
67    prev_exec_cnt = curr_exec_cnt;
68
69    display_put("%s", ESC_CLEAR);
70    display_put("==================================== STAT ====================================\n");
71
72    display_put("Iterations: " ESC_BOLD "%zu" ESC_RESET, curr_exec_cnt);
73    if (hfuzz->mutationsMax) {
74        display_put(" (out of: " ESC_BOLD "%zu" ESC_RESET ")", hfuzz->mutationsMax);
75    }
76    display_put("\n");
77
78    char start_time_str[128];
79    util_getLocalTime("%F %T", start_time_str, sizeof(start_time_str), hfuzz->timeStart);
80    display_put("Start time: " ESC_BOLD "%s" ESC_RESET " (" ESC_BOLD "%lu"
81                ESC_RESET " seconds elapsed)\n", start_time_str, elapsed);
82
83    display_put("Input file/dir: '" ESC_BOLD "%s" ESC_RESET "'\n", hfuzz->inputFile);
84    display_put("Fuzzed cmd: '" ESC_BOLD "%s" ESC_RESET "'\n", hfuzz->cmdline_txt);
85    if (hfuzz->linux.pid > 0) {
86        display_put("Remote cmd [" ESC_BOLD "%d" ESC_RESET "]: '" ESC_BOLD "%s" ESC_RESET "'\n",
87                    hfuzz->linux.pid, hfuzz->linux.pidCmd);
88    }
89
90    display_put("Fuzzing threads: " ESC_BOLD "%zu" ESC_RESET "\n", hfuzz->threadsMax);
91    display_put("Execs (iterations) per second: " ESC_BOLD "%zu" ESC_RESET " (avg: " ESC_BOLD "%zu"
92                ESC_RESET ")\n", exec_per_sec, elapsed ? (curr_exec_cnt / elapsed) : 0);
93
94    /* If dry run, print also the input file count */
95    if (hfuzz->origFlipRate == 0.0L && hfuzz->useVerifier) {
96        display_put("Input Files: '" ESC_BOLD "%zu" ESC_RESET "'\n", hfuzz->fileCnt);
97    }
98
99    display_put("Crashes: " ESC_BOLD "%zu" ESC_RESET " (unique: " ESC_BOLD "%zu" ESC_RESET
100                ", blacklist: " ESC_BOLD "%zu" ESC_RESET ", verified: " ESC_BOLD "%zu" ESC_RESET
101                ")\n", ATOMIC_GET(hfuzz->crashesCnt),
102                ATOMIC_GET(hfuzz->uniqueCrashesCnt),
103                ATOMIC_GET(hfuzz->blCrashesCnt), ATOMIC_GET(hfuzz->verifiedCrashesCnt));
104    display_put("Timeouts: " ESC_BOLD "%zu" ESC_RESET "\n", ATOMIC_GET(hfuzz->timeoutedCnt));
105
106    /* Feedback data sources are enabled. Start with common headers. */
107    if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE || hfuzz->useSanCov) {
108        display_put("Number of dynamic files: " ESC_BOLD "%zu" ESC_RESET "\n", hfuzz->dynfileqCnt);
109        display_put("Coverage (max):\n");
110    }
111
112    /* HW perf specific counters */
113    if (hfuzz->dynFileMethod & _HF_DYNFILE_INSTR_COUNT) {
114        display_put("  - cpu instructions:      " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
115                    ATOMIC_GET(hfuzz->linux.hwCnts.cpuInstrCnt));
116    }
117    if (hfuzz->dynFileMethod & _HF_DYNFILE_BRANCH_COUNT) {
118        display_put("  - cpu branches:          " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
119                    ATOMIC_GET(hfuzz->linux.hwCnts.cpuBranchCnt));
120    }
121    if (hfuzz->dynFileMethod & _HF_DYNFILE_BTS_BLOCK) {
122        display_put("  - BTS unique blocks: " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
123                    ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
124    }
125    if (hfuzz->dynFileMethod & _HF_DYNFILE_BTS_EDGE) {
126        display_put("  - BTS unique edges:   " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
127                    ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
128    }
129    if (hfuzz->dynFileMethod & _HF_DYNFILE_IPT_BLOCK) {
130        display_put("  - PT unique blocks: " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
131                    ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
132    }
133    if (hfuzz->dynFileMethod & _HF_DYNFILE_CUSTOM) {
134        display_put("  - custom counter:        " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
135                    ATOMIC_GET(hfuzz->linux.hwCnts.customCnt));
136    }
137
138    /* Sanitizer coverage specific counters */
139    if (hfuzz->useSanCov) {
140        uint64_t hitBB = ATOMIC_GET(hfuzz->sanCovCnts.hitBBCnt);
141        uint64_t totalBB = ATOMIC_GET(hfuzz->sanCovCnts.totalBBCnt);
142        uint8_t covPer = totalBB ? ((hitBB * 100) / totalBB) : 0;
143        display_put("  - total hit #bb:  " ESC_BOLD "%" PRIu64 ESC_RESET " (coverage %d%%)\n",
144                    hitBB, covPer);
145        display_put("  - total #dso:     " ESC_BOLD "%" PRIu64 ESC_RESET " (instrumented only)\n",
146                    ATOMIC_GET(hfuzz->sanCovCnts.iDsoCnt));
147        display_put("  - discovered #bb: " ESC_BOLD "%" PRIu64 ESC_RESET " (new from input seed)\n",
148                    ATOMIC_GET(hfuzz->sanCovCnts.newBBCnt));
149        display_put("  - crashes:        " ESC_BOLD "%" PRIu64 ESC_RESET "\n",
150                    ATOMIC_GET(hfuzz->sanCovCnts.crashesCnt));
151    }
152    display_put("==================================== LOGS ====================================\n");
153}
154
155extern void display_display(honggfuzz_t * hfuzz)
156{
157    display_displayLocked(hfuzz);
158}
159