-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: #[export_visibility = ...]
attribute.
#3834
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
base: master
Are you sure you want to change the base?
Conversation
9e36c42
to
a79823e
Compare
a79823e
to
ab37907
Compare
|
||
## Benefit: Smaller binaries | ||
|
||
One undesirable consequence of unnecessary public exports is binary size bloat. |
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.
Should only be the case for libraries. For binaries all functions are already made not-exported.
(when the freeing allocator expects that the pointer it got was earlier | ||
allocated by the same allocator instance). | ||
|
||
This is what happened in https://crbug.com/418073233. In the smaller repro |
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.
I would have expected all of Chromium to use a single rust allocator rather than use a different one for each DSO. Why is that not the case?
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.
I would have expected all of Chromium to use a single rust allocator rather than use a different one for each DSO. Why is that not the case?
Is that really a requirement if foo.so
doesn't export any functions that return pointers to Rust-related objects? I would expect in such a case that which Rust allocator / standard library / etc is used would be an internal implementation detail of foo.so
. IIUC this detail leaks out only because of an unintentional export of a cxx
-generated, internal symbol.
But to try to answer the question - the same allocator is statically linked into Chromium binaries. This means that an executable and an .so
may end up with a separate copy of the same global data structures of the allocator.
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.
Is that really a requirement if foo.so doesn't export any functions that return pointers to Rust-related objects?
It is not a requirement. I'm just surprised that Chromium copies the entire rust standard library between the dylib and executable rather than using the copy from the dylib in the executable to save space.
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.
Is that really a requirement if foo.so doesn't export any functions that return pointers to Rust-related objects?
It is not a requirement. I'm just surprised that Chromium copies the entire rust standard library between the dylib and executable rather than using the copy from the dylib in the executable to save space.
That is indeed a bit unfortunate. I think this is to some extent based on the following:
- Chromium requirement to use an external linker
- Assumption that only
rlib
s /static_lib
s can be linked by an external linker, and that an external linker wouldn't be able to handledylib
s
But thank you for bringing this up - maybe this should indeed be treated as an alternative fix for https://crbug.com/418073233. I am not sure what the next steps should be for this aspect:
- Maybe I should open a bug (either under https://github.com/rust-lang/rust/issues, or under https://crbug.com) to move this discussion elsewhere?
- Maybe before opening a bug I should first learn more about
dylib
s...
For most use cases rather than specifying the exact symbol visibility (which may not even be supported by the object file format, like interposable on pe/coff or (with the default two-level namespaces) mach-o) I think having just a way to force SymbolExportLevel::Rust rather than the default SymbolExportLevel::C would be a better idea. This causes it to still be exported from rust dylibs (as necessary to avoid linker errors depending on the exactly when rustc decides to codegen functions), but prevents it from being exported from cdylibs. It doesn't work for staticlibs currently, but for those if you want to limit symbol visibility you have to specify your own version script during linking anyway to prevent exporting all rust mangled symbols too. |
The fact that you are distinguishing between
That's not 100% correct - instead of using a version script, one may also use |
The Chromium case is effectively equivalent to using staticlibs, not to using rust dylibs/cdylibs.
That doesn't apply to the standard library unless you go out of your way using unstable features to recompile the standard library. |
Ack / agreed.
Thank you for bringing up this point. This probably should be explicitly addressed by the RFC (*), but I am not sure if I agree with your conclusions so far. This is because:
(*) I am not sure what the right process is here. Should I add commits to the RFC as we keep discussing here? Should I first give people an opportunity to review the first draft? |
I think this is a good opportunity to expand the design space (and documentation) of "various levels of exportendess" a bit, even if the resulting proposal for There are multiple attributes that targets this similar space ( What I'd like to see is a table of "levels of exportedness" combined with the kinds of end artifacts, and how we can users can express all those levels with the attributes listed above.
In particular, one of my requirements is that |
This is something only rustc must be allowed to do (other than for symbols defined in inline asm called from within the same inline asm block). Only rustc knows if all callers will end up in the same object file as the definition and it doesn't provide any guarantees around when this happens. So exposing this to the user is a stability hazard.
For regular functions and
For rlib this doesn't make sense. There is no way to make rlibs a symbol export boundary without introducing an expensive link/object file rewrite step for each individual rlib. For staticlib it would be nice to have a symbol export boundary, but unfortunately we don't have one right now even for
This makes sense to me. See the end of my comment.
This has to always be the case if it is visible outside of the object file. The very point of rust dylibs is that rust code in a separate DSO can call any public function, which thanks to cross-crate inlining can call effectively every function that rustc wouldn't make private to the current object file. And again, rustc doesn't provide any guarantees when this happens, so allowing you to not export symbols from a rust dylib is a stability hazard.
Yes.
No
Not really aside from the visibility information we already tell the linker (export from rust dylib, don't export from cdylib). Currently rustc internally works with three different symbol export levels:
It makes sense to me to allow |
I think this probably should be captured somehow as one of the alternatives in the RFC. Is there a specific syntax that you have in mind here? I guess one option would be to have a |
👍
I don't think this is a good name as it is still meant to be usable from C, just not outside of the linked DSO.
This would be an option, although ideally if we manage to stop exporting all symbols from staticlibs, I would like the same attribute to be usable to prevent export from both cdylib and staticlib, so it should probably not mention cdylib in the name. I don't have suggestions for a better name though. |
Frankly, if we have |
This RFC proposes to add
#[export_visibility = …]
attribute, which seems like a reasonable way to address the following issues:#[no_mangle]
symbols are exported from acdylib
rust#98449This RFC complements the
-Zdefault-visibility=...
command-line flag, which is tracked in rust-lang/rust#131090This PR replaces the Major Change Proposal (MCP) at rust-lang/compiler-team#881
(/cc @bjorn3, @ChrisDenton, @chorman0773, @joshtriplett, @mati865, @workingjubilee, and @Urgau who have kindly provided feedback in the Zulip thread associated with that MCP)
/cc @tmandry from rust-lang/rust-project-goals#253, because one area where this RFC seems needed is FFI tooling
Rendered