Move all vm internals out

This commit is contained in:
Syrus Akbary
2023-04-08 00:37:48 -07:00
parent 8be88859be
commit 6c4ea497b9
3 changed files with 31 additions and 16 deletions

View File

@@ -143,6 +143,21 @@ impl Memory {
} }
/// Attempts to clone this memory (if its clonable) in a new store /// Attempts to clone this memory (if its clonable) in a new store
pub fn clone_in_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Option<Self> {
if !self.ty(store).shared {
// We should only be able to duplicate in a new store if the memory is shared
return None;
}
self.0
.try_clone(&store)
.map(|new_memory| Self::new_from_existing(new_store, new_memory.into()))
}
/// Attempts to duplicate this memory (if its clonable) in a new store
pub fn duplicate_in_store( pub fn duplicate_in_store(
&self, &self,
store: &impl AsStoreRef, store: &impl AsStoreRef,

View File

@@ -6,7 +6,7 @@ use std::{
use futures::Future; use futures::Future;
use tokio::runtime::Handle; use tokio::runtime::Handle;
use wasmer::{vm::VMSharedMemory, AsStoreMut, Memory, Module, Store, StoreMut}; use wasmer::{AsStoreMut, Memory, Module, Store, StoreMut};
use crate::os::task::thread::WasiThreadError; use crate::os::task::thread::WasiThreadError;
@@ -84,17 +84,14 @@ impl VirtualTaskManager for TokioTaskManager {
spawn_type: SpawnType, spawn_type: SpawnType,
) -> Result<Option<Memory>, WasiThreadError> { ) -> Result<Option<Memory>, WasiThreadError> {
match spawn_type { match spawn_type {
SpawnType::CreateWithType(mem) => { SpawnType::CreateWithType(mut mem) => {
let style = store.engine().tunables().memory_style(&mem.ty); mem.ty.shared = true;
VMSharedMemory::new(&mem.ty, &style) Memory::new(&mut store, mem.ty)
.map_err(|err| { .map_err(|err| {
tracing::error!("could not create memory: {err}"); tracing::error!("could not create memory: {err}");
WasiThreadError::MemoryCreateFailed WasiThreadError::MemoryCreateFailed
}) })
.map(|m| { .map(Some)
Some(m.into())
.map(|vm_memory| Memory::new_from_existing(&mut store, vm_memory))
})
} }
SpawnType::NewThread(mem) => Ok(Some(mem)), SpawnType::NewThread(mem) => Ok(Some(mem)),
SpawnType::Create => Ok(None), SpawnType::Create => Ok(None),

View File

@@ -56,13 +56,6 @@ pub fn thread_spawn<M: MemorySize>(
let thread_id: Tid = thread_handle.id().into(); let thread_id: Tid = thread_handle.id().into();
Span::current().record("tid", thread_id); Span::current().record("tid", thread_id);
// We need a copy of the process memory and a packaged store in order to
// launch threads and reactors
let thread_memory = wasi_try!(ctx.data().memory().try_clone(&ctx).ok_or_else(|| {
error!("failed - the memory could not be cloned");
Errno::Notcapable
}));
let mut store = ctx.data().runtime.new_store(); let mut store = ctx.data().runtime.new_store();
// This function takes in memory and a store and creates a context that // This function takes in memory and a store and creates a context that
@@ -107,6 +100,17 @@ pub fn thread_spawn<M: MemorySize>(
} }
}; };
// We need a copy of the process memory and a packaged store in order to
// launch threads and reactors
let thread_memory = wasi_try!(ctx
.data()
.memory()
.clone_in_store(&ctx, &mut store)
.ok_or_else(|| {
error!("failed - the memory could not be cloned");
Errno::Notcapable
}));
// This function calls into the module // This function calls into the module
let start_ptr_offset = start_ptr.offset(); let start_ptr_offset = start_ptr.offset();
let call_module = move |ctx: &WasiFunctionEnv, store: &mut Store| { let call_module = move |ctx: &WasiFunctionEnv, store: &mut Store| {
@@ -225,7 +229,6 @@ pub fn thread_spawn<M: MemorySize>(
warn!("thread failed - the program does not export a `wasi_thread_start` function"); warn!("thread failed - the program does not export a `wasi_thread_start` function");
return Errno::Notcapable; return Errno::Notcapable;
} }
let thread_memory = Memory::new_from_existing(&mut store, thread_memory);
let spawn_type = crate::runtime::SpawnType::NewThread(thread_memory); let spawn_type = crate::runtime::SpawnType::NewThread(thread_memory);
// Now spawn a thread // Now spawn a thread