wasmer_vm/
table.rs

1// This file contains code from external sources.
2// Attributions: https://github.com/wasmerio/wasmer/blob/main/docs/ATTRIBUTIONS.md
3
4//! Memory management for tables.
5//!
6//! `Table` is to WebAssembly tables what `Memory` is to WebAssembly linear memories.
7
8use crate::store::MaybeInstanceOwned;
9use crate::vmcontext::VMTableDefinition;
10use crate::Trap;
11use crate::VMExternRef;
12use crate::VMFuncRef;
13use std::cell::UnsafeCell;
14use std::convert::TryFrom;
15use std::fmt;
16use std::ptr::NonNull;
17use wasmer_types::TableStyle;
18use wasmer_types::{TableType, TrapCode, Type as ValType};
19
20/// A reference stored in a table. Can be either an externref or a funcref.
21#[derive(Debug, Clone)]
22pub enum TableElement {
23    /// Opaque pointer to arbitrary hostdata.
24    ExternRef(Option<VMExternRef>),
25    /// Pointer to function: contains enough information to call it.
26    FuncRef(Option<VMFuncRef>),
27}
28
29impl From<TableElement> for RawTableElement {
30    fn from(other: TableElement) -> Self {
31        match other {
32            TableElement::ExternRef(extern_ref) => Self { extern_ref },
33            TableElement::FuncRef(func_ref) => Self { func_ref },
34        }
35    }
36}
37
38#[repr(C)]
39#[derive(Clone, Copy)]
40pub union RawTableElement {
41    pub(crate) extern_ref: Option<VMExternRef>,
42    pub(crate) func_ref: Option<VMFuncRef>,
43}
44
45#[cfg(test)]
46#[test]
47fn table_element_size_test() {
48    use std::mem::size_of;
49    assert_eq!(size_of::<RawTableElement>(), size_of::<VMExternRef>());
50    assert_eq!(size_of::<RawTableElement>(), size_of::<VMFuncRef>());
51}
52
53impl fmt::Debug for RawTableElement {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        f.debug_struct("RawTableElement").finish()
56    }
57}
58
59impl Default for RawTableElement {
60    fn default() -> Self {
61        Self { func_ref: None }
62    }
63}
64
65impl Default for TableElement {
66    fn default() -> Self {
67        Self::FuncRef(None)
68    }
69}
70
71/// A table instance.
72#[derive(Debug)]
73pub struct VMTable {
74    vec: Vec<RawTableElement>,
75    maximum: Option<u32>,
76    /// The WebAssembly table description.
77    table: TableType,
78    /// Our chosen implementation style.
79    style: TableStyle,
80    vm_table_definition: MaybeInstanceOwned<VMTableDefinition>,
81}
82
83impl VMTable {
84    /// Create a new linear table instance with specified minimum and maximum number of elements.
85    ///
86    /// This creates a `Table` with metadata owned by a VM, pointed to by
87    /// `vm_table_location`: this can be used to create a local table.
88    pub fn new(table: &TableType, style: &TableStyle) -> Result<Self, String> {
89        unsafe { Self::new_inner(table, style, None) }
90    }
91
92    /// Returns the size of the table
93    pub fn get_runtime_size(&self) -> u32 {
94        self.vec.len() as u32
95    }
96
97    /// Create a new linear table instance with specified minimum and maximum number of elements.
98    ///
99    /// This creates a `Table` with metadata owned by a VM, pointed to by
100    /// `vm_table_location`: this can be used to create a local table.
101    ///
102    /// # Safety
103    /// - `vm_table_location` must point to a valid location in VM memory.
104    pub unsafe fn from_definition(
105        table: &TableType,
106        style: &TableStyle,
107        vm_table_location: NonNull<VMTableDefinition>,
108    ) -> Result<Self, String> {
109        Self::new_inner(table, style, Some(vm_table_location))
110    }
111
112    /// Create a new `Table` with either self-owned or VM owned metadata.
113    unsafe fn new_inner(
114        table: &TableType,
115        style: &TableStyle,
116        vm_table_location: Option<NonNull<VMTableDefinition>>,
117    ) -> Result<Self, String> {
118        match table.ty {
119            ValType::FuncRef | ValType::ExternRef => (),
120            ty => {
121                return Err(format!(
122                    "tables of types other than funcref or externref ({})",
123                    ty
124                ))
125            }
126        };
127        if let Some(max) = table.maximum {
128            if max < table.minimum {
129                return Err(format!(
130                    "Table minimum ({}) is larger than maximum ({})!",
131                    table.minimum, max
132                ));
133            }
134        }
135        let table_minimum = usize::try_from(table.minimum)
136            .map_err(|_| "Table minimum is bigger than usize".to_string())?;
137        let mut vec = vec![RawTableElement::default(); table_minimum];
138        let base = vec.as_mut_ptr();
139        match style {
140            TableStyle::CallerChecksSignature => Ok(Self {
141                vec,
142                maximum: table.maximum,
143                table: *table,
144                style: style.clone(),
145                vm_table_definition: if let Some(table_loc) = vm_table_location {
146                    {
147                        let mut ptr = table_loc;
148                        let td = ptr.as_mut();
149                        td.base = base as _;
150                        td.current_elements = table_minimum as _;
151                    }
152                    MaybeInstanceOwned::Instance(table_loc)
153                } else {
154                    MaybeInstanceOwned::Host(Box::new(UnsafeCell::new(VMTableDefinition {
155                        base: base as _,
156                        current_elements: table_minimum as _,
157                    })))
158                },
159            }),
160        }
161    }
162
163    /// Get the `VMTableDefinition`.
164    fn get_vm_table_definition(&self) -> NonNull<VMTableDefinition> {
165        self.vm_table_definition.as_ptr()
166    }
167
168    /// Returns the type for this Table.
169    pub fn ty(&self) -> &TableType {
170        &self.table
171    }
172
173    /// Returns the style for this Table.
174    pub fn style(&self) -> &TableStyle {
175        &self.style
176    }
177
178    /// Returns the number of allocated elements.
179    pub fn size(&self) -> u32 {
180        // TODO: investigate this function for race conditions
181        unsafe {
182            let td_ptr = self.get_vm_table_definition();
183            let td = td_ptr.as_ref();
184            td.current_elements
185        }
186    }
187
188    /// Grow table by the specified amount of elements.
189    ///
190    /// Returns `None` if table can't be grown by the specified amount
191    /// of elements, otherwise returns the previous size of the table.
192    pub fn grow(&mut self, delta: u32, init_value: TableElement) -> Option<u32> {
193        let size = self.size();
194        let new_len = size.checked_add(delta)?;
195        if self.maximum.map_or(false, |max| new_len > max) {
196            return None;
197        }
198        if new_len == size {
199            debug_assert_eq!(delta, 0);
200            return Some(size);
201        }
202
203        self.vec
204            .resize(usize::try_from(new_len).unwrap(), init_value.into());
205
206        // update table definition
207        unsafe {
208            let mut td_ptr = self.get_vm_table_definition();
209            let td = td_ptr.as_mut();
210            td.current_elements = new_len;
211            td.base = self.vec.as_mut_ptr() as _;
212        }
213        Some(size)
214    }
215
216    /// Get reference to the specified element.
217    ///
218    /// Returns `None` if the index is out of bounds.
219    pub fn get(&self, index: u32) -> Option<TableElement> {
220        let raw_data = self.vec.get(index as usize).cloned()?;
221        Some(match self.table.ty {
222            ValType::ExternRef => TableElement::ExternRef(unsafe { raw_data.extern_ref }),
223            ValType::FuncRef => TableElement::FuncRef(unsafe { raw_data.func_ref }),
224            _ => todo!("getting invalid type from table, handle this error"),
225        })
226    }
227
228    /// Set reference to the specified element.
229    ///
230    /// # Errors
231    ///
232    /// Returns an error if the index is out of bounds.
233    pub fn set(&mut self, index: u32, reference: TableElement) -> Result<(), Trap> {
234        match self.vec.get_mut(index as usize) {
235            Some(slot) => {
236                match (self.table.ty, reference) {
237                    (ValType::ExternRef, r @ TableElement::ExternRef(_)) => {
238                        *slot = r.into();
239                    }
240                    (ValType::FuncRef, r @ TableElement::FuncRef(_)) => {
241                        *slot = r.into();
242                    }
243                    // This path should never be hit by the generated code due to Wasm
244                    // validation.
245                    (ty, v) => {
246                        panic!(
247                            "Attempted to set a table of type {} with the value {:?}",
248                            ty, v
249                        )
250                    }
251                };
252
253                Ok(())
254            }
255            None => Err(Trap::lib(TrapCode::TableAccessOutOfBounds)),
256        }
257    }
258
259    /// Return a `VMTableDefinition` for exposing the table to compiled wasm code.
260    pub fn vmtable(&self) -> NonNull<VMTableDefinition> {
261        self.get_vm_table_definition()
262    }
263
264    /// Copy `len` elements from `src_table[src_index..]` into `dst_table[dst_index..]`.
265    ///
266    /// # Errors
267    ///
268    /// Returns an error if the range is out of bounds of either the source or
269    /// destination tables.
270    pub fn copy(
271        &mut self,
272        src_table: &Self,
273        dst_index: u32,
274        src_index: u32,
275        len: u32,
276    ) -> Result<(), Trap> {
277        // https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-table-copy
278
279        if src_index
280            .checked_add(len)
281            .map_or(true, |n| n > src_table.size())
282        {
283            return Err(Trap::lib(TrapCode::TableAccessOutOfBounds));
284        }
285
286        if dst_index.checked_add(len).map_or(true, |m| m > self.size()) {
287            return Err(Trap::lib(TrapCode::TableAccessOutOfBounds));
288        }
289
290        let srcs = src_index..src_index + len;
291        let dsts = dst_index..dst_index + len;
292
293        // Note on the unwraps: the bounds check above means that these will
294        // never panic.
295        //
296        // TODO: investigate replacing this get/set loop with a `memcpy`.
297        if dst_index <= src_index {
298            for (s, d) in (srcs).zip(dsts) {
299                self.set(d, src_table.get(s).unwrap())?;
300            }
301        } else {
302            for (s, d) in srcs.rev().zip(dsts.rev()) {
303                self.set(d, src_table.get(s).unwrap())?;
304            }
305        }
306
307        Ok(())
308    }
309
310    /// Copies the table into a new table
311    pub fn copy_on_write(&self) -> Result<Self, String> {
312        let mut ret = Self::new(&self.table, &self.style)?;
313        ret.copy(self, 0, 0, self.size())
314            .map_err(|trap| format!("failed to copy the table - {:?}", trap))?;
315        Ok(ret)
316    }
317
318    /// Copy `len` elements from `table[src_index..]` to `table[dst_index..]`.
319    ///
320    /// # Errors
321    ///
322    /// Returns an error if the range is out of bounds of either the source or
323    /// destination tables.
324    pub fn copy_within(&mut self, dst_index: u32, src_index: u32, len: u32) -> Result<(), Trap> {
325        // https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-table-copy
326
327        if src_index.checked_add(len).map_or(true, |n| n > self.size()) {
328            return Err(Trap::lib(TrapCode::TableAccessOutOfBounds));
329        }
330
331        if dst_index.checked_add(len).map_or(true, |m| m > self.size()) {
332            return Err(Trap::lib(TrapCode::TableAccessOutOfBounds));
333        }
334
335        let srcs = src_index..src_index + len;
336        let dsts = dst_index..dst_index + len;
337
338        // Note on the unwraps: the bounds check above means that these will
339        // never panic.
340        //
341        // TODO: investigate replacing this get/set loop with a `memcpy`.
342        if dst_index <= src_index {
343            for (s, d) in (srcs).zip(dsts) {
344                self.set(d, self.get(s).unwrap())?;
345            }
346        } else {
347            for (s, d) in srcs.rev().zip(dsts.rev()) {
348                self.set(d, self.get(s).unwrap())?;
349            }
350        }
351
352        Ok(())
353    }
354}