Skip to content

Commit

Permalink
Implement injection of a forkserver into the compiler process
Browse files Browse the repository at this point in the history
Implement a forkserver inspired by AFL fuzzer. Unlike the original
design, this one relies on library preloading and seccomp to
eliminate static or dynamic recompilation of the compiler.

For single-threaded, single-process compilers this can be
several times faster, provided that the interacting
with any input files is delayed until absolutely necessary.

For example, it is the case when the compiler process first
loads the standard library for several seconds and then
processes scratch files in several milliseconds.
  • Loading branch information
atrosinenko committed Jan 5, 2020
1 parent 987a379 commit 5761994
Show file tree
Hide file tree
Showing 13 changed files with 814 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -38,6 +39,7 @@ private static final class ExitException extends Exception { }
private final MinimizationStrategy strategy;
private final List<ASTCutter> cutters;
private List<Node> currentRoots;
private List<SCMConfiguration.FileMapping> fileMappings;

public SourceCodeMinimizer(SCMConfiguration configuration) throws IOException {
MessageDigest md = null;
Expand All @@ -56,7 +58,8 @@ public SourceCodeMinimizer(SCMConfiguration configuration) throws IOException {

Charset sourceCharset = configuration.getSourceCharset();
cutters = new ArrayList<>();
for (SCMConfiguration.FileMapping mapping: configuration.getFileMappings()) {
fileMappings = configuration.getFileMappings();
for (SCMConfiguration.FileMapping mapping: fileMappings) {
Files.copy(mapping.input, mapping.output, StandardCopyOption.REPLACE_EXISTING);
ASTCutter cutter = new ASTCutter(parser, sourceCharset, mapping.output);
cutters.add(cutter);
Expand Down Expand Up @@ -85,6 +88,15 @@ public boolean allInputsAreParseable() throws IOException {
return true;
}

@Override
public List<Path> getScratchFileNames() {
List<Path> result = new ArrayList<>();
for (SCMConfiguration.FileMapping mapping: fileMappings) {
result.add(mapping.output);
}
return result;
}

@Override
public NodeInformationProvider getNodeInformationProvider() {
return language.getNodeInformationProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.beust.jcommander.Parameter;

import java.io.IOException;
import java.io.PrintStream;

/**
Expand All @@ -34,30 +35,40 @@ public String getName() {
}
}

private InvariantOperations ops;
private String[] commandArgs;
private final String compilerCommandLine;
protected InvariantOperations ops;
private int spawnCount;
private int fruitfulTests;

private static String[] createCommandLine(AbstractConfiguration configuration) {
if (SystemUtils.IS_OS_WINDOWS) {
return new String[] { "cmd.exe", "/C", configuration.compilerCommandLine };
} else {
return new String[] { "/bin/sh", "-c", configuration.compilerCommandLine };
}
}

protected AbstractExternalProcessInvariant(AbstractConfiguration configuration) {
commandArgs = createCommandLine(configuration);
compilerCommandLine = configuration.compilerCommandLine;
}

@Override
public void initialize(InvariantOperations ops) {
public void initialize(InvariantOperations ops) throws IOException {
this.ops = ops;
}

protected abstract boolean testSatisfied(ProcessBuilder pb) throws Exception;

protected String getCompilerCommandLine() {
return compilerCommandLine;
}

protected ProcessBuilder createProcessBuilder() {
ProcessBuilder pb = new ProcessBuilder();
if (SystemUtils.IS_OS_WINDOWS) {
pb.command("cmd.exe", "/C", compilerCommandLine);
} else {
pb.command("/bin/sh", "-c", compilerCommandLine);
}
return pb;
}

protected boolean testSatisfied() throws Exception {
return testSatisfied(createProcessBuilder());
}

@Override
public boolean checkIsSatisfied() throws Exception {
// First, make a fast check that the source can be parsed at all
Expand All @@ -67,7 +78,7 @@ public boolean checkIsSatisfied() throws Exception {

// then proceed to spawning subprocess
spawnCount += 1;
boolean result = testSatisfied(new ProcessBuilder().command(commandArgs));
boolean result = testSatisfied();
fruitfulTests += result ? 1 : 0;

return result;
Expand Down
Loading

0 comments on commit 5761994

Please sign in to comment.