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.
This commit is contained in:
Ryan Kurtz 2024-12-06 09:08:19 -05:00
parent 57df41297f
commit e1e54a4d89
7 changed files with 37 additions and 30 deletions

View file

@ -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) {

View file

@ -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;

View file

@ -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}

View file

@ -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);

View file

@ -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");

View file

@ -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" />

View file

@ -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" />