Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5182: The builtin int type in Swift binaries has been changed from 8 bytes to 4 bytes. The Swift Demangler now demangles the Swift.Int type to __int64 (or __int32) to avoid conflicts with non-Swift structures and functions that may be found in the program.

Ryan Kurtze1e54a4d891a
diff --git a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftDemangledTree.java b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftDemangledTree.java
index 6368fc73c4..163de74d0e 100644
--- a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftDemangledTree.java
+++ b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftDemangledTree.java
@@ -4,9 +4,9 @@
* 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.
@@ -79,9 +79,10 @@ public class SwiftDemangledTree {
*
* @param nativeDemangler The Swift native demangler
* @param mangled The mangled string
+ * @param is64bit Whether or not the mangled string is from a 64-bit program
* @throws DemangledException If there was an issue demangling
*/
- public SwiftDemangledTree(SwiftNativeDemangler nativeDemangler, String mangled)
+ public SwiftDemangledTree(SwiftNativeDemangler nativeDemangler, String mangled, boolean is64bit)
throws DemangledException {
SwiftNativeDemangledOutput demangledOutput;
try {
@@ -100,12 +101,12 @@ public class SwiftDemangledTree {
SwiftNode node;
try {
NodeProperties properties = new NodeProperties(SwiftDemangledNodeKind.valueOf(kind),
- text, index, depth, mangled, demangledString);
+ text, index, depth, mangled, demangledString, is64bit);
node = SwiftNode.get(properties);
}
catch (IllegalArgumentException e) {
NodeProperties properties = new NodeProperties(SwiftDemangledNodeKind.Unsupported,
- text, index, depth, mangled, demangledString);
+ text, index, depth, mangled, demangledString, is64bit);
node = new SwiftUnsupportedNode(kind, properties);
}
if (node.getDepth() == 0) {
diff --git a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftDemangler.java b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftDemangler.java
index 5d74cc09bc..d49cf4eb58 100644
--- a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftDemangler.java
+++ b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/SwiftDemangler.java
@@ -36,6 +36,7 @@ public class SwiftDemangler implements Demangler {
private Map<String, SwiftNode> cache;
private SwiftTypeMetadata typeMetadata;
private SwiftNativeDemangler nativeDemangler;
+ private boolean is64bit = true;
/**
* Creates a new {@link SwiftDemangler} that is not associated with any {@link Program}.
@@ -81,6 +82,7 @@ public class SwiftDemangler implements Demangler {
program.setPreferredRootNamespaceCategoryPath(
SwiftDataTypeUtils.SWIFT_CATEGORY.getPath());
typeMetadata = new SwiftTypeMetadata(program, TaskMonitor.DUMMY, new MessageLog());
+ is64bit = program.getDefaultPointerSize() == 8;
}
}
catch (CancelledException e) {
@@ -129,7 +131,7 @@ public class SwiftDemangler implements Demangler {
setSwiftNativeDemangler(options);
SwiftNode root = cache.containsKey(mangled) ? cache.get(mangled)
- : new SwiftDemangledTree(nativeDemangler, mangled).getRoot();
+ : new SwiftDemangledTree(nativeDemangler, mangled, is64bit).getRoot();
cache.put(mangled, root);
if (root == null) {
return null;
diff --git a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/nodes/SwiftNode.java b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/nodes/SwiftNode.java
index ae76fee8a0..1d263b4d56 100644
--- a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/nodes/SwiftNode.java
+++ b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/nodes/SwiftNode.java
@@ -4,9 +4,9 @@
* 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.
@@ -44,9 +44,10 @@ public abstract class SwiftNode {
* 0)
* @param mangled The mangled string associated with this {@link SwiftNode}
* @param originalDemangled The natively demangled string
+ * @param is64bit Whether or not the mangled string is from a 64-bit program
*/
public record NodeProperties(SwiftDemangledNodeKind kind, String text, String index,
- int depth, String mangled, String originalDemangled) {}
+ int depth, String mangled, String originalDemangled, boolean is64bit) {}
/**
* Gets a new {@link SwiftNode} with the given with the given {@link NodeProperties}
diff --git a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/nodes/SwiftStructureNode.java b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/nodes/SwiftStructureNode.java
index 9d20bb2a3c..3bb23d340a 100644
--- a/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/nodes/SwiftStructureNode.java
+++ b/Ghidra/Features/SwiftDemangler/src/main/java/ghidra/app/util/demangler/swift/nodes/SwiftStructureNode.java
@@ -4,9 +4,9 @@
* 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.
@@ -57,15 +57,17 @@ public class SwiftStructureNode extends SwiftNode {
String mangled = properties.mangled();
String orig = properties.originalDemangled();
+ String intDataType =
+ properties.is64bit() ? DemangledDataType.INT64 : DemangledDataType.INT32;
if (SwiftDataTypeUtils.isSwiftNamespace(namespace)) {
DemangledDataType type = switch (name) {
case "Bool" -> new SwiftPrimitive(mangled, orig, DemangledDataType.BOOL);
- case "Int" -> new SwiftPrimitive(mangled, orig, DemangledDataType.INT);
+ case "Int" -> new SwiftPrimitive(mangled, orig, intDataType);
case "Int8" -> new SwiftPrimitive(mangled, orig, DemangledDataType.INT8);
case "Int16" -> new SwiftPrimitive(mangled, orig, DemangledDataType.INT16);
case "Int32" -> new SwiftPrimitive(mangled, orig, DemangledDataType.INT32);
case "Int64" -> new SwiftPrimitive(mangled, orig, DemangledDataType.INT64);
- case "UInt" -> new SwiftPrimitive(mangled, orig, DemangledDataType.INT, true);
+ case "UInt" -> new SwiftPrimitive(mangled, orig, intDataType, true);
case "UInt8" -> new SwiftPrimitive(mangled, orig, DemangledDataType.INT8, true);
case "UInt16" -> new SwiftPrimitive(mangled, orig, DemangledDataType.INT16, true);
case "UInt32" -> new SwiftPrimitive(mangled, orig, DemangledDataType.INT32, true);
diff --git a/Ghidra/Features/SwiftDemangler/src/test/java/ghidra/app/util/demangler/swift/SwiftDemanglerTest.java b/Ghidra/Features/SwiftDemangler/src/test/java/ghidra/app/util/demangler/swift/SwiftDemanglerTest.java
index 6971dd9e20..7ad9f33d95 100644
--- a/Ghidra/Features/SwiftDemangler/src/test/java/ghidra/app/util/demangler/swift/SwiftDemanglerTest.java
+++ b/Ghidra/Features/SwiftDemangler/src/test/java/ghidra/app/util/demangler/swift/SwiftDemanglerTest.java
@@ -197,7 +197,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
String mangled =
"_$s18SwiftDemanglerTest25testJunitFunctionAndTypes_6label2___________7label14____Si_SftSiz_s4Int8Vs5Int16Vs5Int32Vs5Int64VSus5UInt8Vs6UInt16Vs6UInt32Vs6UInt64VS2fS2dSSSaySiGSbSJtF";
String demangled =
- "struct tuple2 default SwiftDemanglerTest::testJunitFunctionAndTypes(int *,__int8 label2,__int16,__int32,__int64,unsigned int,unsigned __int8,unsigned __int16,unsigned __int32,unsigned __int64,float,float,double,double label14,struct Swift::String,Swift::Array<int>[],bool,struct Swift::Character)";
+ "struct tuple2 default SwiftDemanglerTest::testJunitFunctionAndTypes(__int64 *,__int8 label2,__int16,__int32,__int64,unsigned __int64,unsigned __int8,unsigned __int16,unsigned __int32,unsigned __int64,float,float,double,double label14,struct Swift::String,Swift::Array<__int64>[],bool,struct Swift::Character)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -208,7 +208,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
throw new AssertException("Demangled return type is not a structure");
}
assertEquals(struct.getFields().size(), 2);
- assertEquals(struct.getFields().get(0).type().toString(), "int");
+ assertEquals(struct.getFields().get(0).type().toString(), "__int64");
assertEquals(struct.getFields().get(1).type().toString(), "float");
}
@@ -241,7 +241,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest11MyStructureV6label1ACSi_tcfC";
String demangled =
- "struct SwiftDemanglerTest::MyStructure default SwiftDemanglerTest::MyStructure::init(int label1)";
+ "struct SwiftDemanglerTest::MyStructure default SwiftDemanglerTest::MyStructure::init(__int64 label1)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -285,7 +285,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest11MyStructureV8myMethod6label16label2S2i_SitF";
String demangled =
- "int default SwiftDemanglerTest::MyStructure::myMethod(int label1,int label2,struct SwiftDemanglerTest::MyStructure)";
+ "__int64 default SwiftDemanglerTest::MyStructure::myMethod(__int64 label1,__int64 label2,struct SwiftDemanglerTest::MyStructure)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -311,7 +311,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest11MyStructureV1zSivg";
String demangled =
- "int default SwiftDemanglerTest::MyStructure::get_z(struct SwiftDemanglerTest::MyStructure)";
+ "__int64 default SwiftDemanglerTest::MyStructure::get_z(struct SwiftDemanglerTest::MyStructure)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -336,7 +336,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
SwiftDemanglerTest.MyStructure.z.setter : Swift.Int
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest11MyStructureV1zSivs";
- String demangled = "__thiscall SwiftDemanglerTest::MyStructure::set_z(int)";
+ String demangled = "__thiscall SwiftDemanglerTest::MyStructure::set_z(__int64)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -370,7 +370,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest11MyStructureVyS2icig";
String demangled =
- "int default SwiftDemanglerTest::MyStructure::get_subscript(int,struct SwiftDemanglerTest::MyStructure)";
+ "__int64 default SwiftDemanglerTest::MyStructure::get_subscript(__int64,struct SwiftDemanglerTest::MyStructure)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -403,7 +403,8 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
SwiftDemanglerTest.MyStructure.subscript.setter : (Swift.Int) -> Swift.Int
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest11MyStructureVyS2icis";
- String demangled = "int __thiscall SwiftDemanglerTest::MyStructure::set_subscript(int)";
+ String demangled =
+ "__int64 __thiscall SwiftDemanglerTest::MyStructure::set_subscript(__int64)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -461,7 +462,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest7MyClassC6label1ACSi_tcfC";
String demangled =
- "class SwiftDemanglerTest::MyClass * __thiscall SwiftDemanglerTest::MyClass::__allocating_init(int label1)";
+ "class SwiftDemanglerTest::MyClass * __thiscall SwiftDemanglerTest::MyClass::__allocating_init(__int64 label1)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -498,7 +499,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest7MyClassC6label1ACSi_tcfc";
String demangled =
- "class SwiftDemanglerTest::MyClass * __thiscall SwiftDemanglerTest::MyClass::init(int label1)";
+ "class SwiftDemanglerTest::MyClass * __thiscall SwiftDemanglerTest::MyClass::init(__int64 label1)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -542,7 +543,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest7MyClassC8myMethod6label16label2S2i_SitF";
String demangled =
- "int __thiscall SwiftDemanglerTest::MyClass::myMethod(int label1,int label2)";
+ "__int64 __thiscall SwiftDemanglerTest::MyClass::myMethod(__int64 label1,__int64 label2)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -568,7 +569,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest7MyClassC1zSivg";
String demangled =
- "int __thiscall SwiftDemanglerTest::MyClass::get_z(class SwiftDemanglerTest::MyClass *)";
+ "__int64 __thiscall SwiftDemanglerTest::MyClass::get_z(class SwiftDemanglerTest::MyClass *)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -593,7 +594,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
SwiftDemanglerTest.MyClass.z.setter : Swift.Int
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest7MyClassC1zSivs";
- String demangled = "__thiscall SwiftDemanglerTest::MyClass::set_z(int)";
+ String demangled = "__thiscall SwiftDemanglerTest::MyClass::set_z(__int64)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -618,7 +619,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
SwiftDemanglerTest.MyClass.z.modify : Swift.Int
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest7MyClassC1zSivM";
- String demangled = "__thiscall SwiftDemanglerTest::MyClass::modify_z(int)";
+ String demangled = "__thiscall SwiftDemanglerTest::MyClass::modify_z(__int64)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
@@ -695,7 +696,7 @@ public class SwiftDemanglerTest extends AbstractGenericTest {
**********************************************************************/
String mangled = "_$s18SwiftDemanglerTest16MyAssociatedEnumO8myMethod6label1ACSi_tF";
String demangled =
- "struct SwiftDemanglerTest::MyAssociatedEnum default SwiftDemanglerTest::MyAssociatedEnum::myMethod(int label1,struct SwiftDemanglerTest::MyAssociatedEnum,undefined)";
+ "struct SwiftDemanglerTest::MyAssociatedEnum default SwiftDemanglerTest::MyAssociatedEnum::myMethod(__int64 label1,struct SwiftDemanglerTest::MyAssociatedEnum,undefined)";
if (!(demangler.demangle(mangled) instanceof DemangledFunction function)) {
throw new AssertException("Demangled object is not a function");
diff --git a/Ghidra/Processors/AARCH64/data/languages/AARCH64_swift.cspec b/Ghidra/Processors/AARCH64/data/languages/AARCH64_swift.cspec
index 1ad1f4d34f..926a155ea5 100644
--- a/Ghidra/Processors/AARCH64/data/languages/AARCH64_swift.cspec
+++ b/Ghidra/Processors/AARCH64/data/languages/AARCH64_swift.cspec
@@ -9,7 +9,7 @@
<pointer_size value="8" />
<wchar_size value="4" />
<short_size value="2" />
- <integer_size value="8" />
+ <integer_size value="4" />
<long_size value="8" />
<long_long_size value="8" />
<float_size value="4" />
diff --git a/Ghidra/Processors/x86/data/languages/x86-64-swift.cspec b/Ghidra/Processors/x86/data/languages/x86-64-swift.cspec
index a88fbea255..92af97dd4e 100644
--- a/Ghidra/Processors/x86/data/languages/x86-64-swift.cspec
+++ b/Ghidra/Processors/x86/data/languages/x86-64-swift.cspec
@@ -8,7 +8,7 @@
<pointer_size value="8" />
<wchar_size value="4" />
<short_size value="2" />
- <integer_size value="8" />
+ <integer_size value="4" />
<long_size value="8" />
<long_long_size value="8" />
<float_size value="4" />