Skip to content

Commit a2bdb3b

Browse files
committed
Shim 'libc::getrandom' in addition to 'libc::syscall(libc::SYS_getrandom)'
1 parent bc82f83 commit a2bdb3b

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/shims/foreign_items.rs

Lines changed: 24 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,20 @@ 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<'mir, 'tcx>(this: &mut MiriEvalContext<'mir, 'tcx>,
973+
args: &[OpTy<'tcx, Tag>],
974+
dest: PlaceTy<'tcx, Tag>) -> InterpResult<'tcx> {
975+
let ptr = this.read_scalar(args[0])?.not_undef()?;
976+
let len = this.read_scalar(args[1])?.to_usize(this)?;
977+
978+
979+
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
980+
// neither of which have any effect on our current PRNG
981+
let _flags = this.read_scalar(args[2])?.to_i32()?;
982+
983+
this.gen_random(ptr, len as usize)?;
984+
this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?;
985+
Ok(())
986+
}

0 commit comments

Comments
 (0)