bytecodealliance / wasm-tools

CLI and Rust libraries for low-level manipulation of WebAssembly modules
Apache License 2.0
1.3k stars 234 forks source link

wasm-encoder: add method to allow fetching raw Function bytes. #1630

Closed cfallin closed 3 months ago

cfallin commented 3 months ago

When generating function bodies with wasm-encoder, it is sometimes useful to take the function bytecode, cache it, then reuse it later. (For my specific use-case, in weval, I would like to cache weval-specialized function bodies and reuse them when creating new modules.)

Unfortunately the existing API of wasm-encoder makes this almost but not quite easy: Function implements Encode::encode, but this will produce a function body with the length prefixed. However there's no way to use this unmodified with the next level up the entity hierarchy: CodeSection::raw wants the function bytes without the length prefixed.

This is a fairly annoying if small API gap, and otherwise requires manually stripping the length prefix, a leb128-encoded integer. It's also a small footgun: I naively did not realize this mismatch and tried to do the above, only to get perplexing type errors with locals.

This PR adds one method on wasm_encoder::Function to return the inner bytes directly, and the doc-comment contains an example showing the intended use-case.

alexcrichton commented 3 months ago

Happy to add more helpers and/or edit existing helpers as well if needed!

cfallin commented 3 months ago

Thanks! I think I have what I need for now (I have caching working in weval).

It looks like the release schedule is somewhat ad-hoc here but that we have the same automation as in Wasmtime -- if it's not too much trouble, would it be possible to do a point release with this? No particular rush if another release is coming soon anyway.

alexcrichton commented 3 months ago

It's true yeah there's no release cadence here, but one difference from Wasmtime is that there's not great support for backports and/or release branches. The current patch release process only works if there aren't otherwise breaking changes on main relative to the last release, which there currently are. Would it be best to get this out pretty soon? If so I might manually make a release branch for this and backport it

cfallin commented 3 months ago

Ah, no worries then, happy to wait for the next major release. To be concrete I don't have any reason to ask for it to be expedited this week in particular, then I'm on PTO for two weeks, so any time in the next month is probably fine :-)