Index: lib/fuzzer/FuzzerLoop.cpp =================================================================== --- lib/fuzzer/FuzzerLoop.cpp +++ lib/fuzzer/FuzzerLoop.cpp @@ -525,6 +525,8 @@ bool DuringInitialCorpusExecution) { if (!HasMoreMallocsThanFrees) return; // mallocs==frees, a leak is unlikely. if (!Options.DetectLeaks) return; + if (!DuringInitialCorpusExecution && + TotalNumberOfRuns >= Options.MaxNumberOfRuns) return; if (!&(EF->__lsan_enable) || !&(EF->__lsan_disable) || !(EF->__lsan_do_recoverable_leak_check)) return; // No lsan. Index: test/fuzzer/MaxNumberOfRunsTest.cpp =================================================================== --- /dev/null +++ test/fuzzer/MaxNumberOfRunsTest.cpp @@ -0,0 +1,15 @@ +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. + +// Make sure libFuzzer respects `-runs=N` value even when testing a memory leak. +#include +#include + +static volatile size_t RunsCounter; + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { + ++RunsCounter; + char* ptr = new char; + if (RunsCounter != 4) delete ptr; + return 0; +} Index: test/fuzzer/max-number-of-runs.test =================================================================== --- /dev/null +++ test/fuzzer/max-number-of-runs.test @@ -0,0 +1,3 @@ +CHECK: Done 4 runs +RUN: %cpp_compiler %S/MaxNumberOfRunsTest.cpp -o %t-MaxNumberOfRunsTest +RUN: not %t-MaxNumberOfRunsTest -seed=1 -runs=4 2>&1 | FileCheck %s