Skip to main content

wasmtime/runtime/vm/memory/
shared_memory.rs

1use crate::Engine;
2use crate::prelude::*;
3use crate::runtime::vm::memory::{LocalMemory, MmapMemory, validate_atomic_addr};
4use crate::runtime::vm::parking_spot::{ParkingSpot, Waiter};
5use crate::runtime::vm::{self, Memory, VMMemoryDefinition, WaitResult};
6use std::cell::RefCell;
7use std::ops::Range;
8use std::ptr::NonNull;
9use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
10use std::sync::{Arc, RwLock};
11use std::time::{Duration, Instant};
12use wasmtime_environ::Trap;
13
14/// For shared memory (and only for shared memory), this lock-version restricts
15/// access when growing the memory or checking its size. This is to conform with
16/// the [thread proposal]: "When `IsSharedArrayBuffer(...)` is true, the return
17/// value should be the result of an atomic read-modify-write of the new size to
18/// the internal `length` slot."
19///
20/// [thread proposal]:
21///     https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md#webassemblymemoryprototypegrow
22#[derive(Clone)]
23pub struct SharedMemory(Arc<SharedMemoryInner>);
24
25struct SharedMemoryInner {
26    memory: RwLock<LocalMemory>,
27    spot: ParkingSpot,
28    ty: wasmtime_environ::Memory,
29    def: LongTermVMMemoryDefinition,
30}
31
32impl SharedMemory {
33    /// Construct a new [`SharedMemory`].
34    pub fn new(engine: &Engine, ty: &wasmtime_environ::Memory) -> Result<Self> {
35        let tunables = engine.tunables();
36        let memory_tunables = wasmtime_environ::MemoryTunables::new(
37            tunables,
38            wasmtime_environ::MemoryKind::LinearMemory,
39        );
40        // Note that without a limiter being passed to `limit_new` this
41        // `assert_ready` should never panic.
42        let (minimum_bytes, maximum_bytes) = vm::assert_ready(Memory::limit_new(ty, None))?;
43        let mmap_memory = MmapMemory::new(ty, &memory_tunables, minimum_bytes, maximum_bytes)?;
44        let boxed: Box<dyn crate::LinearMemory> = try_new::<Box<_>>(mmap_memory)?;
45        Self::wrap(
46            engine,
47            ty,
48            LocalMemory::new(ty, &memory_tunables, boxed, None)?,
49        )
50    }
51
52    /// Wrap an existing [Memory] with the locking provided by a [SharedMemory].
53    pub fn wrap(
54        engine: &Engine,
55        ty: &wasmtime_environ::Memory,
56        memory: LocalMemory,
57    ) -> Result<Self> {
58        if !engine.config().shared_memory {
59            bail!(
60                "shared memory support is disabled for this engine -- see `Config::shared_memory`"
61            );
62        }
63        if !ty.shared {
64            bail!("shared memory must have a `shared` memory type");
65        }
66        Ok(Self(try_new::<Arc<_>>(SharedMemoryInner {
67            ty: *ty,
68            spot: ParkingSpot::default(),
69            def: LongTermVMMemoryDefinition(memory.vmmemory()),
70            memory: RwLock::new(memory),
71        })?))
72    }
73
74    /// Return the memory type for this [`SharedMemory`].
75    pub fn ty(&self) -> &wasmtime_environ::Memory {
76        &self.0.ty
77    }
78
79    /// Convert this shared memory into a [`Memory`].
80    pub fn as_memory(self) -> Memory {
81        Memory::Shared(self)
82    }
83
84    /// Return a pointer to the shared memory's [VMMemoryDefinition].
85    pub fn vmmemory_ptr(&self) -> NonNull<VMMemoryDefinition> {
86        NonNull::from(&self.0.def.0)
87    }
88
89    /// Same as `LocalMemory::grow`, except with `&self`.
90    pub fn grow(&self, delta_pages: u64) -> Result<Option<(usize, usize)>, Error> {
91        let mut memory = self.0.memory.write().unwrap();
92        // Without a limiter being passed in this shouldn't have an await point,
93        // so it should be safe to assert that it's ready.
94        let result = vm::assert_ready(memory.grow(delta_pages, None))?;
95        if let Some((_old_size_in_bytes, new_size_in_bytes)) = result {
96            // Store the new size to the `VMMemoryDefinition` for JIT-generated
97            // code (and runtime functions) to access. No other code can be
98            // growing this memory due to the write lock, but code in other
99            // threads could have access to this shared memory and we want them
100            // to see the most consistent version of the `current_length`; a
101            // weaker consistency is possible if we accept them seeing an older,
102            // smaller memory size (assumption: memory only grows) but presently
103            // we are aiming for accuracy.
104            //
105            // Note that it could be possible to access a memory address that is
106            // now-valid due to changes to the page flags in `grow` above but
107            // beyond the `memory.size` that we are about to assign to. In these
108            // and similar cases, discussion in the thread proposal concluded
109            // that: "multiple accesses in one thread racing with another
110            // thread's `memory.grow` that are in-bounds only after the grow
111            // commits may independently succeed or trap" (see
112            // https://github.com/WebAssembly/threads/issues/26#issuecomment-433930711).
113            // In other words, some non-determinism is acceptable when using
114            // `memory.size` on work being done by `memory.grow`.
115            self.0
116                .def
117                .0
118                .current_length
119                .store(new_size_in_bytes, Ordering::SeqCst);
120        }
121        Ok(result)
122    }
123
124    /// Implementation of `memory.atomic.notify` for this shared memory.
125    pub fn atomic_notify(&self, addr_index: u64, count: u32) -> Result<u32, Trap> {
126        let ptr = validate_atomic_addr(&self.0.def.0, addr_index, 4, 4)?;
127        log::trace!("memory.atomic.notify(addr={addr_index:#x}, count={count})");
128        let ptr = unsafe { &*ptr };
129        Ok(self.0.spot.notify(ptr, count))
130    }
131
132    /// Implementation of `memory.atomic.wait32` for this shared memory.
133    pub fn atomic_wait32(
134        &self,
135        addr_index: u64,
136        expected: u32,
137        timeout: Option<Duration>,
138    ) -> Result<WaitResult, Trap> {
139        let addr = validate_atomic_addr(&self.0.def.0, addr_index, 4, 4)?;
140        log::trace!(
141            "memory.atomic.wait32(addr={addr_index:#x}, expected={expected}, timeout={timeout:?})"
142        );
143
144        // SAFETY: `addr_index` was validated by `validate_atomic_addr` above.
145        assert!(std::mem::size_of::<AtomicU32>() == 4);
146        assert!(std::mem::align_of::<AtomicU32>() <= 4);
147        let atomic = unsafe { AtomicU32::from_ptr(addr.cast()) };
148        // Wasm linear memory is always little-endian, but `AtomicU32` uses the
149        // host's native endianness.
150        let expected = expected.to_le();
151
152        // Note that `checked_add` is used such that when `timeout` is too large
153        // it'll cause there to be no timeout at all if we can't represent the
154        // deadline. That effectively maps to the requested timeout since if we
155        // can't represent the deadline we'll be here awhile.
156        let deadline = timeout.and_then(|d| Instant::now().checked_add(d));
157
158        WAITER.with(|waiter| {
159            let mut waiter = waiter.borrow_mut();
160            Ok(self.0.spot.wait32(atomic, expected, deadline, &mut waiter))
161        })
162    }
163
164    /// Implementation of `memory.atomic.wait64` for this shared memory.
165    pub fn atomic_wait64(
166        &self,
167        addr_index: u64,
168        expected: u64,
169        timeout: Option<Duration>,
170    ) -> Result<WaitResult, Trap> {
171        let addr = validate_atomic_addr(&self.0.def.0, addr_index, 8, 8)?;
172        log::trace!(
173            "memory.atomic.wait64(addr={addr_index:#x}, expected={expected}, timeout={timeout:?})"
174        );
175
176        // SAFETY: `addr_index` was validated by `validate_atomic_addr` above.
177        assert!(std::mem::size_of::<AtomicU64>() == 8);
178        assert!(std::mem::align_of::<AtomicU64>() <= 8);
179        let atomic = unsafe { AtomicU64::from_ptr(addr.cast()) };
180        // Wasm linear memory is always little-endian, but `AtomicU64` uses the
181        // host's native endianness.
182        let expected = expected.to_le();
183
184        // See `atomic_wait32` for why this is using `checked_add`.
185        let deadline = timeout.and_then(|d| Instant::now().checked_add(d));
186
187        WAITER.with(|waiter| {
188            let mut waiter = waiter.borrow_mut();
189            Ok(self.0.spot.wait64(atomic, expected, deadline, &mut waiter))
190        })
191    }
192
193    pub(crate) fn byte_size(&self) -> usize {
194        self.0.memory.read().unwrap().byte_size()
195    }
196
197    pub(crate) fn needs_init(&self) -> bool {
198        self.0.memory.read().unwrap().needs_init()
199    }
200
201    pub(crate) fn wasm_accessible(&self) -> Range<usize> {
202        self.0.memory.read().unwrap().wasm_accessible()
203    }
204}
205
206thread_local! {
207    /// Structure used in conjunction with `ParkingSpot` to block the current
208    /// thread if necessary. Note that this is lazily initialized.
209    static WAITER: RefCell<Waiter> = const { RefCell::new(Waiter::new()) };
210}
211
212/// Shared memory needs some representation of a `VMMemoryDefinition` for
213/// JIT-generated code to access. This structure owns the base pointer and
214/// length to the actual memory and we share this definition across threads by:
215/// - never changing the base pointer; according to the specification, shared
216///   memory must be created with a known maximum size so it can be allocated
217///   once and never moved
218/// - carefully changing the length, using atomic accesses in both the runtime
219///   and JIT-generated code.
220struct LongTermVMMemoryDefinition(VMMemoryDefinition);
221unsafe impl Send for LongTermVMMemoryDefinition {}
222unsafe impl Sync for LongTermVMMemoryDefinition {}