swift-android-sdk icon indicating copy to clipboard operation
swift-android-sdk copied to clipboard

Error when running swift-algorithmsPackageTests.xctest: "unknown reloc type 16"

Open marcprux opened this issue 1 year ago • 5 comments

When building and running tests against swift-algorithms on an x86 host+emulator with the Swift 6.0 Android toolchain, I'm seeing the following warning and error:

WARNING: linker: /data/local/tmp/swift-android/swift-algorithms/swift-algorithmsPackageTests.xctest: unsupported flags DT_FLAGS_1=0x8000001
CANNOT LINK EXECUTABLE "/data/local/tmp/swift-android/swift-algorithms/swift-algorithmsPackageTests.xctest": unknown reloc type 16 @ 0x7e9b7e351e48 (16114)
Error: Error terminated(code: 134) running command: /Users/runner/Library/Android/sdk/platform-tools/adb shell /data/local/tmp/swift-android/swift-algorithms/swift-algorithmsPackageTests.xctest

This doesn't occur when running on an arm host+emulator (the tests all run and pass on my machine).

It's especially odd, since this package is passing tests on CI.

Is this something that termux-elf-cleaner might help with? I notice that we are using it in CI, but only for the swift-argument-parser tests and not any other packages.

marcprux avatar Sep 26 '24 20:09 marcprux

What Android APIs are you running it on in the emulator? List out what APIs you tried for each of arm and x86.

Is this something that termux-elf-cleaner might help with?

No, I tried, it doesn't. This is related to the TLS issue I mentioned earlier.

finagolfin avatar Sep 27 '24 01:09 finagolfin

This is related to the TLS issue I mentioned earlier.

FTR, the issue mentioned is: https://github.com/finagolfin/swift-android-sdk/pull/170#issuecomment-2351990840

marcprux avatar Sep 28 '24 18:09 marcprux

What Android APIs are you running it on in the emulator? List out what APIs you tried for each of arm and x86.

I haven't tested against arm API variations yet, but on x86, testing swift-algorithms fails against API 25–28, and passes on API 29+ (see https://github.com/marcprux/swift-algorithms/actions/runs/11114132859). This tracks with the note about TLS becoming available in API 29: https://github.com/aosp-mirror/platform_bionic/blob/master/android-changes-for-ndk-developers.md#elf-tls-available-for-api-level--29

marcprux avatar Sep 30 '24 21:09 marcprux

I haven't tested against arm API variations yet

OK, but you tested it on some AArch64 device or emulator, what API was that? I just wonder if it works before API 29 on AArch64.

This tracks with the note about TLS becoming available in API 29

Yep, this is why I raised the API used on the CI emulator from 24 to 29 with Swift 6, because all the test runners were failing otherwise.

The problem is that a Swift file in Foundation calls that inline _swift_stdlib_gettid() function from the stdlib's synchronization shims that now uses a TLS variable, and that C++ code is presumably compiled by the Swift compiler somehow without specifying an API level, which on Android then defaults to the latest API level, ie with native TLS.

We'll need to find where in the Swift compiler such inline compilation of C++ functions is done and check what it's doing wrong. The full target triple with the API level is passed to the ClangImporter that integrates C/C++ headers, so I'm not sure why this C++ compilation doesn't.

finagolfin avatar Oct 01 '24 02:10 finagolfin

OK, but you tested it on some AArch64 device or emulator, what API was that? I just wonder if it works before API 29 on AArch64.

It was API 34.

marcprux avatar Oct 01 '24 03:10 marcprux

I don't recall when this changed, but it works now. Closing.

marcprux avatar Oct 31 '24 00:10 marcprux

Doesn't appear to be the case, where did you see it working?

finagolfin avatar Oct 31 '24 16:10 finagolfin

Doesn't appear to be the case, where did you see it working?

Sorry, you are right. I was accidentally running against Android API 29 rather than 28 as I had thought.

marcprux avatar Oct 31 '24 17:10 marcprux

I've submitted a fix for this upstream, swiftlang/swift#77883.

finagolfin avatar Dec 01 '24 10:12 finagolfin

Not sure whether this should be a separate issue report or not, but I was just testing swift-http-types with the pending 6.0.3 SDK against an API 34 aarch64 emulator and hit an error that sounds related to the TLS issue:

error: "./swift-http-typesPackageTests.xctest": executable's TLS segment is underaligned: alignment is 4, needs to be at least 64 for ARM64 Bionic

Not that it doesn't occur when running against an API 29 x86_64 emulator with the 6.0.2 SDK on my CI for the project.

swift-http-types is a pretty simple package with no dependencies. ~I haven't tried to debug, but I suspect it might be something to do with NIOLock.swift.~

marcprux avatar Dec 12 '24 21:12 marcprux

Yeah, that's related to this. Unfortunately, the linked fix will have to go into the compiler first before we can use it, will try to get that in for the next patch release.

finagolfin avatar Dec 12 '24 22:12 finagolfin

I expect you already know this, but FYI it is this reference to Synchronization.Mutex that triggers the crash:

    #if compiler(>=6.0)
    @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
    private final class _StorageWithMutex: _Storage, @unchecked Sendable {
        let mutex = Mutex<Void>(())

        override func withLock<Result>(_ body: () throws -> Result) rethrows -> Result {
            try self.mutex.withLock { _ in
                try body()
            }
        }
    }
    #endif  // compiler(>=6.0)

Removing it (and fixing the code that references it to use the fallback NIOLock) lets the tests run (and pass) on my aarch64 emulator.

marcprux avatar Dec 12 '24 22:12 marcprux

Got the emulated TLS fix for this into the release/6.0 branch upstream, will close this once it ships in the next patch release and we can run the release branch tests in the emulator at API 24 again, as I've already been able to do in trunk.

finagolfin avatar Jan 15 '25 17:01 finagolfin

I just released a 6.1 bundle, which no longer has this problem and has been tested in the API 24 emulator for months now.

finagolfin avatar Apr 04 '25 18:04 finagolfin