rust/tests/codegen/sanitizer
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
..
cfi Rollup merge of #138349 - 1c3t3a:external-weak-cfi, r=rcvalle 2025-03-17 16:34:50 +01:00
kcfi tests: use minicore more 2025-02-24 09:26:54 +00:00
aarch64-shadow-call-stack-with-fixed-x18.rs tests: use minicore more 2025-02-24 09:26:54 +00:00
address-sanitizer-globals-tracking.rs Run rustfmt on tests/codegen/. 2024-05-31 15:56:43 +10:00
dataflow-instrument-functions.rs Run rustfmt on tests/codegen/. 2024-05-31 15:56:43 +10:00
kasan-emits-instrumentation.rs tests: use minicore more 2025-02-24 09:26:54 +00:00
memory-track-origins.rs Run rustfmt on tests/codegen/. 2024-05-31 15:56:43 +10:00
memtag-attr-check.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
no-sanitize-inlining.rs Run rustfmt on tests/codegen/. 2024-05-31 15:56:43 +10:00
no-sanitize.rs Allow disabling ASan instrumentation for globals 2024-11-02 22:35:34 +01:00
riscv64-shadow-call-stack.rs tests: use minicore more 2025-02-24 09:26:54 +00:00
safestack-attr-check.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
sanitizer-recover.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00
scs-attr-check.rs [AUTO_GENERATED] Migrate compiletest to use ui_test-style //@ directives 2024-02-22 16:04:04 +00:00