-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.c
275 lines (223 loc) · 9.85 KB
/
report.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#ifndef DISABLE_HTTP_DASHBOARD
#include <stdbool.h>
#include <search.h>
#include <unistd.h>
#include <glib.h>
#include <sched.h>
#include <stdio.h>
#include <ClearSilver.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include "sysfuzz.h"
#include "typelib.h"
#include "iknowthis.h"
// This routine adds some global error statistics to the HDF tree, as well as
// calculating the total number of tests executed and total number of fuzzers
// currently enabled.
static void generate_global_statistics(HDF *hdf)
{
const gint MaxErrnoValue = 1024;
HDF *errors = NULL;
guint total = 0;
guint failures = 0;
guint successes = 0;
guint fuzzercount = 0;
// Create the Global Errors heirarchy.
hdf_set_value(hdf, "Global.errors", NULL);
// Retrieve that object.
errors = hdf_get_obj(hdf, "Global.errors");
// Quick compare callback for lfind().
gint compare_error(gconstpointer a, gconstpointer b)
{
return ((const error_record_t *)(a))->error
- ((const error_record_t *)(b))->error;
}
// For every errno code possible, count how many times each fuzzer has seen
// it. This is obviously not optimal, but this is not a performance
// critical lookup.
for (guint code = 0; code < MaxErrnoValue; code++) {
error_record_t *error = NULL;
error_record_t key = { code, 0 };
guint count = 0;
// For every system call, check if it has this errno.
for (guint sysno = 0; sysno < MAX_SYSCALL_NUM; sysno++) {
// Retrieve pointer to this fuzzer.
syscall_fuzzer_t *fuzzer = &system_call_fuzzers[sysno];
// Sanity checks.
g_assert_cmpuint(fuzzer->numerrors, <, MAX_ERROR_CODES);
// Search it's errors structure for this errno.
error = lfind(&key, // key
fuzzer->errors, // base
&fuzzer->numerrors, // num
sizeof key, // size
compare_error); // compare
// If there was a hit, add it to the count.
if (error) {
count += error->count;
}
// Keep track of statistics, only for the first iteration.
if (code == 0) {
total += fuzzer->total;
failures += fuzzer->failures;
successes += fuzzer->total - fuzzer->failures;
// Count the fuzzers that are enabled.
if (fuzzer->name && !(fuzzer->flags & SYS_DISABLED)) {
fuzzercount++;
}
}
}
// Add the final count to the data if it was non-zero.
if (count) {
gchar *nodedesc = g_strdup_printf("%u.description", code);
gchar *nodecount = g_strdup_printf("%u.count", code);
hdf_set_value(errors, nodedesc, custom_strerror_wrapper(code));
hdf_set_int_value(errors, nodecount, count);
// Clean up.
g_free(nodedesc);
g_free(nodecount);
}
}
// Add some global statistics.
hdf_set_int_value(hdf, "Global.num_fuzzers", fuzzercount);
hdf_set_int_value(hdf, "Global.total_executions", total);
hdf_set_int_value(hdf, "Global.total_failures", failures);
hdf_set_int_value(hdf, "Global.total_successes", successes);
return;
}
// This routine scans the list of fuzzers for best and worst performers, and
// adds an appropriate node to the HDF.
static void generate_fuzzer_statistics(HDF *hdf)
{
syscall_fuzzer_t *fastest = NULL;
syscall_fuzzer_t *slowest = NULL;
// For every system call, collect statistics.
for (guint sysno = 0; sysno < MAX_SYSCALL_NUM; sysno++) {
// Retrieve pointer to this fuzzer.
syscall_fuzzer_t *fuzzer = &system_call_fuzzers[sysno];
if (!fuzzer->name || (fuzzer->flags & SYS_DISABLED))
continue;
// Compare statistics.
if (!fastest || fuzzer->average < fastest->average)
fastest = fuzzer;
if (!slowest || fuzzer->average > slowest->average)
slowest = fuzzer;
}
// Check we have a result
if (fastest == NULL || slowest == NULL) {
g_warning("statistics generation may fail, there do not appear to be any fuzzers enabled");
return;
}
hdf_set_value(hdf, "Global.fastest_fuzzer.name", fastest->name);
hdf_set_int_value(hdf, "Global.fastest_fuzzer.speed", fastest->average * 1000000);
hdf_set_value(hdf, "Global.slowest_fuzzer.name", slowest->name);
hdf_set_int_value(hdf, "Global.slowest_fuzzer.speed", slowest->average * 1000000);
return;
}
// This routine adds lots of details about this specific fuzzer to the HDF.
void pretty_print_fuzzer(HDF *hdf, syscall_fuzzer_t *fuzzer)
{
gchar *name = g_strdup_printf("Fuzzer.%u", fuzzer->number);
HDF *info = NULL;
// Create an heirarchy for this fuzzer
hdf_set_value(hdf, name, NULL);
// Retrieve that object.
info = hdf_get_obj(hdf, name);
hdf_set_value(info, "Name", fuzzer->name);
hdf_set_int_value(info, "Total", fuzzer->total);
hdf_set_int_value(info, "Failures", fuzzer->failures);
hdf_set_int_value(info, "NumErrors", fuzzer->numerrors);
hdf_set_value(info, "Errors", NULL);
// Now switch to error tree.
info = hdf_get_obj(info, "Errors");
// Enumerate all the errors this fuzzer has seen.
for (gint i = 0; i < fuzzer->numerrors; i++) {
gchar *error = g_strdup_printf("%u.error", i);
gchar *count = g_strdup_printf("%u.count", i);
hdf_set_value(info, error, custom_strerror_wrapper(fuzzer->errors[i].error));
hdf_set_int_value(info, count, fuzzer->errors[i].count);
g_free(error);
g_free(count);
}
g_free(name);
return;
}
void create_fuzzer_report(HDF *hdf)
{
// First create the global error statistics.
generate_global_statistics(hdf);
// Get some overall fuzzer stats (Slowest, Fastest, etc.)
generate_fuzzer_statistics(hdf);
// Create some empty hdf nodes.
hdf_set_value(hdf, "Global.fuzzer_missing", NULL); // Not defined
hdf_set_value(hdf, "Global.fuzzer_disabled", NULL); // Defined but disabled
hdf_set_value(hdf, "Global.fuzzer_always_fails", NULL); // Always fail, but not marked SYS_FAIL.
hdf_set_value(hdf, "Global.fuzzer_always_same", NULL); // Always return the same value, but not marked SYS_BORING.
hdf_set_value(hdf, "Global.fuzzer_not_boring", NULL); // Marked SYS_BORING, but returning multiple values.
// Now we need to create a list of interesting events the user needs to
// investigate. The first one is system calls that have no associated
// fuzzers, so scan the list for fuzzers with NULL callback.
for (guint i = 0; i < MAX_SYSCALL_NUM; i++) {
// Is the system call undefined?
if (system_call_fuzzers[i].callback == NULL) {
gchar *node = g_strdup_printf("%u.number", i);
hdf_set_int_value(hdf_get_obj(hdf, "Global.fuzzer_missing"),
node,
i);
g_free(node);
// There are no other useful statistics from this.
continue;
}
pretty_print_fuzzer(hdf, &system_call_fuzzers[i]);
// Is the system call disabled?
if (system_call_fuzzers[i].flags & SYS_DISABLED) {
gchar *node = g_strdup_printf("%u.name", i);
hdf_set_value(hdf_get_obj(hdf, "Global.fuzzer_disabled"),
node,
system_call_fuzzers[i].name);
g_free(node);
// Nothing else useful can happen to disabled fuzzers.
continue;
}
// We can't do any more analysis if it has never been executed, so check here.
if (system_call_fuzzers[i].total == 0)
continue;
// Does it always fail, but not marked SYS_FAIL?
if (system_call_fuzzers[i].total == system_call_fuzzers[i].failures && !(system_call_fuzzers[i].flags & SYS_FAIL)) {
gchar *node = g_strdup_printf("%u.name", i);
hdf_set_value(hdf_get_obj(hdf, "Global.fuzzer_always_fails"),
node,
system_call_fuzzers[i].name);
g_free(node);
}
// Does it always return the same value, but not marked SYS_BORING?
if (((system_call_fuzzers[i].total == system_call_fuzzers[i].failures
&& system_call_fuzzers[i].numerrors == 1)
|| system_call_fuzzers[i].numerrors == 0)
&& !(system_call_fuzzers[i].flags & SYS_BORING)) {
gchar *node = g_strdup_printf("%u.name", i);
hdf_set_value(hdf_get_obj(hdf, "Global.fuzzer_always_same"),
node,
system_call_fuzzers[i].name);
g_free(node);
}
// Is it marked SYS_BORING, but returns multiple value?
if ((system_call_fuzzers[i].flags & SYS_BORING) && !(system_call_fuzzers[i].numerrors <= 1)) {
gchar *node = g_strdup_printf("%u.name", i);
hdf_set_value(hdf_get_obj(hdf, "Global.fuzzer_not_boring"),
node,
system_call_fuzzers[i].name);
g_free(node);
}
// Is it marked SYS_FAIL, but succeeded?
if ((system_call_fuzzers[i].flags & SYS_FAIL) && (system_call_fuzzers[i].failures != system_call_fuzzers[i].total)) {
gchar *node = g_strdup_printf("%u.name", i);
hdf_set_value(hdf_get_obj(hdf, "Global.fuzzer_not_failing"),
node,
system_call_fuzzers[i].name);
g_free(node);
}
}
return;
}
#endif