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

shim Apple's futex primitives #4142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

joboet
Copy link
Member

@joboet joboet commented Jan 19, 2025

This is necessary to unblock rust-lang/rust#122408. The documentation for these is available here.

Because the futex wait operations (os_sync_wait_on_address et al.) return the number of remaining waiters after returning, this required some changes to the common futex infrastructure, which I've changed to take a callback instead of precalculating the return values.

This is necessary to unblock rust-lang/rust#122408. The documentation for these is available [here](https://developer.apple.com/documentation/os/os_sync_wait_on_address?language=objc).

Because the futex wait operations (`os_sync_wait_on_address` et al.) return the number of remaining waiters after returning, this required some changes to the common futex infrastructure, which I've changed to take a callback instead of precalculating the return values.
@RalfJung
Copy link
Member

Thanks for the PR! I just left for a 1-week business trip and will have to catch up with stuff when I am back so it could take a bit before I can take a closer look.

@rustbot
Copy link
Collaborator

rustbot commented Jan 29, 2025

☔ The latest upstream changes (possibly 489a487) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot rustbot added the S-waiting-on-author Status: Waiting for the PR author to address review comments label Jan 29, 2025
Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Thanks a lot! Overall approach looks great, but I have a bunch of questions and comments. :)

Comment on lines +738 to +741
/// * On a signal, `retval_succ` is called with the number of waiters on the
/// futex and its result is written to `dest`.
/// * On a timeout, `retval_timeout` is written to `dest` and `errno_timeout`
/// is set as the last error.
Copy link
Member

Choose a reason for hiding this comment

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

Most of the variables referenced here do not exist any more.

}
|ecx, unblock: UnblockKind| match unblock {
UnblockKind::Ready => {
ecx.write_scalar(Scalar::from_target_isize(0, ecx), &dest)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ecx.write_scalar(Scalar::from_target_isize(0, ecx), &dest)
ecx.write_int(0, &dest)

|this, unblock: UnblockKind| {
match unblock {
UnblockKind::Ready => {
this.write_scalar(Scalar::from_i32(1), &dest)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
this.write_scalar(Scalar::from_i32(1), &dest)
this.write_int(1, &dest)

}
UnblockKind::TimedOut => {
this.set_last_error(IoError::WindowsError("ERROR_TIMEOUT"))?;
this.write_scalar(Scalar::from_i32(0), &dest)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
this.write_scalar(Scalar::from_i32(0), &dest)
this.write_int(0, &dest)

@@ -216,6 +224,53 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.write_scalar(res, dest)?;
}

"os_sync_wait_on_address" => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"os_sync_wait_on_address" => {
// Futex primitives
"os_sync_wait_on_address" => {

fn wake_nobody() {
let futex = 0;

// Wake 1 waiter. Expect zero waiters woken up, as nobody is waiting.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Wake 1 waiter. Expect zero waiters woken up, as nobody is waiting.
// Wake 1 waiter. Expect ENOENT, as nobody is waiting.

),
-1
);
assert_eq!(io::Error::last_os_error().raw_os_error().unwrap(), libc::ENOENT);
Copy link
Member

Choose a reason for hiding this comment

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

So this is the same error as "nobody is waiting"? Interesting.

Comment on lines +90 to +92
// Add 200ms (note: this only works under miri because it uses nanosecond units for
// mach_absolute_time).
deadline += 200_000_000;
Copy link
Member

Choose a reason for hiding this comment

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

This sounds like we are doing something wrong, then?

t.join().unwrap();
}

fn param_mismatch() {
Copy link
Member

Choose a reason for hiding this comment

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

Please add comments pointing out what the mismatch is in each case.

size_of::<i32>(),
libc::OS_SYNC_WAIT_ON_ADDRESS_NONE,
),
0,
Copy link
Member

Choose a reason for hiding this comment

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

Please add a test where wait_on_address returns something non-zero (since there are more waiters).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: Waiting for the PR author to address review comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants