Skip to content

Commit

Permalink
Added a cleanup step that will mark all the memory as inaccessible an…
Browse files Browse the repository at this point in the history
…d thus force threads to exit
  • Loading branch information
john-sharratt committed Mar 17, 2023
1 parent fbd6fc4 commit 779bf3b
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 16 deletions.
8 changes: 8 additions & 0 deletions lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ impl Memory {
self.0.grow(store, delta)
}

/// Makes all the memory inaccessible to any reads or writes
pub fn make_inaccessible(
&self,
store: &impl AsStoreRef,
) -> Result<(), MemoryError> {
self.0.make_inaccessable(store)
}

/// Copies the memory to a new store and returns a memory reference to it
pub fn copy_to_store(
&self,
Expand Down
9 changes: 9 additions & 0 deletions lib/api/src/sys/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ impl Memory {
self.handle.get_mut(store.objects_mut()).grow(delta.into())
}

/// Makes all the memory inaccessible to any reads or writes
pub fn make_inaccessable(
&self,
store: &impl AsStoreRef
) -> Result<(), MemoryError>
{
self.handle.get(store.as_store_ref().objects()).make_inaccessible()
}

pub fn copy_to_store(
&self,
store: &impl AsStoreRef,
Expand Down
22 changes: 22 additions & 0 deletions lib/sys-utils/src/memory/fd_memory/fd_mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ impl FdMmap {
.map_err(|e| e.to_string())
}

/// Make the entire memory inaccessible to both reads and writes.
pub fn make_all_inaccessible(&self) -> Result<(), String> {
self.make_inaccessible(0, self.len)
}

/// Make the memory starting at `start` and extending for `len` bytes inaccessible
/// to both reads and writes.
/// `start` and `len` must be native page-size multiples and describe a range within
/// `self`'s reserved memory.
pub fn make_inaccessible(&self, start: usize, len: usize) -> Result<(), String> {
let page_size = region::page::size();
assert_eq!(start & (page_size - 1), 0);
assert_eq!(len & (page_size - 1), 0);
assert!(len <= self.len);
assert!(start <= self.len - len);

// Commit the accessible size.
let ptr = self.ptr as *const u8;
unsafe { region::protect(ptr.add(start), len, region::Protection::NONE) }
.map_err(|e| e.to_string())
}

/// Return the allocated memory as a slice of u8.
pub fn as_slice(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.ptr as *const u8, self.len) }
Expand Down
34 changes: 34 additions & 0 deletions lib/sys-utils/src/memory/fd_memory/memories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ impl WasmMmap {
size: self.size,
})
}

/// Makes all the memory inaccessible to reads and writes
pub fn make_inaccessible(&self) -> Result<(), MemoryError> {
self
.alloc
.make_all_inaccessible()
.map_err(MemoryError::Region)
}
}

/// A linear memory instance.
Expand Down Expand Up @@ -307,6 +315,11 @@ impl VMOwnedMemory {
config: self.config.clone(),
})
}

/// Makes all the memory inaccessible to reads and writes
fn make_inaccessible(&self) -> Result<(), MemoryError> {
self.mmap.make_inaccessible()
}
}

impl LinearMemory for VMOwnedMemory {
Expand Down Expand Up @@ -349,6 +362,11 @@ impl LinearMemory for VMOwnedMemory {
let forked = Self::duplicate(self)?;
Ok(Box::new(forked))
}

/// Makes all the memory inaccessible to reads and writes
fn make_inaccessible(&self) -> Result<(), MemoryError> {
Self::make_inaccessible(self)
}
}

/// A shared linear memory instance.
Expand Down Expand Up @@ -395,6 +413,12 @@ impl VMSharedMemory {
config: self.config.clone(),
})
}

/// Makes all the memory inaccessible to reads and writes
pub fn make_inaccessible(&self) -> Result<(), MemoryError> {
let guard = self.mmap.write().unwrap();
guard.make_inaccessible()
}
}

