Skip to content

Conversation

@alexcrichton
Copy link
Collaborator

This commit is an improvement to the WASIp2 target which changes how the interface wasi:cli/run is defined and exported. Previously the wasm32-wasip2 target would define a _start wasm export which required the use of the WASIp1-to-WASIp2 adapter to hook it up to wasi:cli/run. This meant that despite libc not actually using any WASIp2 APIs the adapter was still used. The purpose of this commit is to change this.

The changes made here are:

  • The linker-level _start symbol now has a wasm-level export name of wasi:cli/run@0.2.0#run. This means that _start isn't a wasm-level export any more, meaning there's nothing for the adapter to import.
  • The signature of the C-level _start function is now different based on WASI version (returns an int in WASIp2, nothing on WASIp1)
  • The crt1-command.o object file now contains type information for wasi:cli/run (which previously wasn't present anywhere in wasi-libc). This means that when this object is linked in wit-component will know what to do after linking.

The end result is that binaries are now produced entirely without the WASIp1-to-WASIp2 adapter and linking with -Wl,--wasi-adapter=none, for example, produces a working binary.

Another minor change made in this commit is that the crt1-*.c files are now included in clang-format like all other source files.

Build-wise this requires the usage of wasm-tools at build-time to embed type information in the clang-produced object file for crt1-command.o.

exit_result_void_void_t status = { .is_err = true };
exit_exit(&status);
}
return r != 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the exit call no longer needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a target-specific entrypoint signature I was able to update to directly return the result that the wasi:cli/run entrypoint returns rather than being required to go through the wasi:cli/exit route. A bit of a nice benefit as programs will now actually naturally return as opposed to prior where they would unconditionally raise a trap to exit.

else()
# wasip2+: crt1-command.o is modified from what CMake produces to
# additionally have a custom section representing the type information needed
# for its contained export. This is the `wasi:cli/run` interface, for example.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be hard to add an assembly syntax for this so that clang/llvm can produce this object file without needing external tools?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I poked at that a bit, but was unfruitful in my ability to get it working. That being said you probably know whether this is possible more than I, so question for you!

The need here is that a custom section needs to be located inside of crt1-command.o (wasm custom section, not linking custom section). This wasm-tools command will embed the data within the object with a particular section name. We can pretty easily generate the data to embed ahead-of-time (similar to how wit-bindgen-generated bindings are checked in to this repo). Effectively what I want is something like:

__asm__(
  ".custom_section.component_type:other:naming:information"
#embed <the_world.wasm>
);

or... something like that. I couldn't figure out either .custom_section for __asm__, #embed, nor how to emit a wasm.custom_section in Clang via a C global or something like that. Do you know if these are all possible to combine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks like something that it should be possible to support with the asm syntax yes. Assuming that #embed plays nice with __asm__ in general (i.e. on other platforms) i don't see why it shouldn't for us.

BTW, In this case are you actually embedding a wasm file as a custom section in another wasm file (i.e. the object file)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my problem is I can't actually figure out a syntax/incantation that works. I suspect something like this should work, but I'm not enough of a clang wizard to figure it out.

And yeah, the type information for components is itself a component (a wasm file) which is embedded in objects.

This commit is an improvement to the WASIp2 target which changes how the
interface `wasi:cli/run` is defined and exported. Previously the
`wasm32-wasip2` target would define a `_start` wasm export which
required the use of the WASIp1-to-WASIp2 adapter to hook it up to
`wasi:cli/run`. This meant that despite libc not actually using any
WASIp2 APIs the adapter was still used. The purpose of this commit is to
change this.

The changes made here are:

* The linker-level `_start` symbol now has a wasm-level export name of
  `wasi:cli/run@0.2.0#run`. This means that `_start` isn't a wasm-level
  export any more, meaning there's nothing for the adapter to import.
* The signature of the C-level `_start` function is now different based
  on WASI version (returns an `int` in WASIp2, nothing on WASIp1)
* The `crt1-command.o` object file now contains type information for
  `wasi:cli/run` (which previously wasn't present anywhere in
  wasi-libc). This means that when this object is linked in
  `wit-component` will know what to do after linking.

The end result is that binaries are now produced entirely without the
WASIp1-to-WASIp2 adapter and linking with `-Wl,--wasi-adapter=none`,
for example, produces a working binary.

Another minor change made in this commit is that the `crt1-*.c` files
are now included in `clang-format` like all other source files.

Build-wise this requires the usage of `wasm-tools` at build-time to
embed type information in the clang-produced object file for
`crt1-command.o`.
// that this shouldn't be too problematic (in theory).
__attribute__((export_name("wasi:cli/run@0.2.0#run"))) int _start(void)
#elif defined(__wasip3__)
__attribute__((export_name("wasi:cli/run@0.3.0-rc-2025-09-16#run"))) int
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't wasi:cli/run.run an async function in wit, and thus would need a different annotation and a callback companion entry point? I guess this can work because the runtime adapts this blocking implementation?

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.

4 participants