645c0fddd2
Previously, it was only put on scalars with range validity invariants like bool, was uninit was obviously invalid for those. Since then, we have normatively declared all uninit primitives to be undefined behavior and can therefore put `noundef` on them. The remaining concern was the `mem::uninitialized` function, which cause quite a lot of UB in the older parts of the ecosystem. This function now doesn't return uninit values anymore, making users of it safe from this change. The only real sources of UB where people could encounter uninit primitives are `MaybeUninit::uninit().assume_init()`, which has always be clear in the docs about being UB and from heap allocations (like reading from the spare capacity of a vec. This is hopefully rare enough to not break anything.
35 lines
1.0 KiB
Rust
35 lines
1.0 KiB
Rust
// compile-flags: -O
|
|
|
|
// Once we're done with llvm 14 and earlier, this test can be deleted.
|
|
|
|
#![crate_type = "lib"]
|
|
|
|
use std::mem::MaybeUninit;
|
|
|
|
// Boxing a `MaybeUninit` value should not copy junk from the stack
|
|
#[no_mangle]
|
|
pub fn box_uninitialized() -> Box<MaybeUninit<usize>> {
|
|
// CHECK-LABEL: @box_uninitialized
|
|
// CHECK-NOT: store
|
|
// CHECK-NOT: alloca
|
|
// CHECK-NOT: memcpy
|
|
// CHECK-NOT: memset
|
|
Box::new(MaybeUninit::uninit())
|
|
}
|
|
|
|
// https://github.com/rust-lang/rust/issues/58201
|
|
#[no_mangle]
|
|
pub fn box_uninitialized2() -> Box<MaybeUninit<[usize; 1024 * 1024]>> {
|
|
// CHECK-LABEL: @box_uninitialized2
|
|
// CHECK-NOT: store
|
|
// CHECK-NOT: alloca
|
|
// CHECK-NOT: memcpy
|
|
// CHECK-NOT: memset
|
|
Box::new(MaybeUninit::uninit())
|
|
}
|
|
|
|
// Hide the LLVM 15+ `allocalign` attribute in the declaration of __rust_alloc
|
|
// from the CHECK-NOT above. We don't check the attributes here because we can't rely
|
|
// on all of them being set until LLVM 15.
|
|
// CHECK: declare noalias{{.*}} @__rust_alloc(i{{[0-9]+}} noundef, i{{[0-9]+.*}} noundef)
|