Merge remote-tracking branch 'origin/GP-5136_ghidra1_SarifStructureComponents'

This commit is contained in:
Ryan Kurtz 2024-11-20 06:13:59 -05:00
commit 55b2016b16
3 changed files with 101 additions and 105 deletions

View File

@ -30,14 +30,10 @@ public class IsfComposite extends AbstractIsfObject {
public IsfComposite(Composite composite, IsfDataTypeWriter writer, TaskMonitor monitor) {
super(composite);
size = composite.getLength();
size = composite.isZeroLength() ? 0 : composite.getLength();
kind = composite instanceof Structure ? "struct" : "union";
DataTypeComponent[] components = composite.getComponents();
if (components.length == 0) {
// NB: composite.getLength always returns > 0
size = 0;
}
DataTypeComponent[] components = composite.getDefinedComponents();
fields = new JsonObject();
for (DataTypeComponent component : components) {
if (monitor.isCancelled()) {

View File

@ -15,29 +15,12 @@
*/
package sarif;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import org.junit.Test;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.Array;
import ghidra.program.model.data.ArrayDataType;
import ghidra.program.model.data.ByteDataType;
import ghidra.program.model.data.CategoryPath;
import ghidra.program.model.data.FunctionDefinition;
import ghidra.program.model.data.FunctionDefinitionDataType;
import ghidra.program.model.data.Pointer;
import ghidra.program.model.data.Pointer32DataType;
import ghidra.program.model.data.PointerDataType;
import ghidra.program.model.data.ProgramBasedDataTypeManager;
import ghidra.program.model.data.Structure;
import ghidra.program.model.data.StructureDataType;
import ghidra.program.model.data.TypeDef;
import ghidra.program.model.data.TypedefDataType;
import ghidra.program.model.data.Union;
import ghidra.program.model.data.UnionDataType;
import ghidra.program.model.data.*;
import ghidra.program.util.ProgramDiff;
public class DataTypesSarifTest extends AbstractSarifTest {
@ -67,7 +50,7 @@ public class DataTypesSarifTest extends AbstractSarifTest {
ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty());
assert (differences.isEmpty());
}
@Test
@ -80,7 +63,7 @@ public class DataTypesSarifTest extends AbstractSarifTest {
ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty());
assert (differences.isEmpty());
}
@Test
@ -95,7 +78,7 @@ public class DataTypesSarifTest extends AbstractSarifTest {
ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty());
assert (differences.isEmpty());
}
@Test
@ -112,7 +95,7 @@ public class DataTypesSarifTest extends AbstractSarifTest {
ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty());
assert (differences.isEmpty());
}
@Test
@ -134,20 +117,20 @@ public class DataTypesSarifTest extends AbstractSarifTest {
ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty());
assert (differences.isEmpty());
}
@Test
public void testCreateStructure() throws Exception {
ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
StructureDataType sdt = new StructureDataType("test", 0);
Structure sdt = createComplexStructureDataType(dataMgr);
Structure struct = (Structure) dataMgr.addDataType(sdt, null);
assertNotNull(struct);
ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty());
assert (differences.isEmpty());
}
@Test
@ -160,7 +143,7 @@ public class DataTypesSarifTest extends AbstractSarifTest {
ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty());
assert (differences.isEmpty());
}
@Test
@ -174,7 +157,73 @@ public class DataTypesSarifTest extends AbstractSarifTest {
ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty());
assert (differences.isEmpty());
}
static Structure createComplexStructureDataType(DataTypeManager dtm) throws Exception {
/**
* /structA
* aligned(8) pack(disabled)
* Structure structA {
* 0 char 1 a0 ""
* 4 int 4 a1 ""
* 16 int[4] 16 a3 ""
* 32 char[0] 0 az ""
* 40 short 2 a4 ""
* 48 int[0] 0 aflex ""
* }
* Length: 48 Alignment: 8
*/
Structure structA = new StructureDataType("structA", 0);
structA.insertAtOffset(0, CharDataType.dataType, -1, "a0", null);
structA.insertAtOffset(4, IntegerDataType.dataType, -1, "a1", null);
structA.insertAtOffset(0x10, new ArrayDataType(CharDataType.dataType, 0, -1), -1, "az",
null);
structA.insertAtOffset(0x10, new ArrayDataType(IntegerDataType.dataType, 4, -1), -1, "a3",
null);
structA.insertAtOffset(0x28, ShortDataType.dataType, -1, "a4", null);
structA.insertAtOffset(0x30, new ArrayDataType(IntegerDataType.dataType, 0, -1), -1,
"aflex", null);
structA.setExplicitMinimumAlignment(8);
/**
* /structB
* pack()
* Structure structB {
* 0 short 2 b0 ""
* 2 byte[0] 0 bfs ""
* 2 int:4(0) 1 bf1 ""
* 2 int:6(4) 2 bf2 ""
* 3 int:2(2) 1 bf3 ""
* 8 structA 48 b1 ""
* }
* Length: 56 Alignment: 8
*/
Structure structB = new StructureDataType("structB", 0);
structB.setPackingEnabled(true);
structB.add(ShortDataType.dataType, "b0", null);
structB.add(new ArrayDataType(ByteDataType.dataType, 0, -1), "bfs", null);
structB.addBitField(IntegerDataType.dataType, 4, "bf1", null);
structB.addBitField(IntegerDataType.dataType, 6, "bf2", null);
structB.addBitField(IntegerDataType.dataType, 2, "bf3", null);
structB.add(structA, "b1", null);
/**
* /structC
* pack()
* Structure structC {
* 0 char 1 c0 ""
* 8 structB 56 c1 ""
* }
* Length: 64 Alignment: 8
*/
Structure structC = new StructureDataType("structC", 0);
structC.setPackingEnabled(true);
structC.add(CharDataType.dataType, "c0", null);
structC.add(structB, "c1", null);
return structC;
}
}

