Ghidra is a software reverse engineering (SRE) framework
GP-6193: Environment variables can now be set in launch.properties using ENVVARS= or ENVVARS_[PLATFORM]=
Ryan Kurtz02567fd35550
diff --git a/Ghidra/RuntimeScripts/Common/support/launch.properties b/Ghidra/RuntimeScripts/Common/support/launch.propertiesindex 14f0096465..aaf0472b08 100644--- a/Ghidra/RuntimeScripts/Common/support/launch.properties+++ b/Ghidra/RuntimeScripts/Common/support/launch.properties@@ -24,6 +24,9 @@ VMARGS_LINUX=-Dsun.java2d.uiScale=1VMARGS_LINUX=-Dawt.useSystemAAFontSettings=onVMARGS_WINDOWS=-Dsun.java2d.d3d=false+# Fix blank windows on non-parenting windows managers such as XMonad and Sway+#ENVVARS_LINUX=_JAVA_AWT_WM_NONREPARENTING=1+# The Ghidra application establishes the default SSLContext for all# secure client connections based upon Java's default TLS protocol enablement.# Setting this property will restrict the enabled TLS protocol versions for@@ -42,7 +45,7 @@ VMARGS=-Djdk.tls.client.protocols=TLSv1.2,TLSv1.3# SSL/TLS connections. This VMARG should only be uncommented while actively troubleshooting# since it will log a significant amount of data.#-# See the "Troubleshoting JSSE" section of the following internet hosted document:+# See the "Troubleshooting JSSE" section of the following internet hosted document:# https://docs.oracle.com/en/java/javase/17/security/java-secure-socket-extension-jsse-reference-guide.html## NOTE: When this debug logging is enabled be sure to launch Ghidra with debugGhidra instead ofdiff --git a/Ghidra/RuntimeScripts/Linux/support/launch.sh b/Ghidra/RuntimeScripts/Linux/support/launch.shindex 167835405d..5a8ec3c514 100755--- a/Ghidra/RuntimeScripts/Linux/support/launch.sh+++ b/Ghidra/RuntimeScripts/Linux/support/launch.sh@@ -43,9 +43,9 @@ function showUsage() {exit 1}-VMARGS_FROM_CALLER= # Passed in from the outer script as one long string, no spaces-VMARGS_FROM_LAUNCH_SH=() # Defined in this script, added to array-VMARGS_FROM_LAUNCH_PROPS=() # Retrieved from LaunchSupport, added to array+VMARGS_FROM_CALLER= # Passed in from the outer script as one long string, no spaces+VMARGS_FROM_LAUNCH_SH=() # Defined in this script, added to array+VMARGS_FROM_LAUNCH_PROPS=() # Retrieved from LaunchSupport, added to arrayARGS=()INDEX=0@@ -169,6 +169,15 @@ if [ ! $? -eq 0 ]; thenfiJAVA_CMD="${LS_JAVA_HOME}/bin/java"+# Get the configurable environment variables from the launch properties+# Only set them if they are currently unset or empty+while IFS=$'\r\n' read -r line; do+ IFS='=' read -r key value <<< "$line"+ if [ -z ${!key} ]; then+ export $key=$value+ fi+done < <("${JAVA_CMD}" -cp "${LS_CPATH}" LaunchSupport "${INSTALL_DIR}" -envvars)+# Get the configurable VM arguments from the launch propertieswhile IFS=$'\r\n' read -r line; doVMARGS_FROM_LAUNCH_PROPS+=("$line")diff --git a/Ghidra/RuntimeScripts/Windows/support/launch.bat b/Ghidra/RuntimeScripts/Windows/support/launch.batindex 92f5ffddbb..f317f53dd4 100644--- a/Ghidra/RuntimeScripts/Windows/support/launch.bat+++ b/Ghidra/RuntimeScripts/Windows/support/launch.bat@@ -168,6 +168,16 @@ if "%LS_JAVA_HOME%" == "" ()set "JAVA_CMD=%LS_JAVA_HOME%\bin\java"+:: Get the configurable environment variables from the launch properties+:: Only set them if they are currently undefined+for /f "delims=*" %%i in ('call "%JAVA_CMD%" -cp "%LS_CPATH%" LaunchSupport "%INSTALL_DIR%" -envvars') do (+ for /f "tokens=1* delims==" %%a in ("%%i") do (+ if not defined %%a (+ set %%a=%%b+ )+ )+)+:: Get the configurable VM arguments from the launch propertiesfor /f "delims=*" %%i in ('call "%JAVA_CMD%" -cp "%LS_CPATH%" LaunchSupport "%INSTALL_DIR%" -vmargs') do set VMARG_LIST=!VMARG_LIST! %%idiff --git a/GhidraBuild/LaunchSupport/src/main/java/LaunchSupport.java b/GhidraBuild/LaunchSupport/src/main/java/LaunchSupport.javaindex 414e70fb03..bca5dea6e6 100644--- a/GhidraBuild/LaunchSupport/src/main/java/LaunchSupport.java+++ b/GhidraBuild/LaunchSupport/src/main/java/LaunchSupport.java@@ -44,6 +44,7 @@ public class LaunchSupport {* <li><b>-jdk_home_check: </b> Verify that the specified Java home directory contains a* supported version of java. No output is produced.</li>* <li><b>-vmargs: </b> Get JVM arguments and output on stdout (one per line).</li>+ * <li><b>-envvars: </b> Get environment variables and output on stdout (one per line).</li>* </ul>* Optional arguments supported by -java_home and -jdk_home:* <ul>@@ -114,6 +115,9 @@ public class LaunchSupport {case "-vmargs":exitCode = handleVmArgs(appConfig);break;+ case "-envvars":+ exitCode = handleEnvVars(appConfig);+ break;default:System.err.println("LaunchSupport received illegal argument: " + mode);break;@@ -351,4 +355,24 @@ public class LaunchSupport {appConfig.getLaunchProperties().getVmArgList().forEach(e -> System.out.print(e + "\r\n"));return EXIT_SUCCESS;}++ /**+ * Handles getting the environment variables. If they are successfully determined, they are+ * printed to STDOUT as a new-line delimited string that can be parsed and added to the+ * environment, and an exit code that indicates success is returned.++ * @param appConfig The appConfig configuration that defines what we support.+ * @return A suggested exit code based on whether or not the environment variables were+ * successfully gotten.+ */+ private static int handleEnvVars(AppConfig appConfig) {+ if (appConfig.getLaunchProperties() == null) {+ System.err.println("Launch properties file was not specified!");+ return EXIT_FAILURE;+ }++ // Force newline style to make cross-platform parsing consistent+ appConfig.getLaunchProperties().getEnvVarList().forEach(e -> System.out.print(e + "\r\n"));+ return EXIT_SUCCESS;+ }}diff --git a/GhidraBuild/LaunchSupport/src/main/java/ghidra/launch/LaunchProperties.java b/GhidraBuild/LaunchSupport/src/main/java/ghidra/launch/LaunchProperties.javaindex efac026caf..f0b25fd987 100644--- a/GhidraBuild/LaunchSupport/src/main/java/ghidra/launch/LaunchProperties.java+++ b/GhidraBuild/LaunchSupport/src/main/java/ghidra/launch/LaunchProperties.java@@ -46,6 +46,16 @@ public class LaunchProperties {*/public static String VMARGS_PLATFORM = "VMARGS_" + JavaFinder.getCurrentPlatform();+ /**+ * The environment variables to use to launch (all platforms).+ */+ public static String ENVVARS = "ENVVARS";++ /**+ * The environment variables to use to launch (current platform only).+ */+ public static String ENVVARS_PLATFORM = "ENVVARS_" + JavaFinder.getCurrentPlatform();+private Map<String, List<String>> propertyMap;private File launchPropertiesFile;@@ -132,6 +142,26 @@ public class LaunchProperties {return ret;}+ /**+ * Gets a {@link List} of environment variables to use for the launch for the current+ * {@link Platform platform}.+ *+ * @return A {@link List} of environment variables to use for the launch for the current+ * {@link Platform}+ */+ public List<String> getEnvVarList() {+ List<String> ret = new ArrayList<>();+ List<String> envVarList = propertyMap.get(ENVVARS);+ if (envVarList != null) {+ ret.addAll(envVarList);+ }+ List<String> envVarPlatformList = propertyMap.get(ENVVARS_PLATFORM);+ if (envVarPlatformList != null) {+ ret.addAll(envVarPlatformList);+ }+ return ret;+ }+/*** Parses and gets the launch properties from the given launch properties file.*diff --git a/GhidraDocs/GettingStarted.md b/GhidraDocs/GettingStarted.mdindex b16663fae5..9c8e0871c7 100644--- a/GhidraDocs/GettingStarted.md+++ b/GhidraDocs/GettingStarted.md@@ -1,5 +1,5 @@# Getting Started with Ghidra-The information provided in this document is effective as of Ghidra 12.0 and is subject to change+The information provided in this document is effective as of Ghidra 12.1 and is subject to changewith future releases.## Table of Contents@@ -458,9 +458,10 @@ There are several ways you can get help with using Ghidra:* GUI icons may not render correctly in some configurations of Linux. Setting`VMARGS=-Dsun.java2d.opengl` to `true` in `<GhidraInstallDir>/support/launch.properties` may fixthis issue.-* Non-reparenting window managers such as Xmonad and Sway may render Ghidra windows as blank. This- is a known java issue (https://bugs.openjdk.org/browse/JDK-8058197) which can only be addressed- by setting the environment variable `_JAVA_AWT_WM_NONREPARENTING=1` prior to launching Ghidra.+* Non-reparenting window managers such as XMonad and Sway may render Ghidra windows as blank. This+ is a known java issue (https://bugs.openjdk.org/browse/JDK-8058197) which can be addressed by+ uncommenting `#ENVVARS_LINUX=_JAVA_AWT_WM_NONREPARENTING=1` in+ `<GhidraInstallDir>/support/launch.properties`.### macOS* Building new Ghidra module extensions on macOS (OS X) using a network drive (including a