Skip to content

Commit 48c5509

Browse files
committed
Made PR_GET_NAME not based on pthread impl
1 parent 1827917 commit 48c5509

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/shims/unix/android/thread.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use std::iter;
2+
13
use rustc_span::Symbol;
24
use rustc_target::spec::abi::Abi;
35

46
use crate::helpers::check_min_arg_count;
5-
use crate::shims::unix::thread::EvalContextExt as _;
7+
use crate::shims::unix::thread::{DEFAULT_THREAD_NAME, EvalContextExt as _};
68
use crate::*;
79

810
pub fn prctl<'tcx>(
@@ -40,11 +42,25 @@ pub fn prctl<'tcx>(
4042
PR_GET_NAME => {
4143
let [_, name] = check_min_arg_count("prctl(PR_GET_NAME, ...)", args)?;
4244

43-
let thread = this.pthread_self()?;
44-
let name = this.read_scalar(name)?;
45-
let name_len = Scalar::from_target_usize(TASK_COMM_LEN as u64, this);
45+
let name_out = this.read_scalar(name)?;
46+
let name_out = name_out.to_pointer(this)?;
47+
48+
let thread = this.pthread_self()?.to_int(this.libc_ty_layout("pthread_t").size)?;
49+
let thread = ThreadId::try_from(thread).unwrap();
4650

47-
this.pthread_getname_np(thread, name, name_len)?
51+
// FIXME: we should use the program name if the thread name is not set
52+
let name = this.get_thread_name(thread).unwrap_or(DEFAULT_THREAD_NAME).to_owned();
53+
let name_len = name.len().max(TASK_COMM_LEN - 1);
54+
55+
this.eval_context_mut().write_bytes_ptr(
56+
name_out,
57+
name.iter()
58+
.take(name_len)
59+
.copied()
60+
.chain(iter::repeat_n(0u8, TASK_COMM_LEN.strict_sub(name_len))),
61+
)?;
62+
63+
Scalar::from_u32(0)
4864
}
4965
op => {
5066
this.handle_unsupported_foreign_item(format!("can't execute prctl with OP {op}"))?;

src/shims/unix/thread.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use rustc_target::spec::abi::Abi;
22

33
use crate::*;
44

5+
pub const DEFAULT_THREAD_NAME: &[u8] = b"<unnamed>";
6+
57
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
68
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
79
fn pthread_create(
@@ -103,7 +105,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
103105
let len = len.to_target_usize(this)?;
104106

105107
// FIXME: we should use the program name if the thread name is not set
106-
let name = this.get_thread_name(thread).unwrap_or(b"<unnamed>").to_owned();
108+
let name = this.get_thread_name(thread).unwrap_or(DEFAULT_THREAD_NAME).to_owned();
107109
let (success, _written) = this.write_c_str(&name, name_out, len)?;
108110

109111
interp_ok(if success { Scalar::from_u32(0) } else { this.eval_libc("ERANGE") })

0 commit comments

Comments
 (0)