Skip to content

Commit

Permalink
Merge branch 'master' into feat-module-import-export
Browse files Browse the repository at this point in the history
  • Loading branch information
yjhmelody committed Nov 24, 2022
2 parents ea9348b + eaf5e9d commit df41863
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
6 changes: 6 additions & 0 deletions crates/arena/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ pub struct Arena<Idx, T> {
marker: PhantomData<Idx>,
}

/// `Arena` does not store `Idx` therefore it is `Send` without its bound.
unsafe impl<Idx, T> Send for Arena<Idx, T> where T: Send {}

/// `Arena` does not store `Idx` therefore it is `Sync` without its bound.
unsafe impl<Idx, T> Sync for Arena<Idx, T> where T: Send {}

impl<Idx, T> Default for Arena<Idx, T> {
fn default() -> Self {
Self::new()
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ edition = "2021"

[dependencies]

wasi-common = "2.0.1"
wasi-cap-std-sync = "2.0.1"
wiggle = "2.0.0"
wasi-common = "2.0"
wasi-cap-std-sync = "2.0"
wiggle = { version = "2.0", default-features = false, features = ["wiggle_metadata"] }
wasmi = { version = "0.20.0", path = "../wasmi" }

[dev-dependencies]
Expand Down
50 changes: 50 additions & 0 deletions crates/wasi/src/guest_memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use wiggle::{borrow::BorrowChecker, BorrowHandle, GuestError, GuestMemory, Region};

/// Lightweight `wasmi::Memory` wrapper so we can implement the
/// `wiggle::GuestMemory` trait on it.
pub struct WasmiGuestMemory<'a> {
mem: &'a mut [u8],
bc: BorrowChecker,
}

impl<'a> WasmiGuestMemory<'a> {
pub fn new(mem: &'a mut [u8]) -> Self {
Self {
mem,
// Wiggle does not expose any methods for functions to re-enter
// the WebAssembly instance, or expose the memory via non-wiggle
// mechanisms. However, the user-defined code may end up
// re-entering the instance, in which case this is an incorrect
// implementation - we require exactly one BorrowChecker exist per
// instance.
bc: BorrowChecker::new(),
}
}
}

unsafe impl GuestMemory for WasmiGuestMemory<'_> {
fn base(&self) -> (*mut u8, u32) {
(self.mem.as_ptr() as *mut u8, self.mem.len() as u32)
}
fn has_outstanding_borrows(&self) -> bool {
self.bc.has_outstanding_borrows()
}
fn is_shared_borrowed(&self, r: Region) -> bool {
self.bc.is_shared_borrowed(r)
}
fn is_mut_borrowed(&self, r: Region) -> bool {
self.bc.is_mut_borrowed(r)
}
fn shared_borrow(&self, r: Region) -> Result<BorrowHandle, GuestError> {
self.bc.shared_borrow(r)
}
fn mut_borrow(&self, r: Region) -> Result<BorrowHandle, GuestError> {
self.bc.mut_borrow(r)
}
fn shared_unborrow(&self, h: BorrowHandle) {
self.bc.shared_unborrow(h)
}
fn mut_unborrow(&self, h: BorrowHandle) {
self.bc.mut_unborrow(h)
}
}
2 changes: 2 additions & 0 deletions crates/wasi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod guest_memory;
pub mod snapshots;

pub use self::guest_memory::WasmiGuestMemory;
pub use snapshots::preview_1::define_wasi;
pub use wasi_cap_std_sync::{
clocks,
Expand Down
3 changes: 2 additions & 1 deletion crates/wasi/src/snapshots/preview_1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::WasmiGuestMemory;
use std::{
pin::Pin,
task::{Context, RawWaker, RawWakerVTable, Waker},
Expand Down Expand Up @@ -64,7 +65,7 @@ macro_rules! impl_add_to_linker_for_funcs {
};
let(mem, ctx) = mem.data_and_store_mut(&mut caller);
let ctx = wasi_ctx(ctx);
let mem = wiggle::wasmtime::WasmtimeGuestMemory::new(mem);
let mem = WasmiGuestMemory::new(mem);

match wasi_common::snapshots::preview_1::wasi_snapshot_preview1::$fname(ctx, &mem, $($arg,)*).await {
Ok(r) => Ok(<$ret>::from(r)),
Expand Down
10 changes: 10 additions & 0 deletions crates/wasmi/src/engine/code_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ pub struct InstructionPtr {
ptr: *const Instruction,
}

/// It is safe to send an [`InstructionPtr`] to another thread.
///
/// The access to the pointed-to [`Instruction`] is read-only and
/// [`Instruction`] itself is [`Send`].
///
/// However, it is not safe to share an [`InstructionPtr`] between threads
/// due to their [`InstructionPtr::offset`] method which relinks the
/// internal pointer and is not synchronized.
unsafe impl Send for InstructionPtr {}

impl InstructionPtr {
/// Creates a new [`InstructionPtr`] for `instr`.
#[inline]
Expand Down
10 changes: 10 additions & 0 deletions crates/wasmi/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ pub struct Store<T> {
user_state: T,
}

#[test]
fn test_store_is_send_sync() {
const _: () = {
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
let _ = assert_send::<Store<()>>;
let _ = assert_sync::<Store<()>>;
};
}

impl<T> Store<T> {
/// Creates a new store.
pub fn new(engine: &Engine, user_state: T) -> Self {
Expand Down

0 comments on commit df41863

Please sign in to comment.