Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile and overwrite test #175

Merged
merged 16 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/maven.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ jobs:
shell: cmd

- name: Test Cli
timeout-minutes: 10
run: |
cd jrd
mvn --batch-mode test -Dtest=CliTest -DfailIfNoTests=false
mvn --batch-mode test -Dtest=*CliTest -DfailIfNoTests=false "-Dsurefire.reportFormat=plain"
99 changes: 76 additions & 23 deletions runtime-decompiler/src/main/java/org/jrd/backend/data/Cli.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jrd.backend.data;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.github.mkoncek.classpathless.api.ClassIdentifier;
import io.github.mkoncek.classpathless.api.ClassesProvider;
import io.github.mkoncek.classpathless.api.ClasspathlessCompiler;
Expand All @@ -25,6 +26,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -54,21 +56,25 @@ public class Cli {
protected static final String HELP = "-help";
protected static final String H = "-h";

protected static final String R = "-r";
protected static final String P = "-p";
protected static final String CP = "-cp";

private final List<String> filteredArgs;
private final VmManager vmManager;
private final PluginManager pluginManager;
private Saving saving;
private boolean isVerbose;

protected static class Saving implements CommonUtils.StatusKeeper {
protected static final String DEFAULT = "default";
protected static final String EXACT = "exact";
protected static final String FQN = "fqn";
protected static final String DIR = "dir";
static class Saving implements CommonUtils.StatusKeeper {
static final String DEFAULT = "default";
static final String EXACT = "exact";
static final String FQN = "fqn";
static final String DIR = "dir";
private final String as;
private final String like;

public Saving(String as, String like) {
Saving(String as, String like) {
this.as = as;
if (like == null) {
this.like = DEFAULT;
Expand Down Expand Up @@ -116,6 +122,10 @@ public int toInt(String suffix) {
throw new RuntimeException("Unknown saving type: " + like + ". Allowed are: " + FQN + "," + DIR + "," + EXACT);
}
}

public PrintStream openPrintStream() throws IOException {
return new PrintStream(new FileOutputStream(this.as), true, "UTF-8");
}
}

public Cli(String[] orig, Model model) {
Expand Down Expand Up @@ -258,13 +268,28 @@ private void overwrite() throws Exception {
}
}

@SuppressFBWarnings(value = "OS_OPEN_STREAM", justification = "The stream is clsoed as conditionally as is created")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably could be cheesed by having the condition as a final boolean variable. Probably.

private void api() throws Exception {
if (filteredArgs.size() != 2) {
throw new IllegalArgumentException("Incorrect argument count! Please use '" + Help.API_FORMAT + "'.");
PrintStream out = System.out;
try {
if (saving != null && saving.as != null) {
out = saving.openPrintStream();
}

if (filteredArgs.size() != 2) {
throw new IllegalArgumentException("Incorrect argument count! Please use '" + Help.API_FORMAT + "'.");
}

VmInfo vmInfo = getVmInfo(filteredArgs.get(1));
AgentApiGenerator.initItems(vmInfo, vmManager, pluginManager);
out.println(AgentApiGenerator.getInterestingHelp());

out.flush();
} finally {
if (saving != null && saving.as != null) {
out.close();
}
}
VmInfo vmInfo = getVmInfo(filteredArgs.get(1));
AgentApiGenerator.initItems(vmInfo, vmManager, pluginManager);
System.out.println(AgentApiGenerator.getInterestingHelp());
}

private void init() throws Exception {
Expand Down Expand Up @@ -299,13 +324,13 @@ private final class CompileArguments {
for (int i = 1; i < filteredArgs.size(); i++) {
String arg = filteredArgs.get(i);

if ("-p".equals(arg)) {
if (P.equals(arg)) {
wantedCustomCompiler = filteredArgs.get(i + 1);
i++; // shift
} else if ("-cp".equals(arg)) {
} else if (CP.equals(arg)) {
puc = filteredArgs.get(i + 1);
i++; // shift
} else if ("-r".equals(arg)) {
} else if (R.equals(arg)) {
isRecursive = true;
} else {
File fileToCompile = new File(arg);
Expand Down Expand Up @@ -684,26 +709,54 @@ private static boolean matchesAtLeastOne(String clazz, List<Pattern> filter) {
return false;
}

private void listPlugins() {
@SuppressFBWarnings(value = "OS_OPEN_STREAM", justification = "The stream is clsoed as conditionally as is created")
private void listPlugins() throws IOException {
if (filteredArgs.size() != 1) {
throw new RuntimeException(LIST_PLUGINS + " does not expect arguments.");
}

for (DecompilerWrapper dw : pluginManager.getWrappers()) {
System.out.printf(
"%s %s/%s - %s%n",
dw.getName(), dw.getScope(), invalidityToString(dw.isInvalidWrapper()), dw.getFileLocation()
);
PrintStream out = System.out;
try {
if (saving != null && saving.as != null) {
out = saving.openPrintStream();
}

for (DecompilerWrapper dw : pluginManager.getWrappers()) {
out.printf(
"%s %s/%s - %s%n",
dw.getName(), dw.getScope(), invalidityToString(dw.isInvalidWrapper()), dw.getFileLocation()
);
}

out.flush();
} finally {
if (saving != null && saving.as != null) {
out.close();
}
}
}

private void listJvms() {
@SuppressFBWarnings(value = "OS_OPEN_STREAM", justification = "The stream is clsoed as conditionally as is created")
private void listJvms() throws IOException {
if (filteredArgs.size() != 1) {
throw new RuntimeException(LIST_JVMS + " does not expect arguments.");
}

for (VmInfo vmInfo : vmManager.getVmInfoSet()) {
System.out.println(vmInfo.getVmPid() + " " + vmInfo.getVmName());
PrintStream out = System.out;
try {
if (saving != null && saving.as != null) {
out = saving.openPrintStream();
}

for (VmInfo vmInfo : vmManager.getVmInfoSet()) {
out.println(vmInfo.getVmPid() + " " + vmInfo.getVmName());
}

out.flush();
} finally {
if (saving != null && saving.as != null) {
out.close();
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions runtime-decompiler/src/main/java/org/jrd/backend/data/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class Help {
static final String LIST_JVMS_FORMAT = LIST_JVMS;
static final String LIST_PLUGINS_FORMAT = LIST_PLUGINS;
static final String LIST_CLASSES_FORMAT = LIST_CLASSES + " <PUC> [<CLASS REGEX>...]";
static final String COMPILE_FORMAT = COMPILE + " [-p <PLUGIN>] [-cp <PUC>] [-r] <PATH>...";
static final String COMPILE_FORMAT = COMPILE + " [" + P + " <PLUGIN>] [" + CP + " <PUC>] [" + R + "] <PATH>...";
static final String DECOMPILE_FORMAT = DECOMPILE + " <PUC> <PLUGIN> <CLASS REGEX>...";
static final String OVERWRITE_FORMAT = OVERWRITE + " <PUC> <FQN> [<CLASS FILE>]";
static final String INIT_FORMAT = INIT + " <PUC> <FQN>";
Expand All @@ -40,9 +40,9 @@ public final class Help {
private static final String LIST_CLASSES_TEXT = "List all loaded classes of a process, optionally filtering them.\n" +
"Only '" + SAVE_LIKE + " " + Saving.EXACT + "' or '" + SAVE_LIKE + " " + Saving.DEFAULT +
"' are allowed as saving modifiers.";
private static final String COMPILE_TEXT = "Compile local files against runtime classpath, specified by -cp.\n" +
"Use -p to utilize some plugins' (like jasm or jcoder) bundled compilers.\n" +
"Use -r for recursive search if <PATH> is a directory.\n" +
private static final String COMPILE_TEXT = "Compile local files against runtime classpath, specified by " + CP + ".\n" +
"Use " + P + " to utilize some plugins' (like jasm or jcoder) bundled compilers.\n" +
"Use " + R + " for recursive search if <PATH> is a directory.\n" +
"If the argument of '" + SAVE_AS + "' is a valid PID or URL, " +
"the compiled code will be attempted to be injected into that process.\n" +
"If multiple PATHs were specified, but no '" + SAVE_AS + "', the process fails.";
Expand Down Expand Up @@ -109,8 +109,8 @@ public final class Help {
NOTES.put(NOTES_SAVE, NOTES_SAVE_ITEMS);
}

private static final String[] UNSAVABLE_OPTIONS = {HELP, H, LIST_JVMS, LIST_PLUGINS, OVERWRITE, INIT, API};
private static final String[] SAVABLE_OPTIONS = {LIST_CLASSES, BYTES, BASE64, COMPILE, DECOMPILE};
private static final String[] UNSAVABLE_OPTIONS = {HELP, H, OVERWRITE, INIT};
private static final String[] SAVABLE_OPTIONS = {LIST_CLASSES, BYTES, BASE64, COMPILE, DECOMPILE, API, LIST_JVMS, LIST_PLUGINS};

private static final int LONGEST_FORMAT_LENGTH =
Stream.of(ALL_OPTIONS.keySet(), SAVING_OPTIONS.keySet())
Expand Down
Loading