-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_section.go
More file actions
executable file
·76 lines (57 loc) · 2.16 KB
/
code_section.go
File metadata and controls
executable file
·76 lines (57 loc) · 2.16 KB
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
package wax
import "bytes"
/*
Code Section
http://webassembly.github.io/spec/core/binary/modules.html#code-section
The code section has the id 10.
It decodes into a vector of code entries that are pairs of value type vectors and expressions.
They represent the locals and body field of the functions in the funcs component of a module.
The type fields of the respective functions are encoded separately in the function section.
The encoding of each code entry consists of
- the u32 size of the function code in bytes,
- the actual function code, which in turn consists of
- the declaration of locals,
- the function body as an expression.
Local declarations are compressed into a vector whose entries consist of
- a u32 count,
- a value type,
denoting count locals of the same value type.
codesec ::= code*: section10(vec(code)) => code*
code ::= size:u32 code:func => code (if size = ||func||)
func ::= (t*)*:vec(locals) e:expr => concat((t*)*),e* (if |concat((t*)*)| < 2^32)
locals ::= n:u32 t:valtype => tn
Here, code ranges over pairs (valtype*,expr).
The meta function concat((t*)*) concatenates all sequences ti* in (t*)*.
Any code for which the length of the resulting sequence is out of bounds of the maximum size of a vector is malformed.
Note
Like with sections, the code size is not needed for decoding, but can be used to skip functions when navigating through a binary. The module is malformed if a size does not match the length of the respective function code.
*/
type CodeSection struct {
SectionBase
Code []Code
}
func ParseCodeSection(ber *BinaryEncodingReader, id SectionID) (*CodeSection, error) {
sb, err := ParseSectionBase(ber, id)
if err != nil {
return nil, err
}
cr := NewBinaryEncodingReader(bytes.NewReader(sb.Content))
// Read count of vector
count64, _, err := cr.ReadVaruint()
if err != nil {
return nil, err
}
count := uint32(count64)
codes := make([]Code, 0, count)
for i := uint32(0); i < count; i++ {
c, err := ParseCode(cr)
if err != nil {
return nil, err
}
codes = append(codes, *c)
}
return &CodeSection{
SectionBase: *sb,
Code: codes,
}, nil
}