Skip to content

Commit

Permalink
implement SecRandomCopyBytes for macOS RNG
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 21, 2019
1 parent 8fd40db commit a25b71b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/fn_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,18 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
"_NSGetArgv" => {
this.write_scalar(Scalar::Ptr(this.machine.argv.unwrap()), dest)?;
},
"SecRandomCopyBytes" => {
let len = this.read_scalar(args[1])?.to_usize()?;
let ptr = this.read_scalar(args[2])?.to_ptr()?;

if len > 0 {
let data = gen_random(this, len as usize)?;
this.memory_mut().get_mut(ptr.alloc_id)?
.write_bytes(tcx, ptr, &data)?;
}

this.write_null(dest)?;
}

// Windows API stubs.
// HANDLE = isize
Expand Down
16 changes: 6 additions & 10 deletions test-cargo-miri/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ fn fixed_rng() {

#[test]
fn entropy_rng() {
#[cfg(not(target_os="macos"))] // FIXME entropy does not work on macOS
// (Not disabling the entire test as that would change the output.)
{
// Use this opportunity to test querying the RNG (needs an external crate, hence tested here and not in the compiletest suite)
let mut rng = SmallRng::from_entropy();
let _val = rng.gen::<i32>();
// Use this opportunity to test querying the RNG (needs an external crate, hence tested here and not in the compiletest suite)
let mut rng = SmallRng::from_entropy();
let _val = rng.gen::<i32>();

// Also try per-thread RNG.
let mut rng = rand::thread_rng();
let _val = rng.gen::<i32>();
}
// Also try per-thread RNG.
let mut rng = rand::thread_rng();
let _val = rng.gen::<i32>();
}

// A test that won't work on miri
Expand Down

0 comments on commit a25b71b

Please sign in to comment.