Skip to content

Commit 7e4aaec

Browse files
committed
Add a stable flag to control codegen UB checks
1 parent 5d5edf0 commit 7e4aaec

19 files changed

+31
-16
lines changed

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ fn test_codegen_options_tracking_hash() {
609609
tracked!(debug_assertions, Some(true));
610610
tracked!(debuginfo, DebugInfo::Limited);
611611
tracked!(embed_bitcode, false);
612+
tracked!(extra_ub_checks, Some(true));
612613
tracked!(force_frame_pointers, Some(false));
613614
tracked!(force_unwind_tables, Some(true));
614615
tracked!(inline_threshold, Some(0xf007ba11));

compiler/rustc_mir_transform/src/check_alignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'tcx> MirPass<'tcx> for CheckAlignment {
1818
if sess.target.llvm_target == "i686-pc-windows-msvc" {
1919
return false;
2020
}
21-
sess.opts.debug_assertions
21+
sess.extra_ub_checks()
2222
}
2323

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

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,8 @@ options! {
13161316
"emit bitcode in rlibs (default: yes)"),
13171317
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
13181318
"extra data to put in each output filename"),
1319+
extra_ub_checks: Option<bool> = (None, parse_opt_bool, [TRACKED],
1320+
"insert extra runtime checks in codegen that catch Undefined Behavior"),
13191321
force_frame_pointers: Option<bool> = (None, parse_opt_bool, [TRACKED],
13201322
"force use of the frame pointers"),
13211323
#[rustc_lint_opt_deny_field_access("use `Session::must_emit_unwind_tables` instead of this field")]

compiler/rustc_session/src/session.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,10 @@ impl Session {
11041104
self.opts.cg.overflow_checks.unwrap_or(self.opts.debug_assertions)
11051105
}
11061106

1107+
pub fn extra_ub_checks(&self) -> bool {
1108+
self.opts.cg.extra_ub_checks.unwrap_or(self.opts.debug_assertions)
1109+
}
1110+
11071111
pub fn relocation_model(&self) -> RelocModel {
11081112
self.opts.cg.relocation_model.unwrap_or(self.target.relocation_model)
11091113
}

src/doc/rustc/src/codegen-options/index.md

+11
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ This option allows you to put extra data in each output filename. It takes a
140140
string to add as a suffix to the filename. See the [`--emit`
141141
flag][option-emit] for more information.
142142

143+
## extra-ub-checks
144+
145+
This flag controls whether the compiler inserts runtime checks during code generation
146+
to catch Undefined Behavior.
147+
148+
* `y`, `yes`, `on`, `true`, or no value: insert such checks regardless of debug-assertions.
149+
* `n`, `no`, `off`, `false`: do not emit such checks regardless of debug-assertions.
150+
151+
If not specified, extra UB checks are enabled if
152+
[debug-assertions](#debug-assertions) are enabled, disabled otherwise.
153+
143154
## force-frame-pointers
144155

145156
This flag forces the use of frame pointers. It takes one of the following

src/tools/miri/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,5 @@ pub const MIRI_DEFAULT_ARGS: &[&str] = &[
138138
"-Zmir-emit-retag",
139139
"-Zmir-keep-place-mention",
140140
"-Zmir-opt-level=0",
141-
"-Zmir-enable-passes=-CheckAlignment",
141+
"-Cextra-ub-checks=false",
142142
];

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
fn main() {
55
// Try many times as this might work by chance.

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];

0 commit comments

Comments
 (0)