-
Notifications
You must be signed in to change notification settings - Fork 7
/
ww.go
190 lines (161 loc) · 4.36 KB
/
ww.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
package ww
import (
"context"
"crypto/rand"
_ "embed"
"errors"
"fmt"
"io"
"log/slog"
"net"
"runtime"
"strings"
"capnproto.org/go/capnp/v3"
"capnproto.org/go/capnp/v3/rpc"
"github.com/stealthrocket/wazergo"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
wasi "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/wetware/pkg/api/core"
"github.com/wetware/pkg/auth"
"github.com/wetware/pkg/rom"
"github.com/wetware/pkg/system"
)
type SystemError interface {
error
ExitCode() uint32
}
// Ww is the execution context for WebAssembly (WASM) bytecode,
// allowing it to interact with (1) the local host and (2) the
// cluster environment.
type Ww struct {
NS string
Env, Args []string
Stdout, Stderr io.WriteCloser
Sess auth.Session
Cache wazero.CompilationCache
LogLevel slog.Level
}
// String returns the cluster namespace in which the wetware is
// executing. If ww.NS has been assigned a non-empty string, it
// returns the string unchanged. Else, it defaults to "ww".
//
// This may change in the future.
func (ww *Ww) String() string {
if ww.NS != "" {
return ww.NS
}
return "ww"
}
func (ww Ww) Logger() *slog.Logger {
return slog.Default().With(
"ns", ww.NS)
}
func (ww Ww) NewRuntime(ctx context.Context) wazero.Runtime {
if ww.Cache == nil {
ww.Cache = wazero.NewCompilationCache()
}
return wazero.NewRuntimeWithConfig(ctx, wazero.
NewRuntimeConfigCompiler().
WithCompilationCache(ww.Cache).
WithCloseOnContextDone(true))
}
// Exec compiles and runs the ww instance's ROM in a WASM runtime.
// It returns any error produced by the compilation or execution of
// the ROM.
func (ww Ww) Exec(ctx context.Context, rom rom.ROM) error {
// Spawn a new runtime.
r := ww.NewRuntime(ctx)
defer r.Close(ctx)
c, err := wasi.Instantiate(ctx, r)
if err != nil {
return err
}
defer c.Close(ctx)
// Instantiate wetware system socket.
host, guest := net.Pipe()
defer host.Close()
// Instantiate the system host module.
sys, err := system.Instantiate(ctx, r, guest)
if err != nil {
return err
}
defer sys.Close(ctx)
ctx = wazergo.WithModuleInstance(ctx, sys)
// Compile guest module.
compiled, err := r.CompileModule(ctx, rom.Bytecode)
if err != nil {
return err
}
defer compiled.Close(ctx)
// Bind the Wetware environment to the wazero.ModuleConfig.
mc, err := ww.BindConfig(rom, guest)
if err != nil {
return err
}
defer ww.BindSocket(host).Close()
// Instantiate the guest module.
mod, err := r.InstantiateModule(ctx, compiled, mc)
if err != nil {
return err
}
defer mod.Close(ctx)
return ww.run(ctx, mod)
}
func (ww Ww) BindSocket(sock io.ReadWriteCloser) *rpc.Conn {
server := core.Terminal_NewServer(ww.Sess)
logger := ww.Logger().With(
"host", ww.Sess.Peer(),
"peer", ww.Sess.Peer(),
"vat", ww.Sess.Vat())
return rpc.NewConn(rpc.NewStreamTransport(sock), &rpc.Options{
BootstrapClient: capnp.NewClient(server),
ErrorReporter: system.ErrorReporter{Logger: logger},
})
}
func (ww Ww) BindConfig(rom rom.ROM, r io.Reader) (wazero.ModuleConfig, error) {
args := append([]string{rom.String()}, ww.Args...)
return ww.BindEnv(wazero.NewModuleConfig().
WithOsyield(runtime.Gosched).
WithRandSource(rand.Reader).
WithStartFunctions(). // don't automatically call _start while instanitating.
WithSysNanosleep().
WithSysNanotime().
WithSysWalltime().
WithArgs(args...).
WithName(rom.String()).
WithStdout(ww.Stdout).
WithStderr(ww.Stderr).
WithStdin(r))
}
func (ww Ww) BindEnv(mc wazero.ModuleConfig) (wazero.ModuleConfig, error) {
return bindEnv(mc.
WithEnv("ns", ww.String()).
WithEnv("WW_LOGLVL", ww.LogLevel.String()),
ww.Env)
}
func bindEnv(mc wazero.ModuleConfig, vars []string) (wazero.ModuleConfig, error) {
for _, s := range vars {
if ss, ok := parseEnvVar(s); ok {
mc = mc.WithEnv(ss[0], ss[1])
} else {
return nil, fmt.Errorf("invalid env var: %s", ss)
}
}
return mc, nil
}
func parseEnvVar(s string) (ss [2]string, ok bool) {
if kv := strings.SplitN(s, "=", 1); len(kv) == 2 {
ss[0], ss[1] = kv[0], kv[1]
ok = true
}
return
}
func (ww Ww) run(ctx context.Context, mod api.Module) error {
// Grab the the main() function and call it with the system context.
fn := mod.ExportedFunction("_start")
if fn == nil {
return errors.New("missing export: _start")
}
return fn.CallWithStack(ctx, nil)
}