wiggle: allow wiggle to use shared memory (#5054)
`wiggle` looks for an exported `Memory` named `"memory"` to use for its guest slices. This change allows it to use a `SharedMemory` if this is the kind of memory used for the export. It is `unsafe` to use shared memory in Wiggle because of broken Rust guarantees: previously, Wiggle could hand out slices to WebAssembly linear memory that could be concurrently modified by some other thread. With the introduction of Wiggle's new `UnsafeGuestSlice` (#5225, #5229, #5264), Wiggle should now correctly communicate its guarantees through its API.
This commit is contained in:
@@ -112,13 +112,19 @@ fn generate_func(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let body = quote! {
|
let body = quote! {
|
||||||
let mem = match caller.get_export("memory") {
|
let export = caller.get_export("memory");
|
||||||
Some(#rt::wasmtime_crate::Extern::Memory(m)) => m,
|
let (mem, ctx) = match &export {
|
||||||
|
Some(#rt::wasmtime_crate::Extern::Memory(m)) => {
|
||||||
|
let (mem, ctx) = m.data_and_store_mut(&mut caller);
|
||||||
|
let ctx = get_cx(ctx);
|
||||||
|
(#rt::wasmtime::WasmtimeGuestMemory::new(mem), ctx)
|
||||||
|
}
|
||||||
|
Some(#rt::wasmtime_crate::Extern::SharedMemory(m)) => {
|
||||||
|
let ctx = get_cx(caller.data_mut());
|
||||||
|
(#rt::wasmtime::WasmtimeGuestMemory::shared(m.data()), ctx)
|
||||||
|
}
|
||||||
_ => #rt::anyhow::bail!("missing required memory export"),
|
_ => #rt::anyhow::bail!("missing required memory export"),
|
||||||
};
|
};
|
||||||
let (mem , ctx) = mem.data_and_store_mut(&mut caller);
|
|
||||||
let ctx = get_cx(ctx);
|
|
||||||
let mem = #rt::wasmtime::WasmtimeGuestMemory::new(mem);
|
|
||||||
Ok(<#ret_ty>::from(#abi_func(ctx, &mem #(, #arg_names)*) #await_ ?))
|
Ok(<#ret_ty>::from(#abi_func(ctx, &mem #(, #arg_names)*) #await_ ?))
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user