Skip to content

Use host's rng when communication is enabled #914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ byteorder = { version = "1.1", features = ["i128"]}
cargo_metadata = { version = "0.8", optional = true }
directories = { version = "2.0", optional = true }
rustc_version = { version = "0.2.3", optional = true }
getrandom = "0.1.10"
env_logger = "0.6"
log = "0.4"
shell-escape = "0.1.4"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ Several `-Z` flags are relevant for Miri:
will miss bugs in your program. However, this can also help to make Miri run
faster.
* `-Zmiri-enable-communication` enables communication between the host
environment and Miri, i.e., all the host environment variables are available
during Miri runtime.
environment and Miri, i.e., Miri uses the host's random number generator and
all the host environment variables are available during runtime.
* `-Zmir-opt-level` controls how many MIR optimizations are performed. Miri
overrides the default to be `0`; be advised that using any higher level can
make Miri miss bugs in your program because they got optimized away.
Expand Down
12 changes: 10 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
Align::from_bytes(1).unwrap()
)?.expect("we already checked for size 0");

let rng = this.memory_mut().extra.rng.get_mut();
let mut data = vec![0; len];
rng.fill_bytes(&mut data);

if this.machine.communicate {
// Fill the buffer using the host's rng.
getrandom::getrandom(&mut data)
.map_err(|err| err_unsup_format!("getrandom failed: {}", err))?;
}
else {
let rng = this.memory_mut().extra.rng.get_mut();
rng.fill_bytes(&mut data);
}

let tcx = &{this.tcx.tcx};
this.memory_mut().get_mut(ptr.alloc_id)?.write_bytes(tcx, ptr, &data)
Expand Down
3 changes: 2 additions & 1 deletion src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ pub struct Evaluator<'tcx> {
/// TLS state.
pub(crate) tls: TlsData<'tcx>,

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

Expand Down