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

Add shared memories #4187

Merged
merged 47 commits into from
Jun 8, 2022
Merged

Commits on Jun 6, 2022

  1. Add shared memories

    This change adds the ability to use shared memories in Wasmtime when the
    [threads proposal] is enabled. Shared memories are annotated as `shared`
    in the WebAssembly syntax, e.g., `(memory 1 1 shared)`, and are
    protected from concurrent access during `memory.size` and `memory.grow`.
    
    [threads proposal]: https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md
    
    In order to implement this in Wasmtime, there are two main cases to
    cover:
        - a program may simply create a shared memory and possibly export it;
        this means that Wasmtime itself must be able to create shared
        memories
        - a user may create a shared memory externally and pass it in as an
        import during instantiation; this is the case when the program
        contains code like `(import "env" "memory" (memory 1 1
        shared))`--this case is handled by a new Wasmtime API
        type--`SharedMemory`
    
    Because of the first case, this change allows any of the current
    memory-creation mechanisms to work as-is. Wasmtime can still create
    either static or dynamic memories in either on-demand or pooling modes,
    and any of these memories can be considered shared. When shared, the
    `Memory` runtime container will lock appropriately during `memory.size`
    and `memory.grow` operations; since all memories use this container, it
    is an ideal place for implementing the locking once and once only.
    
    The second case is covered by the new `SharedMemory` structure. It uses
    the same `Mmap` allocation under the hood as non-shared memories, but
    allows the user to perform the allocation externally to Wasmtime and
    share the memory across threads (via an `Arc`). The pointer address to
    the actual memory is carefully wired through and owned by the
    `SharedMemory` structure itself. This means that there are differing
    views of where to access the pointer (i.e., `VMMemoryDefinition`): for
    owned memories (the default), the `VMMemoryDefinition` is stored
    directly by the `VMContext`; in the `SharedMemory` case, however, this
    `VMContext` must point to this separate structure.
    
    To ensure that the `VMContext` can always point to the correct
    `VMMemoryDefinition`, this change alters the `VMContext` structure.
    Since a `SharedMemory` owns its own `VMMemoryDefinition`, the
    `defined_memories` table in the `VMContext` becomes a sequence of
    pointers--in the shared memory case, they point to the
    `VMMemoryDefinition` owned by the `SharedMemory` and in the owned memory
    case (i.e., not shared) they point to `VMMemoryDefinition`s stored in a
    new table, `owned_memories`.
    
    This change adds an additional indirection (through the `*mut
    VMMemoryDefinition` pointer) that could add overhead. Using an imported
    memory as a proxy, we measured a 1-3% overhead of this approach on the
    `pulldown-cmark` benchmark. To avoid this, Cranelift-generated code will
    special-case the owned memory access (i.e., load a pointer directly to
    the `owned_memories` entry) for `memory.size` so that only
    shared memories (and imported memories, as before) incur the indirection
    cost.
    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    13113d8 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    8e88076 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    6281c86 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    9a6d871 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    234ea40 View commit details
    Browse the repository at this point in the history
  6. review: improve tests

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    681a627 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    41a5016 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    4cc2ddf View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    9cc4db0 View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    7a723e1 View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    04aaf4f View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    26c22f5 View commit details
    Browse the repository at this point in the history
  13. review: remove TODO

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    188cb65 View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    06dbb4d View commit details
    Browse the repository at this point in the history
  15. review: add limiter assertion

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    2f9d139 View commit details
    Browse the repository at this point in the history
  16. review: remove TODO

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    e236a24 View commit details
    Browse the repository at this point in the history
  17. review: improve tests

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    0b1f51b View commit details
    Browse the repository at this point in the history
  18. review: fix doc test

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    90d452e View commit details
    Browse the repository at this point in the history
  19. fix: fixes based on discussion with Alex

    This changes several key parts:
     - adds memory indexes to imports and exports
     - makes `VMMemoryDefinition::current_length` an atomic usize
    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    7402dc1 View commit details
    Browse the repository at this point in the history
  20. Configuration menu
    Copy the full SHA
    f79b322 View commit details
    Browse the repository at this point in the history
  21. review: remove TODO

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    25f4a4e View commit details
    Browse the repository at this point in the history
  22. Configuration menu
    Copy the full SHA
    09f8d3d View commit details
    Browse the repository at this point in the history
  23. Configuration menu
    Copy the full SHA
    6669977 View commit details
    Browse the repository at this point in the history
  24. Configuration menu
    Copy the full SHA
    bee0ece View commit details
    Browse the repository at this point in the history
  25. fix: doc link

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    12420e4 View commit details
    Browse the repository at this point in the history
  26. fix: add TODOs to c-api

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    1be4ad0 View commit details
    Browse the repository at this point in the history
  27. fix: broken doc link

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    e44a58a View commit details
    Browse the repository at this point in the history
  28. Configuration menu
    Copy the full SHA
    14ce663 View commit details
    Browse the repository at this point in the history
  29. Configuration menu
    Copy the full SHA
    f087502 View commit details
    Browse the repository at this point in the history
  30. Configuration menu
    Copy the full SHA
    b8ef602 View commit details
    Browse the repository at this point in the history
  31. Configuration menu
    Copy the full SHA
    e37f881 View commit details
    Browse the repository at this point in the history
  32. Configuration menu
    Copy the full SHA
    87da77a View commit details
    Browse the repository at this point in the history
  33. review: remove TODO

    abrown committed Jun 6, 2022
    Configuration menu
    Copy the full SHA
    17f8961 View commit details
    Browse the repository at this point in the history
  34. Configuration menu
    Copy the full SHA
    8d981bf View commit details
    Browse the repository at this point in the history

Commits on Jun 7, 2022

  1. Configuration menu
    Copy the full SHA
    b154a16 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    e90c3ea View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    bd6e799 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    2bdaaed View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    19f3081 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    0a13a7d View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    75ddcd1 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    a96fccd View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    480a1e1 View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    be221c7 View commit details
    Browse the repository at this point in the history
  11. review: move code around

    abrown committed Jun 7, 2022
    Configuration menu
    Copy the full SHA
    6868519 View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    e8101a0 View commit details
    Browse the repository at this point in the history

Commits on Jun 8, 2022

  1. Configuration menu
    Copy the full SHA
    9e71b72 View commit details
    Browse the repository at this point in the history