impl LinearMemory for VMSharedMemory {
Expand Down Expand Up @@ -443,6 +467,11 @@ impl LinearMemory for VMSharedMemory {
let forked = Self::duplicate(self)?;
Ok(Box::new(forked))
}

/// Makes all the memory inaccessible to reads and writes
fn make_inaccessible(&self) -> Result<(), MemoryError> {
Self::make_inaccessible(self)
}
}

impl From<VMOwnedMemory> for VMMemory {
Expand Down Expand Up @@ -510,6 +539,11 @@ impl LinearMemory for VMMemory {
fn duplicate(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError> {
self.0.duplicate()
}

/// Makes all the memory inaccessible to reads and writes
fn make_inaccessible(&self) -> Result<(), MemoryError> {
self.0.make_inaccessible()
}
}

impl VMMemory {
Expand Down
42 changes: 42 additions & 0 deletions lib/vm/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ impl WasmMmap {
size: self.size,
})
}

/// Makes all the memory inaccessible to reads and writes
pub fn make_inaccessible(&self) -> Result<(), MemoryError> {
self
.alloc
.make_all_inaccessible()
.map_err(MemoryError::Region)
}
}

/// A linear memory instance.
Expand Down Expand Up @@ -295,6 +303,11 @@ impl VMOwnedMemory {
config: self.config.clone(),
})
}

/// Makes all the memory inaccessible to reads and writes
pub fn make_inaccessible(&self) -> Result<(), MemoryError> {
self.mmap.make_inaccessible()
}
}

impl LinearMemory for VMOwnedMemory {
Expand Down Expand Up @@ -337,6 +350,11 @@ impl LinearMemory for VMOwnedMemory {
let forked = Self::duplicate(self)?;
Ok(Box::new(forked))
}

/// Makes all the memory inaccessible to reads and writes
fn make_inaccessible(&self) -> Result<(), MemoryError> {
Self::make_inaccessible(self)
}
}

/// A shared linear memory instance.
Expand Down Expand Up @@ -383,6 +401,12 @@ impl VMSharedMemory {
config: self.config.clone(),
})
}

/// Makes all the memory inaccessible to reads and writes
pub fn make_inaccessible(&self) -> Result<(), MemoryError> {
let guard = self.mmap.write().unwrap();
guard.make_inaccessible()
}
}

impl LinearMemory for VMSharedMemory {
Expand Down Expand Up @@ -431,6 +455,11 @@ impl LinearMemory for VMSharedMemory {
let forked = Self::duplicate(self)?;
Ok(Box::new(forked))
}

/// Makes all the memory inaccessible to reads and writes
fn make_inaccessible(&self) -> Result<(), MemoryError> {
Self::make_inaccessible(self)
}
}

