Skip to content

Commit

Permalink
Auto merge of #60156 - RalfJung:macos-rand, r=oli-obk,alexcrichton
Browse files Browse the repository at this point in the history
use SecRandomCopyBytes on macOS in Miri

This is a hack to fix rust-lang/miri#686: on macOS, rustc will open `/dev/urandom` to initialize a `HashMap`. That's quite hard to emulate properly in Miri without a full-blown implementation of file descriptors.  However, Miri needs an implementation of `SecRandomCopyBytes` anyway to support [getrandom](https://crates.io/crates/getrandom), so using it here should work just as well.

This will only have an effect when libstd is compiled specifically for Miri, but that will generally be the case when people use `cargo miri`.

This is clearly a hack, so I am opening this to start a discussion about whether we are okay with such a hack or not.

Cc @oli-obk
  • Loading branch information
bors committed May 2, 2019
2 parents 92b5e20 + 16ad977 commit 758dc9a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/bootstrap/bin/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ fn main() {
// The flags here should be kept in sync with `add_miri_default_args`
// in miri's `src/lib.rs`.
cmd.arg("-Zalways-encode-mir");
cmd.arg("--cfg=miri");
// These options are preferred by miri, to be able to perform better validation,
// but the bootstrap compiler might not understand them.
if stage != "0" {
Expand Down
5 changes: 4 additions & 1 deletion src/libstd/sys/unix/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn hashmap_random_keys() -> (u64, u64) {

#[cfg(all(unix,
not(target_os = "ios"),
not(all(target_os = "macos", miri)),
not(target_os = "openbsd"),
not(target_os = "freebsd"),
not(target_os = "fuchsia")))]
Expand Down Expand Up @@ -106,7 +107,9 @@ mod imp {
// once per thread in `hashmap_random_keys`. Therefore `SecRandomCopyBytes` is
// only used on iOS where direct access to `/dev/urandom` is blocked by the
// sandbox.
#[cfg(target_os = "ios")]
// HACK: However, we do use this when running in Miri on macOS; intercepting this is much
// easier than intercepting accesses to /dev/urandom.
#[cfg(any(target_os = "ios", all(target_os = "macos", miri)))]
mod imp {
use crate::io;
use crate::ptr;
Expand Down

0 comments on commit 758dc9a

Please sign in to comment.