Skip to content
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 support for async functions in Wasmtime #2434

Merged
merged 21 commits into from
Feb 26, 2021

Commits on Feb 26, 2021

  1. Implement support for async functions in Wasmtime

    This is an implementation of [RFC 2] in Wasmtime which is to support
    `async`-defined host functions. At a high level support is added by
    executing WebAssembly code that might invoke an asynchronous host
    function on a separate native stack. When the host function's future is
    not ready we switch back to the main native stack to continue execution.
    
    There's a whole bunch of details in this commit, and it's a bit much to
    go over them all here in this commit message. The most important changes
    here are:
    
    * A new `wasmtime-fiber` crate has been written to manage the low-level
      details of stack-switching. Unixes use `mmap` to allocate a stack and
      Windows uses the native fibers implementation. We'll surely want to
      refactor this to move stack allocation elsewhere in the future. Fibers
      are intended to be relatively general with a lot of type paremters to
      fling values back and forth across suspension points. The whole crate
      is a giant wad of `unsafe` unfortunately and involves handwritten
      assembly with custom dwarf CFI directives to boot. Definitely deserves
      a close eye in review!
    
    * The `Store` type has two new methods -- `block_on` and `on_fiber`
      which bridge between the async and non-async worlds. Lots of unsafe
      fiddly bits here as we're trying to communicate context pointers
      between disparate portions of the code. Extra eyes and care in review
      is greatly appreciated.
    
    * The APIs for binding `async` functions are unfortunately pretty ugly
      in `Func`. This is mostly due to language limitations and compiler
      bugs (I believe) in Rust. Instead of `Func::wrap` we have a
      `Func::wrapN_async` family of methods, and we've also got a whole
      bunch of `Func::getN_async` methods now too. It may be worth
      rethinking the API of `Func` to try to make the documentation page
      actually grok'able.
    
    This isn't super heavily tested but the various test should suffice for
    engaging hopefully nearly all the infrastructure in one form or another.
    This is just the start though!
    
    [RFC 2]: bytecodealliance/rfcs#2
    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    c471a1c View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    e46bccb View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    20833b5 View commit details
    Browse the repository at this point in the history
  4. Fix a typo

    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    549461c View commit details
    Browse the repository at this point in the history
  5. Update lock file

    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    30993d6 View commit details
    Browse the repository at this point in the history
  6. Implement periodically yielding with fuel consumption

    This commit implements APIs on `Store` to periodically yield execution
    of futures through the consumption of fuel. When fuel runs out a
    future's execution is yielded back to the caller, and then upon
    resumption fuel is re-injected. The goal of this is to allow cooperative
    multi-tasking with futures.
    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    d94648c View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    06974ac View commit details
    Browse the repository at this point in the history
  8. Save/restore the frame pointer in fiber switching

    Turns out this is another caller-saved register!
    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    0e243da View commit details
    Browse the repository at this point in the history
  9. Simplify x86_64 fiber asm

    Take a leaf out of aarch64's playbook and don't have extra memory to
    load/store these arguments, instead leverage how `wasmtime_fiber_switch`
    already loads a bunch of data into registers which we can then
    immediately start using on a fiber's start without any extra memory
    accesses.
    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    f6a211b View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    5996126 View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    44c73e8 View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    d837e47 View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    625e9c9 View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    4951df8 View commit details
    Browse the repository at this point in the history
  15. Configuration menu
    Copy the full SHA
    5026144 View commit details
    Browse the repository at this point in the history
  16. Tweak async fuel to eventually run out.

    With fuel it's probably best to not provide any way to inject infinite
    fuel.
    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    ca4c492 View commit details
    Browse the repository at this point in the history
  17. Fix some typos

    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    0aa739a View commit details
    Browse the repository at this point in the history
  18. Cleanup asm a bit

    * Use a shared header file to deduplicate some directives
    * Guarantee hidden visibility for functions
    * Enable gc-sections on macOS x86_64
    * Add `.type` annotations for ARM
    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    77a2f7e View commit details
    Browse the repository at this point in the history
  19. Update lock file

    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    a8d73f0 View commit details
    Browse the repository at this point in the history
  20. Fix compile error

    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    663a3f7 View commit details
    Browse the repository at this point in the history
  21. Review comments

    alexcrichton committed Feb 26, 2021
    Configuration menu
    Copy the full SHA
    807a226 View commit details
    Browse the repository at this point in the history