Skip to content

Ghidra is a software reverse engineering (SRE) framework

move SourceBundleInfo out of BundleHost

Jason P. Leasure4c655c2e922b
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 52d60e2bed..984d67c88f 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
@@ -23,7 +23,6 @@ import org.osgi.framework.Bundle;
import generic.io.NullPrintWriter;
import generic.jar.ResourceFile;
import ghidra.app.script.osgi.*;
-import ghidra.app.script.osgi.BundleHost.SourceBundleInfo;
import ghidra.util.Msg;
public class JavaScriptProvider extends GhidraScriptProvider {
@@ -34,7 +33,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
if (_bundle_host == null) {
_bundle_host = new BundleHost();
try {
- _bundle_host.start_felix();
+ _bundle_host.startFelix();
}
catch (OSGiException | IOException e) {
throw new RuntimeException(e);
@@ -43,7 +42,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return _bundle_host;
}
- private SourceBundleInfo getBundleInfoForScript(ResourceFile sourceFile) {
+ public SourceBundleInfo getBundleInfoForScript(ResourceFile sourceFile) {
ResourceFile sourceDir = getSourceDirectoryForScript(sourceFile);
if (sourceDir == null) {
return null;
@@ -138,10 +137,12 @@ public class JavaScriptProvider extends GhidraScriptProvider {
}
// as much source as possible built, install bundle and start it if necessary
Bundle b = bi.getBundle();
+ if (b == null) {
+ b = bi.install();
+ needsBundleActivate = true;
+ }
+
if (needsBundleActivate) {
- if (b == null) {
- b = bi.install();
- }
bundle_host.synchronousStart(b);
}
@@ -235,16 +236,29 @@ public class JavaScriptProvider extends GhidraScriptProvider {
return "//";
}
+ /**
+ * compile sourcefile and test that the corresponding class is loadable.
+ *
+ * @param sourceFile path to Java source file
+ * @param writer for messages to user
+ * @return true if compilation succeeded and script can be loaded
+ * @deprecated compilation of a single script doesn't make sense anymore, directories are compiled to bundles.
+ *
+ */
@Deprecated
- protected boolean needsCompile(ResourceFile sourceFile, File classFile) {
- return true;
+ protected boolean compile(ResourceFile sourceFile, final PrintWriter writer) {
+ try {
+ return getScriptInstance(sourceFile, writer) != null;
+ }
+ catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
+ e.printStackTrace();
+ return false;
+ }
}
- @SuppressWarnings("unused")
@Deprecated
- protected boolean compile(ResourceFile sourceFile, final PrintWriter writer)
- throws ClassNotFoundException {
- return false;
+ protected boolean needsCompile(ResourceFile sourceFile, File classFile) {
+ return true;
}
}
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 4d6fe062fc..c18b11974c 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
@@ -36,7 +36,6 @@ import aQute.bnd.osgi.*;
import aQute.bnd.osgi.Clazz.QUERY;
import generic.jar.ResourceFile;
import ghidra.app.script.*;
-import ghidra.app.script.osgi.BundleHost.SourceBundleInfo;
public class BundleCompiler {
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 6667dfeafb..1892763a17 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
@@ -36,8 +36,6 @@ import org.osgi.framework.wiring.*;
import org.osgi.service.log.*;
import generic.jar.ResourceFile;
-import ghidra.app.script.GhidraScriptUtil;
-import ghidra.app.script.ScriptInfo;
import ghidra.framework.Application;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.*;
@@ -54,11 +52,11 @@ public class BundleHost {
}
}
- // XXX this should be remembered in savestate
+ // XXX this should be remembered in bundlehosts's savestate
HashMap<ResourceFile, SourceBundleInfo> file2sbi = new HashMap<>();
public SourceBundleInfo getSourceBundleInfo(ResourceFile sourceDir) {
- return file2sbi.computeIfAbsent(sourceDir, SourceBundleInfo::new);
+ return file2sbi.computeIfAbsent(sourceDir, sd -> new SourceBundleInfo(this, sd));
}
// XXX consumers should clean up after themselves
@@ -96,193 +94,6 @@ public class BundleHost {
StringBuilder message = new StringBuilder();
}
- public class SourceBundleInfo {
- final private ResourceFile sourceDir;
- final String symbolicName;
- final private Path binDir;
- final private String bundleLoc;
-
- //// information indexed by source file
-
- // XXX add separate missing requirements tracking
- final HashMap<ResourceFile, BuildFailure> buildErrors = new HashMap<>();
-
- // cached values parsed form @imports tags on default-package source files
-
- public SourceBundleInfo(ResourceFile sourceDir) {
- this.sourceDir = sourceDir;
- this.symbolicName = getSymbolicNameFromSourceDir(sourceDir);
- this.binDir = BundleHost.this.getCompiledBundlesDir().resolve(symbolicName);
-
- this.bundleLoc =
- "reference:file://" + getBinDir().toAbsolutePath().normalize().toString();
-
- }
-
- public String classNameForScript(ResourceFile sourceFile) {
- String p;
- try {
- p = sourceFile.getCanonicalPath();
- p = p.substring(1 + getSourceDir().getCanonicalPath().length(), p.length() - 5);// relative path less ".java"
- return p.replace(File.separatorChar, '.');
- }
- catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
-
- public Bundle getBundle() {
- return BundleHost.this.getBundle(getBundleLoc());
- }
-
- public String getBundleLoc() {
- return bundleLoc;
- }
-
- public ResourceFile getSourceDir() {
- return sourceDir;
- }
-
- public Path getBinDir() {
- return binDir;
- }
-
- public Bundle install() throws GhidraBundleException {
- return BundleHost.this.installFromLoc(getBundleLoc());
- }
-
- public void buildError(ResourceFile rf, String err) {
- BuildFailure f = buildErrors.computeIfAbsent(rf, x -> new BuildFailure());
- f.when = rf.lastModified();
- f.message.append(err);
- }
-
- public String getPreviousBuildErrors() {
- return buildErrors.values().stream().map(e -> e.message.toString()).collect(
- Collectors.joining());
- }
-
- /**
- * check for build errors from last time
- * @param rf file to test
- * @return true if this file had errors and hasn't changed
- */
- private boolean syncBuildErrors(ResourceFile rf) {
- BuildFailure f = buildErrors.get(rf);
- if (f != null) {
- if (f.when == rf.lastModified()) {
- return true;
- }
- buildErrors.remove(rf);
- }
- return false;
- }
-
- public List<BundleRequirement> getRequirements() {
- return foundRequirements;
- }
-
- final List<ResourceFile> newSources = new ArrayList<>();
- List<Path> oldBin = new ArrayList<>();
-
- boolean foundNewManifest;
- private List<BundleRequirement> foundRequirements;
-
- private void computeRequirements() throws OSGiException {
- foundRequirements = null;
- Map<String, BundleRequirement> dedupedreqs = new HashMap<>();
- // parse metadata from all Java source in sourceDir
- for (ResourceFile rf : sourceDir.listFiles()) {
- if (rf.getName().endsWith(".java")) {
- // NB: ScriptInfo will update field values if lastModified has changed since last time they were computed
- ScriptInfo si = GhidraScriptUtil.getScriptInfo(rf);
- String imps = si.getImports();
- if (imps != null && !imps.isEmpty()) {
- for (BundleRequirement req : BundleHost.parseImports(imps)) {
- dedupedreqs.put(req.toString(), req);
- }
- }
- }
- }
- foundRequirements = new ArrayList<>(dedupedreqs.values());
- }
-
- /**
- * look for new sources, metadata, manifest file.
- *
- * @param writer for reporting status to user
- * @throws IOException while accessing manifest file
- * @throws OSGiException while parsing imports
- */
- public void updateFromFilesystem(PrintWriter writer) throws IOException, OSGiException {
-
- // look for new source files
- newSources.clear();
- oldBin.clear();
- ResourceFile smf =
- new ResourceFile(getSourceDir(), "META-INF" + File.separator + "MANIFEST.MF");
- Path dmf = getBinDir().resolve("META-INF").resolve("MANIFEST.MF");
-
- foundNewManifest = smf.exists() && (Files.notExists(dmf) ||
- smf.lastModified() > Files.getLastModifiedTime(dmf).toMillis());
-
- BundleHost.visitDiscrepencies(getSourceDir(), getBinDir(), (sf, bfs) -> {
- if (sf != null) {
- newSources.add(sf);
- }
- if (bfs != null) {
- oldBin.addAll(bfs);
- }
- });
-
- computeRequirements();
-
- // remove source files that failed last time and haven't changed
- Iterator<ResourceFile> it = newSources.iterator();
- while (it.hasNext()) {
- ResourceFile sf = it.next();
- if (syncBuildErrors(sf)) {
- it.remove();
- }
- }
- }
-
- public void deleteOldBinaries() throws IOException {
- for (Path bf : oldBin) {
- Files.delete(bf);
- }
- // oldBin.clear();
- }
-
- public int getFailingSourcesCount() {
- return buildErrors.size();
- }
-
- public int getNewSourcesCount() {
- return newSources.size();
- }
-
- public List<ResourceFile> getNewSources() {
- return newSources;
- }
-
- public boolean newManifestFile() {
- return foundNewManifest;
- }
-
- long lastCompileAttempt;
-
- public void compileAttempted() {
- lastCompileAttempt = System.currentTimeMillis();
- }
-
- public long getLastCompileAttempt() {
- return lastCompileAttempt;
- }
-
- }
-
String buildExtraPackages() {
Set<String> packages = new HashSet<>();
getPackagesFromClasspath(packages);
@@ -381,7 +192,7 @@ public class BundleHost {
* @throws OSGiException framework failures
* @throws IOException filesystem setup
*/
- public void start_felix() throws OSGiException, IOException {
+ public void startFelix() throws OSGiException, IOException {
Properties config = new Properties();
@@ -495,7 +306,7 @@ public class BundleHost {
}
- private Path getOsgiDir() {
+ static private Path getOsgiDir() {
Path usersettings = Application.getUserSettingsDirectory().toPath();
return usersettings.resolve("osgi");
}
@@ -511,7 +322,7 @@ public class BundleHost {
return getCompiledBundlesDir().toAbsolutePath().toString();
}
- public Path getCompiledBundlesDir() {
+ static public Path getCompiledBundlesDir() {
return getOsgiDir().resolve("compiled-bundles");
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/SourceBundleInfo.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/SourceBundleInfo.java
new file mode 100644
index 0000000000..c321ddeedc
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/osgi/SourceBundleInfo.java
@@ -0,0 +1,212 @@
+package ghidra.app.script.osgi;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.*;
+import java.util.stream.Collectors;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.wiring.BundleRequirement;
+
+import generic.jar.ResourceFile;
+import ghidra.app.script.GhidraScriptUtil;
+import ghidra.app.script.ScriptInfo;
+import ghidra.app.script.osgi.BundleHost.BuildFailure;
+
+public class SourceBundleInfo {
+ /**
+ *
+ */
+ private final BundleHost bundle_host;
+ final private ResourceFile sourceDir;
+ final String symbolicName;
+ final private Path binDir;
+ final private String bundleLoc;
+
+ //// information indexed by source file
+
+ // XXX add separate missing requirements tracking
+ final HashMap<ResourceFile, BuildFailure> buildErrors = new HashMap<>();
+
+ // cached values parsed form @imports tags on default-package source files
+
+ public SourceBundleInfo(BundleHost bundleHost, ResourceFile sourceDir) {
+ bundle_host = bundleHost;
+ this.sourceDir = sourceDir;
+ this.symbolicName = BundleHost.getSymbolicNameFromSourceDir(sourceDir);
+ this.binDir = BundleHost.getCompiledBundlesDir().resolve(symbolicName);
+
+ this.bundleLoc = "reference:file://" + getBinDir().toAbsolutePath().normalize().toString();
+
+ }
+
+ static public Path getBindirFromScriptFile(ResourceFile sourceFile) {
+ ResourceFile tmpSourceDir = sourceFile.getParentFile();
+ String tmpSymbolicName = BundleHost.getSymbolicNameFromSourceDir(tmpSourceDir);
+ return BundleHost.getCompiledBundlesDir().resolve(tmpSymbolicName);
+ }
+
+ public String classNameForScript(ResourceFile sourceFile) {
+ String p;
+ try {
+ p = sourceFile.getCanonicalPath();
+ p = p.substring(1 + getSourceDir().getCanonicalPath().length(), p.length() - 5);// relative path less ".java"
+ return p.replace(File.separatorChar, '.');
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public Bundle getBundle() {
+ return bundle_host.getBundle(getBundleLoc());
+ }
+
+ public String getBundleLoc() {
+ return bundleLoc;
+ }
+
+ public ResourceFile getSourceDir() {
+ return sourceDir;
+ }
+
+ public Path getBinDir() {
+ return binDir;
+ }
+
+ public Bundle install() throws GhidraBundleException {
+ return bundle_host.installFromLoc(getBundleLoc());
+ }
+
+ public void buildError(ResourceFile rf, String err) {
+ BuildFailure f = buildErrors.computeIfAbsent(rf, x -> new BundleHost.BuildFailure());
+ f.when = rf.lastModified();
+ f.message.append(err);
+ }
+
+ public String getPreviousBuildErrors() {
+ return buildErrors.values().stream().map(e -> e.message.toString()).collect(
+ Collectors.joining());
+ }
+
+ /**
+ * check for build errors from last time
+ * @param rf file to test
+ * @return true if this file had errors and hasn't changed
+ */
+ private boolean syncBuildErrors(ResourceFile rf) {
+ BuildFailure f = buildErrors.get(rf);
+ if (f != null) {
+ if (f.when == rf.lastModified()) {
+ return true;
+ }
+ buildErrors.remove(rf);
+ }
+ return false;
+ }
+
+ public List<BundleRequirement> getRequirements() {
+ return foundRequirements;
+ }
+
+ final List<ResourceFile> newSources = new ArrayList<>();
+ List<Path> oldBin = new ArrayList<>();
+
+ boolean foundNewManifest;
+ private List<BundleRequirement> foundRequirements;
+
+ private void computeRequirements() throws OSGiException {
+ foundRequirements = null;
+ Map<String, BundleRequirement> dedupedreqs = new HashMap<>();
+ // parse metadata from all Java source in sourceDir
+ for (ResourceFile rf : sourceDir.listFiles()) {
+ if (rf.getName().endsWith(".java")) {
+ // NB: ScriptInfo will update field values if lastModified has changed since last time they were computed
+ ScriptInfo si = GhidraScriptUtil.getScriptInfo(rf);
+ String imps = si.getImports();
+ if (imps != null && !imps.isEmpty()) {
+ for (BundleRequirement req : BundleHost.parseImports(imps)) {
+ dedupedreqs.put(req.toString(), req);
+ }
+ }
+ }
+ }
+ foundRequirements = new ArrayList<>(dedupedreqs.values());
+ }
+
+ /**
+ * look for new sources, metadata, manifest file.
+ *
+ * @param writer for reporting status to user
+ * @throws IOException while accessing manifest file
+ * @throws OSGiException while parsing imports
+ */
+ public void updateFromFilesystem(PrintWriter writer) throws IOException, OSGiException {
+
+ // look for new source files
+ newSources.clear();
+ oldBin.clear();
+ ResourceFile smf =
+ new ResourceFile(getSourceDir(), "META-INF" + File.separator + "MANIFEST.MF");
+ Path dmf = getBinDir().resolve("META-INF").resolve("MANIFEST.MF");
+
+ foundNewManifest = smf.exists() && (Files.notExists(dmf) ||
+ smf.lastModified() > Files.getLastModifiedTime(dmf).toMillis());
+
+ BundleHost.visitDiscrepencies(getSourceDir(), getBinDir(), (sf, bfs) -> {
+ if (sf != null) {
+ newSources.add(sf);
+ }
+ if (bfs != null) {
+ oldBin.addAll(bfs);
+ }
+ });
+
+ computeRequirements();
+
+ // remove source files that failed last time and haven't changed
+ Iterator<ResourceFile> it = newSources.iterator();
+ while (it.hasNext()) {
+ ResourceFile sf = it.next();
+ if (syncBuildErrors(sf)) {
+ it.remove();
+ }
+ }
+ }
+
+ public void deleteOldBinaries() throws IOException {
+ for (Path bf : oldBin) {
+ Files.delete(bf);
+ }
+ // oldBin.clear();
+ }
+
+ public int getFailingSourcesCount() {
+ return buildErrors.size();
+ }
+
+ public int getNewSourcesCount() {
+ return newSources.size();
+ }
+
+ public List<ResourceFile> getNewSources() {
+ return newSources;
+ }
+
+ public boolean newManifestFile() {
+ return foundNewManifest;
+ }
+
+ long lastCompileAttempt;
+
+ public void compileAttempted() {
+ lastCompileAttempt = System.currentTimeMillis();
+ }
+
+ public long getLastCompileAttempt() {
+ return lastCompileAttempt;
+ }
+
+}
\ No newline at end of file