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>
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package binary
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
|
|
"github.com/tetratelabs/wazero/internal/ieee754"
|
|
"github.com/tetratelabs/wazero/internal/leb128"
|
|
wasm "github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
func decodeConstantExpression(r *bytes.Reader) (*wasm.ConstantExpression, error) {
|
|
b, err := r.ReadByte()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read opcode: %v", err)
|
|
}
|
|
|
|
remainingBeforeData := int64(r.Len())
|
|
offsetAtData := r.Size() - remainingBeforeData
|
|
|
|
opcode := b
|
|
switch opcode {
|
|
case wasm.OpcodeI32Const:
|
|
_, _, err = leb128.DecodeInt32(r)
|
|
case wasm.OpcodeI64Const:
|
|
_, _, err = leb128.DecodeInt64(r)
|
|
case wasm.OpcodeF32Const:
|
|
_, err = ieee754.DecodeFloat32(r)
|
|
case wasm.OpcodeF64Const:
|
|
_, err = ieee754.DecodeFloat64(r)
|
|
case wasm.OpcodeGlobalGet:
|
|
_, _, err = leb128.DecodeUint32(r)
|
|
default:
|
|
return nil, fmt.Errorf("%v for const expression opt code: %#x", ErrInvalidByte, b)
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read value: %v", err)
|
|
}
|
|
|
|
if b, err = r.ReadByte(); err != nil {
|
|
return nil, fmt.Errorf("look for end opcode: %v", err)
|
|
}
|
|
|
|
if b != wasm.OpcodeEnd {
|
|
return nil, fmt.Errorf("constant expression has been not terminated")
|
|
}
|
|
|
|
data := make([]byte, remainingBeforeData-int64(r.Len()))
|
|
if _, err := r.ReadAt(data, offsetAtData); err != nil {
|
|
return nil, fmt.Errorf("error re-buffering ConstantExpression.Data")
|
|
}
|
|
|
|
return &wasm.ConstantExpression{Opcode: opcode, Data: data}, nil
|
|
}
|