Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
merged
Browse files Browse the repository at this point in the history
  • Loading branch information
domenukk committed Sep 22, 2020
2 parents 6f55dfb + ef370d0 commit 0a6dce1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
3 changes: 3 additions & 0 deletions include/afl-returns.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ typedef enum afl_ret {
AFL_RET_NO_FUZZ_WORKERS,
AFL_RET_TRIM_FAIL,
AFL_RET_ERROR_INPUT_COPY,
AFL_RET_EMPTY,

} afl_ret_t;

Expand All @@ -67,6 +68,8 @@ static inline char *afl_ret_stringify(afl_ret_t afl_ret) {
return "Target did not behave as expected";
case AFL_RET_ERROR_INPUT_COPY:
return "Error creating input copy";
case AFL_RET_EMPTY:
return "Empty data";
case AFL_RET_ALLOC:
if (!errno) { return "Allocation failed"; }
/* fall-through */
Expand Down
92 changes: 61 additions & 31 deletions src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include <dirent.h>
#include <time.h>
#include <limits.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "engine.h"
#include "aflpp.h"
#include "afl-returns.h"
Expand Down Expand Up @@ -152,50 +154,47 @@ afl_ret_t afl_engine_add_feedback(afl_engine_t *engine, afl_feedback_t *feedback

}

afl_ret_t afl_engine_load_testcases_from_dir(afl_engine_t *engine, char *dirpath,
afl_input_t *(*custom_input_new)(void)) {
afl_ret_t __afl_engine_load_testcases_from_dir(afl_engine_t *engine, char *dirpath,
afl_input_t *(*custom_input_new)(void)) {

DIR * dir_in = NULL;
struct dirent *dir_ent = NULL;
char infile[PATH_MAX];
size_t i;
uint32_t ok = 0;

afl_input_t *input;
size_t dir_name_size = strlen(dirpath);

if (dirpath[dir_name_size - 1] == '/') { dirpath[dir_name_size - 1] = '\x00'; }

if (!(dir_in = opendir(dirpath))) { return AFL_RET_FILE_OPEN_ERROR; }

/* Since, this'll be the first execution, Let's start up the executor here */

if ((engine->executions == 0) && engine->executor->funcs.init_cb) {

AFL_TRY(engine->executor->funcs.init_cb(engine->executor), {
while ((dir_ent = readdir(dir_in))) {

closedir(dir_in);
return err;
if (dir_ent->d_name[0] == '.') {

});
continue; // skip anything that starts with '.'

}
}

while ((dir_ent = readdir(dir_in))) {
snprintf((char *)infile, sizeof(infile), "%s/%s", dirpath, dir_ent->d_name);
infile[sizeof(infile) - 1] = '\0';

if (dir_ent->d_name[0] == '.') {
/* TODO: Error handling? */
struct stat st;
if (access(infile, R_OK) != 0 || stat(infile, &st) != 0) continue;
if (S_ISDIR(st.st_mode)) {

continue; // skip anything that starts with '.'
if (__afl_engine_load_testcases_from_dir(engine, infile, custom_input_new) == AFL_RET_SUCCESS) ok = 1;
continue;

}

if (!S_ISREG(st.st_mode)) continue;

/* TODO: Not sure if this makes any sense at all? */
if (custom_input_new) {

input = custom_input_new();

}

else {
} else {

input = afl_input_new();

Expand All @@ -210,20 +209,26 @@ afl_ret_t afl_engine_load_testcases_from_dir(afl_engine_t *engine, char *dirpath

}

snprintf((char *)infile, sizeof(infile), "%s/%s", dirpath, dir_ent->d_name);
infile[sizeof(infile) - 1] = '\0';

AFL_TRY(input->funcs.load_from_file(input, infile), {return err;});
AFL_TRY(input->funcs.load_from_file(input, infile), {

if (!input->len) {
DBG("Empty input read from %s", infile);
WARNF("Error loading seed %s: %s", infile, afl_ret_stringify(err));
free(input);
continue;
}

});

afl_ret_t run_result = engine->funcs.execute(engine, input);

if (engine->verbose) OKF("Loaded seed %s", infile);
if (run_result == AFL_RET_SUCCESS) {

if (engine->verbose) OKF("Loaded seed %s", infile);
ok = 1;

} else {

WARNF("Error loading seed %s", infile);

}

/* We add the corpus to the queue initially for all the feedback queues */

Expand All @@ -246,7 +251,32 @@ afl_ret_t afl_engine_load_testcases_from_dir(afl_engine_t *engine, char *dirpath

closedir(dir_in);

return AFL_RET_SUCCESS;
if (ok)
return AFL_RET_SUCCESS;
else
return AFL_RET_EMPTY;

}

afl_ret_t afl_engine_load_testcases_from_dir(afl_engine_t *engine, char *dirpath,
afl_input_t *(*custom_input_new)(void)) {

size_t dir_name_size = strlen(dirpath);
if (dirpath[dir_name_size - 1] == '/') { dirpath[dir_name_size - 1] = 0; }
if (access(dirpath, R_OK | X_OK) != 0) return AFL_RET_FILE_OPEN_ERROR;

/* Since, this'll be the first execution, Let's start up the executor here */
if ((engine->executions == 0) && engine->executor->funcs.init_cb) {

AFL_TRY(engine->executor->funcs.init_cb(engine->executor), {

return err;

});

}

return __afl_engine_load_testcases_from_dir(engine, dirpath, custom_input_new);

}

Expand Down

0 comments on commit 0a6dce1

Please sign in to comment.