Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ffi/rs/allocator_library/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)

Expand All @@ -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"],
)
5 changes: 5 additions & 0 deletions ffi/rs/global_allocator_library/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
14 changes: 14 additions & 0 deletions ffi/rs/global_allocator_library/global_allocator_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {}
39 changes: 37 additions & 2 deletions rust/settings/settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down
24 changes: 0 additions & 24 deletions test/integration/cc_common_link/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading