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

Refactor module instantiation in the runtime. #2454

Closed
wants to merge 24 commits into from

Commits on Feb 4, 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 authored and peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    c484c1f View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    3d8f26a View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    8e78482 View commit details
    Browse the repository at this point in the history
  4. Fix a typo

    alexcrichton authored and peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    069d02b View commit details
    Browse the repository at this point in the history
  5. Update lock file

    alexcrichton authored and peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    9260750 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 authored and peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    5797a3f View commit details
    Browse the repository at this point in the history
  7. Fix compile without async

    alexcrichton authored and peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    24faf2f 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 authored and peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    ead9567 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 authored and peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    0108943 View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    f21b479 View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    4468167 View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    bd3c2f1 View commit details
    Browse the repository at this point in the history
  13. Use CreateFiberEx on Windows

    alexcrichton authored and peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    a96b0a9 View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    f421321 View commit details
    Browse the repository at this point in the history
  15. Configuration menu
    Copy the full SHA
    f68f9a2 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 authored and peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    fd5a7da View commit details
    Browse the repository at this point in the history
  17. Refactor module instantiation in the runtime.

    This commit refactors module instantiation in the runtime to allow for
    different instance allocation strategy implementations.
    
    It adds an `InstanceAllocator` trait with the current implementation put behind
    the `OnDemandInstanceAllocator` struct.
    
    The Wasmtime API has been updated to allow a `Config` to have an instance
    allocation strategy set which will determine how instances get allocated.
    
    This change is in preparation for an alternative *pooling* instance allocator
    that can reserve all needed host process address space in advance.
    
    This commit also makes changes to the `wasmtime_environ` crate to represent
    compiled modules in a way that reduces copying at instantiation time.
    peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    32a3ff7 View commit details
    Browse the repository at this point in the history
  18. Allow instance allocators control over module compilation.

    This commit introduces two new methods on `InstanceAllocator`:
    
    * `validate_module` - this method is used to validate a module after
      translation but before compilation. It will be used for the upcoming pooling
      allocator to ensure a module being compiled adheres to the limits of the
      allocator.
    
    * `adjust_tunables` - this method is used to adjust the `Tunables` given the
      JIT compiler.  The pooling allocator will use this to force all memories to
      be static during compilation.
    peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    9bbd982 View commit details
    Browse the repository at this point in the history
  19. Refactor runtime Table to support static storage.

    This commit refactors `Table` in the runtime such that it can be created from a
    pointer to existing table data.
    
    The current `Vec` backing of the `Table` is considered to be "dynamic" storage.
    
    This will be used for the upcoming pooling allocator where table memory is
    managed externally to the instance.
    
    The `table.copy` implementation was improved to use slice primitives for doing
    the copying.
    
    Fixes bytecodealliance#983.
    peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    2980ed6 View commit details
    Browse the repository at this point in the history
  20. Store memories and tables on Instance as PrimaryMap.

    This commit changes how memories and tables are stored in `Instance`.
    
    Previously, the memories and tables were stored as a `BoxedSlice`. Storing it
    this way requires an allocation to change the length of the memories and
    tables, which is desirable for a pooling instance allocator that is reusing an
    `Instance` structure for a new instantiation.
    
    By storing it instead as `PrimaryMap`, the memories and tables can be resized
    without any allocations (the capacity of these maps will always be the
    configured limits of the pooling allocator).
    peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    a0704a2 View commit details
    Browse the repository at this point in the history
  21. Change how Instance stores instantiated memories in the runtime.

    This commit changes `Instance` such that memories can be stored statically,
    with just a base pointer, size, maximum, and a callback to make memory
    accessible.
    
    Previously the memories were being stored as boxed trait objects, which would
    require the pooling allocator to do some unpleasant things to avoid
    allocations.
    
    With this change, the pooling allocator can simply define a memory for the
    instance without using a trait object.
    peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    3c63a49 View commit details
    Browse the repository at this point in the history
  22. Ensure default allocator is used for instance deallocation.

    Handles created with `create_handle` need to be deallocated with the default
    (on-demand) instance allocator.
    
    This commit changes Store such that handles can be added with a flag that is
    used to force deallocation via the default instance allocator when the Store is
    dropped.
    peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    04f814b View commit details
    Browse the repository at this point in the history
  23. Only treat a memory as static when the minimum is also within bounds.

    With the change to artificially limit unbounded memories based on Tunables,
    it's possible to hit the assert where the minimum might exceed the static
    memory bound.
    
    This commit removes the assert in favor of a check to see if the minimum also
    fits within the static memory bound. It also corrects the maximum bounding to
    ensure the minimum between the memory's maximum and the configured maximum is
    used.
    
    If it does not fit, the memory will be treated as dynamic.  In the case of the
    pooling instance allocator, the bounds will be checked again during translation
    and an appropriate error will be returned as dynamic memories are not supported
    for that allocator.
    peterhuene committed Feb 4, 2021
    Configuration menu
    Copy the full SHA
    84e3bcd View commit details
    Browse the repository at this point in the history

Commits on Feb 5, 2021

  1. Implement allocating fiber stacks for an instance allocator.

    This commit implements allocating fiber stacks in an instance allocator.
    
    The on-demand instance allocator doesn't support custom stacks, so the
    implementation will use the allocation from `wasmtime-fiber` for the fiber
    stacks.
    
    In the future, the pooling instance allocator will return custom stacks to use
    on Linux and macOS.
    
    On Windows, the native fiber implementation will always be used.
    peterhuene committed Feb 5, 2021
    Configuration menu
    Copy the full SHA
    3107349 View commit details
    Browse the repository at this point in the history