Skip to content

Commit

Permalink
Explicitly specify types to arguments of 'libc::syscall'
Browse files Browse the repository at this point in the history
The 'libc::syscall' function uses varargs - as a result, its arguments
are completely untyped. THe user must ensure that it is called with the
proper types for the targeted syscall - otherwise, the calling
convention might cause arguments to be put into the wrong registers.

This commit explicitly casts the arguments to 'libc::syscall' to the
proper type for the 'getrandom' syscall. This ensures that the correct
types for the target platform will always be used, instead of relying on
the types used happening to match those required by the target platform.
  • Loading branch information
Aaron1011 committed Aug 4, 2019
1 parent ab44edf commit 8599283
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/linux_android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
static HAS_GETRANDOM: LazyBool = LazyBool::new();
if HAS_GETRANDOM.unsync_init(is_getrandom_available) {
sys_fill_exact(dest, |buf| unsafe {
libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr(), buf.len(), 0) as libc::ssize_t
getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
})
} else {
use_file::getrandom_inner(dest)
}
}

fn is_getrandom_available() -> bool {
let res = unsafe { libc::syscall(libc::SYS_getrandom, 0, 0, libc::GRND_NONBLOCK) };
let res = unsafe { getrandom(0 as *mut libc::c_void, 0, libc::GRND_NONBLOCK) };
if res < 0 {
match last_os_error().raw_os_error() {
Some(libc::ENOSYS) => false, // No kernel support
Expand All @@ -34,3 +34,9 @@ fn is_getrandom_available() -> bool {
true
}
}

unsafe fn getrandom(buf: *mut libc::c_void,
buflen: libc::size_t,
flags: libc::c_uint) -> libc::ssize_t {
libc::syscall(libc::SYS_getrandom, buf, buflen, flags) as libc::ssize_t
}

0 comments on commit 8599283

Please sign in to comment.