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

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -27,17 +27,13 @@ public class IsfComposite extends AbstractIsfObject {
public String kind; public String kind;
public Integer size; public Integer size;
public JsonObject fields; public JsonObject fields;
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

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -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 {
@ -63,11 +46,11 @@ public class DataTypesSarifTest extends AbstractSarifTest {
cp = new CategoryPath("/A/B/C/D/E/F/G/H"); cp = new CategoryPath("/A/B/C/D/E/F/G/H");
dataMgr.createCategory(cp); dataMgr.createCategory(cp);
assertTrue(dataMgr.containsCategory(cp)); assertTrue(dataMgr.containsCategory(cp));
ProgramDiff programDiff = readWriteCompare(); ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor); AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty()); assert (differences.isEmpty());
} }
@Test @Test
@ -78,9 +61,9 @@ public class DataTypesSarifTest extends AbstractSarifTest {
assertNotNull(array); assertNotNull(array);
ProgramDiff programDiff = readWriteCompare(); ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor); AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty()); assert (differences.isEmpty());
} }
@Test @Test
@ -93,9 +76,9 @@ public class DataTypesSarifTest extends AbstractSarifTest {
assertNotNull(td); assertNotNull(td);
ProgramDiff programDiff = readWriteCompare(); ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor); AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty()); assert (differences.isEmpty());
} }
@Test @Test
@ -110,9 +93,9 @@ public class DataTypesSarifTest extends AbstractSarifTest {
assertNotNull(ptr); assertNotNull(ptr);
ProgramDiff programDiff = readWriteCompare(); ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor); AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty()); assert (differences.isEmpty());
} }
@Test @Test
@ -130,24 +113,24 @@ public class DataTypesSarifTest extends AbstractSarifTest {
p = new Pointer32DataType(null); p = new Pointer32DataType(null);
ptr = (Pointer) dataMgr.resolve(p, null); ptr = (Pointer) dataMgr.resolve(p, null);
assertNotNull(ptr); assertNotNull(ptr);
ProgramDiff programDiff = readWriteCompare(); ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor); AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty()); assert (differences.isEmpty());
} }
@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);
ProgramDiff programDiff = readWriteCompare(); ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor); AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty()); assert (differences.isEmpty());
} }
@Test @Test
@ -158,9 +141,9 @@ public class DataTypesSarifTest extends AbstractSarifTest {
assertNotNull(union); assertNotNull(union);
ProgramDiff programDiff = readWriteCompare(); ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor); AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty()); assert (differences.isEmpty());
} }
@Test @Test
@ -172,9 +155,75 @@ public class DataTypesSarifTest extends AbstractSarifTest {
assertNotNull(funcDef); assertNotNull(funcDef);
ProgramDiff programDiff = readWriteCompare(); ProgramDiff programDiff = readWriteCompare();
AddressSetView differences = programDiff.getDifferences(monitor); 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

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -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,58 +30,16 @@ 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();
AddressSetView differences = programDiff.getDifferences(monitor); AddressSetView differences = programDiff.getDifferences(monitor);
assert(differences.isEmpty()); assert (differences.isEmpty());
} }
} }