Skip to content

Commit

Permalink
Merge pull request #4170 from eduardosm/update-rand
Browse files Browse the repository at this point in the history
Update rand and getrandom dependencies
  • Loading branch information
RalfJung authored Feb 1, 2025
2 parents 44e9863 + 0bc2ad5 commit 73b483d
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 36 deletions.
92 changes: 79 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/alloc_addresses/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/alloc_addresses/reuse_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -88,10 +88,10 @@ impl ReusePool {
thread: ThreadId,
) -> Option<(u64, Option<VClock>)> {
// 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
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/data_race.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/intrinsics/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<usize>() % 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()),
};

Expand Down Expand Up @@ -1714,7 +1714,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
if unique {
CTFE_ALLOC_SALT
} else {
ecx.machine.rng.borrow_mut().gen::<usize>() % ADDRS_PER_ANON_GLOBAL
ecx.machine.rng.borrow_mut().random_range(0..ADDRS_PER_ANON_GLOBAL)
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/math.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rand::Rng as _;
use rand::distributions::Distribution as _;
use rustc_apfloat::Float as _;
use rustc_apfloat::ieee::IeeeFloat;

Expand All @@ -18,13 +17,12 @@ pub(crate) fn apply_random_float_error<F: rustc_apfloat::Float>(
// (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::<bool>() { -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading

0 comments on commit 73b483d

Please sign in to comment.