Skip to content

Commit 5c30d65

Browse files
committed
Auto merge of #93670 - erikdesjardins:noundef, r=nikic
Apply noundef attribute to &T, &mut T, Box<T>, bool This doesn't handle `char` because it's a bit awkward to distinguish it from `u32` at this point in codegen. Note that this _does not_ change whether or not it is UB for `&`, `&mut`, or `Box` to point to undef. It only applies to the pointer itself, not the pointed-to memory. Fixes (partially) #74378. r? `@nikic` cc `@RalfJung`
2 parents 3cfa4de + 4013077 commit 5c30d65

File tree

13 files changed

+94
-91
lines changed

13 files changed

+94
-91
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl ArgAttributeExt for ArgAttribute {
3737
where
3838
F: FnMut(llvm::Attribute),
3939
{
40-
for_each_kind!(self, f, NoAlias, NoCapture, NonNull, ReadOnly, InReg)
40+
for_each_kind!(self, f, NoAlias, NoCapture, NonNull, ReadOnly, InReg, NoUndef)
4141
}
4242
}
4343

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ pub enum Attribute {
189189
StackProtectReq = 30,
190190
StackProtectStrong = 31,
191191
StackProtect = 32,
192+
NoUndef = 33,
192193
}
193194

194195
/// LLVMIntPredicate

compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ enum LLVMRustAttribute {
8282
StackProtectReq = 30,
8383
StackProtectStrong = 31,
8484
StackProtect = 32,
85+
NoUndef = 33,
8586
};
8687

8788
typedef struct OpaqueRustString *RustStringRef;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
224224
return Attribute::StackProtectStrong;
225225
case StackProtect:
226226
return Attribute::StackProtect;
227+
case NoUndef:
228+
return Attribute::NoUndef;
227229
}
228230
report_fatal_error("bad AttributeKind");
229231
}

compiler/rustc_middle/src/ty/layout.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -3051,9 +3051,10 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
30513051
layout: TyAndLayout<'tcx>,
30523052
offset: Size,
30533053
is_return: bool| {
3054-
// Booleans are always an i1 that needs to be zero-extended.
3054+
// Booleans are always a noundef i1 that needs to be zero-extended.
30553055
if scalar.is_bool() {
30563056
attrs.ext(ArgExtension::Zext);
3057+
attrs.set(ArgAttribute::NoUndef);
30573058
return;
30583059
}
30593060

@@ -3078,6 +3079,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
30783079
_ => pointee.size,
30793080
};
30803081

3082+
// `Box`, `&T`, and `&mut T` cannot be undef.
3083+
// Note that this only applies to the value of the pointer itself;
3084+
// this attribute doesn't make it UB for the pointed-to data to be undef.
3085+
attrs.set(ArgAttribute::NoUndef);
3086+
30813087
// `Box` pointer parameters never alias because ownership is transferred
30823088
// `&mut` pointer parameters never alias other parameters,
30833089
// or mutable global data

compiler/rustc_target/src/abi/call/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ mod attr_impl {
7474
// or not to actually emit the attribute. It can also be controlled
7575
// with the `-Zmutable-noalias` debugging option.
7676
const NoAliasMutRef = 1 << 6;
77+
const NoUndef = 1 << 7;
7778
}
7879
}
7980
}
@@ -495,7 +496,11 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
495496
// For non-immediate arguments the callee gets its own copy of
496497
// the value on the stack, so there are no aliases. It's also
497498
// program-invisible so can't possibly capture
498-
attrs.set(ArgAttribute::NoAlias).set(ArgAttribute::NoCapture).set(ArgAttribute::NonNull);
499+
attrs
500+
.set(ArgAttribute::NoAlias)
501+
.set(ArgAttribute::NoCapture)
502+
.set(ArgAttribute::NonNull)
503+
.set(ArgAttribute::NoUndef);
499504
attrs.pointee_size = layout.size;
500505
// FIXME(eddyb) We should be doing this, but at least on
501506
// i686-pc-windows-msvc, it results in wrong stack offsets.

src/test/codegen/fastcall-inreg.rs

+10-45
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,17 @@
22
// as "inreg" like the C/C++ compilers for the platforms.
33
// x86 only.
44

