|
| 1 | +//@only-target: android # Miri supports prctl for Android only |
| 2 | +use std::ffi::{CStr, CString}; |
| 3 | +use std::thread; |
| 4 | + |
| 5 | +// The Linux kernel all names 16 bytes long including the null terminator. |
| 6 | +const MAX_THREAD_NAME_LEN: usize = 16; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + // The short name should be shorter than 16 bytes which POSIX promises |
| 10 | + // for thread names. The length includes a null terminator. |
| 11 | + let short_name = "test_named".to_owned(); |
| 12 | + let long_name = std::iter::once("test_named_thread_truncation") |
| 13 | + .chain(std::iter::repeat(" yada").take(100)) |
| 14 | + .collect::<String>(); |
| 15 | + |
| 16 | + fn set_thread_name(name: &CStr) -> i32 { |
| 17 | + unsafe { libc::prctl(libc::PR_SET_NAME, name.as_ptr().cast::<libc::c_char>()) } |
| 18 | + } |
| 19 | + |
| 20 | + fn get_thread_name(name: &mut [u8]) -> i32 { |
| 21 | + assert!(name.len() >= MAX_THREAD_NAME_LEN); |
| 22 | + unsafe { libc::prctl(libc::PR_GET_NAME, name.as_mut_ptr().cast::<libc::c_char>()) } |
| 23 | + } |
| 24 | + |
| 25 | + // Set name via Rust API, get it via prctl. |
| 26 | + let long_name2 = long_name.clone(); |
| 27 | + thread::Builder::new() |
| 28 | + .name(long_name.clone()) |
| 29 | + .spawn(move || { |
| 30 | + let mut buf = vec![0u8; MAX_THREAD_NAME_LEN]; |
| 31 | + assert_eq!(get_thread_name(&mut buf), 0); |
| 32 | + let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); |
| 33 | + let truncated_name = &long_name2[..long_name2.len().min(MAX_THREAD_NAME_LEN - 1)]; |
| 34 | + assert_eq!(cstr.to_bytes(), truncated_name.as_bytes()); |
| 35 | + }) |
| 36 | + .unwrap() |
| 37 | + .join() |
| 38 | + .unwrap(); |
| 39 | + |
| 40 | + // Set name via prctl and get it again (short name). |
| 41 | + thread::Builder::new() |
| 42 | + .spawn(move || { |
| 43 | + // Set short thread name. |
| 44 | + let cstr = CString::new(short_name.clone()).unwrap(); |
| 45 | + assert!(cstr.to_bytes_with_nul().len() <= MAX_THREAD_NAME_LEN); // this should fit |
| 46 | + assert_eq!(set_thread_name(&cstr), 0); |
| 47 | + |
| 48 | + let mut buf = vec![0u8; MAX_THREAD_NAME_LEN]; |
| 49 | + assert_eq!(get_thread_name(&mut buf), 0); |
| 50 | + let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); |
| 51 | + assert_eq!(cstr.to_bytes(), short_name.as_bytes()); |
| 52 | + }) |
| 53 | + .unwrap() |
| 54 | + .join() |
| 55 | + .unwrap(); |
| 56 | + |
| 57 | + // Set name via prctl and get it again (long name). |
| 58 | + thread::Builder::new() |
| 59 | + .spawn(move || { |
| 60 | + // Set full thread name. |
| 61 | + let cstr = CString::new(long_name.clone()).unwrap(); |
| 62 | + assert!(cstr.to_bytes_with_nul().len() > MAX_THREAD_NAME_LEN); |
| 63 | + // Names are truncated by the Linux kernel. |
| 64 | + assert_eq!(set_thread_name(&cstr), 0); |
| 65 | + |
| 66 | + let mut buf = vec![0u8; MAX_THREAD_NAME_LEN]; |
| 67 | + assert_eq!(get_thread_name(&mut buf), 0); |
| 68 | + let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); |
| 69 | + assert_eq!(cstr.to_bytes(), &long_name.as_bytes()[..(MAX_THREAD_NAME_LEN - 1)]); |
| 70 | + }) |
| 71 | + .unwrap() |
| 72 | + .join() |
| 73 | + .unwrap(); |
| 74 | +} |
0 commit comments