rust/tests/codegen/sanitizer/cfi
Matthias Krüger 8f5c09b37c
Rollup merge of #138349 - 1c3t3a:external-weak-cfi, r=rcvalle
Emit function declarations for functions with `#[linkage="extern_weak"]`

Currently, when declaring an extern weak function in Rust, we use the following syntax:
```rust
unsafe extern "C" {
   #[linkage = "extern_weak"]
   static FOO: Option<unsafe extern "C" fn() -> ()>;
}
```
This allows runtime-checking the extern weak symbol through the Option.

When emitting LLVM-IR, the Rust compiler currently emits this static as an i8, and a pointer that is initialized with the value of the global i8 and represents the nullabilty e.g.
```
`@FOO` = extern_weak global i8
`@_rust_extern_with_linkage_FOO` = internal global ptr `@FOO`
```

This approach does not work well with CFI, where we need to attach CFI metadata to a concrete function declaration, which was pointed out in https://github.com/rust-lang/rust/issues/115199.

This change switches to emitting a proper function declaration instead of a global i8. This allows CFI to work for extern_weak functions. Example:
```
`@_rust_extern_with_linkage_FOO` = internal global ptr `@FOO`
...
declare !type !61 !type !62 !type !63 !type !64 extern_weak void `@FOO(double)` unnamed_addr #6
```

We keep initializing the Rust internal symbol with the function declaration, which preserves the correct behavior for runtime checking the Option.

r? `@rcvalle`

cc `@jakos-sec`

try-job: test-various
2025-03-17 16:34:50 +01:00
..
add-canonical-jump-tables-flag.rs
add-cfi-normalize-integers-flag.rs Add missing module flags for CFI and KCFI sanitizers 2024-08-21 20:23:56 +00:00
add-enable-split-lto-unit-flag.rs
dbg-location-on-cfi-blocks.rs CFI: Append debug location to CFI blocks 2024-11-11 09:17:43 +00:00
emit-type-checks-attr-no-sanitize.rs Set signext or zeroext for integer arguments on LoongArch64 2024-10-23 04:42:21 +02:00
emit-type-checks.rs Set signext or zeroext for integer arguments on LoongArch64 2024-10-23 04:42:21 +02:00
emit-type-metadata-attr-cfi-encoding.rs
emit-type-metadata-id-itanium-cxx-abi-const-generics.rs Implement #[define_opaque] attribute for functions. 2025-03-11 12:05:02 +00:00
emit-type-metadata-id-itanium-cxx-abi-drop-in-place.rs
emit-type-metadata-id-itanium-cxx-abi-function-types.rs
emit-type-metadata-id-itanium-cxx-abi-lifetimes.rs Implement #[define_opaque] attribute for functions. 2025-03-11 12:05:02 +00:00
emit-type-metadata-id-itanium-cxx-abi-method-secondary-typeid.rs
emit-type-metadata-id-itanium-cxx-abi-paths.rs Implement #[define_opaque] attribute for functions. 2025-03-11 12:05:02 +00:00
emit-type-metadata-id-itanium-cxx-abi-pointer-types.rs
emit-type-metadata-id-itanium-cxx-abi-primitive-types.rs
emit-type-metadata-id-itanium-cxx-abi-repr-transparent-types.rs
emit-type-metadata-id-itanium-cxx-abi-sequence-types.rs
emit-type-metadata-id-itanium-cxx-abi-trait-types.rs
emit-type-metadata-id-itanium-cxx-abi-user-defined-types.rs
emit-type-metadata-itanium-cxx-abi-generalized.rs
emit-type-metadata-itanium-cxx-abi-normalized-generalized.rs
emit-type-metadata-itanium-cxx-abi-normalized.rs
emit-type-metadata-itanium-cxx-abi.rs
emit-type-metadata-trait-objects.rs
external_weak_symbols.rs Emit function declarations for functions with #[linkage="extern_weak"] 2025-03-17 08:27:53 +00:00
generalize-pointers.rs
normalize-integers.rs