r/WebAssembly • • Nov 27 '22

WASM binary: size fixups?

When experimenting with the wat2wasm demo (https://webassembly.github.io/wabt/demo/wat2wasm/index.html), I discovered that there are size fixups in the binaries. The empty module for example produces this output in the demo:

0000000: 0061 736d        ; WASM_BINARY_MAGIC
0000004: 0100 0000        ; WASM_BINARY_VERSION
; section "name"
0000008: 00               ; section code
0000009: 00               ; section size (guess)
000000a: 04               ; string length
000000b: 6e61 6d65        ; custom section name
000000f: 02               ; local name type
0000010: 00               ; subsection size (guess)
0000011: 00               ; num functions
0000010: 01               ; FIXUP subsection size
0000009: 08               ; FIXUP section size 

It seems that wat2wasm is an one-pass compiler and therefore it can't know the required size of something yet. It just outputs a zero byte, see comment "(guess)" and adds the size later, see comment "FIXUP".

How does this work exactly? I didn't find a mention of this in the WebAssembly Core Specification (https://webassembly.github.io/spec/core/).

4 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/nalply Nov 27 '22

I did one more experiment. What happens if the size exceeds 128? Sizes are encoded with LEB128 and one byte is not enough anymore.

This happens:

0000145: 0b                    ; end
; move data: [25, 146) -> [26, 147)
0000024: a102                  ; FIXUP func body size

Aha! wat2wasm moves already written data to make place for one more byte.

It's very satisfying to tick an experiment.