Skip to content

Commit f714cb0

Browse files
committed
Massage wasmtime-wasi to use vendored primitives
This commit adjust the wasmtime 36.0.0 state of the `wasmtime-wasi` crate to use the vendored primitives in the prior commit. This is much more involved than just a cherry-pick because the internals of `wasmtime-wasi` have greatly changed since the 36.0.0 release, namely: * WASIp3 support was added and shared code between p2/p3 was refactored to its own module. * The `cap-std` dependency was largely removed which vendored some fringe platform-specific code from that and surrounding crates. These major changes aren't present on the 36.0.0 branch, and additionally the public types of `wasmtime-wasi` contain more `cap_std` types than before. To handle all of this the commit here leaves the public type definitions as-is but helpers internally all switch to using the previously-vendored primitives. This involved vendoring more code from the `main` branch and is a bit invasive, but this is all necessary to sever dependencies with the `cap-std` crates.
1 parent 5e30efd commit f714cb0

9 files changed

Lines changed: 438 additions & 165 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/wasi/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ winx = "0.36.0"
6060
workspace = true
6161
features = [
6262
"Wdk_Storage_FileSystem",
63+
"Wdk_Foundation",
6364
"Win32_Foundation",
6465
"Win32_Storage_FileSystem",
6566
"Win32_System_IO",
6667
"Win32_System_Ioctl",
6768
"Win32_System_Performance",
69+
"Win32_System_WindowsProgramming",
70+
"Win32_System_SystemServices",
6871
]
6972

7073
[features]

crates/wasi/src/ctx.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::sockets::{SocketAddrCheck, SocketAddrUse, WasiSocketsCtx};
66
use crate::{DirPerms, FilePerms, OpenMode};
77
use anyhow::Result;
88
use cap_rand::RngCore;
9-
use cap_std::ambient_authority;
109
use std::future::Future;
1110
use std::mem;
1211
use std::net::SocketAddr;

crates/wasi/src/filesystem.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ bitflags::bitflags! {
3030
const MUTATE = 0b10;
3131
}
3232
}
33+
34+
pub(crate) mod primitives;

