GP-5136 Changed SARIF export to only include defined-components when

outputing a structure definition
This commit is contained in:
ghidra1 2024-11-15 16:18:46 -05:00
parent 32c39f3518
commit 2390546961
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) { public IsfComposite(Composite composite, IsfDataTypeWriter writer, TaskMonitor monitor) {
super(composite); super(composite);
size = composite.getLength(); size = composite.isZeroLength() ? 0 : composite.getLength();
kind = composite instanceof Structure ? "struct" : "union"; kind = composite instanceof Structure ? "struct" : "union";
DataTypeComponent[] components = composite.getComponents(); DataTypeComponent[] components = composite.getDefinedComponents();
if (components.length == 0) {
// NB: composite.getLength always returns > 0
size = 0;
}
fields = new JsonObject(); fields = new JsonObject();
for (DataTypeComponent component : components) { for (DataTypeComponent component : components) {
if (monitor.isCancelled()) { if (monitor.isCancelled()) {

View File

@ -15,29 +15,12 @@
*/ */
package sarif; package sarif;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
import ghidra.program.model.address.AddressSetView; import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.Array; import ghidra.program.model.data.*;
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.util.ProgramDiff; import ghidra.program.util.ProgramDiff;
public class DataTypesSarifTest extends AbstractSarifTest { public class DataTypesSarifTest extends AbstractSarifTest {
@ -140,7 +123,7 @@ public class DataTypesSarifTest extends AbstractSarifTest {
@Test @Test
public void testCreateStructure() throws Exception { public void testCreateStructure() throws Exception {
ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager(); ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
StructureDataType sdt = new StructureDataType("test", 0); Structure sdt = createComplexStructureDataType(dataMgr);
Structure struct = (Structure) dataMgr.addDataType(sdt, null); Structure struct = (Structure) dataMgr.addDataType(sdt, null);
assertNotNull(struct); assertNotNull(struct);
@ -177,4 +160,70 @@ public class DataTypesSarifTest extends AbstractSarifTest {
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 org.junit.Test;
import ghidra.program.model.address.AddressSetView; 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.ProgramBasedDataTypeManager;
import ghidra.program.model.data.ShortDataType;
import ghidra.program.model.data.Structure; import ghidra.program.model.data.Structure;
import ghidra.program.model.data.StructureDataType;
import ghidra.program.util.ProgramDiff; import ghidra.program.util.ProgramDiff;
public class DefinedDataSarifTest extends AbstractSarifTest { public class DefinedDataSarifTest extends AbstractSarifTest {
@ -37,53 +30,11 @@ public class DefinedDataSarifTest extends AbstractSarifTest {
@Test @Test
public void testDefinedData() throws Exception { 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(); 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(); ProgramDiff programDiff = readWriteCompare();