Skip to content

Commit f14f72e

Browse files
committed
implements arc4random_buf shim for freebsd/solarish platforms.
close rust-lang#3914
1 parent c361d52 commit f14f72e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/tools/miri/src/shims/unix/foreign_items.rs

+14
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,20 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
792792
this.gen_random(ptr, len)?;
793793
this.write_scalar(Scalar::from_target_usize(len, this), dest)?;
794794
}
795+
"arc4random_buf" => {
796+
// This function is non-standard but exists with the same signature and
797+
// same behavior (eg never fails) on FreeBSD and Solaris/Illumos.
798+
if !matches!(&*this.tcx.sess.target.os, "freebsd" | "illumos" | "solaris") {
799+
throw_unsup_format!(
800+
"`arc4random_buf` is not supported on {}",
801+
this.tcx.sess.target.os
802+
);
803+
}
804+
let [ptr, len] = this.check_shim(abi, Abi::C { unwind: false}, link_name, args)?;
805+
let ptr = this.read_pointer(ptr)?;
806+
let len = this.read_target_usize(len)?;
807+
this.gen_random(ptr, len)?;
808+
}
795809
"_Unwind_RaiseException" => {
796810
// This is not formally part of POSIX, but it is very wide-spread on POSIX systems.
797811
// It was originally specified as part of the Itanium C++ ABI:

src/tools/miri/tests/pass-dep/libc/libc-random.rs

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ fn main() {
66
test_getentropy();
77
#[cfg(not(target_os = "macos"))]
88
test_getrandom();
9+
#[cfg(any(target_os = "freebsd", target_os = "illumos", target_os = "solaris"))]
10+
test_arc4random_buf();
911
}
1012

1113
fn test_getentropy() {
@@ -61,3 +63,14 @@ fn test_getrandom() {
6163
);
6264
}
6365
}
66+
67+
#[cfg(any(target_os = "freebsd", target_os = "illumos", target_os = "solaris"))]
68+
fn test_arc4random_buf() {
69+
// FIXME: Use declaration from libc once <https://github.com/rust-lang/libc/pull/3944> lands.
70+
extern "C" {
71+
fn arc4random_buf(buf: *mut libc::c_void, size: libc::size_t);
72+
}
73+
let mut buf = [0u8; 5];
74+
unsafe { arc4random_buf(buf.as_mut_ptr() as _, buf.len()) };
75+
assert!(buf != [0u8; 5]);
76+
}

0 commit comments

Comments
 (0)