Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 7cec6c6

Browse files
committed
[libFuzzer] don't create too many trace-based mutations as it may be too slow
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@259600 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e1e04cb commit 7cec6c6

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/Fuzzer/FuzzerTraceState.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ class TraceState {
259259
Signed >>= 16;
260260
return Signed == 0 || Signed == -1L;
261261
}
262+
263+
// We don't want to create too many trace-based mutations as it is both
264+
// expensive and useless. So after some number of mutations is collected,
265+
// start rejecting some of them. The more there are mutations the more we
266+
// reject.
267+
bool WantToHandleOneMoreMutation() {
268+
const size_t FirstN = 64;
269+
// Gladly handle first N mutations.
270+
if (NumMutations <= FirstN) return true;
271+
size_t Diff = NumMutations - FirstN;
272+
size_t DiffLog = sizeof(long) * 8 - __builtin_clzl((long)Diff);
273+
assert(DiffLog > 0 && DiffLog < 64);
274+
bool WantThisOne = USF.GetRand()(1 << DiffLog) == 0; // 1 out of DiffLog.
275+
return WantThisOne;
276+
}
277+
262278
static const size_t kMaxMutations = 1 << 16;
263279
size_t NumMutations;
264280
TraceBasedMutation Mutations[kMaxMutations];
@@ -362,7 +378,7 @@ void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits,
362378

363379
int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
364380
size_t DataSize) {
365-
if (NumMutations >= kMaxMutations) return 0;
381+
if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0;
366382
int Res = 0;
367383
const uint8_t *Beg = *CurrentUnitData;
368384
const uint8_t *End = Beg + *CurrentUnitSize;
@@ -383,7 +399,7 @@ int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
383399
int TraceState::TryToAddDesiredData(const uint8_t *PresentData,
384400
const uint8_t *DesiredData,
385401
size_t DataSize) {
386-
if (NumMutations >= kMaxMutations) return 0;
402+
if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0;
387403
int Res = 0;
388404
const uint8_t *Beg = *CurrentUnitData;
389405
const uint8_t *End = Beg + *CurrentUnitSize;

0 commit comments

Comments
 (0)