Skip to content

Commit

Permalink
all: Reformat with google-java-format
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Sep 6, 2023
1 parent fac4309 commit fb22ffa
Show file tree
Hide file tree
Showing 221 changed files with 8,326 additions and 5,479 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public class FuzzTargetTestWrapper {
private static final String FRAME_PREFIX = "\tat ";
private static final Pattern SANITIZER_FINDING = Pattern.compile("^SUMMARY: \\w*Sanitizer");
private static final String THREAD_DUMP_HEADER = "Stack traces of all JVM threads:";
private static final Set<String> PUBLIC_JAZZER_PACKAGES = Collections.unmodifiableSet(
Stream.of("api", "replay", "sanitizers").collect(Collectors.toSet()));
private static final Set<String> PUBLIC_JAZZER_PACKAGES =
Collections.unmodifiableSet(
Stream.of("api", "replay", "sanitizers").collect(Collectors.toSet()));

public static void main(String[] args) {
Runfiles runfiles;
Expand Down Expand Up @@ -80,10 +81,11 @@ public static void main(String[] args) {
allowedFindings =
Arrays.stream(args[8].split(",")).filter(s -> !s.isEmpty()).collect(Collectors.toSet());
// Map all files/dirs to real location
arguments = Arrays.stream(args)
.skip(9)
.map(arg -> arg.startsWith("-") ? arg : runfiles.rlocation(arg))
.collect(toList());
arguments =
Arrays.stream(args)
.skip(9)
.map(arg -> arg.startsWith("-") ? arg : runfiles.rlocation(arg))
.collect(toList());
} catch (IOException | ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
System.exit(1);
Expand All @@ -108,19 +110,28 @@ public static void main(String[] args) {
if (hookJarActualPath != null) {
command.add(String.format("--main_advice_classpath=%s", hookJarActualPath));
}
command.add("--jvm_flags="
+ String.join(" ", "-XX:-OmitStackTraceInFastThrow", "-XX:+UseParallelGC",
"-XX:+IgnoreUnrecognizedVMOptions", "-XX:+CriticalJNINatives",
"-XX:+EnableDynamicAgentLoading"));
command.add(
"--jvm_flags="
+ String.join(
" ",
"-XX:-OmitStackTraceInFastThrow",
"-XX:+UseParallelGC",
"-XX:+IgnoreUnrecognizedVMOptions",
"-XX:+CriticalJNINatives",
"-XX:+EnableDynamicAgentLoading"));
if (System.getenv("JAZZER_DEBUG") != null) {
command.add("--debug");
}
} else {
command.add(String.format("--cp=%s",
hookJarActualPath == null
? targetJarActualPath
: String.join(System.getProperty("path.separator"), targetJarActualPath.toString(),
hookJarActualPath.toString())));
command.add(
String.format(
"--cp=%s",
hookJarActualPath == null
? targetJarActualPath
: String.join(
System.getProperty("path.separator"),
targetJarActualPath.toString(),
hookJarActualPath.toString())));
}
command.add(String.format("-artifact_prefix=%s/", outputDir));
command.add(String.format("--reproducer_path=%s", outputDir));
Expand All @@ -130,8 +141,9 @@ public static void main(String[] args) {
command.addAll(arguments);

// Make JVM error reports available in test outputs.
processBuilder.environment().put(
"JAVA_TOOL_OPTIONS", String.format("-XX:ErrorFile=%s/hs_err_pid%%p.log", outputDir));
processBuilder
.environment()
.put("JAVA_TOOL_OPTIONS", String.format("-XX:ErrorFile=%s/hs_err_pid%%p.log", outputDir));
processBuilder.redirectOutput(Redirect.INHERIT);
processBuilder.redirectInput(Redirect.INHERIT);
processBuilder.command(command);
Expand All @@ -155,26 +167,27 @@ public static void main(String[] args) {
}
// Assert that we either found a crash in Java (exit code 77), a sanitizer crash (exit code
// 76), or a timeout (exit code 70).
if (exitCode != 76 && exitCode != 77
if (exitCode != 76
&& exitCode != 77
&& !(allowedFindings.contains("timeout") && exitCode == 70)) {
System.err.printf("Did expect a crash, but Jazzer exited with exit code %d%n", exitCode);
System.exit(1);
}
List<Path> outputFiles = Files.list(outputDir).collect(toList());
// Verify that libFuzzer dumped a crashing input.
if (shouldVerifyCrashInput
&& outputFiles.stream().noneMatch(
name -> name.getFileName().toString().startsWith("crash-"))
&& outputFiles.stream()
.noneMatch(name -> name.getFileName().toString().startsWith("crash-"))
&& !(allowedFindings.contains("timeout")
&& outputFiles.stream().anyMatch(
name -> name.getFileName().toString().startsWith("timeout-")))) {
&& outputFiles.stream()
.anyMatch(name -> name.getFileName().toString().startsWith("timeout-")))) {
System.err.printf("No crashing input found in %s%n", outputDir);
System.exit(1);
}
// Verify that libFuzzer dumped a crash reproducer.
if (shouldVerifyCrashReproducer
&& outputFiles.stream().noneMatch(
name -> name.getFileName().toString().startsWith("Crash_"))) {
&& outputFiles.stream()
.noneMatch(name -> name.getFileName().toString().startsWith("Crash_"))) {
System.err.printf("No crash reproducer found in %s%n", outputDir);
System.exit(1);
}
Expand All @@ -199,19 +212,25 @@ private static void verifyFuzzerOutput(
List<String> stackTrace;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(fuzzerOutput))) {
stackTrace =
reader.lines()
reader
.lines()
.peek(System.err::println)
.filter(line
-> line.startsWith(EXCEPTION_PREFIX) || line.startsWith(FRAME_PREFIX)
|| line.equals(THREAD_DUMP_HEADER) || SANITIZER_FINDING.matcher(line).find())
.filter(
line ->
line.startsWith(EXCEPTION_PREFIX)
|| line.startsWith(FRAME_PREFIX)
|| line.equals(THREAD_DUMP_HEADER)
|| SANITIZER_FINDING.matcher(line).find())
.collect(toList());
}
if (expectedFindings.isEmpty()) {
if (stackTrace.isEmpty()) {
return;
}
throw new IllegalStateException(String.format(
"Did not expect a finding, but got a stack trace:%n%s", String.join("\n", stackTrace)));
throw new IllegalStateException(
String.format(
"Did not expect a finding, but got a stack trace:%n%s",
String.join("\n", stackTrace)));
}
if (expectedFindings.contains("native")) {
// Expect a native sanitizer finding as well as a thread dump with at least one frame.
Expand Down Expand Up @@ -247,8 +266,10 @@ private static void verifyFuzzerOutput(
}
for (String finding : findings) {
if (!expectedFindings.contains(finding)) {
throw new IllegalStateException(String.format("Got finding %s, but expected one of: %s",
findings.get(0), String.join(", ", expectedFindings)));
throw new IllegalStateException(
String.format(
"Got finding %s, but expected one of: %s",
findings.get(0), String.join(", ", expectedFindings)));
}
}
List<String> unexpectedFrames =
Expand All @@ -257,14 +278,17 @@ private static void verifyFuzzerOutput(
.map(line -> line.substring(FRAME_PREFIX.length()))
.filter(line -> line.startsWith("com.code_intelligence.jazzer."))
// With --nohooks, Jazzer does not filter out its own stack frames.
.filter(line
-> !noHooks
&& !PUBLIC_JAZZER_PACKAGES.contains(
line.substring("com.code_intelligence.jazzer.".length()).split("\\.")[0]))
.filter(
line ->
!noHooks
&& !PUBLIC_JAZZER_PACKAGES.contains(
line.substring("com.code_intelligence.jazzer.".length())
.split("\\.")[0]))
.collect(toList());
if (!unexpectedFrames.isEmpty()) {
throw new IllegalStateException(
String.format("Unexpected strack trace frames:%n%n%s%n%nin:%n%s",
String.format(
"Unexpected strack trace frames:%n%n%s%n%nin:%n%s",
String.join("\n", unexpectedFrames), String.join("\n", stackTrace)));
}
}
Expand All @@ -288,8 +312,9 @@ private static String compile(File source, Path api, Path targetJar) throws IOEx
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(source);
List<String> options = Arrays.asList(
"-classpath", String.join(File.pathSeparator, api.toString(), targetJar.toString()));
List<String> options =
Arrays.asList(
"-classpath", String.join(File.pathSeparator, api.toString(), targetJar.toString()));
System.out.printf(
"Compile crash reproducer %s with options %s%n", source.getAbsolutePath(), options);
CompilationTask task =
Expand All @@ -301,24 +326,27 @@ private static String compile(File source, Path api, Path targetJar) throws IOEx
}
}

private static void execute(String className, Path outputDir, Path api, Path targetJar,
Set<String> expectedFindings) throws IOException, ReflectiveOperationException {
private static void execute(
String className, Path outputDir, Path api, Path targetJar, Set<String> expectedFindings)
throws IOException, ReflectiveOperationException {
try {
System.out.printf("Execute crash reproducer %s%n", className);
URLClassLoader classLoader = new URLClassLoader(
new URL[] {
outputDir.toUri().toURL(),
api.toUri().toURL(),
targetJar.toUri().toURL(),
},
getPlatformClassLoader());
URLClassLoader classLoader =
new URLClassLoader(
new URL[] {
outputDir.toUri().toURL(), api.toUri().toURL(), targetJar.toUri().toURL(),
},
getPlatformClassLoader());
Class<?> crashReproducerClass = classLoader.loadClass(className);
Method main = crashReproducerClass.getMethod("main", String[].class);
System.setProperty("jazzer.is_reproducer", "true");
main.invoke(null, new Object[] {new String[] {}});
if (!expectedFindings.isEmpty()) {
throw new IllegalStateException("Expected crash with any of "
+ String.join(", ", expectedFindings) + " not reproduced by " + className);
throw new IllegalStateException(
"Expected crash with any of "
+ String.join(", ", expectedFindings)
+ " not reproduced by "
+ className);
}
System.out.println("Reproducer finished successfully without finding");
} catch (InvocationTargetException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

public class JarStripper {
private static final Map<String, String> ZIP_FS_PROPERTIES = new HashMap<>();

static {
// We copy the input to the output path before modifying, so don't try to create a new file at
// that path if something went wrong.
Expand All @@ -58,17 +59,19 @@ public static void main(String[] args) {

Path inFile = Paths.get(args[0]);
Path outFile = Paths.get(args[1]);
Map<Boolean, List<String>> rawPaths = unmodifiableMap(
Arrays.stream(args)
.skip(2)
.map(arg -> {
if (arg.startsWith("+")) {
return new SimpleEntry<>(true, arg.substring(1));
} else {
return new SimpleEntry<>(false, arg);
}
})
.collect(partitioningBy(e -> e.getKey(), mapping(e -> e.getValue(), toList()))));
Map<Boolean, List<String>> rawPaths =
unmodifiableMap(
Arrays.stream(args)
.skip(2)
.map(
arg -> {
if (arg.startsWith("+")) {
return new SimpleEntry<>(true, arg.substring(1));
} else {
return new SimpleEntry<>(false, arg);
}
})
.collect(partitioningBy(e -> e.getKey(), mapping(e -> e.getValue(), toList()))));

try {
Files.copy(inFile, outFile);
Expand Down Expand Up @@ -98,16 +101,18 @@ public static void main(String[] args) {
PathMatcher pathsToKeep = toPathMatcher(zipFs, rawPaths.get(true), true);
try (Stream<Path> walk = Files.walk(zipFs.getPath(""))) {
walk.sorted(Comparator.reverseOrder())
.filter(path
-> (pathsToKeep != null && !pathsToKeep.matches(path))
|| (pathsToDelete != null && pathsToDelete.matches(path)))
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
.filter(
path ->
(pathsToKeep != null && !pathsToKeep.matches(path))
|| (pathsToDelete != null && pathsToDelete.matches(path)))
.forEach(
path -> {
try {
Files.delete(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
} catch (Throwable e) {
Throwable throwable = e;
Expand All @@ -123,10 +128,12 @@ private static PathMatcher toPathMatcher(FileSystem fs, List<String> paths, bool
if (paths.isEmpty()) {
return null;
}
return fs.getPathMatcher(String.format("glob:{%s}",
paths.stream()
.flatMap(pattern -> keep ? toKeepGlobs(pattern) : toRemoveGlobs(pattern))
.collect(joining(","))));
return fs.getPathMatcher(
String.format(
"glob:{%s}",
paths.stream()
.flatMap(pattern -> keep ? toKeepGlobs(pattern) : toRemoveGlobs(pattern))
.collect(joining(","))));
}

private static Stream<String> toRemoveGlobs(String path) {
Expand All @@ -141,7 +148,8 @@ private static Stream<String> toRemoveGlobs(String path) {
private static Stream<String> toKeepGlobs(String path) {
// When keeping something, also keep all parents.
String[] segments = path.split("/");
return Stream.concat(Stream.of(path),
return Stream.concat(
Stream.of(path),
IntStream.range(0, segments.length)
.mapToObj(i -> Arrays.stream(segments).limit(i).collect(joining("/"))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import com.code_intelligence.jazzer.junit.FuzzTest;
import com.example.JunitSpringWebApplication.HelloRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
Expand Down Expand Up @@ -79,7 +78,8 @@ public void fuzzTestShouldFail(FuzzedDataProvider data) throws Exception {
}

String name = data.consumeRemainingAsString();
mockMvc.perform(get("/buggy-hello").param("name", name))
mockMvc
.perform(get("/buggy-hello").param("name", name))
.andExpect(content().string(containsString(name)));
}

Expand All @@ -92,9 +92,10 @@ public void fuzzTestWithDtoShouldFail(HelloRequest helloRequest) throws Exceptio
helloRequest != null && helloRequest.name != null && !helloRequest.name.isBlank());

mockMvc
.perform(post("/hello")
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(helloRequest)))
.perform(
post("/hello")
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(helloRequest)))
.andExpect(content().string(containsString(helloRequest.name)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class JavaSeedFuzzTest {
Base64.getDecoder().decode("q0vPdz5oeJIW3k2U4VJ+aWDufzzZbKAcevc9cNoUTSM=");

static Stream<Arguments> fuzzTheFlag() {
return Stream.of(arguments(asList("red", "herring"), 0),
return Stream.of(
arguments(asList("red", "herring"), 0),
// This argument passes the hash check, but does not trigger the finding right away. This
// is meant to verify that the seed ends up in the corpus, serving as the base for future
// mutations rather than just being executed once.
Expand All @@ -49,8 +50,9 @@ static Stream<Arguments> fuzzTheFlag() {
@FuzzTest
void fuzzTheFlag(@NotNull List<@NotNull String> flagParts, int secret)
throws NoSuchAlgorithmException {
byte[] hash = MessageDigest.getInstance("SHA-256").digest(
String.join("", flagParts).getBytes(StandardCharsets.UTF_8));
byte[] hash =
MessageDigest.getInstance("SHA-256")
.digest(String.join("", flagParts).getBytes(StandardCharsets.UTF_8));
if (MessageDigest.isEqual(hash, FLAG_SHA256) && secret == 1337) {
throw new Error("Fl4g 4nd s3cr3et f0und!");
}
Expand Down
Loading

0 comments on commit fb22ffa

Please sign in to comment.