miri/shims/unix/android/
thread.rs

1use rustc_abi::Size;
2use rustc_middle::ty::Ty;
3use rustc_span::Symbol;
4use rustc_target::callconv::{Conv, FnAbi};
5
6use crate::helpers::check_min_vararg_count;
7use crate::shims::unix::thread::{EvalContextExt as _, ThreadNameResult};
8use crate::*;
9
10const TASK_COMM_LEN: usize = 16;
11
12pub fn prctl<'tcx>(
13    ecx: &mut MiriInterpCx<'tcx>,
14    link_name: Symbol,
15    abi: &FnAbi<'tcx, Ty<'tcx>>,
16    args: &[OpTy<'tcx>],
17    dest: &MPlaceTy<'tcx>,
18) -> InterpResult<'tcx> {
19    let ([op], varargs) = ecx.check_shim_variadic(abi, Conv::C, link_name, args)?;
20
21    // FIXME: Use constants once https://github.com/rust-lang/libc/pull/3941 backported to the 0.2 branch.
22    let pr_set_name = 15;
23    let pr_get_name = 16;
24
25    let res = match ecx.read_scalar(op)?.to_i32()? {
26        op if op == pr_set_name => {
27            let [name] = check_min_vararg_count("prctl(PR_SET_NAME, ...)", varargs)?;
28            let name = ecx.read_scalar(name)?;
29            let thread = ecx.pthread_self()?;
30            // The Linux kernel silently truncates long names.
31            // https://www.man7.org/linux/man-pages/man2/PR_SET_NAME.2const.html
32            let res =
33                ecx.pthread_setname_np(thread, name, TASK_COMM_LEN, /* truncate */ true)?;
34            assert_eq!(res, ThreadNameResult::Ok);
35            Scalar::from_u32(0)
36        }
37        op if op == pr_get_name => {
38            let [name] = check_min_vararg_count("prctl(PR_GET_NAME, ...)", varargs)?;
39            let name = ecx.read_scalar(name)?;
40            let thread = ecx.pthread_self()?;
41            let len = Scalar::from_target_usize(TASK_COMM_LEN as u64, ecx);
42            ecx.check_ptr_access(
43                name.to_pointer(ecx)?,
44                Size::from_bytes(TASK_COMM_LEN),
45                CheckInAllocMsg::MemoryAccessTest,
46            )?;
47            let res = ecx.pthread_getname_np(thread, name, len, /* truncate*/ false)?;
48            assert_eq!(res, ThreadNameResult::Ok);
49            Scalar::from_u32(0)
50        }
51        op => throw_unsup_format!("Miri does not support `prctl` syscall with op={}", op),
52    };
53    ecx.write_scalar(res, dest)?;
54    interp_ok(())
55}