From a9f7cc3b4968d1ec5a8bc857a83ea7ade33fffa8 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 27 Jun 2018 08:56:19 -0700 Subject: [PATCH] [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. --- src/libstd/sys/unix/rand.rs | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/src/libstd/sys/unix/rand.rs b/src/libstd/sys/unix/rand.rs index 3f7f067149084..01c0ada4ffbe5 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 { - 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()) } } }