Skip to content

Commit bd99bc7

Browse files
authored
Merge pull request #4132 from YohDeadfall/freebsd-thread-name-changes
Switched FreeBSD to pthread_setname_np
2 parents 3dd4e1e + 1f51d82 commit bd99bc7

File tree

3 files changed

+44
-41
lines changed

3 files changed

+44
-41
lines changed

ci/ci.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ case $HOST_TARGET in
150150
# Partially supported targets (tier 2)
151151
BASIC="empty_main integer heap_alloc libc-mem vec string btreemap" # ensures we have the basics: pre-main code, system allocator
152152
UNIX="hello panic/panic panic/unwind concurrency/simple atomic libc-mem libc-misc libc-random env num_cpus" # the things that are very similar across all Unixes, and hence easily supported there
153-
TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal $BASIC $UNIX time hashmap random fs libc-pipe
154-
TEST_TARGET=i686-unknown-freebsd run_tests_minimal $BASIC $UNIX time hashmap random fs libc-pipe
153+
TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal $BASIC $UNIX time hashmap random threadname pthread fs libc-pipe
154+
TEST_TARGET=i686-unknown-freebsd run_tests_minimal $BASIC $UNIX time hashmap random threadname pthread fs libc-pipe
155155
TEST_TARGET=x86_64-unknown-illumos run_tests_minimal $BASIC $UNIX time hashmap random thread sync available-parallelism tls libc-pipe fs
156156
TEST_TARGET=x86_64-pc-solaris run_tests_minimal $BASIC $UNIX time hashmap random thread sync available-parallelism tls libc-pipe fs
157157
TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX time hashmap random sync threadname pthread epoll eventfd

src/shims/unix/freebsd/foreign_items.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,38 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
2121
let this = self.eval_context_mut();
2222
match link_name.as_str() {
2323
// Threading
24-
"pthread_set_name_np" => {
24+
"pthread_setname_np" => {
2525
let [thread, name] = this.check_shim(abi, Conv::C, link_name, args)?;
2626
let max_len = usize::MAX; // FreeBSD does not seem to have a limit.
27-
// FreeBSD's pthread_set_name_np does not return anything.
28-
this.pthread_setname_np(
27+
let res = match this.pthread_setname_np(
2928
this.read_scalar(thread)?,
3029
this.read_scalar(name)?,
3130
max_len,
3231
/* truncate */ false,
33-
)?;
32+
)? {
33+
ThreadNameResult::Ok => Scalar::from_u32(0),
34+
ThreadNameResult::NameTooLong => unreachable!(),
35+
ThreadNameResult::ThreadNotFound => this.eval_libc("ESRCH"),
36+
};
37+
this.write_scalar(res, dest)?;
3438
}
35-
"pthread_get_name_np" => {
39+
"pthread_getname_np" => {
3640
let [thread, name, len] = this.check_shim(abi, Conv::C, link_name, args)?;
37-
// FreeBSD's pthread_get_name_np does not return anything
38-
// and uses strlcpy, which truncates the resulting value,
41+
// FreeBSD's pthread_getname_np uses strlcpy, which truncates the resulting value,
3942
// but always adds a null terminator (except for zero-sized buffers).
4043
// https://github.com/freebsd/freebsd-src/blob/c2d93a803acef634bd0eede6673aeea59e90c277/lib/libthr/thread/thr_info.c#L119-L144
41-
this.pthread_getname_np(
44+
let res = match this.pthread_getname_np(
4245
this.read_scalar(thread)?,
4346
this.read_scalar(name)?,
4447
this.read_scalar(len)?,
4548
/* truncate */ true,
46-
)?;
49+
)? {
50+
ThreadNameResult::Ok => Scalar::from_u32(0),
51+
// `NameTooLong` is possible when the buffer is zero sized,
52+
ThreadNameResult::NameTooLong => Scalar::from_u32(0),
53+
ThreadNameResult::ThreadNotFound => this.eval_libc("ESRCH"),
54+
};
55+
this.write_scalar(res, dest)?;
4756
}
4857

4958
// File related shims

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

+24-30
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ fn main() {
2929

3030
fn set_thread_name(name: &CStr) -> i32 {
3131
cfg_if::cfg_if! {
32-
if #[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris"))] {
32+
if #[cfg(any(
33+
target_os = "linux",
34+
target_os = "freebsd",
35+
target_os = "illumos",
36+
target_os = "solaris"
37+
))] {
3338
unsafe { libc::pthread_setname_np(libc::pthread_self(), name.as_ptr().cast()) }
34-
} else if #[cfg(target_os = "freebsd")] {
35-
// pthread_set_name_np does not return anything
36-
unsafe { libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast()) };
37-
0
3839
} else if #[cfg(target_os = "macos")] {
3940
unsafe { libc::pthread_setname_np(name.as_ptr().cast()) }
4041
} else {
@@ -47,19 +48,14 @@ fn main() {
4748
cfg_if::cfg_if! {
4849
if #[cfg(any(
4950
target_os = "linux",
51+
target_os = "freebsd",
5052
target_os = "illumos",
5153
target_os = "solaris",
5254
target_os = "macos"
5355
))] {
5456
unsafe {
5557
libc::pthread_getname_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len())
5658
}
57-
} else if #[cfg(target_os = "freebsd")] {
58-
// pthread_get_name_np does not return anything
59-
unsafe {
60-
libc::pthread_get_name_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len())
61-
};
62-
0
6359
} else {
6460
compile_error!("get_thread_name not supported for this OS")
6561
}
@@ -201,27 +197,25 @@ fn main() {
201197
.unwrap();
202198

203199
// Now set the name for a non-existing thread and verify error codes.
204-
// (FreeBSD doesn't return an error code.)
205-
#[cfg(not(target_os = "freebsd"))]
206-
{
207-
let invalid_thread = 0xdeadbeef;
208-
let error = {
209-
cfg_if::cfg_if! {
210-
if #[cfg(target_os = "linux")] {
211-
libc::ENOENT
212-
} else {
213-
libc::ESRCH
214-
}
200+
let invalid_thread = 0xdeadbeef;
201+
let error = {
202+
cfg_if::cfg_if! {
203+
if #[cfg(target_os = "linux")] {
204+
libc::ENOENT
205+
} else {
206+
libc::ESRCH
215207
}
216-
};
217-
#[cfg(not(target_os = "macos"))]
218-
{
219-
// macOS has no `setname` function accepting a thread id as the first argument.
220-
let res = unsafe { libc::pthread_setname_np(invalid_thread, [0].as_ptr()) };
221-
assert_eq!(res, error);
222208
}
223-
let mut buf = [0; 64];
224-
let res = unsafe { libc::pthread_getname_np(invalid_thread, buf.as_mut_ptr(), buf.len()) };
209+
};
210+
211+
#[cfg(not(target_os = "macos"))]
212+
{
213+
// macOS has no `setname` function accepting a thread id as the first argument.
214+
let res = unsafe { libc::pthread_setname_np(invalid_thread, [0].as_ptr()) };
225215
assert_eq!(res, error);
226216
}
217+
218+
let mut buf = [0; 64];
219+
let res = unsafe { libc::pthread_getname_np(invalid_thread, buf.as_mut_ptr(), buf.len()) };
220+
assert_eq!(res, error);
227221
}

0 commit comments

Comments
 (0)