diff --git a/ffi/rs/allocator_library/BUILD.bazel b/ffi/rs/allocator_library/BUILD.bazel index 76a776c13e..1612c9f2cd 100644 --- a/ffi/rs/allocator_library/BUILD.bazel +++ b/ffi/rs/allocator_library/BUILD.bazel @@ -18,11 +18,18 @@ srcs = select({ "//conditions:default": ["allocator_library.rs"], }) +# This is hacky, but lets us compile the shims even on stable. +rustc_env = {"RUSTC_BOOTSTRAP": "1"} + +rustc_flags = [] + rust_library( name = "allocator_library", srcs = srcs, allocator_libraries = "@rules_rust//ffi/rs:empty_allocator_libraries", edition = "2024", + rustc_env = rustc_env, + rustc_flags = rustc_flags, tags = ["manual"], ) @@ -31,5 +38,7 @@ rust_library_without_process_wrapper( srcs = srcs, allocator_libraries = "@rules_rust//ffi/rs:empty_allocator_libraries", edition = "2024", + rustc_env = rustc_env, + rustc_flags = rustc_flags, tags = ["manual"], ) diff --git a/ffi/rs/global_allocator_library/BUILD.bazel b/ffi/rs/global_allocator_library/BUILD.bazel index 743c98d334..270186395f 100644 --- a/ffi/rs/global_allocator_library/BUILD.bazel +++ b/ffi/rs/global_allocator_library/BUILD.bazel @@ -17,5 +17,10 @@ rust_library( srcs = srcs, allocator_libraries = "@rules_rust//ffi/rs:empty_allocator_libraries", edition = "2024", + rustc_env = {"RUSTC_BOOTSTRAP": "1"}, + rustc_flags = select({ + "//rust/settings:experimental_use_allocator_libraries_with_mangled_symbols.legacy.rg_oom": ["--cfg=rules_rust_allocator_legacy_rg_oom"], + "//conditions:default": [], + }), tags = ["manual"], ) diff --git a/ffi/rs/global_allocator_library/global_allocator_library.rs b/ffi/rs/global_allocator_library/global_allocator_library.rs index 22ddeaf540..2ac722fb05 100644 --- a/ffi/rs/global_allocator_library/global_allocator_library.rs +++ b/ffi/rs/global_allocator_library/global_allocator_library.rs @@ -23,11 +23,13 @@ #![feature(linkage)] unsafe extern "C" { + #[cfg(rules_rust_allocator_legacy_rg_oom)] #[rustc_std_internal_symbol] fn __rg_oom(size: usize, align: usize) -> *mut u8; } #[linkage = "weak"] +#[cfg(rules_rust_allocator_legacy_rg_oom)] #[rustc_std_internal_symbol] fn __rust_alloc_error_handler(size: usize, align: usize) { unsafe { @@ -41,7 +43,19 @@ fn __rust_alloc_error_handler(size: usize, align: usize) { #[rustc_std_internal_symbol] static mut __rust_alloc_error_handler_should_panic: u8 = 1; +// See https://github.com/rust-lang/rust/pull/143387. +#[linkage = "weak"] +#[rustc_std_internal_symbol] +fn __rust_alloc_error_handler_should_panic_v2() -> u8 { + return 1; +} + // See https://github.com/rust-lang/rust/issues/73632#issuecomment-1563462239 #[linkage = "weak"] #[rustc_std_internal_symbol] static mut __rust_no_alloc_shim_is_unstable: u8 = 0; + +// See https://github.com/rust-lang/rust/pull/141061. +#[linkage = "weak"] +#[rustc_std_internal_symbol] +fn __rust_no_alloc_shim_is_unstable_v2() {} diff --git a/rust/settings/settings.bzl b/rust/settings/settings.bzl index da44833b5e..72b3c5f6ad 100644 --- a/rust/settings/settings.bzl +++ b/rust/settings/settings.bzl @@ -3,6 +3,7 @@ Definitions for all `@rules_rust//rust` settings """ +load("@bazel_skylib//lib:selects.bzl", "selects") load( "@bazel_skylib//rules:common_settings.bzl", "bool_flag", @@ -221,8 +222,6 @@ def experimental_use_allocator_libraries_with_mangled_symbols(name): the result of building the rust allocator libraries via a provider, which can be consumed by the rust build actions. We attach an instance of this as a common attribute to the rust rule set. - - TODO: how this interacts with stdlibs """ bool_flag( name = name, @@ -243,6 +242,42 @@ def experimental_use_allocator_libraries_with_mangled_symbols(name): }, ) + legacy_configs = [{ + # For rust toolchains prior to this rust commit: + # Support #[alloc_error_handler] without the allocator shim + # https://github.com/rust-lang/rust/commit/116f4ae171e292423304ee6842a11c48a4fff5ba + "name": "rg_oom", + "latest_versions": ["1.91.1", "nightly/2025-10-15", "beta/2025-10-26"], + }] + + legacy_names = [config["name"] for config in legacy_configs] + + legacy_flag_name = "%s.legacy" % name + + string_flag( + name = legacy_flag_name, + values = legacy_names + ["latest"], + build_setting_default = "latest", + ) + + for name in legacy_names: + native.config_setting( + name = "%s.%s_only" % (legacy_flag_name, name), + flag_values = { + legacy_flag_name: name, + }, + ) + + for index in range(len(legacy_names)): + match_any = [ + "%s.%s_only" % (legacy_flag_name, later) + for later in legacy_names[index:] + ] + selects.config_setting_group( + name = "%s.%s" % (legacy_flag_name, legacy_names[index]), + match_any = match_any, + ) + def experimental_use_coverage_metadata_files(): """A flag to have coverage tooling added as `coverage_common.instrumented_files_info.metadata_files` instead of \ reporting tools like `llvm-cov` and `llvm-profdata` as runfiles to each test. diff --git a/test/integration/cc_common_link/MODULE.bazel b/test/integration/cc_common_link/MODULE.bazel index 9b580a0160..88e351a565 100644 --- a/test/integration/cc_common_link/MODULE.bazel +++ b/test/integration/cc_common_link/MODULE.bazel @@ -16,30 +16,6 @@ bazel_dep(name = "platforms", version = "1.0.0") rust = use_extension("@rules_rust//rust:extensions.bzl", "rust") rust.toolchain( edition = "2018", - target_settings = [ - "@rules_rust//rust/settings:experimental_use_allocator_libraries_with_mangled_symbols_off", - ], -) - -# Generate a toolchain to be used for rust-based allocator symbols. - -# A recent enough version of rustc that mangles the internal allocator symbols. -VERSION = "nightly/2025-07-08" - -rust.repository_set( - name = "rust_with_alloc_mangling_linux_x86_64", - allocator_library = "@rules_rust//ffi/rs:empty", - edition = "2021", - exec_triple = "x86_64-unknown-linux-gnu", - target_compatible_with = [ - "@platforms//cpu:x86_64", - "@platforms//os:linux", - ], - target_settings = [ - "@rules_rust//rust/settings:experimental_use_allocator_libraries_with_mangled_symbols_on", - ], - target_triple = "x86_64-unknown-linux-gnu", - versions = [VERSION], ) use_repo(rust, "rust_toolchains") diff --git a/test/integration/cc_common_link_with_global_alloc/MODULE.bazel b/test/integration/cc_common_link_with_global_alloc/MODULE.bazel index 69bf57a72c..fed410f157 100644 --- a/test/integration/cc_common_link_with_global_alloc/MODULE.bazel +++ b/test/integration/cc_common_link_with_global_alloc/MODULE.bazel @@ -22,7 +22,7 @@ rust.toolchain( # Generate a toolchain to be used for rust-based allocator symbols. # A recent enough version of rustc that mangles the internal allocator symbols. -VERSION = "nightly/2025-04-08" +VERSION = "nightly/2026-01-25" rust.repository_set( name = "rust_with_alloc_mangling_linux_x86_64",