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

Use Default visibility for rustc-generated C symbol declarations #123994

Closed
wants to merge 1 commit into from

Conversation

chbaker0
Copy link
Contributor

Previously, visibility for these symbols was determined by the default-hidden-visibility target option or the presence of -Zdefault-hidden-visibility. This leads to issue #123427, where use of the flag leads to undefined hidden symbols (i.e., references that can never be resolved to an exported symbol from another shared library) for functions often provided by a platform shared library, such as memcpy and memcmp from libc.so.

References to symbols provided by shared libraries must have default visibility. Hidden visibility is mostly useful for defined symbols.

@rustbot
Copy link
Collaborator

rustbot commented Apr 16, 2024

r? @lcnr

rustbot has assigned @lcnr.
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 S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 16, 2024
@chbaker0
Copy link
Contributor Author

tagging @alexcrichton because their wasm additions introduced the original default-hidden-visibility field. I'm still unsure of all the nuances of how it should work for wasm.

@alexcrichton
Copy link
Member

At the time the wasm target was added I vaguely recall that this was basically required to get the target working. For example it was just a quirk that was wasm-specific and wasn't intended to affect any other target. I don't recall the exact reason why it was necessary, but I vaguely recall it being required as otherwise all symbols were always exported from all wasm binaries. I just tried a local compilation with RUSTFLAGS=-Zdefault-hidden-visibility=no for wasm and it only exported the symbols I expected to be exported. Given that it should be ok to just flat out remove this option if it's causing problems.

@chbaker0
Copy link
Contributor Author

We do actually want the command line flag for another use case: controlling symbol export when the final link step is not performed by rustc. Related: rust-lang/compiler-team#656 and #73295

If you build a staticlib or rlib without this option, then link it with C code into a shared object, all Rust symbols are visible in the SO. This is an issue if Rust is an internal implementation detail of the SO, and especially if you have more than one SO linked with Rust code.

@alexcrichton
Copy link
Member

Ah ok makes sense! I can clarify then that at least for wasm-related stuff it's ok to disgregard this, I'll send a parallel PR to disable this option for wasm instead of enable it by default.

@lcnr
Copy link
Contributor

lcnr commented Apr 23, 2024

r? compiler

@petrochenkov
Copy link
Contributor

This looks like a reasonable default for foreign functions.

The change have very little impact on unsuspecting users now because none of the built-in targets use default_hidden_visibility now (wasm was migrated off it), and -Zdefault-hidden-visibility is unstable and opt-in.

r=me after considering #123994 (comment).
@rustbot author

@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 28, 2024
@chbaker0
Copy link
Contributor Author

Sorry I sat on this so long. Not sure if I should go ahead and submit, so I'll wait

@chbaker0
Copy link
Contributor Author

chbaker0 commented Aug 1, 2024

@rustbot ready

@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 Aug 1, 2024
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Aug 1, 2024

📌 Commit a1c27e3 has been approved by petrochenkov

It is now in the queue for this repository.

@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 Aug 1, 2024
tgross35 added a commit to tgross35/rust that referenced this pull request Aug 1, 2024
…petrochenkov

Use Default visibility for rustc-generated C symbol declarations

Previously, visibility for these symbols was determined by the `default-hidden-visibility` target option or the presence of `-Zdefault-hidden-visibility`. This leads to issue rust-lang#123427, where use of the flag leads to undefined hidden symbols (i.e., references that can never be resolved to an exported symbol from another shared library) for functions often provided by a platform shared library, such as `memcpy` and `memcmp` from `libc.so`.

References to symbols provided by shared libraries must have default visibility. Hidden visibility is mostly useful for _defined_ symbols.
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 1, 2024
Rollup of 5 pull requests

Successful merges:

 - rust-lang#123994 (Use Default visibility for rustc-generated C symbol declarations)
 - rust-lang#126818 (Better handle suggestions for the already present code and fix some suggestions)
 - rust-lang#127624 (Migrate and rename `issue-47551`, `issue-35164` and `issue-69368` `run-make` tests to rmake)
 - rust-lang#128361 (Migrate `link-cfg` and `rustdoc-default-output` `run-make` tests to rmake)
 - rust-lang#128436 (Update sysinfo version to 0.31.2)

