about summary refs log tree commit diff
path: root/src/libstd/sys/unix/rand.rs
diff options
context:
space:
mode:
authorAdam Barth <abarth@google.com>2018-06-27 08:56:19 -0700
committerAdam Barth <abarth@google.com>2018-06-27 08:56:19 -0700
commita9f7cc3b4968d1ec5a8bc857a83ea7ade33fffa8 (patch)
tree97c4a8a4c6125892d8ab9949167648ed9f423518 /src/libstd/sys/unix/rand.rs
parent142c98dd5a9fbd60c13e62a5c1358a40ee622dbb (diff)
downloadrust-a9f7cc3b4968d1ec5a8bc857a83ea7ade33fffa8.tar.gz
rust-a9f7cc3b4968d1ec5a8bc857a83ea7ade33fffa8.zip
[fuchsia] Update zx_cprng_draw to target semantics
This change is the final step in improving the semantics of
zx_cprng_draw. Now the syscall always generates the requested number of
bytes. If the syscall would have failed to generate the requested number
of bytes, the syscall either terminates the entire operating system or
terminates the calling process, depending on whether the error is a
result of the kernel misbehaving or the userspace program misbehaving.
Diffstat (limited to 'src/libstd/sys/unix/rand.rs')
-rw-r--r--src/libstd/sys/unix/rand.rs28
1 files changed, 2 insertions, 26 deletions
diff --git a/src/libstd/sys/unix/rand.rs b/src/libstd/sys/unix/rand.rs
index 3f7f0671490..01c0ada4ffb 100644
--- a/src/libstd/sys/unix/rand.rs
+++ b/src/libstd/sys/unix/rand.rs
@@ -183,34 +183,10 @@ mod imp {
 mod imp {
     #[link(name = "zircon")]
     extern {
-        fn zx_cprng_draw_new(buffer: *mut u8, len: usize) -> i32;
-    }
-
-    fn getrandom(buf: &mut [u8]) -> Result<usize, i32> {
-        unsafe {
-            let status = zx_cprng_draw_new(buf.as_mut_ptr(), buf.len());
-            if status == 0 {
-                Ok(buf.len())
-            } else {
-                Err(status)
-            }
-        }
+        fn zx_cprng_draw(buffer: *mut u8, len: usize);
     }
 
     pub fn fill_bytes(v: &mut [u8]) {
-        let mut buf = v;
-        while !buf.is_empty() {
-            let ret = getrandom(buf);
-            match ret {
-                Err(err) => {
-                    panic!("kernel zx_cprng_draw call failed! (returned {}, buf.len() {})",
-                        err, buf.len())
-                }
-                Ok(actual) => {
-                    let move_buf = buf;
-                    buf = &mut move_buf[(actual as usize)..];
-                }
-            }
-        }
+        unsafe { zx_cprng_draw(v.as_mut_ptr(), v.len()) }
     }
 }