diff --git a/ffi/rs/allocator_library/BUILD.bazel b/ffi/rs/allocator_library/BUILD.bazel index 76a776c13e..d97686afe1 100644 --- a/ffi/rs/allocator_library/BUILD.bazel +++ b/ffi/rs/allocator_library/BUILD.bazel @@ -1,34 +1,30 @@ -load("@rules_rust//rust:defs.bzl", "rust_library") +load("@rules_rust//rust:defs.bzl", "rust_static_library") # buildifier: disable=bzl-visibility load( "@rules_rust//rust/private:rust.bzl", - "rust_library_without_process_wrapper", + "rust_static_library_without_process_wrapper", ) package( default_visibility = ["@rules_rust//ffi/rs:__subpackages__"], ) -srcs = select({ - # Windows doesn't support weak symbol linkage. - # If someone can make this work on Windows, please do! - # For now we will silently not supply any symbols, because it would be very messy to conditionally define the default allocator library on toolchains depending on the platform. - "@platforms//os:windows": ["empty.rs"], - "//conditions:default": ["allocator_library.rs"], -}) +# When building a staticlib crate, rustc injects the default allocator +# redirections (e.g., __rust_alloc -> __rdl_alloc). +# So we use an empty static library to provide these. -rust_library( +rust_static_library( name = "allocator_library", - srcs = srcs, + srcs = ["empty.rs"], allocator_libraries = "@rules_rust//ffi/rs:empty_allocator_libraries", edition = "2024", tags = ["manual"], ) -rust_library_without_process_wrapper( +rust_static_library_without_process_wrapper( name = "allocator_library_without_process_wrapper", - srcs = srcs, + srcs = ["empty.rs"], allocator_libraries = "@rules_rust//ffi/rs:empty_allocator_libraries", edition = "2024", tags = ["manual"], diff --git a/ffi/rs/allocator_library/allocator_library.rs b/ffi/rs/allocator_library/allocator_library.rs deleted file mode 100644 index 27661f8754..0000000000 --- a/ffi/rs/allocator_library/allocator_library.rs +++ /dev/null @@ -1,98 +0,0 @@ -// Workaround for Rust issue https://github.com/rust-lang/rust/issues/73632 -// We provide the allocator functions that rustc leaves in rlibs. These are -// normally provided by rustc during the linking phase (since the allocator in -// use can vary), but if rustc doesn't do the final link we have to provide -// these manually. Hopefully we can make progress on the above bug and -// eventually not need this kludge. -// -// Recently rustc started mangling these symbols, so we rewrote them in -// rust. -// https://github.com/rust-lang/rust/pull/127173 -// -// This code uses unstable internal rustc features that are only available when -// using a nightly toolchain. Also, it is only compatible with versions -// of rustc that include the symbol mangling, such as nightly/2025-04-08 or -// later. -// -// This has been translated from our c++ version -// rules_rust/ffi/cc/allocator_library/allocator_library.cc. -#![no_std] -#![allow(warnings)] -#![allow(internal_features)] -#![feature(rustc_attrs)] -#![feature(linkage)] - -unsafe extern "C" { - #[rustc_std_internal_symbol] - fn __rdl_alloc(size: usize, align: usize) -> *mut u8; - - #[rustc_std_internal_symbol] - fn __rdl_dealloc(ptr: *mut u8, size: usize, align: usize); - - #[rustc_std_internal_symbol] - fn __rdl_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8; - - #[rustc_std_internal_symbol] - fn __rdl_alloc_zeroed(size: usize, align: usize) -> *mut u8; -} - -#[linkage = "weak"] -#[rustc_std_internal_symbol] -fn __rust_alloc(size: usize, align: usize) -> *mut u8 { - unsafe { - return __rdl_alloc(size, align); - } -} - -#[linkage = "weak"] -#[rustc_std_internal_symbol] -fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize) { - unsafe { - return __rdl_dealloc(ptr, size, align); - } -} - -#[linkage = "weak"] -#[rustc_std_internal_symbol] -fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8 { - unsafe { - return __rdl_realloc(ptr, old_size, align, new_size); - } -} - -#[linkage = "weak"] -#[rustc_std_internal_symbol] -fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8 { - unsafe { - return __rdl_alloc_zeroed(size, align); - } -} - -#[linkage = "weak"] -#[rustc_std_internal_symbol] -fn __rust_alloc_error_handler(size: usize, align: usize) { - panic!(); -} - -// New feature as of https://github.com/rust-lang/rust/pull/88098. -// This symbol is normally emitted by rustc. 0 means OOMs should abort, 1 means OOMs should panic. -#[linkage = "weak"] -#[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/private/rust.bzl b/rust/private/rust.bzl index 55d1fbb746..0b2a88a4a6 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -1382,6 +1382,26 @@ rust_library_without_process_wrapper = rule( ], ) +def _rust_static_library_without_process_wrapper_impl(ctx): + providers = _rust_static_library_impl(ctx) + return providers + [_RustBuiltWithoutProcessWrapperInfo()] + +rust_static_library_without_process_wrapper = rule( + implementation = _rust_static_library_without_process_wrapper_impl, + doc = "A variant of `rust_static_library` that uses a minimal process wrapper for `Rustc` actions.", + attrs = dict(_common_attrs_for_binary_without_process_wrapper(_common_attrs).items()), + fragments = ["cpp"], + toolchains = [ + str(Label("//rust:toolchain_type")), + config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False), + ], + provides = [ + CcInfo, + rust_common.test_crate_info, + _RustBuiltWithoutProcessWrapperInfo, + ], +) + def _test_attrs_for_binary_without_process_wrapper(attrs): new_attrs = {} new_attrs.update(attrs) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 14b02dcfe3..42616d8868 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1980,6 +1980,9 @@ def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_co cc_infos.append(libstd_and_allocator_cc_info) providers = [cc_common.merge_cc_infos(cc_infos = cc_infos)] + if crate_info.type == "staticlib": + # The static archive is the output. + dot_a = crate_info.output if dot_a: providers.append(AllocatorLibrariesImplInfo(static_archive = dot_a)) return providers 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")