From d936bf6d9b777cc66a5971425b7425c10215edb2 Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 17 Jul 2024 17:42:30 +0200 Subject: [PATCH] Fix #23110 - Improve the swift demangler to handle more constructions ##bin * s9CryptoKit8Curve448O7SigningO9PublicKeyV16isValidSignature_3forSbx_q_t10Foundation12DataProtocolRzAjKR_r0_lF * s9CryptoKit8Curve448O7SigningO9PublicKeyV17rawRepresentation10Foundation4DataVvg * s9CryptoKit8Curve448O7SigningO10PrivateKeyV06publicF0AE06PublicF0VvpMV * ... --- libr/bin/mangling/swift-sd.c | 43 ++++- test/db/cmd/classes | 54 ++++++ test/db/formats/mach0/objc | 1 + test/db/formats/mach0/swift | 327 ++++++++++++++++++++++++++++++--- test/db/formats/mangling/swift | 30 ++- 5 files changed, 429 insertions(+), 26 deletions(-) diff --git a/libr/bin/mangling/swift-sd.c b/libr/bin/mangling/swift-sd.c index 6b1c7b8b504f3..33d38064b2d2b 100644 --- a/libr/bin/mangling/swift-sd.c +++ b/libr/bin/mangling/swift-sd.c @@ -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! @@ -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; } @@ -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 diff --git a/test/db/cmd/classes b/test/db/cmd/classes index cf897a46ba456..e3e77e246008a 100644 --- a/test/db/cmd/classes +++ b/test/db/cmd/classes @@ -34,6 +34,12 @@ EXPECT=< const*, unsigned long, swift::TargetProtocolDescriptorRef const*)::$_3::operator()() const::{lambda(void*)#1}::__invoke(void*) {} // 0x1000036b0 func ProtocolClassConstraint, swift::TargetMetadata const*, unsigned long, swift::TargetProtocolDescriptorRef const*)::$_3::operator()() const::TheLazy {} // 0x100008588 } +class SuperKlass_1 { + func symbolic klass.SuperKlass.allocator() {} // 0x100003e7e +} +class SomeClass_1 { + func symbolic klass.SomeClass.allocator() {} // 0x100003e84 +} class swift { func getRootSuperclass() {} // 0x100002b70 func swift51override_conformsToSwiftProtocol(swift::TargetMetadata const*, swift::TargetProtocolDescriptor const*, llvm::StringRef, swift::TargetProtocolConformanceDescriptor const* (*)(swift::TargetMetadata const*, swift::TargetProtocolDescriptor const*, llvm::StringRef)) {} // 0x100002bb0 @@ -369,25 +375,25 @@ EXPECT=< const*, unsigned long, swift::TargetProtocolDescriptorRef const*)::$_3::operator()() const::{lambda(void*)#1}::__invoke(void*) 0x100008588 ? method 1 ProtocolClassConstraint, swift::TargetMetadata const*, unsigned long, swift::TargetProtocolDescriptorRef const*)::$_3::operator()() const::TheLazy -0x00000000 [0x100002b70 - 0x100008568] 23032 c++ class 8 swift +0x00000000 [0x100003e7e - 0x100003e7e] 0 ? class 8 SuperKlass_1 +0x100003e7e ? method 0 symbolic klass.SuperKlass.allocator +0x00000000 [0x100003e84 - 0x100003e84] 0 ? class 9 SomeClass_1 +0x100003e84 ? method 0 symbolic klass.SomeClass.allocator +0x00000000 [0x100002b70 - 0x100008568] 23032 c++ class 10 swift 0x100002b70 c++ method 0 getRootSuperclass() 0x100002bb0 c++ method 1 swift51override_conformsToSwiftProtocol(swift::TargetMetadata const*, swift::TargetProtocolDescriptor const*, llvm::StringRef, swift::TargetProtocolConformanceDescriptor const* (*)(swift::TargetMetadata const*, swift::TargetProtocolDescriptor const*, llvm::StringRef)) 0x1000031f0 c++ method 2 Lazy<(anonymous namespace)::ConformanceState>::defaultInitCallback(void*)