Ghidra is a software reverse engineering (SRE) framework
do dependency checking across bundles with static capabilities checking
Jason P. Leasure1317e0ad4d63
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 5b29ece90b..936ac030c9 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@@ -19,6 +19,7 @@ import java.io.*;import java.nio.file.Files;import java.nio.file.Path;import java.util.*;+import java.util.Map.Entry;import java.util.concurrent.CopyOnWriteArrayList;import java.util.concurrent.CountDownLatch;import java.util.function.Function;@@ -26,6 +27,8 @@ import java.util.stream.Collectors;import org.apache.felix.framework.FrameworkFactory;import org.apache.felix.framework.util.FelixConstants;+import org.jgrapht.graph.DirectedMultigraph;+import org.jgrapht.traverse.TopologicalOrderIterator;import org.osgi.framework.*;import org.osgi.framework.launch.Framework;import org.osgi.framework.wiring.*;@@ -36,7 +39,6 @@ import ghidra.framework.Application;import ghidra.framework.options.SaveState;import ghidra.framework.plugintool.PluginTool;import ghidra.util.Msg;-import ghidra.util.exception.CancelledException;import ghidra.util.task.*;/**@@ -673,7 +675,53 @@ public class BundleHost {listeners.remove(bundleHostListener);}- protected void activateAll(Collection<GhidraBundle> bundles, TaskMonitor monitor,+ /**+ * Activate a set of bundles and any dependencies in topological order. This method doesn't rely on the+ * framework, and so will add non-active dependencies.+ *+ * @param bundles bundles to activate+ * @param monitor a task monitor+ * @param console where to write build messages+ */+ public void activateAll(Collection<GhidraBundle> bundles, TaskMonitor monitor,+ PrintWriter console) {++ List<GhidraBundle> availableBundles = getGhidraBundles().stream()+ .filter(GhidraBundle::isEnabled)+ .collect(Collectors.toList());++ BundleDependencyGraph dependencyGraph =+ new BundleDependencyGraph(availableBundles, bundles, monitor);++ monitor.setMaximum(dependencyGraph.vertexSet().size());++ for (GhidraBundle bundle : dependencyGraph.inTopologicalOrder()) {+ if (monitor.isCancelled()) {+ break;+ }+ try {+ bundle.build(console);+ activateSynchronously(bundle.getLocationIdentifier());+ }+ catch (GhidraBundleException e) {+ fireBundleException(e);+ }+ catch (Exception e) {+ e.printStackTrace(console);+ }+ monitor.incrementProgress(1);+ }+ }++ /**+ * Activate a set of bundles in dependency topological order by resolving against currently+ * active bundles in stages. No bundles outside those requested will be activated.+ *+ * @param bundles bundles to activate+ * @param monitor a task monitor+ * @param console where to write build messages+ */+ public void activateInStages(Collection<GhidraBundle> bundles, TaskMonitor monitor,PrintWriter console) {List<GhidraBundle> bundlesRemaining = new ArrayList<>(bundles);@@ -783,17 +831,6 @@ public class BundleHost {listeners.add(bundleHostListener);}- private void startActivateAllTask(Collection<GhidraBundle> bundlesToActivate) {- if (!bundlesToActivate.isEmpty()) {- new TaskLauncher(new Task("restoring bundle state", true, true, false) {- @Override- public void run(TaskMonitor monitor) throws CancelledException {- activateAll(bundlesToActivate, monitor, new NullPrintWriter());- }- });- }- }-/*** Restore the list of managed bundles from {@code saveState} and each bundle's state.*@@ -845,7 +882,9 @@ public class BundleHost {}}add(newBundles);- startActivateAllTask(bundlesToActivate);++ TaskLauncher.launchNonModal("restoring bundle state",+ (monitor) -> activateInStages(bundlesToActivate, monitor, new NullPrintWriter()));}/**@@ -875,6 +914,118 @@ public class BundleHost {saveState.putBooleans(SAVE_STATE_TAG_SYSTEM, bundleIsSystem);}+ private static class Dependency {+ // exists only to be distinguished by id+ }++ private static class BundleDependencyGraph+ extends DirectedMultigraph<GhidraBundle, Dependency> {+ final Map<GhidraBundle, List<BundleCapability>> capabilityMap = new HashMap<>();+ final List<GhidraBundle> availableBundles;+ final TaskMonitor monitor;++ BundleDependencyGraph(List<GhidraBundle> availableBundles,+ Collection<GhidraBundle> startingBundles, TaskMonitor monitor) {+ super(null, null, false);+ this.availableBundles = availableBundles;+ this.monitor = monitor;++ Map<GhidraBundle, Set<GhidraBundle>> front = new HashMap<>();+ for (GhidraBundle bundle : startingBundles) {+ front.put(bundle, null);+ }++ while (!front.isEmpty() && !monitor.isCancelled()) {+ addFront(front);+ Map<GhidraBundle, Set<GhidraBundle>> newFront = new HashMap<>();++ for (GhidraBundle bundle : front.keySet()) {+ resolve(bundle, newFront);+ }+ handleBackEdges(newFront);+ front = newFront;+ }++ }++ Iterable<GhidraBundle> inTopologicalOrder() {+ return () -> new TopologicalOrderIterator<>(this);+ }++ void handleBackEdges(Map<GhidraBundle, Set<GhidraBundle>> newFront) {+ Iterator<Entry<GhidraBundle, Set<GhidraBundle>>> newFrontIter =+ newFront.entrySet().iterator();+ while (newFrontIter.hasNext() && !monitor.isCancelled()) {+ Entry<GhidraBundle, Set<GhidraBundle>> entry = newFrontIter.next();+ GhidraBundle src = entry.getKey();+ if (containsVertex(src)) {+ for (GhidraBundle dst : entry.getValue()) {+ addEdge(src, dst, new Dependency());+ }+ newFrontIter.remove();+ }+ }+ }++ void addFront(Map<GhidraBundle, Set<GhidraBundle>> front) {+ for (Entry<GhidraBundle, Set<GhidraBundle>> e : front.entrySet()) {+ GhidraBundle src = e.getKey();+ availableBundles.add(src);+ addVertex(src);+ Set<GhidraBundle> dsts = e.getValue();+ if (dsts != null) {+ for (GhidraBundle dst : dsts) {+ addEdge(src, dst, new Dependency());+ }+ }+ }+ }++ void resolve(GhidraBundle bundle, Map<GhidraBundle, Set<GhidraBundle>> newFront) {+ List<BundleRequirement> reqs;+ try {+ reqs = new ArrayList<>(bundle.getAllRequirements());+ }+ catch (GhidraBundleException e) {+ throw new RuntimeException(e);+ }+ if (reqs.isEmpty()) {+ return;+ }++ for (GhidraBundle depBundle : availableBundles) {+ for (BundleCapability cap : getCapabilities(depBundle)) {+ if (monitor.isCancelled()) {+ return;+ }+ Iterator<BundleRequirement> reqIter = reqs.iterator();+ while (reqIter.hasNext()) {+ BundleRequirement req = reqIter.next();+ if (req.matches(cap)) {+ newFront.computeIfAbsent(depBundle, b -> new HashSet<>()).add(bundle);+ reqIter.remove();+ }+ }+ if (reqs.isEmpty()) {+ return;+ }+ }+ }+ }++ List<BundleCapability> getCapabilities(GhidraBundle bundle) {+ return capabilityMap.computeIfAbsent(bundle, b -> {+ try {+ return b.getAllCapabilities();+ }+ catch (GhidraBundleException e) {+ Msg.error(this, "getting capabilities", e);+ return null;+ }+ });+ }+ }+/*** The {@code BundleListener} that notifies {@link BundleHostListener}s of bundle activation changes*/diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.javaindex 95990635b3..fedc7d14aa 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusComponentProvider.java@@ -17,6 +17,7 @@ package ghidra.app.plugin.core.osgi;import java.awt.*;import java.io.File;+import java.io.PrintWriter;import java.util.*;import java.util.List;import java.util.stream.Collectors;@@ -217,8 +218,7 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {}// then activate them all- new TaskLauncher(- new EnableAndActivateBundlesTask("activating", true, true, false, statuses),+ new TaskLauncher(new EnableAndActivateBundlesTask("activating", statuses, true),getComponent(), 1000);}@@ -283,7 +283,8 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {List<File> files = fileChooser.getSelectedFiles();if (!files.isEmpty()) {- Preferences.setProperty(PREFERENCE_LAST_SELECTED_BUNDLE, files.get(0).getAbsolutePath());+ Preferences.setProperty(PREFERENCE_LAST_SELECTED_BUNDLE,+ files.get(0).getAbsolutePath());List<ResourceFile> resourceFiles =files.stream().map(ResourceFile::new).collect(Collectors.toUnmodifiableList());Collection<GhidraBundle> bundles = bundleHost.add(resourceFiles, true, false);@@ -300,14 +301,13 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {}protected void doEnableBundles() {- new TaskLauncher(- new EnableAndActivateBundlesTask("enabling", true, true, false, getSelectedStatuses()),+ new TaskLauncher(new EnableAndActivateBundlesTask("enabling", getSelectedStatuses(), false),getComponent(), 1000);}protected void doDisableBundles() {- new TaskLauncher(new DeactivateAndDisableBundlesTask("disabling", true, true, false,- getSelectedStatuses()), getComponent(), 1000);+ new TaskLauncher(new DeactivateAndDisableBundlesTask("disabling", getSelectedStatuses()),+ getComponent(), 1000);}protected void doActivateDeactivateBundle(BundleStatus status, boolean activate) {@@ -366,7 +366,7 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {private RemoveBundlesTask(String title, List<BundleStatus> statuses) {super(title);this.deactivateBundlesTask =- new DeactivateAndDisableBundlesTask("deactivating", true, true, false, statuses);+ new DeactivateAndDisableBundlesTask("deactivating", statuses);this.statuses = statuses;}@@ -397,10 +397,20 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {private class EnableAndActivateBundlesTask extends Task {private final List<BundleStatus> statuses;- private EnableAndActivateBundlesTask(String title, boolean canCancel, boolean hasProgress,- boolean isModal, List<BundleStatus> statuses) {- super(title, canCancel, hasProgress, isModal);+ private final boolean inStages;++ /**+ * A task to enable and activate bundles.+ *+ * @param title the title+ * @param statuses the bundle statuses+ * @param inStages see {@link BundleHost#activateInStages}+ */+ private EnableAndActivateBundlesTask(String title, List<BundleStatus> statuses,+ boolean inStages) {+ super(title, true, true, false);this.statuses = statuses;+ this.inStages = inStages;}@Override@@ -422,8 +432,13 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {}notifyTableDataChanged();- bundleHost.activateAll(bundles, monitor,- getTool().getService(ConsoleService.class).getStdErr());+ PrintWriter writer = getTool().getService(ConsoleService.class).getStdErr();+ if (inStages) {+ bundleHost.activateInStages(bundles, monitor, writer);+ }+ else {+ bundleHost.activateAll(bundles, monitor, writer);+ }boolean anybusy = false;for (BundleStatus status : statuses) {@@ -443,9 +458,8 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {private class DeactivateAndDisableBundlesTask extends Task {final List<BundleStatus> statuses;- private DeactivateAndDisableBundlesTask(String title, boolean canCancel,- boolean hasProgress, boolean isModal, List<BundleStatus> statuses) {- super(title, canCancel, hasProgress, isModal);+ private DeactivateAndDisableBundlesTask(String title, List<BundleStatus> statuses) {+ super(title, true, true, false);this.statuses = statuses;}@@ -490,11 +504,11 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {try {GhidraBundle bundle = bundleHost.getExistingGhidraBundle(status.getFile());if (activate) {- bundle.build(console.getStdErr());if (status.getSummary().startsWith(BundleHost.ACTIVATING_BUNDLE_ERROR_MSG)) {status.setSummary("");}- bundleHost.activateSynchronously(bundle.getLocationIdentifier());+ bundleHost.activateAll(Collections.singletonList(bundle), monitor,+ console.getStdErr());}else { // deactivatebundleHost.deactivateSynchronously(bundle.getLocationIdentifier());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 2473c4cae9..5043ae9c03 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@@ -20,6 +20,7 @@ import java.io.PrintWriter;import java.util.List;import org.osgi.framework.Bundle;+import org.osgi.framework.wiring.BundleCapability;import org.osgi.framework.wiring.BundleRequirement;import generic.jar.ResourceFile;@@ -83,7 +84,9 @@ public abstract class GhidraBundle {*/public abstract String getLocationIdentifier();- abstract List<BundleRequirement> getAllRequirements() throws GhidraBundleException;+ public abstract List<BundleRequirement> getAllRequirements() throws GhidraBundleException;++ public abstract List<BundleCapability> getAllCapabilities() throws GhidraBundleException;/*** @return the file where this bundle is loaded fromdiff --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 26e782f1ac..6076abde3e 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@@ -16,14 +16,16 @@package ghidra.app.plugin.core.osgi;import java.io.PrintWriter;-import java.util.Collections;import java.util.List;+import java.util.Map;+import java.util.jar.Attributes;import java.util.jar.Manifest;+import java.util.stream.Collectors;-import org.osgi.framework.BundleException;+import org.apache.felix.framework.util.manifestparser.ManifestParser;+import org.osgi.framework.wiring.BundleCapability;import org.osgi.framework.wiring.BundleRequirement;-import aQute.bnd.osgi.Constants;import aQute.bnd.osgi.Jar;import generic.jar.ResourceFile;@@ -62,24 +64,31 @@ public class GhidraJarBundle extends GhidraBundle {return bundleLocation;}- @Override- public List<BundleRequirement> getAllRequirements() throws GhidraBundleException {+ protected ManifestParser createManifestParser() {try (Jar jar = new Jar(file.getFile(true))) {Manifest manifest = jar.getManifest();- String importPackageString =- manifest.getMainAttributes().getValue(Constants.IMPORT_PACKAGE);- if (importPackageString != null) {- return OSGiUtils.parseImportPackage(importPackageString);- }- return Collections.emptyList();- }- catch (BundleException e) {- throw new GhidraBundleException(this.getLocationIdentifier(), "error parsing imports",- e);+ Attributes mainAttributes = manifest.getMainAttributes();+ Map<String, Object> headerMap = mainAttributes.entrySet()+ .stream()+ .collect(+ Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));+ return new ManifestParser(null, null, null, headerMap);}catch (Exception e) {throw new RuntimeException(e);}}+ @Override+ public List<BundleRequirement> getAllRequirements() throws GhidraBundleException {+ ManifestParser manifestParser = createManifestParser();+ return manifestParser.getRequirements();+ }++ @Override+ public List<BundleCapability> getAllCapabilities() throws GhidraBundleException {+ ManifestParser manifestParser = createManifestParser();+ return manifestParser.getCapabilities();+ }+}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraPlaceholderBundle.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraPlaceholderBundle.javaindex 9b66c9fa04..e12469bae5 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraPlaceholderBundle.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraPlaceholderBundle.java@@ -19,6 +19,7 @@ import java.io.PrintWriter;import java.util.Collections;import java.util.List;+import org.osgi.framework.wiring.BundleCapability;import org.osgi.framework.wiring.BundleRequirement;import generic.jar.ResourceFile;@@ -58,7 +59,12 @@ public class GhidraPlaceholderBundle extends GhidraBundle {}@Override- List<BundleRequirement> getAllRequirements() {+ public List<BundleRequirement> getAllRequirements() throws GhidraBundleException {+ return Collections.emptyList();+ }++ @Override+ public List<BundleCapability> getAllCapabilities() throws GhidraBundleException {return Collections.emptyList();}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 840561a713..d3f7ead4b9 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@@ -32,17 +32,18 @@ import java.util.stream.Stream;import javax.tools.*;import javax.tools.JavaFileObject.Kind;+import org.apache.felix.framework.util.manifestparser.ManifestParser;import org.osgi.framework.Bundle;import org.osgi.framework.BundleException;import org.osgi.framework.Constants;-import org.osgi.framework.wiring.BundleRequirement;-import org.osgi.framework.wiring.BundleWiring;+import org.osgi.framework.wiring.*;import org.phidias.compile.BundleJavaManager;import aQute.bnd.osgi.*;import aQute.bnd.osgi.Clazz.QUERY;import generic.io.NullPrintWriter;import generic.jar.ResourceFile;+import generic.jar.ResourceFileFilter;import ghidra.app.script.*;import ghidra.util.Msg;@@ -56,6 +57,8 @@ import ghidra.util.Msg;*/public class GhidraSourceBundle extends GhidraBundle {private static final String GENERATED_ACTIVATOR_CLASSNAME = "GeneratedActivator";+ private static final String GENERATED_VERSION = "1.0";+/** Match the leftover part of a class file on removing the class name, e.g.* we've found "MyClass.java", so we match "MyClass.class" by removing "MyClass" then@@ -273,29 +276,85 @@ public class GhidraSourceBundle extends GhidraBundle {return dedupedReqs;}- @Override- public List<BundleRequirement> getAllRequirements() throws GhidraBundleException {- updateRequirementsFromMetadata();- Map<String, BundleRequirement> reqs = getComputedReqs();- // insert requirements from a source manifest+ protected ManifestParser createSourceManifestParser() {ResourceFile manifestFile = getSourceManifestFile();if (manifestFile.exists()) {- try {- try (InputStream manifestInputStream = manifestFile.getInputStream()) {- Manifest manifest = new Manifest(manifestInputStream);- String importPackage = manifest.getMainAttributes().getValue("Import-Package");- for (BundleRequirement r : OSGiUtils.parseImportPackage(importPackage)) {- reqs.putIfAbsent(r.toString(), r);- }- }+ try (InputStream manifestInputStream = manifestFile.getInputStream()) {+ Manifest manifest = new Manifest(manifestInputStream);+ Attributes mainAttributes = manifest.getMainAttributes();+ Map<String, Object> headerMap = mainAttributes.entrySet()+ .stream()+ .collect(Collectors.toMap(e -> e.getKey().toString(),+ e -> e.getValue().toString()));+ return new ManifestParser(null, null, null, headerMap);}catch (IOException | BundleException e) {throw new RuntimeException(e);}}+ return null;+ }++ @Override+ public List<BundleRequirement> getAllRequirements() throws GhidraBundleException {+ ManifestParser manifestParser = createSourceManifestParser();+ if (manifestParser != null) {+ return manifestParser.getRequirements();+ }++ updateRequirementsFromMetadata();+ Map<String, BundleRequirement> reqs = getComputedReqs();return new ArrayList<>(reqs.values());}+ protected static void findPackageDirs(List<String> packages, ResourceFile directory) {+ for (ResourceFile file : directory+ .listFiles(f -> f.isDirectory() || f.getName().endsWith(".java"))) {+ if (!file.getName().matches("internal|private")) {+ boolean added = false;+ if (file.isDirectory()) {+ findPackageDirs(packages, file);+ }+ else if (!added) {+ added = true;+ packages.add(directory.getAbsolutePath());+ }+ }+ }+ }++ @Override+ public List<BundleCapability> getAllCapabilities() throws GhidraBundleException {+ ManifestParser manifestParser = createSourceManifestParser();+ if (manifestParser != null) {+ return manifestParser.getCapabilities();+ }++ int sourceDirLength = getSourceDirectory().getAbsolutePath().length() + 1;+ List<String> packageDirs = new ArrayList<>();+ findPackageDirs(packageDirs, getSourceDirectory());+ StringBuilder sb = new StringBuilder();+ for (String packageDir : packageDirs) {+ // skip unnamed package+ if (packageDir.length() > sourceDirLength) {+ String packageName =+ packageDir.substring(sourceDirLength).replace(File.separatorChar, '.');+ sb.append(',');+ sb.append(packageName);+ sb.append(";version=\"" + GENERATED_VERSION + "\"");+ }+ }+ try {+ if (sb.length() == 0) {+ return Collections.emptyList();+ }+ return OSGiUtils.parseExportPackage(sb.substring(1));+ }+ catch (BundleException e) {+ throw new GhidraBundleException(getLocationIdentifier(), "exports error", e);+ }+ }+/*** look for new sources, metadata, manifest file.*@@ -720,7 +779,7 @@ public class GhidraSourceBundle extends GhidraBundle {analyzer.setJar(new Jar(binaryDir.toFile())); // give bnd the contentsanalyzer.setProperty("Bundle-SymbolicName",GhidraSourceBundle.sourceDirHash(getSourceDirectory()));- analyzer.setProperty("Bundle-Version", "1.0");+ analyzer.setProperty("Bundle-Version", GENERATED_VERSION);// XXX we must constrain analyzed imports according to constraints declared in @importpackage tagsanalyzer.setProperty("Import-Package", "*");analyzer.setProperty("Export-Package", "!*.private.*,!*.internal.*,*");@@ -787,7 +846,7 @@ public class GhidraSourceBundle extends GhidraBundle {* create and compile a default bundle activator** @param bindir destination for class file- * @param activatorClassName the name to use for the genearted activator class+ * @param activatorClassName the name to use for the generated activator class* @param writer for writing compile errors* @return true if compilation succeeded* @throws IOException for failed write of source/binary activatordiff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiUtils.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiUtils.javaindex 4ea0fb9dc1..4ad7d00f9e 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiUtils.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiUtils.java@@ -28,6 +28,7 @@ import java.util.stream.Stream;import org.apache.felix.framework.util.manifestparser.ManifestParser;import org.osgi.framework.*;+import org.osgi.framework.wiring.BundleCapability;import org.osgi.framework.wiring.BundleRequirement;import ghidra.util.Msg;@@ -111,13 +112,27 @@ public class OSGiUtils {*/static List<BundleRequirement> parseImportPackage(String importPackageString)throws BundleException {- // parse it with Felix's ManifestParser to a list of BundleRequirement objectsMap<String, Object> headerMap = new HashMap<>();headerMap.put(Constants.IMPORT_PACKAGE, importPackageString);ManifestParser manifestParser = new ManifestParser(null, null, null, headerMap);return manifestParser.getRequirements();}+ /**+ * parse Export-Package string from a bundle manifest+ *+ * @param exportPackageString Import-Package value+ * @return deduced capabilities or null if there was an error+ * @throws BundleException on parse failure+ */+ static List<BundleCapability> parseExportPackage(String exportPackageString)+ throws BundleException {+ Map<String, Object> headerMap = new HashMap<>();+ headerMap.put(Constants.EXPORT_PACKAGE, exportPackageString);+ ManifestParser manifestParser = new ManifestParser(null, null, null, headerMap);+ return manifestParser.getCapabilities();+ }+// from https://dzone.com/articles/locate-jar-classpath-givenstatic String findJarForClass(Class<?> c) {URL location;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 c4b2f6f7b4..9ea87aec86 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@@ -16,12 +16,14 @@package ghidra.app.script;import java.io.*;+import java.util.Collections;import org.osgi.framework.Bundle;import generic.jar.ResourceFile;import ghidra.app.plugin.core.osgi.*;import ghidra.util.Msg;+import ghidra.util.task.TaskMonitor;public class JavaScriptProvider extends GhidraScriptProvider {private final BundleHost bundleHost;@@ -111,14 +113,10 @@ public class JavaScriptProvider extends GhidraScriptProvider {*/public Class<?> loadClass(ResourceFile sourceFile, PrintWriter writer) throws Exception {GhidraSourceBundle bundle = getBundleForSource(sourceFile);- bundle.build(writer);-- Bundle osgiBundle = bundleHost.install(bundle);-- bundleHost.activateSynchronously(osgiBundle);+ bundleHost.activateAll(Collections.singletonList(bundle), TaskMonitor.DUMMY, writer);String classname = bundle.classNameForScript(sourceFile);- Class<?> clazz = osgiBundle.loadClass(classname); // throws ClassNotFoundException+ Class<?> clazz = bundle.getOSGiBundle().loadClass(classname); // throws ClassNotFoundExceptionreturn clazz;}