Skip to content

Commit 382475d

Browse files
committed
Auto merge of rust-lang#3161 - devnexen:freebsd_upd2, r=RalfJung
freebsd adding getentropy interception support
2 parents f86fa09 + b7a98e3 commit 382475d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/tools/miri/ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ case $HOST_TARGET in
108108
MIRI_TEST_TARGET=aarch64-unknown-linux-gnu run_tests
109109
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
110110
MIRI_TEST_TARGET=i686-pc-windows-gnu run_tests
111-
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthreads atomic env/var
111+
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthreads libc-getentropy atomic env/var
112112
MIRI_TEST_TARGET=aarch64-linux-android run_tests_minimal hello integer vec panic/panic
113113
MIRI_TEST_TARGET=wasm32-wasi run_tests_minimal no_std integer strings wasm
114114
MIRI_TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std integer strings wasm

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
4747
this.read_scalar(len)?,
4848
)?;
4949
}
50+
"getentropy" => {
51+
let [buf, bufsize] =
52+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
53+
let buf = this.read_pointer(buf)?;
54+
let bufsize = this.read_target_usize(bufsize)?;
55+
56+
// getentropy sets errno to EIO when the buffer size exceeds 256 bytes.
57+
// https://man.freebsd.org/cgi/man.cgi?query=getentropy&sektion=3&format=html
58+
if bufsize > 256 {
59+
let err = this.eval_libc("EIO");
60+
this.set_last_error(err)?;
61+
this.write_scalar(Scalar::from_i32(-1), dest)?
62+
} else {
63+
this.gen_random(buf, bufsize)?;
64+
this.write_scalar(Scalar::from_i32(0), dest)?;
65+
}
66+
}
5067

5168
// errno
5269
"__error" => {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@only-target-freebsd
2+
3+
fn main() {
4+
let mut buf1 = [0u8; 256];
5+
let mut buf2 = [0u8; 257];
6+
unsafe {
7+
assert_eq!(libc::getentropy(buf1.as_mut_ptr() as *mut libc::c_void, buf1.len()), 0);
8+
assert_eq!(libc::getentropy(buf2.as_mut_ptr() as *mut libc::c_void, buf2.len()), -1);
9+
assert_eq!(std::io::Error::last_os_error().raw_os_error().unwrap(), libc::EIO);
10+
}
11+
}

0 commit comments

Comments
 (0)