diff --git a/rust-version b/rust-version index 71d06eff494c..0bcadc0a366f 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -1603a70f82240ba2d27f72f964e36614d7620ad3 +20ffea6938b5839c390252e07940b99e3b6a889a diff --git a/src/shims/unix/foreign_items.rs b/src/shims/unix/foreign_items.rs index 6ea10de0b8a8..0ca838b2103f 100644 --- a/src/shims/unix/foreign_items.rs +++ b/src/shims/unix/foreign_items.rs @@ -437,7 +437,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx } "pthread_self" => { let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; - this.pthread_self(dest)?; + let res = this.pthread_self()?; + this.write_scalar(res, dest)?; } "sched_yield" => { let [] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; diff --git a/src/shims/unix/linux/foreign_items.rs b/src/shims/unix/linux/foreign_items.rs index bae3780b460c..817bfc7d1cc9 100644 --- a/src/shims/unix/linux/foreign_items.rs +++ b/src/shims/unix/linux/foreign_items.rs @@ -69,6 +69,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let result = this.pthread_condattr_getclock(attr, clock_id)?; this.write_scalar(Scalar::from_i32(result), dest)?; } + "pthread_setname_np" => { + let [thread, name] = + this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; + let res = this.pthread_setname_np( + this.read_scalar(thread)?.check_init()?, + this.read_scalar(name)?.check_init()?, + )?; + this.write_scalar(res, dest)?; + } // Dynamically invoked syscalls "syscall" => { diff --git a/src/shims/unix/macos/foreign_items.rs b/src/shims/unix/macos/foreign_items.rs index 35751d5818ab..da8e8a63bfe8 100644 --- a/src/shims/unix/macos/foreign_items.rs +++ b/src/shims/unix/macos/foreign_items.rs @@ -173,8 +173,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx // Threading "pthread_setname_np" => { let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; - let name = this.read_pointer(name)?; - this.pthread_setname_np(name)?; + let thread = this.pthread_self()?; + this.pthread_setname_np(thread, this.read_scalar(name)?.check_init()?)?; } // Incomplete shims that we "stub out" just to get pre-main initialization code to work. diff --git a/src/shims/unix/thread.rs b/src/shims/unix/thread.rs index 1a8531e88047..22114eae6724 100644 --- a/src/shims/unix/thread.rs +++ b/src/shims/unix/thread.rs @@ -84,11 +84,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx Ok(0) } - fn pthread_self(&mut self, dest: &PlaceTy<'tcx, Provenance>) -> InterpResult<'tcx> { + fn pthread_self(&mut self) -> InterpResult<'tcx, Scalar> { let this = self.eval_context_mut(); let thread_id = this.get_active_thread(); - this.write_scalar(Scalar::from_uint(thread_id.to_u32(), dest.layout.size), dest) + Ok(Scalar::from_machine_usize(thread_id.into(), this)) + } + + fn pthread_setname_np( + &mut self, + thread: Scalar, + name: Scalar, + ) -> InterpResult<'tcx, Scalar> { + let this = self.eval_context_mut(); + + let thread = ThreadId::try_from(thread.to_machine_usize(this)?).unwrap(); + let name = name.to_pointer(this)?; + + let name = this.read_c_str(name)?.to_owned(); + this.set_thread_name(thread, name); + + Ok(Scalar::from_u32(0)) } fn prctl(&mut self, args: &[OpTy<'tcx, Provenance>]) -> InterpResult<'tcx, i32> { @@ -117,7 +133,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx // byte. Since `read_c_str` returns the string without the null // byte, we need to truncate to 15. name.truncate(15); - this.set_active_thread_name(name); + this.set_thread_name(this.get_active_thread(), name); } else if option == this.eval_libc_i32("PR_GET_NAME")? { if args.len() < 2 { throw_ub_format!( @@ -127,7 +143,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx } let address = this.read_pointer(&args[1])?; - let mut name = this.get_active_thread_name().to_vec(); + let mut name = this.get_thread_name(this.get_active_thread()).to_vec(); name.push(0u8); assert!(name.len() <= 16); this.write_bytes_ptr(address, name)?; @@ -138,16 +154,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx Ok(0) } - fn pthread_setname_np(&mut self, name: Pointer>) -> InterpResult<'tcx> { - let this = self.eval_context_mut(); - this.assert_target_os("macos", "pthread_setname_np"); - - let name = this.read_c_str(name)?.to_owned(); - this.set_active_thread_name(name); - - Ok(()) - } - fn sched_yield(&mut self) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); diff --git a/src/thread.rs b/src/thread.rs index 683694f482ea..6f394fa42fc9 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -69,9 +69,9 @@ impl From for ThreadId { } } -impl ThreadId { - pub fn to_u32_scalar(&self) -> Scalar { - Scalar::from_u32(self.0) +impl From for u64 { + fn from(t: ThreadId) -> Self { + t.0.into() } } @@ -394,14 +394,9 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> { Ok(()) } - /// Set the name of the active thread. - fn set_active_thread_name(&mut self, new_thread_name: Vec) { - self.active_thread_mut().thread_name = Some(new_thread_name); - } - - /// Get the name of the active thread. - pub fn get_active_thread_name(&self) -> &[u8] { - self.active_thread_ref().thread_name() + /// Set the name of the given thread. + pub fn set_thread_name(&mut self, thread: ThreadId, new_thread_name: Vec) { + self.threads[thread].thread_name = Some(new_thread_name); } /// Get the name of the given thread. @@ -704,18 +699,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx } #[inline] - fn set_active_thread_name(&mut self, new_thread_name: Vec) { + fn set_thread_name(&mut self, thread: ThreadId, new_thread_name: Vec) { let this = self.eval_context_mut(); - this.machine.threads.set_active_thread_name(new_thread_name); + this.machine.threads.set_thread_name(thread, new_thread_name); } #[inline] - fn get_active_thread_name<'c>(&'c self) -> &'c [u8] + fn get_thread_name<'c>(&'c self, thread: ThreadId) -> &'c [u8] where 'mir: 'c, { let this = self.eval_context_ref(); - this.machine.threads.get_active_thread_name() + this.machine.threads.get_thread_name(thread) } #[inline]