Skip to content

Fix viceroy adapt for wasip2 with main function - #748

Open
chenyan2002 wants to merge 4 commits into
mainfrom
yan/adapt-wasip2
Open

chenyan2002 wants to merge 4 commits into
mainfrom
yan/adapt-wasip2

Conversation

@chenyan2002

@chenyan2002 chenyan2002 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

When a core module with main function is built with target wasm32-wasip2, the entry export becomes wasi:cli/run@0.2.0#run, instead of _start.

This means we need to synthesize a _start function by calling wasi:cli/run@0.2.0#run, and call wasi:cli/exit if the return is non-zero.

Another "simpler" option is to change the declaration in the adapter with

#[link(wasm_import_module = "__main_module__")]
extern "C" {
    #[link_name = "wasi:cli/run@0.2.0#run"]
    fn run() -> i32;
}

But that means we need yet another adapter binary.

@chenyan2002
chenyan2002 requested a review from a team as a code owner October 20, 2026 23:07
@dgohman-fastly

Copy link
Copy Markdown
Contributor

Instead of trapping if the error code is nonzero, could it call wasi:cli/exit instead?

@chenyan2002

Copy link
Copy Markdown
Contributor Author

Good idea. I also changed the synthesize function to use walrus, because importing "wasi:cli/exit" may change all the function index, and walrus ends up with much shorter code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants