...
1 package wasi_snapshot_preview1
2
3 import (
4 "context"
5 "io"
6
7 "github.com/tetratelabs/wazero/api"
8 "github.com/tetratelabs/wazero/experimental/sys"
9 "github.com/tetratelabs/wazero/internal/wasip1"
10 "github.com/tetratelabs/wazero/internal/wasm"
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 var randomGet = newHostFunc(wasip1.RandomGetName, randomGetFn, []api.ValueType{i32, i32}, "buf", "buf_len")
38
39 func randomGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
40 sysCtx := mod.(*wasm.ModuleInstance).Sys
41 randSource := sysCtx.RandSource()
42 buf, bufLen := uint32(params[0]), uint32(params[1])
43
44 randomBytes, ok := mod.Memory().Read(buf, bufLen)
45 if !ok {
46 return sys.EFAULT
47 }
48
49
50 if _, err := io.ReadAtLeast(randSource, randomBytes, int(bufLen)); err != nil {
51 return sys.EIO
52 }
53
54 return 0
55 }
56
View as plain text