Search code examples
webassembly

Format of size field in wasm sections?


I am fighting a bug in my own webAssembly JS to wasm compiler. I am not sure if I have the size format correct and what the limits are

For example a data segment with 127 bytes

00 41 00 0b 7f ...data
            ^^ size 127 bytes

Then with 128 bytes

00 41 00 0b 8001 ...data
            ^^^^ size 128

I am guessing that the high bit if on indicates that there will be a byte following. That would mean that the next step would be

00 41 00 0b ff7f ...data
            ^^^^ size 16383

00 41 00 0b 808001 ...data
            ^^^^^^ size 16384

So the length is in nibit order (3 bytes 6 nibits) 103254 with nibits 1 and 3 having the top bit set such that looking at bits S1110000 S3332222 A5554444 where S is always on and A is always off and numbers represent nibit bit order.

  • Is this correct and what is the largest value that can be represented in this format?

  • Is this the only size format?

  • Is there a standard algorithm to convert from a Javascript Number representing length to this format as currently my solution seams very hacky?


Solution

  • The Wasm binary format uses LEB128 format for all integer numbers. In the case of sizes specifically unsigned LEB128. However, unlike general LEB128s, Wasm imposes a maximum length, see the specification.

    You can find plenty of sample implementations of LEB128 encoding/decoding on the Web, e.g., it's a 3-line function in the Wasm reference interpreter (in OCaml).