Skip to content

Commit 1827917

Browse files
committed
Implemented truncation for prctl
1 parent 37d75f9 commit 1827917

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/shims/unix/android/thread.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,31 @@ pub fn prctl<'tcx>(
2020
// https://github.com/rust-lang/libc/pull/3941 lands.
2121
const PR_SET_NAME: i32 = 15;
2222
const PR_GET_NAME: i32 = 16;
23+
const TASK_COMM_LEN: usize = 16;
2324

2425
let [op] = check_min_arg_count("prctl", args)?;
2526
let res = match this.read_scalar(op)?.to_i32()? {
2627
PR_SET_NAME => {
2728
let [_, name] = check_min_arg_count("prctl(PR_SET_NAME, ...)", args)?;
2829

29-
let tid = this.pthread_self()?;
30-
let name = this.read_scalar(name)?;
31-
let name_len = 16;
30+
let name = this.read_scalar(name)?.to_pointer(this)?;
31+
let name =
32+
this.read_c_str(name)?.iter().take(TASK_COMM_LEN - 1).copied().collect::<Vec<_>>();
33+
34+
let thread = this.pthread_self()?.to_int(this.libc_ty_layout("pthread_t").size)?;
35+
let thread = ThreadId::try_from(thread).unwrap();
3236

33-
this.pthread_setname_np(tid, name, name_len)?
37+
this.set_thread_name(thread, name);
38+
Scalar::from_u32(0)
3439
}
3540
PR_GET_NAME => {
3641
let [_, name] = check_min_arg_count("prctl(PR_GET_NAME, ...)", args)?;
3742

38-
let tid = this.pthread_self()?;
43+
let thread = this.pthread_self()?;
3944
let name = this.read_scalar(name)?;
40-
let name_len = Scalar::from_target_usize(16, this);
45+
let name_len = Scalar::from_target_usize(TASK_COMM_LEN as u64, this);
4146

42-
this.pthread_getname_np(tid, name, name_len)?
47+
this.pthread_getname_np(thread, name, name_len)?
4348
}
4449
op => {
4550
this.handle_unsupported_foreign_item(format!("can't execute prctl with OP {op}"))?;

tests/pass-dep/libc/threadname.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ignore-target: windows # No pthreads and prctl on Windows
22
use std::ffi::CStr;
3-
#[cfg(not(target_os = "freebsd"))]
4-
use std::ffi::CString;
53
use std::thread;
64

75
fn main() {
@@ -69,10 +67,11 @@ fn main() {
6967

7068
// Also test directly calling pthread_setname to check its return value.
7169
assert_eq!(set_thread_name(&cstr), 0);
72-
// But with a too long name it should fail (except on FreeBSD where the
73-
// function has no return, hence cannot indicate failure).
74-
#[cfg(not(target_os = "freebsd"))]
75-
assert_ne!(set_thread_name(&CString::new(long_name).unwrap()), 0);
70+
// But with a too long name it should fail except:
71+
// * on FreeBSD where the function has no return, hence cannot indicate failure,
72+
// * on Android where prctl silently truncates the string.
73+
#[cfg(not(any(target_os = "freebsd", target_os = "android")))]
74+
assert_ne!(set_thread_name(&std::ffi::CString::new(long_name).unwrap()), 0);
7675
});
7776
result.unwrap().join().unwrap();
7877
}

0 commit comments

Comments
 (0)