summaryrefslogtreecommitdiff
path: root/mangle.c
diff options
context:
space:
mode:
authorRobert Swiecki <robert@swiecki.net>2018-01-13 14:03:39 +0100
committerRobert Swiecki <robert@swiecki.net>2018-01-13 14:03:39 +0100
commit0f2c30afccb75ec6a5c29131184c2b672cd5b95d (patch)
tree0cafbfdfaf1cb18663c3446db23377ac81e6fb17 /mangle.c
parent43775f95d9e6b8c46c52e0454b8721d023b9236b (diff)
downloadhonggfuzz-0f2c30afccb75ec6a5c29131184c2b672cd5b95d.tar.gz
move input preparation from mangle to input
Diffstat (limited to 'mangle.c')
-rw-r--r--mangle.c130
1 files changed, 4 insertions, 126 deletions
diff --git a/mangle.c b/mangle.c
index d4e82f07..ea253e81 100644
--- a/mangle.c
+++ b/mangle.c
@@ -36,23 +36,8 @@
#include "input.h"
#include "libhfcommon/common.h"
-#include "libhfcommon/files.h"
#include "libhfcommon/log.h"
#include "libhfcommon/util.h"
-#include "subproc.h"
-
-static void mangle_setSize(run_t* run, size_t sz) {
- if (sz > run->global->maxFileSz) {
- PLOG_F("Too large size requested: %zu > maxSize: %zu", sz, run->global->maxFileSz);
- }
- if (ftruncate(run->dynamicFileFd, sz) == -1) {
- PLOG_F("ftruncate(fd=%d, size=%zu)", run->dynamicFileFd, sz);
- }
- if (lseek(run->dynamicFileFd, (off_t)0, SEEK_SET) == (off_t)-1) {
- PLOG_F("lseek(fd=%d, 0, SEEK_SET)", run->dynamicFileFd);
- }
- run->dynamicFileSz = sz;
-}
static inline void mangle_Overwrite(run_t* run, const uint8_t* src, size_t off, size_t sz) {
size_t maxToCopy = run->dynamicFileSz - off;
@@ -92,7 +77,7 @@ static void mangle_Inflate(run_t* run, size_t off, size_t len) {
len = run->global->maxFileSz - run->dynamicFileSz;
}
- mangle_setSize(run, run->dynamicFileSz + len);
+ input_setSize(run, run->dynamicFileSz + len);
mangle_Move(run, off, off + len, run->dynamicFileSz);
}
@@ -511,7 +496,7 @@ static void mangle_CloneByte(run_t* run) {
static void mangle_Resize(run_t* run) {
size_t sz = util_rndGet(0, run->global->maxFileSz);
- mangle_setSize(run, sz);
+ input_setSize(run, sz);
}
static void mangle_Expand(run_t* run) {
@@ -530,7 +515,7 @@ static void mangle_Shrink(run_t* run) {
size_t len = util_rndGet(1, run->dynamicFileSz - 1);
size_t off = util_rndGet(0, len);
- mangle_setSize(run, run->dynamicFileSz - len);
+ input_setSize(run, run->dynamicFileSz - len);
mangle_Move(run, off + len, off, run->dynamicFileSz);
}
@@ -551,7 +536,7 @@ static void mangle_ASCIIVal(run_t* run) {
mangle_Overwrite(run, (uint8_t*)buf, off, strlen(buf));
}
-static void mangle_mangleContent(run_t* run) {
+void mangle_mangleContent(run_t* run) {
if (run->mutationsPerRun == 0U) {
return;
}
@@ -595,110 +580,3 @@ static void mangle_mangleContent(run_t* run) {
mangleFuncs[choice](run);
}
}
-
-static bool mangle_checkSizeNRewind(run_t* run) {
- struct stat st;
- if (fstat(run->dynamicFileFd, &st) == -1) {
- PLOG_E("fstat(fd=%d)", run->dynamicFileFd);
- return false;
- }
- if ((size_t)st.st_size > run->global->maxFileSz) {
- LOG_W("External tool created too large of a file, '%zu', truncating it to '%zu'",
- (size_t)st.st_size, run->global->maxFileSz);
- mangle_setSize(run, run->global->maxFileSz);
- } else {
- mangle_setSize(run, (size_t)st.st_size);
- }
- return true;
-}
-
-bool mangle_prepareDynamicInput(run_t* run) {
- run->origFileName = "[DYNAMIC]";
-
- {
- MX_SCOPED_RWLOCK_READ(&run->global->dynfileq_mutex);
-
- if (run->global->dynfileqCnt == 0) {
- LOG_F(
- "The dynamic file corpus is empty. Apparently, the initial fuzzing of the "
- "provided file corpus (-f) has not produced any follow-up files with positive "
- "coverage and/or CPU counters");
- }
-
- if (run->dynfileqCurrent == NULL) {
- run->dynfileqCurrent = TAILQ_FIRST(&run->global->dynfileq);
- } else {
- if (run->dynfileqCurrent == TAILQ_LAST(&run->global->dynfileq, dyns_t)) {
- run->dynfileqCurrent = TAILQ_FIRST(&run->global->dynfileq);
- } else {
- run->dynfileqCurrent = TAILQ_NEXT(run->dynfileqCurrent, pointers);
- }
- }
- }
-
- mangle_setSize(run, run->dynfileqCurrent->size);
- memcpy(run->dynamicFile, run->dynfileqCurrent->data, run->dynfileqCurrent->size);
- mangle_mangleContent(run);
-
- return true;
-}
-
-bool mangle_prepareStaticFile(run_t* run, bool rewind) {
- mangle_setSize(run, run->global->maxFileSz);
-
- static __thread char fname[PATH_MAX];
- if (input_getNext(run, fname, /* rewind= */ rewind) == false) {
- return false;
- }
- run->origFileName = files_basename(fname);
-
- ssize_t fileSz = files_readFileToBufMax(fname, run->dynamicFile, run->global->maxFileSz);
- if (fileSz < 0) {
- LOG_E("Couldn't read contents of '%s'", fname);
- return false;
- }
-
- mangle_setSize(run, fileSz);
- mangle_mangleContent(run);
-
- return true;
-}
-
-bool mangle_prepareExternalFile(run_t* run) {
- mangle_setSize(run, (size_t)0);
- run->origFileName = "[EXTERNAL]";
-
- char fname[PATH_MAX];
- snprintf(fname, sizeof(fname), "/dev/fd/%d", run->dynamicFileFd);
-
- const char* const argv[] = {run->global->exe.externalCommand, fname, NULL};
- if (subproc_System(run, argv) != 0) {
- LOG_E("Subprocess '%s' returned abnormally", run->global->exe.externalCommand);
- return false;
- }
- LOG_D("Subporcess '%s' finished with success", run->global->exe.externalCommand);
-
- if (!mangle_checkSizeNRewind(run)) {
- return false;
- }
-
- return true;
-}
-
-bool mangle_postProcessFile(run_t* run) {
- char fname[PATH_MAX];
- snprintf(fname, sizeof(fname), "/dev/fd/%d", run->dynamicFileFd);
-
- const char* const argv[] = {run->global->exe.postExternalCommand, fname, NULL};
- if (subproc_System(run, argv) != 0) {
- LOG_E("Subprocess '%s' returned abnormally", run->global->exe.postExternalCommand);
- return false;
- }
- LOG_D("Subporcess '%s' finished with success", run->global->exe.externalCommand);
-
- if (!mangle_checkSizeNRewind(run)) {
- return false;
- }
-
- return true;
-}