Skip to content

Ghidra is a software reverse engineering (SRE) framework

add placeholder bundle for missing file, rewire bundlestatusmanager

Jason P. Leasureb53e752b8aaa
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.java
index 82f58d1a61..f1c4151dbb 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
@@ -88,6 +88,15 @@ public class BundleHost {
return false;
}
+ public boolean disable(GhidraBundle gbundle) {
+ if (gbundle.isEnabled()) {
+ gbundle.setEnabled(false);
+ fireBundleEnablementChange(gbundle, false);
+ return true;
+ }
+ return false;
+ }
+
public GhidraBundle addGhidraBundle(ResourceFile path, boolean enabled, boolean systemBundle) {
GhidraBundle gb = newGhidraBundle(this, path, enabled, systemBundle);
bp2gb.put(path, gb);
@@ -119,13 +128,13 @@ public class BundleHost {
switch (GhidraBundle.getType(path)) {
case SourceDir:
return new GhidraSourceBundle(bh, path, enabled, systemBundle);
- case BndScript:
case Jar:
return new GhidraJarBundle(bh, path, enabled, systemBundle);
+ case BndScript:
default:
break;
}
- return null;
+ return new GhidraPlaceholderBundle(bh, path, enabled, systemBundle);
}
// XXX consumers must clean up after themselves
@@ -739,12 +748,6 @@ public class BundleHost {
for (int i = 0; i < pathArr.length; i++) {
ResourceFile bp = generic.util.Path.fromPathString(pathArr[i]);
- if (!bp.exists()) {
- Msg.showWarn(this, null, "missing bundle path",
- "the bundle path " + bp.toString() + " no longer exists, removing");
-
- continue;
- }
boolean en = enableArr[i];
boolean act = activeArr[i];
boolean sys = systemArr[i];
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatus.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatus.java
index 7e92a4e20c..3b417c49ea 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatus.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatus.java
@@ -26,6 +26,7 @@ import generic.util.Path;
public class BundleStatus implements Comparable<BundleStatus> {
final Path path;
final GhidraBundle.Type type;
+ final String bundleLoc;
boolean active = false;
boolean busy = false;
@@ -48,9 +49,10 @@ public class BundleStatus implements Comparable<BundleStatus> {
return type;
}
- BundleStatus(ResourceFile path, boolean enabled, boolean readonly) {
+ BundleStatus(ResourceFile path, boolean enabled, boolean readonly, String bundleLoc) {
this.path = new Path(path, enabled, false, readonly);
type = GhidraBundle.getType(getPath());
+ this.bundleLoc = bundleLoc;
}
public boolean isDirectory() {
@@ -98,4 +100,8 @@ public class BundleStatus implements Comparable<BundleStatus> {
return path.getPathAsString();
}
+ public String getBundleLoc() {
+ return bundleLoc;
+ }
+
}
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.java
index 68a866b4b1..b4615e4cef 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,7 +17,7 @@ package ghidra.app.plugin.core.osgi;
import java.awt.*;
import java.io.File;
-import java.util.Arrays;
+import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
@@ -38,6 +38,7 @@ import ghidra.framework.plugintool.ComponentProviderAdapter;
import ghidra.framework.plugintool.PluginTool;
import ghidra.framework.preferences.Preferences;
import ghidra.util.HelpLocation;
+import ghidra.util.Msg;
import ghidra.util.exception.CancelledException;
import ghidra.util.filechooser.GhidraFileChooserModel;
import ghidra.util.filechooser.GhidraFileFilter;
@@ -73,14 +74,26 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
bundleStatusTableModel.addListener(new BundleStatusChangeRequestListener() {
@Override
public void bundleEnablementChangeRequest(BundleStatus status, boolean enabled) {
- if (!enabled && status.isActive()) {
- startActivateDeactiveTask(status, false);
+ GhidraBundle gb = bundleHost.getExistingGhidraBundle(status.getPath());
+ if (gb instanceof GhidraPlaceholderBundle) {
+ return;
+ }
+ if (enabled) {
+ bundleHost.enable(gb);
+ }
+ else {
+ if (status.isActive()) {
+ startActivateDeactiveTask(status, false);
+ }
+ bundleHost.disable(gb);
}
}
@Override
public void bundleActivationChangeRequest(BundleStatus status, boolean newValue) {
- startActivateDeactiveTask(status, newValue);
+ if (status.isEnabled()) {
+ startActivateDeactiveTask(status, newValue);
+ }
}
});
@@ -177,8 +190,8 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
ResourceFile path = (ResourceFile) data.getValue();
JLabel c = (JLabel) super.getTableCellRendererComponent(data);
c.setText(Path.toPathString(path));
-
- if (!path.exists()) {
+ GhidraBundle gb = bundleHost.getExistingGhidraBundle(path);
+ if (gb == null || gb instanceof GhidraPlaceholderBundle || !path.exists()) {
c.setForeground(Color.RED);
}
return c;
@@ -338,9 +351,26 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
if (selectedModelRows == null || selectedModelRows.length == 0) {
return;
}
+ doDeactivateBundles();
+
+ Map<Boolean, List<GhidraBundle>> bundles =
+ bundleStatusTableModel.getRowObjects(selectedModelRows).stream().map(
+ bs -> bundleHost.getExistingGhidraBundle(bs.getPath())).collect(
+ Collectors.partitioningBy(gb -> gb.isSystemBundle()));
+ List<GhidraBundle> systemBundles = bundles.get(true);
+ if (!systemBundles.isEmpty()) {
+ StringBuilder sb = new StringBuilder();
+ for (GhidraBundle gb : systemBundles) {
+ bundleHost.disable(gb);
+ sb.append(gb.getPath() + "\n");
+ }
+ Msg.showWarn(this, this.getComponent(), "Unabled to remove",
+ "System bundles cannot be removed:\n" + sb.toString());
+
+ }
+
+ bundleHost.remove(bundles.get(false));
- bundleStatusTableModel.remove(selectedModelRows);
- bundleStatusTable.clearSelection();
}
private void showAddBundlesFileChooser() {
@@ -402,15 +432,19 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
bundleStatusTableModel.getRowObjects(selectedModelRows).stream().filter(
bs -> !bs.isActive()).collect(Collectors.toUnmodifiableList());
+ List<GhidraBundle> gbs = new ArrayList<>();
for (BundleStatus bs : statuses) {
- bs.setBusy(true);
+ GhidraBundle gb = bundleHost.getExistingGhidraBundle(bs.getPath());
+ if (!(gb instanceof GhidraPlaceholderBundle)) {
+ bs.setBusy(true);
+ bundleHost.enable(gb);
+ gbs.add(gb);
+ }
}
notifyTableDataChanged();
- List<GhidraBundle> gbs = statuses.stream().map(
- bs -> bundleHost.getExistingGhidraBundle(bs.getPath())).collect(
- Collectors.toList());
- bundleHost.activateAll(gbs, monitor, getTool().getService(ConsoleService.class).getStdErr());
+ bundleHost.activateAll(gbs, monitor,
+ getTool().getService(ConsoleService.class).getStdErr());
boolean anybusy = false;
for (BundleStatus bs : statuses) {
@@ -435,31 +469,22 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
new TaskLauncher(new Task("deactivating", true, true, false) {
@Override
public void run(TaskMonitor monitor) throws CancelledException {
- long startTime = System.nanoTime();
-
List<GhidraBundle> gbs =
bundleStatusTableModel.getRowObjects(selectedModelRows).stream().filter(
bs -> bs.isActive()).map(
bs -> bundleHost.getExistingGhidraBundle(bs.getPath())).collect(
Collectors.toList());
- int total = gbs.size();
- int total_deactivated = 0;
monitor.setMaximum(gbs.size());
for (GhidraBundle gb : gbs) {
try {
bundleHost.deactivateSynchronously(gb.getBundleLoc());
- ++total_deactivated;
}
catch (GhidraBundleException | InterruptedException e) {
e.printStackTrace(console.getStdErr());
}
monitor.incrementProgress(1);
}
- long endTime = System.nanoTime();
- console.getStdOut().printf("%d/%d bundles deactivated in %3.2f seconds.\n",
- total_deactivated, total, (endTime - startTime) / 1e9);
-
}
}, getComponent(), 1000);
}
@@ -473,13 +498,13 @@ public class BundleStatusComponentProvider extends ComponentProviderAdapter {
@Override
public void run(TaskMonitor monitor) throws CancelledException {
try {
- GhidraBundle sb = bundleHost.getExistingGhidraBundle(status.getPath());
+ GhidraBundle gb = bundleHost.getExistingGhidraBundle(status.getPath());
if (activate) {
- sb.build(console.getStdErr());
- bundleHost.activateSynchronously(sb.getBundleLoc());
+ gb.build(console.getStdErr());
+ bundleHost.activateSynchronously(gb.getBundleLoc());
}
else { // deactivate
- bundleHost.deactivateSynchronously(sb.getBundleLoc());
+ bundleHost.deactivateSynchronously(gb.getBundleLoc());
}
}
catch (Exception e) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusTableModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusTableModel.java
index b8fc6f4418..051e2d752c 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusTableModel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusTableModel.java
@@ -137,14 +137,6 @@ public class BundleStatusTableModel extends AbstractSortedTableModel<BundleStatu
return status;
}
- private String getBundleLoc(BundleStatus status) {
- GhidraBundle gb = bundleHost.getExistingGhidraBundle(status.getPath());
- if (gb != null) {
- return gb.getBundleLoc();
- }
- return null;
- }
-
BundleStatusTableModel(BundleStatusComponentProvider provider, BundleHost bundleHost) {
super();
this.provider = provider;
@@ -240,17 +232,15 @@ public class BundleStatusTableModel extends AbstractSortedTableModel<BundleStatu
return list;
}
-
private void addNewStatusNoFire(GhidraBundle gb) {
- BundleStatus status = new BundleStatus(gb.getPath(), gb.isEnabled(), gb.isSystemBundle());
+ BundleStatus status =
+ new BundleStatus(gb.getPath(), gb.isEnabled(), gb.isSystemBundle(), gb.getBundleLoc());
if (statuses.contains(status)) {
- throw new RuntimeException("Bundle status manager already contains " + gb.getPath().toString());
+ throw new RuntimeException(
+ "Bundle status manager already contains " + gb.getPath().toString());
}
status.setActive(gb.isActive());
- String loc = getBundleLoc(status);
- if (loc != null) {
- loc2status.put(loc, status);
- }
+ loc2status.put(status.getBundleLoc(), status);
statuses.add(status);
}
@@ -266,11 +256,10 @@ public class BundleStatusTableModel extends AbstractSortedTableModel<BundleStatu
private int removeStatusNoFire(BundleStatus status) {
if (!status.isReadOnly()) {
int i = statuses.indexOf(status);
- loc2status.remove(getBundleLoc(status));
+ statuses.remove(i);
+ loc2status.remove(status.getBundleLoc());
return i;
}
- Msg.showInfo(this, this.provider.getComponent(), "Unabled to remove path",
- "System path cannot be removed: " + status.getPath().toString());
return -1;
}
@@ -431,10 +420,7 @@ public class BundleStatusTableModel extends AbstractSortedTableModel<BundleStatu
private void computeCache() {
loc2status.clear();
for (BundleStatus status : statuses) {
- String loc = getBundleLoc(status);
- if (loc != null) {
- loc2status.put(loc, status);
- }
+ loc2status.put(status.getBundleLoc(), status);
}
}
@@ -446,8 +432,8 @@ public class BundleStatusTableModel extends AbstractSortedTableModel<BundleStatu
* @param paths the statuses to use
*/
public void setPathsForTesting(List<ResourceFile> paths) {
- this.statuses =
- paths.stream().map(f -> new BundleStatus(f, true, false)).collect(Collectors.toList());
+ this.statuses = paths.stream().map(f -> new BundleStatus(f, true, false, null)).collect(
+ Collectors.toList());
computeCache();
fireTableDataChanged();
}
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.java
new file mode 100644
index 0000000000..f61095b205
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraPlaceholderBundle.java
@@ -0,0 +1,63 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.app.plugin.core.osgi;
+
+import java.io.PrintWriter;
+import java.util.Collections;
+import java.util.List;
+
+import org.osgi.framework.wiring.BundleRequirement;
+
+import generic.jar.ResourceFile;
+
+public class GhidraPlaceholderBundle extends GhidraBundle {
+
+ GhidraPlaceholderBundle(BundleHost bundleHost, ResourceFile path, boolean enabled,
+ boolean systemBundle) {
+ super(bundleHost, path, enabled, systemBundle);
+ }
+
+ @Override
+ boolean clean() {
+ return false;
+ }
+
+ @Override
+ public boolean build(PrintWriter writer) throws Exception {
+ return false;
+ }
+
+ @Override
+ public String getBundleLoc() {
+ return "invalid://" + getPath();
+ }
+
+ @Override
+ List<BundleRequirement> getAllReqs() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return false;
+ }
+
+ @Override
+ public boolean isActive() {
+ return false;
+ }
+
+}
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.java
index 72bc83c18c..38a57f42ce 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
@@ -453,8 +453,8 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
public List<ResourceFile> getScriptDirectories() {
return bundleHost.getGhidraBundles().stream().filter(
- GhidraSourceBundle.class::isInstance).map(GhidraBundle::getPath).collect(
- Collectors.toList());
+ gb -> gb.isEnabled() && gb instanceof GhidraSourceBundle).map(
+ GhidraBundle::getPath).collect(Collectors.toList());
}
public List<ResourceFile> getWritableScriptDirectories() {
@@ -715,40 +715,35 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
@Override
public void bundleEnablementChange(GhidraBundle gbundle, boolean newEnablment) {
if (gbundle instanceof GhidraSourceBundle) {
- performRefresh();
+ refresh();
}
}
@Override
public void bundleAdded(GhidraBundle gbundle) {
plugin.getTool().setConfigChanged(true);
- performRefresh();
+ refresh();
}
@Override
public void bundlesAdded(Collection<GhidraBundle> gbundles) {
plugin.getTool().setConfigChanged(true);
- performRefresh();
+ refresh();
}
@Override
public void bundleRemoved(GhidraBundle gbundle) {
plugin.getTool().setConfigChanged(true);
- performRefresh();
+ refresh();
}
@Override
public void bundlesRemoved(Collection<GhidraBundle> gbundles) {
plugin.getTool().setConfigChanged(true);
- performRefresh();
+ refresh();
}
}
- private void performRefresh() {
- infoManager.clearMetadata();
- refresh();
- }
-
void refresh() {
hasBeenRefreshed = true;
@@ -777,6 +772,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
for (ResourceFile file : scriptsToRemove) {
removeScript(file);
+ infoManager.removeMetadata(file);
}
infoManager.refreshDuplicates();
@@ -1141,7 +1137,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
@Override
public void componentShown() {
if (!hasBeenRefreshed) {
- performRefresh();
+ refresh();
}
}
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.java
index f33f5d7874..2ad984aaec 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
@@ -98,6 +98,11 @@ public abstract class AbstractGhidraScriptMgrPluginTest
env = new TestEnv();
env.showTool(program);
env.getTool().addPlugin(CodeBrowserPlugin.class.getName());
+ Path userScriptDir = java.nio.file.Paths.get(GhidraScriptUtil.USER_SCRIPTS_DIR);
+ if (Files.notExists(userScriptDir)) {
+ Files.createDirectories(userScriptDir);
+ }
+
env.getTool().addPlugin(GhidraScriptMgrPlugin.class.getName());
browser = env.getPlugin(CodeBrowserPlugin.class);