Skip to content

Commit

Permalink
Fix #23110 - Improve the swift demangler to handle more constructions…
Browse files Browse the repository at this point in the history
… ##bin

* s9CryptoKit8Curve448O7SigningO9PublicKeyV16isValidSignature_3forSbx_q_t10Foundation12DataProtocolRzAjKR_r0_lF
* s9CryptoKit8Curve448O7SigningO9PublicKeyV17rawRepresentation10Foundation4DataVvg
* s9CryptoKit8Curve448O7SigningO10PrivateKeyV06publicF0AE06PublicF0VvpMV
* ...
  • Loading branch information
trufae authored Jul 17, 2024
1 parent 8a58808 commit d936bf6
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 26 deletions.
43 changes: 42 additions & 1 deletion libr/bin/mangling/swift-sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,43 @@ static char *my_swift_demangler(const char *s) {
break;
case 'V':
p = resolve (types, q + 1, &attr); // type
if (!p) {
int n = 0;
repeat:;
const char *Q = getnum (q + 1, &n);
const char *res = getstring (Q, n);
if (res) {
r_strbuf_append (out, ".");
r_strbuf_append (out, res);
}
q = Q + n;
if (q >= q_end) {
continue;
}
if (!isdigit (*q)) {
while (*q) {
if (isdigit (*q)) {
break;
}
q++;
}
}
if (isdigit (*q)) {
q--;
goto repeat;
#if 0
int n = 0;
const char *Q = getnum (q, &n);
const char *res = getstring (Q, n);
if (res) {
r_strbuf_append (out, ".");
r_strbuf_append (out, res);
}
q = Q + n;
#endif
}
}
q++;
break;
case '_':
// it's return value time!
Expand Down Expand Up @@ -802,6 +839,7 @@ static char *my_swift_demangler(const char *s) {
}

R_API char *r_bin_demangle_swift(const char *s, bool syscmd, bool trylib) {
const char *os = s;
if (r_str_startswith (s, "_$")) {
s += 2;
}
Expand Down Expand Up @@ -887,7 +925,10 @@ R_API char *r_bin_demangle_swift(const char *s, bool syscmd, bool trylib) {
}
break;
}
return NULL;
if (s > os) {
s--;
}
// return NULL;
}
} else {
// TIFF ones found on COFF binaries, swift-unrelated, return early to avoid FP
Expand Down
54 changes: 54 additions & 0 deletions test/db/cmd/classes
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ EXPECT=<<EOF
0x00000000 objc var 0 isa
0x100003e30 objc var 1 name
0x100008200 [0x100008200 - 0x100008200] 0 objc class 0 main.Employer :: main.Employee
0x00000000 [0x100003f04 - 0x100003f04] 0 ? class 4 Greet
0x100003f04 ? method 0 symbolic main.Greet.
0x00000000 [0x100003f14 - 0x100003f14] 0 ? class 5 Employee_1
0x100003f14 ? method 0 symbolic main.Employee.allocator
0x00000000 [0x100003f1e - 0x100003f1e] 0 ? class 6 Employer_1
0x100003f1e ? method 0 symbolic main.Employer.allocator
---
class Employee {
property name : Swift.String;
Expand All @@ -48,6 +54,15 @@ class main.Employee : Swift._SwiftObject {
@objc
class main.Employer : main.Employee {
}
class Greet {
func symbolic main.Greet.() {} // 0x100003f04
}
class Employee_1 {
func symbolic main.Employee.allocator() {} // 0x100003f14
}
class Employer_1 {
func symbolic main.Employer.allocator() {} // 0x100003f1e
}
---
[
{
Expand Down Expand Up @@ -106,6 +121,45 @@ class main.Employer : main.Employee {
"rawsuper": [
"_TtC4main8Employee"
]
},
{
"classname": "Greet",
"addr": 0,
"index": 4,
"methods": [
{
"name": "symbolic main.Greet.",
"flag": "section.5.__TEXT.__swift5_typeref",
"rawname": "_symbolic $s4main5GreetP",
"addr": 4294983428
}
]
},
{
"classname": "Employee_1",
"addr": 0,
"index": 5,
"methods": [
{
"name": "symbolic main.Employee.allocator",
"flag": "method.Employee_1.symbolic_main.Employee.allocator",
"rawname": "_symbolic _____ 4main8EmployeeC",
"addr": 4294983444
}
]
},
{
"classname": "Employer_1",
"addr": 0,
"index": 6,
"methods": [
{
"name": "symbolic main.Employer.allocator",
"flag": "method.Employer_1.symbolic_main.Employer.allocator",
"rawname": "_symbolic _____ 4main8EmployerC",
"addr": 4294983454
}
]
}
]
EOF
Expand Down
1 change: 1 addition & 0 deletions test/db/formats/mach0/objc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ EXPECT=<<EOF
0x100003078 [0x100001ad0 - 0x100001ad0] 0 objc class 0 TestSwiftObjc.ThisIsASwiftClass(TEST) :: TestSwiftObjc.ThisIsASwiftClass
0x00001f34 [0x00001f34 - 0x00001f34] 0 swift class 0 ThisIsASwiftClass :: __C.NSObject
0x1000031e8 [0x100001d80 - 0x100001d80] 0 objc class 0 TestSwiftObjc.ThisIsASwiftClass :: NSObject
0x00000000 [0x100001f80 - 0x100001f80] 0 ? class 3 ThisIsASwiftClass_1
EOF
RUN

Expand Down
Loading

0 comments on commit d936bf6

Please # to comment.