From ea060ce1cd721de09867a2057af20087c0060f7a Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 28 Oct 2023 11:05:15 +0200 Subject: [PATCH 1/2] consolidate and extend testing for _ patterns discarding the place --- ...dangling_pointer_deref_match_underscore.rs | 14 ---- tests/pass/underscore_pattern.rs | 76 +++++++++++++++++++ ...score.stdout => underscore_pattern.stdout} | 0 .../union-uninhabited-match-underscore.rs | 17 ----- 4 files changed, 76 insertions(+), 31 deletions(-) delete mode 100644 tests/pass/dangling_pointer_deref_match_underscore.rs create mode 100644 tests/pass/underscore_pattern.rs rename tests/pass/{union-uninhabited-match-underscore.stdout => underscore_pattern.stdout} (100%) delete mode 100644 tests/pass/union-uninhabited-match-underscore.rs diff --git a/tests/pass/dangling_pointer_deref_match_underscore.rs b/tests/pass/dangling_pointer_deref_match_underscore.rs deleted file mode 100644 index c3cff1f428..0000000000 --- a/tests/pass/dangling_pointer_deref_match_underscore.rs +++ /dev/null @@ -1,14 +0,0 @@ -// A `_` binding in a match is a nop, so we do not detect that the pointer is dangling. -//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation - -fn main() { - let p = { - let b = Box::new(42); - &*b as *const i32 - }; - unsafe { - match *p { - _ => {} - } - } -} diff --git a/tests/pass/underscore_pattern.rs b/tests/pass/underscore_pattern.rs new file mode 100644 index 0000000000..f9b42c5bc8 --- /dev/null +++ b/tests/pass/underscore_pattern.rs @@ -0,0 +1,76 @@ +// Various tests ensuring that underscore patterns really just construct the place, but don't check its contents. +#![feature(strict_provenance)] +use std::ptr; + +fn main() { + dangling_deref_match(); + union_uninhabited_match(); + dangling_let(); + invalid_let(); + dangling_let_type_annotation(); + invalid_let_type_annotation(); +} + +fn dangling_deref_match() { + let p = { + let b = Box::new(42); + &*b as *const i32 + }; + unsafe { + match *p { + _ => {} + } + } +} + +fn union_uninhabited_match() { + #[derive(Copy, Clone)] + enum Void {} + union Uninit { + value: T, + uninit: (), + } + unsafe { + let x: Uninit = Uninit { uninit: () }; + match x.value { + // rustc warns about un unreachable pattern, + // but is wrong in unsafe code. + #[allow(unreachable_patterns)] + _ => println!("hi from the void!"), + } + } +} + +fn dangling_let() { + unsafe { + let ptr = ptr::invalid::(0x40); + let _ = *ptr; + } +} + +fn invalid_let() { + unsafe { + let val = 3u8; + let ptr = ptr::addr_of!(val).cast::(); + let _ = *ptr; + } +} + +// Adding a type annotation used to change how MIR is generated, make sure we cover both cases. +fn dangling_let_type_annotation() { + unsafe { + let ptr = ptr::invalid::(0x40); + let _: bool = *ptr; + } +} + +fn invalid_let_type_annotation() { + unsafe { + let val = 3u8; + let ptr = ptr::addr_of!(val).cast::(); + let _: bool = *ptr; + } +} + +// FIXME: we should also test `!`, not just `bool` -- but that s currently buggy: +// https://github.com/rust-lang/rust/issues/117288 diff --git a/tests/pass/union-uninhabited-match-underscore.stdout b/tests/pass/underscore_pattern.stdout similarity index 100% rename from tests/pass/union-uninhabited-match-underscore.stdout rename to tests/pass/underscore_pattern.stdout diff --git a/tests/pass/union-uninhabited-match-underscore.rs b/tests/pass/union-uninhabited-match-underscore.rs deleted file mode 100644 index 33db9c2d34..0000000000 --- a/tests/pass/union-uninhabited-match-underscore.rs +++ /dev/null @@ -1,17 +0,0 @@ -fn main() { - #[derive(Copy, Clone)] - enum Void {} - union Uninit { - value: T, - uninit: (), - } - unsafe { - let x: Uninit = Uninit { uninit: () }; - match x.value { - // rustc warns about un unreachable pattern, - // but is wrong in unsafe code. - #[allow(unreachable_patterns)] - _ => println!("hi from the void!"), - } - } -} From 507fc239ad4fd7d3c31ba045b9b47f18254c658c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 28 Oct 2023 11:18:13 +0200 Subject: [PATCH 2/2] add some tests specifically for validity checks arising from match binders --- .../validity/match_binder_checks_validity1.rs | 15 +++++++++++++++ .../validity/match_binder_checks_validity1.stderr | 15 +++++++++++++++ .../validity/match_binder_checks_validity2.rs | 14 ++++++++++++++ .../validity/match_binder_checks_validity2.stderr | 15 +++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 tests/fail/validity/match_binder_checks_validity1.rs create mode 100644 tests/fail/validity/match_binder_checks_validity1.stderr create mode 100644 tests/fail/validity/match_binder_checks_validity2.rs create mode 100644 tests/fail/validity/match_binder_checks_validity2.stderr diff --git a/tests/fail/validity/match_binder_checks_validity1.rs b/tests/fail/validity/match_binder_checks_validity1.rs new file mode 100644 index 0000000000..6c1df45ac0 --- /dev/null +++ b/tests/fail/validity/match_binder_checks_validity1.rs @@ -0,0 +1,15 @@ +fn main() { + #[derive(Copy, Clone)] + enum Void {} + union Uninit { + value: T, + uninit: (), + } + unsafe { + let x: Uninit = Uninit { uninit: () }; + match x.value { + #[allow(unreachable_patterns)] + _x => println!("hi from the void!"), //~ERROR: invalid value + } + } +} diff --git a/tests/fail/validity/match_binder_checks_validity1.stderr b/tests/fail/validity/match_binder_checks_validity1.stderr new file mode 100644 index 0000000000..c234467bdd --- /dev/null +++ b/tests/fail/validity/match_binder_checks_validity1.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: constructing invalid value: encountered a value of uninhabited type `main::Void` + --> $DIR/match_binder_checks_validity1.rs:LL:CC + | +LL | _x => println!("hi from the void!"), + | ^^ constructing invalid value: encountered a value of uninhabited type `main::Void` + | + = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior + = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information + = note: BACKTRACE: + = note: inside `main` at $DIR/match_binder_checks_validity1.rs:LL:CC + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + diff --git a/tests/fail/validity/match_binder_checks_validity2.rs b/tests/fail/validity/match_binder_checks_validity2.rs new file mode 100644 index 0000000000..0517263a8f --- /dev/null +++ b/tests/fail/validity/match_binder_checks_validity2.rs @@ -0,0 +1,14 @@ +fn main() { + #[derive(Copy, Clone)] + union Uninit { + value: T, + uninit: u8, + } + unsafe { + let x: Uninit = Uninit { uninit: 3 }; + match x.value { + #[allow(unreachable_patterns)] + _x => println!("hi from the void!"), //~ERROR: invalid value + } + } +} diff --git a/tests/fail/validity/match_binder_checks_validity2.stderr b/tests/fail/validity/match_binder_checks_validity2.stderr new file mode 100644 index 0000000000..8af2d37d74 --- /dev/null +++ b/tests/fail/validity/match_binder_checks_validity2.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/match_binder_checks_validity2.rs:LL:CC + | +LL | _x => println!("hi from the void!"), + | ^^ constructing invalid value: encountered 0x03, but expected a boolean + | + = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior + = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information + = note: BACKTRACE: + = note: inside `main` at $DIR/match_binder_checks_validity2.rs:LL:CC + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error +