Skip to content

Commit bbda6ab

Browse files
committed
Auto merge of rust-lang#129498 - saethlin:ptr-read-write-precondition, r=<try>
Try enabling precondition checks on ptr::{read,write} The overhead here is probably too much, but let's have the measurement anyway. This will fail at least one codegen test. r? `@ghost` - [ ] Ralf says: If we keep this attribute, the docs on the intrinsic should be updated.
2 parents d6c8169 + f9b6f51 commit bbda6ab

13 files changed

+59
-13
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
999999
rustc_no_mir_inline, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
10001000
"#[rustc_no_mir_inline] prevents the MIR inliner from inlining a function while not affecting codegen"
10011001
),
1002+
rustc_attr!(
1003+
rustc_no_ubchecks, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
1004+
"#[rustc_no_ubchecks] asks the compiler to delete UB checks from a function"
1005+
),
10021006
rustc_attr!(
10031007
rustc_intrinsic_must_be_overridden, Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::Yes,
10041008
"the `#[rustc_intrinsic_must_be_overridden]` attribute is used to declare intrinsics without real bodies",

compiler/rustc_mir_transform/src/instsimplify.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,32 @@ impl<'tcx> MirPass<'tcx> for InstSimplify {
3737
}
3838

3939
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
40+
let def_id = body.source.def_id();
4041
let ctx = InstSimplifyContext {
4142
tcx,
4243
local_decls: &body.local_decls,
43-
param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()),
44+
param_env: tcx.param_env_reveal_all_normalized(def_id),
4445
};
4546
let preserve_ub_checks =
4647
attr::contains_name(tcx.hir().krate_attrs(), sym::rustc_preserve_ub_checks);
48+
// FIXME(async_closures) tcx.has_attr on async closures seems to ICE. Not sure why.
49+
let remove_ub_checks = if tcx.is_coroutine(def_id) {
50+
false
51+
} else {
52+
tcx.has_attr(def_id, sym::rustc_no_ubchecks)
53+
};
4754
for block in body.basic_blocks.as_mut() {
4855
for statement in block.statements.iter_mut() {
4956
match statement.kind {
5057
StatementKind::Assign(box (_place, ref mut rvalue)) => {
51-
if !preserve_ub_checks {
52-
ctx.simplify_ub_check(&statement.source_info, rvalue);
58+
if remove_ub_checks {
59+
ctx.simplify_ub_check(&statement.source_info, rvalue, false);
60+
} else if !preserve_ub_checks {
61+
ctx.simplify_ub_check(
62+
&statement.source_info,
63+
rvalue,
64+
tcx.sess.ub_checks(),
65+
);
5366
}
5467
ctx.simplify_bool_cmp(&statement.source_info, rvalue);
5568
ctx.simplify_ref_deref(&statement.source_info, rvalue);
@@ -199,9 +212,14 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
199212
}
200213
}
201214

202-
fn simplify_ub_check(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
215+
fn simplify_ub_check(
216+
&self,
217+
source_info: &SourceInfo,
218+
rvalue: &mut Rvalue<'tcx>,
219+
ub_checks: bool,
220+
) {
203221
if let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue {
204-
let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());
222+
let const_ = Const::from_bool(self.tcx, ub_checks);
205223
let constant = ConstOperand { span: source_info.span, const_, user_ty: None };
206224
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
207225
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,7 @@ symbols! {
16601660
rustc_never_returns_null_ptr,
16611661
rustc_never_type_options,
16621662
rustc_no_mir_inline,
1663+
rustc_no_ubchecks,
16631664
rustc_nonnull_optimization_guaranteed,
16641665
rustc_nounwind,
16651666
rustc_object_lifetime_default,

library/alloc/src/boxed.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
12201220
#[must_use = "losing the pointer will leak memory"]
12211221
#[unstable(feature = "allocator_api", issue = "32838")]
12221222
#[inline]
1223+
#[cfg_attr(not(bootstrap), rustc_no_ubchecks)]
12231224
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
12241225
let mut b = mem::ManuallyDrop::new(b);
12251226
// 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
@@ -111,6 +111,7 @@ impl<T> ManuallyDrop<T> {
111111
#[must_use = "if you don't need the value, you can use `ManuallyDrop::drop` instead"]
112112
#[stable(feature = "manually_drop_take", since = "1.42.0")]
113113
#[inline]
114+
#[cfg_attr(not(bootstrap), rustc_no_ubchecks)]
114115
pub unsafe fn take(slot: &mut ManuallyDrop<T>) -> T {
115116
// SAFETY: we are reading from a reference, which is guaranteed
116117
// to be valid for reads.

library/core/src/mem/mod.rs

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

library/core/src/ptr/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
11951195
/// `swap_nonoverlapping` tries to use) so no need to manually SIMD it.
11961196
#[inline]
11971197
#[rustc_const_unstable(feature = "const_swap", issue = "83163")]
1198+
#[cfg_attr(not(bootstrap), rustc_no_ubchecks)]
11981199
const unsafe fn swap_nonoverlapping_simple_untyped<T>(x: *mut T, y: *mut T, count: usize) {
11991200
let x = x.cast::<MaybeUninit<T>>();
12001201
let y = y.cast::<MaybeUninit<T>>();
@@ -1425,7 +1426,7 @@ pub const unsafe fn read<T>(src: *const T) -> T {
14251426

14261427
// SAFETY: the caller must guarantee that `src` is valid for reads.
14271428
unsafe {
1428-
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
1429+
#[cfg(debug_assertions)]
14291430
ub_checks::assert_unsafe_precondition!(
14301431
check_language_ub,
14311432
"ptr::read requires that the pointer argument is aligned and non-null",
@@ -1634,7 +1635,6 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
16341635
// `dst` cannot overlap `src` because the caller has mutable access
16351636
// to `dst` while `src` is owned by this function.
16361637
unsafe {
1637-
#[cfg(debug_assertions)] // Too expensive to always enable (for now?)
16381638
ub_checks::assert_unsafe_precondition!(
16391639
check_language_ub,
16401640
"ptr::write requires that the pointer argument is aligned and non-null",

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

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

66
//@ compile-flags: -C no-prepopulate-passes -Zinline-mir=no
7-
//@ ignore-debug: precondition checks in ptr::read make them a bad candidate for MIR inlining
87

98
#![crate_type = "lib"]
109

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

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

54
#![crate_type = "lib"]
65

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

-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-debug: precondition checks on ptr::read/write are under cfg(debug_assertions)
43
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
54

65
#![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-debug: precondition checks are under cfg(debug_assertions)
43
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
54

65
#![crate_type = "lib"]

0 commit comments

Comments
 (0)