Skip to content

Commit a9f7cc3

Browse files
author
Adam Barth
committed
[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.
1 parent 142c98d commit a9f7cc3

File tree

1 file changed

+2
-26
lines changed

1 file changed

+2
-26
lines changed

src/libstd/sys/unix/rand.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -183,34 +183,10 @@ mod imp {
183183
mod imp {
184184
#[link(name = "zircon")]
185185
extern {
186-
fn zx_cprng_draw_new(buffer: *mut u8, len: usize) -> i32;
187-
}
188-
189-
fn getrandom(buf: &mut [u8]) -> Result<usize, i32> {
190-
unsafe {
191-
let status = zx_cprng_draw_new(buf.as_mut_ptr(), buf.len());
192-
if status == 0 {
193-
Ok(buf.len())
194-
} else {
195-
Err(status)
196-
}
197-
}
186+
fn zx_cprng_draw(buffer: *mut u8, len: usize);
198187
}
199188

200189
pub fn fill_bytes(v: &mut [u8]) {
201-
let mut buf = v;
202-
while !buf.is_empty() {
203-
let ret = getrandom(buf);
204-
match ret {
205-
Err(err) => {
206-
panic!("kernel zx_cprng_draw call failed! (returned {}, buf.len() {})",
207-
err, buf.len())
208-
}
209-
Ok(actual) => {
210-
let move_buf = buf;
211-
buf = &mut move_buf[(actual as usize)..];
212-
}
213-
}
214-
}
190+
unsafe { zx_cprng_draw(v.as_mut_ptr(), v.len()) }
215191
}
216192
}

0 commit comments

Comments
 (0)