Skip to content

Commit dac2412

Browse files
committed
account for different max thread name lengths on different platforms
1 parent 70087ea commit dac2412

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

src/tools/miri/src/shims/unix/freebsd/foreign_items.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
2626
"pthread_set_name_np" => {
2727
let [thread, name] =
2828
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
29-
let res =
30-
this.pthread_setname_np(this.read_scalar(thread)?, this.read_scalar(name)?)?;
29+
let max_len = usize::MAX; // freebsd does not seem to have a limit.
30+
let res = this.pthread_setname_np(
31+
this.read_scalar(thread)?,
32+
this.read_scalar(name)?,
33+
max_len,
34+
)?;
3135
this.write_scalar(res, dest)?;
3236
}
3337

src/tools/miri/src/shims/unix/linux/foreign_items.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
6868
"pthread_setname_np" => {
6969
let [thread, name] =
7070
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
71-
let res =
72-
this.pthread_setname_np(this.read_scalar(thread)?, this.read_scalar(name)?)?;
71+
let max_len = 16;
72+
let res = this.pthread_setname_np(
73+
this.read_scalar(thread)?,
74+
this.read_scalar(name)?,
75+
max_len,
76+
)?;
7377
this.write_scalar(res, dest)?;
7478
}
7579
"pthread_getname_np" => {

src/tools/miri/src/shims/unix/macos/foreign_items.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
176176
"pthread_setname_np" => {
177177
let [name] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
178178
let thread = this.pthread_self()?;
179-
this.pthread_setname_np(thread, this.read_scalar(name)?)?;
179+
let max_len = this.eval_libc("MAXTHREADNAMESIZE")?.to_machine_usize(this)?;
180+
this.pthread_setname_np(
181+
thread,
182+
this.read_scalar(name)?,
183+
max_len.try_into().unwrap(),
184+
)?;
180185
}
181186
"pthread_getname_np" => {
182187
let [thread, name, len] =

src/tools/miri/src/shims/unix/thread.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
6767
Ok(Scalar::from_machine_usize(thread_id.into(), this))
6868
}
6969

70+
/// Set the name of the current thread. `max_name_len` is the maximal length of the name
71+
/// including the null terminator.
7072
fn pthread_setname_np(
7173
&mut self,
7274
thread: Scalar<Provenance>,
7375
name: Scalar<Provenance>,
76+
max_name_len: usize,
7477
) -> InterpResult<'tcx, Scalar<Provenance>> {
7578
let this = self.eval_context_mut();
7679

@@ -79,8 +82,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
7982

8083
let name = this.read_c_str(name)?.to_owned();
8184

82-
if name.len() > 15 {
83-
// Thread names are limited to 16 characaters, including the null terminator.
85+
// Comparing with `>=` to account for null terminator.
86+
if name.len() >= max_name_len {
8487
return this.eval_libc("ERANGE");
8588
}
8689

0 commit comments

Comments
 (0)