cfi: add diag mode support
Currently a Rust CFI failure only inserts a ud2. However, for clang we have the option for a helpful diagnostic message that explains the violation and is especially helpful for fixing it. This message works through hooking the UBSan runtime and calling into it with the necessary information for a helpful error message. In clang, this is enabled via `-fno-sanitize-trap=cfi`.
This change adds the same behavior to rustc's CFI. Instead of a `no-sanitize-trap` flag, we added `-Z cfi-mode={diag|trap}`, with trap as the default. The diag mode will print the following error message for a violation:
```
tests/ui/sanitizer/cfi/fn-ptr-type-mismatch.rs:1:1: runtime error: control flow integrity check for type fn(i32, i32) -> i32 failed during indirect function call
fn_ptr_type_mismatch.ecd806f409c5c1fc-cgu.0: note: fn_ptr_type_mismatch::add_one defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tests/ui/sanitizer/cfi/fn-ptr-type-mismatch.rs:1:1
```
r? @rcvalle
CFI: Add support for the adt_const_params feature
Adds support for encoding constants of types allowed by the adt_const_params and unsized_const_params features (i.e., char, str, tuple, array, slice, struct, enum, and reference values) as literal arguments, and fixes the encoding of bool values to be 0 and 1, and of negative integer values to be their decimal values preceded by n (fixesrust-lang/rust#159299).
Currently a Rust CFI failure only inserts a ud2. However, for clang
we have the option for a helpful diagnostic message that explains the
violation and is especially helpful for fixing it. This message works
through hooking the UBSan runtime and calling into it with the
necessary information for a helpful error message. In clang, this is
enabled via -fno-sanitize-trap=cfi.
This change adds the same behavior to rustc's CFI. Instead of a
no-sanitize-trap flag, we added -Z cfi-mode={diag|trap}, with trap
as the default. The diag mode will print the following error message
for a violation:
```
tests/ui/sanitizer/cfi/fn-ptr-type-mismatch.rs:1:1: runtime error: control flow integrity check for type fn(i32, i32) -> i32 failed during indirect function call
fn_ptr_type_mismatch.ecd806f409c5c1fc-cgu.0: note: fn_ptr_type_mismatch::add_one defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tests/ui/sanitizer/cfi/fn-ptr-type-mismatch.rs:1:1
```
MIR GVN propagates MaybeUninit::uninit() as `const <uninit>` in aggregate
constructions and codegen emits a memcpy from an `[N x i8] undef` global for
each such field, which LLVM materializes as zero-initialization.
We extract the existing `all_bytes_uninit` skip already present for
`Rvalue::Use` and `Rvalue::Repeat` to a separate helper function, and use
it in the `Rvalue::Aggregate` field loop.
Fix `bool` calling convention for aarch64, etc.
We have been making a broad assumption about the way `bool` should be handled that *may* be correct for Rust calling Rust but is wildly incorrect for cross-language FFI calls. The ABI of `bool` is generally poorly-specified in any C psABI, and often deliberately allows for deviation after the first 8 bits.
This has surfaced as a bug that can corrupt Rust programs on aarch64, but it is a potential latent bug when it comes to every ABI. The simplest solution is to simply stop applying the special case when generating `bool`'s ArgAttributes. This yields control to the later ABI implementations, which may then sign or zero extend the `bool` (or not) according to their needs.
Add CFI tests for return types and never type
In a few different conversations, the CFI types of various functions has come up. First, the CFI type of returning `!` came up in https://github.com/rust-lang/rust/issues/159446#issuecomment-5011154213, where we found that it doesn't have the same CFI type as returning `()`, which it probably should.
Then in rust-lang/rust#159935, I wanted to make sure that this does not change the CFI type of functions when `*mut c_void` appears as an argument. Luckily it does not since it's a lang item, but there's no test for this case.
Thus, add tests for all of these cases and a few others.
AI assistance was involved with writing the test.
When adding `OperandValue::Uninit` to skip stores for entirely-uninit
constants, we missed the `PassMode::Direct | PassMode::Pair` branch of
`codegen_return_terminator`, which called `immediate_or_packed_pair`
unconditionally. A function directly returning an all-uninit value (e.g.
`MaybeUninit::uninit()`) would hit that branch and ICE in
`OperandRef::immediate`, as reported against tokio and reduced by tmiasko.
use `minicore::simd::Simd` more
follow-up to https://github.com/rust-lang/rust/pull/159656.
I've used `grep -rlZ "repr_simd" . | xargs -0 grep -l "use minicore"` to find all test files that used both `repr_simd` and `use minicore`, they now all use `minicore::simd` to `repr_simd` is no longer needed for them.
codegen: skip stores for entirely-uninit constant aggregate fields
MIR GVN (since rust-lang/rust#147827) propagates MaybeUninit::uninit() as `const <uninit>` in aggregate constructions. Without this fix, codegen would emit a memcpy from an `[N x i8] undef` global for each such field, which LLVM materializes as zero-initialization.
This mirrors the existing `all_bytes_uninit` skip already present for `Rvalue::Use` (added in rust-lang/rust#147827) into the `Rvalue::Aggregate` field loop.
Fixes: rust-lang/rust#157743
Apply RemoveNoopLandingPads post-monomorphization
On Cargo this cuts ~5% of the LLVM IR lines we generate (measured with -Cno-prepopulate-passes).
Closes https://github.com/rust-lang/rust/issues/159399.
Adds support for encoding constants of types allowed by the
adt_const_params and unsized_const_params features (i.e., char, str,
tuple, array, slice, struct, enum, and reference values) as literal
arguments, and fixes the encoding of bool values to be 0 and 1, and of
negative integer values to be their decimal values preceded by n.
MIR GVN propagates MaybeUninit::uninit() as `const <uninit>` in aggregate
constructions and codegen emits a memcpy from an `[N x i8] undef` global for
each such field, which LLVM materializes as zero-initialization.
Instead of special-casing `all_bytes_uninit` at each call site, add a new
`OperandValue::Uninit` variant that `eval_mir_constant_to_operand` returns for
any all-bytes-uninit constant. `store_with_flags` is a no-op for this variant,
so the fix applies uniformly across `Rvalue::Use`, `Rvalue::Repeat`, and
`Rvalue::Aggregate` without per-site checks. The variant propagates through
field extraction and transmutes, and is handled appropriately at the remaining
call sites (function arguments, discriminant reads, debug info, etc.).
This speeds up LLVM and improves codegen overall. As an example, for
cargo this cuts ~5% of the LLVM IR lines we generate (measured with
-Cno-prepopulate-passes).
[aarch64][win] Pass oversized c-variadic args indirectly on Arm64EC
On Arm64EC the variadic portion of a c-variadic call must follow the MS x64 ABI: any argument that does not fit in 8 bytes, or whose size is not 1, 2, 4, or 8 bytes, is passed by reference. rustc's `va_arg` implementation already reads such arguments indirectly, but the caller side in `aarch64::compute_abi_info` passed a scalar `i128` by value (it is not an aggregate, so it bypassed `classify_arg`). The callee then dereferenced the inline value as a pointer, causing an access violation (0xC0000005) at runtime.
Mark variadic-tail arguments larger than 8 bytes, or whose size is not a power of two, as indirect on Arm64EC so the caller and the `va_arg` reader agree. Fixed arguments are unaffected: a fixed `i128` is still passed by value, matching MSVC and Clang.
Add a codegen test verifying that a variadic `i128` is passed as a pointer while a fixed `i128` is passed by value.
On Arm64EC the variadic portion of a c-variadic call must follow the MS
x64 ABI: any argument that does not fit in 8 bytes, or whose size is not
1, 2, 4, or 8 bytes, is passed by reference. rustc's `va_arg`
implementation already reads such arguments indirectly, but the caller
side in `aarch64::compute_abi_info` passed a scalar `i128` by value (it
is not an aggregate, so it bypassed `classify_arg`). The callee then
dereferenced the inline value as a pointer, causing an access violation
(0xC0000005) at runtime.
Mark variadic-tail arguments larger than 8 bytes, or whose size is not a
power of two, as indirect on Arm64EC so the caller and the `va_arg`
reader agree. Fixed arguments are unaffected: a fixed `i128` is still
passed by value, matching MSVC and Clang.
Add a codegen test verifying that a variadic `i128` is passed as a
pointer while a fixed `i128` is passed by value.
Fix ignore-llvm-version directive in codegen-llvm/array-equality.rs
There needs to be spaces around the dash, otherwise it doesn't work as a range (which matters now that LLVM trunk is 24).
Pointer authentication config and user facing options
This patch brings:
* unified handling of pointer authentication options through:
`-Zpointer-authentication`, with possible values:
`aarch64-jump-table-hardening`, `auth-traps`, `calls`, `elf-got`,
`function-pointer-type-discrimination`, `indirect-gotos`, `init-fini`,
`init-fini-address-discrimination`, `return-addresses`. Toggled with
`+`/`-`.
* centralized handling of pointer authentication features. Session holds
`pointer_auth_config: Option<PointerAuthConfig>`
* encapsulation of schema for function pointers and init/fini through
`PointerAuthSchema`. This allowed for retiring of `PacMetadata`.
* refactor enabling of pointer authentication in code, instead of
relying on the target (`pauthtest`) use the session
disable range-len-try-from.rs on s390x
This optimisation is currently broken on s390x[^1]. It looks like a big-endian issue in general. but as we only know about the test fails on s390x we only disable it there.
Should be reenabled when the problem is fixed and llvm backend is updated.
[^1]: https://github.com/llvm/llvm-project/issues/208712
This patch brings:
* unified handling of pointer authentication options through:
`-Zpointer-authentication`, with possible values:
`aarch64-jump-table-hardening`, `auth-traps`, `calls`, `elf-got`,
`function-pointer-type-discrimination`, `indirect-gotos`, `init-fini`,
`init-fini-address-discrimination`, `return-addresses`. Toggled with
`+`/`-`.
* centralized handling of pointer authentication features. Session holds
`pointer_auth_config: Option<PointerAuthConfig>`
* encapsulation of schema for function pointers and init/fini through
`PointerAuthSchema`. This allowed for retiring of `PacMetadata`.
* refactor enabling of pointer authentication in code, instead of
relying on the target (`pauthtest`) use the session
This optimisation is currently broken on s390x[^1].
It looks like a big-endian issue in general. but as
we only know about the test fails on s390x we only
disable it there.
Should be reenabled when the problem is fixed and llvm
backend is updated.
[^1]: https://github.com/llvm/llvm-project/issues/208712
compiler: Remove `-Zmutable-noalias`
Let us mark the end of an era. The `noalias` optimization has been working relatively well for some time now. There may still be issues to sort out with `noalias`. If so, those are mostly on LLVM's side, where it may need to have a better model of what `noalias` means as that is necessary for soundly inferring it. We also may need to not add `noalias` somewhere we currently do, if our semantics do not actually match that. Either way, it doesn't seem useful to configure on a compilation-wide basis.