Skip to content

Commit 631d5fa

Browse files
committed
Auto merge of #914 - christianpoveda:use-host-rng, r=RalfJung
Use host's rng when communication is enabled This uses the host's randomness when the communication enabled flag is used. I am not sure about the error handling. I was thinking about fallbacking to `rand` if `getrandom` fails and also print something so the user knows miri is not using the host's rng because it failed. Let me know what you think. Related issue: #800. r? @RalfJung @oli-obk
2 parents d77fe6c + f53b5b0 commit 631d5fa

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ byteorder = { version = "1.1", features = ["i128"]}
3535
cargo_metadata = { version = "0.8", optional = true }
3636
directories = { version = "2.0", optional = true }
3737
rustc_version = { version = "0.2.3", optional = true }
38+
getrandom = "0.1.10"
3839
env_logger = "0.6"
3940
log = "0.4"
4041
shell-escape = "0.1.4"

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ Several `-Z` flags are relevant for Miri:
158158
will miss bugs in your program. However, this can also help to make Miri run
159159
faster.
160160
* `-Zmiri-enable-communication` enables communication between the host
161-
environment and Miri, i.e., all the host environment variables are available
162-
during Miri runtime.
161+
environment and Miri, i.e., Miri uses the host's random number generator and
162+
all the host environment variables are available during runtime.
163163
* `-Zmir-opt-level` controls how many MIR optimizations are performed. Miri
164164
overrides the default to be `0`; be advised that using any higher level can
165165
make Miri miss bugs in your program because they got optimized away.

src/helpers.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9797
Align::from_bytes(1).unwrap()
9898
)?.expect("we already checked for size 0");
9999

100-
let rng = this.memory_mut().extra.rng.get_mut();
101100
let mut data = vec![0; len];
102-
rng.fill_bytes(&mut data);
101+
102+
if this.machine.communicate {
103+
// Fill the buffer using the host's rng.
104+
getrandom::getrandom(&mut data)
105+
.map_err(|err| err_unsup_format!("getrandom failed: {}", err))?;
106+
}
107+
else {
108+
let rng = this.memory_mut().extra.rng.get_mut();
109+
rng.fill_bytes(&mut data);
110+
}
103111

104112
let tcx = &{this.tcx.tcx};
105113
this.memory_mut().get_mut(ptr.alloc_id)?.write_bytes(tcx, ptr, &data)

src/machine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ pub struct Evaluator<'tcx> {
9393
/// TLS state.
9494
pub(crate) tls: TlsData<'tcx>,
9595

96-
/// If enabled, the `env_vars` field is populated with the host env vars during initialization.
96+
/// If enabled, the `env_vars` field is populated with the host env vars during initialization
97+
/// and random number generation is delegated to the host.
9798
pub(crate) communicate: bool,
9899
}
99100

0 commit comments

Comments
 (0)