about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-10-28 08:32:23 +0000
committerbors <bors@rust-lang.org>2022-10-28 08:32:23 +0000
commit3321c2d3e75f18df77f4cbde70b4a7a659697c8b (patch)
tree38bd9ffb3ad277eb2d1f7e6da78548384d281c20
parentbd4a56bce872b3238502d20bb17d6590b73bcf99 (diff)
parent6e3b0df8b8f21c3b833c386c7b8c6554f0be2b94 (diff)
downloadrust-3321c2d3e75f18df77f4cbde70b4a7a659697c8b.tar.gz
rust-3321c2d3e75f18df77f4cbde70b4a7a659697c8b.zip
Auto merge of #2626 - RalfJung:pthread_setname_np, r=RalfJung
pthread_setname_np returns an int on macOS

Fixes https://github.com/rust-lang/miri/issues/2625
-rw-r--r--src/tools/miri/src/shims/unix/macos/foreign_items.rs5
-rw-r--r--src/tools/miri/tests/pass-dep/shims/pthreads.rs18
2 files changed, 19 insertions, 4 deletions
diff --git a/src/tools/miri/src/shims/unix/macos/foreign_items.rs b/src/tools/miri/src/shims/unix/macos/foreign_items.rs
index 371f56ca355..221dc39697f 100644
--- a/src/tools/miri/src/shims/unix/macos/foreign_items.rs
+++ b/src/tools/miri/src/shims/unix/macos/foreign_items.rs
@@ -177,11 +177,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
                 let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
                 let thread = this.pthread_self()?;
                 let max_len = this.eval_libc("MAXTHREADNAMESIZE")?.to_machine_usize(this)?;
-                this.pthread_setname_np(
+                let res = this.pthread_setname_np(
                     thread,
                     this.read_scalar(name)?,
                     max_len.try_into().unwrap(),
                 )?;
+                // Contrary to the manpage, `pthread_setname_np` on macOS still
+                // returns an integer indicating success.
+                this.write_scalar(res, dest)?;
             }
             "pthread_getname_np" => {
                 let [thread, name, len] =
diff --git a/src/tools/miri/tests/pass-dep/shims/pthreads.rs b/src/tools/miri/tests/pass-dep/shims/pthreads.rs
index bbddca74754..ae02a8e3c98 100644
--- a/src/tools/miri/tests/pass-dep/shims/pthreads.rs
+++ b/src/tools/miri/tests/pass-dep/shims/pthreads.rs
@@ -1,6 +1,6 @@
 //@ignore-target-windows: No libc on Windows
 #![feature(cstr_from_bytes_until_nul)]
-use std::ffi::CStr;
+use std::ffi::{CStr, CString};
 use std::thread;
 
 fn main() {
@@ -135,6 +135,13 @@ fn test_named_thread_truncation() {
         .chain(std::iter::repeat(" yada").take(100))
         .collect::<String>();
 
+    fn set_thread_name(name: &CStr) -> i32 {
+        #[cfg(target_os = "linux")]
+        return unsafe { libc::pthread_setname_np(libc::pthread_self(), name.as_ptr().cast()) };
+        #[cfg(target_os = "macos")]
+        return unsafe { libc::pthread_setname_np(name.as_ptr().cast()) };
+    }
+
     let result = thread::Builder::new().name(long_name.clone()).spawn(move || {
         // Rust remembers the full thread name itself.
         assert_eq!(thread::current().name(), Some(long_name.as_str()));
@@ -142,11 +149,16 @@ fn test_named_thread_truncation() {
         // But the system is limited -- make sure we successfully set a truncation.
         let mut buf = vec![0u8; long_name.len() + 1];
         unsafe {
-            libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len());
-        }
+            libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
+        };
         let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
         assert!(cstr.to_bytes().len() >= 15); // POSIX seems to promise at least 15 chars
         assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));
+
+        // Also test directly calling pthread_setname to check its return value.
+        assert_eq!(set_thread_name(&cstr), 0);
+        // But with a too long name it should fail.
+        assert_ne!(set_thread_name(&CString::new(long_name).unwrap()), 0);
     });
     result.unwrap().join().unwrap();
 }