impl From<VMOwnedMemory> for VMMemory {
Expand Down Expand Up @@ -498,6 +527,11 @@ impl LinearMemory for VMMemory {
fn duplicate(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError> {
self.0.duplicate()
}

/// Makes all the memory inaccessible to reads and writes
fn make_inaccessible(&self) -> Result<(), MemoryError> {
self.0.make_inaccessible()
}
}

impl VMMemory {
Expand Down Expand Up @@ -561,6 +595,11 @@ impl VMMemory {
pub fn duplicate(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError> {
LinearMemory::duplicate(self)
}

/// Makes all the memory inaccessible to reads and writes
pub fn make_inaccessible(&self) -> Result<(), MemoryError> {
LinearMemory::make_inaccessible(self)
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -616,4 +655,7 @@ where

/// Copies this memory to a new memory
fn duplicate(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError>;

/// Makes all the memory inaccessible to reads and writes
fn make_inaccessible(&self) -> Result<(), MemoryError>;
}
22 changes: 22 additions & 0 deletions lib/vm/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,28 @@ impl Mmap {
Ok(())
}

/// Make the entire memory inaccessible to both reads and writes.
pub fn make_all_inaccessible(&self) -> Result<(), String> {
self.make_inaccessible(0, self.total_size)
}

/// Make the memory starting at `start` and extending for `len` bytes inaccessible
/// to both reads and writes.
/// `start` and `len` must be native page-size multiples and describe a range within
/// `self`'s reserved memory.
pub fn make_inaccessible(&self, start: usize, len: usize) -> Result<(), String> {
let page_size = region::page::size();
assert_eq!(start & (page_size - 1), 0);
assert_eq!(len & (page_size - 1), 0);
assert_le!(len, self.total_size);
assert_le!(start, self.total_size - len);

// Commit the accessible size.
let ptr = self.ptr as *const u8;
unsafe { region::protect(ptr.add(start), len, region::Protection::NONE) }
.map_err(|e| e.to_string())
}

/// Return the allocated memory as a slice of u8.
pub fn as_slice(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.ptr as *const u8, self.total_size) }
Expand Down
18 changes: 9 additions & 9 deletions lib/wasi/src/bin_factory/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
pub fn spawn_exec(
binary: BinaryPackage,
name: &str,
store: Store,
mut store: Store,
env: WasiEnv,
runtime: &Arc<dyn WasiRuntime + Send + Sync + 'static>,
compiled_modules: &ModuleCache,
Expand All @@ -42,15 +42,15 @@ pub fn spawn_exec(
VirtualBusError::CompileError
});
if module.is_err() {
env.blocking_cleanup(Some(Errno::Noexec.into()));
env.blocking_cleanup(&store, Some(Errno::Noexec.into()));
}
let module = module?;
compiled_modules.set_compiled_module(binary.hash().as_str(), compiler, &module);
module
}
(None, None) => {
error!("package has no entry [{}]", name,);
env.blocking_cleanup(Some(Errno::Noexec.into()));
env.blocking_cleanup(&mut store, Some(Errno::Noexec.into()));
return Err(VirtualBusError::CompileError);
}
};
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn spawn_exec_module(
error!("wasi[{}]::wasm instantiate error ({})", pid, err);
wasi_env
.data(&store)
.blocking_cleanup(Some(Errno::Noexec.into()));
.blocking_cleanup(&store, Some(Errno::Noexec.into()));
return;
}
};
Expand All @@ -138,7 +138,7 @@ pub fn spawn_exec_module(
error!("wasi[{}]::wasi initialize error ({})", pid, err);
wasi_env
.data(&store)
.blocking_cleanup(Some(Errno::Noexec.into()));
.blocking_cleanup(&store, Some(Errno::Noexec.into()));
return;
}

Expand All @@ -148,7 +148,7 @@ pub fn spawn_exec_module(
thread.thread.set_status_finished(Err(err.into()));
wasi_env
.data(&store)
.blocking_cleanup(Some(Errno::Noexec.into()));
.blocking_cleanup(&store, Some(Errno::Noexec.into()));
return;
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ pub fn spawn_exec_module(
};

// Cleanup the environment
wasi_env.data(&store).blocking_cleanup(Some(code));
wasi_env.data(&store).blocking_cleanup(&store, Some(code));

debug!("wasi[{pid}]::main() has exited with {code}");
thread.thread.set_status_finished(ret.map(|a| a.into()));
Expand All @@ -201,7 +201,7 @@ impl BinFactory {
pub fn spawn<'a>(
&'a self,
name: String,
store: Store,
mut store: Store,
env: WasiEnv,
) -> Pin<Box<dyn Future<Output = Result<TaskJoinHandle, VirtualBusError>> + 'a>> {
Box::pin(async move {
Expand All @@ -211,7 +211,7 @@ impl BinFactory {
.await
.ok_or(VirtualBusError::NotFound);
if binary.is_err() {
env.cleanup(Some(Errno::Noent.into())).await;
env.cleanup(&mut store, Some(Errno::Noent.into())).await;
}
let binary = binary?;

Expand Down
Loading

0 comments on commit 779bf3b

Please sign in to comment.