5-
// ignore-aarch64
6-
// ignore-aarch64_be
7-
// ignore-arm
8-
// ignore-armeb
9-
// ignore-avr
10-
// ignore-bpfel
11-
// ignore-bpfeb
12-
// ignore-hexagon
13-
// ignore-mips
14-
// ignore-mips64
15-
// ignore-msp430
16-
// ignore-powerpc64
17-
// ignore-powerpc64le
18-
// ignore-powerpc
19-
// ignore-r600
20-
// ignore-riscv64
21-
// ignore-amdgcn
22-
// ignore-sparc
23-
// ignore-sparc64
24-
// ignore-sparcv9
25-
// ignore-sparcel
26-
// ignore-s390x
27-
// ignore-tce
28-
// ignore-thumb
29-
// ignore-thumbeb
30-
// ignore-x86_64
31-
// ignore-xcore
32-
// ignore-nvptx
33-
// ignore-nvptx64
34-
// ignore-le32
35-
// ignore-le64
36-
// ignore-amdil
37-
// ignore-amdil64
38-
// ignore-hsail
39-
// ignore-hsail64
40-
// ignore-spir
41-
// ignore-spir64
42-
// ignore-kalimba
43-
// ignore-shave
44-
// ignore-wasm32
45-
// ignore-wasm64
46-
// ignore-emscripten
47-
48-
// compile-flags: -C no-prepopulate-passes
5+
// compile-flags: --target i686-unknown-linux-gnu -C no-prepopulate-passes
6+
// needs-llvm-components: x86
497

508
#![crate_type = "lib"]
9+
#![no_core]
10+
#![feature(no_core, lang_items)]
11+
12+
#[lang = "sized"]
13+
trait Sized {}
14+
#[lang = "copy"]
15+
trait Copy {}
5116

5217
pub mod tests {
5318
// CHECK: @f1(i32 inreg %_1, i32 inreg %_2, i32 %_3)
@@ -70,7 +35,7 @@ pub mod tests {
7035
#[no_mangle]
7136
pub extern "fastcall" fn f5(_: i64, _: i32) {}
7237

73-
// CHECK: @f6(i1 inreg zeroext %_1, i32 inreg %_2, i32 %_3)
38+
// CHECK: @f6(i1 inreg noundef zeroext %_1, i32 inreg %_2, i32 %_3)
7439
#[no_mangle]
7540
pub extern "fastcall" fn f6(_: bool, _: i32, _: i32) {}
7641
}

src/test/codegen/function-arguments.rs

+43-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#![crate_type = "lib"]
44
#![feature(rustc_attrs)]
55

6+
use std::mem::MaybeUninit;
7+
68
pub struct S {
79
_field: [i32; 8],
810
}
@@ -11,68 +13,79 @@ pub struct UnsafeInner {
1113
_field: std::cell::UnsafeCell<i16>,
1214
}
1315

14-
// CHECK: zeroext i1 @boolean(i1 zeroext %x)
16+
// CHECK: noundef zeroext i1 @boolean(i1 noundef zeroext %x)
1517
#[no_mangle]
1618
pub fn boolean(x: bool) -> bool {
1719
x
1820
}
1921

20-
// CHECK: @readonly_borrow(i32* noalias readonly align 4 dereferenceable(4) %_1)
22+
// CHECK: i8 @maybeuninit_boolean(i8 %x)
23+
#[no_mangle]
24+
pub fn maybeuninit_boolean(x: MaybeUninit<bool>) -> MaybeUninit<bool> {
25+
x
26+
}
27+
28+
// CHECK: @readonly_borrow(i32* noalias noundef readonly align 4 dereferenceable(4) %_1)
2129
// FIXME #25759 This should also have `nocapture`
2230
#[no_mangle]
2331
pub fn readonly_borrow(_: &i32) {
2432
}
2533

26-
// CHECK: @static_borrow(i32* noalias readonly align 4 dereferenceable(4) %_1)
34+
// CHECK: @static_borrow(i32* noalias noundef readonly align 4 dereferenceable(4) %_1)
2735
// static borrow may be captured
2836
#[no_mangle]
2937
pub fn static_borrow(_: &'static i32) {
3038
}
3139

32-
// CHECK: @named_borrow(i32* noalias readonly align 4 dereferenceable(4) %_1)
40+
// CHECK: @named_borrow(i32* noalias noundef readonly align 4 dereferenceable(4) %_1)
3341
// borrow with named lifetime may be captured
3442
#[no_mangle]
3543
pub fn named_borrow<'r>(_: &'r i32) {
3644
}
3745

38-
// CHECK: @unsafe_borrow(i16* align 2 dereferenceable(2) %_1)
46+
// CHECK: @unsafe_borrow(i16* noundef align 2 dereferenceable(2) %_1)
3947
// unsafe interior means this isn't actually readonly and there may be aliases ...
4048
#[no_mangle]
4149
pub fn unsafe_borrow(_: &UnsafeInner) {
4250
}
4351

