From f8a484e81cff505dd29c7054ee72e2cd0b0e6c97 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 15 Apr 2023 13:24:28 +0000 Subject: [PATCH 1/3] Add miri tests. --- .../dangling_pointer_deref_match_never.rs | 17 +++++++++++++++++ .../dangling_pointer_deref_match_never.stderr | 15 +++++++++++++++ .../dangling_pointer_deref_match_underscore.rs | 14 ++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 tests/fail/dangling_pointers/dangling_pointer_deref_match_never.rs create mode 100644 tests/fail/dangling_pointers/dangling_pointer_deref_match_never.stderr create mode 100644 tests/pass/dangling_pointer_deref_match_underscore.rs diff --git a/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.rs b/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.rs new file mode 100644 index 0000000000..723c3f1e15 --- /dev/null +++ b/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.rs @@ -0,0 +1,17 @@ +// Make sure we find these even with many checks disabled. +//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation + +#![allow(unreachable_code)] +#![feature(never_type)] + +fn main() { + let p = { + let b = Box::new(42); + &*b as *const i32 as *const ! + }; + unsafe { + match *p {} //~ ERROR: entering unreachable code + } + panic!("this should never print"); +} + diff --git a/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.stderr b/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.stderr new file mode 100644 index 0000000000..2ca6fd028b --- /dev/null +++ b/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: entering unreachable code + --> $DIR/dangling_pointer_deref_match_never.rs:LL:CC + | +LL | match *p {} + | ^^ entering unreachable code + | + = 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/dangling_pointer_deref_match_never.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/pass/dangling_pointer_deref_match_underscore.rs b/tests/pass/dangling_pointer_deref_match_underscore.rs new file mode 100644 index 0000000000..c3cff1f428 --- /dev/null +++ b/tests/pass/dangling_pointer_deref_match_underscore.rs @@ -0,0 +1,14 @@ +// 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 { + _ => {} + } + } +} From 2e6e707c3f0f58f9c988f53563b360700232df8c Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Sep 2023 09:38:04 +0000 Subject: [PATCH 2/3] Add miri test matching on `!`. --- tests/fail/never_match_never.rs | 10 ++++++++++ tests/fail/never_match_never.stderr | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/fail/never_match_never.rs create mode 100644 tests/fail/never_match_never.stderr diff --git a/tests/fail/never_match_never.rs b/tests/fail/never_match_never.rs new file mode 100644 index 0000000000..5f2f471bf6 --- /dev/null +++ b/tests/fail/never_match_never.rs @@ -0,0 +1,10 @@ +// This should fail even without validation +//@compile-flags: -Zmiri-disable-validation + +#![feature(never_type)] +#![allow(unreachable_code)] + +fn main() { + let ptr: *const (i32, !) = &0i32 as *const i32 as *const _; + unsafe { match (*ptr).1 {} } //~ ERROR: entering unreachable code +} diff --git a/tests/fail/never_match_never.stderr b/tests/fail/never_match_never.stderr new file mode 100644 index 0000000000..33dab81d5b --- /dev/null +++ b/tests/fail/never_match_never.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: entering unreachable code + --> $DIR/never_match_never.rs:LL:CC + | +LL | unsafe { match (*ptr).1 {} } + | ^^^^^^^^ entering unreachable code + | + = 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/never_match_never.rs:LL:CC + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + From 479374f241a2fa7a44910fa0a92c0ac4c73a14c3 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 24 Oct 2023 16:22:32 +0000 Subject: [PATCH 3/3] Test match underscore on void from union. --- .../pass/union-uninhabited-match-underscore.rs | 17 +++++++++++++++++ .../union-uninhabited-match-underscore.stdout | 1 + 2 files changed, 18 insertions(+) create mode 100644 tests/pass/union-uninhabited-match-underscore.rs create mode 100644 tests/pass/union-uninhabited-match-underscore.stdout diff --git a/tests/pass/union-uninhabited-match-underscore.rs b/tests/pass/union-uninhabited-match-underscore.rs new file mode 100644 index 0000000000..33db9c2d34 --- /dev/null +++ b/tests/pass/union-uninhabited-match-underscore.rs @@ -0,0 +1,17 @@ +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!"), + } + } +} diff --git a/tests/pass/union-uninhabited-match-underscore.stdout b/tests/pass/union-uninhabited-match-underscore.stdout new file mode 100644 index 0000000000..ff731696f0 --- /dev/null +++ b/tests/pass/union-uninhabited-match-underscore.stdout @@ -0,0 +1 @@ +hi from the void!