Skip to content

Commit

Permalink
GP-1994 refined datatype search and add ability to specify a program's
Browse files Browse the repository at this point in the history
preferred root-namespace category node
  • Loading branch information
ghidra1 committed May 13, 2022
1 parent 4b60084 commit d7fc209
Show file tree
Hide file tree
Showing 12 changed files with 735 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
import ghidra.program.util.ProgramChangeRecord;
import ghidra.trace.database.DBTrace;
import ghidra.trace.database.listing.*;
import ghidra.trace.database.memory.*;
import ghidra.trace.database.memory.DBTraceMemoryRegisterSpace;
import ghidra.trace.database.memory.DBTraceMemorySpace;
import ghidra.trace.database.symbol.DBTraceFunctionSymbolView;
import ghidra.trace.model.Trace.*;
import ghidra.trace.model.TraceAddressSnapRange;
Expand Down Expand Up @@ -1071,6 +1072,18 @@ public void setCompiler(String compiler) {
throw new UnsupportedOperationException();
}

@Override
public CategoryPath getPreferredRootNamespaceCategoryPath() {
// TODO: not yet implemented
return null;
}

@Override
public void setPreferredRootNamespaceCategoryPath(String categoryPath) {
// TODO: not yet implemented
throw new UnsupportedOperationException();
}

