Merge remote-tracking branch 'origin/GP-6097_ghidragon_read_write_lock--SQUASHED'

This commit is contained in:
ghidra1
2026-04-14 16:30:45 -04:00
141 changed files with 6382 additions and 9258 deletions
@@ -286,7 +286,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceAddressPropertyManager createAddressPropertyManager(
DBTraceThreadManager threadManager) throws CancelledException, IOException {
return createTraceManager("Address Property Manager",
(openMode, monitor) -> new DBTraceAddressPropertyManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceAddressPropertyManager(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager));
}
@@ -294,7 +294,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceBookmarkManager createBookmarkManager(DBTraceThreadManager threadManager)
throws CancelledException, IOException {
return createTraceManager("Bookmark Manager",
(openMode, monitor) -> new DBTraceBookmarkManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceBookmarkManager(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager));
}
@@ -302,7 +302,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceBreakpointManager createBreakpointManager(DBTraceObjectManager objectManager)
throws CancelledException, IOException {
return createTraceManager("Breakpoint Manager",
(openMode, monitor) -> new DBTraceBreakpointManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceBreakpointManager(dbh, openMode, lock, monitor,
this, objectManager));
}
@@ -312,7 +312,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
DBTraceOverlaySpaceAdapter overlayAdapter, DBTraceReferenceManager referenceManager)
throws CancelledException, IOException {
return createTraceManager("Code Manager",
(openMode, monitor) -> new DBTraceCodeManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceCodeManager(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager, platformManager, dataTypeManager, overlayAdapter,
referenceManager));
}
@@ -321,7 +321,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceCommentAdapter createCommentAdapter(DBTraceThreadManager threadManager)
throws CancelledException, IOException {
return createTraceManager("Comment Adapter",
(openMode, monitor) -> new DBTraceCommentAdapter(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceCommentAdapter(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager));
}
@@ -329,7 +329,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceDataSettingsAdapter createDataSettingsAdapter(
DBTraceThreadManager threadManager) throws CancelledException, IOException {
return createTraceManager("Data Settings Adapter",
(openMode, monitor) -> new DBTraceDataSettingsAdapter(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceDataSettingsAdapter(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager));
}
@@ -337,7 +337,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceDataTypeManager createDataTypeManager(DBTracePlatformManager platformManager)
throws CancelledException, IOException {
return createTraceManager("Data Type Manager", (openMode,
monitor) -> new DBTraceDataTypeManager(dbh, openMode, rwLock, monitor, this,
monitor) -> new DBTraceDataTypeManager(dbh, openMode, lock, monitor, this,
platformManager.getHostPlatform()));
}
@@ -345,7 +345,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceEquateManager createEquateManager(DBTraceThreadManager threadManager)
throws CancelledException, IOException {
return createTraceManager("Equate Manager",
(openMode, monitor) -> new DBTraceEquateManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceEquateManager(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager));
}
@@ -353,7 +353,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTracePlatformManager createPlatformManager()
throws CancelledException, IOException {
return createTraceManager("Platform Manager",
(openMode, monitor) -> new DBTracePlatformManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTracePlatformManager(dbh, openMode, lock, monitor,
baseCompilerSpec, this));
}
@@ -361,7 +361,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceMemoryManager createMemoryManager(DBTraceThreadManager threadManager,
DBTraceOverlaySpaceAdapter overlayAdapter) throws IOException, CancelledException {
return createTraceManager("Memory Manager",
(openMode, monitor) -> new DBTraceMemoryManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceMemoryManager(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager, overlayAdapter));
}
@@ -369,14 +369,14 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceModuleManager createModuleManager(DBTraceObjectManager objectManager)
throws CancelledException, IOException {
return createTraceManager("Module Manager",
(openMode, monitor) -> new DBTraceModuleManager(dbh, openMode, rwLock, monitor, this,
(openMode, monitor) -> new DBTraceModuleManager(dbh, openMode, lock, monitor, this,
objectManager));
}
@DependentService
protected DBTraceObjectManager createObjectManager() throws CancelledException, IOException {
return createTraceManager("Object Manager",
(openMode, monitor) -> new DBTraceObjectManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceObjectManager(dbh, openMode, lock, monitor,
baseLanguage, this));
}
@@ -384,14 +384,14 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceOverlaySpaceAdapter createOverlaySpaceAdapter()
throws CancelledException, IOException {
return createTraceManager("Overlay Space Adapter", (openMode,
monitor) -> new DBTraceOverlaySpaceAdapter(dbh, openMode, rwLock, monitor, this));
monitor) -> new DBTraceOverlaySpaceAdapter(dbh, openMode, lock, monitor, this));
}
@DependentService
protected DBTraceReferenceManager createReferenceManager(DBTraceThreadManager threadManager,
DBTraceOverlaySpaceAdapter overlayAdapter) throws CancelledException, IOException {
return createTraceManager("Reference Manager",
(openMode, monitor) -> new DBTraceReferenceManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceReferenceManager(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager, overlayAdapter));
}
@@ -400,7 +400,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
DBTraceThreadManager threadManager, DBTracePlatformManager platformManager)
throws CancelledException, IOException {
return createTraceManager("Context Manager",
(openMode, monitor) -> new DBTraceRegisterContextManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceRegisterContextManager(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager, platformManager));
}
@@ -408,7 +408,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceStackManager createStackManager(DBTraceThreadManager threadManager,
DBTraceOverlaySpaceAdapter overlayAdapter) throws CancelledException, IOException {
return createTraceManager("Stack Manager",
(openMode, monitor) -> new DBTraceStackManager(dbh, openMode, rwLock, monitor, this,
(openMode, monitor) -> new DBTraceStackManager(dbh, openMode, lock, monitor, this,
threadManager, overlayAdapter));
}
@@ -416,7 +416,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceStaticMappingManager createStaticMappingManager(
DBTraceOverlaySpaceAdapter overlayAdapter) throws CancelledException, IOException {
return createTraceManager("Static Mapping Manager",
(openMode, monitor) -> new DBTraceStaticMappingManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceStaticMappingManager(dbh, openMode, lock, monitor,
this, overlayAdapter));
}
@@ -425,7 +425,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
DBTraceDataTypeManager dataTypeManager, DBTraceOverlaySpaceAdapter overlayAdapter)
throws CancelledException, IOException {
return createTraceManager("Symbol Manager",
(openMode, monitor) -> new DBTraceSymbolManager(dbh, openMode, rwLock, monitor,
(openMode, monitor) -> new DBTraceSymbolManager(dbh, openMode, lock, monitor,
baseLanguage, this, threadManager, dataTypeManager, overlayAdapter));
}
@@ -433,7 +433,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceThreadManager createThreadManager(DBTraceObjectManager objectManager)
throws IOException, CancelledException {
return createTraceManager("Thread Manager",
(openMode, monitor) -> new DBTraceThreadManager(dbh, openMode, rwLock, monitor, this,
(openMode, monitor) -> new DBTraceThreadManager(dbh, openMode, lock, monitor, this,
objectManager));
}
@@ -441,7 +441,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
protected DBTraceTimeManager createTimeManager(DBTraceThreadManager threadManager)
throws IOException, CancelledException {
return createTraceManager("Time Manager", (openMode, monitor) -> new DBTraceTimeManager(dbh,
openMode, rwLock, monitor, this, threadManager));
openMode, lock, monitor, this, threadManager));
}
@Override
@@ -650,12 +650,12 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
@Override
public LockHold lockRead() {
return LockHold.lock(rwLock.readLock());
return LockHold.lock(lock.readLock());
}
@Override
public LockHold lockWrite() {
return LockHold.lock(rwLock.writeLock());
return LockHold.lock(lock.writeLock());
}
public void sourceArchiveChanged(UniversalID sourceArchiveID) {
@@ -755,7 +755,7 @@ public class DBTrace extends DBCachedDomainObjectAdapter implements Trace, Trace
@Override
protected void clearCache(boolean all) {
try (LockHold hold = LockHold.lock(rwLock.writeLock())) {
try (LockHold hold = LockHold.lock(lock.writeLock())) {
for (DBTraceManager m : managers) {
m.invalidateCache(all);
}
@@ -4,9 +4,9 @@
* 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.
@@ -78,7 +78,7 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {
protected abstract V doGetContaining(K key);
protected List<? extends T> getAllInRangeCacheContaining(K key) {
protected synchronized List<? extends T> getAllInRangeCacheContaining(K key) {
List<T> result = new ArrayList<>();
for (Entry<TraceAddressSnapRange, T> ent : rangeCache) {
TraceAddressSnapRange range = ent.getKey();
@@ -93,7 +93,7 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {
return result;
}
protected T getFirstInRangeCacheContaining(K key) {
protected synchronized T getFirstInRangeCacheContaining(K key) {
for (Entry<TraceAddressSnapRange, T> ent : rangeCache) {
TraceAddressSnapRange range = ent.getKey();
if (!range.getLifespan().contains(key.snap)) {
@@ -107,12 +107,12 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {
return null;
}
protected boolean isInCachedRange(long snap, Address address) {
protected synchronized boolean isInCachedRange(long snap, Address address) {
return rangeCacheRange != null && rangeCacheRange.getLifespan().contains(snap) &&
rangeCacheRange.getRange().contains(address);
}
protected void ensureInCachedRange(long snap, Address address) {
protected synchronized void ensureInCachedRange(long snap, Address address) {
if (isInCachedRange(snap, address)) {
return;
}
@@ -120,15 +120,15 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {
loadRangeCache(rangeCacheRange = computeNewCachedRange(snap, address));
}
protected TraceAddressSnapRange computeNewCachedRange(long snap, Address address) {
protected synchronized TraceAddressSnapRange computeNewCachedRange(long snap, Address address) {
return ImmutableTraceAddressSnapRange.centered(address, snap, addressBreadth, snapBreadth);
}
public V getContaining(K key) {
public synchronized V getContaining(K key) {
return pointCache.computeIfAbsent(key, this::doGetContaining);
}
public void notifyNewEntry(Lifespan lifespan, Address address, T item) {
public synchronized void notifyNewEntry(Lifespan lifespan, Address address, T item) {
// TODO: Can this be smarter?
pointCache.clear();
if (rangeCacheRange != null &&
@@ -139,7 +139,7 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {
}
}
public void notifyNewEntry(Lifespan lifespan, AddressRange range, T item) {
public synchronized void notifyNewEntry(Lifespan lifespan, AddressRange range, T item) {
// TODO: Can this be smarter?
pointCache.clear();
if (rangeCacheRange != null &&
@@ -150,7 +150,7 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {
}
}
public void notifyNewEntries(Lifespan lifespan, AddressSetView addresses, T item) {
public synchronized void notifyNewEntries(Lifespan lifespan, AddressSetView addresses, T item) {
// TODO: Can this be smarter?
pointCache.clear();
if (rangeCacheRange != null &&
@@ -164,17 +164,18 @@ public abstract class DBTraceCacheForContainingQueries<K extends GetKey, V, T> {
}
}
public void notifyEntryRemoved(Lifespan lifespan, AddressRange range, T item) {
public synchronized void notifyEntryRemoved(Lifespan lifespan, AddressRange range, T item) {
// TODO: Can this be smarter?
invalidate();
}
public void notifyEntryShapeChanged(Lifespan lifespan, AddressRange range, T item) {
public synchronized void notifyEntryShapeChanged(Lifespan lifespan, AddressRange range,
T item) {
// TODO: Can this be smarter?
invalidate();
}
public void invalidate() {
public synchronized void invalidate() {
pointCache.clear();
rangeCache.clear();
rangeCacheRange = null;
@@ -4,9 +4,9 @@
* 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.
@@ -41,7 +41,7 @@ public abstract class DBTraceCacheForSequenceQueries<T> {
this.max = range.getMaxAddress();
}
public T getFloor(Address address) {
public synchronized T getFloor(Address address) {
Entry<Address, T> floor = nav.floorEntry(address);
if (floor != null) {
return floor.getValue();
@@ -60,7 +60,7 @@ public abstract class DBTraceCacheForSequenceQueries<T> {
return ent.getValue();
}
public T getCeiling(Address address) {
public synchronized T getCeiling(Address address) {
Entry<Address, T> ceiling = nav.ceilingEntry(address);
if (ceiling != null) {
return ceiling.getValue();
@@ -79,19 +79,20 @@ public abstract class DBTraceCacheForSequenceQueries<T> {
return ent.getValue();
}
public void load(
public synchronized void load(
ArrayList<? extends Entry<? extends TraceAddressSnapRange, ? extends T>> entries) {
for (Entry<? extends TraceAddressSnapRange, ? extends T> ent : entries) {
nav.put(ent.getKey().getX1(), ent.getValue());
}
}
protected boolean contains(Address address) {
protected synchronized boolean contains(Address address) {
return min.hasSameAddressSpace(address) && min.compareTo(address) <= 0 &&
max.compareTo(address) >= 0;
}
protected void reInit(@SuppressWarnings("hiding") long snap, AddressRange range) {
protected synchronized void reInit(@SuppressWarnings("hiding") long snap,
AddressRange range) {
this.snap = snap;
this.min = range.getMinAddress();
this.max = range.getMaxAddress();
@@ -101,7 +102,7 @@ public abstract class DBTraceCacheForSequenceQueries<T> {
protected final int maxRegions;
protected final int addressBreadth;
// TODO: Depending on the number of regions, LinkedList may perform better
protected final List<CachedRegion> cache = new ArrayList<>();
protected final List<CachedRegion> cache = Collections.synchronizedList(new ArrayList<>());
public DBTraceCacheForSequenceQueries(int maxRegions, int addressBreadth) {
this.maxRegions = maxRegions;
@@ -65,13 +65,13 @@ public abstract class AbstractBaseDBTraceDefinedUnitsView<T extends AbstractDBTr
}
@Override
protected void loadRangeCache(TraceAddressSnapRange range) {
protected synchronized void loadRangeCache(TraceAddressSnapRange range) {
rangeCache.addAll(
mapSpace.reduce(TraceAddressSnapRangeQuery.intersecting(range)).entries());
}
@Override
protected T doGetContaining(GetKey key) {
protected synchronized T doGetContaining(GetKey key) {
ensureInCachedRange(key.snap, key.addr);
return getFirstInRangeCacheContaining(key);
}
@@ -87,14 +87,15 @@ public abstract class AbstractBaseDBTraceDefinedUnitsView<T extends AbstractDBTr
}
@Override
protected void loadCachedRegion(CachedRegion region) {
protected synchronized void loadCachedRegion(CachedRegion region) {
region.load(new ArrayList<>(
mapSpace.reduce(TraceAddressSnapRangeQuery.intersecting(region.min, region.max,
region.snap, region.snap)).entries()));
}
@Override
protected Entry<TraceAddressSnapRange, T> doFloorEntry(long snap, Address max) {
protected synchronized Entry<TraceAddressSnapRange, T> doFloorEntry(long snap,
Address max) {
Address spaceMin = space.space.getMinAddress();
return mapSpace
.reduce(TraceAddressSnapRangeQuery.intersecting(spaceMin, max, snap, snap)
@@ -103,7 +104,8 @@ public abstract class AbstractBaseDBTraceDefinedUnitsView<T extends AbstractDBTr
}
@Override
protected Entry<TraceAddressSnapRange, T> doCeilingEntry(long snap, Address min) {
protected synchronized Entry<TraceAddressSnapRange, T> doCeilingEntry(long snap,
Address min) {
Address spaceMax = space.space.getMaxAddress();
return mapSpace
.reduce(TraceAddressSnapRangeQuery.intersecting(min, spaceMax, snap, snap))
@@ -366,7 +366,7 @@ public class DBTraceInstruction extends AbstractDBTraceCodeUnit<DBTraceInstructi
@Override
public Address getFallThrough() {
try (LockHold hold = LockHold.lock(space.lock.readLock())) {
checkIsValid();
refreshIfNeeded();
if (isFallThroughOverridden()) {
DBTraceReferenceSpace refSpace = space.referenceManager.get(space.space, false);
if (refSpace == null) {
@@ -392,7 +392,7 @@ public class DBTraceInstruction extends AbstractDBTraceCodeUnit<DBTraceInstructi
@Override
public Address getFallFrom() {
try (LockHold hold = LockHold.lock(space.lock.readLock())) {
checkIsValid();
refreshIfNeeded();
// Go back one, considering alignment
DBTraceInstruction ins = this;
int alignment = Math.min(1, getLanguage().getInstructionAlignment());
@@ -515,7 +515,7 @@ public class DBTraceInstruction extends AbstractDBTraceCodeUnit<DBTraceInstructi
@Override
public boolean hasFallthrough() {
try (LockHold hold = LockHold.lock(space.lock.readLock())) {
checkIsValid();
refreshIfNeeded();
if (isFallThroughOverridden()) {
return getFallThrough() != null; // dest stored as reference
}
@@ -646,7 +646,7 @@ public class DBTraceInstruction extends AbstractDBTraceCodeUnit<DBTraceInstructi
return getBytes();
}
try (LockHold hold = LockHold.lock(space.lock.readLock())) {
checkIsValid();
refreshIfNeeded();
int len = getPrototype().getLength();
byte[] b = new byte[len];
Address addr = getAddress();
@@ -79,19 +79,19 @@ public abstract class ByteCache {
return new Page();
}
public boolean canCache(Address address, int len) {
public synchronized boolean canCache(Address address, int len) {
long cacheBufOff = address.getOffset() & ~OFFSET_MASK;
return cacheBufOff + len < pageCount * SIZE;
}
public byte read(Address address) throws MemoryAccessException {
public synchronized byte read(Address address) throws MemoryAccessException {
Address pageStart = address.getNewAddress(address.getOffset() & OFFSET_MASK);
Page page = ensurePageCached(pageStart, 1);
int cacheBufOff = (int) address.subtract(pageStart);
return page.bytes[cacheBufOff];
}
public int read(Address address, ByteBuffer buf) throws MemoryAccessException {
public synchronized int read(Address address, ByteBuffer buf) throws MemoryAccessException {
long startOff = address.getOffset();
long startPage = startOff & OFFSET_MASK;
int bufStart = buf.position();
@@ -108,7 +108,7 @@ public abstract class ByteCache {
return buf.position() - bufStart;
}
protected int choosePage(Address address, int len) {
private int choosePage(Address address, int len) {
for (int i = 0; i < pageCount; i++) {
Page page = pages[i];
if (page.contains(address, len)) {
@@ -135,21 +135,17 @@ public abstract class ByteCache {
if (chosen == 0) {
return pages[0];
}
synchronized (pages) {
Page temp = pages[chosen];
pages[chosen] = pages[0];
pages[0] = temp;
return temp;
}
Page temp = pages[chosen];
pages[chosen] = pages[0];
pages[0] = temp;
return temp;
}
protected abstract int doLoad(Address address, ByteBuffer buf) throws MemoryAccessException;
public void invalidate(AddressRange range) {
synchronized (pages) {
for (Page p : pages) {
p.invalidate(range);
}
public synchronized void invalidate(AddressRange range) {
for (Page p : pages) {
p.invalidate(range);
}
}
}
@@ -54,11 +54,6 @@ public class DBTraceProgramViewBookmarkManager implements TraceProgramViewBookma
return bookmarkManager.defineBookmarkType(type, icon, color, priority);
}
@Override
public boolean isDefinedType(String type) {
return bookmarkManager.isDefinedType(type);
}
@Override
public BookmarkType[] getBookmarkTypes() {
Collection<? extends TraceBookmarkType> types = bookmarkManager.getDefinedBookmarkTypes();
@@ -236,7 +236,7 @@ public abstract class AbstractDBTraceSymbol extends DBAnnotatedObject
@Override
public String[] getPath() {
try (LockHold hold = LockHold.lock(manager.lock.readLock())) {
checkIsValid();
refreshIfNeeded();
if (isGlobal()) {
return new String[] { getName() };
}
@@ -89,7 +89,7 @@ public abstract class AbstractDBTraceSymbolSingleTypeWithLocationView<T extends
}
@Override
protected void loadRangeCache(TraceAddressSnapRange range) {
protected synchronized void loadRangeCache(TraceAddressSnapRange range) {
rangeCache.clear();
DBTraceAddressSnapRangePropertyMapSpace<Long, DBTraceSymbolIDEntry> idSpace =
manager.idMap.getForSpace(range.getRange().getAddressSpace(), false);
@@ -116,7 +116,7 @@ public abstract class AbstractDBTraceSymbolSingleTypeWithLocationView<T extends
}
@Override
protected Collection<? extends T> doGetContaining(GetSymbolsKey key) {
protected synchronized Collection<? extends T> doGetContaining(GetSymbolsKey key) {
if (key.thread != null) {
List<T> result =
new ArrayList<>(getIntersecting(Lifespan.at(key.snap),
@@ -15,7 +15,7 @@
*/
package ghidra.trace.database.map;
import static ghidra.lifecycle.Unfinished.TODO;
import static ghidra.lifecycle.Unfinished.*;
import static org.junit.Assert.*;
import java.io.File;
@@ -115,7 +115,7 @@ public class DBTraceAddressSnapRangePropertyMapSpaceTest
@Override
protected void clearCache(boolean all) {
try (LockHold hold = LockHold.lock(rwLock.writeLock())) {
try (LockHold hold = LockHold.lock(lock.writeLock())) {
// TODO: Should each space have an invalidateCache method?
super.clearCache(all);
try {
@@ -1,35 +0,0 @@
/* ###
* 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.util;
public class GhidraLockHold implements AutoCloseable {
public static GhidraLockHold lock(Lock lock) {
GhidraLockHold hold = new GhidraLockHold(lock);
hold.lock.acquire();
return hold;
}
private Lock lock;
protected GhidraLockHold(Lock lock) {
this.lock = lock;
}
@Override
public void close() {
this.lock.release();
}
}
@@ -19,7 +19,7 @@ import java.io.IOException;
import java.util.List;
import db.DBRecord;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.util.LockHold;
import ghidra.util.database.DBCachedObjectStoreFactory.DBFieldCodec;
import ghidra.util.database.annot.DBAnnotatedObjectInfo;
@@ -116,7 +116,7 @@ import ghidra.util.database.annot.DBAnnotatedObjectInfo;
* Note that there is no way to specify the primary key. For object stores, the primary key is
* always the object id, and its type is always {@code long}.
*/
public class DBAnnotatedObject extends DatabaseObject {
public class DBAnnotatedObject extends DbObject {
private final DBCachedObjectStore<?> store;
private final DBCachedDomainObjectAdapter adapter;
private final List<DBFieldCodec<?, ?, ?>> codecs; // The codecs, ordered by field
@@ -131,7 +131,7 @@ public class DBAnnotatedObject extends DatabaseObject {
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected DBAnnotatedObject(DBCachedObjectStore<?> store, DBRecord record) {
super(store == null ? null : store.cache, record == null ? -1 : record.getKey());
super(record == null ? -1 : record.getKey());
this.store = store;
this.record = record;
if (store != null) {
@@ -15,14 +15,12 @@
*/
package ghidra.util.database;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import db.DBHandle;
import ghidra.framework.data.DBDomainObjectSupport;
import ghidra.framework.data.OpenMode;
import ghidra.util.Msg;
import ghidra.util.Swing;
import ghidra.util.*;
import ghidra.util.task.TaskMonitor;
/**
@@ -85,89 +83,12 @@ public abstract class DBCachedDomainObjectAdapter extends DBDomainObjectSupport
}
}
/**
* Adapts a {@link ghidra.util.Lock} to the {@link Lock} interface
*
* <p>
* Not all operations are supported. In particular, no {@link #lockInterruptibly()},
* {@link #tryLock(long,TimeUnit)}, nor {@link #newCondition()}.
*/
static class GhidraLockWrappingLock implements Lock {
private final ghidra.util.Lock ghidraLock;
public GhidraLockWrappingLock(ghidra.util.Lock ghidraLock) {
this.ghidraLock = ghidraLock;
}
@Override
public void lock() {
ghidraLock.acquire();
}
@Override
public void lockInterruptibly() throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public boolean tryLock() {
synchronized (ghidraLock) { // Yes, sync on the lock's intrinsic lock
Thread lockOwner = ghidraLock.getOwner();
if (lockOwner == null || lockOwner == Thread.currentThread()) {
ghidraLock.acquire();
return true;
}
return false;
}
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public void unlock() {
ghidraLock.release();
}
@Override
public Condition newCondition() {
throw new UnsupportedOperationException();
}
}
/**
* Not a true read-write lock, but adapts a {@link ghidra.util.Lock} to the
* {@link ReadWriteLock} interface. The read lock and the write lock are just the same lock
*/
static class GhidraLockWrappingRWLock implements ReadWriteLock {
private final GhidraLockWrappingLock oneLock;
public GhidraLockWrappingRWLock(ghidra.util.Lock ghidraLock) {
this.oneLock = new GhidraLockWrappingLock(ghidraLock);
}
@Override
public Lock readLock() {
return oneLock;
}
@Override
public Lock writeLock() {
return oneLock;
}
}
protected ReadWriteLock rwLock;
/**
* @see DBDomainObjectSupport
*/
protected DBCachedDomainObjectAdapter(DBHandle dbh, OpenMode openMode, TaskMonitor monitor,
String name, int timeInterval, int bufSize, Object consumer) {
super(dbh, openMode, monitor, name, timeInterval, bufSize, consumer);
this.rwLock = new GhidraLockWrappingRWLock(lock);
}
/**
@@ -175,7 +96,7 @@ public abstract class DBCachedDomainObjectAdapter extends DBDomainObjectSupport
*
* @return the lock
*/
public ReadWriteLock getReadWriteLock() {
return rwLock;
public Lock getReadWriteLock() {
return lock;
}
}
@@ -4,9 +4,9 @@
* 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.
@@ -28,7 +28,7 @@ import org.apache.commons.lang3.tuple.ImmutablePair;
import db.*;
import db.util.ErrorHandler;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DbCache;
import ghidra.program.model.address.KeyRange;
import ghidra.util.LockHold;
import ghidra.util.database.DBCachedObjectStoreFactory.DBFieldCodec;
@@ -41,7 +41,7 @@ import ghidra.util.database.annot.DBAnnotatedField;
* <p>
* Essentially, this provides object-based accessed to records in the table via DAOs. See
* {@link DBAnnotatedObject} for further documentation including an example object definition. The
* store keeps a cache of objects using {@link DBObjectCache}. See
* store keeps a cache of objects using {@link DbCache}. See
* {@link DBCachedObjectStoreFactory} for documentation describing how to create a store, including
* for the example object definition.
*
@@ -200,7 +200,7 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa
}
E get(long key) throws IOException {
T cached = cache.get(key);
T cached = cache.getCachedInstance(key);
if (cached != null) {
return fromObject(cached);
}
@@ -580,7 +580,7 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa
@Override
boolean typedContains(Long u) throws IOException {
if (cache.get(u) != null) {
if (cache.getCachedInstance(u) != null) {
return true;
}
return table.hasRecord(u);
@@ -619,11 +619,12 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa
if (record == null) {
return null;
}
T cached = cache.get(record);
T cached = cache.getCachedInstance(record);
if (cached != null) {
return cached;
}
T found = factory.create(DBCachedObjectStore.this, record);
cache.add(found);
found.doRefresh(record);
return found;
}
@@ -747,7 +748,7 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa
final DBCachedDomainObjectAdapter adapter;
final DBHandle dbh;
final DBObjectCache<T> cache;
final DbCache<T> cache;
private final Class<T> objectType;
private final DBAnnotatedObjectFactory<T> factory;
private final String tableName;
@@ -783,8 +784,9 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa
this.table = table;
this.tableName = table.getName();
this.schema = table.getSchema();
this.cache = new DBObjectCache<>(1000); // TODO: Parameterize this?
this.lock = adapter.getReadWriteLock();
this.cache = new DbCache<T>(new DebuggerFactoryAdapter<>(this, table, factory),
adapter.getReadWriteLock(), 1000);
this.codecs = DBCachedObjectStoreFactory.getCodecs(objectType);
this.asForwardMap = new DBCachedObjectStoreMap<>(this, adapter, lock, Direction.FORWARD);
@@ -923,11 +925,12 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa
}
}
protected T doCreate(long key) throws IOException {
private T doCreate(long key) throws IOException {
DBRecord rec = schema.createRecord(key);
table.putRecord(rec);
T created = factory.create(this, rec);
created.fresh(true);
cache.add(created);
created.doUpdateAll();
return created;
}
@@ -1224,7 +1227,7 @@ public class DBCachedObjectStore<T extends DBAnnotatedObject> implements ErrorHa
* @return true if cached
*/
boolean isCached(long key) {
return cache.get(key) != null;
return cache.getCachedInstance(key) != null;
}
/**
@@ -0,0 +1,66 @@
/* ###
* 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.util.database;
import java.io.IOException;
import db.DBRecord;
import db.Table;
import ghidra.program.database.DbCache;
import ghidra.program.database.DbFactory;
/**
* Maps {@link DBAnnotatedObjectFactory} objects into {@link DbFactory} objects that is needed by
* the {@link DbCache}.
*
* @param <T> The DBAnnotatedObject type
*/
public class DebuggerFactoryAdapter<T extends DBAnnotatedObject> implements DbFactory<T> {
private DBCachedObjectStore<T> store;
private Table table;
private DBAnnotatedObjectFactory<T> factory;
public DebuggerFactoryAdapter(DBCachedObjectStore<T> store, Table table,
DBAnnotatedObjectFactory<T> factory) {
this.store = store;
this.table = table;
this.factory = factory;
}
@Override
public T instantiate(long key) {
try {
DBRecord record = table.getRecord(key);
return instantiate(record);
}
catch (IOException e) {
store.dbError(e);
}
return null;
}
@Override
public T instantiate(DBRecord record) {
if (record == null) {
return null;
}
T t = factory.create(store, record);
t.refresh(record);
return t;
}
}
@@ -874,27 +874,31 @@ public abstract class AbstractConstraintsTree<
* {@link #checkDataIntegrity(DBTreeDataRecord)} instead of this method.
*/
public void checkIntegrity() {
// Before we visit, integrity check that cache. Visiting will affect cache.
for (Entry<Long, Collection<DR>> ent : cachedDataChildren.entrySet()) {
Set<DR> databasedChildren = new TreeSet<>(Comparator.comparing(DR::getKey));
// NOTE: Bypass the cache by using the variant with a key parameter
databasedChildren.addAll(getDataChildrenOf(ent.getKey()));
Set<DR> cachedChildren = new TreeSet<>(Comparator.comparing(DR::getKey));
cachedChildren.addAll(ent.getValue());
if (!databasedChildren.equals(cachedChildren)) {
throw new AssertionError("Cached children of node " + ent.getKey() +
" out of sync: cache=" + cachedChildren + " db=" + databasedChildren);
synchronized (cachedDataChildren) {
// Before we visit, integrity check that cache. Visiting will affect cache.
for (Entry<Long, Collection<DR>> ent : cachedDataChildren.entrySet()) {
Set<DR> databasedChildren = new TreeSet<>(Comparator.comparing(DR::getKey));
// NOTE: Bypass the cache by using the variant with a key parameter
databasedChildren.addAll(getDataChildrenOf(ent.getKey()));
Set<DR> cachedChildren = new TreeSet<>(Comparator.comparing(DR::getKey));
cachedChildren.addAll(ent.getValue());
if (!databasedChildren.equals(cachedChildren)) {
throw new AssertionError("Cached children of node " + ent.getKey() +
" out of sync: cache=" + cachedChildren + " db=" + databasedChildren);
}
}
}
for (Entry<Long, Collection<NR>> ent : cachedNodeChildren.entrySet()) {
Set<NR> databasedChildren = new TreeSet<>(Comparator.comparing(NR::getKey));
// NOTE: Bypass the cache by using the variant with a key parameter
databasedChildren.addAll(getNodeChildrenOf(ent.getKey()));
Set<NR> cachedChildren = new TreeSet<>(Comparator.comparing(NR::getKey));
cachedChildren.addAll(ent.getValue());
if (!databasedChildren.equals(cachedChildren)) {
throw new AssertionError("Cached children of node " + ent.getKey() +
" out of sync: cache=" + cachedChildren + " db=" + databasedChildren);
synchronized (cachedNodeChildren) {
for (Entry<Long, Collection<NR>> ent : cachedNodeChildren.entrySet()) {
Set<NR> databasedChildren = new TreeSet<>(Comparator.comparing(NR::getKey));
// NOTE: Bypass the cache by using the variant with a key parameter
databasedChildren.addAll(getNodeChildrenOf(ent.getKey()));
Set<NR> cachedChildren = new TreeSet<>(Comparator.comparing(NR::getKey));
cachedChildren.addAll(ent.getValue());
if (!databasedChildren.equals(cachedChildren)) {
throw new AssertionError("Cached children of node " + ent.getKey() +
" out of sync: cache=" + cachedChildren + " db=" + databasedChildren);
}
}
}
visit(null, new TreeRecordVisitor() {
@@ -43,7 +43,6 @@ public class BookmarkNavigator {
final static Icon WARNING_ICON = new GIcon("icon.plugin.bookmark.type.warning");
final static Icon ERROR_ICON = new GIcon("icon.plugin.bookmark.type.error");
final static Icon ANALYSIS_ICON = new GIcon("icon.plugin.bookmark.type.analysis");
final static Icon DEFAULT_ICON = new GIcon("icon.plugin.bookmark.type.default");
final static int NOTE_PRIORITY = MarkerService.BOOKMARK_PRIORITY;
final static int ERROR_PRIORITY = MarkerService.BOOKMARK_PRIORITY + BIG_CHANGE;
@@ -81,14 +80,6 @@ public class BookmarkNavigator {
}
Icon icon = bmt.getIcon();
if (icon == null) {
if (bookmarkManager.isDefinedType(type)) {
// This implies the client defined a type, but did not pass a valid icon. In this
// case we will show a special icon.
icon = DEFAULT_ICON;
}
}
Color color = bmt.getMarkerColor();
if (color == null) {
color = DEFAULT_COLOR;
@@ -21,7 +21,7 @@ import javax.help.UnsupportedOperationException;
import docking.widgets.OptionDialog;
import docking.widgets.fieldpanel.support.*;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.database.data.DataTypeUtilities;
import ghidra.program.model.data.*;
import ghidra.program.model.lang.InsufficientBytesException;
@@ -1443,7 +1443,7 @@ public abstract class CompEditorModel<T extends Composite> extends CompositeEdit
else {
// Check for managed datatype changing
DataType originalDt = originalDTM.getDataType(newPath);
if (!(originalDt instanceof DatabaseObject)) {
if (!(originalDt instanceof DbObject)) {
return;
}
DataType dt = viewDTM.findMyDataTypeFromOriginalID(originalDTM.getID(originalDt));
@@ -1532,7 +1532,7 @@ public abstract class CompEditorModel<T extends Composite> extends CompositeEdit
// undo transactions for the viewDTM. An editor save could generate quite a few with
// potentially many types getting changed by one change.
DataType changedDt = originalDTM.getDataType(path);
if (!(changedDt instanceof DatabaseObject)) {
if (!(changedDt instanceof DbObject)) {
return;
}
DataType viewDt =
@@ -22,7 +22,7 @@ import java.util.TreeSet;
import javax.help.UnsupportedOperationException;
import db.util.ErrorHandler;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.model.data.*;
import ghidra.program.model.lang.ProgramArchitecture;
import ghidra.util.Swing;
@@ -231,7 +231,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAl
return viewComposite;
}
DataType resolvedDt = super.resolve(dataType, handler);
if ((dataType instanceof DatabaseObject) && originalDTM.contains(dataType)) {
if ((dataType instanceof DbObject) && originalDTM.contains(dataType)) {
long originalId = originalDTM.getID(dataType);
long myId = getID(resolvedDt);
dataTypeIDMap.put(myId, originalId);
@@ -247,14 +247,14 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAl
throw new IllegalArgumentException("datatype is not from this manager");
}
if (existingViewDt instanceof DatabaseObject) {
if (existingViewDt instanceof DbObject) {
dataTypeIDMap.remove(getID(existingViewDt));
}
DataType newResolvedDt =
super.replaceDataType(existingViewDt, replacementDt, updateCategoryPath);
if (newResolvedDt instanceof DatabaseObject &&
if (newResolvedDt instanceof DbObject &&
replacementDt.getDataTypeManager() == originalDTM) {
long originalId = originalDTM.getID(replacementDt);
long myId = getID(newResolvedDt);
@@ -271,7 +271,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAl
throw new IllegalArgumentException("datatype is not from this manager");
}
if (existingViewDt instanceof DatabaseObject) {
if (existingViewDt instanceof DbObject) {
dataTypeIDMap.remove(getID(existingViewDt));
}
@@ -293,7 +293,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAl
Iterator<DataType> allDataTypes = getAllDataTypes();
while (allDataTypes.hasNext()) {
DataType dt = allDataTypes.next();
if (dt == viewComposite || !(dt instanceof DatabaseObject)) {
if (dt == viewComposite || !(dt instanceof DbObject)) {
continue;
}
@@ -396,7 +396,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAl
long id = orphanIds.removeFirst();
if (!hasParent(id)) {
DataType dt = getDataType(id);
if (dt instanceof DatabaseObject) {
if (dt instanceof DbObject) {
if (dt == viewComposite) {
continue;
@@ -485,7 +485,7 @@ public class CompositeViewerDataTypeManager<T extends Composite> extends StandAl
if (existingViewDt.getDataTypeManager() != this) {
throw new IllegalArgumentException("datatype is not from this manager");
}
if (!(existingViewDt instanceof DatabaseObject)) {
if (!(existingViewDt instanceof DbObject)) {
return false;
}
return dataTypeIDMap.getOriginalIDFromViewID(getID(existingViewDt)) != null;
@@ -36,7 +36,7 @@ import ghidra.app.plugin.core.stackeditor.StackFrameDataType.StackComponentWrapp
import ghidra.app.util.datatype.EmptyCompositeException;
import ghidra.framework.plugintool.Plugin;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.symbol.SourceType;
@@ -1125,7 +1125,7 @@ public class StackEditorModel extends CompositeEditorModel<StackFrameDataType> {
// Check for managed datatype changing
DataType originalDt = originalDTM.getDataType(newPath);
if (!(originalDt instanceof DatabaseObject)) {
if (!(originalDt instanceof DbObject)) {
return;
}
DataType dt = viewDTM.findMyDataTypeFromOriginalID(originalDTM.getID(originalDt));
@@ -1161,7 +1161,7 @@ public class StackEditorModel extends CompositeEditorModel<StackFrameDataType> {
}
DataType changedDt = originalDTM.getDataType(path);
if (!(changedDt instanceof DatabaseObject)) {
if (!(changedDt instanceof DbObject)) {
return;
}
DataType viewDt = viewDTM.findMyDataTypeFromOriginalID(originalDTM.getID(changedDt));
@@ -1250,7 +1250,7 @@ public class StackEditorModel extends CompositeEditorModel<StackFrameDataType> {
for (int i = comps.length - 1; i >= 0; i--) {
DataTypeComponent component = comps[i];
DataType compDt = component.getDataType();
if (compDt instanceof DatabaseObject) {
if (compDt instanceof DbObject) {
// NOTE: viewDTM only maps view-to-original IDs for DataTypeDB
long myId = viewDTM.getID(compDt);
if (viewDTM.findOriginalDataTypeFromMyID(myId) == null) {
@@ -28,7 +28,7 @@ import ghidra.app.util.DataTypeNamingUtil;
import ghidra.app.util.bin.format.dwarf.attribs.DWARFNumericAttribute;
import ghidra.app.util.bin.format.dwarf.expression.DWARFExpressionException;
import ghidra.app.util.bin.format.golang.rtti.types.GoKind;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.database.data.DataTypeUtilities;
import ghidra.program.model.data.*;
import ghidra.program.model.data.Enum;
@@ -247,7 +247,7 @@ public class DWARFDataTypeImporter {
* offset -> ddt
*/
private void recordTempDataType(DWARFDataType ddt) {
if (ddt.dataType instanceof DatabaseObject) {
if (ddt.dataType instanceof DbObject) {
// don't store info about types that are already in the database
return;
}
@@ -455,7 +455,6 @@ abstract class OperandFieldHelper extends FieldFactory {
if (separator != null) {
results.add(separator);
}
for (int opIndex = 0; opIndex < numOperands; opIndex++) {
OpInfo opInfo = new OpInfo(inst, opIndex);
addOperandElements(opInfo, results);
@@ -21,7 +21,7 @@ import javax.swing.*;
import org.junit.Test;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.model.data.*;
public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTest {
@@ -312,7 +312,7 @@ public class StructureEditorFlexAlignmentTest extends AbstractStructureEditorTes
private DataTypeComponent addFlexDataType(Structure struct, DataType dataType, String name,
String comment) {
ArrayDataType a = new ArrayDataType(dataType, 0, 1);
if (struct instanceof DatabaseObject) {
if (struct instanceof DbObject) {
DataTypeManager dtm = struct.getDataTypeManager();
return dtm.withTransaction("Add Flex Array", () -> struct.add(a, name, comment));
}
@@ -4,9 +4,9 @@
* 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.
@@ -17,31 +17,37 @@ package ghidra.program.database;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.*;
import org.junit.Before;
import org.junit.Test;
import db.*;
import ghidra.program.model.address.KeyRange;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
import ghidra.util.Lock;
/**
*/
public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {
private DBObjectCache<TestObj> cache = new DBObjectCache<TestObj>(100);
private DbCache<TestObj> cache;
private Lock lock = new Lock("test");
private Schema schema;
private Map<Long, DBRecord> database = new HashMap<>();
/**
* Constructor for ObjectCacheTest.
* @param arg0
*/
public ObjectCacheTest() {
super();
@Before
public void setUp() {
Field[] fields = { new IntField() };
String[] fieldNames = { "Value" };
schema = new Schema(1, "id", fields, fieldNames);
cache = new DbCache<TestObj>(new TestFactory(), lock, 100);
}
@Test
public void testSetInvalid() {
TestObj obj1 = getTestObj(1);
obj1.setInvalid();
public void testDeleted() {
TestObj obj1 = createTestObj(1, 1);
deleteTestObj(obj1);
try {
obj1.setValue(10);
fail("Should have thrown a concurrent modification excption here.");
@@ -53,8 +59,10 @@ public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {
@Test
public void testDeleteFromCacheSetsDeletedOnObject() {
TestObj obj1 = getTestObj(1);
TestObj obj1 = createTestObj(1, 1);
cache.delete(1);
assertFalse(obj1.isValid());
try {
obj1.setValue(10);
fail("Should have thrown a concurrent modification excption here.");
@@ -66,11 +74,12 @@ public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {
@Test
public void testDeleteUndoProblem() {
TestObj obj1 = getTestObj(1);
obj1.setInvalid();
obj1.checkIsValid();
TestObj obj1 = createTestObj(1, 1);
deleteTestObj(obj1);
assertFalse(obj1.refreshIfNeeded());
assertTrue(obj1.isDeleted(lock));
TestObj obj2 = getTestObj(1);
TestObj obj2 = createTestObj(1, 1);
assertTrue(obj1 != obj2);
obj1.getValue(); // previously, this would have caused obj2 to be removed
@@ -84,7 +93,7 @@ public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {
@Test
public void testDeleteRange() {
for (int i = 0; i < 10; i++) {
getTestObj(i);
createTestObj(i, i);
}
assertEquals(10, cache.size());
cache.delete(Arrays.asList(new KeyRange(4, 6)));
@@ -94,43 +103,105 @@ public class ObjectCacheTest extends AbstractGhidraHeadedIntegrationTest {
@Test
public void testDeleteBigRange() {
for (int i = 0; i < 10; i++) {
getTestObj(i);
createTestObj(i, i);
}
assertEquals(10, cache.size());
cache.delete(Arrays.asList(new KeyRange(2, 100)));
assertEquals(2, cache.size());
}
private TestObj getTestObj(int key) {
TestObj obj = cache.get(key);
if (obj == null) {
obj = new TestObj(cache, key);
@Test
public void testRefresh() {
TestObj obj1 = createTestObj(1, 1);
obj1.setInvalid();
assertFalse(obj1.isValid());
assertEquals(1, obj1.getValue());
assertTrue(obj1.isValid());
}
@Test
public void testInvalideCache() {
TestObj obj1 = createTestObj(1, 1);
TestObj obj2 = createTestObj(2, 2);
TestObj obj3 = createTestObj(3, 3);
assertTrue(obj1.isValid());
assertTrue(obj1.isValid());
assertTrue(obj1.isValid());
cache.invalidate();
assertFalse(obj1.isValid());
assertFalse(obj1.isValid());
assertFalse(obj1.isValid());
assertTrue(obj1 == getTestObj(1));
assertTrue(obj2 == getTestObj(2));
assertTrue(obj3 == getTestObj(3));
assertTrue(obj1.isValid());
assertTrue(obj1.isValid());
assertTrue(obj1.isValid());
}
private TestObj createTestObj(long key, int value) {
DBRecord record = schema.createRecord(key);
record.setIntValue(0, value);
database.put(key, record);
return cache.getCachedInstance(record);
}
private TestObj getTestObj(long key) {
return cache.getCachedInstance(key);
}
private void deleteTestObj(TestObj obj) {
database.remove(obj.getKey());
obj.setInvalid();
}
class TestFactory implements DbFactory<TestObj> {
@Override
public TestObj instantiate(long key) {
DBRecord record = database.get(key);
return instantiate(record);
}
return obj;
@Override
public TestObj instantiate(DBRecord record) {
return new TestObj(record);
}
}
class TestObj extends DbObject {
private DBRecord record;
TestObj(DBRecord record) {
super(record.getKey());
this.record = record;
}
public void setValue(int value) {
checkDeleted();
record.setIntValue(0, value);
}
public int getValue() {
refreshIfNeeded();
return record.getIntValue(0);
}
@Override
protected boolean refresh() {
DBRecord refreshedRecord = database.get(record.getKey());
if (refreshedRecord != null) {
record = refreshedRecord;
return true;
}
return false;
}
}
}
class TestObj extends DatabaseObject {
int value = -1;
TestObj(DBObjectCache<TestObj> cache, long key) {
super(cache, key);
}
public void setValue(int value) {
checkDeleted();
this.value = value;
}
public int getValue() {
checkIsValid();
return value;
}
@Override
protected boolean refresh() {
// return false to simulate the record has been deleted
return false;
}
}
@@ -4,9 +4,9 @@
* 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.
@@ -20,7 +20,7 @@ import static org.junit.Assert.*;
import org.junit.*;
import ghidra.docking.settings.Settings;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.database.ProgramBuilder;
import ghidra.program.database.data.PointerTypedefInspector;
import ghidra.program.model.address.AddressSpace;
@@ -96,7 +96,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationT
assertEquals("char * " + formatAttributes("offset(0x8)"), dt.getName());
DataType dbDt = dtm.resolve(dt, null);
assertTrue(dbDt instanceof TypeDef);
assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB
assertTrue(dbDt instanceof DbObject); // transforms to TypedefDB
assertTrue(dt.isEquivalent(dbDt));
assertEquals(dt.getName(), dbDt.getName());
@@ -112,7 +112,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationT
assertEquals("char * " + formatAttributes("relative"), dt.getName());
dbDt = dtm.resolve(dt, null);
assertTrue(dbDt instanceof TypeDef);
assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB
assertTrue(dbDt instanceof DbObject); // transforms to TypedefDB
assertTrue(dt.isEquivalent(dbDt));
assertEquals(dt.getName(), dbDt.getName());
@@ -131,7 +131,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationT
assertEquals("char *16 " + formatAttributes("space(register)"), dt.getName());
DataType dbDt = dtm.resolve(dt, null);
assertTrue(dbDt instanceof TypeDef);
assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB
assertTrue(dbDt instanceof DbObject); // transforms to TypedefDB
assertTrue(dt.isEquivalent(dbDt));
assertEquals(dt.getName(), dbDt.getName());
@@ -147,7 +147,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationT
assertEquals("char *32 " + formatAttributes("space(register)"), dt.getName());
dbDt = dtm.resolve(dt, null);
assertTrue(dbDt instanceof TypeDef);
assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB
assertTrue(dbDt instanceof DbObject); // transforms to TypedefDB
assertTrue(dt.isEquivalent(dbDt));
assertEquals(dt.getName(), dbDt.getName());
@@ -169,7 +169,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationT
assertEquals("foo *16 " + formatAttributes("space(register)"), dt.getName());
DataType dbDt = dtm.resolve(dt, null);
assertTrue(dbDt instanceof TypeDef);
assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB
assertTrue(dbDt instanceof DbObject); // transforms to TypedefDB
assertTrue(dt.isEquivalent(dbDt));
assertEquals(dt.getName(), dbDt.getName());
@@ -244,7 +244,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationT
assertEquals(2, dt.getLength());
assertEquals("foo *16 " + formatAttributes("space(register)"), dt.getName());
TypeDef dbDt = (TypeDef) dtm.resolve(dt, null);
assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB
assertTrue(dbDt instanceof DbObject); // transforms to TypedefDB
assertTrue(dt.isEquivalent(dbDt));
assertEquals(dt.getName(), dbDt.getName());
@@ -252,7 +252,8 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationT
assertTrue(dbDt == dtm.resolve(dt, null)); // should resolve to same instance
dt = (TypeDef) dt.copy(dtm);
PointerTypeSettingsDefinition.DEF.setType(dt.getDefaultSettings(), PointerType.IMAGE_BASE_RELATIVE);
PointerTypeSettingsDefinition.DEF.setType(dt.getDefaultSettings(),
PointerType.IMAGE_BASE_RELATIVE);
TypeDef dbDt2 = (TypeDef) dtm.resolve(dt, null); // should resolve to new instance
assertTrue(dbDt != dbDt2);
assertEquals("foo *16 " + formatAttributes("image-base-relative,space(register)"),
@@ -276,7 +277,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationT
assertEquals(2, dt.getLength());
assertEquals("foo *16 " + formatAttributes("space(register)"), dt.getName());
TypeDef dbDt = (TypeDef) dtm.resolve(dt, null);
assertTrue(dbDt instanceof DatabaseObject); // transforms to TypedefDB
assertTrue(dbDt instanceof DbObject); // transforms to TypedefDB
assertTrue(dt.isEquivalent(dbDt));
assertEquals(dt.getName(), dbDt.getName());
@@ -290,7 +291,7 @@ public class PointerTypedefDataTypeTest extends AbstractGhidraHeadedIntegrationT
assertTrue(dbDt != dbDt2);
assertFalse(dbDt.isEquivalent(dbDt2));
assertFalse(dbDt2.isEquivalent(dbDt));
assertTrue(dbDt2 instanceof DatabaseObject); // transforms to TypedefDB
assertTrue(dbDt2 instanceof DbObject); // transforms to TypedefDB
assertTrue(dt2.isEquivalent(dbDt2));
assertEquals(dt2.getName(), dbDt2.getName());
@@ -37,6 +37,7 @@ import ghidra.program.model.util.CodeUnitInsertionException;
import ghidra.program.model.util.PropertyMap;
import ghidra.test.ToyProgramBuilder;
import ghidra.util.Lock;
import ghidra.util.Lock.Closeable;
import ghidra.util.SaveableColor;
import ghidra.util.exception.NoValueException;
import ghidra.util.task.TaskMonitor;
@@ -992,8 +993,7 @@ public class CodeManagerTest extends AbstractGenericTest {
ProgramDB pdb = (ProgramDB) program;
Lock lock = pdb.getLock();
lock.acquire();
try {
try (Closeable c = lock.write()) {
Instruction instructionAt = pdb.getListing().getInstructionAt(addr(0x2000));
assertEquals(FlowOverride.NONE, instructionAt.getFlowOverride());
@@ -1007,9 +1007,6 @@ public class CodeManagerTest extends AbstractGenericTest {
instructionAt = pdb.getListing().getInstructionAt(addr(0x2000));
assertEquals(FlowOverride.CALL, instructionAt.getFlowOverride());
}
finally {
lock.release();
}
}
@Test
@@ -1020,9 +1017,7 @@ public class CodeManagerTest extends AbstractGenericTest {
ProgramDB pdb = (ProgramDB) program;
Lock lock = pdb.getLock();
lock.acquire();
try {
try (Closeable c = lock.write()) {
Instruction instructionAt = pdb.getListing().getInstructionAt(addr(0x2000));
assertEquals(FlowOverride.NONE, instructionAt.getFlowOverride());
@@ -1031,9 +1026,6 @@ public class CodeManagerTest extends AbstractGenericTest {
instructionAt = pdb.getListing().getInstructionAt(addr(0x2002));
assertEquals(null, instructionAt);
}
finally {
lock.release();
}
}
private Address addr(long l) {
@@ -4,9 +4,9 @@
* 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.
@@ -79,12 +79,12 @@ public class DefaultDataCacheTest extends AbstractGenericTest {
assertTrue(cu instanceof Data);
DataDB data = (DataDB) cu;
assertTrue(!data.isDefined());
assertTrue(!((Boolean) invokeInstanceMethod("isInvalid", data)));
assertFalse((Boolean) invokeInstanceMethod("needsRefreshing", data));
AddressSet restrictedSet = new AddressSet(addr(0x1000), addr(0x1003));
Disassembler disassembler = Disassembler.getDisassembler(program, TaskMonitor.DUMMY, null);
AddressSetView disAddrs = disassembler.disassemble(addr(0x1000), restrictedSet);
assertTrue(!disAddrs.isEmpty());
assertTrue(!((Boolean) invokeInstanceMethod("checkIsValid", data)));
assertFalse((Boolean) invokeInstanceMethod("needsRefreshing", data));
assertNull(listing.getCodeUnitAt(addr(0x1001)));
}
@@ -4,9 +4,9 @@
* 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.
@@ -19,14 +19,13 @@ import static ghidra.feature.fid.db.FunctionsTable.*;
import db.DBRecord;
import ghidra.feature.fid.hash.FidHashQuad;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.util.NumericUtilities;
/**
* Represents a function record in the FID database.
*/
public class FunctionRecord extends DatabaseObject implements FidHashQuad {
public class FunctionRecord extends DbObject implements FidHashQuad {
public final static int HAS_TERMINATOR_FLAG = 1;
public final static int AUTO_PASS_FLAG = 2;
public final static int AUTO_FAIL_FLAG = 4;
@@ -46,11 +45,10 @@ public class FunctionRecord extends DatabaseObject implements FidHashQuad {
/**
* Package private constructor, to be called from FunctionsTable exclusively.
* @param fid database (for string references)
* @param cache FunctionRecord object cache
* @param record record for this function
*/
FunctionRecord(FidDB fid, DBObjectCache<FunctionRecord> cache, DBRecord record) {
super(cache, record.getKey());
FunctionRecord(FidDB fid, DBRecord record) {
super(record.getKey());
this.record = record;
this.fidDb = fid;
}
@@ -169,6 +167,7 @@ public class FunctionRecord extends DatabaseObject implements FidHashQuad {
byte val = record.getByteValue(FLAGS_COL);
return ((val & FORCE_RELATION_FLAG) != 0);
}
/**
* Returns the record id (primary key).
* @return the record id
@@ -4,9 +4,9 @@
* 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.
@@ -22,7 +22,9 @@ import java.util.regex.Pattern;
import db.*;
import ghidra.feature.fid.hash.FidHashQuad;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DbFactory;
import ghidra.program.database.DbCache;
import ghidra.util.Lock;
import ghidra.util.UniversalIdGenerator;
/**
@@ -62,20 +64,19 @@ public class FunctionsTable {
Table table;
FidDB fidDb;
StringsTable stringsTable;
DBObjectCache<FunctionRecord> functionCache;
DbCache<FunctionRecord> functionCache;
/**
* Creates or attaches a functions table.
* @param fid the fid database
* @param handle database handle
* @param stringsTable strings table (must be created first!)
* @param create whether to create or just attach
* @throws IOException if create fails
*/
public FunctionsTable(FidDB fid, DBHandle handle) throws IOException {
table = handle.getTable(FUNCTIONS_TABLE);
this.fidDb = fid;
this.stringsTable = fid.getStringsTable();
functionCache = new DBObjectCache<>(CACHE_SIZE);
functionCache = new DbCache<>(new FunctionRecordFactory(), new Lock("Fid"), CACHE_SIZE);
}
public static void createTable(DBHandle handle) throws IOException {
@@ -116,11 +117,7 @@ public class FunctionsTable {
if (record.getLongValue(SPECIFIC_HASH_COL) != hash) {
continue;
}
FunctionRecord functionRecord = functionCache.get(record);
if (functionRecord == null) {
functionRecord = new FunctionRecord(fidDb, functionCache, record);
}
list.add(functionRecord);
list.add(functionCache.getCachedInstance(record));
}
return list;
}
@@ -141,12 +138,10 @@ public class FunctionsTable {
List<FunctionRecord> list = new ArrayList<>();
while (iterator.hasNext()) {
Field key = iterator.next();
FunctionRecord functionRecord = functionCache.get(key.getLongValue());
if (functionRecord == null) {
DBRecord record = table.getRecord(key);
functionRecord = new FunctionRecord(fidDb, functionCache, record);
FunctionRecord functionRecord = functionCache.getCachedInstance(key.getLongValue());
if (functionRecord != null) {
list.add(functionRecord);
}
list.add(functionRecord);
}
return list;
}
@@ -179,7 +174,8 @@ public class FunctionsTable {
byte flags = (byte) (hasTerminator ? FunctionRecord.HAS_TERMINATOR_FLAG : 0);
record.setByteValue(FLAGS_COL, flags);
table.putRecord(record);
FunctionRecord functionRecord = new FunctionRecord(fidDb, functionCache, record);
FunctionRecord functionRecord = new FunctionRecord(fidDb, record);
functionCache.add(functionRecord);
return functionRecord;
}
@@ -188,7 +184,7 @@ public class FunctionsTable {
* @param functionID is the id of the function record to modify
* @param flagMask is the bit to modify
* @param value is true to set, false to clear
* @throws IOException
* @throws IOException if error occurs while reading database
*/
void modifyFlags(long functionID, int flagMask, boolean value) throws IOException {
DBRecord record = table.getRecord(functionID);
@@ -217,30 +213,12 @@ public class FunctionsTable {
*/
public List<FunctionRecord> getFunctionRecordsByNameSubstring(String nameSearch)
throws IOException {
DBFieldIterator iterator = table.indexKeyIterator(NAME_ID_COL);
if (!iterator.hasNext()) {
return Collections.emptyList();
}
List<FunctionRecord> list = new ArrayList<>();
RecordIterator iterator = table.iterator();
while (iterator.hasNext()) {
Field key = iterator.next();
FunctionRecord functionRecord = functionCache.get(key.getLongValue());
if (functionRecord == null) {
DBRecord record = table.getRecord(key);
long nameID = record.getLongValue(NAME_ID_COL);
StringRecord nameRecord = stringsTable.lookupString(nameID);
String name = nameRecord.getValue();
if (name.contains(nameSearch)) {
functionRecord = new FunctionRecord(fidDb, functionCache, record);
}
}
else {
if (!functionRecord.getName().contains(nameSearch)) {
functionRecord = null;
}
}
if (functionRecord != null) {
DBRecord record = iterator.next();
FunctionRecord functionRecord = functionCache.getCachedInstance(record);
if (functionRecord.getName().contains(nameSearch)) {
list.add(functionRecord);
}
}
@@ -256,32 +234,14 @@ public class FunctionsTable {
*/
public List<FunctionRecord> getFunctionRecordsByNameRegex(String regex) throws IOException {
Matcher matcher = Pattern.compile(regex).matcher("");
DBFieldIterator iterator = table.indexKeyIterator(NAME_ID_COL);
if (!iterator.hasNext()) {
return Collections.emptyList();
}
List<FunctionRecord> list = new ArrayList<>();
RecordIterator iterator = table.iterator();
while (iterator.hasNext()) {
Field key = iterator.next();
FunctionRecord functionRecord = functionCache.get(key.getLongValue());
if (functionRecord == null) {
DBRecord record = table.getRecord(key);
long nameID = record.getLongValue(NAME_ID_COL);
StringRecord nameRecord = stringsTable.lookupString(nameID);
String name = nameRecord.getValue();
matcher.reset(name);
if (matcher.matches()) {
functionRecord = new FunctionRecord(fidDb, functionCache, record);
}
}
else {
matcher.reset(functionRecord.getName());
if (!matcher.matches()) {
functionRecord = null;
}
}
if (functionRecord != null) {
DBRecord record = iterator.next();
FunctionRecord functionRecord = functionCache.getCachedInstance(record);
matcher.reset(functionRecord.getName());
if (matcher.matches()) {
list.add(functionRecord);
}
}
@@ -295,14 +255,7 @@ public class FunctionsTable {
* @throws IOException if database seek encounters an error
*/
public FunctionRecord getFunctionByID(long functionID) throws IOException {
FunctionRecord functionRecord = functionCache.get(functionID);
if (functionRecord == null) {
DBRecord record = table.getRecord(functionID);
if (record != null) {
functionRecord = new FunctionRecord(fidDb, functionCache, record);
}
}
return functionRecord;
return functionCache.getCachedInstance(functionID);
}
/**
@@ -323,11 +276,7 @@ public class FunctionsTable {
StringRecord domainPathRecord = stringsTable.lookupString(domainPathID);
String domainPath = domainPathRecord.getValue();
if (domainPath.contains(domainPathSearch)) {
FunctionRecord functionRecord = functionCache.get(record);
if (functionRecord == null) {
functionRecord = new FunctionRecord(fidDb, functionCache, record);
}
list.add(functionRecord);
list.add(functionCache.getCachedInstance(record));
}
}
return list;
@@ -356,20 +305,31 @@ public class FunctionsTable {
List<FunctionRecord> list = new ArrayList<>();
while (iterator.hasNext()) {
Field key = iterator.next();
FunctionRecord functionRecord = functionCache.get(key.getLongValue());
if (functionRecord == null) {
DBRecord record = table.getRecord(key);
if (record.getLongValue(LIBRARY_ID_COL) == libraryKey) {
functionRecord = new FunctionRecord(fidDb, functionCache, record);
list.add(functionRecord);
}
}
else {
if (functionRecord.getLibraryID() == libraryKey) {
list.add(functionRecord);
}
FunctionRecord functionRecord = functionCache.getCachedInstance(key.getLongValue());
if (functionRecord.getLibraryID() == libraryKey) {
list.add(functionRecord);
}
}
return list;
}
class FunctionRecordFactory implements DbFactory<FunctionRecord> {
@Override
public FunctionRecord instantiate(long key) {
try {
DBRecord record = table.getRecord(key);
return record == null ? null : instantiate(record);
}
catch (IOException e) {
throw new RuntimeException("serious delayed database access error", e);
}
}
@Override
public FunctionRecord instantiate(DBRecord record) {
return new FunctionRecord(fidDb, record);
}
}
}
@@ -4,9 +4,9 @@
* 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.
@@ -15,13 +15,12 @@
*/
package ghidra.feature.fid.db;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
/**
* A string record in the FID database.
*/
public class StringRecord extends DatabaseObject {
public class StringRecord extends DbObject {
/**
* The value of the string.
*/
@@ -29,12 +28,11 @@ public class StringRecord extends DatabaseObject {
/**
* Constructor with the primary key and the string value.
* @param cache StringRecord object cache
* @param key primary key
* @param value the string value
*/
public StringRecord(DBObjectCache<StringRecord> cache, long key, String value) {
super(cache, key);
public StringRecord(long key, String value) {
super(key);
this.value = value;
}
@@ -4,9 +4,9 @@
* 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.
@@ -18,7 +18,9 @@ package ghidra.feature.fid.db;
import java.io.IOException;
import db.*;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DbFactory;
import ghidra.program.database.DbCache;
import ghidra.util.Lock;
import ghidra.util.UniversalIdGenerator;
/**
@@ -43,17 +45,16 @@ public class StringsTable {
static int[] INDEXED_COLUMNS = new int[] { STRING_VALUE_COL };
Table table;
DBObjectCache<StringRecord> stringCache;
DbCache<StringRecord> stringCache;
/**
* Creates or attaches the string table.
* @param handle the database handle
* @param create whether to create or attach
* @throws IOException if the database has a problem
*/
public StringsTable(DBHandle handle) throws IOException {
table = handle.getTable(STRINGS_TABLE);
stringCache = new DBObjectCache<>(CACHE_SIZE);
stringCache = new DbCache<>(new StringRecordFactory(), new Lock("Fid"), CACHE_SIZE);
}
public static void createTable(DBHandle handle) throws IOException {
@@ -99,20 +100,25 @@ public class StringsTable {
* @return the string record, or null if no such key
*/
StringRecord lookupString(long stringID) {
StringRecord stringRecord = stringCache.get(stringID);
if (stringRecord == null) {
DBRecord record;
return stringCache.getCachedInstance(stringID);
}
class StringRecordFactory implements DbFactory<StringRecord> {
@Override
public StringRecord instantiate(long key) {
try {
record = table.getRecord(stringID);
if (record != null) {
stringRecord =
new StringRecord(stringCache, stringID, record.getString(STRING_VALUE_COL));
}
DBRecord record = table.getRecord(key);
return record == null ? null : instantiate(record);
}
catch (IOException e) {
throw new RuntimeException("serious delayed database access error", e);
}
}
return stringRecord;
@Override
public StringRecord instantiate(DBRecord record) {
return new StringRecord(record.getKey(), record.getString(STRING_VALUE_COL));
}
}
}
@@ -4,9 +4,9 @@
* 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.
@@ -25,9 +25,11 @@ import ghidra.feature.vt.api.impl.*;
import ghidra.feature.vt.api.main.*;
import ghidra.feature.vt.api.util.VTAssociationStatusException;
import ghidra.framework.data.OpenMode;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DbFactory;
import ghidra.program.database.DbCache;
import ghidra.program.model.address.Address;
import ghidra.util.Lock;
import ghidra.util.Lock.Closeable;
import ghidra.util.exception.*;
import ghidra.util.task.TaskLauncher;
import ghidra.util.task.TaskMonitor;
@@ -39,9 +41,9 @@ public class AssociationDatabaseManager implements VTAssociationManager {
private VTAssociationTableDBAdapter associationTableAdapter;
private VTMatchMarkupItemTableDBAdapter markupItemTableAdapter;
private DBObjectCache<MarkupItemStorageDB> markupItemCache;
private DbCache<MarkupItemStorageDB> markupItemCache;
private List<AssociationHook> associationHooks = new ArrayList<>();
private DBObjectCache<VTAssociationDB> associationCache;
private DbCache<VTAssociationDB> associationCache;
private AcceptedStatusCache acceptedStatusCache = new AcceptedStatusCache();
Lock lock;
@@ -67,8 +69,8 @@ public class AssociationDatabaseManager implements VTAssociationManager {
AssociationDatabaseManager(VTSessionDB session) {
this.session = session;
lock = session.getLock();
associationCache = new DBObjectCache<>(10);
markupItemCache = new DBObjectCache<>(10);
associationCache = new DbCache<>(new AssociationFactory(), lock, 10);
markupItemCache = new DbCache<>(new MarkupFactory(), lock, 10);
}
/**
@@ -84,8 +86,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {
Collection<MarkupItemStorageDB> items = new ArrayList<>();
VTAssociationDB associationDB = (VTAssociationDB) association;
try {
lock.acquire();
try (Closeable c = lock.read()) {
int recordCount = markupItemTableAdapter.getRecordCount();
if (recordCount == 0) {
recordCount = 1; // to give the appearance of progress
@@ -98,7 +99,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {
while (records.hasNext()) {
monitor.checkCancelled();
DBRecord record = records.next();
items.add(getMarkupItemForRecord(record));
items.add(markupItemCache.getCachedInstance(record));
monitor.incrementProgress(1);
}
@@ -107,9 +108,6 @@ public class AssociationDatabaseManager implements VTAssociationManager {
catch (IOException e) {
session.dbError(e);
}
finally {
lock.release();
}
return items;
}
@@ -148,20 +146,6 @@ public class AssociationDatabaseManager implements VTAssociationManager {
}
}
private MarkupItemStorageDB getMarkupItemForRecord(DBRecord markupItemRecord) {
try {
lock.acquire();
MarkupItemStorageDB markupItem = markupItemCache.get(markupItemRecord);
if (markupItem == null) {
markupItem = new MarkupItemStorageDB(markupItemRecord, markupItemCache, this);
}
return markupItem;
}
finally {
lock.release();
}
}
Address getDestinationAddressFromLong(long longValue) {
return session.getDestinationAddressFromLong(longValue);
}
@@ -188,7 +172,8 @@ public class AssociationDatabaseManager implements VTAssociationManager {
try {
DBRecord record = markupItemTableAdapter.createMarkupItemRecord(markupItem);
MarkupItemStorageDB appliedMarkupItem = getMarkupItemForRecord(record);
MarkupItemStorageDB appliedMarkupItem = new MarkupItemStorageDB(record, this);
markupItemCache.add(appliedMarkupItem);
return appliedMarkupItem;
}
catch (IOException e) {
@@ -213,18 +198,15 @@ public class AssociationDatabaseManager implements VTAssociationManager {
boolean isBlocked = isBlocked(sourceAddress, destinationAddress);
VTAssociationDB newAssociation = null;
try {
lock.acquire();
try (Closeable c = lock.write()) {
DBRecord record = associationTableAdapter.insertRecord(sourceLong, destinationLong,
type, isBlocked ? BLOCKED : AVAILABLE, 0);
newAssociation = new VTAssociationDB(this, associationCache, record);
newAssociation = new VTAssociationDB(this, record);
associationCache.add(newAssociation);
}
catch (IOException e) {
session.dbError(e);
}
finally {
lock.release();
}
session.setChanged(VTEvent.ASSOCIATION_ADDED, null, newAssociation);
return newAssociation;
}
@@ -274,33 +256,28 @@ public class AssociationDatabaseManager implements VTAssociationManager {
@Override
public List<VTAssociation> getAssociations() {
List<VTAssociation> list = new ArrayList<>();
lock.acquire();
try {
try (Closeable c = lock.read()) {
RecordIterator iterator = associationTableAdapter.getRecords();
for (; iterator.hasNext();) {
DBRecord nextRecord = iterator.next();
list.add(getAssociationForRecord(nextRecord));
list.add(associationCache.getCachedInstance(nextRecord));
}
}
catch (IOException e) {
session.dbError(e);
}
finally {
lock.release();
}
return list;
}
@Override
public VTAssociation getAssociation(Address sourceAddress, Address destinationAddress) {
lock.acquire();
try {
try (Closeable c = lock.read()) {
long addressKey = session.getLongFromSourceAddress(sourceAddress);
RecordIterator iterator =
associationTableAdapter.getRecordsForSourceAddress(addressKey);
while (iterator.hasNext()) {
DBRecord record = iterator.next();
VTAssociationDB associationDB = getAssociationForRecord(record);
VTAssociationDB associationDB = associationCache.getCachedInstance(record);
if (associationDB.getDestinationAddress().equals(destinationAddress)) {
return associationDB;
}
@@ -309,9 +286,6 @@ public class AssociationDatabaseManager implements VTAssociationManager {
catch (IOException e) {
session.dbError(e);
}
finally {
lock.release();
}
return null;
}
@@ -323,7 +297,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {
associationTableAdapter.getRecordsForSourceAddress(addressKey);
while (iterator.hasNext()) {
DBRecord record = iterator.next();
VTAssociationDB associationDB = getAssociationForRecord(record);
VTAssociationDB associationDB = associationCache.getCachedInstance(record);
Address dbDestinatonAddress = associationDB.getDestinationAddress();
if (destinationAddress.equals(dbDestinatonAddress)) {
return associationDB;
@@ -337,99 +311,51 @@ public class AssociationDatabaseManager implements VTAssociationManager {
return null;
}
private VTAssociationDB getAssociationForRecord(DBRecord record) {
if (record == null) {
throw new AssertException("How can we have a null record?!!!");
}
try {
lock.acquire();
VTAssociationDB associationDB = associationCache.get(record);
if (associationDB == null) {
associationDB = new VTAssociationDB(this, associationCache, record);
}
return associationDB;
}
finally {
lock.release();
}
}
VTAssociationDB getAssociation(long associationKey) {
try {
lock.acquire();
VTAssociationDB associationDB = associationCache.get(associationKey);
if (associationDB != null) {
return associationDB;
}
DBRecord record = associationTableAdapter.getRecord(associationKey);
if (record == null) {
return null;
}
return new VTAssociationDB(this, associationCache, record);
}
catch (IOException e) {
session.dbError(e);
}
finally {
lock.release();
}
return null;
}
public VTSessionDB getSession() {
return session;
}
@Override
public Collection<VTAssociation> getRelatedAssociationsBySourceAddress(Address sourceAddress) {
lock.acquire();
try {
try (Closeable c = lock.read()) {
long sourceId = session.getLongFromSourceAddress(sourceAddress);
Set<DBRecord> relatedRecords =
associationTableAdapter.getRelatedAssociationRecordsBySourceAddress(sourceId);
List<VTAssociation> associations = new ArrayList<>();
for (DBRecord record : relatedRecords) {
associations.add(getAssociationForRecord(record));
associations.add(associationCache.getCachedInstance(record));
}
return associations;
}
catch (IOException e) {
session.dbError(e);
}
finally {
lock.release();
}
return Collections.emptyList();
}
@Override
public Collection<VTAssociation> getRelatedAssociationsByDestinationAddress(
Address destinationAddress) {
lock.acquire();
try {
try (Closeable c = lock.read()) {
long destinationId = session.getLongFromDestinationAddress(destinationAddress);
Set<DBRecord> relatedRecords = associationTableAdapter
.getRelatedAssociationRecordsByDestinationAddress(destinationId);
List<VTAssociation> associations = new ArrayList<>();
for (DBRecord record : relatedRecords) {
associations.add(getAssociationForRecord(record));
associations.add(associationCache.getCachedInstance(record));
}
return associations;
}
catch (IOException e) {
session.dbError(e);
}
finally {
lock.release();
}
return Collections.emptyList();
}
@Override
public Collection<VTAssociation> getRelatedAssociationsBySourceAndDestinationAddress(
Address sourceAddress, Address destinationAddress) {
lock.acquire();
try {
try (Closeable c = lock.read()) {
long sourceId = session.getLongFromSourceAddress(sourceAddress);
long destinationId = session.getLongFromDestinationAddress(destinationAddress);
Set<DBRecord> relatedRecords =
@@ -437,16 +363,13 @@ public class AssociationDatabaseManager implements VTAssociationManager {
sourceId, destinationId);
List<VTAssociation> associations = new ArrayList<>();
for (DBRecord record : relatedRecords) {
associations.add(getAssociationForRecord(record));
associations.add(associationCache.getCachedInstance(record));
}
return associations;
}
catch (IOException e) {
session.dbError(e);
}
finally {
lock.release();
}
return Collections.emptyList();
}
@@ -564,7 +487,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {
sourceId, destinationId);
relatedRecords.remove(association.getRecord()); // don't change the given association
for (DBRecord record : relatedRecords) {
relatedAssociaitons.add(getAssociationForRecord(record));
relatedAssociaitons.add(associationCache.getCachedInstance(record));
}
}
catch (IOException e) {
@@ -581,7 +504,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {
session.dbError(e);
}
VTAssociationDB association = getAssociationForRecord(record);
VTAssociationDB association = associationCache.getCachedInstance(record);
Address sourceAddress = association.getSourceAddress();
Address destinationAddress = association.getDestinationAddress();
VTAssociationStatus status = association.getStatus();
@@ -619,6 +542,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {
void removeMarkupRecord(long key) {
try {
markupItemTableAdapter.removeMarkupItemRecord(key);
markupItemCache.delete(key);
}
catch (IOException e) {
session.dbError(e);
@@ -661,60 +585,43 @@ public class AssociationDatabaseManager implements VTAssociationManager {
private boolean disposed = false;
void dispose() {
lock.acquire();
try {
try (Closeable c = lock.write()) {
disposed = true;
invalidate();
}
finally {
lock.release();
}
}
void invalidate() {
lock.acquire();
try {
try (Closeable c = lock.write()) {
invalid = true;
acceptedSourceAssociations.clear();
acceptedDestinationAssociations.clear();
}
finally {
lock.release();
}
}
void remove(Address sourceAddress, Address destinationAddress) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
if (disposed || invalid) {
return;
}
acceptedSourceAssociations.remove(sourceAddress);
acceptedDestinationAssociations.remove(destinationAddress);
}
finally {
lock.release();
}
}
void add(Address sourceAddress, Address destinationAddress) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
if (disposed || invalid) {
return;
}
acceptedSourceAssociations.add(sourceAddress);
acceptedDestinationAssociations.add(destinationAddress);
}
finally {
lock.release();
}
}
boolean isBlocked(Address sourceAddress, Address destinationAddress) {
lock.acquire();
try {
try (Closeable c = lock.read()) {
if (disposed) {
return true;
@@ -735,9 +642,6 @@ public class AssociationDatabaseManager implements VTAssociationManager {
return true;
}
}
finally {
lock.release();
}
return false;
}
@@ -749,7 +653,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {
.getRelatedAssociationRecordsBySourceAndDestinationAddress(sourceID,
destinationID);
for (DBRecord record : relatedRecords) {
VTAssociationDB associationDB = getAssociationForRecord(record);
VTAssociationDB associationDB = associationCache.getCachedInstance(record);
VTAssociationStatus status = associationDB.getStatus();
if (status == ACCEPTED) {
return true;
@@ -785,7 +689,7 @@ public class AssociationDatabaseManager implements VTAssociationManager {
while (it.hasNext()) {
monitor.increment();
DBRecord record = it.next();
VTAssociationDB associationDB = getAssociationForRecord(record);
VTAssociationDB associationDB = associationCache.getCachedInstance(record);
VTAssociationStatus status = associationDB.getStatus();
if (status == ACCEPTED) {
Address sourceAddress = associationDB.getSourceAddress();
@@ -804,4 +708,48 @@ public class AssociationDatabaseManager implements VTAssociationManager {
}
}
}
private class AssociationFactory implements DbFactory<VTAssociationDB> {
@Override
public VTAssociationDB instantiate(long key) {
try {
DBRecord record = associationTableAdapter.getRecord(key);
return record == null ? null : instantiate(record);
}
catch (IOException e) {
session.dbError(e);
return null;
}
}
@Override
public VTAssociationDB instantiate(DBRecord rec) {
return new VTAssociationDB(AssociationDatabaseManager.this, rec);
}
}
private class MarkupFactory implements DbFactory<MarkupItemStorageDB> {
@Override
public MarkupItemStorageDB instantiate(long key) {
try {
DBRecord record = markupItemTableAdapter.getRecord(key);
return record == null ? null : instantiate(record);
}
catch (IOException e) {
session.dbError(e);
return null;
}
}
@Override
public MarkupItemStorageDB instantiate(DBRecord rec) {
return new MarkupItemStorageDB(rec, AssociationDatabaseManager.this);
}
}
VTAssociation getAssociation(long associationKey) {
return associationCache.getCachedInstance(associationKey);
}
}
@@ -4,9 +4,9 @@
* 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.
@@ -25,22 +25,21 @@ import ghidra.feature.vt.api.main.VTMarkupItemStatus;
import ghidra.feature.vt.api.markuptype.VTMarkupType;
import ghidra.feature.vt.api.markuptype.VTMarkupTypeFactory;
import ghidra.feature.vt.api.util.Stringable;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.database.map.AddressMap;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.Program;
import ghidra.util.Lock.Closeable;
public class MarkupItemStorageDB extends DatabaseObject implements MarkupItemStorage {
public class MarkupItemStorageDB extends DbObject implements MarkupItemStorage {
private final AssociationDatabaseManager associationManager;
private final VTAssociation association;
private final VTSessionDB session;
private DBRecord record;
MarkupItemStorageDB(DBRecord record, DBObjectCache<MarkupItemStorageDB> cache,
AssociationDatabaseManager associationManager) {
super(cache, record.getKey());
MarkupItemStorageDB(DBRecord record, AssociationDatabaseManager associationManager) {
super(record.getKey());
this.record = record;
this.associationManager = associationManager;
this.session = associationManager.getSession();
@@ -60,47 +59,67 @@ public class MarkupItemStorageDB extends DatabaseObject implements MarkupItemSto
@Override
public Address getSourceAddress() {
long addressLong = record.getLongValue(SOURCE_ADDRESS_COL.column());
Program program = session.getSourceProgram();
AddressMap addressMap = program.getAddressMap();
return addressMap.decodeAddress(addressLong);
try (Closeable c = associationManager.lock.read()) {
refreshIfNeeded();
long addressLong = record.getLongValue(SOURCE_ADDRESS_COL.column());
Program program = session.getSourceProgram();
AddressMap addressMap = program.getAddressMap();
return addressMap.decodeAddress(addressLong);
}
}
@Override
public Address getDestinationAddress() {
long addressLong = record.getLongValue(DESTINATION_ADDRESS_COL.column());
Program program = session.getDestinationProgram();
AddressMap addressMap = program.getAddressMap();
return addressMap.decodeAddress(addressLong);
try (Closeable c = associationManager.lock.read()) {
refreshIfNeeded();
long addressLong = record.getLongValue(DESTINATION_ADDRESS_COL.column());
Program program = session.getDestinationProgram();
AddressMap addressMap = program.getAddressMap();
return addressMap.decodeAddress(addressLong);
}
}
@Override
public String getDestinationAddressSource() {
return record.getString(ADDRESS_SOURCE_COL.column());
try (Closeable c = associationManager.lock.read()) {
refreshIfNeeded();
return record.getString(ADDRESS_SOURCE_COL.column());
}
}
@Override
public VTMarkupItemStatus getStatus() {
checkIsValid();
byte ordinal = record.getByteValue(STATUS_COL.column());
return VTMarkupItemStatus.values()[ordinal];
try (Closeable c = associationManager.lock.read()) {
refreshIfNeeded();
byte ordinal = record.getByteValue(STATUS_COL.column());
return VTMarkupItemStatus.values()[ordinal];
}
}
@Override
public String getStatusDescription() {
return record.getString(STATUS_DESCRIPTION_COL.column());
try (Closeable c = associationManager.lock.read()) {
refreshIfNeeded();
return record.getString(STATUS_DESCRIPTION_COL.column());
}
}
@Override
public Stringable getSourceValue() {
String string = record.getString(SOURCE_VALUE_COL.column());
return Stringable.getStringable(string, session.getSourceProgram());
try (Closeable c = associationManager.lock.read()) {
refreshIfNeeded();
String string = record.getString(SOURCE_VALUE_COL.column());
return Stringable.getStringable(string, session.getSourceProgram());
}
}
@Override
public Stringable getDestinationValue() {
String string = record.getString(ORIGINAL_DESTINATION_VALUE_COL.column());
return Stringable.getStringable(string, session.getDestinationProgram());
try (Closeable c = associationManager.lock.read()) {
refreshIfNeeded();
String string = record.getString(ORIGINAL_DESTINATION_VALUE_COL.column());
return Stringable.getStringable(string, session.getDestinationProgram());
}
}
@Override
@@ -131,16 +150,13 @@ public class MarkupItemStorageDB extends DatabaseObject implements MarkupItemSto
@Override
public MarkupItemStorage reset() {
associationManager.lock.acquire();
try {
try (Closeable c = associationManager.lock.write()) {
checkDeleted();
MarkupItemStorage storage = new MarkupItemStorageImpl(getAssociation(), getMarkupType(),
getSourceAddress(), getDestinationAddress(), getDestinationAddressSource());
associationManager.removeMarkupRecord(record.getKey());
return storage;
}
finally {
associationManager.lock.release();
}
}
@Override
@@ -4,9 +4,9 @@
* 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.
@@ -24,21 +24,25 @@ import ghidra.feature.vt.api.impl.MarkupItemManagerImpl;
import ghidra.feature.vt.api.impl.VTEvent;
import ghidra.feature.vt.api.main.*;
import ghidra.feature.vt.api.util.VTAssociationStatusException;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.model.address.Address;
import ghidra.util.Lock.Closeable;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.TaskMonitor;
public class VTAssociationDB extends DatabaseObject implements VTAssociation {
public class VTAssociationDB extends DbObject implements VTAssociation {
public DBRecord record;
private MarkupItemManagerImpl markupManager;
public final AssociationDatabaseManager associationDBM;
public VTAssociationDB(AssociationDatabaseManager associationManager,
DBObjectCache<VTAssociationDB> cache, DBRecord record) {
super(cache, record.getKey());
/**
* Constructor
* @param associationManager the association database manager
* @param record the record for the association
*/
VTAssociationDB(AssociationDatabaseManager associationManager, DBRecord record) {
super(record.getKey());
this.associationDBM = associationManager;
this.record = record;
markupManager = new MarkupItemManagerImpl(this);
@@ -93,41 +97,29 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {
@Override
public Address getSourceAddress() {
associationDBM.lock.acquire();
try {
checkIsValid();
try (Closeable c = associationDBM.lock.read()) {
refreshIfNeeded();
return associationDBM
.getSourceAddressFromLong(record.getLongValue(SOURCE_ADDRESS_COL.column()));
}
finally {
associationDBM.lock.release();
}
}
@Override
public Address getDestinationAddress() {
associationDBM.lock.acquire();
try {
checkIsValid();
try (Closeable c = associationDBM.lock.read()) {
refreshIfNeeded();
return associationDBM.getDestinationAddressFromLong(
record.getLongValue(DESTINATION_ADDRESS_COL.column()));
}
finally {
associationDBM.lock.release();
}
}
@Override
public VTAssociationType getType() {
associationDBM.lock.acquire();
try {
checkIsValid();
try (Closeable c = associationDBM.lock.read()) {
refreshIfNeeded();
byte associationTypeOrdinal = record.getByteValue(TYPE_COL.column());
return VTAssociationType.values()[associationTypeOrdinal];
}
finally {
associationDBM.lock.release();
}
}
public void markupItemStatusChanged(VTMarkupItem markupItem) {
@@ -136,37 +128,25 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {
@Override
public VTAssociationStatus getStatus() {
associationDBM.lock.acquire();
try {
checkIsValid();
try (Closeable c = associationDBM.lock.read()) {
refreshIfNeeded();
return VTAssociationStatus.values()[record.getByteValue(STATUS_COL.column())];
}
finally {
associationDBM.lock.release();
}
}
@Override
public VTAssociationMarkupStatus getMarkupStatus() {
associationDBM.lock.acquire();
try {
checkIsValid();
try (Closeable c = associationDBM.lock.read()) {
refreshIfNeeded();
return new VTAssociationMarkupStatus(record.getByteValue(APPLIED_STATUS_COL.column()));
}
finally {
associationDBM.lock.release();
}
}
DBRecord getRecord() {
associationDBM.lock.acquire();
try {
checkIsValid();
try (Closeable c = associationDBM.lock.read()) {
refreshIfNeeded();
return record;
}
finally {
associationDBM.lock.release();
}
}
@Override
@@ -220,20 +200,15 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {
@Override
public int getVoteCount() {
associationDBM.lock.acquire();
try {
checkIsValid();
try (Closeable c = associationDBM.lock.read()) {
refreshIfNeeded();
return record.getIntValue(VOTE_COUNT_COL.column());
}
finally {
associationDBM.lock.release();
}
}
@Override
public void setMarkupStatus(VTAssociationMarkupStatus status) {
associationDBM.lock.acquire();
try {
try (Closeable c = associationDBM.lock.write()) {
checkDeleted();
VTAssociationMarkupStatus existingStatus = getMarkupStatus();
if (status.equals(existingStatus)) {
@@ -246,14 +221,10 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {
.setObjectChanged(VTEvent.ASSOCIATION_MARKUP_STATUS_CHANGED, this,
existingStatus, status);
}
finally {
associationDBM.lock.release();
}
}
public void setStatus(VTAssociationStatus status) {
associationDBM.lock.acquire();
try {
try (Closeable c = associationDBM.lock.write()) {
checkDeleted();
VTAssociationStatus existingStatus = getStatus();
if (status == existingStatus) {
@@ -266,15 +237,11 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {
.setObjectChanged(VTEvent.ASSOCIATION_STATUS_CHANGED, this, existingStatus,
status);
}
finally {
associationDBM.lock.release();
}
}
@Override
public void setVoteCount(int voteCount) {
associationDBM.lock.acquire();
try {
try (Closeable c = associationDBM.lock.write()) {
checkDeleted();
voteCount = Math.max(0, voteCount);
record.setIntValue(VOTE_COUNT_COL.column(), voteCount);
@@ -282,9 +249,6 @@ public class VTAssociationDB extends DatabaseObject implements VTAssociation {
associationDBM.getSession()
.setObjectChanged(VTEvent.VOTE_COUNT_CHANGED, this, null, null);
}
finally {
associationDBM.lock.release();
}
}
@Override
@@ -4,9 +4,9 @@
* 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.
@@ -23,14 +23,14 @@ import db.DBRecord;
import ghidra.feature.vt.api.impl.VTEvent;
import ghidra.feature.vt.api.impl.VTProgramCorrelatorInfo;
import ghidra.feature.vt.api.main.*;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.model.address.Address;
import ghidra.util.Lock;
import ghidra.util.Lock.Closeable;
import ghidra.util.SystemUtilities;
import ghidra.util.exception.AssertException;
public class VTMatchDB extends DatabaseObject implements VTMatch {
public class VTMatchDB extends DbObject implements VTMatch {
private DBRecord record;
private final VTMatchSetDB matchSet;
@@ -42,8 +42,8 @@ public class VTMatchDB extends DatabaseObject implements VTMatch {
private boolean doCalculateHash = true;
private int hash;
public VTMatchDB(DBObjectCache<VTMatchDB> cache, DBRecord record, VTMatchSetDB matchSet) {
super(cache, record.getKey());
public VTMatchDB(DBRecord record, VTMatchSetDB matchSet) {
super(record.getKey());
this.record = record;
this.matchSet = matchSet;
session = (VTSessionDB) matchSet.getSession();
@@ -105,8 +105,7 @@ public class VTMatchDB extends DatabaseObject implements VTMatch {
@Override
public void setTag(VTMatchTag tag) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
checkDeleted();
if (record == null) {
return;
@@ -127,9 +126,6 @@ public class VTMatchDB extends DatabaseObject implements VTMatch {
updateRecord();
session.setObjectChanged(VTEvent.MATCH_TAG_CHANGED, this, oldTag, newTagDB);
}
finally {
lock.release();
}
}
private VTAssociation loadAssociation() {
@@ -145,16 +141,12 @@ public class VTMatchDB extends DatabaseObject implements VTMatch {
@Override
public VTAssociation getAssociation() {
lock.acquire();
try {
checkIsValid();
try (Closeable c = lock.read()) {
refreshIfNeeded();
if (association == null) {
association = loadAssociation();
}
}
finally {
lock.release();
}
return association;
}
@@ -30,22 +30,22 @@ import ghidra.feature.vt.api.main.*;
import ghidra.framework.data.OpenMode;
import ghidra.framework.options.Options;
import ghidra.framework.options.ToolOptions;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.*;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.listing.Program;
import ghidra.util.Lock;
import ghidra.util.Lock.Closeable;
import ghidra.util.Msg;
import ghidra.util.exception.VersionException;
import ghidra.util.task.TaskMonitor;
import ghidra.util.xml.XmlUtilities;
public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {
public class VTMatchSetDB extends DbObject implements VTMatchSet {
private final DBRecord matchSetRecord;
private DBObjectCache<VTMatchDB> matchCache;
private DbCache<VTMatchDB> matchCache;
private final VTSessionDB session;
private VTMatchTableDBAdapter matchTableAdapter;
@@ -73,13 +73,13 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {
}
private VTMatchSetDB(DBRecord record, VTSessionDB session, DBHandle dbHandle, Lock lock) {
super(null, record.getKey());// cache not supported
super(record.getKey());// cache not supported
this.matchSetRecord = record;
this.session = session;
this.dbHandle = dbHandle;
this.lock = lock;
matchCache = new DBObjectCache<>(10);
matchCache = new DbCache<>(new MatchFactory(), lock, 10);
}
private void createTableAdapters(long tableID) throws IOException {
@@ -162,20 +162,17 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {
VTAssociationDB associationDB = associationManager.getOrCreateAssociationDB(
info.getSourceAddress(), info.getDestinationAddress(), info.getAssociationType());
VTMatchTag tag = info.getTag();
VTMatch newMatch = null;
try {
lock.acquire();
VTMatchDB newMatch = null;
try (Closeable c = lock.write()) {
VTMatchTagDB tagDB = session.getOrCreateMatchTagDB(tag);
DBRecord matchRecord =
matchTableAdapter.insertMatchRecord(info, this, associationDB, tagDB);
newMatch = getMatchForRecord(matchRecord);
newMatch = new VTMatchDB(matchRecord, this);
matchCache.add(newMatch);
}
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
if (newMatch != null) {
session.setObjectChanged(VTEvent.MATCH_ADDED, newMatch, null, newMatch);
}
@@ -213,8 +210,8 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {
VTAssociation association = match.getAssociation();
Address sourceAddress = association.getSourceAddress();
Address destinationAddress = association.getDestinationAddress();
try {
lock.acquire();
try (Closeable c = lock.write()) {
checkDeleted();
long matchKey = matchDb.getKey();
boolean deleted = matchTableAdapter.deleteRecord(matchKey);
if (deleted) {
@@ -231,9 +228,6 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
DeletedMatch deletedMatch = new DeletedMatch(sourceAddress, destinationAddress);
session.setObjectChanged(VTEvent.MATCH_DELETED, match, deletedMatch, null);
@@ -247,35 +241,31 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {
@Override
public Collection<VTMatch> getMatches() {
List<VTMatch> list = new LinkedList<>();
try {
lock.acquire();
try (Closeable c = lock.read()) {
RecordIterator iterator = matchTableAdapter.getRecords();
while (iterator.hasNext()) {
DBRecord nextRecord = iterator.next();
list.add(getMatchForRecord(nextRecord));
list.add(matchCache.getCachedInstance(nextRecord));
}
}
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
return list;
}
@Override
public Collection<VTMatch> getMatches(VTAssociation association) {
VTAssociationDB associationDB = (VTAssociationDB) association;
List<VTMatch> list = new LinkedList<>();
if (associationDB == null) {
return list; // No association, so no matches.
}
try {
try (Closeable c = lock.read()) {
VTAssociationDB associationDB = (VTAssociationDB) association;
if (associationDB == null) {
return list; // No association, so no matches.
}
RecordIterator iterator = matchTableAdapter.getRecords(associationDB.getKey());
while (iterator.hasNext()) {
DBRecord nextRecord = iterator.next();
VTMatch match = getMatchForRecord(nextRecord);
VTMatch match = matchCache.getCachedInstance(nextRecord);
list.add(match);
}
}
@@ -287,44 +277,23 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {
@Override
public Collection<VTMatch> getMatches(Address sourceAddress, Address destinationAddress) {
AssociationDatabaseManager associationManager = session.getAssociationManagerDBM();
VTAssociationDB existingAssociationDB =
associationManager.getExistingAssociationDB(sourceAddress, destinationAddress);
if (existingAssociationDB == null) {
return Collections.emptyList();
try (Closeable c = lock.read()) {
AssociationDatabaseManager associationManager = session.getAssociationManagerDBM();
VTAssociationDB existingAssociationDB =
associationManager.getExistingAssociationDB(sourceAddress, destinationAddress);
if (existingAssociationDB == null) {
return Collections.emptyList();
}
return getMatches(existingAssociationDB);
}
return getMatches(existingAssociationDB);
}
@Override
protected boolean refresh() {
// MatchSets are not cached, so this method is not used
return true;
}
private VTMatch getMatchForRecord(DBRecord matchRecord) {
try {
lock.acquire();
VTMatchDB match = matchCache.get(matchRecord);
if (match == null) {
match = new VTMatchDB(matchCache, matchRecord, this);
}
return match;
}
finally {
lock.release();
}
}
DBRecord getMatchRecord(long matchRecordKey) {
try {
return matchTableAdapter.getMatchRecord(matchRecordKey);
}
catch (IOException e) {
dbError(e);
}
return null;
}
Program getDestinationProgram() {
return session.getDestinationProgram();
}
@@ -345,9 +314,34 @@ public class VTMatchSetDB extends DatabaseObject implements VTMatchSet {
matchCache.invalidate();
}
DBRecord getMatchRecord(long matchKey) {
try {
return matchTableAdapter.getMatchRecord(matchKey);
}
catch (IOException e) {
session.dbError(e);
return null;
}
}
@Override
public String toString() {
return "Match Set " + getID() + " - " + getMatchCount() + " matches [Correlator=" +
getProgramCorrelatorInfo().getName() + "]";
}
private class MatchFactory implements DbFactory<VTMatchDB> {
@Override
public VTMatchDB instantiate(long matchKey) {
DBRecord record = getMatchRecord(matchKey);
return record == null ? null : instantiate(record);
}
@Override
public VTMatchDB instantiate(DBRecord rec) {
return new VTMatchDB(rec, VTMatchSetDB.this);
}
}
}
@@ -15,25 +15,24 @@
*/
package ghidra.feature.vt.api.db;
import static ghidra.feature.vt.api.db.VTMatchTagDBAdapter.ColumnDescription.TAG_NAME_COL;
import static ghidra.feature.vt.api.db.VTMatchTagDBAdapter.ColumnDescription.*;
import java.io.IOException;
import db.DBRecord;
import ghidra.feature.vt.api.main.VTMatchTag;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
/**
* The database object for a user defined tag on a version tracking match.
*/
public class VTMatchTagDB extends DatabaseObject implements VTMatchTag {
public class VTMatchTagDB extends DbObject implements VTMatchTag {
private VTSessionDB sessionDB;
private DBRecord record;
VTMatchTagDB(VTSessionDB sessionDB, DBObjectCache<VTMatchTagDB> cache, DBRecord record) {
super(cache, record.getKey());
VTMatchTagDB(VTSessionDB sessionDB, DBRecord record) {
super(record.getKey());
this.sessionDB = sessionDB;
this.record = record;
}
@@ -75,7 +74,7 @@ public class VTMatchTagDB extends DatabaseObject implements VTMatchTag {
* null if the match tag has been deleted.
*/
DBRecord getRecord() {
return checkIsValid() ? record : null;
return refreshIfNeeded() ? record : null;
}
@Override
@@ -32,12 +32,14 @@ import ghidra.framework.model.*;
import ghidra.framework.model.TransactionInfo.Status;
import ghidra.framework.options.Options;
import ghidra.framework.store.LockException;
import ghidra.program.database.DBObjectCache;
import ghidra.program.database.DbCache;
import ghidra.program.database.DbFactory;
import ghidra.program.database.map.AddressMap;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.listing.Program;
import ghidra.util.*;
import ghidra.util.Lock.Closeable;
import ghidra.util.exception.*;
import ghidra.util.task.TaskLauncher;
import ghidra.util.task.TaskMonitor;
@@ -88,7 +90,7 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
private VTMatchSetTableDBAdapter matchSetTableAdapter;
private AssociationDatabaseManager associationManager;
private VTMatchTagDBAdapter matchTagAdapter;
private DBObjectCache<VTMatchTagDB> tagCache = new DBObjectCache<>(10);
private DbCache<VTMatchTagDB> tagCache;
private Program sourceProgram;
private Program destinationProgram;
@@ -127,7 +129,7 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
public VTSessionDB(String name, Program sourceProgram, Program destinationProgram,
Object consumer) throws IOException {
super(new DBHandle(), UNUSED_DEFAULT_NAME, EVENT_NOTIFICATION_DELAY, consumer);
tagCache = new DbCache<>(new TagFactory(), lock, 10);
propertyTable = dbh.getTable(PROPERTY_TABLE_NAME);
int ID = startTransaction("Constructing New Version Tracking Match Set");
@@ -435,16 +437,12 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
@Override
protected void clearCache(boolean all) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
super.clearCache(all);
associationManager.invalidateCache();
tagCache.invalidate();
reconcileCachedMatchSets();
}
finally {
lock.release();
}
}
@Override
@@ -512,14 +510,10 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
@Override
public VTMatchSet createMatchSet(VTProgramCorrelator correlator) {
try {
lock.acquire();
try (Closeable c = lock.write()) {
long id = matchSetTableAdapter.getNextMatchSetID();
return createMatchSet(correlator, id);
}
finally {
lock.release();
}
}
private VTMatchSet createMatchSet(VTProgramCorrelator correlator, long id) {
@@ -588,7 +582,7 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
@Override
public List<VTMatchSet> getMatchSets() {
return new ArrayList<>(matchSetMap.values());
return lock.withRead(() -> new ArrayList<>(matchSetMap.values()));
}
AddressSet getSourceAddressSet(DBRecord record) throws IOException {
@@ -649,17 +643,13 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
@Override
public List<VTMatch> getMatches(VTAssociation association) {
try {
lock.acquire();
try (Closeable c = lock.read()) {
List<VTMatch> matches = new ArrayList<>();
for (VTMatchSet matchSet : matchSetMap.values()) {
matches.addAll(matchSet.getMatches(association));
}
return matches;
}
finally {
lock.release();
}
}
/**
@@ -679,30 +669,22 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
@Override
public VTMatchSet getManualMatchSet() {
try {
lock.acquire();
try (Closeable c = lock.read()) {
if (manualMatchSet == null) {
manualMatchSet = findMatchSet(ManualMatchProgramCorrelator.class.getName());
}
return manualMatchSet;
}
finally {
lock.release();
}
}
@Override
public VTMatchSet getImpliedMatchSet() {
try {
lock.acquire();
try (Closeable c = lock.read()) {
if (impliedMatchSet == null) {
impliedMatchSet = findMatchSet(ImpliedMatchProgramCorrelator.class.getName());
}
return impliedMatchSet;
}
finally {
lock.release();
}
}
private VTMatchSet findMatchSet(String correlatorClassName) {
@@ -719,8 +701,7 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
@Override
public void deleteMatchTag(VTMatchTag tag) {
String tagName = tag.getName();
try {
lock.acquire();
try (Closeable c = lock.write()) {
VTMatchTagDB tagDB = getMatchTagDB(tagName);
if (tagDB == null) {
return; // not sure if this can happen
@@ -734,30 +715,24 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
setObjectChanged(VTEvent.TAG_REMOVED, this, tagName, null);
}
@Override
public VTMatchTagDB createMatchTag(String tagName) {
VTMatchTagDB matchTag = null;
try {
lock.acquire();
try (Closeable c = lock.write()) {
matchTag = getMatchTagDB(tagName);
if (matchTag != null) {
return matchTag;
}
DBRecord record = matchTagAdapter.insertRecord(tagName);
matchTag = new VTMatchTagDB(this, tagCache, record);
matchTag = new VTMatchTagDB(this, record);
tagCache.add(matchTag);
}
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
setObjectChanged(VTEvent.TAG_ADDED, matchTag, null, matchTag);
return matchTag;
}
@@ -773,66 +748,28 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
return null;
}
public VTMatchTag getMatchTag(long tagKey) {
VTMatchTagDB tag = tagCache.getCachedInstance(tagKey);
return tag != null ? tag : VTMatchTag.UNTAGGED;
}
@Override
public Set<VTMatchTag> getMatchTags() {
Set<VTMatchTag> tags = new HashSet<>();
try {
lock.acquire();
try (Closeable c = lock.read()) {
RecordIterator records = matchTagAdapter.getRecords();
while (records.hasNext()) {
DBRecord record = records.next();
tags.add(getMatchTagNew(record));
tags.add(tagCache.getCachedInstance(record));
}
}
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
return tags;
}
private VTMatchTagDB getMatchTagNew(DBRecord record) {
if (record == null) {
throw new AssertException("How can we have a null record?!!!");
}
try {
lock.acquire();
VTMatchTagDB matchTagDB = tagCache.get(record);
if (matchTagDB == null) {
matchTagDB = new VTMatchTagDB(this, tagCache, record);
}
return matchTagDB;
}
finally {
lock.release();
}
}
public VTMatchTag getMatchTag(long key) {
try {
lock.acquire();
VTMatchTagDB matchTagDB = tagCache.get(key);
if (matchTagDB != null) {
return matchTagDB;
}
DBRecord record = matchTagAdapter.getRecord(key);
if (record != null) {
return new VTMatchTagDB(this, tagCache, record);
}
}
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
return VTMatchTag.UNTAGGED;
}
DBRecord getTagRecord(long key) throws IOException {
return matchTagAdapter.getRecord(key);
}
@@ -875,4 +812,24 @@ public class VTSessionDB extends DomainObjectAdapterDB implements VTSession {
associationManager.dispose();
super.close();
}
private class TagFactory implements DbFactory<VTMatchTagDB> {
@Override
public VTMatchTagDB instantiate(long key) {
try {
DBRecord record = matchTagAdapter.getRecord(key);
return record == null ? null : instantiate(record);
}
catch (IOException e) {
dbError(e);
return null;
}
}
@Override
public VTMatchTagDB instantiate(DBRecord record) {
return new VTMatchTagDB(VTSessionDB.this, record);
}
}
}
@@ -27,7 +27,7 @@ import ghidra.feature.vt.api.markuptype.*;
import ghidra.feature.vt.api.util.Stringable;
import ghidra.feature.vt.api.util.VersionTrackingApplyException;
import ghidra.framework.options.ToolOptions;
import ghidra.program.database.DatabaseObject;
import ghidra.program.database.DbObject;
import ghidra.program.model.address.Address;
import ghidra.program.util.ProgramLocation;
import ghidra.util.SystemUtilities;
@@ -402,7 +402,7 @@ public class MarkupItemImpl implements VTMarkupItem {
}
public boolean isStoredInDB() {
return (markupItemStorage instanceof DatabaseObject);
return (markupItemStorage instanceof DbObject);
}
@Override
@@ -0,0 +1,193 @@
/* ###
* 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 generic.cache;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import org.apache.commons.collections4.map.LRUMap;
/**
* Generic cache implementation that removes items from the cache when they are no longer
* referenced. It uses weak references for the values and a small hard cache
* so that recent items don't get garbage collected immediately.
* <P>
* This class is thread safe. All public methods are synchronized.
*
* @param <K> The type of the object stored in this cache
* @param <V> The type of the object stored in this cache
*/
public class WeakReferenceCache<K, V> {
private Map<K, KeyedReference<K, V>> refsById;
private ReferenceQueue<V> refQueue;
private LRUMap<K, V> hardCache;
/**
* Constructs a new WeakReferenceCache with a given hard cache size. The hard cache size is
* the minimum number of objects to keep in the cache. Typically, the cache will contain
* more than this number, but the excess objects are subject to garbage collection.
* @param hardCacheSize the minimum number of objects to keep in the cache.
*/
public WeakReferenceCache(int hardCacheSize) {
refsById = new HashMap<>();
refQueue = new ReferenceQueue<>();
hardCache = new LRUMap<>(hardCacheSize);
}
/**
* Retrieves the database object with the given key from the cache.
* @param key the key of the object to retrieve.
* @return the cached object or null if the object with that key is not currently cached.
*/
public synchronized V get(K key) {
KeyedReference<K, V> ref = refsById.get(key);
if (ref == null) {
return null;
}
V v = ref.get();
if (v == null) {
refsById.remove(key);
return null;
}
hardCache.put(key, v);
return v;
}
/**
* Returns the number of objects currently in the cache.
* @return the number of objects currently in the cache.
*/
public synchronized int size() {
return refsById.size();
}
/**
* Sets the number of objects to protect against garbage collection.
* @param size the minimum number of objects to keep in the cache.
*/
public synchronized void setHardCacheSize(int size) {
hardCache.clear();
hardCache = new LRUMap<>(size);
}
/**
* Adds the given database object to the cache.
* @param key the key for the cached object
* @param data the object to add to the cache
* @return the object that has been added to the cache
*/
public synchronized V add(K key, V data) {
processQueue();
hardCache.put(key, data);
KeyedReference<K, V> ref = new KeyedReference<>(key, data, refQueue);
refsById.put(key, ref);
return data;
}
/**
* Returns an List of all the cached objects.
* @return an List of all the cached objects.
*/
public synchronized List<V> getCachedObjects() {
List<V> list = new ArrayList<>();
processQueue();
for (KeyedReference<K, V> ref : refsById.values()) {
V v = ref.get();
if (v != null) {
list.add(v);
}
}
return list;
}
/**
* Applies the given consumer to all values in the cache.
* @param consumer the consumer to apply to all values in the cache
*/
public synchronized void apply(Consumer<V> consumer) {
processQueue();
for (KeyedReference<K, V> ref : refsById.values()) {
V v = ref.get();
if (v != null) {
consumer.accept(v);
}
}
}
/**
* Removes the object with the given key from the cache.
* @param key the key of the object to remove
* @return the value that was removed from the cache
*/
public synchronized V delete(K key) {
processQueue();
KeyedReference<K, V> ref = refsById.remove(key);
V v = null;
if (ref != null) {
v = ref.get();
ref.clear();
}
return v;
}
public synchronized void deleteIf(Predicate<V> predicate) {
Iterator<KeyedReference<K, V>> iterator = refsById.values().iterator();
while (iterator.hasNext()) {
KeyedReference<K, V> ref = iterator.next();
V v = ref.get();
if (v == null || predicate.test(v)) {
iterator.remove();
}
}
}
// we know the cast is safe--we put them in there
@SuppressWarnings("unchecked")
private void processQueue() {
KeyedReference<K, V> ref;
while ((ref = (KeyedReference<K, V>) refQueue.poll()) != null) {
K key = ref.getKey();
KeyedReference<K, V> oldValue = refsById.remove(key);
if (oldValue != null && oldValue != ref) {
// we have put another item in the cache with the same key. Further, we
// removed the item, but the garbage collector had not put the item on the
// reference queue until after we added a new reference to the cache.
// We want to keep the last value that was added, as it has not been deleted.
refsById.put(key, oldValue);
}
}
}
private static class KeyedReference<K, V> extends WeakReference<V> {
private K key;
KeyedReference(K key, V obj, ReferenceQueue<V> queue) {
super(obj, queue);
this.key = key;
}
K getKey() {
return key;
}
}
}
@@ -4,9 +4,9 @@
* 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.
@@ -16,14 +16,16 @@
package ghidra.framework.data;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import generic.timer.GhidraTimer;
import generic.timer.GhidraTimerFactory;
import ghidra.framework.model.*;
import ghidra.util.*;
import ghidra.util.Lock.Closeable;
import ghidra.util.datastruct.WeakDataStructureFactory;
import ghidra.util.datastruct.WeakSet;
import utility.function.Callback;
/**
* A class to queue and send {@link DomainObjectChangeRecord} events.
@@ -225,35 +227,14 @@ class DomainObjectChangeSupport {
//=================================================================================================
// Note: all clients of lockQueue() must not call external APIs that could use locking
private void withLock(Runnable r) {
try {
writeLock.acquire();
r.run();
}
finally {
writeLock.release();
}
private void withLock(Callback r) {
writeLock.withWrite(r);
}
// Note: all clients of lockQueue() must not call external APIs that could use locking
private <T> T withLock(Callable<T> c) {
try {
writeLock.acquire();
T result;
try {
result = c.call();
return result;
}
catch (Exception e) {
// sholudn't happen
Msg.error(this, "Exception while updating change records", e);
return null;
}
}
finally {
writeLock.release();
private <T> T withLock(Supplier<T> s) {
try (Closeable c = writeLock.write()) {
return s.get();
}
}
@@ -4,9 +4,9 @@
* 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.
@@ -15,14 +15,41 @@
*/
package ghidra.util;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Supplier;
import utility.function.Callback;
/**
* Ghidra synchronization lock. This class allows creation of named locks for
* synchronizing modification of multiple tables in the Ghidra database.
* Ghidra synchronization read/write lock that provides read and write methods for acquiring either
* a shared read access or an exclusive write access.
* <P>
* It extends Java's ReentrantReadWriteLock, but
* adds convenient methods to get either a read or write lock that returns an {@link AutoCloseable}
* object that can be used in a try with resources block that will auto release the lock when
* the try block is exited.
* <P>
* To use a lock for share read access the general pattern is something like:
* <PRE>
* int getSize() {
* try (Closeable c = lock.read()) {
* return record.getIntValue(SIZE);
* }
* }
* </PRE>
* <P>
* Similarly, to get exclusive access for modification:
* <PRE>
* int getSize() {
* try (Closeable c = lock.write()) {
* return record.setIntValue(SIZE);
* database.updateRecord(record);
* }
* }
* </PRE>
*
*/
public class Lock {
private Thread owner;
private int lockAquireCount = 0;
private int waiterCount = 0;
public class Lock extends ReentrantReadWriteLock {
private String name;
/**
@@ -34,58 +61,34 @@ public class Lock {
this.name = name;
}
/**
* Acquire this synchronization lock. (i.e. begin synchronizing on this named
* lock.)
*/
public synchronized void acquire() {
Thread currThread = Thread.currentThread();
while (true) {
if (owner == null) {
lockAquireCount = 1;
owner = currThread;
return;
}
else if (owner == currThread) {
lockAquireCount++;
return;
}
try {
waiterCount++;
wait();
}
catch (InterruptedException e) {
// exception from another threads notify(), ignore
// and try to get lock again
}
finally {
waiterCount--;
}
}
@Override
public String toString() {
return name + " Lock";
}
/**
* Releases this lock, since you are through with the code that needed
* synchronization.
* Acquires the read lock that can allow simultaneous access by all read threads. Will block
* if any thread already has a write lock.
* @return An AutoCloseable handle to the lock that be used in a try with resources block to
* automatically release the lock when the block is exited.
*/
public synchronized void release() {
Thread currThread = Thread.currentThread();
public Closeable read() {
super.readLock().lock();
return () -> super.readLock().unlock();
if (lockAquireCount > 0 && (owner == currThread)) {
if (--lockAquireCount == 0) {
owner = null;
// This is purely to help sample profiling. If notify() is called the
// sampler can attribute time to the methods calling this erroneously. For some reason
// the visualvm sampler gets a sample more often when notify() is called.
if (waiterCount != 0) {
notify();
}
}
}
else {
throw new IllegalStateException("Attempted to release an unowned lock: " + name);
}
}
/**
* Acquires the exclusive write lock that prevents any other thread, reader or writer, from
* getting a lock while the write lock is held. Will block if any other thread has either
* a read or write lock. VERY IMPORTANT, any thread that attempts to acquire the write lock
* while already holding the read lock, will cause an immediate deadlock.
* @return An AutoCloseable handle to the lock that be used in a try with resources block to
* automatically release the lock when the block is exited.
*/
public Closeable write() {
super.writeLock().lock();
return () -> super.writeLock().unlock();
}
/**
@@ -93,7 +96,50 @@ public class Lock {
*
* @return the thread that owns the lock or null.
*/
@Override
public Thread getOwner() {
return owner;
return super.getOwner();
}
/**
* A convenience method for acquiring a read lock, executing a supplier object,
* then releasing the lock.
* @param <T> the supplier return type
* @param supplier the supplier to execute while holding a read lock.
* @return the result from the supplier
*/
public <T> T withRead(Supplier<T> supplier) {
readLock().lock();
try {
return supplier.get();
}
finally {
readLock().unlock();
}
}
/**
* A convenience method for acquiring a write lock, executing a callback object,
* then releasing the lock.
* @param callback the callback to execute while holding a write lock.
*/
public void withWrite(Callback callback) {
writeLock().lock();
try {
callback.call();
}
finally {
writeLock().unlock();
}
}
/**
* Object to auto close an acquired read or write lock. Note that we can't just use the
* java {@link java.io.Closeable} because it throws an exception on the close call that
* we don't want.
*/
public interface Closeable extends AutoCloseable {
@Override
public void close();
}
}
@@ -1,337 +0,0 @@
/* ###
* 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;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.*;
import db.DBRecord;
import ghidra.program.model.address.KeyRange;
/**
* Generic cache implementation for objects that extend DatabaseObject. This is a reference based
* cache such that objects are only ever automatically removed from the cache when there are no
* references to that object. It also maintains a small "hard" cache so that recently accessed objects
* are not prematurely removed from the cache if there are no references to them.
*
* @param <T> The type of the object stored in this cache
*/
public class DBObjectCache<T extends DatabaseObject> {
private Map<Long, KeyedSoftReference<T>> map;
private ReferenceQueue<T> refQueue;
private LinkedList<T> hardCache;
private int hardCacheSize;
private volatile int invalidateCount;
/**
* Constructs a new DBObjectCache with a given hard cache size. The hard cache size is
* the minimum number of objects to keep in the cache. Typically, the cache will contain
* more than this number, but the excess objects are subject to garbage collections
* @param hardCacheSize the minimum number of objects to keep in the cache.
*/
public DBObjectCache(int hardCacheSize) {
this.hardCacheSize = hardCacheSize;
map = new HashMap<>();
refQueue = new ReferenceQueue<>();
hardCache = new LinkedList<>();
}
/**
* Retrieves the database object with the given key from the cache.
* @param key the key of the object to retrieve.
* @return the cached object or null if the object with that key is not currently cached.
*/
public synchronized T get(long key) {
KeyedSoftReference<T> ref = map.get(key);
if (ref != null) {
T obj = ref.get();
if (obj == null) {
map.remove(key);
}
else {
if (obj.checkIsValid()) {
addToHardCache(obj);
return obj;
}
map.remove(key);
}
}
return null;
}
/**
* Retrieves the database object with the given record and associated key from the cache.
* This form should be used in conjunction with record iterators to avoid unnecessary
* record query during a possible object refresh. To benefit from the record the cached
* object must implement the {@link DatabaseObject#refresh(DBRecord)} method which by default
* ignores the record and simply calls {@link DatabaseObject#refresh()}.
* @param objectRecord the valid record corresponding to the object to be retrieved and possibly
* used to refresh the associated object if found in cache
* @return the cached object or null if the object with that key is not currently cached.
*/
public synchronized T get(DBRecord objectRecord) {
long key = objectRecord.getKey();
KeyedSoftReference<T> ref = map.get(key);
if (ref != null) {
T obj = ref.get();
if (obj == null) {
map.remove(key);
}
else {
if (obj.checkIsValid(objectRecord)) {
addToHardCache(obj);
return obj;
}
map.remove(key);
}
}
return null;
}
/**
* Returns the number of objects currently in the cache.
* @return the number of objects currently in the cache.
*/
public int size() {
return map.size();
}
/**
* Sets the number of objects to protect against garbage collection.
* @param size the minimum number of objects to keep in the cache.
*/
public synchronized void setHardCacheSize(int size) {
while (hardCache.size() > size) {
hardCache.removeLast();
}
this.hardCacheSize = size;
}
/**
* Adds the given database object to the cache.
* @param data the object to add to the cache.
*/
void put(T data) {
processQueue();
long key = data.getKey();
addToHardCache(data);
KeyedSoftReference<T> ref = new KeyedSoftReference<>(key, data, refQueue);
map.put(key, ref);
}
/**
* Returns an List of all the cached objects.
* @return an List of all the cached objects.
*/
public synchronized List<T> getCachedObjects() {
ArrayList<T> list = new ArrayList<>();
processQueue();
for (KeyedSoftReference<T> ref : map.values()) {
T obj = ref.get();
if (obj != null) {
list.add(obj);
}
}
return list;
}
/**
* Delete all objects from HashMap whose key is contained
* within the specified keyRanges.
* @param keyRanges key ranges to delete
*/
//TODO: Discourage large cases by only allowing a single range to be specified
public synchronized void delete(List<KeyRange> keyRanges) {
hardCache.clear();
processQueue();
long rangesSize = getKeyRangesSize(keyRanges); // < 0 too many ranges
if (rangesSize < 0 || rangesSize > map.size()) {
deleteLargeKeyRanges(keyRanges);
}
else {
deleteSmallKeyRanges(keyRanges);
}
}
/**
* Delete all objects from cache whose key is contained
* within the specified keyRanges. Iteration over all
* keys contained within keyRanges will be performed.
* @param keyRanges key ranges to delete
*/
private void deleteSmallKeyRanges(List<KeyRange> keyRanges) {
for (KeyRange range : keyRanges) {
for (long key = range.minKey; key <= range.maxKey; key++) {
KeyedSoftReference<T> ref = map.remove(key);
if (ref != null) {
DatabaseObject obj = ref.get();
if (obj != null) {
obj.setDeleted();
ref.clear();
}
}
}
}
}
/**
* Delete all objects from cache whose key is contained
* within the specified keyRanges. Iteration over all
* keys contained within map will be performed.
* @param keyRanges key ranges to delete
*/
private void deleteLargeKeyRanges(List<KeyRange> keyRanges) {
map.values().removeIf(ref -> checkRef(ref, keyRanges));
}
private boolean checkRef(KeyedSoftReference<T> ref, List<KeyRange> keyRanges) {
long key = ref.getKey();
if (keyRangesContain(keyRanges, key)) {
DatabaseObject obj = ref.get();
if (obj != null) {
obj.setDeleted();
ref.clear();
}
return true;
}
return false;
}
/**
* Return total number of keys covered by list of keyRanges.
* @param keyRanges key ranges to get the number of keys
* @return number of keys, or -1 if more than Long.MAX_VALUE keys
*/
private long getKeyRangesSize(List<KeyRange> keyRanges) {
long size = 0;
for (KeyRange range : keyRanges) {
size += range.length();
if (size < 0) {
return -1;
}
}
return size;
}
private boolean keyRangesContain(List<KeyRange> keyRanges, long key) {
for (KeyRange range : keyRanges) {
if (range.contains(key)) {
return true;
}
}
return false;
}
/**
* Marks all the cached objects as invalid. Invalid objects will have to refresh themselves
* before they are allowed to be used. If an invalidated object cannot refresh itself, then
* the object is removed from the cache and discarded and the application can no longer use
* that instance of the object.
*/
public synchronized void invalidate() {
hardCache.clear();
processQueue();
if (++invalidateCount <= 0) {
invalidateCount = 1;
for (KeyedSoftReference<T> ref : map.values()) {
DatabaseObject obj = ref.get();
if (obj != null) {
obj.setInvalid();
}
}
}
}
/**
* Get the current invalidate counter value which corresponds to the number of time
* the entire cache has been invalidated.
* @return current invalidate counter value.
*/
int getInvalidateCount() {
return invalidateCount;
}
/**
* Removes the object with the given key from the cache.
* @param key the key of the object to remove.
*/
public synchronized void delete(long key) {
processQueue();
KeyedSoftReference<T> ref = map.get(key);
if (ref != null) {
T obj = ref.get();
if (obj != null) {
obj.setDeleted();
ref.clear();
}
map.remove(key);
}
}
private void addToHardCache(T obj) {
hardCache.addLast(obj);
if (hardCache.size() > hardCacheSize) {
hardCache.removeFirst();
}
}
// we know the cast is safe--we put them in there
@SuppressWarnings("unchecked")
private void processQueue() {
KeyedSoftReference<T> ref;
while ((ref = (KeyedSoftReference<T>) refQueue.poll()) != null) {
long key = ref.getKey();
KeyedSoftReference<T> oldValue = map.remove(key);
if (oldValue != null && oldValue != ref) {
// we have put another item in the cache with the same key. Further, we
// removed the item, but the garbage collector had not put the item on the
// reference queue until after we added a new reference to the cache.
// We want to keep the last value that was added, as it has not been deleted.
map.put(key, oldValue);
}
}
}
private static class KeyedSoftReference<T> extends WeakReference<T> {
private long key;
KeyedSoftReference(long key, T obj, ReferenceQueue<T> queue) {
super(obj, queue);
this.key = key;
}
long getKey() {
return key;
}
}
public synchronized void keyChanged(long oldKey, long newKey) {
processQueue();
KeyedSoftReference<T> ref = map.remove(oldKey);
if (ref != null) {
map.put(newKey, ref);
T t = ref.get();
if (t != null) {
t.setInvalid();
}
}
}
}
@@ -33,6 +33,7 @@ import ghidra.program.model.listing.Program;
import ghidra.program.util.ProgramChangeRecord;
import ghidra.program.util.ProgramEvent;
import ghidra.util.InvalidNameException;
import ghidra.util.Lock.Closeable;
import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
@@ -496,14 +497,10 @@ public class DataTypeArchiveDB extends DomainObjectAdapterDB implements DataType
@Override
protected void clearCache(boolean all) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
super.clearCache(all);
dataTypeManager.invalidateCache();
}
finally {
lock.release();
}
}
@Override
@@ -1,231 +0,0 @@
/* ###
* 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;
import java.util.ConcurrentModificationException;
import db.DBRecord;
import ghidra.util.Lock;
/**
* Base class for an cached object in the database. Database objects have keys. They are marked as
* invalid when a database cache is cleared and can be revived on a refresh as long as they haven't
* been deleted. Instantiating an object will cause it to be added immediately to the associated
* cache.
*/
public abstract class DatabaseObject {
protected long key;
private volatile boolean deleted;
@SuppressWarnings("rawtypes")
private final DBObjectCache cache;
private volatile int invalidateCount;
private boolean refreshing = false;
/**
* Constructs a new DatabaseObject and adds it to the specified cache.
*
* @param cache to be used for this object or null if object will not be cached
* @param key database key to uniquely identify this object
*/
@SuppressWarnings("unchecked")
protected DatabaseObject(@SuppressWarnings("rawtypes") DBObjectCache cache, long key) {
this.key = key;
this.cache = cache;
if (cache != null) {
cache.put(this);
this.invalidateCount = cache.getInvalidateCount();
}
}
/**
* Get the database key for this object.
*/
public long getKey() {
return key;
}
/**
* Marks the object as deleted.
*/
protected void setDeleted() {
deleted = true;
}
/**
*
* Invalidate this object. This does not necessarily mean that this object can never be used
* again. If the object can refresh itself, it may still be useable.
*/
public void setInvalid() {
invalidateCount = getCurrentValidationCount() - 1;
}
private void setValid() {
invalidateCount = getCurrentValidationCount();
}
private int getCurrentValidationCount() {
return cache != null ? cache.getInvalidateCount() : 0;
}
protected void keyChanged(long newKey) {
long oldKey = key;
this.key = newKey;
if (cache != null) {
cache.keyChanged(oldKey, key);
}
}
/**
* Returns true if object is currently invalid and must be validated prior to further use.
* A deleted object will be considered valid since it cannot be refreshed.
* An invalid object may result from a cache invalidation which corresponds to wide-spread
* record changes. A common situation where this can occur is an undo/redo operation
* against the underlying database. The methods {@link #checkIsValid()}, {@link #checkDeleted()},
* {@link #validate(Lock)} and {@link #isDeleted(Lock)} are methods which will force
* a re-validation if required.
*
* @return true if this object is invalid and must be re-validated, else false if object state
* is currently valid which may include a deleted state.
*/
protected final boolean isInvalid() {
return !deleted && invalidateCount != getCurrentValidationCount();
}
/**
* Checks if this object has been deleted, in which case any use of the object is not allowed.
* This method should be invoked before any modifications to the object are performed to
* ensure it still exists and is in a valid state.
*
* @throws ConcurrentModificationException if the object has been deleted from the database.
*/
protected void checkDeleted() {
if (!checkIsValid()) {
throw new ConcurrentModificationException("Object has been deleted.");
}
}
/**
* Check whether this object is still valid. If the object is invalid, the object will attempt
* to refresh itself. If the refresh fails, the object will be marked as deleted.
*
* @return true if the object is valid, else false if deleted
*/
protected boolean checkIsValid() {
return checkIsValid(null);
}
/**
* Check whether this object is still valid. If the object is invalid and not deleted, the
* object will attempt to refresh itself using the specified record. If the refresh fails,
* the object will be marked as deleted and removed from cache. If this object is already
* marked as deleted a refresh will not be perormed.
* <P>
* This method may invoke {@link #refresh(DBRecord)} to perform a refresh. It is important
* that such a refresh avoid recursing back into this method.
*
* @param record optional record which may be used to refresh invalid object
* @return true if the object is valid or false if it has been deleted.
*/
protected boolean checkIsValid(DBRecord record) {
if (isInvalid()) {
if (refreshing) {
// NOTE: We need to correct such recursion cases which should be
// avoided since object is not in a valid state until refresh completed.
return !deleted;
}
refreshing = true;
try {
if (refresh(record)) {
// Object is valid
setValid();
return true;
}
// Object has been deleted
if (cache != null) {
cache.delete(key);
}
setDeleted();
return false;
}
finally {
refreshing = false;
}
}
return !deleted;
}
/**
* This method provides a cheap (lock free) way to test if an object is valid. If this object is
* invalid and not deleted, then the lock will be used to refresh as needed. A deleted object
* will not be refreshed.
*
* @param lock the lock that will be used if the object needs to be refreshed.
* @return true if object is valid or false if deleted
*/
protected boolean validate(Lock lock) {
if (!isInvalid()) {
return !deleted;
}
lock.acquire();
try {
return checkIsValid();
}
finally {
lock.release();
}
}
/**
* Returns true if this object has been deleted. Note: once an object has been deleted, it will
* never be "refreshed". For example, if an object is ever deleted and is resurrected via an
* "undo", you will have get a fresh instance of the object.
*
* @param lock object cache lock object
* @return true if this object has been deleted.
*/
public boolean isDeleted(Lock lock) {
return deleted || !validate(lock);
}
/**
* Tells the object to refresh its state from the database.
*
* @return true if the object was able to refresh itself. Return false if the object was
* deleted. Objects that extend this class must implement a refresh method. If an object
* can never refresh itself, then it should always return false.
*/
protected abstract boolean refresh();
/**
* Tells the object to refresh its state from the database using the specified record if not
* null. NOTE: The default implementation ignores the record and invokes refresh().
* Implementations of this method must take care if multiple database tables are used since the
* record supplied could correspond to another object. In some cases it may be best not to
* override this method or ignore the record provided.
*
* @param record valid record associated with object's key (optional, may be null to force
* record lookup or other refresh technique)
* @return true if the object was able to refresh itself. Return false if record is null and
* object was deleted. Objects that extend this class must implement a refresh method.
* If an object can never refresh itself, then it should always return false.
*/
protected boolean refresh(DBRecord record) {
return refresh();
}
}
@@ -0,0 +1,388 @@
/* ###
* 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;
import java.util.Collections;
import java.util.List;
import db.DBRecord;
import generic.cache.WeakReferenceCache;
import ghidra.program.model.address.KeyRange;
import ghidra.util.Lock;
import ghidra.util.Lock.Closeable;
/**
* A DbObject cache that efficiently manages the use of the database lock and uses a
* factory to create objects in the cache when they are not present.
* <P>
* This version of the cache is designed to be used without first acquiring a database lock. It will
* first attempt to retrieve the object without acquiring the lock. If that fails, it will acquire
* the lock, attempt to refresh the object if necessary or possibly create a new instance using
* the factory and add it to the cache.
*
* @param <T> the type of the database object
*/
public class DbCache<T extends DbObject> {
public static final DbCache<?> DUMMY = new DummyCache();
public static final int INVALID_COUNT = -1;
protected Lock lock;
protected DbFactory<T> factory;
protected WeakReferenceCache<Long, T> refCache;
private volatile int modificationCount;
/**
* Constructor
* @param factory the factory for creating new instances of database objects
* @param lock the database lock.
* @param hardCacheSize the size of the hard reference cache
*/
public DbCache(DbFactory<T> factory, Lock lock, int hardCacheSize) {
this.refCache = new WeakReferenceCache<>(hardCacheSize);
this.factory = factory;
this.lock = lock;
}
private DbCache() {
// do nothing (For Dummy)
}
/**
* Returns the number of objects currently in the cache. A database lock is not required.
* @return the number of objects currently in the cache.
*/
public int size() {
return refCache.size();
}
/**
* Adds the given database object to the cache.
* @param dbObject the object to add to the cache.
* @return the object that was cached
*/
public T add(T dbObject) {
dbObject.setCache(this);
return refCache.add(dbObject.getKey(), dbObject);
}
/**
* Retrieves the database object with the given key from the cache. This differs from the
* super's get() method in that it does not require that the database lock be acquired prior
* to calling this method and in fact performs better if the lock is not acquired. This version
* will first look in the cache and if it finds one that is definitely valid (there is
* a quick check that doesn't require the lock), it returns it immediately. Otherwise, the
* cache doesn't have the object or the object needs refreshing. In this case, this method
* will acquire the lock, attempt refresh the object if it exists, and if necessary call a
* factory method to create a new instance and add it to the cache.
*
* @param key the key of the object to retrieve.
* @return the cached object or null if the object with that key is not currently cached.
*/
public T getCachedInstance(Long key) {
T t = refCache.get(key);
if (t != null && t.isValid()) {
return t;
}
try (Closeable c = lock.read()) {
if (t != null && t.refreshIfNeeded()) {
return t;
}
// note that the instantiateAndCacheSafely is synchronized and it will double check the cache
// to prevent multiple threads from getting here and then creating a dbObject.
return instantiateAndCacheSafely(key);
}
}
/**
* Retrieves the object from the cache, but only if it already exists and is valid in the cache.
* It will not attempt to refresh the object or call the factory method to create new instances.
*
* @param key the key of the object to retrieve.
* @return the cached object or null if the object with that key is not currently cached and
* valid.
*/
public T getIfValid(Long key) {
T t = refCache.get(key);
if (t != null && t.isValid()) {
return t;
}
return null;
}
/**
* Retrieves the object with the given key from the cache with checking if it is valid or needs
* to be refreshed. It will not use the factory to create new instances if they don't exist.
* This method is used in very specialized situations where the caller has already done
* checking and they know the status of the object and how to refresh it cheaply if needed.
* @param key the key of the object to retrieve
* @return the object directly from the cache if one exists in the cache.
*/
public T getRaw(Long key) {
return refCache.get(key);
}
/**
* Retrieves the database object with the given record and associated key from the cache.
* This form should be used in conjunction with record iterators to avoid unnecessary
* record query during a possible object refresh. To benefit from the record the cached
* object must implement the {@link DbObject#refresh(DBRecord)} method which by default
* ignores the record and simply calls {@link DbObject#refresh()}.
* <P>
* This method is similar to the get() method in that it can be called without the database
* lock. It will first check if the object is in the cache and definitely valid before
* acquiring the lock and refreshing or creating the database object.
* @param dbRecord the valid record corresponding to the object to be retrieved and possibly
* used to refresh the associated object if found in cache
* @return the cached object or null if the object with that key is not currently cached.
*/
public T getCachedInstance(DBRecord dbRecord) {
if (dbRecord == null) {
return null;
}
Long key = dbRecord.getKey();
T t = refCache.get(key);
if (t != null && t.isValid()) {
return t;
}
try (Closeable c = lock.read()) {
if (t != null && t.refreshIfNeeded(dbRecord)) {
return t;
}
// note that the instantiateAndCacheSafely is synchronized and it will double check the cache
// to prevent multiple threads from getting here and then creating a dbObject.
return instantiateAndCacheSafely(dbRecord);
}
}
/**
* Returns an List of all the cached objects. These objects have not been checked to see if they
* are valid or not.
* @return an List of all the cached objects.
*/
public List<T> getCachedObjects() {
return refCache.getCachedObjects();
}
/**
* Delete all objects from HashMap whose key is contained
* within the specified keyRanges.
* @param keyRanges key ranges to delete
*/
public void delete(List<KeyRange> keyRanges) {
long rangesSize = getKeyRangesSize(keyRanges); // < 0 too many ranges
if (rangesSize < 0 || rangesSize > refCache.size()) {
deleteLargeKeyRanges(keyRanges);
}
else {
deleteSmallKeyRanges(keyRanges);
}
}
/**
* Marks all the cached objects as invalid. Invalid objects will have to refresh themselves
* before they are allowed to be used. If an invalidated object cannot refresh itself, then
* the object is removed from the cache and discarded and the application can no longer use
* that instance of the object.
*/
public void invalidate() {
// Note: the ++ operation here is not atomic, but that shouldn't be an issue. If more
// than one thread attempts to increment the count at the same time, it is possible for
// the count not to be incremented the full number of times, but it will be incremented
// at least by 1, which is all that really matters.
if (++modificationCount < 0) {
// if it overflows, reset to 0 so it is never negative
modificationCount = 0;
}
}
/**
* Removes the object with the given key from the cache. A database lock is not required.
* @param key the key of the object to remove.
*/
public void delete(long key) {
T deleted = refCache.delete(key);
if (deleted != null) {
deleted.setDeleted();
}
}
/**
* Updates the cache for an object whose key has changed.
* @param oldKey the old key
* @param newKey the new key
*/
public void keyChanged(long oldKey, long newKey) {
synchronized (refCache) {
T t = refCache.delete(oldKey);
if (t != null) {
t.setInvalid();
refCache.add(newKey, t);
}
}
}
/**
* Get the current cache modification counter value which corresponds to the number of times
* the entire cache has been invalidated.
* @return the current modification counter value
*/
public int getModificationCount() {
return modificationCount;
}
private synchronized T instantiateAndCacheSafely(Long key) {
// check if another thread got here first
T t = refCache.get(key);
if (t != null && t.refreshIfNeeded()) {
return t;
}
t = factory.instantiate(key);
if (t != null) {
add(t);
}
return t;
}
private synchronized T instantiateAndCacheSafely(DBRecord record) {
// check if another thread got here first
T t = refCache.get(record.getKey());
if (t != null && t.refreshIfNeeded()) {
return t;
}
t = factory.instantiate(record);
add(t);
return t;
}
/**
* Delete all objects from cache whose key is contained
* within the specified keyRanges. Iteration over all
* keys contained within keyRanges will be performed.
* @param keyRanges key ranges to delete
*/
private void deleteSmallKeyRanges(List<KeyRange> keyRanges) {
synchronized (refCache) {
for (KeyRange range : keyRanges) {
for (long key = range.minKey; key <= range.maxKey; key++) {
T dbObject = refCache.delete(key);
if (dbObject != null) {
dbObject.setDeleted();
}
}
}
}
}
/**
* Delete all objects from cache whose key is contained
* within the specified keyRanges. Iteration over all
* keys contained within map will be performed.
* @param keyRanges key ranges to delete
*/
private void deleteLargeKeyRanges(List<KeyRange> keyRanges) {
refCache.deleteIf(v -> checkInRange(v, keyRanges));
}
private boolean checkInRange(T t, List<KeyRange> keyRanges) {
long key = t.getKey();
if (keyRangesContain(keyRanges, key)) {
t.setDeleted();
return true;
}
return false;
}
/**
* Return total number of keys covered by list of keyRanges.
* @param keyRanges key ranges to get the number of keys
* @return number of keys, or -1 if more than Long.MAX_VALUE keys
*/
private long getKeyRangesSize(List<KeyRange> keyRanges) {
long size = 0;
for (KeyRange range : keyRanges) {
size += range.length();
if (size < 0) {
return -1;
}
}
return size;
}
private boolean keyRangesContain(List<KeyRange> keyRanges, long key) {
for (KeyRange range : keyRanges) {
if (range.contains(key)) {
return true;
}
}
return false;
}
private static class DummyCache extends DbCache<DbObject> {
@Override
public int getModificationCount() {
return 0;
}
@Override
public DbObject getCachedInstance(Long key) {
return null;
}
@Override
public DbObject getIfValid(Long key) {
return null;
}
@Override
public DbObject getCachedInstance(DBRecord dbRecord) {
return null;
}
@Override
public int size() {
return 0;
}
@Override
public DbObject add(DbObject dbObject) {
return dbObject;
}
@Override
public void keyChanged(long oldKey, long newKey) {
// do nothing
}
@Override
public void delete(long key) {
// do nothing
}
@Override
public void delete(List<KeyRange> keyRanges) {
// do nothing
}
@Override
public List<DbObject> getCachedObjects() {
return Collections.emptyList();
}
}
}
@@ -0,0 +1,48 @@
/* ###
* 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;
import db.DBRecord;
/**
* Interface for Factories that create {@link DbObject}s. Required by the {@link DbCache}
* <P>
* NOTE: This factory is used to instantiate dbObject instances that are already backed
* by records in the database. In general, these methods should only be used by the
* DbCache class to insure that the objects aren't already cached if they are created using
* this factor, they are added to the cache in a thread safe way.
*
* @param <T> the type of database object
*/
public interface DbFactory<T extends DbObject> {
/**
* Creates a new DatabaseObject of type T for the given key. It is
* expected that a record for this object already exists in the database. This method is
* simply creating the unique instance that is associated with that record.
* @param key the database key the database key
* @return the newly created instance of type T or null if no record found
*/
public T instantiate(long key);
/**
* Creates a new DatabaseObject of type T for the given record.
* @param record the database record to create its associated unique instance of T
* @return the newly created instance of type T
*/
public T instantiate(DBRecord record);
}
@@ -0,0 +1,286 @@
/* ###
* 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;
import java.util.ConcurrentModificationException;
import db.DBRecord;
import ghidra.util.Lock;
import ghidra.util.Lock.Closeable;
/**
* Base class for objects stored in the database.
* <P>
* The general contract for database objects is that there should only ever be one instance for
* a specific database object at any given time. To facilitate this, instances of database objects
* should be stored in a {@link DbCache} and the cache should be queried before creating
* any new instances.
* <P>
* Database objects have keys that are used by the database record and also serves as the key for
* cache lookup. They are marked as invalid when a database cache is invalidated and can be revived
* on a refresh as long as they haven't been deleted.
*/
public abstract class DbObject {
protected long key;
private volatile boolean deleted;
private DbCache<?> cache = DbCache.DUMMY;
private volatile int lastValidModificationCount;
private boolean refreshing = false;
/**
* Constructs a new DatabaseObject and adds it to the specified cache.
*
* @param key database key to uniquely identify this object
*/
protected DbObject(long key) {
this.key = key;
}
/**
* This is a special method for setting the cache and should ONLY be used by the
* {@link DbCache} when an object is added to the cache.
* @param cache the cache this object was added to.
*/
void setCache(DbCache<?> cache) {
this.cache = cache;
this.lastValidModificationCount = cache.getModificationCount();
}
/**
* Get the database key for this object.
* @return the database key for this object.
*/
public long getKey() {
return key;
}
/**
* Marks the object as deleted.
*/
protected void setDeleted() {
deleted = true;
}
/**
*
* Invalidate this object. This does not necessarily mean that this object can never be used
* again. If the object can refresh itself, it may still be usable.
*/
public void setInvalid() {
lastValidModificationCount = DbCache.INVALID_COUNT;
}
/**
* Marks this object as valid. Note that this call does no checking on its own should be used
* very carefully and only when the caller is absolutely sure that the object is valid. Used
* in a special case for default symbols that have no corresponding record.
*/
protected void setValid() {
lastValidModificationCount = cache.getModificationCount();
}
/**
* Method for updating the cache if a object record key changed. This is a very unusual
* situation and should generally be avoided.
* @param newKey the new key for the associated record for this object
*/
protected void keyChanged(long newKey) {
long oldKey = key;
this.key = newKey;
cache.keyChanged(oldKey, key);
}
/**
* Returns true if the object needs to be refreshed prior to further use. An object needs
* to be refreshed if the cache has been invalidated since the last time the object was
* checked for validity. There is a special case if the object has been deleted. It certainly
* doesn't need to be refreshed but clients that have the object already in hand still want to
* be able to call its getter methods without encountering an error. The assumption is that
* eventually the client will be kicked so it knows it needs to re-aquire its objects.
* <P>
* An object that has not been deleted may be marked as stale and in need of a refresh in one of
* two ways. The cache was invalidated or the object was specifically marked as invalid via its
* {@link #setInvalid()} method. A common situation where this can occur is an undo/redo
* operation against the underlying database. The methods {@link #refreshIfNeeded()},
* {@link #checkDeleted()}, {@link #validate(Lock)} and {@link #isDeleted(Lock)} are methods
* which will force a re-validation if required.
* <P>
* This method is final as it is intended to be a "fast" check that doesn't require a lock
* to see if the object is possibly in need of a refresh. We don't want any subclass to add
* additional checks that might require a database lock. The idea is that if the cache
* modification hasn't changed, then we know the object is valid. If the modification count
* has changed then we may or may not be valid and stronger checks are needed which will require
* a lock. Subclasses should override {@link #refreshIfNeeded()}, {@link #refresh()} to do
* the more robust validation checking.
*
* @return true if this object is invalid and must be re-validated, else false if object state
* is currently valid which may include a deleted state.
*/
protected final boolean needsRefreshing() {
if (deleted) {
return false;
}
return lastValidModificationCount != cache.getModificationCount();
}
/**
* Return true if the object is known to be valid at this time. Note that this is is subtly
* different from being the inverse of {@link #needsRefreshing()}, specifically in the case
* where the object has been deleted. For a deleted object, the {@link #isValid} method
* will return false, but the {@link #needsRefreshing()} will also return false.
* @return true if the object is known to be valid at this time.
*/
public final boolean isValid() {
if (deleted) {
return false;
}
return lastValidModificationCount == cache.getModificationCount();
}
/**
* Refreshes the object's state from the database if it is possibly stale. The check to see
* if an object is stale is fast, so this generally is a quick call unless an undo or
* redo has taken place and a true refresh is required. If a refresh is performed, the
* object may be discovered to be deleted. This method should only be called while
* holding a {@link Lock#readLock()}
*
* @return true if the object is valid, else false if deleted
*/
protected boolean refreshIfNeeded() {
return refreshIfNeeded(null);
}
/**
* Refreshes the object's state if it is stale using the given record.
* If the object has already been deleted, it will immediately return false. Otherwise, it
* will check to see if it needs to be refreshed, and if so, refresh it. While refreshing the
* object, it may be discovered that it has been deleted and marked as such.
* @param record a known valid record the object can use to refresh itself, if a refresh is
* needed. Null is permitted, in which case the object will have to do a database lookup to
* retrieve a record if a refresh is needed.
*
* @return true if the object is valid, else false if deleted
*/
synchronized boolean refreshIfNeeded(DBRecord record) {
if (needsRefreshing()) {
doRefresh(record);
}
return !deleted;
}
/**
* Checks if this object has been deleted, in which case any use of the object is not allowed.
* This method should be invoked before any modifications to the object are performed to
* ensure it still exists and is in a valid state.
*
* @throws ConcurrentModificationException if the object has been deleted from the database.
*/
protected void checkDeleted() {
if (!refreshIfNeeded(null)) {
throw new ConcurrentModificationException("Object has been deleted.");
}
}
/**
* Internal method for performing a refresh on a database object. This method may be called
* recursively, which is can detect and short circuit.
* @param record a known valid record the object can use to refresh itself or null. If null
* the object will have to do its own database retrieval of its record.
*/
private void doRefresh(DBRecord record) {
if (refreshing) {
// NOTE: We need to correct such recursion cases which should be
// avoided since object is not in a valid state until refresh completed.
return;
}
refreshing = true;
try {
if (refresh(record)) {
// Object is valid
setValid();
}
else {
// if refresh failed, object has been deleted
cache.delete(key);
setDeleted();
}
}
finally {
refreshing = false;
}
}
/**
* This method provides a cheap (lock free) way to test if an object is valid. If this object is
* invalid and not deleted, then the lock will be used to refresh as needed. A deleted object
* will not be refreshed.
*
* @param lock the lock that will be used if the object needs to be refreshed.
* @return true if object is valid or false if deleted
*/
protected boolean validate(Lock lock) {
if (isValid()) {
return true;
}
if (deleted) {
return false;
}
try (Closeable c = lock.read()) {
return refreshIfNeeded();
}
}
/**
* Returns true if this object has been deleted. Note: once an object has been deleted, it will
* never be "refreshed". For example, if an object is ever deleted and is resurrected via an
* "undo", you will have get a fresh instance of the object.
*
* @param lock object cache lock object
* @return true if this object has been deleted.
*/
public boolean isDeleted(Lock lock) {
return deleted || !validate(lock);
}
/**
* Tells the object to refresh its state from the database.
*
* @return true if the object was able to refresh itself. Return false if the object was
* deleted. Objects that extend this class must implement a refresh method. If an object
* can never refresh itself, then it should always return false.
*/
protected abstract boolean refresh();
/**
* Tells the object to refresh its state from the database using the specified record if not
* null. NOTE: The default implementation ignores the record and invokes refresh().
* Implementations of this method must take care if multiple database tables are used since the
* record supplied could correspond to another object. In some cases it may be best not to
* override this method or ignore the record provided.
*
* @param record valid record associated with object's key (optional, may be null to force
* record lookup or other refresh technique)
* @return true if the object was able to refresh itself. Return false if record is null and
* object was deleted. Objects that extend this class must implement a refresh method.
* If an object can never refresh itself, then it should always return false.
*/
protected boolean refresh(DBRecord record) {
return refresh();
}
}
@@ -4,9 +4,9 @@
* 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.
@@ -24,6 +24,7 @@ import ghidra.program.database.util.AddressRangeMapDB;
import ghidra.program.model.address.*;
import ghidra.program.util.ProgramEvent;
import ghidra.util.Lock;
import ghidra.util.Lock.Closeable;
import ghidra.util.exception.CancelledException;
import ghidra.util.exception.DuplicateNameException;
import ghidra.util.task.TaskMonitor;
@@ -42,24 +43,19 @@ public class IntRangeMapDB implements IntRangeMap {
public static IntRangeMapDB getPropertyMap(ProgramDB program, String mapName,
ErrorHandler errHandler, AddressMap addrMap, Lock lock) {
lock.acquire();
try {
try (Closeable c = lock.read()) {
DBHandle dbh = program.getDBHandle();
String tableName = IntRangeMapDB.TABLE_PREFIX + mapName;
if (dbh.getTable(tableName) != null) {
return new IntRangeMapDB(program, mapName, program, addrMap, lock);
}
}
finally {
lock.release();
}
return null;
}
public static IntRangeMapDB createPropertyMap(ProgramDB program, String mapName,
ErrorHandler errHandler, AddressMap addrMap, Lock lock) throws DuplicateNameException {
lock.acquire();
try {
try (Closeable c = lock.write()) {
DBHandle dbh = program.getDBHandle();
String tableName = TABLE_PREFIX + mapName;
if (dbh.getTable(tableName) != null) {
@@ -69,9 +65,6 @@ public class IntRangeMapDB implements IntRangeMap {
return new IntRangeMapDB(program, mapName, program, addrMap, lock);
}
finally {
lock.release();
}
}
private IntRangeMapDB(ProgramDB program, String mapName, ErrorHandler errHandler,
@@ -99,23 +92,16 @@ public class IntRangeMapDB implements IntRangeMap {
@Override
public void setValue(Address start, Address end, int value) {
checkDeleted();
lock.acquire();
try {
try (Closeable c = lock.write()) {
propertyMap.paintRange(start, end, new IntField(value));
program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);
}
finally {
lock.release();
}
}
@Override
public void setValue(AddressSetView addresses, int value) {
checkDeleted();
lock.acquire();
try {
try (Closeable c = lock.write()) {
AddressRangeIterator iter = addresses.getAddressRanges();
while (iter.hasNext()) {
AddressRange range = iter.next();
@@ -123,42 +109,30 @@ public class IntRangeMapDB implements IntRangeMap {
}
program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);
}
finally {
lock.release();
}
}
@Override
public void clearAll() {
checkDeleted();
lock.acquire();
try {
try (Closeable c = lock.write()) {
propertyMap.dispose();
program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);
}
finally {
lock.release();
}
}
@Override
public void clearValue(Address startAddr, Address endAddr) {
checkDeleted();
lock.acquire();
try {
try (Closeable c = lock.write()) {
propertyMap.clearRange(startAddr, endAddr);
program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);
}
finally {
lock.release();
}
}
@Override
public void clearValue(AddressSetView addresses) {
checkDeleted();
lock.acquire();
try {
try (Closeable c = lock.write()) {
AddressRangeIterator iter = addresses.getAddressRanges();
while (iter.hasNext()) {
AddressRange range = iter.next();
@@ -166,49 +140,34 @@ public class IntRangeMapDB implements IntRangeMap {
}
program.setChanged(ProgramEvent.INT_PROPERTY_MAP_CHANGED, null, mapName);
}
finally {
lock.release();
}
}
@Override
public Integer getValue(Address address) {
checkDeleted();
lock.acquire();
try {
try (Closeable c = lock.read()) {
Field value = propertyMap.getValue(address);
if (value == null) {
return null;
}
return ((IntField) value).getIntValue();
}
finally {
lock.release();
}
}
@Override
public AddressSet getAddressSet() {
checkDeleted();
lock.acquire();
try {
try (Closeable c = lock.read()) {
return propertyMap.getAddressSet();
}
finally {
lock.release();
}
}
@Override
public AddressSet getAddressSet(int value) {
checkDeleted();
lock.acquire();
try {
try (Closeable c = lock.read()) {
return propertyMap.getAddressSet(new IntField(value));
}
finally {
lock.release();
}
}
/**
@@ -216,13 +175,16 @@ public class IntRangeMapDB implements IntRangeMap {
* @param fromAddr move from address
* @param toAddr move to address
* @param length number of address to move
* @param monitor
* @throws CancelledException
* @param monitor the task monitor
* @throws CancelledException if cancelled
*/
@Override
public void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor)
throws CancelledException {
propertyMap.moveAddressRange(fromAddr, toAddr, length, monitor);
checkDeleted();
try (Closeable c = lock.write()) {
propertyMap.moveAddressRange(fromAddr, toAddr, length, monitor);
}
}
}
@@ -59,6 +59,7 @@ import ghidra.program.model.util.AddressSetPropertyMap;
import ghidra.program.model.util.PropertyMapManager;
import ghidra.program.util.*;
import ghidra.util.*;
import ghidra.util.Lock.Closeable;
import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
@@ -1160,8 +1161,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
@Override
public void setName(String newName) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
if (name.equals(newName)) {
return;
}
@@ -1172,9 +1172,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
}
private void refreshName() throws IOException {
@@ -1206,8 +1203,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
}
ProgramOverlayAddressSpace ovSpace = null;
lock.acquire();
try {
try (Closeable c = lock.write()) {
if (imageBaseOverride) {
throw new IllegalStateException(
"Overlay spaces may not be created while an image-base override is active");
@@ -1220,9 +1216,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
return ovSpace;
}
@@ -1230,8 +1223,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
public void renameOverlaySpace(String overlaySpaceName, String newName)
throws NotFoundException, InvalidNameException, DuplicateNameException, LockException {
lock.acquire();
try {
try (Closeable c = lock.write()) {
checkExclusiveAccess();
AddressSpace space = addressFactory.getAddressSpace(overlaySpaceName);
@@ -1253,16 +1245,12 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
}
@Override
public boolean removeOverlaySpace(String overlaySpaceName)
throws LockException, NotFoundException {
lock.acquire();
try {
checkExclusiveAccess();
@@ -1286,9 +1274,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
return false;
}
@@ -1321,8 +1306,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
if (commit) {
checkExclusiveAccess();
}
lock.acquire();
try {
try (Closeable c = lock.write()) {
Address currentImageBase = getImageBase();
if (!(commit && imageBaseOverride) && base.equals(currentImageBase)) {
return;
@@ -1382,9 +1366,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
}
invalidate();
}
finally {
lock.release();
}
//NOTE:
//this needs to be outside the lock...
((TreeManager) managers[TREE_MGR]).imageBaseChanged(commit);
@@ -1396,14 +1377,10 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
if (!imageBaseOverride) {
return;
}
lock.acquire();
try {
try (Closeable c = lock.write()) {
imageBaseOverride = false;
invalidate();
}
finally {
lock.release();
}
//NOTE:
//this needs to be outside the lock...
((TreeManager) managers[TREE_MGR]).imageBaseChanged(false);
@@ -1856,8 +1833,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
@Override
protected void clearCache(boolean all) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
super.clearCache(all);
refreshName();
overlaySpaceAdapter.updateOverlaySpaces(addressFactory);
@@ -1872,9 +1848,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
catch (IOException e) {
dbError(e);
}
finally {
lock.release();
}
}
@Override
@@ -1944,8 +1917,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
throws RollbackException {
// TODO: ensure that managers are notified with address ranges which correspond to a sequential set of address keys
lock.acquire();
try {
try (Closeable c = lock.write()) {
for (int i = NUM_MANAGERS - 1; i >= 0; i--) {
managers[i].deleteAddressRange(startAddr, endAddr, monitor);
}
@@ -1967,9 +1939,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
catch (CancelledException e) {
throw new RollbackException("Operation cancelled");
}
finally {
lock.release();
}
}
/**
@@ -1989,8 +1958,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
// TODO: WARNING! fromAddr range may no longer exist in memory map which could affect certain database iterators
lock.acquire();
try {
try (Closeable c = lock.write()) {
for (int i = NUM_MANAGERS - 1; i >= 0; i--) {
managers[i].moveAddressRange(fromAddr, toAddr, length, monitor);
}
@@ -2010,9 +1978,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
catch (CancelledException e) {
throw new RollbackException("Operation cancelled");
}
finally {
lock.release();
}
}
@Override
@@ -2057,8 +2022,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
checkExclusiveAccess();
lock.acquire();
try {
try (Closeable c = lock.write()) {
setEventsEnabled(false);
try {
boolean redisassemblyRequired = true;
@@ -2167,9 +2131,7 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
}
fireEvent(new DomainObjectChangeRecord(ProgramEvent.LANGUAGE_CHANGED));
}
finally {
lock.release();
}
}
/*
@@ -2281,23 +2243,18 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
@Override
public AddressSetPropertyMap createAddressSetPropertyMap(String mapName)
throws DuplicateNameException {
lock.acquire();
try {
try (Closeable c = lock.write()) {
AddressSetPropertyMapDB map =
AddressSetPropertyMapDB.createPropertyMap(this, mapName, this, addrMap, lock);
addrSetPropertyMap.put(mapName, map);
setChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_ADDED, null, mapName);
return map;
}
finally {
lock.release();
}
}
@Override
public AddressSetPropertyMap getAddressSetPropertyMap(String mapName) {
lock.acquire();
try {
try (Closeable c = lock.read()) {
AddressSetPropertyMapDB map = addrSetPropertyMap.get(mapName);
if (map != null) {
return map;
@@ -2309,15 +2266,11 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
}
return map;
}
finally {
lock.release();
}
}
@Override
public void deleteAddressSetPropertyMap(String mapName) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
AddressSetPropertyMapDB pm = addrSetPropertyMap.remove(mapName);
if (pm == null) {
pm = AddressSetPropertyMapDB.getPropertyMap(this, mapName, this, addrMap, lock);
@@ -2327,29 +2280,21 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
setChanged(ProgramEvent.ADDRESS_PROPERTY_MAP_REMOVED, null, mapName);
}
}
finally {
lock.release();
}
}
@Override
public IntRangeMapDB createIntRangeMap(String mapName) throws DuplicateNameException {
lock.acquire();
try {
try (Closeable c = lock.write()) {
IntRangeMapDB map = IntRangeMapDB.createPropertyMap(this, mapName, this, addrMap, lock);
intRangePropertyMap.put(mapName, map);
setChanged(ProgramEvent.INT_PROPERTY_MAP_ADDED, null, mapName);
return map;
}
finally {
lock.release();
}
}
@Override
public IntRangeMap getIntRangeMap(String mapName) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
IntRangeMapDB rangeMap = intRangePropertyMap.get(mapName);
if (rangeMap != null) {
return rangeMap;
@@ -2361,15 +2306,11 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
}
return rangeMap;
}
finally {
lock.release();
}
}
@Override
public void deleteIntRangeMap(String mapName) {
lock.acquire();
try {
try (Closeable c = lock.write()) {
IntRangeMapDB rangeMap = intRangePropertyMap.remove(mapName);
if (rangeMap == null) {
rangeMap = IntRangeMapDB.getPropertyMap(this, mapName, this, addrMap, lock);
@@ -2380,10 +2321,6 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
setChanged(ProgramEvent.INT_PROPERTY_MAP_REMOVED, null, mapName);
}
}
finally {
lock.release();
}
}
@Override
@@ -2518,15 +2455,11 @@ public class ProgramDB extends DomainObjectAdapterDB implements Program, ChangeM
if (!(compilerSpec instanceof ProgramCompilerSpec)) {
return;
}
lock.acquire();
try {
try (Closeable c = lock.write()) {
((ProgramCompilerSpec) compilerSpec).installExtensions();
getFunctionManager().invalidateCache(true);
getDataTypeManager().invalidateCache();
}
finally {
lock.release();
}
}
private void registerCompilerSpecOptions() {

Some files were not shown because too many files have changed in this diff Show More