-
Notifications
You must be signed in to change notification settings - Fork 243
/
random.go
56 lines (49 loc) · 1.58 KB
/
random.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
package wasi_snapshot_preview1
import (
"context"
"io"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/wasm"
)
const functionRandomGet = "random_get"
// randomGet is the WASI function named functionRandomGet which writes random
// data to a buffer.
//
// # Parameters
//
// - buf: api.Memory offset to write random values
// - bufLen: size of random data in bytes
//
// Result (Errno)
//
// The return value is ErrnoSuccess except the following error conditions:
// - ErrnoFault: `buf` or `bufLen` point to an offset out of memory
// - ErrnoIo: a file system error
//
// For example, if underlying random source was seeded like
// `rand.NewSource(42)`, we expect api.Memory to contain:
//
// bufLen (5)
// +--------------------------+
// | |
// []byte{?, 0x53, 0x8c, 0x7f, 0x96, 0xb1, ?}
// buf --^
//
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-random_getbuf-pointeru8-bufLen-size---errno
var randomGet = wasm.NewGoFunc(
functionRandomGet, functionRandomGet,
[]string{"buf", "buf_len"},
func(ctx context.Context, mod api.Module, buf uint32, bufLen uint32) (errno Errno) {
sysCtx := mod.(*wasm.CallContext).Sys
randSource := sysCtx.RandSource()
randomBytes, ok := mod.Memory().Read(ctx, buf, bufLen)
if !ok { // out-of-range
return ErrnoFault
}
// We can ignore the returned n as it only != byteCount on error
if _, err := io.ReadAtLeast(randSource, randomBytes, int(bufLen)); err != nil {
return ErrnoIo
}
return ErrnoSuccess
},
)