Skip to content

Commit

Permalink
Add linux_rustix opt-in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Oct 16, 2024
1 parent ce35c67 commit 3685d34
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/nopanic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ jobs:
- name: Check (linux_android.rs)
run: ret=$(grep panic target/release/libgetrandom_wrapper.so; echo $?); [ $ret -eq 1 ]

- name: Build (linux_rustix.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_rustix"
run: cargo build --release
- name: Check (linux_rustix.rs)
run: ret=$(grep panic target/release/libgetrandom_wrapper.so; echo $?); [ $ret -eq 1 ]

- name: Build (rdrand.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ jobs:
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
run: cargo test ${{ matrix.cargo_test_opts }} --target=${{ matrix.target }} --features=std
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_rustix"
run: cargo test ${{ matrix.cargo_test_opts }} --target=${{ matrix.target }} --features=std
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback
run: cargo test --features=std
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ jobs:
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
run: cargo clippy --target x86_64-unknown-linux-gnu
- name: Linux (linux_rustix.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_rustix"
run: cargo clippy --target x86_64-unknown-linux-gnu
- name: Linux (linux_android_with_fallback.rs)
run: cargo clippy --target x86_64-unknown-linux-gnu
- name: NetBSD (netbsd.rs)
Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ cfg-if = "1"
compiler_builtins = { version = "0.1", optional = true }
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }

[target.'cfg(unix)'.dependencies]
[target.'cfg(all(unix, not(getrandom_backend = "linux_rustix")))'.dependencies]
libc = { version = "0.2.154", default-features = false }

[target.'cfg(all(getrandom_backend = "linux_rustix", any(target_os = "linux", target_os = "android")))'.dependencies]
rustix = { version = "0.38", default-features = false, features = ["rand"] }

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

Expand All @@ -42,7 +45,7 @@ rustc-dep-of-std = ["compiler_builtins", "core"]
[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
'cfg(getrandom_backend, values("custom", "rdrand", "rndr", "linux_getrandom", "wasm_js", "esp_idf"))',
'cfg(getrandom_backend, values("custom", "rdrand", "rndr", "linux_getrandom", "linux_rustix", "wasm_js", "esp_idf"))',
'cfg(getrandom_browser_test)',
'cfg(getrandom_test_linux_fallback)',
]
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
//! | 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‑*` | [`getrandom`][1] system call using the [`rustix`] crate
//! | `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!
Expand Down Expand Up @@ -243,6 +244,7 @@
//! [platform-support]: https://doc.rust-lang.org/stable/rustc/platform-support.html
//! [WASI]: https://github.com/CraneStation/wasi
//! [Emscripten]: https://www.hellorust.com/setup/emscripten/
//! [`rustix`]: https://docs.rs/rustix
#![doc(
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
Expand Down Expand Up @@ -295,6 +297,8 @@ cfg_if! {
} else if #[cfg(getrandom_backend = "linux_getrandom")] {
mod util_libc;
#[path = "linux_android.rs"] mod imp;
} else if #[cfg(getrandom_backend = "linux_rustix")] {
#[path = "linux_rustix.rs"] mod imp;
} else if #[cfg(getrandom_backend = "rdrand")] {
mod lazy;
#[path = "rdrand.rs"] mod imp;
Expand Down
30 changes: 30 additions & 0 deletions src/linux_rustix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Implementation for Linux / Android without `/dev/urandom` fallback
use crate::{Error, MaybeUninit};
use rustix::rand::{getrandom_uninit, GetRandomFlags};

#[cfg(not(any(target_os = "android", target_os = "linux")))]
compile_error!("`linux_rustix` backend can be enabled only for Linux/Android targets!");

pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
loop {
let res = getrandom_uninit(dest, GetRandomFlags::empty()).map(|(res, _)| res.len());
match res {
Ok(0) => return Err(Error::UNEXPECTED),
Ok(res_len) => {
dest = dest.get_mut(res_len..).ok_or(Error::UNEXPECTED)?;
if !dest.is_empty() {
return Ok(());
}
}
Err(rustix::io::Errno::INTR) => continue,
Err(err) => {
let code = err
.raw_os_error()
.wrapping_neg()
.try_into()
.expect("Errno uses u16 internally");
return Err(Error::from_os_error(code));
}
}
}
}

0 comments on commit 3685d34

Please sign in to comment.