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>
37 lines
1.0 KiB
Go
37 lines
1.0 KiB
Go
package binary
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
|
|
wasm "github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
// decodeMemoryType returns the wasm.MemoryType decoded with the WebAssembly 1.0 (20191205) Binary Format.
|
|
//
|
|
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-memory
|
|
func decodeMemoryType(r *bytes.Reader) (*wasm.MemoryType, error) {
|
|
ret, err := decodeLimitsType(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if ret.Min > wasm.MemoryMaxPages {
|
|
return nil, fmt.Errorf("memory min must be at most 65536 pages (4GiB)")
|
|
}
|
|
if ret.Max != nil {
|
|
if *ret.Max < ret.Min {
|
|
return nil, fmt.Errorf("memory size minimum must not be greater than maximum")
|
|
} else if *ret.Max > wasm.MemoryMaxPages {
|
|
return nil, fmt.Errorf("memory max must be at most 65536 pages (4GiB)")
|
|
}
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
// encodeMemoryType returns the wasm.MemoryType encoded in WebAssembly 1.0 (20191205) Binary Format.
|
|
//
|
|
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-memory
|
|
func encodeMemoryType(i *wasm.MemoryType) []byte {
|
|
return encodeLimitsType(i)
|
|
}
|