Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Android #183

Merged
merged 1 commit into from
Sep 12, 2024
Merged

Add support for Android #183

merged 1 commit into from
Sep 12, 2024

Conversation

finagolfin
Copy link
Contributor

Tested natively on Android AArch64 with a recent trunk Swift toolchain, no problems.

@dnadoba
Copy link
Member

dnadoba commented Aug 10, 2024

@swift-server-bot test this please

@dnadoba
Copy link
Member

dnadoba commented Aug 10, 2024

Great! Thank you!

@dnadoba dnadoba enabled auto-merge (squash) August 10, 2024 11:11
@finagolfin
Copy link
Contributor Author

CI appears to have issues, as this change is compiled out on non-Android and will have no effect on the CI.

@dnadoba
Copy link
Member

dnadoba commented Aug 10, 2024

5.8, 5.9 and 5.10 fail because allocations have improved which I can fix real quick #184

However, nightly unit tests fail and eventually time out:

12:40:01 Test Case 'RFC5280PolicyURINameTests1.testURINameConstraintsExcludedSubtrees' started at 2024-08-10 11:39:22.729
12:40:01 /swift-certificates/Tests/X509Tests/RFC5280PolicyTests.swift:127: error: RFC5280PolicyURINameTests1.testURINameConstraintsExcludedSubtrees : failed - Incorrect validation on excluded subtrees [URI("xn--poema-9qae5a.com.br")] for [URI("http://xn--poema-9qae5a.com.br/")] from root, expected true got validCertificate([Certificate(version: X509v3, serialNumber: b1:b8:d0:7d:c7:8b:96:65:30:f8:1b:8d:7a:32:50:69:59:78:76:e8, issuer: "CN=Swift Certificate Test Intermediate 1,O=Apple,C=US", subject: "CN=Leaf,O=Apple,C=US", notValidBefore: 2024-08-10 11:39:05 +0000, notValidAfter: 2025-08-10 11:39:05 +0000, publicKey: P256.PublicKey, signature: ECDSA, extensions: [BasicConstraints(CA=FALSE), SubjectAlternativeNames(URI("http://xn--poema-9qae5a.com.br/"))]), Certificate(version: X509v3, serialNumber: de:b4:3d:f6:9a:e:d9:3f:e1:f2:b6:fd:37:f0:17:1b:a5:55:2b:41, issuer: "CN=Swift Certificate Test CA 1,O=Apple,C=US", subject: "CN=Swift Certificate Test Intermediate 1,O=Apple,C=US", notValidBefore: 2023-08-11 11:39:05 +0000, notValidAfter: 2025-08-10 11:39:05 +0000, publicKey: P256.PublicKey, signature: ECDSA, extensions: [BasicConstraints(CA=TRUE, maxPathLength=0)]), Certificate(version: X509v3, serialNumber: 61:8b:5c:a9:12:a4:38:1:9d:b:fc:4f:5e:1d:8b:20:71:fa:5d:d6, issuer: "CN=Swift Certificate Test CA 1,O=Apple,C=US", subject: "CN=Swift Certificate Test CA 1,O=Apple,C=US", notValidBefore: 2014-08-13 11:39:05 +0000, notValidAfter: 2034-08-08 11:39:05 +0000, publicKey: P384.PublicKey, signature: ECDSA, extensions: [BasicConstraints(CA=TRUE), NameConstraints(excludedSubtrees: [URI("xn--poema-9qae5a.com.br")])])])

https://ci.swiftserver.group/job/swift-certificates-swift-nightly-prb/542/console

cc: @Lukasa have you seen this before?

@dnadoba
Copy link
Member

dnadoba commented Aug 10, 2024

@swift-server-bot test this please

@Lukasa Lukasa added the 🆕 semver/minor Adds new public API. label Aug 12, 2024
@finagolfin
Copy link
Contributor Author

Hold off, I'm seeing issues with this when building with latest trunk.

