Skip to content

Commit 5ee4fd4

Browse files
committed
Add support for wasm32-wasip1 and wasm32-wasip2, remove support for wasm32-wasi
1 parent aa13fa5 commit 5ee4fd4

File tree

5 files changed

+74
-14
lines changed

5 files changed

+74
-14
lines changed

.github/workflows/tests.yml

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,22 +273,40 @@ jobs:
273273
# does not yet support memory64.
274274
run: cargo test --no-run -Z build-std=std,panic_abort --target=wasm64-unknown-unknown --features=js
275275

276-
wasi-tests:
277-
name: WASI Test
276+
wasip1-tests:
277+
name: WASI Preview 1 Test
278278
runs-on: ubuntu-22.04
279279
steps:
280280
- uses: actions/checkout@v4
281-
- uses: dtolnay/rust-toolchain@stable
281+
- uses: dtolnay/rust-toolchain@nightly
282282
with:
283-
targets: wasm32-wasi
283+
targets: wasm32-wasip1
284+
- name: Install precompiled wasmtime
285+
run: |
286+
VERSION=v24.0.0
287+
URL=https://github.com/bytecodealliance/wasmtime/releases/download/${VERSION}/wasmtime-${VERSION}-x86_64-linux.tar.xz
288+
wget -O - $URL | tar -xJ --strip-components=1 -C ~/.cargo/bin
289+
wasmtime --version
290+
- uses: Swatinem/rust-cache@v2
291+
- run: cargo test --no-default-features --target wasm32-wasip1
292+
293+
wasip2-tests:
294+
name: WASI Preview 2 Test
295+
runs-on: ubuntu-22.04
296+
steps:
297+
- uses: actions/checkout@v4
298+
- uses: dtolnay/rust-toolchain@nightly
299+
with:
300+
targets: wasm32-wasip2
301+
components: rust-src
284302
- name: Install precompiled wasmtime
285303
run: |
286-
VERSION=v2.0.0
304+
VERSION=v24.0.0
287305
URL=https://github.com/bytecodealliance/wasmtime/releases/download/${VERSION}/wasmtime-${VERSION}-x86_64-linux.tar.xz
288306
wget -O - $URL | tar -xJ --strip-components=1 -C ~/.cargo/bin
289307
wasmtime --version
290308
- uses: Swatinem/rust-cache@v2
291-
- run: cargo test --target wasm32-wasi
309+
- run: cargo test --no-default-features --target wasm32-wasip2 -Z build-std=core,alloc
292310

293311
build-tier2:
294312
name: Tier 2 Build

.github/workflows/workspace.yml

Lines changed: 6 additions & 4 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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_os = "wasi", target_env = "p1"))'.dependencies]
2525
wasi = { version = "0.11", default-features = false }
2626

27+
[target.'cfg(all(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

src/wasi.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
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 wasi::random::random::get_random_u64;
23+
24+
let mut chunks = dest.chunks_exact_mut(size_of::<u64>());
25+
for chunk in &mut chunks {
26+
let dst: *mut u64 = chunk.as_mut_ptr().cast();
27+
let val = get_random_u64();
28+
unsafe {
29+
core::ptr::write_unaligned(dst, val);
30+
}
31+
}
32+
33+
let rem = chunks.into_remainder();
34+
let val = get_random_u64();
35+
let src = &val as *const u64;
36+
unsafe {
37+
core::ptr::copy_nonoverlapping(src.cast(), rem.as_mut_ptr(), rem.len());
38+
}
39+
40+
Ok(())
41+
}

0 commit comments

Comments
 (0)