-
Notifications
You must be signed in to change notification settings - Fork 392
Android: Added support for prctl handling thread names #3899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
tests/fail-dep/libc/prctl-get-name-buffer-too-small.stderr
This file contains hidden or 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 hidden or 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>()) } | ||
YohDeadfall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.