Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android/Linux/rdrand: Use once_cell::race::OnceBool instead of LazyBool. #483

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ exclude = [".*"]

[dependencies]
cfg-if = "1"
once_cell = { version = "1.19.0", default-features = false, optional = true }

[target.'cfg(any(target_os = "android", target_os = "linux", all(target_arch = "x86_64", target_env = "sgx")))'.dependencies]
once_cell = { version = "1.19.0", default-features = false, features = ["race"] }

# When built as part of libstd
compiler_builtins = { version = "0.1", optional = true }
Expand All @@ -30,6 +34,10 @@ windows-targets = "0.52"
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dependencies]
wasm-bindgen = { version = "0.2.62", default-features = false, optional = true }
js-sys = { version = "0.3", optional = true }

[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dev-dependencies]
once_cell = { version = "1.19.0", default-features = false, features = ["race"] }

[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dev-dependencies]
wasm-bindgen-test = "0.3.18"

Expand All @@ -40,7 +48,7 @@ std = []
# Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow).
linux_disable_fallback = []
# Feature to enable fallback RDRAND-based implementation on x86/x86_64
rdrand = []
rdrand = ["once_cell/race"]
# Feature to enable JavaScript bindings on wasm*-unknown-unknown
js = ["wasm-bindgen", "js-sys"]
# Feature to enable custom RNG implementations
Expand Down
66 changes: 0 additions & 66 deletions src/lazy.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ use crate::util::{slice_as_uninit_mut, slice_assume_init_mut};
use core::mem::MaybeUninit;

mod error;
mod lazy;
mod util;
// To prevent a breaking change when targets are added, we always export the
// register_custom_getrandom macro, so old Custom RNG crates continue to build.
Expand Down
7 changes: 4 additions & 3 deletions src/linux_android_with_fallback.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Implementation for Linux / Android with `/dev/urandom` fallback
use crate::{lazy::LazyBool, linux_android, use_file, util_libc::last_os_error, Error};
use crate::{linux_android, use_file, util_libc::last_os_error, Error};
use core::mem::MaybeUninit;
use once_cell::race::OnceBool;

pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
// getrandom(2) was introduced in Linux 3.17
static HAS_GETRANDOM: LazyBool = LazyBool::new();
if HAS_GETRANDOM.unsync_init(is_getrandom_available) {
static HAS_GETRANDOM: OnceBool = OnceBool::new();
if HAS_GETRANDOM.get_or_init(is_getrandom_available) {
linux_android::getrandom_inner(dest)
} else {
use_file::getrandom_inner(dest)
Expand Down
7 changes: 4 additions & 3 deletions src/rdrand.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! RDRAND backend for x86(-64) targets
use crate::{lazy::LazyBool, util::slice_as_uninit, Error};
use crate::{util::slice_as_uninit, Error};
use core::mem::{size_of, MaybeUninit};
use once_cell::race::OnceBool;

cfg_if! {
if #[cfg(target_arch = "x86_64")] {
Expand Down Expand Up @@ -94,8 +95,8 @@ fn is_rdrand_good() -> bool {
}

pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
static RDRAND_GOOD: LazyBool = LazyBool::new();
if !RDRAND_GOOD.unsync_init(is_rdrand_good) {
static RDRAND_GOOD: OnceBool = OnceBool::new();
if !RDRAND_GOOD.get_or_init(is_rdrand_good) {
return Err(Error::NO_RDRAND);
}
// SAFETY: After this point, we know rdrand is supported.
Expand Down
2 changes: 0 additions & 2 deletions tests/rdrand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use getrandom::Error;
#[macro_use]
extern crate cfg_if;
#[path = "../src/lazy.rs"]
mod lazy;
#[path = "../src/rdrand.rs"]
mod rdrand;
#[path = "../src/util.rs"]
Expand Down