Skip to content

Commit be6f27b

Browse files
committed
freebsd interceptions update proposal
1 parent fe0b970 commit be6f27b

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
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 atomic env/var
111+
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthreads 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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,23 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
2929
"pthread_set_name_np" => {
3030
let [thread, name] =
3131
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
32-
let max_len = usize::MAX; // freebsd does not seem to have a limit.
33-
let res = this.pthread_setname_np(
32+
let max_len = usize::MAX; // FreeBSD does not seem to have a limit.
33+
// FreeBSD's pthread_set_name_np does not return anything.
34+
this.pthread_setname_np(
3435
this.read_scalar(thread)?,
3536
this.read_scalar(name)?,
3637
max_len,
3738
)?;
38-
this.write_scalar(res, dest)?;
39+
}
40+
"pthread_get_name_np" => {
41+
let [thread, name, len] =
42+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
43+
// FreeBSD's pthread_get_name_np does not return anything.
44+
this.pthread_getname_np(
45+
this.read_scalar(thread)?,
46+
this.read_scalar(name)?,
47+
this.read_scalar(len)?,
48+
)?;
3949
}
4050

4151
// errno

src/tools/miri/tests/pass-dep/shims/pthreads.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
//@ignore-target-windows: No libc on Windows
2-
use std::ffi::{CStr, CString};
2+
use std::ffi::CStr;
3+
#[cfg(not(target_os = "freebsd"))]
4+
use std::ffi::CString;
35
use std::thread;
46

57
fn main() {
8+
#[cfg(not(target_os = "freebsd"))]
69
test_mutex_libc_init_recursive();
10+
#[cfg(not(target_os = "freebsd"))]
711
test_mutex_libc_init_normal();
12+
#[cfg(not(target_os = "freebsd"))]
813
test_mutex_libc_init_errorcheck();
14+
#[cfg(not(target_os = "freebsd"))]
915
test_rwlock_libc_static_initializer();
16+
1017
test_named_thread_truncation();
1118

1219
#[cfg(target_os = "linux")]
1320
test_mutex_libc_static_initializer_recursive();
1421
}
1522

23+
#[cfg(not(target_os = "freebsd"))]
1624
fn test_mutex_libc_init_recursive() {
1725
unsafe {
1826
let mut attr: libc::pthread_mutexattr_t = std::mem::zeroed();
@@ -37,6 +45,7 @@ fn test_mutex_libc_init_recursive() {
3745
}
3846
}
3947

48+
#[cfg(not(target_os = "freebsd"))]
4049
fn test_mutex_libc_init_normal() {
4150
unsafe {
4251
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
@@ -59,6 +68,7 @@ fn test_mutex_libc_init_normal() {
5968
}
6069
}
6170

71+
#[cfg(not(target_os = "freebsd"))]
6272
fn test_mutex_libc_init_errorcheck() {
6373
unsafe {
6474
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
@@ -104,6 +114,7 @@ fn test_mutex_libc_static_initializer_recursive() {
104114
// Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
105115
// need to go a layer deeper and test the behavior of the libc functions, because
106116
// std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
117+
#[cfg(not(target_os = "freebsd"))]
107118
fn test_rwlock_libc_static_initializer() {
108119
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
109120
unsafe {
@@ -137,6 +148,14 @@ fn test_named_thread_truncation() {
137148
fn set_thread_name(name: &CStr) -> i32 {
138149
#[cfg(target_os = "linux")]
139150
return unsafe { libc::pthread_setname_np(libc::pthread_self(), name.as_ptr().cast()) };
151+
#[cfg(target_os = "freebsd")]
152+
unsafe {
153+
// pthread_set_name_np does not return anything only it can fail if the
154+
// thread id is invalid
155+
libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast())
156+
};
157+
#[cfg(target_os = "freebsd")]
158+
return 0;
140159
#[cfg(target_os = "macos")]
141160
return unsafe { libc::pthread_setname_np(name.as_ptr().cast()) };
142161
}
@@ -147,16 +166,25 @@ fn test_named_thread_truncation() {
147166

148167
// But the system is limited -- make sure we successfully set a truncation.
149168
let mut buf = vec![0u8; long_name.len() + 1];
169+
#[cfg(not(target_os = "freebsd"))]
150170
unsafe {
151171
libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
152172
};
173+
#[cfg(target_os = "freebsd")]
174+
unsafe {
175+
// pthread_get_name_np does not return anything only it can fail if the
176+
// thread id is invalid
177+
libc::pthread_get_name_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
178+
};
153179
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
154-
assert!(cstr.to_bytes().len() >= 15); // POSIX seems to promise at least 15 chars
180+
assert!(cstr.to_bytes().len() >= 15, "name is too short: len={}", cstr.to_bytes().len()); // POSIX seems to promise at least 15 chars
155181
assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));
156182

157183
// Also test directly calling pthread_setname to check its return value.
158184
assert_eq!(set_thread_name(&cstr), 0);
159-
// But with a too long name it should fail.
185+
// But with a too long name it should fail (except on FreeBSD where the
186+
// function has no return, hence cannot indicate failure).
187+
#[cfg(not(target_os = "freebsd"))]
160188
assert_ne!(set_thread_name(&CString::new(long_name).unwrap()), 0);
161189
});
162190
result.unwrap().join().unwrap();

0 commit comments

Comments
 (0)