From ceca7c502057fedee2e77329df4ec23b1bebaa03 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Apr 2019 19:06:08 +0200 Subject: [PATCH 1/9] tweak entropy tests a bit --- tests/compile-fail/getrandom.rs | 4 ++-- tests/run-pass/hashmap.rs | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/compile-fail/getrandom.rs b/tests/compile-fail/getrandom.rs index 4dc3e863aa..d54c95c823 100644 --- a/tests/compile-fail/getrandom.rs +++ b/tests/compile-fail/getrandom.rs @@ -1,5 +1,5 @@ -// ignore-macos -// ignore-windows +// ignore-macos: Uses Linux-only APIs +// ignore-windows: Uses Linux-only APIs #![feature(rustc_private)] extern crate libc; diff --git a/tests/run-pass/hashmap.rs b/tests/run-pass/hashmap.rs index b29b681939..796e63c81a 100644 --- a/tests/run-pass/hashmap.rs +++ b/tests/run-pass/hashmap.rs @@ -27,12 +27,13 @@ fn test_map(mut map: HashMap) { } fn main() { - // TODO: Implement random number generation on OS X if cfg!(not(target_os = "macos")) { - let map_normal: HashMap = HashMap::new(); - test_map(map_normal); + let map: HashMap = HashMap::default(); + test_map(map); } else { - let map : HashMap> = Default::default(); + // TODO: Implement random number generation on OS X. + // Until then, use a deterministic map. + let map : HashMap> = HashMap::default(); test_map(map); } } From bd4a772e1a87180f31da81f7d11ef07cff042bbf Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Apr 2019 19:12:56 +0200 Subject: [PATCH 2/9] update cargo miri test to test rng crate a bit; this currently fails --- test-cargo-miri/run-test.py | 2 +- test-cargo-miri/test.stdout.ref | 5 +++-- test-cargo-miri/tests/test.rs | 10 +++++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/test-cargo-miri/run-test.py b/test-cargo-miri/run-test.py index 8c59b6bcde..f1412dbf39 100755 --- a/test-cargo-miri/run-test.py +++ b/test-cargo-miri/run-test.py @@ -43,7 +43,7 @@ def test_cargo_miri_run(): ) def test_cargo_miri_test(): - test("cargo miri test", ["cargo", "miri", "test", "-q"], "test.stdout.ref", "test.stderr.ref") + test("cargo miri test", ["cargo", "miri", "test", "-q", "--", "-Zmiri-seed=feed"], "test.stdout.ref", "test.stderr.ref") test("cargo miri test (with filter)", ["cargo", "miri", "test", "-q", "--", "--", "impl"], "test.stdout.ref2", "test.stderr.ref" diff --git a/test-cargo-miri/test.stdout.ref b/test-cargo-miri/test.stdout.ref index 9c3621f215..318057a7d9 100644 --- a/test-cargo-miri/test.stdout.ref +++ b/test-cargo-miri/test.stdout.ref @@ -5,9 +5,10 @@ test test::rng ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out -running 2 tests +running 3 tests test rng ... ok +test seeded_rng ... ok test simple ... ok -test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out diff --git a/test-cargo-miri/tests/test.rs b/test-cargo-miri/tests/test.rs index e9faaf2fb2..6f7876d679 100644 --- a/test-cargo-miri/tests/test.rs +++ b/test-cargo-miri/tests/test.rs @@ -1,6 +1,6 @@ extern crate rand; -use rand::{Rng, SeedableRng}; +use rand::{SeedableRng, FromEntropy, Rng, rngs::SmallRng}; #[test] fn simple() { @@ -17,6 +17,14 @@ fn rng() { assert_ne!(x, y); } +#[test] +#[cfg(not(target_os="macos"))] // FIXME entropy does not work on macOS +fn seeded_rng() { + // 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::(); +} + // A test that won't work on miri #[cfg(not(miri))] #[test] From 9c8ad5f9e199d45b7c2425389ef718ce65071be2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Apr 2019 19:26:02 +0200 Subject: [PATCH 3/9] fix 0-sized getrandom --- src/fn_call.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/fn_call.rs b/src/fn_call.rs index bb6d6aed49..718f1c24ac 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -241,9 +241,11 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<' // neither of which have any effect on our current PRNG let _flags = this.read_scalar(args[3])?.to_i32()?; - let data = gen_random(this, len as usize)?; - this.memory_mut().get_mut(ptr.alloc_id)? - .write_bytes(tcx, ptr, &data)?; + 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_scalar(Scalar::from_uint(len, dest.layout.size), dest)?; } @@ -769,9 +771,11 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<' let ptr = this.read_scalar(args[0])?.to_ptr()?; let len = this.read_scalar(args[1])?.to_usize(this)?; - let data = gen_random(this, len as usize)?; - this.memory_mut().get_mut(ptr.alloc_id)? - .write_bytes(tcx, ptr, &data)?; + 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_scalar(Scalar::from_bool(true), dest)?; } From ba567d19f09f105d7ed03808d2220b27fc0b8893 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Apr 2019 19:49:36 +0200 Subject: [PATCH 4/9] stub out pthread_atfork to that we support thread_rng() --- src/fn_call.rs | 5 +++++ test-cargo-miri/tests/test.rs | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/fn_call.rs b/src/fn_call.rs index 718f1c24ac..bb9eef0c87 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -624,6 +624,11 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<' this.write_null(dest)?; } + // We don't support fork so we don't have to do anything for atfork. + "pthread_atfork" => { + this.write_null(dest)?; + } + "mmap" => { // This is a horrible hack, but since the guard page mechanism calls mmap and expects a particular return value, we just give it that value. let addr = this.read_scalar(args[0])?.not_undef()?; diff --git a/test-cargo-miri/tests/test.rs b/test-cargo-miri/tests/test.rs index 6f7876d679..1812ca59c4 100644 --- a/test-cargo-miri/tests/test.rs +++ b/test-cargo-miri/tests/test.rs @@ -23,6 +23,10 @@ fn seeded_rng() { // 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::(); + + // Also try per-thread RNG. + let mut rng = rand::thread_rng(); + let _val = rng.gen::(); } // A test that won't work on miri From 5761a0bf61fe932699c0547849da792f4fb2c4fa Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Apr 2019 19:55:22 +0200 Subject: [PATCH 5/9] fix ref file --- test-cargo-miri/test.stdout.ref2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-cargo-miri/test.stdout.ref2 b/test-cargo-miri/test.stdout.ref2 index ce3506709d..32d943623a 100644 --- a/test-cargo-miri/test.stdout.ref2 +++ b/test-cargo-miri/test.stdout.ref2 @@ -7,5 +7,5 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out running 1 test test simple ... ok -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out From 59541d446aabc3b0a15bc1ba2a3f28eeb7608285 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Apr 2019 20:12:55 +0200 Subject: [PATCH 6/9] pick better test names --- test-cargo-miri/test.stdout.ref | 4 ++-- test-cargo-miri/tests/test.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test-cargo-miri/test.stdout.ref b/test-cargo-miri/test.stdout.ref index 318057a7d9..97589e9a16 100644 --- a/test-cargo-miri/test.stdout.ref +++ b/test-cargo-miri/test.stdout.ref @@ -6,8 +6,8 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out running 3 tests -test rng ... ok -test seeded_rng ... ok +test entropy_rng ... ok +test fixed_rng ... ok test simple ... ok test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out diff --git a/test-cargo-miri/tests/test.rs b/test-cargo-miri/tests/test.rs index 1812ca59c4..cea14d0d1c 100644 --- a/test-cargo-miri/tests/test.rs +++ b/test-cargo-miri/tests/test.rs @@ -10,7 +10,7 @@ fn simple() { // Having more than 1 test does seem to make a difference // (i.e., this calls ptr::swap which having just one test does not). #[test] -fn rng() { +fn fixed_rng() { let mut rng = rand::rngs::StdRng::seed_from_u64(0xdeadcafe); let x: u32 = rng.gen(); let y: u32 = rng.gen(); @@ -19,7 +19,7 @@ fn rng() { #[test] #[cfg(not(target_os="macos"))] // FIXME entropy does not work on macOS -fn seeded_rng() { +fn entropy_rng() { // 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::(); From 341798f09da757d18aa6e139748134561b610d00 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Apr 2019 20:14:23 +0200 Subject: [PATCH 7/9] fix tests on macOS --- test-cargo-miri/tests/test.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test-cargo-miri/tests/test.rs b/test-cargo-miri/tests/test.rs index cea14d0d1c..7b6aaf934e 100644 --- a/test-cargo-miri/tests/test.rs +++ b/test-cargo-miri/tests/test.rs @@ -1,3 +1,5 @@ +#![allow(unused_imports)] // FIXME for macOS + extern crate rand; use rand::{SeedableRng, FromEntropy, Rng, rngs::SmallRng}; From 54039cafa83e58f271261d730dd9bf6bf78fffba Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Apr 2019 20:36:33 +0200 Subject: [PATCH 8/9] try to really fix tests on macOS --- test-cargo-miri/tests/test.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test-cargo-miri/tests/test.rs b/test-cargo-miri/tests/test.rs index 7b6aaf934e..69a31c42a7 100644 --- a/test-cargo-miri/tests/test.rs +++ b/test-cargo-miri/tests/test.rs @@ -20,15 +20,18 @@ fn fixed_rng() { } #[test] -#[cfg(not(target_os="macos"))] // FIXME entropy does not work on macOS fn entropy_rng() { - // 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::(); + #[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::(); - // Also try per-thread RNG. - let mut rng = rand::thread_rng(); - let _val = rng.gen::(); + // Also try per-thread RNG. + let mut rng = rand::thread_rng(); + let _val = rng.gen::(); + } } // A test that won't work on miri From a50512f75134d13a71c0cd99d698f21f17523f7f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Apr 2019 21:04:44 +0200 Subject: [PATCH 9/9] fix RtlGenRandom argument size --- src/fn_call.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fn_call.rs b/src/fn_call.rs index bb9eef0c87..64dcce161d 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -774,7 +774,7 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<' // The actual name of 'RtlGenRandom' "SystemFunction036" => { let ptr = this.read_scalar(args[0])?.to_ptr()?; - let len = this.read_scalar(args[1])?.to_usize(this)?; + let len = this.read_scalar(args[1])?.to_u32()?; if len > 0 { let data = gen_random(this, len as usize)?;