-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.go
479 lines (396 loc) · 14.1 KB
/
core.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
package water
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"os"
"runtime"
"sync"
"github.com/refraction-networking/water/internal/log"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/gaukas/wazerofs/memfs"
expsysfs "github.com/tetratelabs/wazero/experimental/sysfs"
)
var (
ErrModuleNotImported = fmt.Errorf("water: importing a module not imported by the WebAssembly module")
ErrFuncNotImported = fmt.Errorf("water: importing a function not imported by the WebAssembly module")
)
// Core provides the low-level access to the WebAssembly runtime
// environment.
type Core interface {
// Config returns the Config used to create the Core.
//
// Practically, this function is not supposed to be used
// to retrieve the Config to be used for creating another
// Core. Instead, it is used to retrieve the Config for
// inspection/debugging purposes.
Config() *Config
// Context returns the base context used by the Core.
Context() context.Context
// ContextCancel cancels the base context used by the Core.
ContextCancel()
// Close closes the Core and releases all the resources
// associated with it.
Close() error
// Exports dumps all the exported functions and globals which
// are provided by the WebAssembly module.
//
// This function returns a map of export name to the type of
// the export.
Exports() map[string]api.ExternType
// ExportedFunction returns the exported function with the
// given name. If no function with the given name is exported,
// this function returns nil.
//
// It is caller's responsibility to ensure that the function
// returned has the correct signature, which can be checked
// by inspecting the returned api.Function.Definition().
ExportedFunction(name string) api.Function
// Imports dumps all the imported functions and globals which
// are required to be provided by the host environment to
// instantiate the WebAssembly module.
//
// This function returns a map of module name to a map of
// import name to the type of the import.
ImportedFunctions() map[string]map[string]api.FunctionDefinition
// ImportFunction imports a function into the WebAssembly module.
//
// The f argument must be a function with the signature matching
// the signature of the function to be imported. Otherwise, the
// behavior of the WebAssembly Transport Module after initialization
// is undefined.
//
// This function can be called ONLY BEFORE calling Instantiate().
ImportFunction(module, name string, f any) error
// InsertConn inserts a net.Conn into the WebAssembly Transport
// Module and returns the key of the inserted connection as a
// file descriptor accessible from the WebAssembly instance.
//
// This function SHOULD be called only if the WebAssembly instance
// execution is blocked/halted/stopped. Otherwise, race conditions
// or undefined behaviors may occur.
InsertConn(conn net.Conn) (fd int32, err error)
// InsertListener inserts a net.Listener into the WebAssembly
// Transport Module and returns the key of the inserted listener
// as a file descriptor accessible from the WebAssembly instance.
//
// This function SHOULD be called only if the WebAssembly instance
// execution is blocked/halted/stopped. Otherwise, race conditions
// or undefined behaviors may occur.
InsertListener(tcpListener net.Listener) (fd int32, err error)
// InsertFile inserts a file into the WebAssembly Transport Module
// and returns the key of the inserted file as a file descriptor
// accessible from the WebAssembly instance.
//
// This function SHOULD be called only if the WebAssembly instance
// execution is blocked/halted/stopped. Otherwise, race conditions
// or undefined behaviors may occur.
InsertFile(osFile *os.File) (fd int32, err error)
// Instantiate instantiates the module into a new instance of
// WebAssembly Transport Module.
Instantiate() error
// Invoke invokes a function in the WebAssembly instance.
//
// If the target function is not exported, this function returns an error.
Invoke(funcName string, params ...uint64) (results []uint64, err error)
// ReadIovs reads data from the memory pointed by iovs and writes it to buf.
ReadIovs(iovs, iovsLen int32, buf []byte) (int, error)
// WASIPreview1 enables the WASI preview1 API.
//
// It is recommended that this function only to be invoked if
// the WATM expects the WASI preview1 API to be available.
//
// Note that at the time of writing, all WATM implementations
// expect the WASI preview1 API to be available.
WASIPreview1() error
// Logger returns the logger used by the Core. If not set, it
// should return the default global logger instead of nil.
Logger() *log.Logger
}
// type guard
var _ Core = (*core)(nil)
// core provides the WASM runtime base and is an internal struct
// that every RuntimeXxx implementation will embed.
//
// core is designed to be unmanaged and version-independent,
// which means it does not provide any functionalities other
// than simply collecting all the WASM runtime-related objects
// without overwriting access on them. And core is not subject
// to breaking changes unless a severe bug needs to be fixed
// in such a breaking manner inevitably.
//
// This struct implements Core.
type core struct {
// config
config *Config
ctx context.Context
ctxCancel context.CancelFunc
runtime wazero.Runtime
module wazero.CompiledModule
instance api.Module
// saved after Exports() is called
exportsLoadOnce sync.Once
exports map[string]api.ExternType
// saved after ImportedFunctions() is called
importedFuncsLoadOnce sync.Once
importedFuncs map[string]map[string]api.FunctionDefinition
importModules map[string]wazero.HostModuleBuilder
closeOnce sync.Once
}
// NewCore creates a new Core with the given config.
//
// It uses the default implementation of interface.Core as
// defined in this file.
//
// Deprecated: use [NewCoreWithContext] instead.
func NewCore(config *Config) (Core, error) {
return NewCoreWithContext(context.Background(), config)
}
// NewCoreWithContext creates a new Core with the given context and config.
//
// It uses the default implementation of interface.Core as
// defined in this file.
//
// The context is used to control the lifetime of the call to
// function calls into the WebAssembly module. If the context
// is canceled or reaches its deadline, any current and future
// function call will return with an error. Call
// [WazeroRuntimeConfigFactory.SetCloseOnContextDone] with false
// to disable this behavior.
func NewCoreWithContext(ctx context.Context, config *Config) (Core, error) {
var err error
c := &core{
config: config,
importModules: make(map[string]wazero.HostModuleBuilder),
}
c.ctx, c.ctxCancel = context.WithCancel(ctx)
c.runtime = wazero.NewRuntimeWithConfig(ctx, config.RuntimeConfig().GetConfig())
if c.module, err = c.runtime.CompileModule(ctx, c.config.WATMBinOrPanic()); err != nil {
return nil, fmt.Errorf("water: (*Runtime).CompileModule returned error: %w", err)
}
runtime.SetFinalizer(c, func(core *core) {
c.Close()
})
return c, nil
}
// Config implements Core.
func (c *core) Config() *Config {
return c.config
}
// Context implements Core.
func (c *core) Context() context.Context {
return c.ctx
}
// ContextCancel implements Core.
func (c *core) ContextCancel() {
c.ctxCancel()
}
func (c *core) cleanup() {
for i := range c.importModules {
delete(c.importModules, i)
}
for i := range c.exports {
delete(c.exports, i)
}
for i := range c.importedFuncs {
delete(c.importedFuncs, i)
}
}
// Close implements Core.
func (c *core) Close() error {
var closeErr error
c.closeOnce.Do(func() {
if c.instance != nil {
if err := c.instance.Close(c.ctx); err != nil {
closeErr = fmt.Errorf("water: (*wazero/api.Module).Close returned error: %w", err)
return
}
c.instance = nil // TODO: force dropped
log.LDebugf(c.config.Logger(), "INSTANCE DROPPED")
}
if c.runtime != nil {
if err := c.runtime.Close(c.ctx); err != nil {
closeErr = fmt.Errorf("water: (*wazero.Runtime).Close returned error: %w", err)
return
}
c.runtime = nil // TODO: force dropped
log.LDebugf(c.config.Logger(), "RUNTIME DROPPED")
}
if c.module != nil {
if err := c.module.Close(c.ctx); err != nil {
closeErr = fmt.Errorf("water: (*wazero.CompiledModule).Close returned error: %w", err)
return
}
c.module = nil // TODO: force dropped
log.LDebugf(c.config.Logger(), "MODULE DROPPED")
}
if c.ctxCancel != nil {
c.ctxCancel()
c.ctxCancel = nil
log.LDebugf(c.config.Logger(), "CONTEXT CANCELED")
}
if c.ctx != nil {
c.ctx = nil // TODO: force dropped
log.LDebugf(c.config.Logger(), "CONTEXT DROPPED")
}
c.cleanup()
})
return closeErr
}
// Exports implements Core.
func (c *core) Exports() map[string]api.ExternType {
c.exportsLoadOnce.Do(func() {
c.exports = c.module.AllExports()
})
return c.exports
}
// ExportedFunction implements Core.
func (c *core) ExportedFunction(name string) api.Function {
if c.instance == nil {
return nil
}
return c.instance.ExportedFunction(name)
}
// ImportedFunctions implements Core.
func (c *core) ImportedFunctions() map[string]map[string]api.FunctionDefinition {
c.importedFuncsLoadOnce.Do(func() {
importedFuncs := c.module.ImportedFunctions()
c.importedFuncs = make(map[string]map[string]api.FunctionDefinition)
for _, importedFunc := range importedFuncs {
mod, name, ok := importedFunc.Import()
if !ok {
continue
}
if _, ok := c.importedFuncs[mod]; !ok {
c.importedFuncs[mod] = make(map[string]api.FunctionDefinition)
}
c.importedFuncs[mod][name] = importedFunc
}
})
return c.importedFuncs
}
// ImportFunction implements Core.
func (c *core) ImportFunction(module, name string, f any) error {
if c.instance != nil {
return fmt.Errorf("water: cannot import function after instantiation")
}
// Unsafe: check if the WebAssembly module really imports this function under
// the given module and name. If not, we warn and skip the import.
if mod, ok := c.ImportedFunctions()[module]; !ok {
log.LDebugf(c.config.Logger(), "water: module %s is not imported by the WebAssembly module.", module)
return ErrModuleNotImported
} else if _, ok := mod[name]; !ok {
log.LWarnf(c.config.Logger(), "water: function %s.%s is not imported by the WebAssembly module.", module, name)
return ErrFuncNotImported
}
if _, ok := c.importModules[module]; !ok {
c.importModules[module] = c.runtime.NewHostModuleBuilder(module)
}
// We don't do this:
//
// _, err := c.importModules[module].NewFunctionBuilder().WithFunc(f).Export(name).Instantiate(c.ctx)
// if err != nil {
// log.LErrorf(c.config.Logger(), "water: (*wazero.HostModuleBuilder).NewFunctionBuilder returned error: %v", err)
// }
//
// Instead we do not instantiate the function here, but wait until Instantiate() is called.
c.importModules[module] = c.importModules[module].NewFunctionBuilder().WithFunc(f).Export(name)
// TODO: return an error if the function already exists or the
// module/function name is invalid.
return nil
}
// Instantiate implements Core.
func (c *core) Instantiate() (err error) {
if c.instance != nil {
return fmt.Errorf("water: double instantiation is not allowed")
}
// Instantiate the imported functions
for _, moduleBuilder := range c.importModules {
if _, err := moduleBuilder.Instantiate(c.ctx); err != nil {
return fmt.Errorf("water: (*wazero.HostModuleBuilder).Instantiate returned error: %w", err)
}
}
// If TransportModuleConfig is set, we pass the config to the runtime.
if c.config.TransportModuleConfig != nil {
mc := c.config.ModuleConfig()
fsCfg := mc.GetFSConfig()
if fsCfg == nil {
fsCfg = wazero.NewFSConfig()
}
memFS := memfs.New()
err := memFS.WriteFile("watm.cfg", c.config.TransportModuleConfig.AsBytes())
if !errors.Is(err, nil) && !errors.Is(err, sys.Errno(0)) {
return fmt.Errorf("water: memFS.WriteFile returned error: %w", err)
}
if expFsCfg, ok := fsCfg.(expsysfs.FSConfig); ok {
fsCfg = expFsCfg.WithSysFSMount(memFS, "/conf/")
mc.SetFSConfig(fsCfg)
}
} else {
log.LWarnf(c.config.Logger(), "water: TransportModuleConfig is not set, skipping...")
}
if c.instance, err = c.runtime.InstantiateModule(
c.ctx,
c.module,
c.config.ModuleConfig().GetConfig()); err != nil {
return fmt.Errorf("water: (*Runtime).InstantiateWithConfig returned error: %w", err)
}
return nil
}
// Invoke implements Core.
func (c *core) Invoke(funcName string, params ...uint64) (results []uint64, err error) {
if c.instance == nil {
return nil, fmt.Errorf("water: cannot invoke function before instantiation")
}
expFunc := c.instance.ExportedFunction(funcName)
if expFunc == nil {
return nil, fmt.Errorf("water: function %q is not exported", funcName)
}
results, err = expFunc.Call(c.ctx, params...)
if err != nil {
return nil, fmt.Errorf("water: (*wazero.ExportedFunction)%q.Call returned error: %w", funcName, err)
}
return
}
var le = binary.LittleEndian
// adapted from fd_write implementation in wazero
func (c *core) ReadIovs(iovs, iovsLen int32, buf []byte) (n int, err error) {
mem := c.instance.Memory()
iovsStop := uint32(iovsLen) << 3 // iovsCount * 8
iovsBuf, ok := mem.Read(uint32(iovs), iovsStop)
if !ok {
return 0, errors.New("ReadIovs: failed to read iovs from memory")
}
for iovsPos := uint32(0); iovsPos < iovsStop; iovsPos += 8 {
offset := le.Uint32(iovsBuf[iovsPos:])
l := le.Uint32(iovsBuf[iovsPos+4:])
b, ok := mem.Read(offset, l)
if !ok {
return 0, errors.New("ReadIovs: failed to read iov from memory")
}
// Write to buf
nCopied := copy(buf[n:], b)
n += nCopied
if nCopied != len(b) {
return n, io.ErrShortBuffer
}
}
return
}
// WASIPreview1 implements Core.
func (c *core) WASIPreview1() error {
if _, err := wasi_snapshot_preview1.Instantiate(c.ctx, c.runtime); err != nil {
return fmt.Errorf("water: wazero/imports/wasi_snapshot_preview1.Instantiate returned error: %w", err)
}
return nil
}
// Logger implements Core.
func (c *core) Logger() *log.Logger {
return c.config.Logger()
}