Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2987: No longer using eval in launch.sh

Ryan Kurtz1640b544c3d9
diff --git a/Ghidra/RuntimeScripts/Linux/support/launch.sh b/Ghidra/RuntimeScripts/Linux/support/launch.sh
index 3340dfeef3..665cd4a4c3 100755
--- a/Ghidra/RuntimeScripts/Linux/support/launch.sh
+++ b/Ghidra/RuntimeScripts/Linux/support/launch.sh
@@ -32,8 +32,8 @@ function showUsage() {
echo " <max-memory>: maximum memory heap size in MB (e.g., 768M or 2G). Use empty \"\" if default"
echo " should be used. This will generally be upto 1/4 of the physical memory available"
echo " to the OS."
- echo " <vmarg-list>: pass-thru args (e.g., \"-Xmx512M -Dmyvar=1 -DanotherVar=2\") - use"
- echo " empty \"\" if vmargs not needed"
+ echo " <vmarg-list>: pass-thru args (e.g., \"-Xmx512M -Dmyvar=1 -DanotherVar=2\"). Use"
+ echo " empty \"\" if vmargs not needed. Spaces are not supported."
echo " <app-classname>: application classname (e.g., ghidra.GhidraRun )"
echo " <app-args>...: arguments to be passed to the application"
echo " "
@@ -44,10 +44,12 @@ function showUsage() {
}
-VMARG_LIST=
+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
+
ARGS=()
INDEX=0
-
WHITESPACE="[[:space:]]"
for AA in "$@"
@@ -72,17 +74,13 @@ do
;;
5)
if [ "$AA" != "" ]; then
- VMARG_LIST=$AA
+ VMARGS_FROM_CALLER=$AA
fi
;;
6)
CLASSNAME=$AA
;;
*)
- # Preserve quoted arguments
- if [[ $AA =~ $WHITESPACE ]]; then
- AA="\"$AA\""
- fi
ARGS[${#ARGS[@]}]=$AA
;;
esac
@@ -147,20 +145,21 @@ fi
JAVA_CMD="${JAVA_HOME}/bin/java"
# Get the configurable VM arguments from the launch properties
-VMARG_LIST+=" $(java -cp "${LS_CPATH}" LaunchSupport "${INSTALL_DIR}" -vmargs)"
+while IFS= read -r line; do
+ VMARGS_FROM_LAUNCH_PROPS+=("$line")
+done < <(java -cp "${LS_CPATH}" LaunchSupport "${INSTALL_DIR}" -vmargs)
# Add extra macOS VM arguments
if [ "$(uname -s)" = "Darwin" ]; then
- VMARG_LIST+=" -Xdock:name=${APPNAME}"
+ VMARGS_FROM_LAUNCH_SH+=("-Xdock:name=${APPNAME}")
fi
# Set Max Heap Size if specified
if [ "${MAXMEM}" != "" ]; then
- VMARG_LIST+=" -Xmx${MAXMEM}"
+ VMARGS_FROM_LAUNCH_SH+=("-Xmx${MAXMEM}")
fi
BACKGROUND=false
-
if [ "${MODE}" = "debug" ] || [ "${MODE}" = "debug-suspend" ]; then
SUSPEND=n
@@ -173,8 +172,8 @@ if [ "${MODE}" = "debug" ] || [ "${MODE}" = "debug-suspend" ]; then
SUSPEND=y
fi
- VMARG_LIST+=" -Dlog4j.configurationFile=\"${DEBUG_LOG4J}\""
- VMARG_LIST+=" -agentlib:jdwp=transport=dt_socket,server=y,suspend=${SUSPEND},address=${DEBUG_ADDRESS}"
+ VMARGS_FROM_LAUNCH_SH+=("-Dlog4j.configurationFile=${DEBUG_LOG4J}")
+ VMARGS_FROM_LAUNCH_SH+=("-agentlib:jdwp=transport=dt_socket,server=y,suspend=${SUSPEND},address=${DEBUG_ADDRESS}")
elif [ "${MODE}" = "fg" ]; then
@@ -189,7 +188,7 @@ else
fi
if [ "${BACKGROUND}" = true ]; then
- eval "\"${JAVA_CMD}\" ${VMARG_LIST} -showversion -cp \"${CPATH}\" ghidra.Ghidra ${CLASSNAME} ${ARGS[@]}" &>/dev/null &
+ "${JAVA_CMD}" "${VMARGS_FROM_LAUNCH_PROPS[@]}" "${VMARGS_FROM_LAUNCH_SH[@]}" ${VMARGS_FROM_CALLER} -showversion -cp "${CPATH}" ghidra.Ghidra ${CLASSNAME} "${ARGS[@]}" &>/dev/null &
# If our process dies immediately, output something so the user knows to run in debug mode.
# Otherwise they'll never see any error output from background mode.
@@ -202,7 +201,7 @@ if [ "${BACKGROUND}" = true ]; then
fi
exit 0
else
- eval "(set -o noglob; \"${JAVA_CMD}\" ${VMARG_LIST} -showversion -cp \"${CPATH}\" ghidra.Ghidra ${CLASSNAME} ${ARGS[@]})"
+ set -o noglob; "${JAVA_CMD}" "${VMARGS_FROM_LAUNCH_PROPS[@]}" "${VMARGS_FROM_LAUNCH_SH[@]}" ${VMARGS_FROM_CALLER} -showversion -cp "${CPATH}" ghidra.Ghidra ${CLASSNAME} "${ARGS[@]}"
exit $?
fi
diff --git a/GhidraBuild/LaunchSupport/src/main/java/LaunchSupport.java b/GhidraBuild/LaunchSupport/src/main/java/LaunchSupport.java
index f4ad7e4252..a2df286c51 100644
--- a/GhidraBuild/LaunchSupport/src/main/java/LaunchSupport.java
+++ b/GhidraBuild/LaunchSupport/src/main/java/LaunchSupport.java
@@ -278,8 +278,8 @@ public class LaunchSupport {
/**
* Handles getting the VM arguments. If they are successfully determined, they are printed
- * to STDOUT as a string that can be added to the command line, and an exit code that
- * indicates success is returned.
+ * to STDOUT as a new-line delimited string that can be parsed and added to the command line,
+ * and an exit code that indicates success is returned.
* @param javaConfig The Java configuration that defines what we support.
* @return A suggested exit code based on whether or not the VM arguments were successfully
@@ -291,7 +291,7 @@ public class LaunchSupport {
return EXIT_FAILURE;
}
- System.out.println(javaConfig.getLaunchProperties().getVmArgs());
+ javaConfig.getLaunchProperties().getVmArgList().forEach(arg -> System.out.println(arg));
return EXIT_SUCCESS;
}
}
diff --git a/GhidraBuild/LaunchSupport/src/main/java/ghidra/launch/LaunchProperties.java b/GhidraBuild/LaunchSupport/src/main/java/ghidra/launch/LaunchProperties.java
index 888f813512..d82634f0a8 100644
--- a/GhidraBuild/LaunchSupport/src/main/java/ghidra/launch/LaunchProperties.java
+++ b/GhidraBuild/LaunchSupport/src/main/java/ghidra/launch/LaunchProperties.java
@@ -100,6 +100,26 @@ public class LaunchProperties {
return sb.toString();
}
+ /**
+ * Gets a {@link List} of VM arguments to use for the launch for the current
+ * {@link Platform platform}.
+ *
+ * @return A {@link List} of VM arguments to use for the launch for the current
+ * {@link Platform}
+ */
+ public List<String> getVmArgList() {
+ List<String> ret = new ArrayList<>();
+ List<String> vmargList = propertyMap.get(VMARGS);
+ if (vmargList != null) {
+ ret.addAll(vmargList);
+ }
+ List<String> vmargPlatformList = propertyMap.get(VMARGS_PLATFORM);
+ if (vmargPlatformList != null) {
+ ret.addAll(vmargPlatformList);
+ }
+ return ret;
+ }
+
/**
* Parses and gets the launch properties from the given launch properties file.
*