about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Carlier <devnexen@gmail.com>2024-09-26 19:32:36 +0100
committerDavid Carlier <devnexen@gmail.com>2024-09-28 20:02:54 +0100
commitf14f72eb34243ea13b0c6a1750a999352abe47fe (patch)
treec1ecfa5474271f42b4ad124d96b96b34ba5279f1
parentc361d52a9b8af2b975512a9569c802cc587bad87 (diff)
downloadrust-f14f72eb34243ea13b0c6a1750a999352abe47fe.tar.gz
rust-f14f72eb34243ea13b0c6a1750a999352abe47fe.zip
implements arc4random_buf shim for freebsd/solarish platforms.
close #3914
-rw-r--r--src/tools/miri/src/shims/unix/foreign_items.rs14
-rw-r--r--src/tools/miri/tests/pass-dep/libc/libc-random.rs13
2 files changed, 27 insertions, 0 deletions
diff --git a/src/tools/miri/src/shims/unix/foreign_items.rs b/src/tools/miri/src/shims/unix/foreign_items.rs
index c06ce57e610..470f13442f9 100644
--- a/src/tools/miri/src/shims/unix/foreign_items.rs
+++ b/src/tools/miri/src/shims/unix/foreign_items.rs
@@ -792,6 +792,20 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
                 this.gen_random(ptr, len)?;
                 this.write_scalar(Scalar::from_target_usize(len, this), dest)?;
             }
+            "arc4random_buf" => {
+                // This function is non-standard but exists with the same signature and
+                // same behavior (eg never fails) on FreeBSD and Solaris/Illumos.
+                if !matches!(&*this.tcx.sess.target.os, "freebsd" | "illumos" | "solaris") {
+                    throw_unsup_format!(
+                        "`arc4random_buf` is not supported on {}",
+                        this.tcx.sess.target.os
+                    );
+                }
+                let [ptr, len] = this.check_shim(abi, Abi::C { unwind: false}, link_name, args)?;
+                let ptr = this.read_pointer(ptr)?;
+                let len = this.read_target_usize(len)?;
+                this.gen_random(ptr, len)?;
+            }
             "_Unwind_RaiseException" => {
                 // This is not formally part of POSIX, but it is very wide-spread on POSIX systems.
                 // It was originally specified as part of the Itanium C++ ABI:
diff --git a/src/tools/miri/tests/pass-dep/libc/libc-random.rs b/src/tools/miri/tests/pass-dep/libc/libc-random.rs
index e951603639c..8f4398cbd8f 100644
--- a/src/tools/miri/tests/pass-dep/libc/libc-random.rs
+++ b/src/tools/miri/tests/pass-dep/libc/libc-random.rs
@@ -6,6 +6,8 @@ fn main() {
     test_getentropy();
     #[cfg(not(target_os = "macos"))]
     test_getrandom();
+    #[cfg(any(target_os = "freebsd", target_os = "illumos", target_os = "solaris"))]
+    test_arc4random_buf();
 }
 
 fn test_getentropy() {
@@ -61,3 +63,14 @@ fn test_getrandom() {
         );
     }
 }
+
+#[cfg(any(target_os = "freebsd", target_os = "illumos", target_os = "solaris"))]
+fn test_arc4random_buf() {
+    // FIXME: Use declaration from libc once <https://github.com/rust-lang/libc/pull/3944> lands.
+    extern "C" {
+        fn arc4random_buf(buf: *mut libc::c_void, size: libc::size_t);
+    }
+    let mut buf = [0u8; 5];
+    unsafe { arc4random_buf(buf.as_mut_ptr() as _, buf.len()) };
+    assert!(buf != [0u8; 5]);
+}