@finagolfin finagolfin marked this pull request as draft August 15, 2024 09:16
auto-merge was automatically disabled August 15, 2024 09:16

Pull request was converted to draft

@@ -23,6 +23,9 @@ import Musl
import CoreFoundation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused by whether these imports need to be there at all, as I've been seeing changes in what the trunk compiler I build natively on Android expects here in the last month.

To compare to other platforms, I commented out the imports of CoreFoundation and the three linux C libraries in this file- Bionic, Musl, and Glibc- and tried building the latter two on Fedora 40, where both compiled fine without these imports (Musl was built with the latest July 2 SDK snapshot, Glibc starting from the latest trunk snapshot toolchain back to 5.9.2). This file uses the inet_pton() C function, which clearly comes from those C libraries.

The issue is that a trunk source snapshot of the compiler from June and July built natively on Android worked the same, ie these two imports were unnecessary, but an August trunk snapshot now requires import Android.

I don't know if something changed in the compiler's ClangImporter or LLVM in the last month, but the Android module map is identical from the July trunk snapshot to August.

Anyone know what is going on here, if not with my Android issue, at least why these imports seem unneeded for Glibc/Musl?

@ian-twilightcoder, @compnerd, @etcwilde, @al45tair, perhaps one of you knows what's going on with these overlay imports.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The glibc modulemap/header thing that we have today isn't properly modularizing glibc. Instead of using that mega header, each of the top-level C headers should end up in its own top-level module so if that header is seen through a transitive include, that's the module definition that will get used. I don't know what changed specifically, but what we have was technically undefined, so we're still in the wrong with glibc. It just means that something pulled in something from CoreFoundation that pulled in the necessary header before something else pulled in the bit of glibc/bionic that pulled in that same header, which ends up with CoreFoundation saying that it owns the canonical definition of that thing rather than the libc. We ran into this in corelibs-foundation as well: swiftlang/swift-corelibs-foundation#5005

The musl modulemap should be better behaved though as I seem to remember Alastair putting quite a lot of work to modularize it correctly. @ian-twilightcoder can probably give you more details though on how to proceed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a little experiment to see if these libc imports are needed at all, deleting every import Musl and import Glibc in the swift-nio repo and then trying to build with each libc on linux with the July 2 trunk snapshot toolchain, as that's the latest Musl SDK available.

The good news is that caused a bunch of errors, so I then put back each import one by one until I was left with this cleaned-up patch, ie it still built fine with these imports deleted, even running and passing all the same tests for the Glibc build:

diff --git a/Sources/NIOFileSystem/FileSystem.swift b/Sources/NIOFileSystem/FileSystem.swift
index 291102d0..d158c41d 100644
--- a/Sources/NIOFileSystem/FileSystem.swift
+++ b/Sources/NIOFileSystem/FileSystem.swift
@@ -21,10 +21,6 @@ import NIOPosix
 
 #if canImport(Darwin)
 import Darwin
-#elseif canImport(Glibc)
-import Glibc
-#elseif canImport(Musl)
-import Musl
 #elseif canImport(Bionic)
 import Bionic
 #endif
diff --git a/Sources/NIOFileSystem/FileSystemError+Syscall.swift b/Sources/NIOFileSystem/FileSystemError+Syscall.swift
index 633b6dc7..377203d3 100644
--- a/Sources/NIOFileSystem/FileSystemError+Syscall.swift
+++ b/Sources/NIOFileSystem/FileSystemError+Syscall.swift
@@ -17,10 +17,6 @@ import SystemPackage
 
 #if canImport(Darwin)
 import Darwin
