Skip to content

Commit 294d980

Browse files
committed
Put checks that detect UB under their own flag below debug_assertions
1 parent c7491b9 commit 294d980

25 files changed

+50
-22
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ fn codegen_stmt<'tcx>(
789789
layout.offset_of_subfield(fx, fields.iter()).bytes()
790790
}
791791
NullOp::UbChecks => {
792-
let val = fx.tcx.sess.opts.debug_assertions;
792+
let val = fx.tcx.sess.ub_checks();
793793
let val = CValue::by_val(
794794
fx.bcx.ins().iconst(types::I8, i64::try_from(val).unwrap()),
795795
fx.layout_of(fx.tcx.types.bool),

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
682682
bx.cx().const_usize(val)
683683
}
684684
mir::NullOp::UbChecks => {
685-
let val = bx.tcx().sess.opts.debug_assertions;
685+
let val = bx.tcx().sess.ub_checks();
686686
bx.cx().const_bool(val)
687687
}
688688
};

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ fn test_unstable_options_tracking_hash() {
847847
tracked!(trap_unreachable, Some(false));
848848
tracked!(treat_err_as_bug, NonZero::new(1));
849849
tracked!(tune_cpu, Some(String::from("abc")));
850+
tracked!(ub_checks, Some(false));
850851
tracked!(uninit_const_chunk_threshold, 123);
851852
tracked!(unleash_the_miri_inside_of_you, true);
852853
tracked!(use_ctors_section, Some(true));

compiler/rustc_mir_transform/src/check_alignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'tcx> MirPass<'tcx> for CheckAlignment {
1616
if sess.target.llvm_target == "i686-pc-windows-msvc" {
1717
return false;
1818
}
19-
sess.opts.debug_assertions
19+
sess.ub_checks()
2020
}
2121

2222
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/instsimplify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
149149

150150
fn simplify_ub_check(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
151151
if let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue {
152-
let const_ = Const::from_bool(self.tcx, self.tcx.sess.opts.debug_assertions);
152+
let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());
153153
let constant = ConstOperand { span: source_info.span, const_, user_ty: None };
154154
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
155155
}

compiler/rustc_session/src/options.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,9 @@ written to standard error output)"),
19941994
"in diagnostics, use heuristics to shorten paths referring to items"),
19951995
tune_cpu: Option<String> = (None, parse_opt_string, [TRACKED],
19961996
"select processor to schedule for (`rustc --print target-cpus` for details)"),
1997+
#[rustc_lint_opt_deny_field_access("use `Session::ub_checks` instead of this field")]
1998+
ub_checks: Option<bool> = (None, parse_opt_bool, [TRACKED],
1999+
"emit runtime checks for Undefined Behavior (default: -Cdebug-assertions)"),
19972000
ui_testing: bool = (false, parse_bool, [UNTRACKED],
19982001
"emit compiler diagnostics in a form suitable for UI testing (default: no)"),
19992002
uninit_const_chunk_threshold: usize = (16, parse_number, [TRACKED],

compiler/rustc_session/src/session.rs

+4
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,10 @@ impl Session {
735735
self.opts.cg.overflow_checks.unwrap_or(self.opts.debug_assertions)
736736
}
737737

738+
pub fn ub_checks(&self) -> bool {
739+
self.opts.unstable_opts.ub_checks.unwrap_or(self.opts.debug_assertions)
740+
}
741+
738742
pub fn relocation_model(&self) -> RelocModel {
739743
self.opts.cg.relocation_model.unwrap_or(self.target.relocation_model)
740744
}

src/tools/miri/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub const MIRI_DEFAULT_ARGS: &[&str] = &[
144144
"-Zmir-emit-retag",
145145
"-Zmir-keep-place-mention",
146146
"-Zmir-opt-level=0",
147-
"-Zmir-enable-passes=-CheckAlignment",
147+
"-Zub-checks=no",
148148
// Deduplicating diagnostics means we miss events when tracking what happens during an
149149
// execution. Let's not do that.
150150
"-Zdeduplicate-diagnostics=no",

src/tools/miri/tests/fail/unaligned_pointers/alignment.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@normalize-stderr-test: "\| +\^+" -> "| ^"
2-
//@compile-flags: -Cdebug-assertions=no
32

43
fn main() {
54
// No retry needed, this fails reliably.

src/tools/miri/tests/fail/unaligned_pointers/atomic_unaligned.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@compile-flags: -Zmiri-symbolic-alignment-check -Cdebug-assertions=no
1+
//@compile-flags: -Zmiri-symbolic-alignment-check
22
#![feature(core_intrinsics)]
33

44
fn main() {

src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@compile-flags: -Cdebug-assertions=no
2-
31
#[repr(transparent)]
42
struct HasDrop(u8);
53

src/tools/miri/tests/fail/unaligned_pointers/dyn_alignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// should find the bug even without, but gets masked by optimizations
2-
//@compile-flags: -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
2+
//@compile-flags: -Zmiri-disable-stacked-borrows
33
//@normalize-stderr-test: "but found [0-9]+" -> "but found $$ALIGN"
44

55
#[repr(align(256))]

src/tools/miri/tests/fail/unaligned_pointers/intptrcast_alignment_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@compile-flags: -Zmiri-symbolic-alignment-check -Zmiri-permissive-provenance -Cdebug-assertions=no
1+
//@compile-flags: -Zmiri-symbolic-alignment-check -Zmiri-permissive-provenance
22
// With the symbolic alignment check, even with intptrcast and without
33
// validation, we want to be *sure* to catch bugs that arise from pointers being
44
// insufficiently aligned. The only way to achieve that is not to let programs

src/tools/miri/tests/fail/unaligned_pointers/reference_to_packed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without SB
2-
//@compile-flags: -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
2+
//@compile-flags: -Zmiri-disable-stacked-borrows
33

44
#![allow(dead_code, unused_variables)]
55

src/tools/miri/tests/fail/unaligned_pointers/unaligned_ptr1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without validation or Stacked Borrows.
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

44
fn main() {
55
// Try many times as this might work by chance.

src/tools/miri/tests/fail/unaligned_pointers/unaligned_ptr2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without validation or Stacked Borrows.
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

44
fn main() {
55
// No retry needed, this fails reliably.

src/tools/miri/tests/fail/unaligned_pointers/unaligned_ptr3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without validation or Stacked Borrows.
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

44
fn main() {
55
// Try many times as this might work by chance.

src/tools/miri/tests/fail/unaligned_pointers/unaligned_ptr4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without validation or Stacked Borrows.
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
2+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
33

44
fn main() {
55
// Make sure we notice when a u16 is loaded at offset 1 into a u8 allocation.

src/tools/miri/tests/fail/unaligned_pointers/unaligned_ptr_zst.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without validation
2-
//@compile-flags: -Zmiri-disable-validation -Cdebug-assertions=no
2+
//@compile-flags: -Zmiri-disable-validation
33

44
fn main() {
55
// Try many times as this might work by chance.

src/tools/miri/tests/fail/unaligned_pointers/unaligned_ref_addr_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This should fail even without Stacked Borrows.
2-
//@compile-flags: -Zmiri-disable-stacked-borrows -Cdebug-assertions=no
2+
//@compile-flags: -Zmiri-disable-stacked-borrows
33

44
#![allow(invalid_reference_casting)] // for u16 -> u32
55

src/tools/miri/tests/pass/disable-alignment-check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@revisions: stack tree
22
//@[tree]compile-flags: -Zmiri-tree-borrows
3-
//@compile-flags: -Zmiri-disable-alignment-check -Cdebug-assertions=no
3+
//@compile-flags: -Zmiri-disable-alignment-check
44

55
fn main() {
66
let mut x = [0u8; 20];

tests/codegen/ub-checks.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// With -Zub-checks=yes (enabled by default by -Cdebug-assertions=yes) we will produce a runtime
2+
// check that the index to slice::get_unchecked is in-bounds of the slice. That is tested for by
3+
// tests/ui/precondition-checks/out-of-bounds-get-unchecked.rs
4+
//
5+
// This test ensures that such a runtime check is *not* emitted when debug-assertions are enabled,
6+
// but ub-checks are explicitly disabled.
7+
8+
//@ compile-flags: -O -Cdebug-assertions=yes -Zub-checks=no
9+
10+
#![crate_type = "lib"]
11+
12+
use std::ops::Range;
13+
14+
// CHECK-LABEL: @slice_get_unchecked(
15+
#[no_mangle]
16+
pub unsafe fn slice_get_unchecked(x: &[i32], i: usize) -> &i32 {
17+
// CHECK-NEXT: start:
18+
// CHECK-NEXT: icmp ult
19+
// CHECK-NEXT: tail call void @llvm.assume
20+
// CHECK-NEXT: getelementptr inbounds
21+
// CHECK-NEXT: ret ptr
22+
x.get_unchecked(i)
23+
}

tests/ui/precondition-checks/misaligned-slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ run-fail
2-
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=yes
2+
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
33
//@ error-pattern: unsafe precondition(s) violated: slice::from_raw_parts
44
//@ ignore-debug
55

tests/ui/precondition-checks/null-slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ run-fail
2-
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=yes
2+
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
33
//@ error-pattern: unsafe precondition(s) violated: slice::from_raw_parts
44
//@ ignore-debug
55

tests/ui/precondition-checks/out-of-bounds-get-unchecked.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ run-fail
2-
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=yes
2+
//@ compile-flags: -Copt-level=3 -Cdebug-assertions=no -Zub-checks=yes
33
//@ error-pattern: slice::get_unchecked requires
44
//@ ignore-debug
55

0 commit comments

Comments
 (0)