Skip to content

Commit

Permalink
Add support for wasm32-wasip1 and wasm32-wasip2, remove support f…
Browse files Browse the repository at this point in the history
…or `wasm32-wasi`
  • Loading branch information
newpavlov committed Sep 5, 2024
1 parent aa13fa5 commit 5ee4fd4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
30 changes: 24 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -273,22 +273,40 @@ jobs:
# does not yet support memory64.
run: cargo test --no-run -Z build-std=std,panic_abort --target=wasm64-unknown-unknown --features=js

wasi-tests:
name: WASI Test
wasip1-tests:
name: WASI Preview 1 Test
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
targets: wasm32-wasi
targets: wasm32-wasip1
- name: Install precompiled wasmtime
run: |
VERSION=v24.0.0
URL=https://github.com/bytecodealliance/wasmtime/releases/download/${VERSION}/wasmtime-${VERSION}-x86_64-linux.tar.xz
wget -O - $URL | tar -xJ --strip-components=1 -C ~/.cargo/bin
wasmtime --version
- uses: Swatinem/rust-cache@v2
- run: cargo test --no-default-features --target wasm32-wasip1

wasip2-tests:
name: WASI Preview 2 Test
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
with:
targets: wasm32-wasip2
components: rust-src
- name: Install precompiled wasmtime
run: |
VERSION=v2.0.0
VERSION=v24.0.0
URL=https://github.com/bytecodealliance/wasmtime/releases/download/${VERSION}/wasmtime-${VERSION}-x86_64-linux.tar.xz
wget -O - $URL | tar -xJ --strip-components=1 -C ~/.cargo/bin
wasmtime --version
- uses: Swatinem/rust-cache@v2
- run: cargo test --target wasm32-wasi
- run: cargo test --no-default-features --target wasm32-wasip2 -Z build-std=core,alloc

build-tier2:
name: Tier 2 Build
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# Fixed Nigthly version is used to prevent
# CI failures which are not relevant to PR changes
# on introduction of new Clippy lints.
toolchain: nightly-2024-06-11
toolchain: nightly-2024-09-04
components: clippy,rust-src
- name: std feature
run: cargo clippy --features std
Expand Down Expand Up @@ -59,8 +59,10 @@ jobs:
run: cargo clippy -Zbuild-std=core --target x86_64-unknown-redox
- name: VxWorks (vxworks.rs)
run: cargo clippy -Zbuild-std=core --target x86_64-wrs-vxworks
- name: WASI (wasi.rs)
run: cargo clippy -Zbuild-std=core --target wasm32-wasip2
- name: WASI preview 1 (wasi.rs)
run: cargo clippy -Zbuild-std=core --target wasm32-wasip1
- name: WASI preview 2 (wasi.rs)
run: cargo clippy -Zbuild-std=core,alloc --target wasm32-wasip2
- name: Windows 7 (windows7.rs)
run: cargo clippy -Zbuild-std=core --target x86_64-win7-windows-msvc
- name: Windows (windows.rs)
Expand All @@ -86,7 +88,7 @@ jobs:
- uses: dtolnay/rust-toolchain@master
with:
# We need Nightly for doc_auto_cfg
toolchain: nightly-2024-06-11
toolchain: nightly-2024-09-04
- uses: Swatinem/rust-cache@v2
- name: Generate Docs
env:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Breaking Changes
- Update MSRV to 1.38 [#425]
- Remove support of the `wasm32-wasi` target (use `wasm32-wasip1` or `wasm32-wasip2` instead) [#499]

### Added
- `wasm32-wasip1` and `wasm32-wasip2` support [#499]

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

## [0.2.15] - 2024-05-06
### Added
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core"
[target.'cfg(unix)'.dependencies]
libc = { version = "0.2.154", default-features = false }

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

[target.'cfg(all(target_os = "wasi", target_env = "p2"))'.dependencies]
wasi = { version = "0.13", default-features = false }

[target.'cfg(all(windows, not(target_vendor = "win7")))'.dependencies]
windows-targets = "0.52"

Expand Down
38 changes: 35 additions & 3 deletions src/wasi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
//! Implementation for WASI
//! Implementation for WASI (preview 1 and 2)
//!
//! `target_env = "p1"` was introduced only in Rust 1.80, so on earlier compiler versions this
//! code will result in a compilation error.
use crate::Error;
use core::mem::MaybeUninit;
use wasi::random_get;

#[cfg(not(any(target_env = "p1", target_env = "p2")))]
compile_error!(
"Unknown version of WASI (only previews 1 and 2 are supported) \
or Rust version older than 1.80 was used"
);

#[cfg(target_env = "p1")]
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
unsafe { random_get(dest.as_mut_ptr().cast::<u8>(), dest.len()) }
unsafe { wasi::random_get(dest.as_mut_ptr().cast::<u8>(), dest.len()) }
.map_err(|e| Error::from_os_error(e.raw().into()))
}

#[cfg(target_env = "p2")]
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
use wasi::random::random::get_random_u64;

let mut chunks = dest.chunks_exact_mut(size_of::<u64>());
for chunk in &mut chunks {
let dst: *mut u64 = chunk.as_mut_ptr().cast();
let val = get_random_u64();
unsafe {
core::ptr::write_unaligned(dst, val);
}
}

let rem = chunks.into_remainder();
let val = get_random_u64();
let src = &val as *const u64;
unsafe {
core::ptr::copy_nonoverlapping(src.cast(), rem.as_mut_ptr(), rem.len());
}

Ok(())
}

0 comments on commit 5ee4fd4

Please sign in to comment.