Skip to content

Commit 4cbbb34

Browse files
authored
Remove automatic (delayed) reseed-on-fork (#1379)
* benches/generators.rs: standardize thread_rng benchmarks * Remove cfgs from examples * Remove ReadRng * Add ThreadRng::reseed and doc to use * Remove fork protection from ReseedingRng; remove libc dep * Enable ReseedingRng without std * Move ReseedingRng up; remove module rand::rngs::adapter
1 parent b45e892 commit 4cbbb34

13 files changed

+53
-311
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1111
## [0.9.1] - unreleased
1212
- Add the `Slice::num_choices` method to the Slice distribution (#1402)
1313

14+
### Generators
15+
- `ReseedingRng::reseed` also resets the random data cache.
16+
- Remove fork-protection from `ReseedingRng` and `ThreadRng`. Instead, it is recommended to call `ThreadRng::reseed` on fork.
17+
1418
## [0.9.0-alpha.0] - 2024-02-18
1519
This is a pre-release. To depend on this version, use `rand = "=0.9.0-alpha.0"` to prevent automatic updates (which can be expected to include breaking changes).
1620

Cargo.toml

+2-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ include = ["src/", "LICENSE-*", "README.md", "CHANGELOG.md", "COPYRIGHT"]
2121
# To build locally:
2222
# RUSTDOCFLAGS="--cfg doc_cfg -Zunstable-options --generate-link-to-definition" cargo +nightly doc --all --all-features --no-deps --open
2323
all-features = true
24-
rustdoc-args = ["--cfg", "doc_cfg", "--generate-link-to-definition"]
24+
rustdoc-args = ["--cfg", "doc_cfg", "-Zunstable-options", "--generate-link-to-definition"]
2525

2626
[package.metadata.playground]
2727
features = ["small_rng", "serde1"]
@@ -34,7 +34,7 @@ serde1 = ["serde", "rand_core/serde1"]
3434

3535
# Option (enabled by default): without "std" rand uses libcore; this option
3636
# enables functionality expected to be available on a standard platform.
37-
std = ["rand_core/std", "rand_chacha?/std", "alloc", "libc"]
37+
std = ["rand_core/std", "rand_chacha?/std", "alloc"]
3838

3939
# Option: "alloc" enables support for Vec and Box when not using "std"
4040
alloc = ["rand_core/alloc"]
@@ -71,10 +71,6 @@ serde = { version = "1.0.103", features = ["derive"], optional = true }
7171
rand_chacha = { path = "rand_chacha", version = "=0.9.0-alpha.0", default-features = false, optional = true }
7272
zerocopy = { version = "=0.8.0-alpha.6", default-features = false, features = ["simd"] }
7373

74-
[target.'cfg(unix)'.dependencies]
75-
# Used for fork protection (reseeding.rs)
76-
libc = { version = "0.2.22", optional = true, default-features = false }
77-
7874
[dev-dependencies]
7975
rand_pcg = { path = "rand_pcg", version = "=0.9.0-alpha.0" }
8076
# Only to test serde1

SECURITY.md

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ are expected to provide the following:
2323
For some RNGs, notably `OsRng`, `ThreadRng` and those wrapped by `ReseedingRng`,
2424
we provide limited mitigations against side-channel attacks:
2525

26-
- After a process fork on Unix, there is an upper-bound on the number of bits
27-
output by the RNG before the processes diverge, after which outputs from
28-
each process's RNG are uncorrelated
2926
- After the state (memory) of an RNG is leaked, there is an upper-bound on the
3027
number of bits of output by the RNG before prediction of output by an
3128
observer again becomes computationally-infeasible

benches/generators.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core::mem::size_of;
1818
use test::{black_box, Bencher};
1919

2020
use rand::prelude::*;
21-
use rand::rngs::adapter::ReseedingRng;
21+
use rand::rngs::ReseedingRng;
2222
use rand::rngs::{mock::StepRng, OsRng};
2323
use rand_chacha::{ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Rng};
2424
use rand_pcg::{Pcg32, Pcg64, Pcg64Mcg, Pcg64Dxsm};
@@ -52,6 +52,7 @@ gen_bytes!(gen_bytes_std, StdRng::from_entropy());
5252
#[cfg(feature = "small_rng")]
5353
gen_bytes!(gen_bytes_small, SmallRng::from_thread_rng());
5454
gen_bytes!(gen_bytes_os, OsRng);
55+
gen_bytes!(gen_bytes_thread, thread_rng());
5556

5657
macro_rules! gen_uint {
5758
($fnn:ident, $ty:ty, $gen:expr) => {
@@ -82,6 +83,7 @@ gen_uint!(gen_u32_std, u32, StdRng::from_entropy());
8283
#[cfg(feature = "small_rng")]
8384
gen_uint!(gen_u32_small, u32, SmallRng::from_thread_rng());
8485
gen_uint!(gen_u32_os, u32, OsRng);
86+
gen_uint!(gen_u32_thread, u32, thread_rng());
8587

8688
gen_uint!(gen_u64_step, u64, StepRng::new(0, 1));
8789
gen_uint!(gen_u64_pcg32, u64, Pcg32::from_entropy());
@@ -95,6 +97,7 @@ gen_uint!(gen_u64_std, u64, StdRng::from_entropy());
9597
#[cfg(feature = "small_rng")]
9698
gen_uint!(gen_u64_small, u64, SmallRng::from_thread_rng());
9799
gen_uint!(gen_u64_os, u64, OsRng);
100+
gen_uint!(gen_u64_thread, u64, thread_rng());
98101

99102
macro_rules! init_gen {
100103
($fnn:ident, $gen:ident) => {
@@ -141,24 +144,3 @@ reseeding_bytes!(reseeding_chacha20_32k, 32);
141144
reseeding_bytes!(reseeding_chacha20_64k, 64);
142145
reseeding_bytes!(reseeding_chacha20_256k, 256);
143146
reseeding_bytes!(reseeding_chacha20_1M, 1024);
144-
145-
146-
macro_rules! threadrng_uint {
147-
($fnn:ident, $ty:ty) => {
148-
#[bench]
149-
fn $fnn(b: &mut Bencher) {
150-
let mut rng = thread_rng();
151-
b.iter(|| {
152-
let mut accum: $ty = 0;
153-
for _ in 0..RAND_BENCH_N {
154-
accum = accum.wrapping_add(rng.gen::<$ty>());
155-
}
156-
accum
157-
});
158-
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
159-
}
160-
};
161-
}
162-
163-
threadrng_uint!(thread_rng_u32, u32);
164-
threadrng_uint!(thread_rng_u64, u64);

examples/monte-carlo.rs

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
//! We can use the above fact to estimate the value of π: pick many points in
2424
//! the square at random, calculate the fraction that fall within the circle,
2525
//! and multiply this fraction by 4.
26-
27-
#![cfg(all(feature = "std", feature = "std_rng"))]
28-
2926
use rand::distributions::{Distribution, Uniform};
3027

3128
fn main() {

examples/monty-hall.rs

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
//!
2727
//! [Monty Hall Problem]: https://en.wikipedia.org/wiki/Monty_Hall_problem
2828
29-
#![cfg(all(feature = "std", feature = "std_rng"))]
30-
3129
use rand::distributions::{Distribution, Uniform};
3230
use rand::Rng;
3331

examples/rayon-monte-carlo.rs

-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
//! over BATCH_SIZE trials. Manually batching also turns out to be faster
3939
//! for the nondeterministic version of this program as well.
4040
41-
#![cfg(all(feature = "std", feature = "std_rng"))]
42-
4341
use rand::distributions::{Distribution, Uniform};
4442
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
4543
use rayon::prelude::*;

src/rngs/adapter/mod.rs

-16
This file was deleted.

src/rngs/adapter/read.rs

-150
This file was deleted.

src/rngs/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@
9696
//! [`rand_xoshiro`]: https://crates.io/crates/rand_xoshiro
9797
//! [`rng` tag]: https://crates.io/keywords/rng
9898
99-
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
100-
#[cfg(feature = "std")] pub mod adapter;
99+
mod reseeding;
100+
pub use reseeding::ReseedingRng;
101101

102102
pub mod mock; // Public so we don't export `StepRng` directly, making it a bit
103103
// more clear it is intended for testing.

0 commit comments

Comments
 (0)