Ghidra is a software reverse engineering (SRE) framework
consolidate bundle management into bundlehost
Jason P. Leasure2c27ca137655
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.javaindex 72959962c0..0b52408eca 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java@@ -34,6 +34,7 @@ import org.osgi.framework.wiring.*;import org.osgi.service.log.*;import generic.jar.ResourceFile;+import ghidra.app.script.GhidraScriptUtil;import ghidra.framework.Application;import ghidra.util.exception.CancelledException;import ghidra.util.task.*;@@ -66,20 +67,26 @@ public class BundleHost {}}- // XXX this should be remembered by bundlehosts's savestate- HashMap<ResourceFile, GhidraBundle> file2sbi = new HashMap<>();+ HashMap<ResourceFile, GhidraBundle> bp2gb = new HashMap<>();public GhidraBundle getGhidraBundle(ResourceFile path) {- return file2sbi.computeIfAbsent(path, this::createGhidraBundle);+ return bp2gb.computeIfAbsent(path, p -> newGhidraBundle0(path, true, false));}- public GhidraBundle createGhidraBundle(ResourceFile path) {+ public GhidraBundle newGhidraBundle(ResourceFile path, boolean enabled, boolean systemBundle) {+ GhidraBundle gb = newGhidraBundle0(path, enabled, systemBundle);+ bp2gb.put(path, gb);+ return gb;+ }++ private GhidraBundle newGhidraBundle0(ResourceFile path, boolean enabled,+ boolean systemBundle) {switch (GhidraBundle.getType(path)) {case SourceDir:- return new GhidraSourceBundle(this, path);+ return new GhidraSourceBundle(this, path, enabled, systemBundle);case BndScript:case Jar:- return new GhidraJarBundle(this, path);+ return new GhidraJarBundle(this, path, enabled, systemBundle);default:break;}@@ -87,8 +94,8 @@ public class BundleHost {}// XXX consumers must clean up after themselves- public void removeSourceBundleInfo(ResourceFile path) {- file2sbi.remove(path);+ public void removeGhidraBundle(ResourceFile path) {+ bp2gb.remove(path);}/**@@ -99,7 +106,6 @@ public class BundleHost {* @throws BundleException on parse failure*/static List<BundleRequirement> parseImports(String imports) throws BundleException {-// parse it with Felix's ManifestParser to a list of BundleRequirement objectsMap<String, Object> headerMap = new HashMap<>();headerMap.put(Constants.IMPORT_PACKAGE, imports);@@ -574,4 +580,19 @@ public class BundleHost {}}+ private List<ResourceFile> bundlePaths = new ArrayList<>();++ {+ setBundlePaths(GhidraScriptUtil.getSystemScriptPaths());+ getBundlePaths().add(0, GhidraScriptUtil.getUserScriptDirectory());+ }++ public List<ResourceFile> getBundlePaths() {+ return bundlePaths;+ }++ public void setBundlePaths(List<ResourceFile> bundlePaths) {+ this.bundlePaths = bundlePaths;+ }+}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraBundle.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraBundle.javaindex 3441c1cbda..8c9a1dbcae 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraBundle.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraBundle.java@@ -9,10 +9,18 @@ import org.osgi.framework.wiring.BundleRequirement;import generic.jar.ResourceFile;-public interface GhidraBundle {+public abstract class GhidraBundle {- enum Type {- BndScript, Jar, SourceDir, INVALID+ protected final ResourceFile path;+ protected final BundleHost bundleHost;+ protected boolean enabled;+ protected boolean systemBundle;++ GhidraBundle(BundleHost bundleHost, ResourceFile path, boolean enabled, boolean systemBundle) {+ this.bundleHost = bundleHost;+ this.path = path;+ this.enabled = enabled;+ this.systemBundle = systemBundle;}/**@@ -20,24 +28,38 @@ public interface GhidraBundle {** @return true if anything was done*/- boolean clean();+ abstract boolean clean();/**- * attempt to build loadable bundle, if possible+ * build OSGi bundle if possible** @param writer console for user messages- * @return true if build was successful- * @throws Exception XXX+ * @return true if build happened, false if already built+ * @throws Exception sorry, wasn't possible*/- boolean build(PrintWriter writer) throws Exception;+ abstract boolean build(PrintWriter writer) throws Exception;++ abstract String getSummary();- String getBundleLoc();+ abstract String getBundleLoc();++ abstract List<BundleRequirement> getAllReqs();++ public ResourceFile getPath() {+ return path;+ }- Bundle getBundle() throws GhidraBundleException;+ public boolean isEnabled() {+ return enabled;+ }- Bundle install() throws GhidraBundleException;+ public boolean isSystemBundle() {+ return systemBundle;+ }- String getSummary();+ enum Type {+ BndScript, Jar, SourceDir, INVALID+ }static GhidraBundle.Type getType(ResourceFile rf) {if (rf.isDirectory()) {@@ -67,6 +89,11 @@ public interface GhidraBundle {return GhidraBundle.Type.INVALID;}- List<BundleRequirement> getAllReqs();+ public Bundle getBundle() {+ return bundleHost.getBundle(getBundleLoc());+ }+ public Bundle install() throws GhidraBundleException {+ return bundleHost.installFromLoc(getBundleLoc());+ }}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraJarBundle.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraJarBundle.javaindex 17022e2e82..e3468db66a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraJarBundle.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraJarBundle.java@@ -5,22 +5,18 @@ import java.util.Collections;import java.util.List;import java.util.jar.Manifest;-import org.osgi.framework.Bundle;import org.osgi.framework.wiring.BundleRequirement;import aQute.bnd.osgi.Constants;import aQute.bnd.osgi.Jar;import generic.jar.ResourceFile;-public class GhidraJarBundle implements GhidraBundle {- final private BundleHost bundleHost;-+public class GhidraJarBundle extends GhidraBundle {final String bundleLoc;- final ResourceFile path;- public GhidraJarBundle(BundleHost bundleHost, ResourceFile path) {- this.bundleHost = bundleHost;- this.path = path;+ public GhidraJarBundle(BundleHost bundleHost, ResourceFile path, boolean enabled,+ boolean systemBundle) {+ super(bundleHost, path, enabled, systemBundle);this.bundleLoc = "file://" + path.getAbsolutePath().toString();}@@ -31,7 +27,7 @@ public class GhidraJarBundle implements GhidraBundle {@Overridepublic boolean build(PrintWriter writer) throws Exception {- return true;+ return false;}@Override@@ -39,16 +35,6 @@ public class GhidraJarBundle implements GhidraBundle {return bundleLoc;}- @Override- public Bundle getBundle() throws GhidraBundleException {- return bundleHost.getBundle(getBundleLoc());- }-- @Override- public Bundle install() throws GhidraBundleException {- return bundleHost.installFromLoc(getBundleLoc());- }-@Overridepublic String getSummary() {return null;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.javaindex 3e490a8c10..851d462aa8 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraSourceBundle.java@@ -50,14 +50,11 @@ import ghidra.util.Msg;/*** The SourceBundleInfo class is a cache of information for bundles built from source directories.*/-public class GhidraSourceBundle implements GhidraBundle {+public class GhidraSourceBundle extends GhidraBundle {public interface DiscrepencyCallback {void found(ResourceFile source_file, Collection<Path> class_files) throws Throwable;}- final private BundleHost bundleHost;- final private ResourceFile sourceDir;-final private String symbolicName;final private Path binDir;final private String bundleLoc;@@ -76,14 +73,14 @@ public class GhidraSourceBundle implements GhidraBundle {// cached values parsed form @imports tags on default-package source files- public GhidraSourceBundle(BundleHost bundleHost, ResourceFile sourceDirectory) {- this.bundleHost = bundleHost;- this.sourceDir = sourceDirectory;- this.symbolicName = BundleHost.getSymbolicNameFromSourceDir(sourceDir);+ public GhidraSourceBundle(BundleHost bundleHost, ResourceFile sourceDirectory, boolean enabled,+ boolean systemBundle) {+ super(bundleHost, sourceDirectory, enabled, systemBundle);++ this.symbolicName = BundleHost.getSymbolicNameFromSourceDir(path);this.binDir = BundleHost.getCompiledBundlesDir().resolve(symbolicName);this.bundleLoc = "reference:file://" + getBinDir().toAbsolutePath().normalize().toString();-}/**@@ -112,7 +109,7 @@ public class GhidraSourceBundle implements GhidraBundle {}ResourceFile getSourceDir() {- return sourceDir;+ return path;}Path getBinDir() {@@ -153,7 +150,7 @@ public class GhidraSourceBundle implements GhidraBundle {buildReqs.clear();req2file.clear();- for (ResourceFile rf : sourceDir.listFiles()) {+ for (ResourceFile rf : path.listFiles()) {if (rf.getName().endsWith(".java")) {// without GhidraScriptComponentProvider.updateAvailableScriptFilesForDirectory, or GhidraScriptComponentProvider.newScript// this might be the earliest need for ScriptInfo, so allow construction.@@ -403,16 +400,6 @@ public class GhidraSourceBundle implements GhidraBundle {return false;}- @Override- public Bundle install() throws GhidraBundleException {- return bundleHost.installFromLoc(getBundleLoc());- }-- @Override- public Bundle getBundle() throws GhidraBundleException {- return bundleHost.getBundle(getBundleLoc());- }-@Overridepublic boolean clean() {try {@@ -434,7 +421,7 @@ public class GhidraSourceBundle implements GhidraBundle {private ResourceFile[] correspondingBinaries(ResourceFile source) {String parentPath = source.getParentFile().getAbsolutePath();- String relpath = parentPath.substring(sourceDir.getAbsolutePath().length());+ String relpath = parentPath.substring(path.getAbsolutePath().length());if (relpath.startsWith(File.separator)) {relpath = relpath.substring(1);}@@ -475,11 +462,10 @@ public class GhidraSourceBundle implements GhidraBundle {try {// delete class files for which java is either newer, or no longer existsDeque<ResourceFile> stack = new ArrayDeque<>();- stack.add(sourceDir);+ stack.add(path);while (!stack.isEmpty()) {ResourceFile sd = stack.pop();- String relpath =- sd.getAbsolutePath().substring(sourceDir.getAbsolutePath().length());+ String relpath = sd.getAbsolutePath().substring(path.getAbsolutePath().length());if (relpath.startsWith(File.separator)) {relpath = relpath.substring(1);}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.javaindex 8b90b7c867..b9c5486cbf 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java@@ -134,7 +134,8 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {}private void performRefresh() {- GhidraScriptUtil.setScriptBundlePaths(bundleStatusProvider.getModel().getEnabledPaths());+ GhidraScriptUtil.getBundleHost().setBundlePaths(+ bundleStatusProvider.getModel().getEnabledPaths());GhidraScriptUtil.clearMetadata();refresh();}@@ -1062,7 +1063,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {// pull in the just-loaded pathsList<ResourceFile> paths = bundleStatusProvider.getModel().getEnabledPaths();- GhidraScriptUtil.setScriptBundlePaths(paths);+ GhidraScriptUtil.initializeScriptBundlePaths(paths);actionManager.restoreUserDefinedKeybindings(saveState);actionManager.restoreScriptsThatAreInTool(saveState);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.javaindex 629530226a..2893402a7b 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java@@ -43,15 +43,66 @@ public class GhidraScriptUtil {*/public static String USER_SCRIPTS_DIR = buildUserScriptsDirectory();- private static List<ResourceFile> scriptBundlePaths = new ArrayList<>();-private static Map<ResourceFile, ScriptInfo> scriptFileToInfoMap = new HashMap<>();private static Map<String, List<ResourceFile>> scriptNameToFilesMap = new HashMap<>();- static {- scriptBundlePaths = getSystemScriptPaths();- scriptBundlePaths.add(0, getUserScriptDirectory());+ static BundleHost _bundleHost = BundleHost.getInstance();++ public static BundleHost getBundleHost() {+ return _bundleHost;+ }++ /**+ * Sets the script bundle paths+ * @param newPaths the new script bundle paths+ */+ public static void initializeScriptBundlePaths(List<ResourceFile> newPaths) {+ _bundleHost.setBundlePaths(new ArrayList<>(newPaths));+ }++ /**+ * Uses the given name to find a matching script. This method only works because of the+ * limitation that all script names in Ghidra must be unique. If the given name has multiple+ * script matches, then a warning will be logged.+ *+ * @param name The name for which to find a script+ * @return The ScriptInfo that has the given name+ */+ public static ScriptInfo findScriptByName(String name) {+ List<ResourceFile> matchingFiles = scriptNameToFilesMap.get(name);+ if (matchingFiles != null && !matchingFiles.isEmpty()) {+ ScriptInfo info = scriptFileToInfoMap.get(matchingFiles.get(0));+ if (matchingFiles.size() > 1) {+ Msg.warn(GhidraScriptUtil.class, "Found duplicate scripts for name: " + name ++ ". Binding to script: " + info.getSourceFile());+ }+ return info;+ }++ ResourceFile file = findScriptFileInPaths(_bundleHost.getBundlePaths(), name);+ if (file == null) {+ return null;+ }++ return getExistingScriptInfo(file); // this will cache the created info+ }++ public static List<ResourceFile> getAllScripts() {+ List<ResourceFile> scriptList = new ArrayList<>();+ for (ResourceFile dirPath : _bundleHost.getBundlePaths()) {+ updateAvailableScriptFilesForDirectory(scriptList, dirPath);+ }+ return scriptList;+ }++ /**+ * Returns a list of the current script directories.+ * @return a list of the current script directories+ */+ public static List<ResourceFile> getScriptSourceDirectories() {+ return _bundleHost.getBundlePaths().stream().filter(ResourceFile::isDirectory).collect(+ Collectors.toList());}/**@@ -131,31 +182,6 @@ public class GhidraScriptUtil {}}- /**- * Returns a list of the current script directories.- * @return a list of the current script directories- */- public static List<ResourceFile> getScriptSourceDirectories() {- return scriptBundlePaths.stream().filter(ResourceFile::isDirectory).collect(- Collectors.toList());- }-- /**- * Sets the script bundle paths- * @param newPaths the new script bundle paths- */- public static void setScriptBundlePaths(List<ResourceFile> newPaths) {- scriptBundlePaths = new ArrayList<>(newPaths);- }-- public static List<ResourceFile> getAllScripts() {- List<ResourceFile> scriptList = new ArrayList<>();- for (ResourceFile dirPath : scriptBundlePaths) {- updateAvailableScriptFilesForDirectory(scriptList, dirPath);- }- return scriptList;- }-/*** Returns the list of exploded bundle directories* @return the list@@ -449,33 +475,6 @@ public class GhidraScriptUtil {return path + ".java";}- /**- * Uses the given name to find a matching script. This method only works because of the- * limitation that all script names in Ghidra must be unique. If the given name has multiple- * script matches, then a warning will be logged.- *- * @param name The name for which to find a script- * @return The ScriptInfo that has the given name- */- public static ScriptInfo findScriptByName(String name) {- List<ResourceFile> matchingFiles = scriptNameToFilesMap.get(name);- if (matchingFiles != null && !matchingFiles.isEmpty()) {- ScriptInfo info = scriptFileToInfoMap.get(matchingFiles.get(0));- if (matchingFiles.size() > 1) {- Msg.warn(GhidraScriptUtil.class, "Found duplicate scripts for name: " + name +- ". Binding to script: " + info.getSourceFile());- }- return info;- }-- ResourceFile file = findScriptFileInPaths(scriptBundlePaths, name);- if (file == null) {- return null;- }-- return getExistingScriptInfo(file); // this will cache the created info- }-private static void updateAvailableScriptFilesForDirectory(List<ResourceFile> scriptAccumulator,ResourceFile directory) {ResourceFile[] files = directory.listFiles();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.javaindex 852c3027d0..b459b8fbf6 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@@ -24,12 +24,18 @@ import ghidra.app.plugin.core.osgi.*;import ghidra.util.Msg;public class JavaScriptProvider extends GhidraScriptProvider {- static public GhidraSourceBundle getBundleForSource(ResourceFile sourceFile) {+ final private BundleHost _bundleHost;++ public JavaScriptProvider() {+ _bundleHost = GhidraScriptUtil.getBundleHost();+ }++ public GhidraSourceBundle getBundleForSource(ResourceFile sourceFile) {ResourceFile sourceDir = getSourceDirectoryContaining(sourceFile);if (sourceDir == null) {return null;}- return (GhidraSourceBundle) BundleHost.getInstance().getGhidraBundle(sourceDir);+ return (GhidraSourceBundle) _bundleHost.getGhidraBundle(sourceDir);}@Override@@ -47,7 +53,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {try {Bundle b = getBundleForSource(sourceFile).getBundle();if (b != null) {- BundleHost.getInstance().deactivateSynchronously(b);+ _bundleHost.deactivateSynchronously(b);}}catch (GhidraBundleException | InterruptedException e) {@@ -90,14 +96,11 @@ public class JavaScriptProvider extends GhidraScriptProvider {}}- static public Class<?> loadClass(ResourceFile sourceFile, PrintWriter writer) throws Exception {-- BundleHost bundleHost = BundleHost.getInstance();-+ public Class<?> loadClass(ResourceFile sourceFile, PrintWriter writer) throws Exception {GhidraSourceBundle bi = getBundleForSource(sourceFile);bi.build(writer);Bundle b = bi.install();- bundleHost.activateSynchronously(b);+ _bundleHost.activateSynchronously(b);String classname = bi.classNameForScript(sourceFile);Class<?> clazz = b.loadClass(classname); // throws ClassNotFoundExceptiondiff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/GhidraScriptRunner.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/GhidraScriptRunner.javaindex 2970418564..87392a4f12 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/GhidraScriptRunner.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/GhidraScriptRunner.java@@ -204,7 +204,7 @@ public class GhidraScriptRunner implements GhidraLaunchable {paths.addAll(GhidraScriptUtil.getSystemScriptPaths());paths.add(0, GhidraScriptUtil.getUserScriptDirectory());}- GhidraScriptUtil.setScriptBundlePaths(paths);+ GhidraScriptUtil.initializeScriptBundlePaths(paths);StringBuffer buf = new StringBuffer("HEADLESS Script Paths:");for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.javaindex 5c711bdbe8..a51c001778 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java@@ -673,7 +673,7 @@ public class HeadlessAnalyzer {paths.addAll(GhidraScriptUtil.getSystemScriptPaths());paths.add(0, GhidraScriptUtil.getUserScriptDirectory());}- GhidraScriptUtil.setScriptBundlePaths(paths);+ GhidraScriptUtil.initializeScriptBundlePaths(paths);StringBuffer buf = new StringBuffer("HEADLESS Script Paths:");for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) {diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.javaindex 14301b4b6e..6afa942ebd 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java@@ -636,7 +636,7 @@ public abstract class AbstractGhidraScriptMgrPluginTest@SuppressWarnings("unchecked")List<ResourceFile> scriptBundlePaths =- (List<ResourceFile>) getInstanceField("scriptBundlePaths", GhidraScriptUtil.class);+ (List<ResourceFile>) getInstanceField("bundlePaths", GhidraScriptUtil.getBundleHost());scriptBundlePaths.add(scriptDir);}