]> git.lizzy.rs Git - rust.git/commitdiff
move some tests to more suitable locations
authorRalf Jung <post@ralfj.de>
Mon, 23 May 2022 10:19:42 +0000 (12:19 +0200)
committerRalf Jung <post@ralfj.de>
Mon, 23 May 2022 10:19:42 +0000 (12:19 +0200)
22 files changed:
tests/compile-fail/dangling_pointers/null_pointer_deref.rs [new file with mode: 0644]
tests/compile-fail/dangling_pointers/null_pointer_deref_zst.rs [new file with mode: 0644]
tests/compile-fail/dangling_pointers/null_pointer_write.rs [new file with mode: 0644]
tests/compile-fail/dangling_pointers/null_pointer_write_zst.rs [new file with mode: 0644]
tests/compile-fail/null_pointer_deref.rs [deleted file]
tests/compile-fail/null_pointer_deref_zst.rs [deleted file]
tests/compile-fail/null_pointer_write.rs [deleted file]
tests/compile-fail/null_pointer_write_zst.rs [deleted file]
tests/compile-fail/provenance/ptr_int_unexposed.rs [new file with mode: 0644]
tests/compile-fail/provenance/ptr_legacy_provenance.rs [new file with mode: 0644]
tests/compile-fail/provenance/strict-provenance-offset.rs [new file with mode: 0644]
tests/compile-fail/provenance/strict_provenance_transmute.rs [new file with mode: 0644]
tests/compile-fail/ptr_int_unexposed.rs [deleted file]
tests/compile-fail/ptr_integer_array_transmute.rs [deleted file]
tests/compile-fail/ptr_legacy_provenance.rs [deleted file]
tests/compile-fail/strict-provenance-offset.rs [deleted file]
tests/compile-fail/strict_provenance_transmute.rs [deleted file]
tests/compile-fail/too-big-slice.rs [deleted file]
tests/compile-fail/too-big-unsized.rs [deleted file]
tests/compile-fail/validity/ptr_integer_array_transmute.rs [new file with mode: 0644]
tests/compile-fail/validity/too-big-slice.rs [new file with mode: 0644]
tests/compile-fail/validity/too-big-unsized.rs [new file with mode: 0644]

