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…
Understanding every byte in a WASM module
11–20 of 38 posts
Re: Understanding every byte in a WASM module
#12Here is a demo with some Rust output: https://evmar.github.io/weave/?wasm/rust.wasm be sure to click on 'code' to dive into individual functions.
Re: Understanding every byte in a WASM module
#13Earlier 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/
Re: Understanding every byte in a WASM module
#14Earlier 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.
Re: Understanding every byte in a WASM module
#15Either 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!
Re: Understanding every byte in a WASM module
#16A 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/
Re: Understanding every byte in a WASM module
#17I 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.
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
#18A 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/
Re: Understanding every byte in a WASM module
#19Earlier 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…
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
#20A 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/