Skip to content

Commit d8fcbdf

Browse files
committed
Try enabling precondition checks on ptr::{read,write}
1 parent 1daec06 commit d8fcbdf

13 files changed

+47
-15
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
10141014
rustc_no_mir_inline, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
10151015
"#[rustc_no_mir_inline] prevents the MIR inliner from inlining a function while not affecting codegen"
10161016
),
1017+
rustc_attr!(
1018+
rustc_no_ubchecks, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
1019+
"#[rustc_no_ubchecks] asks the compiler to delete UB checks from a function"
1020+
),
10171021

10181022
// ==========================================================================
10191023
// Internal attributes, Testing:

compiler/rustc_mir_transform/src/instsimplify.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,27 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
3131
}
3232

3333
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
34+
let def_id = body.source.def_id();
3435
let ctx = InstSimplifyContext {
3536
tcx,
3637
local_decls: &body.local_decls,
3738
typing_env: body.typing_env(tcx),
3839
};
3940
let preserve_ub_checks =
4041
attr::contains_name(tcx.hir().krate_attrs(), sym::rustc_preserve_ub_checks);
42+
let remove_ub_checks = if tcx.is_coroutine(def_id) {
43+
false
44+
} else {
45+
tcx.has_attr(def_id, sym::rustc_no_ubchecks)
46+
};
4147
for block in body.basic_blocks.as_mut() {
4248
for statement in block.statements.iter_mut() {
4349
match statement.kind {
4450
StatementKind::Assign(box (_place, ref mut rvalue)) => {
45-
if !preserve_ub_checks {
46-
ctx.simplify_ub_check(rvalue);
51+
if remove_ub_checks {
52+
ctx.simplify_ub_check(rvalue, false);
53+
} else if !preserve_ub_checks {
54+
ctx.simplify_ub_check(rvalue, tcx.sess.ub_checks());
4755
}
4856
ctx.simplify_bool_cmp(rvalue);
4957
ctx.simplify_ref_deref(rvalue);
@@ -160,9 +168,9 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
160168
}
161169
}
162170

163-
fn simplify_ub_check(&self, rvalue: &mut Rvalue<'tcx>) {
171+
fn simplify_ub_check(&self, rvalue: &mut Rvalue<'tcx>, ub_checks: bool) {
164172
if let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue {
165-
let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());
173+
let const_ = Const::from_bool(self.tcx, ub_checks);
166174
let constant = ConstOperand { span: DUMMY_SP, const_, user_ty: None };
167175
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
168176
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,7 @@ symbols! {
17351735
rustc_never_returns_null_ptr,
17361736
rustc_never_type_options,
17371737
rustc_no_mir_inline,
1738+
rustc_no_ubchecks,
17381739
rustc_nonnull_optimization_guaranteed,
17391740
rustc_nounwind,
17401741
rustc_object_lifetime_default,

library/alloc/src/boxed.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
13871387
#[must_use = "losing the pointer will leak memory"]
13881388
#[unstable(feature = "allocator_api", issue = "32838")]
13891389
#[inline]
1390+
#[cfg_attr(not(bootstrap), rustc_no_ubchecks)]
13901391
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
13911392
let mut b = mem::ManuallyDrop::new(b);
13921393
// We carefully get the raw pointer out in a way that Miri's aliasing model understands what

library/core/src/mem/manually_drop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ impl<T> ManuallyDrop<T> {
217217
#[must_use = "if you don't need the value, you can use `ManuallyDrop::drop` instead"]
218218
#[stable(feature = "manually_drop_take", since = "1.42.0")]
219219
#[inline]
220+
#[cfg_attr(not(bootstrap), rustc_no_ubchecks)]
220221
pub unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
221222
// SAFETY: we are reading from a reference, which is guaranteed
222223
// to be valid for reads.

library/core/src/mem/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ pub fn take<T: Default>(dest: &mut T) -> T {
857857
#[must_use = "if you don't need the old value, you can just assign the new value directly"]
858858
#[rustc_const_stable(feature = "const_replace", since = "1.83.0")]
859859
#[cfg_attr(not(test), rustc_diagnostic_item = "mem_replace")]
860+
#[cfg_attr(not(bootstrap), rustc_no_ubchecks)]
860861
pub const fn replace<T>(dest: &mut T, src: T) -> T {
861862
// It may be tempting to use `swap` to avoid `unsafe` here. Don't!
862863
// The compiler optimizes the implementation below to two `memcpy`s

library/core/src/ptr/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
11301130
/// LLVM can vectorize this (at least it can for the power-of-two-sized types
11311131
/// `swap_nonoverlapping` tries to use) so no need to manually SIMD it.
11321132
#[inline]
1133+
#[cfg_attr(not(bootstrap), rustc_no_ubchecks)]
11331134
const unsafe fn swap_nonoverlapping_simple_untyped<T>(x: *mut T, y: *mut T, count: usize) {
11341135
let x = x.cast::<MaybeUninit<T>>();
11351136
let y = y.cast::<MaybeUninit<T>>();
@@ -1361,7 +1362,6 @@ pub const unsafe fn read<T>(src: *const T) -> T {
13611362

13621363
// SAFETY: the caller must guarantee that `src` is valid for reads.
13631364
unsafe {
1364-
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
13651365
ub_checks::assert_unsafe_precondition!(
13661366
check_language_ub,
13671367
"ptr::read requires that the pointer argument is aligned and non-null",
@@ -1565,7 +1565,6 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
15651565
// `dst` cannot overlap `src` because the caller has mutable access
15661566
// to `dst` while `src` is owned by this function.
15671567
unsafe {
1568-
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
15691568
ub_checks::assert_unsafe_precondition!(
15701569
check_language_ub,
15711570
"ptr::write requires that the pointer argument is aligned and non-null",

tests/codegen/mem-replace-big-type.rs

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
// known to be `1` after inlining).
55

66
//@ compile-flags: -C no-prepopulate-passes -Zinline-mir=no
7-
//@ ignore-std-debug-assertions
8-
// Reason: precondition checks in ptr::read make them a bad candidate for MIR inlining
97
//@ needs-deterministic-layouts
108

119
#![crate_type = "lib"]

tests/codegen/mem-replace-simple-type.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ compile-flags: -O -C no-prepopulate-passes
22
//@ only-x86_64 (to not worry about usize differing)
3-
//@ ignore-std-debug-assertions
4-
// Reason: precondition checks make mem::replace not a candidate for MIR inlining
53

64
#![crate_type = "lib"]
75

tests/mir-opt/pre-codegen/mem_replace.mem_replace.PreCodegen.after.panic-abort.mir

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@ fn mem_replace(_1: &mut u32, _2: u32) -> u32 {
66
let mut _0: u32;
77
scope 1 (inlined std::mem::replace::<u32>) {
88
scope 2 {
9-
scope 4 (inlined std::ptr::write::<u32>) {
9+
scope 7 (inlined std::ptr::write::<u32>) {
10+
scope 8 (inlined core::ub_checks::check_language_ub) {
11+
scope 9 (inlined core::ub_checks::check_language_ub::runtime) {
12+
}
13+
}
14+
scope 10 (inlined align_of::<u32>) {
15+
}
1016
}
1117
}
1218
scope 3 (inlined std::ptr::read::<u32>) {
19+
scope 4 (inlined core::ub_checks::check_language_ub) {
20+
scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
21+
}
22+
}
23+
scope 6 (inlined align_of::<u32>) {
24+
}
1325
}
1426
}
1527

tests/mir-opt/pre-codegen/mem_replace.mem_replace.PreCodegen.after.panic-unwind.mir

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@ fn mem_replace(_1: &mut u32, _2: u32) -> u32 {
66
let mut _0: u32;
77
scope 1 (inlined std::mem::replace::<u32>) {
88
scope 2 {
9-
scope 4 (inlined std::ptr::write::<u32>) {
9+
scope 7 (inlined std::ptr::write::<u32>) {
10+
scope 8 (inlined core::ub_checks::check_language_ub) {
11+
scope 9 (inlined core::ub_checks::check_language_ub::runtime) {
12+
}
13+
}
14+
scope 10 (inlined align_of::<u32>) {
15+
}
1016
}
1117
}
1218
scope 3 (inlined std::ptr::read::<u32>) {
19+
scope 4 (inlined core::ub_checks::check_language_ub) {
20+
scope 5 (inlined core::ub_checks::check_language_ub::runtime) {
21+
}
22+
}
23+
scope 6 (inlined align_of::<u32>) {
24+
}
1325
}
1426
}
1527

tests/mir-opt/pre-codegen/mem_replace.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// skip-filecheck
22
//@ compile-flags: -O -C debuginfo=0 -Zmir-opt-level=2 -Zinline-mir
3-
//@ ignore-std-debug-assertions
4-
// Reason: precondition checks on ptr::read/write are under cfg(debug_assertions)
53
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
64

75
#![crate_type = "lib"]

tests/mir-opt/pre-codegen/ptr_offset.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// skip-filecheck
22
//@ compile-flags: -O -C debuginfo=0 -Zmir-opt-level=2 -Zinline-mir
3-
//@ ignore-std-debug-assertions (precondition checks are under cfg(debug_assertions))
43
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
54

65
#![crate_type = "lib"]

0 commit comments

Comments
 (0)