55package wasm
66
77import (
8+ "errors"
89 "fmt"
910 "io"
1011
@@ -22,13 +23,13 @@ type Unmarshaler interface {
2223}
2324
2425// ValueType represents the type of a valid value in Wasm
25- type ValueType int8
26+ type ValueType uint8
2627
2728const (
28- ValueTypeI32 ValueType = - 0x01
29- ValueTypeI64 ValueType = - 0x02
30- ValueTypeF32 ValueType = - 0x03
31- ValueTypeF64 ValueType = - 0x04
29+ ValueTypeI32 ValueType = 0x7f
30+ ValueTypeI64 ValueType = 0x7e
31+ ValueTypeF32 ValueType = 0x7d
32+ ValueTypeF64 ValueType = 0x7c
3233)
3334
3435var valueTypeStrMap = map [ValueType ]string {
@@ -47,10 +48,10 @@ func (t ValueType) String() string {
4748}
4849
4950// TypeFunc represents the value type of a function
50- const TypeFunc int = - 0x20
51+ const TypeFunc uint8 = 0x60
5152
5253func (t * ValueType ) UnmarshalWASM (r io.Reader ) error {
53- v , err := leb128 . ReadVarint32 (r )
54+ v , err := ReadByte (r )
5455 if err != nil {
5556 return err
5657 }
@@ -59,13 +60,13 @@ func (t *ValueType) UnmarshalWASM(r io.Reader) error {
5960}
6061
6162func (t ValueType ) MarshalWASM (w io.Writer ) error {
62- _ , err := leb128 . WriteVarint64 (w , int64 (t ))
63+ err := writeByte (w , byte (t ))
6364 return err
6465}
6566
6667// BlockType represents the signature of a structured block
6768type BlockType ValueType // varint7
68- const BlockTypeEmpty BlockType = - 0x40
69+ const BlockTypeEmpty BlockType = 0x40
6970
7071func (b BlockType ) String () string {
7172 if b == BlockTypeEmpty {
@@ -75,22 +76,24 @@ func (b BlockType) String() string {
7576}
7677
7778// ElemType describes the type of a table's elements
78- type ElemType int // varint7
79+ type ElemType uint8 // varint7
7980// ElemTypeAnyFunc descibres an any_func value
80- const ElemTypeAnyFunc ElemType = - 0x10
81+ const ElemTypeAnyFunc ElemType = 0x70
8182
8283func (t * ElemType ) UnmarshalWASM (r io.Reader ) error {
83- b , err := leb128 . ReadVarint32 (r )
84+ b , err := ReadByte (r )
8485 if err != nil {
8586 return err
8687 }
88+ if b != uint8 (ElemTypeAnyFunc ) {
89+ return fmt .Errorf ("wasm: unsupported elem type:%d" , b )
90+ }
8791 * t = ElemType (b )
8892 return nil
8993}
9094
9195func (t ElemType ) MarshalWASM (w io.Writer ) error {
92- _ , err := leb128 .WriteVarint64 (w , int64 (t ))
93- return err
96+ return writeByte (w , byte (t ))
9497}
9598
9699func (t ElemType ) String () string {
@@ -104,7 +107,7 @@ func (t ElemType) String() string {
104107// FunctionSig describes the signature of a declared function in a WASM module
105108type FunctionSig struct {
106109 // value for the 'func` type constructor
107- Form int8
110+ Form uint8 // must be 0x60
108111 // The parameter types of the function
109112 ParamTypes []ValueType
110113 ReturnTypes []ValueType
@@ -124,11 +127,14 @@ func (e InvalidTypeConstructorError) Error() string {
124127}
125128
126129func (f * FunctionSig ) UnmarshalWASM (r io.Reader ) error {
127- form , err := leb128 . ReadVarint32 (r )
130+ form , err := ReadByte (r )
128131 if err != nil {
129132 return err
130133 }
131- f .Form = int8 (form )
134+ if form != TypeFunc {
135+ return fmt .Errorf ("wasm: unknown function form: %x" , form )
136+ }
137+ f .Form = uint8 (form )
132138
133139 paramCount , err := leb128 .ReadVarUint32 (r )
134140 if err != nil {
@@ -162,7 +168,7 @@ func (f *FunctionSig) UnmarshalWASM(r io.Reader) error {
162168}
163169
164170func (f * FunctionSig ) MarshalWASM (w io.Writer ) error {
165- _ , err := leb128 . WriteVarint64 (w , int64 ( f .Form ) )
171+ err := writeByte (w , f .Form )
166172 if err != nil {
167173 return err
168174 }
@@ -205,12 +211,16 @@ func (g *GlobalVar) UnmarshalWASM(r io.Reader) error {
205211 return err
206212 }
207213
208- m , err := leb128 . ReadVarUint32 (r )
214+ m , err := ReadByte (r )
209215 if err != nil {
210216 return err
211217 }
212218
213- g .Mutable = m == 1
219+ if m != 0x00 && m != 0x01 {
220+ return errors .New ("wasm: invalid global mutable flag" )
221+ }
222+
223+ g .Mutable = m == 0x01
214224
215225 return nil
216226}
@@ -219,14 +229,11 @@ func (g *GlobalVar) MarshalWASM(w io.Writer) error {
219229 if err := g .Type .MarshalWASM (w ); err != nil {
220230 return err
221231 }
222- var m uint32
232+ var m uint8
223233 if g .Mutable {
224234 m = 1
225235 }
226- if _ , err := leb128 .WriteVarUint32 (w , m ); err != nil {
227- return err
228- }
229- return nil
236+ return writeByte (w , m )
230237}
231238
232239// Table describes a table in a Wasm module.
@@ -310,17 +317,20 @@ func (e External) MarshalWASM(w io.Writer) error {
310317
311318// ResizableLimits describe the limit of a table or linear memory.
312319type ResizableLimits struct {
313- Flags uint32 // 1 if the Maximum field is valid
320+ Flags uint8 // 1 if the Maximum field is valid, 0 otherwise
314321 Initial uint32 // initial length (in units of table elements or wasm pages)
315322 Maximum uint32 // If flags is 1, it describes the maximum size of the table or memory
316323}
317324
318325func (lim * ResizableLimits ) UnmarshalWASM (r io.Reader ) error {
319326 * lim = ResizableLimits {}
320- f , err := leb128 . ReadVarUint32 (r )
327+ f , err := ReadByte (r )
321328 if err != nil {
322329 return err
323330 }
331+ if f != 0 && f != 1 {
332+ return errors .New ("wasm: invalid limit flag" )
333+ }
324334 lim .Flags = f
325335
326336 lim .Initial , err = leb128 .ReadVarUint32 (r )
@@ -339,14 +349,18 @@ func (lim *ResizableLimits) UnmarshalWASM(r io.Reader) error {
339349}
340350
341351func (lim * ResizableLimits ) MarshalWASM (w io.Writer ) error {
342- if _ , err := leb128 .WriteVarUint32 (w , uint32 (lim .Flags )); err != nil {
352+ f := lim .Flags
353+ if f != 0 && f != 1 {
354+ return errors .New ("wasm: invalid limit flag" )
355+ }
356+ if _ , err := w .Write ([]byte {f }); err != nil {
343357 return err
344358 }
345- if _ , err := leb128 .WriteVarUint32 (w , uint32 ( lim .Initial ) ); err != nil {
359+ if _ , err := leb128 .WriteVarUint32 (w , lim .Initial ); err != nil {
346360 return err
347361 }
348362 if lim .Flags & 0x1 != 0 {
349- if _ , err := leb128 .WriteVarUint32 (w , uint32 ( lim .Maximum ) ); err != nil {
363+ if _ , err := leb128 .WriteVarUint32 (w , lim .Maximum ); err != nil {
350364 return err
351365 }
352366 }
0 commit comments