Ghidra is a software reverse engineering (SRE) framework
connect OSGi framework to bundlestatusprovider
Jason P. Leasure8d5557117276
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 8900480a7b..cc9793795d 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@@ -29,6 +29,7 @@ import javax.swing.tree.TreePath;import javax.swing.tree.TreeSelectionModel;import org.apache.commons.lang3.StringUtils;+import org.osgi.framework.Bundle;import docking.ActionContext;import docking.action.KeyBindingData;@@ -42,6 +43,7 @@ import generic.jar.ResourceFile;import ghidra.app.plugin.core.script.osgi.*;import ghidra.app.script.*;import ghidra.app.script.osgi.BundleHost;+import ghidra.app.script.osgi.SourceBundleInfo;import ghidra.app.services.ConsoleService;import ghidra.framework.options.SaveState;import ghidra.framework.plugintool.ComponentProviderAdapter;@@ -100,7 +102,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {GhidraScriptComponentProvider(GhidraScriptMgrPlugin plugin, BundleHost bundleHost) {super(plugin.getTool(), "Script Manager", plugin.getName());this.plugin = plugin;- this.bundleHost=bundleHost;+ this.bundleHost = bundleHost;setHelpLocation(new HelpLocation(plugin.getName(), plugin.getName()));setIcon(ResourceManager.loadImage("images/play.png"));@@ -740,8 +742,30 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {@Overridepublic void run(TaskMonitor monitor) throws CancelledException {try {- bundleHost.setActive(path, activate);- path.setActive(activate);+ if (activate) {+ if (path.getType() == BundlePath.Type.SourceDir) {+ SourceBundleInfo sbi = bundleHost.getSourceBundleInfo(path.getPath());+ ConsoleService console =+ plugin.getTool().getService(ConsoleService.class);+ bundleHost.compileSourceBundle(sbi, console.getStdErr());+ }+ String loc = bundleStatusProvider.getModel().getBundleLoc(path);+ if (loc != null) {+ Bundle bundle = bundleHost.getBundle(loc);+ if (bundle != null) {+ bundle.start();+ }+ }+ }+ else { // deactivate+ String loc = bundleStatusProvider.getModel().getBundleLoc(path);+ if (loc != null) {+ Bundle bundle = bundleHost.getBundle(loc);+ if (bundle != null) {+ bundle.uninstall();+ }+ }+ }}catch (Exception e) {Msg.showError(this, GhidraScriptComponentProvider.this.getComponent(),@@ -757,7 +781,8 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {}private void build() {- bundleStatusProvider = new BundleStatusProvider(plugin.getTool(), plugin.getName());+ bundleStatusProvider =+ new BundleStatusProvider(plugin.getTool(), plugin.getName(), bundleHost);bundleStatusProvider.addListener(new BundlePathManagerListener() {@Override@@ -768,7 +793,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {@Overridepublic void bundleEnablementChanged(BundlePath path, boolean enabled) {- if (path.isActive()) {+ if (!enabled && path.isActive()) {startActivateDeactiveTask(path, false);}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundlePath.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundlePath.javaindex 7cb2a2ce66..e9893a0342 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundlePath.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundlePath.java@@ -20,6 +20,9 @@ import java.io.File;import generic.jar.ResourceFile;import generic.util.Path;+/**+ * The BundlePath class represents the runtime state and user preferences for OSGi bundles in Ghidra.+ */public class BundlePath extends Path {final Type type;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundleStatusModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundleStatusModel.javaindex ae182722a4..7b3bfcc217 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundleStatusModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundleStatusModel.java@@ -19,9 +19,13 @@ package ghidra.app.plugin.core.script.osgi;import java.util.*;import java.util.stream.Collectors;+import org.osgi.framework.*;+import docking.widgets.table.AbstractSortedTableModel;import generic.jar.ResourceFile;import ghidra.app.script.GhidraScriptUtil;+import ghidra.app.script.osgi.BundleHost;+import ghidra.app.script.osgi.SourceBundleInfo;import ghidra.framework.preferences.Preferences;import ghidra.util.Msg;@@ -126,20 +130,103 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundlePath> {private BundleStatusProvider provider;private List<BundlePath> paths;+ private BundleHost bundleHost;+ BundleListener bundleListener;++ private Map<String, BundlePath> loc2bp = new HashMap<>();++ BundlePath getPath(String bundleLocation) {+ return loc2bp.get(bundleLocation);+ }++ public String getBundleLoc(BundlePath bp) {+ switch (bp.getType()) {+ case Jar:+ return bp.getPath().getAbsolutePath();+ case SourceDir:+ SourceBundleInfo bi = bundleHost.getSourceBundleInfo(bp.getPath());+ return bi.getBundleLoc();+ case BndScript:+ // XXX+ case INVALID:+ default:+ break;+ }+ return null;+ }- BundleStatusModel(BundleStatusProvider provider) {+ /**+ * (re)compute cached mapping from bundleloc to bundlepath+ */+ private void computeCache() {+ loc2bp.clear();+ for (BundlePath bp : paths) {+ String loc = getBundleLoc(bp);+ if (loc != null) {+ loc2bp.put(loc, bp);+ }+ }+ }++ BundleStatusModel(BundleStatusProvider provider, BundleHost bundleHost) {super();this.provider = provider;+ this.bundleHost = bundleHost;// add unmodifiable pathsthis.paths = GhidraScriptUtil.getSystemScriptPaths().stream().distinct().map(f -> new BundlePath(f, true, true)).collect(Collectors.toList());// add user paththis.paths.add(0, new BundlePath(GhidraScriptUtil.getUserScriptDirectory(), true, false));+ computeCache();++ bundleHost.addListener(bundleListener = new BundleListener() {+ @Override+ public void bundleChanged(BundleEvent event) {+ Bundle b = event.getBundle();+ BundlePath bp;+ switch (event.getType()) {+ case BundleEvent.INSTALLED:+ case BundleEvent.LAZY_ACTIVATION:+ case BundleEvent.RESOLVED:+ case BundleEvent.STARTING:+ case BundleEvent.STOPPED:+ case BundleEvent.STOPPING:+ case BundleEvent.UNRESOLVED:+ case BundleEvent.UPDATED:+ break;+ case BundleEvent.STARTED:+ bp = getPath(b.getLocation());+ if (bp != null) {+ bp.setActive(true);+ int row = getRowIndex(bp);+ fireTableRowsUpdated(row, row);+ }+ break;+ case BundleEvent.UNINSTALLED:+ bp = getPath(b.getLocation());+ if (bp != null) {+ bp.setActive(false);+ int row = getRowIndex(bp);+ fireTableRowsUpdated(row, row);+ }+ break;+ default:+ System.err.printf("What is a %d event??", event.getType());+ break;+ }+ }+ });fireTableDataChanged();}+ @Override+ public void dispose() {+ super.dispose();+ bundleHost.removeListener(bundleListener);+ }+void clear() {paths.clear();}@@ -158,15 +245,32 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundlePath> {return list;}- void addPath(BundlePath path) {+ private void addPath(BundlePath path) {if (paths.contains(path)) {return;}+ String loc = getBundleLoc(path);+ if (loc != null) {+ loc2bp.put(loc, path);+ }+int index = paths.size();paths.add(path);fireTableRowsInserted(index, index);}+ BundlePath addNewPath(ResourceFile path, boolean enabled, boolean readonly) {+ BundlePath p = new BundlePath(path, enabled, readonly);+ addPath(p);+ return p;+ }++ BundlePath addNewPath(String path, boolean enabled, boolean readonly) {+ BundlePath p = new BundlePath(path, enabled, readonly);+ addPath(p);+ return p;+ }+void remove(int[] selectedRows) {List<BundlePath> list = new ArrayList<>();for (int selectedRow : selectedRows) {@@ -175,6 +279,7 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundlePath> {for (BundlePath path : list) {if (!path.isReadOnly()) {paths.remove(path);+ loc2bp.remove(getBundleLoc(path));}else {Msg.showInfo(this, this.provider.getComponent(), "Unabled to remove path",@@ -256,8 +361,7 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundlePath> {return false;}}- BundlePath p = new BundlePath(dir, true, false);- addPath(p);+ addNewPath(dir, true, false);Preferences.setProperty(BundleStatusProvider.preferenceForLastSelectedBundle,dir.getAbsolutePath());provider.fireBundlesChanged();@@ -285,6 +389,7 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundlePath> {public void setPathsForTesting(List<String> testingPaths) {this.paths = testingPaths.stream().map(f -> new BundlePath(f, true, false)).collect(Collectors.toList());+ computeCache();fireTableDataChanged();}@@ -292,12 +397,10 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundlePath> {* This is for testing only!** insert path, marked editable and non-readonly- * @param index index to insert at* @param path the path to insert*/- public void insertPathForTesting(int index, String path) {- paths.add(0, new BundlePath(path, true, false));- fireTableRowsInserted(0, 0);+ public void insertPathForTesting(String path) {+ addNewPath(path, true, false);}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundleStatusProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundleStatusProvider.javaindex 600a4e9467..ee90627ed3 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundleStatusProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/osgi/BundleStatusProvider.java@@ -29,6 +29,7 @@ import docking.widgets.filechooser.GhidraFileChooser;import docking.widgets.filechooser.GhidraFileChooserMode;import docking.widgets.table.*;import generic.jar.ResourceFile;+import ghidra.app.script.osgi.BundleHost;import ghidra.framework.options.SaveState;import ghidra.framework.plugintool.ComponentProviderAdapter;import ghidra.framework.plugintool.PluginTool;@@ -75,9 +76,9 @@ public class BundleStatusProvider extends ComponentProviderAdapter {}}- public BundleStatusProvider(PluginTool tool, String owner) {+ public BundleStatusProvider(PluginTool tool, String owner, BundleHost bundleHost) {super(tool, "Bundle Status Manager", owner);- this.bundleStatusModel = new BundleStatusModel(this);+ this.bundleStatusModel = new BundleStatusModel(this, bundleHost);this.filter = new GhidraFileFilter() {@Override@@ -118,13 +119,13 @@ public class BundleStatusProvider extends ComponentProviderAdapter {addButton = new JButton(ResourceManager.loadImage("images/Plus.png"));addButton.setName("AddBundle");addButton.setToolTipText("Display file chooser to add bundles to list");- addButton.addActionListener(e -> add());+ addButton.addActionListener(e -> addButtonAction());addButton.setFocusable(false);removeButton = new JButton(ResourceManager.loadImage("images/edit-delete.png"));removeButton.setName("RemoveBundle");removeButton.setToolTipText("Remove selected bundle(s) from list");- removeButton.addActionListener(e -> remove());+ removeButton.addActionListener(e -> removeButtonAction());removeButton.setFocusable(false);JPanel buttonPanel = new JPanel(new GridBagLayout());@@ -231,7 +232,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {removeButton.setEnabled(rows.length > 0);}- private void remove() {+ private void removeButtonAction() {int[] selectedRows = bundlePathTable.getSelectedRows();if (selectedRows == null || selectedRows.length == 0) {return;@@ -252,7 +253,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {fireBundlesChanged();}- private void add() {+ private void addButtonAction() {if (fileChooser == null) {fileChooser = new GhidraFileChooser(panel);fileChooser.setMultiSelectionEnabled(true);@@ -292,8 +293,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {Preferences.setProperty(preferenceForLastSelectedBundle,files.get(0).getAbsolutePath());for (File element : files) {- BundlePath p = new BundlePath(new ResourceFile(element), true, false);- bundleStatusModel.addPath(p);+ bundleStatusModel.addNewPath(new ResourceFile(element), true, false);}fireBundlesChanged();}@@ -313,19 +313,19 @@ public class BundleStatusProvider extends ComponentProviderAdapter {String[] pathArr = new String[paths.size()];boolean[] enableArr = new boolean[paths.size()];- boolean[] readArr = new boolean[paths.size()];+ boolean[] readonlyArr = new boolean[paths.size()];int index = 0;for (BundlePath path : paths) {pathArr[index] = path.getPathAsString();enableArr[index] = path.isEnabled();- readArr[index] = path.isReadOnly();+ readonlyArr[index] = path.isReadOnly();++index;}ss.putStrings("BundleStatus_PATH", pathArr);ss.putBooleans("BundleStatus_ENABLE", enableArr);- ss.putBooleans("BundleStatus_READ", readArr);+ ss.putBooleans("BundleStatus_READ", readonlyArr);}/**@@ -339,38 +339,24 @@ public class BundleStatusProvider extends ComponentProviderAdapter {return;}- boolean[] enableArr =- ss.getBooleans("BundleStatus_ENABLE", new boolean[pathArr.length]);- boolean[] readArr = ss.getBooleans("BundleStatus_READ", new boolean[pathArr.length]);+ boolean[] enableArr = ss.getBooleans("BundleStatus_ENABLE", new boolean[pathArr.length]);+ boolean[] readonlyArr = ss.getBooleans("BundleStatus_READ", new boolean[pathArr.length]);- List<BundlePath> oldPaths = bundleStatusModel.getAllPaths();+ List<BundlePath> currentPaths = bundleStatusModel.getAllPaths();bundleStatusModel.clear();for (int i = 0; i < pathArr.length; i++) {- BundlePath path = new BundlePath(pathArr[i], enableArr[i], readArr[i]);- BundlePath oldPath = getPath(path.getPathAsString(), oldPaths);- if (oldPath != null) {- if (!oldPath.isEditable()) {- boolean enabled = path.isEnabled();- path = oldPath;- path.setEnabled(enabled);- }- oldPaths.remove(oldPath);+ BundlePath currentPath = getPath(pathArr[i], currentPaths);+ if (currentPath != null) {+ currentPaths.remove(currentPath);+ bundleStatusModel.addNewPath(pathArr[i],enableArr[i],readonlyArr[i]);}- else if (path.isReadOnly()) {+ else if (!readonlyArr[i]) {// skip read-only paths which are not present in the current config// This is needed to thin-out old default entries- continue;+ bundleStatusModel.addNewPath(pathArr[i],enableArr[i],readonlyArr[i]);}- bundleStatusModel.addPath(path);}-- for (BundlePath path : oldPaths) {- if (!path.isEditable()) {- bundleStatusModel.addPath(path);- }- }-fireBundlesChanged();}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 873a9520ce..61773ec458 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@@ -20,7 +20,6 @@ import java.lang.reflect.InvocationTargetException;import org.osgi.framework.Bundle;-import generic.io.NullPrintWriter;import generic.jar.ResourceFile;import ghidra.app.script.osgi.*;import ghidra.util.Msg;@@ -90,12 +89,8 @@ public class JavaScriptProvider extends GhidraScriptProvider {static public Class<?> loadClass(ResourceFile sourceFile, PrintWriter writer)throws IOException, OSGiException, ClassNotFoundException, InterruptedException {- if (writer == null) {- writer = new NullPrintWriter();- }-SourceBundleInfo bi = getBundleInfoForSource(sourceFile);- Bundle b = BundleHost.getInstance().activate(bi, writer);+ Bundle b = BundleHost.getInstance().compileSourceBundle(bi, writer);String classname = bi.classNameForScript(sourceFile);Class<?> clazz = b.loadClass(classname); // throws ClassNotFoundExceptionreturn clazz;@@ -104,7 +99,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {public static ResourceFile getSourceDirectoryContaining(ResourceFile sourceFile) {String sourcePath = sourceFile.getAbsolutePath();for (ResourceFile sourceDir : GhidraScriptUtil.getScriptSourceDirectories()) {- if (sourcePath.startsWith(sourceDir.getAbsolutePath())) {+ if (sourcePath.startsWith(sourceDir.getAbsolutePath()+File.separatorChar)) {return sourceDir;}}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.javaindex aafbb1c693..d87aa91457 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@@ -35,8 +35,8 @@ import org.osgi.framework.launch.Framework;import org.osgi.framework.wiring.*;import org.osgi.service.log.*;+import generic.io.NullPrintWriter;import generic.jar.ResourceFile;-import ghidra.app.plugin.core.script.osgi.BundlePath;import ghidra.app.script.GhidraScriptUtil;import ghidra.util.exception.CancelledException;import ghidra.util.task.*;@@ -534,26 +534,7 @@ public class BundleHost {}/**- * Change the status of the bundle referenced by <code>path</code>- *- * @param path the bundle path to activate/deactivate- * @param value the new activation value- * @throws InterruptedException- */- public void setActive(BundlePath path, boolean value) throws InterruptedException {- if (path.isDirectory()) {- SourceBundleInfo sbi = getSourceBundleInfo(path.getPath());- }- Thread.sleep(3000);- }-- /**- * synchronously perform steps necessary to activate bundle:- * <ol>- * <li>if source bundle, checkCompile</li>- * <li>b</li>- *- * </ol>+ * compile a source bundle if it's binary is out of sync** @param bi the bundle info* @param writer where to write issues@@ -562,8 +543,12 @@ public class BundleHost {* @return the activated bundle* @throws InterruptedException if interrupted while waiting for bundle state change*/- public Bundle activate(SourceBundleInfo bi, PrintWriter writer)+ public Bundle compileSourceBundle(SourceBundleInfo bi, PrintWriter writer)throws OSGiException, IOException, InterruptedException {+ if (writer == null) {+ writer = new NullPrintWriter();+ }+bi.updateFromFilesystem(writer);// needsCompile => needsBundleActivate@@ -609,10 +594,10 @@ public class BundleHost {if (needsCompile) {bi.deleteOldBinaries();- BundleCompiler bc = new BundleCompiler(this);+ BundleCompiler bundleCompiler = new BundleCompiler(this);long startTime = System.nanoTime();- bc.compileToExplodedBundle(bi, writer);+ bundleCompiler.compileToExplodedBundle(bi, writer);long endTime = System.nanoTime();writer.printf("%3.2f seconds compile time.\n", (endTime - startTime) / 1e9);}@@ -630,4 +615,12 @@ public class BundleHost {return b;}+ public void addListener(BundleListener bundleListener) {+ bc.addBundleListener(bundleListener);+ }++ public void removeListener(BundleListener bundleListener) {+ bc.removeBundleListener(bundleListener);+ }+}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.javaindex b8a445309a..5a37ee2136 100644--- 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@@ -29,10 +29,10 @@ import ghidra.app.script.GhidraScriptUtil;import ghidra.app.script.ScriptInfo;import ghidra.app.script.osgi.BundleHost.BuildFailure;+/**+ * The SourceBundleInfo class is a cache of information about source directory bundles.+ */public class SourceBundleInfo {- /**- *- */private final BundleHost bundle_host;final private ResourceFile sourceDir;final String symbolicName;@@ -223,5 +223,4 @@ public class SourceBundleInfo {public long getLastCompileAttempt() {return lastCompileAttempt;}-}diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.javaindex c9c18f8578..034f6be98e 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java@@ -287,7 +287,7 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTesdir.getFile(false).mkdirs();SwingUtilities.invokeLater(() -> {- bundleStatusProvider.getModel().insertPathForTesting(0, dir.getAbsolutePath());+ bundleStatusProvider.getModel().insertPathForTesting(dir.getAbsolutePath());});waitForSwing();