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

Abstract ProcThreadAttributeList into its own struct #123604

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

Conversation

michaelvanstraten
Copy link
Contributor

@michaelvanstraten michaelvanstraten commented Apr 7, 2024

As extensively discussed in issue #114854, the current implementation of the unstable windows_process_extensions_raw_attribute features lacks support for passing a raw pointer.

This PR wants to explore the opportunity to abstract away the ProcThreadAttributeList into its own struct to for one improve safety and usability and secondly make it possible to maybe also use it to spawn new threads.

try-job: x86_64-mingw

@rustbot
Copy link
Collaborator

rustbot commented Apr 7, 2024

r? @ChrisDenton

rustbot has assigned @ChrisDenton.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 7, 2024
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment on lines 16 to 17
#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
pub use sys::process::{ProcThreadAttributeList, ProcThreadAttributeListBuilder};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't exactly know the right way to export this here. @Dylan-DPC can you guide me on this?

Copy link
Member

Choose a reason for hiding this comment

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

As with the imports above, you need to start the import with crate::. But see my review comment.

@michaelvanstraten michaelvanstraten marked this pull request as ready for review May 4, 2024 11:50
@michaelvanstraten
Copy link
Contributor Author

r? @Dylan-DPC

@rustbot rustbot assigned Dylan-DPC and unassigned ChrisDenton May 4, 2024
@Dylan-DPC
Copy link
Member

Assigning it back to Chris giving them more time to review it :) (and well i am not the right person to review it :P)

r? @ChrisDenton

@rustbot rustbot assigned ChrisDenton and unassigned Dylan-DPC May 4, 2024
Copy link
Member

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

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

The public struct should be defined and documented in std/src/os. Internally it can just be a simple wrapper around the std/src/sys types but nothing in sys should be directly exposed to users.

Comment on lines 16 to 17
#[unstable(feature = "windows_process_extensions_raw_attribute", issue = "114854")]
pub use sys::process::{ProcThreadAttributeList, ProcThreadAttributeListBuilder};
Copy link
Member

Choose a reason for hiding this comment

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

As with the imports above, you need to start the import with crate::. But see my review comment.

/// # Safety
///
/// This function is marked as `unsafe` because it deals with raw pointers and sizes.
/// It is the responsibility of the caller to ensure the value lives longer than the
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
/// It is the responsibility of the caller to ensure the value lives longer than the
/// It is the responsibility of the caller to ensure the value pointed to by the `value_ptr` lives longer than the

///
/// This function is marked as `unsafe` because it deals with raw pointers and sizes.
/// It is the responsibility of the caller to ensure the value lives longer than the
/// [`ProcThreadAttributeListBuilder`] as well as the validity of the size parameter.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
/// [`ProcThreadAttributeListBuilder`] as well as the validity of the size parameter.
/// [`ProcThreadAttributeList`] as well as the validity of the size parameter.

