From 33d6349e9e771a8ff871286c2eb6f6abaced1344 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Wed, 13 May 2026 11:26:20 -0400 Subject: [PATCH 1/2] Upgrade to rand 0.10.1 Make Endpoint::new panicking on CSPRNG init failure. Co-authored-by: Dirkjan Ochtman --- Cargo.lock | 238 ++++++++++++++++--- Cargo.toml | 6 +- quinn-proto/Cargo.toml | 2 +- quinn-proto/src/cid_generator.rs | 2 +- quinn-proto/src/config/mod.rs | 4 +- quinn-proto/src/congestion/bbr/mod.rs | 14 +- quinn-proto/src/connection/mod.rs | 2 +- quinn-proto/src/connection/packet_builder.rs | 2 +- quinn-proto/src/connection/spaces.rs | 2 +- quinn-proto/src/endpoint.rs | 15 +- quinn-proto/src/tests/mod.rs | 2 +- quinn-proto/src/token.rs | 6 +- quinn-proto/src/token_memory_cache.rs | 2 +- quinn-proto/src/transport_parameters.rs | 29 ++- quinn/src/tests.rs | 2 +- quinn/tests/many_connections.rs | 2 +- 16 files changed, 257 insertions(+), 73 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 71f42c22a2..1f03f8a103 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -514,6 +514,17 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "chacha20" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f8d983286843e49675a4b7a2d174efe136dc93a18d69130dd18198a6c167601" +dependencies = [ + "cfg-if", + "cpufeatures", + "rand_core", +] + [[package]] name = "ciborium" version = "0.2.2" @@ -683,6 +694,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cpufeatures" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b2a41393f66f16b0823bb79094d54ac5fbd34ab292ddafb9a0456ac9f87d201" +dependencies = [ + "libc", +] + [[package]] name = "crc" version = "3.4.0" @@ -924,10 +944,9 @@ version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef975e30683b2d965054bb0a836f8973857c4ebf6acf274fe46617cd285060d8" dependencies = [ - "foldhash", + "foldhash 0.2.0", "libm", "portable-atomic", - "rand", "siphasher", ] @@ -959,6 +978,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "foldhash" version = "0.2.0" @@ -1065,12 +1090,26 @@ name = "getrandom" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "139ef39800118c7683f2fd3c98c1b23c09ae076556b435f8e9064ae108aaeeec" dependencies = [ "cfg-if", "js-sys", "libc", "r-efi", + "rand_core", "wasip2", + "wasip3", "wasm-bindgen", ] @@ -1110,6 +1149,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash 0.1.5", +] + [[package]] name = "hashbrown" version = "0.17.0" @@ -1335,6 +1383,12 @@ dependencies = [ "zerovec", ] +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + [[package]] name = "ident_case" version = "1.0.1" @@ -1369,7 +1423,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.17.0", + "serde", + "serde_core", ] [[package]] @@ -1479,6 +1535,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + [[package]] name = "libc" version = "0.2.185" @@ -1861,15 +1923,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" -[[package]] -name = "ppv-lite86" -version = "0.2.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" -dependencies = [ - "zerocopy", -] - [[package]] name = "prettyplease" version = "0.2.37" @@ -1973,7 +2026,7 @@ dependencies = [ "aws-lc-rs", "bytes", "fastbloom", - "getrandom 0.3.4", + "getrandom 0.4.1", "hex-literal", "lru-slab", "qlog", @@ -2025,38 +2078,26 @@ checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" [[package]] name = "rand" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea" -dependencies = [ - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.9.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +checksum = "d2e8e8bcc7961af1fdac401278c6a831614941f6164ee3bf4ce61b7edb162207" dependencies = [ - "ppv-lite86", + "chacha20", + "getrandom 0.4.1", "rand_core", ] [[package]] name = "rand_core" -version = "0.9.5" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" -dependencies = [ - "getrandom 0.3.4", -] +checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba" [[package]] name = "rand_pcg" -version = "0.9.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b48ac3f7ffaab7fac4d2376632268aa5f89abdb55f7ebf8f4d11fffccb2320f7" +checksum = "2a36dd10a879f5c16e363eed24471b321b114ce3112c2d5d8df06545fcf48152" dependencies = [ "rand_core", ] @@ -2828,6 +2869,12 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + [[package]] name = "untrusted" version = "0.9.0" @@ -2898,6 +2945,15 @@ dependencies = [ "wit-bindgen", ] +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen", +] + [[package]] name = "wasm-bindgen" version = "0.2.118" @@ -2992,6 +3048,40 @@ version = "0.2.118" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23cda5ecc67248c48d3e705d3e03e00af905769b78b9d2a1678b663b8b9d4472" +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + [[package]] name = "web-time" version = "1.1.0" @@ -3135,6 +3225,88 @@ name = "wit-bindgen" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] [[package]] name = "writeable" diff --git a/Cargo.toml b/Cargo.toml index 4c1580d666..533a6b7ca6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,16 +22,16 @@ bytes = "1" clap = { version = "4", features = ["derive"] } crc = "3" directories-next = "2" -fastbloom = "0.17" +fastbloom = { version = "0.17", default-features = false } futures-io = "0.3.19" -getrandom = { version = "0.3", default-features = false } +getrandom = { version = "0.4", default-features = false } hdrhistogram = { version = "7.2", default-features = false } hex-literal = "1" lru-slab = "0.1.2" log = "0.4" pin-project-lite = "0.2" qlog = "0.17" -rand = "0.9" +rand = "0.10.1" rcgen = "0.14" ring = "0.17" rustc-hash = "2" diff --git a/quinn-proto/Cargo.toml b/quinn-proto/Cargo.toml index 9d04b08a1e..5b7c514f93 100644 --- a/quinn-proto/Cargo.toml +++ b/quinn-proto/Cargo.toml @@ -69,7 +69,7 @@ web-time = { workspace = true } [dev-dependencies] assert_matches = { workspace = true } hex-literal = { workspace = true } -rand_pcg = "0.9" +rand_pcg = "0.10" rcgen = { workspace = true } tracing-subscriber = { workspace = true } wasm-bindgen-test = { workspace = true } diff --git a/quinn-proto/src/cid_generator.rs b/quinn-proto/src/cid_generator.rs index 7af4eca84a..810f5603e7 100644 --- a/quinn-proto/src/cid_generator.rs +++ b/quinn-proto/src/cid_generator.rs @@ -1,6 +1,6 @@ use std::hash::Hasher; -use rand::{Rng, RngCore}; +use rand::{Rng, RngExt}; use crate::Duration; use crate::MAX_CID_SIZE; diff --git a/quinn-proto/src/config/mod.rs b/quinn-proto/src/config/mod.rs index b7c799248a..129ee1dbaa 100644 --- a/quinn-proto/src/config/mod.rs +++ b/quinn-proto/src/config/mod.rs @@ -179,7 +179,7 @@ impl Default for EndpointConfig { fn default() -> Self { #[cfg(all(feature = "aws-lc-rs", not(feature = "ring")))] use aws_lc_rs::hmac; - use rand::RngCore; + use rand::Rng; #[cfg(feature = "ring")] use ring::hmac; @@ -396,7 +396,7 @@ impl ServerConfig { pub fn with_crypto(crypto: Arc) -> Self { #[cfg(all(feature = "aws-lc-rs", not(feature = "ring")))] use aws_lc_rs::hkdf; - use rand::RngCore; + use rand::Rng; #[cfg(feature = "ring")] use ring::hkdf; diff --git a/quinn-proto/src/congestion/bbr/mod.rs b/quinn-proto/src/congestion/bbr/mod.rs index a8607b7134..69f91f6797 100644 --- a/quinn-proto/src/congestion/bbr/mod.rs +++ b/quinn-proto/src/congestion/bbr/mod.rs @@ -2,12 +2,14 @@ use std::any::Any; use std::fmt::Debug; use std::sync::Arc; -use rand::{Rng, SeedableRng}; +use rand::rngs::{StdRng, SysRng}; +use rand::{Rng, RngExt, SeedableRng}; use crate::congestion::ControllerMetrics; use crate::congestion::bbr::bw_estimation::BandwidthEstimation; use crate::congestion::bbr::min_max::MinMax; use crate::connection::RttEstimator; +use crate::endpoint::NoRandomBytes; use crate::{Duration, Instant}; use super::{BASE_DATAGRAM_SIZE, Controller, ControllerFactory}; @@ -56,14 +58,14 @@ pub struct Bbr { bw_at_last_round: u64, round_wo_bw_gain: u64, ack_aggregation: AckAggregationState, - random_number_generator: rand::rngs::StdRng, + random_number_generator: StdRng, } impl Bbr { /// Construct a state using the given `config` and current time `now` - pub fn new(config: Arc, current_mtu: u16) -> Self { + pub fn new(config: Arc, current_mtu: u16) -> Result { let initial_window = config.initial_window; - Self { + Ok(Self { config, current_mtu: current_mtu as u64, max_bandwidth: BandwidthEstimation::default(), @@ -97,8 +99,8 @@ impl Bbr { bw_at_last_round: 0, round_wo_bw_gain: 0, ack_aggregation: AckAggregationState::default(), - random_number_generator: rand::rngs::StdRng::from_os_rng(), - } + random_number_generator: StdRng::try_from_rng(&mut SysRng)?, + }) } fn enter_startup_mode(&mut self) { diff --git a/quinn-proto/src/connection/mod.rs b/quinn-proto/src/connection/mod.rs index d885867c02..18c9d3be37 100644 --- a/quinn-proto/src/connection/mod.rs +++ b/quinn-proto/src/connection/mod.rs @@ -10,7 +10,7 @@ use std::{ use bytes::{Bytes, BytesMut}; use frame::StreamMetaVec; -use rand::{Rng, SeedableRng, rngs::StdRng}; +use rand::{RngExt, SeedableRng, rngs::StdRng}; use thiserror::Error; use tracing::{debug, error, trace, trace_span, warn}; diff --git a/quinn-proto/src/connection/packet_builder.rs b/quinn-proto/src/connection/packet_builder.rs index e31dc193f3..189479c244 100644 --- a/quinn-proto/src/connection/packet_builder.rs +++ b/quinn-proto/src/connection/packet_builder.rs @@ -1,5 +1,5 @@ use bytes::Bytes; -use rand::Rng; +use rand::RngExt; use tracing::{debug, trace, trace_span}; use super::{Connection, SentFrames, spaces::SentPacket}; diff --git a/quinn-proto/src/connection/spaces.rs b/quinn-proto/src/connection/spaces.rs index b5f412622e..b2b38e07e8 100644 --- a/quinn-proto/src/connection/spaces.rs +++ b/quinn-proto/src/connection/spaces.rs @@ -5,7 +5,7 @@ use std::{ ops::{Bound, Index, IndexMut}, }; -use rand::Rng; +use rand::{Rng, RngExt}; use rustc_hash::FxHashSet; use tracing::trace; diff --git a/quinn-proto/src/endpoint.rs b/quinn-proto/src/endpoint.rs index 6f5d94b295..9dcd15e3aa 100644 --- a/quinn-proto/src/endpoint.rs +++ b/quinn-proto/src/endpoint.rs @@ -8,7 +8,10 @@ use std::{ }; use bytes::{BufMut, Bytes, BytesMut}; -use rand::{Rng, RngCore, SeedableRng, rngs::StdRng}; +use rand::{ + Rng, RngExt, SeedableRng, + rngs::{StdRng, SysRng}, +}; use rustc_hash::FxHashMap; use slab::Slab; use thiserror::Error; @@ -61,15 +64,19 @@ impl Endpoint { /// `allow_mtud` enables path MTU detection when requested by `Connection` configuration for /// better performance. This requires that outgoing packets are never fragmented, which can be /// achieved via e.g. the `IPV6_DONTFRAG` socket option. + /// + /// Returns an error if the endpoint cannot be seeded from the operating system random source. pub fn new( config: Arc, server_config: Option>, allow_mtud: bool, ) -> Self { Self { - rng: config - .rng_seed - .map_or_else(StdRng::from_os_rng, StdRng::from_seed), + rng: match config.rng_seed { + Some(seed) => StdRng::from_seed(seed), + None => StdRng::try_from_rng(&mut SysRng) + .expect("failed to seed random number generator from system"), + }, index: ConnectionIndex::default(), connections: Slab::new(), local_cid_generator: (config.connection_id_generator_factory.as_ref())(), diff --git a/quinn-proto/src/tests/mod.rs b/quinn-proto/src/tests/mod.rs index bcff9ae597..07e7f4d42d 100644 --- a/quinn-proto/src/tests/mod.rs +++ b/quinn-proto/src/tests/mod.rs @@ -10,7 +10,7 @@ use assert_matches::assert_matches; use aws_lc_rs::hmac; use bytes::{Bytes, BytesMut}; use hex_literal::hex; -use rand::RngCore; +use rand::Rng; #[cfg(feature = "ring")] use ring::hmac; #[cfg(all(feature = "rustls-aws-lc-rs", not(feature = "rustls-ring")))] diff --git a/quinn-proto/src/token.rs b/quinn-proto/src/token.rs index c128d06030..a3ffebae78 100644 --- a/quinn-proto/src/token.rs +++ b/quinn-proto/src/token.rs @@ -5,7 +5,7 @@ use std::{ }; use bytes::{Buf, BufMut, Bytes}; -use rand::Rng; +use rand::{Rng, RngExt}; use crate::{ Duration, RESET_TOKEN_SIZE, ServerConfig, SystemTime, UNIX_EPOCH, @@ -411,7 +411,6 @@ mod test { use super::*; #[cfg(all(feature = "aws-lc-rs", not(feature = "ring")))] use aws_lc_rs::hkdf; - use rand::prelude::*; #[cfg(feature = "ring")] use ring::hkdf; @@ -484,9 +483,6 @@ mod test { #[test] fn invalid_token_returns_err() { - use super::*; - use rand::RngCore; - let rng = &mut rand::rng(); let mut master_key = [0; 64]; diff --git a/quinn-proto/src/token_memory_cache.rs b/quinn-proto/src/token_memory_cache.rs index 3fce05f8ab..e94254effe 100644 --- a/quinn-proto/src/token_memory_cache.rs +++ b/quinn-proto/src/token_memory_cache.rs @@ -159,7 +159,7 @@ mod tests { use rand_pcg::Pcg32; fn new_rng() -> impl Rng { - Pcg32::from_seed(0xdeadbeefdeadbeefdeadbeefdeadbeefu128.to_le_bytes()) + Pcg32::new(0xdeadbeefdeadbeef, 0xdeadbeefdeadbeef) } #[test] diff --git a/quinn-proto/src/transport_parameters.rs b/quinn-proto/src/transport_parameters.rs index d1ddfbe953..1fe99c3034 100644 --- a/quinn-proto/src/transport_parameters.rs +++ b/quinn-proto/src/transport_parameters.rs @@ -12,7 +12,7 @@ use std::{ }; use bytes::{Buf, BufMut}; -use rand::{Rng as _, RngCore, seq::SliceRandom as _}; +use rand::{Rng, RngExt, seq::SliceRandom as _}; use thiserror::Error; use crate::{ @@ -148,7 +148,7 @@ impl TransportParameters { cid_gen: &dyn ConnectionIdGenerator, initial_src_cid: ConnectionId, server_config: Option<&ServerConfig>, - rng: &mut impl RngCore, + rng: &mut impl Rng, ) -> Self { Self { initial_src_cid: Some(initial_src_cid), @@ -557,7 +557,7 @@ impl ReservedTransportParameter { /// The implementation is inspired by quic-go and quiche: /// 1. /// 2. - fn random(rng: &mut impl RngCore) -> Self { + fn random(rng: &mut impl Rng) -> Self { let id = Self::generate_reserved_id(rng); let payload_len = rng.random_range(0..Self::MAX_PAYLOAD_LEN); @@ -585,7 +585,7 @@ impl ReservedTransportParameter { /// Reserved transport parameter identifiers are used to test compliance with the requirement /// that unknown transport parameters must be ignored by peers. /// See: and - fn generate_reserved_id(rng: &mut impl RngCore) -> VarInt { + fn generate_reserved_id(rng: &mut impl Rng) -> VarInt { let id = { let rand = rng.random_range(0u64..(1 << 62) - 27); let n = rand / 31; @@ -721,6 +721,10 @@ fn decode_cid(len: usize, value: &mut Option, r: &mut impl Buf) -> #[cfg(test)] mod test { + use std::convert::Infallible; + + use rand::TryRng; + use super::*; #[test] @@ -784,21 +788,22 @@ mod test { struct StepRng(u64); - impl RngCore for StepRng { + impl TryRng for StepRng { + type Error = Infallible; + #[inline] - fn next_u32(&mut self) -> u32 { - self.next_u64() as u32 + fn try_next_u32(&mut self) -> Result { + Ok(self.next_u64() as u32) } #[inline] - fn next_u64(&mut self) -> u64 { + fn try_next_u64(&mut self) -> Result { let res = self.0; self.0 = self.0.wrapping_add(1); - res + Ok(res) } - #[inline] - fn fill_bytes(&mut self, dst: &mut [u8]) { + fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> { let mut left = dst; while left.len() >= 8 { let (l, r) = left.split_at_mut(8); @@ -809,6 +814,8 @@ mod test { if n > 0 { left.copy_from_slice(&self.next_u32().to_le_bytes()[..n]); } + + Ok(()) } } diff --git a/quinn/src/tests.rs b/quinn/src/tests.rs index 4e45297dc8..d0c31ffa1d 100755 --- a/quinn/src/tests.rs +++ b/quinn/src/tests.rs @@ -23,7 +23,7 @@ use crate::runtime::TokioRuntime; use crate::{Duration, Instant}; use bytes::Bytes; use proto::{RandomConnectionIdGenerator, crypto::rustls::QuicClientConfig}; -use rand::{RngCore, SeedableRng, rngs::StdRng}; +use rand::{Rng, SeedableRng, rngs::StdRng}; use rustls::{ RootCertStore, pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer}, diff --git a/quinn/tests/many_connections.rs b/quinn/tests/many_connections.rs index acd08cbe8d..a22371e791 100644 --- a/quinn/tests/many_connections.rs +++ b/quinn/tests/many_connections.rs @@ -8,7 +8,7 @@ use std::{ use crc::Crc; use quinn::{ConnectionError, ReadError, StoppedError, TransportConfig, WriteError}; -use rand::{self, RngCore}; +use rand::{self, Rng}; use rustls::pki_types::{CertificateDer, PrivatePkcs8KeyDer}; use tokio::runtime::Builder; From b9b460b1cbcd906c539a4c47ff7fba5be2b97efd Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Wed, 13 May 2026 11:26:37 -0400 Subject: [PATCH 2/2] Switch BBR RNG to PCG --- quinn-proto/Cargo.toml | 2 +- quinn-proto/src/congestion/bbr/mod.rs | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/quinn-proto/Cargo.toml b/quinn-proto/Cargo.toml index 5b7c514f93..c018ef1541 100644 --- a/quinn-proto/Cargo.toml +++ b/quinn-proto/Cargo.toml @@ -50,6 +50,7 @@ lru-slab = { workspace = true } qlog = { workspace = true, optional = true } rustc-hash = { workspace = true } rand = { workspace = true } +rand_pcg = "0.10" ring = { workspace = true, optional = true } rustls = { workspace = true, optional = true } rustls-platform-verifier = { workspace = true, optional = true } @@ -69,7 +70,6 @@ web-time = { workspace = true } [dev-dependencies] assert_matches = { workspace = true } hex-literal = { workspace = true } -rand_pcg = "0.10" rcgen = { workspace = true } tracing-subscriber = { workspace = true } wasm-bindgen-test = { workspace = true } diff --git a/quinn-proto/src/congestion/bbr/mod.rs b/quinn-proto/src/congestion/bbr/mod.rs index 69f91f6797..a8fdca0ae1 100644 --- a/quinn-proto/src/congestion/bbr/mod.rs +++ b/quinn-proto/src/congestion/bbr/mod.rs @@ -2,14 +2,13 @@ use std::any::Any; use std::fmt::Debug; use std::sync::Arc; -use rand::rngs::{StdRng, SysRng}; -use rand::{Rng, RngExt, SeedableRng}; +use rand::{RngExt, SeedableRng}; +use rand_pcg::Pcg32; use crate::congestion::ControllerMetrics; use crate::congestion::bbr::bw_estimation::BandwidthEstimation; use crate::congestion::bbr::min_max::MinMax; use crate::connection::RttEstimator; -use crate::endpoint::NoRandomBytes; use crate::{Duration, Instant}; use super::{BASE_DATAGRAM_SIZE, Controller, ControllerFactory}; @@ -58,14 +57,14 @@ pub struct Bbr { bw_at_last_round: u64, round_wo_bw_gain: u64, ack_aggregation: AckAggregationState, - random_number_generator: StdRng, + random_number_generator: Pcg32, } impl Bbr { /// Construct a state using the given `config` and current time `now` - pub fn new(config: Arc, current_mtu: u16) -> Result { + pub fn new(config: Arc, current_mtu: u16) -> Self { let initial_window = config.initial_window; - Ok(Self { + Self { config, current_mtu: current_mtu as u64, max_bandwidth: BandwidthEstimation::default(), @@ -99,8 +98,8 @@ impl Bbr { bw_at_last_round: 0, round_wo_bw_gain: 0, ack_aggregation: AckAggregationState::default(), - random_number_generator: StdRng::try_from_rng(&mut SysRng)?, - }) + random_number_generator: Pcg32::from_rng(&mut rand::rng()), + } } fn enter_startup_mode(&mut self) {