-
Notifications
You must be signed in to change notification settings - Fork 243
/
runtime.go
216 lines (189 loc) · 6.17 KB
/
runtime.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
package vs
import (
"context"
"errors"
"fmt"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/wasm"
)
type RuntimeConfig struct {
Name string
ModuleName string
ModuleWasm []byte
FuncNames []string
NeedsWASI bool
NeedsMemoryExport bool
// LogFn requires the implementation to export a function "env.log" which accepts i32i32_v.
// The implementation invoke this with a byte slice allocated from the offset, length pair.
// This function simulates a host function that logs a message.
LogFn func([]byte) error
// EnvFReturnValue is set to non-zero if we want the runtime to instantiate "env" module with the function "f"
// which accepts one i64 value and returns the EnvFReturnValue as i64. This is mutually exclusive to LogFn.
EnvFReturnValue uint64
}
type Runtime interface {
Name() string
Compile(context.Context, *RuntimeConfig) error
Instantiate(context.Context, *RuntimeConfig) (Module, error)
Close(context.Context) error
}
type Module interface {
CallI32_I32(ctx context.Context, funcName string, param uint32) (uint32, error)
CallI32I32_V(ctx context.Context, funcName string, x, y uint32) error
CallI32_V(ctx context.Context, funcName string, param uint32) error
CallV_V(ctx context.Context, funcName string) error
CallI64_I64(ctx context.Context, funcName string, param uint64) (uint64, error)
WriteMemory(ctx context.Context, offset uint32, bytes []byte) error
Memory() []byte
Close(context.Context) error
}
func NewWazeroInterpreterRuntime() Runtime {
return newWazeroRuntime("wazero-interpreter", wazero.NewRuntimeConfigInterpreter().WithWasmCore2())
}
func NewWazeroCompilerRuntime() Runtime {
return newWazeroRuntime(compilerRuntime, wazero.NewRuntimeConfigCompiler().WithWasmCore2())
}
func newWazeroRuntime(name string, config wazero.RuntimeConfig) *wazeroRuntime {
return &wazeroRuntime{name: name, config: config}
}
type wazeroRuntime struct {
name string
config wazero.RuntimeConfig
runtime wazero.Runtime
logFn func([]byte) error
env, compiled wazero.CompiledModule
}
type wazeroModule struct {
wasi api.Closer
env, mod api.Module
funcs map[string]api.Function
}
func (r *wazeroRuntime) Name() string {
return r.name
}
func (m *wazeroModule) Memory() []byte {
return m.mod.Memory().(*wasm.MemoryInstance).Buffer
}
func (r *wazeroRuntime) log(ctx context.Context, m api.Module, offset, byteCount uint32) {
buf, ok := m.Memory().Read(ctx, offset, byteCount)
if !ok {
panic("out of memory reading log buffer")
}
if err := r.logFn(buf); err != nil {
panic(err)
}
}
func (r *wazeroRuntime) Compile(ctx context.Context, cfg *RuntimeConfig) (err error) {
r.runtime = wazero.NewRuntimeWithConfig(ctx, r.config)
if cfg.LogFn != nil {
r.logFn = cfg.LogFn
if r.env, err = r.runtime.NewModuleBuilder("env").
ExportFunction("log", r.log).Compile(ctx, wazero.NewCompileConfig()); err != nil {
return err
}
} else if cfg.EnvFReturnValue != 0 {
if r.env, err = r.runtime.NewModuleBuilder("env").
ExportFunction("f",
// Note: accepting (context.Context, api.Module) is the slowest type of host function with wazero.
func(context.Context, api.Module, uint64) uint64 {
return cfg.EnvFReturnValue
},
).Compile(ctx, wazero.NewCompileConfig()); err != nil {
return err
}
}
r.compiled, err = r.runtime.CompileModule(ctx, cfg.ModuleWasm, wazero.NewCompileConfig())
return
}
func (r *wazeroRuntime) Instantiate(ctx context.Context, cfg *RuntimeConfig) (mod Module, err error) {
wazeroCfg := wazero.NewModuleConfig().WithName(cfg.ModuleName)
m := &wazeroModule{funcs: map[string]api.Function{}}
// Instantiate WASI, if configured.
if cfg.NeedsWASI {
if m.wasi, err = wasi_snapshot_preview1.Instantiate(ctx, r.runtime); err != nil {
return
}
}
// Instantiate the host module, "env", if configured.
if env := r.env; env != nil {
if m.env, err = r.runtime.InstantiateModule(ctx, env, wazero.NewModuleConfig()); err != nil {
return
}
}
// Instantiate the module.
if m.mod, err = r.runtime.InstantiateModule(ctx, r.compiled, wazeroCfg); err != nil {
return
}
// Ensure function exports exist.
for _, funcName := range cfg.FuncNames {
if fn := m.mod.ExportedFunction(funcName); fn == nil {
return nil, fmt.Errorf("%s is not an exported function", funcName)
} else {
m.funcs[funcName] = fn
}
}
mod = m
return
}
func (r *wazeroRuntime) Close(ctx context.Context) (err error) {
if compiled := r.compiled; compiled != nil {
err = compiled.Close(ctx)
}
r.compiled = nil
if env := r.env; env != nil {
err = env.Close(ctx)
}
r.env = nil
return
}
func (m *wazeroModule) CallV_V(ctx context.Context, funcName string) (err error) {
_, err = m.funcs[funcName].Call(ctx)
return
}
func (m *wazeroModule) CallI32_I32(ctx context.Context, funcName string, param uint32) (uint32, error) {
if results, err := m.funcs[funcName].Call(ctx, uint64(param)); err != nil {
return 0, err
} else if len(results) > 0 {
return uint32(results[0]), nil
}
return 0, nil
}
func (m *wazeroModule) CallI32I32_V(ctx context.Context, funcName string, x, y uint32) (err error) {
_, err = m.funcs[funcName].Call(ctx, uint64(x), uint64(y))
return
}
func (m *wazeroModule) CallI32_V(ctx context.Context, funcName string, param uint32) (err error) {
_, err = m.funcs[funcName].Call(ctx, uint64(param))
return
}
func (m *wazeroModule) CallI64_I64(ctx context.Context, funcName string, param uint64) (uint64, error) {
if results, err := m.funcs[funcName].Call(ctx, param); err != nil {
return 0, err
} else if len(results) > 0 {
return results[0], nil
}
return 0, nil
}
func (m *wazeroModule) WriteMemory(ctx context.Context, offset uint32, bytes []byte) error {
if !m.mod.Memory().Write(ctx, offset, bytes) {
return errors.New("out of memory writing name")
}
return nil
}
func (m *wazeroModule) Close(ctx context.Context) (err error) {
if mod := m.mod; mod != nil {
err = mod.Close(ctx)
}
m.mod = nil
if env := m.env; env != nil {
err = env.Close(ctx)
}
m.env = nil
if wasi := m.wasi; wasi != nil {
err = wasi.Close(ctx)
}
m.wasi = nil
return
}