Skip to content

Commit 9edb55f

Browse files
committed
Add a test for poll_oneoff
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.
1 parent 63574ab commit 9edb55f

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

Sources/WASI/WASI.swift

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,21 +2012,9 @@ public class WASIBridgeToHost: WASI {
20122012
subscriptions: UnsafeGuestBufferPointer<WASIAbi.Subscription>,
20132013
events: UnsafeGuestBufferPointer<WASIAbi.Event>
20142014
) throws -> WASIAbi.Size {
2015-
for subscription in subscriptions {
2016-
switch subscription.union {
2017-
case .clock:
2018-
throw WASIAbi.Errno.ENOTSUP
2019-
2020-
case .fdRead(let fd), .fdWrite(let fd):
2021-
guard case let .file(entry) = self.fdTable[fd] else {
2022-
throw WASIAbi.Errno.EBADF
2023-
2024-
}
2025-
throw WASIAbi.Errno.ENOTSUP
2026-
}
2027-
}
2028-
2029-
return 0
2015+
guard !subscriptions.isEmpty else { throw WASIAbi.Errno.EINVAL }
2016+
try poll(subscriptions: subscriptions, self.fdTable)
2017+
return .init(subscriptions.count)
20302018
}
20312019

20322020
func random_get(buffer: UnsafeGuestPointer<UInt8>, length: WASIAbi.Size) {

Tests/WASITests/WASITests.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,26 +147,35 @@ final class WASITests: XCTestCase {
147147
var pointer = start
148148
let read = WASIAbi.Subscription.Union.fdRead(.init(0))
149149
let write = WASIAbi.Subscription.Union.fdWrite(.init(0))
150-
let clock = WASIAbi.Subscription.Union.clock(.init(id: .REALTIME, timeout: 42, precision: 0, flags: []))
150+
let writeOffset = WASIAbi.Subscription.sizeInGuest
151+
let timeout: WASIAbi.Timestamp = 100_000_000
152+
let clock = WASIAbi.Subscription.Union.clock(.init(id: .REALTIME, timeout: timeout, precision: 0, flags: []))
153+
let clockOffset = writeOffset + WASIAbi.Subscription.sizeInGuest
151154
let event = WASIAbi.Event(userData: 3, error: .EIO, eventType: .fdRead, fdReadWrite: .init(nBytes: 37, flags: [.hangup]))
155+
let eventOffset = clockOffset + WASIAbi.Subscription.sizeInGuest
156+
let finalOffset = eventOffset + WASIAbi.Event.sizeInGuest
152157
WASIAbi.Subscription.writeToGuest(at: &pointer, value: .init(userData: 1, union: read))
153-
XCTAssertEqual(pointer.offset, 48)
158+
XCTAssertEqual(pointer.offset, writeOffset)
154159
WASIAbi.Subscription.writeToGuest(at: &pointer, value: .init(userData: 2, union: write))
155-
XCTAssertEqual(pointer.offset, 48 * 2)
160+
XCTAssertEqual(pointer.offset, clockOffset)
156161
WASIAbi.Subscription.writeToGuest(at: &pointer, value: .init(userData: 3, union: clock))
157-
XCTAssertEqual(pointer.offset, 48 * 3)
162+
XCTAssertEqual(pointer.offset, eventOffset)
158163
WASIAbi.Event.writeToGuest(at: &pointer, value: event)
159-
XCTAssertEqual(pointer.offset, 48 * 3 + 32)
164+
XCTAssertEqual(pointer.offset, finalOffset)
160165

161166
// Test that reading back yields same result
162167
pointer = start
163168
XCTAssertEqual(WASIAbi.Subscription.readFromGuest(&pointer), .init(userData: 1, union: read))
164-
XCTAssertEqual(pointer.offset, 48)
169+
XCTAssertEqual(pointer.offset, writeOffset)
165170
XCTAssertEqual(WASIAbi.Subscription.readFromGuest(&pointer), .init(userData: 2, union: write))
166-
XCTAssertEqual(pointer.offset, 48 * 2)
171+
XCTAssertEqual(pointer.offset, clockOffset)
167172
XCTAssertEqual(WASIAbi.Subscription.readFromGuest(&pointer), .init(userData: 3, union: clock))
168-
XCTAssertEqual(pointer.offset, 48 * 3)
173+
XCTAssertEqual(pointer.offset, eventOffset)
169174
XCTAssertEqual(WASIAbi.Event.readFromGuest(&pointer), event)
170-
XCTAssertEqual(pointer.offset, 48 * 3 + 32)
175+
XCTAssertEqual(pointer.offset, finalOffset)
176+
XCTAssertTrue(try ContinuousClock().measure {
177+
let clockPointer = UnsafeGuestBufferPointer<WASIAbi.Subscription>(baseAddress: .init(memorySpace: memory, offset: clockOffset), count: 1)
178+
XCTAssertEqual(try WASIBridgeToHost().poll_oneoff(subscriptions: clockPointer, events: .init(baseAddress: .init(memorySpace: memory, offset: finalOffset), count: 1)), 1)
179+
} > .nanoseconds(timeout))
171180
}
172181
}

0 commit comments

Comments
 (0)