Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 695a1b6

Browse files
committed
Auto merge of rust-lang#3899 - YohDeadfall:prctl-thread-name, r=RalfJung
Android: Added support for prctl handling thread names Addresses the first part of rust-lang#3618.
2 parents 13e5e4b + c2f43ba commit 695a1b6

File tree

14 files changed

+185
-7
lines changed

14 files changed

+185
-7
lines changed

src/tools/miri/ci/ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ case $HOST_TARGET in
154154
TEST_TARGET=i686-unknown-freebsd run_tests_minimal $BASIC $UNIX time hashmap random threadname pthread fs libc-pipe
155155
TEST_TARGET=x86_64-unknown-illumos run_tests_minimal $BASIC $UNIX time hashmap random thread sync available-parallelism tls libc-pipe
156156
TEST_TARGET=x86_64-pc-solaris run_tests_minimal $BASIC $UNIX time hashmap random thread sync available-parallelism tls libc-pipe
157-
TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX time hashmap pthread --skip threadname
157+
TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX time hashmap threadname pthread
158158
TEST_TARGET=wasm32-wasip2 run_tests_minimal $BASIC wasm
159159
TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std empty_main wasm # this target doesn't really have std
160160
TEST_TARGET=thumbv7em-none-eabihf run_tests_minimal no_std

src/tools/miri/src/shims/unix/android/foreign_items.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_span::Symbol;
22
use rustc_target::spec::abi::Abi;
33

4+
use crate::shims::unix::android::thread::prctl;
45
use crate::*;
56

67
pub fn is_dyn_sym(_name: &str) -> bool {
@@ -25,6 +26,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
2526
this.write_scalar(errno_place.to_ref(this).to_scalar(), dest)?;
2627
}
2728

29+
// Threading
30+
"prctl" => prctl(this, link_name, abi, args, dest)?,
31+
2832
_ => return interp_ok(EmulateItemResult::NotSupported),
2933
}
3034
interp_ok(EmulateItemResult::NeedsReturn)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod foreign_items;
2+
pub mod thread;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use rustc_span::Symbol;
2+
use rustc_target::abi::Size;
3+
use rustc_target::spec::abi::Abi;
4+
5+
use crate::helpers::check_min_arg_count;
6+
use crate::shims::unix::thread::EvalContextExt as _;
7+
use crate::*;
8+
9+
const TASK_COMM_LEN: usize = 16;
10+
11+
pub fn prctl<'tcx>(
12+
this: &mut MiriInterpCx<'tcx>,
13+
link_name: Symbol,
14+
abi: Abi,
15+
args: &[OpTy<'tcx>],
16+
dest: &MPlaceTy<'tcx>,
17+
) -> InterpResult<'tcx> {
18+
// We do not use `check_shim` here because `prctl` is variadic. The argument
19+
// count is checked bellow.
20+
this.check_abi_and_shim_symbol_clash(abi, Abi::C { unwind: false }, link_name)?;
21+
22+
// FIXME: Use constants once https://github.com/rust-lang/libc/pull/3941 backported to the 0.2 branch.
23+
let pr_set_name = 15;
24+
let pr_get_name = 16;
25+
26+
let [op] = check_min_arg_count("prctl", args)?;
27+
let res = match this.read_scalar(op)?.to_i32()? {
28+
op if op == pr_set_name => {
29+
let [_, name] = check_min_arg_count("prctl(PR_SET_NAME, ...)", args)?;
30+
let name = this.read_scalar(name)?;
31+
let thread = this.pthread_self()?;
32+
// The Linux kernel silently truncates long names.
33+
// https://www.man7.org/linux/man-pages/man2/PR_SET_NAME.2const.html
34+
let res =
35+
this.pthread_setname_np(thread, name, TASK_COMM_LEN, /* truncate */ true)?;
36+
assert!(res);
37+
Scalar::from_u32(0)
38+
}
39+
op if op == pr_get_name => {
40+
let [_, name] = check_min_arg_count("prctl(PR_GET_NAME, ...)", args)?;
41+
let name = this.read_scalar(name)?;
42+
let thread = this.pthread_self()?;
43+
let len = Scalar::from_target_usize(TASK_COMM_LEN as u64, this);
44+
this.check_ptr_access(
45+
name.to_pointer(this)?,
46+
Size::from_bytes(TASK_COMM_LEN),
47+
CheckInAllocMsg::MemoryAccessTest,
48+
)?;
49+
let res = this.pthread_getname_np(thread, name, len, /* truncate*/ false)?;
50+
assert!(res);
51+
Scalar::from_u32(0)
52+
}
53+
op => throw_unsup_format!("Miri does not support `prctl` syscall with op={}", op),
54+
};
55+
this.write_scalar(res, dest)?;
56+
interp_ok(())
57+
}

