From 42b73252f1522e283aee80409037690742745922 Mon Sep 17 00:00:00 2001
From: Dan <46821332+nsadeveloper789@users.noreply.github.com>
Date: Wed, 28 Aug 2024 11:19:29 -0400
Subject: [PATCH] GP-4876: JShell launcher for Ghidra
---
Ghidra/Features/Base/.launch/JShell.launch | 34 +++++++++++++
Ghidra/Features/Base/certification.manifest | 1 +
.../Base/src/main/java/ghidra/JShellRun.java | 49 +++++++++++++++++++
Ghidra/RuntimeScripts/Linux/support/jshellRun | 19 +++++++
.../Windows/support/jshellRun.bat | 11 +++++
Ghidra/RuntimeScripts/certification.manifest | 2 +
6 files changed, 116 insertions(+)
create mode 100644 Ghidra/Features/Base/.launch/JShell.launch
create mode 100644 Ghidra/Features/Base/src/main/java/ghidra/JShellRun.java
create mode 100755 Ghidra/RuntimeScripts/Linux/support/jshellRun
create mode 100644 Ghidra/RuntimeScripts/Windows/support/jshellRun.bat
diff --git a/Ghidra/Features/Base/.launch/JShell.launch b/Ghidra/Features/Base/.launch/JShell.launch
new file mode 100644
index 0000000000..c657097019
--- /dev/null
+++ b/Ghidra/Features/Base/.launch/JShell.launch
@@ -0,0 +1,34 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Ghidra/Features/Base/certification.manifest b/Ghidra/Features/Base/certification.manifest
index 8f6c07bb61..c053587a20 100644
--- a/Ghidra/Features/Base/certification.manifest
+++ b/Ghidra/Features/Base/certification.manifest
@@ -12,6 +12,7 @@
.launch/Ghidra Code Coverage.launch||GHIDRA||||END|
.launch/Ghidra.launch||GHIDRA||||END|
.launch/Headless.launch||GHIDRA||||END|
+.launch/JShell.launch||GHIDRA||||END|
Module.manifest||GHIDRA||||END|
data/ElfFunctionsThatDoNotReturn||GHIDRA||||END|
data/ExtensionPoint.manifest||GHIDRA||||END|
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/JShellRun.java b/Ghidra/Features/Base/src/main/java/ghidra/JShellRun.java
new file mode 100644
index 0000000000..c8ccbf5afb
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/JShellRun.java
@@ -0,0 +1,49 @@
+/* ###
+ * 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;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.stream.Stream;
+
+import ghidra.framework.*;
+import jdk.jshell.tool.JavaShellToolBuilder;
+
+public class JShellRun implements GhidraLaunchable {
+ @Override
+ public void launch(GhidraApplicationLayout layout, String[] args) throws Exception {
+ if (Stream.of(args).anyMatch(a -> a.startsWith("--execution"))) {
+ System.err.println("Ignoring --execution option. Overridden to local");
+ }
+
+ ArrayList fullArgs = new ArrayList<>();
+ fullArgs.addAll(Arrays.asList(args));
+
+ ApplicationConfiguration configuration;
+ if (fullArgs.remove("--headless")) {
+ configuration = new HeadlessGhidraApplicationConfiguration();
+ }
+ else {
+ GhidraApplicationConfiguration gac = new GhidraApplicationConfiguration();
+ gac.setShowSplashScreen(false);
+ configuration = gac;
+ }
+ Application.initializeApplication(layout, configuration);
+
+ fullArgs.add("--execution=local");
+ JavaShellToolBuilder.builder().start(fullArgs.toArray(String[]::new));
+ }
+}
diff --git a/Ghidra/RuntimeScripts/Linux/support/jshellRun b/Ghidra/RuntimeScripts/Linux/support/jshellRun
new file mode 100755
index 0000000000..c9a5a1a3f8
--- /dev/null
+++ b/Ghidra/RuntimeScripts/Linux/support/jshellRun
@@ -0,0 +1,19 @@
+#!/usr/bin/env bash
+
+#----------------------------------------
+# Ghidra JShell launch
+#----------------------------------------
+
+# Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
+# the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
+#MAXMEM=2G
+
+# Resolve symbolic link if present and get the directory this script lives in.
+# NOTE: "readlink -f" is best but works on Linux only, "readlink" will only work if your PWD
+# contains the link you are calling (which is the best we can do on macOS), and the "echo" is the
+# fallback, which doesn't attempt to do anything with links.
+SCRIPT_FILE="$(readlink -f "$0" 2>/dev/null || readlink "$0" 2>/dev/null || echo "$0")"
+SCRIPT_DIR="${SCRIPT_FILE%/*}"
+
+# Launch Ghidra
+"${SCRIPT_DIR}"/launch.sh fg jdk Ghidra-JShell "${MAXMEM}" "" ghidra.JShellRun "$@"
diff --git a/Ghidra/RuntimeScripts/Windows/support/jshellRun.bat b/Ghidra/RuntimeScripts/Windows/support/jshellRun.bat
new file mode 100644
index 0000000000..37238450c0
--- /dev/null
+++ b/Ghidra/RuntimeScripts/Windows/support/jshellRun.bat
@@ -0,0 +1,11 @@
+:: Ghidra JShell launch
+
+@echo off
+setlocal
+
+:: Maximum heap memory may be changed if default is inadequate. This will generally be up to 1/4 of
+:: the physical memory available to the OS. Uncomment MAXMEM setting if non-default value is needed.
+::set MAXMEM=2G
+
+call "%~dp0launch.bat" fg jdk Ghidra-JShell "%MAXMEM%" "" ghidra.JShellRun %*
+
diff --git a/Ghidra/RuntimeScripts/certification.manifest b/Ghidra/RuntimeScripts/certification.manifest
index be6ade10e3..860d43bd8e 100644
--- a/Ghidra/RuntimeScripts/certification.manifest
+++ b/Ghidra/RuntimeScripts/certification.manifest
@@ -26,6 +26,7 @@ Linux/support/convertStorage||GHIDRA||||END|
Linux/support/gdbGADPServerRun||GHIDRA||||END|
Linux/support/ghidraClean||GHIDRA||||END|
Linux/support/ghidraDebug||GHIDRA||||END|
+Linux/support/jshellRun||GHIDRA||||END|
Linux/support/pythonRun||GHIDRA||||END|
Linux/support/sleigh||GHIDRA||||END|
Windows/ghidraRun.bat||GHIDRA||||END|
@@ -45,6 +46,7 @@ Windows/support/dbgmodelGADPServerRun.bat||GHIDRA||||END|
Windows/support/ghidra.ico||GHIDRA||||END|
Windows/support/ghidraClean.bat||GHIDRA||||END|
Windows/support/ghidraDebug.bat||GHIDRA||||END|
+Windows/support/jshellRun.bat||GHIDRA||||END|
Windows/support/launch.bat||GHIDRA||||END|
Windows/support/pythonRun.bat||GHIDRA||||END|
Windows/support/sleigh.bat||GHIDRA||||END|