Live data from Hacker News

Understanding every byte in a WASM module

danielmangum.com

11–20 of 38 posts

Re: Understanding every byte in a WASM module

#11

A while back I ran through some exercises that had me writing WAT directly. It's a great way to understand what WASM is doing, and is surprisingly easy to pick up (though of course you wouldn't want to write an actual program with it). It's just system calls with s-expressions.

For some time I've been fascinated by the codebase of a small WAT compiler written in JavaScript. https://github.com/stagas/wat-compiler/blob/main/story.txt And I mean "small" as a real complement to how readable and compact the entire compiler is. It's been a great way to appreciate the design of the WASM text format and WASM overall. It's not a Lisp but has a similar feel to it. I've been meaning to get more fluent…

Very cool. You might be interested in AssemblyScript, if you haven't heard of them before. They did something very similar: https://www.assemblyscript.org/

Re: Understanding every byte in a WASM module

#13

Earlier quoted context omitted.

For some time I've been fascinated by the codebase of a small WAT compiler written in JavaScript. https://github.com/stagas/wat-compiler/blob/main/story.txt And I mean "small" as a real complement to how readable and compact the entire compiler is. It's been a great way to appreciate the design of the WASM text format and WASM overall. It's not a Lisp but has a similar feel to it. I've been meaning to get more fluent…

Very cool. You might be interested in AssemblyScript, if you haven't heard of them before. They did something very similar: https://www.assemblyscript.org/

Ah yes, I'd love to get more familiar with AssemblyScript too. One thing I like about the afore-mentioned WAT compiler though, is that it's simple to run in the browser, which the AssemblyScript compiler doesn't support, if I understand correctly. That characteristic of the compiler would be useful if I want to build a higher-level language on top of it, for example with a web playground (code editor and runtime).

Re: Understanding every byte in a WASM module

#14
post #8
post #4

Earlier quoted context omitted.

> the nice design But integers are read 7 bits at a time. Wouldn't it be much easier to use 8 bits?

It is a balance between simplicity, speed and size. They choose size, honesty leb128 isn’t *too* complex (you can see an implementation in my repo I pointed too). But yes they don’t take all the simplicity tradeoffs.

Protobuf seems to use a very similar encoding.

Re: Understanding every byte in a WASM module

#15
This seems sort of like understanding machine code vs assembly; it's much easier to learn WAT and translate to/from WASM as necessary using the wabt tools [0].

Either way its super cool how simple WebAssembly is, you can really get your hands dirty and understand exactly every detail of how your program runs!

[0] https://github.com/WebAssembly/wabt

Re: Understanding every byte in a WASM module

#16
post #9

A while back I ran through some exercises that had me writing WAT directly. It's a great way to understand what WASM is doing, and is surprisingly easy to pick up (though of course you wouldn't want to write an actual program with it). It's just system calls with s-expressions.

WAT is a fun format! I'm curating some readable samples here -- https://github.com/eliben/wasm-wat-samples/

Bookmarking that for later, thanks for sharing!

Re: Understanding every byte in a WASM module

#17

I really appreciate this article. I’ve noticed that modern streaming web players have started using WASM as part of their obfuscation toolbox. Understanding WASM modules is important for reverse engineering.

The browser devtools can give you a disassembled view of the WebAssembly byte code (basically the wasm2wat output) and also allow to set breakpoints and debug-step through the disassembly.

Since WASM is a "structured-programming ISA" with code blocks that can be intended, it's actually quite convenient.

Re: Understanding every byte in a WASM module

#18
post #9

A while back I ran through some exercises that had me writing WAT directly. It's a great way to understand what WASM is doing, and is surprisingly easy to pick up (though of course you wouldn't want to write an actual program with it). It's just system calls with s-expressions.

WAT is a fun format! I'm curating some readable samples here -- https://github.com/eliben/wasm-wat-samples/

I already had that bookmarked from using it several months ago to try and figure things out. It's been very helpful. Thank you for creating it!

Re: Understanding every byte in a WASM module

#19
post #7
post #4

Earlier quoted context omitted.

> the nice design But integers are read 7 bits at a time. Wouldn't it be much easier to use 8 bits?

This is basically the design of UTF-8 or varint encoding. The integer's representation is read one byte (8 bits) at a time, which can then be interpreted by looking at the first bit which says whether the integer is "done" or more bytes need to be read. (If we were using all 8 bits for the representation and an additional leading bit for whether it continues, we'd end up having to read 9 bits or something, which is a…

UTF-8 is a bit more complex than that.

The first byte in a sequence basically tells you how many bytes are following by looking at the top-most bits.

    0xxxxxxx => no followup bytes, just the 7-bit ASCII code
    110xxxxx => 1 byte follows
    1110xxxx => 2 bytes follow
    11110xxx => 3 bytes follow
The followup bytes then always have the pattern 10xxxxxx.

E.g. the only bytes that have bit 7 cleared are the single-byte 7-bit ASCII character codes.

Re: Understanding every byte in a WASM module

#20
post #9

A while back I ran through some exercises that had me writing WAT directly. It's a great way to understand what WASM is doing, and is surprisingly easy to pick up (though of course you wouldn't want to write an actual program with it). It's just system calls with s-expressions.

WAT is a fun format! I'm curating some readable samples here -- https://github.com/eliben/wasm-wat-samples/

I have a hand-written BF interpreter in WAT, along with hand-written asm.js for comparison, over here https://github.com/magcius/bfasm
Post reply on HN