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