about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2023-04-15 13:24:28 +0000
committerCamille GILLOT <gillot.camille@gmail.com>2023-10-24 15:30:17 +0000
commit7c669562b19cb451639b2a7329f4c984170d73be (patch)
treeaaee52fdbf719baf190099987fc2639f14b43505
parentec28dc7aa7668b7646de369467386903ac69bafe (diff)
downloadrust-7c669562b19cb451639b2a7329f4c984170d73be.tar.gz
rust-7c669562b19cb451639b2a7329f4c984170d73be.zip
Add miri tests.
-rw-r--r--src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.rs17
-rw-r--r--src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.stderr15
-rw-r--r--src/tools/miri/tests/pass/dangling_pointer_deref_match_underscore.rs14
3 files changed, 46 insertions, 0 deletions
diff --git a/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.rs b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.rs
new file mode 100644
index 00000000000..723c3f1e158
--- /dev/null
+++ b/src/tools/miri/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/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.stderr b/src/tools/miri/tests/fail/dangling_pointers/dangling_pointer_deref_match_never.stderr
new file mode 100644
index 00000000000..2ca6fd028b0
--- /dev/null
+++ b/src/tools/miri/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/src/tools/miri/tests/pass/dangling_pointer_deref_match_underscore.rs b/src/tools/miri/tests/pass/dangling_pointer_deref_match_underscore.rs
new file mode 100644
index 00000000000..c3cff1f4280
--- /dev/null
+++ b/src/tools/miri/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 {
+            _ => {}
+        }
+    }
+}