|
| 1 | +// Workaround for Rust issue https://github.com/rust-lang/rust/issues/73632 |
| 2 | +// We provide the allocator functions that rustc leaves in rlibs. These are |
| 3 | +// normally provided by rustc during the linking phase (since the allocator in |
| 4 | +// use can vary), but if rustc doesn't do the final link we have to provide |
| 5 | +// these manually. Hopefully we can make progress on the above bug and |
| 6 | +// eventually not need this kludge. |
| 7 | +// |
| 8 | +// Recently rustc started mangling these symbols, so we rewrote them in |
| 9 | +// rust. |
| 10 | +// https://github.com/rust-lang/rust/pull/127173 |
| 11 | +// |
| 12 | +// This code uses unstable internal rustc features that are only available when |
| 13 | +// using a nightly toolchain. Also, it is only compatible with versions |
| 14 | +// of rustc that include the symbol mangling, such as nightly/2025-04-08 or |
| 15 | +// later. |
| 16 | +// |
| 17 | +// This has been translated from our c++ version |
| 18 | +// rules_rust/ffi/cc/allocator_library/allocator_library.cc. |
| 19 | +#![no_std] |
| 20 | +#![allow(warnings)] |
| 21 | +#![allow(internal_features)] |
| 22 | +#![feature(rustc_attrs)] |
| 23 | +#![feature(linkage)] |
| 24 | + |
| 25 | +unsafe extern "C" { |
| 26 | + #[rustc_std_internal_symbol] |
| 27 | + fn __rdl_alloc(size: usize, align: usize) -> *mut u8; |
| 28 | + |
| 29 | + #[rustc_std_internal_symbol] |
| 30 | + fn __rdl_dealloc(ptr: *mut u8, size: usize, align: usize); |
| 31 | + |
| 32 | + #[rustc_std_internal_symbol] |
| 33 | + fn __rdl_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8; |
| 34 | + |
| 35 | + #[rustc_std_internal_symbol] |
| 36 | + fn __rdl_alloc_zeroed(size: usize, align: usize) -> *mut u8; |
| 37 | +} |
| 38 | + |
| 39 | +#[linkage = "weak"] |
| 40 | +#[rustc_std_internal_symbol] |
| 41 | +fn __rust_alloc(size: usize, align: usize) -> *mut u8 { |
| 42 | + unsafe { |
| 43 | + return __rdl_alloc(size, align); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[linkage = "weak"] |
| 48 | +#[rustc_std_internal_symbol] |
| 49 | +fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize) { |
| 50 | + unsafe { |
| 51 | + return __rdl_dealloc(ptr, size, align); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[linkage = "weak"] |
| 56 | +#[rustc_std_internal_symbol] |
| 57 | +fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8 { |
| 58 | + unsafe { |
| 59 | + return __rdl_realloc(ptr, old_size, align, new_size); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[linkage = "weak"] |
| 64 | +#[rustc_std_internal_symbol] |
| 65 | +fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8 { |
| 66 | + unsafe { |
| 67 | + return __rdl_alloc_zeroed(size, align); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[linkage = "weak"] |
| 72 | +#[rustc_std_internal_symbol] |
| 73 | +fn __rust_alloc_error_handler(size: usize, align: usize) { |
| 74 | + panic!(); |
| 75 | +} |
| 76 | + |
| 77 | +// New feature as of https://github.com/rust-lang/rust/pull/88098. |
| 78 | +// This symbol is normally emitted by rustc. 0 means OOMs should abort, 1 means OOMs should panic. |
| 79 | +#[linkage = "weak"] |
| 80 | +#[rustc_std_internal_symbol] |
| 81 | +static mut __rust_alloc_error_handler_should_panic: u8 = 1; |
| 82 | + |
| 83 | +// See https://github.com/rust-lang/rust/issues/73632#issuecomment-1563462239 |
| 84 | +#[linkage = "weak"] |
| 85 | +#[rustc_std_internal_symbol] |
| 86 | +static mut __rust_no_alloc_shim_is_unstable: u8 = 0; |
0 commit comments