forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for prctl handling thread names
- Loading branch information
1 parent
3386ae2
commit c2f43ba
Showing
14 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod foreign_items; | ||
pub mod thread; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use rustc_span::Symbol; | ||
use rustc_target::abi::Size; | ||
use rustc_target::spec::abi::Abi; | ||
|
||
use crate::helpers::check_min_arg_count; | ||
use crate::shims::unix::thread::EvalContextExt as _; | ||
use crate::*; | ||
|
||
const TASK_COMM_LEN: usize = 16; | ||
|
||
pub fn prctl<'tcx>( | ||
this: &mut MiriInterpCx<'tcx>, | ||
link_name: Symbol, | ||
abi: Abi, | ||
args: &[OpTy<'tcx>], | ||
dest: &MPlaceTy<'tcx>, | ||
) -> InterpResult<'tcx> { | ||
// We do not use `check_shim` here because `prctl` is variadic. The argument | ||
// count is checked bellow. | ||
this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?; | ||
|
||
// FIXME: Use constants once https://github.com/rust-lang/libc/pull/3941 backported to the 0.2 branch. | ||
let pr_set_name = 15; | ||
let pr_get_name = 16; | ||
|
||
let [op] = check_min_arg_count("prctl", args)?; | ||
let res = match this.read_scalar(op)?.to_i32()? { | ||
op if op == pr_set_name => { | ||
let [_, name] = check_min_arg_count("prctl(PR_SET_NAME, ...)", args)?; | ||
let name = this.read_scalar(name)?; | ||
let thread = this.pthread_self()?; | ||
// The Linux kernel silently truncates long names. | ||
// https://www.man7.org/linux/man-pages/man2/PR_SET_NAME.2const.html | ||
let res = | ||
this.pthread_setname_np(thread, name, TASK_COMM_LEN, /* truncate */ true)?; | ||
assert!(res); | ||
Scalar::from_u32(0) | ||
} | ||
op if op == pr_get_name => { | ||
let [_, name] = check_min_arg_count("prctl(PR_GET_NAME, ...)", args)?; | ||
let name = this.read_scalar(name)?; | ||
let thread = this.pthread_self()?; | ||
let len = Scalar::from_target_usize(TASK_COMM_LEN as u64, this); | ||
this.check_ptr_access( | ||
name.to_pointer(this)?, | ||
Size::from_bytes(TASK_COMM_LEN), | ||
CheckInAllocMsg::MemoryAccessTest, | ||
)?; | ||
let res = this.pthread_getname_np(thread, name, len, /* truncate*/ false)?; | ||
assert!(res); | ||
Scalar::from_u32(0) | ||
} | ||
op => throw_unsup_format!("Miri does not support `prctl` syscall with op={}", op), | ||
}; | ||
this.write_scalar(res, dest)?; | ||
interp_ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/tools/miri/tests/fail-dep/libc/prctl-get-name-buffer-too-small.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//! Ensure we report UB when the buffer is smaller than 16 bytes (even if the thread | ||
//! name would fit in the smaller buffer). | ||
//@only-target: android # Miri supports prctl for Android only | ||
|
||
fn main() { | ||
let mut buf = vec![0u8; 15]; | ||
unsafe { | ||
libc::prctl(libc::PR_GET_NAME, buf.as_mut_ptr().cast::<libc::c_char>()); //~ ERROR: memory access failed: expected a pointer to 16 bytes of memory, but got alloc952 which is only 15 bytes from the end of the allocation | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/tools/miri/tests/fail-dep/libc/prctl-get-name-buffer-too-small.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error: Undefined Behavior: memory access failed: expected a pointer to 16 bytes of memory, but got ALLOC which is only 15 bytes from the end of the allocation | ||
--> tests/fail-dep/libc/prctl-threadname.rs:LL:CC | ||
| | ||
LL | libc::prctl(libc::PR_GET_NAME, buf.as_mut_ptr().cast::<libc::c_char>()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: expected a pointer to 16 bytes of memory, but got ALLOC which is only 15 bytes from the end of the allocation | ||
| | ||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
help: ALLOC was allocated here: | ||
--> tests/fail-dep/libc/prctl-threadname.rs:LL:CC | ||
| | ||
LL | let mut buf = vec![0u8; 15]; | ||
| ^^^^^^^^^^^^^ | ||
= note: BACKTRACE (of the first span): | ||
= note: inside `main` at tests/fail-dep/libc/prctl-threadname.rs:LL:CC | ||
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//@only-target: android # Miri supports prctl for Android only | ||
use std::ffi::{CStr, CString}; | ||
use std::thread; | ||
|
||
// The Linux kernel all names 16 bytes long including the null terminator. | ||
const MAX_THREAD_NAME_LEN: usize = 16; | ||
|
||
fn main() { | ||
// The short name should be shorter than 16 bytes which POSIX promises | ||
// for thread names. The length includes a null terminator. | ||
let short_name = "test_named".to_owned(); | ||
let long_name = std::iter::once("test_named_thread_truncation") | ||
.chain(std::iter::repeat(" yada").take(100)) | ||
.collect::<String>(); | ||
|
||
fn set_thread_name(name: &CStr) -> i32 { | ||
unsafe { libc::prctl(libc::PR_SET_NAME, name.as_ptr().cast::<libc::c_char>()) } | ||
} | ||
|
||
fn get_thread_name(name: &mut [u8]) -> i32 { | ||
assert!(name.len() >= MAX_THREAD_NAME_LEN); | ||
unsafe { libc::prctl(libc::PR_GET_NAME, name.as_mut_ptr().cast::<libc::c_char>()) } | ||
} | ||
|
||
// Set name via Rust API, get it via prctl. | ||
let long_name2 = long_name.clone(); | ||
thread::Builder::new() | ||
.name(long_name.clone()) | ||
.spawn(move || { | ||
let mut buf = vec![0u8; MAX_THREAD_NAME_LEN]; | ||
assert_eq!(get_thread_name(&mut buf), 0); | ||
let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); | ||
let truncated_name = &long_name2[..long_name2.len().min(MAX_THREAD_NAME_LEN - 1)]; | ||
assert_eq!(cstr.to_bytes(), truncated_name.as_bytes()); | ||
}) | ||
.unwrap() | ||
.join() | ||
.unwrap(); | ||
|
||
// Set name via prctl and get it again (short name). | ||
thread::Builder::new() | ||
.spawn(move || { | ||
// Set short thread name. | ||
let cstr = CString::new(short_name.clone()).unwrap(); | ||
assert!(cstr.to_bytes_with_nul().len() <= MAX_THREAD_NAME_LEN); // this should fit | ||
assert_eq!(set_thread_name(&cstr), 0); | ||
|
||
let mut buf = vec![0u8; MAX_THREAD_NAME_LEN]; | ||
assert_eq!(get_thread_name(&mut buf), 0); | ||
let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); | ||
assert_eq!(cstr.to_bytes(), short_name.as_bytes()); | ||
}) | ||
.unwrap() | ||
.join() | ||
.unwrap(); | ||
|
||
// Set name via prctl and get it again (long name). | ||
thread::Builder::new() | ||
.spawn(move || { | ||
// Set full thread name. | ||
let cstr = CString::new(long_name.clone()).unwrap(); | ||
assert!(cstr.to_bytes_with_nul().len() > MAX_THREAD_NAME_LEN); | ||
// Names are truncated by the Linux kernel. | ||
assert_eq!(set_thread_name(&cstr), 0); | ||
|
||
let mut buf = vec![0u8; MAX_THREAD_NAME_LEN]; | ||
assert_eq!(get_thread_name(&mut buf), 0); | ||
let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); | ||
assert_eq!(cstr.to_bytes(), &long_name.as_bytes()[..(MAX_THREAD_NAME_LEN - 1)]); | ||
}) | ||
.unwrap() | ||
.join() | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters