Skip to content

Commit

Permalink
* Started working on Jar driver
Browse files Browse the repository at this point in the history
  • Loading branch information
katherine-hough committed Nov 26, 2023
1 parent 82c3551 commit 1a8ec9e
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 176 deletions.
36 changes: 10 additions & 26 deletions Phosphor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,19 @@
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
<manifestEntries>
<Premain-Class>
edu.columbia.cs.psl.phosphor.Java8PreMain
</Premain-Class>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
<excludes>
<exclude>java/</exclude>
<exclude>sun/</exclude>
</excludes>
<forceCreation>true</forceCreation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<doclint>none</doclint>
<quiet>true</quiet>
<source>8</source>
<excludePackageNames>sun.*:java.*</excludePackageNames>
</configuration>
<executions>
<execution>
<id>attach-javadoc</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>generate-stubs</id>
Expand Down Expand Up @@ -95,7 +77,8 @@
<excludes>
<!-- These classes will be overridden
by the ones inside the JAR -->
<exclude>edu/columbia/cs/psl/phosphor/org/objectweb/asm/tree/FrameNode.class
<exclude>
edu/columbia/cs/psl/phosphor/org/objectweb/asm/tree/FrameNode.class
</exclude>
</excludes>
</filter>
Expand All @@ -109,6 +92,7 @@
</excludes>
</filter>
</filters>
<createSourcesJar>true</createSourcesJar>
<shadeSourcesContent>true</shadeSourcesContent>
<minimizeJar>true</minimizeJar>
</configuration>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import org.apache.commons.cli.*;
import org.objectweb.asm.ClassVisitor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.EnumMap;
import java.util.*;

public enum PhosphorOption {

Expand Down Expand Up @@ -369,42 +370,94 @@ public static Options createOptions(boolean forRuntimeInst) {
}
}
}
for(OptionGroup group : groupMap.values()) {
for (OptionGroup group : groupMap.values()) {
options.addOptionGroup(group);
}
return options;
}