si_ex = c::STARTUPINFOEXW {
StartupInfo: si,
lpAttributeList: proc_thread_attribute_list.0.as_mut_ptr() as _,
lpAttributeList: proc_thread_attribute_list.as_mut_ptr().cast::<c_void>(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't really understand why the win API require a *mut to the attribute list here.

Copy link
Member

Choose a reason for hiding this comment

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

Because the C headers do not distinguish. between const and mut so, when automatically generating the bindings, mut is the most reasonable option. You can open an issue on the win32metadata repo if you'd like them to manually add some metadata so the Rust bindings know which one to use.

Though it doesn't matter too much. In rust, const and mut on pointers is just a lint. You can do, e.g. proc_thread_attribute_list.as_ptr().cast::<c_void>().cast_mut() to go from a reference to a pointer.

Copy link
Contributor Author

@michaelvanstraten michaelvanstraten May 19, 2024

Choose a reason for hiding this comment

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

I thought that was highly illegal because of some llvm attributes that rustc sets when dealing with shared references.

Copy link
Member

Choose a reason for hiding this comment

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

Pointers and references are different. You cannot turn a & into an &mut. You can turn an & into a *mut so long as you don't ever write to the memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, my concern was that with the win api having an interface that explicitly expects a *mut I thought it might be the case that they actually write to it, but good to know. I will add a safety comment and open an issue over at the win32 crate repo.

@bors
Copy link
Contributor

bors commented May 19, 2024

☔ The latest upstream changes (presumably #125280) made this pull request unmergeable. Please resolve the merge conflicts.

@michaelvanstraten
Copy link
Contributor Author

michaelvanstraten commented May 22, 2024

@rustbot label +S-waiting-on-author -S-waiting-on-review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 22, 2024
@michaelvanstraten
Copy link
Contributor Author

The public struct should be defined and documented in std/src/os. Internally it can just be a simple wrapper around the std/src/sys types but nothing in sys should be directly exposed to users.

Would https://github.com/rust-lang/rust/blob/master/library/std/src/os/windows/process.rs, even be the right location for it considering it can be used to create a thread? Maybe std/src/os/windows/attribute_list.rs would be appropriate?

@michaelvanstraten
Copy link
Contributor Author

What I don't understand is that there exists a CreateRemoteThreadEx but no CreateThreadEx, or at least I could not find one. The remote-thread function is probably uninteresting to the standard library, while things such as setting the processor group affinity are.

Is it possible to create a thread for the current process with CreateRemoteThreadEx?

@tim-weis
Copy link

Is it possible to create a thread for the current process with CreateRemoteThreadEx?

Yes. Indeed, the OS doesn't expose a special function to create a thread in the current process. NtCreateThread() always requires a process handle to be supplied, which closely resembles the Win32 API's CreateRemoteThread[Ex]() function. The Win32 API's CreateThread() function is a convenience that passes the (pseudo-)handle returned by GetCurrentProcess() to the Native API to create a "remote" thread in the process of the calling thread.

@michaelvanstraten
Copy link
Contributor Author

@ChrisDenton fourth time is the charm.

@ChrisDenton
Copy link
Member

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 10, 2024
…ute_list, r=<try>

Abstract `ProcThreadAttributeList` into its own struct

As extensively discussed in issue rust-lang#114854, the current implementation of the unstable `windows_process_extensions_raw_attribute` features lacks support for passing a raw pointer.

This PR wants to explore the opportunity to abstract away the `ProcThreadAttributeList` into its own struct to for one improve safety and usability and secondly make it possible to maybe also use it to spawn new threads.

try-job: x86_64-mingw
@bors
Copy link
Contributor

bors commented Sep 10, 2024

⌛ Trying commit 9d0a42c with merge 7d87654...

@michaelvanstraten
Copy link
Contributor Author

If this should fail, I will set up a Windows machine to run the doc-tests locally.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Sep 10, 2024

💔 Test failed - checks-actions

@michaelvanstraten
Copy link
Contributor Author

Okay, let me fix this locally before I waste more CI time.

@michaelvanstraten michaelvanstraten force-pushed the proc_thread_attribute_list branch 4 times, most recently from d9e74ac to 47927bd Compare September 10, 2024 15:09
@michaelvanstraten
Copy link
Contributor Author

@ChrisDenton, looks good on my local win11 build. I think you can push to try again once more.

@ChrisDenton
Copy link
Member

@bors try

@bors
Copy link
Contributor

bors commented Sep 11, 2024

⌛ Trying commit 47927bd with merge c5b2675...

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 11, 2024
…ute_list, r=<try>

Abstract `ProcThreadAttributeList` into its own struct

As extensively discussed in issue rust-lang#114854, the current implementation of the unstable `windows_process_extensions_raw_attribute` features lacks support for passing a raw pointer.

This PR wants to explore the opportunity to abstract away the `ProcThreadAttributeList` into its own struct to for one improve safety and usability and secondly make it possible to maybe also use it to spawn new threads.

try-job: x86_64-mingw
@bors
Copy link
Contributor

bors commented Sep 11, 2024

☀️ Try build successful - checks-actions
Build commit: c5b2675 (c5b26757a5741cdad535e0266119ca93331b8f22)

@michaelvanstraten
Copy link
Contributor Author

@ChrisDenton is there something I should do?

@michaelvanstraten
Copy link
Contributor Author

@ChrisDenton, I've resolved the two comments I made my self, from my perspective this should be ready to be merged.

@rust-log-analyzer

This comment has been minimized.

@michaelvanstraten
Copy link
Contributor Author

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants