-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
module.go
340 lines (281 loc) · 7.22 KB
/
module.go
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package module
import (
"fmt"
"strings"
"github.com/open-policy-agent/opa/internal/wasm/instruction"
"github.com/open-policy-agent/opa/internal/wasm/types"
)
type (
// Module represents a WASM module.
Module struct {
Version uint32
Type TypeSection
Import ImportSection
Function FunctionSection
Table TableSection
Element ElementSection
Global GlobalSection
Export ExportSection
Code RawCodeSection
Data DataSection
}
// TypeSection represents a WASM type section.
TypeSection struct {
Functions []FunctionType
}
// ImportSection represents a WASM import section.
ImportSection struct {
Imports []Import
}
// FunctionSection represents a WASM function section.
FunctionSection struct {
TypeIndices []uint32
}
// TableSection represents a WASM table section.
TableSection struct {
Tables []Table
}
// ElementSection represents a WASM element section.
ElementSection struct {
Segments []ElementSegment
}
// GlobalSection represents a WASM global section.
GlobalSection struct {
Globals []Global
}
// ExportSection represents a WASM export section.
ExportSection struct {
Exports []Export
}
// RawCodeSection represents a WASM code section. The code section is left as a
// raw byte sequence. See CodeSection for the decoded version.
RawCodeSection struct {
Segments []RawCodeSegment
}
// DataSection represents a WASM data section.
DataSection struct {
Segments []DataSegment
}
// FunctionType represents a WASM function type definition.
FunctionType struct {
Params []types.ValueType
Results []types.ValueType
}
// Import represents a WASM import statement.
Import struct {
Module string
Name string
Descriptor ImportDescriptor
}
// ImportDescriptor represents a WASM import descriptor.
ImportDescriptor interface {
fmt.Stringer
Kind() ImportDescriptorType
}
// ImportDescriptorType defines allowed kinds of import descriptors.
ImportDescriptorType int
// FunctionImport represents a WASM function import statement.
FunctionImport struct {
Func uint32
}
// MemoryImport represents a WASM memory import statement.
MemoryImport struct {
Mem MemType
}
// MemType defines the attributes of a memory import.
MemType struct {
Lim Limit
}
// TableImport represents a WASM table import statement.
TableImport struct {
Type types.ElementType
Lim Limit
}
// ElementSegment represents a WASM element segment.
ElementSegment struct {
Index uint32
Offset Expr
Indices []uint32
}
// GlobalImport represents a WASM global variable import statement.
GlobalImport struct {
Type types.ValueType
Mutable bool
}
// Limit represents a WASM limit.
Limit struct {
Min uint32
Max *uint32
}
// Table represents a WASM table statement.
Table struct {
Type types.ElementType
Lim Limit
}
// Global represents a WASM global statement.
Global struct {
Type types.ValueType
Mutable bool
Init Expr
}
// Export represents a WASM export statement.
Export struct {
Name string
Descriptor ExportDescriptor
}
// ExportDescriptor represents a WASM export descriptor.
ExportDescriptor struct {
Type ExportDescriptorType
Index uint32
}
// ExportDescriptorType defines the allowed kinds of export descriptors.
ExportDescriptorType int
// RawCodeSegment represents a binary-encoded WASM code segment.
RawCodeSegment struct {
Code []byte
}
// DataSegment represents a WASM data segment.
DataSegment struct {
Index uint32
Offset Expr
Init []byte
}
// Expr represents a WASM expression.
Expr struct {
Instrs []instruction.Instruction
}
// CodeEntry represents a code segment entry.
CodeEntry struct {
Func Function
}
// Function represents a function in a code segment.
Function struct {
Locals []LocalDeclaration
Expr Expr
}
// LocalDeclaration represents a local variable declaration.
LocalDeclaration struct {
Count uint32
Type types.ValueType
}
)
// Defines the allowed kinds of imports.
const (
FunctionImportType ImportDescriptorType = iota
TableImportType
MemoryImportType
GlobalImportType
)
func (x ImportDescriptorType) String() string {
switch x {
case FunctionImportType:
return "func"
case TableImportType:
return "table"
case MemoryImportType:
return "memory"
case GlobalImportType:
return "global"
}
panic("illegal value")
}
// Defines the allowed kinds of exports.
const (
FunctionExportType ExportDescriptorType = iota
TableExportType
MemoryExportType
GlobalExportType
)
func (x ExportDescriptorType) String() string {
switch x {
case FunctionExportType:
return "func"
case TableExportType:
return "table"
case MemoryExportType:
return "memory"
case GlobalExportType:
return "global"
}
panic("illegal value")
}
// Kind returns the function import type kind.
func (i FunctionImport) Kind() ImportDescriptorType {
return FunctionImportType
}
func (i FunctionImport) String() string {
return fmt.Sprintf("%v[type=%v]", i.Kind(), i.Func)
}
// Kind returns the memory import type kind.
func (i MemoryImport) Kind() ImportDescriptorType {
return MemoryImportType
}
func (i MemoryImport) String() string {
return fmt.Sprintf("%v[%v]", i.Kind(), i.Mem.Lim)
}
// Kind returns the table import type kind.
func (i TableImport) Kind() ImportDescriptorType {
return TableImportType
}
func (i TableImport) String() string {
return fmt.Sprintf("%v[%v, %v]", i.Kind(), i.Type, i.Lim)
}
// Kind returns the global import type kind.
func (i GlobalImport) Kind() ImportDescriptorType {
return GlobalImportType
}
func (i GlobalImport) String() string {
return fmt.Sprintf("%v[%v, mut=%v]", i.Kind(), i.Type, i.Mutable)
}
func (tpe FunctionType) String() string {
params := make([]string, len(tpe.Params))
results := make([]string, len(tpe.Results))
for i := range tpe.Params {
params[i] = tpe.Params[i].String()
}
for i := range tpe.Results {
results[i] = tpe.Results[i].String()
}
return "(" + strings.Join(params, ", ") + ") -> (" + strings.Join(results, ", ") + ")"
}
// Equal returns true if tpe equals other.
func (tpe FunctionType) Equal(other FunctionType) bool {
if len(tpe.Params) != len(other.Params) || len(tpe.Results) != len(other.Results) {
return false
}
for i := range tpe.Params {
if tpe.Params[i] != other.Params[i] {
return false
}
}
for i := range tpe.Results {
if tpe.Results[i] != other.Results[i] {
return false
}
}
return true
}
func (imp Import) String() string {
return fmt.Sprintf("%v %v.%v", imp.Descriptor.String(), imp.Module, imp.Name)
}
func (exp Export) String() string {
return fmt.Sprintf("%v[%v] %v", exp.Descriptor.Type, exp.Descriptor.Index, exp.Name)
}
func (seg RawCodeSegment) String() string {
return fmt.Sprintf("<code %d bytes>", len(seg.Code))
}
func (seg DataSegment) String() string {
return fmt.Sprintf("<data index=%v [%v] len=%d bytes>", seg.Index, seg.Offset, len(seg.Init))
}
func (e Expr) String() string {
return fmt.Sprintf("%d instr(s)", len(e.Instrs))
}
func (lim Limit) String() string {
if lim.Max == nil {
return fmt.Sprintf("min=%v", lim.Min)
}
return fmt.Sprintf("min=%v max=%v", lim.Min, lim.Max)
}