crates/wasi/src/filesystem/primitives/file_type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ impl FileTypeExt for FileType {
115115
/// `std`. Use `cap_fs_ext::FileTypeExt` instead of calling this directly.
116116
#[cfg(windows)]
117117
#[doc(hidden)]
118+
#[allow(unused)]
118119
pub trait _WindowsFileTypeExt {
119120
fn is_block_device(&self) -> bool;
120121
fn is_char_device(&self) -> bool;

crates/wasi/src/filesystem/primitives/tests/fs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::helpers as h;
55
use super::sys_common::io::tmpdir;
66
use super::sys_common::symlink_junction;
77
use crate::filesystem::primitives as p;
8-
use rand::Rng;
8+
use cap_rand::RngCore;
99
use std::fs::File;
1010
use std::io::prelude::*;
1111
use std::io::{ErrorKind, SeekFrom};
@@ -964,7 +964,7 @@ fn _assert_send_sync() {
964964
#[test]
965965
fn binary_file() {
966966
let mut bytes = [0; 1024];
967-
rand::rng().fill_bytes(&mut bytes);
967+
cap_rand::thread_rng(cap_std::ambient_authority()).fill_bytes(&mut bytes);
968968

969969
let tmpdir = tmpdir();
970970

@@ -979,7 +979,7 @@ fn binary_file() {
979979
#[test]
980980
fn write_then_read() {
981981
let mut bytes = [0; 1024];
982-
rand::rng().fill_bytes(&mut bytes);
982+
cap_rand::thread_rng(cap_std::ambient_authority()).fill_bytes(&mut bytes);
983983

984984
let tmpdir = tmpdir();
985985

crates/wasi/src/p2/filesystem.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::runtime::{AbortOnDropJoinHandle, spawn_blocking};
44
use crate::{DirPerms, FilePerms, OpenMode, TrappableError};
55
use anyhow::anyhow;
66
use bytes::{Bytes, BytesMut};
7+
use io_lifetimes::AsFilelike;
78
use std::io;
89
use std::mem;
910
use std::sync::Arc;
@@ -113,30 +114,32 @@ impl File {
113114
/// - [Implement opt-in for enabling WASI to block the current thread](https://github.com/bytecodealliance/wasmtime/pull/8190)
114115
pub(crate) async fn run_blocking<F, R>(&self, body: F) -> R
115116
where
116-
F: FnOnce(&cap_std::fs::File) -> R + Send + 'static,
117+
F: FnOnce(&std::fs::File) -> R + Send + 'static,
117118
R: Send + 'static,
118119
{
119-
match self.as_blocking_file() {
120-
Some(file) => body(file),
121-
None => self.spawn_blocking(body).await,
120+
if let Some(file) = self.as_blocking_file() {
121+
return body(&file);
122122
}
123+
self.spawn_blocking(body).await
123124
}
124125

125126
pub(crate) fn spawn_blocking<F, R>(&self, body: F) -> AbortOnDropJoinHandle<R>
126127
where
127-
F: FnOnce(&cap_std::fs::File) -> R + Send + 'static,
128+
F: FnOnce(&std::fs::File) -> R + Send + 'static,
128129
R: Send + 'static,
129130
{
130131
let f = self.file.clone();
131-
spawn_blocking(move || body(&f))
132+
spawn_blocking(move || body(&f.as_filelike_view()))
132133
}
133134

134135
/// Returns `Some` when the current thread is allowed to block in filesystem
135136
/// operations, and otherwise returns `None` to indicate that
136137
/// `spawn_blocking` must be used.
137-
pub(crate) fn as_blocking_file(&self) -> Option<&cap_std::fs::File> {
138+
pub(crate) fn as_blocking_file(
139+
&self,
140+
) -> Option<io_lifetimes::views::FilelikeView<'_, std::fs::File>> {
138141
if self.allow_blocking_current_thread {
139-
Some(&self.file)
142+
Some(self.file.as_filelike_view())
140143
} else {
141144
None
142145
}
@@ -203,14 +206,14 @@ impl Dir {
203206
/// - [Implement opt-in for enabling WASI to block the current thread](https://github.com/bytecodealliance/wasmtime/pull/8190)
204207
pub(crate) async fn run_blocking<F, R>(&self, body: F) -> R
205208
where
206-
F: FnOnce(&cap_std::fs::Dir) -> R + Send + 'static,
209+
F: FnOnce(&std::fs::File) -> R + Send + 'static,
207210
R: Send + 'static,
208211
{
209212
if self.allow_blocking_current_thread {
210-
body(&self.dir)
213+
body(&self.dir.as_filelike_view())
211214
} else {
212215
let d = self.dir.clone();
213-
spawn_blocking(move || body(&d)).await
216+
spawn_blocking(move || body(&d.as_filelike_view())).await
214217
}
215218
}
216219
}
@@ -236,7 +239,7 @@ impl FileInputStream {
236239
}
237240
}
238241

239-
fn blocking_read(file: &cap_std::fs::File, offset: u64, size: usize) -> ReadState {
242+
fn blocking_read(file: &std::fs::File, offset: u64, size: usize) -> ReadState {
240243
use system_interface::fs::FileIoExt;
241244

242245
let mut buf = BytesMut::zeroed(size.min(crate::MAX_READ_SIZE_ALLOC));
@@ -392,7 +395,7 @@ impl FileOutputStream {
392395
}
393396

394397
fn blocking_write(
395-
file: &cap_std::fs::File,
398+
file: &std::fs::File,
396399
mut buf: Bytes,
397400
mode: FileOutputMode,
398401
) -> io::Result<usize> {

0 commit comments

Comments
 (0)