diff --git a/Cargo.lock b/Cargo.lock index 16bfe962a0..57a757f908 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -351,7 +351,19 @@ checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.13.3+wasi-0.2.2", + "windows-targets 0.52.6", ] [[package]] @@ -529,12 +541,12 @@ dependencies = [ "chrono-tz", "colored", "directories", - "getrandom", + "getrandom 0.3.1", "libc", "libffi", "libloading", "measureme", - "rand", + "rand 0.9.0", "regex", "rustc_version", "smallvec", @@ -662,7 +674,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" dependencies = [ "phf_shared", - "rand", + "rand 0.8.5", ] [[package]] @@ -692,7 +704,7 @@ version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" dependencies = [ - "zerocopy", + "zerocopy 0.7.35", ] [[package]] @@ -729,19 +741,28 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ - "libc", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" +dependencies = [ "rand_chacha", - "rand_core", + "rand_core 0.9.0", + "zerocopy 0.8.14", ] [[package]] name = "rand_chacha" -version = "0.3.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.9.0", ] [[package]] @@ -749,8 +770,15 @@ name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "rand_core" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff" dependencies = [ - "getrandom", + "getrandom 0.3.1", + "zerocopy 0.8.14", ] [[package]] @@ -768,7 +796,7 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" dependencies = [ - "getrandom", + "getrandom 0.2.15", "libredox", "thiserror", ] @@ -1105,6 +1133,15 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -1244,6 +1281,15 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] + [[package]] name = "zerocopy" version = "0.7.35" @@ -1251,7 +1297,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ "byteorder", - "zerocopy-derive", + "zerocopy-derive 0.7.35", +] + +[[package]] +name = "zerocopy" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a367f292d93d4eab890745e75a778da40909cab4d6ff8173693812f79c4a2468" +dependencies = [ + "zerocopy-derive 0.8.14", ] [[package]] @@ -1264,3 +1319,14 @@ dependencies = [ "quote", "syn", ] + +[[package]] +name = "zerocopy-derive" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3931cb58c62c13adec22e38686b559c86a30565e16ad6e8510a337cedc611e1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index 07937e7fef..de80722fc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,8 +18,8 @@ test = false # we have no unit tests doctest = false # and no doc tests [dependencies] -getrandom = { version = "0.2", features = ["std"] } -rand = "0.8" +getrandom = { version = "0.3", features = ["std"] } +rand = "0.9" smallvec = { version = "1.7", features = ["drain_filter"] } aes = { version = "0.8.3", features = ["hazmat"] } measureme = "11" diff --git a/src/alloc_addresses/mod.rs b/src/alloc_addresses/mod.rs index 3d0fc5590e..a4f2a117b1 100644 --- a/src/alloc_addresses/mod.rs +++ b/src/alloc_addresses/mod.rs @@ -217,7 +217,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> { // We have to pick a fresh address. // Leave some space to the previous allocation, to give it some chance to be less aligned. // We ensure that `(global_state.next_base_addr + slack) % 16` is uniformly distributed. - let slack = rng.gen_range(0..16); + let slack = rng.random_range(0..16); // From next_base_addr + slack, round up to adjust for alignment. let base_addr = global_state .next_base_addr diff --git a/src/alloc_addresses/reuse_pool.rs b/src/alloc_addresses/reuse_pool.rs index b0c7ee7dff..c0d24a9fbb 100644 --- a/src/alloc_addresses/reuse_pool.rs +++ b/src/alloc_addresses/reuse_pool.rs @@ -58,7 +58,7 @@ impl ReusePool { // We don't remember stack addresses: there's a lot of them (so the perf impact is big), // and we only want to reuse stack slots within the same thread or else we'll add a lot of // undesired synchronization. - if kind == MemoryKind::Stack || !rng.gen_bool(self.address_reuse_rate) { + if kind == MemoryKind::Stack || !rng.random_bool(self.address_reuse_rate) { return; } let clock = clock(); @@ -88,10 +88,10 @@ impl ReusePool { thread: ThreadId, ) -> Option<(u64, Option)> { // Determine whether we'll even attempt a reuse. As above, we don't do reuse for stack addresses. - if kind == MemoryKind::Stack || !rng.gen_bool(self.address_reuse_rate) { + if kind == MemoryKind::Stack || !rng.random_bool(self.address_reuse_rate) { return None; } - let cross_thread_reuse = rng.gen_bool(self.address_reuse_cross_thread_rate); + let cross_thread_reuse = rng.random_bool(self.address_reuse_cross_thread_rate); // Determine the pool to take this from. let subpool = self.subpool(align); // Let's see if we can find something of the right size. We want to find the full range of @@ -118,7 +118,7 @@ impl ReusePool { return None; } // Pick a random element with the desired size. - let idx = rng.gen_range(begin..end); + let idx = rng.random_range(begin..end); // Remove it from the pool and return. let (chosen_addr, chosen_size, chosen_thread, clock) = subpool.remove(idx); debug_assert!(chosen_size >= size && chosen_addr % align.bytes() == 0); diff --git a/src/concurrency/data_race.rs b/src/concurrency/data_race.rs index 4cdc9348dc..b1ca434361 100644 --- a/src/concurrency/data_race.rs +++ b/src/concurrency/data_race.rs @@ -830,7 +830,7 @@ pub trait EvalContextExt<'tcx>: MiriInterpCxExt<'tcx> { let success_rate = 1.0 - this.machine.cmpxchg_weak_failure_rate; let cmpxchg_success = eq.to_scalar().to_bool()? && if can_fail_spuriously { - this.machine.rng.get_mut().gen_bool(success_rate) + this.machine.rng.get_mut().random_bool(success_rate) } else { true }; diff --git a/src/concurrency/thread.rs b/src/concurrency/thread.rs index 6d22dd8d68..a8a2491304 100644 --- a/src/concurrency/thread.rs +++ b/src/concurrency/thread.rs @@ -1138,7 +1138,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { use rand::Rng as _; let this = self.eval_context_mut(); - if this.machine.rng.get_mut().gen_bool(this.machine.preemption_rate) { + if this.machine.rng.get_mut().random_bool(this.machine.preemption_rate) { this.yield_active_thread(); } } diff --git a/src/helpers.rs b/src/helpers.rs index 83b5081c3a..5204056a3d 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -421,7 +421,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { if this.machine.communicate() { // Fill the buffer using the host's rng. - getrandom::getrandom(&mut data) + getrandom::fill(&mut data) .map_err(|err| err_unsup_format!("host getrandom failed: {}", err))?; } else { let rng = this.machine.rng.get_mut(); diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 9eebbc5d36..bce78adcae 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -141,7 +141,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // FIXME: should we check for validity here? It's tricky because we do not have a // place. Codegen does not seem to set any attributes like `noundef` for intrinsic // calls, so we don't *have* to do anything. - let branch: bool = this.machine.rng.get_mut().gen(); + let branch: bool = this.machine.rng.get_mut().random(); this.write_scalar(Scalar::from_bool(branch), dest)?; } @@ -289,7 +289,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let a = this.read_scalar(a)?.to_f32()?; let b = this.read_scalar(b)?.to_f32()?; let c = this.read_scalar(c)?.to_f32()?; - let fuse: bool = this.machine.rng.get_mut().gen(); + let fuse: bool = this.machine.rng.get_mut().random(); let res = if fuse { // FIXME: Using host floats, to work around https://github.com/rust-lang/rustc_apfloat/issues/11 a.to_host().mul_add(b.to_host(), c.to_host()).to_soft() @@ -304,7 +304,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let a = this.read_scalar(a)?.to_f64()?; let b = this.read_scalar(b)?.to_f64()?; let c = this.read_scalar(c)?.to_f64()?; - let fuse: bool = this.machine.rng.get_mut().gen(); + let fuse: bool = this.machine.rng.get_mut().random(); let res = if fuse { // FIXME: Using host floats, to work around https://github.com/rust-lang/rustc_apfloat/issues/11 a.to_host().mul_add(b.to_host(), c.to_host()).to_soft() diff --git a/src/intrinsics/simd.rs b/src/intrinsics/simd.rs index 6cc34149f6..45e316b190 100644 --- a/src/intrinsics/simd.rs +++ b/src/intrinsics/simd.rs @@ -304,7 +304,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let c = this.read_scalar(&this.project_index(&c, i)?)?; let dest = this.project_index(&dest, i)?; - let fuse: bool = intrinsic_name == "fma" || this.machine.rng.get_mut().gen(); + let fuse: bool = intrinsic_name == "fma" || this.machine.rng.get_mut().random(); // Works for f32 and f64. // FIXME: using host floats to work around https://github.com/rust-lang/miri/issues/2468. diff --git a/src/machine.rs b/src/machine.rs index 5cb6640bf8..ff4b0ac382 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -1499,7 +1499,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> { catch_unwind: None, timing, is_user_relevant: ecx.machine.is_user_relevant(&frame), - salt: ecx.machine.rng.borrow_mut().gen::() % ADDRS_PER_ANON_GLOBAL, + salt: ecx.machine.rng.borrow_mut().random_range(0..ADDRS_PER_ANON_GLOBAL), data_race: ecx.machine.data_race.as_ref().map(|_| data_race::FrameState::default()), }; @@ -1714,7 +1714,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> { if unique { CTFE_ALLOC_SALT } else { - ecx.machine.rng.borrow_mut().gen::() % ADDRS_PER_ANON_GLOBAL + ecx.machine.rng.borrow_mut().random_range(0..ADDRS_PER_ANON_GLOBAL) } } diff --git a/src/math.rs b/src/math.rs index 8422e7ccb6..7117f722fe 100644 --- a/src/math.rs +++ b/src/math.rs @@ -1,5 +1,4 @@ use rand::Rng as _; -use rand::distributions::Distribution as _; use rustc_apfloat::Float as _; use rustc_apfloat::ieee::IeeeFloat; @@ -18,13 +17,12 @@ pub(crate) fn apply_random_float_error( // (When read as binary, the position of the first `1` determines the exponent, // and the remaining bits fill the mantissa. `PREC` is one plus the size of the mantissa, // so this all works out.) - let dist = rand::distributions::Uniform::new(0, 1 << F::PRECISION); - let r = F::from_u128(dist.sample(rng)).value; + let r = F::from_u128(rng.random_range(0..(1 << F::PRECISION))).value; // Multiply this with 2^(scale - PREC). The result is between 0 and // 2^PREC * 2^(scale - PREC) = 2^scale. let err = r.scalbn(err_scale.strict_sub(F::PRECISION.try_into().unwrap())); // give it a random sign - let err = if rng.gen::() { -err } else { err }; + let err = if rng.random() { -err } else { err }; // multiple the value with (1+err) (val * (F::from_u128(1).value + err).value).value } diff --git a/src/operator.rs b/src/operator.rs index 0017a3991b..124b2a765a 100644 --- a/src/operator.rs +++ b/src/operator.rs @@ -108,7 +108,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // Pick one of the NaNs. let nan = nans.choose(&mut *rand).unwrap(); // Non-deterministically flip the sign. - if rand.gen() { + if rand.random() { // This will properly flip even for NaN. -nan } else { diff --git a/src/shims/unix/fs.rs b/src/shims/unix/fs.rs index ee95bdd5af..be2a2a9a8f 100644 --- a/src/shims/unix/fs.rs +++ b/src/shims/unix/fs.rs @@ -1550,7 +1550,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { } } fn mkstemp(&mut self, template_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> { - use rand::seq::SliceRandom; + use rand::seq::IndexedRandom; // POSIX defines the template string. const TEMPFILE_TEMPLATE_STR: &str = "XXXXXX"; diff --git a/test_dependencies/Cargo.lock b/test_dependencies/Cargo.lock index 0a5e9f62dd..af92f9d0de 100644 --- a/test_dependencies/Cargo.lock +++ b/test_dependencies/Cargo.lock @@ -105,6 +105,18 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.13.3+wasi-0.2.2", + "windows-targets", +] + [[package]] name = "gimli" version = "0.29.0" @@ -178,6 +190,7 @@ dependencies = [ "cfg-if", "getrandom 0.1.16", "getrandom 0.2.15", + "getrandom 0.3.1", "libc", "num_cpus", "page_size", @@ -359,6 +372,15 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "wasm-bindgen" version = "0.2.92" @@ -507,3 +529,12 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] diff --git a/test_dependencies/Cargo.toml b/test_dependencies/Cargo.toml index da9d20726a..78dddaf11d 100644 --- a/test_dependencies/Cargo.toml +++ b/test_dependencies/Cargo.toml @@ -15,6 +15,7 @@ cfg-if = "1" getrandom_01 = { package = "getrandom", version = "0.1" } getrandom_02 = { package = "getrandom", version = "0.2", features = ["js"] } +getrandom_03 = { package = "getrandom", version = "0.3" } [target.'cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))'.dependencies] tempfile = "3" diff --git a/tests/pass-dep/getrandom.rs b/tests/pass-dep/getrandom.rs index a5bc5ec707..d359730e7f 100644 --- a/tests/pass-dep/getrandom.rs +++ b/tests/pass-dep/getrandom.rs @@ -3,7 +3,7 @@ //@revisions: isolation no_isolation //@[no_isolation]compile-flags: -Zmiri-disable-isolation -/// Test direct calls of getrandom 0.1 and 0.2. +/// Test direct calls of getrandom 0.1, 0.2 and 0.3. fn main() { let mut data = vec![0; 16]; @@ -13,4 +13,6 @@ fn main() { getrandom_01::getrandom(&mut data).unwrap(); getrandom_02::getrandom(&mut data).unwrap(); + + getrandom_03::fill(&mut data).unwrap(); }