-
Couldn't load subscription status.
- Fork 517
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
Merged
krasimirgg
merged 28 commits into
bazelbuild:main
from
krasimirgg:rustc_std_internal_symbols
Jul 8, 2025
Merged
add allocator libraries compatible with the new rustc mangling #3403
krasimirgg
merged 28 commits into
bazelbuild:main
from
krasimirgg:rustc_std_internal_symbols
Jul 8, 2025
+645
−37
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
scentini
approved these changes
Jul 8, 2025
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_stdrust_libraryis used as a dependency of acc_binaryand when theexperimental_use_cc_common_linksetting 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_libraryis used as a dependency of arust_binary, or when theexperimental_use_cc_common_linksetting is used.For older versions of rustc, the allocator symbol definitions can be provided via the
rust_toolchain'sallocator_libraryorglobal_allocator_libraryattributes, with sample targets like//ffi/cc/allocator_libraryand//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_toolchainas the old cc versions, as that would create a build graph cycle:
rust_librarydepends on arust_toolchain,rust_toolchaindepends on the allocator library,rust_librarydirectly.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_librariesrule, which builds the new rust allocator libraries and exposes them via a newAllocatorLibrariesInfoprovider. Since we cannot attach such a target as an attribute to therust_toolchain, we attach it as a newallocator_librariescommon 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_libraryand//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_ccinfofeature to let us inject a custom allocator library as a standard library dependency and using that in therust_allocator_librariesimplementation 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_toolchainattributeexperimental_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.For testing, I created variants for the cc_common.link / no_std / global_allocator test matrix. I found it useful to be able to declare
target_settingsvia therust.repository_setrule, similarly to thetarget_compatible_with. This lets us include additional toolchains in the same repository that are distinguished by the setting, like in: https://github.com/bazelbuild/rules_rust/pull/3403/files#diff-164dd740b73baa94f9a84ed0d4a7ec29f8adeb34ff8430492747e2443f7f39d3