account for different max thread name lengths on different platforms

This commit is contained in:
Ralf Jung 2022-10-26 10:50:11 +02:00
parent 70087eaa35
commit dac2412890
4 changed files with 23 additions and 7 deletions

View File

@ -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)?;
}

View File

@ -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" => {

View File

@ -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] =

View File

@ -67,10 +67,13 @@ fn pthread_self(&mut self) -> InterpResult<'tcx, Scalar<Provenance>> {
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<Provenance>,
name: Scalar<Provenance>,
max_name_len: usize,
) -> InterpResult<'tcx, Scalar<Provenance>> {
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");
}