-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Start using windows sys
for Windows FFI bindings in std
#110152
Conversation
r? @thomcc (rustbot has picked a reviewer for you, use r? to override) |
7f9615f
to
18868df
Compare
(rebased on master branch) |
Definitely want to avoid using a hacked |
It's mostly just making the following the edits, which I believe could be done by using the pub type HANDLE = *mut ::core::ffi::c_void;
pub type FindFileHandle = *mut ::core::ffi::c_void;
pub type BCRYPT_ALG_HANDLE = *mut ::core::ffi::c_void;
pub type HMODULE = *mut ::core::ffi::c_void;
pub const INVALID_HANDLE_VALUE: HANDLE = ::core::ptr::invalid_mut(!0);
#[cfg(target_pointer_width = "32")]
pub type SOCKET = u32;
#[cfg(target_pointer_width = "64")]
pub type SOCKET = u64; The most hacky thing it does is change the way imports work to fit std's currently style. E.g.: #[link(name = "kernel32")]
extern "system" {
pub fn CloseHandle(hobject: HANDLE) -> BOOL;
pub fn GetLastError() -> WIN32_ERROR;
// etc,
} But I do hope we can move to raw-dylib. |
This comment has been minimized.
This comment has been minimized.
For the type changes, can we find some middle ground with the Win32 metadata or is there a technical reason you need these just so? I’d prefer that we don’t manage forked metadata in the short term as I’m working on some Rust-friendly metadata authoring support which will make this a lot easier in the long run. For the imports, is that because We could also plausibly add a |
For that matter, I'd be happy to discuss changes to |
To take the changes one by one... pub type HANDLE = *mut ::core::ffi::c_void;
pub type FindFileHandle = *mut ::core::ffi::c_void;
pub type BCRYPT_ALG_HANDLE = *mut ::core::ffi::c_void;
pub type HMODULE = *mut ::core::ffi::c_void;
pub const INVALID_HANDLE_VALUE: HANDLE = ::core::ptr::invalid_mut(!0); There are still some discussion about how handle types should be... handled. By keeping the status quo within the std I was hoping to avoid trying to force that issue at this time. I suspect it may be a blocker otherwise. #[cfg(target_pointer_width = "32")]
pub type SOCKET = u32;
#[cfg(target_pointer_width = "64")]
pub type SOCKET = u64; This is less of an issue but Rust stably defines the #[link(name = "kernel32")]
extern "system" {
pub fn CloseHandle(hobject: HANDLE) -> BOOL;
pub fn GetLastError() -> WIN32_ERROR;
// etc,
} The linking changes again are there to keep the status quo for now. Using |
For the handle types, I would like the metadata to actually store the true native type def rather than tip the scales in favor of C#. We'll likely get that resolved on the Rust side with the new metadata authoring support. Anyway, sounds like adding standalone flags to |
For sure! That sounds good to me. |
Take this for a spin: microsoft/windows-rs#2439 |
1fdcc6c
to
d84d939
Compare
Ok, I've updated this PR and the description to reflect the current code gen. This no longer uses a hacked windows-bindgen (see above) but a few things still differ between regular windows-sys and these bindings (see OP). |
This comment has been minimized.
This comment has been minimized.
The update looks great! Let me know when you need me to publish an update of the |
Sure thing. I think I'd want to wait for the next metadata update but there's no rush, I'm happy to let this sit for a little bit to give people time to comment, if anyone is inclined to do so. |
d84d939
to
ec5bed7
Compare
☔ The latest upstream changes (presumably #110331) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
ec5bed7
to
95a4e88
Compare
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #109133) made this pull request unmergeable. Please resolve the merge conflicts. |
Ok, I think with the latest metadata this is looking good to go. @kennykerr, A new release of |
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.
Sorry for the delay here, I've been occupied with family stuff.
This looks great and avoids most of the problems I'd normally complain about in something like this 😆. (In particular, windows-sys is heavyweight enough compared to what we need from it that I prefer this approach to a direct dep)
Only real nit is that IMO we should require the .lst file be sorted, (and verify that in CI). I'd accept an argument for why it's fine as-is if you feel really strongly, though.
@rustbot author |
I've sorted @rustbot ready |
Sorry if I'm blind, but I don't see a change to tidy. |
Tidy checks everything between |
Oh, my bad. Yeah, thanks @bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (7e7483d): comparison URL. Overall result: ✅ improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 656.874s -> 656.5s (-0.06%) |
It appears this merge has broken
It's unclear to me why doctest wants to build this Windows-specific file on a non-Windows platform (and why this apparently does not cause failures on any other non-Windows platform?). |
Yes that puzzled me too so I made a PR to remove it (#111401). Hopefully that should fix the issue. |
Switch to using windows-sys for FFI. In order to avoid some currently contentious issues, this uses windows-bindgen to generate a smaller set of bindings instead of using the full crate.
Unlike the windows-sys crate, the generated bindings uses
*mut c_void
for handle types instead ofisize
. This to sidestep opsem concerns about mixing pointer types and integers between languages. Note thatSOCKET
remains defined as an integer but instead of being a usize, it's changed to fit the standard library definition:The generated bindings also customizes the
#[link]
imports. I hope to switch to using raw-dylib but I don't want to tie that too closely with the switch to windows-sys.Changes outside of the bindings are, for the most part, fairly minimal (e.g. some differences in
*mut
vs.*const
or a few types differ). One issue is that our own bindings sometimes mix in higher level types, likeBorrowedHandle
. This is pretty adhoc though.