Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit f851563

Browse files
committed
consolidate unsafe code in concurrent.rs and friends
This adds a few abstractions to reduce the amount of unsafe code and make what remains easier to reason about locally (i.e. without relying as much on functions only being called in certain contexts, or on non-local safe code upholding invariants). This introduces a `StoreToken` abstraction for witnessing the data type parameter `T` for a `StoreContextMut<T>` and later safely casting from a `&mut dyn VMStore` to a `&mut StoreInner<T>` within a closure by asserting that the `StoreId`s match. This also eliminates various transmutes and other unsafe shenanigans to avoid a `T: 'static` bound since we've finally decided avoiding such a bound is impractical. Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent e7bbbd0 commit f851563

35 files changed

Lines changed: 1745 additions & 1513 deletions

File tree

benches/call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ fn wasm_to_host(c: &mut Criterion) {
360360
return;
361361
}
362362

363-
let mut typed = Linker::new(&engine);
363+
let mut typed = Linker::<()>::new(&engine);
364364
typed
365365
.func_wrap_async("", "nop", |caller, _: ()| {
366366
Box::new(async {

crates/fuzzing/src/oracles/dummy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::Context;
44
use wasmtime::*;
55

66
/// Create a set of dummy functions/globals/etc for the given imports.
7-
pub fn dummy_linker<T>(store: &mut Store<T>, module: &Module) -> Result<Linker<T>> {
7+
pub fn dummy_linker<T: 'static>(store: &mut Store<T>, module: &Module) -> Result<Linker<T>> {
88
let mut linker = Linker::new(store.engine());
99
linker.allow_shadowing(true);
1010
for import in module.imports() {

crates/misc/component-async-tests/http/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<T: WasiHttpView> wasi::http::types::HostFields for WasiHttpImpl<T> {
217217
}
218218

219219
impl<C: WasiHttpViewConcurrent> wasi::http::types::HostBodyConcurrent for WasiHttp<C> {
220-
async fn new<T>(
220+
async fn new<T: 'static>(
221221
accessor: &mut Accessor<T, Self>,
222222
stream: HostStream<u8>,
223223
) -> wasmtime::Result<Resource<Body>> {
@@ -230,7 +230,7 @@ impl<C: WasiHttpViewConcurrent> wasi::http::types::HostBodyConcurrent for WasiHt
230230
})
231231
}
232232

233-
async fn new_with_trailers<T>(
233+
async fn new_with_trailers<T: 'static>(
234234
accessor: &mut Accessor<T, Self>,
235235
stream: HostStream<u8>,
236236
trailers: HostFuture<Resource<Fields>>,
@@ -246,7 +246,7 @@ impl<C: WasiHttpViewConcurrent> wasi::http::types::HostBodyConcurrent for WasiHt
246246

247247
// TODO: once access to the store is possible in a non-async context (similar to Accessor pattern)
248248
// we should convert this to a sync function that works w/ &mut self.
249-
async fn finish<T>(
249+
async fn finish<T: 'static>(
250250
accessor: &mut Accessor<T, Self>,
251251
this: Resource<Body>,
252252
) -> wasmtime::Result<HostFuture<Resource<Fields>>> {

crates/wasi-common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ macro_rules! define_wasi {
117117
where U: Send
118118
+ crate::snapshots::preview_0::wasi_unstable::WasiUnstable
119119
+ crate::snapshots::preview_1::wasi_snapshot_preview1::WasiSnapshotPreview1,
120+
T: 'static,
120121
$($bounds)*
121122
{
122123
snapshots::preview_1::add_wasi_snapshot_preview1_to_linker(linker, get_cx)?;

crates/wasi-common/tests/all/async_.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::*;
22
use test_programs_artifacts::*;
33
use wasi_common::tokio::{add_to_linker, WasiCtxBuilder};
4+
use wasi_common::WasiCtx;
45

56
foreach_preview1!(assert_test_exists);
67

@@ -20,7 +21,7 @@ async fn run(path: &str, inherit_stdio: bool) -> Result<()> {
2021
let engine = test_programs_artifacts::engine(|config| {
2122
config.async_support(true);
2223
});
23-
let mut linker = Linker::new(&engine);
24+
let mut linker = Linker::<WasiCtx>::new(&engine);
2425
add_to_linker(&mut linker, |cx| cx)?;
2526

2627
// Create our wasi context.

crates/wasi-http/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ use wasmtime_wasi::p2::IoImpl;
287287
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
288288
/// }
289289
/// ```
290-
pub fn add_to_linker_async<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
290+
pub fn add_to_linker_async<T: 'static>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
291291
where
292292
T: WasiHttpView + wasmtime_wasi::p2::WasiView + 'static,
293293
{
@@ -300,7 +300,7 @@ where
300300
///
301301
/// This is useful when using [`wasmtime_wasi::p2::add_to_linker_async`] for
302302
/// example to avoid re-adding the same interfaces twice.
303-
pub fn add_only_http_to_linker_async<T>(
303+
pub fn add_only_http_to_linker_async<T: 'static>(
304304
l: &mut wasmtime::component::Linker<T>,
305305
) -> anyhow::Result<()>
306306
where

crates/wasi-http/src/p3/host/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ impl<T> HostRequestConcurrent for WasiHttp<T>
739739
where
740740
T: WasiHttpView + 'static,
741741
{
742-
async fn new<U>(
742+
async fn new<U: 'static>(
743743
store: &mut Accessor<U, Self>,
744744
headers: Resource<WithChildren<http::HeaderMap>>,
745745
contents: Option<HostStream<u8>>,
@@ -1066,7 +1066,7 @@ impl<T> HostResponseConcurrent for WasiHttp<T>
10661066
where
10671067
T: WasiHttpView + 'static,
10681068
{
1069-
async fn new<U>(
1069+
async fn new<U: 'static>(
10701070
store: &mut Accessor<U, Self>,
10711071
headers: Resource<WithChildren<http::HeaderMap>>,
10721072
contents: Option<HostStream<u8>>,

crates/wasi-http/src/p3/proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::p3::{Request, Response};
1212

1313
impl Proxy {
1414
/// Call `handle` on [Proxy] getting a [Future] back.
15-
pub fn handle<T, S: AsContextMut<Data = T>, R: Into<Request>>(
15+
pub fn handle<T: 'static, S: AsContextMut<Data = T>, R: Into<Request>>(
1616
&self,
1717
mut store: S,
1818
req: R,

crates/wasi/src/p3/filesystem/host.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ where
142142
})
143143
}
144144

145-
async fn write_via_stream<U>(
145+
async fn write_via_stream<U: 'static>(
146146
store: &mut Accessor<U, Self>,
147147
fd: Resource<Descriptor>,
148148
data: HostStream<u8>,
@@ -191,7 +191,7 @@ where
191191
}
192192
}
193193

194-
async fn append_via_stream<U>(
194+
async fn append_via_stream<U: 'static>(
195195
store: &mut Accessor<U, Self>,
196196
fd: Resource<Descriptor>,
197197
data: HostStream<u8>,

crates/wasi/src/p3/sockets/host/types/tcp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ where
372372
}
373373
}
374374

375-
async fn send<U>(
375+
async fn send<U: 'static>(
376376
store: &mut Accessor<U, Self>,
377377
socket: Resource<TcpSocket>,
378378
data: HostStream<u8>,

0 commit comments

Comments
 (0)