Adds ability to disable mutable globals and improves decode perf (#315)

This adds `RuntimeConfig.WithFeatureMutableGlobal(enabled bool)`, which
allows disabling of mutable globals. When disabled, any attempt to add a
mutable global, either explicitly or implicitly via decoding wasm will
fail.

To support this, there's a new `Features` bitflag that can allow up to
63 feature toggles without passing structs.

While here, I fixed a significant performance problem in decoding
binary:

Before
```
BenchmarkCodecExample/binary.DecodeModule-16         	  184243	      5623 ns/op	    3848 B/op	     184 allocs/op
```

Now
```
BenchmarkCodecExample/binary.DecodeModule-16         	  294084	      3520 ns/op	    2176 B/op	      91 allocs/op

```

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-03-03 10:31:10 +08:00
committed by GitHub
parent 490f830096
commit cfb11f352a
33 changed files with 519 additions and 221 deletions

10
wasm.go
View File

@@ -65,12 +65,16 @@ func NewRuntime() Runtime {
// NewRuntimeWithConfig returns a runtime with the given configuration.
func NewRuntimeWithConfig(config *RuntimeConfig) Runtime {
return &runtime{store: internalwasm.NewStore(config.ctx, config.engine)}
return &runtime{
store: internalwasm.NewStore(config.ctx, config.engine, config.enabledFeatures),
enabledFeatures: config.enabledFeatures,
}
}
// runtime allows decoupling of public interfaces from internal representation.
type runtime struct {
store *internalwasm.Store
store *internalwasm.Store
enabledFeatures internalwasm.Features
}
// Module implements wasm.Store Module
@@ -101,7 +105,7 @@ func (r *runtime) DecodeModule(source []byte) (*DecodedModule, error) {
decoder = text.DecodeModule
}
internal, err := decoder(source)
internal, err := decoder(source, r.enabledFeatures)
if err != nil {
return nil, err
} else if err = internal.Validate(); err != nil {