about summary refs log tree commit diff
path: root/src/tools/miri/tests/fail/function_pointers
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2022-09-21 15:36:26 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2022-09-21 15:36:26 +0000
commitf45b570e08f0f4146e16e426842210f395e8284a (patch)
treef49f598509fda5b0bdbb7b9c0d97f0f2503ffe18 /src/tools/miri/tests/fail/function_pointers
parent3f3167fb59341ac3240ca1774f48e8c053219131 (diff)
parent75dd959a3a40eb5b4574f8d2e23aa6efbeb33573 (diff)
downloadrust-f45b570e08f0f4146e16e426842210f395e8284a.tar.gz
rust-f45b570e08f0f4146e16e426842210f395e8284a.zip
Add 'src/tools/miri/' from commit '75dd959a3a40eb5b4574f8d2e23aa6efbeb33573'
git-subtree-dir: src/tools/miri
git-subtree-mainline: 3f3167fb59341ac3240ca1774f48e8c053219131
git-subtree-split: 75dd959a3a40eb5b4574f8d2e23aa6efbeb33573
Diffstat (limited to 'src/tools/miri/tests/fail/function_pointers')
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_box_int_to_fn_ptr.rs9
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_box_int_to_fn_ptr.stderr15
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.rs7
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.stderr15
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.rs7
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.stderr15
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.rs7
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.stderr15
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.rs7
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.stderr15
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.rs9
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.stderr15
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_int_to_fn_ptr.rs8
-rw-r--r--src/tools/miri/tests/fail/function_pointers/cast_int_to_fn_ptr.stderr15
-rw-r--r--src/tools/miri/tests/fail/function_pointers/deref_fn_ptr.rs8
-rw-r--r--src/tools/miri/tests/fail/function_pointers/deref_fn_ptr.stderr15
-rw-r--r--src/tools/miri/tests/fail/function_pointers/execute_memory.rs12
-rw-r--r--src/tools/miri/tests/fail/function_pointers/execute_memory.stderr15
-rw-r--r--src/tools/miri/tests/fail/function_pointers/fn_ptr_offset.rs14
-rw-r--r--src/tools/miri/tests/fail/function_pointers/fn_ptr_offset.stderr15
20 files changed, 238 insertions, 0 deletions
diff --git a/src/tools/miri/tests/fail/function_pointers/cast_box_int_to_fn_ptr.rs b/src/tools/miri/tests/fail/function_pointers/cast_box_int_to_fn_ptr.rs
new file mode 100644
index 00000000000..9815569b607
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_box_int_to_fn_ptr.rs
@@ -0,0 +1,9 @@
+// Validation makes this fail in the wrong place
+//@compile-flags: -Zmiri-disable-validation
+
+fn main() {
+    let b = Box::new(42);
+    let g = unsafe { std::mem::transmute::<&Box<usize>, &fn(i32)>(&b) };
+
+    (*g)(42) //~ ERROR: it does not point to a function
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/cast_box_int_to_fn_ptr.stderr b/src/tools/miri/tests/fail/function_pointers/cast_box_int_to_fn_ptr.stderr
new file mode 100644
index 00000000000..ad43c2c9d3f
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_box_int_to_fn_ptr.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: using ALLOC as function pointer but it does not point to a function
+  --> $DIR/cast_box_int_to_fn_ptr.rs:LL:CC
+   |
+LL |     (*g)(42)
+   |     ^^^^^^^^ using ALLOC as function pointer but it does not point to a function
+   |
+   = 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/cast_box_int_to_fn_ptr.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/function_pointers/cast_fn_ptr1.rs b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.rs
new file mode 100644
index 00000000000..c0e96a43cc5
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.rs
@@ -0,0 +1,7 @@
+fn main() {
+    fn f() {}
+
+    let g = unsafe { std::mem::transmute::<fn(), fn(i32)>(f) };
+
+    g(42) //~ ERROR: calling a function with more arguments than it expected
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.stderr b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.stderr
new file mode 100644
index 00000000000..bb2a2637959
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr1.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: calling a function with more arguments than it expected
+  --> $DIR/cast_fn_ptr1.rs:LL:CC
+   |
+LL |     g(42)
+   |     ^^^^^ calling a function with more arguments than it expected
+   |
+   = 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/cast_fn_ptr1.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/function_pointers/cast_fn_ptr2.rs b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.rs
new file mode 100644
index 00000000000..20384f0965b
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.rs
@@ -0,0 +1,7 @@
+fn main() {
+    fn f(_: (i32, i32)) {}
+
+    let g = unsafe { std::mem::transmute::<fn((i32, i32)), fn(i32)>(f) };
+
+    g(42) //~ ERROR: calling a function with argument of type (i32, i32) passing data of type i32
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.stderr b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.stderr
new file mode 100644
index 00000000000..086712e0d13
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr2.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: calling a function with argument of type (i32, i32) passing data of type i32
+  --> $DIR/cast_fn_ptr2.rs:LL:CC
+   |
+LL |     g(42)
+   |     ^^^^^ calling a function with argument of type (i32, i32) passing data of type i32
+   |
+   = 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/cast_fn_ptr2.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/function_pointers/cast_fn_ptr3.rs b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.rs
new file mode 100644
index 00000000000..920fb51abb6
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.rs
@@ -0,0 +1,7 @@
+fn main() {
+    fn f(_: (i32, i32)) {}
+
+    let g = unsafe { std::mem::transmute::<fn((i32, i32)), fn()>(f) };
+
+    g() //~ ERROR: calling a function with fewer arguments than it requires
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.stderr b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.stderr
new file mode 100644
index 00000000000..55fd7d60720
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr3.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: calling a function with fewer arguments than it requires
+  --> $DIR/cast_fn_ptr3.rs:LL:CC
+   |
+LL |     g()
+   |     ^^^ calling a function with fewer arguments than it requires
+   |
+   = 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/cast_fn_ptr3.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/function_pointers/cast_fn_ptr4.rs b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.rs
new file mode 100644
index 00000000000..f0ea5ccfe0f
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.rs
@@ -0,0 +1,7 @@
+fn main() {
+    fn f(_: *const [i32]) {}
+
+    let g = unsafe { std::mem::transmute::<fn(*const [i32]), fn(*const i32)>(f) };
+
+    g(&42 as *const i32) //~ ERROR: calling a function with argument of type *const [i32] passing data of type *const i32
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.stderr b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.stderr
new file mode 100644
index 00000000000..610425658fe
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr4.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: calling a function with argument of type *const [i32] passing data of type *const i32
+  --> $DIR/cast_fn_ptr4.rs:LL:CC
+   |
+LL |     g(&42 as *const i32)
+   |     ^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type *const [i32] passing data of type *const i32
+   |
+   = 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/cast_fn_ptr4.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/function_pointers/cast_fn_ptr5.rs b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.rs
new file mode 100644
index 00000000000..0fdab49b94b
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.rs
@@ -0,0 +1,9 @@
+fn main() {
+    fn f() -> u32 {
+        42
+    }
+
+    let g = unsafe { std::mem::transmute::<fn() -> u32, fn()>(f) };
+
+    g() //~ ERROR: calling a function with return type u32 passing return place of type ()
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.stderr b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.stderr
new file mode 100644
index 00000000000..c4e08b58430
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_fn_ptr5.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: calling a function with return type u32 passing return place of type ()
+  --> $DIR/cast_fn_ptr5.rs:LL:CC
+   |
+LL |     g()
+   |     ^^^ calling a function with return type u32 passing return place of type ()
+   |
+   = 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/cast_fn_ptr5.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/function_pointers/cast_int_to_fn_ptr.rs b/src/tools/miri/tests/fail/function_pointers/cast_int_to_fn_ptr.rs
new file mode 100644
index 00000000000..dbf8a560fb7
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_int_to_fn_ptr.rs
@@ -0,0 +1,8 @@
+// Validation makes this fail in the wrong place
+//@compile-flags: -Zmiri-disable-validation
+
+fn main() {
+    let g = unsafe { std::mem::transmute::<usize, fn(i32)>(42) };
+
+    g(42) //~ ERROR: is a dangling pointer
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/cast_int_to_fn_ptr.stderr b/src/tools/miri/tests/fail/function_pointers/cast_int_to_fn_ptr.stderr
new file mode 100644
index 00000000000..81fc9716a41
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/cast_int_to_fn_ptr.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: out-of-bounds pointer use: 0x2a[noalloc] is a dangling pointer (it has no provenance)
+  --> $DIR/cast_int_to_fn_ptr.rs:LL:CC
+   |
+LL |     g(42)
+   |     ^^^^^ out-of-bounds pointer use: 0x2a[noalloc] is a dangling pointer (it has no provenance)
+   |
+   = 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/cast_int_to_fn_ptr.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/function_pointers/deref_fn_ptr.rs b/src/tools/miri/tests/fail/function_pointers/deref_fn_ptr.rs
new file mode 100644
index 00000000000..f071b63902f
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/deref_fn_ptr.rs
@@ -0,0 +1,8 @@
+fn f() {}
+
+fn main() {
+    let x: u8 = unsafe {
+        *std::mem::transmute::<fn(), *const u8>(f) //~ ERROR: out-of-bounds
+    };
+    panic!("this should never print: {}", x);
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/deref_fn_ptr.stderr b/src/tools/miri/tests/fail/function_pointers/deref_fn_ptr.stderr
new file mode 100644
index 00000000000..7ce0b08695e
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/deref_fn_ptr.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: dereferencing pointer failed: ALLOC has size 0, so pointer to 1 byte starting at offset 0 is out-of-bounds
+  --> $DIR/deref_fn_ptr.rs:LL:CC
+   |
+LL |         *std::mem::transmute::<fn(), *const u8>(f)
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: ALLOC has size 0, so pointer to 1 byte starting at offset 0 is out-of-bounds
+   |
+   = 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/deref_fn_ptr.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/function_pointers/execute_memory.rs b/src/tools/miri/tests/fail/function_pointers/execute_memory.rs
new file mode 100644
index 00000000000..967933e769b
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/execute_memory.rs
@@ -0,0 +1,12 @@
+// Validation makes this fail in the wrong place
+//@compile-flags: -Zmiri-disable-validation
+
+#![feature(box_syntax)]
+
+fn main() {
+    let x = box 42;
+    unsafe {
+        let f = std::mem::transmute::<Box<i32>, fn()>(x);
+        f() //~ ERROR: function pointer but it does not point to a function
+    }
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/execute_memory.stderr b/src/tools/miri/tests/fail/function_pointers/execute_memory.stderr
new file mode 100644
index 00000000000..10c53ca2bea
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/execute_memory.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: using ALLOC as function pointer but it does not point to a function
+  --> $DIR/execute_memory.rs:LL:CC
+   |
+LL |         f()
+   |         ^^^ using ALLOC as function pointer but it does not point to a function
+   |
+   = 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/execute_memory.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/function_pointers/fn_ptr_offset.rs b/src/tools/miri/tests/fail/function_pointers/fn_ptr_offset.rs
new file mode 100644
index 00000000000..eba0953ac86
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/fn_ptr_offset.rs
@@ -0,0 +1,14 @@
+// Validation makes this fail in the wrong place
+//@compile-flags: -Zmiri-disable-validation
+
+use std::mem;
+
+fn f() {}
+
+fn main() {
+    let x: fn() = f;
+    let y: *mut u8 = unsafe { mem::transmute(x) };
+    let y = y.wrapping_offset(1);
+    let x: fn() = unsafe { mem::transmute(y) };
+    x(); //~ ERROR: function pointer but it does not point to a function
+}
diff --git a/src/tools/miri/tests/fail/function_pointers/fn_ptr_offset.stderr b/src/tools/miri/tests/fail/function_pointers/fn_ptr_offset.stderr
new file mode 100644
index 00000000000..f8c519c1b54
--- /dev/null
+++ b/src/tools/miri/tests/fail/function_pointers/fn_ptr_offset.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: using ALLOC+0x1 as function pointer but it does not point to a function
+  --> $DIR/fn_ptr_offset.rs:LL:CC
+   |
+LL |     x();
+   |     ^^^ using ALLOC+0x1 as function pointer but it does not point to a function
+   |
+   = 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/fn_ptr_offset.rs:LL:CC
+
+note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
+
+error: aborting due to previous error
+