-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Safely enforce thread name requirements #127918
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -161,7 +161,7 @@ mod tests; | |||||||||||||
use crate::any::Any; | ||||||||||||||
use crate::cell::{OnceCell, UnsafeCell}; | ||||||||||||||
use crate::env; | ||||||||||||||
use crate::ffi::{CStr, CString}; | ||||||||||||||
use crate::ffi::CStr; | ||||||||||||||
use crate::fmt; | ||||||||||||||
use crate::io; | ||||||||||||||
use crate::marker::PhantomData; | ||||||||||||||
|
@@ -487,11 +487,7 @@ impl Builder { | |||||||||||||
amt | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
let my_thread = name.map_or_else(Thread::new_unnamed, |name| unsafe { | ||||||||||||||
Thread::new( | ||||||||||||||
CString::new(name).expect("thread name may not contain interior null bytes"), | ||||||||||||||
) | ||||||||||||||
}); | ||||||||||||||
let my_thread = name.map_or_else(Thread::new_unnamed, |name| Thread::new(name.into())); | ||||||||||||||
let their_thread = my_thread.clone(); | ||||||||||||||
|
||||||||||||||
let my_packet: Arc<Packet<'scope, T>> = Arc::new(Packet { | ||||||||||||||
|
@@ -1273,10 +1269,34 @@ impl ThreadId { | |||||||||||||
/// The internal representation of a `Thread`'s name. | ||||||||||||||
enum ThreadName { | ||||||||||||||
Main, | ||||||||||||||
Other(CString), | ||||||||||||||
Other(ThreadNameString), | ||||||||||||||
Unnamed, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// This module ensures private fields are kept private, which is necessary to enforce the safety requirements. | ||||||||||||||
mod thread_name_string { | ||||||||||||||
use crate::ffi::{CStr, CString}; | ||||||||||||||
|
||||||||||||||
/// Like a `String` it's guaranteed UTF-8 and like a `CString` it's null terminated. | ||||||||||||||
pub(crate) struct ThreadNameString { | ||||||||||||||
inner: CString, | ||||||||||||||
} | ||||||||||||||
impl core::ops::Deref for ThreadNameString { | ||||||||||||||
type Target = CStr; | ||||||||||||||
fn deref(&self) -> &CStr { | ||||||||||||||
&self.inner | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
impl From<String> for ThreadNameString { | ||||||||||||||
fn from(s: String) -> Self { | ||||||||||||||
Self { | ||||||||||||||
inner: CString::new(s).expect("thread name may not contain interior null bytes"), | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
pub(crate) use thread_name_string::ThreadNameString; | ||||||||||||||
|
||||||||||||||
/// The internal representation of a `Thread` handle | ||||||||||||||
struct Inner { | ||||||||||||||
name: ThreadName, // Guaranteed to be UTF-8 | ||||||||||||||
|
@@ -1316,25 +1336,20 @@ pub struct Thread { | |||||||||||||
|
||||||||||||||
impl Thread { | ||||||||||||||
/// Used only internally to construct a thread object without spawning. | ||||||||||||||
/// | ||||||||||||||
/// # Safety | ||||||||||||||
/// `name` must be valid UTF-8. | ||||||||||||||
pub(crate) unsafe fn new(name: CString) -> Thread { | ||||||||||||||
unsafe { Self::new_inner(ThreadName::Other(name)) } | ||||||||||||||
pub(crate) fn new(name: ThreadNameString) -> Thread { | ||||||||||||||
Self::new_inner(ThreadName::Other(name)) | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
pub(crate) fn new_unnamed() -> Thread { | ||||||||||||||
unsafe { Self::new_inner(ThreadName::Unnamed) } | ||||||||||||||
Self::new_inner(ThreadName::Unnamed) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// Used in runtime to construct main thread | ||||||||||||||
pub(crate) fn new_main() -> Thread { | ||||||||||||||
unsafe { Self::new_inner(ThreadName::Main) } | ||||||||||||||
Self::new_inner(ThreadName::Main) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// # Safety | ||||||||||||||
/// If `name` is `ThreadName::Other(_)`, the contained string must be valid UTF-8. | ||||||||||||||
unsafe fn new_inner(name: ThreadName) -> Thread { | ||||||||||||||
fn new_inner(name: ThreadName) -> Thread { | ||||||||||||||
// We have to use `unsafe` here to construct the `Parker` in-place, | ||||||||||||||
// which is required for the UNIX implementation. | ||||||||||||||
// | ||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.