Skip to content

Commit ea14bf4

Browse files
author
metzman
committed
[fuzzer][afl] Remove AFL_DRIVER_EXTRA_STATS_FILENAME
Summary: Remove this feature as it is unused, buggy, and not worth correcting since the forkserver makes it difficult. Reviewers: morehouse, jfb Reviewed By: morehouse Differential Revision: https://reviews.llvm.org/D57308 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/fuzzer@352392 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent fb675a7 commit ea14bf4

File tree

1 file changed

+4
-165
lines changed

1 file changed

+4
-165
lines changed

afl/afl_driver.cpp

+4-165
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,18 @@ clang++ afl_driver.cpp test_fuzzer.o afl-llvm-rt.o.o
3131
rm -rf IN OUT; mkdir IN OUT; echo z > IN/z;
3232
$AFL_HOME/afl-fuzz -i IN -o OUT ./a.out
3333
################################################################################
34-
Environment Variables:
35-
There are a few environment variables that can be set to use features that
36-
afl-fuzz doesn't have.
37-
38-
AFL_DRIVER_STDERR_DUPLICATE_FILENAME: Setting this *appends* stderr to the file
39-
specified. If the file does not exist, it is created. This is useful for getting
40-
stack traces (when using ASAN for example) or original error messages on hard to
41-
reproduce bugs.
42-
43-
AFL_DRIVER_EXTRA_STATS_FILENAME: Setting this causes afl_driver to write extra
44-
statistics to the file specified. Currently these are peak_rss_mb
45-
(the peak amount of virtual memory used in MB) and slowest_unit_time_secs. If
46-
the file does not exist it is created. If the file does exist then
47-
afl_driver assumes it was restarted by afl-fuzz and will try to read old
48-
statistics from the file. If that fails then the process will quit.
34+
AFL_DRIVER_STDERR_DUPLICATE_FILENAME: Setting this environment variable
35+
*appends* stderr to the file specified. If the file does not exist, it is
36+
created. This is useful for getting stack traces (when using ASAN for example)
37+
or original error messages on hard to reproduce bugs.
4938
5039
*/
5140
#include <assert.h>
5241
#include <errno.h>
53-
#include <signal.h>
5442
#include <stdint.h>
5543
#include <stdio.h>
5644
#include <stdlib.h>
5745
#include <string.h>
58-
#include <sys/resource.h>
59-
#include <sys/time.h>
6046
#include <unistd.h>
6147

6248
#include <fstream>
@@ -98,17 +84,6 @@ statistics from the file. If that fails then the process will quit.
9884
#error "Support for your platform has not been implemented"
9985
#endif
10086

