Skip to content

Commit 28001b4

Browse files
Rollup merge of #129201 - joboet:random_faster_sources, r=joshtriplett
std: implement the `random` feature (alternative version) Implements the ACP rust-lang/libs-team#393. This PR is an alternative version of #129120 that replaces `getentropy` with `CCRandomGenerateBytes` (on macOS) and `arc4random_buf` (other BSDs), since that function is not suited for generating large amounts of data and should only be used to seed other CPRNGs. `CCRandomGenerateBytes`/`arc4random_buf` on the other hand is (on modern platforms) just as secure and uses its own, very strong CPRNG (ChaCha20 on the BSDs, AES on macOS) periodically seeded with `getentropy`.
2 parents 3acf865 + 7136cc4 commit 28001b4

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/shims/unix/macos/foreign_items.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
7878
this.write_pointer(environ, dest)?;
7979
}
8080

81+
// Random data generation
82+
"CCRandomGenerateBytes" => {
83+
let [bytes, count] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
84+
let bytes = this.read_pointer(bytes)?;
85+
let count = this.read_target_usize(count)?;
86+
let success = this.eval_libc_i32("kCCSuccess");
87+
this.gen_random(bytes, count)?;
88+
this.write_int(success, dest)?;
89+
}
90+
8191
// Time related shims
8292
"mach_absolute_time" => {
8393
let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@only-target: apple # This directly tests apple-only functions
2+
3+
fn main() {
4+
let mut bytes = [0u8; 24];
5+
let ret = unsafe { libc::CCRandomGenerateBytes(bytes.as_mut_ptr().cast(), bytes.len()) };
6+
assert_eq!(ret, libc::kCCSuccess);
7+
}

0 commit comments

Comments
 (0)