wasmtime/trampoline/
memory.rs

1use crate::memory::{LinearMemory, MemoryCreator};
2use crate::module::BareModuleInfo;
3use crate::store::{InstanceId, StoreOpaque};
4use crate::MemoryType;
5use anyhow::{anyhow, Result};
6use std::convert::TryFrom;
7use std::ops::Range;
8use std::sync::Arc;
9use wasmtime_environ::{
10    DefinedMemoryIndex, DefinedTableIndex, EntityIndex, MemoryPlan, MemoryStyle, Module,
11    PrimaryMap, WASM_PAGE_SIZE,
12};
13use wasmtime_runtime::{
14    CompiledModuleId, Imports, InstanceAllocationRequest, InstanceAllocator, Memory, MemoryImage,
15    OnDemandInstanceAllocator, RuntimeLinearMemory, RuntimeMemoryCreator, SharedMemory, StorePtr,
16    Table, VMMemoryDefinition,
17};
18
19/// Create a "frankenstein" instance with a single memory.
20///
21/// This separate instance is necessary because Wasm objects in Wasmtime must be
22/// attached to instances (versus the store, e.g.) and some objects exist
23/// outside: a host-provided memory import, shared memory.
24pub fn create_memory(
25    store: &mut StoreOpaque,
26    memory_ty: &MemoryType,
27    preallocation: Option<&SharedMemory>,
28) -> Result<InstanceId> {
29    let mut module = Module::new();
30
31    // Create a memory plan for the memory, though it will never be used for
32    // constructing a memory with an allocator: instead the memories are either
33    // preallocated (i.e., shared memory) or allocated manually below.
34    let plan = wasmtime_environ::MemoryPlan::for_memory(
35        memory_ty.wasmtime_memory().clone(),
36        &store.engine().config().tunables,
37    );
38    let memory_id = module.memory_plans.push(plan.clone());
39
40    // Since we have only associated a single memory with the "frankenstein"
41    // instance, it will be exported at index 0.
42    debug_assert_eq!(memory_id.as_u32(), 0);
43    module
44        .exports
45        .insert(String::new(), EntityIndex::Memory(memory_id));
46
47    // We create an instance in the on-demand allocator when creating handles
48    // associated with external objects. The configured instance allocator
49    // should only be used when creating module instances as we don't want host
50    // objects to count towards instance limits.
51    let runtime_info = &BareModuleInfo::maybe_imported_func(Arc::new(module), None).into_traitobj();
52    let host_state = Box::new(());
53    let imports = Imports::default();
54    let request = InstanceAllocationRequest {
55        imports,
56        host_state,
57        store: StorePtr::new(store.traitobj()),
58        runtime_info,
59    };
60
61    unsafe {
62        let handle = SingleMemoryInstance {
63            preallocation,
64            ondemand: OnDemandInstanceAllocator::default(),
65        }
66        .allocate(request)?;
67        let instance_id = store.add_instance(handle.clone(), true);
68        Ok(instance_id)
69    }
70}
71
72struct LinearMemoryProxy {
73    mem: Box<dyn LinearMemory>,
74}
75
76impl RuntimeLinearMemory for LinearMemoryProxy {
77    fn byte_size(&self) -> usize {
78        self.mem.byte_size()
79    }
80
81    fn maximum_byte_size(&self) -> Option<usize> {
82        self.mem.maximum_byte_size()
83    }
84
85    fn grow_to(&mut self, new_size: usize) -> Result<()> {
86        self.mem.grow_to(new_size)
87    }
88
89    fn vmmemory(&mut self) -> VMMemoryDefinition {
90        VMMemoryDefinition {
91            base: self.mem.as_ptr(),
92            current_length: self.mem.byte_size().into(),
93        }
94    }
95
96    fn needs_init(&self) -> bool {
97        true
98    }
99
100    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
101        self
102    }
103
104    fn wasm_accessible(&self) -> Range<usize> {
105        self.mem.wasm_accessible()
106    }
107}
108
109#[derive(Clone)]
110pub(crate) struct MemoryCreatorProxy(pub Arc<dyn MemoryCreator>);
111
112impl RuntimeMemoryCreator for MemoryCreatorProxy {
113    fn new_memory(
114        &self,
115        plan: &MemoryPlan,
116        minimum: usize,
117        maximum: Option<usize>,
118        _: Option<&Arc<MemoryImage>>,
119    ) -> Result<Box<dyn RuntimeLinearMemory>> {
120        let ty = MemoryType::from_wasmtime_memory(&plan.memory);
121        let reserved_size_in_bytes = match plan.style {
122            MemoryStyle::Static { bound } => {
123                Some(usize::try_from(bound * (WASM_PAGE_SIZE as u64)).unwrap())
124            }
125            MemoryStyle::Dynamic { .. } => None,
126        };
127        self.0
128            .new_memory(
129                ty,
130                minimum,
131                maximum,
132                reserved_size_in_bytes,
133                usize::try_from(plan.offset_guard_size).unwrap(),
134            )
135            .map(|mem| Box::new(LinearMemoryProxy { mem }) as Box<dyn RuntimeLinearMemory>)
136            .map_err(|e| anyhow!(e))
137    }
138}
139
140struct SingleMemoryInstance<'a> {
141    preallocation: Option<&'a SharedMemory>,
142    ondemand: OnDemandInstanceAllocator,
143}
144
145unsafe impl InstanceAllocator for SingleMemoryInstance<'_> {
146    fn allocate_index(&self, req: &InstanceAllocationRequest) -> Result<usize> {
147        self.ondemand.allocate_index(req)
148    }
149
150    fn deallocate_index(&self, index: usize) {
151        self.ondemand.deallocate_index(index)
152    }
153
154    fn allocate_memories(
155        &self,
156        index: usize,
157        req: &mut InstanceAllocationRequest,
158        mem: &mut PrimaryMap<DefinedMemoryIndex, Memory>,
159    ) -> Result<()> {
160        assert_eq!(req.runtime_info.module().memory_plans.len(), 1);
161        match self.preallocation {
162            Some(shared_memory) => {
163                mem.push(shared_memory.clone().as_memory());
164            }
165            None => {
166                self.ondemand.allocate_memories(index, req, mem)?;
167            }
168        }
169        Ok(())
170    }
171
172    fn deallocate_memories(&self, index: usize, mems: &mut PrimaryMap<DefinedMemoryIndex, Memory>) {
173        self.ondemand.deallocate_memories(index, mems)
174    }
175
176    fn allocate_tables(
177        &self,
178        index: usize,
179        req: &mut InstanceAllocationRequest,
180        tables: &mut PrimaryMap<DefinedTableIndex, Table>,
181    ) -> Result<()> {
182        self.ondemand.allocate_tables(index, req, tables)
183    }
184
185    fn deallocate_tables(&self, index: usize, tables: &mut PrimaryMap<DefinedTableIndex, Table>) {
186        self.ondemand.deallocate_tables(index, tables)
187    }
188
189    #[cfg(feature = "async")]
190    fn allocate_fiber_stack(&self) -> Result<wasmtime_fiber::FiberStack> {
191        unreachable!()
192    }
193
194    #[cfg(feature = "async")]
195    unsafe fn deallocate_fiber_stack(&self, _stack: &wasmtime_fiber::FiberStack) {
196        unreachable!()
197    }
198
199    fn purge_module(&self, _: CompiledModuleId) {
200        unreachable!()
201    }
202}