rust/compiler/rustc_codegen_ssa/src
Matthias Krüger a5d38ede1d
Rollup merge of #145724 - folkertdev:track-caller-drop-no-mangle, r=fee1-dead
the `#[track_caller]` shim should not inherit `#[no_mangle]`

fixes https://github.com/rust-lang/rust/issues/143162

builds on https://github.com/rust-lang/rust/pull/143293 which introduced a mechanism to strip attributes from shims.

cc `@Jules-Bertholet` `@workingjubilee` `@bjorn3`

---

Summary:

This PR fixes an interaction between `#[track_caller]`, `#[no_mangle]`, and casting to a function pointer.

A function annotated with `#[track_caller]` internally has a hidden extra argument for the panic location. The `#[track_caller]` attribute is only allowed on `extern "Rust"` functions. When a function is annotated with both `#[no_mangle]` and `#[track_caller]`, the exported symbol has the signature that includes the extra panic location argument. This works on stable rust today:

```rust
extern "Rust" {
    #[track_caller]
    fn rust_track_caller_ffi_test_tracked() -> &'static Location<'static>;
}

mod provides {
    use std::panic::Location;
    #[track_caller] // UB if we did not have this!
    #[no_mangle]
    fn rust_track_caller_ffi_test_tracked() -> &'static Location<'static> {
        Location::caller()
    }
}
```

When a `#[track_caller]` function is converted to a function pointer, a shim is added to drop the additional argument. So this is a valid program:

```rust
#[track_caller]
fn foo() {}

fn main() {
    let f = foo as fn();
    f();
}
```

The issue arises when `foo` is additionally annotated with `#[no_mangle]`, the generated shim currently inherits this attribute, also exporting a symbol named `foo`, but one without the hidden panic location argument. The linker rightfully complains about a duplicate symbol.

The solution of this PR is to have the generated shim drop the `#[no_mangle]` attribute.
2025-10-18 08:08:36 +02:00
..
back Support #[alloc_error_handler] without the allocator shim 2025-10-10 13:04:53 +00:00
debuginfo Flatten ifs in rustc_codegen_ssa 2025-03-17 18:56:52 +00:00
mir Rollup merge of #145724 - folkertdev:track-caller-drop-no-mangle, r=fee1-dead 2025-10-18 08:08:36 +02:00
traits Move computation of allocator shim contents to cg_ssa 2025-10-10 13:04:55 +00:00
assert_module_sources.rs Print CGU reuse statistics when -Zprint-mono-items is enabled 2025-08-04 15:43:50 +02:00
base.rs miri: use allocator_shim_contents codegen helper 2025-10-15 21:23:14 +02:00
codegen_attrs.rs Use span from parsed attribute 2025-10-14 16:17:36 +02:00
common.rs Move NativeLibKind from rustc_session to rustc_hir 2025-08-27 20:24:59 +02:00
errors.rs Fix double error for #[no_mangle] on closures 2025-10-08 17:46:33 +02:00
lib.rs Ensure fat LTO doesn't merge everything into the allocator module 2025-09-06 13:31:41 +00:00
meth.rs Remove DynKind 2025-09-17 04:46:46 +02:00
mono_item.rs Port the #[linkage] attribute to the new attribute system 2025-08-13 21:01:37 +02:00
size_of_val.rs remove explicit deref of AbiAlign for most methods 2025-09-28 15:02:14 -07:00
target_features.rs Add an experimental unsafe(force_target_feature) attribute. 2025-08-22 01:26:26 +02:00