r? `@ghost`
`@rustbot` modify labels: rollup
@tgross35
Copy link
Contributor

tgross35 commented Aug 2, 2024

@bors r-

Looks like this failed in #128519 (comment)

@bors bors removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Aug 2, 2024
@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 2, 2024
@chbaker0
Copy link
Contributor Author

chbaker0 commented Aug 2, 2024

The failure happens when targeting wasm32-wasip1, and the rustc output simply indicates that dylib is unsupported on that platform.

The test should only run on platforms where dylibs are supported. I'm not sure how to accomplish that with compiletest. I added an ignore-wasm32 directive but now that's just playing whack-a-mole with platforms that don't have dylib support.

What do other tests do? I can't find examples.

@rustbot ready

@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 Aug 2, 2024
@petrochenkov
Copy link
Contributor

The failure happens when targeting wasm32-wasip1, and the rustc output simply indicates that dylib is unsupported on that platform.

The test should only run on platforms where dylibs are supported. I'm not sure how to accomplish that with compiletest. I added an ignore-wasm32 directive but now that's just playing whack-a-mole with platforms that don't have dylib support.

What do other tests do? I can't find examples.

@rustbot ready

The //@ needs-dynamic-linking directive seems to be what you need.
@rustbot author

@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 Aug 5, 2024
@chbaker0
Copy link
Contributor Author

chbaker0 commented Aug 5, 2024

@rustbot ready

//@ needs-dynamic-linking does not work for wasm32-wasip1; it runs anyway. I assume this is a quirk or bug related to that target specifically. The latest revision includes both that directive and //@ ignore-wasm32

@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 Aug 5, 2024
@petrochenkov
Copy link
Contributor

Ah, ok, the target supports dynamic linking, but only cdylibs, not dylib crates.
All wasm targets and also nvptx64-nvidia-cuda have this property.
So the right directives are

//@ needs-dynamic-linking
//@ ignore-wasm
//@ ignore-nvptx64

@rustbot author

@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 Aug 6, 2024
@bors
Copy link
Contributor

bors commented Oct 1, 2024

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

@davidlattimore
Copy link
Contributor

My change - #130005 - caused conflicts with this PR. @chbaker0 - I'm happy to pick this up unless you wanted to?

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 11, 2024
…vis, r=Urgau

Use Default visibility for rustc-generated C symbol declarations

Non-default visibilities should only be used for definitions, not declarations, otherwise linking can fail.

This is based on rust-lang#123994.

Issue rust-lang#123427

When I changed `default-hidden-visibility` to `default-visibility` in rust-lang#130005, I updated all places in the code that used `default-hidden-visibility`, replicating the hidden-visibility bug to also happen for protected visibility.

Without this change, trying to build rustc with `-Z default-visibility=protected` fails with a link error.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 11, 2024
Rollup merge of rust-lang#131519 - davidlattimore:intrinsics-default-vis, r=Urgau

Use Default visibility for rustc-generated C symbol declarations

Non-default visibilities should only be used for definitions, not declarations, otherwise linking can fail.

This is based on rust-lang#123994.

Issue rust-lang#123427

When I changed `default-hidden-visibility` to `default-visibility` in rust-lang#130005, I updated all places in the code that used `default-hidden-visibility`, replicating the hidden-visibility bug to also happen for protected visibility.

Without this change, trying to build rustc with `-Z default-visibility=protected` fails with a link error.
@Dylan-DPC
Copy link
Member

@chbaker0 any updates on this? thanks

@danakj
Copy link
Contributor

danakj commented Oct 25, 2024

@davidlattimore I think it's okay for you to pick this up, thanks for offering.

@danakj
Copy link
Contributor

danakj commented Oct 25, 2024

Oh, looks like it's done already in #131519

@bjorn3
Copy link
Member

bjorn3 commented Oct 28, 2024

This PR can be closed, right?

@davidlattimore
Copy link
Contributor

From my perspective, it's fine to close

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.