Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1915 - Data Type Manager - Added an action to search for enums by value

dragonmacher4353b04294d1
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/DataTypeManagerPlugin/data_type_manager_window.html b/Ghidra/Features/Base/src/main/help/help/topics/DataTypeManagerPlugin/data_type_manager_window.html
index 0fd896502c..9219b6bb1d 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/DataTypeManagerPlugin/data_type_manager_window.html
+++ b/Ghidra/Features/Base/src/main/help/help/topics/DataTypeManagerPlugin/data_type_manager_window.html
@@ -721,7 +721,18 @@
</P>
</BLOCKQUOTE>
- <H3><A name="Find_Structures_By_Size"></A>Find Structures by Size</H3>
+
+ <H3><A name="Find_Enums_By_Value"></A>Find Enums by Value</H3>
+
+ <BLOCKQUOTE>
+
+ <P>This is similar to the <B>Find Data Types by Size</B> action except
+ that it lets you specify one or more enum values (or value ranges) that will be used to
+ show only enums that contain any of the specified values.</P>
+ </BLOCKQUOTE>
+
+
+ <H3><A name="Find_Structures_By_Size"></A>Find Structures by Size</H3>
<BLOCKQUOTE>
@@ -729,7 +740,7 @@
that it filters results to show only Structures.</P>
</BLOCKQUOTE>
- <H3><A name="Find_Structures_By_Offset"></A>Find Structures by Offset</H3>
+ <H3><A name="Find_Structures_By_Offset"></A>Find Structures by Offset</H3>
<BLOCKQUOTE>
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeViewerProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeViewerProvider.java
index 618e11c03c..06b6ef1d4e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeViewerProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeViewerProvider.java
@@ -511,7 +511,7 @@ public class CodeViewerProvider extends NavigatableComponentProviderAdapter
new ToggleActionBuilder("Show Function Variables By Default", plugin.getName())
.popupMenuPath("Function", "Show Variables By Default")
.popupMenuGroup("Visibility", "5")
- .helpLocation(new HelpLocation("CodeBrowserPlugin", "Show_All_Variables"))
+ .helpLocation(new HelpLocation("CodeBrowserPlugin", "Show_Variables"))
.selected(true)
.withContext(ProgramLocationActionContext.class)
.onAction(c -> showVariablesForAllFunctions(toggleVariablesAction.isSelected()))
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 3f5bb0c064..9528dd8e67 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
@@ -189,12 +189,13 @@ public class DataTypesProvider extends ComponentProviderAdapter {
// VeryLast group
addLocalAction(new FindDataTypesByNameAction(plugin, "1"));
addLocalAction(new FindDataTypesBySizeAction(plugin, "2"));
- addLocalAction(new FindStructuresByOffsetAction(plugin, "3"));
- addLocalAction(new FindStructuresBySizeAction(plugin, "4"));
- includeDataMembersInSearchAction = new IncludeDataTypesInFilterAction(plugin, this, "5");
+ addLocalAction(new FindEnumsByValueAction(plugin, "3"));
+ addLocalAction(new FindStructuresByOffsetAction(plugin, "4"));
+ addLocalAction(new FindStructuresBySizeAction(plugin, "5"));
+ includeDataMembersInSearchAction = new IncludeDataTypesInFilterAction(plugin, this, "6");
addLocalAction(includeDataMembersInSearchAction);
- filterOnNameOnlyAction = new FilterOnNameOnlyAction(plugin, this, "6");
+ filterOnNameOnlyAction = new FilterOnNameOnlyAction(plugin, this, "7");
addLocalAction(filterOnNameOnlyAction);
addLocalAction(new ApplyFunctionDataTypesAction(plugin)); // Tree
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindEnumsByValueAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindEnumsByValueAction.java
new file mode 100644
index 0000000000..bda2173326
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindEnumsByValueAction.java
@@ -0,0 +1,141 @@
+/* ###
+ * 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.datamgr.actions;
+
+import java.util.Iterator;
+
+import docking.ActionContext;
+import docking.action.DockingAction;
+import docking.action.MenuData;
+import docking.widgets.dialogs.NumberRangeInputDialog;
+import docking.widgets.tree.GTreeNode;
+import docking.widgets.tree.support.GTreeFilter;
+import ghidra.app.plugin.core.datamgr.DataTypeManagerPlugin;
+import ghidra.app.plugin.core.datamgr.DataTypesProvider;
+import ghidra.app.plugin.core.datamgr.tree.DataTypeArchiveGTree;
+import ghidra.app.plugin.core.datamgr.tree.DataTypeNode;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.Enum;
+import ghidra.util.HelpLocation;
+import ghidra.util.datastruct.Range;
+import ghidra.util.datastruct.SortedRangeList;
+import util.CollectionUtils;
+
+/**
+ * Finds enum data types by matching user supplied enum values or ranges.
+ */
+public class FindEnumsByValueAction extends DockingAction {
+
+ public static final String NAME = "Find Enums by Value";
+
+ private DataTypeManagerPlugin plugin;
+
+ public FindEnumsByValueAction(DataTypeManagerPlugin plugin, String menuSubGroup) {
+ super(NAME, plugin.getName());
+ this.plugin = plugin;
+
+ setMenuBarData(
+ new MenuData(new String[] { NAME + "..." }, null, "VeryLast", -1, menuSubGroup));
+ setHelpLocation(new HelpLocation("DataTypeManagerPlugin", "Find_Enums_By_Value"));
+ }
+
+ @Override
+ public void actionPerformed(ActionContext context) {
+
+ NumberRangeInputDialog inputDialog = new NumberRangeInputDialog(NAME, "Values(s)");
+ inputDialog.setHelpLocation(getHelpLocation());
+ if (!inputDialog.show()) {
+ return;
+ }
+
+ SortedRangeList values = inputDialog.getValue();
+ DataTypesProvider newProvider = plugin.createProvider();
+ newProvider.setTitle(NAME);
+ DataTypeArchiveGTree tree = newProvider.getGTree();
+ tree.setFilterProvider(
+ new SecondaryTreeFilterProvider(tree, new OffsetGTreeFilter(values)));
+ newProvider.setVisible(true);
+ }
+
+ private class OffsetGTreeFilter implements GTreeFilter {
+
+ private final SortedRangeList offsets;
+
+ OffsetGTreeFilter(SortedRangeList offsets) {
+ this.offsets = offsets;
+ }
+
+ @Override
+ public boolean showFilterMatches() {
+ return true;
+ }
+
+ @Override
+ public boolean acceptsNode(GTreeNode node) {
+ if (!(node instanceof DataTypeNode)) {
+ return false;
+ }
+ DataTypeNode dataTypeNode = (DataTypeNode) node;
+ DataType dataType = dataTypeNode.getDataType();
+ if (!(dataType instanceof Enum)) {
+ return false;
+ }
+
+ Enum enuum = (Enum) dataType;
+ OffsetIterator it = new OffsetIterator(enuum);
+ for (Long value : CollectionUtils.asIterable(it)) {
+ for (Range range : offsets) {
+ int offset = value.intValue();
+ if (range.contains(offset)) {
+ return true;
+ }
+ if (offset < range.min) {
+ // ranges are ascending sorted order; the enum value is already
+ // smaller than this range, so no more ranges can match
+ break;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private class OffsetIterator implements Iterator<Long> {
+
+ private int index = 0;
+ private int length;
+ private long[] values;
+
+ OffsetIterator(Enum enuum) {
+ this.values = enuum.getValues();
+ this.length = values.length;
+ }
+
+ @Override
+ public boolean hasNext() {
+ if (index >= length) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public Long next() {
+ return values[index++];
+ }
+ }
+ }
+}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindStructuresByOffsetAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindStructuresByOffsetAction.java
index 18181e2efd..99d66ebe2e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindStructuresByOffsetAction.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/actions/FindStructuresByOffsetAction.java
@@ -4,9 +4,9 @@
* 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.
@@ -56,6 +56,7 @@ public class FindStructuresByOffsetAction extends DockingAction {
public void actionPerformed(ActionContext context) {
NumberRangeInputDialog inputDialog = new NumberRangeInputDialog(NAME, "Offset(s)");
+ inputDialog.setHelpLocation(getHelpLocation());
if (!inputDialog.show()) {
return;
}
@@ -100,9 +101,9 @@ public class FindStructuresByOffsetAction extends DockingAction {
if (range.contains(structureOffset)) {
return true;
}
- if (structureOffset > range.max) {
+ if (structureOffset < range.min) {
// ranges are ascending sorted order; the structure offset is already
- // bigger than this range, so no more ranges can match
+ // smaller than this range, so no more ranges can match
break;
}
}
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/DataTypeManagerPluginTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/DataTypeManagerPluginTest.java
index a9972ba0ee..0d2e289fe6 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/DataTypeManagerPluginTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/datamgr/DataTypeManagerPluginTest.java
@@ -61,6 +61,7 @@ import ghidra.program.database.ProgramBuilder;
import ghidra.program.database.ProgramDB;
import ghidra.program.database.data.ProgramDataTypeManager;
import ghidra.program.model.data.*;
+import ghidra.program.model.data.Enum;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
import ghidra.test.TestEnv;
import util.CollectionUtils;
@@ -770,6 +771,26 @@ public class DataTypeManagerPluginTest extends AbstractGhidraHeadedIntegrationTe
assertNotNull(message, action);
}
+ @Test
+ public void testAction_FindEnumByValue_Range() {
+
+ createEnumWithValues_2_4_8();
+ createEnumWithValues_100_200();
+ createEnumWithValues_1_300_1000();
+
+ DockingActionIf action = getAction(plugin, FindEnumsByValueAction.NAME);
+ performAction(action, false);
+
+ NumberRangeInputDialog dialog = waitForDialogComponent(NumberRangeInputDialog.class);
+ setText(dialog, "0x8:0x10, 300");
+
+ pressButtonByText(dialog, "OK");
+
+ DataTypesProvider resultsProvider =
+ waitForComponentProvider(DataTypesProvider.class, FindEnumsByValueAction.NAME);
+ assertMatchingEnums(resultsProvider, "Enum_2_4_8", "Enum_1_300_1000");
+ }
+
@Test
public void testAction_FindStructureByOffset() {
@@ -824,7 +845,7 @@ public class DataTypeManagerPluginTest extends AbstractGhidraHeadedIntegrationTe
createStructureWithOffset_0x4(); // 0x4
createStructureWithOffset_0x8(); // 0x4, 0x8
createStructureWithOffset_0x10(); // 0x4, 0x8, 0x10
- createStructureWithOffset_0x20(); // 0x8, 0x16, 0x20
+ createStructureWithOffset_0x20(); // 0x8, 0x10, 0x18, 0x32
DockingActionIf action = getAction(plugin, FindStructuresByOffsetAction.NAME);
performAction(action, false);
@@ -845,8 +866,8 @@ public class DataTypeManagerPluginTest extends AbstractGhidraHeadedIntegrationTe
createStructureWithOffset_0x4(); // 6
createStructureWithOffset_0x8(); // 10
- createStructureWithOffset_0x10(); // 12
- createStructureWithOffset_0x20(); // 22
+ createStructureWithOffset_0x10(); // 18
+ createStructureWithOffset_0x20(); // 40
DockingActionIf action = getAction(plugin, FindStructuresBySizeAction.NAME);
performAction(action, false);
@@ -885,14 +906,14 @@ public class DataTypeManagerPluginTest extends AbstractGhidraHeadedIntegrationTe
createStructureWithOffset_0x4(); // 6
createStructureWithOffset_0x8(); // 10
- createStructureWithOffset_0x10(); // 12
- createStructureWithOffset_0x20(); // 22
+ createStructureWithOffset_0x10(); // 18
+ createStructureWithOffset_0x20(); // 40
DockingActionIf action = getAction(plugin, FindStructuresBySizeAction.NAME);
performAction(action, false);
NumberRangeInputDialog dialog = waitForDialogComponent(NumberRangeInputDialog.class);
- setText(dialog, "12:22");
+ setText(dialog, "12:42");
pressButtonByText(dialog, "OK");
@@ -1183,6 +1204,32 @@ public class DataTypeManagerPluginTest extends AbstractGhidraHeadedIntegrationTe
waitForTree(tree);
}
+ private void createEnumWithValues_2_4_8() {
+
+ EnumDataType enuum = new EnumDataType("Enum_2_4_8", 3);
+ enuum.add("Two", 2);
+ enuum.add("Four", 4);
+ enuum.add("Eight", 8);
+ builder.addDataType(enuum);
+ }
+
+ private void createEnumWithValues_100_200() {
+
+ EnumDataType enuum = new EnumDataType("Enum_100_200", 2);
+ enuum.add("One Hundred", 100);
+ enuum.add("Two Hundred", 200);
+ builder.addDataType(enuum);
+ }
+
+ private void createEnumWithValues_1_300_1000() {
+
+ EnumDataType enuum = new EnumDataType("Enum_1_300_1000", 3);
+ enuum.add("One", 1);
+ enuum.add("Three Hundred", 300);
+ enuum.add("One Thousand", 1000);
+ builder.addDataType(enuum);
+ }
+
private void createStructureWithOffset_0x4() {
StructureDataType stuct = new StructureDataType("Structure_0x4", 0);
@@ -1203,10 +1250,9 @@ public class DataTypeManagerPluginTest extends AbstractGhidraHeadedIntegrationTe
private void createStructureWithOffset_0x10() {
StructureDataType stuct = new StructureDataType("Structure_0x10", 0);
+ stuct.add(new QWordDataType());
+ stuct.add(new QWordDataType());
stuct.add(new DWordDataType());
- stuct.add(new DWordDataType());
- stuct.add(new WordDataType());
- stuct.add(new WordDataType());
builder.addDataType(stuct);
}
@@ -1215,8 +1261,9 @@ public class DataTypeManagerPluginTest extends AbstractGhidraHeadedIntegrationTe
StructureDataType stuct = new StructureDataType("Structure_0x20", 0);
stuct.add(new QWordDataType());
stuct.add(new QWordDataType());
- stuct.add(new DWordDataType());
- stuct.add(new WordDataType());
+ stuct.add(new QWordDataType());
+ stuct.add(new QWordDataType());
+ stuct.add(new QWordDataType());
builder.addDataType(stuct);
}
@@ -1343,6 +1390,21 @@ public class DataTypeManagerPluginTest extends AbstractGhidraHeadedIntegrationTe
}
}
+ private void assertMatchingEnums(DataTypesProvider resultsProvider, String... names) {
+
+ DataTypeArchiveGTree gTree = resultsProvider.getGTree();
+ waitForTree(gTree);
+ Map<String, Enum> enums = getEnums(resultsProvider);
+ assertEquals("Incorrect number of matches.\n\tExpected: " + Arrays.toString(names) +
+ "\n\tFound: " + enums.keySet(), names.length, enums.size());
+ assertEquals(names.length, enums.size());
+ for (String name : names) {
+ if (!enums.containsKey(name)) {
+ fail("Enum not found in results: '" + name + "'.\nFound: " + enums.keySet());
+ }
+ }
+ }
+
private void assertMatchingStructures(DataTypesProvider resultsProvider, String... names) {
DataTypeArchiveGTree gTree = resultsProvider.getGTree();
@@ -1358,6 +1420,26 @@ public class DataTypeManagerPluginTest extends AbstractGhidraHeadedIntegrationTe
}
}
+ private Map<String, Enum> getEnums(DataTypesProvider resultsProvider) {
+
+ Map<String, Enum> map = new HashMap<>();
+ DataTypeArchiveGTree gTree = resultsProvider.getGTree();
+ GTreeNode rootNode = gTree.getViewRoot();
+ Iterator<GTreeNode> it = rootNode.iterator(true);
+ for (GTreeNode node : CollectionUtils.asIterable(it)) {
+ if (!(node instanceof DataTypeNode)) {
+ continue;
+ }
+ DataTypeNode dtNode = (DataTypeNode) node;
+ DataType dt = dtNode.getDataType();
+ if (dt instanceof Enum) {
+ map.put(dt.getName(), (Enum) dt);
+ }
+ }
+
+ return map;
+ }
+
private Map<String, Structure> getStructures(DataTypesProvider resultsProvider) {
Map<String, Structure> map = new HashMap<>();
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/Enum.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/Enum.java
index 0e571c3603..93044b293c 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/Enum.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/Enum.java
@@ -41,7 +41,7 @@ public interface Enum extends DataType {
/**
* Returns all names that map to the given value.
* @param value value for the enum entries.
- * @return all names; null if there is not name for the given value.
+ * @return all names; null if there is no name for the given value.
*/
public String[] getNames(long value);