Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7238: Finding Swift demangler with GHIDRA_SWIFT_DEMANGLER env var, if not found at a known location

Ryan Kurtz6d4ec0120094
diff --git a/Ghidra/Features/SwiftDemangler/README.md b/Ghidra/Features/SwiftDemangler/README.md
index e8e34ce128..cdceebd467 100644
--- a/Ghidra/Features/SwiftDemangler/README.md
+++ b/Ghidra/Features/SwiftDemangler/README.md
@@ -21,4 +21,8 @@ protocol descriptor for SwiftUI.View
The resulting tree is parsed by the Ghidra Swift Demangler to form and apply a demangled symbol
name.
-The `Demangler Swift` Analyzer assumes that the native Swift Demangler is on the `PATH`.
+The `Demangler Swift` Analyzer looks for the path to the native Swift Demangler binary
+in the `GHIDRA_SWIFT_DEMANGLER` environment variable. If that environment variable is not set, the
+following paths will be tried on non-Windows platforms:
+* `/usr/bin/swift-demangle`
+* `/usr/bin/swift`
diff --git a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/plugin/core/analysis/SwiftDemanglerAnalyzer.java b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/plugin/core/analysis/SwiftDemanglerAnalyzer.java
index aeb1beddf4..1fc643306f 100644
--- a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/plugin/core/analysis/SwiftDemanglerAnalyzer.java
+++ b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/plugin/core/analysis/SwiftDemanglerAnalyzer.java
@@ -35,7 +35,9 @@ public class SwiftDemanglerAnalyzer extends AbstractDemanglerAnalyzer {
private static final String NAME = "Demangler Swift";
private static final String DESCRIPTION =
- "Demangles Swift symbols and applies appropriate datatype and calling conventions where possible. Requires Swift to be on the PATH.";
+ "Demangles Swift symbols and applies appropriate datatype and calling conventions where " +
+ "possible. Requires native Swift demangler to be on system in known location or " +
+ "pointed to by the GHIDRA_SWIFT_DEMANGLER environment variable.";
private static final String OPTION_NAME_INCOMPLETE_PREFIX =
"Use incomplete demangle label prefix (%s)"
@@ -105,7 +107,8 @@ public class SwiftDemanglerAnalyzer extends AbstractDemanglerAnalyzer {
catch (IOException e) {
log.appendMsg(e.getMessage());
log.appendMsg("You must have Swift installed to demangle Swift symbols.\n" +
- "See the \"Demangler Swift\" analyzer options to configure.");
+ " * If installed but not found, set the GHIDRA_SWIFT_DEMANGLER environment\n" +
+ " variable to the full path of the native Swift demangler (swift/swift-demangle)");
}
}
return false;
diff --git a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftNativeDemangler.java b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftNativeDemangler.java
index 0fa5a5758c..88c8139c55 100644
--- a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftNativeDemangler.java
+++ b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftNativeDemangler.java
@@ -20,6 +20,11 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+
+import ghidra.framework.Application;
+import ghidra.framework.OperatingSystem;
+
/**
* A class used to launch the Swift native demangler.
* <p>
@@ -34,7 +39,7 @@ import java.util.stream.Collectors;
*/
public class SwiftNativeDemangler {
- private String nativeDemanglerCmd;
+ private String nativeDemanglerPath;
private boolean standaloneDemanglerBinary;
/**
@@ -57,18 +62,25 @@ public class SwiftNativeDemangler {
* @throws IOException if there was a problem finding or running the Swift native demangler
*/
public SwiftNativeDemangler() throws IOException {
- List<String> demanglerNames = List.of("swift-demangle", "swift");
+ List<String> demanglerPaths = new ArrayList<>();
+ String envVar = System.getenv("GHIDRA_SWIFT_DEMANGLER");
+ if (StringUtils.isNotBlank(envVar)) {
+ demanglerPaths.add(envVar);
+ }
+ if (OperatingSystem.CURRENT_OPERATING_SYSTEM != OperatingSystem.WINDOWS) {
+ demanglerPaths.add("/usr/bin/swift-demangle");
+ demanglerPaths.add("/usr/bin/swift");
+ }
IOException ioe = null;
- for (String demanglerName : demanglerNames) {
- nativeDemanglerCmd = demanglerName; // expect swift demangler to be on the PATH
+ for (String path : demanglerPaths) {
try {
- int exitCode =
- new ProcessBuilder(List.of(nativeDemanglerCmd, "--version")).start().waitFor();
+ Process p = new ProcessBuilder(List.of(path, "--version")).start();
+ int exitCode = p.waitFor();
if (exitCode == 0) {
ioe = null;
- standaloneDemanglerBinary =
- new File(nativeDemanglerCmd).getName().contains("-demangle");
- break;
+ nativeDemanglerPath = path;
+ standaloneDemanglerBinary = new File(path).getName().contains("-demangle");
+ return;
}
ioe = new IOException("Native Swift demangler exited with code: " + exitCode);
}
@@ -79,9 +91,10 @@ public class SwiftNativeDemangler {
ioe = new IOException(e);
}
}
- if (ioe != null) {
- throw ioe;
+ if (ioe == null) {
+ ioe = new IOException("Native Swift demangler not found.");
}
+ throw ioe;
}
/**
@@ -139,14 +152,15 @@ public class SwiftNativeDemangler {
*/
private BufferedReader demangle(String mangled, List<String> options) throws IOException {
List<String> command = new ArrayList<>();
- command.add(nativeDemanglerCmd);
+ command.add(nativeDemanglerPath);
if (!standaloneDemanglerBinary) {
command.add("demangle");
}
command.addAll(options);
command.add("--"); // indicate end of flags
command.add(mangled);
- Process p = new ProcessBuilder(command).redirectErrorStream(true).start();
+ File dir = Application.getInstallationDirectory().getFile(false);
+ Process p = new ProcessBuilder(command).redirectErrorStream(true).directory(dir).start();
return new BufferedReader(new InputStreamReader(p.getInputStream()));
}
}
diff --git a/Ghidra/RuntimeScripts/Common/support/launch.properties b/Ghidra/RuntimeScripts/Common/support/launch.properties
index 42e00fdba8..feb083c23e 100644
--- a/Ghidra/RuntimeScripts/Common/support/launch.properties
+++ b/Ghidra/RuntimeScripts/Common/support/launch.properties
@@ -169,3 +169,7 @@ VMARGS=--enable-native-access=ALL-UNNAMED
# Prevents PyGhidra from restoring sys.modules to its original state after a PyGhidra script is run.
# Used to help debug potential issues that might arise from mucking with sys.modules.
#VMARGS=-Dpyghidra.sys.modules.restore.disable=true
+
+# Full path to the native Swift demangler binary. This is typically named "swift", but some Swift
+# installations also provide a standalone "swift-demangle" binary.
+#ENVVARS=GHIDRA_SWIFT_DEMANGLER=