From 0ec9b46d2e21401dd81e0f80fa4bc852239cc6ee Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Wed, 24 May 2023 09:08:16 +0200 Subject: [PATCH] junit: Generate temporary seeds with deterministic names This removes a potential source of non-deterministic behavior while fuzzing with a fixed `-seed`. --- .../jazzer/junit/FuzzTestExecutor.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/code_intelligence/jazzer/junit/FuzzTestExecutor.java b/src/main/java/com/code_intelligence/jazzer/junit/FuzzTestExecutor.java index e20f511ab..3416d828d 100644 --- a/src/main/java/com/code_intelligence/jazzer/junit/FuzzTestExecutor.java +++ b/src/main/java/com/code_intelligence/jazzer/junit/FuzzTestExecutor.java @@ -33,7 +33,11 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; +import java.util.Base64; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -201,8 +205,21 @@ static FuzzTestExecutor fromContext(ExtensionContext extensionContext) { } public void addSeed(byte[] bytes) throws IOException { - Path seed = Files.createTempFile(javaSeedsDir, "seed", null); - Files.write(seed, bytes); + Path tmpSeed = Files.createTempFile(javaSeedsDir, "tmp-seed-", null); + Files.write(tmpSeed, bytes); + + byte[] hash; + try { + hash = MessageDigest.getInstance("SHA-256").digest(bytes); + } catch (NoSuchAlgorithmException e) { + // Always available. + throw new IllegalStateException(e); + } + // Case-insensitive file systems lose at most one bit of entropy per character, that is, the + // resulting file name still encodes more than 200 bits of entropy. + String basename = "seed-" + Base64.getUrlEncoder().withoutPadding().encodeToString(hash); + Path seed = javaSeedsDir.resolve(basename); + Files.move(tmpSeed, seed, StandardCopyOption.REPLACE_EXISTING); } @SuppressWarnings("OptionalGetWithoutIsPresent")