X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Ftools%2Fmiri%2Fsrc%2Fshims%2Funix%2Fthread.rs;h=b43682710bbe5c4adfba87955bb184f259348845;hb=d4bd794f5e8c63dd22c3e7876eef026af4b93e1e;hp=59474d8d10ad7b058e207536356b8b5a3f623ab0;hpb=f72d76df5a1d4acc134e3cb03f61dec7d53eb18e;p=rust.git diff --git a/src/tools/miri/src/shims/unix/thread.rs b/src/tools/miri/src/shims/unix/thread.rs index 59474d8d10a..b43682710bb 100644 --- a/src/tools/miri/src/shims/unix/thread.rs +++ b/src/tools/miri/src/shims/unix/thread.rs @@ -67,10 +67,13 @@ fn pthread_self(&mut self) -> InterpResult<'tcx, Scalar> { Ok(Scalar::from_machine_usize(thread_id.into(), this)) } + /// Set the name of the current thread. `max_name_len` is the maximal length of the name + /// including the null terminator. fn pthread_setname_np( &mut self, thread: Scalar, name: Scalar, + max_name_len: usize, ) -> InterpResult<'tcx, Scalar> { let this = self.eval_context_mut(); @@ -78,11 +81,35 @@ fn pthread_setname_np( let name = name.to_pointer(this)?; let name = this.read_c_str(name)?.to_owned(); + + // Comparing with `>=` to account for null terminator. + if name.len() >= max_name_len { + return this.eval_libc("ERANGE"); + } + this.set_thread_name(thread, name); Ok(Scalar::from_u32(0)) } + fn pthread_getname_np( + &mut self, + thread: Scalar, + name_out: Scalar, + len: Scalar, + ) -> InterpResult<'tcx, Scalar> { + let this = self.eval_context_mut(); + + let thread = ThreadId::try_from(thread.to_machine_usize(this)?).unwrap(); + let name_out = name_out.to_pointer(this)?; + let len = len.to_machine_usize(this)?; + + let name = this.get_thread_name(thread).to_owned(); + let (success, _written) = this.write_c_str(&name, name_out, len)?; + + if success { Ok(Scalar::from_u32(0)) } else { this.eval_libc("ERANGE") } + } + fn sched_yield(&mut self) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut();