Skip to content

Commit 9781e23

Browse files
committed
implements arc4random_buf shim for freebsd/solarish platforms.
close #3914
1 parent 36ae133 commit 9781e23

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Diff for: 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:

Diff for: tests/pass-dep/libc/libc-random.rs

+14
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,15 @@ 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+
extern "C" {
70+
fn arc4random_buf(buf: *mut libc::c_void, size: libc::size_t);
71+
}
72+
let mut buf = [0u8; 5];
73+
unsafe {
74+
arc4random_buf(buf.as_mut_ptr(), buf.len());
75+
assert!(buf != [0u8; 5]);
76+
}
77+
}

0 commit comments

Comments
 (0)