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

Only register WSACleanup if WSAStartup is actually ever called #85595

Closed
wants to merge 2 commits into from

Conversation

CDirkx
Copy link
Contributor

@CDirkx CDirkx commented May 23, 2021

Fix for #85441.

Because WSACleanup appears in cleanup currently WS2_32.dll is always linked, even if no network functionality is ever used.
To prevent this, WSACleanup has to only appear in init, hence the workaround of registering it in a static.

If anyone knows a cleaner solution, let me know.

@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 23, 2021
@@ -28,6 +28,10 @@ pub struct Socket(c::SOCKET);

static INIT: Once = Once::new();

// Workaround to prevent linking to `WS2_32.dll` when no network functionality is used.
// See issue #85441.
static mut CLEANUP: Option<unsafe extern "system" fn() -> i32> = None;
Copy link
Member

@nagisa nagisa May 23, 2021

Choose a reason for hiding this comment

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

Consider using SyncLazy/SyncOnceCell for the INIT.

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 combined the two statics in a SyncOnceCell.

@nagisa
Copy link
Member

nagisa commented May 23, 2021

This seems okay to me. Is there any way a test could be written for this?

@klensy
Copy link
Contributor

klensy commented May 23, 2021

This seems okay to me. Is there any way a test could be written for this?

dumpbin.exe /dependents empty.exe

For example from issue, output will be:

Microsoft (R) COFF/PE Dumper Version 14.28.29914.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file empty.exe

File Type: EXECUTABLE IMAGE

  Image has the following dependencies:

    WS2_32.dll
    KERNEL32.dll
    VCRUNTIME140.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-math-l1-1-0.dll
    api-ms-win-crt-stdio-l1-1-0.dll
    api-ms-win-crt-locale-l1-1-0.dll
    api-ms-win-crt-heap-l1-1-0.dll

  Summary

        1000 .data
        1000 .pdata
        7000 .rdata
        1000 .reloc
       19000 .text

we can grep output, but this is ugly.

@CDirkx
Copy link
Contributor Author

CDirkx commented May 23, 2021

I could add a run-make test to inspect a produced binary, however I don't know what tool to do that with: Is dumbbin.exe available on CI? Is there an already available tool that can show loaded dlls?

@klensy
Copy link
Contributor

klensy commented May 23, 2021

dumpbin is visual studio component, which included in github runner, i guess, but maybe there already some tools used for checking linked libraries\symbols in tests.

@klensy
Copy link
Contributor

klensy commented May 23, 2021

Test no-dllimport-w-cross-lang-lto looks promising, perhaps we can check that ws2_32 symbols not exist in exe.

@CDirkx
Copy link
Contributor Author

CDirkx commented May 24, 2021

I added a run-make test using objdump which I believe should be available on CI as well.

@klensy
Copy link
Contributor

klensy commented Jun 6, 2021

Works, at least under x86_64-pc-windows-msvc.

@nagisa
Copy link
Member

nagisa commented Jun 6, 2021

r? @nagisa
@bors r+

Thanks!

@bors
Copy link
Contributor

bors commented Jun 6, 2021

📌 Commit f798596 has been approved by nagisa

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 6, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jun 6, 2021
Only register `WSACleanup` if `WSAStartup` is actually ever called

Fix for rust-lang#85441.

Because `WSACleanup` appears in `cleanup` currently `WS2_32.dll` is always linked, even if no network functionality is ever used.
To prevent this, `WSACleanup` has to only appear in `init`, hence the workaround of registering it in a static.

If anyone knows a cleaner solution, let me know.
@GuillaumeGomez
Copy link
Member

Failed in #86079.

@bors: r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 6, 2021
@CDirkx
Copy link
Contributor Author

CDirkx commented Jun 7, 2021

Good news, using objdump works on CI. Still figuring out how to reproduce the failure under x86_64-pc-windows-gnu.

@CDirkx

This comment has been minimized.

@rustbot rustbot added the O-windows Operating system: Windows label Jun 23, 2021
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jun 23, 2021
@crlf0710
Copy link
Member

crlf0710 commented Jul 9, 2021

@CDirkx Ping from triage, any updates on this?

@CDirkx
Copy link
Contributor Author

CDirkx commented Jul 9, 2021

I had trouble getting the tests to run under x86_64-pc-windows-gnu on my machine in order to debug the problem on that platform. I'll try again this weekend though.

@JohnCSimon JohnCSimon 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 25, 2021
@@ -26,26 +26,31 @@ pub mod netc {

pub struct Socket(c::SOCKET);

static INIT: Once = Once::new();
static WSA: SyncOnceCell<unsafe extern "system" fn() -> i32> = SyncOnceCell::new();
Copy link
Member

Choose a reason for hiding this comment

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

Could this be named WSA_CLEANUP? Or otherwise wrap the fn pointer in a struct - as it stands, the WSA name doesn't make it obvious what the fn pointer is supposed to be.

@camelid camelid 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 29, 2021
@JohnCSimon
Copy link
Member

Ping from triage:
@CDirkx Can you please post your status on this PR?

@nagisa
Copy link
Member

nagisa commented Oct 2, 2021

I looked at the build failure. I believe this is likely related to the fact that the gnu targets have function_sections disabled

// FIXME(#13846) this should be enabled for windows
function_sections: false,
thus preventing the linker from removing the references to various networking calls, which then pull in the undesirable dll.

I don't think re-enabling the function sections is going to be feasible, so we should only run that test on -msvc targets...

Since Christiaan seems to be away since at least July, I'm going to close this and open a fixed PR.

@mati865
Copy link
Contributor

mati865 commented Oct 2, 2021

FWIW I think it's not an issue with -ffunction-sections but rather issue with -Wl,--gc-sections which is buggy in binutils when targetting MinGW.
function_sections: true works fine when using LLVM toolchain for MinGW.

Manishearth added a commit to Manishearth/rust that referenced this pull request Oct 4, 2021
Only register `WSACleanup` if `WSAStartup` is actually ever called

See rust-lang#85595

Fixes rust-lang#85441
Manishearth added a commit to Manishearth/rust that referenced this pull request Oct 4, 2021
Only register `WSACleanup` if `WSAStartup` is actually ever called

See rust-lang#85595

Fixes rust-lang#85441
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-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.