44-
// CHECK: @mutable_unsafe_borrow(i16* noalias align 2 dereferenceable(2) %_1)
52+
// CHECK: @mutable_unsafe_borrow(i16* noalias noundef align 2 dereferenceable(2) %_1)
4553
// ... unless this is a mutable borrow, those never alias
4654
#[no_mangle]
4755
pub fn mutable_unsafe_borrow(_: &mut UnsafeInner) {
4856
}
4957

50-
// CHECK: @mutable_borrow(i32* noalias align 4 dereferenceable(4) %_1)
58+
// CHECK: @mutable_borrow(i32* noalias noundef align 4 dereferenceable(4) %_1)
5159
// FIXME #25759 This should also have `nocapture`
5260
#[no_mangle]
5361
pub fn mutable_borrow(_: &mut i32) {
5462
}
5563

56-
// CHECK: @indirect_struct(%S* noalias nocapture dereferenceable(32) %_1)
64+
// CHECK: @indirect_struct(%S* noalias nocapture noundef dereferenceable(32) %_1)
5765
#[no_mangle]
5866
pub fn indirect_struct(_: S) {
5967
}
6068

61-
// CHECK: @borrowed_struct(%S* noalias readonly align 4 dereferenceable(32) %_1)
69+
// CHECK: @borrowed_struct(%S* noalias noundef readonly align 4 dereferenceable(32) %_1)
6270
// FIXME #25759 This should also have `nocapture`
6371
#[no_mangle]
6472
pub fn borrowed_struct(_: &S) {
6573
}
6674

75+
// CHECK: @raw_struct(%S* %_1)
76+
#[no_mangle]
77+
pub fn raw_struct(_: *const S) {
78+
}
79+
6780
// `Box` can get deallocated during execution of the function, so it should
6881
// not get `dereferenceable`.
69-
// CHECK: noalias nonnull align 4 i32* @_box(i32* noalias nonnull align 4 %x)
82+
// CHECK: noalias noundef nonnull align 4 i32* @_box(i32* noalias noundef nonnull align 4 %x)
7083
#[no_mangle]
7184
pub fn _box(x: Box<i32>) -> Box<i32> {
7285
x
7386
}
7487

