Skip to content

Commit 3de7ab0

Browse files
committed
Check coverage for imported seeds
1 parent 974db6a commit 3de7ab0

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

Diff for: fuzz.c

+15
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ static void fuzz_setDynamicMainState(run_t* run) {
127127
.timeExecUSecs = 1,
128128
.path = "[DYNAMIC-0-SIZE]",
129129
.timedout = false,
130+
.imported = false,
130131
.data = (uint8_t*)"",
131132
};
132133
dynfile_t* tmp_dynfile = run->dynfile;
@@ -277,12 +278,26 @@ static void fuzz_perfFeedback(run_t* run) {
277278
run->dynfile->cov[1] = softCurCmp;
278279
run->dynfile->cov[2] = run->hwCnts.cpuInstrCnt + run->hwCnts.cpuBranchCnt;
279280
run->dynfile->cov[3] = run->dynfile->size ? (64 - util_Log2(run->dynfile->size)) : 64;
281+
282+
/* Push useful imported input to dynamic queue again for the further mutations */
283+
if (run->dynfile->imported) {
284+
LOG_I("File imported: %s", run->dynfile->path);
285+
run->dynfile->imported = false;
286+
}
280287
input_addDynamicInput(run);
281288

282289
if (run->global->socketFuzzer.enabled) {
283290
LOG_D("SocketFuzzer: fuzz: new BB (perf)");
284291
fuzz_notifySocketFuzzerNewCov(run->global);
285292
}
293+
} else if (run->dynfile->imported) {
294+
/* Remove useless imported inputs from corpus */
295+
LOG_D("Removing useless imported file: %s", run->dynfile->path);
296+
char fname[PATH_MAX];
297+
snprintf(fname, PATH_MAX, "%s/%s",
298+
run->global->io.outputDir ? run->global->io.outputDir : run->global->io.inputDir,
299+
run->dynfile->path);
300+
unlink(fname);
286301
}
287302
}
288303

Diff for: honggfuzz.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,13 @@ static uint8_t mainThreadLoop(honggfuzz_t* hfuzz) {
265265
setupSignalsMainThread();
266266
setupMainThreadTimer();
267267

268+
uint64_t dynamicQueuePollTime = time(NULL);
268269
for (;;) {
269-
if (hfuzz->io.dynamicInputDir) {
270+
if (hfuzz->io.dynamicInputDir &&
271+
time(NULL) - dynamicQueuePollTime > _HF_SYNC_TIME) {
270272
LOG_D("Loading files from the dynamic input queue...");
271273
input_enqueueDynamicInputs(hfuzz);
274+
dynamicQueuePollTime = time(NULL);
272275
}
273276

274277
if (hfuzz->display.useScreen) {

Diff for: honggfuzz.h

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
/* Default maximum size of produced inputs */
7878
#define _HF_INPUT_DEFAULT_SIZE (1024ULL * 8)
7979

80+
/* Time (seconds) between checking dynamic input directory to import files */
81+
#define _HF_SYNC_TIME 10
82+
8083
/* Per-thread bitmap */
8184
#define _HF_PERTHREAD_BITMAP_FD 1018
8285
/* FD used to report back used int/str constants from the fuzzed process */
@@ -156,6 +159,7 @@ struct _dynfile_t {
156159
fuzzState_t phase;
157160
bool timedout;
158161
uint8_t* data;
162+
bool imported;
159163
TAILQ_ENTRY(_dynfile_t) pointers;
160164
};
161165

Diff for: input.c

+19
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ void input_addDynamicInput(run_t* run) {
377377
dynfile->timeExecUSecs = util_timeNowUSecs() - run->timeStartedUSecs;
378378
dynfile->data = (uint8_t*)util_AllocCopy(run->dynfile->data, run->dynfile->size);
379379
dynfile->src = run->dynfile->src;
380+
dynfile->imported = run->dynfile->imported,
380381
memcpy(dynfile->cov, run->dynfile->cov, sizeof(dynfile->cov));
381382
if (run->dynfile->src) {
382383
ATOMIC_POST_INC(run->dynfile->src->refs);
@@ -551,7 +552,13 @@ bool input_prepareDynamicInput(run_t* run, bool needs_mangle) {
551552
run->current = run->global->io.dynfileqCurrent;
552553
run->global->io.dynfileqCurrent = TAILQ_NEXT(run->global->io.dynfileqCurrent, pointers);
553554

555+
/* Do not count skip_factor on unmeasured (imported) inputs */
556+
if (run->current->imported) {
557+
break;
558+
}
559+
554560
int skip_factor = input_skipFactor(run, run->current);
561+
555562
if (skip_factor <= 0) {
556563
run->triesLeft = -(skip_factor);
557564
break;
@@ -569,10 +576,21 @@ bool input_prepareDynamicInput(run_t* run, bool needs_mangle) {
569576
run->dynfile->refs = 0;
570577
run->dynfile->phase = fuzz_getState(run->global);
571578
run->dynfile->timedout = run->current->timedout;
579+
run->dynfile->imported = run->current->imported;
572580
memcpy(run->dynfile->cov, run->current->cov, sizeof(run->dynfile->cov));
573581
snprintf(run->dynfile->path, sizeof(run->dynfile->path), "%s", run->current->path);
574582
memcpy(run->dynfile->data, run->current->data, run->current->size);
575583

584+
/* Run unmangled imported input to measure coverage. It would be added
585+
to dynamic queue again in case of profit.
586+
*/
587+
if (run->current->imported) {
588+
TAILQ_REMOVE(&run->global->io.dynfileq, run->current, pointers);
589+
ATOMIC_POST_DEC(run->global->io.newUnitsAdded);
590+
run->triesLeft = 0;
591+
return true;
592+
}
593+
576594
if (needs_mangle) {
577595
mangle_mangleContent(run);
578596
}
@@ -677,6 +695,7 @@ void input_enqueueDynamicInputs(honggfuzz_t* hfuzz) {
677695
.timeExecUSecs = 1,
678696
.path = "",
679697
.timedout = false,
698+
.imported = true,
680699
.data = dynamicFile,
681700
};
682701
tmp_run.timeStartedUSecs = util_timeNowUSecs() - 1;

0 commit comments

Comments
 (0)