Auto merge of #886 - Aaron1011:shim/getrandom, r=RalfJung

Shim 'libc::getrandom' in addition to 'libc::syscall(libc::SYS_getrandom)'
This commit is contained in:
bors
2019-08-04 21:02:08 +00:00
2 changed files with 42 additions and 9 deletions
+25 -9
View File
@@ -293,15 +293,9 @@ fn emulate_foreign_item(
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
match this.read_scalar(args[0])?.to_usize(this)? {
id if id == sys_getrandom => {
let ptr = this.read_scalar(args[1])?.not_undef()?;
let len = this.read_scalar(args[2])?.to_usize(this)?;
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
// neither of which have any effect on our current PRNG
let _flags = this.read_scalar(args[3])?.to_i32()?;
this.gen_random(ptr, len as usize)?;
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
// The first argument is the syscall id,
// so skip over it.
linux_getrandom(this, &args[1..], dest)?;
}
id => {
throw_unsup_format!("miri does not support syscall ID {}", id)
@@ -309,6 +303,10 @@ fn emulate_foreign_item(
}
}
"getrandom" => {
linux_getrandom(this, args, dest)?;
}
"dlsym" => {
let _handle = this.read_scalar(args[0])?;
let symbol = this.read_scalar(args[1])?.not_undef()?;
@@ -969,3 +967,21 @@ fn eval_path_scalar(&mut self, path: &[&str]) -> InterpResult<'tcx, Option<Scala
return Ok(None);
}
}
// Shims the linux 'getrandom()' syscall.
fn linux_getrandom<'tcx>(
this: &mut MiriEvalContext<'_, 'tcx>,
args: &[OpTy<'tcx, Tag>],
dest: PlaceTy<'tcx, Tag>,
) -> InterpResult<'tcx> {
let ptr = this.read_scalar(args[0])?.not_undef()?;
let len = this.read_scalar(args[1])?.to_usize(this)?;
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
// neither of which have any effect on our current PRNG.
let _flags = this.read_scalar(args[2])?.to_i32()?;
this.gen_random(ptr, len as usize)?;
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
Ok(())
}
+17
View File
@@ -0,0 +1,17 @@
// Unfortunately, compiletest_rs does not support 'only-linux',
// so we need to ignore Windows and macOS instead.
// ignore-macos: Uses Linux-only APIs
// ignore-windows: Uses Linux-only APIs
#![feature(rustc_private)]
extern crate libc;
fn main() {
let mut buf = [0u8; 5];
unsafe {
assert_eq!(libc::syscall(libc::SYS_getrandom, 0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0);
assert_eq!(libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr() as *mut libc::c_void, 5 as libc::size_t, 0 as libc::c_uint), 5);
assert_eq!(libc::getrandom(0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0);
assert_eq!(libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, 5 as libc::size_t, 0 as libc::c_uint), 5);
}
}