summaryrefslogtreecommitdiff
path: root/vendor/github.com/tetratelabs/wazero/internal/wasm/host.go
blob: bca686d1df6855b831305e84fdb40c425ecdd6ff (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package wasm

import (
	"errors"
	"fmt"

	"github.com/tetratelabs/wazero/api"
	"github.com/tetratelabs/wazero/internal/wasmdebug"
)

type HostFuncExporter interface {
	ExportHostFunc(*HostFunc)
}

// HostFunc is a function with an inlined type, used for NewHostModule.
// Any corresponding FunctionType will be reused or added to the Module.
type HostFunc struct {
	// ExportName is the only value returned by api.FunctionDefinition.
	ExportName string

	// Name is equivalent to the same method on api.FunctionDefinition.
	Name string

	// ParamTypes is equivalent to the same method on api.FunctionDefinition.
	ParamTypes []ValueType

	// ParamNames is equivalent to the same method on api.FunctionDefinition.
	ParamNames []string

	// ResultTypes is equivalent to the same method on api.FunctionDefinition.
	ResultTypes []ValueType

	// ResultNames is equivalent to the same method on api.FunctionDefinition.
	ResultNames []string

	// Code is the equivalent function in the SectionIDCode.
	Code Code
}

// WithGoModuleFunc returns a copy of the function, replacing its Code.GoFunc.
func (f *HostFunc) WithGoModuleFunc(fn api.GoModuleFunc) *HostFunc {
	ret := *f
	ret.Code.GoFunc = fn
	return &ret
}

// NewHostModule is defined internally for use in WASI tests and to keep the code size in the root directory small.
func NewHostModule(
	moduleName string,
	exportNames []string,
	nameToHostFunc map[string]*HostFunc,
	enabledFeatures api.CoreFeatures,
) (m *Module, err error) {
	if moduleName != "" {
		m = &Module{NameSection: &NameSection{ModuleName: moduleName}}
	} else {
		return nil, errors.New("a module name must not be empty")
	}

	if exportCount := uint32(len(nameToHostFunc)); exportCount > 0 {
		m.ExportSection = make([]Export, 0, exportCount)
		m.Exports = make(map[string]*Export, exportCount)
		if err = addFuncs(m, exportNames, nameToHostFunc, enabledFeatures); err != nil {
			return
		}
	}

	m.IsHostModule = true
	// Uses the address of *wasm.Module as the module ID so that host functions can have each state per compilation.
	// Downside of this is that compilation cache on host functions (trampoline codes for Go functions and
	// Wasm codes for Wasm-implemented host functions) are not available and compiles each time. On the other hand,
	// compilation of host modules is not costly as it's merely small trampolines vs the real-world native Wasm binary.
	// TODO: refactor engines so that we can properly cache compiled machine codes for host modules.
	m.AssignModuleID([]byte(fmt.Sprintf("@@@@@@@@%p", m)), // @@@@@@@@ = any 8 bytes different from Wasm header.
		nil, false)
	return
}

func addFuncs(
	m *Module,
	exportNames []string,
	nameToHostFunc map[string]*HostFunc,
	enabledFeatures api.CoreFeatures,
) (err error) {
	if m.NameSection == nil {
		m.NameSection = &NameSection{}
	}
	moduleName := m.NameSection.ModuleName

	for _, k := range exportNames {
		hf := nameToHostFunc[k]
		if hf.Name == "" {
			hf.Name = k // default name to export name
		}
		switch hf.Code.GoFunc.(type) {
		case api.GoModuleFunction, api.GoFunction:
			continue // already parsed
		}

		// Resolve the code using reflection
		hf.ParamTypes, hf.ResultTypes, hf.Code, err = parseGoReflectFunc(hf.Code.GoFunc)
		if err != nil {
			return fmt.Errorf("func[%s.%s] %w", moduleName, k, err)
		}

		// Assign names to the function, if they exist.
		params := hf.ParamTypes
		if paramNames := hf.ParamNames; paramNames != nil {
			if paramNamesLen := len(paramNames); paramNamesLen != len(params) {
				return fmt.Errorf("func[%s.%s] has %d params, but %d params names", moduleName, k, paramNamesLen, len(params))
			}
		}

		results := hf.ResultTypes
		if resultNames := hf.ResultNames; resultNames != nil {
			if resultNamesLen := len(resultNames); resultNamesLen != len(results) {
				return fmt.Errorf("func[%s.%s] has %d results, but %d results names", moduleName, k, resultNamesLen, len(results))
			}
		}
	}

	funcCount := uint32(len(exportNames))
	m.NameSection.FunctionNames = make([]NameAssoc, 0, funcCount)
	m.FunctionSection = make([]Index, 0, funcCount)
	m.CodeSection = make([]Code, 0, funcCount)

	idx := Index(0)
	for _, name := range exportNames {
		hf := nameToHostFunc[name]
		debugName := wasmdebug.FuncName(moduleName, name, idx)
		typeIdx, typeErr := m.maybeAddType(hf.ParamTypes, hf.ResultTypes, enabledFeatures)
		if typeErr != nil {
			return fmt.Errorf("func[%s] %v", debugName, typeErr)
		}
		m.FunctionSection = append(m.FunctionSection, typeIdx)
		m.CodeSection = append(m.CodeSection, hf.Code)

		export := hf.ExportName
		m.ExportSection = append(m.ExportSection, Export{Type: ExternTypeFunc, Name: export, Index: idx})
		m.Exports[export] = &m.ExportSection[len(m.ExportSection)-1]
		m.NameSection.FunctionNames = append(m.NameSection.FunctionNames, NameAssoc{Index: idx, Name: hf.Name})

		if len(hf.ParamNames) > 0 {
			localNames := NameMapAssoc{Index: idx}
			for i, n := range hf.ParamNames {
				localNames.NameMap = append(localNames.NameMap, NameAssoc{Index: Index(i), Name: n})
			}
			m.NameSection.LocalNames = append(m.NameSection.LocalNames, localNames)
		}
		if len(hf.ResultNames) > 0 {
			resultNames := NameMapAssoc{Index: idx}
			for i, n := range hf.ResultNames {
				resultNames.NameMap = append(resultNames.NameMap, NameAssoc{Index: Index(i), Name: n})
			}
			m.NameSection.ResultNames = append(m.NameSection.ResultNames, resultNames)
		}
		idx++
	}
	return nil
}

func (m *Module) maybeAddType(params, results []ValueType, enabledFeatures api.CoreFeatures) (Index, error) {
	if len(results) > 1 {
		// Guard >1.0 feature multi-value
		if err := enabledFeatures.RequireEnabled(api.CoreFeatureMultiValue); err != nil {
			return 0, fmt.Errorf("multiple result types invalid as %v", err)
		}
	}
	for i := range m.TypeSection {
		t := &m.TypeSection[i]
		if t.EqualsSignature(params, results) {
			return Index(i), nil
		}
	}

	result := m.SectionElementCount(SectionIDType)
	m.TypeSection = append(m.TypeSection, FunctionType{Params: params, Results: results})
	return result, nil
}