Skip to content

Commit a5baa15

Browse files
committed
getrandom: test with and without isolation
also add some comments for why we keep certain old obscure APIs supported
1 parent 6a47bd4 commit a5baa15

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

src/tools/miri/src/shims/unix/linux/foreign_items.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
117117
// `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)`
118118
// is called if a `HashMap` is created the regular way (e.g. HashMap<K, V>).
119119
id if id == sys_getrandom => {
120+
// Used by getrandom 0.1
120121
// The first argument is the syscall id, so skip over it.
121122
if args.len() < 4 {
122123
throw_ub_format!(
123124
"incorrect number of arguments for `getrandom` syscall: got {}, expected at least 4",
124125
args.len()
125126
);
126127
}
127-
getrandom(this, &args[1], &args[2], &args[3], dest)?;
128+
129+
let ptr = this.read_pointer(&args[1])?;
130+
let len = this.read_target_usize(&args[2])?;
131+
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
132+
// neither of which have any effect on our current PRNG.
133+
// See <https://github.com/rust-lang/rust/pull/79196> for a discussion of argument sizes.
134+
let _flags = this.read_scalar(&args[3])?.to_i32();
135+
136+
this.gen_random(ptr, len)?;
137+
this.write_scalar(Scalar::from_target_usize(len, this), dest)?;
128138
}
129139
// `futex` is used by some synchronization primitives.
130140
id if id == sys_futex => {
@@ -196,24 +206,3 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
196206
Ok(EmulateItemResult::NeedsJumping)
197207
}
198208
}
199-
200-
// Shims the linux `getrandom` syscall.
201-
fn getrandom<'tcx>(
202-
this: &mut MiriInterpCx<'_, 'tcx>,
203-
ptr: &OpTy<'tcx, Provenance>,
204-
len: &OpTy<'tcx, Provenance>,
205-
flags: &OpTy<'tcx, Provenance>,
206-
dest: &MPlaceTy<'tcx, Provenance>,
207-
) -> InterpResult<'tcx> {
208-
let ptr = this.read_pointer(ptr)?;
209-
let len = this.read_target_usize(len)?;
210-
211-
// The only supported flags are GRND_RANDOM and GRND_NONBLOCK,
212-
// neither of which have any effect on our current PRNG.
213-
// See <https://github.com/rust-lang/rust/pull/79196> for a discussion of argument sizes.
214-
let _flags = this.read_scalar(flags)?.to_i32();
215-
216-
this.gen_random(ptr, len)?;
217-
this.write_scalar(Scalar::from_target_usize(len, this), dest)?;
218-
Ok(())
219-
}

src/tools/miri/src/shims/windows/foreign_items.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
513513
throw_machine_stop!(TerminationInfo::Exit { code: code.into(), leak_check: false });
514514
}
515515
"SystemFunction036" => {
516+
// used by getrandom 0.1
516517
// This is really 'RtlGenRandom'.
517518
let [ptr, len] =
518519
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
@@ -522,6 +523,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
522523
this.write_scalar(Scalar::from_bool(true), dest)?;
523524
}
524525
"ProcessPrng" => {
526+
// used by `std`
525527
let [ptr, len] =
526528
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
527529
let ptr = this.read_pointer(ptr)?;
@@ -530,6 +532,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
530532
this.write_scalar(Scalar::from_i32(1), dest)?;
531533
}
532534
"BCryptGenRandom" => {
535+
// used by getrandom 0.2
533536
let [algorithm, ptr, len, flags] =
534537
this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
535538
let algorithm = this.read_scalar(algorithm)?;

src/tools/miri/tests/pass-dep/getrandom.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// mac-os `getrandom_01` does some pointer shenanigans
22
//@compile-flags: -Zmiri-permissive-provenance
3+
//@revisions: isolation no_isolation
4+
//@[no_isolation]compile-flags: -Zmiri-disable-isolation
35

46
/// Test direct calls of getrandom 0.1 and 0.2.
5-
/// Make sure they work even with isolation enabled (i.e., we do not hit a file-based fallback path).
67
fn main() {
78
let mut data = vec![0; 16];
89
getrandom_01::getrandom(&mut data).unwrap();

0 commit comments

Comments
 (0)