Ghidra is a software reverse engineering (SRE) framework
BitFields - corrected internal structure pack/unpack and other minor issues
ghidra10a457bf26b48
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.javaindex 31b11254c0..db855f65c9 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java@@ -202,7 +202,10 @@ public class BitFieldPlacementComponent extends JPanel {repaint();}- private int getPreferredHeight() {+ /**+ * @return fixed height of component+ */+ public int getPreferredHeight() {return MY_HEIGHT + SCROLLBAR_THICKNESS;}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.javaindex 593662644e..c42010b15a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorModel.java@@ -1222,96 +1222,6 @@ public abstract class CompEditorModel extends CompositeEditorModel {return numIndicesRemoved;}- /**- * Unpackage the selected component in the structure or array. This means replace the structure- * with the data types for its component parts. For an array replace the array with the data type- * for each array element.- * If the component isn't a structure or union then returns false.- * @return true if unpackage succeeded.- *- * @throws UsrException if the component can't be unpackaged.- */- @Override- public void unpackage(int rowIndex) throws UsrException {- int componentOrdinal = convertRowToOrdinal(rowIndex);- DataTypeComponent currentComp = viewComposite.getComponent(componentOrdinal);- if (currentComp == null) {- throw new UsrException("Can only unpackage an array or structure.");- }- DataType currentDataType = currentComp.getDataType();- if (!((currentDataType instanceof Array) || (currentDataType instanceof Structure))) {- throw new UsrException("Can only unpackage an array or structure.");- }- if (isEditingField()) {- endFieldEditing();- }-- // Get the fieldname and comment before removing.- String fieldName = currentComp.getFieldName();- String comment = currentComp.getComment();-- int numComps = 0;- // This component is an array so unpackage it.- if (currentDataType instanceof Array) {- Array array = (Array) currentDataType;- int elementLen = array.getElementLength();- numComps = array.getNumElements();- // Remove the array.- delete(componentOrdinal);- if (numComps > 0) {- // Add the array's elements- try {- DataType dt = array.getDataType();- insertMultiple(rowIndex, dt, elementLen, numComps);- }- catch (InvalidDataTypeException ie) {- // Do nothing.- }- catch (OutOfMemoryError memExc) {- throw memExc; // rethrow the exception.- }- }- }- // This component is a structure so unpackage it.- else if (currentDataType instanceof Structure) {- Structure struct = (Structure) currentDataType;- numComps = struct.getNumComponents();- if (numComps > 0) {- // Remove the structure.- deleteComponent(rowIndex);- try {- // Add the structure's elements- for (int i = 0; i < numComps; i++) {- DataTypeComponent dtc = struct.getComponent(i);- insert(rowIndex + i, dtc.getDataType(), dtc.getLength(), dtc.getFieldName(),- dtc.getComment());- }- }- catch (OutOfMemoryError memExc) {- throw memExc; // re-throw the exception.- }- }- }- selection.clear();- selection.addRange(rowIndex, rowIndex + numComps);-- DataTypeComponent comp = getComponent(rowIndex);- // Set the field name and comment the same as before- try {- if (comp.getFieldName() == null) {- comp.setFieldName(fieldName);- }- }- catch (DuplicateNameException exc) {- Msg.showError(this, null, null, null);- }- comp.setComment(comment);-- fixSelection();- componentEdited();- selectionChanged();- }-/*** Returns the number of component rows in the editor. If unlocked, there* is a blank row at the end for inserting. Therefore this number can bediff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorPanel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorPanel.javaindex 6624714a55..be3a18566d 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorPanel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorPanel.java@@ -234,6 +234,10 @@ public class CompEditorPanel extends CompositeEditorPanel {bitViewScrollPane.getViewport().setBackground(getBackground());bitViewScrollPane.setBorder(null);+ // establish default preferred size of panel based upon fixed preferred height of bitViewComponent+ Dimension bitViewerDefaultSize = new Dimension(800, bitViewComponent.getPreferredHeight());+ bitViewScrollPane.setPreferredSize(bitViewerDefaultSize);+bitViewPanel.add(bitViewScrollPane);return bitViewPanel;}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.javaindex 535b44d2f5..a7be5ed583 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java@@ -186,6 +186,9 @@ public abstract class CompositeEditorPanel extends JPanel// Starting cell edit.SwingUtilities.invokeLater(() -> {int editingRow = table.getEditingRow();+ if (editingRow < 0) {+ return;+ }int modelColumn = table.convertColumnIndexToModel(table.getEditingColumn());if (!launchBitFieldEditor(modelColumn, editingRow)) {model.beginEditingField(editingRow, modelColumn);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CreateInternalStructureAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CreateInternalStructureAction.javaindex 253cbcc04e..5eba7855c8 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CreateInternalStructureAction.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CreateInternalStructureAction.java@@ -15,15 +15,14 @@*/package ghidra.app.plugin.core.compositeeditor;-import ghidra.util.SystemUtilities;-import ghidra.util.exception.UsrException;-import java.util.Arrays;import javax.swing.ImageIcon;-import resources.ResourceManager;import docking.ActionContext;+import ghidra.util.SystemUtilities;+import ghidra.util.exception.UsrException;+import resources.ResourceManager;/*** Action for use in the structure data type editor.@@ -39,7 +38,7 @@ public class CreateInternalStructureAction extends CompositeEditorTableAction {"Create a new structure from the selected components and replace them with it.";private static String[] popupPath = new String[] { ACTION_NAME };- public CreateInternalStructureAction(CompositeEditorProvider provider) {+ public CreateInternalStructureAction(StructureEditorProvider provider) {super(provider, EDIT_ACTION_PREFIX + ACTION_NAME, GROUP_NAME, popupPath, null,createInternalStructureIcon);setDescription(DESCRIPTION);@@ -66,15 +65,11 @@ public class CreateInternalStructureAction extends CompositeEditorTableAction {}}requestTableFocus();- SystemUtilities.runSwingLater(new Runnable() {-- @Override- public void run() {- provider.toFront();- provider.requestFocus();- provider.editorPanel.requestFocus();- provider.editorPanel.table.requestFocus();- }+ SystemUtilities.runSwingLater(() -> {+ provider.toFront();+ provider.requestFocus();+ provider.editorPanel.requestFocus();+ provider.editorPanel.table.requestFocus();});}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/EditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/EditorModel.javaindex 198d0d3726..ec2ba18e70 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/EditorModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/EditorModel.java@@ -420,14 +420,6 @@ public interface EditorModel {*/public DataTypeComponent replace(int rowIndex, DataType dt, int dtLength) throws UsrException;- /**- * Replace the component at the indicated index with its subcomponents.- * The component data type must be a composite data type to be unpackaged.- * @param rowIndex index of the row whose component is to be unpackaged in the editor's composite data type.- * @throws UsrException if the component can't be unpackaged.- */- public void unpackage(int rowIndex) throws UsrException;-/*** Gets the maximum number of bytes available for a data type that is added at the indicated* index. This can vary based on whether or not it is in a selection.diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorModel.javaindex 453110d7ad..8dca5a6b1f 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorModel.java@@ -1363,9 +1363,9 @@ class StructureEditorModel extends CompEditorModel {final StructureDataType structureDataType =new StructureDataType(originalCategoryPath, uniqueName, length, originalDTM);- if (isAligned()) {- structureDataType.setPackingValue(getPackingValue());- }+// if (isAligned()) {+// structureDataType.setPackingValue(getPackingValue());+// }// Get data type components to make into structure.DataTypeComponent firstDtc = null;@@ -1385,7 +1385,7 @@ class StructureEditorModel extends CompEditorModel {length += compLength;- if (!isAligned() && component.isBitFieldComponent()) {+ if (!structureDataType.isInternallyAligned() && component.isBitFieldComponent()) {BitFieldDataType bitfield = (BitFieldDataType) dt;structureDataType.insertBitFieldAt(component.getOffset() - firstDtc.getOffset(),compLength, bitfield.getBitOffset(), bitfield.getBaseDataType(),@@ -1473,4 +1473,115 @@ class StructureEditorModel extends CompEditorModel {originalDTM.endTransaction(transactionID, commit);}}++ /**+ * Unpackage the selected component in the structure or array. This means replace the structure+ * with the data types for its component parts. For an array replace the array with the data type+ * for each array element.+ * If the component isn't a structure or union then returns false.+ * @return true if unpackage succeeded.+ *+ * @throws UsrException if the component can't be unpackaged.+ */+ public void unpackage(int rowIndex) throws UsrException {+ int componentOrdinal = convertRowToOrdinal(rowIndex);+ DataTypeComponent currentComp = viewComposite.getComponent(componentOrdinal);+ if (currentComp == null) {+ throw new UsrException("Can only unpackage an array or structure.");+ }+ DataType currentDataType = currentComp.getDataType();+ if (!((currentDataType instanceof Array) || (currentDataType instanceof Structure))) {+ throw new UsrException("Can only unpackage an array or structure.");+ }+ if (isEditingField()) {+ endFieldEditing();+ }++ Structure viewStruct = (Structure) viewComposite;++ // Get the fieldname and comment before removing.+ String fieldName = currentComp.getFieldName();+ String comment = currentComp.getComment();+ int currentCompLen = currentComp.getLength();++ int numComps = 0;+ // This component is an array so unpackage it.+ if (currentDataType instanceof Array) {+ Array array = (Array) currentDataType;+ int elementLen = array.getElementLength();+ numComps = array.getNumElements();+ // Remove the array.+ delete(componentOrdinal);+ if (numComps > 0) {+ // Add the array's elements+ try {+ DataType dt = array.getDataType();+ insertMultiple(rowIndex, dt, elementLen, numComps);+ }+ catch (InvalidDataTypeException ie) {+ // Do nothing.+ }+ catch (OutOfMemoryError memExc) {+ throw memExc; // rethrow the exception.+ }+ }+ }+ // This component is a structure so unpackage it.+ else if (currentDataType instanceof Structure) {+ Structure struct = (Structure) currentDataType;+ numComps = struct.getNumComponents();+ if (numComps > 0) {+ // Remove the structure.+ int currentOffset = currentComp.getOffset();+ deleteComponent(rowIndex);+ try {+ // Add the structure's elements++ for (int i = 0; i < numComps; i++) {+ DataTypeComponent dtc = struct.getComponent(i);+ DataType dt = dtc.getDataType();+ int compLength = dtc.getLength();+ if (!isAligned()) {+ if (dtc.isBitFieldComponent()) {+ BitFieldDataType bitfield = (BitFieldDataType) dt;+ viewStruct.insertBitFieldAt(currentOffset + dtc.getOffset(),+ compLength, bitfield.getBitOffset(), bitfield.getBaseDataType(),+ bitfield.getDeclaredBitSize(), dtc.getFieldName(),+ dtc.getComment());+ }+ else {+ viewStruct.insertAtOffset(currentOffset + dtc.getOffset(), dt,+ compLength, dtc.getFieldName(), dtc.getComment());+ }+ }+ else {+ insert(rowIndex + i, dt, compLength, dtc.getFieldName(),+ dtc.getComment());+ }+ }+ }+ catch (OutOfMemoryError memExc) {+ throw memExc; // re-throw the exception.+ }+ }+ }+ selection.clear();+ selection.addRange(rowIndex, rowIndex + numComps);++ DataTypeComponent comp = getComponent(rowIndex);+ // Set the field name and comment the same as before+ try {+ if (comp.getFieldName() == null) {+ comp.setFieldName(fieldName);+ }+ }+ catch (DuplicateNameException exc) {+ Msg.showError(this, null, null, null);+ }+ comp.setComment(comment);++ fixSelection();+ componentEdited();+ selectionChanged();+ }}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnpackageAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnpackageAction.javaindex da30acb6a9..e6da64b862 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnpackageAction.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnpackageAction.java@@ -1,6 +1,5 @@/* ###* IP: GHIDRA- * REVIEWED: YES** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -16,17 +15,16 @@*/package ghidra.app.plugin.core.compositeeditor;-import ghidra.util.exception.UsrException;-import java.awt.event.KeyEvent;import javax.swing.ImageIcon;import javax.swing.KeyStroke;-import resources.ResourceManager;import docking.ActionContext;import docking.action.KeyBindingData;import docking.widgets.OptionDialog;+import ghidra.util.exception.UsrException;+import resources.ResourceManager;/*** Action for use in the composite data type editor.@@ -42,7 +40,7 @@ public class UnpackageAction extends CompositeEditorTableAction {private KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, 0);private static String[] popupPath = new String[] { ACTION_NAME };- public UnpackageAction(CompositeEditorProvider provider) {+ public UnpackageAction(StructureEditorProvider provider) {super(provider, EDIT_ACTION_PREFIX + ACTION_NAME, GROUP_NAME, popupPath, null,unpackageIcon);setDescription(DESCRIPTION);@@ -66,7 +64,7 @@ public class UnpackageAction extends CompositeEditorTableAction {}}try {- model.unpackage(currentRowIndex);+ ((StructureEditorModel) model).unpackage(currentRowIndex);}catch (UsrException e1) {model.setStatus(e1.getMessage(), true);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorModel.javaindex c023d2e62c..82cc2f6a48 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorModel.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorModel.java@@ -1223,14 +1223,6 @@ class StackEditorModel extends CompositeEditorModel {return max / dtc.getLength();}- /* (non-Javadoc)- * @see ghidra.app.plugin.compositeeditor.EditorModel#unpackage(int)- */- @Override- public void unpackage(int currentIndex) throws UsrException {- throw new UsrException("Unpackaging isn't allowed.");- }-@Overridepublic void dataTypeChanged(DataTypeManager dataTypeManager, DataTypePath path) {if (isLoaded()) {diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/AbstractEditorTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/AbstractEditorTest.javaindex 05c6a4a14a..8be3d39ca7 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/AbstractEditorTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/AbstractEditorTest.java@@ -745,6 +745,8 @@ public abstract class AbstractEditorTest extends AbstractGhidraHeadedIntegration}protected void doubleClickTableCell(int row, int column) throws Exception {+ clickTableCell(getTable(), row, column, 1);+ waitForSwing();clickTableCell(getTable(), row, column, 2);// Double click a second time if not editing,// since editing sometimes gets canceled by a selection event in the TestEnvdiff --git a/Ghidra/Framework/Graph/src/test/java/ghidra/graph/GraphAlgorithmsTest.java b/Ghidra/Framework/Graph/src/test/java/ghidra/graph/GraphAlgorithmsTest.javaindex 97f38ac4fb..d32a0c99ff 100644--- a/Ghidra/Framework/Graph/src/test/java/ghidra/graph/GraphAlgorithmsTest.java+++ b/Ghidra/Framework/Graph/src/test/java/ghidra/graph/GraphAlgorithmsTest.java@@ -1273,7 +1273,7 @@ public class GraphAlgorithmsTest extends AbstractGraphAlgorithmsTest {edge(v5, v6);edge(v7, v3);- List<List<TestV>> circuits = GraphAlgorithms.findCircuits(g, false, TaskMonitor.DUMMY);+ List<List<TestV>> circuits = GraphAlgorithms.findCircuits(g, TaskMonitor.DUMMY);assertEquals(3, circuits.size());}