about summary refs log tree commit diff
path: root/library/std/src/sys/pal/wasi
AgeCommit message (Collapse)AuthorLines
2025-08-28std: Start supporting WASIp2 nativelyAlex Crichton-594/+0
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
2025-08-26remove deprecated Error::description in implsMarijn Schouten-7/+1
2025-08-16library: Migrate from `cfg_if` to `cfg_select`Josh Triplett-13/+15
Migrate the standard library from using the external `cfg_if` crate to using the now-built-in `cfg_select` macro. This does not yet eliminate the dependency from `library/std/Cargo.toml`, because while the standard library itself no longer uses `cfg_if`, it also incorporates the `backtrace` crate, which does. Migration assisted by the following vim command (after selecting the full `cfg_if!` invocation): ``` '<,'>s/\(cfg_if::\)\?cfg_if/cfg_select/ | '<,'>s/^\( *\)} else {/\1}\r\1_ => {/c | '<,'>s/^\( *\)} else if #\[cfg(\(.*\))\] /\1}\r\1\2 => /e | '<,'>s/if #\[cfg(\(.*\))\] {/\1 => {/e ``` This is imperfect, but substantially accelerated the process. This prompts for confirmation on the `} else {` since that can also appear inside one of the arms. This also requires manual intervention to handle any multi-line conditions.
2025-08-12Constify SystemTime methodsltdk-7/+18
2025-08-06Print thread ID in panic message if thread name is unknownTrevor Gross-0/+4
`panic!` does not print any identifying information for threads that are unnamed. However, in many cases, the thread ID can be determined. This changes the panic message from something like this: thread '<unnamed>' panicked at src/main.rs:3:5: explicit panic To something like this: thread '<unnamed>' (0xff9bf) panicked at src/main.rs:3:5: explicit panic Stack overflow messages are updated as well. This change applies to both named and unnamed threads. The ID printed is the OS integer thread ID rather than the Rust thread ID, which should also be what debuggers print.
2025-07-28thread name in stack overflow messagejoboet-2/+2
2025-07-06sleep_until: use clock_nanosleep where possibledvdsk-1/+9
Using clock nanosleep leads to more accurate sleep times on platforms where it is supported. To enable using clock_nanosleep this makes `sleep_until` platform specific. That unfortunatly requires identical placeholder implementations for the other platforms (windows/mac/wasm etc). we will land platform specific implementations for those later. See the `sleep_until` tracking issue. This requires an accessors for the Instant type. As that accessor is only used on the platforms that have clock_nanosleep it is marked as allow_unused. 32bit time_t targets do not use clock_nanosleep atm, they instead rely on the same placeholder as the other platforms. We could make them use clock_nanosleep too in the future using `__clock_nanosleep_time64`. __clock_nanosleep_time64 is documented at: https://www.gnu.org/software/libc/manual/html_node/64_002dbit-time-symbol-handling.html
2025-04-21Move `sys::pal::os::Env` into `sys::env`Thalia Archibald-146/+4
Although `Env` (as `Vars`), `Args`, path functions, and OS constants are publicly exposed via `std::env`, their implementations are each self-contained. Keep them separate in `std::sys` and make a new module, `sys::env`, for `Env`.
2025-04-18Combine env consts into std::sys::env_constsThalia Archibald-12/+0
2025-04-12Move args into std::sysThalia Archibald-62/+0
2025-04-04Move fd into sysThalia Archibald-333/+0
2025-03-29Rollup merge of #138757 - rust-wasi-web:wasi-thread-stack-size, r=alexcrichtonMatthias Krüger-1/+1
wasm: increase default thread stack size to 1 MB The default stack size for the [main thread is 1 MB as specified by linker options](https://github.com/rust-lang/rust/blob/38cf49dde8a5b0b284bb6dffd423d223c9f8f7a3/compiler/rustc_target/src/spec/base/wasm.rs#L14). However, the default stack size for threads was only 64 kB. This is surprisingly small and thus we increase it to 1 MB to match the main thread.
2025-03-22std: move process implementations to `sys`joboet-2/+0
As per #117276, this moves the implementations of `Process` and friends out of the `pal` module and into the `sys` module, removing quite a lot of error-prone `#[path]` imports in the process (hah, get it ;-)). I've also made the `zircon` module a dedicated submodule of `pal::unix`, hopefully we can move some other definitions there as well (they are currently quite a lot of duplications in `sys`). Also, the `ensure_no_nuls` function on Windows now lives in `sys::pal::windows` – it's not specific to processes and shared by the argument implementation.
2025-03-20wasm: increase default thread stack size to 1 MBSebastian Urban-1/+1
The default stack size for the main thread is 1 MB as specified by linker options. However, the default stack size for threads was only 64 kB. This is surprisingly small and thus we increase it to 1 MB to match the main thread.
2025-03-09std: move stdio to `sys`joboet-118/+0
As per #117276, this moves the platform definitions of `Stdout` and friends into `sys`. This PR also unifies the UNIX and Hermit implementations and moves the `__rust_print_err` function needed by libunwind on SGX into the dedicated module for such helper functions.
2025-03-08Move fs into sysThalia Archibald-873/+0
2025-03-06library: Use size_of from the prelude instead of importedThalia Archibald-9/+8
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the prelude instead of importing or qualifying them. These functions were added to all preludes in Rust 1.80.
2025-02-20Implement read_buf for WASI stdinThalia Archibald-1/+6
2025-02-17Rollup merge of #137165 - thaliaarchi:file-tell, r=ChrisDentonMatthias Krüger-0/+4
Use `tell` for `<File as Seek>::stream_position` Some platforms have a more efficient way to get the current offset of the file than by seeking. For example, Wasi has `fd_tell` and SOLID has `SOLID_FS_Ftell`. Implement `<File as Seek>::stream_position()` in terms of those. I do not use any APIs that were not already used in `std`. Although, the `libc` crate has [`ftell`](https://docs.rs/libc/latest/libc/fn.ftell.html), [`ftello`](https://docs.rs/libc/latest/libc/fn.ftello.html), and [`ftello64`](https://docs.rs/libc/latest/libc/fn.ftello64.html), I do not know platform coverage. It appears that Windows has no `tell`-like API. I have checked that it builds on each relevant platform.
2025-02-17Use tell for <File as Seek>::stream_positionThalia Archibald-0/+4
2025-02-17Rollup merge of #136844 - thaliaarchi:const-io-error, r=ChrisDentonMatthias Krüger-3/+2
Use `const_error!` when possible Replace usages of `io::Error::new(io::ErrorKind::Variant, "constant string")` with `io::const_error!(io::ErrorKind::Variant, "constant string")` to avoid allocations when possible. Additionally, fix `&&str` error messages in SGX and missing/misplaced trailing commas in `const_error!`.
2025-02-13std: Apply rust_2024_incompatible_patEric Huss-2/+2
2025-02-10Fix &&str and trailing commas in io::const_error!Thalia Archibald-1/+1
2025-02-10Use io::const_error! when possible over io::Error::newThalia Archibald-2/+1
2025-02-09Mark extern blocks as unsafeMichael Goulet-4/+4
2025-02-07std: get rid of `sys_common::io`joboet-1/+1
2025-02-07std: move `io` module out of `pal`joboet-85/+0
2025-02-02std: move network code into `sys`joboet-546/+1
As per #117276, this PR moves `sys_common::net` and the `sys::pal::net` into the newly created `sys::net` module. In order to support #135141, I've moved all the current network code into a separate `connection` module, future functions like `hostname` can live in separate modules. I'll probably do a follow-up PR and clean up some of the actual code, this is mostly just a reorganization.
2025-01-24Update a bunch of comments from before wasi support was addedbjorn3-2/+1
2024-12-10Rollup merge of #133184 - osiewicz:wasm-fix-infinite-loop-in-remove-dir-all, ↵León Orell Valerian Liehr-70/+105
r=Noratrieb wasi/fs: Improve stopping condition for <ReadDir as Iterator>::next When upgrading [Zed](https://github.com/zed-industries/zed/pull/19349) to Rust 1.82 I've encountered a test failure in our test suite. Specifically, one of our extension tests started hanging. I've tracked it down to a call to std::fs::remove_dir_all not returning when an extension is compiled with Rust 1.82 Our extension system uses WASM components, thus I've looked at the diff between 1.81 and 1.82 with respect to WASI and found 736f773844e7ebf05ccb827c17b7ad9eb28aa295 As it turned out, calling remove_dir_all from extension returned io::ErrorKind::NotFound in 1.81; the underlying issue is that the ReadDir iterator never actually terminates iteration, however since it loops around, with 1.81 we'd come across an entry second time and fail to remove it, since it would've been removed previously. With 1.82 and 736f773844e7ebf05ccb827c17b7ad9eb28aa295 it is no longer the case, thus we're seeing the hang. The tests do pass when everything but the extensions is compiled with 1.82. This commit makes ReadDir::next adhere to readdir contract, namely it will no longer call readdir once the returned # of bytes is smaller than the size of a passed-in buffer. Previously we'd only terminate the loop if readdir returned 0.
2024-11-30Rollup merge of #133496 - rust-wasi-web:wasi-available-parallelism, r=Amanieu许杰友 Jieyou Xu (Joe)-3/+13
thread::available_parallelism for wasm32-wasip1-threads The target has limited POSIX support and provides the `libc::sysconf` function which allows querying the number of available CPUs.
2024-11-27Implement code reviewSebastian Urban-2/+1
2024-11-26chore: Improve doc commentsPiotr Osiewicz-2/+3
2024-11-26std: update internal uses of `io::const_error!`joboet-4/+3
2024-11-26thread::available_parallelism for wasm32-wasip1-threadsSebastian Urban-3/+14
The target has limited POSIX support and provides the sysconf function which allows querying the number of available CPUs.
2024-11-26Refactor ReadDir into a state machinePiotr Osiewicz-70/+101
2024-11-18wasi/fs: Improve stopping condition for <ReadDir as Iterator>::nextPiotr Osiewicz-14/+17
When upgrading [Zed](https://github.com/zed-industries/zed/pull/19349) to Rust 1.82 I've encountered a test failure in our test suite. Specifically, one of our extension tests started hanging. I've tracked it down to a call to std::fs::remove_dir_all not returning when an extension is compiled with Rust 1.82 Our extension system uses WASM components, thus I've looked at the diff between 1.81 and 1.82 with respect to WASI and found 736f773844e7ebf05ccb827c17b7ad9eb28aa295 As it turned out, calling remove_dir_all from extension returned io::ErrorKind::NotFound in 1.81; the underlying issue is that the ReadDir iterator never actually terminates iteration, however since it loops around, with 1.81 we'd come across an entry second time and fail to remove it, since it would've been removed previously. With 1.82 and 736f773844e7ebf05ccb827c17b7ad9eb28aa295 it is no longer the case, thus we're seeing the hang. This commit makes ReadDir::next adhere to readdir contract, namely it will no longer call readdir once the returned # of bytes is smaller than the size of a passed-in buffer. Previously we'd only terminate the loop if readdir returned 0.
2024-11-14Rollup merge of #132790 - aDotInTheVoid:ioslice-asslice-rides-again, r=cuviperJubilee-1/+6
Add as_slice/into_slice for IoSlice/IoSliceMut. ACP: https://github.com/rust-lang/libs-team/issues/93 Tracking issue: #132818 Based on a623c5233ae7f6b540e5c00f2be02f40b33b0793 (CC `@mpdn)` and #111277 (CC `@Lucretiel).` Closes: #124659 Tracking Issue: TODO try-job: test-various try-job: dist-various-1 try-job: dist-various-2 r? libs
2024-11-09Add as_slice/into_slice for IoSlice/IoSliceMut.Alona Enraght-Moony-1/+6
Co-authored-by: Mike Pedersen <mike@mikepedersen.dk> Co-authored-by: Nathan West <Lucretiel@gmail.com>
2024-10-13Implement file_lock featureChristopher Berner-0/+20
This adds lock(), lock_shared(), try_lock(), try_lock_shared(), and unlock() to File gated behind the file_lock feature flag
2024-09-23std: implement the `random` featurejoboet-12/+2
Implements the ACP https://github.com/rust-lang/libs-team/issues/393.
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-1/+1
2024-09-08Remove needless returns detected by clippy in librariesEduardo Sánchez Muñoz-3/+3
2024-08-29wasi: Fix sleeping for `Duration::MAX`Alex Crichton-30/+31
This commit fixes an assert in the WASI-specific implementation of thread sleep to ensure that sleeping for a very large period of time blocks instead of panicking. This can come up when testing programs that sleep "forever", for example.
2024-08-27std: move allocators to `sys`joboet-2/+0
2024-08-23Rollup merge of #127623 - lolbinarycat:fix_remove_dir_all, r=AmanieuMatthias Krüger-6/+14
fix: fs::remove_dir_all: treat internal ENOENT as success fixes #127576 try-job: test-various
2024-08-22fix: fs::remove_dir_all: treat ENOENT as successbinarycat-6/+14
fixes #127576 windows implementation still needs some work
2024-08-05WASI fixing unsafe_op_in_unsafe_fn for std::{os, sys}Georgii Rylov-16/+22
2024-07-29Reformat `use` declarations.Nicholas Nethercote-20/+9
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-24Rollup merge of #127733 - GrigorenkoPV:don't-forget, r=AmanieuMatthias Krüger-6/+4
Replace some `mem::forget`'s with `ManuallyDrop` > but I would like to see a larger effort to replace all uses of `mem::forget`. _Originally posted by `@saethlin` in https://github.com/rust-lang/rust/issues/127584#issuecomment-2226087767_ So, r? `@saethlin` Sorry, I have finished writing all of this before I got your response.