From b44fd97af6999e032f71e9e7c265b43163cb4052 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 19 Aug 2019 10:43:09 -0500 Subject: [PATCH 1/3] Use host's rng when communication is enabled --- Cargo.toml | 1 + src/helpers.rs | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 644a3476a6dd..1ab0580cc631 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/helpers.rs b/src/helpers.rs index c0e1ec2cd75b..d134a19f4c23 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -97,9 +97,18 @@ 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| { + InterpError::Unsupported(UnsupportedOpInfo::Unsupported(err.to_string())) + })?; + } + 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) From 98129631b5a71c9a43724903499b11170b518e7a Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 20 Aug 2019 10:47:38 -0500 Subject: [PATCH 2/3] Use err_unsup_format instead --- src/helpers.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index d134a19f4c23..330d6bc996b7 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -101,9 +101,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx if this.machine.communicate { // Fill the buffer using the host's rng. - getrandom::getrandom(&mut data).map_err(|err| { - InterpError::Unsupported(UnsupportedOpInfo::Unsupported(err.to_string())) - })?; + getrandom::getrandom(&mut data) + .map_err(|err| err_unsup_format!("getrandom failed: {}", err))?; } else { let rng = this.memory_mut().extra.rng.get_mut(); From f53b5b0fb4cc9a716eb27929c3829f096e90b6eb Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 20 Aug 2019 10:47:57 -0500 Subject: [PATCH 3/3] Update -Zmiri-enable-communication docs --- README.md | 4 ++-- src/machine.rs | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f34569d865a8..502c562d1357 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/machine.rs b/src/machine.rs index b4aac147f94f..b11ecfa44135 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -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, }