Skip to content

Commit

Permalink
Use getrandom on more architectures and on android
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 26, 2018
1 parent 319690f commit 00713a6
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,11 @@ mod imp {
}
}

#[cfg(all(target_os = "linux",
any(target_arch = "x86_64",
target_arch = "x86",
target_arch = "arm",
target_arch = "aarch64",
target_arch = "powerpc")))]
#[cfg(all(any(target_os = "linux", target_os = "android"),
any(target_arch = "x86_64", target_arch = "x86",
target_arch = "arm", target_arch = "aarch64",
target_arch = "s390x", target_arch = "powerpc",
target_arch = "mips", target_arch = "mips64")))]
fn getrandom(buf: &mut [u8]) -> libc::c_long {
extern "C" {
fn syscall(number: libc::c_long, ...) -> libc::c_long;
Expand All @@ -204,16 +203,29 @@ mod imp {
const NR_GETRANDOM: libc::c_long = 384;
#[cfg(target_arch = "aarch64")]
const NR_GETRANDOM: libc::c_long = 278;
#[cfg(target_arch = "s390x")]
const NR_GETRANDOM: libc::c_long = 349;
#[cfg(target_arch = "powerpc")]
const NR_GETRANDOM: libc::c_long = 384;

const NR_GETRANDOM: libc::c_long = 359;
#[cfg(target_arch = "mips")] // old ABI
const NR_GETRANDOM: libc::c_long = 4353;
#[cfg(target_arch = "mips64")]
const NR_GETRANDOM: libc::c_long = 5313;

const GRND_NONBLOCK: libc::c_uint = 0x0001;

unsafe {
syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)
}
}

#[cfg(not(all(any(target_os = "linux", target_os = "android"),
any(target_arch = "x86_64", target_arch = "x86",
target_arch = "arm", target_arch = "aarch64",
target_arch = "s390x", target_arch = "powerpc",
target_arch = "mips", target_arch = "mips64"))))]
fn getrandom(_buf: &mut [u8]) -> libc::c_long { -1 }

fn getrandom_try_fill(dest: &mut [u8]) -> Result<(), Error> {
trace!("OsRng: reading {} bytes via getrandom", dest.len());
let mut read = 0;
Expand Down Expand Up @@ -248,12 +260,11 @@ mod imp {
Ok(())
}

#[cfg(all(target_os = "linux",
any(target_arch = "x86_64",
target_arch = "x86",
target_arch = "arm",
target_arch = "aarch64",
target_arch = "powerpc")))]
#[cfg(all(any(target_os = "linux", target_os = "android"),
any(target_arch = "x86_64", target_arch = "x86",
target_arch = "arm", target_arch = "aarch64",
target_arch = "s390x", target_arch = "powerpc",
target_arch = "mips", target_arch = "mips64")))]
fn is_getrandom_available() -> bool {
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use std::sync::{Once, ONCE_INIT};
Expand All @@ -278,12 +289,11 @@ mod imp {
AVAILABLE.load(Ordering::Relaxed)
}

#[cfg(not(all(target_os = "linux",
any(target_arch = "x86_64",
target_arch = "x86",
target_arch = "arm",
target_arch = "aarch64",
target_arch = "powerpc"))))]
#[cfg(not(all(any(target_os = "linux", target_os = "android"),
any(target_arch = "x86_64", target_arch = "x86",
target_arch = "arm", target_arch = "aarch64",
target_arch = "s390x", target_arch = "powerpc",
target_arch = "mips", target_arch = "mips64"))))]
fn is_getrandom_available() -> bool { false }

// TODO: remove outer Option when `Mutex::new(None)` is a constant expression
Expand Down

0 comments on commit 00713a6

Please sign in to comment.