-#elseif canImport(Glibc)
-import Glibc
-#elseif canImport(Musl)
-import Musl
 #endif
 
 extension FileSystemError {
diff --git a/Sources/NIOFileSystem/Internal/System Calls/FileDescriptor+Syscalls.swift b/Sources/NIOFileSystem/Internal/System Calls/FileDescriptor+Syscalls.swift
index 9212839c..be4ba011 100644
--- a/Sources/NIOFileSystem/Internal/System Calls/FileDescriptor+Syscalls.swift	
+++ b/Sources/NIOFileSystem/Internal/System Calls/FileDescriptor+Syscalls.swift	
@@ -18,11 +18,7 @@ import SystemPackage
 
 #if canImport(Darwin)
 import Darwin
-#elseif canImport(Glibc)
-import Glibc
-import CNIOLinux
-#elseif canImport(Musl)
-import Musl
+#elseif canImport(Glibc) || canImport(Musl)
 import CNIOLinux
 #elseif canImport(Bionic)
 import Bionic
diff --git a/Sources/NIOFileSystem/Internal/System Calls/Syscall.swift b/Sources/NIOFileSystem/Internal/System Calls/Syscall.swift
index 0a80b9f1..d738d856 100644
--- a/Sources/NIOFileSystem/Internal/System Calls/Syscall.swift	
+++ b/Sources/NIOFileSystem/Internal/System Calls/Syscall.swift	
@@ -18,11 +18,7 @@ import SystemPackage
 #if canImport(Darwin)
 import Darwin
 import CNIODarwin
-#elseif canImport(Glibc)
-import Glibc
-import CNIOLinux
-#elseif canImport(Musl)
-import Musl
+#elseif canImport(Glibc) || canImport(Musl)
 import CNIOLinux
 #elseif canImport(Bionic)
 import Bionic
diff --git a/Sources/NIOFileSystem/Internal/SystemFileHandle.swift b/Sources/NIOFileSystem/Internal/SystemFileHandle.swift
index 41406573..83cc013b 100644
--- a/Sources/NIOFileSystem/Internal/SystemFileHandle.swift
+++ b/Sources/NIOFileSystem/Internal/SystemFileHandle.swift
@@ -20,11 +20,7 @@ import NIOPosix
 
 #if canImport(Darwin)
 import Darwin
-#elseif canImport(Glibc)
-import Glibc
-import CNIOLinux
-#elseif canImport(Musl)
-import Musl
+#elseif canImport(Glibc) || canImport(Musl)
 import CNIOLinux
 #elseif canImport(Bionic)
 import Bionic
diff --git a/Sources/NIOPosix/VsockAddress.swift b/Sources/NIOPosix/VsockAddress.swift
index 94d0f797..e08725c3 100644
--- a/Sources/NIOPosix/VsockAddress.swift
+++ b/Sources/NIOPosix/VsockAddress.swift
@@ -17,11 +17,6 @@ import NIOCore
 #if canImport(Darwin)
 import CNIODarwin
 #elseif os(Linux) || os(Android)
-#if canImport(Glibc)
-import Glibc
-#elseif canImport(Musl)
-import Musl
-#endif
 import CNIOLinux
 #endif
 private let vsockUnimplemented = "VSOCK support is not implemented for this platform"

The bad news is that I don't know why some of these are not needed. The imports in NIOPosix/VsockAddress.swift aren't needed because one of the Glibc imports in that module is @_exported, so that appears to cover all files in that module. I presume the same for NIOFileSystem/Internal/SystemFileHandle.swift and NIOFileSystem/FileSystem.swift, which both explicitly import NIOPosix. The other three are a mystery.

Pinging @ian-twilightcoder, perhaps you know what's going on here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe this is related to the current leaks in the module system, such as for member declarations that is currently being fixed, so given Ian hasn't responded, I will just change this so the compiler won't error, and we can merge and then revisit it later, once we know the Swift module leaks have been fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@finagolfin finagolfin marked this pull request as ready for review September 12, 2024 07:48
@finagolfin
Copy link
Contributor Author

Updated this pull to import the Android overlay instead and rebased, ready for review.

@Lukasa
Copy link
Contributor

Lukasa commented Sep 12, 2024

@swift-server-bot add to allowlist

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easiest PR ever, thanks @finagolfin, and thanks for your attention to detail here as well, it's much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants