@@ -112,7 +112,12 @@ public static FuzzTestExecutor prepare(ExtensionContext context, String maxDurat
112
112
&& corpusFilesOrDirs .stream ().map (Paths ::get ).allMatch (Files ::isRegularFile )) {
113
113
javaSeedsDir = Optional .empty ();
114
114
} 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 ));
116
121
}
117
122
118
123
libFuzzerArgs .add ("-max_total_time=" + durationStringToSeconds (maxDuration ));
@@ -169,7 +174,8 @@ private static Optional<String> translateJUnitTimeoutToLibFuzzerFlag(ExtensionCo
169
174
*
170
175
* @return the temporary Java seed corpus directory
171
176
*/
172
- private static Path addInputAndSeedDirs (ExtensionContext context , List <String > libFuzzerArgs )
177
+ private static Path addInputAndSeedDirs (
178
+ ExtensionContext context , List <String > libFuzzerArgs , boolean createDefaultGeneratedCorpusDir )
173
179
throws IOException {
174
180
Class <?> fuzzTestClass = context .getRequiredTestClass ();
175
181
Method fuzzTestMethod = context .getRequiredTestMethod ();
@@ -184,20 +190,25 @@ private static Path addInputAndSeedDirs(ExtensionContext context, List<String> l
184
190
// The path is specified relative to the current working directory, which with JUnit is the
185
191
// project directory.
186
192
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 );
198
195
}
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
+ }
199
209
200
- libFuzzerArgs .add (absoluteCorpusDir );
210
+ libFuzzerArgs .add (absoluteCorpusDir );
211
+ }
201
212
202
213
// We can only emit findings into the source tree version of the inputs directory, not e.g. the
203
214
// copy under Maven's target directory. If it doesn't exist, collect the inputs in the current
0 commit comments