60 #include <sys/resource.h>
64 #define LIBFUZZER_LINUX 1
65 #define LIBFUZZER_APPLE 0
67 #define LIBFUZZER_LINUX 0
68 #define LIBFUZZER_APPLE 1
70 #error "Support for your platform has not been implemented"
78 #define CHECK_ERROR(cond, error_message) \
80 fprintf(stderr, (error_message)); \
110 "slowest_unit_time_sec : %u\n";
115 if (getrusage(RUSAGE_SELF, &usage))
117 if (LIBFUZZER_LINUX) {
119 return usage.ru_maxrss >> 10;
120 }
else if (LIBFUZZER_APPLE) {
122 return usage.ru_maxrss >> 20;
124 assert(0 &&
"GetPeakRSSMb() is not implemented for your platform");
130 void (*callback)(
int, siginfo_t *,
void *)) {
131 struct sigaction sigact;
132 memset(&sigact, 0,
sizeof(sigact));
133 sigact.sa_sigaction = callback;
134 if (sigaction(signum, &sigact, 0)) {
135 fprintf(stderr,
"libFuzzer: sigaction failed with %d\n", errno);
145 if (peak_rss < previous_peak_rss)
148 int chars_printed = fprintf(extra_stats_file, kExtraStatsFormatString,
149 peak_rss, slowest_unit_time_secs);
151 CHECK_ERROR(chars_printed != 0,
"Failed to write extra_stats_file");
154 "Failed to close extra_stats_file");
161 static bool first_crash =
true;
163 "Crashed in crash signal handler. This is a bug in the fuzzer.");
178 char *extra_stats_filename = getenv(
"AFL_DRIVER_EXTRA_STATS_FILENAME");
179 if (!extra_stats_filename)
186 extra_stats_file = fopen(extra_stats_filename,
"r");
189 if (extra_stats_file) {
190 int matches = fscanf(extra_stats_file, kExtraStatsFormatString,
191 &previous_peak_rss, &slowest_unit_time_secs);
195 CHECK_ERROR(matches == kNumExtraStats,
"Extra stats file is corrupt");
197 CHECK_ERROR(fclose(extra_stats_file) == 0,
"Failed to close file");
200 extra_stats_file = fopen(extra_stats_filename,
"w");
202 "Failed to open extra stats file for writing");
205 extra_stats_file = fopen(extra_stats_filename,
"w+");
206 CHECK_ERROR(extra_stats_file,
"failed to create extra stats file");
210 int crash_signals[] = {SIGSEGV, SIGBUS, SIGABRT, SIGILL, SIGFPE, SIGINT,
213 const size_t num_signals =
sizeof(crash_signals) /
sizeof(crash_signals[0]);
215 for (
size_t idx = 0; idx < num_signals; idx++)
224 char* stderr_duplicate_filename =
225 getenv(
"AFL_DRIVER_STDERR_DUPLICATE_FILENAME");
227 if (!stderr_duplicate_filename)
230 FILE* stderr_duplicate_stream =
231 freopen(stderr_duplicate_filename,
"a+", stderr);
233 if (!stderr_duplicate_stream) {
236 "Failed to duplicate stderr to AFL_DRIVER_STDERR_DUPLICATE_FILENAME");
241 int main(
int argc,
char **argv) {
242 fprintf(stderr,
"======================= INFO =========================\n"
243 "This binary is built for AFL-fuzz.\n"
244 "To run the target function on a single input execute this:\n"
246 "To run the fuzzing execute this:\n"
247 " afl-fuzz [afl-flags] %s [N] "
248 "-- run N fuzzing iterations before "
249 "re-spawning the process (default: 1000)\n"
250 "======================================================\n",
265 time_t unit_time_secs;
268 ssize_t n_read =
read(0, AflInputBuf, kMaxAflInputSize);
272 uint8_t *copy =
new uint8_t[n_read];
273 memcpy(copy, AflInputBuf, n_read);
275 struct timeval unit_start_time;
276 CHECK_ERROR(gettimeofday(&unit_start_time, NULL) == 0,
277 "Calling gettimeofday failed");
282 struct timeval unit_stop_time;
283 CHECK_ERROR(gettimeofday(&unit_stop_time, NULL) == 0,
284 "Calling gettimeofday failed");
287 unit_time_secs = unit_stop_time.tv_sec - unit_start_time.tv_sec;
288 if (slowest_unit_time_secs < unit_time_secs)
289 slowest_unit_time_secs = unit_time_secs;
294 fprintf(stderr,
"%s: successfully executed %d input(s)\n", argv[0], num_runs);
static void SetSigaction(int signum, void(*callback)(int, siginfo_t *, void *))
static uint32_t previous_peak_rss
value_type read(const void *memory)
Read a value of a particular endianness from memory.
static const char * kExtraStatsFormatString
static volatile char suppress_warning1
static volatile char suppress_warning2
static FILE * extra_stats_file
static const int kNumExtraStats
static void maybe_duplicate_stderr()
static volatile char AFL_DEFER_FORKSVR[]
__attribute__((weak)) int LLVMFuzzerInitialize(int *argc
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
int __afl_persistent_loop(unsigned int)
static uint8_t AflInputBuf[kMaxAflInputSize]
static void crash_handler(int, siginfo_t *, void *)
static time_t slowest_unit_time_secs
int main(int argc, char **argv)
static volatile char AFL_PERSISTENT[]
#define CHECK_ERROR(cond, error_message)
int LLVMFuzzerInitialize(int *argc, char ***argv)
static void write_extra_stats()
static const size_t kMaxAflInputSize
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void maybe_initialize_extra_stats()