Files
wazero/internal/wasm/binary/const_expr.go
Crypt Keeper fb0b311844 Consistently uses LEB128 signed encoding for global constants (#443)
Global constants can be defined in wasm or in ModuleBuilder. In either
case, they end up being decoded and interpreted during instantiation.
This chooses signed encoding to avoid surprises. A more comprehensive
explanation was added to RATIONALE.md, but the motivation was a global
100 coming out negative.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-06 09:50:47 +08:00

58 lines
1.6 KiB
Go

package binary
import (
"bytes"
"fmt"
"github.com/tetratelabs/wazero/internal/ieee754"
"github.com/tetratelabs/wazero/internal/leb128"
"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:
// Treat constants as signed as their interpretation is not yet known per /RATIONALE.md
_, _, err = leb128.DecodeInt32(r)
case wasm.OpcodeI64Const:
// Treat constants as signed as their interpretation is not yet known per /RATIONALE.md
_, _, 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
}