diff --git a/Cargo.lock b/Cargo.lock index fec66d3c7..d1eccff87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,13 +166,14 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.17" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" dependencies = [ "cfg-if", "libc", - "wasi", + "r-efi", + "wasip2", ] [[package]] @@ -321,22 +322,27 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + [[package]] name = "rand" -version = "0.8.6" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ca0ecfa931c29007047d1bc58e623ab12e5590e8c7cc53200d5202b69266d8a" +checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea" dependencies = [ - "libc", "rand_chacha", "rand_core", ] [[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", @@ -344,9 +350,9 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.6.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" dependencies = [ "getrandom", ] @@ -659,6 +665,15 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "wasip2" +version = "1.0.3+wasi-0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20064672db26d7cdc89c7798c48a0fdfac8213434a1186e5ef29fd560ae223d6" +dependencies = [ + "wit-bindgen", +] + [[package]] name = "windows-link" version = "0.2.1" @@ -683,6 +698,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + [[package]] name = "zerocopy" version = "0.8.48" diff --git a/Cargo.toml b/Cargo.toml index a7a531613..44b477734 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ log = { version = "0.4.27", default-features = false } pin-project-lite = "0.2.13" toml = ">=0.5.0, <0.9.0" tokio = "1.33" -rand = { version = "0.8.5", default-features = false } +rand = { version = "0.9.4", default-features = false } serde = { version = "1.0.192", features = ["derive"] } serde_json = { version = "1.0.111" } serde_test = { version = "1.0.176" } diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 9e011cfcd..717ba33fc 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -172,18 +172,18 @@ checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" [[package]] name = "rand" -version = "0.8.6" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ca0ecfa931c29007047d1bc58e623ab12e5590e8c7cc53200d5202b69266d8a" +checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea" dependencies = [ "rand_core", ] [[package]] name = "rand_core" -version = "0.6.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" [[package]] name = "serde" diff --git a/statime-linux/Cargo.toml b/statime-linux/Cargo.toml index 6c72a83c5..10ef347cc 100644 --- a/statime-linux/Cargo.toml +++ b/statime-linux/Cargo.toml @@ -33,7 +33,7 @@ log = { workspace = true, default-features = true } pin-project-lite.workspace = true toml.workspace = true tokio = { workspace = true, features = ["net", "rt-multi-thread", "time", "macros", "sync", "io-util"] } -rand = { workspace = true, default-features = false, features = ["std", "std_rng"] } +rand = { workspace = true, default-features = false, features = ["std", "std_rng", "os_rng"] } serde.workspace = true serde_json.workspace = true diff --git a/statime-linux/src/main.rs b/statime-linux/src/main.rs index 496fd31ad..351cc1cd0 100644 --- a/statime-linux/src/main.rs +++ b/statime-linux/src/main.rs @@ -368,7 +368,7 @@ async fn actual_main() { HardwareClock::None => add_sw_clock(&mut clock_port_map), }; - let rng = StdRng::from_entropy(); + let rng = StdRng::from_os_rng(); let port = instance.add_port( port_config.into(), KalmanConfiguration::default(), diff --git a/statime-stm32/Cargo.lock b/statime-stm32/Cargo.lock index 418d416d5..1dda4f166 100644 --- a/statime-stm32/Cargo.lock +++ b/statime-stm32/Cargo.lock @@ -716,11 +716,11 @@ dependencies = [ [[package]] name = "rand" -version = "0.8.6" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ca0ecfa931c29007047d1bc58e623ab12e5590e8c7cc53200d5202b69266d8a" +checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea" dependencies = [ - "rand_core", + "rand_core 0.9.5", ] [[package]] @@ -729,6 +729,12 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" + [[package]] name = "regex-automata" version = "0.4.14" @@ -1041,7 +1047,7 @@ dependencies = [ "fugit-timer", "micromath", "nb 1.1.0", - "rand_core", + "rand_core 0.6.4", "stm32-fmc", "stm32f7", "time", diff --git a/statime/src/config/port.rs b/statime/src/config/port.rs index db264b790..7bb015754 100644 --- a/statime/src/config/port.rs +++ b/statime/src/config/port.rs @@ -102,7 +102,7 @@ impl PortConfig { /// For more information see *IEEE1588-2019 section 9.2.6.12* pub fn announce_duration(&self, rng: &mut impl Rng) -> core::time::Duration { // add some randomness so that not all timers expire at the same time - let factor = 1.0 + rng.sample::(rand::distributions::Open01); + let factor = 1.0 + rng.sample::(rand::distr::Open01); let duration = self.announce_interval.as_core_duration(); duration.mul_f64(factor * self.announce_receipt_timeout as u32 as f64) diff --git a/statime/src/port/mod.rs b/statime/src/port/mod.rs index df901cfbc..4cee1043d 100644 --- a/statime/src/port/mod.rs +++ b/statime/src/port/mod.rs @@ -118,7 +118,6 @@ pub(crate) mod state; /// # } /// # } /// # let (instance_config, time_properties_ds) = unimplemented!(); -/// use rand::thread_rng; /// use statime::config::{AcceptAnyMaster, DelayMechanism, PortConfig, PtpMinorVersion}; /// use statime::filters::BasicFilter; /// use statime::PtpInstance; @@ -140,7 +139,13 @@ pub(crate) mod state; /// }; /// let filter_config = 1.0; /// let clock = system::Clock {}; -/// let rng = thread_rng(); +/// # struct MockRng(u64); +/// # impl rand::RngCore for MockRng { +/// # fn next_u32(&mut self) -> u32 { self.next_u64() as u32 } +/// # fn next_u64(&mut self) -> u64 { self.0 = self.0.wrapping_add(1); self.0 } +/// # fn fill_bytes(&mut self, dest: &mut [u8]) { for chunk in dest.chunks_mut(8) { let b = self.next_u64().to_le_bytes(); chunk.copy_from_slice(&b[..chunk.len()]); } } +/// # } +/// let rng = MockRng(0); /// /// let port_in_bmca = instance.add_port(port_config, filter_config, clock, rng); /// @@ -722,6 +727,24 @@ mod tests { }; // General test infra + pub(super) struct MockRng(u64); + + impl rand::RngCore for MockRng { + fn next_u32(&mut self) -> u32 { + self.next_u64() as u32 + } + fn next_u64(&mut self) -> u64 { + self.0 = self.0.wrapping_add(1); + self.0 + } + fn fill_bytes(&mut self, dest: &mut [u8]) { + for chunk in dest.chunks_mut(8) { + let bytes = self.next_u64().to_le_bytes(); + chunk.copy_from_slice(&bytes[..chunk.len()]); + } + } + } + pub(super) struct TestClock; impl Clock for TestClock { @@ -749,7 +772,7 @@ mod tests { pub(super) fn setup_test_port( state: &RefCell, - ) -> Port<'_, Running, AcceptAnyMaster, rand::rngs::mock::StepRng, TestClock, BasicFilter> { + ) -> Port<'_, Running, AcceptAnyMaster, MockRng, TestClock, BasicFilter> { let port = Port::<_, _, _, _, BasicFilter>::new( state, PortConfig { @@ -767,7 +790,7 @@ mod tests { 0.25, TestClock, Default::default(), - rand::rngs::mock::StepRng::new(2, 1), + MockRng(0), ); let (port, _) = port.end_bmca(); @@ -777,7 +800,7 @@ mod tests { pub(super) fn setup_test_port_custom_identity( state: &RefCell, port_identity: PortIdentity, - ) -> Port<'_, Running, AcceptAnyMaster, rand::rngs::mock::StepRng, TestClock, BasicFilter> { + ) -> Port<'_, Running, AcceptAnyMaster, MockRng, TestClock, BasicFilter> { let port = Port::<_, _, _, _, BasicFilter>::new( state, PortConfig { @@ -795,7 +818,7 @@ mod tests { 0.25, TestClock, port_identity, - rand::rngs::mock::StepRng::new(2, 1), + MockRng(0), ); let (port, _) = port.end_bmca(); @@ -805,7 +828,7 @@ mod tests { pub(super) fn setup_test_port_custom_filter( state: &RefCell, filter_config: F::Config, - ) -> Port<'_, Running, AcceptAnyMaster, rand::rngs::mock::StepRng, TestClock, F> { + ) -> Port<'_, Running, AcceptAnyMaster, MockRng, TestClock, F> { let port = Port::<_, _, _, _, F>::new( state, PortConfig { @@ -823,7 +846,7 @@ mod tests { filter_config, TestClock, Default::default(), - rand::rngs::mock::StepRng::new(2, 1), + MockRng(0), ); let (port, _) = port.end_bmca(); diff --git a/statime/src/port/slave.rs b/statime/src/port/slave.rs index 01db6cab1..e9b4ea43f 100644 --- a/statime/src/port/slave.rs +++ b/statime/src/port/slave.rs @@ -495,7 +495,7 @@ impl Port<'_, Running, response_recv_time: None, }; - let random = self.rng.sample::(rand::distributions::Open01); + let random = self.rng.sample::(rand::distr::Open01); let factor = random * 2.0f64; let duration = log_min_pdelay_req_interval .as_core_duration() @@ -545,7 +545,7 @@ impl Port<'_, Running, recv_time: None, }; - let random = self.rng.sample::(rand::distributions::Open01); + let random = self.rng.sample::(rand::distr::Open01); let factor = random * 2.0f64; let duration = log_min_delay_req_interval .as_core_duration() diff --git a/statime/src/ptp_instance.rs b/statime/src/ptp_instance.rs index aa01af4d8..dc45c2d7c 100644 --- a/statime/src/ptp_instance.rs +++ b/statime/src/ptp_instance.rs @@ -55,7 +55,13 @@ use crate::{ /// # let port_config: statime::config::PortConfig = unimplemented!(); /// # let filter_config = unimplemented!(); /// # let clock: MockClock = unimplemented!(); -/// # let rng: rand::rngs::mock::StepRng = unimplemented!(); +/// # struct MockRng(u64); +/// # impl rand::RngCore for MockRng { +/// # fn next_u32(&mut self) -> u32 { self.next_u64() as u32 } +/// # fn next_u64(&mut self) -> u64 { self.0 = self.0.wrapping_add(1); self.0 } +/// # fn fill_bytes(&mut self, dest: &mut [u8]) { for chunk in dest.chunks_mut(8) { let b = self.next_u64().to_le_bytes(); chunk.copy_from_slice(&b[..chunk.len()]); } } +/// # } +/// # let rng: MockRng = unimplemented!(); /// # /// use statime::PtpInstance; /// use statime::config::{AcceptAnyMaster, ClockIdentity, ClockQuality, InstanceConfig, TimePropertiesDS, TimeSource};