-
Notifications
You must be signed in to change notification settings - Fork 492
[DRAFT] add allocator libraries compatible with the new rustc mangling #3403
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
Draft
krasimirgg
wants to merge
24
commits into
bazelbuild:main
Choose a base branch
from
krasimirgg:rustc_std_internal_symbols
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[DRAFT] add allocator libraries compatible with the new rustc mangling #3403
krasimirgg
wants to merge
24
commits into
bazelbuild:main
from
krasimirgg:rustc_std_internal_symbols
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds a version of the allocator support libraries implemented in rust, to account for the new internal allocator symbol mangling applied by rustc (rust-lang/rust#127173).
This mechanism relies on unstable language features and requires a nightly rustc from 2025-04-05 or later. It is designed to be compatible with:
no_std
rust_library
is used as a dependency of acc_binary
and when theexperimental_use_cc_common_link
setting is used.Rustc generates references to internal allocator symbols when building rust libraries. At link time, rustc generates
the definitions of these symbols. When rustc is not used as the final linker, we need to generate the definitions
ourselves. This happens for example when a
rust_library
is used as a dependency of arust_binary
, or when theexperimental_use_cc_common_link
setting is used.For older versions of rustc, the allocator symbol definitions can be provided via the
rust_toolchain
'sallocator_library
orglobal_allocator_library
attributes, with sample targets like//ffi/cc/allocator_library
and//ffi/cc/global_allocator_library
.Recent versions of rustc started mangling these allocator symbols (rust-lang/rust#127173). The
mangling uses a scheme that is specific to the exact version of the compiler. This makes the cc allocator library
definitions ineffective. To work around this, we provide rust versions of the symbol definitions annotated with an
unstable language attribute that instructs rustc to mangle them consistently. Because of the usage of unstable language features, this is only compatible with nightly versions of the compiler.
Since the new symbol definitions are written in rust, we cannot just attach them as attributes on the
rust_toolchain
as the old cc versions, as that would create a build graph cycle:
rust_library
depends on arust_toolchain
,rust_toolchain
depends on the allocator library,rust_library
directly.The bootstrapping cycle can be avoided by defining a separate internal "initial" rust toolchain specifically for
building the rust allocator libraries, and use a transition to attach the generated allocator libraries to the "main" rust
toolchain. But that duplicates the whole subgraph of the build around the rust toolchains, repository and supporting
tools used for them.
Instead, we define a new custom
rust_allocator_libraries
rule, which builds the new rust allocator libraries and exposes them via a newAllocatorLibrariesInfo
provider. Since we cannot attach such a target as an attribute to therust_toolchain
, we attach it as a newallocator_libraries
common attribute to the rust rules. We ported the cc versions into the new rust implementations of the (default) allocator and global allocator flavors in//ffi/rs/allocator_library
and//ffi/rs/global_allocator_library
.The rust standard libraries themselves have references to the allocator libraries. For correct linking order of the standard libraries, we need to establish the new rust allocator libraries as link-time dependencies of the standard libraries. The specific set of linker inputs depends on the no_std flavor and the choice of (default) allocator vs. global allocator. We establish the linking dependencies by extending the
rust_toolchain
_make_libstd_and_allocator_ccinfo
feature to let us inject a custom allocator library as a standard library dependency and using that in therust_allocator_libraries
implementation to generate the final standard libraries linker graph.The new functionality is opt-in and gated via:
//rust/settings:experimental_use_allocator_libraries_with_mangled_symbols
, which supplies the default value for the repository, andrust_toolchain
attributeexperimental_use_allocator_libraries_with_mangled_symbols
, which lets the user override the choice of using the new-style rust-based allocator symbols, or the old-style cc-based ones for a specific toolchain.