Trying to make WASM Binary instrumenter by following TigerStyle

So was trying to write this library wsplice which let’s you instrument wasm binary, with almost zero-allocation, and of course trying to follow the TigerStyle. The indecies resolving part is very very naive mostly O(n^2) look-ups since I was trying to avoid any sort of allocation from the beginning. This is unfinished there is no proper resolver for function references, no Assembler(there exists something which pretends like one but just to inject few instructions), doesn’t support most of the GC related opcodes and other specs.

The main idea was the caller provides a writer, edit plans, and it should emit the edited wasm.

I will come clean I probably don’t undestand the full TigerStyle, specially the Fault model part. Whatever I understood tried to apply that for this library. The fact checks are mostly expected “kind” bytes for different sections/sub-sections and index/range in bounds or not, otherwise I couldn’t think of much fact check possible. The library does decode some level of wasm function body, since, I wanted to support “function body rewriter” iterator, so that user can at least know that the next opcode is X and it’s position so they can decide to decode the rest of the operands and other things on their own if they wanted, during opcode decoding a lot of the things can go wrong I just don’t have the idea on how to fact check every single one of them so most of the time I just let it panic through unreachable.

In the code base edit, plan might look different but in the paragaph they actually mean the same thing edit plan, it just means some type of instruction what should get emitted as the final output in the context of Wasm Instrumenting.

So, for easier use case I had to introduce a Builder which accepts allocator each time user wants to add an construct an edit plan. Which you might guess violates TigerStyle’s strict allocation policy, where allocation must not happen after initialization, or misunderstood the essence of it.
I was thinking of Introducing Storage which the user can initialize each “edit” slots(whatever they might need, like you might not want to add a new type so that “add type edit” slot can be empty) and pass that to Builder, then builder uses that buffer for each required slot, if the user provided buffer gets exhausted I could just return an error. But again it didn’t feel ergonomic.
Currently it follows the std.ArrayList.append for every new edit plan you pass an allocator, we could introduce *AssumeCapacity variants and a initCapacity initializer of the Builder but again I am struggling to decide.

If you have any questions regarding my design choices or an improvment please let me know, I am really eager to hear your thoughts.

Disclaimer: I’ve used AI for writing the library specially heavily for generting tests, comments, and to learn about WASM specs. But the decisions on how X,Y,Z should behave was decided by me everywhere. docs/ARCHITECTURE.md is an abomination of that, sorry, but I couldn’t just express my thoughts in English properly for some reason…

1 Like