Skip to main content

miri/shims/unix/linux_like/
thread.rs

1use rustc_abi::Size;
2use rustc_middle::ty::Ty;
3use rustc_span::Symbol;
4use rustc_target::callconv::FnAbi;
5
6use crate::shims::unix::thread::{EvalContextExt as _, ThreadNameResult};
7use crate::*;
8
9const TASK_COMM_LEN: u64 = 16;
10
11pub fn prctl<'tcx>(
12    ecx: &mut MiriInterpCx<'tcx>,
13    link_name: Symbol,
14    abi: &FnAbi<'tcx, Ty<'tcx>>,
15    args: &[OpTy<'tcx>],
16    dest: &MPlaceTy<'tcx>,
17) -> InterpResult<'tcx> {
18    let ([op], varargs) = ecx.check_shim_sig_variadic(
19        shim_sig!(extern "C" fn(i32, ...) -> i32),
20        (link_name, abi, args),
21    )?;
22
23    let pr_set_name = ecx.eval_libc_i32("PR_SET_NAME");
24    let pr_get_name = ecx.eval_libc_i32("PR_GET_NAME");
25
26    let res = match ecx.read_scalar(&op)?.to_i32()? {
27        op if op == pr_set_name => {
28            let ([name], _) = ecx.check_varargs(
29                shim_varargs![*libc::c_char],
30                varargs,
31                "prctl(PR_SET_NAME, ...)",
32            )?;
33
34            let name = ecx.read_pointer(&name)?;
35            // The Linux kernel silently truncates long names.
36            // https://www.man7.org/linux/man-pages/man2/PR_SET_NAME.2const.html
37            let res =
38                ecx.pthread_setname_np(None, name, TASK_COMM_LEN, /* truncate */ true)?;
39            assert_eq!(res, ThreadNameResult::Ok);
40            Scalar::from_u32(0)
41        }
42        op if op == pr_get_name => {
43            let ([name], _) = ecx.check_varargs(
44                shim_varargs![*libc::c_char],
45                varargs,
46                "prctl(PR_GET_NAME, ...)",
47            )?;
48
49            let name = ecx.read_pointer(&name)?;
50            let len = Scalar::from_target_usize(TASK_COMM_LEN, ecx);
51            ecx.check_ptr_access(
52                name,
53                Size::from_bytes(TASK_COMM_LEN),
54                CheckInAllocMsg::MemoryAccess,
55            )?;
56            let res = ecx.pthread_getname_np(None, name, len, /* truncate*/ false)?;
57            assert_eq!(res, ThreadNameResult::Ok);
58            Scalar::from_u32(0)
59        }
60        op => throw_unsup_format!("Miri does not support `prctl` syscall with op={}", op),
61    };
62    ecx.write_scalar(res, dest)?;
63    interp_ok(())
64}