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

freebsd interceptions update proposal #3143

Merged
merged 3 commits into from
Nov 12, 2023
Merged

Conversation

devnexen
Copy link
Contributor

No description provided.

@RalfJung
Copy link
Member

Thanks for the PR!

However, new functions without tests are generally not a good idea. Why is the pthread_setname_np alias needed? And for pthread_get_name_np, we have a test in tests/pass-dep/shims/pthreads.rs. You could split out the thread name part of the test and enable it on freebsd (by editing ci.sh).

@devnexen devnexen force-pushed the fbsd_update branch 9 times, most recently from 3995215 to 7b9fc06 Compare October 27, 2023 06:49
let [thread, name] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let max_len = usize::MAX; // freebsd does not seem to have a limit.
let max_len = 20; // freebsd silently truncates to MAXCOMLEN (+ 1).
Copy link
Member

Choose a reason for hiding this comment

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

And MAXCOMLEN is 19 then, I assume?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

Copy link
Member

Choose a reason for hiding this comment

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

Is there somewhere you can link to that documents this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Okay, please add those links to the code. :)

libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast())
};
#[cfg(target_os = "freebsd")]
return 0;
Copy link
Member

Choose a reason for hiding this comment

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

Why do you make it return 0? With a too long name it should fail, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pthread_set_name_np functions differently than Linux's. The only errno returned to userland is ESRCH.

Copy link
Member

Choose a reason for hiding this comment

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

That's not what you implemented though. In src/shims/unix/freebsd/foreign_items.rs you're returning errors the same way Linux would.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestions.

#[cfg(target_os = "freebsd")]
unsafe {
libc::pthread_get_name_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
};
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
assert!(cstr.to_bytes().len() >= 15); // POSIX seems to promise at least 15 chars
Copy link
Member

Choose a reason for hiding this comment

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

This assertion fails. Does it also fail locally when you do ./miri run --target x86_64-unknown-freebsd tests/pass-dep/shims/pthreads.rs? If yes then you have a shot at debugging this. Having the assert! print the actual length on failure would probably be a good start.

Suggested change
assert!(cstr.to_bytes().len() >= 15); // POSIX seems to promise at least 15 chars
assert!(cstr.to_bytes().len() >= 15, "name is too short: len={}", cstr.to_bytes().len()); // POSIX seems to promise at least 15 chars

let [thread, name] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let max_len = usize::MAX; // freebsd does not seem to have a limit.
let max_len = 20; // freebsd silently truncates to MAXCOMLEN (+ 1).
let res = this.pthread_setname_np(
this.read_scalar(thread)?,
this.read_scalar(name)?,
max_len,
)?;
this.write_scalar(res, dest)?;
Copy link
Member

Choose a reason for hiding this comment

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

I'm very surprised that this does not explode. Something is weird. This function returns nothing, and here we are assuming it returns an i32, so that should fail somewhere...

@@ -26,17 +26,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
this.write_null(dest)?;
}
"pthread_set_name_np" => {
// Old vs new name
"pthread_set_name_np" | "pthread_setname_np" => {
Copy link
Member

Choose a reason for hiding this comment

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

According to the manpage these are not the same, they have different return types. So the implementation will also have to be different, and both variants need to be tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact I ll remove the new names for now and focus on the current ones, not even libc crate have them.

@devnexen devnexen force-pushed the fbsd_update branch 3 times, most recently from 809057d to ab5ae00 Compare October 27, 2023 23:03
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.

Sorry for the silence, I didn't realize this was ready for another round of review. Please don't just push and assume we'll get notified; github does not always send notifications for force pushes and it's never clear whether such a push is meant to resolve all prior discussions or whether more pushes will come.

@rustbot author
(write @rustbot review when this is ready for review again)

@@ -26,16 +26,26 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
this.write_null(dest)?;
}
// Old vs new name
Copy link
Member

Choose a reason for hiding this comment

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

This comment seems outdated?

src/shims/unix/freebsd/foreign_items.rs Show resolved Hide resolved
src/shims/unix/freebsd/foreign_items.rs Show resolved Hide resolved
test_mutex_libc_init_errorcheck();
#[cfg(not(target_os = "freebsd"))]
test_rwlock_libc_static_initializer();
test_named_thread_truncation();
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
test_named_thread_truncation();
test_named_thread_truncation();

Comment on lines 151 to 158
unsafe {
libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast())
};
#[cfg(target_os = "freebsd")]
return 0;
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
unsafe {
libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast())
};
#[cfg(target_os = "freebsd")]
return 0;
unsafe {
libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast());
// pthread_set_name_np does not return anything, just assume it went well.
return 0;
};

let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
assert!(cstr.to_bytes().len() >= 15); // POSIX seems to promise at least 15 chars
assert!(cstr.to_bytes().len() >= 15, "name is too short: len={}", cstr.to_bytes().len()); // POSIX seems to promise at least 15 chars
assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));

// Also test directly calling pthread_setname to check its return value.
assert_eq!(set_thread_name(&cstr), 0);
// But with a too long name it should fail.
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
// But with a too long name it should fail.
// But with a too long name it should fail (except on FreeBSD where the function
// has no return value and hence cannot indicate failure).

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments labels Nov 12, 2023
@devnexen devnexen force-pushed the fbsd_update branch 2 times, most recently from 2c6a1df to beacc3c Compare November 12, 2023 11:13
@devnexen
Copy link
Contributor Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Nov 12, 2023
@RalfJung
Copy link
Member

Looking good, thanks! I just did some tweaks.

@bors r+

@bors
Copy link
Contributor

bors commented Nov 12, 2023

📌 Commit 19933b9 has been approved by RalfJung

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Nov 12, 2023

⌛ Testing commit 19933b9 with merge 485779a...

bors added a commit that referenced this pull request Nov 12, 2023
freebsd interceptions update proposal
@RalfJung
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Nov 12, 2023

📌 Commit 195776b has been approved by RalfJung

It is now in the queue for this repository.

@RalfJung
Copy link
Member

@bors force

@bors
Copy link
Contributor

bors commented Nov 12, 2023

⌛ Testing commit 195776b with merge c30f30c...

@bors
Copy link
Contributor

bors commented Nov 12, 2023

☀️ Test successful - checks-actions
Approved by: RalfJung
Pushing c30f30c to master...

@bors bors merged commit c30f30c into rust-lang:master Nov 12, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Waiting for a review to complete
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants