Skip to content

Ghidra is a software reverse engineering (SRE) framework

BitFields - corrected zero-length bitfield editor rendering. Corrected test related issues.

ghidra1678b81ccbdca
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.java
index 21d1afc6e2..f2497d9c4c 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
@@ -198,16 +198,14 @@ public class BitFieldPlacementComponent extends JPanel {
return;
}
- // TODO: consider showing a animated hashed-box around original bit boundary
- // of the component being modified
-
editMode = EditMode.EDIT;
editOrdinal = editDtc.getOrdinal();
this.editComponent = editDtc;
BitFieldPlacement placement = new BitFieldPlacement(editDtc);
- bitFieldAllocation = new BitFieldAllocation(placement.rightBit - placement.leftBit + 1,
- (8 * allocationByteSize) - placement.rightBit - 1);
+ int bitSize = placement.zeroBitField ? 0 : (placement.rightBit - placement.leftBit + 1);
+ bitFieldAllocation =
+ new BitFieldAllocation(bitSize, (8 * allocationByteSize) - placement.rightBit - 1);
updatePreferredSize();
repaint();
}
@@ -837,6 +835,9 @@ public class BitFieldPlacementComponent extends JPanel {
public DataTypeComponent getConflict() {
BitAttributes c = conflict;
while (c != null && c.dtc.isZeroBitFieldComponent()) {
+ // TODO: improve conflict detection
+ // Zero-length bitfield could be conflict if placement is
+ // offcut with another component (currently ignored)
c = conflict.conflict;
}
return c != null ? c.dtc : null;
@@ -844,19 +845,28 @@ public class BitFieldPlacementComponent extends JPanel {
void layout(int x, int y, int width, int height) {
rectangle = new Rectangle(x, y, width, height);
+ if (conflict != null) {
+ conflict.layout(x, y, width, height);
+ }
}
void paint(Graphics g, BitAttributes bitAttrsToLeft, boolean paintRightLine) {
// bit box
Color c = getColor();
g.setColor(c);
- g.fillRect(rectangle.x, rectangle.y, BIT_WIDTH, CELL_HEIGHT);
- if (zeroBitfield ||
- (dtc != null && conflict != null && conflict.dtc.isZeroBitFieldComponent())) {
+ if (zeroBitfield) {
+
+ if (conflict != null) {
+ conflict.paint(g, bitAttrsToLeft, paintRightLine);
+ }
+ if (!bigEndian) {
+ bitAttrsToLeft = null;
+ }
+
c = ACTIVE_BITFIELD_BITS_COLOR;
Color lineColor = INTERIOR_LINE_COLOR;
- if (dtc != null) {
+ if (dtc != null && dtc != editComponent) {
c = BITFIELD_COMPONENT_COLOR;
lineColor = LINE_COLOR;
}
@@ -870,6 +880,12 @@ public class BitFieldPlacementComponent extends JPanel {
g.setColor(lineColor);
g.fillRect(xLine, rectangle.y, BIT_SEPARATOR_THICKNESS, CELL_HEIGHT);
}
+ else {
+ g.fillRect(rectangle.x, rectangle.y, BIT_WIDTH, CELL_HEIGHT);
+ if (conflict != null && conflict.dtc.isZeroBitFieldComponent()) {
+ conflict.paint(g, null, false);
+ }
+ }
if (bitAttrsToLeft != null && dtc != null && bitAttrsToLeft.unallocated) {
// draw left bit line if we know better than the undefined to our left
@@ -891,23 +907,17 @@ public class BitFieldPlacementComponent extends JPanel {
}
Color getColor() {
- // zero-length stripe will be added later and
- // should treated as a conflict
if (unallocated) {
return UNDEFINED_BIT_COLOR;
}
- if (conflict != null && !conflict.unallocated) {
- if (zeroBitfield) {
- return conflict.getColor();
- }
- if (!conflict.dtc.isZeroBitFieldComponent()) {
- return CONFLICT_BITS_COLOR;
- }
+ if (conflict != null && !conflict.unallocated && !conflict.zeroBitfield &&
+ conflict.dtc.getDataType() != DataType.DEFAULT) {
+ return CONFLICT_BITS_COLOR;
}
if (dtc == editComponent) {
return ACTIVE_BITFIELD_BITS_COLOR; // edit field
}
- if (zeroBitfield || dtc.getDataType() == DataType.DEFAULT) {
+ if (dtc.getDataType() == DataType.DEFAULT) {
return UNDEFINED_BIT_COLOR;
}
return dtc.isBitFieldComponent() ? BITFIELD_COMPONENT_COLOR
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.java
index 49d36fadc1..453110d7ad 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
@@ -628,10 +628,7 @@ class StructureEditorModel extends CompEditorModel {
FieldRange range = selection.getFieldRange(0);
DataTypeComponent comp = getComponent(range.getStart().getIndex().intValue());
- if (comp == null || comp.isFlexibleArrayComponent()) {
- return true;
- }
- if (comp.isBitFieldComponent()) {
+ if (comp == null || comp.isFlexibleArrayComponent() || comp.isBitFieldComponent()) {
return false;
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorModel.java
index c4cc0fb787..14ce95fb45 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorModel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorModel.java
@@ -229,7 +229,7 @@ class UnionEditorModel extends CompEditorModel {
}
FieldRange range = selection.getFieldRange(0);
DataTypeComponent comp = getComponent(range.getStart().getIndex().intValue());
- return (comp == null || !comp.isBitFieldComponent());
+ return (comp != null && !comp.isBitFieldComponent());
}
/**
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorAlignmentTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorAlignmentTest.java
index 047ae6923d..301c982f7b 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorAlignmentTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorAlignmentTest.java
@@ -32,7 +32,7 @@ import ghidra.program.model.symbol.SourceType;
public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
@Test
- public void testUnalignedStructure() {
+ public void testUnalignedStructure() {
init(emptyStructure, pgmRootCat, false);
assertTrue(structureModel.hasChanges());// initial unsaved empty structure
@@ -84,7 +84,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
}
@Test
- public void testDefaultAlignedStructure() throws Exception {
+ public void testDefaultAlignedStructure() throws Exception {
init(emptyStructure, pgmRootCat, false);
DataType arrayDt = new ArrayDataType(new CharDataType(), 5, 1);
@@ -112,7 +112,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
}
@Test
- public void testEnablementDefaultAlignedStructure() throws Exception {
+ public void testEnablementDefaultAlignedStructure() throws Exception {
emptyStructure.setInternallyAligned(true);
init(emptyStructure, pgmRootCat, false);
@@ -127,6 +127,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
if ((pActions[i] instanceof FavoritesAction) ||
(pActions[i] instanceof CycleGroupAction) ||
(pActions[i] instanceof EditFieldAction) ||
+ (pActions[i] instanceof InsertUndefinedAction) ||
(pActions[i] instanceof PointerAction) ||
(pActions[i] instanceof HexNumbersAction) || (actions[i] instanceof ApplyAction)) {
checkEnablement(pActions[i], true);
@@ -146,7 +147,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
}
@Test
- public void testMachineAlignedStructure() throws Exception {
+ public void testMachineAlignedStructure() throws Exception {
init(emptyStructure, pgmRootCat, false);
DataType arrayDt = new ArrayDataType(new CharDataType(), 5, 1);
@@ -167,7 +168,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
}
@Test
- public void testByValueAlignedStructure() throws Exception {
+ public void testByValueAlignedStructure() throws Exception {
init(emptyStructure, pgmRootCat, false);
CompEditorPanel editorPanel = (CompEditorPanel) getPanel();
@@ -201,27 +202,27 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
}
@Test
- public void testByValue1AlignedStructure() throws Exception {
+ public void testByValue1AlignedStructure() throws Exception {
checkByValueAlignedStructure(1, 4, 16);
}
@Test
- public void testByValue2AlignedStructure() throws Exception {
+ public void testByValue2AlignedStructure() throws Exception {
checkByValueAlignedStructure(2, 4, 16);
}
@Test
- public void testByValue4AlignedStructure() throws Exception {
+ public void testByValue4AlignedStructure() throws Exception {
checkByValueAlignedStructure(4, 4, 16);
}
@Test
- public void testByValue8AlignedStructure() throws Exception {
+ public void testByValue8AlignedStructure() throws Exception {
checkByValueAlignedStructure(8, 8, 16);
}
@Test
- public void testByValue16AlignedStructure() throws Exception {
+ public void testByValue16AlignedStructure() throws Exception {
checkByValueAlignedStructure(16, 16, 16);
}
@@ -262,7 +263,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
}
@Test
- public void testDefaultAlignedPacked1Structure() throws Exception {
+ public void testDefaultAlignedPacked1Structure() throws Exception {
int value = 1;
emptyStructure.setInternallyAligned(true);
emptyStructure.setPackingValue(value);
@@ -298,7 +299,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
}
@Test
- public void testAlignedEditToFunctionDefinitionDataType() throws Exception {
+ public void testAlignedEditToFunctionDefinitionDataType() throws Exception {
int value = 1;
startTransaction("addExternal");
@@ -351,7 +352,7 @@ public class StructureEditorAlignmentTest extends AbstractStructureEditorTest {
}
@Test
- public void testSelectionOnGoFromUnalignedToAlignedStructure() throws Exception {
+ public void testSelectionOnGoFromUnalignedToAlignedStructure() throws Exception {
init(emptyStructure, pgmRootCat, false);
CompEditorPanel editorPanel = (CompEditorPanel) getPanel();
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorLockedEnablementTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorLockedEnablementTest.java
index 4cd8361243..e3ffdb8c5c 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorLockedEnablementTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/compositeeditor/StructureEditorLockedEnablementTest.java
@@ -102,9 +102,9 @@ public class StructureEditorLockedEnablementTest extends AbstractStructureEditor
assertEquals(getModel().getTypeName(), "Structure");
for (CompositeEditorTableAction action : actions) {
- if ((action instanceof EditFieldAction) || (action instanceof InsertUndefinedAction) ||
- (action instanceof PointerAction) || (action instanceof HexNumbersAction) ||
- (action instanceof ApplyAction)) {
+ if ((action instanceof EditFieldAction) || (action instanceof AddBitFieldAction) ||
+ (action instanceof InsertUndefinedAction) || (action instanceof PointerAction) ||
+ (action instanceof HexNumbersAction) || (action instanceof ApplyAction)) {
checkEnablement(action, true);
}
else if (action instanceof FavoritesAction) {
@@ -152,8 +152,9 @@ public class StructureEditorLockedEnablementTest extends AbstractStructureEditor
getModel().getOriginalCategoryPath().getPath());
for (CompositeEditorTableAction action : actions) {
- if ((action instanceof EditFieldAction) || (action instanceof InsertUndefinedAction) ||
- (action instanceof PointerAction) || (action instanceof HexNumbersAction)) {
+ if ((action instanceof EditFieldAction) || (action instanceof AddBitFieldAction) ||
+ (action instanceof InsertUndefinedAction) || (action instanceof PointerAction) ||
+ (action instanceof HexNumbersAction)) {
checkEnablement(action, true);
}
else if (action instanceof FavoritesAction) {
@@ -196,6 +197,7 @@ public class StructureEditorLockedEnablementTest extends AbstractStructureEditor
}
else if ((action instanceof CycleGroupAction) ||
(action instanceof InsertUndefinedAction) || (action instanceof EditFieldAction) ||
+ (action instanceof AddBitFieldAction) ||
(action instanceof ShowComponentPathAction) || (action instanceof MoveDownAction) ||
(action instanceof DuplicateAction) ||
(action instanceof DuplicateMultipleAction) || (action instanceof ClearAction) ||
@@ -225,6 +227,7 @@ public class StructureEditorLockedEnablementTest extends AbstractStructureEditor
}
else if ((action instanceof CycleGroupAction) ||
(action instanceof InsertUndefinedAction) || (action instanceof EditFieldAction) ||
+ (action instanceof AddBitFieldAction) ||
(action instanceof ShowComponentPathAction) || (action instanceof MoveUpAction) ||
(action instanceof MoveDownAction) || (action instanceof ClearAction) ||
(action instanceof DeleteAction) || (action instanceof ArrayAction) ||
@@ -252,6 +255,7 @@ public class StructureEditorLockedEnablementTest extends AbstractStructureEditor
}
else if ((action instanceof CycleGroupAction) ||
(action instanceof InsertUndefinedAction) || (action instanceof EditFieldAction) ||
+ (action instanceof AddBitFieldAction) ||
(action instanceof ShowComponentPathAction) || (action instanceof MoveUpAction) ||
(action instanceof ClearAction) || (action instanceof DeleteAction) ||
(action instanceof DuplicateAction) ||
diff --git a/Ghidra/Features/Base/src/test/java/ghidra/program/model/data/StructureImplLittleEndianBitFieldTest.java b/Ghidra/Features/Base/src/test/java/ghidra/program/model/data/StructureImplLittleEndianBitFieldTest.java
index 9c7e21eadb..b40aff9378 100644
--- a/Ghidra/Features/Base/src/test/java/ghidra/program/model/data/StructureImplLittleEndianBitFieldTest.java
+++ b/Ghidra/Features/Base/src/test/java/ghidra/program/model/data/StructureImplLittleEndianBitFieldTest.java
@@ -15,15 +15,8 @@
*/
package ghidra.program.model.data;
-import java.io.File;
-import java.io.IOException;
-import java.net.URL;
-import java.util.Iterator;
-
import org.junit.Test;
-import resources.ResourceManager;
-
public class StructureImplLittleEndianBitFieldTest extends AbstractCompositeImplBitFieldTest {
// NOTE: verified bitfields sample built with Apple LLVM version 9.0.0 (clang-900.0.39.2)
@@ -38,28 +31,28 @@ public class StructureImplLittleEndianBitFieldTest extends AbstractCompositeImpl
//writeArchive();
}
- private void writeArchive() throws IOException {
- URL resource = ResourceManager.getResource(C_SOURCE_FILE);
- File f = new File(resource.getPath() + ".gdt");
- if (f.exists()) {
- f.delete();
- }
-
- FileDataTypeManager fileDtMgr = FileDataTypeManager.createFileArchive(f);
- int txId = fileDtMgr.startTransaction("Save Datatypes");
- try {
- Iterator<Composite> composites = getDataTypeManager().getAllComposites();
- while (composites.hasNext()) {
- fileDtMgr.addDataType(composites.next(), null);
- }
- }
- finally {
- fileDtMgr.endTransaction(txId, true);
- }
-
- fileDtMgr.save();
- fileDtMgr.close();
- }
+// private void writeArchive() throws IOException {
+// URL resource = ResourceManager.getResource(C_SOURCE_FILE);
+// File f = new File(resource.getPath() + ".gdt");
+// if (f.exists()) {
+// f.delete();
+// }
+//
+// FileDataTypeManager fileDtMgr = FileDataTypeManager.createFileArchive(f);
+// int txId = fileDtMgr.startTransaction("Save Datatypes");
+// try {
+// Iterator<Composite> composites = getDataTypeManager().getAllComposites();
+// while (composites.hasNext()) {
+// fileDtMgr.addDataType(composites.next(), null);
+// }
+// }
+// finally {
+// fileDtMgr.endTransaction(txId, true);
+// }
+//
+// fileDtMgr.save();
+// fileDtMgr.close();
+// }
@Override
protected DataTypeManager getDataTypeManager() {
diff --git a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/bitfields.h b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/bitfields.h
index 2b2db866f2..4353d43963 100644
--- a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/bitfields.h
+++ b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/bitfields.h
@@ -33,7 +33,7 @@ struct A1 {
};
struct A2 {
- oddStruct a; // test for check alignment overlap (gcc)
+ struct oddStruct a; // test for check alignment overlap (gcc)
int b:3;
int c:8;
int d:8;