Skip to content

Commit 2ccc4fa

Browse files
authored
Unrolled build for #145944
Rollup merge of #145944 - alexcrichton:native-wasip2, r=tgross35 std: Start supporting WASIp2 natively This commit is the start of an effort to support WASIp2 natively in the standard library. Before this commit the `wasm32-wasip2` target behaved exactly like `wasm32-wasip1` target by importing APIs from the core wasm module `wasi_snapshot_preview1`. These APIs are satisfied by the `wasm-component-ld` target by using an [adapter] which implements WASIp1 in terms of WASIp2. This adapter comes at a cost, however, in terms of runtime indirection and instantiation cost, so ideally the adapter would be removed entirely. The purpose of this adapter was to provide a smoother on-ramp from WASIp1 to WASIp2 when it was originally created. The `wasm32-wasip2` target has been around for long enough now that it's much more established. Additionally the only thing historically blocking using WASIp2 directly was implementation effort. Work is now underway to migrate wasi-libc itself to using WASIp2 directly and now seems as good a time as any to migrate the Rust standard library too. Implementation-wise the milestones here are: * The `wasm32-wasip2` target now also depends on the `wasi` crate at version 0.14.* in addition to the preexisting dependency of 0.11.*. The 0.14.* release series binds WASIp2 APIs instead of WASIp1 APIs. * Some preexisting naming around `mod wasi` or `wasi.rs` was renamed to `wasip1` where appropriate. For example `std::sys::pal::wasi` is now called `std::sys::pal::wasip1`. * More platform-specific WASI modules are now split between WASIp1 and WASIp2. For example getting the current time, randomness, and process arguments now use WASIp2 APIs directly instead of using WASIp1 APIs that require an adapter. It's worth pointing out that this PR does not migrate the entire standard library away from using WASIp1 APIs on the `wasm32-wasip2` target. Everything related to file descriptors and filesystem APIs is still using WASIp1. Migrating that is left for a future PR. In the meantime the goal of this change is to lay the groundwork necessary for migrating in the future. Eventually the goal is to drop the `wasi` 0.11.* dependency on the `wasm32-wasip2` target (the `wasm32-wasip1` target will continue to retain this dependency). [adapter]: https://github.com/bytecodealliance/wasmtime/blob/main/crates/wasi-preview1-component-adapter/README.md
2 parents 51ff895 + 5d81f03 commit 2ccc4fa

File tree

20 files changed

+222
-16
lines changed

20 files changed

+222
-16
lines changed

library/Cargo.lock

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ dependencies = [
327327
"rustc-demangle",
328328
"std_detect",
329329
"unwind",
330-
"wasi",
330+
"wasi 0.11.1+wasi-snapshot-preview1",
331+
"wasi 0.14.3+wasi-0.2.4",
331332
"windows-targets 0.0.0",
332333
]
333334

@@ -399,6 +400,17 @@ dependencies = [
399400
"rustc-std-workspace-core",
400401
]
401402

403+
[[package]]
404+
name = "wasi"
405+
version = "0.14.3+wasi-0.2.4"
406+
source = "registry+https://github.com/rust-lang/crates.io-index"
407+
checksum = "6a51ae83037bdd272a9e28ce236db8c07016dd0d50c27038b3f407533c030c95"
408+
dependencies = [
409+
"rustc-std-workspace-alloc",
410+
"rustc-std-workspace-core",
411+
"wit-bindgen",
412+
]
413+
402414
[[package]]
403415
name = "windows-sys"
404416
version = "0.59.0"
@@ -475,3 +487,13 @@ name = "windows_x86_64_msvc"
475487
version = "0.52.6"
476488
source = "registry+https://github.com/rust-lang/crates.io-index"
477489
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
490+
491+
[[package]]
492+
name = "wit-bindgen"
493+
version = "0.45.0"
494+
source = "registry+https://github.com/rust-lang/crates.io-index"
495+
checksum = "052283831dbae3d879dc7f51f3d92703a316ca49f91540417d38591826127814"
496+
dependencies = [
497+
"rustc-std-workspace-alloc",
498+
"rustc-std-workspace-core",
499+
]

library/std/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ wasi = { version = "0.11.0", features = [
8080
'rustc-dep-of-std',
8181
], default-features = false }
8282

83+
[target.'cfg(all(target_os = "wasi", target_env = "p2"))'.dependencies]
84+
wasip2 = { version = '0.14.3', features = [
85+
'rustc-dep-of-std',
86+
], default-features = false, package = 'wasi' }
87+
8388
[target.'cfg(target_os = "uefi")'.dependencies]
8489
r-efi = { version = "5.2.0", features = ['rustc-dep-of-std'] }
8590
r-efi-alloc = { version = "2.0.0", features = ['rustc-dep-of-std'] }

library/std/src/sys/args/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ cfg_select! {
3232
mod uefi;
3333
pub use uefi::*;
3434
}
35-
target_os = "wasi" => {
36-
mod wasi;
37-
pub use wasi::*;
35+
all(target_os = "wasi", target_env = "p1") => {
36+
mod wasip1;
37+
pub use wasip1::*;
38+
}
39+
all(target_os = "wasi", target_env = "p2") => {
40+
mod wasip2;
41+
pub use wasip2::*;
3842
}
3943
target_os = "xous" => {
4044
mod xous;
File renamed without changes.

library/std/src/sys/args/wasip2.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub use super::common::Args;
2+
3+
/// Returns the command line arguments
4+
pub fn args() -> Args {
5+
Args::new(wasip2::cli::environment::get_arguments().into_iter().map(|arg| arg.into()).collect())
6+
}

library/std/src/sys/pal/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ cfg_select! {
4949
mod wasip2;
5050
pub use self::wasip2::*;
5151
}
52-
target_os = "wasi" => {
53-
mod wasi;
54-
pub use self::wasi::*;
52+
all(target_os = "wasi", target_env = "p1") => {
53+
mod wasip1;
54+
pub use self::wasip1::*;
5555
}
5656
target_family = "wasm" => {
5757
mod wasm;

0 commit comments

Comments
 (0)