-
Notifications
You must be signed in to change notification settings - Fork 18
Add poll_oneoff
for POSIX hosts to WASI.swift
#187
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
base: main
Are you sure you want to change the base?
Conversation
poll_oneoff
ABI types to WASI.swift
poll_oneoff
for POSIX to WASI.swift
poll_oneoff
for POSIX to WASI.swift
poll_oneoff
for POSIX hosts to WASI.swift
Did we bump up the minimum supposed macOS version in the Swift OSS toolchain? |
For macOS it's now the same as in SwiftPM https://github.com/swiftlang/swift-package-manager/blob/d8bc782bbb84012f3cdff97a4a2afed9029cda25/Package.swift#L133 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update wasi-testsuite and ensure that https://github.com/WebAssembly/wasi-testsuite/blob/main/tests/rust/src/bin/poll_oneoff_stdio.rs integration test passes. I think we need to write back events buffer and also check error after poll
library call.
c2ac973
to
f410f0f
Compare
The change declares a clock subscription type in according with the WebAssembly System Interface standard ABI, which is required for testing clocks and timers in Embedded Swift for WebAssembly. The ABI for this type is specified publicly in corresponding ABI documentation: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#subscription_clock rdar://149935761
Testing Swift Concurrency with WebAssembly System Interface requires a missing `subscription` record per the corresponding standard specification: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-subscription-record This change adds a subscription union type, with the ABI described in this standard document.
To test Swift Concurrency with WebAssembly System Interface, an Event API is missing from WasmKit runtime, as specified in the standard specification https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-event-record This change adds Event with the ABI as prescribed in the standard, allowing Swift Concurrency tests to utilize timer events.
Testing Swift Concurrency for WebAssembly requires presence of `epoll_oneoff`, which is absent in WasmKit. This change provides a stub according to the ABI specified in WebAssembly System Interface standard https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#poll_oneoff The stub has no concrete implementation, but provides placeholders that map to clock and file descriptor value that can be polled as specified in the standard.
To support Swift Concurrency tested in WebAssembly System Interface environment, `Clock` and `Subscription` types should conform to `GuestPointee` types, which provides size and alignment for correct ABI implementation. This fixes current build issues, where these types didn't have correct size and alignment specified.
Types like Clock, Subscription, and Event that cover WebAssembly System Interface should have their size, alignment, and load/store functionality tested for conformance with the ABI specified in the standard https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-subscription_clock-record This change adds corresponding tests that cover this.
Implementation of `poll_oneoff` in WebAssembly System Interface standard (https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#poll_oneoff) can be approximated in operating systems implementing POSIX standard with `poll` (https://www.unix.com/man_page/posix/2/ppoll). This change adds such approximation, converting clock subscriptions using nanoseconds to milliseconds, while mapping file descriptor read and write subscriptions to `POLLIN` and `POLLOUT` events in POSIX.
Implementation for POSIX platforms of `poll_oneoff` syscall from WebAssembly System Interface standard should have corresponding tests in WasmKit. This change wires it up via `WASIBridgeToHost` for testability, which then allows exercising clock subscription events in a simple test. Hardcoded test constants for pointer offsets are also generalized to make the test more readable.
9e8306e
to
5c288db
Compare
A testsuite-conforming implementation of `poll_oneff` from WebAssembly System Interface (WASI) standard specification (https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#poll_oneoff) should write to the provided buffer of events that occurred before a given clock timeout. WasmKit in its current implementation had an issue with writing to such buffer, since offset for each element was calculated incorrectly in the `WasmTypes/GuestMemory.swift` source file. Implementation of `poll` function in `WASI/Poll.swift` now takes this buffer as an argument, accumulates user data of file descriptor polling subscriptions in `fdUserData`, while storing clock timer user data in `clockUserData`, which allows later writes to the events buffer for conformances with the test suite. Errors thrown by the host system implementation of `poll` are converted to corresponding WASI errors.
Per the WebAssembly System Interface (WASI) standard specification, size of `errno` variant is 2 bytes, which is equivalent to `UInt16` in Swift. In WasmKit it's currently specified incorrectly to `UInt32`, which leads to bugs when reading and writing `errno` values in guest memory. Corresponding uses of `WASIAbi.Errno` zero-extend value of 2 bytes to 4 bytes when returning 32-bit integers, since WebAssembly doesn't have a separate 16-bit integer type and zero-extension of narrower integers is the expected behavior. Additionally `ClockId` type specifies a fixed number of cases, disallowing unknown raw values, which could be specified in the future. This edge case is exercised in the WASI specification test suite. The change relaxes this restriction, making the implementation compliant with the test.
With `WASIAbi.Errno` type set to 16-bit (2 bytes) width in WasmKit per WebAssembly System Interface specification (https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#errno), corresponding uses need to be updated to zero-extend result value to 32-bit, which is the standard behavior in absence of a built-in 16-bit integer type in WebAssembly.
With `WASIAbi.Errno` type set to 16-bit (2 bytes) width in WasmKit per WebAssembly System Interface specification (https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#errno), corresponding uses need to be updated to zero-extend result value to 32-bit, which is the standard behavior in absence of a built-in 16-bit integer type in WebAssembly. Additionally for wasi-testsuite compatibility, WasmKit should return the number of events occurred in the span of `poll_oneoff` call, not the number of subscription that were passed to it.
Testing Swift Concurrency for WebAssembly requires presence of
poll_oneoff
, which is absent in WasmKit. This change provides an implementation for POSIX hosts according to the ABI specified in WebAssembly System Interface standard https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#poll_oneoffrdar://149935761