101-
// Used to avoid repeating error checking boilerplate. If cond is false, a
102-
// fatal error has occurred in the program. In this event print error_message
103-
// to stderr and abort(). Otherwise do nothing. Note that setting
104-
// AFL_DRIVER_STDERR_DUPLICATE_FILENAME may cause error_message to be appended
105-
// to the file as well, if the error occurs after the duplication is performed.
106-
#define CHECK_ERROR(cond, error_message) \
107-
if (!(cond)) { \
108-
fprintf(stderr, "%s\n", (error_message)); \
109-
abort(); \
110-
}
111-
11287
// libFuzzer interface is thin, so we don't include any libFuzzer headers.
11388
extern "C" {
11489
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
@@ -129,14 +104,6 @@ static volatile char suppress_warning1 = AFL_DEFER_FORKSVR[0];
129104
static const size_t kMaxAflInputSize = 1 << 20;
130105
static uint8_t AflInputBuf[kMaxAflInputSize];
131106

132-
// Variables we need for writing to the extra stats file.
133-
static FILE *extra_stats_file = NULL;
134-
static uint32_t previous_peak_rss = 0;
135-
static time_t slowest_unit_time_secs = 0;
136-
static const int kNumExtraStats = 2;
137-
static const char *kExtraStatsFormatString = "peak_rss_mb : %u\n"
138-
"slowest_unit_time_sec : %u\n";
139-
140107
// Experimental feature to use afl_driver without AFL's deferred mode.
141108
// Needs to run before __afl_auto_init.
142109
__attribute__((constructor(0))) void __decide_deferred_forkserver(void) {
@@ -148,117 +115,6 @@ __attribute__((constructor(0))) void __decide_deferred_forkserver(void) {
148115
}
149116
}
150117

151-
// Copied from FuzzerUtil.cpp.
152-
size_t GetPeakRSSMb() {
153-
struct rusage usage;
154-
if (getrusage(RUSAGE_SELF, &usage))
155-
return 0;
156-
if (LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD ||
157-
LIBFUZZER_OPENBSD) {
158-
// ru_maxrss is in KiB
159-
return usage.ru_maxrss >> 10;
160-
} else if (LIBFUZZER_APPLE) {
161-
// ru_maxrss is in bytes
162-
return usage.ru_maxrss >> 20;
163-
}
164-
assert(0 && "GetPeakRSSMb() is not implemented for your platform");
165-
return 0;
166-
}
167-
168-
// Based on SetSigaction in FuzzerUtil.cpp
169-
static void SetSigaction(int signum,
170-
void (*callback)(int, siginfo_t *, void *)) {
171-
struct sigaction sigact;
172-
memset(&sigact, 0, sizeof(sigact));
173-
sigact.sa_sigaction = callback;
174-
if (sigaction(signum, &sigact, 0)) {
175-
fprintf(stderr, "libFuzzer: sigaction failed with %d\n", errno);
176-
exit(1);
177-
}
178-
}
179-
180-
// Write extra stats to the file specified by the user. If none is specified
181-
// this function will never be called.
182-
static void write_extra_stats() {
183-
uint32_t peak_rss = GetPeakRSSMb();
184-
185-
if (peak_rss < previous_peak_rss)
186-
peak_rss = previous_peak_rss;
187-
188-
int chars_printed = fprintf(extra_stats_file, kExtraStatsFormatString,
189-
peak_rss, slowest_unit_time_secs);
190-
191-
CHECK_ERROR(chars_printed != 0, "Failed to write extra_stats_file");
192-
193-
CHECK_ERROR(fclose(extra_stats_file) == 0,
194-
"Failed to close extra_stats_file");
195-
}
196-
197-
// Call write_extra_stats before we exit.
198-
static void crash_handler(int, siginfo_t *, void *) {
199-
// Make sure we don't try calling write_extra_stats again if we crashed while
200-
// trying to call it.
201-
static bool first_crash = true;
202-
CHECK_ERROR(first_crash,
203-
"Crashed in crash signal handler. This is a bug in the fuzzer.");
204-
205-
first_crash = false;
206-
write_extra_stats();
207-
}
208-
209-
// If the user has specified an extra_stats_file through the environment
210-
// variable AFL_DRIVER_EXTRA_STATS_FILENAME, then perform necessary set up
211-
// to write stats to it on exit. If no file is specified, do nothing. Otherwise
212-
// install signal and exit handlers to write to the file when the process exits.
213-
// Then if the file doesn't exist create it and set extra stats to 0. But if it
214-
// does exist then read the initial values of the extra stats from the file
215-
// and check that the file is writable.
216-
static void maybe_initialize_extra_stats() {
217-
// If AFL_DRIVER_EXTRA_STATS_FILENAME isn't set then we have nothing to do.
218-
char *extra_stats_filename = getenv("AFL_DRIVER_EXTRA_STATS_FILENAME");
219-
if (!extra_stats_filename)
220-
return;
221-
222-
// Open the file and find the previous peak_rss_mb value.
223-
// This is necessary because the fuzzing process is restarted after N
224-
// iterations are completed. So we may need to get this value from a previous
225-
// process to be accurate.
226-
extra_stats_file = fopen(extra_stats_filename, "r");
227-
228-
// If extra_stats_file already exists: read old stats from it.
229-
if (extra_stats_file) {
230-
int matches = fscanf(extra_stats_file, kExtraStatsFormatString,
231-
&previous_peak_rss, &slowest_unit_time_secs);
232-
233-
// Make sure we have read a real extra stats file and that we have used it
234-
// to set slowest_unit_time_secs and previous_peak_rss.
235-
CHECK_ERROR(matches == kNumExtraStats, "Extra stats file is corrupt");
236-
237-
CHECK_ERROR(fclose(extra_stats_file) == 0, "Failed to close file");
238-
239-
// Now open the file for writing.
240-
extra_stats_file = fopen(extra_stats_filename, "w");
241-
CHECK_ERROR(extra_stats_file,
242-
"Failed to open extra stats file for writing");
243-
} else {
244-
// Looks like this is the first time in a fuzzing job this is being called.
245-
extra_stats_file = fopen(extra_stats_filename, "w+");
246-
CHECK_ERROR(extra_stats_file, "failed to create extra stats file");
247-
}
248-
249-
// Make sure that crash_handler gets called on any kind of fatal error.
250-
int crash_signals[] = {SIGSEGV, SIGBUS, SIGABRT, SIGILL, SIGFPE, SIGINT,
251-
SIGTERM};
252-
253-
const size_t num_signals = sizeof(crash_signals) / sizeof(crash_signals[0]);
254-
255-
for (size_t idx = 0; idx < num_signals; idx++)
256-
SetSigaction(crash_signals[idx], crash_handler);
257-
258-
// Make sure it gets called on other kinds of exits.
259-
atexit(write_extra_stats);
260-
}
261-
262118
// If the user asks us to duplicate stderr, then do it.
263119
static void maybe_duplicate_stderr() {
264120
char* stderr_duplicate_filename =
@@ -323,7 +179,6 @@ int main(int argc, char **argv) {
323179
// Do any other expensive one-time initialization here.
324180

325181
maybe_duplicate_stderr();
326-
maybe_initialize_extra_stats();
327182

328183
if (!getenv("AFL_DRIVER_DONT_DEFER"))
329184
__afl_manual_init();
@@ -344,7 +199,6 @@ int main(int argc, char **argv) {
344199
uint8_t dummy_input[1] = {0};
345200
LLVMFuzzerTestOneInput(dummy_input, 1);
346201

347-
time_t unit_time_secs;
348202
int num_runs = 0;
349203
while (__afl_persistent_loop(N)) {
350204
ssize_t n_read = read(0, AflInputBuf, kMaxAflInputSize);
@@ -353,23 +207,8 @@ int main(int argc, char **argv) {
353207
// overflows. Don't use unique_ptr/etc to avoid extra dependencies.
354208
uint8_t *copy = new uint8_t[n_read];
355209
memcpy(copy, AflInputBuf, n_read);
356-
357-
struct timeval unit_start_time;
358-
CHECK_ERROR(gettimeofday(&unit_start_time, NULL) == 0,
359-
"Calling gettimeofday failed");
360-
361210
num_runs++;
362211
LLVMFuzzerTestOneInput(copy, n_read);
363-
364-
struct timeval unit_stop_time;
365-
CHECK_ERROR(gettimeofday(&unit_stop_time, NULL) == 0,
366-
"Calling gettimeofday failed");
367-
368-
// Update slowest_unit_time_secs if we see a new max.
369-
unit_time_secs = unit_stop_time.tv_sec - unit_start_time.tv_sec;
370-
if (slowest_unit_time_secs < unit_time_secs)
371-
slowest_unit_time_secs = unit_time_secs;
372-
373212
delete[] copy;
374213
}
375214
}

0 commit comments

Comments
 (0)