75-
// CHECK: @struct_return(%S* noalias nocapture sret(%S) dereferenceable(32){{( %0)?}})
88+
// CHECK: @struct_return(%S* noalias nocapture noundef sret(%S) dereferenceable(32){{( %0)?}})
7689
#[no_mangle]
7790
pub fn struct_return() -> S {
7891
S {
@@ -86,48 +99,58 @@ pub fn struct_return() -> S {
8699
pub fn helper(_: usize) {
87100
}
88101

89-
// CHECK: @slice([0 x i8]* noalias nonnull readonly align 1 %_1.0, [[USIZE]] %_1.1)
102+
// CHECK: @slice([0 x i8]* noalias noundef nonnull readonly align 1 %_1.0, [[USIZE]] %_1.1)
90103
// FIXME #25759 This should also have `nocapture`
91104
#[no_mangle]
92105
pub fn slice(_: &[u8]) {
93106
}
94107

95-
// CHECK: @mutable_slice([0 x i8]* noalias nonnull align 1 %_1.0, [[USIZE]] %_1.1)
108+
// CHECK: @mutable_slice([0 x i8]* noalias noundef nonnull align 1 %_1.0, [[USIZE]] %_1.1)
96109
// FIXME #25759 This should also have `nocapture`
97110
#[no_mangle]
98111
pub fn mutable_slice(_: &mut [u8]) {
99112
}
100113

101-
// CHECK: @unsafe_slice([0 x i16]* nonnull align 2 %_1.0, [[USIZE]] %_1.1)
114+
// CHECK: @unsafe_slice([0 x i16]* noundef nonnull align 2 %_1.0, [[USIZE]] %_1.1)
102115
// unsafe interior means this isn't actually readonly and there may be aliases ...
103116
#[no_mangle]
104117
pub fn unsafe_slice(_: &[UnsafeInner]) {
105118
}
106119

107-
// CHECK: @str([0 x i8]* noalias nonnull readonly align 1 %_1.0, [[USIZE]] %_1.1)
120+
// CHECK: @raw_slice([0 x i8]* %_1.0, [[USIZE]] %_1.1)
121+
#[no_mangle]
122+
pub fn raw_slice(_: *const [u8]) {
123+
}
124+
125+
// CHECK: @str([0 x i8]* noalias noundef nonnull readonly align 1 %_1.0, [[USIZE]] %_1.1)
108126
// FIXME #25759 This should also have `nocapture`
109127
#[no_mangle]
110128
pub fn str(_: &[u8]) {
111129
}
112130

113-
// CHECK: @trait_borrow({}* nonnull align 1 %_1.0, [3 x [[USIZE]]]* noalias readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
131+
// CHECK: @trait_borrow({}* noundef nonnull align 1 %_1.0, [3 x [[USIZE]]]* noalias noundef readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
114132
// FIXME #25759 This should also have `nocapture`
115133
#[no_mangle]
116134
pub fn trait_borrow(_: &Drop) {
117135
}
118136

119-
// CHECK: @trait_box({}* noalias nonnull align 1{{( %0)?}}, [3 x [[USIZE]]]* noalias readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}})
137+
// CHECK: @trait_raw({}* %_1.0, [3 x [[USIZE]]]* noalias noundef readonly align {{.*}} dereferenceable({{.*}}) %_1.1)
138+
#[no_mangle]
139+
pub fn trait_raw(_: *const Drop) {
140+
}
141+
142+
// CHECK: @trait_box({}* noalias noundef nonnull align 1{{( %0)?}}, [3 x [[USIZE]]]* noalias noundef readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}})
120143
#[no_mangle]
121144
pub fn trait_box(_: Box<Drop>) {
122145
}
123146

124-
// CHECK: { i8*, i8* } @trait_option(i8* noalias align 1 %x.0, i8* %x.1)
147+
// CHECK: { i8*, i8* } @trait_option(i8* noalias noundef align 1 %x.0, i8* %x.1)
125148
#[no_mangle]
126149
pub fn trait_option(x: Option<Box<Drop>>) -> Option<Box<Drop>> {
127150
x
128151
}
129152

130-
// CHECK: { [0 x i16]*, [[USIZE]] } @return_slice([0 x i16]* noalias nonnull readonly align 2 %x.0, [[USIZE]] %x.1)
153+
// CHECK: { [0 x i16]*, [[USIZE]] } @return_slice([0 x i16]* noalias noundef nonnull readonly align 2 %x.0, [[USIZE]] %x.1)
131154
#[no_mangle]
132155
pub fn return_slice(x: &[u16]) -> &[u16] {
133156
x
@@ -139,7 +162,7 @@ pub fn enum_id_1(x: Option<Result<u16, u16>>) -> Option<Result<u16, u16>> {
139162
x
140163
}
141164

142-
// CHECK: { i8, i8 } @enum_id_2(i1 zeroext %x.0, i8 %x.1)
165+
// CHECK: { i8, i8 } @enum_id_2(i1 noundef zeroext %x.0, i8 %x.1)
143166
#[no_mangle]
144167
pub fn enum_id_2(x: Option<u8>) -> Option<u8> {
145168
x

src/test/codegen/packed.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub struct BigPacked2 {
5252
#[no_mangle]
5353
pub fn call_pkd1(f: fn() -> Array) -> BigPacked1 {
5454
// CHECK: [[ALLOCA:%[_a-z0-9]+]] = alloca %Array
55-
// CHECK: call void %{{.*}}(%Array* noalias nocapture sret{{.*}} dereferenceable(32) [[ALLOCA]])
55+
// CHECK: call void %{{.*}}(%Array* noalias nocapture noundef sret{{.*}} dereferenceable(32) [[ALLOCA]])
5656
// CHECK: call void @llvm.memcpy.{{.*}}(i8* align 1 %{{.*}}, i8* align 4 %{{.*}}, i{{[0-9]+}} 32, i1 false)
5757
// check that calls whose destination is a field of a packed struct
5858
// go through an alloca rather than calling the function with an
@@ -64,7 +64,7 @@ pub fn call_pkd1(f: fn() -> Array) -> BigPacked1 {
6464
#[no_mangle]
6565
pub fn call_pkd2(f: fn() -> Array) -> BigPacked2 {
6666
// CHECK: [[ALLOCA:%[_a-z0-9]+]] = alloca %Array
67-
// CHECK: call void %{{.*}}(%Array* noalias nocapture sret{{.*}} dereferenceable(32) [[ALLOCA]])
67+
// CHECK: call void %{{.*}}(%Array* noalias nocapture noundef sret{{.*}} dereferenceable(32) [[ALLOCA]])
6868
// CHECK: call void @llvm.memcpy.{{.*}}(i8* align 2 %{{.*}}, i8* align 4 %{{.*}}, i{{[0-9]+}} 32, i1 false)
6969
// check that calls whose destination is a field of a packed struct
7070
// go through an alloca rather than calling the function with an

0 commit comments

Comments
 (0)