Skip to content

Commit 6c6fbef

Browse files
authored
Add support for wasm32-wasip1 and wasm32-wasip2, remove support for wasm32-wasi (#499)
The `wasm32-wasi` target will be removed in Rust 1.84 (https://blog.rust-lang.org/2024/04/09/updates-to-rusts-wasi-targets.html) and existing users are encouraged to migrate to either `wasm32-wasip1`, or `wasm32-wasip2`. Strictly speaking, this is a breaking change despite affecting only deprecated target, so it's probably better to release it in `getrandom` v0.3.
1 parent aa13fa5 commit 6c6fbef

File tree

7 files changed

+86
-27
lines changed

7 files changed

+86
-27
lines changed

.cargo/config.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Allow normal use of "cargo run" and "cargo test" on these wasm32 platforms.
22
[target.wasm32-unknown-unknown]
33
runner = 'wasm-bindgen-test-runner'
4-
[target.wasm32-wasi]
4+
[target.wasm32-wasip1]
5+
runner = 'wasmtime'
6+
[target.wasm32-wasip2]
57
runner = 'wasmtime'
68

79
# Just run on node by default (that's where emscripten is tested)

.github/workflows/tests.yml

+10-6
Original file line numberDiff line numberDiff line change
@@ -274,21 +274,25 @@ jobs:
274274
run: cargo test --no-run -Z build-std=std,panic_abort --target=wasm64-unknown-unknown --features=js
275275

276276
wasi-tests:
277-
name: WASI Test
277+
name: WASI Tests
278278
runs-on: ubuntu-22.04
279279
steps:
280280
- uses: actions/checkout@v4
281-
- uses: dtolnay/rust-toolchain@stable
281+
- uses: dtolnay/rust-toolchain@master
282282
with:
283-
targets: wasm32-wasi
284-
- name: Install precompiled wasmtime
283+
toolchain: nightly # TODO: Use stable after 1.82 is released as stable
284+
targets: wasm32-wasip1,wasm32-wasip2
285+
- name: Install Wasmtime
285286
run: |
286-
VERSION=v2.0.0
287+
VERSION=v24.0.0
287288
URL=https://github.com/bytecodealliance/wasmtime/releases/download/${VERSION}/wasmtime-${VERSION}-x86_64-linux.tar.xz
288289
wget -O - $URL | tar -xJ --strip-components=1 -C ~/.cargo/bin
289290
wasmtime --version
290291
- uses: Swatinem/rust-cache@v2
291-
- run: cargo test --target wasm32-wasi
292+
- name: WASI 0.1 Test
293+
run: cargo test --target wasm32-wasip1
294+
- name: WASI 0.2 Test
295+
run: cargo test --target wasm32-wasip2
292296

293297
build-tier2:
294298
name: Tier 2 Build

.github/workflows/workspace.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
# Fixed Nigthly version is used to prevent
2424
# CI failures which are not relevant to PR changes
2525
# on introduction of new Clippy lints.
26-
toolchain: nightly-2024-06-11
26+
toolchain: nightly-2024-09-04
2727
components: clippy,rust-src
2828
- name: std feature
2929
run: cargo clippy --features std
@@ -59,8 +59,10 @@ jobs:
5959
run: cargo clippy -Zbuild-std=core --target x86_64-unknown-redox
6060
- name: VxWorks (vxworks.rs)
6161
run: cargo clippy -Zbuild-std=core --target x86_64-wrs-vxworks
62-
- name: WASI (wasi.rs)
63-
run: cargo clippy -Zbuild-std=core --target wasm32-wasip2
62+
- name: WASI preview 1 (wasi.rs)
63+
run: cargo clippy -Zbuild-std=core --target wasm32-wasip1
64+
- name: WASI preview 2 (wasi.rs)
65+
run: cargo clippy -Zbuild-std=core,alloc --target wasm32-wasip2
6466
- name: Windows 7 (windows7.rs)
6567
run: cargo clippy -Zbuild-std=core --target x86_64-win7-windows-msvc
6668
- name: Windows (windows.rs)
@@ -86,7 +88,7 @@ jobs:
8688
- uses: dtolnay/rust-toolchain@master
8789
with:
8890
# We need Nightly for doc_auto_cfg
89-
toolchain: nightly-2024-06-11
91+
toolchain: nightly-2024-09-04
9092
- uses: Swatinem/rust-cache@v2
9193
- name: Generate Docs
9294
env:

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Breaking Changes
1010
- Update MSRV to 1.38 [#425]
11+
- Remove support of the `wasm32-wasi` target (use `wasm32-wasip1` or `wasm32-wasip2` instead) [#499]
12+
13+
### Added
14+
- `wasm32-wasip1` and `wasm32-wasip2` support [#499]
1115

1216
[#425]: https://github.com/rust-random/getrandom/pull/425
17+
[#499]: https://github.com/rust-random/getrandom/pull/499
1318

1419
## [0.2.15] - 2024-05-06
1520
### Added

Cargo.toml

+11-11
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core"
2121
[target.'cfg(unix)'.dependencies]
2222
libc = { version = "0.2.154", default-features = false }
2323

24-
[target.'cfg(target_os = "wasi")'.dependencies]
24+
[target.'cfg(all(target_arch = "wasm32", target_os = "wasi", target_env = "p1"))'.dependencies]
2525
wasi = { version = "0.11", default-features = false }
2626

27+
[target.'cfg(all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"))'.dependencies]
28+
wasi = { version = "0.13", default-features = false }
29+
2730
[target.'cfg(all(windows, not(target_vendor = "win7")))'.dependencies]
2831
windows-targets = "0.52"
2932

@@ -46,10 +49,7 @@ js = ["wasm-bindgen", "js-sys"]
4649
# Feature to enable custom RNG implementations
4750
custom = []
4851
# Unstable feature to support being a libstd dependency
49-
rustc-dep-of-std = [
50-
"compiler_builtins",
51-
"core",
52-
]
52+
rustc-dep-of-std = ["compiler_builtins", "core"]
5353
# Unstable/test-only feature to run wasm-bindgen tests in a browser
5454
test-in-browser = []
5555

@@ -64,10 +64,10 @@ rustdoc-args = ["--cfg", "docsrs"]
6464
# workaround for https://github.com/cross-rs/cross/issues/1345
6565
[package.metadata.cross.target.x86_64-unknown-netbsd]
6666
pre-build = [
67-
"mkdir -p /tmp/netbsd",
68-
"curl https://cdn.netbsd.org/pub/NetBSD/NetBSD-9.2/amd64/binary/sets/base.tar.xz -O",
69-
"tar -C /tmp/netbsd -xJf base.tar.xz",
70-
"cp /tmp/netbsd/usr/lib/libexecinfo.so /usr/local/x86_64-unknown-netbsd/lib",
71-
"rm base.tar.xz",
72-
"rm -rf /tmp/netbsd",
67+
"mkdir -p /tmp/netbsd",
68+
"curl https://cdn.netbsd.org/pub/NetBSD/NetBSD-9.2/amd64/binary/sets/base.tar.xz -O",
69+
"tar -C /tmp/netbsd -xJf base.tar.xz",
70+
"cp /tmp/netbsd/usr/lib/libexecinfo.so /usr/local/x86_64-unknown-netbsd/lib",
71+
"rm base.tar.xz",
72+
"rm -rf /tmp/netbsd",
7373
]

src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
//! | VxWorks | `*‑wrs‑vxworks‑*` | `randABytes` after checking entropy pool initialization with `randSecure`
2525
//! | ESP-IDF | `*‑espidf` | [`esp_fill_random`]
2626
//! | Emscripten | `*‑emscripten` | [`getentropy`][13]
27-
//! | WASI | `wasm32‑wasi` | [`random_get`]
27+
//! | WASI 0.1 | `wasm32‑wasip1` | [`random_get`]
28+
//! | WASI 0.2 | `wasm32‑wasip2` | [`get-random-u64`]
2829
//! | Web Browser and Node.js | `wasm*‑*‑unknown` | [`Crypto.getRandomValues`] if available, then [`crypto.randomFillSync`] if on Node.js, see [WebAssembly support]
2930
//! | SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes`
3031
//! | Nintendo 3DS | `*-nintendo-3ds` | [`getrandom`][18]
@@ -191,7 +192,8 @@
191192
//! [`cprng_draw`]: https://fuchsia.dev/fuchsia-src/zircon/syscalls/cprng_draw
192193
//! [`crypto.randomFillSync`]: https://nodejs.org/api/crypto.html#cryptorandomfillsyncbuffer-offset-size
193194
//! [`esp_fill_random`]: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html#_CPPv415esp_fill_randomPv6size_t
194-
//! [`random_get`]: https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#-random_getbuf-pointeru8-buf_len-size---errno
195+
//! [`random_get`]: https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-random_getbuf-pointeru8-buf_len-size---errno
196+
//! [`get-random-u64`]: https://github.com/WebAssembly/WASI/blob/v0.2.1/wasip2/random/random.wit#L23-L28
195197
//! [WebAssembly support]: #webassembly-support
196198
//! [`wasm-bindgen`]: https://github.com/rustwasm/wasm-bindgen
197199
//! [`module`]: https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-js-imports/module.html

src/wasi.rs

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
1-
//! Implementation for WASI
1+
//! Implementation for WASI (preview 1 and 2)
2+
//!
3+
//! `target_env = "p1"` was introduced only in Rust 1.80, so on earlier compiler versions this
4+
//! code will result in a compilation error.
25
use crate::Error;
36
use core::mem::MaybeUninit;
4-
use wasi::random_get;
57

8+
#[cfg(not(any(target_env = "p1", target_env = "p2")))]
9+
compile_error!(
10+
"Unknown version of WASI (only previews 1 and 2 are supported) \
11+
or Rust version older than 1.80 was used"
12+
);
13+
14+
#[cfg(target_env = "p1")]
615
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
7-
unsafe { random_get(dest.as_mut_ptr().cast::<u8>(), dest.len()) }
16+
unsafe { wasi::random_get(dest.as_mut_ptr().cast::<u8>(), dest.len()) }
817
.map_err(|e| Error::from_os_error(e.raw().into()))
918
}
19+
20+
#[cfg(target_env = "p2")]
21+
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
22+
use core::ptr::copy_nonoverlapping;
23+
use wasi::random::random::get_random_u64;
24+
25+
let (prefix, chunks, suffix) = unsafe { dest.align_to_mut::<MaybeUninit<u64>>() };
26+
27+
// We use `get_random_u64` instead of `get_random_bytes` because the latter creates
28+
// an allocation due to the Wit IDL [restrictions][0]. This should be fine since
29+
// the main use case of `getrandom` is seed generation.
30+
//
31+
// [0]: https://github.com/WebAssembly/wasi-random/issues/27
32+
if !prefix.is_empty() {
33+
let val = get_random_u64();
34+
let src = (&val as *const u64).cast();
35+
unsafe {
36+
copy_nonoverlapping(src, prefix.as_mut_ptr(), prefix.len());
37+
}
38+
}
39+
40+
for dst in chunks {
41+
dst.write(get_random_u64());
42+
}
43+
44+
if !suffix.is_empty() {
45+
let val = get_random_u64();
46+
let src = (&val as *const u64).cast();
47+
unsafe {
48+
copy_nonoverlapping(src, suffix.as_mut_ptr(), suffix.len());
49+
}
50+
}
51+
52+
Ok(())
53+
}

0 commit comments

Comments
 (0)