-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement shared host functions. #2625
Conversation
Subscribe to Label Actioncc @kubkon, @peterhuene
This issue or pull request has been labeled: "wasi", "wasmtime:api"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
7c1b8ae
to
c10bf06
Compare
c10bf06
to
3144f59
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Just dropping my thoughts; leaving proper review and signoff to Alex, therefore I'm "commenting" rather than "approving".)
26ea121
to
454276b
Compare
4792401
to
3c43ed4
Compare
000bf5e
to
4a34973
Compare
Now that both the async PR and wiggle support for async have landed, I need to add support for async shared host functions too. I'll push that up with a rebase. |
This isn't quite done as I need to add tests for |
Subscribe to Label Actioncc @peterhuene
This issue or pull request has been labeled: "wasmtime:c-api"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
Looking good to me! Could you detail more why the removal of the lifetime from |
I did run afoul of the borrow-checker when implementing the wiggle wasmtime integration as the context is coming from Let me double check that this is absolutely necessary before we proceed with this, as it is a breaking change for API users. |
It looks like I was missing a lifetime constraint on the future in the callback signature, so I'll revert the change to |
This commit introduces defining host functions at the `Config` rather than with `Func` tied to a `Store`. The intention here is to enable a host to define all of the functions once with a `Config` and then use a `Linker` (or directly with `Store::get_host_func`) to use the functions when instantiating a module. This should help improve the performance of use cases where a `Store` is short-lived and redefining the functions at every module instantiation is a noticeable performance hit. This commit adds `add_to_config` to the code generation for Wasmtime's `Wasi` type. The new method adds the WASI functions to the given config as host functions. This commit adds context functions to `Store`: `get` to get a context of a particular type and `set` to set the context on the store. For safety, `set` cannot replace an existing context value of the same type. `Wasi::set_context` was added to set the WASI context for a `Store` when using `Wasi::add_to_config`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok everything looks good to me modulo one bit. Currently we have Func::new_async
panic if it's not paired with an async store, and similarly Func::new
panics if paired with an async store. We can't panic when Config::*
functions are called to define function since we don't have a store, but I think today you can define an async host function which gets paired with a sync store, which I don't think should work.
I think we could solve this one of a few ways:
- Move the async-ness to
Config
instead ofStore
, panic inConfig
on a mismatch - Return
None
fromget_host_func
if the async-ness of the function mismatches with the store. - Panic in
get_host_func
if the async-ness mismatches.
I think I'd lean towards the first option if we could, I don't think there's actually any use for async
being at the Store
level instead of Config
, that's just originally how it played out. While it's technically possible to mix stores in the same engine with different async configurations, I'm not sure if there's much practical use to that.
+1 |
That works for me too. I'll see about implementing that. I'm also adding |
This commit moves the concept of "async-ness" to `Config` rather than `Store`. Note: this is a breaking API change for anyone that's already adopted the new async support in Wasmtime. Now `Config::new_async` is used to create an "async" config and any `Store` associated with that config is inherently "async". This is needed for async shared host functions to have some sanity check during their execution (async host functions, like "async" `Func`, need to be called with the "async" variants).
This commit updates the async function tests to also smoke the shared host functions, plus `Func::wrap0_async`. This also changes the "wrap async" method names on `Config` to `wrap$N_host_func_async` to slightly better match what is on `Func`.
@alexcrichton I pushed up a few more commits, including moving the "async-ness" to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious on your thoughts on new_async
vs Config::async_
as a configurator, but otherwise can you update the WASI example as well to use the add_to_config
method instead of the add_to_linker
?
I'd be ok either outright removing add_to_linker
, but in general it seems like we should nudge folks to add_to_config
if possible.
I'll update the WASI example to define the host functions in I think we will want to leave |
This commit moves the instantiated instance allocator from `Config` into `Engine`. This makes certain settings in `Config` no longer order-dependent, which is how `Config` should ideally be. This also removes the confusing concept of the "default" instance allocator, instead opting to construct the on-demand instance allocator when needed. This does alter the semantics of the instance allocator as now each `Engine` gets its own instance allocator rather than sharing a single one between all engines created from a configuration.
6223144
to
0e70704
Compare
I like the most recent commit and think it works well. I'd probably change I agree with you, though, that |
I'm +1 on I just meant that deferring the instance allocator instantiation helps Host functions would be the last thing contributing to the state of |
Eh I'm not too worried about the breaking change here, in theory it's O(1) per embedding so while it's a big change for us since we have so many tests for consumers it's probably not that bad. (I also have other desired breaking changes like #2719 which are large-ish) I suppose another thing we could do is go the route of TBH I'm not sure I know of a great way to manage this, but I don't think it matters too much. We don't have to get it 100% right just yet so as long as something works pretty well I think it's fine to land. |
This is a breaking API change for anyone using `Engine::new`. As creating the pooling instance allocator may fail (likely cause is not enough memory for the provided limits), instead of panicking when creating an `Engine`, `Engine::new` now returns a `Result`.
This commit removes `Config::new_async` in favor of treating "async support" as any other setting on `Config`. The setting is `Config::async_support`.
So I've removed I've also (a little hackily imo) deferred the check of async shared host functions requiring |
This commit removes the order dependency where async support must be enabled on the `Config` prior to defining async host functions. The check is now delayed to when an `Engine` is created from the config.
This commit updates the WASI example to use `Wasi::add_to_config`. As only a single store and instance are used in the example, it has no semantic difference from the previous example, but the intention is to steer users towards defining WASI on the config and only using `Wasi::add_to_linker` when more explicit scoping of the WASI context is required.
7e96169
to
f1f5443
Compare
Subscribe to Label Actioncc @fitzgen
This issue or pull request has been labeled: "fuzzing"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
👍 |
This PR introduces defining host functions at the
Config
rather than withFunc
tied to aStore
.The intention here is to enable a host to define all of the functions once at
with a
Config
and then use aLinker
(or directly withStore::get_host_func
) to use the functions when instantiating a module.This should help improve the performance of use cases where a
Store
isshort-lived and redefining the functions at every module instantiation a
noticeable performance hit.
See bytecodealliance/rfcs#9 regarding these changes.