Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tid argument in sched_(get/set)affinity #4130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/shims/unix/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,17 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let cpusetsize = this.read_target_usize(cpusetsize)?;
let mask = this.read_pointer(mask)?;

// TODO: when https://github.com/rust-lang/miri/issues/3730 is fixed this should use its notion of tid/pid
let thread_id = match pid {
0 => this.active_thread(),
let thread_id = match (pid, &*this.tcx.sess.target.os) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit odd to match on the pair here. I think this is more clear as

if pid == 0 { ... }
else if this.tcx.sess.target.os == "linux" {
  // On Linux we support `gettid`, which is guaranteed to support IDs matching `sched_getaffinity`.
  ...
} else { ... }

(0, _) => this.active_thread(),
(tid, "linux") => {
if let Ok(tid) = this.machine.threads.thread_id_try_from(tid) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not return the right ID. See the gettid implementation to see how ThreadId is converted to the user-visible TID; this here will have to do the opposite transformation. Please put both directions in some shared place to make sure they remain consistent in the future.

tid
} else {
this.set_last_error_and_return(LibcError("ESRCH"), dest)?;

return interp_ok(EmulateItemResult::NeedsReturn);
}
},
_ => throw_unsup_format!("`sched_getaffinity` is only supported with a pid of 0 (indicating the current thread)"),
};

Expand Down Expand Up @@ -717,9 +725,17 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let cpusetsize = this.read_target_usize(cpusetsize)?;
let mask = this.read_pointer(mask)?;

// TODO: when https://github.com/rust-lang/miri/issues/3730 is fixed this should use its notion of tid/pid
let thread_id = match pid {
0 => this.active_thread(),
let thread_id = match (pid, &*this.tcx.sess.target.os) {
(0, _) => this.active_thread(),
(tid, "linux") => {
if let Ok(tid) = this.machine.threads.thread_id_try_from(tid) {
tid
} else {
this.set_last_error_and_return(LibcError("ESRCH"), dest)?;

return interp_ok(EmulateItemResult::NeedsReturn);
}
},
_ => throw_unsup_format!("`sched_setaffinity` is only supported with a pid of 0 (indicating the current thread)"),
};

Expand Down
Loading