Skip to content

Ghidra is a software reverse engineering (SRE) framework

Test stack trace fixes; updated the Data Types provider to clear the preview node when its archive is closed

dragonmacher49f2512e6d38
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/DataTypeManagerPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/DataTypeManagerPlugin.java
index b3cf35615d..f89b0e1daa 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/DataTypeManagerPlugin.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/DataTypeManagerPlugin.java
@@ -128,6 +128,8 @@ public class DataTypeManagerPlugin extends ProgramPlugin
ProjectArchive projectArchive = (ProjectArchive) archive;
projectArchive.getDomainObject().removeListener(DataTypeManagerPlugin.this);
}
+
+ provider.archiveClosed(archive.getDataTypeManager());
}
@Override
@@ -197,7 +199,7 @@ public class DataTypeManagerPlugin extends ProgramPlugin
/**
* Add project archive name to recently opened list
* @param projectName the project name
- * @param pathname the pathname
+ * @param pathname the pathname
*/
public void addRecentlyOpenedProjectArchive(String projectName, String pathname) {
String projectPathname = DataTypeManagerHandler.getProjectPathname(projectName, pathname);
@@ -215,7 +217,7 @@ public class DataTypeManagerPlugin extends ProgramPlugin
/**
* Add project archive to recently opened list provided it is contained within the
- * active project and is not a specific version (i.e., only latest version can be
+ * active project and is not a specific version (i.e., only latest version can be
* remembered).
* @param pa project archive
*/
@@ -238,8 +240,8 @@ public class DataTypeManagerPlugin extends ProgramPlugin
Project project = tool.getProjectManager().getActiveProject();
if (project != null && project.getName().equals(projectName)) {
DomainFile df = project.getProjectData().getFile(pathname);
- if (df != null && DataTypeArchiveContentHandler.DATA_TYPE_ARCHIVE_CONTENT_TYPE.equals(
- df.getContentType())) {
+ if (df != null && DataTypeArchiveContentHandler.DATA_TYPE_ARCHIVE_CONTENT_TYPE
+ .equals(df.getContentType())) {
return df;
}
}
@@ -305,7 +307,7 @@ public class DataTypeManagerPlugin extends ProgramPlugin
@Override
protected void programClosed(Program program) {
- // assumption: at this point programDeactivated(Program) has been called, so we don't
+ // assumption: at this point programDeactivated(Program) has been called, so we don't
// have to perform any cleanup that is done by that method.
provider.programClosed();
editorManager.dismissEditors(program.getDataTypeManager());
@@ -395,8 +397,8 @@ public class DataTypeManagerPlugin extends ProgramPlugin
private void createStandardArchivesMenu() {
installArchiveMap = new TreeMap<>();
- for (ResourceFile archiveFile : Application.findFilesByExtensionInApplication(
- FileDataTypeManager.SUFFIX)) {
+ for (ResourceFile archiveFile : Application
+ .findFilesByExtensionInApplication(FileDataTypeManager.SUFFIX)) {
Path path = new Path(archiveFile);
String absoluteFilePath = path.getPathAsString();
if (absoluteFilePath.indexOf("data/typeinfo") < 0) {
@@ -524,6 +526,7 @@ public class DataTypeManagerPlugin extends ProgramPlugin
@Override
public void closeArchive(DataTypeManager dtm) {
dataTypeManagerHandler.closeArchive(dtm);
+ provider.archiveClosed(dtm);
}
@Override
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/DataTypesProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/DataTypesProvider.java
index 0755c48db1..407f20518c 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/DataTypesProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/DataTypesProvider.java
@@ -833,6 +833,19 @@ public class DataTypesProvider extends ComponentProviderAdapter {
archiveGTree.cancelWork();
}
+ void archiveClosed(DataTypeManager dtm) {
+ if (lastPreviewNode == null || !(lastPreviewNode instanceof DataTypeNode)) {
+ return;
+ }
+
+ DataTypeNode dtNode = (DataTypeNode) lastPreviewNode;
+ DataType dt = dtNode.getDataType();
+ DataTypeManager dtManager = dt.getDataTypeManager();
+ if (dtm.equals(dtManager)) {
+ lastPreviewNode = null;
+ }
+ }
+
void programRenamed() {
ArchiveRootNode rootNode = (ArchiveRootNode) archiveGTree.getModelRoot();
List<GTreeNode> allChildren = rootNode.getChildren();
diff --git a/Ghidra/Features/Base/src/test/java/ghidra/util/extensions/ExtensionUtilsTest.java b/Ghidra/Features/Base/src/test/java/ghidra/util/extensions/ExtensionUtilsTest.java
index 5f0485e644..90fd391712 100644
--- a/Ghidra/Features/Base/src/test/java/ghidra/util/extensions/ExtensionUtilsTest.java
+++ b/Ghidra/Features/Base/src/test/java/ghidra/util/extensions/ExtensionUtilsTest.java
@@ -25,27 +25,27 @@ import java.util.zip.ZipOutputStream;
import org.junit.Before;
import org.junit.Test;
+import docking.test.AbstractDockingTest;
import generic.jar.ResourceFile;
-import generic.test.AbstractGenericTest;
import ghidra.framework.Application;
import ghidra.framework.plugintool.dialog.*;
import utilities.util.FileUtilities;
import utility.application.ApplicationLayout;
/**
- * Tests for the {@link ExtensionUtils} class.
+ * Tests for the {@link ExtensionUtils} class.
*
*/
-public class ExtensionUtilsTest extends AbstractGenericTest {
+public class ExtensionUtilsTest extends AbstractDockingTest {
- // Name used in all tests when creating extensions.
+ // Name used in all tests when creating extensions.
private String DEFAULT_EXT_NAME = "test";
private ApplicationLayout gLayout;
- /**
+ /*
* Create dummy archive and installation folders in the temp space that we can populate
- * with extensions.
+ * with extensions.
*/
@Before
public void setup() throws IOException {
@@ -64,7 +64,7 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
createExtensionDirs();
}
- /**
+ /*
* Verifies that we can install an extension from a .zip file.
*/
@Test
@@ -78,11 +78,11 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
checkDirtyInstall(DEFAULT_EXT_NAME);
}
- /**
+ /*
* Verifies that we can install an extension from a folder.
*/
@Test
- public void testInstallExtensionFromFolder() throws ExtensionException, IOException {
+ public void testInstallExtensionFromFolder() throws IOException {
// Create an extension and install it.
ResourceFile rFile = createExtensionFolder();
@@ -92,7 +92,7 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
checkDirtyInstall(DEFAULT_EXT_NAME);
}
- /**
+ /*
* Verifies that we can uninstall an extension.
*/
@Test
@@ -104,7 +104,7 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
checkDirtyInstall(DEFAULT_EXT_NAME);
- // Get the extension object that we need to uninstall - there will only
+ // Get the extension object that we need to uninstall - there will only
// be one in the set.
Set<ExtensionDetails> extensions = ExtensionUtils.getExtensions();
assertTrue(extensions.size() == 1);
@@ -117,10 +117,10 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
checkCleanInstall();
}
- /**
+ /*
* Verifies that trying to install an extension when there's already one with the same
* name installed will overwrite the existing and not throw an exception
- *
+ *
* @throws Exception if there's a problem creating the temp extension folder
*/
@Test
@@ -131,15 +131,15 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
ExtensionUtils.install(rFile);
// Now create another extension with the same name and try
- // to install it.
+ // to install it.
rFile = new ResourceFile(createExtensionZip(DEFAULT_EXT_NAME));
boolean install = ExtensionUtils.install(rFile);
assertEquals(install, true);
}
- /**
- * Verifies that we can properly recognize a valid .zip file.
+ /*
+ * Verifies that we can properly recognize a valid .zip file.
*/
@Test
public void testIsZip() throws IOException, ExtensionException {
@@ -147,7 +147,7 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
assertTrue(ExtensionUtils.isZip(zipFile));
}
- /**
+ /*
* Verifies that we can identify when a .zip is a valid extension archive vs.
* just a regular old zip (ROZ).
* <p>
@@ -162,7 +162,7 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
assertTrue(!ExtensionUtils.isExtension(new ResourceFile(zipFile2)));
}
- /**
+ /*
* Verifies that we can recognize when a directory represents an extension.
* <p>
* Note: The presence of an extensions.properties file is the difference.
@@ -177,18 +177,18 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
assertTrue(!ExtensionUtils.isExtension(new ResourceFile(nonExtDir)));
}
- /**
+ /*
* Verifies that the we can retrieve all unique extensions in the archive and
* install folders.
* <p>
* Note: This test eliminates the need to test the methods for retrieving archived vs. installed
- * extensions individually.
+ * extensions individually.
*/
@Test
public void testGetExtensions() throws ExtensionException, IOException {
- // First create an extension and install it, so we have 2 extensions: one in
- // the archive folder, and one in the install folder.
+ // First create an extension and install it, so we have 2 extensions: one in
+ // the archive folder, and one in the install folder.
File zipFile = createExtensionZip(DEFAULT_EXT_NAME);
ExtensionUtils.install(new ResourceFile(zipFile));
@@ -209,8 +209,8 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
assertTrue(extensions.size() == 3);
}
- /**
- * Catch-all test for verifying that 'bad' inputs to utility functions are
+ /*
+ * Catch-all test for verifying that 'bad' inputs to utility functions are
* handled properly.
*/
@Test
@@ -233,14 +233,16 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
assertTrue(foundError == false);
}
- //==============================================================================
+//==================================================================================================
+// Private Methods
+//==================================================================================================
- /**
+ /*
* Creates the extension archive and installation directories.
- *
+ *
* @throws IOException if there's an error creating the directories
*/
- public final void createExtensionDirs() throws IOException {
+ private void createExtensionDirs() throws IOException {
ResourceFile extensionDir = gLayout.getExtensionArchiveDir();
if (!extensionDir.exists()) {
@@ -248,7 +250,7 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
throw new IOException("Failed to create extension archive directory for test");
}
}
-
+
ResourceFile installDir = gLayout.getExtensionInstallationDirs().get(0);
if (!installDir.exists()) {
if (!installDir.mkdir()) {
@@ -257,7 +259,7 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
}
}
- /**
+ /*
* Verifies that the installation folder is empty.
*/
private boolean checkCleanInstall() {
@@ -265,10 +267,10 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
return (files == null || files.length == 0);
}
- /**
+ /*
* Verifies that the installation folder is not empty and contains a folder
* with the given name.
- *
+ *
* @param name the name of the installed extension
*/
private void checkDirtyInstall(String name) {
@@ -277,10 +279,10 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
assertTrue(files[0].getName().equals(name));
}
- /**
+ /*
* Creates a valid extension in the archive folder. This extension is not a
* .zip, but a folder.
- *
+ *
* @return the file representing the extension
* @throws IOException if there's an error creating the extension
*/
@@ -296,9 +298,9 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
return root;
}
- /**
- * Create a generic zip that is a valid extension archive.
- *
+ /*
+ * Create a generic zip that is a valid extension archive.
+ *
* @param zipName name of the zip to create
* @return a zip file
* @throws IOException if there's an error creating the zip
@@ -320,10 +322,10 @@ public class ExtensionUtilsTest extends AbstractGenericTest {
return f;
}
- /**
+ /*
* Create a generic zip that is NOT a valid extension archive (because it doesn't
* have an extension.properties file).
- *
+ *
* @param zipName name of the zip to create
* @return a zip file
* @throws IOException if there's an error creating the zip
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/hover/FunctionSignatureDecompilerHover.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/hover/FunctionSignatureDecompilerHover.java
index 5c0872040b..84d94e3d13 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/hover/FunctionSignatureDecompilerHover.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/hover/FunctionSignatureDecompilerHover.java
@@ -24,6 +24,7 @@ import docking.widgets.fieldpanel.support.FieldLocation;
import ghidra.GhidraOptions;
import ghidra.app.decompiler.*;
import ghidra.app.decompiler.component.ClangTextField;
+import ghidra.app.decompiler.component.DecompilerUtils;
import ghidra.app.plugin.core.hover.AbstractConfigurableHover;
import ghidra.app.util.ToolTipUtils;
import ghidra.framework.plugintool.PluginTool;
@@ -34,7 +35,6 @@ import ghidra.program.model.listing.Program;
import ghidra.program.model.pcode.HighConstant;
import ghidra.program.model.pcode.Varnode;
import ghidra.program.model.scalar.Scalar;
-import ghidra.program.model.symbol.*;
import ghidra.program.util.ProgramLocation;
/**
@@ -83,76 +83,67 @@ public class FunctionSignatureDecompilerHover extends AbstractConfigurableHover
ClangToken token = ((ClangTextField) field).getToken(fieldLocation);
if (token instanceof ClangFuncNameToken) {
- // Obvious function reference
- String name = token.getText();
- Symbol symbol = getSymbolForLocation(program, name);
- if (symbol == null) {
- return null;
- }
-
- if (symbol.getSymbolType() == SymbolType.FUNCTION) {
- Function function = program.getFunctionManager().getFunctionAt(symbol.getAddress());
- String content = ToolTipUtils.getToolTipText(function, false);
- return createTooltipComponent(content);
- }
+ Function function = DecompilerUtils.getFunction(program, (ClangFuncNameToken) token);
+ String content = ToolTipUtils.getToolTipText(function, false);
+ return createTooltipComponent(content);
}
else if (token instanceof ClangVariableToken) {
// Reference to function-address: "x = &foo;" where 'foo' is a function
Varnode vn = ((ClangVariableToken) token).getVarnode();
- if (vn == null) {
- return null;
+ Scalar scalar = getScalar(vn);
+ Function function = getFunctionAtAddress(program, scalar);
+ if (function != null) {
+ String content = ToolTipUtils.getToolTipText(function, false);
+ content = content.replaceFirst(HTML,
+ HTML + italic(bold("Reference to Function")) + "<br/><br/>");
+ return createTooltipComponent(content);
}
+ }
- if (!(vn.getHigh() instanceof HighConstant)) {
- return null;
- }
+ return null;
+ }
- HighConstant hv = (HighConstant) vn.getHigh();
- long offset = vn.getOffset();
- int sz = vn.getSize();
- boolean isSigned = true;
- if (hv.getDataType() instanceof AbstractIntegerDataType) {
- isSigned = ((AbstractIntegerDataType) hv.getDataType()).isSigned();
- }
+ private Scalar getScalar(Varnode vn) {
+ if (vn == null) {
+ return null;
+ }
- if (sz > 8) {
- // our Scalar can currently only handle long values
- return null;
- }
+ if (!(vn.getHigh() instanceof HighConstant)) {
+ return null;
+ }
- Scalar scalar = new Scalar(sz * 8, offset, isSigned);
- long scalarLong = scalar.getValue();
- AddressFactory factory = program.getAddressFactory();
- AddressSpace space = factory.getDefaultAddressSpace();
- try {
- Address asAddress = factory.getAddress(space.getSpaceID(), scalarLong);
- Function function = program.getListing().getFunctionAt(asAddress);
- if (function != null) {
- String content = ToolTipUtils.getToolTipText(function, false);
- content = content.replaceFirst(HTML,
- HTML + italic(bold("Reference to Function")) + "<br/><br/>");
-
- return createTooltipComponent(content);
- }
- }
- catch (AddressOutOfBoundsException ex) {
- return null; // Constant does not make sense as an address
- }
+ HighConstant hv = (HighConstant) vn.getHigh();
+ long offset = vn.getOffset();
+ int sz = vn.getSize();
+ boolean isSigned = true;
+ if (hv.getDataType() instanceof AbstractIntegerDataType) {
+ isSigned = ((AbstractIntegerDataType) hv.getDataType()).isSigned();
}
- return null;
+ if (sz > 8) {
+ return null; // our Scalar can currently only handle long values
+ }
+
+ return new Scalar(sz * 8, offset, isSigned);
}
- private Symbol getSymbolForLocation(Program program, String symName) {
- SymbolTable symTable = program.getSymbolTable();
- SymbolIterator symbols = symTable.getSymbols(symName);
- for (Symbol symbol : symbols) {
- if (symbol.getSymbolType() == SymbolType.FUNCTION) {
- return symbol;
- }
+ private Function getFunctionAtAddress(Program program, Scalar scalar) {
+
+ if (scalar == null) {
+ return null;
+ }
+
+ long scalarLong = scalar.getValue();
+ AddressFactory factory = program.getAddressFactory();
+ AddressSpace space = factory.getDefaultAddressSpace();
+ try {
+ Address asAddress = factory.getAddress(space.getSpaceID(), scalarLong);
+ return program.getListing().getFunctionAt(asAddress);
+ }
+ catch (AddressOutOfBoundsException ex) {
+ return null; // scalar is not an address
}
- return null;
}
}
diff --git a/Ghidra/Framework/Docking/src/test/java/docking/widgets/tree/support/TreeIteratorTest.java b/Ghidra/Framework/Docking/src/test/java/docking/widgets/tree/support/TreeIteratorTest.java
index 80bd922f24..ceb28375ad 100644
--- a/Ghidra/Framework/Docking/src/test/java/docking/widgets/tree/support/TreeIteratorTest.java
+++ b/Ghidra/Framework/Docking/src/test/java/docking/widgets/tree/support/TreeIteratorTest.java
@@ -23,6 +23,7 @@ import org.junit.Before;
import org.junit.Test;
import docking.widgets.tree.*;
+import generic.test.AbstractGenericTest;
/**
* Class to test the {@link DepthFirstIterator} and {@link BreadthFirstIterator} classes for
@@ -46,12 +47,15 @@ public class TreeIteratorTest {
node0_0 = new GTestNode("Node0_0");
node0_1 = new GTestNode("Node0_1");
node1_0 = new GTestNode("Node1_0");
- root.addNode(node0);
- root.addNode(node1);
- root.addNode(node2);
- node0.addNode(node0_0);
- node0.addNode(node0_1);
- node1.addNode(node1_0);
+
+ AbstractGenericTest.runSwing(() -> {
+ root.addNode(node0);
+ root.addNode(node1);
+ root.addNode(node2);
+ node0.addNode(node0_0);
+ node0.addNode(node0_1);
+ node1.addNode(node1_0);
+ });
}
@Test