Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-410 cache CategoryPath

dev74736819c8f712f515
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/data/CategoryTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/data/CategoryTest.java
index 81f1c83f29..6c971f37d6 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/data/CategoryTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/data/CategoryTest.java
@@ -110,14 +110,10 @@ public class CategoryTest extends AbstractGhidraHeadedIntegrationTest {
assertEquals("SubCat-B", sub2.getName());
}
- @Test
+ @Test(expected = InvalidNameException.class)
public void testCreateCategoryBadName() throws Exception {
- try {
- root.createCategory("");
- Assert.fail("Should not create category with empty name");
- }
- catch (InvalidNameException e) {
- }
+ root.createCategory("");
+ Assert.fail("Should not create category with empty name");
}
@Test
@@ -151,7 +147,10 @@ public class CategoryTest extends AbstractGhidraHeadedIntegrationTest {
@Test
public void testSetName() throws Exception {
Category sub1 = root.createCategory("SubCat-A");
+ assertEquals("/SubCat-A", sub1.getCategoryPath().getPath());
+
sub1.setName("MyCategory");
+ assertEquals("/MyCategory", sub1.getCategoryPath().getPath());
assertNotNull(root.getCategory("MyCategory"));
Category sub2 = root.createCategory("NewCategory");
@@ -160,16 +159,12 @@ public class CategoryTest extends AbstractGhidraHeadedIntegrationTest {
assertNotNull(root.getCategory("new name"));
}
- @Test
+ @Test(expected = InvalidNameException.class)
public void testSetBadName() throws Exception {
Category sub1 = root.createCategory("SubCat-A");
- try {
- sub1.setName(null);
- Assert.fail("Should not have set name to null");
- }
- catch (InvalidNameException e) {
- }
+ sub1.setName(null);
+ Assert.fail("Should not have set name to null");
}
@Test
@@ -276,6 +271,18 @@ public class CategoryTest extends AbstractGhidraHeadedIntegrationTest {
assertNotNull(sub1.createCategory("Sub-cat"));
}
+ @Test
+ public void testCategoryPathUpdateAfterMoveParent() throws Exception {
+ Category catA = root.createCategory("A");
+ Category catB = catA.createCategory("B");
+ Category catC = catB.createCategory("C");
+ assertEquals("/A/B/C", catC.getCategoryPath().getPath());
+
+ root.moveCategory(catB, monitor);
+
+ assertEquals("/B/C", catC.getCategoryPath().getPath());
+ }
+
@Test
public void testMoveParentCategory() throws Exception {
Category catA = root.createCategory("A");
@@ -284,6 +291,8 @@ public class CategoryTest extends AbstractGhidraHeadedIntegrationTest {
catC.createCategory("D");
long idB = catB.getID();
long idC = catC.getID();
+ assertEquals("/A/B/C", catC.getCategoryPath().getPath());
+
root.moveCategory(catB, monitor);
assertTrue(dataMgr.containsCategory(new CategoryPath("/B/C")));
@@ -439,7 +448,7 @@ public class CategoryTest extends AbstractGhidraHeadedIntegrationTest {
Category sub2 = sub1.createCategory("sub2");
sub2.addDataType(str, null);
- ArrayList<DataType> list = new ArrayList<DataType>();
+ ArrayList<DataType> list = new ArrayList<>();
dataMgr.findDataTypes(name, list);
assertEquals(3, list.size());
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ArrayDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ArrayDB.java
index 4f00591502..1d9e811feb 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ArrayDB.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/ArrayDB.java
@@ -88,13 +88,21 @@ class ArrayDB extends DataTypeDB implements Array {
@Override
public String getDisplayName() {
- validate(lock);
String localDisplayName = displayName;
- if (localDisplayName == null) {
- localDisplayName = DataTypeUtilities.getDisplayName(this, false);
- displayName = localDisplayName;
+ if (localDisplayName != null && !isInvalid()) {
+ return localDisplayName;
+ }
+ lock.acquire();
+ try {
+ checkIsValid();
+ if ( displayName == null ) {
+ displayName = DataTypeUtilities.getDisplayName(this, false);
+ }
+ return displayName;
+ }
+ finally {
+ lock.release();
}
- return localDisplayName;
}
@Override
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CategoryDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CategoryDB.java
index 6b3c3e71da..774aa2d4a4 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CategoryDB.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/CategoryDB.java
@@ -42,6 +42,7 @@ class CategoryDB extends DatabaseObject implements Category {
private LazyLoadingCachingMap<String, CategoryDB> subcategoryMap;
private LazyLoadingCachingMap<String, DataType> dataTypeMap;
private ConflictMap conflictMap;
+ private CategoryPath categoryPath;
/**
* Category Constructor
@@ -82,16 +83,26 @@ class CategoryDB extends DatabaseObject implements Category {
this(dtMgr, cache, DataTypeManagerDB.ROOT_CATEGORY_ID, null, "/");
}
- /**
- * @see ghidra.program.model.data.Category#getCategoryPath()
- */
@Override
public CategoryPath getCategoryPath() {
- validate(mgr.lock);
- if (isRoot() || isDeleted()) {
- return CategoryPath.ROOT;
+ CategoryPath localCategoryPath = categoryPath;
+ if (localCategoryPath != null && !isInvalid()) {
+ return localCategoryPath;
+ }
+ mgr.lock.acquire();
+ try {
+ checkIsValid();
+ if (isRoot() || isDeleted()) {
+ categoryPath = CategoryPath.ROOT;
+ }
+ if (categoryPath == null) {
+ categoryPath = new CategoryPath(parent.getCategoryPath(), name);
+ }
+ return categoryPath;
+ }
+ finally {
+ mgr.lock.release();
}
- return new CategoryPath(parent.getCategoryPath(), name);
}
@Override
@@ -104,6 +115,7 @@ class CategoryDB extends DatabaseObject implements Category {
subcategoryMap.clear();
dataTypeMap.clear();
conflictMap.clear();
+ categoryPath = null;
if (isRoot()) {
return true;
@@ -130,17 +142,11 @@ class CategoryDB extends DatabaseObject implements Category {
*/
@Override
public String getName() {
- mgr.lock.acquire();
- try {
- checkIsValid();
- if (isRoot()) {
- return mgr.getName();
- }
- return name;
- }
- finally {
- mgr.lock.release();
+ validate(mgr.lock);
+ if (isRoot()) {
+ return mgr.getName();
}
+ return name;
}
/**
@@ -394,6 +400,9 @@ class CategoryDB extends DatabaseObject implements Category {
if (mgr != category.getDataTypeManager()) {
throw new IllegalArgumentException("Category does not belong to my DataTypeManager");
}
+ if (!(category instanceof CategoryDB)) {
+ throw new IllegalArgumentException("Category is not a CategoryDB");
+ }
CategoryDB categoryDB = (CategoryDB) category;
mgr.lock.acquire();
try {
@@ -402,13 +411,12 @@ class CategoryDB extends DatabaseObject implements Category {
throw new DuplicateNameException(
"Category named " + categoryDB.getName() + " already exists");
}
- CategoryPath categoryPath = getCategoryPath();
- if (categoryPath.isAncestorOrSelf(categoryDB.getCategoryPath())) {
+ CategoryPath destCategoryPath = getCategoryPath();
+ CategoryPath movedCategoryOriginalPath = categoryDB.getCategoryPath();
+ if (destCategoryPath.isAncestorOrSelf(movedCategoryOriginalPath)) {
throw new IllegalArgumentException(
"Moved category is an ancestor of destination category!");
}
- CategoryPath oldPath = categoryDB.getCategoryPath();
-
try {
categoryDB.setParent(this);
}
@@ -416,7 +424,7 @@ class CategoryDB extends DatabaseObject implements Category {
mgr.dbError(e);
}
subcategoryMap.put(categoryDB.getName(), categoryDB);
- mgr.categoryMoved(oldPath, categoryDB);
+ mgr.categoryMoved(movedCategoryOriginalPath, categoryDB);
}
finally {
mgr.lock.release();
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeDB.java
index 8cb24332e8..e892c67bd1 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeDB.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/DataTypeDB.java
@@ -164,13 +164,22 @@ abstract class DataTypeDB extends DatabaseObject implements DataType, ChangeList
*/
@Override
public Settings getDefaultSettings() {
- validate(lock);
Settings localDefaultSettings = defaultSettings;
- if (localDefaultSettings == null) {
- localDefaultSettings = new SettingsDBManager(dataMgr, this, key);
- defaultSettings = localDefaultSettings;
+ if (localDefaultSettings != null && !isInvalid()) {
+ return localDefaultSettings;
+ }
+ lock.acquire();
+ try {
+ checkIsValid();
+ if (defaultSettings == null) {
+ defaultSettings = new SettingsDBManager(dataMgr, this, key);
+ }
+ return defaultSettings;
}
- return localDefaultSettings;
+ finally {
+ lock.release();
+ }
+
}
/**
@@ -286,15 +295,24 @@ abstract class DataTypeDB extends DatabaseObject implements DataType, ChangeList
@Override
public CategoryPath getCategoryPath() {
- validate(lock);
- if (category == null) {
- category = dataMgr.getCategory(doGetCategoryID());
+ Category cat = category;
+ if (cat != null && !isInvalid()) {
+ return cat.getCategoryPath();
+ }
+ lock.acquire();
+ try {
+ checkIsValid();
+ if (category == null) {
+ category = dataMgr.getCategory(doGetCategoryID());
+ }
+ if (category == null) {
+ category = dataMgr.getRootCategory();
+ }
+ return category.getCategoryPath();
}
- if (category == null) {
- category = dataMgr.getRootCategory();
- return CategoryPath.ROOT;
+ finally {
+ lock.release();
}
- return category.getCategoryPath();
}
@Override
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/PointerDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/PointerDB.java
index ff630c1b01..2c034e9bdd 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/PointerDB.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/data/PointerDB.java
@@ -136,23 +136,31 @@ class PointerDB extends DataTypeDB implements Pointer {
@Override
public String getDisplayName() {
- // NOTE: Pointer display name only specifies length if null base type
- validate(lock);
String localDisplayName = displayName;
- if (localDisplayName == null) {
- DataType dt = getDataType();
- if (dt == null) {
- localDisplayName = PointerDataType.POINTER_NAME;
- if (!isDynamicallySized()) {
- localDisplayName += Integer.toString(getLength() * 8);
+ if (localDisplayName != null && !isInvalid()) {
+ return localDisplayName;
+ }
+ lock.acquire();
+ try {
+ checkIsValid();
+ if ( displayName == null ) {
+ // NOTE: Pointer display name only specifies length if null base type
+ DataType dt = getDataType();
+ if (dt == null) {
+ displayName = PointerDataType.POINTER_NAME;
+ if (!isDynamicallySized()) {
+ displayName += Integer.toString(getLength() * 8);
+ }
+ }
+ else {
+ displayName = dt.getDisplayName() + " *";
}
}
- else {
- localDisplayName = dt.getDisplayName() + " *";
- }
- displayName = localDisplayName;
+ return displayName;
+ }
+ finally {
+ lock.release();
}
- return localDisplayName;
}
@Override