How to build libswiftCore.dylib, libswift_Concurrency.dylib suitable for injection via DYLD_INSERT_LIBRARIES into iOS app?

Hello!

I'm currently trying to debug a really mysterious memory corruption issue in a large application I'm working on.

Neither ASAN on the device, nor libgmalloc on the simulator is able to catch the source of the corruption.

However, the heap is presumably corrupted, as libmalloc's self-check, enabled via MallocCheckHeapStart/MallocCheckHeapEach, detects corruption on the device but not on the simulator.

To facilitate further debugging with the help of ASAN I'm trying to build the Swift standard library with ASAN enabled from the 5.8 branch to later inject it instead of the OS-provided libs. I assume this should work due to the promised ABI stability of Swift's stdlib.

I've successfully managed to build libswiftCore.dylib, libswift_Concurrency.dylib, and a bunch of other dylibs for OSX, and am able to inject these debug libs into the SwiftUI demo app via DYLD_FRAMEWORK_PATH.

For some reason, the same approach does not work on an iOS 16.5.1 device. I've built a custom libswiftCore.dylib and other libs and injected them via DYLD_INSERT_LIBRARIES, but I'm getting runtime crashes like:


2023-07-14 17:14:56.146449+0200 DemoSwiftUIApp[3914:1365435] -[_TtGCs23_ContiguousArrayStorageCSo8NSNumber_$ copyWithZone:]: unrecognized selector sent to instance 0x107e26d90

and


2023-07-14 17:18:24.420986+0200 ActualApp[3921:1366832] failed to demangle witness for associated type '' in conformance 'Swift.Array<(extension in Dispatch):__C.OS_dispatch_queue.Attributes>: Sequence' from mangled name '\^A\M^WP\M-s\M^?ySayxGG' - subject type x does not conform to protocol Collection

Is there anything fundamentally wrong with the idea I have, or have I built with improper flags?

The command I've used to build the libraries is:


utils/build-script --ios --enable-asan --skip-build-benchmarks --skip-watchos --skip-tvos --skip-test-osx --swift-darwin-supported-archs arm64 --sccache --release-debuginfo --swift-disable-dead-stripping

Thanks a lot in advance!

It seems like the crash could be due to ABI mismatch. I've compiled libswiftCore.dylib for arm64, while on device Apple's library is targeted arm64e. Unfortunately when build with --swift-darwin-supported-archs arm64e it fails and I'm not sure is it expected behavior or not.