public static CommandLine configure(boolean forRuntimeInst, String[] args) {
CommandLineParser parser = new DefaultParser();
Options options = createOptions(forRuntimeInst);
public static CommandLine configure(boolean isRuntime, String[] args) {
String commandSynopsis = "java -jar phosphor.jar [OPTIONS] <SOURCE> <DEST>";
Options options = createOptions(isRuntime);
CommandLine line;
try {
line = parser.parse(options, args);
} catch(org.apache.commons.cli.ParseException exp) {
if(forRuntimeInst) {
System.err.println(exp.getMessage());
line = new DefaultParser().parse(options, args);
} catch (ParseException e) {
if (isRuntime) {
System.err.println(e.getMessage());
return null;
}
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java -jar phosphor.jar [OPTIONS] [input] [output]", options);
System.err.println(exp.getMessage());
if(exp.getMessage().contains("-multiTaint")) {
System.err.println("Note: the -multiTaint option has been removed, and is now enabled by default (int tags no longer exist)");
}
return null;
new HelpFormatter().printHelp(commandSynopsis, options);
throw new IllegalArgumentException(e);
}
if(!forRuntimeInst && (line.hasOption("help") || line.getArgs().length != 2)) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java -jar phosphor.jar [OPTIONS] [input] [output]", options);
if (!isRuntime && line.hasOption("help")) {
new HelpFormatter().printHelp(commandSynopsis, options);
return null;
}
for(PhosphorOption phosphorOption : values()) {
phosphorOption.configure(forRuntimeInst, line.hasOption(phosphorOption.optionName), line);
if (!isRuntime && line.getArgs().length != 2) {
new HelpFormatter().printHelp(commandSynopsis, options);
throw new IllegalArgumentException("Missing command line arguments");
}
for (PhosphorOption phosphorOption : values()) {
phosphorOption.configure(isRuntime, line.hasOption(phosphorOption.optionName), line);
}
return line;
}

public static Set<Class<?>> getClassOptionValues(CommandLine line) {
// This method should not be called from instrumented JVMs.
// Therefore, the use of JCL classes is acceptable.
Set<Class<?>> classes = new HashSet<>();
for (Option option : line.getOptions()) {
if (option.getType().equals(Class.class)) {
if (line.hasOption(option.getOpt())) {
try {
classes.add((Class<?>) line.getParsedOptionValue(option.getOpt()));
} catch (ParseException e) {
throw new IllegalArgumentException(
"Failed to process " + option.getOpt() + ": " + line.getOptionValue(option.getOpt()));
}
}
}
}
return classes;
}

public static CommandLine configure(Properties properties, File source, File destination) {
// This method should not be called from instrumented JVMs.
// Therefore, the use of JCL classes is acceptable.
List<String> arguments = new LinkedList<>();
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
if (value == null || value.isEmpty() || value.equals("true")) {
arguments.add("-" + key);
} else if (!value.equals("false")) {
arguments.add("-" + key);
arguments.add(value);
}
}
arguments.add(source.getAbsolutePath());
arguments.add(destination.getAbsolutePath());
return configure(false, arguments.toArray(new String[0]));
}

public static Properties toProperties(CommandLine line) {
// This method should not be called from instrumented JVMs.
// Therefore, the use of JCL classes is acceptable.
Properties properties = new Properties();
for (Option option : line.getOptions()) {
String key = option.getOpt();
if (line.hasOption(key)) {
String value = line.getOptionValue(key);
if (value == null) {
value = "true";
}
properties.put(key, value);
}
}
return properties;
}

private enum PhosphorOptionGroup {
GENERAL, CONTROL_PROPAGATION
}
Expand Down
5 changes: 0 additions & 5 deletions Phosphor/src/main/resources/META-INF/MANIFEST.MF

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public Instrumenter(Instrumentation instrumentation, boolean verbose) {
this.verbose = verbose;
}

public void process(File source, File target) throws IOException, InterruptedException, ExecutionException {
public void process(File source, File destination) throws IOException, InterruptedException, ExecutionException {
if (!source.exists()) {
throw new IllegalArgumentException("Source file not found: " + source);
} else if (!source.isDirectory() && !isClass(source.getName()) && !isArchive(source.getName())) {
throw new IllegalArgumentException("Unknown source file type: " + source);
}
Queue<Future<Void>> futures = new LinkedList<>();
processFile(futures, source, target);
processFile(futures, source, destination);
while (!futures.isEmpty()) {
futures.poll().get();
}
Expand All @@ -51,9 +51,9 @@ public void process(File source, File target) throws IOException, InterruptedExc
}
}

private void instrumentClass(File source, File target) {
private void instrumentClass(File source, File destination) {
try (InputStream input = Files.newInputStream(source.toPath());
OutputStream output = Files.newOutputStream(target.toPath())) {
OutputStream output = Files.newOutputStream(destination.toPath())) {
instrumentClass(InstrumentUtil.readAllBytes(input), output);
} catch (Throwable t) {
errors.add(t);
Expand All @@ -69,27 +69,27 @@ private void instrumentClass(byte[] classFileBuffer, OutputStream output) throws
}
}

private void processFile(Collection<Future<Void>> futures, File source, File target)
private void processFile(Collection<Future<Void>> futures, File source, File destination)
throws IOException, InterruptedException {
if (source.isDirectory()) {
InstrumentUtil.ensureDirectory(target);
InstrumentUtil.ensureDirectory(destination);
for (File child : Objects.requireNonNull(source.listFiles())) {
processFile(futures, child, new File(target, child.getName()));
processFile(futures, child, new File(destination, child.getName()));
}
} else if (isClass(source.getName())) {
futures.add(executor.submit(() -> instrumentClass(source, target), null));
futures.add(executor.submit(() -> instrumentClass(source, destination), null));
} else if (isArchive(source.getName())) {
processZip(Files.newInputStream(source.toPath()), Files.newOutputStream(target.toPath()));
processZip(Files.newInputStream(source.toPath()), Files.newOutputStream(destination.toPath()));
} else {
if (copy(source, target)) {
if (source.canExecute() && !target.setExecutable(true)) {
errors.add(new IOException("Failed to set execute permission for: " + target));
if (copy(source, destination)) {
if (source.canExecute() && !destination.setExecutable(true)) {
errors.add(new IOException("Failed to set execute permission for: " + destination));
}
if (source.canRead() && !target.setReadable(true)) {
errors.add(new IOException("Failed to set read permission for: " + target));
if (source.canRead() && !destination.setReadable(true)) {
errors.add(new IOException("Failed to set read permission for: " + destination));
}
if (source.canWrite() && !target.setWritable(true)) {
errors.add(new IOException("Failed to set write permission for: " + target));
if (source.canWrite() && !destination.setWritable(true)) {
errors.add(new IOException("Failed to set write permission for: " + destination));
}
}
}
Expand Down Expand Up @@ -144,9 +144,9 @@ private void writeZipResults(OutputStream out, List<Future<ZipResult>> futures)
}
}

private boolean copy(File source, File target) {
private boolean copy(File source, File destination) {
try (InputStream in = Files.newInputStream(source.toPath());
OutputStream out = Files.newOutputStream(target.toPath())) {
OutputStream out = Files.newOutputStream(destination.toPath())) {
InstrumentUtil.copy(in, out);
return true;
} catch (IOException e) {
Expand Down Expand Up @@ -193,24 +193,24 @@ public ZipResult(ZipEntry entry, byte[] buffer) throws IOException, InterruptedE

public static long instrument(
File source,
File target,
File destination,
Properties options,
Instrumentation instrumentation,
boolean verbose,
String modules)
throws IOException {
if (target.exists()) {
throw new IllegalArgumentException("Target location for instrumentation already exists.");
if (destination.exists()) {
throw new IllegalArgumentException("Destination location for instrumentation already exists.");
}
if (!source.exists()) {
throw new IllegalArgumentException("Source location not found: " + source);
}
long startTime = System.currentTimeMillis();
try {
if (InstrumentUtil.isModularJvm(source)) {
JLinkInvoker.invoke(source, target, instrumentation, options, modules);
JLinkInvoker.invoke(source, destination, instrumentation, options, modules);
} else {
new Instrumenter(instrumentation, verbose).process(source, target);
new Instrumenter(instrumentation, verbose).process(source, destination);
}
return System.currentTimeMillis() - startTime;
} catch (IOException | InterruptedException | ExecutionException e) {
Expand Down
Loading

0 comments on commit 1a8ec9e

Please sign in to comment.