@Override
public String getExecutablePath() {
return trace.getExecutablePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ghidra.program.database.IntRangeMap;
import ghidra.program.database.map.AddressMap;
import ghidra.program.model.address.*;
import ghidra.program.model.data.CategoryPath;
import ghidra.program.model.lang.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.pcode.Varnode;
Expand Down Expand Up @@ -141,6 +142,16 @@ public void setCompiler(String compiler) {
view.setCompiler(compiler);
}

@Override
public CategoryPath getPreferredRootNamespaceCategoryPath() {
return view.getPreferredRootNamespaceCategoryPath();
}

@Override
public void setPreferredRootNamespaceCategoryPath(String categoryPath) {
view.setPreferredRootNamespaceCategoryPath(categoryPath);
}

@Override
public String getExecutablePath() {
return view.getExecutablePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ else if (UNDEFINED.equals(name)) {
static DataType findDataType(DataTypeManager dataTypeManager, Demangled namespace,
String dtName) {

// TODO: add support for use of Program.getPreferredRootNamespaceCategoryPath when
// searching for datatypes

List<DataType> list = new ArrayList<>();
dataTypeManager.findDataTypes(dtName, list);
if (list.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidra.program.database.data;

import static org.junit.Assert.*;

import org.junit.*;

import ghidra.program.database.ProgramBuilder;
import ghidra.program.database.ProgramDB;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.GhidraClass;
import ghidra.program.model.symbol.*;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;

public class DataTypeUtilitiesFindTest extends AbstractGhidraHeadedIntegrationTest {
private ProgramDB program;
private DataTypeManagerDB dataMgr;
private SymbolTable symTab;

private GhidraClass a;
private Namespace ab;
private GhidraClass aba;
private Namespace abab;
private GhidraClass ababa;

@Before
public void setUp() throws Exception {
program = createDefaultProgram(testName.getMethodName(), ProgramBuilder._TOY, this);
dataMgr = program.getDataTypeManager();
symTab = program.getSymbolTable();
program.startTransaction("Test");

a = symTab.createClass(program.getGlobalNamespace(), "A", SourceType.USER_DEFINED);
ab = symTab.createNameSpace(a, "B", SourceType.USER_DEFINED); // A::B
aba = symTab.createClass(ab, "A", SourceType.USER_DEFINED); // A::B::A
abab = symTab.createNameSpace(aba, "B", SourceType.USER_DEFINED); // A::B::A::B
ababa = symTab.createClass(abab, "A", SourceType.USER_DEFINED); // A::B::A::B::A

StructureDataType structA = new StructureDataType("A", 0);

dataMgr.resolve(structA, null);

CategoryPath cp = new CategoryPath("/x/A");
structA.setCategoryPath(cp);
dataMgr.resolve(structA, null);

cp = new CategoryPath("/x/A/B");
structA.setCategoryPath(cp);
dataMgr.resolve(structA, null);

cp = new CategoryPath("/x/A/B/A");
structA.setCategoryPath(cp);
dataMgr.resolve(structA, null);

cp = new CategoryPath("/x/A/B/A/B");
structA.setCategoryPath(cp);
dataMgr.resolve(structA, null);

cp = new CategoryPath("/x/A/B/A/B/A");
structA.setCategoryPath(cp);
dataMgr.resolve(structA, null);

cp = new CategoryPath("/y/A");
structA.setCategoryPath(cp);
dataMgr.resolve(structA, null);

cp = new CategoryPath("/y/A/B");
structA.setCategoryPath(cp);
dataMgr.resolve(structA, null);

cp = new CategoryPath("/y/A/B/A");
structA.setCategoryPath(cp);
dataMgr.resolve(structA, null);

cp = new CategoryPath("/y/A/B/A/B");
structA.setCategoryPath(cp);
// omit struct fro category

cp = new CategoryPath("/y/A/B/A/B/A");
structA.setCategoryPath(cp);
dataMgr.resolve(structA, null);

}

@After
public void tearDown() throws Exception {
program.release(this);
}

private void assertPath(DataType dt, String path) {
assertNotNull(dt);
assertEquals(path, dt.getPathName());
}

@Test
public void testFindDataType() {

DataType dt = DataTypeUtilities.findDataType(dataMgr, program.getGlobalNamespace(), "A",
Structure.class);
assertPath(dt, "/A");

dt = DataTypeUtilities.findDataType(dataMgr, null, "A", null);
assertPath(dt, "/A");

dt = DataTypeUtilities.findDataType(dataMgr, ab, "A", Structure.class);
assertPath(dt, "/x/A/B/A");

dt = DataTypeUtilities.findDataType(dataMgr, aba, "A", Structure.class);
assertPath(dt, "/x/A/B/A/A");

program.setPreferredRootNamespaceCategoryPath("/y");

dt = DataTypeUtilities.findDataType(dataMgr, program.getGlobalNamespace(), "A",
Structure.class);
assertPath(dt, "/A");

dt = DataTypeUtilities.findDataType(dataMgr, null, "A", null);
assertPath(dt, "/A");

dt = DataTypeUtilities.findDataType(dataMgr, ab, "A", Structure.class);
assertPath(dt, "/y/A/B/A");

dt = DataTypeUtilities.findDataType(dataMgr, aba, "A", Structure.class);
assertPath(dt, "/y/A/B/A/A");

}

@Test
public void findExistingClassStruct() {

// NOTE: search gives preference to class structure found in parent-namespace

DataType dt = DataTypeUtilities.findExistingClassStruct(dataMgr, a);
assertPath(dt, "/A");

dt = DataTypeUtilities.findExistingClassStruct(dataMgr, aba); // A::B::A
assertPath(dt, "/x/A/B/A");

dt = DataTypeUtilities.findExistingClassStruct(dataMgr, ababa); // A::B::A::B::A
assertPath(dt, "/x/A/B/A/B/A");

program.setPreferredRootNamespaceCategoryPath("/y");

dt = DataTypeUtilities.findExistingClassStruct(dataMgr, a);
assertPath(dt, "/y/A/A"); // not found in parent /y

dt = DataTypeUtilities.findExistingClassStruct(dataMgr, aba); // A::B::A
assertPath(dt, "/y/A/B/A");

dt = DataTypeUtilities.findExistingClassStruct(dataMgr, ababa); // A::B::A::B::A
assertPath(dt, "/y/A/B/A/B/A/A"); // not found in parent /y/A/B/A/B

}

@Test
public void findNamespaceQualifiedDataType() {

DataType dt =
DataTypeUtilities.findNamespaceQualifiedDataType(dataMgr, "A", Structure.class);
assertPath(dt, "/A");

dt = DataTypeUtilities.findNamespaceQualifiedDataType(dataMgr, "A", null);
assertPath(dt, "/A");

dt = DataTypeUtilities.findNamespaceQualifiedDataType(dataMgr, "A::A", Structure.class);
assertPath(dt, "/x/A/A");

dt = DataTypeUtilities.findNamespaceQualifiedDataType(dataMgr, "A::B::A", Structure.class);
assertPath(dt, "/x/A/B/A");

program.setPreferredRootNamespaceCategoryPath("/y");

dt = DataTypeUtilities.findNamespaceQualifiedDataType(dataMgr, "A", Structure.class);
assertPath(dt, "/A");

dt = DataTypeUtilities.findNamespaceQualifiedDataType(dataMgr, "A", null);
assertPath(dt, "/A");

dt = DataTypeUtilities.findNamespaceQualifiedDataType(dataMgr, "A::A", Structure.class);
assertPath(dt, "/y/A/A");

dt = DataTypeUtilities.findNamespaceQualifiedDataType(dataMgr, "A::B::A", Structure.class);
assertPath(dt, "/y/A/B/A");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ public void stateChanged(ChangeEvent e) {
private ArrayList<Object> consumers;
protected Map<String, String> metadata = new LinkedHashMap<String, String>();

// A flag indicating whether the domain object has changed. Any methods of this domain object
// which cause its state to change must set this flag to true
// FIXME: (see GP-2003) "changed" flag is improperly manipulated by various methods.
// In general, comitted transactions will trigger all valid cases of setting flag to true,
// there may be a few cases where setting it to false may be appropriate. Without a transation
// it's unclear why it should ever need to get set true.

// A flag indicating whether the domain object has changed.
protected boolean changed = false;

// a flag indicating that this object is temporary
Expand Down
Loading

0 comments on commit d7fc209

Please sign in to comment.