Skip to content

Commit 93e110f

Browse files
committed
Auto merge of #886 - Aaron1011:shim/getrandom, r=RalfJung
Shim 'libc::getrandom' in addition to 'libc::syscall(libc::SYS_getrandom)'
2 parents 96adbf6 + f830a6c commit 93e110f

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/shims/foreign_items.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,22 +293,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
293293
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
294294
match this.read_scalar(args[0])?.to_usize(this)? {
295295
id if id == sys_getrandom => {
296-
let ptr = this.read_scalar(args[1])?.not_undef()?;
297-
let len = this.read_scalar(args[2])?.to_usize(this)?;
298-
299-
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
300-
// neither of which have any effect on our current PRNG
301-
let _flags = this.read_scalar(args[3])?.to_i32()?;
302-
303-
this.gen_random(ptr, len as usize)?;
304-
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
296+
// The first argument is the syscall id,
297+
// so skip over it.
298+
linux_getrandom(this, &args[1..], dest)?;
305299
}
306300
id => {
307301
throw_unsup_format!("miri does not support syscall ID {}", id)
308302
}
309303
}
310304
}
311305

306+
"getrandom" => {
307+
linux_getrandom(this, args, dest)?;
308+
}
309+
312310
"dlsym" => {
313311
let _handle = this.read_scalar(args[0])?;
314312
let symbol = this.read_scalar(args[1])?.not_undef()?;
@@ -969,3 +967,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
969967
return Ok(None);
970968
}
971969
}
970+
971+
// Shims the linux 'getrandom()' syscall.
972+
fn linux_getrandom<'tcx>(
973+
this: &mut MiriEvalContext<'_, 'tcx>,
974+
args: &[OpTy<'tcx, Tag>],
975+
dest: PlaceTy<'tcx, Tag>,
976+
) -> InterpResult<'tcx> {
977+
let ptr = this.read_scalar(args[0])?.not_undef()?;
978+
let len = this.read_scalar(args[1])?.to_usize(this)?;
979+
980+
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
981+
// neither of which have any effect on our current PRNG.
982+
let _flags = this.read_scalar(args[2])?.to_i32()?;
983+
984+
this.gen_random(ptr, len as usize)?;
985+
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
986+
Ok(())
987+
}

tests/run-pass/linux-getrandom.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Unfortunately, compiletest_rs does not support 'only-linux',
2+
// so we need to ignore Windows and macOS instead.
3+
// ignore-macos: Uses Linux-only APIs
4+
// ignore-windows: Uses Linux-only APIs
5+
#![feature(rustc_private)]
6+
extern crate libc;
7+
8+
fn main() {
9+
let mut buf = [0u8; 5];
10+
unsafe {
11+
assert_eq!(libc::syscall(libc::SYS_getrandom, 0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0);
12+
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);
13+
14+
assert_eq!(libc::getrandom(0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0);
15+
assert_eq!(libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, 5 as libc::size_t, 0 as libc::c_uint), 5);
16+
}
17+
}

0 commit comments

Comments
 (0)