Skip to content

Ghidra is a software reverse engineering (SRE) framework

BitFields - updated tests

ghidra154ac180e1adf
diff --git a/Ghidra/Features/Base/src/test/java/ghidra/program/database/data/UnionDataTypeTest.java b/Ghidra/Features/Base/src/test/java/ghidra/program/database/data/UnionDataTypeTest.java
index 6f3ab218a8..33f630ce0b 100644
--- a/Ghidra/Features/Base/src/test/java/ghidra/program/database/data/UnionDataTypeTest.java
+++ b/Ghidra/Features/Base/src/test/java/ghidra/program/database/data/UnionDataTypeTest.java
@@ -140,24 +140,47 @@ public class UnionDataTypeTest extends AbstractGTest {
}
@Test
- public void testBitFieldUnionLength() throws Exception {
+ public void testBitFieldUnion() throws Exception {
int cnt = union.getNumComponents();
for (int i = 0; i < cnt; i++) {
union.delete(0);
}
+ // NOTE: bitOffset ignored for union
union.insertBitField(0, 4, 12, IntegerDataType.dataType, 2, "bf1", "bf1Comment");
union.insert(0, ShortDataType.dataType);
- //@formatter:off
- CompositeTestUtils.assertExpectedComposite(this, "/TestUnion\n" +
- "Unaligned\n" +
- "Union TestUnion {\n" +
- " 0 short 2 null \"\"\n" +
- " 1 byte:2(4) 1 bf1 \"bf1Comment\"\n" +
- "}\n" +
- "Size = 4 Actual Alignment = 1", union);
- //@formatter:on
+ //@formatter:off
+ CompositeTestUtils.assertExpectedComposite(this, "/TestUnion\n" +
+ "Unaligned\n" +
+ "Union TestUnion {\n" +
+ " 0 short 2 null \"\"\n" +
+ " 0 int:2(0) 1 bf1 \"bf1Comment\"\n" +
+ "}\n" +
+ "Size = 2 Actual Alignment = 1", union);
+ //@formatter:on
+ }
+
+ @Test
+ public void testAlignedBitFieldUnion() throws Exception {
+
+ int cnt = union.getNumComponents();
+ for (int i = 0; i < cnt; i++) {
+ union.delete(0);
+ }
+ union.insertBitField(0, 4, 12, IntegerDataType.dataType, 2, "bf1", "bf1Comment");
+ union.insert(0, ShortDataType.dataType);
+ union.setInternallyAligned(true);
+
+ //@formatter:off
+ CompositeTestUtils.assertExpectedComposite(this, "/TestUnion\n" +
+ "Aligned\n" +
+ "Union TestUnion {\n" +
+ " 0 short 2 null \"\"\n" +
+ " 0 int:2(0) 1 bf1 \"bf1Comment\"\n" +
+ "}\n" +
+ "Size = 4 Actual Alignment = 4", union);
+ //@formatter:on
}
@Test
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/Composite.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/Composite.java
index 6f396f9602..0e9d762e64 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/Composite.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/Composite.java
@@ -203,8 +203,8 @@ public interface Composite extends DataType {
/**
* Inserts a new bitfield at the specified ordinal position in this composite.
- * Within an aligned composites the specified byteWidth and bitOffset will be ignored
- * while bitOffset is always ignored for unions.
+ * Within aligned composites and unions the specified byteWidth and bitOffset will be
+ * ignored.
* The component length will be computed based upon the specified parameters and will
* be reduced from byteWidth to its minimal size for the new component.
* <p>
diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/database/data/StructureDBTest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/database/data/StructureDBTest.java
index 171fddbde7..07adcb2012 100644
--- a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/database/data/StructureDBTest.java
+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/database/data/StructureDBTest.java
@@ -39,6 +39,7 @@ public class StructureDBTest extends AbstractGTest {
dataMgr = new StandAloneDataTypeManager("dummyDTM");
// default data organization is little-endian
+ // default BitFieldPackingImpl uses gcc conventions with type alignment enabled
dataMgr.startTransaction("Test");
diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/database/data/UnionDBTest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/database/data/UnionDBTest.java
index 800a76154d..c1d3eb050a 100644
--- a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/database/data/UnionDBTest.java
+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/database/data/UnionDBTest.java
@@ -40,6 +40,7 @@ public class UnionDBTest extends AbstractGTest {
dataMgr = new StandAloneDataTypeManager("dummydataMgr");
// default data organization is little-endian
+ // default BitFieldPackingImpl uses gcc conventions
dataMgr.startTransaction("Test");
@@ -156,12 +157,13 @@ public class UnionDBTest extends AbstractGTest {
}
@Test
- public void testBitFieldUnionLength() throws Exception {
+ public void testBitFieldUnion() throws Exception {
int cnt = union.getNumComponents();
for (int i = 0; i < cnt; i++) {
union.delete(0);
}
+ // NOTE: bitOffset ignored for union
union.insertBitField(0, 4, 12, IntegerDataType.dataType, 2, "bf1", "bf1Comment");
union.insert(0, ShortDataType.dataType);
@@ -170,9 +172,31 @@ public class UnionDBTest extends AbstractGTest {
"Unaligned\n" +
"Union TestUnion {\n" +
" 0 short 2 null \"\"\n" +
- " 1 byte:2(4) 1 bf1 \"bf1Comment\"\n" +
+ " 0 int:2(0) 1 bf1 \"bf1Comment\"\n" +
"}\n" +
- "Size = 4 Actual Alignment = 1", union);
+ "Size = 2 Actual Alignment = 1", union);
+ //@formatter:on
+ }
+
+ @Test
+ public void testAlignedBitFieldUnion() throws Exception {
+
+ int cnt = union.getNumComponents();
+ for (int i = 0; i < cnt; i++) {
+ union.delete(0);
+ }
+ union.insertBitField(0, 4, 12, IntegerDataType.dataType, 2, "bf1", "bf1Comment");
+ union.insert(0, ShortDataType.dataType);
+ union.setInternallyAligned(true);
+
+ //@formatter:off
+ CompositeTestUtils.assertExpectedComposite(this, "/TestUnion\n" +
+ "Aligned\n" +
+ "Union TestUnion {\n" +
+ " 0 short 2 null \"\"\n" +
+ " 0 int:2(0) 1 bf1 \"bf1Comment\"\n" +
+ "}\n" +
+ "Size = 4 Actual Alignment = 4", union);
//@formatter:on
}