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
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(
&self,
store: &impl AsStoreRef,

View File

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

View File

@@ -56,13 +56,6 @@ pub fn thread_spawn<M: MemorySize>(
let thread_id: Tid = thread_handle.id().into();
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();
// 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
let start_ptr_offset = start_ptr.offset();
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");
return Errno::Notcapable;
}
let thread_memory = Memory::new_from_existing(&mut store, thread_memory);
let spawn_type = crate::runtime::SpawnType::NewThread(thread_memory);
// Now spawn a thread