-
Notifications
You must be signed in to change notification settings - Fork 10
/
plugin.go
310 lines (269 loc) · 9.26 KB
/
plugin.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
package extism
import (
"context"
"errors"
"fmt"
observe "github.com/dylibso/observe-sdk/go"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"os"
"strconv"
"strings"
"sync/atomic"
"time"
)
type CompiledPlugin struct {
runtime wazero.Runtime
main wazero.CompiledModule
extism wazero.CompiledModule
env api.Module
// when a module (main) is instantiated, it may have a module name that's added
// to the data section of the wasm. If this is the case, we won't be able to
// instantiate that module more than once. This counter acts as the module name
// incrementing each time we instantiate the module.
instanceCount atomic.Uint64
// this is the raw wasm bytes of the provided module, it is required when using a tracing observeAdapter.
// If an adapter is not provided, this field will be nil.
wasmBytes []byte
hasWasi bool
manifest Manifest
observeAdapter *observe.AdapterBase
observeOptions *observe.Options
maxHttp int64
maxVar int64
enableHttpResponseHeaders bool
}
type PluginConfig struct {
RuntimeConfig wazero.RuntimeConfig
EnableWasi bool
ObserveAdapter *observe.AdapterBase
ObserveOptions *observe.Options
EnableHttpResponseHeaders bool
// ModuleConfig is only used when a plugins are built using the NewPlugin
// function. In this function, the plugin is both compiled, and an instance
// of the plugin is instantiated, and the ModuleConfig is passed to the
// instance.
//
// When plugins are built using NewCompiledPlugin, the ModuleConfig has no
// effect because the instance is not created. Instead, the ModuleConfig is
// passed directly in calls to the CompiledPlugin.Instance method.
ModuleConfig wazero.ModuleConfig
}
// NewPlugin creates compiles and instantiates a plugin that is ready
// to be used. Plugins are not thread-safe. If you need to use a plugin
// across multiple goroutines, use NewCompiledPlugin and create instances
// of the plugin using the CompiledPlugin.Instance method.
func NewPlugin(
ctx context.Context,
manifest Manifest,
config PluginConfig,
functions []HostFunction,
) (*Plugin, error) {
c, err := NewCompiledPlugin(ctx, manifest, config, functions)
if err != nil {
return nil, err
}
p, err := c.Instance(ctx, PluginInstanceConfig{
ModuleConfig: config.ModuleConfig,
})
if err != nil {
return nil, err
}
p.close = append(p.close, c.Close)
return p, nil
}
// NewCompiledPlugin creates a compiled plugin that is ready to be instantiated.
// You can instantiate the plugin multiple times using the CompiledPlugin.Instance
// method and run those instances concurrently.
func NewCompiledPlugin(
ctx context.Context,
manifest Manifest,
config PluginConfig,
funcs []HostFunction,
) (*CompiledPlugin, error) {
count := len(manifest.Wasm)
if count == 0 {
return nil, fmt.Errorf("manifest can't be empty")
}
var cfg wazero.RuntimeConfig
if config.RuntimeConfig == nil {
cfg = wazero.NewRuntimeConfig()
} else {
cfg = config.RuntimeConfig
}
// Make sure function calls are cancelled if the context is cancelled
if manifest.Timeout > 0 {
cfg = cfg.WithCloseOnContextDone(true)
}
if manifest.Memory != nil {
if manifest.Memory.MaxPages > 0 {
cfg = cfg.WithMemoryLimitPages(manifest.Memory.MaxPages)
}
}
p := CompiledPlugin{
manifest: manifest,
runtime: wazero.NewRuntimeWithConfig(ctx, cfg),
observeAdapter: config.ObserveAdapter,
observeOptions: config.ObserveOptions,
enableHttpResponseHeaders: config.EnableHttpResponseHeaders,
}
if config.EnableWasi {
wasi_snapshot_preview1.MustInstantiate(ctx, p.runtime)
p.hasWasi = true
}
// Build host modules
hostModules := make(map[string][]HostFunction)
for _, f := range funcs {
hostModules[f.Namespace] = append(hostModules[f.Namespace], f)
}
for name, funcs := range hostModules {
_, err := buildHostModule(ctx, p.runtime, name, funcs)
if err != nil {
return nil, fmt.Errorf("building host module: %w", err)
}
}
// Compile the extism module
var err error
p.extism, err = p.runtime.CompileModule(ctx, extismRuntimeWasm)
if err != nil {
return nil, fmt.Errorf("instantiating extism module: %w", err)
}
// Build and instantiate extism:host/env module
p.env, err = instantiateEnvModule(ctx, p.runtime)
if err != nil {
return nil, err
}
// Try to find the main module:
// - There is always one main module
// - If a Wasm value has the Name field set to "main" then use that module
// - If there is only one module in the manifest then that is the main module by default
// - Otherwise the last module listed is the main module
modules := map[string]wazero.CompiledModule{}
for i, wasm := range manifest.Wasm {
data, err := wasm.ToWasmData(ctx)
if err != nil {
return nil, err
}
_, mainExists := modules["main"]
if data.Name == "" || i == len(manifest.Wasm)-1 && !mainExists {
data.Name = "main"
}
_, okm := modules[data.Name]
if data.Name == "extism:host/env" || okm {
return nil, fmt.Errorf("module name collision: '%s'", data.Name)
}
if data.Hash != "" {
calculatedHash := calculateHash(data.Data)
if data.Hash != calculatedHash {
return nil, fmt.Errorf("hash mismatch for module '%s'", data.Name)
}
}
if p.observeAdapter != nil {
p.wasmBytes = data.Data
}
m, err := p.runtime.CompileModule(ctx, data.Data)
if err != nil {
return nil, err
}
if data.Name == "main" {
p.main = m
} else {
modules[data.Name] = m
}
}
if p.main == nil {
return nil, errors.New("no main module found")
}
// We no longer need the wasm in the manifest so nil it
// to make the slice eligible for garbage collection.
p.manifest.Wasm = nil
return &p, nil
}
func (p *CompiledPlugin) Close(ctx context.Context) error {
return p.runtime.Close(ctx)
}
func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConfig) (*Plugin, error) {
var closers []func(ctx context.Context) error
moduleConfig := config.ModuleConfig
if moduleConfig == nil {
moduleConfig = wazero.NewModuleConfig()
}
moduleConfig = moduleConfig.WithName(strconv.Itoa(int(p.instanceCount.Add(1))))
// NOTE: this is only necessary for guest modules because
// host modules have the same access privileges as the host itself
fs := wazero.NewFSConfig()
for host, guest := range p.manifest.AllowedPaths {
if strings.HasPrefix(host, "ro:") {
trimmed := strings.TrimPrefix(host, "ro:")
fs = fs.WithReadOnlyDirMount(trimmed, guest)
} else {
fs = fs.WithDirMount(host, guest)
}
}
// NOTE: we don't want wazero to call the start function, we will initialize
// the guest runtime manually.
// See: https://github.com/extism/go-sdk/pull/1#issuecomment-1650527495
moduleConfig = moduleConfig.WithStartFunctions().WithFSConfig(fs)
_, wasiOutput := os.LookupEnv("EXTISM_ENABLE_WASI_OUTPUT")
if p.hasWasi && wasiOutput {
moduleConfig = moduleConfig.WithStderr(os.Stderr).WithStdout(os.Stdout)
}
var trace *observe.TraceCtx
var err error
if p.observeAdapter != nil {
trace, err = p.observeAdapter.NewTraceCtx(ctx, p.runtime, p.wasmBytes, p.observeOptions)
if err != nil {
return nil, fmt.Errorf("failed to initialize Observe Adapter: %v", err)
}
trace.Finish()
}
// Compile and instantiate the extism runtime. This runtime is stateful and needs to be
// instantiated on a per-instance basis. We don't provide a name because the module needs
// to be anonymous -- you cannot instantiate multiple modules with the same name into the
// same runtime. It is okay that this is anonymous, because this module is only called
// from Go host functions and not from the Wasm module itself.
extism, err := p.runtime.InstantiateModule(ctx, p.extism, wazero.NewModuleConfig())
if err != nil {
return nil, fmt.Errorf("instantiating extism module: %w", err)
}
closers = append(closers, extism.Close)
main, err := p.runtime.InstantiateModule(ctx, p.main, moduleConfig)
if err != nil {
return nil, fmt.Errorf("instantiating module: %w", err)
}
closers = append(closers, main.Close)
p.maxHttp = int64(1024 * 1024 * 50)
if p.manifest.Memory != nil && p.manifest.Memory.MaxHttpResponseBytes >= 0 {
p.maxHttp = p.manifest.Memory.MaxHttpResponseBytes
}
p.maxVar = int64(1024 * 1024)
if p.manifest.Memory != nil && p.manifest.Memory.MaxVarBytes >= 0 {
p.maxVar = p.manifest.Memory.MaxVarBytes
}
var headers map[string]string = nil
if p.enableHttpResponseHeaders {
headers = map[string]string{}
}
instance := &Plugin{
close: closers,
extism: extism,
hasWasi: p.hasWasi,
module: main,
Timeout: time.Duration(p.manifest.Timeout) * time.Millisecond,
Config: p.manifest.Config,
Var: make(map[string][]byte),
AllowedHosts: p.manifest.AllowedHosts,
AllowedPaths: p.manifest.AllowedPaths,
LastStatusCode: 0,
LastResponseHeaders: headers,
MaxHttpResponseBytes: p.maxHttp,
MaxVarBytes: p.maxVar,
guestRuntime: guestRuntime{},
Adapter: p.observeAdapter,
log: logStd,
traceCtx: trace,
}
instance.guestRuntime = detectGuestRuntime(ctx, instance)
return instance, nil
}