Skip to content

Commit 00ae882

Browse files
committed
Fixed get thread name behavior for FreeBSD
1 parent 0c93071 commit 00ae882

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/shims/unix/freebsd/foreign_items.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
3434
"pthread_get_name_np" => {
3535
let [thread, name, len] =
3636
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
37-
// FreeBSD's pthread_get_name_np does not return anything.
37+
// FreeBSD's pthread_get_name_np does not return anything
38+
// and uses strlcpy, which truncates the resulting value,
39+
// but always adds a null terminator (except for zero-sized buffers).
40+
// https://github.com/freebsd/freebsd-src/blob/c2d93a803acef634bd0eede6673aeea59e90c277/lib/libthr/thread/thr_info.c#L119-L144
3841
this.pthread_getname_np(
3942
this.read_scalar(thread)?,
4043
this.read_scalar(name)?,
4144
this.read_scalar(len)?,
42-
false,
45+
/* truncate */ true,
4346
)?;
4447
}
4548

tests/pass-dep/libc/pthread-threadname.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ fn main() {
7979
// if the buffer is shorter than the thread name.
8080
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
8181
assert_eq!(get_thread_name(&mut buf[..4]), libc::ERANGE);
82-
83-
// For libc implementation for macOS it's not an error
84-
// for a buffer being too short for the thread name.
85-
#[cfg(target_os = "macos")]
82+
// On macOS and FreeBSD it's not an error for the buffer to be
83+
// too short for the thread name -- they truncate instead.
84+
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
8685
{
8786
// Ensure that a zero sized buffer returns no error.
8887
assert_eq!(get_thread_name(&mut buf[..0]), 0);
@@ -123,8 +122,8 @@ fn main() {
123122
// Also test directly calling pthread_setname to check its return value.
124123
assert_eq!(set_thread_name(&cstr), 0);
125124

126-
// But with a too long name it should fail (except on FreeBSD where the
127-
// function has no return, hence cannot indicate failure).
125+
// But with a too long name it should fail (except on FreeBSD where
126+
// names of arbitrary size seem to be supported).
128127
// On macOS, the error code is different.
129128
#[cfg(not(any(target_os = "freebsd", target_os = "macos")))]
130129
assert_eq!(set_thread_name(&CString::new(long_name).unwrap()), libc::ERANGE);

0 commit comments

Comments
 (0)