Skip to content

Commit a99e97a

Browse files
Add 0..=isize::MAX range metadata to size loads from vtables
1 parent 023b513 commit a99e97a

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,18 @@ impl Integer {
775775
}
776776
}
777777

778+
/// Returns the largest signed value that can be represented by this Integer.
779+
#[inline]
780+
pub fn signed_max(self) -> i128 {
781+
match self {
782+
I8 => i8::MAX as i128,
783+
I16 => i16::MAX as i128,
784+
I32 => i32::MAX as i128,
785+
I64 => i64::MAX as i128,
786+
I128 => i128::MAX,
787+
}
788+
}
789+
778790
/// Finds the smallest Integer type which can represent the signed value.
779791
#[inline]
780792
pub fn fit_signed(x: i128) -> Integer {

compiler/rustc_codegen_ssa/src/glue.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
2929
let align = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN)
3030
.get_usize(bx, vtable);
3131

32+
// Size is always <= isize::MAX.
33+
let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128;
34+
bx.range_metadata(size, WrappingRange { start: 0, end: size_bound });
3235
// Alignment is always nonzero.
3336
bx.range_metadata(align, WrappingRange { start: 1, end: !0 });
3437

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
110110
_ => bug!(),
111111
};
112112
let value = meth::VirtualIndex::from_index(idx).get_usize(bx, vtable);
113-
if name == sym::vtable_align {
113+
match name {
114+
// Size is always <= isize::MAX.
115+
sym::vtable_size => {
116+
let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128;
117+
bx.range_metadata(value, WrappingRange { start: 0, end: size_bound });
118+
},
114119
// Alignment is always nonzero.
115-
bx.range_metadata(value, WrappingRange { start: 1, end: !0 });
116-
};
120+
sym::vtable_align => bx.range_metadata(value, WrappingRange { start: 1, end: !0 }),
121+
_ => {}
122+
}
117123
value
118124
}
119125
sym::pref_align_of

src/test/codegen/dst-vtable-align-nonzero.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// compile-flags: -O
22

33
#![crate_type = "lib"]
4+
#![feature(core_intrinsics)]
45

56
// This test checks that we annotate alignment loads from vtables with nonzero range metadata,
67
// and that this allows LLVM to eliminate redundant `align >= 1` checks.
@@ -42,4 +43,21 @@ pub fn does_not_eliminate_runtime_check_when_align_2(
4243
&x.dst
4344
}
4445

46+
// CHECK-LABEL: @align_load_from_align_of_val
47+
#[no_mangle]
48+
pub fn align_load_from_align_of_val(x: &dyn Trait) -> usize {
49+
// CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]]
50+
core::mem::align_of_val(x)
51+
}
52+
53+
// CHECK-LABEL: @align_load_from_vtable_align_intrinsic
54+
#[no_mangle]
55+
pub unsafe fn align_load_from_vtable_align_intrinsic(x: &dyn Trait) -> usize {
56+
let (data, vtable): (*const (), *const ()) = core::mem::transmute(x);
57+
// CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]]
58+
let align = core::intrinsics::vtable_align(vtable);
59+
// make this function unique so it doesn't get merged with the previous
60+
align + 1
61+
}
62+
4563
// CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
#![feature(core_intrinsics)]
5+
6+
// Check that we annotate size loads from vtables with 0..(isize::MAX + 1) range metadata.
7+
8+
pub trait Trait {
9+
fn f(&self);
10+
}
11+
12+
// Note that rustc uses inclusive bounds, but LLVM uses exclusive bounds for range metadata.
13+
// CHECK-LABEL: @generate_exclusive_bound
14+
#[no_mangle]
15+
pub fn generate_exclusive_bound() -> usize {
16+
// CHECK: ret [[USIZE:i[0-9]+]] [[EXCLUSIVE_BOUND:[-0-9]+]]
17+
isize::MAX as usize + 1
18+
}
19+
20+
// CHECK-LABEL: @size_load_from_size_of_val
21+
#[no_mangle]
22+
pub fn size_load_from_size_of_val(x: &dyn Trait) -> usize {
23+
// CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META:![0-9]+]]
24+
core::mem::size_of_val(x)
25+
}
26+
27+
// CHECK-LABEL: @size_load_from_vtable_size_intrinsic
28+
#[no_mangle]
29+
pub unsafe fn size_load_from_vtable_size_intrinsic(x: &dyn Trait) -> usize {
30+
let (data, vtable): (*const (), *const ()) = core::mem::transmute(x);
31+
// CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]]
32+
let size = core::intrinsics::vtable_size(vtable);
33+
// make this function unique so it doesn't get merged with the previous
34+
size + 1
35+
}
36+
37+
// CHECK: [[RANGE_META]] = !{[[USIZE]] 0, [[USIZE]] [[EXCLUSIVE_BOUND]]}

0 commit comments

Comments
 (0)