summaryrefslogtreecommitdiff
path: root/vendor/github.com/tetratelabs/wazero/internal/wasm/binary/const_expr.go
blob: edfc0a086bf5534b8d0b561691faa1e189c8bbcd (plain)
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
package binary

import (
	"bytes"
	"fmt"
	"io"

	"github.com/tetratelabs/wazero/api"
	"github.com/tetratelabs/wazero/internal/ieee754"
	"github.com/tetratelabs/wazero/internal/leb128"
	"github.com/tetratelabs/wazero/internal/wasm"
)

func decodeConstantExpression(r *bytes.Reader, enabledFeatures api.CoreFeatures, ret *wasm.ConstantExpression) error {
	b, err := r.ReadByte()
	if err != nil {
		return 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:
		buf := make([]byte, 4)
		if _, err := io.ReadFull(r, buf); err != nil {
			return fmt.Errorf("read f32 constant: %v", err)
		}
		_, err = ieee754.DecodeFloat32(buf)
	case wasm.OpcodeF64Const:
		buf := make([]byte, 8)
		if _, err := io.ReadFull(r, buf); err != nil {
			return fmt.Errorf("read f64 constant: %v", err)
		}
		_, err = ieee754.DecodeFloat64(buf)
	case wasm.OpcodeGlobalGet:
		_, _, err = leb128.DecodeUint32(r)
	case wasm.OpcodeRefNull:
		if err := enabledFeatures.RequireEnabled(api.CoreFeatureBulkMemoryOperations); err != nil {
			return fmt.Errorf("ref.null is not supported as %w", err)
		}
		reftype, err := r.ReadByte()
		if err != nil {
			return fmt.Errorf("read reference type for ref.null: %w", err)
		} else if reftype != wasm.RefTypeFuncref && reftype != wasm.RefTypeExternref {
			return fmt.Errorf("invalid type for ref.null: 0x%x", reftype)
		}
	case wasm.OpcodeRefFunc:
		if err := enabledFeatures.RequireEnabled(api.CoreFeatureBulkMemoryOperations); err != nil {
			return fmt.Errorf("ref.func is not supported as %w", err)
		}
		// Parsing index.
		_, _, err = leb128.DecodeUint32(r)
	case wasm.OpcodeVecPrefix:
		if err := enabledFeatures.RequireEnabled(api.CoreFeatureSIMD); err != nil {
			return fmt.Errorf("vector instructions are not supported as %w", err)
		}
		opcode, err = r.ReadByte()
		if err != nil {
			return fmt.Errorf("read vector instruction opcode suffix: %w", err)
		}

		if opcode != wasm.OpcodeVecV128Const {
			return fmt.Errorf("invalid vector opcode for const expression: %#x", opcode)
		}

		remainingBeforeData = int64(r.Len())
		offsetAtData = r.Size() - remainingBeforeData

		n, err := r.Read(make([]byte, 16))
		if err != nil {
			return fmt.Errorf("read vector const instruction immediates: %w", err)
		} else if n != 16 {
			return fmt.Errorf("read vector const instruction immediates: needs 16 bytes but was %d bytes", n)
		}
	default:
		return fmt.Errorf("%v for const expression opt code: %#x", ErrInvalidByte, b)
	}

	if err != nil {
		return fmt.Errorf("read value: %v", err)
	}

	if b, err = r.ReadByte(); err != nil {
		return fmt.Errorf("look for end opcode: %v", err)
	}

	if b != wasm.OpcodeEnd {
		return fmt.Errorf("constant expression has been not terminated")
	}

	ret.Data = make([]byte, remainingBeforeData-int64(r.Len())-1)
	if _, err = r.ReadAt(ret.Data, offsetAtData); err != nil {
		return fmt.Errorf("error re-buffering ConstantExpression.Data")
	}
	ret.Opcode = opcode
	return nil
}