From 98eb384eab9cf12d39b76689b89264b4e929f587 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 30 Oct 2023 08:23:37 +0100 Subject: make sure we catch UB with _ pattern in various syntactic positions --- .../dangling_pointer_project_underscore.rs | 12 ----------- .../dangling_pointer_project_underscore.stderr | 25 ---------------------- .../dangling_pointer_project_underscore_let.rs | 12 +++++++++++ .../dangling_pointer_project_underscore_let.stderr | 25 ++++++++++++++++++++++ ...inter_project_underscore_let_type_annotation.rs | 12 +++++++++++ ...r_project_underscore_let_type_annotation.stderr | 25 ++++++++++++++++++++++ .../dangling_pointer_project_underscore_match.rs | 15 +++++++++++++ ...angling_pointer_project_underscore_match.stderr | 25 ++++++++++++++++++++++ src/tools/miri/tests/pass/underscore_pattern.rs | 17 ++++++--------- .../miri/tests/pass/underscore_pattern.stdout | 1 - 10 files changed, 120 insertions(+), 49 deletions(-) delete mode 100644 src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore.rs delete mode 100644 src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore.stderr create mode 100644 src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let.rs create mode 100644 src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let.stderr create mode 100644 src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let_type_annotation.rs create mode 100644 src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let_type_annotation.stderr create mode 100644 src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_match.rs create mode 100644 src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_match.stderr delete mode 100644 src/tools/miri/tests/pass/underscore_pattern.stdout (limited to 'src') diff --git a/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore.rs b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore.rs deleted file mode 100644 index 22a5ce8ea74..00000000000 --- a/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore.rs +++ /dev/null @@ -1,12 +0,0 @@ -// Make sure we find these even with many checks disabled. -//@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 as *const (u8, u8, u8, u8) - }; - unsafe { - let _ = (*p).1; //~ ERROR: out-of-bounds pointer arithmetic - } -} diff --git a/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore.stderr b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore.stderr deleted file mode 100644 index 20f3a25a0b1..00000000000 --- a/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore.stderr +++ /dev/null @@ -1,25 +0,0 @@ -error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling - --> $DIR/dangling_pointer_project_underscore.rs:LL:CC - | -LL | let _ = (*p).1; - | ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling - | - = 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 -help: ALLOC was allocated here: - --> $DIR/dangling_pointer_project_underscore.rs:LL:CC - | -LL | let b = Box::new(42); - | ^^^^^^^^^^^^ -help: ALLOC was deallocated here: - --> $DIR/dangling_pointer_project_underscore.rs:LL:CC - | -LL | }; - | ^ - = note: BACKTRACE (of the first span): - = note: inside `main` at $DIR/dangling_pointer_project_underscore.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/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let.rs b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let.rs new file mode 100644 index 00000000000..22a5ce8ea74 --- /dev/null +++ b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let.rs @@ -0,0 +1,12 @@ +// Make sure we find these even with many checks disabled. +//@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 as *const (u8, u8, u8, u8) + }; + unsafe { + let _ = (*p).1; //~ ERROR: out-of-bounds pointer arithmetic + } +} diff --git a/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let.stderr b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let.stderr new file mode 100644 index 00000000000..16841626dc2 --- /dev/null +++ b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let.stderr @@ -0,0 +1,25 @@ +error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling + --> $DIR/dangling_pointer_project_underscore_let.rs:LL:CC + | +LL | let _ = (*p).1; + | ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling + | + = 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 +help: ALLOC was allocated here: + --> $DIR/dangling_pointer_project_underscore_let.rs:LL:CC + | +LL | let b = Box::new(42); + | ^^^^^^^^^^^^ +help: ALLOC was deallocated here: + --> $DIR/dangling_pointer_project_underscore_let.rs:LL:CC + | +LL | }; + | ^ + = note: BACKTRACE (of the first span): + = note: inside `main` at $DIR/dangling_pointer_project_underscore_let.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/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let_type_annotation.rs b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let_type_annotation.rs new file mode 100644 index 00000000000..fc10a826c1e --- /dev/null +++ b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let_type_annotation.rs @@ -0,0 +1,12 @@ +// Make sure we find these even with many checks disabled. +//@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 as *const (u8, u8, u8, u8) + }; + unsafe { + let _: u8 = (*p).1; //~ ERROR: out-of-bounds pointer arithmetic + } +} diff --git a/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let_type_annotation.stderr b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let_type_annotation.stderr new file mode 100644 index 00000000000..0cdb6639a2f --- /dev/null +++ b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_let_type_annotation.stderr @@ -0,0 +1,25 @@ +error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling + --> $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC + | +LL | let _: u8 = (*p).1; + | ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling + | + = 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 +help: ALLOC was allocated here: + --> $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC + | +LL | let b = Box::new(42); + | ^^^^^^^^^^^^ +help: ALLOC was deallocated here: + --> $DIR/dangling_pointer_project_underscore_let_type_annotation.rs:LL:CC + | +LL | }; + | ^ + = note: BACKTRACE (of the first span): + = note: inside `main` at $DIR/dangling_pointer_project_underscore_let_type_annotation.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/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_match.rs b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_match.rs new file mode 100644 index 00000000000..8541da84857 --- /dev/null +++ b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_match.rs @@ -0,0 +1,15 @@ +// Make sure we find these even with many checks disabled. +//@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 as *const (u8, u8, u8, u8) + }; + unsafe { + match (*p).1 { + //~^ ERROR: out-of-bounds pointer arithmetic + _ => {} + } + } +} diff --git a/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_match.stderr b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_match.stderr new file mode 100644 index 00000000000..625a7b5f60b --- /dev/null +++ b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_project_underscore_match.stderr @@ -0,0 +1,25 @@ +error: Undefined Behavior: out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling + --> $DIR/dangling_pointer_project_underscore_match.rs:LL:CC + | +LL | match (*p).1 { + | ^^^^^^ out-of-bounds pointer arithmetic: ALLOC has been freed, so this pointer is dangling + | + = 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 +help: ALLOC was allocated here: + --> $DIR/dangling_pointer_project_underscore_match.rs:LL:CC + | +LL | let b = Box::new(42); + | ^^^^^^^^^^^^ +help: ALLOC was deallocated here: + --> $DIR/dangling_pointer_project_underscore_match.rs:LL:CC + | +LL | }; + | ^ + = note: BACKTRACE (of the first span): + = note: inside `main` at $DIR/dangling_pointer_project_underscore_match.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/src/tools/miri/tests/pass/underscore_pattern.rs b/src/tools/miri/tests/pass/underscore_pattern.rs index f9b42c5bc8e..b0e85bc1bb0 100644 --- a/src/tools/miri/tests/pass/underscore_pattern.rs +++ b/src/tools/miri/tests/pass/underscore_pattern.rs @@ -3,15 +3,15 @@ use std::ptr; fn main() { - dangling_deref_match(); - union_uninhabited_match(); + dangling_match(); + invalid_match(); dangling_let(); invalid_let(); dangling_let_type_annotation(); invalid_let_type_annotation(); } -fn dangling_deref_match() { +fn dangling_match() { let p = { let b = Box::new(42); &*b as *const i32 @@ -23,20 +23,15 @@ fn dangling_deref_match() { } } -fn union_uninhabited_match() { - #[derive(Copy, Clone)] - enum Void {} +fn invalid_match() { union Uninit { value: T, uninit: (), } unsafe { - let x: Uninit = Uninit { uninit: () }; + 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/src/tools/miri/tests/pass/underscore_pattern.stdout b/src/tools/miri/tests/pass/underscore_pattern.stdout deleted file mode 100644 index ff731696f01..00000000000 --- a/src/tools/miri/tests/pass/underscore_pattern.stdout +++ /dev/null @@ -1 +0,0 @@ -hi from the void! -- cgit 1.4.1-3-g733a5