59 #include <sys/resource.h>
69 #define LIBFUZZER_LINUX 1
70 #define LIBFUZZER_APPLE 0
72 #define LIBFUZZER_LINUX 0
73 #define LIBFUZZER_APPLE 1
75 #error "Support for your platform has not been implemented"
83 #define CHECK_ERROR(cond, error_message) \
85 fprintf(stderr, (error_message)); \
115 "slowest_unit_time_sec : %u\n";
120 if (getrusage(RUSAGE_SELF, &usage))
122 if (LIBFUZZER_LINUX) {
124 return usage.ru_maxrss >> 10;
125 }
else if (LIBFUZZER_APPLE) {
127 return usage.ru_maxrss >> 20;
129 assert(0 &&
"GetPeakRSSMb() is not implemented for your platform");
135 void (*callback)(
int, siginfo_t *,
void *)) {
136 struct sigaction sigact;
137 memset(&sigact, 0,
sizeof(sigact));
138 sigact.sa_sigaction = callback;
139 if (sigaction(signum, &sigact, 0)) {
140 fprintf(stderr,
"libFuzzer: sigaction failed with %d\n", errno);
150 if (peak_rss < previous_peak_rss)
153 int chars_printed = fprintf(extra_stats_file, kExtraStatsFormatString,
154 peak_rss, slowest_unit_time_secs);
156 CHECK_ERROR(chars_printed != 0,
"Failed to write extra_stats_file");
159 "Failed to close extra_stats_file");
166 static bool first_crash =
true;
168 "Crashed in crash signal handler. This is a bug in the fuzzer.");
183 char *extra_stats_filename = getenv(
"AFL_DRIVER_EXTRA_STATS_FILENAME");
184 if (!extra_stats_filename)
191 extra_stats_file = fopen(extra_stats_filename,
"r");
194 if (extra_stats_file) {
195 int matches = fscanf(extra_stats_file, kExtraStatsFormatString,
196 &previous_peak_rss, &slowest_unit_time_secs);
200 CHECK_ERROR(matches == kNumExtraStats,
"Extra stats file is corrupt");
202 CHECK_ERROR(fclose(extra_stats_file) == 0,
"Failed to close file");
205 extra_stats_file = fopen(extra_stats_filename,
"w");
207 "Failed to open extra stats file for writing");
210 extra_stats_file = fopen(extra_stats_filename,
"w+");
211 CHECK_ERROR(extra_stats_file,
"failed to create extra stats file");
215 int crash_signals[] = {SIGSEGV, SIGBUS, SIGABRT, SIGILL, SIGFPE, SIGINT,
218 const size_t num_signals =
sizeof(crash_signals) /
sizeof(crash_signals[0]);
220 for (
size_t idx = 0; idx < num_signals; idx++)
229 char* stderr_duplicate_filename =
230 getenv(
"AFL_DRIVER_STDERR_DUPLICATE_FILENAME");
232 if (!stderr_duplicate_filename)
235 FILE* stderr_duplicate_stream =
236 freopen(stderr_duplicate_filename,
"a+", stderr);
238 if (!stderr_duplicate_stream) {
241 "Failed to duplicate stderr to AFL_DRIVER_STDERR_DUPLICATE_FILENAME");
249 assert(
false &&
"LLVMFuzzerMutate should not be called from afl_driver");
255 for (
int i = 1; i < argc; i++) {
256 std::ifstream in(argv[i]);
258 size_t length = in.tellg();
259 in.seekg (0, in.beg);
260 std::cout <<
"Reading " << length <<
" bytes from " << argv[i] << std::endl;
262 std::vector<char> bytes(length);
263 in.read(bytes.data(), bytes.size());
267 std::cout <<
"Execution successfull" << std::endl;
272 int main(
int argc,
char **argv) {
274 "======================= INFO =========================\n"
275 "This binary is built for AFL-fuzz.\n"
276 "To run the target function on individual input(s) execute this:\n"
279 " %s INPUT_FILE1 [INPUT_FILE2 ... ]\n"
280 "To fuzz with afl-fuzz execute this:\n"
281 " afl-fuzz [afl-flags] %s [-N]\n"
282 "afl-fuzz will run N iterations before "
283 "re-spawning the process (default: 1000)\n"
284 "======================================================\n",
285 argv[0], argv[0], argv[0]);
296 if (argc == 2 && argv[1][0] ==
'-')
297 N = atoi(argv[1] + 1);
298 else if(argc == 2 && (N = atoi(argv[1])) > 0)
299 fprintf(stderr,
"WARNING: using the deprecated call style `%s %d`\n",
305 time_t unit_time_secs;
308 ssize_t n_read =
read(0, AflInputBuf, kMaxAflInputSize);
312 uint8_t *copy =
new uint8_t[n_read];
313 memcpy(copy, AflInputBuf, n_read);
315 struct timeval unit_start_time;
316 CHECK_ERROR(gettimeofday(&unit_start_time, NULL) == 0,
317 "Calling gettimeofday failed");
322 struct timeval unit_stop_time;
323 CHECK_ERROR(gettimeofday(&unit_stop_time, NULL) == 0,
324 "Calling gettimeofday failed");
327 unit_time_secs = unit_stop_time.tv_sec - unit_start_time.tv_sec;
328 if (slowest_unit_time_secs < unit_time_secs)
329 slowest_unit_time_secs = unit_time_secs;
334 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
int ExecuteFilesOnyByOne(int argc, char **argv)
static const char * kExtraStatsFormatString
static volatile char suppress_warning1
size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize)
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()
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
static const size_t kMaxAflInputSize
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void maybe_initialize_extra_stats()