Skip to content

Ghidra is a software reverse engineering (SRE) framework

parse classpath manually, don't use guava, add some logging

Jason P. Leasure59bd3806cca3
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java
index c239da4bfa..83da0d3c40 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java
@@ -49,8 +49,6 @@ public class JavaScriptProvider extends GhidraScriptProvider {
}
}
- private JavaScriptClassLoader loader = new JavaScriptClassLoader();
-
@Override
public String getDescription() {
return "Java";
@@ -93,6 +91,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
// there's new source, so uninstall any existing bundle, delete old class files, and recompile
if (!newSource.isEmpty()) {
+ writer.printf("%s has changed: %d new\n", bi.sourceDir.toString(), newSource.size());
Bundle b = bi.getBundle();
if (b != null) {
try {
@@ -100,6 +99,8 @@ public class JavaScriptProvider extends GhidraScriptProvider {
}
catch (BundleException | InterruptedException e) {
e.printStackTrace();
+ Msg.error(this, "uninstalling bundle", e);
+ return null;
}
}
bundle_host.stopBundleWatcher();
@@ -110,6 +111,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
}
catch (IOException e) {
e.printStackTrace();
+ Msg.error(this, "deleting old binary files", e);
return null;
}
@@ -121,6 +123,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
}
catch (IOException e) {
e.printStackTrace();
+ Msg.error(this, "compiling bundle", e);
return null;
}
@@ -130,11 +133,13 @@ public class JavaScriptProvider extends GhidraScriptProvider {
// wait for bundle to be started
try {
if (!bundle_host.waitForBundleStart(bi.bundleLoc)) {
+ Msg.error(this, "starting bundle");
return null;
}
}
- catch (InterruptedException | BundleException e1) {
- e1.printStackTrace();
+ catch (InterruptedException | BundleException e) {
+ e.printStackTrace();
+ Msg.error(this, "starting bundle", e);
return null;
}
@@ -147,6 +152,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException
| SecurityException e) {
e.printStackTrace();
+ Msg.error(this, "instantiatiating script", e);
return null;
}
if (object instanceof GhidraScript) {
@@ -161,10 +167,6 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return null; // class is not a script
}
- private void forceClassReload() {
- loader = new JavaScriptClassLoader(); // this forces the script class to be reloaded
- }
-
/**
* Gets the class file corresponding to the given source file and class name. If the class is in a package, the
* class name should include the full package name.
@@ -181,69 +183,6 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return file;
}
- protected boolean needsCompile(ResourceFile sourceFile, File classFile) {
-
- // Need to compile if there is no class file.
- if (!classFile.exists()) {
- return true;
- }
-
- // Need to compile if the script's source file is newer than its corresponding
- // class file.
- if (sourceFile.lastModified() > classFile.lastModified()) {
- return true;
- }
-
- // Need to compile if parent classes are not up to date.
- return !areAllParentClassesUpToDate(sourceFile);
- }
-
- protected boolean scriptCompiledExternally(File classFile) {
-
- Long modifiedTimeWhenLoaded = loader.lastModified(classFile);
- if (modifiedTimeWhenLoaded == null) {
- // never been loaded, so doesn't matter
- return false;
- }
-
- if (classFile.lastModified() > modifiedTimeWhenLoaded) {
- return true;
- }
-
- return false;
- }
-
- private boolean areAllParentClassesUpToDate(ResourceFile sourceFile) {
-
- List<Class<?>> parentClasses = getParentClasses(sourceFile);
- if (parentClasses == null) {
- // some class is missing!
- return false;
- }
-
- if (parentClasses.isEmpty()) {
- // nothing to do--no parent class to re-compile
- return true;
- }
-
- // check each parent for modification
- for (Class<?> clazz : parentClasses) {
- ResourceFile parentFile = getSourceFile(clazz);
- if (parentFile == null) {
- continue; // not sure if this can happen (inner-class, maybe?)
- }
-
- // Parent class might have a non-default java package, so use class's full name.
- File clazzFile = getClassFile(parentFile, clazz.getName());
-
- if (parentFile.lastModified() > clazzFile.lastModified()) {
- return false;
- }
- }
-
- return true;
- }
-
private class ScriptBundleInfo {
final ResourceFile sourceDir;
final String symbolicName;
@@ -299,6 +238,104 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return null;
}
+ @Override
+ public void createNewScript(ResourceFile newScript, String category) throws IOException {
+ String scriptName = newScript.getName();
+ String className = scriptName;
+ int dotpos = scriptName.lastIndexOf('.');
+ if (dotpos >= 0) {
+ className = scriptName.substring(0, dotpos);
+ }
+ PrintWriter writer = new PrintWriter(new FileWriter(newScript.getFile(false)));
+
+ writeHeader(writer, category);
+
+ writer.println("import ghidra.app.script.GhidraScript;");
+
+ for (Package pkg : Package.getPackages()) {
+ if (pkg.getName().startsWith("ghidra.program.model.")) {
+ writer.println("import " + pkg.getName() + ".*;");
+ }
+ }
+
+ writer.println("");
+
+ writer.println("public class " + className + " extends GhidraScript {");
+ writer.println("");
+
+ writer.println(" public void run() throws Exception {");
+
+ writeBody(writer);
+
+ writer.println(" }");
+ writer.println("");
+ writer.println("}");
+ writer.close();
+ }
+
+ @Override
+ public String getCommentCharacter() {
+ return "//";
+ }
+
+ @Deprecated
+ private JavaScriptClassLoader loader = new JavaScriptClassLoader();
+
+ @Deprecated
+ private void forceClassReload() {
+ loader = new JavaScriptClassLoader(); // this forces the script class to be reloaded
+ }
+
+ @Deprecated
+ protected boolean needsCompile(ResourceFile sourceFile, File classFile) {
+
+ // Need to compile if there is no class file.
+ if (!classFile.exists()) {
+ return true;
+ }
+
+ // Need to compile if the script's source file is newer than its corresponding
+ // class file.
+ if (sourceFile.lastModified() > classFile.lastModified()) {
+ return true;
+ }
+
+ // Need to compile if parent classes are not up to date.
+ return !areAllParentClassesUpToDate(sourceFile);
+ }
+
+ @Deprecated
+ private boolean areAllParentClassesUpToDate(ResourceFile sourceFile) {
+
+ List<Class<?>> parentClasses = getParentClasses(sourceFile);
+ if (parentClasses == null) {
+ // some class is missing!
+ return false;
+ }
+
+ if (parentClasses.isEmpty()) {
+ // nothing to do--no parent class to re-compile
+ return true;
+ }
+
+ // check each parent for modification
+ for (Class<?> clazz : parentClasses) {
+ ResourceFile parentFile = getSourceFile(clazz);
+ if (parentFile == null) {
+ continue; // not sure if this can happen (inner-class, maybe?)
+ }
+
+ // Parent class might have a non-default java package, so use class's full name.
+ File clazzFile = getClassFile(parentFile, clazz.getName());
+
+ if (parentFile.lastModified() > clazzFile.lastModified()) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
@Deprecated
protected boolean compile(ResourceFile sourceFile, final PrintWriter writer)
throws ClassNotFoundException {
@@ -476,44 +513,4 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return System.getProperty("java.class.path") + File.pathSeparator + scriptBinDirs;
}
- @Override
- public void createNewScript(ResourceFile newScript, String category) throws IOException {
- String scriptName = newScript.getName();
- String className = scriptName;
- int dotpos = scriptName.lastIndexOf('.');
- if (dotpos >= 0) {
- className = scriptName.substring(0, dotpos);
- }
- PrintWriter writer = new PrintWriter(new FileWriter(newScript.getFile(false)));
-
- writeHeader(writer, category);
-
- writer.println("import ghidra.app.script.GhidraScript;");
-
- for (Package pkg : Package.getPackages()) {
- if (pkg.getName().startsWith("ghidra.program.model.")) {
- writer.println("import " + pkg.getName() + ".*;");
- }
- }
-
- writer.println("");
-
- writer.println("public class " + className + " extends GhidraScript {");
- writer.println("");
-
- writer.println(" public void run() throws Exception {");
-
- writeBody(writer);
-
- writer.println(" }");
- writer.println("");
- writer.println("}");
- writer.close();
- }
-
- @Override
- public String getCommentCharacter() {
- return "//";
- }
-
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleCompiler.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleCompiler.java
index 4ce39231b4..01f33e0189 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleCompiler.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleCompiler.java
@@ -44,7 +44,7 @@ public class BundleCompiler {
this.bh = bh;
}
- /** compile a source directory to an exploded bundle */
+ // compile a source directory to an exploded bundle
public void compileToExplodedBundle(ResourceFile srcdir, Path bindir, Writer output)
throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleHost.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleHost.java
index afc5eca12e..4b8ab1c7b8 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleHost.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/BundleHost.java
@@ -22,9 +22,11 @@ import java.io.*;
import java.net.URL;
import java.nio.file.*;
import java.util.*;
+import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.felix.fileinstall.internal.FileInstall;
import org.apache.felix.framework.FrameworkFactory;
@@ -34,50 +36,28 @@ import org.osgi.framework.*;
import org.osgi.framework.launch.Framework;
import org.osgi.service.log.*;
-import com.google.common.reflect.ClassPath;
-import com.google.common.reflect.ClassPath.ClassInfo;
-
import generic.jar.ResourceFile;
import ghidra.framework.Application;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.*;
public class BundleHost {
-
void dispose() {
if (felix != null) {
stop_felix();
}
}
- String buildExtraPackages0() {
- Arrays.stream(Package.getPackages()).map(x -> x.getName()).sorted().distinct().forEach(
- s -> System.err.printf("%s\n", s));
- return Arrays.stream(Package.getPackages()).map(
- x -> x.getName()).sorted().distinct().collect(Collectors.joining(","));
- }
-
String buildExtraPackages() {
- try {
- // return Arrays.stream(BundleHost.class.getClassLoader().getDefinedPackages()).map(x -> x.getName()).sorted().distinct().collect(Collectors.joining(","));
- ClassPath.from(BundleHost.class.getClassLoader()).getTopLevelClasses().stream().map(
- ClassInfo::getPackageName).sorted().distinct().forEach(
- s -> System.err.printf("%s\n", s));
- return ClassPath.from(
- BundleHost.class.getClassLoader()).getTopLevelClasses().stream().map(
- ClassInfo::getPackageName).sorted().distinct().collect(Collectors.joining(","));
- }
- catch (IOException e1) {
- e1.printStackTrace();
- }
- return null;
+ Set<String> packages = new HashSet<>();
+ getPackagesFromClasspath(packages);
+ return packages.stream().collect(Collectors.joining(","));
}
BundleContext bc;
Framework felix;
Bundle fileinstall_bundle;
- /** install a bundle from disk */
Bundle installFromPath(String path_to_jar) throws BundleException {
Path p = Paths.get(path_to_jar);
return bc.installBundle("file://" + p.toAbsolutePath().normalize().toString());
@@ -108,6 +88,7 @@ public class BundleHost {
}
@SuppressWarnings("rawtypes")
+
@Override
protected void doLog(final Bundle bundle, final ServiceReference sr, final int level,
final String msg, final Throwable throwable) {
@@ -243,12 +224,7 @@ public class BundleHost {
return bc.getBundle(bundleLoc);
}
- /**
- * return true if it was running in the first place
- *
- * @throws InterruptedException
- * @throws BundleException
- */
+ // return true if it was running in the first place
public boolean synchronousStop(Bundle b) throws InterruptedException, BundleException {
if (b != null) {
switch (b.getState()) {
@@ -417,7 +393,7 @@ public class BundleHost {
}
// from https://dzone.com/articles/locate-jar-classpath-given
- public static String findJarForClass(Class<?> c) {
+ static String findJarForClass(Class<?> c) {
final URL location;
final String classLocation = c.getName().replace('.', '/') + ".class";
final ClassLoader loader = c.getClassLoader();
@@ -433,12 +409,54 @@ public class BundleHost {
if (m.find()) {
return m.group(1);
}
- else {
- return null; // not loaded from jar?
+ return null; // not loaded from jar?
+ }
+ return null;
+ }
+
+ static void getPackagesFromClasspath(Set<String> s) {
+ getClasspathElements().forEach(p -> {
+ if (Files.isDirectory(p)) {
+ collectPackagesFromDirectory(p, s);
+ }
+ else if (p.toString().endsWith(".jar")) {
+ collectPackagesFromJar(p, s);
}
+ });
+ }
+
+ static Stream<Path> getClasspathElements() {
+ String classpathStr = System.getProperty("java.class.path");
+ return Collections.list(new StringTokenizer(classpathStr, File.pathSeparator)).stream().map(
+ String.class::cast).map(Paths::get).map(Path::normalize);
+ }
+
+ static void collectPackagesFromDirectory(Path dirPath, Set<String> s) {
+ try {
+ Files.walk(dirPath).filter(p -> p.toString().endsWith(".class")).forEach(p -> {
+ String n = dirPath.relativize(p).toString();
+ int lastSlash = n.lastIndexOf('/');
+ s.add(lastSlash > 0 ? n.substring(0, lastSlash).replace('/', '.') : "");
+ });
+
}
- else {
- return null;
+ catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ static void collectPackagesFromJar(Path jarPath, Set<String> s) {
+ try {
+ try (JarFile j = new JarFile(jarPath.toFile())) {
+ j.stream().filter(je -> je.getName().endsWith(".class")).forEach(je -> {
+ String n = je.getName();
+ int lastSlash = n.lastIndexOf('/');
+ s.add(lastSlash > 0 ? n.substring(0, lastSlash).replace('/', '.') : "");
+ });
+ }
+ }
+ catch (IOException e) {
+ e.printStackTrace();
}
}