Debian Codegraph @

imports/wasi_snapshot_preview1/poll_test.go

raw symbols SCIP
1 package wasi_snapshot_preview1_test
2
3 import (
4 "io/fs"
5 "os"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/tetratelabs/wazero"
11 "github.com/tetratelabs/wazero/api"
12 experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
13 "github.com/tetratelabs/wazero/internal/sys"
14 "github.com/tetratelabs/wazero/internal/testing/require"
15 "github.com/tetratelabs/wazero/internal/wasip1"
16 "github.com/tetratelabs/wazero/internal/wasm"
17 sysapi "github.com/tetratelabs/wazero/sys"
18 )
19
20 func Test_pollOneoff(t *testing.T) {
21 mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
22 defer r.Close(testCtx)
23
24 mem := []byte{
25 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
26 wasip1.EventTypeClock, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // event type and padding
27 wasip1.ClockIDMonotonic, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // clockID
28 0x01, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // timeout (ns)
29 0x01, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // precision (ns)
30 0x00, 0x00, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // flags (relative)
31 '?', // stopped after encoding
32 }
33
34 expectedMem := []byte{
35 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
36 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
37 wasip1.EventTypeClock, 0x0, 0x0, 0x0, // 4 bytes for type enum
38 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
39 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, '?', // stopped after encoding
40 }
41
42 in := uint32(0) // past in
43 out := uint32(128) // past in
44 nsubscriptions := uint32(1)
45 resultNevents := uint32(512) // past out
46
47 maskMemory(t, mod, 1024)
48 mod.Memory().Write(in, mem)
49
50 requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PollOneoffName, uint64(in), uint64(out), uint64(nsubscriptions),
51 uint64(resultNevents))
52 require.Equal(t, `
53 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=128,nsubscriptions=1)
54 <== (nevents=1,errno=ESUCCESS)
55 `, "\n"+log.String())
56
57 outMem, ok := mod.Memory().Read(out, uint32(len(expectedMem)))
58 require.True(t, ok)
59 require.Equal(t, expectedMem, outMem)
60
61 nevents, ok := mod.Memory().ReadUint32Le(resultNevents)
62 require.True(t, ok)
63 require.Equal(t, nsubscriptions, nevents)
64 }
65
66 func Test_pollOneoff_Errors(t *testing.T) {
67 mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
68 defer r.Close(testCtx)
69
70 tests := []struct {
71 name string
72 in, out, nsubscriptions, resultNevents uint32
73 mem []byte // at offset in
74 expectedErrno wasip1.Errno
75 expectedMem []byte // at offset out
76 expectedLog string
77 }{
78 {
79 name: "in out of range",
80 in: wasm.MemoryPageSize,
81 nsubscriptions: 1,
82 out: 128, // past in
83 resultNevents: 512, // past out
84 expectedErrno: wasip1.ErrnoFault,
85 expectedLog: `
86 ==> wasi_snapshot_preview1.poll_oneoff(in=65536,out=128,nsubscriptions=1)
87 <== (nevents=,errno=EFAULT)
88 `,
89 },
90 {
91 name: "out out of range",
92 out: wasm.MemoryPageSize,
93 resultNevents: 512, // past out
94 nsubscriptions: 1,
95 expectedErrno: wasip1.ErrnoFault,
96 expectedLog: `
97 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=65536,nsubscriptions=1)
98 <== (nevents=,errno=EFAULT)
99 `,
100 },
101 {
102 name: "resultNevents out of range",
103 resultNevents: wasm.MemoryPageSize,
104 nsubscriptions: 1,
105 expectedErrno: wasip1.ErrnoFault,
106 expectedLog: `
107 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=0,nsubscriptions=1)
108 <== (nevents=,errno=EFAULT)
109 `,
110 },
111 {
112 name: "nsubscriptions zero",
113 out: 128, // past in
114 resultNevents: 512, // past out
115 expectedErrno: wasip1.ErrnoInval,
116 expectedLog: `
117 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=128,nsubscriptions=0)
118 <== (nevents=,errno=EINVAL)
119 `,
120 },
121 }
122
123 for _, tt := range tests {
124 tc := tt
125 t.Run(tc.name, func(t *testing.T) {
126 defer log.Reset()
127
128 maskMemory(t, mod, 1024)
129
130 if tc.mem != nil {
131 mod.Memory().Write(tc.in, tc.mem)
132 }
133
134 requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PollOneoffName, uint64(tc.in), uint64(tc.out),
135 uint64(tc.nsubscriptions), uint64(tc.resultNevents))
136 require.Equal(t, tc.expectedLog, "\n"+log.String())
137
138 out, ok := mod.Memory().Read(tc.out, uint32(len(tc.expectedMem)))
139 require.True(t, ok)
140 require.Equal(t, tc.expectedMem, out)
141
142 // Events should be written on success regardless of nested failure.
143 if tc.expectedErrno == wasip1.ErrnoSuccess {
144 nevents, ok := mod.Memory().ReadUint32Le(tc.resultNevents)
145 require.True(t, ok)
146 require.Equal(t, uint32(1), nevents)
147 _ = nevents
148 }
149 })
150 }
151 }
152
153 func Test_pollOneoff_Stdin(t *testing.T) {
154 w, r, err := os.Pipe()
155 require.NoError(t, err)
156 defer w.Close()
157 defer r.Close()
158 _, _ = w.Write([]byte("wazero"))
159
160 tests := []struct {
161 name string
162 in, out, nsubscriptions, resultNevents uint32
163 mem []byte // at offset in
164 stdin experimentalsys.File
165 expectedErrno wasip1.Errno
166 expectedMem []byte // at offset out
167 expectedLog string
168 expectedNevents uint32
169 }{
170 {
171 name: "Read without explicit timeout (no tty)",
172 nsubscriptions: 1,
173 expectedNevents: 1,
174 stdin: &sys.StdinFile{Reader: strings.NewReader("test")},
175 mem: fdReadSub,
176 expectedErrno: wasip1.ErrnoSuccess,
177 out: 128, // past in
178 resultNevents: 512, // past out
179 expectedMem: []byte{
180 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
181 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
182 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, // 4 bytes for type enum
183 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
184 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
185 0x0, 0x0,
186
187 '?', // stopped after encoding
188 },
189 expectedLog: `
190 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=128,nsubscriptions=1)
191 <== (nevents=1,errno=ESUCCESS)
192 `,
193 },
194 {
195 name: "20ms timeout, fdread on tty (buffer ready): both events are written",
196 nsubscriptions: 2,
197 expectedNevents: 2,
198 stdin: &ttyStdinFile{StdinFile: sys.StdinFile{Reader: strings.NewReader("test")}},
199 mem: concat(
200 clockNsSub(20*1000*1000),
201 fdReadSub,
202 ),
203 expectedErrno: wasip1.ErrnoSuccess,
204 out: 128, // past in
205 resultNevents: 512, // past out
206 expectedMem: []byte{
207 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
208 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
209 wasip1.EventTypeClock, 0x0, 0x0, 0x0, // 4 bytes for type enum
210 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
211 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
212 0x0, 0x0,
213
214 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
215 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
216 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, // 4 bytes for type enum
217 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
218 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
219 0x0, 0x0,
220
221 '?', // stopped after encoding
222 },
223 expectedLog: `
224 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=128,nsubscriptions=2)
225 <== (nevents=2,errno=ESUCCESS)
226 `,
227 },
228 {
229 name: "0ns timeout, fdread on tty (buffer ready): both are written",
230 nsubscriptions: 2,
231 expectedNevents: 2,
232 stdin: &ttyStdinFile{StdinFile: sys.StdinFile{Reader: strings.NewReader("test")}},
233 mem: concat(
234 clockNsSub(20*1000*1000),
235 fdReadSub,
236 ),
237 expectedErrno: wasip1.ErrnoSuccess,
238 out: 128, // past in
239 resultNevents: 512, // past out
240 expectedMem: []byte{
241 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
242 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
243 wasip1.EventTypeClock, 0x0, 0x0, 0x0, // 4 bytes for type enum
244 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
245 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
246 0x0, 0x0,
247
248 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
249 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
250 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, // 4 bytes for type enum
251 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
252 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
253 0x0, 0x0,
254
255 '?', // stopped after encoding
256 },
257 expectedLog: `
258 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=128,nsubscriptions=2)
259 <== (nevents=2,errno=ESUCCESS)
260 `,
261 },
262 {
263 name: "0ns timeout, fdread on regular file: both events are written",
264 nsubscriptions: 2,
265 expectedNevents: 2,
266 stdin: &sys.StdinFile{Reader: strings.NewReader("test")},
267 mem: concat(
268 clockNsSub(20*1000*1000),
269 fdReadSub,
270 ),
271 expectedErrno: wasip1.ErrnoSuccess,
272 out: 128, // past in
273 resultNevents: 512, // past out
274 expectedMem: []byte{
275 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
276 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
277 wasip1.EventTypeClock, 0x0, 0x0, 0x0, // 4 bytes for type enum
278 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
279 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
280 0x0, 0x0,
281
282 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
283 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
284 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, // 4 bytes for type enum
285 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
286 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
287 0x0, 0x0,
288
289 '?', // stopped after encoding
290 },
291 expectedLog: `
292 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=128,nsubscriptions=2)
293 <== (nevents=2,errno=ESUCCESS)
294 `,
295 },
296 {
297 name: "1ns timeout, fdread on regular file: both events are written",
298 nsubscriptions: 2,
299 expectedNevents: 2,
300 stdin: &sys.StdinFile{Reader: strings.NewReader("test")},
301 mem: concat(
302 clockNsSub(20*1000*1000),
303 fdReadSub,
304 ),
305 expectedErrno: wasip1.ErrnoSuccess,
306 out: 128, // past in
307 resultNevents: 512, // past out
308 expectedMem: []byte{
309 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
310 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
311 wasip1.EventTypeClock, 0x0, 0x0, 0x0, // 4 bytes for type enum
312 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
313 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
314 0x0, 0x0,
315
316 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
317 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
318 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, // 4 bytes for type enum
319 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
320 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
321 0x0, 0x0,
322
323 '?', // stopped after encoding
324 },
325 expectedLog: `
326 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=128,nsubscriptions=2)
327 <== (nevents=2,errno=ESUCCESS)
328 `,
329 },
330 {
331 name: "20ms timeout, fdread on blocked tty: only clock event is written",
332 nsubscriptions: 2,
333 expectedNevents: 1,
334 stdin: &neverReadyTtyStdinFile{StdinFile: sys.StdinFile{Reader: newBlockingReader(t)}},
335 mem: concat(
336 clockNsSub(20*1000*1000),
337 fdReadSub,
338 ),
339
340 expectedErrno: wasip1.ErrnoSuccess,
341 out: 128, // past in
342 resultNevents: 512, // past out
343 expectedMem: []byte{
344 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
345 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
346 wasip1.EventTypeClock, 0x0, 0x0, 0x0, // 4 bytes for type enum
347 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
348 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
349 0x0, 0x0,
350
351 // 32 empty bytes
352 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
353 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
354 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
355 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
356
357 '?', // stopped after encoding
358 },
359 expectedLog: `
360 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=128,nsubscriptions=2)
361 <== (nevents=1,errno=ESUCCESS)
362 `,
363 },
364 {
365 name: "pollable pipe, multiple subs, events returned out of order",
366 nsubscriptions: 3,
367 expectedNevents: 3,
368 mem: concat(
369 fdReadSub,
370 clockNsSub(20*1000*1000),
371 // Illegal file fd with custom user data to recognize it in the event buffer.
372 fdReadSubFdWithUserData(100, []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77})),
373 stdin: &sys.StdinFile{Reader: w},
374 expectedErrno: wasip1.ErrnoSuccess,
375 out: 128, // past in
376 resultNevents: 512, // past out
377 expectedMem: []byte{
378 // Clock is acknowledged first.
379 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
380 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
381 wasip1.EventTypeClock, 0x0, 0x0, 0x0, // 4 bytes for type enum
382 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
383 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
384 0x0, 0x0,
385
386 // Then an illegal file with custom user data.
387 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, // userdata
388 byte(wasip1.ErrnoBadf), 0x0, // errno is 16 bit
389 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, // 4 bytes for type enum
390 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
391 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
392 0x0, 0x0,
393
394 // Stdin pipes are delayed to invoke sysfs.poll
395 // thus, they are written back last.
396 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
397 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
398 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, // 4 bytes for type enum
399 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
400 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
401 0x0, 0x0,
402
403 '?', // stopped after encoding
404 },
405 expectedLog: `
406 ==> wasi_snapshot_preview1.poll_oneoff(in=0,out=128,nsubscriptions=3)
407 <== (nevents=3,errno=ESUCCESS)
408 `,
409 },
410 }
411
412 for _, tt := range tests {
413 tc := tt
414 t.Run(tc.name, func(t *testing.T) {
415 mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
416 defer r.Close(testCtx)
417 defer log.Reset()
418
419 setStdin(t, mod, tc.stdin)
420
421 maskMemory(t, mod, 1024)
422 if tc.mem != nil {
423 mod.Memory().Write(tc.in, tc.mem)
424 }
425
426 requireErrnoResult(t, tc.expectedErrno, mod, wasip1.PollOneoffName, uint64(tc.in), uint64(tc.out),
427 uint64(tc.nsubscriptions), uint64(tc.resultNevents))
428 require.Equal(t, tc.expectedLog, "\n"+log.String())
429
430 out, ok := mod.Memory().Read(tc.out, uint32(len(tc.expectedMem)))
431 require.True(t, ok)
432 require.Equal(t, tc.expectedMem, out)
433
434 // Events should be written on success regardless of nested failure.
435 if tc.expectedErrno == wasip1.ErrnoSuccess {
436 nevents, ok := mod.Memory().ReadUint32Le(tc.resultNevents)
437 require.True(t, ok)
438 require.Equal(t, tc.expectedNevents, nevents)
439 _ = nevents
440 }
441 })
442 }
443 }
444
445 func setStdin(t *testing.T, mod api.Module, stdin experimentalsys.File) {
446 fsc := mod.(*wasm.ModuleInstance).Sys.FS()
447 f, ok := fsc.LookupFile(sys.FdStdin)
448 require.True(t, ok)
449 f.File = stdin
450 }
451
452 func Test_pollOneoff_Zero(t *testing.T) {
453 poller := &pollStdinFile{StdinFile: sys.StdinFile{Reader: strings.NewReader("test")}, ready: true}
454
455 mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
456 defer r.Close(testCtx)
457 defer log.Reset()
458
459 setStdin(t, mod, poller)
460
461 maskMemory(t, mod, 1024)
462
463 out := uint32(128)
464 nsubscriptions := 2
465 resultNevents := uint32(512)
466
467 mod.Memory().Write(0,
468 concat(
469 clockNsSub(20*1000*1000),
470 fdReadSub,
471 ),
472 )
473
474 expectedMem := []byte{
475 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
476 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
477 wasip1.EventTypeClock, 0x0, 0x0, 0x0, // 4 bytes for type enum
478 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
479 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
480
481 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
482 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
483 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, // 4 bytes for type enum
484 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // pad to 32
485 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
486 0x0, 0x0,
487
488 '?', // stopped after encoding
489 }
490
491 requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PollOneoffName, uint64(0), uint64(out),
492 uint64(nsubscriptions), uint64(resultNevents))
493
494 outMem, ok := mod.Memory().Read(out, uint32(len(expectedMem)))
495 require.True(t, ok)
496 require.Equal(t, expectedMem, outMem)
497
498 // Events should be written on success regardless of nested failure.
499 nevents, ok := mod.Memory().ReadUint32Le(resultNevents)
500 require.True(t, ok)
501 require.Equal(t, uint32(2), nevents)
502
503 // second run: simulate no more data on the fd
504 poller.ready = false
505
506 mod.Memory().Write(0,
507 concat(
508 clockNsSub(20*1000*1000),
509 fdReadSub,
510 ),
511 )
512
513 expectedMem = []byte{
514 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
515 byte(wasip1.ErrnoSuccess), 0x0, // errno is 16 bit
516 wasip1.EventTypeClock, 0x0, 0x0, 0x0, // 4 bytes for type enum
517 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
518 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
519
520 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
521 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
522 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
523 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
524
525 '?', // stopped after encoding
526 }
527
528 requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.PollOneoffName, uint64(0), uint64(out),
529 uint64(nsubscriptions), uint64(resultNevents))
530
531 outMem, ok = mod.Memory().Read(out, uint32(len(expectedMem)))
532 require.True(t, ok)
533 require.Equal(t, expectedMem, outMem)
534
535 nevents, ok = mod.Memory().ReadUint32Le(resultNevents)
536 require.True(t, ok)
537 require.Equal(t, uint32(1), nevents)
538 }
539
540 func concat(bytes ...[]byte) []byte {
541 var res []byte
542 for i := range bytes {
543 res = append(res, bytes[i]...)
544 }
545 return res
546 }
547
548 // subscription for a given timeout in ns
549 func clockNsSub(ns uint64) []byte {
550 return []byte{
551 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
552 wasip1.EventTypeClock, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // event type and padding
553 wasip1.ClockIDMonotonic, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
554 byte(ns), byte(ns >> 8), byte(ns >> 16), byte(ns >> 24),
555 byte(ns >> 32), byte(ns >> 40), byte(ns >> 48), byte(ns >> 56), // timeout (ns)
556 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // precision (ns)
557 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // flags
558 }
559 }
560
561 // subscription for an EventTypeFdRead on a given fd
562 func fdReadSubFd(fd byte) []byte {
563 return []byte{
564 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // userdata
565 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
566 fd, 0x0, 0x0, 0x0, // valid readable FD
567 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
568 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
569 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
570 0x0, 0x0, 0x0, 0x0, // pad to 32 bytes
571 }
572 }
573
574 func fdReadSubFdWithUserData(fd byte, userdata []byte) []byte {
575 return concat(
576 userdata,
577 []byte{
578 wasip1.EventTypeFdRead, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
579 fd, 0x0, 0x0, 0x0, // valid readable FD
580 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
581 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
582 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
583 0x0, 0x0, 0x0, 0x0, // pad to 32 bytes
584 })
585 }
586
587 // subscription for an EventTypeFdRead on stdin
588 var fdReadSub = fdReadSubFd(byte(sys.FdStdin))
589
590 // ttyStat returns fs.ModeCharDevice | fs.ModeCharDevice as an approximation
591 // for isatty.
592 //
593 // See go-isatty for a more specific approach:
594 // https://github.com/mattn/go-isatty/blob/v0.0.18/isatty_tcgets.go#LL11C1-L12C1
595 type ttyStat struct{}
596
597 // Stat implements the same method as documented on sys.File
598 func (ttyStat) Stat() (sysapi.Stat_t, experimentalsys.Errno) {
599 return sysapi.Stat_t{
600 Mode: fs.ModeDevice | fs.ModeCharDevice,
601 Nlink: 1,
602 }, 0
603 }
604
605 type ttyStdinFile struct {
606 sys.StdinFile
607 ttyStat
608 }
609
610 type neverReadyTtyStdinFile struct {
611 sys.StdinFile
612 ttyStat
613 }
614
615 // Poll implements the same method as documented on sys.File
616 func (neverReadyTtyStdinFile) Poll(flag experimentalsys.Pflag, timeoutMillis int32) (ready bool, errno experimentalsys.Errno) {
617 if flag != experimentalsys.POLLIN {
618 return false, experimentalsys.ENOTSUP
619 }
620 switch {
621 case timeoutMillis <= 0:
622 return
623 }
624 time.Sleep(time.Duration(timeoutMillis) * time.Millisecond)
625 return false, 0
626 }
627
628 type pollStdinFile struct {
629 sys.StdinFile
630 ttyStat
631 ready bool
632 }
633
634 // Poll implements the same method as documented on sys.File
635 func (p *pollStdinFile) Poll(flag experimentalsys.Pflag, timeoutMillis int32) (ready bool, errno experimentalsys.Errno) {
636 if flag != experimentalsys.POLLIN {
637 return false, experimentalsys.ENOTSUP
638 }
639 return p.ready, 0
640 }