blob: 85bdc917ee13436a2ebd13480ed029bfe4d16212 [file] [log] [blame]
Jaggerbaa20ea2015-09-06 01:12:08 +02001/*
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>
Jagger7acbf2e2015-09-06 20:02:32 +020030#include <stdarg.h>
Jaggerbaa20ea2015-09-06 01:12:08 +020031#include <stdio.h>
32#include <unistd.h>
33
34#include "log.h"
Robert Swiecki81c6a0d2015-09-08 15:43:20 +020035#include "util.h"
Jaggerbaa20ea2015-09-06 01:12:08 +020036
Jaggerbaa20ea2015-09-06 01:12:08 +020037#define ESC_CLEAR "\033[H\033[2J"
38#define ESC_NAV(x,y) "\033["#x";"#y"H"
Jagger76b11bc2015-09-06 02:11:44 +020039#define ESC_BOLD "\033[1m"
40#define ESC_RESET "\033[0m"
Jaggerbaa20ea2015-09-06 01:12:08 +020041
Jagger7acbf2e2015-09-06 20:02:32 +020042static void display_put(const char *fmt, ...)
43{
44 char buf[1024 * 512];
45
46 va_list args;
47 va_start(args, fmt);
48 int ret = vsnprintf(buf, sizeof(buf), fmt, args);
49 va_end(args);
50
51 if (ret <= 0) {
52 return;
53 }
54 if (write(STDOUT_FILENO, buf, ret) == -1) {
55 return;
56 }
57}
58
Robert Swiecki7353a8d2015-09-08 15:53:59 +020059static void display_displayLocked(honggfuzz_t * hfuzz)
Jaggerbaa20ea2015-09-06 01:12:08 +020060{
Jagger269cea52015-09-06 04:46:07 +020061 unsigned long elapsed = (unsigned long)(time(NULL) - hfuzz->timeStart);
Robert Swiecki81c6a0d2015-09-08 15:43:20 +020062
Jagger7acbf2e2015-09-06 20:02:32 +020063 size_t curr_exec_cnt = __sync_fetch_and_add(&hfuzz->mutationsCnt, 0UL);
Robert Swiecki81c6a0d2015-09-08 15:43:20 +020064 /*
65 * We increase the mutation counter unconditionally in threads, but if it's
66 * above hfuzz->mutationsMax we don't really execute the fuzzing loop.
67 * Therefore at the end of fuzzing, the mutation counter might be higher
68 * than hfuzz->mutationsMax
69 */
70 if (hfuzz->mutationsMax > 0 && curr_exec_cnt > hfuzz->mutationsMax) {
71 curr_exec_cnt = hfuzz->mutationsMax;
72 }
Jaggerbaa20ea2015-09-06 01:12:08 +020073 static size_t prev_exec_cnt = 0UL;
Jaggerbaa20ea2015-09-06 01:12:08 +020074 uintptr_t exec_per_sec = curr_exec_cnt - prev_exec_cnt;
75 prev_exec_cnt = curr_exec_cnt;
76
Jagger7acbf2e2015-09-06 20:02:32 +020077 display_put("%s", ESC_CLEAR);
Jagger3905f552015-09-06 21:04:37 +020078 display_put("============================== STAT ==============================\n");
Jaggerbaa20ea2015-09-06 01:12:08 +020079
Jagger7acbf2e2015-09-06 20:02:32 +020080 display_put("Iterations: " ESC_BOLD "%zu" ESC_RESET, curr_exec_cnt);
Jaggerbaa20ea2015-09-06 01:12:08 +020081 if (hfuzz->mutationsMax) {
Jagger7acbf2e2015-09-06 20:02:32 +020082 display_put(" (out of: " ESC_BOLD "%zu" ESC_RESET ")", hfuzz->mutationsMax);
Jaggerbaa20ea2015-09-06 01:12:08 +020083 }
Jaggerbcd57852015-09-06 23:10:44 +020084 display_put("\n");
Jagger56ed7642015-09-06 04:06:57 +020085
Robert Swiecki81c6a0d2015-09-08 15:43:20 +020086 char start_time_str[128];
87 util_getLocalTime("%F %T", start_time_str, sizeof(start_time_str), hfuzz->timeStart);
88 display_put("Start time: " ESC_BOLD "%s" ESC_RESET " (" ESC_BOLD "%lu"
89 ESC_RESET " seconds elapsed)\n", start_time_str, elapsed);
Jaggerbaa20ea2015-09-06 01:12:08 +020090
Jagger7acbf2e2015-09-06 20:02:32 +020091 display_put("Input file/dir: '" ESC_BOLD "%s" ESC_RESET "'\n", hfuzz->inputFile);
92 display_put("Fuzzed cmd: '" ESC_BOLD "%s" ESC_RESET "'\n", hfuzz->cmdline[0]);
Jaggerbaa20ea2015-09-06 01:12:08 +020093
Jagger7acbf2e2015-09-06 20:02:32 +020094 display_put("Fuzzing threads: " ESC_BOLD "%zu" ESC_RESET "\n", hfuzz->threadsMax);
95 display_put("Execs per second: " ESC_BOLD "%zu" ESC_RESET " (avg: " ESC_BOLD "%zu" ESC_RESET
96 ")\n", exec_per_sec, elapsed ? (curr_exec_cnt / elapsed) : 0);
Jaggerbaa20ea2015-09-06 01:12:08 +020097
Jagger7acbf2e2015-09-06 20:02:32 +020098 display_put("Crashes: " ESC_BOLD "%zu" ESC_RESET "\n",
99 __sync_fetch_and_add(&hfuzz->crashesCnt, 0UL));
100 display_put("Timeouts: " ESC_BOLD "%zu" ESC_RESET "\n",
101 __sync_fetch_and_add(&hfuzz->timeoutedCnt, 0UL));
Jaggerbaa20ea2015-09-06 01:12:08 +0200102
Robert Swiecki53958402015-09-08 16:20:50 +0200103 if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE) {
104 display_put("Dynamic file size: " ESC_BOLD "%zu" ESC_RESET " (max: " ESC_BOLD "%zu"
105 ESC_RESET ")\n", hfuzz->dynamicFileBestSz, hfuzz->maxFileSz);
106 display_put("Coverage (max):\n");
Jaggerbaa20ea2015-09-06 01:12:08 +0200107 }
Robert Swiecki53958402015-09-08 16:20:50 +0200108 if (hfuzz->dynFileMethod & _HF_DYNFILE_INSTR_COUNT) {
109 display_put(" - cpu instructions: " ESC_BOLD "%zu" ESC_RESET "\n",
Jagger3b513552015-09-09 02:08:35 +0200110 __sync_fetch_and_add(&hfuzz->hwCnts.cpuInstrCnt, 0UL));
Robert Swiecki53958402015-09-08 16:20:50 +0200111 }
112 if (hfuzz->dynFileMethod & _HF_DYNFILE_BRANCH_COUNT) {
113 display_put(" - cpu branches: " ESC_BOLD "%zu" ESC_RESET "\n",
Jagger3b513552015-09-09 02:08:35 +0200114 __sync_fetch_and_add(&hfuzz->hwCnts.cpuBranchCnt, 0UL));
Robert Swiecki53958402015-09-08 16:20:50 +0200115 }
116 if (hfuzz->dynFileMethod & _HF_DYNFILE_UNIQUE_BLOCK_COUNT) {
117 display_put(" - unique branch targets: " ESC_BOLD "%zu" ESC_RESET "\n",
Jagger3b513552015-09-09 02:08:35 +0200118 __sync_fetch_and_add(&hfuzz->hwCnts.pcCnt, 0UL));
Robert Swiecki53958402015-09-08 16:20:50 +0200119 }
120 if (hfuzz->dynFileMethod & _HF_DYNFILE_UNIQUE_EDGE_COUNT) {
121 display_put(" - unique branch pairs: " ESC_BOLD "%zu" ESC_RESET "\n",
Jagger3b513552015-09-09 02:08:35 +0200122 __sync_fetch_and_add(&hfuzz->hwCnts.pathCnt, 0UL));
Robert Swiecki53958402015-09-08 16:20:50 +0200123 }
124 if (hfuzz->dynFileMethod & _HF_DYNFILE_CUSTOM) {
125 display_put(" - custom counter: " ESC_BOLD "%zu" ESC_RESET "\n",
Jagger3b513552015-09-09 02:08:35 +0200126 __sync_fetch_and_add(&hfuzz->hwCnts.customCnt, 0UL));
Robert Swiecki53958402015-09-08 16:20:50 +0200127 }
Jagger3905f552015-09-06 21:04:37 +0200128 display_put("============================== LOGS ==============================\n");
Jaggerbaa20ea2015-09-06 01:12:08 +0200129}
Robert Swiecki7353a8d2015-09-08 15:53:59 +0200130
131extern void display_display(honggfuzz_t * hfuzz)
132{
Robert Swiecki944e9ee2015-09-08 20:29:16 +0200133 /* Don't mix up logs and display at this point */
Robert Swiecki7353a8d2015-09-08 15:53:59 +0200134 log_mutexLock();
135 display_displayLocked(hfuzz);
136 log_mutexUnLock();
137}