Skip to content

Commit dd572c4

Browse files
committed
Auto merge of rust-lang#3918 - devnexen:solaris_arc4random_buf, r=RalfJung
implements arc4random_buf shim for freebsd/solarish platforms. close rust-lang#3914
2 parents 5790eb9 + 7cb5a79 commit dd572c4

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/tools/miri/ci/ci.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ case $HOST_TARGET in
150150
# Partially supported targets (tier 2)
151151
BASIC="empty_main integer heap_alloc libc-mem vec string btreemap" # ensures we have the basics: pre-main code, system allocator
152152
UNIX="hello panic/panic panic/unwind concurrency/simple atomic libc-mem libc-misc libc-random env num_cpus" # the things that are very similar across all Unixes, and hence easily supported there
153-
TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal $BASIC $UNIX threadname pthread time fs
154-
TEST_TARGET=i686-unknown-freebsd run_tests_minimal $BASIC $UNIX threadname pthread time fs
155-
TEST_TARGET=x86_64-unknown-illumos run_tests_minimal $BASIC $UNIX thread sync available-parallelism time tls
156-
TEST_TARGET=x86_64-pc-solaris run_tests_minimal $BASIC $UNIX thread sync available-parallelism time tls
153+
TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal $BASIC $UNIX hashmap random threadname pthread time fs
154+
TEST_TARGET=i686-unknown-freebsd run_tests_minimal $BASIC $UNIX hashmap random threadname pthread time fs
155+
TEST_TARGET=x86_64-unknown-illumos run_tests_minimal $BASIC $UNIX hashmap thread sync available-parallelism time tls
156+
TEST_TARGET=x86_64-pc-solaris run_tests_minimal $BASIC $UNIX hashmap thread sync available-parallelism time tls
157157
TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX hashmap pthread time --skip threadname
158158
TEST_TARGET=wasm32-wasip2 run_tests_minimal $BASIC wasm
159159
TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std empty_main wasm # this target doesn't really have std

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)