|
| 1 | +use rustc_span::Symbol; |
| 2 | +use rustc_target::abi::Size; |
| 3 | +use rustc_target::spec::abi::Abi; |
| 4 | + |
| 5 | +usecrate::helpers::check_min_arg_count; |
| 6 | +usecrate::shims::unix::thread::EvalContextExtas _; |
| 7 | +usecrate::*; |
| 8 | + |
| 9 | +constTASK_COMM_LEN:usize = 16; |
| 10 | + |
| 11 | +pubfnprctl<'tcx>( |
| 12 | +this:&mutMiriInterpCx<'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_LENasu64, 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 | +} |
0 commit comments