Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared memory support #9507

Merged
merged 8 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/c-api/include/wasmtime/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ extern "C" {
WASM_API_EXTERN wasm_memorytype_t *wasmtime_memorytype_new(uint64_t min,
bool max_present,
uint64_t max,
bool is_64);
bool is_64,
bool shared);

/**
* \brief Returns the minimum size, in pages, of the specified memory type.
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
Extern::Global(_) => crate::WASM_EXTERN_GLOBAL,
Extern::Table(_) => crate::WASM_EXTERN_TABLE,
Extern::Memory(_) => crate::WASM_EXTERN_MEMORY,
Extern::SharedMemory(_) => todo!(),
Extern::SharedMemory(_) => crate::WASM_EXTERN_SHAREDMEMORY,
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/c-api/src/types/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) enum CExternType {
Func(CFuncType),
Global(CGlobalType),
Memory(CMemoryType),
SharedMemory(CMemoryType),
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
Table(CTableType),
}

Expand All @@ -24,6 +25,7 @@ impl CExternType {
ExternType::Func(f) => CExternType::Func(CFuncType::new(f)),
ExternType::Global(f) => CExternType::Global(CGlobalType::new(f)),
ExternType::Memory(f) => CExternType::Memory(CMemoryType::new(f)),
ExternType::SharedMemory(f) => CExternType::SharedMemory(CMemoryType::new(f)),
ExternType::Table(f) => CExternType::Table(CTableType::new(f)),
}
}
Expand All @@ -35,6 +37,7 @@ pub const WASM_EXTERN_FUNC: wasm_externkind_t = 0;
pub const WASM_EXTERN_GLOBAL: wasm_externkind_t = 1;
pub const WASM_EXTERN_TABLE: wasm_externkind_t = 2;
pub const WASM_EXTERN_MEMORY: wasm_externkind_t = 3;
pub const WASM_EXTERN_SHAREDMEMORY: wasm_externkind_t = 4;

impl wasm_externtype_t {
pub(crate) fn from_extern_type(ty: ExternType) -> wasm_externtype_t {
Expand All @@ -55,6 +58,7 @@ pub extern "C" fn wasm_externtype_kind(et: &wasm_externtype_t) -> wasm_externkin
CExternType::Table(_) => WASM_EXTERN_TABLE,
CExternType::Global(_) => WASM_EXTERN_GLOBAL,
CExternType::Memory(_) => WASM_EXTERN_MEMORY,
CExternType::SharedMemory(_) => WASM_EXTERN_SHAREDMEMORY,
}
}

Expand Down
24 changes: 17 additions & 7 deletions crates/c-api/src/types/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ impl wasm_memorytype_t {
pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasm_memorytype_t> {
match &e.which {
CExternType::Memory(_) => Some(unsafe { &*(e as *const _ as *const _) }),
CExternType::SharedMemory(_) => Some(unsafe { &*(e as *const _ as *const _) }),
_ => None,
}
}

pub(crate) fn ty(&self) -> &CMemoryType {
match &self.ext.which {
CExternType::Memory(f) => &f,
CExternType::SharedMemory(f) => &f,
_ => unsafe { std::hint::unreachable_unchecked() },
}
}
Expand Down Expand Up @@ -71,20 +73,28 @@ pub extern "C" fn wasmtime_memorytype_new(
maximum_specified: bool,
maximum: u64,
memory64: bool,
shared: bool,
) -> Box<wasm_memorytype_t> {
let maximum = if maximum_specified {
Some(maximum)
} else {
None
};
Box::new(wasm_memorytype_t::new(if memory64 {
MemoryType::new64(minimum, maximum)
} else {
MemoryType::new(
Box::new(if shared {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind panicking here for now with unimplemented!() if both shared and memory64 are set? (we need to add a constructor for that but haven't gotten around to it yet)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I take this back, this may want to use MemoryTypeBuilder to support shared 64-bit memories

wasm_memorytype_t::new(MemoryType::shared(
u32::try_from(minimum).unwrap(),
maximum.map(|i| u32::try_from(i).unwrap()),
)
}))
maximum.map(|i| u32::try_from(i).unwrap()).unwrap(),
))
} else {
wasm_memorytype_t::new(if memory64 {
MemoryType::new64(minimum, maximum)
} else {
MemoryType::new(
u32::try_from(minimum).unwrap(),
maximum.map(|i| u32::try_from(i).unwrap()),
)
})
})
}

#[no_mangle]
Expand Down
16 changes: 16 additions & 0 deletions crates/fuzzing/src/oracles/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub fn dummy_extern<T>(store: &mut Store<T>, ty: ExternType) -> Result<Extern> {
ExternType::Global(global_ty) => Extern::Global(dummy_global(store, global_ty)?),
ExternType::Table(table_ty) => Extern::Table(dummy_table(store, table_ty)?),
ExternType::Memory(mem_ty) => Extern::Memory(dummy_memory(store, mem_ty)?),
ExternType::SharedMemory(mem_ty) => {
Extern::SharedMemory(dummy_shared_memory(store.engine(), mem_ty)?)
}
})
}

Expand Down Expand Up @@ -77,8 +80,14 @@ pub fn dummy_memory<T>(store: &mut Store<T>, ty: MemoryType) -> Result<Memory> {
Memory::new(store, ty)
}

/// Construct a dummy shared memory for the given memory type.
pub fn dummy_shared_memory(engine: &Engine, ty: MemoryType) -> Result<SharedMemory> {
SharedMemory::new(engine, ty)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed? (or should it perhaps be integrated elsewhere?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I just removed it


#[cfg(test)]
mod tests {

use super::*;

fn store() -> Store<()> {
Expand Down Expand Up @@ -114,6 +123,13 @@ mod tests {
assert_eq!(memory.size(&store), 1);
}

#[test]
fn dummy_shared_memory_import() {
let store = store();
let shared_memory = dummy_shared_memory(store.engine(), MemoryType::shared(1, 1)).unwrap();
assert_eq!(shared_memory.size(), 1);
}

#[test]
fn dummy_function_import() {
let mut store = store();
Expand Down
2 changes: 2 additions & 0 deletions crates/wasmtime/src/runtime/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,8 @@ pub enum ExternType {
Table(TableType),
/// This external type is the type of a WebAssembly memory.
Memory(MemoryType),
/// This external type is the type of a WebAssembly shared memory.
SharedMemory(MemoryType),
}

macro_rules! extern_type_accessors {
Expand Down
Loading