Ghidra is a software reverse engineering (SRE) framework
GP-3653: Protecting parallel OSGi operations with FileLock
Ryan Kurtze1d60727fc4b
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiParallelLock.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiParallelLock.javanew file mode 100644index 0000000000..54b373e28e--- /dev/null+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiParallelLock.java@@ -0,0 +1,56 @@+/* ###+ * 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.app.plugin.core.osgi;++import java.io.*;++import ghidra.formats.gfilesystem.FSUtilities;+import ghidra.util.Msg;+import utilities.util.FileUtilities;++/**+ * A file-based lock used to protect modifications to OSGi shared resources from other+ * instances of Ghidra running in parallel+ */+public class OSGiParallelLock implements Closeable {++ private static final String LOCK_NAME = "parallel.lock";++ private File lockFile;+ private FileOutputStream fos;++ /**+ * Creates a new OSGi file-based lock+ */+ public OSGiParallelLock() {+ File osgiDir = BundleHost.getOsgiDir().toFile();+ FileUtilities.mkdirs(osgiDir);+ lockFile = new File(osgiDir, LOCK_NAME);++ try {+ fos = new FileOutputStream(lockFile);+ fos.getChannel().lock();+ }+ catch (IOException e) {+ Msg.error(this, "Failed to open OSGi parallel lock: " + lockFile, e);+ }+ }++ @Override+ public void close() {+ FSUtilities.uncheckedClose(fos, "Failed to close OSGi parallel lock: " + lockFile);+ }+}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.javaindex ea621c07aa..542097febf 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java@@ -25,8 +25,7 @@ import java.util.stream.Collectors;import java.util.stream.Stream;import generic.jar.ResourceFile;-import ghidra.app.plugin.core.osgi.BundleHost;-import ghidra.app.plugin.core.osgi.OSGiException;+import ghidra.app.plugin.core.osgi.*;import ghidra.app.plugin.core.script.GhidraScriptMgrPlugin;import ghidra.app.util.headless.HeadlessAnalyzer;import ghidra.framework.Application;@@ -73,7 +72,7 @@ public class GhidraScriptUtil {throw new RuntimeException("GhidraScriptUtil initialized multiple times!");}- try {+ try (OSGiParallelLock lock = new OSGiParallelLock()) {bundleHost = aBundleHost;bundleHost.startFramework();}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.javaindex 01d18ec87f..e66ee56237 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java@@ -87,7 +87,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {@Overridepublic GhidraScript getScriptInstance(ResourceFile sourceFile, PrintWriter writer)throws GhidraScriptLoadException {- try {+ try (OSGiParallelLock lock = new OSGiParallelLock()) {Class<?> clazz = loadClass(sourceFile, writer);if (GhidraScript.class.isAssignableFrom(clazz)) {