Skip to content

Commit

Permalink
freebsd adding getrandom interception.
Browse files Browse the repository at this point in the history
note that os support was added in same time as getentropy.
  • Loading branch information
devnexen committed Nov 15, 2023
1 parent 4c65187 commit 2ef304c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ case $HOST_TARGET in
MIRI_TEST_TARGET=aarch64-unknown-linux-gnu run_tests
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
MIRI_TEST_TARGET=i686-pc-windows-gnu run_tests
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthreads libc-getentropy atomic env/var
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthreads libc-getentropy libc-getrandom atomic env/var
MIRI_TEST_TARGET=aarch64-linux-android run_tests_minimal hello integer vec panic/panic
MIRI_TEST_TARGET=wasm32-wasi run_tests_minimal no_std integer strings wasm
MIRI_TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std integer strings wasm
Expand Down
15 changes: 15 additions & 0 deletions src/shims/unix/freebsd/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
this.read_scalar(len)?,
)?;
}
"getrandom" => {
let [ptr, len, flags] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let len = this.read_target_usize(len)?;
let _flags = this.read_scalar(flags)?.to_i32()?;
// flags on freebsd does not really matter
// in practice, GRND_RANDOM does not particularly draw from /dev/random
// since it is the same as to /dev/urandom.
// GRND_INSECURE is only an alias of GRND_NONBLOCK, which
// does not affect the RNG.
// https://man.freebsd.org/cgi/man.cgi?query=getrandom&sektion=2&n=1
this.gen_random(ptr, len)?;
this.write_scalar(Scalar::from_target_usize(len, this), dest)?;
}

// errno
"__error" => {
Expand Down
5 changes: 4 additions & 1 deletion tests/pass-dep/shims/libc-getrandom.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//@only-target-linux
//@ignore-target-windows: no libc
//@ignore-target-apple: no getrandom

use std::ptr;

fn main() {
let mut buf = [0u8; 5];
unsafe {
#[cfg(target_os = "linux")]
assert_eq!(
libc::syscall(
libc::SYS_getrandom,
Expand All @@ -14,6 +16,7 @@ fn main() {
),
0,
);
#[cfg(target_os = "linux")]
assert_eq!(
libc::syscall(
libc::SYS_getrandom,
Expand Down

0 comments on commit 2ef304c

Please sign in to comment.