-
Notifications
You must be signed in to change notification settings - Fork 2
/
section.go
794 lines (659 loc) · 17.6 KB
/
section.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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
// Copyright 2017 The go-interpreter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wasm
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"OntologyWithPOC/vm/wasmvm/wasm/internal/readpos"
"OntologyWithPOC/vm/wasmvm/wasm/leb128"
)
// SectionID is a 1-byte code that encodes the section code of both known and custom sections.
type SectionID uint8
const (
SectionIDCustom SectionID = 0
SectionIDType SectionID = 1
SectionIDImport SectionID = 2
SectionIDFunction SectionID = 3
SectionIDTable SectionID = 4
SectionIDMemory SectionID = 5
SectionIDGlobal SectionID = 6
SectionIDExport SectionID = 7
SectionIDStart SectionID = 8
SectionIDElement SectionID = 9
SectionIDCode SectionID = 10
SectionIDData SectionID = 11
)
func (s SectionID) String() string {
n, ok := map[SectionID]string{
SectionIDCustom: "custom",
SectionIDType: "type",
SectionIDImport: "import",
SectionIDFunction: "function",
SectionIDTable: "table",
SectionIDMemory: "memory",
SectionIDGlobal: "global",
SectionIDExport: "export",
SectionIDStart: "start",
SectionIDElement: "element",
SectionIDCode: "code",
SectionIDData: "data",
}[s]
if !ok {
return "unknown"
}
return n
}
// Section is a declared section in a WASM module.
type Section struct {
Start int64
End int64
ID SectionID
// Size of this section in bytes
PayloadLen uint32
// Section name, empty if id != 0
Name string
Bytes []byte
}
type InvalidSectionIDError SectionID
func (e InvalidSectionIDError) Error() string {
return fmt.Sprintf("wasm: invalid section ID %d", e)
}
type InvalidCodeIndexError int
func (e InvalidCodeIndexError) Error() string {
return fmt.Sprintf("wasm: invalid index to code section: %d", int(e))
}
var ErrUnsupportedSection = errors.New("wasm: unsupported section")
type MissingSectionError SectionID
func (e MissingSectionError) Error() string {
return fmt.Sprintf("wasm: missing section %s", SectionID(e).String())
}
// reads a valid section from r. The first return value is true if and only if
// the module has been completely read.
func (m *Module) readSection(r *readpos.ReadPos) (bool, error) {
var err error
var id uint32
logger.Println("Reading section ID")
if id, err = leb128.ReadVarUint32(r); err != nil {
if err == io.EOF { // no bytes were read, the reader is empty
return true, nil
}
return false, err
}
s := Section{ID: SectionID(id)}
logger.Println("Reading payload length")
if s.PayloadLen, err = leb128.ReadVarUint32(r); err != nil {
return false, nil
}
payloadDataLen := s.PayloadLen
if s.ID == SectionIDCustom {
nameLen, nameLenSize, err := leb128.ReadVarUint32Size(r)
if err != nil {
return false, err
}
payloadDataLen -= uint32(nameLenSize)
if s.Name, err = readString(r, int(nameLen)); err != nil {
return false, err
}
payloadDataLen -= uint32(len(s.Name))
}
logger.Printf("Section payload length: %d", payloadDataLen)
s.Start = r.CurPos
sectionBytes := new(bytes.Buffer)
sectionBytes.Grow(int(payloadDataLen))
sectionReader := io.LimitReader(io.TeeReader(r, sectionBytes), int64(payloadDataLen))
switch s.ID {
case SectionIDCustom:
logger.Println("section custom")
if err = m.readSectionCustom(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Other = append(m.Other, s)
}
case SectionIDType:
logger.Println("section type")
if err = m.readSectionTypes(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Types.Section = s
}
case SectionIDImport:
logger.Println("section import")
if err = m.readSectionImports(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Import.Section = s
}
case SectionIDFunction:
logger.Println("section function")
if err = m.readSectionFunctions(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Function.Section = s
}
case SectionIDTable:
logger.Println("section table")
if err = m.readSectionTables(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Table.Section = s
}
case SectionIDMemory:
logger.Println("section memory")
if err = m.readSectionMemories(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Memory.Section = s
}
case SectionIDGlobal:
logger.Println("section global")
if err = m.readSectionGlobals(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Global.Section = s
}
case SectionIDExport:
logger.Println("section export")
if err = m.readSectionExports(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Export.Section = s
}
case SectionIDStart:
logger.Println("section start")
if err = m.readSectionStart(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Start.Section = s
}
case SectionIDElement:
logger.Println("section element")
if err = m.readSectionElements(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Elements.Section = s
}
case SectionIDCode:
logger.Println("section code")
if err = m.readSectionCode(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Code.Section = s
}
case SectionIDData:
logger.Println("section data")
if err = m.readSectionData(sectionReader); err == nil {
s.End = r.CurPos
s.Bytes = sectionBytes.Bytes()
m.Data.Section = s
}
default:
return false, InvalidSectionIDError(s.ID)
}
logger.Println(err)
return false, err
}
func (m *Module) readSectionCustom(r io.Reader) error {
_, err := io.Copy(ioutil.Discard, r)
return err
}
// SectionTypes declares all function signatures that will be used in a module.
type SectionTypes struct {
Section
Entries []FunctionSig
}
func (m *Module) readSectionTypes(r io.Reader) error {
s := &SectionTypes{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Entries = make([]FunctionSig, int(count))
for i := range s.Entries {
if s.Entries[i], err = readFunction(r); err != nil {
return err
}
}
m.Types = s
return nil
}
// SectionImports declares all imports that will be used in the module.
type SectionImports struct {
Section
Entries []ImportEntry
}
func (m *Module) readSectionImports(r io.Reader) error {
s := &SectionImports{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Entries = make([]ImportEntry, count)
for i := range s.Entries {
s.Entries[i], err = readImportEntry(r)
if err != nil {
return err
}
}
m.Import = s
return nil
}
func readImportEntry(r io.Reader) (ImportEntry, error) {
i := ImportEntry{}
modLen, err := leb128.ReadVarUint32(r)
if err != nil {
return i, err
}
if i.ModuleName, err = readString(r, int(modLen)); err != nil {
return i, err
}
fieldLen, err := leb128.ReadVarUint32(r)
if err != nil {
return i, err
}
if i.FieldName, err = readString(r, int(fieldLen)); err != nil {
return i, err
}
if i.Kind, err = readExternal(r); err != nil {
return i, err
}
switch i.Kind {
case ExternalFunction:
logger.Println("importing function")
var t uint32
t, err = leb128.ReadVarUint32(r)
i.Type = FuncImport{t}
case ExternalTable:
logger.Println("importing table")
var table *Table
table, err = readTable(r)
if table != nil {
i.Type = TableImport{*table}
}
case ExternalMemory:
logger.Println("importing memory")
var mem *Memory
mem, err = readMemory(r)
if mem != nil {
i.Type = MemoryImport{*mem}
}
case ExternalGlobal:
logger.Println("importing global var")
var gl *GlobalVar
gl, err = readGlobalVar(r)
if gl != nil {
i.Type = GlobalVarImport{*gl}
}
default:
return i, InvalidExternalError(i.Kind)
}
return i, err
}
// SectionFunction declares the signature of all functions defined in the module (in the code section)
type SectionFunctions struct {
Section
// Sequences of indices into (FunctionSignatues).Entries
Types []uint32
}
func (m *Module) readSectionFunctions(r io.Reader) error {
s := &SectionFunctions{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Types = make([]uint32, count)
for i := range s.Types {
t, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Types[i] = t
}
m.Function = s
return nil
}
// SectionTables describes all tables declared by a module.
type SectionTables struct {
Section
Entries []Table
}
func (m *Module) readSectionTables(r io.Reader) error {
s := &SectionTables{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Entries = make([]Table, count)
for i := range s.Entries {
t, err := readTable(r)
if err != nil {
return err
}
s.Entries[i] = *t
}
m.Table = s
return err
}
// SectionMemories describes all linaer memories used by a module.
type SectionMemories struct {
Section
Entries []Memory
}
func (m *Module) readSectionMemories(r io.Reader) error {
s := &SectionMemories{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Entries = make([]Memory, count)
for i := range s.Entries {
m, err := readMemory(r)
if err != nil {
return err
}
s.Entries[i] = *m
}
m.Memory = s
return err
}
// SectionGlobals defines the value of all global variables declared in a module.
type SectionGlobals struct {
Section
Globals []GlobalEntry
}
func (m *Module) readSectionGlobals(r io.Reader) error {
s := &SectionGlobals{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Globals = make([]GlobalEntry, count)
logger.Printf("%d global entries\n", count)
for i := range s.Globals {
s.Globals[i], err = readGlobalEntry(r)
if err != nil {
return err
}
}
m.Global = s
return nil
}
// GlobalEntry declares a global variable.
type GlobalEntry struct {
Type *GlobalVar // Type holds information about the value type and mutability of the variable
Init []byte // Init is an initializer expression that computes the initial value of the variable
IsEnv bool // true if the Global is env import
InitVal uint64 // todo init value from env outside
}
func readGlobalEntry(r io.Reader) (e GlobalEntry, err error) {
logger.Println("reading global_type")
e.Type, err = readGlobalVar(r)
if err != nil {
logger.Println("Error!")
return
}
logger.Println("reading init expr")
// init_expr is delimited by opcode "end" (0x0b)
e.Init, err = readInitExpr(r)
logger.Println("Value:", e.Init)
return e, err
}
// SectionExports declares the export section of a module
type SectionExports struct {
Section
Entries map[string]ExportEntry
}
type DuplicateExportError string
func (e DuplicateExportError) Error() string {
return fmt.Sprintf("Duplicate export entry: %s", e)
}
func (m *Module) readSectionExports(r io.Reader) error {
s := &SectionExports{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Entries = make(map[string]ExportEntry, count)
for i := uint32(0); i < count; i++ {
entry, err := readExportEntry(r)
if err != nil {
return err
}
if _, exists := s.Entries[entry.FieldStr]; exists {
return DuplicateExportError(entry.FieldStr)
}
s.Entries[entry.FieldStr] = entry
}
m.Export = s
return nil
}
// ExportEntry represents an exported entry by the module
type ExportEntry struct {
FieldStr string
Kind External
Index uint32
}
func readExportEntry(r io.Reader) (ExportEntry, error) {
e := ExportEntry{}
fieldLen, err := leb128.ReadVarUint32(r)
if e.FieldStr, err = readString(r, int(fieldLen)); err != nil {
return e, err
}
if e.Kind, err = readExternal(r); err != nil {
return e, err
}
e.Index, err = leb128.ReadVarUint32(r)
return e, err
}
// SectionStartFunction represents the start function section.
type SectionStartFunction struct {
Section
Index uint32 // The index of the start function into the global index space.
}
func (m *Module) readSectionStart(r io.Reader) error {
s := &SectionStartFunction{}
var err error
s.Index, err = leb128.ReadVarUint32(r)
if err != nil {
return err
}
m.Start = s
return nil
}
// SectionElements describes the initial contents of a table's elements.
type SectionElements struct {
Section
Entries []ElementSegment
}
func (m *Module) readSectionElements(r io.Reader) error {
s := &SectionElements{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Entries = make([]ElementSegment, count)
for i := range s.Entries {
s.Entries[i], err = readElementSegment(r)
if err != nil {
return err
}
}
m.Elements = s
return nil
}
// ElementSegment describes a group of repeated elements that begin at a specified offset
type ElementSegment struct {
Index uint32 // The index into the global table space, should always be 0 in the MVP.
Offset []byte // initializer expression for computing the offset for placing elements, should return an i32 value
Elems []uint32
}
func readElementSegment(r io.Reader) (ElementSegment, error) {
s := ElementSegment{}
var err error
if s.Index, err = leb128.ReadVarUint32(r); err != nil {
return s, err
}
if s.Offset, err = readInitExpr(r); err != nil {
return s, err
}
numElems, err := leb128.ReadVarUint32(r)
if err != nil {
return s, err
}
s.Elems = make([]uint32, numElems)
for i := range s.Elems {
e, err := leb128.ReadVarUint32(r)
if err != nil {
return s, err
}
s.Elems[i] = e
}
return s, nil
}
// SectionCode describes the body for every function declared inside a module.
type SectionCode struct {
Section
Bodies []FunctionBody
}
func (m *Module) readSectionCode(r io.Reader) error {
s := &SectionCode{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Bodies = make([]FunctionBody, count)
logger.Printf("%d function bodies\n", count)
for i := range s.Bodies {
logger.Printf("Reading function %d\n", i)
if s.Bodies[i], err = readFunctionBody(r); err != nil {
return err
}
s.Bodies[i].Module = m
}
m.Code = s
if m.Function == nil || len(m.Function.Types) == 0 {
return MissingSectionError(SectionIDFunction)
}
if len(m.Function.Types) != len(s.Bodies) {
return errors.New("The number of entries in the function and code section are unequal")
}
if m.Types == nil {
return MissingSectionError(SectionIDType)
}
return nil
}
var ErrFunctionNoEnd = errors.New("Function body does not end with 0x0b (end)")
type FunctionBody struct {
Module *Module // The parent module containing this function body, for execution purposes
Locals []LocalEntry
Code []byte
}
func readFunctionBody(r io.Reader) (FunctionBody, error) {
f := FunctionBody{}
bodySize, err := leb128.ReadVarUint32(r)
if err != nil {
return f, err
}
body := make([]byte, bodySize)
if _, err = io.ReadFull(r, body); err != nil {
return f, err
}
bytesReader := bytes.NewBuffer(body)
localCount, err := leb128.ReadVarUint32(bytesReader)
if err != nil {
return f, err
}
f.Locals = make([]LocalEntry, localCount)
for i := range f.Locals {
if f.Locals[i], err = readLocalEntry(bytesReader); err != nil {
return f, err
}
}
logger.Printf("bodySize: %d, localCount: %d\n", bodySize, localCount)
code := bytesReader.Bytes()
logger.Printf("Read %d bytes for function body", len(code))
if code[len(code)-1] != end {
return f, ErrFunctionNoEnd
}
f.Code = code[:len(code)-1]
return f, nil
}
type LocalEntry struct {
Count uint32 // The total number of local variables of the given Type used in the function body
Type ValueType // The type of value stored by the variable
}
func readLocalEntry(r io.Reader) (LocalEntry, error) {
l := LocalEntry{}
var err error
l.Count, err = leb128.ReadVarUint32(r)
if err != nil {
return l, err
}
l.Type, err = readValueType(r)
if err != nil {
return l, err
}
return l, nil
}
// SectionData describes the initial values of a module's linear memory
type SectionData struct {
Section
Entries []DataSegment
}
func (m *Module) readSectionData(r io.Reader) error {
s := &SectionData{}
count, err := leb128.ReadVarUint32(r)
if err != nil {
return err
}
s.Entries = make([]DataSegment, count)
for i := range s.Entries {
if s.Entries[i], err = readDataSegment(r); err != nil {
return err
}
}
m.Data = s
return err
}
// DataSegment describes a group of repeated elements that begin at a specified offset in the linear memory
type DataSegment struct {
Index uint32 // The index into the global linear memory space, should always be 0 in the MVP.
Offset []byte // initializer expression for computing the offset for placing elements, should return an i32 value
Data []byte
}
func readDataSegment(r io.Reader) (DataSegment, error) {
s := DataSegment{}
var err error
if s.Index, err = leb128.ReadVarUint32(r); err != nil {
return s, err
}
if s.Offset, err = readInitExpr(r); err != nil {
return s, err
}
size, err := leb128.ReadVarUint32(r)
if err != nil {
return s, err
}
s.Data, err = readBytes(r, int(size))
return s, err
}