View File

@ -18,15 +18,8 @@ package sarif;
import org.junit.Test;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.ArrayDataType;
import ghidra.program.model.data.ByteDataType;
import ghidra.program.model.data.CharDataType;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.IntegerDataType;
import ghidra.program.model.data.ProgramBasedDataTypeManager;
import ghidra.program.model.data.ShortDataType;
import ghidra.program.model.data.Structure;
import ghidra.program.model.data.StructureDataType;
import ghidra.program.util.ProgramDiff;
public class DefinedDataSarifTest extends AbstractSarifTest {
@ -37,58 +30,16 @@ public class DefinedDataSarifTest extends AbstractSarifTest {
@Test
public void testDefinedData() throws Exception {
Structure structA = new StructureDataType("structA", 0);
structA.add(IntegerDataType.dataType, "a0", null);
structA.add(new ArrayDataType(CharDataType.dataType, 0, -1), "az", null);
structA.add(DataType.DEFAULT);
structA.add(ByteDataType.dataType, "a1", null);
structA.add(new ArrayDataType(IntegerDataType.dataType, 0, -1), "aflex", null);
/**
* /structB
* pack()
* Structure structB {
* 0 short 2 b0 ""
* 2 byte[0] 0 bfs ""
* 4 int:4(0) 1 bf1 ""
* 4 int:6(4) 2 bf2 ""
* 5 int:2(2) 1 bf3 ""
* 6 structA 6 b1 ""
* }
* Size = 12 Actual Alignment = 4
*/
Structure structB = new StructureDataType("structB", 0);
structB.setPackingEnabled(true);
structB.add(ShortDataType.dataType, "b0", null);
structB.add(new ArrayDataType(ByteDataType.dataType, 0, -1), "bfs", null);
structB.addBitField(IntegerDataType.dataType, 4, "bf1", null);
structB.addBitField(IntegerDataType.dataType, 6, "bf2", null);
structB.addBitField(IntegerDataType.dataType, 2, "bf3", null);
structB.add(structA, "b1", null);
/**
* /structC
* pack()
* Structure structC {
* 0 char 1 c0 ""
* 4 structB 12 c1 ""
* }
* Size = 16 Actual Alignment = 4
*/
Structure structC = new StructureDataType("structC", 0);
structC.setPackingEnabled(true);
structC.add(CharDataType.dataType, "c0", null);
structC.add(structB, "c1", null);
ProgramBasedDataTypeManager dtm = program.getDataTypeManager();
structC = (Structure) dtm.resolve(structC, null);
Structure sdt = DataTypesSarifTest.createComplexStructureDataType(dtm);
Structure struct = (Structure) dtm.resolve(sdt, null);
program.getListing().createData(addr(0x100), structC);
program.getListing().createData(addr(0x100), struct);
ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty());
assert (differences.isEmpty());
}
}