Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-154 Fix FS refhandle leakage, and loadspec stream stuff

dev747368d3fdaa56722a
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/CsHintLoadSpecChooser.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/CsHintLoadSpecChooser.java
index b877fcd0ef..2b80b34c6e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/CsHintLoadSpecChooser.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/CsHintLoadSpecChooser.java
@@ -49,8 +49,9 @@ public class CsHintLoadSpecChooser implements LoadSpecChooser {
return loaderMap.values()
.stream()
.flatMap(loadSpec -> loadSpec.stream())
- .filter(loadSpec -> loadSpec.getLanguageCompilerSpec().compilerSpecID
- .equals(compilerSpecID))
+ .filter(
+ loadSpec -> loadSpec != null && loadSpec.getLanguageCompilerSpec() != null &&
+ loadSpec.getLanguageCompilerSpec().compilerSpecID.equals(compilerSpecID))
.findFirst()
.orElse(null);
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FSRL.java b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FSRL.java
index 3e97704001..a01458e1cd 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FSRL.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FSRL.java
@@ -477,21 +477,12 @@ public class FSRL {
return false;
}
}
- else if (!parent.equals(other.parent)) {
+ else if (!parent.isEquivalent(other.parent)) {
return false;
}
// Path
- if (path == null) {
- if (other.path != null) {
- return false;
- }
- }
- else if (!path.equals(other.path)) {
- return false;
- }
-
- return true;
+ return Objects.equals(path, other.path);
}
/**
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemCache.java b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemCache.java
index f8ce58fa3d..0483c9a545 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemCache.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemCache.java
@@ -104,7 +104,9 @@ public class FileSystemCache implements FileSystemEventListener {
public synchronized void add(GFileSystem fs) {
FSCacheInfo fsci = new FSCacheInfo(fs);
fs.getRefManager().addListener(this);
- filesystems.put(fs.getFSRL(), fsci);
+ if (filesystems.put(fs.getFSRL(), fsci) != null) {
+ Msg.warn(this, "Added second instance of same filesystem! " + fs.getFSRL());
+ }
}
/**
@@ -121,7 +123,19 @@ public class FileSystemCache implements FileSystemEventListener {
return rootFS.getRefManager().create();
}
FSCacheInfo fsci = filesystems.get(fsrl);
- return (fsci != null) ? fsci.ref.dup() : null;
+ if (fsci != null) {
+ return fsci.ref.dup();
+ }
+ // If the query FSRL doesn't have a MD5, do a slow scan
+ // for filesystems that match by equiv.
+ if (fsrl.getMD5() == null) {
+ for (Entry<FSRLRoot, FSCacheInfo> entry : filesystems.entrySet()) {
+ if (entry.getKey().isEquivalent(fsrl)) {
+ return entry.getValue().ref.dup();
+ }
+ }
+ }
+ return null;
}
/**
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java
index af69bdf49e..35ed602834 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java
@@ -233,6 +233,12 @@ public class FileSystemService {
throw new IOException("Invalid FSRL specified: " + fsrl);
}
String md5 = fsrl.getMD5();
+ if (md5 == null && fsrl.getNestingDepth() == 1) {
+ File f = localFS.getLocalFile(fsrl);
+ if (f.isFile()) {
+ md5 = fileFingerprintCache.getMD5(f.getPath(), f.lastModified(), f.length());
+ }
+ }
FSRLRoot fsRoot = fsrl.getFS();
FileCacheEntry result = (md5 != null) ? fileCache.getFile(md5) : null;
@@ -267,6 +273,14 @@ public class FileSystemService {
", md5 now " + result.md5);
}
}
+ if (fsrl.getNestingDepth() == 1) {
+ File f = localFS.getLocalFile(fsrl);
+ if (f.isFile()) {
+ fileFingerprintCache.add(f.getPath(), result.md5, f.lastModified(),
+ f.length());
+ }
+ }
+
}
}