about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-03-23 06:02:34 +0000
committerbors <bors@rust-lang.org>2020-03-23 06:02:34 +0000
commit8ff785011be6625e32afceee3a08e5cff7470feb (patch)
treef9a842b8f8e390c261192943061f48d6754f65da /src/libstd/sys
parent37c945dd6178cb520eb1e450a795f8c3b3cc5a3b (diff)
parent5f91f30b0ace8a687e1acd2e057e27d4b57ad364 (diff)
downloadrust-8ff785011be6625e32afceee3a08e5cff7470feb.tar.gz
rust-8ff785011be6625e32afceee3a08e5cff7470feb.zip
Auto merge of #70296 - Centril:rollup-wvfmb3n, r=Centril
Rollup of 9 pull requests

Successful merges:

 - #69251 (#[track_caller] in traits)
 - #69880 (miri engine: turn error sanity checks into assertions)
 - #70207 (Use getentropy(2) on macos)
 - #70227 (Only display definition when suggesting a typo)
 - #70236 (resolve: Avoid "self-confirming" import resolutions in one more case)
 - #70248 (parser: simplify & remove unused field)
 - #70249 (handle ConstKind::Unresolved after monomorphizing)
 - #70269 (remove redundant closures (clippy::redundant_closure))
 - #70270 (Clean up E0449 explanation)

Failed merges:

r? @ghost
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/unix/rand.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/libstd/sys/unix/rand.rs b/src/libstd/sys/unix/rand.rs
index 9ce5f3d014c..eed6fbf13b7 100644
--- a/src/libstd/sys/unix/rand.rs
+++ b/src/libstd/sys/unix/rand.rs
@@ -12,6 +12,7 @@ pub fn hashmap_random_keys() -> (u64, u64) {
 
 #[cfg(all(
     unix,
+    not(target_os = "macos"),
     not(target_os = "ios"),
     not(target_os = "openbsd"),
     not(target_os = "freebsd"),
@@ -92,6 +93,42 @@ mod imp {
     }
 }
 
+#[cfg(target_os = "macos")]
+mod imp {
+    use crate::fs::File;
+    use crate::io::Read;
+    use crate::sys::os::errno;
+    use libc::{c_int, c_void, size_t};
+
+    fn getentropy_fill_bytes(v: &mut [u8]) -> bool {
+        weak!(fn getentropy(*mut c_void, size_t) -> c_int);
+
+        getentropy
+            .get()
+            .map(|f| {
+                // getentropy(2) permits a maximum buffer size of 256 bytes
+                for s in v.chunks_mut(256) {
+                    let ret = unsafe { f(s.as_mut_ptr() as *mut c_void, s.len()) };
+                    if ret == -1 {
+                        panic!("unexpected getentropy error: {}", errno());
+                    }
+                }
+                true
+            })
+            .unwrap_or(false)
+    }
+
+    pub fn fill_bytes(v: &mut [u8]) {
+        if getentropy_fill_bytes(v) {
+            return;
+        }
+
+        // for older macos which doesn't support getentropy
+        let mut file = File::open("/dev/urandom").expect("failed to open /dev/urandom");
+        file.read_exact(v).expect("failed to read /dev/urandom")
+    }
+}
+
 #[cfg(target_os = "openbsd")]
 mod imp {
     use crate::sys::os::errno;