-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathparser.go
674 lines (552 loc) · 15.2 KB
/
parser.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//go:generate stringer -trimprefix sec -type sectionID
package wasm
import (
"fmt"
"io"
"io/ioutil"
)
// magicnumber is a magic number which must appear as the very first bytes of a
// wasm file.
const magicnumber = 0x6d736100 // \0asm
// opEnd is the op code for a section end
const opEnd = 0x0b
type sectionID uint8
const (
secCustom sectionID = iota // 0x00
secType // 0x01
secImport // 0x02
secFunction // 0x03
secTable // 0x04
secMemory // 0x05
secGlobal // 0x06
secExport // 0x07
secStart // 0x08
secElement // 0x09
secCode // 0x0A
secData // 0x0B
)
type parser struct {
r *reader
}
var errDone = fmt.Errorf("done")
// Parse parses the input to a WASM module.
func Parse(r io.Reader) (*Module, error) {
p := &parser{
r: newReader(r),
}
if err := p.parsePreamble(); err != nil {
return nil, err
}
// Parse file sections
var m Module
for {
err := p.parseSection(&m.Sections)
if err != nil {
if err == errDone {
break
}
return nil, fmt.Errorf("[0x%06x] parse section: %v", p.r.Index(), err)
}
}
return &m, nil
}
func (p *parser) parsePreamble() error {
var h, v uint32
if err := read(p.r, &h); err != nil {
return fmt.Errorf("could not read file header")
}
if h != magicnumber {
return fmt.Errorf("not a wasm file")
}
if err := read(p.r, &v); err != nil {
return fmt.Errorf("could not version")
}
if v != 1 {
return fmt.Errorf("unsupported version %d", v)
}
return nil
}
func (p *parser) parseSection(ss *[]Section) error {
var i uint8
if err := readVarUint7(p.r, &i); err != nil {
if err == io.EOF {
return errDone
}
return fmt.Errorf("read section id: %v", err)
}
sid := sectionID(i)
var s Section
var err error
base := §ion{
id: sid,
name: sid.String(),
}
if err := readVarUint32(p.r, &base.size); err != nil {
return fmt.Errorf("read type section payload length: %v", err)
}
switch sid {
case secCustom:
s, err = p.parseCustomSection(base)
case secType:
s, err = p.parseTypeSection(base)
case secImport:
s, err = p.parseImportSection(base)
case secFunction:
s, err = p.parseFunctionSection(base)
case secTable:
s, err = p.parseTableSection(base)
case secMemory:
s, err = p.parseMemorySection(base)
case secGlobal:
s, err = p.parseGlobalSection(base)
case secExport:
s, err = p.parseExportSection(base)
case secStart:
s, err = p.parseStartSection(base)
case secElement:
s, err = p.parseElementSection(base)
case secCode:
s, err = p.parseCodeSection(base)
case secData:
s, err = p.parseDataSection(base)
default:
if _, err := io.CopyN(ioutil.Discard, p.r, int64(base.size)); err != nil {
return fmt.Errorf("discard section payload, %d bytes: %v", base.size, err)
}
if sid > secData {
// This happens if the previous section was not read to the end,
// indicating a bug in that section parser.
return fmt.Errorf("data corrupted; section id 0x%02x not valid", sid)
}
// Skip unknown section
return nil
}
if err != nil {
return err
}
if s != nil {
*ss = append(*ss, s)
}
return nil
}
func (p *parser) parseCustomSection(base *section) (Section, error) {
var nl uint32
if err := readVarUint32(p.r, &nl); err != nil {
return nil, fmt.Errorf("read section name length: %v", err)
}
b := make([]byte, nl)
if err := read(p.r, &b); err != nil {
return nil, fmt.Errorf("read section name: %v", err)
}
name := string(b)
base.size -= uint32(nl) // sizeof name
base.size -= uint32(varUint32Size(nl)) // sizeof name_len
if name == "name" {
// A name section is a special custom section meant for debugging
// purposes. It's defined in the spec so we'll parse it.
return p.parseNameSection(base, name, base.size)
}
s := SectionCustom{
section: base,
SectionName: name,
}
// set raw bytes
s.Payload = make([]byte, base.size)
if err := read(p.r, s.Payload); err != nil {
return nil, fmt.Errorf("read custom section payload: %v", err)
}
return &s, nil
}
func (p *parser) parseTypeSection(base *section) (*SectionType, error) {
s := SectionType{section: base}
err := p.loopCount(func() error {
var e FuncType
if err := readVarInt7(p.r, &e.Form); err != nil {
return fmt.Errorf("read form: %v", err)
}
p.loopCount(func() error {
var param int8
if err := readVarInt7(p.r, ¶m); err != nil {
return fmt.Errorf("read function param type: %v", err)
}
e.Params = append(e.Params, param)
return nil
})
var rc uint8
if err := readVarUint1(p.r, &rc); err != nil {
return fmt.Errorf("read number of returns from function: %v", err)
}
e.ReturnTypes = make([]int8, rc)
for i := range e.ReturnTypes {
if err := readVarInt7(p.r, &e.ReturnTypes[i]); err != nil {
return fmt.Errorf("read function return type: %v", err)
}
}
s.Entries = append(s.Entries, e)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
func (p *parser) parseImportSection(base *section) (*SectionImport, error) {
s := SectionImport{section: base}
err := p.loopCount(func() error {
var e ImportEntry
var ml uint32
if err := readVarUint32(p.r, &ml); err != nil {
return fmt.Errorf("read module length: %v", err)
}
mn := make([]byte, ml)
if err := read(p.r, mn); err != nil {
return fmt.Errorf("read module name: %v", err)
}
e.Module = string(mn)
var fl uint32
if err := readVarUint32(p.r, &fl); err != nil {
return fmt.Errorf("read field length: %v", err)
}
fn := make([]byte, fl)
if err := read(p.r, fn); err != nil {
return fmt.Errorf("read field name")
}
e.Field = string(fn)
var kind uint8
if err := read(p.r, &kind); err != nil {
return fmt.Errorf("read kind: %v", err)
}
e.Kind = ExternalKind(kind)
switch e.Kind {
case ExtKindFunction:
e.FunctionType = &FunctionType{}
if err := readVarUint32(p.r, &e.FunctionType.Index); err != nil {
return fmt.Errorf("read function type index: %v", err)
}
case ExtKindTable:
e.TableType = &TableType{}
if err := readVarInt7(p.r, &e.TableType.ElemType); err != nil {
return fmt.Errorf("read table element type: %v", err)
}
if err := p.parseResizableLimits(&e.TableType.Limits); err != nil {
return fmt.Errorf("read table resizable limits: %v", err)
}
case ExtKindMemory:
e.MemoryType = &MemoryType{}
if err := p.parseResizableLimits(&e.MemoryType.Limits); err != nil {
return fmt.Errorf("read memory resizable limits: %v", err)
}
case ExtKindGlobal:
e.GlobalType = &GlobalType{}
if err := readVarInt7(p.r, &e.GlobalType.ContentType); err != nil {
return fmt.Errorf("read global content type: %v", err)
}
var m uint8
if err := readVarUint1(p.r, &m); err != nil {
return fmt.Errorf("read global mutability: %v", err)
}
e.GlobalType.Mutable = m == 1
}
s.Entries = append(s.Entries, e)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
func (p *parser) parseFunctionSection(base *section) (*SectionFunction, error) {
s := SectionFunction{section: base}
err := p.loopCount(func() error {
var t uint32
if err := readVarUint32(p.r, &t); err != nil {
return fmt.Errorf("read function type: %v", err)
}
s.Types = append(s.Types, t)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
func (p *parser) parseTableSection(base *section) (*SectionTable, error) {
s := SectionTable{section: base}
err := p.loopCount(func() error {
var e MemoryType
if err := p.parseResizableLimits(&e.Limits); err != nil {
return fmt.Errorf("read memory resizable limits: %v", err)
}
s.Entries = append(s.Entries, e)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
func (p *parser) parseMemorySection(base *section) (*SectionMemory, error) {
s := SectionMemory{section: base}
err := p.loopCount(func() error {
var e MemoryType
if err := p.parseResizableLimits(&e.Limits); err != nil {
return fmt.Errorf("read memory resizable limits: %v", err)
}
s.Entries = append(s.Entries, e)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
func (p *parser) parseGlobalSection(base *section) (*SectionGlobal, error) {
s := SectionGlobal{section: base}
err := p.loopCount(func() error {
var e GlobalVariable
if err := readVarInt7(p.r, &e.Type.ContentType); err != nil {
return fmt.Errorf("read global content type: %v", err)
}
if err := read(p.r, &e.Type.Mutable); err != nil {
return fmt.Errorf("read global mutability: %v", err)
}
if err := readUntil(p.r, opEnd, &e.Init); err != nil {
return fmt.Errorf("read global init expression: %v", err)
}
s.Globals = append(s.Globals, e)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
func (p *parser) parseExportSection(base *section) (*SectionExport, error) {
s := SectionExport{section: base}
err := p.loopCount(func() error {
var e ExportEntry
var fl uint32
if err := readVarUint32(p.r, &fl); err != nil {
return fmt.Errorf("read field length: %v", err)
}
f := make([]byte, fl)
if err := read(p.r, f); err != nil {
return fmt.Errorf("read field")
}
e.Field = string(f)
var kind uint8
if err := readVarUint7(p.r, &kind); err != nil {
return fmt.Errorf("read kind: %v", err)
}
e.Kind = ExternalKind(kind)
if err := readVarUint32(p.r, &e.Index); err != nil {
return fmt.Errorf("read index: %v", err)
}
s.Entries = append(s.Entries, e)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
func (p *parser) parseStartSection(base *section) (*SectionStart, error) {
s := SectionStart{section: base}
if err := readVarUint32(p.r, &s.Index); err != nil {
return nil, fmt.Errorf("read start index: %v", err)
}
return &s, nil
}
func (p *parser) parseElementSection(base *section) (*SectionElement, error) {
s := SectionElement{section: base}
err := p.loopCount(func() error {
var e ElemSegment
if err := readVarUint32(p.r, &e.Index); err != nil {
return fmt.Errorf("read element index: %v", err)
}
if err := readUntil(p.r, opEnd, &e.Offset); err != nil {
return fmt.Errorf("read offset expression: %v", err)
}
var numElem uint32
if err := readVarUint32(p.r, &numElem); err != nil {
return fmt.Errorf("read number of elements: %v", err)
}
e.Elems = make([]uint32, int(numElem))
for i := range e.Elems {
if err := readVarUint32(p.r, &e.Elems[i]); err != nil {
return fmt.Errorf("read element function index %d: %v", i, err)
}
}
s.Entries = append(s.Entries, e)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
func (p *parser) parseCodeSection(base *section) (*SectionCode, error) {
s := SectionCode{section: base}
err := p.loopCount(func() error {
var e FunctionBody
var bs uint32
if err := readVarUint32(p.r, &bs); err != nil {
return fmt.Errorf("read body size: %v", err)
}
end := p.r.Index() + int(bs)
p.loopCount(func() error {
var l LocalEntry
if err := readVarUint32(p.r, &l.Count); err != nil {
return fmt.Errorf("read local entry count: %v", err)
}
if err := read(p.r, &l.Type); err != nil {
return fmt.Errorf("read local entry value type: %v", err)
}
e.Locals = append(e.Locals, l)
return nil
})
numBytes := end - p.r.Index()
e.Code = make([]byte, numBytes)
if err := read(p.r, e.Code); err != nil {
return fmt.Errorf("read function bytecode: %v", err)
}
s.Bodies = append(s.Bodies, e)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
func (p *parser) parseDataSection(base *section) (*SectionData, error) {
s := SectionData{section: base}
err := p.loopCount(func() error {
var e DataSegment
if err := readVarUint32(p.r, &e.Index); err != nil {
return fmt.Errorf("read data segment index: %v", err)
}
if err := readUntil(p.r, opEnd, &e.Offset); err != nil {
return fmt.Errorf("read data section offset initializer: %v", err)
}
var size uint32
if err := readVarUint32(p.r, &size); err != nil {
return fmt.Errorf("read data section size: %v", err)
}
e.Data = make([]byte, size)
if err := read(p.r, e.Data); err != nil {
return fmt.Errorf("read data section data: %v", err)
}
s.Entries = append(s.Entries, e)
return nil
})
if err != nil {
return nil, err
}
return &s, nil
}
// name types are used to identify the type in a Name section.
const (
nameTypeModule uint8 = iota // 0x00
nameTypeFunction // 0x01
nameTypeLocal // 0x02
)
func (p *parser) parseNameSection(base *section, name string, n uint32) (*SectionName, error) {
s := SectionName{
section: base,
SectionName: name,
}
var t uint8
if err := read(p.r, &t); err != nil {
return nil, fmt.Errorf("read name type: %v", err)
}
var pl uint32
if err := readVarUint32(p.r, &pl); err != nil {
return nil, fmt.Errorf("read payload length: %v", err)
}
switch t {
case nameTypeModule:
var l uint32
if err := readVarUint32(p.r, &l); err != nil {
return nil, fmt.Errorf("read module name length: %v", err)
}
name := make([]byte, l)
if err := read(p.r, name); err != nil {
return nil, fmt.Errorf("read module name: %v", err)
}
s.Module = string(name)
case nameTypeFunction:
s.Functions = &NameMap{}
if err := p.parseNameMap(s.Functions); err != nil {
return nil, fmt.Errorf("read function name map: %v", err)
}
case nameTypeLocal:
s.Locals = &Locals{}
p.loopCount(func() error {
var l LocalName
if err := readVarUint32(p.r, &l.Index); err != nil {
return fmt.Errorf("read local func index: %v", err)
}
if err := p.parseNameMap(&l.LocalMap); err != nil {
return fmt.Errorf("read local name map: %v", err)
}
s.Locals.Funcs = append(s.Locals.Funcs, l)
return nil
})
default:
return nil, fmt.Errorf("unknown name type 0x%02x", t)
}
return &s, nil
}
func (p *parser) parseResizableLimits(l *ResizableLimits) error {
var hasMax uint8
if err := readVarUint1(p.r, &hasMax); err != nil {
return fmt.Errorf("flags: %v", err)
}
if err := readVarUint32(p.r, &l.Initial); err != nil {
return fmt.Errorf("initial: %v", err)
}
if hasMax == 0 {
return nil
}
if err := readVarUint32(p.r, &l.Maximum); err != nil {
return fmt.Errorf("maximum: %v", err)
}
return nil
}
// loopCount reads a varuint32 count and and calls the f n times. All sections
// except custom start with this pattern.
//
// If f returns an error, further processing is not done and the error is
// returned to the caller.
func (p *parser) loopCount(f func() error) error {
var n uint32
if err := readVarUint32(p.r, &n); err != nil {
return fmt.Errorf("read section count: %v", err)
}
for i := uint32(0); i < n; i++ {
if err := f(); err != nil {
return fmt.Errorf("entry %d: %v", i, err)
}
}
return nil
}
func (p *parser) parseNameMap(v *NameMap) error {
p.loopCount(func() error {
var n Naming
if err := readVarUint32(p.r, &n.Index); err != nil {
return fmt.Errorf("read naming index: %v", err)
}
var l uint32
if err := readVarUint32(p.r, &l); err != nil {
return fmt.Errorf("read naming length: %v", err)
}
name := make([]byte, l)
if err := read(p.r, name); err != nil {
return fmt.Errorf("read name: %v", err)
}
n.Name = string(name)
v.Names = append(v.Names, n)
return nil
})
return nil
}