WASIX Preparation

This commit extracts changes to core libraries made in the WASIX branch.

It is not reasonable to extract the partial commit history, so this is
just a batch commit.

The history will make a bit more sense again if we decide to merge the
WASIX branch with full commit history.
This commit is contained in:
Christoph Herzog
2022-12-15 12:14:21 +01:00
parent 47200450b3
commit 5109a407c4
81 changed files with 2008 additions and 500 deletions

View File

@@ -116,6 +116,27 @@ impl WasmMmap {
Ok(prev_pages)
}
/// Copies the memory
/// (in this case it performs a copy-on-write to save memory)
pub fn fork(&mut self) -> Result<Self, MemoryError> {
let mem_length = self.size.bytes().0;
let mut alloc = self
.alloc
.fork(Some(mem_length))
.map_err(MemoryError::Generic)?;
let base_ptr = alloc.as_mut_ptr();
Ok(Self {
vm_memory_definition: MaybeInstanceOwned::Host(Box::new(UnsafeCell::new(
VMMemoryDefinition {
base: base_ptr,
current_length: mem_length,
},
))),
alloc,
size: self.size,
})
}
}
/// A linear memory instance.
@@ -157,18 +178,6 @@ pub struct VMOwnedMemory {
unsafe impl Send for VMOwnedMemory {}
unsafe impl Sync for VMOwnedMemory {}
/// A shared linear memory instance.
#[derive(Debug, Clone)]
pub struct VMSharedMemory {
// The underlying allocation.
mmap: Arc<RwLock<WasmMmap>>,
// Configuration of this memory
config: VMMemoryConfig,
}
unsafe impl Send for VMSharedMemory {}
unsafe impl Sync for VMSharedMemory {}
impl VMOwnedMemory {
/// Create a new linear memory instance with specified minimum and maximum number of wasm pages.
///
@@ -270,9 +279,7 @@ impl VMOwnedMemory {
},
})
}
}
impl VMOwnedMemory {
/// Converts this owned memory into shared memory
pub fn to_shared(self) -> VMSharedMemory {
VMSharedMemory {
@@ -280,6 +287,14 @@ impl VMOwnedMemory {
config: self.config,
}
}
/// Copies this memory to a new memory
pub fn fork(&mut self) -> Result<Self, MemoryError> {
Ok(Self {
mmap: self.mmap.fork()?,
config: self.config.clone(),
})
}
}
impl LinearMemory for VMOwnedMemory {
@@ -316,8 +331,26 @@ impl LinearMemory for VMOwnedMemory {
fn try_clone(&self) -> Option<Box<dyn LinearMemory + 'static>> {
None
}
/// Copies this memory to a new memory
fn fork(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError> {
let forked = Self::fork(self)?;
Ok(Box::new(forked))
}
}
/// A shared linear memory instance.
#[derive(Debug, Clone)]
pub struct VMSharedMemory {
// The underlying allocation.
mmap: Arc<RwLock<WasmMmap>>,
// Configuration of this memory
config: VMMemoryConfig,
}
unsafe impl Send for VMSharedMemory {}
unsafe impl Sync for VMSharedMemory {}
impl VMSharedMemory {
/// Create a new linear memory instance with specified minimum and maximum number of wasm pages.
///
@@ -341,6 +374,15 @@ impl VMSharedMemory {
) -> Result<Self, MemoryError> {
Ok(VMOwnedMemory::from_definition(memory, style, vm_memory_location)?.to_shared())
}
/// Copies this memory to a new memory
pub fn fork(&mut self) -> Result<Self, MemoryError> {
let mut guard = self.mmap.write().unwrap();
Ok(Self {
mmap: Arc::new(RwLock::new(guard.fork()?)),
config: self.config.clone(),
})
}
}
impl LinearMemory for VMSharedMemory {
@@ -379,9 +421,15 @@ impl LinearMemory for VMSharedMemory {
guard.vm_memory_definition.as_ptr()
}
/// Owned memory can not be cloned (this will always return None)
/// Shared memory can always be cloned
fn try_clone(&self) -> Option<Box<dyn LinearMemory + 'static>> {
None
Some(Box::new(self.clone()))
}
/// Copies this memory to a new memory
fn fork(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError> {
let forked = Self::fork(self)?;
Ok(Box::new(forked))
}
}
@@ -445,6 +493,11 @@ impl LinearMemory for VMMemory {
unsafe fn initialize_with_data(&self, start: usize, data: &[u8]) -> Result<(), Trap> {
self.0.initialize_with_data(start, data)
}
/// Copies this memory to a new memory
fn fork(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError> {
self.0.fork()
}
}
impl VMMemory {
@@ -497,12 +550,17 @@ impl VMMemory {
/// are natively supported
/// - VMOwnedMemory -> VMMemory
/// - Box<dyn LinearMemory + 'static> -> VMMemory
pub fn from_custom<IntoVMMemory>(memory: IntoVMMemory) -> VMMemory
pub fn from_custom<IntoVMMemory>(memory: IntoVMMemory) -> Self
where
IntoVMMemory: Into<VMMemory>,
IntoVMMemory: Into<Self>,
{
memory.into()
}
/// Copies this memory to a new memory
pub fn fork(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError> {
LinearMemory::fork(self)
}
}
#[doc(hidden)]
@@ -555,4 +613,7 @@ where
initialize_memory_with_data(memory, start, data)
}
/// Copies this memory to a new memory
fn fork(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError>;
}