Skip to content

Commit 73b483d

Browse files
authored
Merge pull request #4170 from eduardosm/update-rand
Update rand and getrandom dependencies
2 parents 44e9863 + 0bc2ad5 commit 73b483d

File tree

16 files changed

+134
-36
lines changed

16 files changed

+134
-36
lines changed

Cargo.lock

+79-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ test = false # we have no unit tests
1818
doctest = false # and no doc tests
1919

2020
[dependencies]
21-
getrandom = { version = "0.2", features = ["std"] }
22-
rand = "0.8"
21+
getrandom = { version = "0.3", features = ["std"] }
22+
rand = "0.9"
2323
smallvec = { version = "1.7", features = ["drain_filter"] }
2424
aes = { version = "0.8.3", features = ["hazmat"] }
2525
measureme = "11"

src/alloc_addresses/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
217217
// We have to pick a fresh address.
218218
// Leave some space to the previous allocation, to give it some chance to be less aligned.
219219
// We ensure that `(global_state.next_base_addr + slack) % 16` is uniformly distributed.
220-
let slack = rng.gen_range(0..16);
220+
let slack = rng.random_range(0..16);
221221
// From next_base_addr + slack, round up to adjust for alignment.
222222
let base_addr = global_state
223223
.next_base_addr

src/alloc_addresses/reuse_pool.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl ReusePool {
5858
// We don't remember stack addresses: there's a lot of them (so the perf impact is big),
5959
// and we only want to reuse stack slots within the same thread or else we'll add a lot of
6060
// undesired synchronization.
61-
if kind == MemoryKind::Stack || !rng.gen_bool(self.address_reuse_rate) {
61+
if kind == MemoryKind::Stack || !rng.random_bool(self.address_reuse_rate) {
6262
return;
6363
}
6464
let clock = clock();
@@ -88,10 +88,10 @@ impl ReusePool {
8888
thread: ThreadId,
8989
) -> Option<(u64, Option<VClock>)> {
9090
// Determine whether we'll even attempt a reuse. As above, we don't do reuse for stack addresses.
91-
if kind == MemoryKind::Stack || !rng.gen_bool(self.address_reuse_rate) {
91+
if kind == MemoryKind::Stack || !rng.random_bool(self.address_reuse_rate) {
9292
return None;
9393
}
94-
let cross_thread_reuse = rng.gen_bool(self.address_reuse_cross_thread_rate);
94+
let cross_thread_reuse = rng.random_bool(self.address_reuse_cross_thread_rate);
9595
// Determine the pool to take this from.
9696
let subpool = self.subpool(align);
9797
// 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 {
118118
return None;
119119
}
120120
// Pick a random element with the desired size.
121-
let idx = rng.gen_range(begin..end);
121+
let idx = rng.random_range(begin..end);
122122
// Remove it from the pool and return.
123123
let (chosen_addr, chosen_size, chosen_thread, clock) = subpool.remove(idx);
124124
debug_assert!(chosen_size >= size && chosen_addr % align.bytes() == 0);

src/concurrency/data_race.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ pub trait EvalContextExt<'tcx>: MiriInterpCxExt<'tcx> {
830830
let success_rate = 1.0 - this.machine.cmpxchg_weak_failure_rate;
831831
let cmpxchg_success = eq.to_scalar().to_bool()?
832832
&& if can_fail_spuriously {
833-
this.machine.rng.get_mut().gen_bool(success_rate)
833+
this.machine.rng.get_mut().random_bool(success_rate)
834834
} else {
835835
true
836836
};

src/concurrency/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11381138
use rand::Rng as _;
11391139

11401140
let this = self.eval_context_mut();
1141-
if this.machine.rng.get_mut().gen_bool(this.machine.preemption_rate) {
1141+
if this.machine.rng.get_mut().random_bool(this.machine.preemption_rate) {
11421142
this.yield_active_thread();
11431143
}
11441144
}

src/helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
421421

422422
if this.machine.communicate() {
423423
// Fill the buffer using the host's rng.
424-
getrandom::getrandom(&mut data)
424+
getrandom::fill(&mut data)
425425
.map_err(|err| err_unsup_format!("host getrandom failed: {}", err))?;
426426
} else {
427427
let rng = this.machine.rng.get_mut();

src/intrinsics/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
141141
// FIXME: should we check for validity here? It's tricky because we do not have a
142142
// place. Codegen does not seem to set any attributes like `noundef` for intrinsic
143143
// calls, so we don't *have* to do anything.
144-
let branch: bool = this.machine.rng.get_mut().gen();
144+
let branch: bool = this.machine.rng.get_mut().random();
145145
this.write_scalar(Scalar::from_bool(branch), dest)?;
146146
}
147147

@@ -289,7 +289,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
289289
let a = this.read_scalar(a)?.to_f32()?;
290290
let b = this.read_scalar(b)?.to_f32()?;
291291
let c = this.read_scalar(c)?.to_f32()?;
292-
let fuse: bool = this.machine.rng.get_mut().gen();
292+
let fuse: bool = this.machine.rng.get_mut().random();
293293
let res = if fuse {
294294
// FIXME: Using host floats, to work around https://github.com/rust-lang/rustc_apfloat/issues/11
295295
a.to_host().mul_add(b.to_host(), c.to_host()).to_soft()
@@ -304,7 +304,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
304304
let a = this.read_scalar(a)?.to_f64()?;
305305
let b = this.read_scalar(b)?.to_f64()?;
306306
let c = this.read_scalar(c)?.to_f64()?;
307-
let fuse: bool = this.machine.rng.get_mut().gen();
307+
let fuse: bool = this.machine.rng.get_mut().random();
308308
let res = if fuse {
309309
// FIXME: Using host floats, to work around https://github.com/rust-lang/rustc_apfloat/issues/11
310310
a.to_host().mul_add(b.to_host(), c.to_host()).to_soft()

src/intrinsics/simd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
304304
let c = this.read_scalar(&this.project_index(&c, i)?)?;
305305
let dest = this.project_index(&dest, i)?;
306306

307-
let fuse: bool = intrinsic_name == "fma" || this.machine.rng.get_mut().gen();
307+
let fuse: bool = intrinsic_name == "fma" || this.machine.rng.get_mut().random();
308308

309309
// Works for f32 and f64.
310310
// FIXME: using host floats to work around https://github.com/rust-lang/miri/issues/2468.

src/machine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
14991499
catch_unwind: None,
15001500
timing,
15011501
is_user_relevant: ecx.machine.is_user_relevant(&frame),
1502-
salt: ecx.machine.rng.borrow_mut().gen::<usize>() % ADDRS_PER_ANON_GLOBAL,
1502+
salt: ecx.machine.rng.borrow_mut().random_range(0..ADDRS_PER_ANON_GLOBAL),
15031503
data_race: ecx.machine.data_race.as_ref().map(|_| data_race::FrameState::default()),
15041504
};
15051505

@@ -1714,7 +1714,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
17141714
if unique {
17151715
CTFE_ALLOC_SALT
17161716
} else {
1717-
ecx.machine.rng.borrow_mut().gen::<usize>() % ADDRS_PER_ANON_GLOBAL
1717+
ecx.machine.rng.borrow_mut().random_range(0..ADDRS_PER_ANON_GLOBAL)
17181718
}
17191719
}
17201720

src/math.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rand::Rng as _;
2-
use rand::distributions::Distribution as _;
32
use rustc_apfloat::Float as _;
43
use rustc_apfloat::ieee::IeeeFloat;
54

@@ -18,13 +17,12 @@ pub(crate) fn apply_random_float_error<F: rustc_apfloat::Float>(
1817
// (When read as binary, the position of the first `1` determines the exponent,
1918
// and the remaining bits fill the mantissa. `PREC` is one plus the size of the mantissa,
2019
// so this all works out.)
21-
let dist = rand::distributions::Uniform::new(0, 1 << F::PRECISION);
22-
let r = F::from_u128(dist.sample(rng)).value;
20+
let r = F::from_u128(rng.random_range(0..(1 << F::PRECISION))).value;
2321
// Multiply this with 2^(scale - PREC). The result is between 0 and
2422
// 2^PREC * 2^(scale - PREC) = 2^scale.
2523
let err = r.scalbn(err_scale.strict_sub(F::PRECISION.try_into().unwrap()));
2624
// give it a random sign
27-
let err = if rng.gen::<bool>() { -err } else { err };
25+
let err = if rng.random() { -err } else { err };
2826
// multiple the value with (1+err)
2927
(val * (F::from_u128(1).value + err).value).value
3028
}

src/operator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
108108
// Pick one of the NaNs.
109109
let nan = nans.choose(&mut *rand).unwrap();
110110
// Non-deterministically flip the sign.
111-
if rand.gen() {
111+
if rand.random() {
112112
// This will properly flip even for NaN.
113113
-nan
114114
} else {

src/shims/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
15501550
}
15511551
}
15521552
fn mkstemp(&mut self, template_op: &OpTy<'tcx>) -> InterpResult<'tcx, Scalar> {
1553-
use rand::seq::SliceRandom;
1553+
use rand::seq::IndexedRandom;
15541554

15551555
// POSIX defines the template string.
15561556
const TEMPFILE_TEMPLATE_STR: &str = "XXXXXX";

0 commit comments

Comments
 (0)