Skip to content

Commit 5189af3

Browse files
committed
junit: Only create .cifuzz-corpus if it is the generated corpus
If users add custom corpus directories, the first of those will be used as the generated corpus instead of the default `.cifuzz-corpus` directory. We now no longer create this directory if it is going to stay empty because it isn't used as the generated corpus directory.
1 parent 7725a74 commit 5189af3

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/main/java/com/code_intelligence/jazzer/junit/FuzzTestExecutor.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ public static FuzzTestExecutor prepare(ExtensionContext context, String maxDurat
112112
&& corpusFilesOrDirs.stream().map(Paths::get).allMatch(Files::isRegularFile)) {
113113
javaSeedsDir = Optional.empty();
114114
} else {
115-
javaSeedsDir = Optional.of(addInputAndSeedDirs(context, libFuzzerArgs));
115+
// Only create the default generated corpus directory if it is used as the generated corpus
116+
// directory, i.e., if the user didn't provide any custom corpus directories that come before
117+
// it on the libFuzzer command line.
118+
boolean createDefaultGeneratedCorpusDir = corpusFilesOrDirs.isEmpty();
119+
javaSeedsDir =
120+
Optional.of(addInputAndSeedDirs(context, libFuzzerArgs, createDefaultGeneratedCorpusDir));
116121
}
117122

118123
libFuzzerArgs.add("-max_total_time=" + durationStringToSeconds(maxDuration));
@@ -169,7 +174,8 @@ private static Optional<String> translateJUnitTimeoutToLibFuzzerFlag(ExtensionCo
169174
*
170175
* @return the temporary Java seed corpus directory
171176
*/
172-
private static Path addInputAndSeedDirs(ExtensionContext context, List<String> libFuzzerArgs)
177+
private static Path addInputAndSeedDirs(
178+
ExtensionContext context, List<String> libFuzzerArgs, boolean createDefaultGeneratedCorpusDir)
173179
throws IOException {
174180
Class<?> fuzzTestClass = context.getRequiredTestClass();
175181
Method fuzzTestMethod = context.getRequiredTestMethod();
@@ -184,20 +190,25 @@ private static Path addInputAndSeedDirs(ExtensionContext context, List<String> l
184190
// The path is specified relative to the current working directory, which with JUnit is the
185191
// project directory.
186192
Path generatedCorpusDir = baseDir.resolve(generatedCorpusPath(fuzzTestClass, fuzzTestMethod));
187-
Files.createDirectories(generatedCorpusDir);
188-
String absoluteCorpusDir = generatedCorpusDir.toAbsolutePath().toString();
189-
190-
// Even if support for long paths (+260 characters) is enabled on Windows,
191-
// libFuzzer does not work properly. This can be circumvented by prepending "\\?\" to the path,
192-
// see:
193-
// https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry
194-
// Error message: "GetFileAttributesA() failed for <path> (Error code: 3)."
195-
// https://github.com/llvm/llvm-project/blob/release/17.x/compiler-rt/lib/fuzzer/FuzzerIOWindows.cpp#L65
196-
if (Utils.isWindows()) {
197-
absoluteCorpusDir = "\\\\?\\" + absoluteCorpusDir;
193+
if (createDefaultGeneratedCorpusDir) {
194+
Files.createDirectories(generatedCorpusDir);
198195
}
196+
if (Files.exists(generatedCorpusDir)) {
197+
String absoluteCorpusDir = generatedCorpusDir.toAbsolutePath().toString();
198+
199+
// Even if support for long paths (+260 characters) is enabled on Windows,
200+
// libFuzzer does not work properly. This can be circumvented by prepending "\\?\" to the
201+
// path,
202+
// see:
203+
// https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry
204+
// Error message: "GetFileAttributesA() failed for <path> (Error code: 3)."
205+
// https://github.com/llvm/llvm-project/blob/release/17.x/compiler-rt/lib/fuzzer/FuzzerIOWindows.cpp#L65
206+
if (Utils.isWindows()) {
207+
absoluteCorpusDir = "\\\\?\\" + absoluteCorpusDir;
208+
}
199209

200-
libFuzzerArgs.add(absoluteCorpusDir);
210+
libFuzzerArgs.add(absoluteCorpusDir);
211+
}
201212

202213
// We can only emit findings into the source tree version of the inputs directory, not e.g. the
203214
// copy under Maven's target directory. If it doesn't exist, collect the inputs in the current

src/test/java/com/code_intelligence/jazzer/junit/CorpusDirectoryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ public void fuzzingEnabled() throws IOException {
158158
assertCrashFileExistsIn(artifactsDirectory);
159159
assertNoCrashFileExistsIn(baseDir);
160160
assertNoCrashFileExistsIn(explicitGeneratedCorpus);
161-
assertNoCrashFileExistsIn(defaultGeneratedCorpus);
161+
// Default generated corpus directory isn't used and thus should not have been created.
162+
assertThat(Files.notExists(defaultGeneratedCorpus)).isTrue();
162163

163164
// Verify that corpus files are written to given corpus directory and not generated one.
164165
assertThat(Files.list(explicitGeneratedCorpus)).isNotEmpty();
165-
assertThat(Files.list(defaultGeneratedCorpus)).isEmpty();
166166
}
167167

168168
@Test

0 commit comments

Comments
 (0)