From e2bd690c0c1bcf4fd9e1cb057a2ea10136c4190d Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 1 Jan 2025 10:41:49 +0000 Subject: [PATCH 01/13] Move [features] section up Cargo.toml --- Cargo.toml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 57fe2274..e2241b32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,13 @@ repository = "https://github.com/rust-random/getrandom" categories = ["os", "no-std"] exclude = [".*"] +[features] +# Implement std::error::Error for getrandom::Error and +# use std to retrieve OS error descriptions +std = [] +# Unstable feature to support being a libstd dependency +rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"] + [dependencies] cfg-if = "1" @@ -70,13 +77,6 @@ js-sys = { version = "0.3.75", default-features = false } [target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies] wasm-bindgen-test = "0.3" -[features] -# Implement std::error::Error for getrandom::Error and -# use std to retrieve OS error descriptions -std = [] -# Unstable feature to support being a libstd dependency -rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"] - [lints.rust.unexpected_cfgs] level = "warn" check-cfg = [ From d143e98ac36591ad7298f200e99c461b5899b46b Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 1 Jan 2025 11:19:08 +0000 Subject: [PATCH 02/13] Add Rustix feature flag Size of a dependency's Cargo.lock: 33 -> 29 --- .github/workflows/tests.yml | 3 +++ Cargo.toml | 8 ++++++-- README.md | 2 +- src/backends.rs | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 673fd370..fe41178b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -59,6 +59,9 @@ jobs: - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_rustix" run: cargo test --target=${{ matrix.target }} --features=std + - env: + RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_rustix" + run: cargo test --target=${{ matrix.target }} --features=rustix,std - env: RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback run: cargo test --features=std diff --git a/Cargo.toml b/Cargo.toml index e2241b32..31757c3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,10 @@ std = [] # Unstable feature to support being a libstd dependency rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"] +# Optional backend: linux_rustix +# This flag only enables the backend; to use it you also need: getrandom_backend=linux_rustix +rustix = ["dep:rustix"] + [dependencies] cfg-if = "1" @@ -26,12 +30,12 @@ compiler_builtins = { version = "0.1", optional = true } core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" } # linux_android / linux_android_with_fallback -[target.'cfg(all(any(target_os = "linux", target_os = "android"), not(any(target_env = "", getrandom_backend = "linux_rustix", getrandom_backend = "custom"))))'.dependencies] +[target.'cfg(all(any(target_os = "linux", target_os = "android"), not(any(target_env = "", getrandom_backend = "custom"))))'.dependencies] libc = { version = "0.2.154", default-features = false } # linux_rustix [target.'cfg(all(any(target_os = "linux", target_os = "android"), any(target_env = "", getrandom_backend = "linux_rustix")))'.dependencies] -rustix = { version = "0.38.38", default-features = false, features = ["rand"] } +rustix = { version = "0.38.38", default-features = false, features = ["rand"], optional = true } # apple-other [target.'cfg(any(target_os = "ios", target_os = "visionos", target_os = "watchos", target_os = "tvos"))'.dependencies] diff --git a/README.md b/README.md index 7ece9ecf..71b1d561 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ of randomness based on their specific needs: | Backend name | Target | Target Triple | Implementation | ----------------- | -------------------- | ------------------------ | -------------- | `linux_getrandom` | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call (without `/dev/urandom` fallback). Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow). -| `linux_rustix` | Linux, Android | `*‑linux‑*` | Same as `linux_getrandom`, but uses [`rustix`] instead of `libc`. +| `linux_rustix` | Linux, Android | `*‑linux‑*` | Same as `linux_getrandom`, but uses [`rustix`] instead of `libc`. Requires feature `rustix`. | `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction | `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register | `esp_idf` | ESP-IDF | `*‑espidf` | [`esp_fill_random`]. WARNING: can return low-quality entropy without proper hardware configuration! diff --git a/src/backends.rs b/src/backends.rs index f7b720f5..f580aca5 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -13,7 +13,7 @@ cfg_if! { } else if #[cfg(getrandom_backend = "linux_getrandom")] { mod linux_android; pub use linux_android::*; - } else if #[cfg(getrandom_backend = "linux_rustix")] { + } else if #[cfg(all(feature = "rustix", getrandom_backend = "linux_rustix"))] { mod linux_rustix; pub use linux_rustix::*; } else if #[cfg(getrandom_backend = "rdrand")] { From ef12a32661bf6e4d182e113fa70893ff3c5bc61c Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 1 Jan 2025 16:48:19 +0000 Subject: [PATCH 03/13] Use Rustix by default on Linux/Android with features=rustix --- Cargo.toml | 5 +++-- src/backends.rs | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 31757c3d..640ece13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,8 @@ std = [] rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"] # Optional backend: linux_rustix -# This flag only enables the backend; to use it you also need: getrandom_backend=linux_rustix +# This flag enables the backend and uses it by default on Linux/Android. +# The backend may still be overridden by setting getrandom_backend. rustix = ["dep:rustix"] [dependencies] @@ -34,7 +35,7 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" libc = { version = "0.2.154", default-features = false } # linux_rustix -[target.'cfg(all(any(target_os = "linux", target_os = "android"), any(target_env = "", getrandom_backend = "linux_rustix")))'.dependencies] +[target.'cfg(all(any(target_os = "linux", target_os = "android")))'.dependencies] rustix = { version = "0.38.38", default-features = false, features = ["rand"], optional = true } # apple-other diff --git a/src/backends.rs b/src/backends.rs index f580aca5..55ab3ef8 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -55,6 +55,9 @@ cfg_if! { ))] { mod getrandom; pub use getrandom::*; + } else if #[cfg(all(feature = "rustix", any(target_os = "android", target_os = "linux")))] { + mod linux_rustix; + pub use linux_rustix::*; } else if #[cfg(any( // Rust supports Android API level 19 (KitKat) [0] and the next upgrade targets // level 21 (Lollipop) [1], while `getrandom(2)` was added only in From 217483b66369264a56753468ef18499ed3cde6d4 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 1 Jan 2025 17:12:27 +0000 Subject: [PATCH 04/13] Add JS feature flag Size of a dependency's Cargo.lock: 29 -> 16 --- .github/workflows/tests.yml | 5 ++--- Cargo.toml | 15 ++++++++++----- README.md | 11 ++++++----- src/backends.rs | 22 +++++++++++++--------- src/error.rs | 2 +- tests/mod.rs | 2 +- 6 files changed, 33 insertions(+), 24 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index fe41178b..a104d5f6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -241,15 +241,14 @@ jobs: - { description: Web, version: stable, - flags: -Dwarnings --cfg getrandom_backend="wasm_js", - args: --features=std, + args: '--features=std,js', } - { description: Web with Atomics, version: nightly, components: rust-src, flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory', - args: '--features=std -Zbuild-std=panic_abort,std', + args: '--features=std -Zbuild-std=panic_abort,std,js', } steps: - uses: actions/checkout@v4 diff --git a/Cargo.toml b/Cargo.toml index 640ece13..c3c46480 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,11 @@ rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"] # The backend may still be overridden by setting getrandom_backend. rustix = ["dep:rustix"] +# Optional backend: wasm_js +# This flag enables the backend and uses it by default on wasm32 with unknown OS. +# The backend may still be overridden by setting getrandom_backend. +js = ["dep:wasm-bindgen", "dep:js-sys"] + [dependencies] cfg-if = "1" @@ -75,11 +80,11 @@ wasi = { version = "0.13", default-features = false } windows-targets = "0.52" # wasm_js -[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies] -wasm-bindgen = { version = "0.2.98", default-features = false } -[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies] -js-sys = { version = "0.3.75", default-features = false } -[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies] +[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies] +wasm-bindgen = { version = "0.2.98", default-features = false, optional = true } +[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies] +js-sys = { version = "0.3.75", default-features = false, optional = true } +[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies] wasm-bindgen-test = "0.3" [lints.rust.unexpected_cfgs] diff --git a/README.md b/README.md index 71b1d561..d1b54a31 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ of randomness based on their specific needs: | `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction | `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register | `esp_idf` | ESP-IDF | `*‑espidf` | [`esp_fill_random`]. WARNING: can return low-quality entropy without proper hardware configuration! -| `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`] +| `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`]. Requires feature `js`. | `custom` | All targets | `*` | User-provided custom implementation (see [custom backend]) Opt-in backends can be enabled using the `getrandom_backend` configuration flag. @@ -113,17 +113,18 @@ the `wasm32-unknown-unknown` target (i.e. the target used by `wasm-pack`) is not automatically supported since, from the target name alone, we cannot deduce which JavaScript interface should be used (or if JavaScript is available at all). -Instead, *if the `wasm_js` backend is enabled*, this crate will assume +Instead, *if the `js` feature is enabled*, this crate will assume that you are building for an environment containing JavaScript, and will call the appropriate Web Crypto methods [described above](#opt-in-backends) using -the [`wasm-bindgen`] toolchain. Both web browser (main window and Web Workers) +the [`wasm-bindgen`] toolchain (with or without using `getrandom_backend=wasm_js`). +Both web browser (main window and Web Workers) and Node.js (v19 or later) environments are supported. To enable the `wasm_js` backend, you can add the following lines to your project's `.cargo/config.toml` file: ```toml -[target.wasm32-unknown-unknown] -rustflags = ['--cfg', 'getrandom_backend="wasm_js"'] +[dependencies] +getrandom = { version = "0.3", features = ["js"] } ``` ### Custom backend diff --git a/src/backends.rs b/src/backends.rs index 55ab3ef8..0ba2015d 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -22,7 +22,7 @@ cfg_if! { } else if #[cfg(getrandom_backend = "rndr")] { mod rndr; pub use rndr::*; - } else if #[cfg(getrandom_backend = "wasm_js")] { + } else if #[cfg(all(feature = "js", getrandom_backend = "wasm_js"))] { mod wasm_js; pub use wasm_js::*; } else if #[cfg(getrandom_backend = "esp_idf")] { @@ -133,6 +133,18 @@ cfg_if! { ); } } + } else if #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))] { + cfg_if! { + if #[cfg(feature = "js")] { + mod wasm_js; + pub use wasm_js::*; + } else { + compile_error!("the wasm32-unknown-unknown targets are not supported \ + by default, you may need to enable the \"js\" \ + feature flag. For more information see: \ + https://docs.rs/getrandom/#webassembly-support"); + } + } } else if #[cfg(target_os = "hermit")] { mod hermit; pub use hermit::*; @@ -151,14 +163,6 @@ cfg_if! { } else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] { mod rdrand; pub use rdrand::*; - } else if #[cfg(all( - target_arch = "wasm32", - any(target_os = "unknown", target_os = "none") - ))] { - compile_error!("the wasm32-unknown-unknown targets are not supported \ - by default, you may need to enable the \"wasm_js\" \ - configuration flag. For more information see: \ - https://docs.rs/getrandom/#webassembly-support"); } else { compile_error!("target is not supported. You may need to define \ a custom backend see: \ diff --git a/src/error.rs b/src/error.rs index 4004af57..aafff56c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -118,7 +118,7 @@ impl Error { Error::WINDOWS_PROCESS_PRNG => "ProcessPrng: Windows system function failure", #[cfg(all(windows, target_vendor = "win7"))] Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure", - #[cfg(getrandom_backend = "wasm_js")] + #[cfg(all(feature = "js", getrandom_backend = "wasm_js"))] Error::WEB_CRYPTO => "Web Crypto API is unavailable", #[cfg(target_os = "vxworks")] Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized", diff --git a/tests/mod.rs b/tests/mod.rs index c972e768..fac1f81d 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -2,7 +2,7 @@ use core::mem::MaybeUninit; use getrandom::{fill, fill_uninit}; #[cfg(all( - getrandom_backend = "wasm_js", + feature = "js", target_arch = "wasm32", target_os = "unknown" ))] From a94fe508659afead9ba65e5b1a2de0c3e038ed1a Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 3 Jan 2025 08:55:32 +0000 Subject: [PATCH 05/13] CI: use feature js where required --- .github/workflows/build.yml | 6 +++--- .github/workflows/workspace.yml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4a251776..c68487f6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -140,8 +140,8 @@ jobs: fail-fast: false matrix: target: [ - { description: Web, target: wasm32-unknown-unknown }, - { description: WasmV1, target: wasm32v1-none }, + { description: Web, target: wasm32-unknown-unknown, feature: "--features js" }, + { description: WasmV1, target: wasm32v1-none, feature: "" }, ] feature: [ { description: no_std, feature: "", build-std: "core,alloc", std: false }, @@ -163,7 +163,7 @@ jobs: components: rust-src - uses: Swatinem/rust-cache@v2 - name: Build - run: cargo build --target ${{ matrix.target.target }} ${{ matrix.feature.feature }} -Zbuild-std=${{ matrix.feature.build-std }} + run: cargo build --target ${{ matrix.target.target }} ${{ matrix.target.feature }} ${{ matrix.feature.feature }} -Zbuild-std=${{ matrix.feature.build-std }} rdrand-uefi: name: RDRAND UEFI diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index 7f62034a..b41ec359 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -48,11 +48,11 @@ jobs: - name: Web WASM (wasm_js.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" - run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown + run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features js - name: Web WASM with atomics (wasm_js.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory - run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown + run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features js - name: Linux (linux_android.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" From 03b00f138e7756deef34c4edfef9c2e59d8dcc5a Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 3 Jan 2025 09:05:42 +0000 Subject: [PATCH 06/13] Remove feature rustix --- Cargo.toml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0e7e8b1c..639c906f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,11 +18,6 @@ std = [] # Unstable feature to support being a libstd dependency rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"] -# Optional backend: linux_rustix -# This flag enables the backend and uses it by default on Linux/Android. -# The backend may still be overridden by setting getrandom_backend. -rustix = ["dep:rustix"] - # Optional backend: wasm_js # This flag enables the backend and uses it by default on wasm32 with unknown OS. # The backend may still be overridden by setting getrandom_backend. From 375ba77053b237fe99a6734f3b7bd8d4bf68899a Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 3 Jan 2025 09:28:14 +0000 Subject: [PATCH 07/13] CI: WasmV1 also needs js --- .github/workflows/build.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6d9e9189..096936f8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -137,12 +137,12 @@ jobs: fail-fast: false matrix: target: [ - { description: Web, target: wasm32-unknown-unknown, feature: "--features js" }, - { description: WasmV1, target: wasm32v1-none, feature: "" }, + { description: Web, target: wasm32-unknown-unknown }, + { description: WasmV1, target: wasm32v1-none }, ] feature: [ - { description: no_std, feature: "", build-std: "core,alloc", std: false }, - { feature: --features std, build-std: "panic_abort,std", std: true }, + { description: no_std, feature: "--features js", build-std: "core,alloc", std: false }, + { feature: --features js,std, build-std: "panic_abort,std", std: true }, ] atomic: [ { flags: "" }, @@ -160,7 +160,7 @@ jobs: components: rust-src - uses: Swatinem/rust-cache@v2 - name: Build - run: cargo build --target ${{ matrix.target.target }} ${{ matrix.target.feature }} ${{ matrix.feature.feature }} -Zbuild-std=${{ matrix.feature.build-std }} + run: cargo build --target ${{ matrix.target.target }} ${{ matrix.feature.feature }} -Zbuild-std=${{ matrix.feature.build-std }} rdrand-uefi: name: RDRAND UEFI From 991cbdcfabb89963115c5e1e61c314940a72227d Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 3 Jan 2025 09:35:21 +0000 Subject: [PATCH 08/13] cargo fmt --- tests/mod.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/mod.rs b/tests/mod.rs index fac1f81d..03247f31 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,11 +1,7 @@ use core::mem::MaybeUninit; use getrandom::{fill, fill_uninit}; -#[cfg(all( - feature = "js", - target_arch = "wasm32", - target_os = "unknown" -))] +#[cfg(all(feature = "js", target_arch = "wasm32", target_os = "unknown"))] use wasm_bindgen_test::wasm_bindgen_test as test; #[test] From eb060c316b011f9af539122b8dda8e5195b6f4bf Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 3 Jan 2025 09:36:13 +0000 Subject: [PATCH 09/13] CI: remove rustix job --- .github/workflows/tests.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cf1f7dfc..d27fd690 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -56,9 +56,6 @@ jobs: - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" run: cargo test --target=${{ matrix.target }} --features=std - - env: - RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_rustix" - run: cargo test --target=${{ matrix.target }} --features=rustix,std - env: RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback run: cargo test --features=std From f3c8a3884224503c1db543deeefa1f54a0af55b3 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 3 Jan 2025 09:37:21 +0000 Subject: [PATCH 10/13] CI: formatting fix --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 096936f8..f2e0c480 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -142,7 +142,7 @@ jobs: ] feature: [ { description: no_std, feature: "--features js", build-std: "core,alloc", std: false }, - { feature: --features js,std, build-std: "panic_abort,std", std: true }, + { feature: "--features js,std", build-std: "panic_abort,std", std: true }, ] atomic: [ { flags: "" }, From 43ca98e8a9dde6fc84b192407baf05bf1c85d27b Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 3 Jan 2025 09:42:41 +0000 Subject: [PATCH 11/13] CI fix --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d27fd690..c03495bd 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -244,7 +244,7 @@ jobs: version: nightly, components: rust-src, flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory', - args: '--features=std -Zbuild-std=panic_abort,std,js', + args: '--features=std,js -Zbuild-std=panic_abort,std', } steps: - uses: actions/checkout@v4 From 0bf8b1b28e3c539f5f4e27509e98b5690b8f2d55 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 22 Jan 2025 16:05:31 +0000 Subject: [PATCH 12/13] Rename feature js -> wasm_js and remove auto opt-in --- .github/workflows/build.yml | 4 ++-- .github/workflows/tests.yml | 4 ++-- .github/workflows/workspace.yml | 4 ++-- Cargo.toml | 6 +++--- README.md | 26 +++++++++++++------------- src/backends.rs | 22 +++++++++------------- src/error.rs | 2 +- tests/mod.rs | 2 +- 8 files changed, 33 insertions(+), 37 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5c340c33..707ac362 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -142,8 +142,8 @@ jobs: { description: WasmV1, target: wasm32v1-none }, ] feature: [ - { description: no_std, feature: "--features js", build-std: "core,alloc", std: false }, - { feature: "--features js,std", build-std: "panic_abort,std", std: true }, + { description: no_std, feature: "--features wasm_js", build-std: "core,alloc", std: false }, + { feature: "--features wasm_js,std", build-std: "panic_abort,std", std: true }, ] atomic: [ { flags: "" }, diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c03495bd..57cf9e87 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -237,14 +237,14 @@ jobs: - { description: Web, version: stable, - args: '--features=std,js', + args: '--features=std,wasm_js', } - { description: Web with Atomics, version: nightly, components: rust-src, flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory', - args: '--features=std,js -Zbuild-std=panic_abort,std', + args: '--features=std,wasm_js -Zbuild-std=panic_abort,std', } steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index 994db90e..14b37dd9 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -48,11 +48,11 @@ jobs: - name: Web WASM (wasm_js.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" - run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features js + run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features wasm_js - name: Web WASM with atomics (wasm_js.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory - run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features js + run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features wasm_js - name: Linux (linux_android.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" diff --git a/Cargo.toml b/Cargo.toml index 4c795c23..8e6d0e6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,9 +19,9 @@ std = [] rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"] # Optional backend: wasm_js -# This flag enables the backend and uses it by default on wasm32 with unknown OS. -# The backend may still be overridden by setting getrandom_backend. -js = ["dep:wasm-bindgen", "dep:js-sys"] +# This flag enables the backend but does not select it. To use the backend, use +# this flag *and* set getrandom_backend=wasm_js (see README). +wasm_js = ["dep:wasm-bindgen", "dep:js-sys"] [dependencies] cfg-if = "1" diff --git a/README.md b/README.md index 6e7e3e28..0007634c 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ of randomness based on their specific needs: | `linux_getrandom` | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call (without `/dev/urandom` fallback). Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow). | `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction | `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register -| `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`]. Requires feature `js`. +| `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`]. Requires feature `wasm_js` ([see below](#webassembly-support)). | `custom` | All targets | `*` | User-provided custom implementation (see [custom backend]) Opt-in backends can be enabled using the `getrandom_backend` configuration flag. @@ -112,19 +112,19 @@ the `wasm32-unknown-unknown` target (i.e. the target used by `wasm-pack`) is not automatically supported since, from the target name alone, we cannot deduce which JavaScript interface should be used (or if JavaScript is available at all). -Instead, *if the `js` feature is enabled*, this crate will assume -that you are building for an environment containing JavaScript, and will -call the appropriate Web Crypto methods [described above](#opt-in-backends) using -the [`wasm-bindgen`] toolchain (with or without using `getrandom_backend=wasm_js`). -Both web browser (main window and Web Workers) -and Node.js (v19 or later) environments are supported. +To enable `getrandom`'s functionality on `wasm32-unknown-unknown` using the Web +Crypto methods [described above](#opt-in-backends) via [`wasm-bindgen`], do +*both* of the following: -To enable the `wasm_js` backend, you can add the following lines to your -project's `.cargo/config.toml` file: -```toml -[dependencies] -getrandom = { version = "0.3", features = ["js"] } -``` +- Use the `wasm_js` feature flag, i.e. + `getrandom = { version = "0.3", features = ["wasm_js"] }`. + On its own, this only makes the backend available. (As a side effect this + will make your `Cargo.lock` significantly larger if you are not already + using [`wasm-bindgen`], but otherwise enabling this feature is harmless.) +- Set `RUSTFLAGS='--cfg getrandom_backend="wasm_js"'` ([see above](#opt-in-backends)). + +This backend supports both web browsers (main window and Web Workers) +and Node.js (v19 or later) environments. ### Custom backend diff --git a/src/backends.rs b/src/backends.rs index 7abb8d3f..fd360b31 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -19,7 +19,7 @@ cfg_if! { } else if #[cfg(getrandom_backend = "rndr")] { mod rndr; pub use rndr::*; - } else if #[cfg(all(feature = "js", getrandom_backend = "wasm_js"))] { + } else if #[cfg(all(feature = "wasm_js", getrandom_backend = "wasm_js"))] { mod wasm_js; pub use wasm_js::*; } else if #[cfg(target_os = "espidf")] { @@ -127,18 +127,6 @@ cfg_if! { ); } } - } else if #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))] { - cfg_if! { - if #[cfg(feature = "js")] { - mod wasm_js; - pub use wasm_js::*; - } else { - compile_error!("the wasm32-unknown-unknown targets are not supported \ - by default, you may need to enable the \"js\" \ - feature flag. For more information see: \ - https://docs.rs/getrandom/#webassembly-support"); - } - } } else if #[cfg(target_os = "hermit")] { mod hermit; pub use hermit::*; @@ -157,6 +145,14 @@ cfg_if! { } else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] { mod rdrand; pub use rdrand::*; + } else if #[cfg(all( + target_arch = "wasm32", + any(target_os = "unknown", target_os = "none") + ))] { + compile_error!("the wasm32-unknown-unknown targets are not supported \ + by default, you may need to enable the \"wasm_js\" \ + configuration flag. For more information see: \ + https://docs.rs/getrandom/#webassembly-support"); } else { compile_error!("target is not supported. You may need to define \ a custom backend see: \ diff --git a/src/error.rs b/src/error.rs index d94ca2a4..ccbe7837 100644 --- a/src/error.rs +++ b/src/error.rs @@ -116,7 +116,7 @@ impl Error { Error::IOS_RANDOM_GEN => "SecRandomCopyBytes: iOS Security framework failure", #[cfg(all(windows, target_vendor = "win7"))] Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure", - #[cfg(all(feature = "js", getrandom_backend = "wasm_js"))] + #[cfg(all(feature = "wasm_js", getrandom_backend = "wasm_js"))] Error::WEB_CRYPTO => "Web Crypto API is unavailable", #[cfg(target_os = "vxworks")] Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized", diff --git a/tests/mod.rs b/tests/mod.rs index 03247f31..9f1e6338 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,7 +1,7 @@ use core::mem::MaybeUninit; use getrandom::{fill, fill_uninit}; -#[cfg(all(feature = "js", target_arch = "wasm32", target_os = "unknown"))] +#[cfg(all(feature = "wasm_js", target_arch = "wasm32", target_os = "unknown"))] use wasm_bindgen_test::wasm_bindgen_test as test; #[test] From 50b68ba9f8a8343486a7525a75a8a2f56668f442 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 22 Jan 2025 16:57:27 +0000 Subject: [PATCH 13/13] Fix Web test and improve error msgs for wasm_js --- .github/workflows/tests.yml | 1 + src/backends.rs | 32 +++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 57cf9e87..28b91ab9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -237,6 +237,7 @@ jobs: - { description: Web, version: stable, + flags: '-Dwarnings --cfg getrandom_backend="wasm_js"', args: '--features=std,wasm_js', } - { diff --git a/src/backends.rs b/src/backends.rs index fd360b31..1509af11 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -19,9 +19,19 @@ cfg_if! { } else if #[cfg(getrandom_backend = "rndr")] { mod rndr; pub use rndr::*; - } else if #[cfg(all(feature = "wasm_js", getrandom_backend = "wasm_js"))] { - mod wasm_js; - pub use wasm_js::*; + } else if #[cfg(all(getrandom_backend = "wasm_js"))] { + cfg_if! { + if #[cfg(feature = "wasm_js")] { + mod wasm_js; + pub use wasm_js::*; + } else { + compile_error!( + "The \"wasm_js\" backend requires the `wasm_js` feature \ + for `getrandom`. For more information see: \ + https://docs.rs/getrandom/#webassembly-support" + ); + } + } } else if #[cfg(target_os = "espidf")] { mod esp_idf; pub use esp_idf::*; @@ -145,14 +155,14 @@ cfg_if! { } else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] { mod rdrand; pub use rdrand::*; - } else if #[cfg(all( - target_arch = "wasm32", - any(target_os = "unknown", target_os = "none") - ))] { - compile_error!("the wasm32-unknown-unknown targets are not supported \ - by default, you may need to enable the \"wasm_js\" \ - configuration flag. For more information see: \ - https://docs.rs/getrandom/#webassembly-support"); + } else if #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))] { + compile_error!( + "The wasm32-unknown-unknown targets are not supported by default; \ + you may need to enable the \"wasm_js\" configuration flag. Note \ + that enabling the `wasm_js` feature flag alone is insufficient. \ + For more information see: \ + https://docs.rs/getrandom/#webassembly-support" + ); } else { compile_error!("target is not supported. You may need to define \ a custom backend see: \