diff --git a/tests/compile-fail/dangling_pointers/null_pointer_deref.rs b/tests/compile-fail/dangling_pointers/null_pointer_deref.rs
new file mode 100644 (file)
index 0000000..92c45b1
--- /dev/null
@@ -0,0 +1,5 @@
+#[allow(deref_nullptr)]
+fn main() {
+    let x: i32 = unsafe { *std::ptr::null() }; //~ ERROR null pointer is not a valid pointer
+    panic!("this should never print: {}", x);
+}
diff --git a/tests/compile-fail/dangling_pointers/null_pointer_deref_zst.rs b/tests/compile-fail/dangling_pointers/null_pointer_deref_zst.rs
new file mode 100644 (file)
index 0000000..f3830c0
--- /dev/null
@@ -0,0 +1,8 @@
+// Some optimizations remove ZST accesses, thus masking this UB.
+// compile-flags: -Zmir-opt-level=0
+
+#[allow(deref_nullptr)]
+fn main() {
+    let x: () = unsafe { *std::ptr::null() }; //~ ERROR dereferencing pointer failed: null pointer is not a valid pointer
+    panic!("this should never print: {:?}", x);
+}
diff --git a/tests/compile-fail/dangling_pointers/null_pointer_write.rs b/tests/compile-fail/dangling_pointers/null_pointer_write.rs
new file mode 100644 (file)
index 0000000..f8dca68
--- /dev/null
@@ -0,0 +1,4 @@
+#[allow(deref_nullptr)]
+fn main() {
+    unsafe { *std::ptr::null_mut() = 0i32 }; //~ ERROR null pointer is not a valid pointer
+}
diff --git a/tests/compile-fail/dangling_pointers/null_pointer_write_zst.rs b/tests/compile-fail/dangling_pointers/null_pointer_write_zst.rs
new file mode 100644 (file)
index 0000000..63474d9
--- /dev/null
@@ -0,0 +1,11 @@
+// Some optimizations remove ZST accesses, thus masking this UB.
+// compile-flags: -Zmir-opt-level=0
+// error-pattern: memory access failed: null pointer is not a valid pointer
+
+#[allow(deref_nullptr)]
+fn main() {
+    // Not using the () type here, as writes of that type do not even have MIR generated.
+    // Also not assigning directly as that's array initialization, not assignment.
+    let zst_val = [1u8; 0];
+    unsafe { std::ptr::null_mut::<[u8; 0]>().write(zst_val) };
+}
diff --git a/tests/compile-fail/null_pointer_deref.rs b/tests/compile-fail/null_pointer_deref.rs
deleted file mode 100644 (file)
index 92c45b1..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-#[allow(deref_nullptr)]
-fn main() {
-    let x: i32 = unsafe { *std::ptr::null() }; //~ ERROR null pointer is not a valid pointer
-    panic!("this should never print: {}", x);
-}
diff --git a/tests/compile-fail/null_pointer_deref_zst.rs b/tests/compile-fail/null_pointer_deref_zst.rs
deleted file mode 100644 (file)
index f3830c0..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-// Some optimizations remove ZST accesses, thus masking this UB.
-// compile-flags: -Zmir-opt-level=0
-
-#[allow(deref_nullptr)]
-fn main() {
-    let x: () = unsafe { *std::ptr::null() }; //~ ERROR dereferencing pointer failed: null pointer is not a valid pointer
-    panic!("this should never print: {:?}", x);
-}
diff --git a/tests/compile-fail/null_pointer_write.rs b/tests/compile-fail/null_pointer_write.rs
deleted file mode 100644 (file)
index f8dca68..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-#[allow(deref_nullptr)]
-fn main() {
-    unsafe { *std::ptr::null_mut() = 0i32 }; //~ ERROR null pointer is not a valid pointer
-}
diff --git a/tests/compile-fail/null_pointer_write_zst.rs b/tests/compile-fail/null_pointer_write_zst.rs
deleted file mode 100644 (file)
index 63474d9..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-// Some optimizations remove ZST accesses, thus masking this UB.
-// compile-flags: -Zmir-opt-level=0
-// error-pattern: memory access failed: null pointer is not a valid pointer
-
-#[allow(deref_nullptr)]
-fn main() {
-    // Not using the () type here, as writes of that type do not even have MIR generated.
-    // Also not assigning directly as that's array initialization, not assignment.
-    let zst_val = [1u8; 0];
-    unsafe { std::ptr::null_mut::<[u8; 0]>().write(zst_val) };
-}
diff --git a/tests/compile-fail/provenance/ptr_int_unexposed.rs b/tests/compile-fail/provenance/ptr_int_unexposed.rs
new file mode 100644 (file)
index 0000000..2aecb68
--- /dev/null
@@ -0,0 +1,12 @@
+// compile-flags: -Zmiri-permissive-provenance -Zmiri-disable-stacked-borrows
+
+fn main() {
+    let x: i32 = 3;
+    let x_ptr = &x as *const i32;
+
+    // TODO: switch this to addr() once we intrinsify it
+    let x_usize: usize = unsafe { std::mem::transmute(x_ptr) };
+    // Cast back a pointer that did *not* get exposed.
+    let ptr = x_usize as *const i32;
+    assert_eq!(unsafe { *ptr }, 3); //~ ERROR Undefined Behavior: dereferencing pointer failed
+}
diff --git a/tests/compile-fail/provenance/ptr_legacy_provenance.rs b/tests/compile-fail/provenance/ptr_legacy_provenance.rs
new file mode 100644 (file)
index 0000000..aecc146
--- /dev/null
@@ -0,0 +1,21 @@
+// compile-flags: -Zmiri-disable-stacked-borrows
+#![feature(strict_provenance)]
+
+use std::ptr;
+
+// Make sure that with legacy provenance, the allocation id of
+// a casted pointer is determined at cast-time
+fn main() {
+    let x: i32 = 0;
+    let y: i32 = 1;
+
+    let x_ptr = &x as *const i32;
+    let y_ptr = &y as *const i32;
+
+    let x_usize = x_ptr.expose_addr();
+    let y_usize = y_ptr.expose_addr();
+
+    let ptr = ptr::from_exposed_addr::<i32>(y_usize);
+    let ptr = ptr.with_addr(x_usize);
+    assert_eq!(unsafe { *ptr }, 0); //~ ERROR is out-of-bounds
+}
diff --git a/tests/compile-fail/provenance/strict-provenance-offset.rs b/tests/compile-fail/provenance/strict-provenance-offset.rs
new file mode 100644 (file)
index 0000000..6955d02
--- /dev/null
@@ -0,0 +1,9 @@
+// compile-flags: -Zmiri-strict-provenance
+// error-pattern: not a valid pointer
+
+fn main() {
+    let x = 22;
+    let ptr = &x as *const _ as *const u8;
+    let roundtrip = ptr as usize as *const u8;
+    let _ = unsafe { roundtrip.offset(1) };
+}
diff --git a/tests/compile-fail/provenance/strict_provenance_transmute.rs b/tests/compile-fail/provenance/strict_provenance_transmute.rs
new file mode 100644 (file)
index 0000000..a684d65
--- /dev/null
@@ -0,0 +1,27 @@
+// compile-flags: -Zmiri-strict-provenance
+#![feature(strict_provenance)]
+
+use std::mem;
+
+// This is the example from
+// <https://github.com/rust-lang/unsafe-code-guidelines/issues/286#issuecomment-1085144431>.
+
+unsafe fn deref(left: *const u8, right: *const u8) {
+    let left_int: usize = mem::transmute(left); //~ERROR expected plain (non-pointer) bytes
+    let right_int: usize = mem::transmute(right);
+    if left_int == right_int {
+        // The compiler is allowed to replace `left_int` by `right_int` here...
+        let left_ptr: *const u8 = mem::transmute(left_int);
+        // ...which however means here it could be dereferencing the wrong pointer.
+        let _val = *left_ptr;
+    }
+}
+
+fn main() {
+    let ptr1 = &0u8 as *const u8;
+    let ptr2 = &1u8 as *const u8;
+    unsafe {
+        // Two pointers with the same address but different provenance.
+        deref(ptr1, ptr2.with_addr(ptr1.addr()));
+    }
+}
diff --git a/tests/compile-fail/ptr_int_unexposed.rs b/tests/compile-fail/ptr_int_unexposed.rs
deleted file mode 100644 (file)
index 2aecb68..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-// compile-flags: -Zmiri-permissive-provenance -Zmiri-disable-stacked-borrows
-
-fn main() {
-    let x: i32 = 3;
-    let x_ptr = &x as *const i32;
-
-    // TODO: switch this to addr() once we intrinsify it
-    let x_usize: usize = unsafe { std::mem::transmute(x_ptr) };
-    // Cast back a pointer that did *not* get exposed.
-    let ptr = x_usize as *const i32;
-    assert_eq!(unsafe { *ptr }, 3); //~ ERROR Undefined Behavior: dereferencing pointer failed
-}
diff --git a/tests/compile-fail/ptr_integer_array_transmute.rs b/tests/compile-fail/ptr_integer_array_transmute.rs
deleted file mode 100644 (file)
index 7a1ae2f..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-// compile-flags: -Zmiri-check-number-validity
-
-fn main() {
-    let r = &mut 42;
-    let _i: [usize; 1] = unsafe { std::mem::transmute(r) }; //~ ERROR encountered a pointer, but expected plain (non-pointer) bytes
-}
diff --git a/tests/compile-fail/ptr_legacy_provenance.rs b/tests/compile-fail/ptr_legacy_provenance.rs
deleted file mode 100644 (file)
index aecc146..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-// compile-flags: -Zmiri-disable-stacked-borrows
-#![feature(strict_provenance)]
-
-use std::ptr;
-
-// Make sure that with legacy provenance, the allocation id of
-// a casted pointer is determined at cast-time
-fn main() {
-    let x: i32 = 0;
-    let y: i32 = 1;
-
-    let x_ptr = &x as *const i32;
-    let y_ptr = &y as *const i32;
-
-    let x_usize = x_ptr.expose_addr();
-    let y_usize = y_ptr.expose_addr();
-
-    let ptr = ptr::from_exposed_addr::<i32>(y_usize);
-    let ptr = ptr.with_addr(x_usize);
-    assert_eq!(unsafe { *ptr }, 0); //~ ERROR is out-of-bounds
-}
diff --git a/tests/compile-fail/strict-provenance-offset.rs b/tests/compile-fail/strict-provenance-offset.rs
deleted file mode 100644 (file)
index 6955d02..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-// compile-flags: -Zmiri-strict-provenance
-// error-pattern: not a valid pointer
-
-fn main() {
-    let x = 22;
-    let ptr = &x as *const _ as *const u8;
-    let roundtrip = ptr as usize as *const u8;
-    let _ = unsafe { roundtrip.offset(1) };
-}
diff --git a/tests/compile-fail/strict_provenance_transmute.rs b/tests/compile-fail/strict_provenance_transmute.rs
deleted file mode 100644 (file)
index a684d65..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-// compile-flags: -Zmiri-strict-provenance
-#![feature(strict_provenance)]
-
-use std::mem;
-
-// This is the example from
-// <https://github.com/rust-lang/unsafe-code-guidelines/issues/286#issuecomment-1085144431>.
-
-unsafe fn deref(left: *const u8, right: *const u8) {
-    let left_int: usize = mem::transmute(left); //~ERROR expected plain (non-pointer) bytes
-    let right_int: usize = mem::transmute(right);
-    if left_int == right_int {
-        // The compiler is allowed to replace `left_int` by `right_int` here...
-        let left_ptr: *const u8 = mem::transmute(left_int);
-        // ...which however means here it could be dereferencing the wrong pointer.
-        let _val = *left_ptr;
-    }
-}
-
-fn main() {
-    let ptr1 = &0u8 as *const u8;
-    let ptr2 = &1u8 as *const u8;
-    unsafe {
-        // Two pointers with the same address but different provenance.
-        deref(ptr1, ptr2.with_addr(ptr1.addr()));
-    }
-}
diff --git a/tests/compile-fail/too-big-slice.rs b/tests/compile-fail/too-big-slice.rs
deleted file mode 100644 (file)
index 6a4c182..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-use std::mem;
-
-fn main() { unsafe {
-    let ptr = Box::into_raw(Box::new(0u8));
-    let _x: &[u8] = mem::transmute((ptr, usize::MAX)); //~ ERROR: invalid reference metadata: slice is bigger than largest supported object
-} }
diff --git a/tests/compile-fail/too-big-unsized.rs b/tests/compile-fail/too-big-unsized.rs
deleted file mode 100644 (file)
index 824190a..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-use std::mem;
-
-#[allow(unused)]
-struct MySlice {
-    prefix: u64,
-    tail: [u8],
-}
-
-fn main() { unsafe {
-    let ptr = Box::into_raw(Box::new(0u8));
-    // The slice part is actually not "too big", but together with the `prefix` field it is.
-    let _x: &MySlice = mem::transmute((ptr, isize::MAX as usize)); //~ ERROR: invalid reference metadata: total size is bigger than largest supported object
-} }
diff --git a/tests/compile-fail/validity/ptr_integer_array_transmute.rs b/tests/compile-fail/validity/ptr_integer_array_transmute.rs
new file mode 100644 (file)
index 0000000..7a1ae2f
--- /dev/null
@@ -0,0 +1,6 @@
+// compile-flags: -Zmiri-check-number-validity
+
+fn main() {
+    let r = &mut 42;
+    let _i: [usize; 1] = unsafe { std::mem::transmute(r) }; //~ ERROR encountered a pointer, but expected plain (non-pointer) bytes
+}
diff --git a/tests/compile-fail/validity/too-big-slice.rs b/tests/compile-fail/validity/too-big-slice.rs
new file mode 100644 (file)
index 0000000..6a4c182
--- /dev/null
@@ -0,0 +1,6 @@
+use std::mem;
+
+fn main() { unsafe {
+    let ptr = Box::into_raw(Box::new(0u8));
+    let _x: &[u8] = mem::transmute((ptr, usize::MAX)); //~ ERROR: invalid reference metadata: slice is bigger than largest supported object
+} }
diff --git a/tests/compile-fail/validity/too-big-unsized.rs b/tests/compile-fail/validity/too-big-unsized.rs
new file mode 100644 (file)
index 0000000..824190a
--- /dev/null
@@ -0,0 +1,13 @@
+use std::mem;
+
+#[allow(unused)]
+struct MySlice {
+    prefix: u64,
+    tail: [u8],
+}
+
+fn main() { unsafe {
+    let ptr = Box::into_raw(Box::new(0u8));
+    // The slice part is actually not "too big", but together with the `prefix` field it is.
+    let _x: &MySlice = mem::transmute((ptr, isize::MAX as usize)); //~ ERROR: invalid reference metadata: total size is bigger than largest supported object
+} }