wasmerio / wasmer

🚀 The leading Wasm Runtime supporting WASIX and WASI
https://wasmer.io
MIT License
18.97k stars 812 forks source link

Improved WASMER_METADATA_{prefix} for static artifacts #5189

Open Destructor17 opened 2 weeks ago

Destructor17 commented 2 weeks ago

Motivation

When making module from static artifact, it is currently required to include C header made using gen-c-header subcommand. This header includes implementation of generate_serialized_data_{prefix} function. It makes loading static artifacts more complicated, especially from Rust.

generate_serialized_data_{prefix} is actually simple: it accepts no parameters and concatenates WASMER_METADATA_{prefix} with some function pointers. Every invocation of generate_serialized_data_{prefix} returns same data.

Proposed solution

The idea is to append these function pointers to WASMER_METADATA_{prefix} and add WASMER_METADATA_{prefix}_SIZE, so static artifact can be loaded without including generated C header. This solution should not break loading static artifact using generated C header.

Alternatives

It is theoretically possible to move generate_serialized_data_{prefix} to object generated using create-exe and create-obj subcommand, but it seems to be too complicated and not reasonable.

Additional context

Example code loading static artifact. No C code is used.

extern "C" {
    static WASMER_METADATA_WR_AOT: u8;
    static WASMER_METADATA_WR_AOT_SIZE: usize;
}

let module = unsafe {
    wasmer::Module::deserialize(
        &store,
        std::slice::from_raw_parts(&WASMER_METADATA_WR_AOT, WASMER_METADATA_WR_AOT_SIZE),
    )?
};

Here is a draft implementation of this feature. I am not very experienced in rust, so it is not likely that I will make a PR.

xdoardo commented 6 days ago

Thanks! We'll follow up from your draft.