src/tools/miri/src/shims/unix/freebsd/foreign_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
2929
this.read_scalar(thread)?,
3030
this.read_scalar(name)?,
3131
max_len,
32+
/* truncate */ false,
3233
)?;
3334
}
3435
"pthread_get_name_np" => {

src/tools/miri/src/shims/unix/linux/foreign_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
8484
this.read_scalar(thread)?,
8585
this.read_scalar(name)?,
8686
TASK_COMM_LEN,
87+
/* truncate */ false,
8788
)?;
8889
let res = if res { Scalar::from_u32(0) } else { this.eval_libc("ERANGE") };
8990
this.write_scalar(res, dest)?;

src/tools/miri/src/shims/unix/macos/foreign_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
181181
thread,
182182
this.read_scalar(name)?,
183183
this.eval_libc("MAXTHREADNAMESIZE").to_target_usize(this)?.try_into().unwrap(),
184+
/* truncate */ false,
184185
)? {
185186
Scalar::from_u32(0)
186187
} else {

src/tools/miri/src/shims/unix/solarish/foreign_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
3030
this.read_scalar(thread)?,
3131
this.read_scalar(name)?,
3232
max_len,
33+
/* truncate */ false,
3334
)?;
3435
let res = if res { Scalar::from_u32(0) } else { this.eval_libc("ERANGE") };
3536
this.write_scalar(res, dest)?;

src/tools/miri/src/shims/unix/thread.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,29 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
6464
}
6565

6666
/// Set the name of the specified thread. If the name including the null terminator
67-
/// is longer than `name_max_len`, then `false` is returned.
67+
/// is longer or equals to `name_max_len`, then if `truncate` is set the truncated name
68+
/// is used as the thread name, otherwise `false` is returned.
6869
fn pthread_setname_np(
6970
&mut self,
7071
thread: Scalar,
7172
name: Scalar,
7273
name_max_len: usize,
74+
truncate: bool,
7375
) -> InterpResult<'tcx, bool> {
7476
let this = self.eval_context_mut();
7577

7678
let thread = thread.to_int(this.libc_ty_layout("pthread_t").size)?;
7779
let thread = ThreadId::try_from(thread).unwrap();
7880
let name = name.to_pointer(this)?;
79-
let name = this.read_c_str(name)?.to_owned();
81+
let mut name = this.read_c_str(name)?.to_owned();
8082

8183
// Comparing with `>=` to account for null terminator.
8284
if name.len() >= name_max_len {
83-
return interp_ok(false);
85+
if truncate {
86+
name.truncate(name_max_len.saturating_sub(1));
87+
} else {
88+
return interp_ok(false);
89+
}
8490
}
8591

8692
this.set_thread_name(thread, name);

src/tools/miri/test_dependencies/Cargo.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is automatically @generated by Cargo.
22
# It is not intended for manual editing.
3-
version = 3
3+
version = 4
44

55
[[package]]
66
name = "addr2line"
@@ -128,9 +128,9 @@ dependencies = [
128128

129129
[[package]]
130130
name = "libc"
131-
version = "0.2.158"
131+
version = "0.2.161"
132132
source = "registry+https://github.com/rust-lang/crates.io-index"
133-
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
133+
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
134134

135135
[[package]]
136136
name = "linux-raw-sys"

0 commit comments

Comments
 (0)