-
Notifications
You must be signed in to change notification settings - Fork 23
Propose tools and APIs for lowering components to core modules #46
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
Draft
dicej
wants to merge
1
commit into
bytecodealliance:main
Choose a base branch
from
dicej:lower-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+203
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| # Summary | ||
| [summary]: #summary | ||
|
|
||
| This document proposes to create a pair of tools to support running components | ||
| using any compatible WebAssembly. These tools could be used either as an | ||
| alternative to providing native support for components in a given runtime or as | ||
| a temporary polyfill while native support is being built for that runtime. | ||
|
|
||
| # Motivation | ||
| [motivation]: #motivation | ||
|
|
||
| Implementing the complete Component Model specification is a non-trivial task | ||
| which entails significant up-front effort as well as ongoing maintenance. [Jco] | ||
| has proven useful as a polyfill for JS-embedded Wasm runtimes which don't yet | ||
| have native component support, but it entails a performance penalty and doesn't | ||
| support stand-alone Wasm runtimes. | ||
|
|
||
| On the other hand, though Wasmtime has performant, native support for | ||
| components, that code cannot easily be reused for other runtimes. In addition, | ||
| that implementation significantly expands the trusted compute base which must be | ||
| audited for correct and secure behavior beyond what is needed for core Wasm. It | ||
| is closely integrated with the unsafe internals of the runtime, requiring | ||
| specialized knowledge and care to maintain and modify. | ||
|
|
||
| Ideally, a component implementation would provide the best qualities of both of | ||
| those implementations, while addressing or side-stepping their weaknesses: | ||
|
|
||
| - Portable to arbitrary runtimes (JS-embedded or standalone) | ||
| - Performant | ||
| - Secure, e.g. doing as much work (and allocation) as practical in sandboxed | ||
| guest code, minimizing the TCB | ||
| - Maintainable without specialized knowledge of the internals of a particular | ||
| runtime | ||
| - Compatible with embedded and/or memory-limited scenarios | ||
|
|
||
| [Jco]: https://github.com/bytecodealliance/jco | ||
|
|
||
| # Proposal | ||
| [proposal]: #proposal | ||
|
|
||
| This proposal includes four things: | ||
|
|
||
| - A `lower-component` tool which takes a component as input and "lowers" it into | ||
| a core module | ||
| - A C API representing the intrinsics which a host runtime must provide in order | ||
| to run a module produced by `lower-component` | ||
| - A `host-wit-bindgen` tool which takes a WIT world and produces code for a | ||
| chosen target language to instantiate a `lower-component`-generated module and | ||
| invoke its exports | ||
| - A C API representing the operations which a host runtime must provide to | ||
| enable instantiation, invocation, access to memories and globals, etc. for | ||
| `host-wit-bindgen`-generated code to make use of | ||
|
|
||
| ## `lower-component` | ||
|
|
||
| The job of this tool is to take an arbitrary component as input and "lower" it | ||
| into a core module which may be run using an arbitrary Wasm runtime, assuming | ||
| the host provides a small set of intrinsics (covered later) which the module | ||
| will call as import functions. This tool could either be used ahead-of-time or | ||
| just prior to instantiation. | ||
|
|
||
| In general, a component may include a composition of more than one subcomponent, | ||
| each instantiation of which may require its own memory and table. In that case, | ||
| the output module will use multiple memories and tables and include generated | ||
| adapter code to "fuse" the imports of one component to the exports of another, | ||
| handling validation, cross-memory copies, etc., just as Wasmtime's FACT does | ||
| today. | ||
|
|
||
| In addition to the generated "fused adapter" code, the output module will | ||
| include component model runtime code, separately compiled from Rust source, | ||
| which handles, among other things: | ||
|
|
||
| - table management for resource and waitable values | ||
| - guest-to-guest stream and future I/O | ||
| - task and thread bookkeeping | ||
|
|
||
| That runtime code will itself make use of intrinsic functions imported from the | ||
| host in order to do things only the host can do, e.g. create, suspend, and | ||
| resume fibers and collect error backtraces. See the next section for details. | ||
|
|
||
| In the case of component-level exports which involve stream and/or future types, | ||
| the generated module would include function exports which the host may call to | ||
| create new values of those types. This is necessary because the tables for such | ||
| values are managed internally by the guest, not by the host. | ||
|
|
||
| ### Multiply-instantiated Modules | ||
|
|
||
| One challenge with lowering arbitrary components is that a component may | ||
| instantiate the same module more than once. In that case, we have three options: | ||
|
|
||
| - Reject the component | ||
| - Generate an output module with duplicate copies of each function in a | ||
| multiply-instantiated module, one per instance. | ||
| - Note that leaf functions which do not use memory or globals can be reused | ||
| without duplication. | ||
| - This could lead to significant bloat for "batteries-included" guest | ||
| languages like Python which do not have dead-code-elimination. | ||
| - Generate multiple output modules, plus metadata indicating how to instantiate | ||
| and link them together. | ||
| - This would require specifying how that metadata is represented and how the | ||
| whole combination of modules+metadata should be packaged. | ||
|
|
||
| ## Host C API for Lowered Components | ||
|
|
||
| As mentioned above, modules produced using `lower-component` can't (yet) express | ||
| all operations in core Wasm, and therefore must use intrinsics for certain | ||
| things: | ||
|
|
||
| - creating, suspending, and resuming fibers | ||
| - reading and writing fiber-local state | ||
| - generating stack traces for component-level errors | ||
|
|
||
| Fiber management could be expressed using the Stack Switching proposal, and | ||
| indeed `lower-component` will likely have an option to use those instructions, | ||
| but that proposal is not yet widely implemented, so we use intrinsics for | ||
| maximum portability. Hopefully all of the above features will eventually be | ||
| covered by widely-supported core Wasm instructions. | ||
|
|
||
| Note that these intrinsics need not be implemented in C, nor a language with | ||
| native support for the Wasm C ABI; we simply use C as a way to represent an ABI | ||
| in a familiar, human-readable format. | ||
|
|
||
| ``` | ||
| (TODO: Sketch the proposed API) | ||
| ``` | ||
|
|
||
| ## `host-wit-bindgen` | ||
|
|
||
| This tool takes as input a WIT world and produces source code for a given target | ||
| language which may be used to define component-level host functions, instantiate | ||
| `lower-component`-produced modules, and invoke its exports, etc. | ||
|
|
||
| Similar to `wit-bindgen`, this could be packaged either as a single tool | ||
| supporting multiple target languages or as separate tools, one per language. In | ||
| either case, the generated code would bottom out in calls to the runtime as | ||
| defined by the API described in the next section. | ||
|
|
||
| Alternatively, the functionality of `host-wit-bindgen`-generated code could be | ||
| provided by a library providing a general-purpose, dynamic API for creating | ||
| component values, defining host functions, and calling functions. This would be | ||
| useful in scenarios where the shape of the component is not known ahead of time, | ||
| and/or the target language is already so dynamic that code generation is | ||
| redundant. | ||
|
|
||
| ## Host C API for Embedder Bindings | ||
|
|
||
| In theory, `host-wit-bindgen` could support multiple front-ends (e.g. Rust, | ||
| Python, C#, Go, etc.) _and_ multiple back-ends (Wasmtime, WAMR, Wazero, JS, | ||
| etc.), but it's probably easier to define a runtime-agnostic C API which each | ||
| runtime can implement to support the low-level operations required by | ||
| `host-wit-bindgen`-generated code. Those operations include: | ||
|
|
||
| - creating a "store" in which one or more modules may be instantiated | ||
| - defining host functions | ||
| - instantiating a module | ||
| - calling a module's exports | ||
| - reading from and writing to a module's memories and globals | ||
| - creating, suspending, and resuming fibers | ||
| - generating stack traces | ||
| - reading and writing fiber-local state | ||
|
|
||
| Given that a C API doesn't make sense in e.g. a web browser, this could be | ||
| mirrored as a JS API for use in JS-embedded runtimes. | ||
|
|
||
| ``` | ||
| (TODO: Sketch the proposed API) | ||
| ``` | ||
|
|
||
| # Rationale and alternatives | ||
| [rationale-and-alternatives]: #rationale-and-alternatives | ||
|
|
||
| See the `Motivation` section above for rationale. | ||
|
|
||
| At a high level, the alternative to creating a runtime-agnostic component model | ||
| implementation is to implement a native one for each runtime, possibly with some | ||
| parts factored out into reusable libraries. This is the approach we've taken so | ||
| far with Wasmtime, although not necessarily the one we'd choose with the benefit | ||
| of hindsight. In any case, a runtime-agnostic implementation would be useful as | ||
| a temporary polyfill for use in a given runtime until a native implementation is | ||
| complete. | ||
|
|
||
| ## Prior art | ||
|
|
||
| There are already a few projects which polyfill the component model: | ||
|
|
||
| - [Jco](https://github.com/bytecodealliance/jco) for JS-embedded runtimes | ||
| - [Gravity](https://github.com/arcjet/gravity) for Wazero on Go | ||
| - [Meld](https://github.com/pulseengine/meld) for arbitrary runtimes | ||
|
|
||
| # Open questions | ||
| [open-questions]: #open-questions | ||
|
|
||
| - How to handle multiply-instantiated modules, and how common are the components | ||
| which do that? | ||
| - Who should be responsible for type checking during host->guest and guest->host | ||
| invocations? | ||
| - If `host-wit-bindgen`, where will in the flattened module will it find the | ||
| type metadata it needs? | ||
| - If `lower-component`, it could be harder to optimize (e.g. doing it on | ||
| every call, whereas `host-wit-bindgen` could lean on the target language's | ||
| static type guarantees, as `wasmtime-wit-bindgen` does today) | ||
| - How much thread-local state management can be handled by the | ||
| `lower-component`-generated module vs. by the host? | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a really good point and I think it's very important to solve "right", and not just reject components that instantiate a module more than once: that's a fundamental capability of the component model that core Wasm (without metadata/wrapper) doesn't have, and we don't want to bifurcate the ecosystem into components that fit this restriction and those that don't.
Function duplication (your second option) seems conceptually appealing because it hides the complexity, but in practice I suspect a large majority of functions will be duplicated, because almost everything will access memory...
Maybe the best option here is to actually define a "just the module linking, please" subset of the component model semantics that gives (i) a flat index space of core modules, (ii) a wiring diagram instantiating them and connecting imports and exports? The host already has to do some work to provide some intrinsics so this proposal is not "free" in any case; so ingesting such a format should not be too much of an additional sell (though there is certainly a step-function increase from "one core module" to "graph of core modules"). It's also conceptually the cleanest IMHO: this really is a thing that the component semantics can describe that a core Wasm can't, but most core Wasm runtimes should have host APIs to instantiate a thing more than once, so we should just "pass it through".
Just to note it down, though I don't like it: I guess there could be a fourth option here, which is (at a high level) something like "reify the
vmctxas actual Wasm state". That seems to be the most "honest" w.r.t. the lowering paradigm.The idea is that one would reify data structures that look like Wasmtime's instance state as Wasm GC values. A Wasm memory could be an arrayref to an array-of-i8; a Wasm table could be an arrayref to an array-of-whatever. Given those, one could define a
vmctxWasm struct that contains memory refs and table refs as our nativevmctxdoes today, as well as any globals, inlined; then the lowered functions take thisvmctxstruct ref as an implicit first arg.This clearly would have nontrivial runtime overhead as well, since in essence we'd have two levels of indirection for any state access.
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.
Yeah, I expect this is what it would have to look like. One thought that crossed my mind would be to literally output a real component, but one that only uses the absolute minimum set of features needed to embed, instantiate, and link modules. Hosts would need to be able to parse and instantiate these "simple components" but not need to support the entire component model.