Skip to content

Commit 758dc9a

Browse files
committed
Auto merge of #60156 - RalfJung:macos-rand, r=oli-obk,alexcrichton
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
2 parents 92b5e20 + 16ad977 commit 758dc9a

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/bootstrap/bin/rustc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ fn main() {
262262
// The flags here should be kept in sync with `add_miri_default_args`
263263
// in miri's `src/lib.rs`.
264264
cmd.arg("-Zalways-encode-mir");
265+
cmd.arg("--cfg=miri");
265266
// These options are preferred by miri, to be able to perform better validation,
266267
// but the bootstrap compiler might not understand them.
267268
if stage != "0" {

src/libstd/sys/unix/rand.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn hashmap_random_keys() -> (u64, u64) {
1313

1414
#[cfg(all(unix,
1515
not(target_os = "ios"),
16+
not(all(target_os = "macos", miri)),
1617
not(target_os = "openbsd"),
1718
not(target_os = "freebsd"),
1819
not(target_os = "fuchsia")))]
@@ -106,7 +107,9 @@ mod imp {
106107
// once per thread in `hashmap_random_keys`. Therefore `SecRandomCopyBytes` is
107108
// only used on iOS where direct access to `/dev/urandom` is blocked by the
108109
// sandbox.
109-
#[cfg(target_os = "ios")]
110+
// HACK: However, we do use this when running in Miri on macOS; intercepting this is much
111+
// easier than intercepting accesses to /dev/urandom.
112+
#[cfg(any(target_os = "ios", all(target_os = "macos", miri)))]
110113
mod imp {
111114
use crate::io;
112115
use crate::ptr;

0 commit comments

Comments
 (0)