Skip to content

Commit 5a66f68

Browse files
committed
GT-3414 code review, unit tests for DefinedDataIterator.
1 parent 4fbbe98 commit 5a66f68

File tree

5 files changed

+208
-10
lines changed

5 files changed

+208
-10
lines changed

Ghidra/Features/Base/src/main/java/ghidra/program/database/ProgramBuilder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,14 @@ public Namespace createClassNamespace(String name, String parentNamespace, Sourc
695695
return c;
696696
}
697697

698+
public void applyFixedLengthDataType(String addressString, DataType dt, int length)
699+
throws CodeUnitInsertionException {
700+
startTransaction();
701+
DataUtilities.createData(program, addr(addressString), dt, length, false,
702+
ClearDataMode.CLEAR_ALL_CONFLICT_DATA);
703+
endTransaction();
704+
}
705+
698706
public void applyDataType(String addressString, DataType dt) {
699707
applyDataType(addressString, dt, 1);
700708
}
@@ -874,7 +882,7 @@ else if (encoding == StandardCharsets.UTF_16BE || encoding == StandardCharsets.U
874882
}
875883

876884
public Data createString(String address, String string, Charset charset, boolean nullTerminate,
877-
AbstractStringDataType dataType) throws Exception {
885+
DataType dataType) throws Exception {
878886
if (nullTerminate) {
879887
string = string + "\0";
880888
}
@@ -883,7 +891,7 @@ public Data createString(String address, String string, Charset charset, boolean
883891
}
884892

885893
public Data createString(String address, byte[] stringBytes, Charset charset,
886-
AbstractStringDataType dataType) throws Exception {
894+
DataType dataType) throws Exception {
887895
Address addr = addr(address);
888896
setBytes(address, stringBytes);
889897
if (dataType != null) {

Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/string/DefinedStringIteratorTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,17 @@ public void setUp() throws Exception {
7272
builder.createEncodedString("0x500", "This is the last string", StandardCharsets.US_ASCII,
7373
false);
7474

75+
ArrayDataType charArray = new ArrayDataType(new CharDataType(), 50, 1);
76+
builder.createString("0x600", "The 600 chararray", StandardCharsets.US_ASCII, true,
77+
charArray);
78+
7579
// create an empty area for tests to do their own thing
76-
builder.createUninitializedMemory("uninitialized", "0x3000", 100);
80+
builder.createUninitializedMemory("uninitialized", "0x3000", 0x1000);
81+
builder.applyDataType("0x3100", charArray);
82+
builder.applyFixedLengthDataType("0x3200", new StringDataType(), 10);
7783

7884
program = builder.getProgram();
85+
7986
}
8087

8188
@Test
@@ -117,6 +124,11 @@ public void testIterator() throws Exception {
117124
assertEquals(addr(0x500), foundString.getAddress());
118125
assertEquals("This is the last string", foundString.getString(program.getMemory()));
119126

127+
assertTrue(iterator.hasNext());
128+
foundString = iterator.next();
129+
assertEquals(addr(0x600), foundString.getAddress());
130+
assertEquals("The 600 chararray", foundString.getString(program.getMemory()));
131+
120132
assertFalse(iterator.hasNext());
121133
}
122134

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.program.util;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.List;
22+
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
26+
import ghidra.program.database.ProgramDB;
27+
import ghidra.program.model.data.*;
28+
import ghidra.program.model.listing.Data;
29+
import ghidra.program.model.util.CodeUnitInsertionException;
30+
import ghidra.test.AbstractGhidraHeadlessIntegrationTest;
31+
import ghidra.test.ToyProgramBuilder;
32+
import util.CollectionUtils;
33+
34+
public class DefinedDataIteratorTest extends AbstractGhidraHeadlessIntegrationTest {
35+
36+
private ToyProgramBuilder builder;
37+
private ProgramDB program;
38+
private DataTypeManager dtm;
39+
private DataType intDT;
40+
private StringDataType stringDT;
41+
private CharDataType charDT;
42+
private DataType charArray;
43+
private StructureDataType struct1DT;
44+
private ArrayDataType structArray;
45+
private StructureDataType struct2DT;
46+
private TypeDef intTD;
47+
48+
@Before
49+
public void setUp() throws Exception {
50+
51+
builder = new ToyProgramBuilder("DefinedDataIteratorTests", false);
52+
program = builder.getProgram();
53+
dtm = program.getDataTypeManager();
54+
55+
intDT = AbstractIntegerDataType.getSignedDataType(4, dtm);
56+
intTD = new TypedefDataType("int_typedef", intDT);
57+
stringDT = StringDataType.dataType;
58+
charDT = new CharDataType(dtm);
59+
charArray = new ArrayDataType(charDT, 20, charDT.getLength());
60+
61+
struct1DT = new StructureDataType("struct1", 100);
62+
struct1DT.replaceAtOffset(0, intDT, intDT.getLength(), "f1", null);
63+
struct1DT.replaceAtOffset(10, charArray, charArray.getLength(), "f2", null);
64+
struct1DT.replaceAtOffset(50, stringDT, 10, "f3", null);
65+
66+
structArray = new ArrayDataType(struct1DT, 10, struct1DT.getLength());
67+
68+
struct2DT = new StructureDataType("struct2", 200);
69+
struct2DT.replaceAtOffset(0, intDT, intDT.getLength(), "f1", null);
70+
struct2DT.replaceAtOffset(10, struct1DT, intDT.getLength(), "f2", null);
71+
72+
builder.createMemory("test", "0x0", 0x2000);
73+
program = builder.getProgram();
74+
}
75+
76+
@Test
77+
public void test_Ints() throws Exception {
78+
builder.applyFixedLengthDataType("0x0", intDT, intDT.getLength());
79+
builder.createString("0x10", "test1", StandardCharsets.UTF_8, true, stringDT);
80+
builder.applyFixedLengthDataType("0x100", struct1DT, struct1DT.getLength());
81+
82+
List<Data> list = CollectionUtils.asList(
83+
DefinedDataIterator.byDataType(program, dt -> dt instanceof IntegerDataType));
84+
85+
assertTrue(list.get(0).getAddress().getOffset() == 0x0);
86+
assertTrue(list.get(1).getAddress().getOffset() == 0x100);
87+
88+
assertEquals(2, list.size());
89+
}
90+
91+
@Test
92+
public void test_Strings() throws Exception {
93+
builder.applyFixedLengthDataType("0x0", intDT, intDT.getLength());
94+
builder.createString("0x10", "test1", StandardCharsets.UTF_8, true, stringDT);
95+
builder.applyFixedLengthDataType("0x100", struct1DT, struct1DT.getLength());
96+
97+
List<Data> list = CollectionUtils.asList(DefinedDataIterator.definedStrings(program));
98+
99+
assertTrue(list.get(0).getAddress().getOffset() == 0x10);
100+
assertTrue(list.get(1).getAddress().getOffset() == 0x100 + 10);
101+
assertTrue(list.get(2).getAddress().getOffset() == 0x100 + 50);
102+
103+
assertEquals(3, list.size());
104+
}
105+
106+
@Test
107+
public void test_ArrayOfStructs() throws Exception {
108+
builder.applyFixedLengthDataType("0x0", intDT, intDT.getLength());
109+
builder.createString("0x10", "test1", StandardCharsets.UTF_8, true, stringDT);
110+
builder.applyFixedLengthDataType("0x100", structArray, structArray.getLength());
111+
112+
int numElements = structArray.getNumElements();
113+
int lastEle = numElements - 1;
114+
int elementSize = structArray.getElementLength();
115+
116+
List<Data> list = CollectionUtils.asList(DefinedDataIterator.definedStrings(program));
117+
118+
assertEquals(list.get(0).getAddress().getOffset(), 0x10);
119+
assertEquals(list.get(1 + 0).getAddress().getOffset(), 0x100 + 10);
120+
assertEquals(list.get(1 + 1).getAddress().getOffset(), 0x100 + 50);
121+
122+
assertEquals(list.get(1 + (lastEle * 2) + 0).getAddress().getOffset(),
123+
0x100 + (elementSize * lastEle) + 10);
124+
assertEquals(list.get(1 + (lastEle * 2) + 1).getAddress().getOffset(),
125+
0x100 + (elementSize * lastEle) + 50);
126+
127+
assertEquals(1 + (numElements * 2), list.size());
128+
}
129+
130+
@Test
131+
public void test_Typedefs() throws CodeUnitInsertionException {
132+
// 3 ints: 2 are typedefs, 1 is regular int
133+
builder.applyFixedLengthDataType("0x0", intTD, intTD.getLength());
134+
builder.applyFixedLengthDataType("0x10", intTD, intTD.getLength());
135+
builder.applyFixedLengthDataType("0x20", intDT, intTD.getLength());
136+
137+
// iterating by data type ignores typedefs, so we should get all 3 ints
138+
List<Data> list = CollectionUtils.asList(
139+
DefinedDataIterator.byDataType(program, dt -> dt instanceof IntegerDataType));
140+
141+
assertEquals(3, list.size());
142+
143+
// iterating by data instance, we can inspect the actual data type and get the
144+
// typedef
145+
list = CollectionUtils.asList(DefinedDataIterator.byDataInstance(program,
146+
data -> data.getDataType() instanceof TypeDef));
147+
assertEquals(2, list.size());
148+
}
149+
}

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/listing/DataIterator.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@
2626
* @see CollectionUtils#asIterable
2727
*/
2828
public interface DataIterator extends Iterator<Data> {
29-
public static final DataIterator EMPTY = Of(/*nothing*/);
29+
public static final DataIterator EMPTY = of(/*nothing*/);
3030

31-
public static DataIterator Of(Data... dataInstances) {
31+
/**
32+
* Create a DataIterator that returns a sequence of the specified items.
33+
*
34+
* @param dataInstances variable length list of items that will be iterated
35+
* @return new Iterator
36+
*/
37+
public static DataIterator of(Data... dataInstances) {
3238
return new IteratorWrapper(Arrays.asList(dataInstances).iterator());
3339
}
3440

Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/DefinedDataIterator.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import ghidra.program.model.listing.*;
2424

2525
/**
26-
* Iterator that visits each defined data instance in a Program or in the footprint of
26+
* Iterator that visits each defined data instance in the initialized memory of a Program or in the footprint of
2727
* a specified data element.
2828
* <p>
2929
* Data elements that are nested inside of composites or arrays are visited, not just the
@@ -32,7 +32,7 @@
3232
public class DefinedDataIterator implements DataIterator {
3333

3434
/**
35-
* Creates a new iterator that traverses the entire Program's address space, visiting
35+
* Creates a new iterator that traverses the entire Program's address space, returning
3636
* data instances that successfully match the predicate.
3737
*
3838
* @param program Program to search
@@ -45,7 +45,21 @@ public static DefinedDataIterator byDataType(Program program,
4545
}
4646

4747
/**
48-
* Creates a new iterator that traverses the entire Program's address space.
48+
* Creates a new iterator that traverses the entire Program's address space, returning
49+
* data instances that successfully match the predicate.
50+
*
51+
* @param program Program to search
52+
* @param dataInstancePredicate {@link Predicate} that tests each data instance's properties
53+
* @return new iterator
54+
*/
55+
public static DefinedDataIterator byDataInstance(Program program,
56+
Predicate<Data> dataInstancePredicate) {
57+
return new DefinedDataIterator(program, null, null, dataInstancePredicate);
58+
}
59+
60+
/**
61+
* Creates a new iterator that traverses the entire Program's address space returning
62+
* data instances that are strings.
4963
*
5064
* @param program Ghidra {@link Program} to search
5165
* @return new iterator
@@ -57,7 +71,8 @@ public static DefinedDataIterator definedStrings(Program program) {
5771
}
5872

5973
/**
60-
* Creates a new iterator that traverses a portion of the Program's address space.
74+
* Creates a new iterator that traverses a portion of the Program's address space returning
75+
* data instances that are strings.
6176
*
6277
* @param program Ghidra {@link Program} to search
6378
* @param addrs addresses to limit the iteration to
@@ -84,6 +99,12 @@ public static DefinedDataIterator definedStrings(Data singleDataInstance) {
8499

85100
private Predicate<DataType> dataTypePredicate;
86101
private Predicate<Data> dataInstancePredicate;
102+
103+
/**
104+
* LIFO stack of iterators. Newly found iterators of sub-components are
105+
* pushed onto the end and become the current iterator. When an iterator is exhausted,
106+
* it is popped of the end and the uncovered iterator is now the current.
107+
*/
87108
private Deque<DataIterator> itStack = new ArrayDeque<>();
88109
private Data currentDataResult;
89110

@@ -101,7 +122,7 @@ private DefinedDataIterator(Data singleDataInstance, Predicate<DataType> dataTyp
101122
this.dataTypePredicate = dataTypePredicate;
102123
this.dataInstancePredicate = dataInstancePredicate;
103124

104-
itStack.addLast(DataIterator.Of(singleDataInstance));
125+
itStack.addLast(DataIterator.of(singleDataInstance));
105126
}
106127

107128
@Override
@@ -160,6 +181,8 @@ private boolean recursiveMatchesDataTypePredicate(DataType dt) {
160181
return recursiveMatchesDataTypePredicate(elementDT);
161182
}
162183
else if (dt instanceof Structure) {
184+
// handle Structures and general Composite's separately so
185+
// we can focus on just the defined elements of a structure
163186
Structure comp = (Structure) dt;
164187
for (DataTypeComponent dtc : comp.getDefinedComponents()) {
165188
if (recursiveMatchesDataTypePredicate(dtc.getDataType())) {

0 commit comments

Comments
 (0)