Skip to content

Commit 9cd994c

Browse files
committed
Auto merge of #45896 - malbarbo:use-libc-const, r=alexcrichton
Use getrandom syscall for all Linux and Android targets. I suppose we can use it in all Linux and Android targets. In function `is_getrandom_available` is checked if the syscall is available (getrandom syscall was add in version 3.17 of Linux kernel), if the syscall is not available `fill_bytes` fallback to reading from `/dev/urandom`. Update libc to include getrandom related constants.
2 parents 24840da + 8f2dfd1 commit 9cd994c

File tree

2 files changed

+6
-51
lines changed

2 files changed

+6
-51
lines changed

src/libstd/sys/unix/rand.rs

+5-50
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,14 @@ mod imp {
3232
use libc;
3333
use sys::os::errno;
3434

35-
#[cfg(all(target_os = "linux",
36-
any(target_arch = "x86_64",
37-
target_arch = "x86",
38-
target_arch = "arm",
39-
target_arch = "aarch64",
40-
target_arch = "powerpc",
41-
target_arch = "powerpc64",
42-
target_arch = "s390x")))]
35+
#[cfg(any(target_os = "linux", target_os = "android"))]
4336
fn getrandom(buf: &mut [u8]) -> libc::c_long {
44-
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
45-
const NR_GETRANDOM: libc::c_long = 0x40000000 + 318;
46-
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
47-
const NR_GETRANDOM: libc::c_long = 318;
48-
#[cfg(target_arch = "x86")]
49-
const NR_GETRANDOM: libc::c_long = 355;
50-
#[cfg(target_arch = "arm")]
51-
const NR_GETRANDOM: libc::c_long = 384;
52-
#[cfg(target_arch = "s390x")]
53-
const NR_GETRANDOM: libc::c_long = 349;
54-
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
55-
const NR_GETRANDOM: libc::c_long = 359;
56-
#[cfg(target_arch = "aarch64")]
57-
const NR_GETRANDOM: libc::c_long = 278;
58-
59-
const GRND_NONBLOCK: libc::c_uint = 0x0001;
60-
6137
unsafe {
62-
libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)
38+
libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr(), buf.len(), libc::GRND_NONBLOCK)
6339
}
6440
}
6541

66-
#[cfg(not(all(target_os = "linux",
67-
any(target_arch = "x86_64",
68-
target_arch = "x86",
69-
target_arch = "arm",
70-
target_arch = "aarch64",
71-
target_arch = "powerpc",
72-
target_arch = "powerpc64",
73-
target_arch = "s390x"))))]
42+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
7443
fn getrandom(_buf: &mut [u8]) -> libc::c_long { -1 }
7544

7645
fn getrandom_fill_bytes(v: &mut [u8]) -> bool {
@@ -94,14 +63,7 @@ mod imp {
9463
return true
9564
}
9665

97-
#[cfg(all(target_os = "linux",
98-
any(target_arch = "x86_64",
99-
target_arch = "x86",
100-
target_arch = "arm",
101-
target_arch = "aarch64",
102-
target_arch = "powerpc",
103-
target_arch = "powerpc64",
104-
target_arch = "s390x")))]
66+
#[cfg(any(target_os = "linux", target_os = "android"))]
10567
fn is_getrandom_available() -> bool {
10668
use io;
10769
use sync::atomic::{AtomicBool, Ordering};
@@ -125,14 +87,7 @@ mod imp {
12587
AVAILABLE.load(Ordering::Relaxed)
12688
}
12789

128-
#[cfg(not(all(target_os = "linux",
129-
any(target_arch = "x86_64",
130-
target_arch = "x86",
131-
target_arch = "arm",
132-
target_arch = "aarch64",
133-
target_arch = "powerpc",
134-
target_arch = "powerpc64",
135-
target_arch = "s390x"))))]
90+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
13691
fn is_getrandom_available() -> bool { false }
13792

13893
pub fn fill_bytes(v: &mut [u8]) {

0 commit comments

Comments
 (0)