diff --git a/src/tools/miri/src/shims/unix/freebsd/foreign_items.rs b/src/tools/miri/src/shims/unix/freebsd/foreign_items.rs index 70798f98174..d755e5f10ba 100644 --- a/src/tools/miri/src/shims/unix/freebsd/foreign_items.rs +++ b/src/tools/miri/src/shims/unix/freebsd/foreign_items.rs @@ -26,8 +26,12 @@ fn emulate_foreign_item_by_name( "pthread_set_name_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)?, this.read_scalar(name)?)?; + let max_len = usize::MAX; // freebsd does not seem to have a limit. + let res = this.pthread_setname_np( + this.read_scalar(thread)?, + this.read_scalar(name)?, + max_len, + )?; this.write_scalar(res, dest)?; } diff --git a/src/tools/miri/src/shims/unix/linux/foreign_items.rs b/src/tools/miri/src/shims/unix/linux/foreign_items.rs index c004e2292a9..2b53152688b 100644 --- a/src/tools/miri/src/shims/unix/linux/foreign_items.rs +++ b/src/tools/miri/src/shims/unix/linux/foreign_items.rs @@ -68,8 +68,12 @@ fn emulate_foreign_item_by_name( "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)?, this.read_scalar(name)?)?; + let max_len = 16; + let res = this.pthread_setname_np( + this.read_scalar(thread)?, + this.read_scalar(name)?, + max_len, + )?; this.write_scalar(res, dest)?; } "pthread_getname_np" => { diff --git a/src/tools/miri/src/shims/unix/macos/foreign_items.rs b/src/tools/miri/src/shims/unix/macos/foreign_items.rs index 0e931023e6c..371f56ca355 100644 --- a/src/tools/miri/src/shims/unix/macos/foreign_items.rs +++ b/src/tools/miri/src/shims/unix/macos/foreign_items.rs @@ -176,7 +176,12 @@ fn emulate_foreign_item_by_name( "pthread_setname_np" => { let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; let thread = this.pthread_self()?; - this.pthread_setname_np(thread, this.read_scalar(name)?)?; + let max_len = this.eval_libc("MAXTHREADNAMESIZE")?.to_machine_usize(this)?; + this.pthread_setname_np( + thread, + this.read_scalar(name)?, + max_len.try_into().unwrap(), + )?; } "pthread_getname_np" => { let [thread, name, len] = diff --git a/src/tools/miri/src/shims/unix/thread.rs b/src/tools/miri/src/shims/unix/thread.rs index 4320ecd389e..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(); @@ -79,8 +82,8 @@ fn pthread_setname_np( let name = this.read_c_str(name)?.to_owned(); - if name.len() > 15 { - // Thread names are limited to 16 characaters, including the null terminator. + // Comparing with `>=` to account for null terminator. + if name.len() >= max_name_len { return this.eval_libc("ERANGE"); }