Skip to content

Commit

Permalink
fix(madsim): fix determinism for getrandom (#202)
Browse files Browse the repository at this point in the history
* fix determinism for getrandom

Signed-off-by: Runji Wang <[email protected]>

* bump version

Signed-off-by: Runji Wang <[email protected]>

* fix in another way

Signed-off-by: Runji Wang <[email protected]>

---------

Signed-off-by: Runji Wang <[email protected]>
  • Loading branch information
wangrunji0408 committed Apr 7, 2024
1 parent 4ebdbcd commit e9aa251
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## madsim [0.2.27] - 2024-04-07

### Fixed

- Fix the problem that `getrandom` returns different values in multiple runs with the same seed.

## rdkafka [0.3.4] - 2024-03-22

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ members = [
"tonic-example",
]
resolver = "2"

[patch.crates-io]
getrandom = { git = "https://github.com/madsim-rs/getrandom.git", rev = "6c9d9e9" }
3 changes: 2 additions & 1 deletion madsim/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "madsim"
version = "0.2.26"
version = "0.2.27"
edition = "2021"
authors = ["Runji Wang <[email protected]>"]
description = "Deterministic Simulator for distributed systems."
Expand Down Expand Up @@ -63,6 +63,7 @@ tokio-util = { version = "0.7", features = ["codec"] }
criterion = "0.5"
structopt = "0.3"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "io-util"] }
getrandom = "=0.2.13"

[[bench]]
name = "rpc"
Expand Down
35 changes: 32 additions & 3 deletions madsim/src/sim/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@ unsafe extern "C" fn getrandom(mut buf: *mut u8, mut buflen: usize, _flags: u32)
buf = buf.add(std::mem::size_of::<u64>());
buflen -= std::mem::size_of::<u64>();
}
let val = rand.with(|rng| rng.gen::<u64>().to_ne_bytes());
core::ptr::copy(val.as_ptr(), buf, buflen);
// note: do not modify state if buflen == 0
if buflen != 0 {
let val = rand.with(|rng| rng.gen::<u64>().to_ne_bytes());
core::ptr::copy(val.as_ptr(), buf, buflen);
}
return len as _;
}
#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -265,7 +268,7 @@ unsafe extern "C" fn getentropy(buf: *mut u8, buflen: usize) -> i32 {
#[cfg(test)]
mod tests {
use crate::runtime::Runtime;
use std::collections::{BTreeSet, HashMap};
use std::collections::{BTreeSet, HashMap, HashSet};

#[test]
#[cfg_attr(target_os = "linux", ignore)]
Expand Down Expand Up @@ -305,4 +308,30 @@ mod tests {
}
assert_eq!(seqs.len(), 3, "hashmap is not deterministic");
}

// https://github.com/madsim-rs/madsim/issues/201
#[test]
fn getrandom_should_be_deterministic() {
let rnd_fn = || async {
let mut dst = [0];
getrandom::getrandom(&mut dst).unwrap();
dst
};
let builder = crate::runtime::Builder::from_env();
let seed = builder.seed;
let set = (0..10)
.map(|_| {
crate::runtime::Builder {
seed,
count: 1,
jobs: 1,
config: crate::Config::default(),
time_limit: None,
check: false,
}
.run(rnd_fn)
})
.collect::<HashSet<_>>();
assert_eq!(set.len(), 1);
}
}

0 comments on commit e9aa251

Please sign in to comment.