Stream: git-wasmtime

Topic: wasmtime / issue #10218 API to access custom sections


view this post on Zulip Wasmtime GitHub notifications bot (Feb 11 2025 at 09:57):

Timmmm opened issue #10218:

Feature

It would be nice if Component could provide access to custom sections (see the Javascript API).

Benefit

Allows you to access custom sections. For me specifically, I'm writing a better version of pre-commit using WASI and I'm planning to store metadata about how to run the linter in a custom section.

Implementation

I think wasmtime uses wasmparser which can already parse these, so it just needs to be exposed somehow. I think the biggest question is where the data is stored. I assume Component discards all the data it has read after compilation, and it doesn't know in advance whether you want to keep the custom sections or not... I dunno I'm not familiar enough with the implementation to say more.

Alternatives

My current solution is to use wasmparser to parse the file a second time, but it would be nice to not have to do this:

pub fn read_metadata(wasm_path: &Path) -> Result<MyMetadata> {
    let module = std::fs::read(wasm_path)?;

    // TODO: Don't read all into memory... but it's probably fine and
    // wasmparser doesn't provide a handy parse_reader() method.

    let parser = Parser::new(0);
    for payload in parser.parse_all(&module) {
        match payload? {
            Payload::CustomSection(section) if section.name() == "my_metadata" => {
                return Ok(serde_json::from_slice::<MyMetadata>(section.data())?);
            }
            _ => {}
        }
    }
    bail!("No metadata section found in the wasm file");
}

view this post on Zulip Wasmtime GitHub notifications bot (Feb 11 2025 at 15:58):

alexcrichton commented on issue #10218:

I think this is reasonable to have in Wasmtime, yeah, although personally I'd hope that we could get away with a signature such as:

impl Module {
    pub fn custom_sections(module: &[u8]) -> impl Iterator<Item = Result<(&str, &[u8])>> + '_;
}

(or something like that)

Ideally we wouldn't need to preserve all custom sections in the compiled artifact just to support an optional API, but we could also provide methods to optionally include them for example through CodeBuilder.

Components are an interesting case here because components themselves have custom sections as well as the modules internally. That means that the custom section you could be searching for could be anywhere within a component and returning where exactly in the component a custom section was found is an interesting API design question.

Overall I'm kind of on the fence about this. On one hand it's not too too hard to add a dependency on wasmparser (you can even pick it up through Wasmtime) but on the other hand if this is expected from engines (by proxy from JS) then it's also not too hard to support in Wasmtime. One perhaps middle-ground is to provide the api in Wasmtime but basically ignore the component-specific problem of "where was the custom section found?" and defer that to manual usage of wasmparser

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2025 at 18:01):

fitzgen added the enhancement label to Issue #10218.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2025 at 18:01):

fitzgen added the wasmtime:api label to Issue #10218.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2025 at 18:01):

fitzgen added the wasmtime label to Issue #10218.


Last updated: Feb 28 2025 at 02:27 UTC