fix: poll_oneoff expected layout EventTypeFdRead, EventTypeFdWrite (#1239)

While fds are indeed aligned at 4 bytes, the `subscription_u`
union's "`contents_offset`" is 8, so proper content starts
at +8+8 instead of +8+4.

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
This commit is contained in:
Edoardo Vacchi
2023-03-14 23:54:03 +01:00
committed by GitHub
parent ced3011837
commit 00c53d19a2
2 changed files with 8 additions and 5 deletions

View File

@@ -69,19 +69,22 @@ func pollOneoffFn(ctx context.Context, mod api.Module, params []uint64) Errno {
}
// Loop through all subscriptions and write their output.
// Layout is subscription_u: Union
// https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#subscription_u
for i := uint32(0); i < nsubscriptions; i++ {
inOffset := i * 48
outOffset := i * 32
eventType := inBuf[inOffset+8] // +8 past userdata
var errno Errno // errno for this specific event
var errno Errno // errno for this specific event (1-byte)
switch eventType {
case EventTypeClock: // handle later
// +8 past userdata +8 name alignment
// +8 past userdata +8 contents_offset
errno = processClockEvent(ctx, mod, inBuf[inOffset+8+8:])
case EventTypeFdRead, EventTypeFdWrite:
// +8 past userdata +4 FD alignment
errno = processFDEvent(mod, eventType, inBuf[inOffset+8+4:])
// +8 past userdata +8 contents_offset
errno = processFDEvent(mod, eventType, inBuf[inOffset+8+8:])
default:
return ErrnoInval
}

View File

@@ -115,7 +115,7 @@ func Test_pollOneoff_Errors(t *testing.T) {
nsubscriptions: 1,
mem: []byte{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
EventTypeFdRead, 0x0, 0x0, 0x0,
EventTypeFdRead, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
byte(sys.FdStdin), 0x0, 0x0, 0x0, // valid readable FD
'?', // stopped after encoding
},