Skip to content

Commit 68b4d7a

Browse files
authored
Merge pull request #303 from rust-random/wasm64
Add wasm64-unknown-unknown support
2 parents f2d7662 + 3fa761b commit 68b4d7a

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

.github/workflows/tests.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,24 @@ jobs:
219219
- name: Test (custom getrandom)
220220
run: cargo test --target=wasm32-unknown-unknown --features=custom
221221

222+
wasm64-tests:
223+
name: WASM memory64
224+
runs-on: ubuntu-latest
225+
steps:
226+
- uses: actions/checkout@v2
227+
- uses: actions-rs/toolchain@v1
228+
with:
229+
profile: minimal
230+
toolchain: nightly
231+
components: rust-src
232+
override: true
233+
- uses: Swatinem/rust-cache@v1
234+
- name: Build and Link tests (build-std)
235+
# This target is Tier 3, so we have to build libstd ourselves.
236+
# We currently cannot run these tests because wasm-bindgen-test-runner
237+
# does not yet support memory64.
238+
run: cargo test --no-run -Z build-std=std,panic_abort --target=wasm64-unknown-unknown --features=js
239+
222240
wasi-tests:
223241
name: WASI test
224242
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ libc = { version = "0.2.128", default-features = false }
2323
[target.'cfg(target_os = "wasi")'.dependencies]
2424
wasi = { version = "0.11", default-features = false }
2525

26-
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
26+
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dependencies]
2727
wasm-bindgen = { version = "0.2.62", default-features = false, optional = true }
2828
js-sys = { version = "0.3", optional = true }
29-
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies]
29+
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dev-dependencies]
3030
wasm-bindgen-test = "0.3.18"
3131

3232
[features]
3333
# Implement std-only traits for getrandom::Error
3434
std = []
3535
# Feature to enable fallback RDRAND-based implementation on x86/x86_64
3636
rdrand = []
37-
# Feature to enable JavaScript bindings on wasm32-unknown-unknown
37+
# Feature to enable JavaScript bindings on wasm*-unknown-unknown
3838
js = ["wasm-bindgen", "js-sys"]
3939
# Feature to enable custom RNG implementations
4040
custom = []

src/js.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
1616
// Size of our temporary Uint8Array buffer used with WebCrypto methods
1717
// Maximum is 65536 bytes see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
1818
const WEB_CRYPTO_BUFFER_SIZE: usize = 256;
19+
// Node.js's crypto.randomFillSync requires the size to be less than 2**31.
20+
const NODE_MAX_BUFFER_SIZE: usize = (1 << 31) - 1;
1921

2022
enum RngSource {
2123
Node(NodeCrypto),
@@ -38,8 +40,10 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
3840
// have to ensure the memory in `dest` is initialized.
3941
let dest = uninit_slice_fill_zero(dest);
4042

41-
if n.random_fill_sync(dest).is_err() {
42-
return Err(Error::NODE_RANDOM_FILL_SYNC);
43+
for chunk in dest.chunks_mut(NODE_MAX_BUFFER_SIZE) {
44+
if n.random_fill_sync(chunk).is_err() {
45+
return Err(Error::NODE_RANDOM_FILL_SYNC);
46+
}
4347
}
4448
}
4549
RngSource::Web(crypto, buf) => {

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//! | ESP-IDF | `*‑espidf` | [`esp_fill_random`]
3131
//! | Emscripten | `*‑emscripten` | `/dev/urandom` (identical to `/dev/random`)
3232
//! | WASI | `wasm32‑wasi` | [`random_get`]
33-
//! | Web Browser and Node.js | `wasm32‑*‑unknown` | [`Crypto.getRandomValues`] if available, then [`crypto.randomFillSync`] if on Node.js, see [WebAssembly support]
33+
//! | Web Browser and Node.js | `wasm*‑*‑unknown` | [`Crypto.getRandomValues`] if available, then [`crypto.randomFillSync`] if on Node.js, see [WebAssembly support]
3434
//! | SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes`
3535
//! | Nintendo 3DS | `armv6k-nintendo-3ds` | [`getrandom`][1]
3636
//!
@@ -262,12 +262,14 @@ cfg_if! {
262262
any(target_arch = "x86_64", target_arch = "x86")))] {
263263
#[path = "rdrand.rs"] mod imp;
264264
} else if #[cfg(all(feature = "js",
265-
target_arch = "wasm32", target_os = "unknown"))] {
265+
any(target_arch = "wasm32", target_arch = "wasm64"),
266+
target_os = "unknown"))] {
266267
#[path = "js.rs"] mod imp;
267268
} else if #[cfg(feature = "custom")] {
268269
use custom as imp;
269-
} else if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
270-
compile_error!("the wasm32-unknown-unknown target is not supported by \
270+
} else if #[cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"),
271+
target_os = "unknown"))] {
272+
compile_error!("the wasm*-unknown-unknown targets are not supported by \
271273
default, you may need to enable the \"js\" feature. \
272274
For more information see: \
273275
https://docs.rs/getrandom/#webassembly-support");

0 commit comments

Comments
 (0)